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