2 * QEMU System Emulator block driver
4 * Copyright (c) 2003 Fabrice Bellard
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:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
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
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"
38 #define BLOCK_FLAG_LAZY_REFCOUNTS 8
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"
64 #define BLOCK_PROBE_BUF_SIZE 512
66 enum BdrvTrackedRequestType
{
70 BDRV_TRACKED_TRUNCATE
,
73 typedef struct BdrvTrackedRequest
{
77 enum BdrvTrackedRequestType type
;
80 int64_t overlap_offset
;
81 uint64_t overlap_bytes
;
83 QLIST_ENTRY(BdrvTrackedRequest
) list
;
84 Coroutine
*co
; /* owner, used for deadlock detection */
85 CoQueue wait_queue
; /* coroutines blocked on this request */
87 struct BdrvTrackedRequest
*waiting_for
;
91 const char *format_name
;
94 /* set to true if the BlockDriver is a block filter. Block filters pass
95 * certain callbacks that refer to data (see block.c) to their bs->file
96 * or bs->backing (whichever one exists) if the driver doesn't implement
97 * them. Drivers that do not wish to forward must implement them and return
99 * Note that filters are not allowed to modify data.
101 * Filters generally cannot have more than a single filtered child,
102 * because the data they present must at all times be the same as
103 * that on their filtered child. That would be impossible to
104 * achieve for multiple filtered children.
105 * (And this filtered child must then be bs->file or bs->backing.)
109 * Set to true if the BlockDriver is a format driver. Format nodes
110 * generally do not expect their children to be other format nodes
111 * (except for backing files), and so format probing is disabled
116 * Return true if @to_replace can be replaced by a BDS with the
117 * same data as @bs without it affecting @bs's behavior (that is,
118 * without it being visible to @bs's parents).
120 bool (*bdrv_recurse_can_replace
)(BlockDriverState
*bs
,
121 BlockDriverState
*to_replace
);
123 int (*bdrv_probe
)(const uint8_t *buf
, int buf_size
, const char *filename
);
124 int (*bdrv_probe_device
)(const char *filename
);
126 /* Any driver implementing this callback is expected to be able to handle
127 * NULL file names in its .bdrv_open() implementation */
128 void (*bdrv_parse_filename
)(const char *filename
, QDict
*options
, Error
**errp
);
129 /* Drivers not implementing bdrv_parse_filename nor bdrv_open should have
130 * this field set to true, except ones that are defined only by their
132 * An example of the last type will be the quorum block driver.
134 bool bdrv_needs_filename
;
137 * Set if a driver can support backing files. This also implies the
138 * following semantics:
140 * - Return status 0 of .bdrv_co_block_status means that corresponding
141 * blocks are not allocated in this layer of backing-chain
142 * - For such (unallocated) blocks, read will:
143 * - fill buffer with zeros if there is no backing file
144 * - read from the backing file otherwise, where the block layer
145 * takes care of reading zeros beyond EOF if backing file is short
147 bool supports_backing
;
149 /* For handling image reopen for split or non-split files */
150 int (*bdrv_reopen_prepare
)(BDRVReopenState
*reopen_state
,
151 BlockReopenQueue
*queue
, Error
**errp
);
152 void (*bdrv_reopen_commit
)(BDRVReopenState
*reopen_state
);
153 void (*bdrv_reopen_commit_post
)(BDRVReopenState
*reopen_state
);
154 void (*bdrv_reopen_abort
)(BDRVReopenState
*reopen_state
);
155 void (*bdrv_join_options
)(QDict
*options
, QDict
*old_options
);
157 int (*bdrv_open
)(BlockDriverState
*bs
, QDict
*options
, int flags
,
160 /* Protocol drivers should implement this instead of bdrv_open */
161 int (*bdrv_file_open
)(BlockDriverState
*bs
, QDict
*options
, int flags
,
163 void (*bdrv_close
)(BlockDriverState
*bs
);
166 int coroutine_fn (*bdrv_co_create
)(BlockdevCreateOptions
*opts
,
168 int coroutine_fn (*bdrv_co_create_opts
)(BlockDriver
*drv
,
169 const char *filename
,
173 int coroutine_fn (*bdrv_co_amend
)(BlockDriverState
*bs
,
174 BlockdevAmendOptions
*opts
,
178 int (*bdrv_amend_options
)(BlockDriverState
*bs
,
180 BlockDriverAmendStatusCB
*status_cb
,
185 int (*bdrv_make_empty
)(BlockDriverState
*bs
);
188 * Refreshes the bs->exact_filename field. If that is impossible,
189 * bs->exact_filename has to be left empty.
191 void (*bdrv_refresh_filename
)(BlockDriverState
*bs
);
194 * Gathers the open options for all children into @target.
195 * A simple format driver (without backing file support) might
196 * implement this function like this:
198 * QINCREF(bs->file->bs->full_open_options);
199 * qdict_put(target, "file", bs->file->bs->full_open_options);
201 * If not specified, the generic implementation will simply put
202 * all children's options under their respective name.
204 * @backing_overridden is true when bs->backing seems not to be
205 * the child that would result from opening bs->backing_file.
206 * Therefore, if it is true, the backing child's options should be
207 * gathered; otherwise, there is no need since the backing child
208 * is the one implied by the image header.
210 * Note that ideally this function would not be needed. Every
211 * block driver which implements it is probably doing something
212 * shady regarding its runtime option structure.
214 void (*bdrv_gather_child_options
)(BlockDriverState
*bs
, QDict
*target
,
215 bool backing_overridden
);
218 * Returns an allocated string which is the directory name of this BDS: It
219 * will be used to make relative filenames absolute by prepending this
220 * function's return value to them.
222 char *(*bdrv_dirname
)(BlockDriverState
*bs
, Error
**errp
);
225 BlockAIOCB
*(*bdrv_aio_preadv
)(BlockDriverState
*bs
,
226 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
,
227 BlockCompletionFunc
*cb
, void *opaque
);
228 BlockAIOCB
*(*bdrv_aio_pwritev
)(BlockDriverState
*bs
,
229 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
,
230 BlockCompletionFunc
*cb
, void *opaque
);
231 BlockAIOCB
*(*bdrv_aio_flush
)(BlockDriverState
*bs
,
232 BlockCompletionFunc
*cb
, void *opaque
);
233 BlockAIOCB
*(*bdrv_aio_pdiscard
)(BlockDriverState
*bs
,
234 int64_t offset
, int bytes
,
235 BlockCompletionFunc
*cb
, void *opaque
);
237 int coroutine_fn (*bdrv_co_readv
)(BlockDriverState
*bs
,
238 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
);
241 * @offset: position in bytes to read at
242 * @bytes: number of bytes to read
243 * @qiov: the buffers to fill with read data
244 * @flags: currently unused, always 0
246 * @offset and @bytes will be a multiple of 'request_alignment',
247 * but the length of individual @qiov elements does not have to
250 * @bytes will always equal the total size of @qiov, and will be
251 * no larger than 'max_transfer'.
253 * The buffer in @qiov may point directly to guest memory.
255 int coroutine_fn (*bdrv_co_preadv
)(BlockDriverState
*bs
,
256 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
);
257 int coroutine_fn (*bdrv_co_preadv_part
)(BlockDriverState
*bs
,
258 uint64_t offset
, uint64_t bytes
,
259 QEMUIOVector
*qiov
, size_t qiov_offset
, int flags
);
260 int coroutine_fn (*bdrv_co_writev
)(BlockDriverState
*bs
,
261 int64_t sector_num
, int nb_sectors
, QEMUIOVector
*qiov
, int flags
);
263 * @offset: position in bytes to write at
264 * @bytes: number of bytes to write
265 * @qiov: the buffers containing data to write
266 * @flags: zero or more bits allowed by 'supported_write_flags'
268 * @offset and @bytes will be a multiple of 'request_alignment',
269 * but the length of individual @qiov elements does not have to
272 * @bytes will always equal the total size of @qiov, and will be
273 * no larger than 'max_transfer'.
275 * The buffer in @qiov may point directly to guest memory.
277 int coroutine_fn (*bdrv_co_pwritev
)(BlockDriverState
*bs
,
278 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
, int flags
);
279 int coroutine_fn (*bdrv_co_pwritev_part
)(BlockDriverState
*bs
,
280 uint64_t offset
, uint64_t bytes
,
281 QEMUIOVector
*qiov
, size_t qiov_offset
, int flags
);
284 * Efficiently zero a region of the disk image. Typically an image format
285 * would use a compact metadata representation to implement this. This
286 * function pointer may be NULL or return -ENOSUP and .bdrv_co_writev()
287 * will be called instead.
289 int coroutine_fn (*bdrv_co_pwrite_zeroes
)(BlockDriverState
*bs
,
290 int64_t offset
, int bytes
, BdrvRequestFlags flags
);
291 int coroutine_fn (*bdrv_co_pdiscard
)(BlockDriverState
*bs
,
292 int64_t offset
, int bytes
);
294 /* Map [offset, offset + nbytes) range onto a child of @bs to copy from,
295 * and invoke bdrv_co_copy_range_from(child, ...), or invoke
296 * bdrv_co_copy_range_to() if @bs is the leaf child to copy data from.
298 * See the comment of bdrv_co_copy_range for the parameter and return value
301 int coroutine_fn (*bdrv_co_copy_range_from
)(BlockDriverState
*bs
,
307 BdrvRequestFlags read_flags
,
308 BdrvRequestFlags write_flags
);
310 /* Map [offset, offset + nbytes) range onto a child of bs to copy data to,
311 * and invoke bdrv_co_copy_range_to(child, src, ...), or perform the copy
312 * operation if @bs is the leaf and @src has the same BlockDriver. Return
313 * -ENOTSUP if @bs is the leaf but @src has a different BlockDriver.
315 * See the comment of bdrv_co_copy_range for the parameter and return value
318 int coroutine_fn (*bdrv_co_copy_range_to
)(BlockDriverState
*bs
,
324 BdrvRequestFlags read_flags
,
325 BdrvRequestFlags write_flags
);
328 * Building block for bdrv_block_status[_above] and
329 * bdrv_is_allocated[_above]. The driver should answer only
330 * according to the current layer, and should only need to set
331 * BDRV_BLOCK_DATA, BDRV_BLOCK_ZERO, BDRV_BLOCK_OFFSET_VALID,
332 * and/or BDRV_BLOCK_RAW; if the current layer defers to a backing
333 * layer, the result should be 0 (and not BDRV_BLOCK_ZERO). See
334 * block.h for the overall meaning of the bits. As a hint, the
335 * flag want_zero is true if the caller cares more about precise
336 * mappings (favor accurate _OFFSET_VALID/_ZERO) or false for
337 * overall allocation (favor larger *pnum, perhaps by reporting
338 * _DATA instead of _ZERO). The block layer guarantees input
339 * clamped to bdrv_getlength() and aligned to request_alignment,
340 * as well as non-NULL pnum, map, and file; in turn, the driver
341 * must return an error or set pnum to an aligned non-zero value.
343 int coroutine_fn (*bdrv_co_block_status
)(BlockDriverState
*bs
,
344 bool want_zero
, int64_t offset
, int64_t bytes
, int64_t *pnum
,
345 int64_t *map
, BlockDriverState
**file
);
348 * Invalidate any cached meta-data.
350 void coroutine_fn (*bdrv_co_invalidate_cache
)(BlockDriverState
*bs
,
352 int (*bdrv_inactivate
)(BlockDriverState
*bs
);
355 * Flushes all data for all layers by calling bdrv_co_flush for underlying
356 * layers, if needed. This function is needed for deterministic
357 * synchronization of the flush finishing callback.
359 int coroutine_fn (*bdrv_co_flush
)(BlockDriverState
*bs
);
361 /* Delete a created file. */
362 int coroutine_fn (*bdrv_co_delete_file
)(BlockDriverState
*bs
,
366 * Flushes all data that was already written to the OS all the way down to
367 * the disk (for example file-posix.c calls fsync()).
369 int coroutine_fn (*bdrv_co_flush_to_disk
)(BlockDriverState
*bs
);
372 * Flushes all internal caches to the OS. The data may still sit in a
373 * writeback cache of the host OS, but it will survive a crash of the qemu
376 int coroutine_fn (*bdrv_co_flush_to_os
)(BlockDriverState
*bs
);
379 * Drivers setting this field must be able to work with just a plain
380 * filename with '<protocol_name>:' as a prefix, and no other options.
381 * Options may be extracted from the filename by implementing
382 * bdrv_parse_filename.
384 const char *protocol_name
;
387 * Truncate @bs to @offset bytes using the given @prealloc mode
388 * when growing. Modes other than PREALLOC_MODE_OFF should be
389 * rejected when shrinking @bs.
391 * If @exact is true, @bs must be resized to exactly @offset.
392 * Otherwise, it is sufficient for @bs (if it is a host block
393 * device and thus there is no way to resize it) to be at least
394 * @offset bytes in length.
396 * If @exact is true and this function fails but would succeed
397 * with @exact = false, it should return -ENOTSUP.
399 int coroutine_fn (*bdrv_co_truncate
)(BlockDriverState
*bs
, int64_t offset
,
400 bool exact
, PreallocMode prealloc
,
401 BdrvRequestFlags flags
, Error
**errp
);
403 int64_t (*bdrv_getlength
)(BlockDriverState
*bs
);
404 bool has_variable_length
;
405 int64_t (*bdrv_get_allocated_file_size
)(BlockDriverState
*bs
);
406 BlockMeasureInfo
*(*bdrv_measure
)(QemuOpts
*opts
, BlockDriverState
*in_bs
,
409 int coroutine_fn (*bdrv_co_pwritev_compressed
)(BlockDriverState
*bs
,
410 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
);
411 int coroutine_fn (*bdrv_co_pwritev_compressed_part
)(BlockDriverState
*bs
,
412 uint64_t offset
, uint64_t bytes
, QEMUIOVector
*qiov
,
415 int (*bdrv_snapshot_create
)(BlockDriverState
*bs
,
416 QEMUSnapshotInfo
*sn_info
);
417 int (*bdrv_snapshot_goto
)(BlockDriverState
*bs
,
418 const char *snapshot_id
);
419 int (*bdrv_snapshot_delete
)(BlockDriverState
*bs
,
420 const char *snapshot_id
,
423 int (*bdrv_snapshot_list
)(BlockDriverState
*bs
,
424 QEMUSnapshotInfo
**psn_info
);
425 int (*bdrv_snapshot_load_tmp
)(BlockDriverState
*bs
,
426 const char *snapshot_id
,
429 int (*bdrv_get_info
)(BlockDriverState
*bs
, BlockDriverInfo
*bdi
);
430 ImageInfoSpecific
*(*bdrv_get_specific_info
)(BlockDriverState
*bs
,
432 BlockStatsSpecific
*(*bdrv_get_specific_stats
)(BlockDriverState
*bs
);
434 int coroutine_fn (*bdrv_save_vmstate
)(BlockDriverState
*bs
,
437 int coroutine_fn (*bdrv_load_vmstate
)(BlockDriverState
*bs
,
441 int (*bdrv_change_backing_file
)(BlockDriverState
*bs
,
442 const char *backing_file
, const char *backing_fmt
);
444 /* removable device specific */
445 bool (*bdrv_is_inserted
)(BlockDriverState
*bs
);
446 void (*bdrv_eject
)(BlockDriverState
*bs
, bool eject_flag
);
447 void (*bdrv_lock_medium
)(BlockDriverState
*bs
, bool locked
);
449 /* to control generic scsi devices */
450 BlockAIOCB
*(*bdrv_aio_ioctl
)(BlockDriverState
*bs
,
451 unsigned long int req
, void *buf
,
452 BlockCompletionFunc
*cb
, void *opaque
);
453 int coroutine_fn (*bdrv_co_ioctl
)(BlockDriverState
*bs
,
454 unsigned long int req
, void *buf
);
456 /* List of options for creating images, terminated by name == NULL */
457 QemuOptsList
*create_opts
;
459 /* List of options for image amend */
460 QemuOptsList
*amend_opts
;
463 * If this driver supports reopening images this contains a
464 * NULL-terminated list of the runtime options that can be
465 * modified. If an option in this list is unspecified during
466 * reopen then it _must_ be reset to its default value or return
469 const char *const *mutable_opts
;
472 * Returns 0 for completed check, -errno for internal errors.
473 * The check results are stored in result.
475 int coroutine_fn (*bdrv_co_check
)(BlockDriverState
*bs
,
476 BdrvCheckResult
*result
,
479 void (*bdrv_debug_event
)(BlockDriverState
*bs
, BlkdebugEvent event
);
481 /* TODO Better pass a option string/QDict/QemuOpts to add any rule? */
482 int (*bdrv_debug_breakpoint
)(BlockDriverState
*bs
, const char *event
,
484 int (*bdrv_debug_remove_breakpoint
)(BlockDriverState
*bs
,
486 int (*bdrv_debug_resume
)(BlockDriverState
*bs
, const char *tag
);
487 bool (*bdrv_debug_is_suspended
)(BlockDriverState
*bs
, const char *tag
);
489 void (*bdrv_refresh_limits
)(BlockDriverState
*bs
, Error
**errp
);
492 * Returns 1 if newly created images are guaranteed to contain only
493 * zeros, 0 otherwise.
495 int (*bdrv_has_zero_init
)(BlockDriverState
*bs
);
497 /* Remove fd handlers, timers, and other event loop callbacks so the event
498 * loop is no longer in use. Called with no in-flight requests and in
499 * depth-first traversal order with parents before child nodes.
501 void (*bdrv_detach_aio_context
)(BlockDriverState
*bs
);
503 /* Add fd handlers, timers, and other event loop callbacks so I/O requests
504 * can be processed again. Called with no in-flight requests and in
505 * depth-first traversal order with child nodes before parent nodes.
507 void (*bdrv_attach_aio_context
)(BlockDriverState
*bs
,
508 AioContext
*new_context
);
510 /* io queue for linux-aio */
511 void (*bdrv_io_plug
)(BlockDriverState
*bs
);
512 void (*bdrv_io_unplug
)(BlockDriverState
*bs
);
515 * Try to get @bs's logical and physical block size.
516 * On success, store them in @bsz and return zero.
517 * On failure, return negative errno.
519 int (*bdrv_probe_blocksizes
)(BlockDriverState
*bs
, BlockSizes
*bsz
);
521 * Try to get @bs's geometry (cyls, heads, sectors)
522 * On success, store them in @geo and return 0.
523 * On failure return -errno.
524 * Only drivers that want to override guest geometry implement this
525 * callback; see hd_geometry_guess().
527 int (*bdrv_probe_geometry
)(BlockDriverState
*bs
, HDGeometry
*geo
);
530 * bdrv_co_drain_begin is called if implemented in the beginning of a
531 * drain operation to drain and stop any internal sources of requests in
533 * bdrv_co_drain_end is called if implemented at the end of the drain.
535 * They should be used by the driver to e.g. manage scheduled I/O
536 * requests, or toggle an internal state. After the end of the drain new
537 * requests will continue normally.
539 void coroutine_fn (*bdrv_co_drain_begin
)(BlockDriverState
*bs
);
540 void coroutine_fn (*bdrv_co_drain_end
)(BlockDriverState
*bs
);
542 void (*bdrv_add_child
)(BlockDriverState
*parent
, BlockDriverState
*child
,
544 void (*bdrv_del_child
)(BlockDriverState
*parent
, BdrvChild
*child
,
548 * Informs the block driver that a permission change is intended. The
549 * driver checks whether the change is permissible and may take other
550 * preparations for the change (e.g. get file system locks). This operation
551 * is always followed either by a call to either .bdrv_set_perm or
552 * .bdrv_abort_perm_update.
554 * Checks whether the requested set of cumulative permissions in @perm
555 * can be granted for accessing @bs and whether no other users are using
556 * permissions other than those given in @shared (both arguments take
557 * BLK_PERM_* bitmasks).
559 * If both conditions are met, 0 is returned. Otherwise, -errno is returned
560 * and errp is set to an error describing the conflict.
562 int (*bdrv_check_perm
)(BlockDriverState
*bs
, uint64_t perm
,
563 uint64_t shared
, Error
**errp
);
566 * Called to inform the driver that the set of cumulative set of used
567 * permissions for @bs has changed to @perm, and the set of sharable
568 * permission to @shared. The driver can use this to propagate changes to
569 * its children (i.e. request permissions only if a parent actually needs
572 * This function is only invoked after bdrv_check_perm(), so block drivers
573 * may rely on preparations made in their .bdrv_check_perm implementation.
575 void (*bdrv_set_perm
)(BlockDriverState
*bs
, uint64_t perm
, uint64_t shared
);
578 * Called to inform the driver that after a previous bdrv_check_perm()
579 * call, the permission update is not performed and any preparations made
580 * for it (e.g. taken file locks) need to be undone.
582 * This function can be called even for nodes that never saw a
583 * bdrv_check_perm() call. It is a no-op then.
585 void (*bdrv_abort_perm_update
)(BlockDriverState
*bs
);
588 * Returns in @nperm and @nshared the permissions that the driver for @bs
589 * needs on its child @c, based on the cumulative permissions requested by
590 * the parents in @parent_perm and @parent_shared.
592 * If @c is NULL, return the permissions for attaching a new child for the
593 * given @child_class and @role.
595 * If @reopen_queue is non-NULL, don't return the currently needed
596 * permissions, but those that will be needed after applying the
599 void (*bdrv_child_perm
)(BlockDriverState
*bs
, BdrvChild
*c
,
601 BlockReopenQueue
*reopen_queue
,
602 uint64_t parent_perm
, uint64_t parent_shared
,
603 uint64_t *nperm
, uint64_t *nshared
);
605 bool (*bdrv_supports_persistent_dirty_bitmap
)(BlockDriverState
*bs
);
606 bool (*bdrv_co_can_store_new_dirty_bitmap
)(BlockDriverState
*bs
,
608 uint32_t granularity
,
610 int (*bdrv_co_remove_persistent_dirty_bitmap
)(BlockDriverState
*bs
,
615 * Register/unregister a buffer for I/O. For example, when the driver is
616 * interested to know the memory areas that will later be used in iovs, so
617 * that it can do IOMMU mapping with VFIO etc., in order to get better
618 * performance. In the case of VFIO drivers, this callback is used to do
619 * DMA mapping for hot buffers.
621 void (*bdrv_register_buf
)(BlockDriverState
*bs
, void *host
, size_t size
);
622 void (*bdrv_unregister_buf
)(BlockDriverState
*bs
, void *host
);
623 QLIST_ENTRY(BlockDriver
) list
;
625 /* Pointer to a NULL-terminated array of names of strong options
626 * that can be specified for bdrv_open(). A strong option is one
627 * that changes the data of a BDS.
628 * If this pointer is NULL, the array is considered empty.
629 * "filename" and "driver" are always considered strong. */
630 const char *const *strong_runtime_opts
;
633 static inline bool block_driver_can_compress(BlockDriver
*drv
)
635 return drv
->bdrv_co_pwritev_compressed
||
636 drv
->bdrv_co_pwritev_compressed_part
;
639 typedef struct BlockLimits
{
640 /* Alignment requirement, in bytes, for offset/length of I/O
641 * requests. Must be a power of 2 less than INT_MAX; defaults to
642 * 1 for drivers with modern byte interfaces, and to 512
644 uint32_t request_alignment
;
646 /* Maximum number of bytes that can be discarded at once (since it
647 * is signed, it must be < 2G, if set). Must be multiple of
648 * pdiscard_alignment, but need not be power of 2. May be 0 if no
649 * inherent 32-bit limit */
650 int32_t max_pdiscard
;
652 /* Optimal alignment for discard requests in bytes. A power of 2
653 * is best but not mandatory. Must be a multiple of
654 * bl.request_alignment, and must be less than max_pdiscard if
655 * that is set. May be 0 if bl.request_alignment is good enough */
656 uint32_t pdiscard_alignment
;
658 /* Maximum number of bytes that can zeroized at once (since it is
659 * signed, it must be < 2G, if set). Must be multiple of
660 * pwrite_zeroes_alignment. May be 0 if no inherent 32-bit limit */
661 int32_t max_pwrite_zeroes
;
663 /* Optimal alignment for write zeroes requests in bytes. A power
664 * of 2 is best but not mandatory. Must be a multiple of
665 * bl.request_alignment, and must be less than max_pwrite_zeroes
666 * if that is set. May be 0 if bl.request_alignment is good
668 uint32_t pwrite_zeroes_alignment
;
670 /* Optimal transfer length in bytes. A power of 2 is best but not
671 * mandatory. Must be a multiple of bl.request_alignment, or 0 if
672 * no preferred size */
673 uint32_t opt_transfer
;
675 /* Maximal transfer length in bytes. Need not be power of 2, but
676 * must be multiple of opt_transfer and bl.request_alignment, or 0
677 * for no 32-bit limit. For now, anything larger than INT_MAX is
679 uint32_t max_transfer
;
681 /* memory alignment, in bytes so that no bounce buffer is needed */
682 size_t min_mem_alignment
;
684 /* memory alignment, in bytes, for bounce buffer */
685 size_t opt_mem_alignment
;
687 /* maximum number of iovec elements */
691 typedef struct BdrvOpBlocker BdrvOpBlocker
;
693 typedef struct BdrvAioNotifier
{
694 void (*attached_aio_context
)(AioContext
*new_context
, void *opaque
);
695 void (*detach_aio_context
)(void *opaque
);
700 QLIST_ENTRY(BdrvAioNotifier
) list
;
703 struct BdrvChildClass
{
704 /* If true, bdrv_replace_node() doesn't change the node this BdrvChild
708 /* If true, the parent is a BlockDriverState and bdrv_next_all_states()
709 * will return it. This information is used for drain_all, where every node
710 * will be drained separately, so the drain only needs to be propagated to
711 * non-BDS parents. */
714 void (*inherit_options
)(BdrvChildRole role
, bool parent_is_format
,
715 int *child_flags
, QDict
*child_options
,
716 int parent_flags
, QDict
*parent_options
);
718 void (*change_media
)(BdrvChild
*child
, bool load
);
719 void (*resize
)(BdrvChild
*child
);
721 /* Returns a name that is supposedly more useful for human users than the
722 * node name for identifying the node in question (in particular, a BB
723 * name), or NULL if the parent can't provide a better name. */
724 const char *(*get_name
)(BdrvChild
*child
);
726 /* Returns a malloced string that describes the parent of the child for a
727 * human reader. This could be a node-name, BlockBackend name, qdev ID or
728 * QOM path of the device owning the BlockBackend, job type and ID etc. The
729 * caller is responsible for freeing the memory. */
730 char *(*get_parent_desc
)(BdrvChild
*child
);
733 * If this pair of functions is implemented, the parent doesn't issue new
734 * requests after returning from .drained_begin() until .drained_end() is
737 * These functions must not change the graph (and therefore also must not
738 * call aio_poll(), which could change the graph indirectly).
740 * If drained_end() schedules background operations, it must atomically
741 * increment *drained_end_counter for each such operation and atomically
742 * decrement it once the operation has settled.
744 * Note that this can be nested. If drained_begin() was called twice, new
745 * I/O is allowed only after drained_end() was called twice, too.
747 void (*drained_begin
)(BdrvChild
*child
);
748 void (*drained_end
)(BdrvChild
*child
, int *drained_end_counter
);
751 * Returns whether the parent has pending requests for the child. This
752 * callback is polled after .drained_begin() has been called until all
753 * activity on the child has stopped.
755 bool (*drained_poll
)(BdrvChild
*child
);
757 /* Notifies the parent that the child has been activated/inactivated (e.g.
758 * when migration is completing) and it can start/stop requesting
759 * permissions and doing I/O on it. */
760 void (*activate
)(BdrvChild
*child
, Error
**errp
);
761 int (*inactivate
)(BdrvChild
*child
);
763 void (*attach
)(BdrvChild
*child
);
764 void (*detach
)(BdrvChild
*child
);
766 /* Notifies the parent that the filename of its child has changed (e.g.
767 * because the direct child was removed from the backing chain), so that it
768 * can update its reference. */
769 int (*update_filename
)(BdrvChild
*child
, BlockDriverState
*new_base
,
770 const char *filename
, Error
**errp
);
772 bool (*can_set_aio_ctx
)(BdrvChild
*child
, AioContext
*ctx
,
773 GSList
**ignore
, Error
**errp
);
774 void (*set_aio_ctx
)(BdrvChild
*child
, AioContext
*ctx
, GSList
**ignore
);
777 extern const BdrvChildClass child_of_bds
;
780 BlockDriverState
*bs
;
782 const BdrvChildClass
*klass
;
787 * Granted permissions for operating on this BdrvChild (BLK_PERM_* bitmask)
792 * Permissions that can still be granted to other users of @bs while this
793 * BdrvChild is still attached to it. (BLK_PERM_* bitmask)
795 uint64_t shared_perm
;
797 /* backup of permissions during permission update procedure */
798 bool has_backup_perm
;
799 uint64_t backup_perm
;
800 uint64_t backup_shared_perm
;
803 * This link is frozen: the child can neither be replaced nor
804 * detached from the parent.
809 * How many times the parent of this child has been drained
810 * (through klass->drained_*).
811 * Usually, this is equal to bs->quiesce_counter (potentially
812 * reduced by bdrv_drain_all_count). It may differ while the
813 * child is entering or leaving a drained section.
815 int parent_quiesce_counter
;
817 QLIST_ENTRY(BdrvChild
) next
;
818 QLIST_ENTRY(BdrvChild
) next_parent
;
822 * Note: the function bdrv_append() copies and swaps contents of
823 * BlockDriverStates, so if you add new fields to this struct, please
824 * inspect bdrv_append() to determine if the new fields need to be
827 struct BlockDriverState
{
828 /* Protected by big QEMU lock or read-only after opening. No special
829 * locking needed during I/O...
831 int open_flags
; /* flags used to open the file, re-used for re-open */
832 bool read_only
; /* if true, the media is read only */
833 bool encrypted
; /* if true, the media is encrypted */
834 bool sg
; /* if true, the device is a /dev/sg* */
835 bool probed
; /* if true, format was probed rather than specified */
836 bool force_share
; /* if true, always allow all shared permissions */
837 bool implicit
; /* if true, this filter node was automatically inserted */
839 BlockDriver
*drv
; /* NULL means no media */
842 AioContext
*aio_context
; /* event loop used for fd handlers, timers, etc */
843 /* long-running tasks intended to always use the same AioContext as this
844 * BDS may register themselves in this list to be notified of changes
845 * regarding this BDS's context */
846 QLIST_HEAD(, BdrvAioNotifier
) aio_notifiers
;
847 bool walking_aio_notifiers
; /* to make removal during iteration safe */
849 char filename
[PATH_MAX
];
850 char backing_file
[PATH_MAX
]; /* if non zero, the image is a diff of
852 /* The backing filename indicated by the image header; if we ever
853 * open this file, then this is replaced by the resulting BDS's
854 * filename (i.e. after a bdrv_refresh_filename() run). */
855 char auto_backing_file
[PATH_MAX
];
856 char backing_format
[16]; /* if non-zero and backing_file exists */
858 QDict
*full_open_options
;
859 char exact_filename
[PATH_MAX
];
867 /* Flags honored during pwrite (so far: BDRV_REQ_FUA,
868 * BDRV_REQ_WRITE_UNCHANGED).
869 * If a driver does not support BDRV_REQ_WRITE_UNCHANGED, those
870 * writes will be issued as normal writes without the flag set.
871 * This is important to note for drivers that do not explicitly
872 * request a WRITE permission for their children and instead take
873 * the same permissions as their parent did (this is commonly what
874 * block filters do). Such drivers have to be aware that the
875 * parent may have taken a WRITE_UNCHANGED permission only and is
876 * issuing such requests. Drivers either must make sure that
877 * these requests do not result in plain WRITE accesses (usually
878 * by supporting BDRV_REQ_WRITE_UNCHANGED, and then forwarding
879 * every incoming write request as-is, including potentially that
880 * flag), or they have to explicitly take the WRITE permission for
882 unsigned int supported_write_flags
;
883 /* Flags honored during pwrite_zeroes (so far: BDRV_REQ_FUA,
884 * BDRV_REQ_MAY_UNMAP, BDRV_REQ_WRITE_UNCHANGED) */
885 unsigned int supported_zero_flags
;
887 * Flags honoured during truncate (so far: BDRV_REQ_ZERO_WRITE).
889 * If BDRV_REQ_ZERO_WRITE is given, the truncate operation must make sure
890 * that any added space reads as all zeros. If this can't be guaranteed,
891 * the operation must fail.
893 unsigned int supported_truncate_flags
;
895 /* the following member gives a name to every node on the bs graph. */
897 /* element of the list of named nodes building the graph */
898 QTAILQ_ENTRY(BlockDriverState
) node_list
;
899 /* element of the list of all BlockDriverStates (all_bdrv_states) */
900 QTAILQ_ENTRY(BlockDriverState
) bs_list
;
901 /* element of the list of monitor-owned BDS */
902 QTAILQ_ENTRY(BlockDriverState
) monitor_list
;
905 /* operation blockers */
906 QLIST_HEAD(, BdrvOpBlocker
) op_blockers
[BLOCK_OP_TYPE_MAX
];
908 /* The node that this node inherited default options from (and a reopen on
909 * which can affect this node by changing these defaults). This is always a
910 * parent node of this node. */
911 BlockDriverState
*inherits_from
;
912 QLIST_HEAD(, BdrvChild
) children
;
913 QLIST_HEAD(, BdrvChild
) parents
;
916 QDict
*explicit_options
;
917 BlockdevDetectZeroesOptions detect_zeroes
;
919 /* The error object in use for blocking operations on backing_hd */
920 Error
*backing_blocker
;
922 /* Protected by AioContext lock */
924 /* If we are reading a disk image, give its size in sectors.
925 * Generally read-only; it is written to by load_snapshot and
926 * save_snaphost, but the block layer is quiescent during those.
928 int64_t total_sectors
;
930 /* Callback before write request is processed */
931 NotifierWithReturnList before_write_notifiers
;
933 /* threshold limit for writes, in bytes. "High water mark". */
934 uint64_t write_threshold_offset
;
935 NotifierWithReturn write_threshold_notifier
;
937 /* Writing to the list requires the BQL _and_ the dirty_bitmap_mutex.
938 * Reading from the list can be done with either the BQL or the
939 * dirty_bitmap_mutex. Modifying a bitmap only requires
940 * dirty_bitmap_mutex. */
941 QemuMutex dirty_bitmap_mutex
;
942 QLIST_HEAD(, BdrvDirtyBitmap
) dirty_bitmaps
;
944 /* Offset after the highest byte written to */
945 Stat64 wr_highest_offset
;
947 /* If true, copy read backing sectors into image. Can be >1 if more
948 * than one client has requested copy-on-read. Accessed with atomic
953 /* number of in-flight requests; overall and serialising.
954 * Accessed with atomic ops.
956 unsigned int in_flight
;
957 unsigned int serialising_in_flight
;
959 /* counter for nested bdrv_io_plug.
960 * Accessed with atomic ops.
964 /* do we need to tell the quest if we have a volatile write cache? */
965 int enable_write_cache
;
967 /* Accessed with atomic ops. */
969 int recursive_quiesce_counter
;
971 unsigned int write_gen
; /* Current data generation */
973 /* Protected by reqs_lock. */
975 QLIST_HEAD(, BdrvTrackedRequest
) tracked_requests
;
976 CoQueue flush_queue
; /* Serializing flush queue */
977 bool active_flush_req
; /* Flush request in flight? */
979 /* Only read/written by whoever has set active_flush_req to true. */
980 unsigned int flushed_gen
; /* Flushed write generation */
982 /* BdrvChild links to this node may never be frozen */
986 struct BlockBackendRootState
{
989 BlockdevDetectZeroesOptions detect_zeroes
;
992 typedef enum BlockMirrorBackingMode
{
993 /* Reuse the existing backing chain from the source for the target.
994 * - sync=full: Set backing BDS to NULL.
995 * - sync=top: Use source's backing BDS.
996 * - sync=none: Use source as the backing BDS. */
997 MIRROR_SOURCE_BACKING_CHAIN
,
999 /* Open the target's backing chain completely anew */
1000 MIRROR_OPEN_BACKING_CHAIN
,
1002 /* Do not change the target's backing BDS after job completion */
1003 MIRROR_LEAVE_BACKING_CHAIN
,
1004 } BlockMirrorBackingMode
;
1007 /* Essential block drivers which must always be statically linked into qemu, and
1008 * which therefore can be accessed without using bdrv_find_format() */
1009 extern BlockDriver bdrv_file
;
1010 extern BlockDriver bdrv_raw
;
1011 extern BlockDriver bdrv_qcow2
;
1013 int coroutine_fn
bdrv_co_preadv(BdrvChild
*child
,
1014 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
,
1015 BdrvRequestFlags flags
);
1016 int coroutine_fn
bdrv_co_preadv_part(BdrvChild
*child
,
1017 int64_t offset
, unsigned int bytes
,
1018 QEMUIOVector
*qiov
, size_t qiov_offset
, BdrvRequestFlags flags
);
1019 int coroutine_fn
bdrv_co_pwritev(BdrvChild
*child
,
1020 int64_t offset
, unsigned int bytes
, QEMUIOVector
*qiov
,
1021 BdrvRequestFlags flags
);
1022 int coroutine_fn
bdrv_co_pwritev_part(BdrvChild
*child
,
1023 int64_t offset
, unsigned int bytes
,
1024 QEMUIOVector
*qiov
, size_t qiov_offset
, BdrvRequestFlags flags
);
1026 static inline int coroutine_fn
bdrv_co_pread(BdrvChild
*child
,
1027 int64_t offset
, unsigned int bytes
, void *buf
, BdrvRequestFlags flags
)
1029 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, bytes
);
1031 return bdrv_co_preadv(child
, offset
, bytes
, &qiov
, flags
);
1034 static inline int coroutine_fn
bdrv_co_pwrite(BdrvChild
*child
,
1035 int64_t offset
, unsigned int bytes
, void *buf
, BdrvRequestFlags flags
)
1037 QEMUIOVector qiov
= QEMU_IOVEC_INIT_BUF(qiov
, buf
, bytes
);
1039 return bdrv_co_pwritev(child
, offset
, bytes
, &qiov
, flags
);
1042 extern unsigned int bdrv_drain_all_count
;
1043 void bdrv_apply_subtree_drain(BdrvChild
*child
, BlockDriverState
*new_parent
);
1044 void bdrv_unapply_subtree_drain(BdrvChild
*child
, BlockDriverState
*old_parent
);
1046 bool coroutine_fn
bdrv_mark_request_serialising(BdrvTrackedRequest
*req
, uint64_t align
);
1047 BdrvTrackedRequest
*coroutine_fn
bdrv_co_get_self_request(BlockDriverState
*bs
);
1049 int get_tmp_filename(char *filename
, int size
);
1050 BlockDriver
*bdrv_probe_all(const uint8_t *buf
, int buf_size
,
1051 const char *filename
);
1053 void bdrv_parse_filename_strip_prefix(const char *filename
, const char *prefix
,
1058 * bdrv_add_before_write_notifier:
1060 * Register a callback that is invoked before write requests are processed but
1061 * after any throttling or waiting for overlapping requests.
1063 void bdrv_add_before_write_notifier(BlockDriverState
*bs
,
1064 NotifierWithReturn
*notifier
);
1067 * bdrv_add_aio_context_notifier:
1069 * If a long-running job intends to be always run in the same AioContext as a
1070 * certain BDS, it may use this function to be notified of changes regarding the
1071 * association of the BDS to an AioContext.
1073 * attached_aio_context() is called after the target BDS has been attached to a
1074 * new AioContext; detach_aio_context() is called before the target BDS is being
1075 * detached from its old AioContext.
1077 void bdrv_add_aio_context_notifier(BlockDriverState
*bs
,
1078 void (*attached_aio_context
)(AioContext
*new_context
, void *opaque
),
1079 void (*detach_aio_context
)(void *opaque
), void *opaque
);
1082 * bdrv_remove_aio_context_notifier:
1084 * Unsubscribe of change notifications regarding the BDS's AioContext. The
1085 * parameters given here have to be the same as those given to
1086 * bdrv_add_aio_context_notifier().
1088 void bdrv_remove_aio_context_notifier(BlockDriverState
*bs
,
1089 void (*aio_context_attached
)(AioContext
*,
1091 void (*aio_context_detached
)(void *),
1096 * @bs: The BlockDriverState for which an I/O operation has been completed.
1098 * Wake up the main thread if it is waiting on BDRV_POLL_WHILE. During
1099 * synchronous I/O on a BlockDriverState that is attached to another
1100 * I/O thread, the main thread lets the I/O thread's event loop run,
1101 * waiting for the I/O operation to complete. A bdrv_wakeup will wake
1102 * up the main thread if necessary.
1104 * Manual calls to bdrv_wakeup are rarely necessary, because
1105 * bdrv_dec_in_flight already calls it.
1107 void bdrv_wakeup(BlockDriverState
*bs
);
1110 int is_windows_drive(const char *filename
);
1115 * @job_id: The id of the newly-created job, or %NULL to use the
1116 * device name of @bs.
1117 * @bs: Block device to operate on.
1118 * @base: Block device that will become the new base, or %NULL to
1119 * flatten the whole backing file chain onto @bs.
1120 * @backing_file_str: The file name that will be written to @bs as the
1121 * the new backing file if the job completes. Ignored if @base is %NULL.
1122 * @creation_flags: Flags that control the behavior of the Job lifetime.
1123 * See @BlockJobCreateFlags
1124 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
1125 * @on_error: The action to take upon error.
1126 * @errp: Error object.
1128 * Start a streaming operation on @bs. Clusters that are unallocated
1129 * in @bs, but allocated in any image between @base and @bs (both
1130 * exclusive) will be written to @bs. At the end of a successful
1131 * streaming job, the backing file of @bs will be changed to
1132 * @backing_file_str in the written image and to @base in the live
1135 void stream_start(const char *job_id
, BlockDriverState
*bs
,
1136 BlockDriverState
*base
, const char *backing_file_str
,
1137 int creation_flags
, int64_t speed
,
1138 BlockdevOnError on_error
, Error
**errp
);
1142 * @job_id: The id of the newly-created job, or %NULL to use the
1143 * device name of @bs.
1144 * @bs: Active block device.
1145 * @top: Top block device to be committed.
1146 * @base: Block device that will be written into, and become the new top.
1147 * @creation_flags: Flags that control the behavior of the Job lifetime.
1148 * See @BlockJobCreateFlags
1149 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
1150 * @on_error: The action to take upon error.
1151 * @backing_file_str: String to use as the backing file in @top's overlay
1152 * @filter_node_name: The node name that should be assigned to the filter
1153 * driver that the commit job inserts into the graph above @top. NULL means
1154 * that a node name should be autogenerated.
1155 * @errp: Error object.
1158 void commit_start(const char *job_id
, BlockDriverState
*bs
,
1159 BlockDriverState
*base
, BlockDriverState
*top
,
1160 int creation_flags
, int64_t speed
,
1161 BlockdevOnError on_error
, const char *backing_file_str
,
1162 const char *filter_node_name
, Error
**errp
);
1164 * commit_active_start:
1165 * @job_id: The id of the newly-created job, or %NULL to use the
1166 * device name of @bs.
1167 * @bs: Active block device to be committed.
1168 * @base: Block device that will be written into, and become the new top.
1169 * @creation_flags: Flags that control the behavior of the Job lifetime.
1170 * See @BlockJobCreateFlags
1171 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
1172 * @on_error: The action to take upon error.
1173 * @filter_node_name: The node name that should be assigned to the filter
1174 * driver that the commit job inserts into the graph above @bs. NULL means that
1175 * a node name should be autogenerated.
1176 * @cb: Completion function for the job.
1177 * @opaque: Opaque pointer value passed to @cb.
1178 * @auto_complete: Auto complete the job.
1179 * @errp: Error object.
1182 BlockJob
*commit_active_start(const char *job_id
, BlockDriverState
*bs
,
1183 BlockDriverState
*base
, int creation_flags
,
1184 int64_t speed
, BlockdevOnError on_error
,
1185 const char *filter_node_name
,
1186 BlockCompletionFunc
*cb
, void *opaque
,
1187 bool auto_complete
, Error
**errp
);
1190 * @job_id: The id of the newly-created job, or %NULL to use the
1191 * device name of @bs.
1192 * @bs: Block device to operate on.
1193 * @target: Block device to write to.
1194 * @replaces: Block graph node name to replace once the mirror is done. Can
1195 * only be used when full mirroring is selected.
1196 * @creation_flags: Flags that control the behavior of the Job lifetime.
1197 * See @BlockJobCreateFlags
1198 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
1199 * @granularity: The chosen granularity for the dirty bitmap.
1200 * @buf_size: The amount of data that can be in flight at one time.
1201 * @mode: Whether to collapse all images in the chain to the target.
1202 * @backing_mode: How to establish the target's backing chain after completion.
1203 * @zero_target: Whether the target should be explicitly zero-initialized
1204 * @on_source_error: The action to take upon error reading from the source.
1205 * @on_target_error: The action to take upon error writing to the target.
1206 * @unmap: Whether to unmap target where source sectors only contain zeroes.
1207 * @filter_node_name: The node name that should be assigned to the filter
1208 * driver that the mirror job inserts into the graph above @bs. NULL means that
1209 * a node name should be autogenerated.
1210 * @copy_mode: When to trigger writes to the target.
1211 * @errp: Error object.
1213 * Start a mirroring operation on @bs. Clusters that are allocated
1214 * in @bs will be written to @target until the job is cancelled or
1215 * manually completed. At the end of a successful mirroring job,
1216 * @bs will be switched to read from @target.
1218 void mirror_start(const char *job_id
, BlockDriverState
*bs
,
1219 BlockDriverState
*target
, const char *replaces
,
1220 int creation_flags
, int64_t speed
,
1221 uint32_t granularity
, int64_t buf_size
,
1222 MirrorSyncMode mode
, BlockMirrorBackingMode backing_mode
,
1224 BlockdevOnError on_source_error
,
1225 BlockdevOnError on_target_error
,
1226 bool unmap
, const char *filter_node_name
,
1227 MirrorCopyMode copy_mode
, Error
**errp
);
1230 * backup_job_create:
1231 * @job_id: The id of the newly-created job, or %NULL to use the
1232 * device name of @bs.
1233 * @bs: Block device to operate on.
1234 * @target: Block device to write to.
1235 * @speed: The maximum speed, in bytes per second, or 0 for unlimited.
1236 * @sync_mode: What parts of the disk image should be copied to the destination.
1237 * @sync_bitmap: The dirty bitmap if sync_mode is 'bitmap' or 'incremental'
1238 * @bitmap_mode: The bitmap synchronization policy to use.
1239 * @on_source_error: The action to take upon error reading from the source.
1240 * @on_target_error: The action to take upon error writing to the target.
1241 * @creation_flags: Flags that control the behavior of the Job lifetime.
1242 * See @BlockJobCreateFlags
1243 * @cb: Completion function for the job.
1244 * @opaque: Opaque pointer value passed to @cb.
1245 * @txn: Transaction that this job is part of (may be NULL).
1247 * Create a backup operation on @bs. Clusters in @bs are written to @target
1248 * until the job is cancelled or manually completed.
1250 BlockJob
*backup_job_create(const char *job_id
, BlockDriverState
*bs
,
1251 BlockDriverState
*target
, int64_t speed
,
1252 MirrorSyncMode sync_mode
,
1253 BdrvDirtyBitmap
*sync_bitmap
,
1254 BitmapSyncMode bitmap_mode
,
1256 const char *filter_node_name
,
1257 BlockdevOnError on_source_error
,
1258 BlockdevOnError on_target_error
,
1260 BlockCompletionFunc
*cb
, void *opaque
,
1261 JobTxn
*txn
, Error
**errp
);
1263 BdrvChild
*bdrv_root_attach_child(BlockDriverState
*child_bs
,
1264 const char *child_name
,
1265 const BdrvChildClass
*child_class
,
1266 BdrvChildRole child_role
,
1268 uint64_t perm
, uint64_t shared_perm
,
1269 void *opaque
, Error
**errp
);
1270 void bdrv_root_unref_child(BdrvChild
*child
);
1272 void bdrv_get_cumulative_perm(BlockDriverState
*bs
, uint64_t *perm
,
1273 uint64_t *shared_perm
);
1276 * Sets a BdrvChild's permissions. Avoid if the parent is a BDS; use
1277 * bdrv_child_refresh_perms() instead and make the parent's
1278 * .bdrv_child_perm() implementation return the correct values.
1280 int bdrv_child_try_set_perm(BdrvChild
*c
, uint64_t perm
, uint64_t shared
,
1284 * Calls bs->drv->bdrv_child_perm() and updates the child's permission
1285 * masks with the result.
1286 * Drivers should invoke this function whenever an event occurs that
1287 * makes their .bdrv_child_perm() implementation return different
1288 * values than before, but which will not result in the block layer
1289 * automatically refreshing the permissions.
1291 int bdrv_child_refresh_perms(BlockDriverState
*bs
, BdrvChild
*c
, Error
**errp
);
1293 bool bdrv_recurse_can_replace(BlockDriverState
*bs
,
1294 BlockDriverState
*to_replace
);
1297 * Default implementation for BlockDriver.bdrv_child_perm() that can
1298 * be used by block filters and image formats, as long as they use the
1299 * child_of_bds child class and set an appropriate BdrvChildRole.
1301 void bdrv_default_perms(BlockDriverState
*bs
, BdrvChild
*c
,
1302 BdrvChildRole role
, BlockReopenQueue
*reopen_queue
,
1303 uint64_t perm
, uint64_t shared
,
1304 uint64_t *nperm
, uint64_t *nshared
);
1307 * Default implementation for drivers to pass bdrv_co_block_status() to
1310 int coroutine_fn
bdrv_co_block_status_from_file(BlockDriverState
*bs
,
1316 BlockDriverState
**file
);
1318 * Default implementation for drivers to pass bdrv_co_block_status() to
1319 * their backing file.
1321 int coroutine_fn
bdrv_co_block_status_from_backing(BlockDriverState
*bs
,
1327 BlockDriverState
**file
);
1328 const char *bdrv_get_parent_name(const BlockDriverState
*bs
);
1329 void blk_dev_change_media_cb(BlockBackend
*blk
, bool load
, Error
**errp
);
1330 bool blk_dev_has_removable_media(BlockBackend
*blk
);
1331 bool blk_dev_has_tray(BlockBackend
*blk
);
1332 void blk_dev_eject_request(BlockBackend
*blk
, bool force
);
1333 bool blk_dev_is_tray_open(BlockBackend
*blk
);
1334 bool blk_dev_is_medium_locked(BlockBackend
*blk
);
1336 void bdrv_set_dirty(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
);
1338 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap
*bitmap
, HBitmap
**out
);
1339 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap
*bitmap
, HBitmap
*backup
);
1340 bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap
*dest
,
1341 const BdrvDirtyBitmap
*src
,
1342 HBitmap
**backup
, bool lock
);
1344 void bdrv_inc_in_flight(BlockDriverState
*bs
);
1345 void bdrv_dec_in_flight(BlockDriverState
*bs
);
1347 void blockdev_close_all_bdrv_states(void);
1349 int coroutine_fn
bdrv_co_copy_range_from(BdrvChild
*src
, uint64_t src_offset
,
1350 BdrvChild
*dst
, uint64_t dst_offset
,
1352 BdrvRequestFlags read_flags
,
1353 BdrvRequestFlags write_flags
);
1354 int coroutine_fn
bdrv_co_copy_range_to(BdrvChild
*src
, uint64_t src_offset
,
1355 BdrvChild
*dst
, uint64_t dst_offset
,
1357 BdrvRequestFlags read_flags
,
1358 BdrvRequestFlags write_flags
);
1360 int refresh_total_sectors(BlockDriverState
*bs
, int64_t hint
);
1362 void bdrv_set_monitor_owned(BlockDriverState
*bs
);
1363 BlockDriverState
*bds_tree_init(QDict
*bs_opts
, Error
**errp
);
1366 * Simple implementation of bdrv_co_create_opts for protocol drivers
1367 * which only support creation via opening a file
1368 * (usually existing raw storage device)
1370 int coroutine_fn
bdrv_co_create_opts_simple(BlockDriver
*drv
,
1371 const char *filename
,
1374 extern QemuOptsList bdrv_create_opts_simple
;
1376 BdrvDirtyBitmap
*block_dirty_bitmap_lookup(const char *node
,
1378 BlockDriverState
**pbs
,
1380 BdrvDirtyBitmap
*block_dirty_bitmap_merge(const char *node
, const char *target
,
1381 BlockDirtyBitmapMergeSourceList
*bms
,
1382 HBitmap
**backup
, Error
**errp
);
1383 BdrvDirtyBitmap
*block_dirty_bitmap_remove(const char *node
, const char *name
,
1385 BlockDriverState
**bitmap_bs
,
1388 BdrvChild
*bdrv_cow_child(BlockDriverState
*bs
);
1389 BdrvChild
*bdrv_filter_child(BlockDriverState
*bs
);
1390 BdrvChild
*bdrv_filter_or_cow_child(BlockDriverState
*bs
);
1391 BdrvChild
*bdrv_primary_child(BlockDriverState
*bs
);
1392 BlockDriverState
*bdrv_skip_implicit_filters(BlockDriverState
*bs
);
1393 BlockDriverState
*bdrv_skip_filters(BlockDriverState
*bs
);
1394 BlockDriverState
*bdrv_backing_chain_next(BlockDriverState
*bs
);
1396 static inline BlockDriverState
*child_bs(BdrvChild
*child
)
1398 return child
? child
->bs
: NULL
;
1401 static inline BlockDriverState
*bdrv_cow_bs(BlockDriverState
*bs
)
1403 return child_bs(bdrv_cow_child(bs
));
1406 static inline BlockDriverState
*bdrv_filter_bs(BlockDriverState
*bs
)
1408 return child_bs(bdrv_filter_child(bs
));
1411 static inline BlockDriverState
*bdrv_filter_or_cow_bs(BlockDriverState
*bs
)
1413 return child_bs(bdrv_filter_or_cow_child(bs
));
1416 static inline BlockDriverState
*bdrv_primary_bs(BlockDriverState
*bs
)
1418 return child_bs(bdrv_primary_child(bs
));
1421 #endif /* BLOCK_INT_H */