xref: /qemu/include/block/block_int-common.h (revision 70ce076fa6dff60585c229a4b641b13e64bf03cf)
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/aio.h"
28 #include "block/block-common.h"
29 #include "block/block-global-state.h"
30 #include "block/snapshot.h"
31 #include "qemu/clang-tsa.h"
32 #include "qemu/iov.h"
33 #include "qemu/rcu.h"
34 #include "qemu/stats64.h"
35 
36 #define BLOCK_FLAG_LAZY_REFCOUNTS   8
37 
38 #define BLOCK_OPT_SIZE              "size"
39 #define BLOCK_OPT_ENCRYPT           "encryption"
40 #define BLOCK_OPT_ENCRYPT_FORMAT    "encrypt.format"
41 #define BLOCK_OPT_COMPAT6           "compat6"
42 #define BLOCK_OPT_HWVERSION         "hwversion"
43 #define BLOCK_OPT_BACKING_FILE      "backing_file"
44 #define BLOCK_OPT_BACKING_FMT       "backing_fmt"
45 #define BLOCK_OPT_CLUSTER_SIZE      "cluster_size"
46 #define BLOCK_OPT_TABLE_SIZE        "table_size"
47 #define BLOCK_OPT_PREALLOC          "preallocation"
48 #define BLOCK_OPT_SUBFMT            "subformat"
49 #define BLOCK_OPT_COMPAT_LEVEL      "compat"
50 #define BLOCK_OPT_LAZY_REFCOUNTS    "lazy_refcounts"
51 #define BLOCK_OPT_ADAPTER_TYPE      "adapter_type"
52 #define BLOCK_OPT_REDUNDANCY        "redundancy"
53 #define BLOCK_OPT_NOCOW             "nocow"
54 #define BLOCK_OPT_EXTENT_SIZE_HINT  "extent_size_hint"
55 #define BLOCK_OPT_OBJECT_SIZE       "object_size"
56 #define BLOCK_OPT_REFCOUNT_BITS     "refcount_bits"
57 #define BLOCK_OPT_DATA_FILE         "data_file"
58 #define BLOCK_OPT_DATA_FILE_RAW     "data_file_raw"
59 #define BLOCK_OPT_COMPRESSION_TYPE  "compression_type"
60 #define BLOCK_OPT_EXTL2             "extended_l2"
61 
62 #define BLOCK_PROBE_BUF_SIZE        512
63 
64 enum BdrvTrackedRequestType {
65     BDRV_TRACKED_READ,
66     BDRV_TRACKED_WRITE,
67     BDRV_TRACKED_DISCARD,
68     BDRV_TRACKED_TRUNCATE,
69 };
70 
71 /*
72  * That is not quite good that BdrvTrackedRequest structure is public,
73  * as block/io.c is very careful about incoming offset/bytes being
74  * correct. Be sure to assert bdrv_check_request() succeeded after any
75  * modification of BdrvTrackedRequest object out of block/io.c
76  */
77 typedef struct BdrvTrackedRequest {
78     BlockDriverState *bs;
79     int64_t offset;
80     int64_t bytes;
81     enum BdrvTrackedRequestType type;
82 
83     bool serialising;
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 (*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     void GRAPH_WRLOCK_PTR (*bdrv_add_child)(
401         BlockDriverState *parent, BlockDriverState *child, Error **errp);
402 
403     void GRAPH_WRLOCK_PTR (*bdrv_del_child)(
404         BlockDriverState *parent, BdrvChild *child, Error **errp);
405 
406     /**
407      * Informs the block driver that a permission change is intended. The
408      * driver checks whether the change is permissible and may take other
409      * preparations for the change (e.g. get file system locks). This operation
410      * is always followed either by a call to either .bdrv_set_perm or
411      * .bdrv_abort_perm_update.
412      *
413      * Checks whether the requested set of cumulative permissions in @perm
414      * can be granted for accessing @bs and whether no other users are using
415      * permissions other than those given in @shared (both arguments take
416      * BLK_PERM_* bitmasks).
417      *
418      * If both conditions are met, 0 is returned. Otherwise, -errno is returned
419      * and errp is set to an error describing the conflict.
420      */
421     int GRAPH_RDLOCK_PTR (*bdrv_check_perm)(BlockDriverState *bs, uint64_t perm,
422                                             uint64_t shared, Error **errp);
423 
424     /**
425      * Called to inform the driver that the set of cumulative set of used
426      * permissions for @bs has changed to @perm, and the set of shareable
427      * permission to @shared. The driver can use this to propagate changes to
428      * its children (i.e. request permissions only if a parent actually needs
429      * them).
430      *
431      * This function is only invoked after bdrv_check_perm(), so block drivers
432      * may rely on preparations made in their .bdrv_check_perm implementation.
433      */
434     void GRAPH_RDLOCK_PTR (*bdrv_set_perm)(
435         BlockDriverState *bs, uint64_t perm, uint64_t shared);
436 
437     /*
438      * Called to inform the driver that after a previous bdrv_check_perm()
439      * call, the permission update is not performed and any preparations made
440      * for it (e.g. taken file locks) need to be undone.
441      *
442      * This function can be called even for nodes that never saw a
443      * bdrv_check_perm() call. It is a no-op then.
444      */
445     void GRAPH_RDLOCK_PTR (*bdrv_abort_perm_update)(BlockDriverState *bs);
446 
447     /**
448      * Returns in @nperm and @nshared the permissions that the driver for @bs
449      * needs on its child @c, based on the cumulative permissions requested by
450      * the parents in @parent_perm and @parent_shared.
451      *
452      * If @c is NULL, return the permissions for attaching a new child for the
453      * given @child_class and @role.
454      *
455      * If @reopen_queue is non-NULL, don't return the currently needed
456      * permissions, but those that will be needed after applying the
457      * @reopen_queue.
458      */
459      void GRAPH_RDLOCK_PTR (*bdrv_child_perm)(
460         BlockDriverState *bs, BdrvChild *c, BdrvChildRole role,
461         BlockReopenQueue *reopen_queue,
462         uint64_t parent_perm, uint64_t parent_shared,
463         uint64_t *nperm, uint64_t *nshared);
464 
465     /**
466      * Register/unregister a buffer for I/O. For example, when the driver is
467      * interested to know the memory areas that will later be used in iovs, so
468      * that it can do IOMMU mapping with VFIO etc., in order to get better
469      * performance. In the case of VFIO drivers, this callback is used to do
470      * DMA mapping for hot buffers.
471      *
472      * Returns: true on success, false on failure
473      */
474     bool GRAPH_RDLOCK_PTR (*bdrv_register_buf)(
475         BlockDriverState *bs, void *host, size_t size, Error **errp);
476     void GRAPH_RDLOCK_PTR (*bdrv_unregister_buf)(
477         BlockDriverState *bs, void *host, size_t size);
478 
479     /*
480      * This field is modified only under the BQL, and is part of
481      * the global state.
482      */
483     QLIST_ENTRY(BlockDriver) list;
484 
485     /*
486      * I/O API functions. These functions are thread-safe.
487      *
488      * See include/block/block-io.h for more information about
489      * the I/O API.
490      */
491 
492     int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
493 
494     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_amend)(
495         BlockDriverState *bs, BlockdevAmendOptions *opts, bool force,
496         Error **errp);
497 
498     /* aio */
499     BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_preadv)(BlockDriverState *bs,
500         int64_t offset, int64_t bytes, QEMUIOVector *qiov,
501         BdrvRequestFlags flags, BlockCompletionFunc *cb, void *opaque);
502 
503     BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_pwritev)(BlockDriverState *bs,
504         int64_t offset, int64_t bytes, QEMUIOVector *qiov,
505         BdrvRequestFlags flags, BlockCompletionFunc *cb, void *opaque);
506 
507     BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_flush)(
508         BlockDriverState *bs, BlockCompletionFunc *cb, void *opaque);
509 
510     BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_pdiscard)(
511         BlockDriverState *bs, int64_t offset, int bytes,
512         BlockCompletionFunc *cb, void *opaque);
513 
514     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_readv)(BlockDriverState *bs,
515         int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
516 
517     /**
518      * @offset: position in bytes to read at
519      * @bytes: number of bytes to read
520      * @qiov: the buffers to fill with read data
521      * @flags: currently unused, always 0
522      *
523      * @offset and @bytes will be a multiple of 'request_alignment',
524      * but the length of individual @qiov elements does not have to
525      * be a multiple.
526      *
527      * @bytes will always equal the total size of @qiov, and will be
528      * no larger than 'max_transfer'.
529      *
530      * The buffer in @qiov may point directly to guest memory.
531      */
532     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv)(BlockDriverState *bs,
533         int64_t offset, int64_t bytes, QEMUIOVector *qiov,
534         BdrvRequestFlags flags);
535 
536     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv_part)(
537         BlockDriverState *bs, int64_t offset, int64_t bytes,
538         QEMUIOVector *qiov, size_t qiov_offset,
539         BdrvRequestFlags flags);
540 
541     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_writev)(BlockDriverState *bs,
542         int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
543         int flags);
544     /**
545      * @offset: position in bytes to write at
546      * @bytes: number of bytes to write
547      * @qiov: the buffers containing data to write
548      * @flags: zero or more bits allowed by 'supported_write_flags'
549      *
550      * @offset and @bytes will be a multiple of 'request_alignment',
551      * but the length of individual @qiov elements does not have to
552      * be a multiple.
553      *
554      * @bytes will always equal the total size of @qiov, and will be
555      * no larger than 'max_transfer'.
556      *
557      * The buffer in @qiov may point directly to guest memory.
558      */
559     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev)(
560         BlockDriverState *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov,
561         BdrvRequestFlags flags);
562     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_part)(
563         BlockDriverState *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov,
564         size_t qiov_offset, BdrvRequestFlags flags);
565 
566     /*
567      * Efficiently zero a region of the disk image.  Typically an image format
568      * would use a compact metadata representation to implement this.  This
569      * function pointer may be NULL or return -ENOSUP and .bdrv_co_writev()
570      * will be called instead.
571      */
572     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwrite_zeroes)(
573         BlockDriverState *bs, int64_t offset, int64_t bytes,
574         BdrvRequestFlags flags);
575 
576     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pdiscard)(
577         BlockDriverState *bs, int64_t offset, int64_t bytes);
578 
579     /*
580      * Map [offset, offset + nbytes) range onto a child of @bs to copy from,
581      * and invoke bdrv_co_copy_range_from(child, ...), or invoke
582      * bdrv_co_copy_range_to() if @bs is the leaf child to copy data from.
583      *
584      * See the comment of bdrv_co_copy_range for the parameter and return value
585      * semantics.
586      */
587     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_copy_range_from)(
588         BlockDriverState *bs, BdrvChild *src, int64_t offset,
589         BdrvChild *dst, int64_t dst_offset, int64_t bytes,
590         BdrvRequestFlags read_flags, BdrvRequestFlags write_flags);
591 
592     /*
593      * Map [offset, offset + nbytes) range onto a child of bs to copy data to,
594      * and invoke bdrv_co_copy_range_to(child, src, ...), or perform the copy
595      * operation if @bs is the leaf and @src has the same BlockDriver.  Return
596      * -ENOTSUP if @bs is the leaf but @src has a different BlockDriver.
597      *
598      * See the comment of bdrv_co_copy_range for the parameter and return value
599      * semantics.
600      */
601     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_copy_range_to)(
602         BlockDriverState *bs, BdrvChild *src, int64_t src_offset,
603         BdrvChild *dst, int64_t dst_offset, int64_t bytes,
604         BdrvRequestFlags read_flags, BdrvRequestFlags write_flags);
605 
606     /*
607      * Building block for bdrv_block_status[_above] and
608      * bdrv_is_allocated[_above].  The driver should answer only
609      * according to the current layer, and should only need to set
610      * BDRV_BLOCK_DATA, BDRV_BLOCK_ZERO, BDRV_BLOCK_OFFSET_VALID,
611      * and/or BDRV_BLOCK_RAW; if the current layer defers to a backing
612      * layer, the result should be 0 (and not BDRV_BLOCK_ZERO).  See
613      * block.h for the overall meaning of the bits.  As a hint, the
614      * flag want_zero is true if the caller cares more about precise
615      * mappings (favor accurate _OFFSET_VALID/_ZERO) or false for
616      * overall allocation (favor larger *pnum, perhaps by reporting
617      * _DATA instead of _ZERO).  The block layer guarantees input
618      * clamped to bdrv_getlength() and aligned to request_alignment,
619      * as well as non-NULL pnum, map, and file; in turn, the driver
620      * must return an error or set pnum to an aligned non-zero value.
621      *
622      * Note that @bytes is just a hint on how big of a region the
623      * caller wants to inspect.  It is not a limit on *pnum.
624      * Implementations are free to return larger values of *pnum if
625      * doing so does not incur a performance penalty.
626      *
627      * block/io.c's bdrv_co_block_status() will utilize an unclamped
628      * *pnum value for the block-status cache on protocol nodes, prior
629      * to clamping *pnum for return to its caller.
630      */
631     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_block_status)(
632         BlockDriverState *bs,
633         bool want_zero, int64_t offset, int64_t bytes, int64_t *pnum,
634         int64_t *map, BlockDriverState **file);
635 
636     /*
637      * Snapshot-access API.
638      *
639      * Block-driver may provide snapshot-access API: special functions to access
640      * some internal "snapshot". The functions are similar with normal
641      * read/block_status/discard handler, but don't have any specific handling
642      * in generic block-layer: no serializing, no alignment, no tracked
643      * requests. So, block-driver that realizes these APIs is fully responsible
644      * for synchronization between snapshot-access API and normal IO requests.
645      *
646      * TODO: To be able to support qcow2's internal snapshots, this API will
647      * need to be extended to:
648      * - be able to select a specific snapshot
649      * - receive the snapshot's actual length (which may differ from bs's
650      *   length)
651      */
652     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv_snapshot)(
653         BlockDriverState *bs, int64_t offset, int64_t bytes,
654         QEMUIOVector *qiov, size_t qiov_offset);
655 
656     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_snapshot_block_status)(
657         BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes,
658         int64_t *pnum, int64_t *map, BlockDriverState **file);
659 
660     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pdiscard_snapshot)(
661         BlockDriverState *bs, int64_t offset, int64_t bytes);
662 
663     /*
664      * Invalidate any cached meta-data.
665      */
666     void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_invalidate_cache)(
667         BlockDriverState *bs, Error **errp);
668 
669     /*
670      * Flushes all data for all layers by calling bdrv_co_flush for underlying
671      * layers, if needed. This function is needed for deterministic
672      * synchronization of the flush finishing callback.
673      */
674     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush)(BlockDriverState *bs);
675 
676     /* Delete a created file. */
677     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_delete_file)(
678         BlockDriverState *bs, Error **errp);
679 
680     /*
681      * Flushes all data that was already written to the OS all the way down to
682      * the disk (for example file-posix.c calls fsync()).
683      */
684     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush_to_disk)(
685         BlockDriverState *bs);
686 
687     /*
688      * Flushes all internal caches to the OS. The data may still sit in a
689      * writeback cache of the host OS, but it will survive a crash of the qemu
690      * process.
691      */
692     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush_to_os)(
693         BlockDriverState *bs);
694 
695     /*
696      * Truncate @bs to @offset bytes using the given @prealloc mode
697      * when growing.  Modes other than PREALLOC_MODE_OFF should be
698      * rejected when shrinking @bs.
699      *
700      * If @exact is true, @bs must be resized to exactly @offset.
701      * Otherwise, it is sufficient for @bs (if it is a host block
702      * device and thus there is no way to resize it) to be at least
703      * @offset bytes in length.
704      *
705      * If @exact is true and this function fails but would succeed
706      * with @exact = false, it should return -ENOTSUP.
707      */
708     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_truncate)(
709         BlockDriverState *bs, int64_t offset, bool exact,
710         PreallocMode prealloc, BdrvRequestFlags flags, Error **errp);
711 
712     int64_t coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_getlength)(
713         BlockDriverState *bs);
714 
715     int64_t coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_get_allocated_file_size)(
716         BlockDriverState *bs);
717 
718     BlockMeasureInfo *(*bdrv_measure)(QemuOpts *opts, BlockDriverState *in_bs,
719                                       Error **errp);
720 
721     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_compressed)(
722         BlockDriverState *bs, int64_t offset, int64_t bytes,
723         QEMUIOVector *qiov);
724 
725     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_compressed_part)(
726         BlockDriverState *bs, int64_t offset, int64_t bytes,
727         QEMUIOVector *qiov, size_t qiov_offset);
728 
729     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_get_info)(
730         BlockDriverState *bs, BlockDriverInfo *bdi);
731 
732     ImageInfoSpecific * GRAPH_RDLOCK_PTR (*bdrv_get_specific_info)(
733         BlockDriverState *bs, Error **errp);
734     BlockStatsSpecific *(*bdrv_get_specific_stats)(BlockDriverState *bs);
735 
736     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_save_vmstate)(
737         BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
738 
739     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_load_vmstate)(
740         BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
741 
742     int coroutine_fn (*bdrv_co_zone_report)(BlockDriverState *bs,
743             int64_t offset, unsigned int *nr_zones,
744             BlockZoneDescriptor *zones);
745     int coroutine_fn (*bdrv_co_zone_mgmt)(BlockDriverState *bs, BlockZoneOp op,
746             int64_t offset, int64_t len);
747     int coroutine_fn (*bdrv_co_zone_append)(BlockDriverState *bs,
748             int64_t *offset, QEMUIOVector *qiov,
749             BdrvRequestFlags flags);
750 
751     /* removable device specific */
752     bool coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_is_inserted)(
753         BlockDriverState *bs);
754     void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_eject)(
755         BlockDriverState *bs, bool eject_flag);
756     void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_lock_medium)(
757         BlockDriverState *bs, bool locked);
758 
759     /* to control generic scsi devices */
760     BlockAIOCB *coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_aio_ioctl)(
761         BlockDriverState *bs, unsigned long int req, void *buf,
762         BlockCompletionFunc *cb, void *opaque);
763 
764     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_ioctl)(
765         BlockDriverState *bs, unsigned long int req, void *buf);
766 
767     /*
768      * Returns 0 for completed check, -errno for internal errors.
769      * The check results are stored in result.
770      */
771     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_check)(
772         BlockDriverState *bs, BdrvCheckResult *result, BdrvCheckMode fix);
773 
774     void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_debug_event)(
775         BlockDriverState *bs, BlkdebugEvent event);
776 
777     bool (*bdrv_supports_persistent_dirty_bitmap)(BlockDriverState *bs);
778 
779     bool coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_can_store_new_dirty_bitmap)(
780         BlockDriverState *bs, const char *name, uint32_t granularity,
781         Error **errp);
782 
783     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_remove_persistent_dirty_bitmap)(
784         BlockDriverState *bs, const char *name, Error **errp);
785 };
786 
787 static inline bool TSA_NO_TSA block_driver_can_compress(BlockDriver *drv)
788 {
789     return drv->bdrv_co_pwritev_compressed ||
790            drv->bdrv_co_pwritev_compressed_part;
791 }
792 
793 typedef struct BlockLimits {
794     /*
795      * Alignment requirement, in bytes, for offset/length of I/O
796      * requests. Must be a power of 2 less than INT_MAX; defaults to
797      * 1 for drivers with modern byte interfaces, and to 512
798      * otherwise.
799      */
800     uint32_t request_alignment;
801 
802     /*
803      * Maximum number of bytes that can be discarded at once. Must be multiple
804      * of pdiscard_alignment, but need not be power of 2. May be 0 if no
805      * inherent 64-bit limit.
806      */
807     int64_t max_pdiscard;
808 
809     /*
810      * Optimal alignment for discard requests in bytes. A power of 2
811      * is best but not mandatory.  Must be a multiple of
812      * bl.request_alignment, and must be less than max_pdiscard if
813      * that is set. May be 0 if bl.request_alignment is good enough
814      */
815     uint32_t pdiscard_alignment;
816 
817     /*
818      * Maximum number of bytes that can zeroized at once. Must be multiple of
819      * pwrite_zeroes_alignment. 0 means no limit.
820      */
821     int64_t max_pwrite_zeroes;
822 
823     /*
824      * Optimal alignment for write zeroes requests in bytes. A power
825      * of 2 is best but not mandatory.  Must be a multiple of
826      * bl.request_alignment, and must be less than max_pwrite_zeroes
827      * if that is set. May be 0 if bl.request_alignment is good
828      * enough
829      */
830     uint32_t pwrite_zeroes_alignment;
831 
832     /*
833      * Optimal transfer length in bytes.  A power of 2 is best but not
834      * mandatory.  Must be a multiple of bl.request_alignment, or 0 if
835      * no preferred size
836      */
837     uint32_t opt_transfer;
838 
839     /*
840      * Maximal transfer length in bytes.  Need not be power of 2, but
841      * must be multiple of opt_transfer and bl.request_alignment, or 0
842      * for no 32-bit limit.  For now, anything larger than INT_MAX is
843      * clamped down.
844      */
845     uint32_t max_transfer;
846 
847     /*
848      * Maximal hardware transfer length in bytes.  Applies whenever
849      * transfers to the device bypass the kernel I/O scheduler, for
850      * example with SG_IO.  If larger than max_transfer or if zero,
851      * blk_get_max_hw_transfer will fall back to max_transfer.
852      */
853     uint64_t max_hw_transfer;
854 
855     /*
856      * Maximal number of scatter/gather elements allowed by the hardware.
857      * Applies whenever transfers to the device bypass the kernel I/O
858      * scheduler, for example with SG_IO.  If larger than max_iov
859      * or if zero, blk_get_max_hw_iov will fall back to max_iov.
860      */
861     int max_hw_iov;
862 
863 
864     /* memory alignment, in bytes so that no bounce buffer is needed */
865     size_t min_mem_alignment;
866 
867     /* memory alignment, in bytes, for bounce buffer */
868     size_t opt_mem_alignment;
869 
870     /* maximum number of iovec elements */
871     int max_iov;
872 
873     /*
874      * true if the length of the underlying file can change, and QEMU
875      * is expected to adjust automatically.  Mostly for CD-ROM drives,
876      * whose length is zero when the tray is empty (they don't need
877      * an explicit monitor command to load the disk inside the guest).
878      */
879     bool has_variable_length;
880 
881     /* device zone model */
882     BlockZoneModel zoned;
883 
884     /* zone size expressed in bytes */
885     uint32_t zone_size;
886 
887     /* total number of zones */
888     uint32_t nr_zones;
889 
890     /* maximum sectors of a zone append write operation */
891     uint32_t max_append_sectors;
892 
893     /* maximum number of open zones */
894     uint32_t max_open_zones;
895 
896     /* maximum number of active zones */
897     uint32_t max_active_zones;
898 
899     uint32_t write_granularity;
900 } BlockLimits;
901 
902 typedef struct BdrvOpBlocker BdrvOpBlocker;
903 
904 typedef struct BdrvAioNotifier {
905     void (*attached_aio_context)(AioContext *new_context, void *opaque);
906     void (*detach_aio_context)(void *opaque);
907 
908     void *opaque;
909     bool deleted;
910 
911     QLIST_ENTRY(BdrvAioNotifier) list;
912 } BdrvAioNotifier;
913 
914 struct BdrvChildClass {
915     /*
916      * If true, bdrv_replace_node() doesn't change the node this BdrvChild
917      * points to.
918      */
919     bool stay_at_node;
920 
921     /*
922      * If true, the parent is a BlockDriverState and bdrv_next_all_states()
923      * will return it. This information is used for drain_all, where every node
924      * will be drained separately, so the drain only needs to be propagated to
925      * non-BDS parents.
926      */
927     bool parent_is_bds;
928 
929     /*
930      * Global state (GS) API. These functions run under the BQL.
931      *
932      * See include/block/block-global-state.h for more information about
933      * the GS API.
934      */
935     void (*inherit_options)(BdrvChildRole role, bool parent_is_format,
936                             int *child_flags, QDict *child_options,
937                             int parent_flags, QDict *parent_options);
938     void (*change_media)(BdrvChild *child, bool load);
939 
940     /*
941      * Returns a malloced string that describes the parent of the child for a
942      * human reader. This could be a node-name, BlockBackend name, qdev ID or
943      * QOM path of the device owning the BlockBackend, job type and ID etc. The
944      * caller is responsible for freeing the memory.
945      */
946     char *(*get_parent_desc)(BdrvChild *child);
947 
948     /*
949      * Notifies the parent that the child has been activated/inactivated (e.g.
950      * when migration is completing) and it can start/stop requesting
951      * permissions and doing I/O on it.
952      */
953     void GRAPH_RDLOCK_PTR (*activate)(BdrvChild *child, Error **errp);
954     int GRAPH_RDLOCK_PTR (*inactivate)(BdrvChild *child);
955 
956     void GRAPH_WRLOCK_PTR (*attach)(BdrvChild *child);
957     void GRAPH_WRLOCK_PTR (*detach)(BdrvChild *child);
958 
959     /*
960      * If this pair of functions is implemented, the parent doesn't issue new
961      * requests after returning from .drained_begin() until .drained_end() is
962      * called.
963      *
964      * These functions must not change the graph (and therefore also must not
965      * call aio_poll(), which could change the graph indirectly).
966      *
967      * Note that this can be nested. If drained_begin() was called twice, new
968      * I/O is allowed only after drained_end() was called twice, too.
969      */
970     void GRAPH_RDLOCK_PTR (*drained_begin)(BdrvChild *child);
971     void GRAPH_RDLOCK_PTR (*drained_end)(BdrvChild *child);
972 
973     /*
974      * Returns whether the parent has pending requests for the child. This
975      * callback is polled after .drained_begin() has been called until all
976      * activity on the child has stopped.
977      */
978     bool GRAPH_RDLOCK_PTR (*drained_poll)(BdrvChild *child);
979 
980     /*
981      * Notifies the parent that the filename of its child has changed (e.g.
982      * because the direct child was removed from the backing chain), so that it
983      * can update its reference.
984      */
985     int (*update_filename)(BdrvChild *child, BlockDriverState *new_base,
986                            const char *filename,
987                            bool backing_mask_protocol,
988                            Error **errp);
989 
990     bool (*change_aio_ctx)(BdrvChild *child, AioContext *ctx,
991                            GHashTable *visited, Transaction *tran,
992                            Error **errp);
993 
994     /*
995      * I/O API functions. These functions are thread-safe.
996      *
997      * See include/block/block-io.h for more information about
998      * the I/O API.
999      */
1000 
1001     void (*resize)(BdrvChild *child);
1002 
1003     /*
1004      * Returns a name that is supposedly more useful for human users than the
1005      * node name for identifying the node in question (in particular, a BB
1006      * name), or NULL if the parent can't provide a better name.
1007      */
1008     const char *(*get_name)(BdrvChild *child);
1009 
1010     AioContext *(*get_parent_aio_context)(BdrvChild *child);
1011 };
1012 
1013 extern const BdrvChildClass child_of_bds;
1014 
1015 struct BdrvChild {
1016     BlockDriverState *bs;
1017     char *name;
1018     const BdrvChildClass *klass;
1019     BdrvChildRole role;
1020     void *opaque;
1021 
1022     /**
1023      * Granted permissions for operating on this BdrvChild (BLK_PERM_* bitmask)
1024      */
1025     uint64_t perm;
1026 
1027     /**
1028      * Permissions that can still be granted to other users of @bs while this
1029      * BdrvChild is still attached to it. (BLK_PERM_* bitmask)
1030      */
1031     uint64_t shared_perm;
1032 
1033     /*
1034      * This link is frozen: the child can neither be replaced nor
1035      * detached from the parent.
1036      */
1037     bool frozen;
1038 
1039     /*
1040      * True if the parent of this child has been drained by this BdrvChild
1041      * (through klass->drained_*).
1042      *
1043      * It is generally true if bs->quiesce_counter > 0. It may differ while the
1044      * child is entering or leaving a drained section.
1045      */
1046     bool quiesced_parent;
1047 
1048     QLIST_ENTRY(BdrvChild GRAPH_RDLOCK_PTR) next;
1049     QLIST_ENTRY(BdrvChild GRAPH_RDLOCK_PTR) next_parent;
1050 };
1051 
1052 /*
1053  * Allows bdrv_co_block_status() to cache one data region for a
1054  * protocol node.
1055  *
1056  * @valid: Whether the cache is valid (should be accessed with atomic
1057  *         functions so this can be reset by RCU readers)
1058  * @data_start: Offset where we know (or strongly assume) is data
1059  * @data_end: Offset where the data region ends (which is not necessarily
1060  *            the start of a zeroed region)
1061  */
1062 typedef struct BdrvBlockStatusCache {
1063     struct rcu_head rcu;
1064 
1065     bool valid;
1066     int64_t data_start;
1067     int64_t data_end;
1068 } BdrvBlockStatusCache;
1069 
1070 struct BlockDriverState {
1071     /*
1072      * Protected by big QEMU lock or read-only after opening.  No special
1073      * locking needed during I/O...
1074      */
1075     int open_flags; /* flags used to open the file, re-used for re-open */
1076     bool encrypted; /* if true, the media is encrypted */
1077     bool sg;        /* if true, the device is a /dev/sg* */
1078     bool probed;    /* if true, format was probed rather than specified */
1079     bool force_share; /* if true, always allow all shared permissions */
1080     bool implicit;  /* if true, this filter node was automatically inserted */
1081 
1082     BlockDriver *drv; /* NULL means no media */
1083     void *opaque;
1084 
1085     AioContext *aio_context; /* event loop used for fd handlers, timers, etc */
1086     /*
1087      * long-running tasks intended to always use the same AioContext as this
1088      * BDS may register themselves in this list to be notified of changes
1089      * regarding this BDS's context
1090      */
1091     QLIST_HEAD(, BdrvAioNotifier) aio_notifiers;
1092     bool walking_aio_notifiers; /* to make removal during iteration safe */
1093 
1094     char filename[PATH_MAX];
1095     /*
1096      * If not empty, this image is a diff in relation to backing_file.
1097      * Note that this is the name given in the image header and
1098      * therefore may or may not be equal to .backing->bs->filename.
1099      * If this field contains a relative path, it is to be resolved
1100      * relatively to the overlay's location.
1101      */
1102     char backing_file[PATH_MAX];
1103     /*
1104      * The backing filename indicated by the image header.  Contrary
1105      * to backing_file, if we ever open this file, auto_backing_file
1106      * is replaced by the resulting BDS's filename (i.e. after a
1107      * bdrv_refresh_filename() run).
1108      */
1109     char auto_backing_file[PATH_MAX];
1110     char backing_format[16]; /* if non-zero and backing_file exists */
1111 
1112     QDict *full_open_options;
1113     char exact_filename[PATH_MAX];
1114 
1115     /* I/O Limits */
1116     BlockLimits bl;
1117 
1118     /*
1119      * Flags honored during pread
1120      */
1121     BdrvRequestFlags supported_read_flags;
1122     /*
1123      * Flags honored during pwrite (so far: BDRV_REQ_FUA,
1124      * BDRV_REQ_WRITE_UNCHANGED).
1125      * If a driver does not support BDRV_REQ_WRITE_UNCHANGED, those
1126      * writes will be issued as normal writes without the flag set.
1127      * This is important to note for drivers that do not explicitly
1128      * request a WRITE permission for their children and instead take
1129      * the same permissions as their parent did (this is commonly what
1130      * block filters do).  Such drivers have to be aware that the
1131      * parent may have taken a WRITE_UNCHANGED permission only and is
1132      * issuing such requests.  Drivers either must make sure that
1133      * these requests do not result in plain WRITE accesses (usually
1134      * by supporting BDRV_REQ_WRITE_UNCHANGED, and then forwarding
1135      * every incoming write request as-is, including potentially that
1136      * flag), or they have to explicitly take the WRITE permission for
1137      * their children.
1138      */
1139     BdrvRequestFlags supported_write_flags;
1140     /*
1141      * Flags honored during pwrite_zeroes (so far: BDRV_REQ_FUA,
1142      * BDRV_REQ_MAY_UNMAP, BDRV_REQ_WRITE_UNCHANGED)
1143      */
1144     BdrvRequestFlags supported_zero_flags;
1145     /*
1146      * Flags honoured during truncate (so far: BDRV_REQ_ZERO_WRITE).
1147      *
1148      * If BDRV_REQ_ZERO_WRITE is given, the truncate operation must make sure
1149      * that any added space reads as all zeros. If this can't be guaranteed,
1150      * the operation must fail.
1151      */
1152     BdrvRequestFlags supported_truncate_flags;
1153 
1154     /* the following member gives a name to every node on the bs graph. */
1155     char node_name[32];
1156     /* element of the list of named nodes building the graph */
1157     QTAILQ_ENTRY(BlockDriverState) node_list;
1158     /* element of the list of all BlockDriverStates (all_bdrv_states) */
1159     QTAILQ_ENTRY(BlockDriverState) bs_list;
1160     /* element of the list of monitor-owned BDS */
1161     QTAILQ_ENTRY(BlockDriverState) monitor_list;
1162     int refcnt;
1163 
1164     /* operation blockers. Protected by BQL. */
1165     QLIST_HEAD(, BdrvOpBlocker) op_blockers[BLOCK_OP_TYPE_MAX];
1166 
1167     /*
1168      * The node that this node inherited default options from (and a reopen on
1169      * which can affect this node by changing these defaults). This is always a
1170      * parent node of this node.
1171      */
1172     BlockDriverState *inherits_from;
1173 
1174     /*
1175      * @backing and @file are some of @children or NULL. All these three fields
1176      * (@file, @backing and @children) are modified only in
1177      * bdrv_child_cb_attach() and bdrv_child_cb_detach().
1178      *
1179      * See also comment in include/block/block.h, to learn how backing and file
1180      * are connected with BdrvChildRole.
1181      */
1182     QLIST_HEAD(, BdrvChild GRAPH_RDLOCK_PTR) children;
1183     BdrvChild * GRAPH_RDLOCK_PTR backing;
1184     BdrvChild * GRAPH_RDLOCK_PTR file;
1185 
1186     QLIST_HEAD(, BdrvChild GRAPH_RDLOCK_PTR) parents;
1187 
1188     QDict *options;
1189     QDict *explicit_options;
1190     BlockdevDetectZeroesOptions detect_zeroes;
1191 
1192     /* The error object in use for blocking operations on backing_hd */
1193     Error *backing_blocker;
1194 
1195     /*
1196      * If we are reading a disk image, give its size in sectors.
1197      * Generally read-only; it is written to by load_snapshot and
1198      * save_snaphost, but the block layer is quiescent during those.
1199      */
1200     int64_t total_sectors;
1201 
1202     /* threshold limit for writes, in bytes. "High water mark". */
1203     uint64_t write_threshold_offset;
1204 
1205     /*
1206      * Writing to the list requires the BQL _and_ the dirty_bitmap_mutex.
1207      * Reading from the list can be done with either the BQL or the
1208      * dirty_bitmap_mutex.  Modifying a bitmap only requires
1209      * dirty_bitmap_mutex.
1210      */
1211     QemuMutex dirty_bitmap_mutex;
1212     QLIST_HEAD(, BdrvDirtyBitmap) dirty_bitmaps;
1213 
1214     /* Offset after the highest byte written to */
1215     Stat64 wr_highest_offset;
1216 
1217     /*
1218      * If true, copy read backing sectors into image.  Can be >1 if more
1219      * than one client has requested copy-on-read.  Accessed with atomic
1220      * ops.
1221      */
1222     int copy_on_read;
1223 
1224     /*
1225      * number of in-flight requests; overall and serialising.
1226      * Accessed with atomic ops.
1227      */
1228     unsigned int in_flight;
1229     unsigned int serialising_in_flight;
1230 
1231     /* do we need to tell the quest if we have a volatile write cache? */
1232     int enable_write_cache;
1233 
1234     /* Accessed with atomic ops.  */
1235     int quiesce_counter;
1236 
1237     unsigned int write_gen;               /* Current data generation */
1238 
1239     /* Protected by reqs_lock.  */
1240     QemuMutex reqs_lock;
1241     QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
1242     CoQueue flush_queue;                  /* Serializing flush queue */
1243     bool active_flush_req;                /* Flush request in flight? */
1244 
1245     /* Only read/written by whoever has set active_flush_req to true.  */
1246     unsigned int flushed_gen;             /* Flushed write generation */
1247 
1248     /* BdrvChild links to this node may never be frozen */
1249     bool never_freeze;
1250 
1251     /* Lock for block-status cache RCU writers */
1252     CoMutex bsc_modify_lock;
1253     /* Always non-NULL, but must only be dereferenced under an RCU read guard */
1254     BdrvBlockStatusCache *block_status_cache;
1255 
1256     /* array of write pointers' location of each zone in the zoned device. */
1257     BlockZoneWps *wps;
1258 };
1259 
1260 struct BlockBackendRootState {
1261     int open_flags;
1262     BlockdevDetectZeroesOptions detect_zeroes;
1263 };
1264 
1265 typedef enum BlockMirrorBackingMode {
1266     /*
1267      * Reuse the existing backing chain from the source for the target.
1268      * - sync=full: Set backing BDS to NULL.
1269      * - sync=top:  Use source's backing BDS.
1270      * - sync=none: Use source as the backing BDS.
1271      */
1272     MIRROR_SOURCE_BACKING_CHAIN,
1273 
1274     /* Open the target's backing chain completely anew */
1275     MIRROR_OPEN_BACKING_CHAIN,
1276 
1277     /* Do not change the target's backing BDS after job completion */
1278     MIRROR_LEAVE_BACKING_CHAIN,
1279 } BlockMirrorBackingMode;
1280 
1281 
1282 /*
1283  * Essential block drivers which must always be statically linked into qemu, and
1284  * which therefore can be accessed without using bdrv_find_format()
1285  */
1286 extern BlockDriver bdrv_file;
1287 extern BlockDriver bdrv_raw;
1288 extern BlockDriver bdrv_qcow2;
1289 
1290 extern unsigned int bdrv_drain_all_count;
1291 extern QemuOptsList bdrv_create_opts_simple;
1292 
1293 /*
1294  * Common functions that are neither I/O nor Global State.
1295  *
1296  * See include/block/block-common.h for more information about
1297  * the Common API.
1298  */
1299 
1300 static inline BlockDriverState *child_bs(BdrvChild *child)
1301 {
1302     return child ? child->bs : NULL;
1303 }
1304 
1305 int bdrv_check_request(int64_t offset, int64_t bytes, Error **errp);
1306 char *create_tmp_file(Error **errp);
1307 void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix,
1308                                       QDict *options);
1309 
1310 
1311 int bdrv_check_qiov_request(int64_t offset, int64_t bytes,
1312                             QEMUIOVector *qiov, size_t qiov_offset,
1313                             Error **errp);
1314 
1315 #ifdef _WIN32
1316 int is_windows_drive(const char *filename);
1317 #endif
1318 
1319 #endif /* BLOCK_INT_COMMON_H */
1320