]> git.proxmox.com Git - mirror_qemu.git/blame - include/block/block_int-common.h
block: Mark bdrv_*_dirty_bitmap() and callers GRAPH_RDLOCK
[mirror_qemu.git] / include / block / block_int-common.h
CommitLineData
ebc2752b
EGE
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
e2c1c34f
MA
27#include "block/aio.h"
28#include "block/block-common.h"
29#include "block/block-global-state.h"
ebc2752b 30#include "block/snapshot.h"
e2c1c34f 31#include "qemu/iov.h"
ebc2752b 32#include "qemu/rcu.h"
e2c1c34f 33#include "qemu/stats64.h"
ebc2752b
EGE
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
63enum 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 */
76typedef 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
94struct BlockDriver {
69c0bf11
EGE
95 /*
96 * These fields are initialized when this object is created,
97 * and are never changed afterwards.
98 */
99
ebc2752b
EGE
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;
046fd84f
VSO
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.
1921b4f7
VSO
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.
046fd84f
VSO
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
ebc2752b
EGE
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
69c0bf11
EGE
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
ebc2752b
EGE
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
ebc2752b
EGE
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);
ebc2752b 232
69c0bf11 233 /* For handling image reopen for split or non-split files. */
ebc2752b
EGE
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
4ec8df01
KW
249 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_create)(
250 BlockdevCreateOptions *opts, Error **errp);
251
252 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_create_opts)(
253 BlockDriver *drv, const char *filename, QemuOpts *opts, Error **errp);
ebc2752b 254
ebc2752b
EGE
255 int (*bdrv_amend_options)(BlockDriverState *bs,
256 QemuOpts *opts,
257 BlockDriverAmendStatusCB *status_cb,
258 void *cb_opaque,
259 bool force,
260 Error **errp);
261
262 int (*bdrv_make_empty)(BlockDriverState *bs);
263
264 /*
265 * Refreshes the bs->exact_filename field. If that is impossible,
266 * bs->exact_filename has to be left empty.
267 */
268 void (*bdrv_refresh_filename)(BlockDriverState *bs);
269
270 /*
271 * Gathers the open options for all children into @target.
272 * A simple format driver (without backing file support) might
273 * implement this function like this:
274 *
275 * QINCREF(bs->file->bs->full_open_options);
276 * qdict_put(target, "file", bs->file->bs->full_open_options);
277 *
278 * If not specified, the generic implementation will simply put
279 * all children's options under their respective name.
280 *
281 * @backing_overridden is true when bs->backing seems not to be
282 * the child that would result from opening bs->backing_file.
283 * Therefore, if it is true, the backing child's options should be
284 * gathered; otherwise, there is no need since the backing child
285 * is the one implied by the image header.
286 *
287 * Note that ideally this function would not be needed. Every
288 * block driver which implements it is probably doing something
289 * shady regarding its runtime option structure.
290 */
291 void (*bdrv_gather_child_options)(BlockDriverState *bs, QDict *target,
292 bool backing_overridden);
293
294 /*
295 * Returns an allocated string which is the directory name of this BDS: It
296 * will be used to make relative filenames absolute by prepending this
297 * function's return value to them.
298 */
299 char *(*bdrv_dirname)(BlockDriverState *bs, Error **errp);
300
69c0bf11
EGE
301 /*
302 * This informs the driver that we are no longer interested in the result
303 * of in-flight requests, so don't waste the time if possible.
304 *
305 * One example usage is to avoid waiting for an nbd target node reconnect
306 * timeout during job-cancel with force=true.
307 */
308 void (*bdrv_cancel_in_flight)(BlockDriverState *bs);
309
310 int (*bdrv_inactivate)(BlockDriverState *bs);
311
312 int (*bdrv_snapshot_create)(BlockDriverState *bs,
313 QEMUSnapshotInfo *sn_info);
314 int (*bdrv_snapshot_goto)(BlockDriverState *bs,
315 const char *snapshot_id);
316 int (*bdrv_snapshot_delete)(BlockDriverState *bs,
317 const char *snapshot_id,
318 const char *name,
319 Error **errp);
320 int (*bdrv_snapshot_list)(BlockDriverState *bs,
321 QEMUSnapshotInfo **psn_info);
322 int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs,
323 const char *snapshot_id,
324 const char *name,
325 Error **errp);
326
327 int (*bdrv_change_backing_file)(BlockDriverState *bs,
328 const char *backing_file, const char *backing_fmt);
329
330 /* TODO Better pass a option string/QDict/QemuOpts to add any rule? */
331 int (*bdrv_debug_breakpoint)(BlockDriverState *bs, const char *event,
332 const char *tag);
333 int (*bdrv_debug_remove_breakpoint)(BlockDriverState *bs,
334 const char *tag);
335 int (*bdrv_debug_resume)(BlockDriverState *bs, const char *tag);
336 bool (*bdrv_debug_is_suspended)(BlockDriverState *bs, const char *tag);
337
338 void (*bdrv_refresh_limits)(BlockDriverState *bs, Error **errp);
339
340 /*
341 * Returns 1 if newly created images are guaranteed to contain only
342 * zeros, 0 otherwise.
343 */
344 int (*bdrv_has_zero_init)(BlockDriverState *bs);
345
346 /*
347 * Remove fd handlers, timers, and other event loop callbacks so the event
348 * loop is no longer in use. Called with no in-flight requests and in
349 * depth-first traversal order with parents before child nodes.
350 */
351 void (*bdrv_detach_aio_context)(BlockDriverState *bs);
352
353 /*
354 * Add fd handlers, timers, and other event loop callbacks so I/O requests
355 * can be processed again. Called with no in-flight requests and in
356 * depth-first traversal order with child nodes before parent nodes.
357 */
358 void (*bdrv_attach_aio_context)(BlockDriverState *bs,
359 AioContext *new_context);
360
361 /**
362 * Try to get @bs's logical and physical block size.
363 * On success, store them in @bsz and return zero.
364 * On failure, return negative errno.
365 */
366 int (*bdrv_probe_blocksizes)(BlockDriverState *bs, BlockSizes *bsz);
367 /**
368 * Try to get @bs's geometry (cyls, heads, sectors)
369 * On success, store them in @geo and return 0.
370 * On failure return -errno.
371 * Only drivers that want to override guest geometry implement this
372 * callback; see hd_geometry_guess().
373 */
374 int (*bdrv_probe_geometry)(BlockDriverState *bs, HDGeometry *geo);
375
376 void (*bdrv_add_child)(BlockDriverState *parent, BlockDriverState *child,
377 Error **errp);
378 void (*bdrv_del_child)(BlockDriverState *parent, BdrvChild *child,
379 Error **errp);
380
381 /**
382 * Informs the block driver that a permission change is intended. The
383 * driver checks whether the change is permissible and may take other
384 * preparations for the change (e.g. get file system locks). This operation
385 * is always followed either by a call to either .bdrv_set_perm or
386 * .bdrv_abort_perm_update.
387 *
388 * Checks whether the requested set of cumulative permissions in @perm
389 * can be granted for accessing @bs and whether no other users are using
390 * permissions other than those given in @shared (both arguments take
391 * BLK_PERM_* bitmasks).
392 *
393 * If both conditions are met, 0 is returned. Otherwise, -errno is returned
394 * and errp is set to an error describing the conflict.
395 */
396 int (*bdrv_check_perm)(BlockDriverState *bs, uint64_t perm,
397 uint64_t shared, Error **errp);
398
399 /**
400 * Called to inform the driver that the set of cumulative set of used
401 * permissions for @bs has changed to @perm, and the set of sharable
402 * permission to @shared. The driver can use this to propagate changes to
403 * its children (i.e. request permissions only if a parent actually needs
404 * them).
405 *
406 * This function is only invoked after bdrv_check_perm(), so block drivers
407 * may rely on preparations made in their .bdrv_check_perm implementation.
408 */
409 void (*bdrv_set_perm)(BlockDriverState *bs, uint64_t perm, uint64_t shared);
410
411 /*
412 * Called to inform the driver that after a previous bdrv_check_perm()
413 * call, the permission update is not performed and any preparations made
414 * for it (e.g. taken file locks) need to be undone.
415 *
416 * This function can be called even for nodes that never saw a
417 * bdrv_check_perm() call. It is a no-op then.
418 */
419 void (*bdrv_abort_perm_update)(BlockDriverState *bs);
420
421 /**
422 * Returns in @nperm and @nshared the permissions that the driver for @bs
423 * needs on its child @c, based on the cumulative permissions requested by
424 * the parents in @parent_perm and @parent_shared.
425 *
426 * If @c is NULL, return the permissions for attaching a new child for the
427 * given @child_class and @role.
428 *
429 * If @reopen_queue is non-NULL, don't return the currently needed
430 * permissions, but those that will be needed after applying the
431 * @reopen_queue.
432 */
433 void (*bdrv_child_perm)(BlockDriverState *bs, BdrvChild *c,
434 BdrvChildRole role,
435 BlockReopenQueue *reopen_queue,
436 uint64_t parent_perm, uint64_t parent_shared,
437 uint64_t *nperm, uint64_t *nshared);
438
439 /**
440 * Register/unregister a buffer for I/O. For example, when the driver is
441 * interested to know the memory areas that will later be used in iovs, so
442 * that it can do IOMMU mapping with VFIO etc., in order to get better
443 * performance. In the case of VFIO drivers, this callback is used to do
444 * DMA mapping for hot buffers.
f4ec04ba
SH
445 *
446 * Returns: true on success, false on failure
69c0bf11 447 */
d9249c25
KW
448 bool GRAPH_RDLOCK_PTR (*bdrv_register_buf)(
449 BlockDriverState *bs, void *host, size_t size, Error **errp);
450 void GRAPH_RDLOCK_PTR (*bdrv_unregister_buf)(
451 BlockDriverState *bs, void *host, size_t size);
69c0bf11
EGE
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
ebc2752b 473 /* aio */
7b1fb72e 474 BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_preadv)(BlockDriverState *bs,
ebc2752b
EGE
475 int64_t offset, int64_t bytes, QEMUIOVector *qiov,
476 BdrvRequestFlags flags, BlockCompletionFunc *cb, void *opaque);
7b1fb72e
KW
477
478 BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_pwritev)(BlockDriverState *bs,
ebc2752b
EGE
479 int64_t offset, int64_t bytes, QEMUIOVector *qiov,
480 BdrvRequestFlags flags, BlockCompletionFunc *cb, void *opaque);
7b1fb72e 481
88095349
EGE
482 BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_flush)(
483 BlockDriverState *bs, BlockCompletionFunc *cb, void *opaque);
9a5a1c62
EGE
484
485 BlockAIOCB * GRAPH_RDLOCK_PTR (*bdrv_aio_pdiscard)(
486 BlockDriverState *bs, int64_t offset, int bytes,
ebc2752b
EGE
487 BlockCompletionFunc *cb, void *opaque);
488
7b1fb72e 489 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_readv)(BlockDriverState *bs,
ebc2752b
EGE
490 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov);
491
492 /**
493 * @offset: position in bytes to read at
494 * @bytes: number of bytes to read
495 * @qiov: the buffers to fill with read data
496 * @flags: currently unused, always 0
497 *
498 * @offset and @bytes will be a multiple of 'request_alignment',
499 * but the length of individual @qiov elements does not have to
500 * be a multiple.
501 *
502 * @bytes will always equal the total size of @qiov, and will be
503 * no larger than 'max_transfer'.
504 *
505 * The buffer in @qiov may point directly to guest memory.
506 */
7b1fb72e 507 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv)(BlockDriverState *bs,
ebc2752b
EGE
508 int64_t offset, int64_t bytes, QEMUIOVector *qiov,
509 BdrvRequestFlags flags);
510
7b1fb72e
KW
511 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv_part)(
512 BlockDriverState *bs, int64_t offset, int64_t bytes,
ebc2752b
EGE
513 QEMUIOVector *qiov, size_t qiov_offset,
514 BdrvRequestFlags flags);
515
7b1fb72e 516 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_writev)(BlockDriverState *bs,
ebc2752b
EGE
517 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov,
518 int flags);
519 /**
520 * @offset: position in bytes to write at
521 * @bytes: number of bytes to write
522 * @qiov: the buffers containing data to write
523 * @flags: zero or more bits allowed by 'supported_write_flags'
524 *
525 * @offset and @bytes will be a multiple of 'request_alignment',
526 * but the length of individual @qiov elements does not have to
527 * be a multiple.
528 *
529 * @bytes will always equal the total size of @qiov, and will be
530 * no larger than 'max_transfer'.
531 *
532 * The buffer in @qiov may point directly to guest memory.
533 */
7b1fb72e
KW
534 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev)(
535 BlockDriverState *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov,
ebc2752b 536 BdrvRequestFlags flags);
7b1fb72e
KW
537 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_part)(
538 BlockDriverState *bs, int64_t offset, int64_t bytes, QEMUIOVector *qiov,
539 size_t qiov_offset, BdrvRequestFlags flags);
ebc2752b
EGE
540
541 /*
542 * Efficiently zero a region of the disk image. Typically an image format
543 * would use a compact metadata representation to implement this. This
544 * function pointer may be NULL or return -ENOSUP and .bdrv_co_writev()
545 * will be called instead.
546 */
abaf8b75
KW
547 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwrite_zeroes)(
548 BlockDriverState *bs, int64_t offset, int64_t bytes,
549 BdrvRequestFlags flags);
9a5a1c62
EGE
550
551 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pdiscard)(
552 BlockDriverState *bs, int64_t offset, int64_t bytes);
ebc2752b
EGE
553
554 /*
555 * Map [offset, offset + nbytes) range onto a child of @bs to copy from,
556 * and invoke bdrv_co_copy_range_from(child, ...), or invoke
557 * bdrv_co_copy_range_to() if @bs is the leaf child to copy data from.
558 *
559 * See the comment of bdrv_co_copy_range for the parameter and return value
560 * semantics.
561 */
742bf09b
EGE
562 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_copy_range_from)(
563 BlockDriverState *bs, BdrvChild *src, int64_t offset,
564 BdrvChild *dst, int64_t dst_offset, int64_t bytes,
565 BdrvRequestFlags read_flags, BdrvRequestFlags write_flags);
ebc2752b
EGE
566
567 /*
568 * Map [offset, offset + nbytes) range onto a child of bs to copy data to,
569 * and invoke bdrv_co_copy_range_to(child, src, ...), or perform the copy
570 * operation if @bs is the leaf and @src has the same BlockDriver. Return
571 * -ENOTSUP if @bs is the leaf but @src has a different BlockDriver.
572 *
573 * See the comment of bdrv_co_copy_range for the parameter and return value
574 * semantics.
575 */
742bf09b
EGE
576 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_copy_range_to)(
577 BlockDriverState *bs, BdrvChild *src, int64_t src_offset,
578 BdrvChild *dst, int64_t dst_offset, int64_t bytes,
579 BdrvRequestFlags read_flags, BdrvRequestFlags write_flags);
ebc2752b
EGE
580
581 /*
582 * Building block for bdrv_block_status[_above] and
583 * bdrv_is_allocated[_above]. The driver should answer only
584 * according to the current layer, and should only need to set
585 * BDRV_BLOCK_DATA, BDRV_BLOCK_ZERO, BDRV_BLOCK_OFFSET_VALID,
586 * and/or BDRV_BLOCK_RAW; if the current layer defers to a backing
587 * layer, the result should be 0 (and not BDRV_BLOCK_ZERO). See
588 * block.h for the overall meaning of the bits. As a hint, the
589 * flag want_zero is true if the caller cares more about precise
590 * mappings (favor accurate _OFFSET_VALID/_ZERO) or false for
591 * overall allocation (favor larger *pnum, perhaps by reporting
592 * _DATA instead of _ZERO). The block layer guarantees input
593 * clamped to bdrv_getlength() and aligned to request_alignment,
594 * as well as non-NULL pnum, map, and file; in turn, the driver
595 * must return an error or set pnum to an aligned non-zero value.
596 *
597 * Note that @bytes is just a hint on how big of a region the
598 * caller wants to inspect. It is not a limit on *pnum.
599 * Implementations are free to return larger values of *pnum if
600 * doing so does not incur a performance penalty.
601 *
602 * block/io.c's bdrv_co_block_status() will utilize an unclamped
603 * *pnum value for the block-status cache on protocol nodes, prior
604 * to clamping *pnum for return to its caller.
605 */
7ff9579e
KW
606 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_block_status)(
607 BlockDriverState *bs,
ebc2752b
EGE
608 bool want_zero, int64_t offset, int64_t bytes, int64_t *pnum,
609 int64_t *map, BlockDriverState **file);
610
ce14f3b4
VSO
611 /*
612 * Snapshot-access API.
613 *
614 * Block-driver may provide snapshot-access API: special functions to access
615 * some internal "snapshot". The functions are similar with normal
616 * read/block_status/discard handler, but don't have any specific handling
617 * in generic block-layer: no serializing, no alignment, no tracked
618 * requests. So, block-driver that realizes these APIs is fully responsible
619 * for synchronization between snapshot-access API and normal IO requests.
1c14eaab
VSO
620 *
621 * TODO: To be able to support qcow2's internal snapshots, this API will
622 * need to be extended to:
623 * - be able to select a specific snapshot
624 * - receive the snapshot's actual length (which may differ from bs's
625 * length)
ce14f3b4 626 */
7b9e8b22
KW
627 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_preadv_snapshot)(
628 BlockDriverState *bs, int64_t offset, int64_t bytes,
629 QEMUIOVector *qiov, size_t qiov_offset);
630
631 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_snapshot_block_status)(
632 BlockDriverState *bs, bool want_zero, int64_t offset, int64_t bytes,
633 int64_t *pnum, int64_t *map, BlockDriverState **file);
9a5a1c62
EGE
634
635 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pdiscard_snapshot)(
636 BlockDriverState *bs, int64_t offset, int64_t bytes);
ce14f3b4 637
ebc2752b
EGE
638 /*
639 * Invalidate any cached meta-data.
640 */
1b3ff9fe
KW
641 void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_invalidate_cache)(
642 BlockDriverState *bs, Error **errp);
ebc2752b
EGE
643
644 /*
645 * Flushes all data for all layers by calling bdrv_co_flush for underlying
646 * layers, if needed. This function is needed for deterministic
647 * synchronization of the flush finishing callback.
648 */
88095349 649 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush)(BlockDriverState *bs);
ebc2752b
EGE
650
651 /* Delete a created file. */
48aef794
KW
652 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_delete_file)(
653 BlockDriverState *bs, Error **errp);
ebc2752b
EGE
654
655 /*
656 * Flushes all data that was already written to the OS all the way down to
657 * the disk (for example file-posix.c calls fsync()).
658 */
88095349
EGE
659 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush_to_disk)(
660 BlockDriverState *bs);
ebc2752b
EGE
661
662 /*
663 * Flushes all internal caches to the OS. The data may still sit in a
664 * writeback cache of the host OS, but it will survive a crash of the qemu
665 * process.
666 */
88095349
EGE
667 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_flush_to_os)(
668 BlockDriverState *bs);
ebc2752b 669
ebc2752b
EGE
670 /*
671 * Truncate @bs to @offset bytes using the given @prealloc mode
672 * when growing. Modes other than PREALLOC_MODE_OFF should be
673 * rejected when shrinking @bs.
674 *
675 * If @exact is true, @bs must be resized to exactly @offset.
676 * Otherwise, it is sufficient for @bs (if it is a host block
677 * device and thus there is no way to resize it) to be at least
678 * @offset bytes in length.
679 *
680 * If @exact is true and this function fails but would succeed
681 * with @exact = false, it should return -ENOTSUP.
682 */
c2b8e315
KW
683 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_truncate)(
684 BlockDriverState *bs, int64_t offset, bool exact,
685 PreallocMode prealloc, BdrvRequestFlags flags, Error **errp);
686
c86422c5 687 int64_t coroutine_fn (*bdrv_co_getlength)(BlockDriverState *bs);
82618d7b
EGE
688 int64_t coroutine_fn (*bdrv_co_get_allocated_file_size)(
689 BlockDriverState *bs);
690
ebc2752b
EGE
691 BlockMeasureInfo *(*bdrv_measure)(QemuOpts *opts, BlockDriverState *in_bs,
692 Error **errp);
693
7b1fb72e
KW
694 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_compressed)(
695 BlockDriverState *bs, int64_t offset, int64_t bytes,
696 QEMUIOVector *qiov);
697
698 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_pwritev_compressed_part)(
699 BlockDriverState *bs, int64_t offset, int64_t bytes,
700 QEMUIOVector *qiov, size_t qiov_offset);
ebc2752b 701
3d47eb0a
EGE
702 int coroutine_fn (*bdrv_co_get_info)(BlockDriverState *bs,
703 BlockDriverInfo *bdi);
ebc2752b
EGE
704
705 ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs,
706 Error **errp);
707 BlockStatsSpecific *(*bdrv_get_specific_stats)(BlockDriverState *bs);
708
ca5e2ad9 709 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_save_vmstate)(
1b3ff9fe
KW
710 BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
711
ca5e2ad9 712 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_load_vmstate)(
1b3ff9fe 713 BlockDriverState *bs, QEMUIOVector *qiov, int64_t pos);
ebc2752b 714
ebc2752b 715 /* removable device specific */
c73ff92c
EGE
716 bool coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_is_inserted)(
717 BlockDriverState *bs);
79a292e5
KW
718 void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_eject)(
719 BlockDriverState *bs, bool eject_flag);
720 void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_lock_medium)(
721 BlockDriverState *bs, bool locked);
ebc2752b
EGE
722
723 /* to control generic scsi devices */
26c518ab
KW
724 BlockAIOCB *coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_aio_ioctl)(
725 BlockDriverState *bs, unsigned long int req, void *buf,
ebc2752b 726 BlockCompletionFunc *cb, void *opaque);
26c518ab
KW
727
728 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_ioctl)(
729 BlockDriverState *bs, unsigned long int req, void *buf);
ebc2752b 730
ebc2752b
EGE
731 /*
732 * Returns 0 for completed check, -errno for internal errors.
733 * The check results are stored in result.
734 */
1b3ff9fe
KW
735 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_check)(
736 BlockDriverState *bs, BdrvCheckResult *result, BdrvCheckMode fix);
ebc2752b 737
c834dc05
EGE
738 void coroutine_fn (*bdrv_co_debug_event)(BlockDriverState *bs,
739 BlkdebugEvent event);
ebc2752b 740
ebc2752b 741 /* io queue for linux-aio */
c3827069
KW
742 void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_io_plug)(BlockDriverState *bs);
743 void coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_io_unplug)(
744 BlockDriverState *bs);
ebc2752b 745
ebc2752b 746 /**
5e8ac217 747 * bdrv_drain_begin is called if implemented in the beginning of a
ebc2752b
EGE
748 * drain operation to drain and stop any internal sources of requests in
749 * the driver.
5e8ac217 750 * bdrv_drain_end is called if implemented at the end of the drain.
ebc2752b
EGE
751 *
752 * They should be used by the driver to e.g. manage scheduled I/O
753 * requests, or toggle an internal state. After the end of the drain new
754 * requests will continue normally.
5e8ac217
KW
755 *
756 * Implementations of both functions must not call aio_poll().
ebc2752b 757 */
5e8ac217
KW
758 void (*bdrv_drain_begin)(BlockDriverState *bs);
759 void (*bdrv_drain_end)(BlockDriverState *bs);
ebc2752b 760
ebc2752b 761 bool (*bdrv_supports_persistent_dirty_bitmap)(BlockDriverState *bs);
167f748d
KW
762
763 bool coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_can_store_new_dirty_bitmap)(
c2d76808
AF
764 BlockDriverState *bs, const char *name, uint32_t granularity,
765 Error **errp);
167f748d
KW
766
767 int coroutine_fn GRAPH_RDLOCK_PTR (*bdrv_co_remove_persistent_dirty_bitmap)(
c2d76808 768 BlockDriverState *bs, const char *name, Error **errp);
ebc2752b
EGE
769};
770
7b1fb72e 771static inline bool TSA_NO_TSA block_driver_can_compress(BlockDriver *drv)
ebc2752b
EGE
772{
773 return drv->bdrv_co_pwritev_compressed ||
774 drv->bdrv_co_pwritev_compressed_part;
775}
776
777typedef struct BlockLimits {
778 /*
779 * Alignment requirement, in bytes, for offset/length of I/O
780 * requests. Must be a power of 2 less than INT_MAX; defaults to
781 * 1 for drivers with modern byte interfaces, and to 512
782 * otherwise.
783 */
784 uint32_t request_alignment;
785
786 /*
787 * Maximum number of bytes that can be discarded at once. Must be multiple
788 * of pdiscard_alignment, but need not be power of 2. May be 0 if no
789 * inherent 64-bit limit.
790 */
791 int64_t max_pdiscard;
792
793 /*
794 * Optimal alignment for discard requests in bytes. A power of 2
795 * is best but not mandatory. Must be a multiple of
796 * bl.request_alignment, and must be less than max_pdiscard if
797 * that is set. May be 0 if bl.request_alignment is good enough
798 */
799 uint32_t pdiscard_alignment;
800
801 /*
802 * Maximum number of bytes that can zeroized at once. Must be multiple of
803 * pwrite_zeroes_alignment. 0 means no limit.
804 */
805 int64_t max_pwrite_zeroes;
806
807 /*
808 * Optimal alignment for write zeroes requests in bytes. A power
809 * of 2 is best but not mandatory. Must be a multiple of
810 * bl.request_alignment, and must be less than max_pwrite_zeroes
811 * if that is set. May be 0 if bl.request_alignment is good
812 * enough
813 */
814 uint32_t pwrite_zeroes_alignment;
815
816 /*
817 * Optimal transfer length in bytes. A power of 2 is best but not
818 * mandatory. Must be a multiple of bl.request_alignment, or 0 if
819 * no preferred size
820 */
821 uint32_t opt_transfer;
822
823 /*
824 * Maximal transfer length in bytes. Need not be power of 2, but
825 * must be multiple of opt_transfer and bl.request_alignment, or 0
826 * for no 32-bit limit. For now, anything larger than INT_MAX is
827 * clamped down.
828 */
829 uint32_t max_transfer;
830
831 /*
832 * Maximal hardware transfer length in bytes. Applies whenever
833 * transfers to the device bypass the kernel I/O scheduler, for
834 * example with SG_IO. If larger than max_transfer or if zero,
835 * blk_get_max_hw_transfer will fall back to max_transfer.
836 */
837 uint64_t max_hw_transfer;
838
839 /*
840 * Maximal number of scatter/gather elements allowed by the hardware.
841 * Applies whenever transfers to the device bypass the kernel I/O
842 * scheduler, for example with SG_IO. If larger than max_iov
843 * or if zero, blk_get_max_hw_iov will fall back to max_iov.
844 */
845 int max_hw_iov;
846
847
848 /* memory alignment, in bytes so that no bounce buffer is needed */
849 size_t min_mem_alignment;
850
851 /* memory alignment, in bytes, for bounce buffer */
852 size_t opt_mem_alignment;
853
854 /* maximum number of iovec elements */
855 int max_iov;
856} BlockLimits;
857
858typedef struct BdrvOpBlocker BdrvOpBlocker;
859
860typedef struct BdrvAioNotifier {
861 void (*attached_aio_context)(AioContext *new_context, void *opaque);
862 void (*detach_aio_context)(void *opaque);
863
864 void *opaque;
865 bool deleted;
866
867 QLIST_ENTRY(BdrvAioNotifier) list;
868} BdrvAioNotifier;
869
870struct BdrvChildClass {
871 /*
872 * If true, bdrv_replace_node() doesn't change the node this BdrvChild
873 * points to.
874 */
875 bool stay_at_node;
876
877 /*
878 * If true, the parent is a BlockDriverState and bdrv_next_all_states()
879 * will return it. This information is used for drain_all, where every node
880 * will be drained separately, so the drain only needs to be propagated to
881 * non-BDS parents.
882 */
883 bool parent_is_bds;
884
abc5a79c
EGE
885 /*
886 * Global state (GS) API. These functions run under the BQL.
887 *
888 * See include/block/block-global-state.h for more information about
889 * the GS API.
890 */
ebc2752b
EGE
891 void (*inherit_options)(BdrvChildRole role, bool parent_is_format,
892 int *child_flags, QDict *child_options,
893 int parent_flags, QDict *parent_options);
ebc2752b 894 void (*change_media)(BdrvChild *child, bool load);
ebc2752b
EGE
895
896 /*
897 * Returns a malloced string that describes the parent of the child for a
898 * human reader. This could be a node-name, BlockBackend name, qdev ID or
899 * QOM path of the device owning the BlockBackend, job type and ID etc. The
900 * caller is responsible for freeing the memory.
901 */
902 char *(*get_parent_desc)(BdrvChild *child);
903
abc5a79c
EGE
904 /*
905 * Notifies the parent that the child has been activated/inactivated (e.g.
906 * when migration is completing) and it can start/stop requesting
907 * permissions and doing I/O on it.
908 */
909 void (*activate)(BdrvChild *child, Error **errp);
910 int (*inactivate)(BdrvChild *child);
911
303de47b
KW
912 void GRAPH_WRLOCK_PTR (*attach)(BdrvChild *child);
913 void GRAPH_WRLOCK_PTR (*detach)(BdrvChild *child);
abc5a79c
EGE
914
915 /*
916 * Notifies the parent that the filename of its child has changed (e.g.
917 * because the direct child was removed from the backing chain), so that it
918 * can update its reference.
919 */
920 int (*update_filename)(BdrvChild *child, BlockDriverState *new_base,
921 const char *filename, Error **errp);
922
7e8c182f 923 bool (*change_aio_ctx)(BdrvChild *child, AioContext *ctx,
e08cc001
EGE
924 GHashTable *visited, Transaction *tran,
925 Error **errp);
abc5a79c 926
abc5a79c
EGE
927 /*
928 * I/O API functions. These functions are thread-safe.
929 *
930 * See include/block/block-io.h for more information about
931 * the I/O API.
932 */
933
934 void (*resize)(BdrvChild *child);
935
936 /*
937 * Returns a name that is supposedly more useful for human users than the
938 * node name for identifying the node in question (in particular, a BB
939 * name), or NULL if the parent can't provide a better name.
940 */
941 const char *(*get_name)(BdrvChild *child);
942
d5f8d79c
HR
943 AioContext *(*get_parent_aio_context)(BdrvChild *child);
944
ebc2752b
EGE
945 /*
946 * If this pair of functions is implemented, the parent doesn't issue new
947 * requests after returning from .drained_begin() until .drained_end() is
948 * called.
949 *
950 * These functions must not change the graph (and therefore also must not
951 * call aio_poll(), which could change the graph indirectly).
952 *
ebc2752b
EGE
953 * Note that this can be nested. If drained_begin() was called twice, new
954 * I/O is allowed only after drained_end() was called twice, too.
955 */
956 void (*drained_begin)(BdrvChild *child);
2f65df6e 957 void (*drained_end)(BdrvChild *child);
ebc2752b
EGE
958
959 /*
960 * Returns whether the parent has pending requests for the child. This
961 * callback is polled after .drained_begin() has been called until all
962 * activity on the child has stopped.
963 */
964 bool (*drained_poll)(BdrvChild *child);
ebc2752b
EGE
965};
966
967extern const BdrvChildClass child_of_bds;
968
969struct BdrvChild {
970 BlockDriverState *bs;
971 char *name;
972 const BdrvChildClass *klass;
973 BdrvChildRole role;
974 void *opaque;
975
976 /**
977 * Granted permissions for operating on this BdrvChild (BLK_PERM_* bitmask)
978 */
979 uint64_t perm;
980
981 /**
982 * Permissions that can still be granted to other users of @bs while this
983 * BdrvChild is still attached to it. (BLK_PERM_* bitmask)
984 */
985 uint64_t shared_perm;
986
987 /*
988 * This link is frozen: the child can neither be replaced nor
989 * detached from the parent.
990 */
991 bool frozen;
992
993 /*
57e05be3 994 * True if the parent of this child has been drained by this BdrvChild
ebc2752b 995 * (through klass->drained_*).
57e05be3
KW
996 *
997 * It is generally true if bs->quiesce_counter > 0. It may differ while the
ebc2752b
EGE
998 * child is entering or leaving a drained section.
999 */
57e05be3 1000 bool quiesced_parent;
ebc2752b
EGE
1001
1002 QLIST_ENTRY(BdrvChild) next;
1003 QLIST_ENTRY(BdrvChild) next_parent;
1004};
1005
1006/*
1007 * Allows bdrv_co_block_status() to cache one data region for a
1008 * protocol node.
1009 *
1010 * @valid: Whether the cache is valid (should be accessed with atomic
1011 * functions so this can be reset by RCU readers)
1012 * @data_start: Offset where we know (or strongly assume) is data
1013 * @data_end: Offset where the data region ends (which is not necessarily
1014 * the start of a zeroed region)
1015 */
1016typedef struct BdrvBlockStatusCache {
1017 struct rcu_head rcu;
1018
1019 bool valid;
1020 int64_t data_start;
1021 int64_t data_end;
1022} BdrvBlockStatusCache;
1023
1024struct BlockDriverState {
1025 /*
1026 * Protected by big QEMU lock or read-only after opening. No special
1027 * locking needed during I/O...
1028 */
1029 int open_flags; /* flags used to open the file, re-used for re-open */
1030 bool encrypted; /* if true, the media is encrypted */
1031 bool sg; /* if true, the device is a /dev/sg* */
1032 bool probed; /* if true, format was probed rather than specified */
1033 bool force_share; /* if true, always allow all shared permissions */
1034 bool implicit; /* if true, this filter node was automatically inserted */
1035
1036 BlockDriver *drv; /* NULL means no media */
1037 void *opaque;
1038
1039 AioContext *aio_context; /* event loop used for fd handlers, timers, etc */
1040 /*
1041 * long-running tasks intended to always use the same AioContext as this
1042 * BDS may register themselves in this list to be notified of changes
1043 * regarding this BDS's context
1044 */
1045 QLIST_HEAD(, BdrvAioNotifier) aio_notifiers;
1046 bool walking_aio_notifiers; /* to make removal during iteration safe */
1047
1048 char filename[PATH_MAX];
1049 /*
1050 * If not empty, this image is a diff in relation to backing_file.
1051 * Note that this is the name given in the image header and
1052 * therefore may or may not be equal to .backing->bs->filename.
1053 * If this field contains a relative path, it is to be resolved
1054 * relatively to the overlay's location.
1055 */
1056 char backing_file[PATH_MAX];
1057 /*
1058 * The backing filename indicated by the image header. Contrary
1059 * to backing_file, if we ever open this file, auto_backing_file
1060 * is replaced by the resulting BDS's filename (i.e. after a
1061 * bdrv_refresh_filename() run).
1062 */
1063 char auto_backing_file[PATH_MAX];
1064 char backing_format[16]; /* if non-zero and backing_file exists */
1065
1066 QDict *full_open_options;
1067 char exact_filename[PATH_MAX];
1068
ebc2752b
EGE
1069 /* I/O Limits */
1070 BlockLimits bl;
1071
1072 /*
1073 * Flags honored during pread
1074 */
98b3ddc7 1075 BdrvRequestFlags supported_read_flags;
ebc2752b
EGE
1076 /*
1077 * Flags honored during pwrite (so far: BDRV_REQ_FUA,
1078 * BDRV_REQ_WRITE_UNCHANGED).
1079 * If a driver does not support BDRV_REQ_WRITE_UNCHANGED, those
1080 * writes will be issued as normal writes without the flag set.
1081 * This is important to note for drivers that do not explicitly
1082 * request a WRITE permission for their children and instead take
1083 * the same permissions as their parent did (this is commonly what
1084 * block filters do). Such drivers have to be aware that the
1085 * parent may have taken a WRITE_UNCHANGED permission only and is
1086 * issuing such requests. Drivers either must make sure that
1087 * these requests do not result in plain WRITE accesses (usually
1088 * by supporting BDRV_REQ_WRITE_UNCHANGED, and then forwarding
1089 * every incoming write request as-is, including potentially that
1090 * flag), or they have to explicitly take the WRITE permission for
1091 * their children.
1092 */
98b3ddc7 1093 BdrvRequestFlags supported_write_flags;
ebc2752b
EGE
1094 /*
1095 * Flags honored during pwrite_zeroes (so far: BDRV_REQ_FUA,
1096 * BDRV_REQ_MAY_UNMAP, BDRV_REQ_WRITE_UNCHANGED)
1097 */
98b3ddc7 1098 BdrvRequestFlags supported_zero_flags;
ebc2752b
EGE
1099 /*
1100 * Flags honoured during truncate (so far: BDRV_REQ_ZERO_WRITE).
1101 *
1102 * If BDRV_REQ_ZERO_WRITE is given, the truncate operation must make sure
1103 * that any added space reads as all zeros. If this can't be guaranteed,
1104 * the operation must fail.
1105 */
98b3ddc7 1106 BdrvRequestFlags supported_truncate_flags;
ebc2752b
EGE
1107
1108 /* the following member gives a name to every node on the bs graph. */
1109 char node_name[32];
1110 /* element of the list of named nodes building the graph */
1111 QTAILQ_ENTRY(BlockDriverState) node_list;
1112 /* element of the list of all BlockDriverStates (all_bdrv_states) */
1113 QTAILQ_ENTRY(BlockDriverState) bs_list;
1114 /* element of the list of monitor-owned BDS */
1115 QTAILQ_ENTRY(BlockDriverState) monitor_list;
1116 int refcnt;
1117
1118 /* operation blockers. Protected by BQL. */
1119 QLIST_HEAD(, BdrvOpBlocker) op_blockers[BLOCK_OP_TYPE_MAX];
1120
1121 /*
1122 * The node that this node inherited default options from (and a reopen on
1123 * which can affect this node by changing these defaults). This is always a
1124 * parent node of this node.
1125 */
1126 BlockDriverState *inherits_from;
5bb04747
VSO
1127
1128 /*
1129 * @backing and @file are some of @children or NULL. All these three fields
1130 * (@file, @backing and @children) are modified only in
1131 * bdrv_child_cb_attach() and bdrv_child_cb_detach().
1132 *
1133 * See also comment in include/block/block.h, to learn how backing and file
1134 * are connected with BdrvChildRole.
1135 */
ebc2752b 1136 QLIST_HEAD(, BdrvChild) children;
5bb04747
VSO
1137 BdrvChild *backing;
1138 BdrvChild *file;
1139
ebc2752b
EGE
1140 QLIST_HEAD(, BdrvChild) parents;
1141
1142 QDict *options;
1143 QDict *explicit_options;
1144 BlockdevDetectZeroesOptions detect_zeroes;
1145
1146 /* The error object in use for blocking operations on backing_hd */
1147 Error *backing_blocker;
1148
1149 /* Protected by AioContext lock */
1150
1151 /*
1152 * If we are reading a disk image, give its size in sectors.
1153 * Generally read-only; it is written to by load_snapshot and
1154 * save_snaphost, but the block layer is quiescent during those.
1155 */
1156 int64_t total_sectors;
1157
1158 /* threshold limit for writes, in bytes. "High water mark". */
1159 uint64_t write_threshold_offset;
1160
1161 /*
1162 * Writing to the list requires the BQL _and_ the dirty_bitmap_mutex.
1163 * Reading from the list can be done with either the BQL or the
1164 * dirty_bitmap_mutex. Modifying a bitmap only requires
1165 * dirty_bitmap_mutex.
1166 */
1167 QemuMutex dirty_bitmap_mutex;
1168 QLIST_HEAD(, BdrvDirtyBitmap) dirty_bitmaps;
1169
1170 /* Offset after the highest byte written to */
1171 Stat64 wr_highest_offset;
1172
1173 /*
1174 * If true, copy read backing sectors into image. Can be >1 if more
1175 * than one client has requested copy-on-read. Accessed with atomic
1176 * ops.
1177 */
1178 int copy_on_read;
1179
1180 /*
1181 * number of in-flight requests; overall and serialising.
1182 * Accessed with atomic ops.
1183 */
1184 unsigned int in_flight;
1185 unsigned int serialising_in_flight;
1186
1187 /*
1188 * counter for nested bdrv_io_plug.
1189 * Accessed with atomic ops.
1190 */
1191 unsigned io_plugged;
1192
1193 /* do we need to tell the quest if we have a volatile write cache? */
1194 int enable_write_cache;
1195
1196 /* Accessed with atomic ops. */
1197 int quiesce_counter;
ebc2752b
EGE
1198
1199 unsigned int write_gen; /* Current data generation */
1200
1201 /* Protected by reqs_lock. */
1202 CoMutex reqs_lock;
1203 QLIST_HEAD(, BdrvTrackedRequest) tracked_requests;
1204 CoQueue flush_queue; /* Serializing flush queue */
1205 bool active_flush_req; /* Flush request in flight? */
1206
1207 /* Only read/written by whoever has set active_flush_req to true. */
1208 unsigned int flushed_gen; /* Flushed write generation */
1209
1210 /* BdrvChild links to this node may never be frozen */
1211 bool never_freeze;
1212
1213 /* Lock for block-status cache RCU writers */
1214 CoMutex bsc_modify_lock;
1215 /* Always non-NULL, but must only be dereferenced under an RCU read guard */
1216 BdrvBlockStatusCache *block_status_cache;
1217};
1218
1219struct BlockBackendRootState {
1220 int open_flags;
1221 BlockdevDetectZeroesOptions detect_zeroes;
1222};
1223
1224typedef enum BlockMirrorBackingMode {
1225 /*
1226 * Reuse the existing backing chain from the source for the target.
1227 * - sync=full: Set backing BDS to NULL.
1228 * - sync=top: Use source's backing BDS.
1229 * - sync=none: Use source as the backing BDS.
1230 */
1231 MIRROR_SOURCE_BACKING_CHAIN,
1232
1233 /* Open the target's backing chain completely anew */
1234 MIRROR_OPEN_BACKING_CHAIN,
1235
1236 /* Do not change the target's backing BDS after job completion */
1237 MIRROR_LEAVE_BACKING_CHAIN,
1238} BlockMirrorBackingMode;
1239
1240
1241/*
1242 * Essential block drivers which must always be statically linked into qemu, and
1243 * which therefore can be accessed without using bdrv_find_format()
1244 */
1245extern BlockDriver bdrv_file;
1246extern BlockDriver bdrv_raw;
1247extern BlockDriver bdrv_qcow2;
1248
1249extern unsigned int bdrv_drain_all_count;
1250extern QemuOptsList bdrv_create_opts_simple;
1251
1252/*
1253 * Common functions that are neither I/O nor Global State.
1254 *
1255 * See include/block/block-commmon.h for more information about
1256 * the Common API.
1257 */
1258
1259static inline BlockDriverState *child_bs(BdrvChild *child)
1260{
1261 return child ? child->bs : NULL;
1262}
1263
1264int bdrv_check_request(int64_t offset, int64_t bytes, Error **errp);
69fbfff9 1265char *create_tmp_file(Error **errp);
ebc2752b
EGE
1266void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix,
1267 QDict *options);
1268
1269
1270int bdrv_check_qiov_request(int64_t offset, int64_t bytes,
1271 QEMUIOVector *qiov, size_t qiov_offset,
1272 Error **errp);
1273
1274#ifdef _WIN32
1275int is_windows_drive(const char *filename);
1276#endif
1277
1278#endif /* BLOCK_INT_COMMON_H */