Coverage Report

Created: 2026-11-20 08:13

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/qemu/include/block/block_int-common.h
Line
Count
Source
1
/*
2
 * QEMU System Emulator block driver
3
 *
4
 * Copyright (c) 2003 Fabrice Bellard
5
 *
6
 * Permission is hereby granted, free of charge, to any person obtaining a copy
7
 * of this software and associated documentation files (the "Software"), to deal
8
 * in the Software without restriction, including without limitation the rights
9
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
 * copies of the Software, and to permit persons to whom the Software is
11
 * furnished to do so, subject to the following conditions:
12
 *
13
 * The above copyright notice and this permission notice shall be included in
14
 * all copies or substantial portions of the Software.
15
 *
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
 * THE SOFTWARE.
23
 */
24
#ifndef BLOCK_INT_COMMON_H
25
#define BLOCK_INT_COMMON_H
26
27
#include "block/block-common.h"
28
#include "block/block-global-state.h"
29
#include "block/snapshot.h"
30
#include "qemu/aiocb.h"
31
#include "qemu/iov.h"
32
#include "qemu/rcu.h"
33
34
#define BLOCK_FLAG_LAZY_REFCOUNTS   8
35
36
16
#define BLOCK_OPT_SIZE              "size"
37
0
#define BLOCK_OPT_ENCRYPT           "encryption"
38
0
#define BLOCK_OPT_ENCRYPT_FORMAT    "encrypt.format"
39
0
#define BLOCK_OPT_COMPAT6           "compat6"
40
0
#define BLOCK_OPT_HWVERSION         "hwversion"
41
0
#define BLOCK_OPT_BACKING_FILE      "backing_file"
42
0
#define BLOCK_OPT_BACKING_FMT       "backing_fmt"
43
0
#define BLOCK_OPT_CLUSTER_SIZE      "cluster_size"
44
0
#define BLOCK_OPT_TABLE_SIZE        "table_size"
45
0
#define BLOCK_OPT_PREALLOC          "preallocation"
46
0
#define BLOCK_OPT_SUBFMT            "subformat"
47
0
#define BLOCK_OPT_COMPAT_LEVEL      "compat"
48
0
#define BLOCK_OPT_LAZY_REFCOUNTS    "lazy_refcounts"
49
0
#define BLOCK_OPT_ADAPTER_TYPE      "adapter_type"
50
#define BLOCK_OPT_REDUNDANCY        "redundancy"
51
0
#define BLOCK_OPT_NOCOW             "nocow"
52
0
#define BLOCK_OPT_EXTENT_SIZE_HINT  "extent_size_hint"
53
#define BLOCK_OPT_OBJECT_SIZE       "object_size"
54
0
#define BLOCK_OPT_REFCOUNT_BITS     "refcount_bits"
55
0
#define BLOCK_OPT_DATA_FILE         "data_file"
56
0
#define BLOCK_OPT_DATA_FILE_RAW     "data_file_raw"
57
0
#define BLOCK_OPT_COMPRESSION_TYPE  "compression_type"
58
0
#define BLOCK_OPT_EXTL2             "extended_l2"
59
0
#define BLOCK_OPT_KEEP_DATA_FILE    "keep_data_file"
60
61
808k
#define BLOCK_PROBE_BUF_SIZE        512
62
63
enum BdrvTrackedRequestType {
64
    BDRV_TRACKED_READ,
65
    BDRV_TRACKED_WRITE,
66
    BDRV_TRACKED_DISCARD,
67
    BDRV_TRACKED_TRUNCATE,
68
};
69
70
/*
71
 * That is not quite good that BdrvTrackedRequest structure is public,
72
 * as block/io.c is very careful about incoming offset/bytes being
73
 * correct. Be sure to assert bdrv_check_request() succeeded after any
74
 * modification of BdrvTrackedRequest object out of block/io.c
75
 */
76
typedef struct BdrvTrackedRequest {
77
    BlockDriverState *bs;
78
    int64_t offset;
79
    int64_t bytes;
80
    enum BdrvTrackedRequestType type;
81
82
    bool serialising;
83
    bool active_owner;
84
    int64_t overlap_offset;
85
    int64_t overlap_bytes;
86
87
    QLIST_ENTRY(BdrvTrackedRequest) list;
88
    Coroutine *co; /* owner, used for deadlock detection */
89
    CoQueue wait_queue; /* coroutines blocked on this request */
90
91
    struct BdrvTrackedRequest *waiting_for;
92
} BdrvTrackedRequest;
93
94
95
struct BlockDriver {
96
    /*
97
     * These fields are initialized when this object is created,
98
     * and are never changed afterwards.
99
     */
100
101
    const char *format_name;
102
    int instance_size;
103
104
    /*
105
     * Set to true if the BlockDriver is a block filter. Block filters pass
106
     * certain callbacks that refer to data (see block.c) to their bs->file
107
     * or bs->backing (whichever one exists) if the driver doesn't implement
108
     * them. Drivers that do not wish to forward must implement them and return
109
     * -ENOTSUP.
110
     * Note that filters are not allowed to modify data.
111
     *
112
     * Filters generally cannot have more than a single filtered child,
113
     * because the data they present must at all times be the same as
114
     * that on their filtered child.  That would be impossible to
115
     * achieve for multiple filtered children.
116
     * (And this filtered child must then be bs->file or bs->backing.)
117
     */
118
    bool is_filter;
119
    /*
120
     * Only make sense for filter drivers, for others must be false.
121
     * If true, filtered child is bs->backing. Otherwise it's bs->file.
122
     * Two internal filters use bs->backing as filtered child and has this
123
     * field set to true: mirror_top and commit_top. There also two such test
124
     * filters in tests/unit/test-bdrv-graph-mod.c.
125
     *
126
     * Never create any more such filters!
127
     *
128
     * TODO: imagine how to deprecate this behavior and make all filters work
129
     * similarly using bs->file as filtered child.
130
     */
131
    bool filtered_child_is_backing;
132
133
    /*
134
     * Set to true if the BlockDriver is a format driver.  Format nodes
135
     * generally do not expect their children to be other format nodes
136
     * (except for backing files), and so format probing is disabled
137
     * on those children.
138
     */
139
    bool is_format;
140
141
    /*
142
     * Set to true if the BlockDriver supports zoned children.
143
     */
144
    bool supports_zoned_children;
145
146
    /*
147
     * Drivers not implementing bdrv_parse_filename nor bdrv_open should have
148
     * this field set to true, except ones that are defined only by their
149
     * child's bs.
150
     * An example of the last type will be the quorum block driver.
151
     */
152
    bool bdrv_needs_filename;
153
154
    /*
155
     * Set if a driver can support backing files. This also implies the
156
     * following semantics:
157
     *
158
     *  - Return status 0 of .bdrv_co_block_status means that corresponding
159
     *    blocks are not allocated in this layer of backing-chain
160
     *  - For such (unallocated) blocks, read will:
161
     *    - fill buffer with zeros if there is no backing file
162
     *    - read from the backing file otherwise, where the block layer
163
     *      takes care of reading zeros beyond EOF if backing file is short
164
     */
165
    bool supports_backing;
166
167
    /*
168
     * Drivers setting this field must be able to work with just a plain
169
     * filename with '<protocol_name>:' as a prefix, and no other options.
170
     * Options may be extracted from the filename by implementing
171
     * bdrv_parse_filename.
172
     */
173
    const char *protocol_name;
174
175
    /* List of options for creating images, terminated by name == NULL */
176
    QemuOptsList *create_opts;
177
178
    /* List of options for image amend */
179
    QemuOptsList *amend_opts;
180
181
    /*
182
     * If this driver supports reopening images this contains a
183
     * NULL-terminated list of the runtime options that can be
184
     * modified. If an option in this list is unspecified during
185
     * reopen then it _must_ be reset to its default value or return
186
     * an error.
187
     */
188
    const char *const *mutable_opts;
189
190
    /*
191
     * Pointer to a NULL-terminated array of names of strong options
192
     * that can be specified for bdrv_open(). A strong option is one
193
     * that changes the data of a BDS.
194
     * If this pointer is NULL, the array is considered empty.
195
     * "filename" and "driver" are always considered strong.
196
     */
197
    const char *const *strong_runtime_opts;
198
199
200
    /*
201
     * Global state (GS) API. These functions run under the BQL.
202
     *
203
     * See include/block/block-global-state.h for more information about
204
     * the GS API.
205
     */
206
207
    /*
208
     * This function is invoked under BQL before .bdrv_co_amend()
209
     * (which in contrast does not necessarily run under the BQL)
210
     * to allow driver-specific initialization code that requires
211
     * the BQL, like setting up specific permission flags.
212
     */
213
    int GRAPH_RDLOCK_PTR (*bdrv_amend_pre_run)(
214
        BlockDriverState *bs, Error **errp);
215
    /*
216
     * This function is invoked under BQL after .bdrv_co_amend()
217
     * to allow cleaning up what was done in .bdrv_amend_pre_run().
218
     */
219
    void GRAPH_RDLOCK_PTR (*bdrv_amend_clean)(BlockDriverState *bs);
220
221
    /*
222
     * Return true if @to_replace can be replaced by a BDS with the
223
     * same data as @bs without it affecting @bs's behavior (that is,
224
     * without it being visible to @bs's parents).
225
     */
226
    bool GRAPH_RDLOCK_PTR (*bdrv_recurse_can_replace)(
227
        BlockDriverState *bs, BlockDriverState *to_replace);
228
229
    int (*bdrv_probe_device)(const char *filename);
230
231
    /*
232
     * Any driver implementing this callback is expected to be able to handle
233
     * NULL file names in its .bdrv_open() implementation.
234
     */
235
    void (*bdrv_parse_filename)(const char *filename, QDict *options,
236
                                Error **errp);
237
238
    /* For handling image reopen for split or non-split files. */
239
    int GRAPH_UNLOCKED_PTR (*bdrv_reopen_prepare)(
240
        BDRVReopenState *reopen_state, BlockReopenQueue *queue, Error **errp);
241
    void GRAPH_UNLOCKED_PTR (*bdrv_reopen_commit)(
242
        BDRVReopenState *reopen_state);
243
    void GRAPH_UNLOCKED_PTR (*bdrv_reopen_commit_post)(
244
        BDRVReopenState *reopen_state);
245
    void GRAPH_UNLOCKED_PTR (*bdrv_reopen_abort)(
246
        BDRVReopenState *reopen_state);
247
    void (*bdrv_join_options)(QDict *options, QDict *old_options);
248
249
    int GRAPH_UNLOCKED_PTR (*bdrv_open)(
250
        BlockDriverState *bs, QDict *options, int flags, Error **errp);
251
252
    void GRAPH_UNLOCKED_PTR (*bdrv_close)(BlockDriverState *bs);
253
254
    int coroutine_fn GRAPH_UNLOCKED_PTR (*bdrv_co_create)(
255
        BlockdevCreateOptions *opts, Error **errp);
256
257
    int coroutine_fn GRAPH_UNLOCKED_PTR (*bdrv_co_create_opts)(
258
        BlockDriver *drv, const char *filename, QemuOpts *opts, Error **errp);
259
260
    int GRAPH_RDLOCK_PTR (*bdrv_amend_options)(
261
        BlockDriverState *bs, QemuOpts *opts,
262
        BlockDriverAmendStatusCB *status_cb, void *cb_opaque,
263
        bool force, Error **errp);
264
265
    int GRAPH_RDLOCK_PTR (*bdrv_make_empty)(BlockDriverState *bs);
266
267
    /*
268
     * Refreshes the bs->exact_filename field. If that is impossible,
269
     * bs->exact_filename has to be left empty.
270
     */
271
    void GRAPH_RDLOCK_PTR (*bdrv_refresh_filename)(BlockDriverState *bs);
272
273
    /*
274
     * Gathers the open options for all children into @target.
275
     * A simple format driver (without backing file support) might
276
     * implement this function like this:
277
     *
278
     *     QINCREF(bs->file->bs->full_open_options);
279
     *     qdict_put(target, "file", bs->file->bs->full_open_options);
280
     *
281
     * If not specified, the generic implementation will simply put
282
     * all children's options under their respective name.
283
     *
284
     * @backing_overridden is true when bs->backing seems not to be
285
     * the child that would result from opening bs->backing_file.
286
     * Therefore, if it is true, the backing child's options should be
287
     * gathered; otherwise, there is no need since the backing child
288
     * is the one implied by the image header.
289
     *
290
     * Note that ideally this function would not be needed.  Every
291
     * block driver which implements it is probably doing something
292
     * shady regarding its runtime option structure.
293
     */
294
    void GRAPH_RDLOCK_PTR (*bdrv_gather_child_options)(
295
        BlockDriverState *bs, QDict *target, bool backing_overridden);
296
297
    /*
298
     * Returns an allocated string which is the directory name of this BDS: It
299
     * will be used to make relative filenames absolute by prepending this
300
     * function's return value to them.
301
     */
302
    char * GRAPH_RDLOCK_PTR (*bdrv_dirname)(BlockDriverState *bs, Error **errp);
303
304
    /*
305
     * This informs the driver that we are no longer interested in the result
306
     * of in-flight requests, so don't waste the time if possible.
307
     *
308
     * One example usage is to avoid waiting for an nbd target node reconnect
309
     * timeout during job-cancel with force=true.
310
     */
311
    void GRAPH_RDLOCK_PTR (*bdrv_cancel_in_flight)(BlockDriverState *bs);
312
313
    int GRAPH_RDLOCK_PTR (*bdrv_inactivate)(BlockDriverState *bs);
314
315
    int GRAPH_RDLOCK_PTR (*bdrv_snapshot_create)(
316
        BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
317
318
    int GRAPH_UNLOCKED_PTR (*bdrv_snapshot_goto)(
319
        BlockDriverState *bs, const char *snapshot_id);
320
321
    int GRAPH_RDLOCK_PTR (*bdrv_snapshot_delete)(
322
        BlockDriverState *bs, const char *snapshot_id, const char *name,
323
        Error **errp);
324
325
    int GRAPH_RDLOCK_PTR (*bdrv_snapshot_list)(
326
        BlockDriverState *bs, QEMUSnapshotInfo **psn_info);
327
328
    int GRAPH_RDLOCK_PTR (*bdrv_snapshot_load_tmp)(
329
        BlockDriverState *bs, const char *snapshot_id, const char *name,
330
        Error **errp);
331
332
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_change_backing_file)(
333
        BlockDriverState *bs, const char *backing_file,
334
        const char *backing_fmt);
335
336
    /* TODO Better pass a option string/QDict/QemuOpts to add any rule? */
337
    int (*bdrv_debug_breakpoint)(BlockDriverState *bs, const char *event,
338
        const char *tag);
339
    int (*bdrv_debug_remove_breakpoint)(BlockDriverState *bs,
340
        const char *tag);
341
    int (*bdrv_debug_resume)(BlockDriverState *bs, const char *tag);
342
    bool (*bdrv_debug_is_suspended)(BlockDriverState *bs, const char *tag);
343
344
    void GRAPH_RDLOCK_PTR (*bdrv_refresh_limits)(
345
        BlockDriverState *bs, Error **errp);
346
347
    /*
348
     * Returns 1 if newly created images are guaranteed to contain only
349
     * zeros, 0 otherwise.
350
     */
351
    int GRAPH_RDLOCK_PTR (*bdrv_has_zero_init)(BlockDriverState *bs);
352
353
    /*
354
     * Remove fd handlers, timers, and other event loop callbacks so the event
355
     * loop is no longer in use.  Called with no in-flight requests and in
356
     * depth-first traversal order with parents before child nodes.
357
     */
358
    void (*bdrv_detach_aio_context)(BlockDriverState *bs);
359
360
    /*
361
     * Add fd handlers, timers, and other event loop callbacks so I/O requests
362
     * can be processed again.  Called with no in-flight requests and in
363
     * depth-first traversal order with child nodes before parent nodes.
364
     */
365
    void (*bdrv_attach_aio_context)(BlockDriverState *bs,
366
                                    AioContext *new_context);
367
368
    /**
369
     * bdrv_drain_begin is called if implemented in the beginning of a
370
     * drain operation to drain and stop any internal sources of requests in
371
     * the driver.
372
     * bdrv_drain_end is called if implemented at the end of the drain.
373
     *
374
     * They should be used by the driver to e.g. manage scheduled I/O
375
     * requests, or toggle an internal state. After the end of the drain new
376
     * requests will continue normally.
377
     *
378
     * Implementations of both functions must not call aio_poll().
379
     */
380
    void (*bdrv_drain_begin)(BlockDriverState *bs);
381
    void (*bdrv_drain_end)(BlockDriverState *bs);
382
383
    /**
384
     * Try to get @bs's logical and physical block size.
385
     * On success, store them in @bsz and return zero.
386
     * On failure, return negative errno.
387
     */
388
    int GRAPH_RDLOCK_PTR (*bdrv_probe_blocksizes)(
389
        BlockDriverState *bs, BlockSizes *bsz);
390
    /**
391
     * Try to get @bs's geometry (cyls, heads, sectors)
392
     * On success, store them in @geo and return 0.
393
     * On failure return -errno.
394
     * Only drivers that want to override guest geometry implement this
395
     * callback; see hd_geometry_guess().
396
     */
397
    int GRAPH_RDLOCK_PTR (*bdrv_probe_geometry)(
398
        BlockDriverState *bs, HDGeometry *geo);
399
400
    /**
401
     * Hot add a BDS's child. Used in combination with bdrv_del_child, so the
402
     * user can take a child offline when it is broken and take a new child
403
     * online.
404
     *
405
     * All block nodes must be drained.
406
     */
407
    void GRAPH_WRLOCK_PTR (*bdrv_add_child)(
408
        BlockDriverState *parent, BlockDriverState *child, Error **errp);
409
410
    /**
411
     * Hot remove a BDS's child. Used in combination with bdrv_add_child, so the
412
     * user can take a child offline when it is broken and take a new child
413
     * online.
414
     *
415
     * All block nodes must be drained.
416
     */
417
    void GRAPH_WRLOCK_PTR (*bdrv_del_child)(
418
        BlockDriverState *parent, BdrvChild *child, Error **errp);
419
420
    /**
421
     * Informs the block driver that a permission change is intended. The
422
     * driver checks whether the change is permissible and may take other
423
     * preparations for the change (e.g. get file system locks). This operation
424
     * is always followed either by a call to either .bdrv_set_perm or
425
     * .bdrv_abort_perm_update.
426
     *
427
     * Checks whether the requested set of cumulative permissions in @perm
428
     * can be granted for accessing @bs and whether no other users are using
429
     * permissions other than those given in @shared (both arguments take
430
     * BLK_PERM_* bitmasks).
431
     *
432
     * If both conditions are met, 0 is returned. Otherwise, -errno is returned
433
     * and errp is set to an error describing the conflict.
434
     */
435
    int GRAPH_RDLOCK_PTR (*bdrv_check_perm)(BlockDriverState *bs, uint64_t perm,
436
                                            uint64_t shared, Error **errp);
437
438
    /**
439
     * Called to inform the driver that the set of cumulative set of used
440
     * permissions for @bs has changed to @perm, and the set of shareable
441
     * permission to @shared. The driver can use this to propagate changes to
442
     * its children (i.e. request permissions only if a parent actually needs
443
     * them).
444
     *
445
     * This function is only invoked after bdrv_check_perm(), so block drivers
446
     * may rely on preparations made in their .bdrv_check_perm implementation.
447
     */
448
    void GRAPH_RDLOCK_PTR (*bdrv_set_perm)(
449
        BlockDriverState *bs, uint64_t perm, uint64_t shared);
450
451
    /*
452
     * Called to inform the driver that after a previous bdrv_check_perm()
453
     * call, the permission update is not performed and any preparations made
454
     * for it (e.g. taken file locks) need to be undone.
455
     *
456
     * This function can be called even for nodes that never saw a
457
     * bdrv_check_perm() call. It is a no-op then.
458
     */
459
    void GRAPH_RDLOCK_PTR (*bdrv_abort_perm_update)(BlockDriverState *bs);
460
461
    /**
462
     * Returns in @nperm and @nshared the permissions that the driver for @bs
463
     * needs on its child @c, based on the cumulative permissions requested by
464
     * the parents in @parent_perm and @parent_shared.
465
     *
466
     * If @c is NULL, return the permissions for attaching a new child for the
467
     * given @child_class and @role.
468
     *
469
     * If @reopen_queue is non-NULL, don't return the currently needed
470
     * permissions, but those that will be needed after applying the
471
     * @reopen_queue.
472
     */
473
     void GRAPH_RDLOCK_PTR (*bdrv_child_perm)(
474
        BlockDriverState *bs, BdrvChild *c, BdrvChildRole role,
475
        BlockReopenQueue *reopen_queue,
476
        uint64_t parent_perm, uint64_t parent_shared,
477
        uint64_t *nperm, uint64_t *nshared);
478
479
    /**
480
     * Register/unregister a buffer for I/O. For example, when the driver is
481
     * interested to know the memory areas that will later be used in iovs, so
482
     * that it can do IOMMU mapping with VFIO etc., in order to get better
483
     * performance. In the case of VFIO drivers, this callback is used to do
484
     * DMA mapping for hot buffers.
485
     *
486
     * Returns: true on success, false on failure
487
     */
488
    bool GRAPH_RDLOCK_PTR (*bdrv_register_buf)(
489
        BlockDriverState *bs, void *host, size_t size, Error **errp);
490
    void GRAPH_RDLOCK_PTR (*bdrv_unregister_buf)(
491
        BlockDriverState *bs, void *host, size_t size);
492
493
    /*
494
     * This field is modified only under the BQL, and is part of
495
     * the global state.
496
     */
497
    QLIST_ENTRY(BlockDriver) list;
498
499
    /*
500
     * I/O API functions. These functions are thread-safe.
501
     *
502
     * See include/block/block-io.h for more information about
503
     * the I/O API.
504
     */
505
506
    int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
507
508
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_amend)(
509
        BlockDriverState *bs, BlockdevAmendOptions *opts, bool force,
510
        Error **errp);
511
512
    /*
513
     * AIO
514
     * The given completion callback will be run in the same AioContext as the
515
     * one in which the AIO function was called.
516
     */
517
518
    BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_preadv)(BlockDriverState *bs,
519
        int64_t offset, int64_t bytes, QEMUIOVector *qiov,
520
        BdrvRequestFlags flags, BlockCompletionFunc *cb, void *opaque);
521
522
    BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_pwritev)(BlockDriverState *bs,
523
        int64_t offset, int64_t bytes, QEMUIOVector *qiov,
524
        BdrvRequestFlags flags, BlockCompletionFunc *cb, void *opaque);
525
526
    BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_flush)(
527
        BlockDriverState *bs, BlockCompletionFunc *cb, void *opaque);
528
529
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_readv)(BlockDriverState *bs,
530
        int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
531
532
    /**
533
     * @offset: position in bytes to read at
534
     * @bytes: number of bytes to read
535
     * @qiov: the buffers to fill with read data
536
     * @flags: currently unused, always 0
537
     *
538
     * @offset and @bytes will be a multiple of 'request_alignment',
539
     * but the length of individual @qiov elements does not have to
540
     * be a multiple.
541
     *
542
     * @bytes will always equal the total size of @qiov, and will be
543
     * no larger than 'max_transfer'.
544
     *
545
     * The buffer in @qiov may point directly to guest memory.
546
     */
547
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv)(BlockDriverState *bs,
548
        int64_t offset, int64_t bytes, QEMUIOVector *qiov,
549
        BdrvRequestFlags flags);
550
551
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv_part)(
552
        BlockDriverState *bs, int64_t offset, int64_t bytes,
553
        QEMUIOVector *qiov, size_t qiov_offset,
554
        BdrvRequestFlags flags);
555
556
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_writev)(BlockDriverState *bs,
557
        int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
558
        int flags);
559
    /**
560
     * @offset: position in bytes to write at
561
     * @bytes: number of bytes to write
562
     * @qiov: the buffers containing data to write
563
     * @flags: zero or more bits allowed by 'supported_write_flags'
564
     *
565
     * @offset and @bytes will be a multiple of 'request_alignment',
566
     * but the length of individual @qiov elements does not have to
567
     * be a multiple.
568
     *
569
     * @bytes will always equal the total size of @qiov, and will be
570
     * no larger than 'max_transfer'.
571
     *
572
     * The buffer in @qiov may point directly to guest memory.
573
     */
574
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev)(
575
        BlockDriverState *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov,
576
        BdrvRequestFlags flags);
577
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_part)(
578
        BlockDriverState *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov,
579
        size_t qiov_offset, BdrvRequestFlags flags);
580
581
    /*
582
     * Efficiently zero a region of the disk image.  Typically an image format
583
     * would use a compact metadata representation to implement this.  This
584
     * function pointer may be NULL or return -ENOSUP and .bdrv_co_writev()
585
     * will be called instead.
586
     */
587
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwrite_zeroes)(
588
        BlockDriverState *bs, int64_t offset, int64_t bytes,
589
        BdrvRequestFlags flags);
590
591
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pdiscard)(
592
        BlockDriverState *bs, int64_t offset, int64_t bytes);
593
594
    /*
595
     * Map [offset, offset + nbytes) range onto a child of @bs to copy from,
596
     * and invoke bdrv_co_copy_range_from(child, ...), or invoke
597
     * bdrv_co_copy_range_to() if @bs is the leaf child to copy data from.
598
     *
599
     * See the comment of bdrv_co_copy_range for the parameter and return value
600
     * semantics.
601
     */
602
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_copy_range_from)(
603
        BlockDriverState *bs, BdrvChild *src, int64_t offset,
604
        BdrvChild *dst, int64_t dst_offset, int64_t bytes,
605
        BdrvRequestFlags read_flags, BdrvRequestFlags write_flags);
606
607
    /*
608
     * Map [offset, offset + nbytes) range onto a child of bs to copy data to,
609
     * and invoke bdrv_co_copy_range_to(child, src, ...), or perform the copy
610
     * operation if @bs is the leaf and @src has the same BlockDriver.  Return
611
     * -ENOTSUP if @bs is the leaf but @src has a different BlockDriver.
612
     *
613
     * See the comment of bdrv_co_copy_range for the parameter and return value
614
     * semantics.
615
     */
616
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_copy_range_to)(
617
        BlockDriverState *bs, BdrvChild *src, int64_t src_offset,
618
        BdrvChild *dst, int64_t dst_offset, int64_t bytes,
619
        BdrvRequestFlags read_flags, BdrvRequestFlags write_flags);
620
621
    /*
622
     * Building block for bdrv_block_status[_above] and
623
     * bdrv_is_allocated[_above].  The driver should answer only
624
     * according to the current layer, and should only need to set
625
     * BDRV_BLOCK_DATA, BDRV_BLOCK_ZERO, BDRV_BLOCK_OFFSET_VALID,
626
     * and/or BDRV_BLOCK_RAW; if the current layer defers to a backing
627
     * layer, the result should be 0 (and not BDRV_BLOCK_ZERO).  The
628
     * caller will synthesize BDRV_BLOCK_ALLOCATED based on the
629
     * non-zero results.  See block.h for the overall meaning of the
630
     * bits.  As a hint, the flags in @mode may include a bitwise-or
631
     * of BDRV_WANT_ALLOCATED, BDRV_WANT_OFFSET_VALID, or
632
     * BDRV_WANT_ZERO based on what the caller is looking for in the
633
     * results.  The block layer guarantees input clamped to
634
     * bdrv_getlength() and aligned to request_alignment, as well as
635
     * non-NULL pnum, map, and file; in turn, the driver must return
636
     * an error or set pnum to an aligned non-zero value.
637
     *
638
     * Note that @bytes is just a hint on how big of a region the
639
     * caller wants to inspect.  It is not a limit on *pnum.
640
     * Implementations are free to return larger values of *pnum if
641
     * doing so does not incur a performance penalty.
642
     *
643
     * block/io.c's bdrv_co_block_status() will utilize an unclamped
644
     * *pnum value for the block-status cache on protocol nodes, prior
645
     * to clamping *pnum for return to its caller.
646
     */
647
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_block_status)(
648
        BlockDriverState *bs, unsigned int mode,
649
        int64_t offset, int64_t bytes, int64_t *pnum,
650
        int64_t *map, BlockDriverState **file);
651
652
    /*
653
     * Snapshot-access API.
654
     *
655
     * Block-driver may provide snapshot-access API: special functions to access
656
     * some internal "snapshot". The functions are similar with normal
657
     * read/block_status/discard handler, but don't have any specific handling
658
     * in generic block-layer: no serializing, no alignment, no tracked
659
     * requests. So, block-driver that realizes these APIs is fully responsible
660
     * for synchronization between snapshot-access API and normal IO requests.
661
     *
662
     * TODO: To be able to support qcow2's internal snapshots, this API will
663
     * need to be extended to:
664
     * - be able to select a specific snapshot
665
     * - receive the snapshot's actual length (which may differ from bs's
666
     *   length)
667
     */
668
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv_snapshot)(
669
        BlockDriverState *bs, int64_t offset, int64_t bytes,
670
        QEMUIOVector *qiov, size_t qiov_offset);
671
672
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_snapshot_block_status)(
673
        BlockDriverState *bs, unsigned int mode, int64_t offset,
674
        int64_t bytes, int64_t *pnum, int64_t *map, BlockDriverState **file);
675
676
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pdiscard_snapshot)(
677
        BlockDriverState *bs, int64_t offset, int64_t bytes);
678
679
    /*
680
     * Invalidate any cached meta-data.
681
     */
682
    void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_invalidate_cache)(
683
        BlockDriverState *bs, Error **errp);
684
685
    /*
686
     * Flushes all data for all layers by calling bdrv_co_flush for underlying
687
     * layers, if needed. This function is needed for deterministic
688
     * synchronization of the flush finishing callback.
689
     */
690
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush)(BlockDriverState *bs);
691
692
    /* Delete a created file. */
693
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_delete_file)(
694
        BlockDriverState *bs, Error **errp);
695
696
    /*
697
     * Flushes all data that was already written to the OS all the way down to
698
     * the disk (for example file-posix.c calls fsync()).
699
     */
700
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush_to_disk)(
701
        BlockDriverState *bs);
702
703
    /*
704
     * Flushes all internal caches to the OS. The data may still sit in a
705
     * writeback cache of the host OS, but it will survive a crash of the qemu
706
     * process.
707
     */
708
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush_to_os)(
709
        BlockDriverState *bs);
710
711
    /*
712
     * Truncate @bs to @offset bytes using the given @prealloc mode
713
     * when growing.  Modes other than PREALLOC_MODE_OFF should be
714
     * rejected when shrinking @bs.
715
     *
716
     * If @exact is true, @bs must be resized to exactly @offset.
717
     * Otherwise, it is sufficient for @bs (if it is a host block
718
     * device and thus there is no way to resize it) to be at least
719
     * @offset bytes in length.
720
     *
721
     * If @exact is true and this function fails but would succeed
722
     * with @exact = false, it should return -ENOTSUP.
723
     */
724
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_truncate)(
725
        BlockDriverState *bs, int64_t offset, bool exact,
726
        PreallocMode prealloc, BdrvRequestFlags flags, Error **errp);
727
728
    int64_t coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_getlength)(
729
        BlockDriverState *bs);
730
731
    int64_t coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_get_allocated_file_size)(
732
        BlockDriverState *bs);
733
734
    BlockMeasureInfo *(*bdrv_measure)(QemuOpts *opts, BlockDriverState *in_bs,
735
                                      Error **errp);
736
737
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_compressed)(
738
        BlockDriverState *bs, int64_t offset, int64_t bytes,
739
        QEMUIOVector *qiov);
740
741
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_compressed_part)(
742
        BlockDriverState *bs, int64_t offset, int64_t bytes,
743
        QEMUIOVector *qiov, size_t qiov_offset);
744
745
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_get_info)(
746
        BlockDriverState *bs, BlockDriverInfo *bdi);
747
748
    ImageInfoSpecific * GRAPH_RDLOCK_PTR (*bdrv_get_specific_info)(
749
        BlockDriverState *bs, Error **errp);
750
    BlockStatsSpecific *(*bdrv_get_specific_stats)(BlockDriverState *bs);
751
752
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_save_vmstate)(
753
        BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
754
755
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_load_vmstate)(
756
        BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
757
758
    int coroutine_fn (*bdrv_co_zone_report)(BlockDriverState *bs,
759
            int64_t offset, unsigned int *nr_zones,
760
            BlockZoneDescriptor *zones);
761
    int coroutine_fn (*bdrv_co_zone_mgmt)(BlockDriverState *bs, BlockZoneOp op,
762
            int64_t offset, int64_t len);
763
    int coroutine_fn (*bdrv_co_zone_append)(BlockDriverState *bs,
764
            int64_t *offset, QEMUIOVector *qiov,
765
            BdrvRequestFlags flags);
766
767
    /* removable device specific */
768
    bool coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_is_inserted)(
769
        BlockDriverState *bs);
770
    void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_eject)(
771
        BlockDriverState *bs, bool eject_flag);
772
    void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_lock_medium)(
773
        BlockDriverState *bs, bool locked);
774
775
    /* to control generic scsi devices */
776
    BlockAIOCB *coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_aio_ioctl)(
777
        BlockDriverState *bs, unsigned long int req, void *buf,
778
        BlockCompletionFunc *cb, void *opaque);
779
780
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_ioctl)(
781
        BlockDriverState *bs, unsigned long int req, void *buf);
782
783
    /*
784
     * Returns 0 for completed check, -errno for internal errors.
785
     * The check results are stored in result.
786
     */
787
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_check)(
788
        BlockDriverState *bs, BdrvCheckResult *result, BdrvCheckMode fix);
789
790
    void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_debug_event)(
791
        BlockDriverState *bs, BlkdebugEvent event);
792
793
    bool (*bdrv_supports_persistent_dirty_bitmap)(BlockDriverState *bs);
794
795
    bool coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_can_store_new_dirty_bitmap)(
796
        BlockDriverState *bs, const char *name, uint32_t granularity,
797
        Error **errp);
798
799
    int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_remove_persistent_dirty_bitmap)(
800
        BlockDriverState *bs, const char *name, Error **errp);
801
};
802
803
static inline bool TSA_NO_TSA block_driver_can_compress(BlockDriver *drv)
804
0
{
805
0
    return drv->bdrv_co_pwritev_compressed ||
806
0
           drv->bdrv_co_pwritev_compressed_part;
807
0
}
Unexecuted instantiation: block-dirty-bitmap.c:block_driver_can_compress
Unexecuted instantiation: block.c:block_driver_can_compress
Unexecuted instantiation: virtio-blk.c:block_driver_can_compress
Unexecuted instantiation: blkreplay.c:block_driver_can_compress
Unexecuted instantiation: qapi-system.c:block_driver_can_compress
Unexecuted instantiation: block-hmp-cmds.c:block_driver_can_compress
Unexecuted instantiation: qmp-cmds.c:block_driver_can_compress
Unexecuted instantiation: server.c:block_driver_can_compress
Unexecuted instantiation: blockdev.c:block_driver_can_compress
Unexecuted instantiation: blockjob.c:block_driver_can_compress
Unexecuted instantiation: qemu-io-cmds.c:block_driver_can_compress
Unexecuted instantiation: accounting.c:block_driver_can_compress
Unexecuted instantiation: amend.c:block_driver_can_compress
Unexecuted instantiation: backup.c:block_driver_can_compress
Unexecuted instantiation: blkdebug.c:block_driver_can_compress
Unexecuted instantiation: blklogwrites.c:block_driver_can_compress
Unexecuted instantiation: blkverify.c:block_driver_can_compress
Unexecuted instantiation: block-backend.c:block_driver_can_compress
Unexecuted instantiation: block-copy.c:block_driver_can_compress
Unexecuted instantiation: commit.c:block_driver_can_compress
Unexecuted instantiation: copy-before-write.c:block_driver_can_compress
Unexecuted instantiation: copy-on-read.c:block_driver_can_compress
Unexecuted instantiation: create.c:block_driver_can_compress
Unexecuted instantiation: crypto.c:block_driver_can_compress
Unexecuted instantiation: dirty-bitmap.c:block_driver_can_compress
Unexecuted instantiation: filter-compress.c:block_driver_can_compress
Unexecuted instantiation: graph-lock.c:block_driver_can_compress
Unexecuted instantiation: io.c:block_driver_can_compress
Unexecuted instantiation: mirror.c:block_driver_can_compress
Unexecuted instantiation: nbd.c:block_driver_can_compress
Unexecuted instantiation: null.c:block_driver_can_compress
Unexecuted instantiation: preallocate.c:block_driver_can_compress
Unexecuted instantiation: qapi.c:block_driver_can_compress
Unexecuted instantiation: qcow2.c:block_driver_can_compress
Unexecuted instantiation: qcow2-bitmap.c:block_driver_can_compress
Unexecuted instantiation: qcow2-cache.c:block_driver_can_compress
Unexecuted instantiation: qcow2-cluster.c:block_driver_can_compress
Unexecuted instantiation: qcow2-refcount.c:block_driver_can_compress
Unexecuted instantiation: qcow2-snapshot.c:block_driver_can_compress
Unexecuted instantiation: qcow2-threads.c:block_driver_can_compress
Unexecuted instantiation: quorum.c:block_driver_can_compress
Unexecuted instantiation: raw-format.c:block_driver_can_compress
Unexecuted instantiation: snapshot.c:block_driver_can_compress
Unexecuted instantiation: snapshot-access.c:block_driver_can_compress
Unexecuted instantiation: throttle.c:block_driver_can_compress
Unexecuted instantiation: write-threshold.c:block_driver_can_compress
Unexecuted instantiation: qcow.c:block_driver_can_compress
Unexecuted instantiation: vdi.c:block_driver_can_compress
Unexecuted instantiation: vhdx-endian.c:block_driver_can_compress
Unexecuted instantiation: vhdx-log.c:block_driver_can_compress
Unexecuted instantiation: vhdx.c:block_driver_can_compress
Unexecuted instantiation: vmdk.c:block_driver_can_compress
Unexecuted instantiation: vpc.c:block_driver_can_compress
Unexecuted instantiation: cloop.c:block_driver_can_compress
Unexecuted instantiation: bochs.c:block_driver_can_compress
Unexecuted instantiation: vvfat.c:block_driver_can_compress
Unexecuted instantiation: dmg.c:block_driver_can_compress
Unexecuted instantiation: qed-check.c:block_driver_can_compress
Unexecuted instantiation: qed-cluster.c:block_driver_can_compress
Unexecuted instantiation: qed-l2-cache.c:block_driver_can_compress
Unexecuted instantiation: qed-table.c:block_driver_can_compress
Unexecuted instantiation: qed.c:block_driver_can_compress
Unexecuted instantiation: parallels.c:block_driver_can_compress
Unexecuted instantiation: parallels-ext.c:block_driver_can_compress
Unexecuted instantiation: file-posix.c:block_driver_can_compress
Unexecuted instantiation: nvme.c:block_driver_can_compress
Unexecuted instantiation: replication.c:block_driver_can_compress
Unexecuted instantiation: stream.c:block_driver_can_compress
Unexecuted instantiation: bitmap-qmp-cmds.c:block_driver_can_compress
Unexecuted instantiation: block-gen.c:block_driver_can_compress
808
809
typedef struct BlockLimits {
810
    /*
811
     * Alignment requirement, in bytes, for offset/length of I/O
812
     * requests. Must be a power of 2 less than INT_MAX; defaults to
813
     * 1 for drivers with modern byte interfaces, and to 512
814
     * otherwise.
815
     */
816
    uint32_t request_alignment;
817
818
    /*
819
     * Maximum number of bytes that can be discarded at once. Must be multiple
820
     * of pdiscard_alignment, but need not be power of 2. May be 0 if no
821
     * inherent 64-bit limit.
822
     */
823
    int64_t max_pdiscard;
824
825
    /*
826
     * Optimal alignment for discard requests in bytes. Note that this doesn't
827
     * have to be a power of two. Must be a multiple of bl.request_alignment,
828
     * and must be less than max_pdiscard if that is set. May be 0 if
829
     * bl.request_alignment is good enough.
830
     */
831
    uint32_t pdiscard_alignment;
832
833
    /*
834
     * Maximum number of bytes that can zeroized at once. Must be multiple of
835
     * pwrite_zeroes_alignment. 0 means no limit.
836
     */
837
    int64_t max_pwrite_zeroes;
838
839
    /*
840
     * Optimal alignment for write zeroes requests in bytes. Note that this
841
     * doesn't have to be a power of two. Must be a multiple of
842
     * bl.request_alignment, and must be less than max_pwrite_zeroes if that is
843
     * set. May be 0 if bl.request_alignment is good enough.
844
     */
845
    uint32_t pwrite_zeroes_alignment;
846
847
    /*
848
     * Optimal transfer length in bytes.  A power of 2 is best but not
849
     * mandatory.  Must be a multiple of bl.request_alignment, or 0 if
850
     * no preferred size
851
     */
852
    uint32_t opt_transfer;
853
854
    /*
855
     * Maximal transfer length in bytes.  Need not be power of 2, but
856
     * must be multiple of opt_transfer and bl.request_alignment, or 0
857
     * for no 32-bit limit.  For now, anything larger than INT_MAX is
858
     * clamped down.
859
     */
860
    uint32_t max_transfer;
861
862
    /*
863
     * Maximal hardware transfer length in bytes.  Applies whenever
864
     * transfers to the device bypass the kernel I/O scheduler, for
865
     * example with SG_IO.  If larger than max_transfer or if zero,
866
     * blk_get_max_hw_transfer will fall back to max_transfer.
867
     */
868
    uint64_t max_hw_transfer;
869
870
    /*
871
     * Maximum number of scatter/gather elements allowed by the hardware.
872
     * Applies whenever transfers to the device bypass the kernel I/O
873
     * scheduler, for example with SG_IO.  If larger than max_iov
874
     * or if zero, blk_get_max_hw_iov will fall back to max_iov.
875
     */
876
    int max_hw_iov;
877
878
    /*
879
     * Minimal required memory alignment in bytes for zero-copy I/O to succeed.
880
     * For unaligned requests, a bounce buffer will be used.
881
     */
882
    size_t min_mem_alignment;
883
884
    /*
885
     * Optimal memory alignment in bytes. This is the alignment used for any
886
     * buffer allocations QEMU performs internally.
887
     */
888
    size_t opt_mem_alignment;
889
890
    /* maximum number of iovec elements */
891
    int max_iov;
892
893
    /*
894
     * true if the length of the underlying file can change, and QEMU
895
     * is expected to adjust automatically.  Mostly for CD-ROM drives,
896
     * whose length is zero when the tray is empty (they don't need
897
     * an explicit monitor command to load the disk inside the guest).
898
     */
899
    bool has_variable_length;
900
901
    /* device zone model */
902
    BlockZoneModel zoned;
903
904
    /* zone size expressed in bytes */
905
    uint64_t zone_size;
906
907
    /*
908
     * log2 of zone_size, derived by bdrv_refresh_limits(). A zoned device
909
     * always has a zone size that is a power of two, so the zone an offset
910
     * falls in is a shift rather than a division. See bdrv_zone_index().
911
     */
912
    uint32_t zone_size_bits;
913
914
    /* total number of zones */
915
    uint32_t nr_zones;
916
917
    /* maximum number of open zones */
918
    uint32_t max_open_zones;
919
920
    /* maximum number of active zones */
921
    uint32_t max_active_zones;
922
923
    /*
924
     * The granularity that the backend requires of a write to a sequential
925
     * zone, in bytes, or zero if it has none. This describes the host, so it
926
     * must not be reported to a guest: a frontend reports
927
     * blkconf_zone_write_granularity() and checks this against it when the
928
     * device is realized.
929
     */
930
    uint32_t write_granularity;
931
} BlockLimits;
932
933
typedef struct BdrvOpBlocker BdrvOpBlocker;
934
935
typedef struct BdrvAioNotifier {
936
    void (*attached_aio_context)(AioContext *new_context, void *opaque);
937
    void (*detach_aio_context)(void *opaque);
938
939
    void *opaque;
940
    bool deleted;
941
942
    QLIST_ENTRY(BdrvAioNotifier) list;
943
} BdrvAioNotifier;
944
945
struct BdrvChildClass {
946
    /*
947
     * If true, bdrv_replace_node() doesn't change the node this BdrvChild
948
     * points to.
949
     */
950
    bool stay_at_node;
951
952
    /*
953
     * If true, the parent is a BlockDriverState and bdrv_next_all_states()
954
     * will return it. This information is used for drain_all, where every node
955
     * will be drained separately, so the drain only needs to be propagated to
956
     * non-BDS parents.
957
     */
958
    bool parent_is_bds;
959
960
    /*
961
     * Global state (GS) API. These functions run under the BQL.
962
     *
963
     * See include/block/block-global-state.h for more information about
964
     * the GS API.
965
     */
966
    void (*inherit_options)(BdrvChildRole role, bool parent_is_format,
967
                            int *child_flags, QDict *child_options,
968
                            int parent_flags, QDict *parent_options);
969
    void (*change_media)(BdrvChild *child, bool load);
970
971
    /*
972
     * Returns a malloced string that describes the parent of the child for a
973
     * human reader. This could be a node-name, BlockBackend name, qdev ID or
974
     * QOM path of the device owning the BlockBackend, job type and ID etc. The
975
     * caller is responsible for freeing the memory.
976
     */
977
    char *(*get_parent_desc)(BdrvChild *child);
978
979
    /*
980
     * Notifies the parent that the child has been activated/inactivated (e.g.
981
     * when migration is completing) and it can start/stop requesting
982
     * permissions and doing I/O on it.
983
     */
984
    void GRAPH_RDLOCK_PTR (*activate)(BdrvChild *child, Error **errp);
985
    int GRAPH_RDLOCK_PTR (*inactivate)(BdrvChild *child);
986
987
    void GRAPH_WRLOCK_PTR (*attach)(BdrvChild *child);
988
    void GRAPH_WRLOCK_PTR (*detach)(BdrvChild *child);
989
990
    /*
991
     * If this pair of functions is implemented, the parent doesn't issue new
992
     * requests after returning from .drained_begin() until .drained_end() is
993
     * called.
994
     *
995
     * These functions must not change the graph (and therefore also must not
996
     * call aio_poll(), which could change the graph indirectly).
997
     *
998
     * Note that this can be nested. If drained_begin() was called twice, new
999
     * I/O is allowed only after drained_end() was called twice, too.
1000
     */
1001
    void GRAPH_RDLOCK_PTR (*drained_begin)(BdrvChild *child);
1002
    void GRAPH_RDLOCK_PTR (*drained_end)(BdrvChild *child);
1003
1004
    /*
1005
     * Returns whether the parent has pending requests for the child. This
1006
     * callback is polled after .drained_begin() has been called until all
1007
     * activity on the child has stopped.
1008
     */
1009
    bool GRAPH_RDLOCK_PTR (*drained_poll)(BdrvChild *child);
1010
1011
    /*
1012
     * Notifies the parent that the filename of its child has changed (e.g.
1013
     * because the direct child was removed from the backing chain), so that it
1014
     * can update its reference.
1015
     */
1016
    int (*update_filename)(BdrvChild *child, BlockDriverState *new_base,
1017
                           const char *filename,
1018
                           bool backing_mask_protocol,
1019
                           Error **errp);
1020
1021
    /*
1022
     * Notifies the parent that the child is trying to change its AioContext.
1023
     * The parent may in turn change the AioContext of other nodes in the same
1024
     * transaction. Returns true if the change is possible and the transaction
1025
     * can be continued. Returns false and sets @errp if not and the transaction
1026
     * must be aborted.
1027
     *
1028
     * @visited will accumulate all visited BdrvChild objects. The caller is
1029
     * responsible for freeing the list afterwards.
1030
     *
1031
     * Must be called with the affected block nodes drained.
1032
     */
1033
    bool GRAPH_RDLOCK_PTR (*change_aio_ctx)(BdrvChild *child, AioContext *ctx,
1034
                                            GHashTable *visited,
1035
                                            Transaction *tran, Error **errp);
1036
1037
    /*
1038
     * I/O API functions. These functions are thread-safe.
1039
     *
1040
     * See include/block/block-io.h for more information about
1041
     * the I/O API.
1042
     */
1043
1044
    /*
1045
     * Notifies the parent that the child was resized.
1046
     */
1047
    void GRAPH_RDLOCK_PTR (*resize)(BdrvChild *child);
1048
1049
    /*
1050
     * Returns a name that is supposedly more useful for human users than the
1051
     * node name for identifying the node in question (in particular, a BB
1052
     * name), or NULL if the parent can't provide a better name.
1053
     */
1054
    const char *(*get_name)(BdrvChild *child);
1055
1056
    AioContext *(*get_parent_aio_context)(BdrvChild *child);
1057
};
1058
1059
extern const BdrvChildClass child_of_bds;
1060
1061
struct BdrvChild {
1062
    BlockDriverState *bs;
1063
    char *name;
1064
    const BdrvChildClass *klass;
1065
    BdrvChildRole role;
1066
    void *opaque;
1067
1068
    /**
1069
     * Granted permissions for operating on this BdrvChild (BLK_PERM_* bitmask)
1070
     */
1071
    uint64_t perm;
1072
1073
    /**
1074
     * Permissions that can still be granted to other users of @bs while this
1075
     * BdrvChild is still attached to it. (BLK_PERM_* bitmask)
1076
     */
1077
    uint64_t shared_perm;
1078
1079
    /*
1080
     * This link is frozen: the child can neither be replaced nor
1081
     * detached from the parent.
1082
     */
1083
    bool frozen;
1084
1085
    /*
1086
     * True if the parent of this child has been drained by this BdrvChild
1087
     * (through klass->drained_*).
1088
     *
1089
     * It is generally true if bs->quiesce_counter > 0. It may differ while the
1090
     * child is entering or leaving a drained section.
1091
     */
1092
    bool quiesced_parent;
1093
1094
    QLIST_ENTRY(BdrvChild GRAPH_RDLOCK_PTR) next;
1095
    QLIST_ENTRY(BdrvChild GRAPH_RDLOCK_PTR) next_parent;
1096
};
1097
1098
/*
1099
 * Allows bdrv_co_block_status() to cache one data region for a
1100
 * protocol node.
1101
 *
1102
 * @valid: Whether the cache is valid (should be accessed with atomic
1103
 *         functions so this can be reset by RCU readers)
1104
 * @data_start: Offset where we know (or strongly assume) is data
1105
 * @data_end: Offset where the data region ends (which is not necessarily
1106
 *            the start of a zeroed region)
1107
 */
1108
typedef struct BdrvBlockStatusCache {
1109
    struct rcu_head rcu;
1110
1111
    bool valid;
1112
    int64_t data_start;
1113
    int64_t data_end;
1114
} BdrvBlockStatusCache;
1115
1116
struct BlockDriverState {
1117
    /*
1118
     * Protected by big QEMU lock or read-only after opening.  No special
1119
     * locking needed during I/O...
1120
     */
1121
    int open_flags; /* flags used to open the file, re-used for re-open */
1122
    bool encrypted; /* if true, the media is encrypted */
1123
    bool sg;        /* if true, the device is a /dev/sg* */
1124
    bool probed;    /* if true, format was probed rather than specified */
1125
    bool force_share; /* if true, always allow all shared permissions */
1126
    bool implicit;  /* if true, this filter node was automatically inserted */
1127
1128
    BlockDriver *drv; /* NULL means no media */
1129
    void *opaque;
1130
1131
    AioContext *aio_context; /* event loop used for fd handlers, timers, etc */
1132
    /*
1133
     * long-running tasks intended to always use the same AioContext as this
1134
     * BDS may register themselves in this list to be notified of changes
1135
     * regarding this BDS's context
1136
     */
1137
    QLIST_HEAD(, BdrvAioNotifier) aio_notifiers;
1138
    bool walking_aio_notifiers; /* to make removal during iteration safe */
1139
1140
    char filename[PATH_MAX];
1141
    /*
1142
     * If not empty, this image is a diff in relation to backing_file.
1143
     * Note that this is the name given in the image header and
1144
     * therefore may or may not be equal to .backing->bs->filename.
1145
     * If this field contains a relative path, it is to be resolved
1146
     * relatively to the overlay's location.
1147
     */
1148
    char backing_file[PATH_MAX];
1149
    /*
1150
     * The backing filename indicated by the image header.  Contrary
1151
     * to backing_file, if we ever open this file, auto_backing_file
1152
     * is replaced by the resulting BDS's filename (i.e. after a
1153
     * bdrv_refresh_filename() run).
1154
     */
1155
    char auto_backing_file[PATH_MAX];
1156
    char backing_format[16]; /* if non-zero and backing_file exists */
1157
1158
    QDict *full_open_options;
1159
    char exact_filename[PATH_MAX];
1160
1161
    /* I/O Limits */
1162
    BlockLimits bl;
1163
1164
    /*
1165
     * Flags honored during pread
1166
     */
1167
    BdrvRequestFlags supported_read_flags;
1168
    /*
1169
     * Flags honored during pwrite (so far: BDRV_REQ_FUA,
1170
     * BDRV_REQ_WRITE_UNCHANGED).
1171
     * If a driver does not support BDRV_REQ_WRITE_UNCHANGED, those
1172
     * writes will be issued as normal writes without the flag set.
1173
     * This is important to note for drivers that do not explicitly
1174
     * request a WRITE permission for their children and instead take
1175
     * the same permissions as their parent did (this is commonly what
1176
     * block filters do).  Such drivers have to be aware that the
1177
     * parent may have taken a WRITE_UNCHANGED permission only and is
1178
     * issuing such requests.  Drivers either must make sure that
1179
     * these requests do not result in plain WRITE accesses (usually
1180
     * by supporting BDRV_REQ_WRITE_UNCHANGED, and then forwarding
1181
     * every incoming write request as-is, including potentially that
1182
     * flag), or they have to explicitly take the WRITE permission for
1183
     * their children.
1184
     */
1185
    BdrvRequestFlags supported_write_flags;
1186
    /*
1187
     * Flags honored during pwrite_zeroes (so far: BDRV_REQ_FUA,
1188
     * BDRV_REQ_MAY_UNMAP, BDRV_REQ_WRITE_UNCHANGED)
1189
     */
1190
    BdrvRequestFlags supported_zero_flags;
1191
    /*
1192
     * Flags honoured during truncate (so far: BDRV_REQ_ZERO_WRITE).
1193
     *
1194
     * If BDRV_REQ_ZERO_WRITE is given, the truncate operation must make sure
1195
     * that any added space reads as all zeros. If this can't be guaranteed,
1196
     * the operation must fail.
1197
     */
1198
    BdrvRequestFlags supported_truncate_flags;
1199
1200
    /* the following member gives a name to every node on the bs graph. */
1201
    char node_name[32];
1202
    /* element of the list of named nodes building the graph */
1203
    QTAILQ_ENTRY(BlockDriverState) node_list;
1204
    /* element of the list of all BlockDriverStates (all_bdrv_states) */
1205
    QTAILQ_ENTRY(BlockDriverState) bs_list;
1206
    /* element of the list of monitor-owned BDS */
1207
    QTAILQ_ENTRY(BlockDriverState) monitor_list;
1208
    int refcnt;
1209
1210
    /* operation blockers. Protected by BQL. */
1211
    QLIST_HEAD(, BdrvOpBlocker) op_blockers[BLOCK_OP_TYPE_MAX];
1212
1213
    /*
1214
     * The node that this node inherited default options from (and a reopen on
1215
     * which can affect this node by changing these defaults). This is always a
1216
     * parent node of this node.
1217
     */
1218
    BlockDriverState *inherits_from;
1219
1220
    /*
1221
     * @backing and @file are some of @children or NULL. All these three fields
1222
     * (@file, @backing and @children) are modified only in
1223
     * bdrv_child_cb_attach() and bdrv_child_cb_detach().
1224
     *
1225
     * See also comment in include/block/block.h, to learn how backing and file
1226
     * are connected with BdrvChildRole.
1227
     */
1228
    QLIST_HEAD(, BdrvChild GRAPH_RDLOCK_PTR) children;
1229
    BdrvChild * GRAPH_RDLOCK_PTR backing;
1230
    BdrvChild * GRAPH_RDLOCK_PTR file;
1231
1232
    QLIST_HEAD(, BdrvChild GRAPH_RDLOCK_PTR) parents;
1233
1234
    QDict *options;
1235
    QDict *explicit_options;
1236
    BlockdevDetectZeroesOptions detect_zeroes;
1237
1238
    /* The error object in use for blocking operations on backing_hd */
1239
    Error *backing_blocker;
1240
1241
    /*
1242
     * If we are reading a disk image, give its size in sectors.
1243
     * Generally read-only; it is written to by load_snapshot and
1244
     * save_snaphost, but the block layer is quiescent during those.
1245
     */
1246
    int64_t total_sectors;
1247
1248
    /* threshold limit for writes, in bytes. "High water mark". */
1249
    uint64_t write_threshold_offset;
1250
1251
    /*
1252
     * Writing to the list requires the BQL _and_ the dirty_bitmap_mutex.
1253
     * Reading from the list can be done with either the BQL or the
1254
     * dirty_bitmap_mutex.  Modifying a bitmap only requires
1255
     * dirty_bitmap_mutex.
1256
     */
1257
    QemuMutex dirty_bitmap_mutex;
1258
    QLIST_HEAD(, BdrvDirtyBitmap) dirty_bitmaps;
1259
1260
    /* Offset after the highest byte written to */
1261
    uint64_t wr_highest_offset;
1262
1263
    /*
1264
     * If true, copy read backing sectors into image.  Can be >1 if more
1265
     * than one client has requested copy-on-read.  Accessed with atomic
1266
     * ops.
1267
     */
1268
    int copy_on_read;
1269
1270
    /*
1271
     * number of in-flight requests; overall and serialising.
1272
     * Accessed with atomic ops.
1273
     */
1274
    unsigned int in_flight;
1275
    unsigned int serialising_in_flight;
1276
1277
    /* do we need to tell the quest if we have a volatile write cache? */
1278
    int enable_write_cache;
1279
1280
    /* Accessed only in the main thread. */
1281
    int quiesce_counter;
1282
1283
    unsigned int write_gen;               /* Current data generation */
1284
1285
    /* Protected by reqs_lock.  */
1286
    QemuMutex reqs_lock;
1287
    QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
1288
    CoQueue flush_queue;                  /* Serializing flush queue */
1289
    bool active_flush_req;                /* Flush request in flight? */
1290
1291
    /* Only read/written by whoever has set active_flush_req to true.  */
1292
    unsigned int flushed_gen;             /* Flushed write generation */
1293
1294
    /* BdrvChild links to this node may never be frozen */
1295
    bool never_freeze;
1296
1297
    /* Lock for block-status cache RCU writers */
1298
    CoMutex bsc_modify_lock;
1299
    /* Always non-NULL, but must only be dereferenced under an RCU read guard */
1300
    BdrvBlockStatusCache *block_status_cache;
1301
1302
    /* array of write pointers' location of each zone in the zoned device. */
1303
    BlockZoneWps *wps;
1304
};
1305
1306
struct BlockBackendRootState {
1307
    int open_flags;
1308
    BlockdevDetectZeroesOptions detect_zeroes;
1309
};
1310
1311
typedef enum BlockMirrorBackingMode {
1312
    /*
1313
     * Reuse the existing backing chain from the source for the target.
1314
     * - sync=full: Set backing BDS to NULL.
1315
     * - sync=top:  Use source's backing BDS.
1316
     * - sync=none: Use source as the backing BDS.
1317
     */
1318
    MIRROR_SOURCE_BACKING_CHAIN,
1319
1320
    /* Open the target's backing chain completely anew */
1321
    MIRROR_OPEN_BACKING_CHAIN,
1322
1323
    /* Do not change the target's backing BDS after job completion */
1324
    MIRROR_LEAVE_BACKING_CHAIN,
1325
} BlockMirrorBackingMode;
1326
1327
1328
/*
1329
 * Essential block drivers which must always be statically linked into qemu, and
1330
 * which therefore can be accessed without using bdrv_find_format()
1331
 */
1332
extern BlockDriver bdrv_file;
1333
extern BlockDriver bdrv_raw;
1334
extern BlockDriver bdrv_qcow2;
1335
1336
extern unsigned int bdrv_drain_all_count;
1337
extern QemuOptsList bdrv_create_opts_simple;
1338
1339
/*
1340
 * Common functions that are neither I/O nor Global State.
1341
 *
1342
 * See include/block/block-common.h for more information about
1343
 * the Common API.
1344
 */
1345
1346
static inline BlockDriverState *child_bs(BdrvChild *child)
1347
66
{
1348
66
    return child ? child->bs : NULL;
1349
66
}
Unexecuted instantiation: block-dirty-bitmap.c:child_bs
block.c:child_bs
Line
Count
Source
1347
66
{
1348
66
    return child ? child->bs : NULL;
1349
66
}
Unexecuted instantiation: virtio-blk.c:child_bs
Unexecuted instantiation: blkreplay.c:child_bs
Unexecuted instantiation: qapi-system.c:child_bs
Unexecuted instantiation: block-hmp-cmds.c:child_bs
Unexecuted instantiation: qmp-cmds.c:child_bs
Unexecuted instantiation: server.c:child_bs
Unexecuted instantiation: blockdev.c:child_bs
Unexecuted instantiation: blockjob.c:child_bs
Unexecuted instantiation: qemu-io-cmds.c:child_bs
Unexecuted instantiation: accounting.c:child_bs
Unexecuted instantiation: amend.c:child_bs
Unexecuted instantiation: backup.c:child_bs
Unexecuted instantiation: blkdebug.c:child_bs
Unexecuted instantiation: blklogwrites.c:child_bs
Unexecuted instantiation: blkverify.c:child_bs
Unexecuted instantiation: block-backend.c:child_bs
Unexecuted instantiation: block-copy.c:child_bs
Unexecuted instantiation: commit.c:child_bs
Unexecuted instantiation: copy-before-write.c:child_bs
Unexecuted instantiation: copy-on-read.c:child_bs
Unexecuted instantiation: create.c:child_bs
Unexecuted instantiation: crypto.c:child_bs
Unexecuted instantiation: dirty-bitmap.c:child_bs
Unexecuted instantiation: filter-compress.c:child_bs
Unexecuted instantiation: graph-lock.c:child_bs
Unexecuted instantiation: io.c:child_bs
Unexecuted instantiation: mirror.c:child_bs
Unexecuted instantiation: nbd.c:child_bs
Unexecuted instantiation: null.c:child_bs
Unexecuted instantiation: preallocate.c:child_bs
Unexecuted instantiation: qapi.c:child_bs
Unexecuted instantiation: qcow2.c:child_bs
Unexecuted instantiation: qcow2-bitmap.c:child_bs
Unexecuted instantiation: qcow2-cache.c:child_bs
Unexecuted instantiation: qcow2-cluster.c:child_bs
Unexecuted instantiation: qcow2-refcount.c:child_bs
Unexecuted instantiation: qcow2-snapshot.c:child_bs
Unexecuted instantiation: qcow2-threads.c:child_bs
Unexecuted instantiation: quorum.c:child_bs
Unexecuted instantiation: raw-format.c:child_bs
Unexecuted instantiation: snapshot.c:child_bs
Unexecuted instantiation: snapshot-access.c:child_bs
Unexecuted instantiation: throttle.c:child_bs
Unexecuted instantiation: write-threshold.c:child_bs
Unexecuted instantiation: qcow.c:child_bs
Unexecuted instantiation: vdi.c:child_bs
Unexecuted instantiation: vhdx-endian.c:child_bs
Unexecuted instantiation: vhdx-log.c:child_bs
Unexecuted instantiation: vhdx.c:child_bs
Unexecuted instantiation: vmdk.c:child_bs
Unexecuted instantiation: vpc.c:child_bs
Unexecuted instantiation: cloop.c:child_bs
Unexecuted instantiation: bochs.c:child_bs
Unexecuted instantiation: vvfat.c:child_bs
Unexecuted instantiation: dmg.c:child_bs
Unexecuted instantiation: qed-check.c:child_bs
Unexecuted instantiation: qed-cluster.c:child_bs
Unexecuted instantiation: qed-l2-cache.c:child_bs
Unexecuted instantiation: qed-table.c:child_bs
Unexecuted instantiation: qed.c:child_bs
Unexecuted instantiation: parallels.c:child_bs
Unexecuted instantiation: parallels-ext.c:child_bs
Unexecuted instantiation: file-posix.c:child_bs
Unexecuted instantiation: nvme.c:child_bs
Unexecuted instantiation: replication.c:child_bs
Unexecuted instantiation: stream.c:child_bs
Unexecuted instantiation: bitmap-qmp-cmds.c:child_bs
Unexecuted instantiation: block-gen.c:child_bs
1350
1351
int bdrv_check_request(int64_t offset, int64_t bytes, Error **errp);
1352
char *create_tmp_file(Error **errp);
1353
void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix,
1354
                                      QDict *options);
1355
1356
1357
int bdrv_check_qiov_request(int64_t offset, int64_t bytes,
1358
                            QEMUIOVector *qiov, size_t qiov_offset,
1359
                            Error **errp);
1360
1361
#ifdef _WIN32
1362
int is_windows_drive(const char *filename);
1363
#endif
1364
1365
#endif /* BLOCK_INT_COMMON_H */