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