xref: /qemu/include/block/block_int-common.h (revision 06717986)
1ebc2752bSEmanuele Giuseppe Esposito /*
2ebc2752bSEmanuele Giuseppe Esposito  * QEMU System Emulator block driver
3ebc2752bSEmanuele Giuseppe Esposito  *
4ebc2752bSEmanuele Giuseppe Esposito  * Copyright (c) 2003 Fabrice Bellard
5ebc2752bSEmanuele Giuseppe Esposito  *
6ebc2752bSEmanuele Giuseppe Esposito  * Permission is hereby granted, free of charge, to any person obtaining a copy
7ebc2752bSEmanuele Giuseppe Esposito  * of this software and associated documentation files (the "Software"), to deal
8ebc2752bSEmanuele Giuseppe Esposito  * in the Software without restriction, including without limitation the rights
9ebc2752bSEmanuele Giuseppe Esposito  * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10ebc2752bSEmanuele Giuseppe Esposito  * copies of the Software, and to permit persons to whom the Software is
11ebc2752bSEmanuele Giuseppe Esposito  * furnished to do so, subject to the following conditions:
12ebc2752bSEmanuele Giuseppe Esposito  *
13ebc2752bSEmanuele Giuseppe Esposito  * The above copyright notice and this permission notice shall be included in
14ebc2752bSEmanuele Giuseppe Esposito  * all copies or substantial portions of the Software.
15ebc2752bSEmanuele Giuseppe Esposito  *
16ebc2752bSEmanuele Giuseppe Esposito  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17ebc2752bSEmanuele Giuseppe Esposito  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18ebc2752bSEmanuele Giuseppe Esposito  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
19ebc2752bSEmanuele Giuseppe Esposito  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20ebc2752bSEmanuele Giuseppe Esposito  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21ebc2752bSEmanuele Giuseppe Esposito  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22ebc2752bSEmanuele Giuseppe Esposito  * THE SOFTWARE.
23ebc2752bSEmanuele Giuseppe Esposito  */
24ebc2752bSEmanuele Giuseppe Esposito #ifndef BLOCK_INT_COMMON_H
25ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_INT_COMMON_H
26ebc2752bSEmanuele Giuseppe Esposito 
27e2c1c34fSMarkus Armbruster #include "block/aio.h"
28e2c1c34fSMarkus Armbruster #include "block/block-common.h"
29e2c1c34fSMarkus Armbruster #include "block/block-global-state.h"
30ebc2752bSEmanuele Giuseppe Esposito #include "block/snapshot.h"
31e2c1c34fSMarkus Armbruster #include "qemu/iov.h"
32ebc2752bSEmanuele Giuseppe Esposito #include "qemu/rcu.h"
33e2c1c34fSMarkus Armbruster #include "qemu/stats64.h"
34ebc2752bSEmanuele Giuseppe Esposito 
35ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_FLAG_LAZY_REFCOUNTS   8
36ebc2752bSEmanuele Giuseppe Esposito 
37ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_SIZE              "size"
38ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_ENCRYPT           "encryption"
39ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_ENCRYPT_FORMAT    "encrypt.format"
40ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_COMPAT6           "compat6"
41ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_HWVERSION         "hwversion"
42ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_BACKING_FILE      "backing_file"
43ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_BACKING_FMT       "backing_fmt"
44ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_CLUSTER_SIZE      "cluster_size"
45ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_TABLE_SIZE        "table_size"
46ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_PREALLOC          "preallocation"
47ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_SUBFMT            "subformat"
48ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_COMPAT_LEVEL      "compat"
49ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_LAZY_REFCOUNTS    "lazy_refcounts"
50ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_ADAPTER_TYPE      "adapter_type"
51ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_REDUNDANCY        "redundancy"
52ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_NOCOW             "nocow"
53ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_EXTENT_SIZE_HINT  "extent_size_hint"
54ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_OBJECT_SIZE       "object_size"
55ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_REFCOUNT_BITS     "refcount_bits"
56ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_DATA_FILE         "data_file"
57ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_DATA_FILE_RAW     "data_file_raw"
58ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_COMPRESSION_TYPE  "compression_type"
59ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_OPT_EXTL2             "extended_l2"
60ebc2752bSEmanuele Giuseppe Esposito 
61ebc2752bSEmanuele Giuseppe Esposito #define BLOCK_PROBE_BUF_SIZE        512
62ebc2752bSEmanuele Giuseppe Esposito 
63ebc2752bSEmanuele Giuseppe Esposito enum BdrvTrackedRequestType {
64ebc2752bSEmanuele Giuseppe Esposito     BDRV_TRACKED_READ,
65ebc2752bSEmanuele Giuseppe Esposito     BDRV_TRACKED_WRITE,
66ebc2752bSEmanuele Giuseppe Esposito     BDRV_TRACKED_DISCARD,
67ebc2752bSEmanuele Giuseppe Esposito     BDRV_TRACKED_TRUNCATE,
68ebc2752bSEmanuele Giuseppe Esposito };
69ebc2752bSEmanuele Giuseppe Esposito 
70ebc2752bSEmanuele Giuseppe Esposito /*
71ebc2752bSEmanuele Giuseppe Esposito  * That is not quite good that BdrvTrackedRequest structure is public,
72ebc2752bSEmanuele Giuseppe Esposito  * as block/io.c is very careful about incoming offset/bytes being
73ebc2752bSEmanuele Giuseppe Esposito  * correct. Be sure to assert bdrv_check_request() succeeded after any
74ebc2752bSEmanuele Giuseppe Esposito  * modification of BdrvTrackedRequest object out of block/io.c
75ebc2752bSEmanuele Giuseppe Esposito  */
76ebc2752bSEmanuele Giuseppe Esposito typedef struct BdrvTrackedRequest {
77ebc2752bSEmanuele Giuseppe Esposito     BlockDriverState *bs;
78ebc2752bSEmanuele Giuseppe Esposito     int64_t offset;
79ebc2752bSEmanuele Giuseppe Esposito     int64_t bytes;
80ebc2752bSEmanuele Giuseppe Esposito     enum BdrvTrackedRequestType type;
81ebc2752bSEmanuele Giuseppe Esposito 
82ebc2752bSEmanuele Giuseppe Esposito     bool serialising;
83ebc2752bSEmanuele Giuseppe Esposito     int64_t overlap_offset;
84ebc2752bSEmanuele Giuseppe Esposito     int64_t overlap_bytes;
85ebc2752bSEmanuele Giuseppe Esposito 
86ebc2752bSEmanuele Giuseppe Esposito     QLIST_ENTRY(BdrvTrackedRequest) list;
87ebc2752bSEmanuele Giuseppe Esposito     Coroutine *co; /* owner, used for deadlock detection */
88ebc2752bSEmanuele Giuseppe Esposito     CoQueue wait_queue; /* coroutines blocked on this request */
89ebc2752bSEmanuele Giuseppe Esposito 
90ebc2752bSEmanuele Giuseppe Esposito     struct BdrvTrackedRequest *waiting_for;
91ebc2752bSEmanuele Giuseppe Esposito } BdrvTrackedRequest;
92ebc2752bSEmanuele Giuseppe Esposito 
93ebc2752bSEmanuele Giuseppe Esposito 
94ebc2752bSEmanuele Giuseppe Esposito struct BlockDriver {
9569c0bf11SEmanuele Giuseppe Esposito     /*
9669c0bf11SEmanuele Giuseppe Esposito      * These fields are initialized when this object is created,
9769c0bf11SEmanuele Giuseppe Esposito      * and are never changed afterwards.
9869c0bf11SEmanuele Giuseppe Esposito      */
9969c0bf11SEmanuele Giuseppe Esposito 
100ebc2752bSEmanuele Giuseppe Esposito     const char *format_name;
101ebc2752bSEmanuele Giuseppe Esposito     int instance_size;
102ebc2752bSEmanuele Giuseppe Esposito 
103ebc2752bSEmanuele Giuseppe Esposito     /*
104ebc2752bSEmanuele Giuseppe Esposito      * Set to true if the BlockDriver is a block filter. Block filters pass
105ebc2752bSEmanuele Giuseppe Esposito      * certain callbacks that refer to data (see block.c) to their bs->file
106ebc2752bSEmanuele Giuseppe Esposito      * or bs->backing (whichever one exists) if the driver doesn't implement
107ebc2752bSEmanuele Giuseppe Esposito      * them. Drivers that do not wish to forward must implement them and return
108ebc2752bSEmanuele Giuseppe Esposito      * -ENOTSUP.
109ebc2752bSEmanuele Giuseppe Esposito      * Note that filters are not allowed to modify data.
110ebc2752bSEmanuele Giuseppe Esposito      *
111ebc2752bSEmanuele Giuseppe Esposito      * Filters generally cannot have more than a single filtered child,
112ebc2752bSEmanuele Giuseppe Esposito      * because the data they present must at all times be the same as
113ebc2752bSEmanuele Giuseppe Esposito      * that on their filtered child.  That would be impossible to
114ebc2752bSEmanuele Giuseppe Esposito      * achieve for multiple filtered children.
115ebc2752bSEmanuele Giuseppe Esposito      * (And this filtered child must then be bs->file or bs->backing.)
116ebc2752bSEmanuele Giuseppe Esposito      */
117ebc2752bSEmanuele Giuseppe Esposito     bool is_filter;
118ebc2752bSEmanuele Giuseppe Esposito     /*
119046fd84fSVladimir Sementsov-Ogievskiy      * Only make sense for filter drivers, for others must be false.
120046fd84fSVladimir Sementsov-Ogievskiy      * If true, filtered child is bs->backing. Otherwise it's bs->file.
1211921b4f7SVladimir Sementsov-Ogievskiy      * Two internal filters use bs->backing as filtered child and has this
1221921b4f7SVladimir Sementsov-Ogievskiy      * field set to true: mirror_top and commit_top. There also two such test
1231921b4f7SVladimir Sementsov-Ogievskiy      * filters in tests/unit/test-bdrv-graph-mod.c.
124046fd84fSVladimir Sementsov-Ogievskiy      *
125046fd84fSVladimir Sementsov-Ogievskiy      * Never create any more such filters!
126046fd84fSVladimir Sementsov-Ogievskiy      *
127046fd84fSVladimir Sementsov-Ogievskiy      * TODO: imagine how to deprecate this behavior and make all filters work
128046fd84fSVladimir Sementsov-Ogievskiy      * similarly using bs->file as filtered child.
129046fd84fSVladimir Sementsov-Ogievskiy      */
130046fd84fSVladimir Sementsov-Ogievskiy     bool filtered_child_is_backing;
131046fd84fSVladimir Sementsov-Ogievskiy 
132046fd84fSVladimir Sementsov-Ogievskiy     /*
133ebc2752bSEmanuele Giuseppe Esposito      * Set to true if the BlockDriver is a format driver.  Format nodes
134ebc2752bSEmanuele Giuseppe Esposito      * generally do not expect their children to be other format nodes
135ebc2752bSEmanuele Giuseppe Esposito      * (except for backing files), and so format probing is disabled
136ebc2752bSEmanuele Giuseppe Esposito      * on those children.
137ebc2752bSEmanuele Giuseppe Esposito      */
138ebc2752bSEmanuele Giuseppe Esposito     bool is_format;
139ebc2752bSEmanuele Giuseppe Esposito 
140ebc2752bSEmanuele Giuseppe Esposito     /*
141774c726cSSam Li      * Set to true if the BlockDriver supports zoned children.
142774c726cSSam Li      */
143774c726cSSam Li     bool supports_zoned_children;
144774c726cSSam Li 
145774c726cSSam Li     /*
146ebc2752bSEmanuele Giuseppe Esposito      * Drivers not implementing bdrv_parse_filename nor bdrv_open should have
147ebc2752bSEmanuele Giuseppe Esposito      * this field set to true, except ones that are defined only by their
148ebc2752bSEmanuele Giuseppe Esposito      * child's bs.
149ebc2752bSEmanuele Giuseppe Esposito      * An example of the last type will be the quorum block driver.
150ebc2752bSEmanuele Giuseppe Esposito      */
151ebc2752bSEmanuele Giuseppe Esposito     bool bdrv_needs_filename;
152ebc2752bSEmanuele Giuseppe Esposito 
153ebc2752bSEmanuele Giuseppe Esposito     /*
154ebc2752bSEmanuele Giuseppe Esposito      * Set if a driver can support backing files. This also implies the
155ebc2752bSEmanuele Giuseppe Esposito      * following semantics:
156ebc2752bSEmanuele Giuseppe Esposito      *
157ebc2752bSEmanuele Giuseppe Esposito      *  - Return status 0 of .bdrv_co_block_status means that corresponding
158ebc2752bSEmanuele Giuseppe Esposito      *    blocks are not allocated in this layer of backing-chain
159ebc2752bSEmanuele Giuseppe Esposito      *  - For such (unallocated) blocks, read will:
160ebc2752bSEmanuele Giuseppe Esposito      *    - fill buffer with zeros if there is no backing file
161ebc2752bSEmanuele Giuseppe Esposito      *    - read from the backing file otherwise, where the block layer
162ebc2752bSEmanuele Giuseppe Esposito      *      takes care of reading zeros beyond EOF if backing file is short
163ebc2752bSEmanuele Giuseppe Esposito      */
164ebc2752bSEmanuele Giuseppe Esposito     bool supports_backing;
165ebc2752bSEmanuele Giuseppe Esposito 
16669c0bf11SEmanuele Giuseppe Esposito     /*
16769c0bf11SEmanuele Giuseppe Esposito      * Drivers setting this field must be able to work with just a plain
16869c0bf11SEmanuele Giuseppe Esposito      * filename with '<protocol_name>:' as a prefix, and no other options.
16969c0bf11SEmanuele Giuseppe Esposito      * Options may be extracted from the filename by implementing
17069c0bf11SEmanuele Giuseppe Esposito      * bdrv_parse_filename.
17169c0bf11SEmanuele Giuseppe Esposito      */
17269c0bf11SEmanuele Giuseppe Esposito     const char *protocol_name;
17369c0bf11SEmanuele Giuseppe Esposito 
17469c0bf11SEmanuele Giuseppe Esposito     /* List of options for creating images, terminated by name == NULL */
17569c0bf11SEmanuele Giuseppe Esposito     QemuOptsList *create_opts;
17669c0bf11SEmanuele Giuseppe Esposito 
17769c0bf11SEmanuele Giuseppe Esposito     /* List of options for image amend */
17869c0bf11SEmanuele Giuseppe Esposito     QemuOptsList *amend_opts;
17969c0bf11SEmanuele Giuseppe Esposito 
18069c0bf11SEmanuele Giuseppe Esposito     /*
18169c0bf11SEmanuele Giuseppe Esposito      * If this driver supports reopening images this contains a
18269c0bf11SEmanuele Giuseppe Esposito      * NULL-terminated list of the runtime options that can be
18369c0bf11SEmanuele Giuseppe Esposito      * modified. If an option in this list is unspecified during
18469c0bf11SEmanuele Giuseppe Esposito      * reopen then it _must_ be reset to its default value or return
18569c0bf11SEmanuele Giuseppe Esposito      * an error.
18669c0bf11SEmanuele Giuseppe Esposito      */
18769c0bf11SEmanuele Giuseppe Esposito     const char *const *mutable_opts;
18869c0bf11SEmanuele Giuseppe Esposito 
18969c0bf11SEmanuele Giuseppe Esposito     /*
19069c0bf11SEmanuele Giuseppe Esposito      * Pointer to a NULL-terminated array of names of strong options
19169c0bf11SEmanuele Giuseppe Esposito      * that can be specified for bdrv_open(). A strong option is one
19269c0bf11SEmanuele Giuseppe Esposito      * that changes the data of a BDS.
19369c0bf11SEmanuele Giuseppe Esposito      * If this pointer is NULL, the array is considered empty.
19469c0bf11SEmanuele Giuseppe Esposito      * "filename" and "driver" are always considered strong.
19569c0bf11SEmanuele Giuseppe Esposito      */
19669c0bf11SEmanuele Giuseppe Esposito     const char *const *strong_runtime_opts;
19769c0bf11SEmanuele Giuseppe Esposito 
19869c0bf11SEmanuele Giuseppe Esposito 
19969c0bf11SEmanuele Giuseppe Esposito     /*
20069c0bf11SEmanuele Giuseppe Esposito      * Global state (GS) API. These functions run under the BQL.
20169c0bf11SEmanuele Giuseppe Esposito      *
20269c0bf11SEmanuele Giuseppe Esposito      * See include/block/block-global-state.h for more information about
20369c0bf11SEmanuele Giuseppe Esposito      * the GS API.
20469c0bf11SEmanuele Giuseppe Esposito      */
20569c0bf11SEmanuele Giuseppe Esposito 
20669c0bf11SEmanuele Giuseppe Esposito     /*
20769c0bf11SEmanuele Giuseppe Esposito      * This function is invoked under BQL before .bdrv_co_amend()
20869c0bf11SEmanuele Giuseppe Esposito      * (which in contrast does not necessarily run under the BQL)
20969c0bf11SEmanuele Giuseppe Esposito      * to allow driver-specific initialization code that requires
21069c0bf11SEmanuele Giuseppe Esposito      * the BQL, like setting up specific permission flags.
21169c0bf11SEmanuele Giuseppe Esposito      */
212840428a2SEmanuele Giuseppe Esposito     int GRAPH_RDLOCK_PTR (*bdrv_amend_pre_run)(
213840428a2SEmanuele Giuseppe Esposito         BlockDriverState *bs, Error **errp);
21469c0bf11SEmanuele Giuseppe Esposito     /*
21569c0bf11SEmanuele Giuseppe Esposito      * This function is invoked under BQL after .bdrv_co_amend()
21669c0bf11SEmanuele Giuseppe Esposito      * to allow cleaning up what was done in .bdrv_amend_pre_run().
21769c0bf11SEmanuele Giuseppe Esposito      */
218840428a2SEmanuele Giuseppe Esposito     void GRAPH_RDLOCK_PTR (*bdrv_amend_clean)(BlockDriverState *bs);
21969c0bf11SEmanuele Giuseppe Esposito 
22069c0bf11SEmanuele Giuseppe Esposito     /*
22169c0bf11SEmanuele Giuseppe Esposito      * Return true if @to_replace can be replaced by a BDS with the
22269c0bf11SEmanuele Giuseppe Esposito      * same data as @bs without it affecting @bs's behavior (that is,
22369c0bf11SEmanuele Giuseppe Esposito      * without it being visible to @bs's parents).
22469c0bf11SEmanuele Giuseppe Esposito      */
225533c6e4eSKevin Wolf     bool GRAPH_RDLOCK_PTR (*bdrv_recurse_can_replace)(
226533c6e4eSKevin Wolf         BlockDriverState *bs, BlockDriverState *to_replace);
22769c0bf11SEmanuele Giuseppe Esposito 
22869c0bf11SEmanuele Giuseppe Esposito     int (*bdrv_probe_device)(const char *filename);
22969c0bf11SEmanuele Giuseppe Esposito 
23069c0bf11SEmanuele Giuseppe Esposito     /*
23169c0bf11SEmanuele Giuseppe Esposito      * Any driver implementing this callback is expected to be able to handle
23269c0bf11SEmanuele Giuseppe Esposito      * NULL file names in its .bdrv_open() implementation.
23369c0bf11SEmanuele Giuseppe Esposito      */
23469c0bf11SEmanuele Giuseppe Esposito     void (*bdrv_parse_filename)(const char *filename, QDict *options,
23569c0bf11SEmanuele Giuseppe Esposito                                 Error **errp);
23669c0bf11SEmanuele Giuseppe Esposito 
23769c0bf11SEmanuele Giuseppe Esposito     /* For handling image reopen for split or non-split files. */
238ce433d29SKevin Wolf     int GRAPH_UNLOCKED_PTR (*bdrv_reopen_prepare)(
239ce433d29SKevin Wolf         BDRVReopenState *reopen_state, BlockReopenQueue *queue, Error **errp);
240ce433d29SKevin Wolf     void GRAPH_UNLOCKED_PTR (*bdrv_reopen_commit)(
241ce433d29SKevin Wolf         BDRVReopenState *reopen_state);
242ce433d29SKevin Wolf     void GRAPH_UNLOCKED_PTR (*bdrv_reopen_commit_post)(
243ce433d29SKevin Wolf         BDRVReopenState *reopen_state);
244ce433d29SKevin Wolf     void GRAPH_UNLOCKED_PTR (*bdrv_reopen_abort)(
245ce433d29SKevin Wolf         BDRVReopenState *reopen_state);
246ebc2752bSEmanuele Giuseppe Esposito     void (*bdrv_join_options)(QDict *options, QDict *old_options);
247ebc2752bSEmanuele Giuseppe Esposito 
2481a30b0f5SKevin Wolf     int GRAPH_UNLOCKED_PTR (*bdrv_open)(
2491a30b0f5SKevin Wolf         BlockDriverState *bs, QDict *options, int flags, Error **errp);
250ebc2752bSEmanuele Giuseppe Esposito 
251ebc2752bSEmanuele Giuseppe Esposito     /* Protocol drivers should implement this instead of bdrv_open */
2521a30b0f5SKevin Wolf     int GRAPH_UNLOCKED_PTR (*bdrv_file_open)(
2531a30b0f5SKevin Wolf         BlockDriverState *bs, QDict *options, int flags, Error **errp);
254ebc2752bSEmanuele Giuseppe Esposito     void (*bdrv_close)(BlockDriverState *bs);
255ebc2752bSEmanuele Giuseppe Esposito 
2564db7ba3bSKevin Wolf     int coroutine_fn GRAPH_UNLOCKED_PTR (*bdrv_co_create)(
2574ec8df01SKevin Wolf         BlockdevCreateOptions *opts, Error **errp);
2584ec8df01SKevin Wolf 
2594db7ba3bSKevin Wolf     int coroutine_fn GRAPH_UNLOCKED_PTR (*bdrv_co_create_opts)(
2604ec8df01SKevin Wolf         BlockDriver *drv, const char *filename, QemuOpts *opts, Error **errp);
261ebc2752bSEmanuele Giuseppe Esposito 
262bd131d67SKevin Wolf     int GRAPH_RDLOCK_PTR (*bdrv_amend_options)(
263bd131d67SKevin Wolf         BlockDriverState *bs, QemuOpts *opts,
264bd131d67SKevin Wolf         BlockDriverAmendStatusCB *status_cb, void *cb_opaque,
265bd131d67SKevin Wolf         bool force, Error **errp);
266ebc2752bSEmanuele Giuseppe Esposito 
2670bb79c97SKevin Wolf     int GRAPH_RDLOCK_PTR (*bdrv_make_empty)(BlockDriverState *bs);
268ebc2752bSEmanuele Giuseppe Esposito 
269ebc2752bSEmanuele Giuseppe Esposito     /*
270ebc2752bSEmanuele Giuseppe Esposito      * Refreshes the bs->exact_filename field. If that is impossible,
271ebc2752bSEmanuele Giuseppe Esposito      * bs->exact_filename has to be left empty.
272ebc2752bSEmanuele Giuseppe Esposito      */
273b7cfc7d5SKevin Wolf     void GRAPH_RDLOCK_PTR (*bdrv_refresh_filename)(BlockDriverState *bs);
274ebc2752bSEmanuele Giuseppe Esposito 
275ebc2752bSEmanuele Giuseppe Esposito     /*
276ebc2752bSEmanuele Giuseppe Esposito      * Gathers the open options for all children into @target.
277ebc2752bSEmanuele Giuseppe Esposito      * A simple format driver (without backing file support) might
278ebc2752bSEmanuele Giuseppe Esposito      * implement this function like this:
279ebc2752bSEmanuele Giuseppe Esposito      *
280ebc2752bSEmanuele Giuseppe Esposito      *     QINCREF(bs->file->bs->full_open_options);
281ebc2752bSEmanuele Giuseppe Esposito      *     qdict_put(target, "file", bs->file->bs->full_open_options);
282ebc2752bSEmanuele Giuseppe Esposito      *
283ebc2752bSEmanuele Giuseppe Esposito      * If not specified, the generic implementation will simply put
284ebc2752bSEmanuele Giuseppe Esposito      * all children's options under their respective name.
285ebc2752bSEmanuele Giuseppe Esposito      *
286ebc2752bSEmanuele Giuseppe Esposito      * @backing_overridden is true when bs->backing seems not to be
287ebc2752bSEmanuele Giuseppe Esposito      * the child that would result from opening bs->backing_file.
288ebc2752bSEmanuele Giuseppe Esposito      * Therefore, if it is true, the backing child's options should be
289ebc2752bSEmanuele Giuseppe Esposito      * gathered; otherwise, there is no need since the backing child
290ebc2752bSEmanuele Giuseppe Esposito      * is the one implied by the image header.
291ebc2752bSEmanuele Giuseppe Esposito      *
292ebc2752bSEmanuele Giuseppe Esposito      * Note that ideally this function would not be needed.  Every
293ebc2752bSEmanuele Giuseppe Esposito      * block driver which implements it is probably doing something
294ebc2752bSEmanuele Giuseppe Esposito      * shady regarding its runtime option structure.
295ebc2752bSEmanuele Giuseppe Esposito      */
296b7cfc7d5SKevin Wolf     void GRAPH_RDLOCK_PTR (*bdrv_gather_child_options)(
297b7cfc7d5SKevin Wolf         BlockDriverState *bs, QDict *target, bool backing_overridden);
298ebc2752bSEmanuele Giuseppe Esposito 
299ebc2752bSEmanuele Giuseppe Esposito     /*
300ebc2752bSEmanuele Giuseppe Esposito      * Returns an allocated string which is the directory name of this BDS: It
301ebc2752bSEmanuele Giuseppe Esposito      * will be used to make relative filenames absolute by prepending this
302ebc2752bSEmanuele Giuseppe Esposito      * function's return value to them.
303ebc2752bSEmanuele Giuseppe Esposito      */
304b7cfc7d5SKevin Wolf     char * GRAPH_RDLOCK_PTR (*bdrv_dirname)(BlockDriverState *bs, Error **errp);
305ebc2752bSEmanuele Giuseppe Esposito 
30669c0bf11SEmanuele Giuseppe Esposito     /*
30769c0bf11SEmanuele Giuseppe Esposito      * This informs the driver that we are no longer interested in the result
30869c0bf11SEmanuele Giuseppe Esposito      * of in-flight requests, so don't waste the time if possible.
30969c0bf11SEmanuele Giuseppe Esposito      *
31069c0bf11SEmanuele Giuseppe Esposito      * One example usage is to avoid waiting for an nbd target node reconnect
31169c0bf11SEmanuele Giuseppe Esposito      * timeout during job-cancel with force=true.
31269c0bf11SEmanuele Giuseppe Esposito      */
31369c0bf11SEmanuele Giuseppe Esposito     void (*bdrv_cancel_in_flight)(BlockDriverState *bs);
31469c0bf11SEmanuele Giuseppe Esposito 
3153804e3cfSKevin Wolf     int GRAPH_RDLOCK_PTR (*bdrv_inactivate)(BlockDriverState *bs);
31669c0bf11SEmanuele Giuseppe Esposito 
317a32e7818SKevin Wolf     int GRAPH_RDLOCK_PTR (*bdrv_snapshot_create)(
318a32e7818SKevin Wolf         BlockDriverState *bs, QEMUSnapshotInfo *sn_info);
319a32e7818SKevin Wolf 
320a32e7818SKevin Wolf     int GRAPH_UNLOCKED_PTR (*bdrv_snapshot_goto)(
321a32e7818SKevin Wolf         BlockDriverState *bs, const char *snapshot_id);
322a32e7818SKevin Wolf 
323a32e7818SKevin Wolf     int GRAPH_RDLOCK_PTR (*bdrv_snapshot_delete)(
324a32e7818SKevin Wolf         BlockDriverState *bs, const char *snapshot_id, const char *name,
32569c0bf11SEmanuele Giuseppe Esposito         Error **errp);
326a32e7818SKevin Wolf 
32769c0bf11SEmanuele Giuseppe Esposito     int (*bdrv_snapshot_list)(BlockDriverState *bs,
32869c0bf11SEmanuele Giuseppe Esposito                               QEMUSnapshotInfo **psn_info);
32969c0bf11SEmanuele Giuseppe Esposito     int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,
33069c0bf11SEmanuele Giuseppe Esposito                                   const char *snapshot_id,
33169c0bf11SEmanuele Giuseppe Esposito                                   const char *name,
33269c0bf11SEmanuele Giuseppe Esposito                                   Error **errp);
33369c0bf11SEmanuele Giuseppe Esposito 
33469c0bf11SEmanuele Giuseppe Esposito     int (*bdrv_change_backing_file)(BlockDriverState *bs,
33569c0bf11SEmanuele Giuseppe Esposito         const char *backing_file, const char *backing_fmt);
33669c0bf11SEmanuele Giuseppe Esposito 
33769c0bf11SEmanuele Giuseppe Esposito     /* TODO Better pass a option string/QDict/QemuOpts to add any rule? */
33869c0bf11SEmanuele Giuseppe Esposito     int (*bdrv_debug_breakpoint)(BlockDriverState *bs, const char *event,
33969c0bf11SEmanuele Giuseppe Esposito         const char *tag);
34069c0bf11SEmanuele Giuseppe Esposito     int (*bdrv_debug_remove_breakpoint)(BlockDriverState *bs,
34169c0bf11SEmanuele Giuseppe Esposito         const char *tag);
34269c0bf11SEmanuele Giuseppe Esposito     int (*bdrv_debug_resume)(BlockDriverState *bs, const char *tag);
34369c0bf11SEmanuele Giuseppe Esposito     bool (*bdrv_debug_is_suspended)(BlockDriverState *bs, const char *tag);
34469c0bf11SEmanuele Giuseppe Esposito 
345e19b157fSKevin Wolf     void GRAPH_RDLOCK_PTR (*bdrv_refresh_limits)(
346e19b157fSKevin Wolf         BlockDriverState *bs, Error **errp);
34769c0bf11SEmanuele Giuseppe Esposito 
34869c0bf11SEmanuele Giuseppe Esposito     /*
34969c0bf11SEmanuele Giuseppe Esposito      * Returns 1 if newly created images are guaranteed to contain only
35069c0bf11SEmanuele Giuseppe Esposito      * zeros, 0 otherwise.
35169c0bf11SEmanuele Giuseppe Esposito      */
35206717986SKevin Wolf     int GRAPH_RDLOCK_PTR (*bdrv_has_zero_init)(BlockDriverState *bs);
35369c0bf11SEmanuele Giuseppe Esposito 
35469c0bf11SEmanuele Giuseppe Esposito     /*
35569c0bf11SEmanuele Giuseppe Esposito      * Remove fd handlers, timers, and other event loop callbacks so the event
35669c0bf11SEmanuele Giuseppe Esposito      * loop is no longer in use.  Called with no in-flight requests and in
35769c0bf11SEmanuele Giuseppe Esposito      * depth-first traversal order with parents before child nodes.
35869c0bf11SEmanuele Giuseppe Esposito      */
35969c0bf11SEmanuele Giuseppe Esposito     void (*bdrv_detach_aio_context)(BlockDriverState *bs);
36069c0bf11SEmanuele Giuseppe Esposito 
36169c0bf11SEmanuele Giuseppe Esposito     /*
36269c0bf11SEmanuele Giuseppe Esposito      * Add fd handlers, timers, and other event loop callbacks so I/O requests
36369c0bf11SEmanuele Giuseppe Esposito      * can be processed again.  Called with no in-flight requests and in
36469c0bf11SEmanuele Giuseppe Esposito      * depth-first traversal order with child nodes before parent nodes.
36569c0bf11SEmanuele Giuseppe Esposito      */
36669c0bf11SEmanuele Giuseppe Esposito     void (*bdrv_attach_aio_context)(BlockDriverState *bs,
36769c0bf11SEmanuele Giuseppe Esposito                                     AioContext *new_context);
36869c0bf11SEmanuele Giuseppe Esposito 
36969c0bf11SEmanuele Giuseppe Esposito     /**
370ab613350SStefan Hajnoczi      * bdrv_drain_begin is called if implemented in the beginning of a
371ab613350SStefan Hajnoczi      * drain operation to drain and stop any internal sources of requests in
372ab613350SStefan Hajnoczi      * the driver.
373ab613350SStefan Hajnoczi      * bdrv_drain_end is called if implemented at the end of the drain.
374ab613350SStefan Hajnoczi      *
375ab613350SStefan Hajnoczi      * They should be used by the driver to e.g. manage scheduled I/O
376ab613350SStefan Hajnoczi      * requests, or toggle an internal state. After the end of the drain new
377ab613350SStefan Hajnoczi      * requests will continue normally.
378ab613350SStefan Hajnoczi      *
379ab613350SStefan Hajnoczi      * Implementations of both functions must not call aio_poll().
380ab613350SStefan Hajnoczi      */
381ab613350SStefan Hajnoczi     void (*bdrv_drain_begin)(BlockDriverState *bs);
382ab613350SStefan Hajnoczi     void (*bdrv_drain_end)(BlockDriverState *bs);
383ab613350SStefan Hajnoczi 
384ab613350SStefan Hajnoczi     /**
38569c0bf11SEmanuele Giuseppe Esposito      * Try to get @bs's logical and physical block size.
38669c0bf11SEmanuele Giuseppe Esposito      * On success, store them in @bsz and return zero.
38769c0bf11SEmanuele Giuseppe Esposito      * On failure, return negative errno.
38869c0bf11SEmanuele Giuseppe Esposito      */
389221caadcSKevin Wolf     int GRAPH_RDLOCK_PTR (*bdrv_probe_blocksizes)(
390221caadcSKevin Wolf         BlockDriverState *bs, BlockSizes *bsz);
39169c0bf11SEmanuele Giuseppe Esposito     /**
39269c0bf11SEmanuele Giuseppe Esposito      * Try to get @bs's geometry (cyls, heads, sectors)
39369c0bf11SEmanuele Giuseppe Esposito      * On success, store them in @geo and return 0.
39469c0bf11SEmanuele Giuseppe Esposito      * On failure return -errno.
39569c0bf11SEmanuele Giuseppe Esposito      * Only drivers that want to override guest geometry implement this
39669c0bf11SEmanuele Giuseppe Esposito      * callback; see hd_geometry_guess().
39769c0bf11SEmanuele Giuseppe Esposito      */
39869c0bf11SEmanuele Giuseppe Esposito     int (*bdrv_probe_geometry)(BlockDriverState *bs, HDGeometry *geo);
39969c0bf11SEmanuele Giuseppe Esposito 
4009def6082SKevin Wolf     void GRAPH_WRLOCK_PTR (*bdrv_add_child)(
4019def6082SKevin Wolf         BlockDriverState *parent, BlockDriverState *child, Error **errp);
4029def6082SKevin Wolf 
4039def6082SKevin Wolf     void GRAPH_WRLOCK_PTR (*bdrv_del_child)(
4049def6082SKevin Wolf         BlockDriverState *parent, BdrvChild *child, Error **errp);
40569c0bf11SEmanuele Giuseppe Esposito 
40669c0bf11SEmanuele Giuseppe Esposito     /**
40769c0bf11SEmanuele Giuseppe Esposito      * Informs the block driver that a permission change is intended. The
40869c0bf11SEmanuele Giuseppe Esposito      * driver checks whether the change is permissible and may take other
40969c0bf11SEmanuele Giuseppe Esposito      * preparations for the change (e.g. get file system locks). This operation
41069c0bf11SEmanuele Giuseppe Esposito      * is always followed either by a call to either .bdrv_set_perm or
41169c0bf11SEmanuele Giuseppe Esposito      * .bdrv_abort_perm_update.
41269c0bf11SEmanuele Giuseppe Esposito      *
41369c0bf11SEmanuele Giuseppe Esposito      * Checks whether the requested set of cumulative permissions in @perm
41469c0bf11SEmanuele Giuseppe Esposito      * can be granted for accessing @bs and whether no other users are using
41569c0bf11SEmanuele Giuseppe Esposito      * permissions other than those given in @shared (both arguments take
41669c0bf11SEmanuele Giuseppe Esposito      * BLK_PERM_* bitmasks).
41769c0bf11SEmanuele Giuseppe Esposito      *
41869c0bf11SEmanuele Giuseppe Esposito      * If both conditions are met, 0 is returned. Otherwise, -errno is returned
41969c0bf11SEmanuele Giuseppe Esposito      * and errp is set to an error describing the conflict.
42069c0bf11SEmanuele Giuseppe Esposito      */
421bce73bc2SKevin Wolf     int GRAPH_RDLOCK_PTR (*bdrv_check_perm)(BlockDriverState *bs, uint64_t perm,
42269c0bf11SEmanuele Giuseppe Esposito                                             uint64_t shared, Error **errp);
42369c0bf11SEmanuele Giuseppe Esposito 
42469c0bf11SEmanuele Giuseppe Esposito     /**
42569c0bf11SEmanuele Giuseppe Esposito      * Called to inform the driver that the set of cumulative set of used
426a1a62cedSMichael Tokarev      * permissions for @bs has changed to @perm, and the set of shareable
42769c0bf11SEmanuele Giuseppe Esposito      * permission to @shared. The driver can use this to propagate changes to
42869c0bf11SEmanuele Giuseppe Esposito      * its children (i.e. request permissions only if a parent actually needs
42969c0bf11SEmanuele Giuseppe Esposito      * them).
43069c0bf11SEmanuele Giuseppe Esposito      *
43169c0bf11SEmanuele Giuseppe Esposito      * This function is only invoked after bdrv_check_perm(), so block drivers
43269c0bf11SEmanuele Giuseppe Esposito      * may rely on preparations made in their .bdrv_check_perm implementation.
43369c0bf11SEmanuele Giuseppe Esposito      */
434bce73bc2SKevin Wolf     void GRAPH_RDLOCK_PTR (*bdrv_set_perm)(
435bce73bc2SKevin Wolf         BlockDriverState *bs, uint64_t perm, uint64_t shared);
43669c0bf11SEmanuele Giuseppe Esposito 
43769c0bf11SEmanuele Giuseppe Esposito     /*
43869c0bf11SEmanuele Giuseppe Esposito      * Called to inform the driver that after a previous bdrv_check_perm()
43969c0bf11SEmanuele Giuseppe Esposito      * call, the permission update is not performed and any preparations made
44069c0bf11SEmanuele Giuseppe Esposito      * for it (e.g. taken file locks) need to be undone.
44169c0bf11SEmanuele Giuseppe Esposito      *
44269c0bf11SEmanuele Giuseppe Esposito      * This function can be called even for nodes that never saw a
44369c0bf11SEmanuele Giuseppe Esposito      * bdrv_check_perm() call. It is a no-op then.
44469c0bf11SEmanuele Giuseppe Esposito      */
445bce73bc2SKevin Wolf     void GRAPH_RDLOCK_PTR (*bdrv_abort_perm_update)(BlockDriverState *bs);
44669c0bf11SEmanuele Giuseppe Esposito 
44769c0bf11SEmanuele Giuseppe Esposito     /**
44869c0bf11SEmanuele Giuseppe Esposito      * Returns in @nperm and @nshared the permissions that the driver for @bs
44969c0bf11SEmanuele Giuseppe Esposito      * needs on its child @c, based on the cumulative permissions requested by
45069c0bf11SEmanuele Giuseppe Esposito      * the parents in @parent_perm and @parent_shared.
45169c0bf11SEmanuele Giuseppe Esposito      *
45269c0bf11SEmanuele Giuseppe Esposito      * If @c is NULL, return the permissions for attaching a new child for the
45369c0bf11SEmanuele Giuseppe Esposito      * given @child_class and @role.
45469c0bf11SEmanuele Giuseppe Esposito      *
45569c0bf11SEmanuele Giuseppe Esposito      * If @reopen_queue is non-NULL, don't return the currently needed
45669c0bf11SEmanuele Giuseppe Esposito      * permissions, but those that will be needed after applying the
45769c0bf11SEmanuele Giuseppe Esposito      * @reopen_queue.
45869c0bf11SEmanuele Giuseppe Esposito      */
459c629b6d2SKevin Wolf      void GRAPH_RDLOCK_PTR (*bdrv_child_perm)(
460c629b6d2SKevin Wolf         BlockDriverState *bs, BdrvChild *c, BdrvChildRole role,
46169c0bf11SEmanuele Giuseppe Esposito         BlockReopenQueue *reopen_queue,
46269c0bf11SEmanuele Giuseppe Esposito         uint64_t parent_perm, uint64_t parent_shared,
46369c0bf11SEmanuele Giuseppe Esposito         uint64_t *nperm, uint64_t *nshared);
46469c0bf11SEmanuele Giuseppe Esposito 
46569c0bf11SEmanuele Giuseppe Esposito     /**
46669c0bf11SEmanuele Giuseppe Esposito      * Register/unregister a buffer for I/O. For example, when the driver is
46769c0bf11SEmanuele Giuseppe Esposito      * interested to know the memory areas that will later be used in iovs, so
46869c0bf11SEmanuele Giuseppe Esposito      * that it can do IOMMU mapping with VFIO etc., in order to get better
46969c0bf11SEmanuele Giuseppe Esposito      * performance. In the case of VFIO drivers, this callback is used to do
47069c0bf11SEmanuele Giuseppe Esposito      * DMA mapping for hot buffers.
471f4ec04baSStefan Hajnoczi      *
472f4ec04baSStefan Hajnoczi      * Returns: true on success, false on failure
47369c0bf11SEmanuele Giuseppe Esposito      */
474d9249c25SKevin Wolf     bool GRAPH_RDLOCK_PTR (*bdrv_register_buf)(
475d9249c25SKevin Wolf         BlockDriverState *bs, void *host, size_t size, Error **errp);
476d9249c25SKevin Wolf     void GRAPH_RDLOCK_PTR (*bdrv_unregister_buf)(
477d9249c25SKevin Wolf         BlockDriverState *bs, void *host, size_t size);
47869c0bf11SEmanuele Giuseppe Esposito 
47969c0bf11SEmanuele Giuseppe Esposito     /*
48069c0bf11SEmanuele Giuseppe Esposito      * This field is modified only under the BQL, and is part of
48169c0bf11SEmanuele Giuseppe Esposito      * the global state.
48269c0bf11SEmanuele Giuseppe Esposito      */
48369c0bf11SEmanuele Giuseppe Esposito     QLIST_ENTRY(BlockDriver) list;
48469c0bf11SEmanuele Giuseppe Esposito 
48569c0bf11SEmanuele Giuseppe Esposito     /*
48669c0bf11SEmanuele Giuseppe Esposito      * I/O API functions. These functions are thread-safe.
48769c0bf11SEmanuele Giuseppe Esposito      *
48869c0bf11SEmanuele Giuseppe Esposito      * See include/block/block-io.h for more information about
48969c0bf11SEmanuele Giuseppe Esposito      * the I/O API.
49069c0bf11SEmanuele Giuseppe Esposito      */
49169c0bf11SEmanuele Giuseppe Esposito 
49269c0bf11SEmanuele Giuseppe Esposito     int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename);
49369c0bf11SEmanuele Giuseppe Esposito 
494840428a2SEmanuele Giuseppe Esposito     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_amend)(
495840428a2SEmanuele Giuseppe Esposito         BlockDriverState *bs, BlockdevAmendOptions *opts, bool force,
49669c0bf11SEmanuele Giuseppe Esposito         Error **errp);
49769c0bf11SEmanuele Giuseppe Esposito 
498ebc2752bSEmanuele Giuseppe Esposito     /* aio */
4997b1fb72eSKevin Wolf     BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_preadv)(BlockDriverState *bs,
500ebc2752bSEmanuele Giuseppe Esposito         int64_t offset, int64_t bytes, QEMUIOVector *qiov,
501ebc2752bSEmanuele Giuseppe Esposito         BdrvRequestFlags flags, BlockCompletionFunc *cb, void *opaque);
5027b1fb72eSKevin Wolf 
5037b1fb72eSKevin Wolf     BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_pwritev)(BlockDriverState *bs,
504ebc2752bSEmanuele Giuseppe Esposito         int64_t offset, int64_t bytes, QEMUIOVector *qiov,
505ebc2752bSEmanuele Giuseppe Esposito         BdrvRequestFlags flags, BlockCompletionFunc *cb, void *opaque);
5067b1fb72eSKevin Wolf 
50788095349SEmanuele Giuseppe Esposito     BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_flush)(
50888095349SEmanuele Giuseppe Esposito         BlockDriverState *bs, BlockCompletionFunc *cb, void *opaque);
5099a5a1c62SEmanuele Giuseppe Esposito 
5109a5a1c62SEmanuele Giuseppe Esposito     BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_pdiscard)(
5119a5a1c62SEmanuele Giuseppe Esposito         BlockDriverState *bs, int64_t offset, int bytes,
512ebc2752bSEmanuele Giuseppe Esposito         BlockCompletionFunc *cb, void *opaque);
513ebc2752bSEmanuele Giuseppe Esposito 
5147b1fb72eSKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_readv)(BlockDriverState *bs,
515ebc2752bSEmanuele Giuseppe Esposito         int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
516ebc2752bSEmanuele Giuseppe Esposito 
517ebc2752bSEmanuele Giuseppe Esposito     /**
518ebc2752bSEmanuele Giuseppe Esposito      * @offset: position in bytes to read at
519ebc2752bSEmanuele Giuseppe Esposito      * @bytes: number of bytes to read
520ebc2752bSEmanuele Giuseppe Esposito      * @qiov: the buffers to fill with read data
521ebc2752bSEmanuele Giuseppe Esposito      * @flags: currently unused, always 0
522ebc2752bSEmanuele Giuseppe Esposito      *
523ebc2752bSEmanuele Giuseppe Esposito      * @offset and @bytes will be a multiple of 'request_alignment',
524ebc2752bSEmanuele Giuseppe Esposito      * but the length of individual @qiov elements does not have to
525ebc2752bSEmanuele Giuseppe Esposito      * be a multiple.
526ebc2752bSEmanuele Giuseppe Esposito      *
527ebc2752bSEmanuele Giuseppe Esposito      * @bytes will always equal the total size of @qiov, and will be
528ebc2752bSEmanuele Giuseppe Esposito      * no larger than 'max_transfer'.
529ebc2752bSEmanuele Giuseppe Esposito      *
530ebc2752bSEmanuele Giuseppe Esposito      * The buffer in @qiov may point directly to guest memory.
531ebc2752bSEmanuele Giuseppe Esposito      */
5327b1fb72eSKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv)(BlockDriverState *bs,
533ebc2752bSEmanuele Giuseppe Esposito         int64_t offset, int64_t bytes, QEMUIOVector *qiov,
534ebc2752bSEmanuele Giuseppe Esposito         BdrvRequestFlags flags);
535ebc2752bSEmanuele Giuseppe Esposito 
5367b1fb72eSKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv_part)(
5377b1fb72eSKevin Wolf         BlockDriverState *bs, int64_t offset, int64_t bytes,
538ebc2752bSEmanuele Giuseppe Esposito         QEMUIOVector *qiov, size_t qiov_offset,
539ebc2752bSEmanuele Giuseppe Esposito         BdrvRequestFlags flags);
540ebc2752bSEmanuele Giuseppe Esposito 
5417b1fb72eSKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_writev)(BlockDriverState *bs,
542ebc2752bSEmanuele Giuseppe Esposito         int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
543ebc2752bSEmanuele Giuseppe Esposito         int flags);
544ebc2752bSEmanuele Giuseppe Esposito     /**
545ebc2752bSEmanuele Giuseppe Esposito      * @offset: position in bytes to write at
546ebc2752bSEmanuele Giuseppe Esposito      * @bytes: number of bytes to write
547ebc2752bSEmanuele Giuseppe Esposito      * @qiov: the buffers containing data to write
548ebc2752bSEmanuele Giuseppe Esposito      * @flags: zero or more bits allowed by 'supported_write_flags'
549ebc2752bSEmanuele Giuseppe Esposito      *
550ebc2752bSEmanuele Giuseppe Esposito      * @offset and @bytes will be a multiple of 'request_alignment',
551ebc2752bSEmanuele Giuseppe Esposito      * but the length of individual @qiov elements does not have to
552ebc2752bSEmanuele Giuseppe Esposito      * be a multiple.
553ebc2752bSEmanuele Giuseppe Esposito      *
554ebc2752bSEmanuele Giuseppe Esposito      * @bytes will always equal the total size of @qiov, and will be
555ebc2752bSEmanuele Giuseppe Esposito      * no larger than 'max_transfer'.
556ebc2752bSEmanuele Giuseppe Esposito      *
557ebc2752bSEmanuele Giuseppe Esposito      * The buffer in @qiov may point directly to guest memory.
558ebc2752bSEmanuele Giuseppe Esposito      */
5597b1fb72eSKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev)(
5607b1fb72eSKevin Wolf         BlockDriverState *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov,
561ebc2752bSEmanuele Giuseppe Esposito         BdrvRequestFlags flags);
5627b1fb72eSKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_part)(
5637b1fb72eSKevin Wolf         BlockDriverState *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov,
5647b1fb72eSKevin Wolf         size_t qiov_offset, BdrvRequestFlags flags);
565ebc2752bSEmanuele Giuseppe Esposito 
566ebc2752bSEmanuele Giuseppe Esposito     /*
567ebc2752bSEmanuele Giuseppe Esposito      * Efficiently zero a region of the disk image.  Typically an image format
568ebc2752bSEmanuele Giuseppe Esposito      * would use a compact metadata representation to implement this.  This
569ebc2752bSEmanuele Giuseppe Esposito      * function pointer may be NULL or return -ENOSUP and .bdrv_co_writev()
570ebc2752bSEmanuele Giuseppe Esposito      * will be called instead.
571ebc2752bSEmanuele Giuseppe Esposito      */
572abaf8b75SKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwrite_zeroes)(
573abaf8b75SKevin Wolf         BlockDriverState *bs, int64_t offset, int64_t bytes,
574abaf8b75SKevin Wolf         BdrvRequestFlags flags);
5759a5a1c62SEmanuele Giuseppe Esposito 
5769a5a1c62SEmanuele Giuseppe Esposito     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pdiscard)(
5779a5a1c62SEmanuele Giuseppe Esposito         BlockDriverState *bs, int64_t offset, int64_t bytes);
578ebc2752bSEmanuele Giuseppe Esposito 
579ebc2752bSEmanuele Giuseppe Esposito     /*
580ebc2752bSEmanuele Giuseppe Esposito      * Map [offset, offset + nbytes) range onto a child of @bs to copy from,
581ebc2752bSEmanuele Giuseppe Esposito      * and invoke bdrv_co_copy_range_from(child, ...), or invoke
582ebc2752bSEmanuele Giuseppe Esposito      * bdrv_co_copy_range_to() if @bs is the leaf child to copy data from.
583ebc2752bSEmanuele Giuseppe Esposito      *
584ebc2752bSEmanuele Giuseppe Esposito      * See the comment of bdrv_co_copy_range for the parameter and return value
585ebc2752bSEmanuele Giuseppe Esposito      * semantics.
586ebc2752bSEmanuele Giuseppe Esposito      */
587742bf09bSEmanuele Giuseppe Esposito     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_copy_range_from)(
588742bf09bSEmanuele Giuseppe Esposito         BlockDriverState *bs, BdrvChild *src, int64_t offset,
589742bf09bSEmanuele Giuseppe Esposito         BdrvChild *dst, int64_t dst_offset, int64_t bytes,
590742bf09bSEmanuele Giuseppe Esposito         BdrvRequestFlags read_flags, BdrvRequestFlags write_flags);
591ebc2752bSEmanuele Giuseppe Esposito 
592ebc2752bSEmanuele Giuseppe Esposito     /*
593ebc2752bSEmanuele Giuseppe Esposito      * Map [offset, offset + nbytes) range onto a child of bs to copy data to,
594ebc2752bSEmanuele Giuseppe Esposito      * and invoke bdrv_co_copy_range_to(child, src, ...), or perform the copy
595ebc2752bSEmanuele Giuseppe Esposito      * operation if @bs is the leaf and @src has the same BlockDriver.  Return
596ebc2752bSEmanuele Giuseppe Esposito      * -ENOTSUP if @bs is the leaf but @src has a different BlockDriver.
597ebc2752bSEmanuele Giuseppe Esposito      *
598ebc2752bSEmanuele Giuseppe Esposito      * See the comment of bdrv_co_copy_range for the parameter and return value
599ebc2752bSEmanuele Giuseppe Esposito      * semantics.
600ebc2752bSEmanuele Giuseppe Esposito      */
601742bf09bSEmanuele Giuseppe Esposito     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_copy_range_to)(
602742bf09bSEmanuele Giuseppe Esposito         BlockDriverState *bs, BdrvChild *src, int64_t src_offset,
603742bf09bSEmanuele Giuseppe Esposito         BdrvChild *dst, int64_t dst_offset, int64_t bytes,
604742bf09bSEmanuele Giuseppe Esposito         BdrvRequestFlags read_flags, BdrvRequestFlags write_flags);
605ebc2752bSEmanuele Giuseppe Esposito 
606ebc2752bSEmanuele Giuseppe Esposito     /*
607ebc2752bSEmanuele Giuseppe Esposito      * Building block for bdrv_block_status[_above] and
608ebc2752bSEmanuele Giuseppe Esposito      * bdrv_is_allocated[_above].  The driver should answer only
609ebc2752bSEmanuele Giuseppe Esposito      * according to the current layer, and should only need to set
610ebc2752bSEmanuele Giuseppe Esposito      * BDRV_BLOCK_DATA, BDRV_BLOCK_ZERO, BDRV_BLOCK_OFFSET_VALID,
611ebc2752bSEmanuele Giuseppe Esposito      * and/or BDRV_BLOCK_RAW; if the current layer defers to a backing
612ebc2752bSEmanuele Giuseppe Esposito      * layer, the result should be 0 (and not BDRV_BLOCK_ZERO).  See
613ebc2752bSEmanuele Giuseppe Esposito      * block.h for the overall meaning of the bits.  As a hint, the
614ebc2752bSEmanuele Giuseppe Esposito      * flag want_zero is true if the caller cares more about precise
615ebc2752bSEmanuele Giuseppe Esposito      * mappings (favor accurate _OFFSET_VALID/_ZERO) or false for
616ebc2752bSEmanuele Giuseppe Esposito      * overall allocation (favor larger *pnum, perhaps by reporting
617ebc2752bSEmanuele Giuseppe Esposito      * _DATA instead of _ZERO).  The block layer guarantees input
618ebc2752bSEmanuele Giuseppe Esposito      * clamped to bdrv_getlength() and aligned to request_alignment,
619ebc2752bSEmanuele Giuseppe Esposito      * as well as non-NULL pnum, map, and file; in turn, the driver
620ebc2752bSEmanuele Giuseppe Esposito      * must return an error or set pnum to an aligned non-zero value.
621ebc2752bSEmanuele Giuseppe Esposito      *
622ebc2752bSEmanuele Giuseppe Esposito      * Note that @bytes is just a hint on how big of a region the
623ebc2752bSEmanuele Giuseppe Esposito      * caller wants to inspect.  It is not a limit on *pnum.
624ebc2752bSEmanuele Giuseppe Esposito      * Implementations are free to return larger values of *pnum if
625ebc2752bSEmanuele Giuseppe Esposito      * doing so does not incur a performance penalty.
626ebc2752bSEmanuele Giuseppe Esposito      *
627ebc2752bSEmanuele Giuseppe Esposito      * block/io.c's bdrv_co_block_status() will utilize an unclamped
628ebc2752bSEmanuele Giuseppe Esposito      * *pnum value for the block-status cache on protocol nodes, prior
629ebc2752bSEmanuele Giuseppe Esposito      * to clamping *pnum for return to its caller.
630ebc2752bSEmanuele Giuseppe Esposito      */
6317ff9579eSKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_block_status)(
6327ff9579eSKevin Wolf         BlockDriverState *bs,
633ebc2752bSEmanuele Giuseppe Esposito         bool want_zero, int64_t offset, int64_t bytes, int64_t *pnum,
634ebc2752bSEmanuele Giuseppe Esposito         int64_t *map, BlockDriverState **file);
635ebc2752bSEmanuele Giuseppe Esposito 
636ebc2752bSEmanuele Giuseppe Esposito     /*
637ce14f3b4SVladimir Sementsov-Ogievskiy      * Snapshot-access API.
638ce14f3b4SVladimir Sementsov-Ogievskiy      *
639ce14f3b4SVladimir Sementsov-Ogievskiy      * Block-driver may provide snapshot-access API: special functions to access
640ce14f3b4SVladimir Sementsov-Ogievskiy      * some internal "snapshot". The functions are similar with normal
641ce14f3b4SVladimir Sementsov-Ogievskiy      * read/block_status/discard handler, but don't have any specific handling
642ce14f3b4SVladimir Sementsov-Ogievskiy      * in generic block-layer: no serializing, no alignment, no tracked
643ce14f3b4SVladimir Sementsov-Ogievskiy      * requests. So, block-driver that realizes these APIs is fully responsible
644ce14f3b4SVladimir Sementsov-Ogievskiy      * for synchronization between snapshot-access API and normal IO requests.
6451c14eaabSVladimir Sementsov-Ogievskiy      *
6461c14eaabSVladimir Sementsov-Ogievskiy      * TODO: To be able to support qcow2's internal snapshots, this API will
6471c14eaabSVladimir Sementsov-Ogievskiy      * need to be extended to:
6481c14eaabSVladimir Sementsov-Ogievskiy      * - be able to select a specific snapshot
6491c14eaabSVladimir Sementsov-Ogievskiy      * - receive the snapshot's actual length (which may differ from bs's
6501c14eaabSVladimir Sementsov-Ogievskiy      *   length)
651ce14f3b4SVladimir Sementsov-Ogievskiy      */
6527b9e8b22SKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv_snapshot)(
6537b9e8b22SKevin Wolf         BlockDriverState *bs, int64_t offset, int64_t bytes,
6547b9e8b22SKevin Wolf         QEMUIOVector *qiov, size_t qiov_offset);
6557b9e8b22SKevin Wolf 
6567b9e8b22SKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_snapshot_block_status)(
6577b9e8b22SKevin Wolf         BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes,
6587b9e8b22SKevin Wolf         int64_t *pnum, int64_t *map, BlockDriverState **file);
6599a5a1c62SEmanuele Giuseppe Esposito 
6609a5a1c62SEmanuele Giuseppe Esposito     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pdiscard_snapshot)(
6619a5a1c62SEmanuele Giuseppe Esposito         BlockDriverState *bs, int64_t offset, int64_t bytes);
662ce14f3b4SVladimir Sementsov-Ogievskiy 
663ce14f3b4SVladimir Sementsov-Ogievskiy     /*
664ebc2752bSEmanuele Giuseppe Esposito      * Invalidate any cached meta-data.
665ebc2752bSEmanuele Giuseppe Esposito      */
6661b3ff9feSKevin Wolf     void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_invalidate_cache)(
6671b3ff9feSKevin Wolf         BlockDriverState *bs, Error **errp);
668ebc2752bSEmanuele Giuseppe Esposito 
669ebc2752bSEmanuele Giuseppe Esposito     /*
670ebc2752bSEmanuele Giuseppe Esposito      * Flushes all data for all layers by calling bdrv_co_flush for underlying
671ebc2752bSEmanuele Giuseppe Esposito      * layers, if needed. This function is needed for deterministic
672ebc2752bSEmanuele Giuseppe Esposito      * synchronization of the flush finishing callback.
673ebc2752bSEmanuele Giuseppe Esposito      */
67488095349SEmanuele Giuseppe Esposito     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush)(BlockDriverState *bs);
675ebc2752bSEmanuele Giuseppe Esposito 
676ebc2752bSEmanuele Giuseppe Esposito     /* Delete a created file. */
67748aef794SKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_delete_file)(
67848aef794SKevin Wolf         BlockDriverState *bs, Error **errp);
679ebc2752bSEmanuele Giuseppe Esposito 
680ebc2752bSEmanuele Giuseppe Esposito     /*
681ebc2752bSEmanuele Giuseppe Esposito      * Flushes all data that was already written to the OS all the way down to
682ebc2752bSEmanuele Giuseppe Esposito      * the disk (for example file-posix.c calls fsync()).
683ebc2752bSEmanuele Giuseppe Esposito      */
68488095349SEmanuele Giuseppe Esposito     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush_to_disk)(
68588095349SEmanuele Giuseppe Esposito         BlockDriverState *bs);
686ebc2752bSEmanuele Giuseppe Esposito 
687ebc2752bSEmanuele Giuseppe Esposito     /*
688ebc2752bSEmanuele Giuseppe Esposito      * Flushes all internal caches to the OS. The data may still sit in a
689ebc2752bSEmanuele Giuseppe Esposito      * writeback cache of the host OS, but it will survive a crash of the qemu
690ebc2752bSEmanuele Giuseppe Esposito      * process.
691ebc2752bSEmanuele Giuseppe Esposito      */
69288095349SEmanuele Giuseppe Esposito     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush_to_os)(
69388095349SEmanuele Giuseppe Esposito         BlockDriverState *bs);
694ebc2752bSEmanuele Giuseppe Esposito 
695ebc2752bSEmanuele Giuseppe Esposito     /*
696ebc2752bSEmanuele Giuseppe Esposito      * Truncate @bs to @offset bytes using the given @prealloc mode
697ebc2752bSEmanuele Giuseppe Esposito      * when growing.  Modes other than PREALLOC_MODE_OFF should be
698ebc2752bSEmanuele Giuseppe Esposito      * rejected when shrinking @bs.
699ebc2752bSEmanuele Giuseppe Esposito      *
700ebc2752bSEmanuele Giuseppe Esposito      * If @exact is true, @bs must be resized to exactly @offset.
701ebc2752bSEmanuele Giuseppe Esposito      * Otherwise, it is sufficient for @bs (if it is a host block
702ebc2752bSEmanuele Giuseppe Esposito      * device and thus there is no way to resize it) to be at least
703ebc2752bSEmanuele Giuseppe Esposito      * @offset bytes in length.
704ebc2752bSEmanuele Giuseppe Esposito      *
705ebc2752bSEmanuele Giuseppe Esposito      * If @exact is true and this function fails but would succeed
706ebc2752bSEmanuele Giuseppe Esposito      * with @exact = false, it should return -ENOTSUP.
707ebc2752bSEmanuele Giuseppe Esposito      */
708c2b8e315SKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_truncate)(
709c2b8e315SKevin Wolf         BlockDriverState *bs, int64_t offset, bool exact,
710c2b8e315SKevin Wolf         PreallocMode prealloc, BdrvRequestFlags flags, Error **errp);
711c2b8e315SKevin Wolf 
7128ab8140aSKevin Wolf     int64_t coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_getlength)(
7138ab8140aSKevin Wolf         BlockDriverState *bs);
7148ab8140aSKevin Wolf 
715de335638SEmanuele Giuseppe Esposito     int64_t coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_get_allocated_file_size)(
71682618d7bSEmanuele Giuseppe Esposito         BlockDriverState *bs);
71782618d7bSEmanuele Giuseppe Esposito 
718ebc2752bSEmanuele Giuseppe Esposito     BlockMeasureInfo *(*bdrv_measure)(QemuOpts *opts, BlockDriverState *in_bs,
719ebc2752bSEmanuele Giuseppe Esposito                                       Error **errp);
720ebc2752bSEmanuele Giuseppe Esposito 
7217b1fb72eSKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_compressed)(
7227b1fb72eSKevin Wolf         BlockDriverState *bs, int64_t offset, int64_t bytes,
7237b1fb72eSKevin Wolf         QEMUIOVector *qiov);
7247b1fb72eSKevin Wolf 
7257b1fb72eSKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_compressed_part)(
7267b1fb72eSKevin Wolf         BlockDriverState *bs, int64_t offset, int64_t bytes,
7277b1fb72eSKevin Wolf         QEMUIOVector *qiov, size_t qiov_offset);
728ebc2752bSEmanuele Giuseppe Esposito 
729a00e70c0SEmanuele Giuseppe Esposito     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_get_info)(
730a00e70c0SEmanuele Giuseppe Esposito         BlockDriverState *bs, BlockDriverInfo *bdi);
731ebc2752bSEmanuele Giuseppe Esposito 
7323574499aSKevin Wolf     ImageInfoSpecific * GRAPH_RDLOCK_PTR (*bdrv_get_specific_info)(
7333574499aSKevin Wolf         BlockDriverState *bs, Error **errp);
734ebc2752bSEmanuele Giuseppe Esposito     BlockStatsSpecific *(*bdrv_get_specific_stats)(BlockDriverState *bs);
735ebc2752bSEmanuele Giuseppe Esposito 
736ca5e2ad9SEmanuele Giuseppe Esposito     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_save_vmstate)(
7371b3ff9feSKevin Wolf         BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
7381b3ff9feSKevin Wolf 
739ca5e2ad9SEmanuele Giuseppe Esposito     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_load_vmstate)(
7401b3ff9feSKevin Wolf         BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
741ebc2752bSEmanuele Giuseppe Esposito 
7426d43eaa3SSam Li     int coroutine_fn (*bdrv_co_zone_report)(BlockDriverState *bs,
7436d43eaa3SSam Li             int64_t offset, unsigned int *nr_zones,
7446d43eaa3SSam Li             BlockZoneDescriptor *zones);
7456d43eaa3SSam Li     int coroutine_fn (*bdrv_co_zone_mgmt)(BlockDriverState *bs, BlockZoneOp op,
7466d43eaa3SSam Li             int64_t offset, int64_t len);
7474751d09aSSam Li     int coroutine_fn (*bdrv_co_zone_append)(BlockDriverState *bs,
7484751d09aSSam Li             int64_t *offset, QEMUIOVector *qiov,
7494751d09aSSam Li             BdrvRequestFlags flags);
7506d43eaa3SSam Li 
751ebc2752bSEmanuele Giuseppe Esposito     /* removable device specific */
752c73ff92cSEmanuele Giuseppe Esposito     bool coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_is_inserted)(
753c73ff92cSEmanuele Giuseppe Esposito         BlockDriverState *bs);
75479a292e5SKevin Wolf     void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_eject)(
75579a292e5SKevin Wolf         BlockDriverState *bs, bool eject_flag);
75679a292e5SKevin Wolf     void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_lock_medium)(
75779a292e5SKevin Wolf         BlockDriverState *bs, bool locked);
758ebc2752bSEmanuele Giuseppe Esposito 
759ebc2752bSEmanuele Giuseppe Esposito     /* to control generic scsi devices */
76026c518abSKevin Wolf     BlockAIOCB *coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_aio_ioctl)(
76126c518abSKevin Wolf         BlockDriverState *bs, unsigned long int req, void *buf,
762ebc2752bSEmanuele Giuseppe Esposito         BlockCompletionFunc *cb, void *opaque);
76326c518abSKevin Wolf 
76426c518abSKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_ioctl)(
76526c518abSKevin Wolf         BlockDriverState *bs, unsigned long int req, void *buf);
766ebc2752bSEmanuele Giuseppe Esposito 
767ebc2752bSEmanuele Giuseppe Esposito     /*
768ebc2752bSEmanuele Giuseppe Esposito      * Returns 0 for completed check, -errno for internal errors.
769ebc2752bSEmanuele Giuseppe Esposito      * The check results are stored in result.
770ebc2752bSEmanuele Giuseppe Esposito      */
7711b3ff9feSKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_check)(
7721b3ff9feSKevin Wolf         BlockDriverState *bs, BdrvCheckResult *result, BdrvCheckMode fix);
773ebc2752bSEmanuele Giuseppe Esposito 
774cb2bfaa4SEmanuele Giuseppe Esposito     void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_debug_event)(
775cb2bfaa4SEmanuele Giuseppe Esposito         BlockDriverState *bs, BlkdebugEvent event);
776ebc2752bSEmanuele Giuseppe Esposito 
777ebc2752bSEmanuele Giuseppe Esposito     bool (*bdrv_supports_persistent_dirty_bitmap)(BlockDriverState *bs);
778167f748dSKevin Wolf 
779167f748dSKevin Wolf     bool coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_can_store_new_dirty_bitmap)(
780c2d76808SAlberto Faria         BlockDriverState *bs, const char *name, uint32_t granularity,
781ebc2752bSEmanuele Giuseppe Esposito         Error **errp);
782167f748dSKevin Wolf 
783167f748dSKevin Wolf     int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_remove_persistent_dirty_bitmap)(
784c2d76808SAlberto Faria         BlockDriverState *bs, const char *name, Error **errp);
785ebc2752bSEmanuele Giuseppe Esposito };
786ebc2752bSEmanuele Giuseppe Esposito 
7877b1fb72eSKevin Wolf static inline bool TSA_NO_TSA block_driver_can_compress(BlockDriver *drv)
788ebc2752bSEmanuele Giuseppe Esposito {
789ebc2752bSEmanuele Giuseppe Esposito     return drv->bdrv_co_pwritev_compressed ||
790ebc2752bSEmanuele Giuseppe Esposito            drv->bdrv_co_pwritev_compressed_part;
791ebc2752bSEmanuele Giuseppe Esposito }
792ebc2752bSEmanuele Giuseppe Esposito 
793ebc2752bSEmanuele Giuseppe Esposito typedef struct BlockLimits {
794ebc2752bSEmanuele Giuseppe Esposito     /*
795ebc2752bSEmanuele Giuseppe Esposito      * Alignment requirement, in bytes, for offset/length of I/O
796ebc2752bSEmanuele Giuseppe Esposito      * requests. Must be a power of 2 less than INT_MAX; defaults to
797ebc2752bSEmanuele Giuseppe Esposito      * 1 for drivers with modern byte interfaces, and to 512
798ebc2752bSEmanuele Giuseppe Esposito      * otherwise.
799ebc2752bSEmanuele Giuseppe Esposito      */
800ebc2752bSEmanuele Giuseppe Esposito     uint32_t request_alignment;
801ebc2752bSEmanuele Giuseppe Esposito 
802ebc2752bSEmanuele Giuseppe Esposito     /*
803ebc2752bSEmanuele Giuseppe Esposito      * Maximum number of bytes that can be discarded at once. Must be multiple
804ebc2752bSEmanuele Giuseppe Esposito      * of pdiscard_alignment, but need not be power of 2. May be 0 if no
805ebc2752bSEmanuele Giuseppe Esposito      * inherent 64-bit limit.
806ebc2752bSEmanuele Giuseppe Esposito      */
807ebc2752bSEmanuele Giuseppe Esposito     int64_t max_pdiscard;
808ebc2752bSEmanuele Giuseppe Esposito 
809ebc2752bSEmanuele Giuseppe Esposito     /*
810ebc2752bSEmanuele Giuseppe Esposito      * Optimal alignment for discard requests in bytes. A power of 2
811ebc2752bSEmanuele Giuseppe Esposito      * is best but not mandatory.  Must be a multiple of
812ebc2752bSEmanuele Giuseppe Esposito      * bl.request_alignment, and must be less than max_pdiscard if
813ebc2752bSEmanuele Giuseppe Esposito      * that is set. May be 0 if bl.request_alignment is good enough
814ebc2752bSEmanuele Giuseppe Esposito      */
815ebc2752bSEmanuele Giuseppe Esposito     uint32_t pdiscard_alignment;
816ebc2752bSEmanuele Giuseppe Esposito 
817ebc2752bSEmanuele Giuseppe Esposito     /*
818ebc2752bSEmanuele Giuseppe Esposito      * Maximum number of bytes that can zeroized at once. Must be multiple of
819ebc2752bSEmanuele Giuseppe Esposito      * pwrite_zeroes_alignment. 0 means no limit.
820ebc2752bSEmanuele Giuseppe Esposito      */
821ebc2752bSEmanuele Giuseppe Esposito     int64_t max_pwrite_zeroes;
822ebc2752bSEmanuele Giuseppe Esposito 
823ebc2752bSEmanuele Giuseppe Esposito     /*
824ebc2752bSEmanuele Giuseppe Esposito      * Optimal alignment for write zeroes requests in bytes. A power
825ebc2752bSEmanuele Giuseppe Esposito      * of 2 is best but not mandatory.  Must be a multiple of
826ebc2752bSEmanuele Giuseppe Esposito      * bl.request_alignment, and must be less than max_pwrite_zeroes
827ebc2752bSEmanuele Giuseppe Esposito      * if that is set. May be 0 if bl.request_alignment is good
828ebc2752bSEmanuele Giuseppe Esposito      * enough
829ebc2752bSEmanuele Giuseppe Esposito      */
830ebc2752bSEmanuele Giuseppe Esposito     uint32_t pwrite_zeroes_alignment;
831ebc2752bSEmanuele Giuseppe Esposito 
832ebc2752bSEmanuele Giuseppe Esposito     /*
833ebc2752bSEmanuele Giuseppe Esposito      * Optimal transfer length in bytes.  A power of 2 is best but not
834ebc2752bSEmanuele Giuseppe Esposito      * mandatory.  Must be a multiple of bl.request_alignment, or 0 if
835ebc2752bSEmanuele Giuseppe Esposito      * no preferred size
836ebc2752bSEmanuele Giuseppe Esposito      */
837ebc2752bSEmanuele Giuseppe Esposito     uint32_t opt_transfer;
838ebc2752bSEmanuele Giuseppe Esposito 
839ebc2752bSEmanuele Giuseppe Esposito     /*
840ebc2752bSEmanuele Giuseppe Esposito      * Maximal transfer length in bytes.  Need not be power of 2, but
841ebc2752bSEmanuele Giuseppe Esposito      * must be multiple of opt_transfer and bl.request_alignment, or 0
842ebc2752bSEmanuele Giuseppe Esposito      * for no 32-bit limit.  For now, anything larger than INT_MAX is
843ebc2752bSEmanuele Giuseppe Esposito      * clamped down.
844ebc2752bSEmanuele Giuseppe Esposito      */
845ebc2752bSEmanuele Giuseppe Esposito     uint32_t max_transfer;
846ebc2752bSEmanuele Giuseppe Esposito 
847ebc2752bSEmanuele Giuseppe Esposito     /*
848ebc2752bSEmanuele Giuseppe Esposito      * Maximal hardware transfer length in bytes.  Applies whenever
849ebc2752bSEmanuele Giuseppe Esposito      * transfers to the device bypass the kernel I/O scheduler, for
850ebc2752bSEmanuele Giuseppe Esposito      * example with SG_IO.  If larger than max_transfer or if zero,
851ebc2752bSEmanuele Giuseppe Esposito      * blk_get_max_hw_transfer will fall back to max_transfer.
852ebc2752bSEmanuele Giuseppe Esposito      */
853ebc2752bSEmanuele Giuseppe Esposito     uint64_t max_hw_transfer;
854ebc2752bSEmanuele Giuseppe Esposito 
855ebc2752bSEmanuele Giuseppe Esposito     /*
856ebc2752bSEmanuele Giuseppe Esposito      * Maximal number of scatter/gather elements allowed by the hardware.
857ebc2752bSEmanuele Giuseppe Esposito      * Applies whenever transfers to the device bypass the kernel I/O
858ebc2752bSEmanuele Giuseppe Esposito      * scheduler, for example with SG_IO.  If larger than max_iov
859ebc2752bSEmanuele Giuseppe Esposito      * or if zero, blk_get_max_hw_iov will fall back to max_iov.
860ebc2752bSEmanuele Giuseppe Esposito      */
861ebc2752bSEmanuele Giuseppe Esposito     int max_hw_iov;
862ebc2752bSEmanuele Giuseppe Esposito 
863ebc2752bSEmanuele Giuseppe Esposito 
864ebc2752bSEmanuele Giuseppe Esposito     /* memory alignment, in bytes so that no bounce buffer is needed */
865ebc2752bSEmanuele Giuseppe Esposito     size_t min_mem_alignment;
866ebc2752bSEmanuele Giuseppe Esposito 
867ebc2752bSEmanuele Giuseppe Esposito     /* memory alignment, in bytes, for bounce buffer */
868ebc2752bSEmanuele Giuseppe Esposito     size_t opt_mem_alignment;
869ebc2752bSEmanuele Giuseppe Esposito 
870ebc2752bSEmanuele Giuseppe Esposito     /* maximum number of iovec elements */
871ebc2752bSEmanuele Giuseppe Esposito     int max_iov;
872160a29e2SPaolo Bonzini 
873160a29e2SPaolo Bonzini     /*
874160a29e2SPaolo Bonzini      * true if the length of the underlying file can change, and QEMU
875160a29e2SPaolo Bonzini      * is expected to adjust automatically.  Mostly for CD-ROM drives,
876160a29e2SPaolo Bonzini      * whose length is zero when the tray is empty (they don't need
877160a29e2SPaolo Bonzini      * an explicit monitor command to load the disk inside the guest).
878160a29e2SPaolo Bonzini      */
879160a29e2SPaolo Bonzini     bool has_variable_length;
880a735b56eSSam Li 
881a735b56eSSam Li     /* device zone model */
882a735b56eSSam Li     BlockZoneModel zoned;
8836d43eaa3SSam Li 
8846d43eaa3SSam Li     /* zone size expressed in bytes */
8856d43eaa3SSam Li     uint32_t zone_size;
8866d43eaa3SSam Li 
8876d43eaa3SSam Li     /* total number of zones */
8886d43eaa3SSam Li     uint32_t nr_zones;
8896d43eaa3SSam Li 
8906d43eaa3SSam Li     /* maximum sectors of a zone append write operation */
8916d43eaa3SSam Li     uint32_t max_append_sectors;
8926d43eaa3SSam Li 
8936d43eaa3SSam Li     /* maximum number of open zones */
8946d43eaa3SSam Li     uint32_t max_open_zones;
8956d43eaa3SSam Li 
8966d43eaa3SSam Li     /* maximum number of active zones */
8976d43eaa3SSam Li     uint32_t max_active_zones;
898a3c41f06SSam Li 
899a3c41f06SSam Li     uint32_t write_granularity;
900ebc2752bSEmanuele Giuseppe Esposito } BlockLimits;
901ebc2752bSEmanuele Giuseppe Esposito 
902ebc2752bSEmanuele Giuseppe Esposito typedef struct BdrvOpBlocker BdrvOpBlocker;
903ebc2752bSEmanuele Giuseppe Esposito 
904ebc2752bSEmanuele Giuseppe Esposito typedef struct BdrvAioNotifier {
905ebc2752bSEmanuele Giuseppe Esposito     void (*attached_aio_context)(AioContext *new_context, void *opaque);
906ebc2752bSEmanuele Giuseppe Esposito     void (*detach_aio_context)(void *opaque);
907ebc2752bSEmanuele Giuseppe Esposito 
908ebc2752bSEmanuele Giuseppe Esposito     void *opaque;
909ebc2752bSEmanuele Giuseppe Esposito     bool deleted;
910ebc2752bSEmanuele Giuseppe Esposito 
911ebc2752bSEmanuele Giuseppe Esposito     QLIST_ENTRY(BdrvAioNotifier) list;
912ebc2752bSEmanuele Giuseppe Esposito } BdrvAioNotifier;
913ebc2752bSEmanuele Giuseppe Esposito 
914ebc2752bSEmanuele Giuseppe Esposito struct BdrvChildClass {
915ebc2752bSEmanuele Giuseppe Esposito     /*
916ebc2752bSEmanuele Giuseppe Esposito      * If true, bdrv_replace_node() doesn't change the node this BdrvChild
917ebc2752bSEmanuele Giuseppe Esposito      * points to.
918ebc2752bSEmanuele Giuseppe Esposito      */
919ebc2752bSEmanuele Giuseppe Esposito     bool stay_at_node;
920ebc2752bSEmanuele Giuseppe Esposito 
921ebc2752bSEmanuele Giuseppe Esposito     /*
922ebc2752bSEmanuele Giuseppe Esposito      * If true, the parent is a BlockDriverState and bdrv_next_all_states()
923ebc2752bSEmanuele Giuseppe Esposito      * will return it. This information is used for drain_all, where every node
924ebc2752bSEmanuele Giuseppe Esposito      * will be drained separately, so the drain only needs to be propagated to
925ebc2752bSEmanuele Giuseppe Esposito      * non-BDS parents.
926ebc2752bSEmanuele Giuseppe Esposito      */
927ebc2752bSEmanuele Giuseppe Esposito     bool parent_is_bds;
928ebc2752bSEmanuele Giuseppe Esposito 
929abc5a79cSEmanuele Giuseppe Esposito     /*
930abc5a79cSEmanuele Giuseppe Esposito      * Global state (GS) API. These functions run under the BQL.
931abc5a79cSEmanuele Giuseppe Esposito      *
932abc5a79cSEmanuele Giuseppe Esposito      * See include/block/block-global-state.h for more information about
933abc5a79cSEmanuele Giuseppe Esposito      * the GS API.
934abc5a79cSEmanuele Giuseppe Esposito      */
935ebc2752bSEmanuele Giuseppe Esposito     void (*inherit_options)(BdrvChildRole role, bool parent_is_format,
936ebc2752bSEmanuele Giuseppe Esposito                             int *child_flags, QDict *child_options,
937ebc2752bSEmanuele Giuseppe Esposito                             int parent_flags, QDict *parent_options);
938ebc2752bSEmanuele Giuseppe Esposito     void (*change_media)(BdrvChild *child, bool load);
939ebc2752bSEmanuele Giuseppe Esposito 
940ebc2752bSEmanuele Giuseppe Esposito     /*
941ebc2752bSEmanuele Giuseppe Esposito      * Returns a malloced string that describes the parent of the child for a
942ebc2752bSEmanuele Giuseppe Esposito      * human reader. This could be a node-name, BlockBackend name, qdev ID or
943ebc2752bSEmanuele Giuseppe Esposito      * QOM path of the device owning the BlockBackend, job type and ID etc. The
944ebc2752bSEmanuele Giuseppe Esposito      * caller is responsible for freeing the memory.
945ebc2752bSEmanuele Giuseppe Esposito      */
946ebc2752bSEmanuele Giuseppe Esposito     char *(*get_parent_desc)(BdrvChild *child);
947ebc2752bSEmanuele Giuseppe Esposito 
948ebc2752bSEmanuele Giuseppe Esposito     /*
949ebc2752bSEmanuele Giuseppe Esposito      * Notifies the parent that the child has been activated/inactivated (e.g.
950ebc2752bSEmanuele Giuseppe Esposito      * when migration is completing) and it can start/stop requesting
951ebc2752bSEmanuele Giuseppe Esposito      * permissions and doing I/O on it.
952ebc2752bSEmanuele Giuseppe Esposito      */
9533804e3cfSKevin Wolf     void GRAPH_RDLOCK_PTR (*activate)(BdrvChild *child, Error **errp);
9543804e3cfSKevin Wolf     int GRAPH_RDLOCK_PTR (*inactivate)(BdrvChild *child);
955ebc2752bSEmanuele Giuseppe Esposito 
956303de47bSKevin Wolf     void GRAPH_WRLOCK_PTR (*attach)(BdrvChild *child);
957303de47bSKevin Wolf     void GRAPH_WRLOCK_PTR (*detach)(BdrvChild *child);
958ebc2752bSEmanuele Giuseppe Esposito 
959ebc2752bSEmanuele Giuseppe Esposito     /*
960ab613350SStefan Hajnoczi      * If this pair of functions is implemented, the parent doesn't issue new
961ab613350SStefan Hajnoczi      * requests after returning from .drained_begin() until .drained_end() is
962ab613350SStefan Hajnoczi      * called.
963ab613350SStefan Hajnoczi      *
964ab613350SStefan Hajnoczi      * These functions must not change the graph (and therefore also must not
965ab613350SStefan Hajnoczi      * call aio_poll(), which could change the graph indirectly).
966ab613350SStefan Hajnoczi      *
967ab613350SStefan Hajnoczi      * Note that this can be nested. If drained_begin() was called twice, new
968ab613350SStefan Hajnoczi      * I/O is allowed only after drained_end() was called twice, too.
969ab613350SStefan Hajnoczi      */
970d05ab380SEmanuele Giuseppe Esposito     void GRAPH_RDLOCK_PTR (*drained_begin)(BdrvChild *child);
971d05ab380SEmanuele Giuseppe Esposito     void GRAPH_RDLOCK_PTR (*drained_end)(BdrvChild *child);
972ab613350SStefan Hajnoczi 
973ab613350SStefan Hajnoczi     /*
974ab613350SStefan Hajnoczi      * Returns whether the parent has pending requests for the child. This
975ab613350SStefan Hajnoczi      * callback is polled after .drained_begin() has been called until all
976ab613350SStefan Hajnoczi      * activity on the child has stopped.
977ab613350SStefan Hajnoczi      */
978d05ab380SEmanuele Giuseppe Esposito     bool GRAPH_RDLOCK_PTR (*drained_poll)(BdrvChild *child);
979ab613350SStefan Hajnoczi 
980ab613350SStefan Hajnoczi     /*
981ebc2752bSEmanuele Giuseppe Esposito      * Notifies the parent that the filename of its child has changed (e.g.
982ebc2752bSEmanuele Giuseppe Esposito      * because the direct child was removed from the backing chain), so that it
983ebc2752bSEmanuele Giuseppe Esposito      * can update its reference.
984ebc2752bSEmanuele Giuseppe Esposito      */
985ebc2752bSEmanuele Giuseppe Esposito     int (*update_filename)(BdrvChild *child, BlockDriverState *new_base,
986ebc2752bSEmanuele Giuseppe Esposito                            const char *filename, Error **errp);
987ebc2752bSEmanuele Giuseppe Esposito 
9887e8c182fSEmanuele Giuseppe Esposito     bool (*change_aio_ctx)(BdrvChild *child, AioContext *ctx,
989e08cc001SEmanuele Giuseppe Esposito                            GHashTable *visited, Transaction *tran,
990e08cc001SEmanuele Giuseppe Esposito                            Error **errp);
991ebc2752bSEmanuele Giuseppe Esposito 
992abc5a79cSEmanuele Giuseppe Esposito     /*
993abc5a79cSEmanuele Giuseppe Esposito      * I/O API functions. These functions are thread-safe.
994abc5a79cSEmanuele Giuseppe Esposito      *
995abc5a79cSEmanuele Giuseppe Esposito      * See include/block/block-io.h for more information about
996abc5a79cSEmanuele Giuseppe Esposito      * the I/O API.
997abc5a79cSEmanuele Giuseppe Esposito      */
998abc5a79cSEmanuele Giuseppe Esposito 
999abc5a79cSEmanuele Giuseppe Esposito     void (*resize)(BdrvChild *child);
1000abc5a79cSEmanuele Giuseppe Esposito 
1001abc5a79cSEmanuele Giuseppe Esposito     /*
1002abc5a79cSEmanuele Giuseppe Esposito      * Returns a name that is supposedly more useful for human users than the
1003abc5a79cSEmanuele Giuseppe Esposito      * node name for identifying the node in question (in particular, a BB
1004abc5a79cSEmanuele Giuseppe Esposito      * name), or NULL if the parent can't provide a better name.
1005abc5a79cSEmanuele Giuseppe Esposito      */
1006abc5a79cSEmanuele Giuseppe Esposito     const char *(*get_name)(BdrvChild *child);
1007abc5a79cSEmanuele Giuseppe Esposito 
1008d5f8d79cSHanna Reitz     AioContext *(*get_parent_aio_context)(BdrvChild *child);
1009ebc2752bSEmanuele Giuseppe Esposito };
1010ebc2752bSEmanuele Giuseppe Esposito 
1011ebc2752bSEmanuele Giuseppe Esposito extern const BdrvChildClass child_of_bds;
1012ebc2752bSEmanuele Giuseppe Esposito 
1013ebc2752bSEmanuele Giuseppe Esposito struct BdrvChild {
1014ebc2752bSEmanuele Giuseppe Esposito     BlockDriverState *bs;
1015ebc2752bSEmanuele Giuseppe Esposito     char *name;
1016ebc2752bSEmanuele Giuseppe Esposito     const BdrvChildClass *klass;
1017ebc2752bSEmanuele Giuseppe Esposito     BdrvChildRole role;
1018ebc2752bSEmanuele Giuseppe Esposito     void *opaque;
1019ebc2752bSEmanuele Giuseppe Esposito 
1020ebc2752bSEmanuele Giuseppe Esposito     /**
1021ebc2752bSEmanuele Giuseppe Esposito      * Granted permissions for operating on this BdrvChild (BLK_PERM_* bitmask)
1022ebc2752bSEmanuele Giuseppe Esposito      */
1023ebc2752bSEmanuele Giuseppe Esposito     uint64_t perm;
1024ebc2752bSEmanuele Giuseppe Esposito 
1025ebc2752bSEmanuele Giuseppe Esposito     /**
1026ebc2752bSEmanuele Giuseppe Esposito      * Permissions that can still be granted to other users of @bs while this
1027ebc2752bSEmanuele Giuseppe Esposito      * BdrvChild is still attached to it. (BLK_PERM_* bitmask)
1028ebc2752bSEmanuele Giuseppe Esposito      */
1029ebc2752bSEmanuele Giuseppe Esposito     uint64_t shared_perm;
1030ebc2752bSEmanuele Giuseppe Esposito 
1031ebc2752bSEmanuele Giuseppe Esposito     /*
1032ebc2752bSEmanuele Giuseppe Esposito      * This link is frozen: the child can neither be replaced nor
1033ebc2752bSEmanuele Giuseppe Esposito      * detached from the parent.
1034ebc2752bSEmanuele Giuseppe Esposito      */
1035ebc2752bSEmanuele Giuseppe Esposito     bool frozen;
1036ebc2752bSEmanuele Giuseppe Esposito 
1037ebc2752bSEmanuele Giuseppe Esposito     /*
103857e05be3SKevin Wolf      * True if the parent of this child has been drained by this BdrvChild
1039ebc2752bSEmanuele Giuseppe Esposito      * (through klass->drained_*).
104057e05be3SKevin Wolf      *
104157e05be3SKevin Wolf      * It is generally true if bs->quiesce_counter > 0. It may differ while the
1042ebc2752bSEmanuele Giuseppe Esposito      * child is entering or leaving a drained section.
1043ebc2752bSEmanuele Giuseppe Esposito      */
104457e05be3SKevin Wolf     bool quiesced_parent;
1045ebc2752bSEmanuele Giuseppe Esposito 
1046680e0cc4SKevin Wolf     QLIST_ENTRY(BdrvChild GRAPH_RDLOCK_PTR) next;
1047b59b4660SKevin Wolf     QLIST_ENTRY(BdrvChild GRAPH_RDLOCK_PTR) next_parent;
1048ebc2752bSEmanuele Giuseppe Esposito };
1049ebc2752bSEmanuele Giuseppe Esposito 
1050ebc2752bSEmanuele Giuseppe Esposito /*
1051ebc2752bSEmanuele Giuseppe Esposito  * Allows bdrv_co_block_status() to cache one data region for a
1052ebc2752bSEmanuele Giuseppe Esposito  * protocol node.
1053ebc2752bSEmanuele Giuseppe Esposito  *
1054ebc2752bSEmanuele Giuseppe Esposito  * @valid: Whether the cache is valid (should be accessed with atomic
1055ebc2752bSEmanuele Giuseppe Esposito  *         functions so this can be reset by RCU readers)
1056ebc2752bSEmanuele Giuseppe Esposito  * @data_start: Offset where we know (or strongly assume) is data
1057ebc2752bSEmanuele Giuseppe Esposito  * @data_end: Offset where the data region ends (which is not necessarily
1058ebc2752bSEmanuele Giuseppe Esposito  *            the start of a zeroed region)
1059ebc2752bSEmanuele Giuseppe Esposito  */
1060ebc2752bSEmanuele Giuseppe Esposito typedef struct BdrvBlockStatusCache {
1061ebc2752bSEmanuele Giuseppe Esposito     struct rcu_head rcu;
1062ebc2752bSEmanuele Giuseppe Esposito 
1063ebc2752bSEmanuele Giuseppe Esposito     bool valid;
1064ebc2752bSEmanuele Giuseppe Esposito     int64_t data_start;
1065ebc2752bSEmanuele Giuseppe Esposito     int64_t data_end;
1066ebc2752bSEmanuele Giuseppe Esposito } BdrvBlockStatusCache;
1067ebc2752bSEmanuele Giuseppe Esposito 
1068ebc2752bSEmanuele Giuseppe Esposito struct BlockDriverState {
1069ebc2752bSEmanuele Giuseppe Esposito     /*
1070ebc2752bSEmanuele Giuseppe Esposito      * Protected by big QEMU lock or read-only after opening.  No special
1071ebc2752bSEmanuele Giuseppe Esposito      * locking needed during I/O...
1072ebc2752bSEmanuele Giuseppe Esposito      */
1073ebc2752bSEmanuele Giuseppe Esposito     int open_flags; /* flags used to open the file, re-used for re-open */
1074ebc2752bSEmanuele Giuseppe Esposito     bool encrypted; /* if true, the media is encrypted */
1075ebc2752bSEmanuele Giuseppe Esposito     bool sg;        /* if true, the device is a /dev/sg* */
1076ebc2752bSEmanuele Giuseppe Esposito     bool probed;    /* if true, format was probed rather than specified */
1077ebc2752bSEmanuele Giuseppe Esposito     bool force_share; /* if true, always allow all shared permissions */
1078ebc2752bSEmanuele Giuseppe Esposito     bool implicit;  /* if true, this filter node was automatically inserted */
1079ebc2752bSEmanuele Giuseppe Esposito 
1080ebc2752bSEmanuele Giuseppe Esposito     BlockDriver *drv; /* NULL means no media */
1081ebc2752bSEmanuele Giuseppe Esposito     void *opaque;
1082ebc2752bSEmanuele Giuseppe Esposito 
1083ebc2752bSEmanuele Giuseppe Esposito     AioContext *aio_context; /* event loop used for fd handlers, timers, etc */
1084ebc2752bSEmanuele Giuseppe Esposito     /*
1085ebc2752bSEmanuele Giuseppe Esposito      * long-running tasks intended to always use the same AioContext as this
1086ebc2752bSEmanuele Giuseppe Esposito      * BDS may register themselves in this list to be notified of changes
1087ebc2752bSEmanuele Giuseppe Esposito      * regarding this BDS's context
1088ebc2752bSEmanuele Giuseppe Esposito      */
1089ebc2752bSEmanuele Giuseppe Esposito     QLIST_HEAD(, BdrvAioNotifier) aio_notifiers;
1090ebc2752bSEmanuele Giuseppe Esposito     bool walking_aio_notifiers; /* to make removal during iteration safe */
1091ebc2752bSEmanuele Giuseppe Esposito 
1092ebc2752bSEmanuele Giuseppe Esposito     char filename[PATH_MAX];
1093ebc2752bSEmanuele Giuseppe Esposito     /*
1094ebc2752bSEmanuele Giuseppe Esposito      * If not empty, this image is a diff in relation to backing_file.
1095ebc2752bSEmanuele Giuseppe Esposito      * Note that this is the name given in the image header and
1096ebc2752bSEmanuele Giuseppe Esposito      * therefore may or may not be equal to .backing->bs->filename.
1097ebc2752bSEmanuele Giuseppe Esposito      * If this field contains a relative path, it is to be resolved
1098ebc2752bSEmanuele Giuseppe Esposito      * relatively to the overlay's location.
1099ebc2752bSEmanuele Giuseppe Esposito      */
1100ebc2752bSEmanuele Giuseppe Esposito     char backing_file[PATH_MAX];
1101ebc2752bSEmanuele Giuseppe Esposito     /*
1102ebc2752bSEmanuele Giuseppe Esposito      * The backing filename indicated by the image header.  Contrary
1103ebc2752bSEmanuele Giuseppe Esposito      * to backing_file, if we ever open this file, auto_backing_file
1104ebc2752bSEmanuele Giuseppe Esposito      * is replaced by the resulting BDS's filename (i.e. after a
1105ebc2752bSEmanuele Giuseppe Esposito      * bdrv_refresh_filename() run).
1106ebc2752bSEmanuele Giuseppe Esposito      */
1107ebc2752bSEmanuele Giuseppe Esposito     char auto_backing_file[PATH_MAX];
1108ebc2752bSEmanuele Giuseppe Esposito     char backing_format[16]; /* if non-zero and backing_file exists */
1109ebc2752bSEmanuele Giuseppe Esposito 
1110ebc2752bSEmanuele Giuseppe Esposito     QDict *full_open_options;
1111ebc2752bSEmanuele Giuseppe Esposito     char exact_filename[PATH_MAX];
1112ebc2752bSEmanuele Giuseppe Esposito 
1113ebc2752bSEmanuele Giuseppe Esposito     /* I/O Limits */
1114ebc2752bSEmanuele Giuseppe Esposito     BlockLimits bl;
1115ebc2752bSEmanuele Giuseppe Esposito 
1116ebc2752bSEmanuele Giuseppe Esposito     /*
1117ebc2752bSEmanuele Giuseppe Esposito      * Flags honored during pread
1118ebc2752bSEmanuele Giuseppe Esposito      */
111998b3ddc7SStefan Hajnoczi     BdrvRequestFlags supported_read_flags;
1120ebc2752bSEmanuele Giuseppe Esposito     /*
1121ebc2752bSEmanuele Giuseppe Esposito      * Flags honored during pwrite (so far: BDRV_REQ_FUA,
1122ebc2752bSEmanuele Giuseppe Esposito      * BDRV_REQ_WRITE_UNCHANGED).
1123ebc2752bSEmanuele Giuseppe Esposito      * If a driver does not support BDRV_REQ_WRITE_UNCHANGED, those
1124ebc2752bSEmanuele Giuseppe Esposito      * writes will be issued as normal writes without the flag set.
1125ebc2752bSEmanuele Giuseppe Esposito      * This is important to note for drivers that do not explicitly
1126ebc2752bSEmanuele Giuseppe Esposito      * request a WRITE permission for their children and instead take
1127ebc2752bSEmanuele Giuseppe Esposito      * the same permissions as their parent did (this is commonly what
1128ebc2752bSEmanuele Giuseppe Esposito      * block filters do).  Such drivers have to be aware that the
1129ebc2752bSEmanuele Giuseppe Esposito      * parent may have taken a WRITE_UNCHANGED permission only and is
1130ebc2752bSEmanuele Giuseppe Esposito      * issuing such requests.  Drivers either must make sure that
1131ebc2752bSEmanuele Giuseppe Esposito      * these requests do not result in plain WRITE accesses (usually
1132ebc2752bSEmanuele Giuseppe Esposito      * by supporting BDRV_REQ_WRITE_UNCHANGED, and then forwarding
1133ebc2752bSEmanuele Giuseppe Esposito      * every incoming write request as-is, including potentially that
1134ebc2752bSEmanuele Giuseppe Esposito      * flag), or they have to explicitly take the WRITE permission for
1135ebc2752bSEmanuele Giuseppe Esposito      * their children.
1136ebc2752bSEmanuele Giuseppe Esposito      */
113798b3ddc7SStefan Hajnoczi     BdrvRequestFlags supported_write_flags;
1138ebc2752bSEmanuele Giuseppe Esposito     /*
1139ebc2752bSEmanuele Giuseppe Esposito      * Flags honored during pwrite_zeroes (so far: BDRV_REQ_FUA,
1140ebc2752bSEmanuele Giuseppe Esposito      * BDRV_REQ_MAY_UNMAP, BDRV_REQ_WRITE_UNCHANGED)
1141ebc2752bSEmanuele Giuseppe Esposito      */
114298b3ddc7SStefan Hajnoczi     BdrvRequestFlags supported_zero_flags;
1143ebc2752bSEmanuele Giuseppe Esposito     /*
1144ebc2752bSEmanuele Giuseppe Esposito      * Flags honoured during truncate (so far: BDRV_REQ_ZERO_WRITE).
1145ebc2752bSEmanuele Giuseppe Esposito      *
1146ebc2752bSEmanuele Giuseppe Esposito      * If BDRV_REQ_ZERO_WRITE is given, the truncate operation must make sure
1147ebc2752bSEmanuele Giuseppe Esposito      * that any added space reads as all zeros. If this can't be guaranteed,
1148ebc2752bSEmanuele Giuseppe Esposito      * the operation must fail.
1149ebc2752bSEmanuele Giuseppe Esposito      */
115098b3ddc7SStefan Hajnoczi     BdrvRequestFlags supported_truncate_flags;
1151ebc2752bSEmanuele Giuseppe Esposito 
1152ebc2752bSEmanuele Giuseppe Esposito     /* the following member gives a name to every node on the bs graph. */
1153ebc2752bSEmanuele Giuseppe Esposito     char node_name[32];
1154ebc2752bSEmanuele Giuseppe Esposito     /* element of the list of named nodes building the graph */
1155ebc2752bSEmanuele Giuseppe Esposito     QTAILQ_ENTRY(BlockDriverState) node_list;
1156ebc2752bSEmanuele Giuseppe Esposito     /* element of the list of all BlockDriverStates (all_bdrv_states) */
1157ebc2752bSEmanuele Giuseppe Esposito     QTAILQ_ENTRY(BlockDriverState) bs_list;
1158ebc2752bSEmanuele Giuseppe Esposito     /* element of the list of monitor-owned BDS */
1159ebc2752bSEmanuele Giuseppe Esposito     QTAILQ_ENTRY(BlockDriverState) monitor_list;
1160ebc2752bSEmanuele Giuseppe Esposito     int refcnt;
1161ebc2752bSEmanuele Giuseppe Esposito 
1162ebc2752bSEmanuele Giuseppe Esposito     /* operation blockers. Protected by BQL. */
1163ebc2752bSEmanuele Giuseppe Esposito     QLIST_HEAD(, BdrvOpBlocker) op_blockers[BLOCK_OP_TYPE_MAX];
1164ebc2752bSEmanuele Giuseppe Esposito 
1165ebc2752bSEmanuele Giuseppe Esposito     /*
1166ebc2752bSEmanuele Giuseppe Esposito      * The node that this node inherited default options from (and a reopen on
1167ebc2752bSEmanuele Giuseppe Esposito      * which can affect this node by changing these defaults). This is always a
1168ebc2752bSEmanuele Giuseppe Esposito      * parent node of this node.
1169ebc2752bSEmanuele Giuseppe Esposito      */
1170ebc2752bSEmanuele Giuseppe Esposito     BlockDriverState *inherits_from;
11715bb04747SVladimir Sementsov-Ogievskiy 
11725bb04747SVladimir Sementsov-Ogievskiy     /*
11735bb04747SVladimir Sementsov-Ogievskiy      * @backing and @file are some of @children or NULL. All these three fields
11745bb04747SVladimir Sementsov-Ogievskiy      * (@file, @backing and @children) are modified only in
11755bb04747SVladimir Sementsov-Ogievskiy      * bdrv_child_cb_attach() and bdrv_child_cb_detach().
11765bb04747SVladimir Sementsov-Ogievskiy      *
11775bb04747SVladimir Sementsov-Ogievskiy      * See also comment in include/block/block.h, to learn how backing and file
11785bb04747SVladimir Sementsov-Ogievskiy      * are connected with BdrvChildRole.
11795bb04747SVladimir Sementsov-Ogievskiy      */
1180680e0cc4SKevin Wolf     QLIST_HEAD(, BdrvChild GRAPH_RDLOCK_PTR) children;
11815bb04747SVladimir Sementsov-Ogievskiy     BdrvChild *backing;
11825bb04747SVladimir Sementsov-Ogievskiy     BdrvChild *file;
11835bb04747SVladimir Sementsov-Ogievskiy 
1184b59b4660SKevin Wolf     QLIST_HEAD(, BdrvChild GRAPH_RDLOCK_PTR) parents;
1185ebc2752bSEmanuele Giuseppe Esposito 
1186ebc2752bSEmanuele Giuseppe Esposito     QDict *options;
1187ebc2752bSEmanuele Giuseppe Esposito     QDict *explicit_options;
1188ebc2752bSEmanuele Giuseppe Esposito     BlockdevDetectZeroesOptions detect_zeroes;
1189ebc2752bSEmanuele Giuseppe Esposito 
1190ebc2752bSEmanuele Giuseppe Esposito     /* The error object in use for blocking operations on backing_hd */
1191ebc2752bSEmanuele Giuseppe Esposito     Error *backing_blocker;
1192ebc2752bSEmanuele Giuseppe Esposito 
1193ebc2752bSEmanuele Giuseppe Esposito     /* Protected by AioContext lock */
1194ebc2752bSEmanuele Giuseppe Esposito 
1195ebc2752bSEmanuele Giuseppe Esposito     /*
1196ebc2752bSEmanuele Giuseppe Esposito      * If we are reading a disk image, give its size in sectors.
1197ebc2752bSEmanuele Giuseppe Esposito      * Generally read-only; it is written to by load_snapshot and
1198ebc2752bSEmanuele Giuseppe Esposito      * save_snaphost, but the block layer is quiescent during those.
1199ebc2752bSEmanuele Giuseppe Esposito      */
1200ebc2752bSEmanuele Giuseppe Esposito     int64_t total_sectors;
1201ebc2752bSEmanuele Giuseppe Esposito 
1202ebc2752bSEmanuele Giuseppe Esposito     /* threshold limit for writes, in bytes. "High water mark". */
1203ebc2752bSEmanuele Giuseppe Esposito     uint64_t write_threshold_offset;
1204ebc2752bSEmanuele Giuseppe Esposito 
1205ebc2752bSEmanuele Giuseppe Esposito     /*
1206ebc2752bSEmanuele Giuseppe Esposito      * Writing to the list requires the BQL _and_ the dirty_bitmap_mutex.
1207ebc2752bSEmanuele Giuseppe Esposito      * Reading from the list can be done with either the BQL or the
1208ebc2752bSEmanuele Giuseppe Esposito      * dirty_bitmap_mutex.  Modifying a bitmap only requires
1209ebc2752bSEmanuele Giuseppe Esposito      * dirty_bitmap_mutex.
1210ebc2752bSEmanuele Giuseppe Esposito      */
1211ebc2752bSEmanuele Giuseppe Esposito     QemuMutex dirty_bitmap_mutex;
1212ebc2752bSEmanuele Giuseppe Esposito     QLIST_HEAD(, BdrvDirtyBitmap) dirty_bitmaps;
1213ebc2752bSEmanuele Giuseppe Esposito 
1214ebc2752bSEmanuele Giuseppe Esposito     /* Offset after the highest byte written to */
1215ebc2752bSEmanuele Giuseppe Esposito     Stat64 wr_highest_offset;
1216ebc2752bSEmanuele Giuseppe Esposito 
1217ebc2752bSEmanuele Giuseppe Esposito     /*
1218ebc2752bSEmanuele Giuseppe Esposito      * If true, copy read backing sectors into image.  Can be >1 if more
1219ebc2752bSEmanuele Giuseppe Esposito      * than one client has requested copy-on-read.  Accessed with atomic
1220ebc2752bSEmanuele Giuseppe Esposito      * ops.
1221ebc2752bSEmanuele Giuseppe Esposito      */
1222ebc2752bSEmanuele Giuseppe Esposito     int copy_on_read;
1223ebc2752bSEmanuele Giuseppe Esposito 
1224ebc2752bSEmanuele Giuseppe Esposito     /*
1225ebc2752bSEmanuele Giuseppe Esposito      * number of in-flight requests; overall and serialising.
1226ebc2752bSEmanuele Giuseppe Esposito      * Accessed with atomic ops.
1227ebc2752bSEmanuele Giuseppe Esposito      */
1228ebc2752bSEmanuele Giuseppe Esposito     unsigned int in_flight;
1229ebc2752bSEmanuele Giuseppe Esposito     unsigned int serialising_in_flight;
1230ebc2752bSEmanuele Giuseppe Esposito 
1231ebc2752bSEmanuele Giuseppe Esposito     /* do we need to tell the quest if we have a volatile write cache? */
1232ebc2752bSEmanuele Giuseppe Esposito     int enable_write_cache;
1233ebc2752bSEmanuele Giuseppe Esposito 
1234ebc2752bSEmanuele Giuseppe Esposito     /* Accessed with atomic ops.  */
1235ebc2752bSEmanuele Giuseppe Esposito     int quiesce_counter;
1236ebc2752bSEmanuele Giuseppe Esposito 
1237ebc2752bSEmanuele Giuseppe Esposito     unsigned int write_gen;               /* Current data generation */
1238ebc2752bSEmanuele Giuseppe Esposito 
1239ebc2752bSEmanuele Giuseppe Esposito     /* Protected by reqs_lock.  */
1240fa9185fcSStefan Hajnoczi     QemuMutex reqs_lock;
1241ebc2752bSEmanuele Giuseppe Esposito     QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
1242ebc2752bSEmanuele Giuseppe Esposito     CoQueue flush_queue;                  /* Serializing flush queue */
1243ebc2752bSEmanuele Giuseppe Esposito     bool active_flush_req;                /* Flush request in flight? */
1244ebc2752bSEmanuele Giuseppe Esposito 
1245ebc2752bSEmanuele Giuseppe Esposito     /* Only read/written by whoever has set active_flush_req to true.  */
1246ebc2752bSEmanuele Giuseppe Esposito     unsigned int flushed_gen;             /* Flushed write generation */
1247ebc2752bSEmanuele Giuseppe Esposito 
1248ebc2752bSEmanuele Giuseppe Esposito     /* BdrvChild links to this node may never be frozen */
1249ebc2752bSEmanuele Giuseppe Esposito     bool never_freeze;
1250ebc2752bSEmanuele Giuseppe Esposito 
1251ebc2752bSEmanuele Giuseppe Esposito     /* Lock for block-status cache RCU writers */
1252ebc2752bSEmanuele Giuseppe Esposito     CoMutex bsc_modify_lock;
1253ebc2752bSEmanuele Giuseppe Esposito     /* Always non-NULL, but must only be dereferenced under an RCU read guard */
1254ebc2752bSEmanuele Giuseppe Esposito     BdrvBlockStatusCache *block_status_cache;
1255a3c41f06SSam Li 
1256a3c41f06SSam Li     /* array of write pointers' location of each zone in the zoned device. */
1257a3c41f06SSam Li     BlockZoneWps *wps;
1258ebc2752bSEmanuele Giuseppe Esposito };
1259ebc2752bSEmanuele Giuseppe Esposito 
1260ebc2752bSEmanuele Giuseppe Esposito struct BlockBackendRootState {
1261ebc2752bSEmanuele Giuseppe Esposito     int open_flags;
1262ebc2752bSEmanuele Giuseppe Esposito     BlockdevDetectZeroesOptions detect_zeroes;
1263ebc2752bSEmanuele Giuseppe Esposito };
1264ebc2752bSEmanuele Giuseppe Esposito 
1265ebc2752bSEmanuele Giuseppe Esposito typedef enum BlockMirrorBackingMode {
1266ebc2752bSEmanuele Giuseppe Esposito     /*
1267ebc2752bSEmanuele Giuseppe Esposito      * Reuse the existing backing chain from the source for the target.
1268ebc2752bSEmanuele Giuseppe Esposito      * - sync=full: Set backing BDS to NULL.
1269ebc2752bSEmanuele Giuseppe Esposito      * - sync=top:  Use source's backing BDS.
1270ebc2752bSEmanuele Giuseppe Esposito      * - sync=none: Use source as the backing BDS.
1271ebc2752bSEmanuele Giuseppe Esposito      */
1272ebc2752bSEmanuele Giuseppe Esposito     MIRROR_SOURCE_BACKING_CHAIN,
1273ebc2752bSEmanuele Giuseppe Esposito 
1274ebc2752bSEmanuele Giuseppe Esposito     /* Open the target's backing chain completely anew */
1275ebc2752bSEmanuele Giuseppe Esposito     MIRROR_OPEN_BACKING_CHAIN,
1276ebc2752bSEmanuele Giuseppe Esposito 
1277ebc2752bSEmanuele Giuseppe Esposito     /* Do not change the target's backing BDS after job completion */
1278ebc2752bSEmanuele Giuseppe Esposito     MIRROR_LEAVE_BACKING_CHAIN,
1279ebc2752bSEmanuele Giuseppe Esposito } BlockMirrorBackingMode;
1280ebc2752bSEmanuele Giuseppe Esposito 
1281ebc2752bSEmanuele Giuseppe Esposito 
1282ebc2752bSEmanuele Giuseppe Esposito /*
1283ebc2752bSEmanuele Giuseppe Esposito  * Essential block drivers which must always be statically linked into qemu, and
1284ebc2752bSEmanuele Giuseppe Esposito  * which therefore can be accessed without using bdrv_find_format()
1285ebc2752bSEmanuele Giuseppe Esposito  */
1286ebc2752bSEmanuele Giuseppe Esposito extern BlockDriver bdrv_file;
1287ebc2752bSEmanuele Giuseppe Esposito extern BlockDriver bdrv_raw;
1288ebc2752bSEmanuele Giuseppe Esposito extern BlockDriver bdrv_qcow2;
1289ebc2752bSEmanuele Giuseppe Esposito 
1290ebc2752bSEmanuele Giuseppe Esposito extern unsigned int bdrv_drain_all_count;
1291ebc2752bSEmanuele Giuseppe Esposito extern QemuOptsList bdrv_create_opts_simple;
1292ebc2752bSEmanuele Giuseppe Esposito 
1293ebc2752bSEmanuele Giuseppe Esposito /*
1294ebc2752bSEmanuele Giuseppe Esposito  * Common functions that are neither I/O nor Global State.
1295ebc2752bSEmanuele Giuseppe Esposito  *
129604ae220dSWilfred Mallawa  * See include/block/block-common.h for more information about
1297ebc2752bSEmanuele Giuseppe Esposito  * the Common API.
1298ebc2752bSEmanuele Giuseppe Esposito  */
1299ebc2752bSEmanuele Giuseppe Esposito 
1300ebc2752bSEmanuele Giuseppe Esposito static inline BlockDriverState *child_bs(BdrvChild *child)
1301ebc2752bSEmanuele Giuseppe Esposito {
1302ebc2752bSEmanuele Giuseppe Esposito     return child ? child->bs : NULL;
1303ebc2752bSEmanuele Giuseppe Esposito }
1304ebc2752bSEmanuele Giuseppe Esposito 
1305ebc2752bSEmanuele Giuseppe Esposito int bdrv_check_request(int64_t offset, int64_t bytes, Error **errp);
130669fbfff9SBin Meng char *create_tmp_file(Error **errp);
1307ebc2752bSEmanuele Giuseppe Esposito void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix,
1308ebc2752bSEmanuele Giuseppe Esposito                                       QDict *options);
1309ebc2752bSEmanuele Giuseppe Esposito 
1310ebc2752bSEmanuele Giuseppe Esposito 
1311ebc2752bSEmanuele Giuseppe Esposito int bdrv_check_qiov_request(int64_t offset, int64_t bytes,
1312ebc2752bSEmanuele Giuseppe Esposito                             QEMUIOVector *qiov, size_t qiov_offset,
1313ebc2752bSEmanuele Giuseppe Esposito                             Error **errp);
1314ebc2752bSEmanuele Giuseppe Esposito 
1315ebc2752bSEmanuele Giuseppe Esposito #ifdef _WIN32
1316ebc2752bSEmanuele Giuseppe Esposito int is_windows_drive(const char *filename);
1317ebc2752bSEmanuele Giuseppe Esposito #endif
1318ebc2752bSEmanuele Giuseppe Esposito 
1319ebc2752bSEmanuele Giuseppe Esposito #endif /* BLOCK_INT_COMMON_H */
1320