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