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