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