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_H 25 #define BLOCK_INT_H 26 27 #include "block/accounting.h" 28 #include "block/block.h" 29 #include "block/aio-wait.h" 30 #include "qemu/queue.h" 31 #include "qemu/coroutine.h" 32 #include "qemu/stats64.h" 33 #include "qemu/timer.h" 34 #include "qemu/hbitmap.h" 35 #include "block/snapshot.h" 36 #include "qemu/throttle.h" 37 #include "qemu/rcu.h" 38 39 #define BLOCK_FLAG_LAZY_REFCOUNTS 8 40 41 #define BLOCK_OPT_SIZE "size" 42 #define BLOCK_OPT_ENCRYPT "encryption" 43 #define BLOCK_OPT_ENCRYPT_FORMAT "encrypt.format" 44 #define BLOCK_OPT_COMPAT6 "compat6" 45 #define BLOCK_OPT_HWVERSION "hwversion" 46 #define BLOCK_OPT_BACKING_FILE "backing_file" 47 #define BLOCK_OPT_BACKING_FMT "backing_fmt" 48 #define BLOCK_OPT_CLUSTER_SIZE "cluster_size" 49 #define BLOCK_OPT_TABLE_SIZE "table_size" 50 #define BLOCK_OPT_PREALLOC "preallocation" 51 #define BLOCK_OPT_SUBFMT "subformat" 52 #define BLOCK_OPT_COMPAT_LEVEL "compat" 53 #define BLOCK_OPT_LAZY_REFCOUNTS "lazy_refcounts" 54 #define BLOCK_OPT_ADAPTER_TYPE "adapter_type" 55 #define BLOCK_OPT_REDUNDANCY "redundancy" 56 #define BLOCK_OPT_NOCOW "nocow" 57 #define BLOCK_OPT_EXTENT_SIZE_HINT "extent_size_hint" 58 #define BLOCK_OPT_OBJECT_SIZE "object_size" 59 #define BLOCK_OPT_REFCOUNT_BITS "refcount_bits" 60 #define BLOCK_OPT_DATA_FILE "data_file" 61 #define BLOCK_OPT_DATA_FILE_RAW "data_file_raw" 62 #define BLOCK_OPT_COMPRESSION_TYPE "compression_type" 63 #define BLOCK_OPT_EXTL2 "extended_l2" 64 65 #define BLOCK_PROBE_BUF_SIZE 512 66 67 enum BdrvTrackedRequestType { 68 BDRV_TRACKED_READ, 69 BDRV_TRACKED_WRITE, 70 BDRV_TRACKED_DISCARD, 71 BDRV_TRACKED_TRUNCATE, 72 }; 73 74 /* 75 * That is not quite good that BdrvTrackedRequest structure is public, 76 * as block/io.c is very careful about incoming offset/bytes being 77 * correct. Be sure to assert bdrv_check_request() succeeded after any 78 * modification of BdrvTrackedRequest object out of block/io.c 79 */ 80 typedef struct BdrvTrackedRequest { 81 BlockDriverState *bs; 82 int64_t offset; 83 int64_t bytes; 84 enum BdrvTrackedRequestType type; 85 86 bool serialising; 87 int64_t overlap_offset; 88 int64_t overlap_bytes; 89 90 QLIST_ENTRY(BdrvTrackedRequest) list; 91 Coroutine *co; /* owner, used for deadlock detection */ 92 CoQueue wait_queue; /* coroutines blocked on this request */ 93 94 struct BdrvTrackedRequest *waiting_for; 95 } BdrvTrackedRequest; 96 97 int bdrv_check_request(int64_t offset, int64_t bytes, Error **errp); 98 99 struct BlockDriver { 100 const char *format_name; 101 int instance_size; 102 103 /* set to true if the BlockDriver is a block filter. Block filters pass 104 * certain callbacks that refer to data (see block.c) to their bs->file 105 * or bs->backing (whichever one exists) if the driver doesn't implement 106 * them. Drivers that do not wish to forward must implement them and return 107 * -ENOTSUP. 108 * Note that filters are not allowed to modify data. 109 * 110 * Filters generally cannot have more than a single filtered child, 111 * because the data they present must at all times be the same as 112 * that on their filtered child. That would be impossible to 113 * achieve for multiple filtered children. 114 * (And this filtered child must then be bs->file or bs->backing.) 115 */ 116 bool is_filter; 117 /* 118 * Set to true if the BlockDriver is a format driver. Format nodes 119 * generally do not expect their children to be other format nodes 120 * (except for backing files), and so format probing is disabled 121 * on those children. 122 */ 123 bool is_format; 124 /* 125 * Return true if @to_replace can be replaced by a BDS with the 126 * same data as @bs without it affecting @bs's behavior (that is, 127 * without it being visible to @bs's parents). 128 */ 129 bool (*bdrv_recurse_can_replace)(BlockDriverState *bs, 130 BlockDriverState *to_replace); 131 132 int (*bdrv_probe)(const uint8_t *buf, int buf_size, const char *filename); 133 int (*bdrv_probe_device)(const char *filename); 134 135 /* Any driver implementing this callback is expected to be able to handle 136 * NULL file names in its .bdrv_open() implementation */ 137 void (*bdrv_parse_filename)(const char *filename, QDict *options, Error **errp); 138 /* Drivers not implementing bdrv_parse_filename nor bdrv_open should have 139 * this field set to true, except ones that are defined only by their 140 * child's bs. 141 * An example of the last type will be the quorum block driver. 142 */ 143 bool bdrv_needs_filename; 144 145 /* 146 * Set if a driver can support backing files. This also implies the 147 * following semantics: 148 * 149 * - Return status 0 of .bdrv_co_block_status means that corresponding 150 * blocks are not allocated in this layer of backing-chain 151 * - For such (unallocated) blocks, read will: 152 * - fill buffer with zeros if there is no backing file 153 * - read from the backing file otherwise, where the block layer 154 * takes care of reading zeros beyond EOF if backing file is short 155 */ 156 bool supports_backing; 157 158 /* For handling image reopen for split or non-split files */ 159 int (*bdrv_reopen_prepare)(BDRVReopenState *reopen_state, 160 BlockReopenQueue *queue, Error **errp); 161 void (*bdrv_reopen_commit)(BDRVReopenState *reopen_state); 162 void (*bdrv_reopen_commit_post)(BDRVReopenState *reopen_state); 163 void (*bdrv_reopen_abort)(BDRVReopenState *reopen_state); 164 void (*bdrv_join_options)(QDict *options, QDict *old_options); 165 166 int (*bdrv_open)(BlockDriverState *bs, QDict *options, int flags, 167 Error **errp); 168 169 /* Protocol drivers should implement this instead of bdrv_open */ 170 int (*bdrv_file_open)(BlockDriverState *bs, QDict *options, int flags, 171 Error **errp); 172 void (*bdrv_close)(BlockDriverState *bs); 173 174 175 int coroutine_fn (*bdrv_co_create)(BlockdevCreateOptions *opts, 176 Error **errp); 177 int coroutine_fn (*bdrv_co_create_opts)(BlockDriver *drv, 178 const char *filename, 179 QemuOpts *opts, 180 Error **errp); 181 182 int coroutine_fn (*bdrv_co_amend)(BlockDriverState *bs, 183 BlockdevAmendOptions *opts, 184 bool force, 185 Error **errp); 186 187 int (*bdrv_amend_options)(BlockDriverState *bs, 188 QemuOpts *opts, 189 BlockDriverAmendStatusCB *status_cb, 190 void *cb_opaque, 191 bool force, 192 Error **errp); 193 194 int (*bdrv_make_empty)(BlockDriverState *bs); 195 196 /* 197 * Refreshes the bs->exact_filename field. If that is impossible, 198 * bs->exact_filename has to be left empty. 199 */ 200 void (*bdrv_refresh_filename)(BlockDriverState *bs); 201 202 /* 203 * Gathers the open options for all children into @target. 204 * A simple format driver (without backing file support) might 205 * implement this function like this: 206 * 207 * QINCREF(bs->file->bs->full_open_options); 208 * qdict_put(target, "file", bs->file->bs->full_open_options); 209 * 210 * If not specified, the generic implementation will simply put 211 * all children's options under their respective name. 212 * 213 * @backing_overridden is true when bs->backing seems not to be 214 * the child that would result from opening bs->backing_file. 215 * Therefore, if it is true, the backing child's options should be 216 * gathered; otherwise, there is no need since the backing child 217 * is the one implied by the image header. 218 * 219 * Note that ideally this function would not be needed. Every 220 * block driver which implements it is probably doing something 221 * shady regarding its runtime option structure. 222 */ 223 void (*bdrv_gather_child_options)(BlockDriverState *bs, QDict *target, 224 bool backing_overridden); 225 226 /* 227 * Returns an allocated string which is the directory name of this BDS: It 228 * will be used to make relative filenames absolute by prepending this 229 * function's return value to them. 230 */ 231 char *(*bdrv_dirname)(BlockDriverState *bs, Error **errp); 232 233 /* aio */ 234 BlockAIOCB *(*bdrv_aio_preadv)(BlockDriverState *bs, 235 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags, 236 BlockCompletionFunc *cb, void *opaque); 237 BlockAIOCB *(*bdrv_aio_pwritev)(BlockDriverState *bs, 238 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags, 239 BlockCompletionFunc *cb, void *opaque); 240 BlockAIOCB *(*bdrv_aio_flush)(BlockDriverState *bs, 241 BlockCompletionFunc *cb, void *opaque); 242 BlockAIOCB *(*bdrv_aio_pdiscard)(BlockDriverState *bs, 243 int64_t offset, int bytes, 244 BlockCompletionFunc *cb, void *opaque); 245 246 int coroutine_fn (*bdrv_co_readv)(BlockDriverState *bs, 247 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov); 248 249 /** 250 * @offset: position in bytes to read at 251 * @bytes: number of bytes to read 252 * @qiov: the buffers to fill with read data 253 * @flags: currently unused, always 0 254 * 255 * @offset and @bytes will be a multiple of 'request_alignment', 256 * but the length of individual @qiov elements does not have to 257 * be a multiple. 258 * 259 * @bytes will always equal the total size of @qiov, and will be 260 * no larger than 'max_transfer'. 261 * 262 * The buffer in @qiov may point directly to guest memory. 263 */ 264 int coroutine_fn (*bdrv_co_preadv)(BlockDriverState *bs, 265 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags); 266 int coroutine_fn (*bdrv_co_preadv_part)(BlockDriverState *bs, 267 uint64_t offset, uint64_t bytes, 268 QEMUIOVector *qiov, size_t qiov_offset, int flags); 269 int coroutine_fn (*bdrv_co_writev)(BlockDriverState *bs, 270 int64_t sector_num, int nb_sectors, QEMUIOVector *qiov, int flags); 271 /** 272 * @offset: position in bytes to write at 273 * @bytes: number of bytes to write 274 * @qiov: the buffers containing data to write 275 * @flags: zero or more bits allowed by 'supported_write_flags' 276 * 277 * @offset and @bytes will be a multiple of 'request_alignment', 278 * but the length of individual @qiov elements does not have to 279 * be a multiple. 280 * 281 * @bytes will always equal the total size of @qiov, and will be 282 * no larger than 'max_transfer'. 283 * 284 * The buffer in @qiov may point directly to guest memory. 285 */ 286 int coroutine_fn (*bdrv_co_pwritev)(BlockDriverState *bs, 287 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, int flags); 288 int coroutine_fn (*bdrv_co_pwritev_part)(BlockDriverState *bs, 289 uint64_t offset, uint64_t bytes, 290 QEMUIOVector *qiov, size_t qiov_offset, int flags); 291 292 /* 293 * Efficiently zero a region of the disk image. Typically an image format 294 * would use a compact metadata representation to implement this. This 295 * function pointer may be NULL or return -ENOSUP and .bdrv_co_writev() 296 * will be called instead. 297 */ 298 int coroutine_fn (*bdrv_co_pwrite_zeroes)(BlockDriverState *bs, 299 int64_t offset, int bytes, BdrvRequestFlags flags); 300 int coroutine_fn (*bdrv_co_pdiscard)(BlockDriverState *bs, 301 int64_t offset, int bytes); 302 303 /* Map [offset, offset + nbytes) range onto a child of @bs to copy from, 304 * and invoke bdrv_co_copy_range_from(child, ...), or invoke 305 * bdrv_co_copy_range_to() if @bs is the leaf child to copy data from. 306 * 307 * See the comment of bdrv_co_copy_range for the parameter and return value 308 * semantics. 309 */ 310 int coroutine_fn (*bdrv_co_copy_range_from)(BlockDriverState *bs, 311 BdrvChild *src, 312 uint64_t offset, 313 BdrvChild *dst, 314 uint64_t dst_offset, 315 uint64_t bytes, 316 BdrvRequestFlags read_flags, 317 BdrvRequestFlags write_flags); 318 319 /* Map [offset, offset + nbytes) range onto a child of bs to copy data to, 320 * and invoke bdrv_co_copy_range_to(child, src, ...), or perform the copy 321 * operation if @bs is the leaf and @src has the same BlockDriver. Return 322 * -ENOTSUP if @bs is the leaf but @src has a different BlockDriver. 323 * 324 * See the comment of bdrv_co_copy_range for the parameter and return value 325 * semantics. 326 */ 327 int coroutine_fn (*bdrv_co_copy_range_to)(BlockDriverState *bs, 328 BdrvChild *src, 329 uint64_t src_offset, 330 BdrvChild *dst, 331 uint64_t dst_offset, 332 uint64_t bytes, 333 BdrvRequestFlags read_flags, 334 BdrvRequestFlags write_flags); 335 336 /* 337 * Building block for bdrv_block_status[_above] and 338 * bdrv_is_allocated[_above]. The driver should answer only 339 * according to the current layer, and should only need to set 340 * BDRV_BLOCK_DATA, BDRV_BLOCK_ZERO, BDRV_BLOCK_OFFSET_VALID, 341 * and/or BDRV_BLOCK_RAW; if the current layer defers to a backing 342 * layer, the result should be 0 (and not BDRV_BLOCK_ZERO). See 343 * block.h for the overall meaning of the bits. As a hint, the 344 * flag want_zero is true if the caller cares more about precise 345 * mappings (favor accurate _OFFSET_VALID/_ZERO) or false for 346 * overall allocation (favor larger *pnum, perhaps by reporting 347 * _DATA instead of _ZERO). The block layer guarantees input 348 * clamped to bdrv_getlength() and aligned to request_alignment, 349 * as well as non-NULL pnum, map, and file; in turn, the driver 350 * must return an error or set pnum to an aligned non-zero value. 351 * 352 * Note that @bytes is just a hint on how big of a region the 353 * caller wants to inspect. It is not a limit on *pnum. 354 * Implementations are free to return larger values of *pnum if 355 * doing so does not incur a performance penalty. 356 * 357 * block/io.c's bdrv_co_block_status() will utilize an unclamped 358 * *pnum value for the block-status cache on protocol nodes, prior 359 * to clamping *pnum for return to its caller. 360 */ 361 int coroutine_fn (*bdrv_co_block_status)(BlockDriverState *bs, 362 bool want_zero, int64_t offset, int64_t bytes, int64_t *pnum, 363 int64_t *map, BlockDriverState **file); 364 365 /* 366 * This informs the driver that we are no longer interested in the result 367 * of in-flight requests, so don't waste the time if possible. 368 * 369 * One example usage is to avoid waiting for an nbd target node reconnect 370 * timeout during job-cancel with force=true. 371 */ 372 void (*bdrv_cancel_in_flight)(BlockDriverState *bs); 373 374 /* 375 * Invalidate any cached meta-data. 376 */ 377 void coroutine_fn (*bdrv_co_invalidate_cache)(BlockDriverState *bs, 378 Error **errp); 379 int (*bdrv_inactivate)(BlockDriverState *bs); 380 381 /* 382 * Flushes all data for all layers by calling bdrv_co_flush for underlying 383 * layers, if needed. This function is needed for deterministic 384 * synchronization of the flush finishing callback. 385 */ 386 int coroutine_fn (*bdrv_co_flush)(BlockDriverState *bs); 387 388 /* Delete a created file. */ 389 int coroutine_fn (*bdrv_co_delete_file)(BlockDriverState *bs, 390 Error **errp); 391 392 /* 393 * Flushes all data that was already written to the OS all the way down to 394 * the disk (for example file-posix.c calls fsync()). 395 */ 396 int coroutine_fn (*bdrv_co_flush_to_disk)(BlockDriverState *bs); 397 398 /* 399 * Flushes all internal caches to the OS. The data may still sit in a 400 * writeback cache of the host OS, but it will survive a crash of the qemu 401 * process. 402 */ 403 int coroutine_fn (*bdrv_co_flush_to_os)(BlockDriverState *bs); 404 405 /* 406 * Drivers setting this field must be able to work with just a plain 407 * filename with '<protocol_name>:' as a prefix, and no other options. 408 * Options may be extracted from the filename by implementing 409 * bdrv_parse_filename. 410 */ 411 const char *protocol_name; 412 413 /* 414 * Truncate @bs to @offset bytes using the given @prealloc mode 415 * when growing. Modes other than PREALLOC_MODE_OFF should be 416 * rejected when shrinking @bs. 417 * 418 * If @exact is true, @bs must be resized to exactly @offset. 419 * Otherwise, it is sufficient for @bs (if it is a host block 420 * device and thus there is no way to resize it) to be at least 421 * @offset bytes in length. 422 * 423 * If @exact is true and this function fails but would succeed 424 * with @exact = false, it should return -ENOTSUP. 425 */ 426 int coroutine_fn (*bdrv_co_truncate)(BlockDriverState *bs, int64_t offset, 427 bool exact, PreallocMode prealloc, 428 BdrvRequestFlags flags, Error **errp); 429 430 int64_t (*bdrv_getlength)(BlockDriverState *bs); 431 bool has_variable_length; 432 int64_t (*bdrv_get_allocated_file_size)(BlockDriverState *bs); 433 BlockMeasureInfo *(*bdrv_measure)(QemuOpts *opts, BlockDriverState *in_bs, 434 Error **errp); 435 436 int coroutine_fn (*bdrv_co_pwritev_compressed)(BlockDriverState *bs, 437 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov); 438 int coroutine_fn (*bdrv_co_pwritev_compressed_part)(BlockDriverState *bs, 439 uint64_t offset, uint64_t bytes, QEMUIOVector *qiov, 440 size_t qiov_offset); 441 442 int (*bdrv_snapshot_create)(BlockDriverState *bs, 443 QEMUSnapshotInfo *sn_info); 444 int (*bdrv_snapshot_goto)(BlockDriverState *bs, 445 const char *snapshot_id); 446 int (*bdrv_snapshot_delete)(BlockDriverState *bs, 447 const char *snapshot_id, 448 const char *name, 449 Error **errp); 450 int (*bdrv_snapshot_list)(BlockDriverState *bs, 451 QEMUSnapshotInfo **psn_info); 452 int (*bdrv_snapshot_load_tmp)(BlockDriverState *bs, 453 const char *snapshot_id, 454 const char *name, 455 Error **errp); 456 int (*bdrv_get_info)(BlockDriverState *bs, BlockDriverInfo *bdi); 457 ImageInfoSpecific *(*bdrv_get_specific_info)(BlockDriverState *bs, 458 Error **errp); 459 BlockStatsSpecific *(*bdrv_get_specific_stats)(BlockDriverState *bs); 460 461 int coroutine_fn (*bdrv_save_vmstate)(BlockDriverState *bs, 462 QEMUIOVector *qiov, 463 int64_t pos); 464 int coroutine_fn (*bdrv_load_vmstate)(BlockDriverState *bs, 465 QEMUIOVector *qiov, 466 int64_t pos); 467 468 int (*bdrv_change_backing_file)(BlockDriverState *bs, 469 const char *backing_file, const char *backing_fmt); 470 471 /* removable device specific */ 472 bool (*bdrv_is_inserted)(BlockDriverState *bs); 473 void (*bdrv_eject)(BlockDriverState *bs, bool eject_flag); 474 void (*bdrv_lock_medium)(BlockDriverState *bs, bool locked); 475 476 /* to control generic scsi devices */ 477 BlockAIOCB *(*bdrv_aio_ioctl)(BlockDriverState *bs, 478 unsigned long int req, void *buf, 479 BlockCompletionFunc *cb, void *opaque); 480 int coroutine_fn (*bdrv_co_ioctl)(BlockDriverState *bs, 481 unsigned long int req, void *buf); 482 483 /* List of options for creating images, terminated by name == NULL */ 484 QemuOptsList *create_opts; 485 486 /* List of options for image amend */ 487 QemuOptsList *amend_opts; 488 489 /* 490 * If this driver supports reopening images this contains a 491 * NULL-terminated list of the runtime options that can be 492 * modified. If an option in this list is unspecified during 493 * reopen then it _must_ be reset to its default value or return 494 * an error. 495 */ 496 const char *const *mutable_opts; 497 498 /* 499 * Returns 0 for completed check, -errno for internal errors. 500 * The check results are stored in result. 501 */ 502 int coroutine_fn (*bdrv_co_check)(BlockDriverState *bs, 503 BdrvCheckResult *result, 504 BdrvCheckMode fix); 505 506 void (*bdrv_debug_event)(BlockDriverState *bs, BlkdebugEvent event); 507 508 /* TODO Better pass a option string/QDict/QemuOpts to add any rule? */ 509 int (*bdrv_debug_breakpoint)(BlockDriverState *bs, const char *event, 510 const char *tag); 511 int (*bdrv_debug_remove_breakpoint)(BlockDriverState *bs, 512 const char *tag); 513 int (*bdrv_debug_resume)(BlockDriverState *bs, const char *tag); 514 bool (*bdrv_debug_is_suspended)(BlockDriverState *bs, const char *tag); 515 516 void (*bdrv_refresh_limits)(BlockDriverState *bs, Error **errp); 517 518 /* 519 * Returns 1 if newly created images are guaranteed to contain only 520 * zeros, 0 otherwise. 521 */ 522 int (*bdrv_has_zero_init)(BlockDriverState *bs); 523 524 /* Remove fd handlers, timers, and other event loop callbacks so the event 525 * loop is no longer in use. Called with no in-flight requests and in 526 * depth-first traversal order with parents before child nodes. 527 */ 528 void (*bdrv_detach_aio_context)(BlockDriverState *bs); 529 530 /* Add fd handlers, timers, and other event loop callbacks so I/O requests 531 * can be processed again. Called with no in-flight requests and in 532 * depth-first traversal order with child nodes before parent nodes. 533 */ 534 void (*bdrv_attach_aio_context)(BlockDriverState *bs, 535 AioContext *new_context); 536 537 /* io queue for linux-aio */ 538 void (*bdrv_io_plug)(BlockDriverState *bs); 539 void (*bdrv_io_unplug)(BlockDriverState *bs); 540 541 /** 542 * Try to get @bs's logical and physical block size. 543 * On success, store them in @bsz and return zero. 544 * On failure, return negative errno. 545 */ 546 int (*bdrv_probe_blocksizes)(BlockDriverState *bs, BlockSizes *bsz); 547 /** 548 * Try to get @bs's geometry (cyls, heads, sectors) 549 * On success, store them in @geo and return 0. 550 * On failure return -errno. 551 * Only drivers that want to override guest geometry implement this 552 * callback; see hd_geometry_guess(). 553 */ 554 int (*bdrv_probe_geometry)(BlockDriverState *bs, HDGeometry *geo); 555 556 /** 557 * bdrv_co_drain_begin is called if implemented in the beginning of a 558 * drain operation to drain and stop any internal sources of requests in 559 * the driver. 560 * bdrv_co_drain_end is called if implemented at the end of the drain. 561 * 562 * They should be used by the driver to e.g. manage scheduled I/O 563 * requests, or toggle an internal state. After the end of the drain new 564 * requests will continue normally. 565 */ 566 void coroutine_fn (*bdrv_co_drain_begin)(BlockDriverState *bs); 567 void coroutine_fn (*bdrv_co_drain_end)(BlockDriverState *bs); 568 569 void (*bdrv_add_child)(BlockDriverState *parent, BlockDriverState *child, 570 Error **errp); 571 void (*bdrv_del_child)(BlockDriverState *parent, BdrvChild *child, 572 Error **errp); 573 574 /** 575 * Informs the block driver that a permission change is intended. The 576 * driver checks whether the change is permissible and may take other 577 * preparations for the change (e.g. get file system locks). This operation 578 * is always followed either by a call to either .bdrv_set_perm or 579 * .bdrv_abort_perm_update. 580 * 581 * Checks whether the requested set of cumulative permissions in @perm 582 * can be granted for accessing @bs and whether no other users are using 583 * permissions other than those given in @shared (both arguments take 584 * BLK_PERM_* bitmasks). 585 * 586 * If both conditions are met, 0 is returned. Otherwise, -errno is returned 587 * and errp is set to an error describing the conflict. 588 */ 589 int (*bdrv_check_perm)(BlockDriverState *bs, uint64_t perm, 590 uint64_t shared, Error **errp); 591 592 /** 593 * Called to inform the driver that the set of cumulative set of used 594 * permissions for @bs has changed to @perm, and the set of sharable 595 * permission to @shared. The driver can use this to propagate changes to 596 * its children (i.e. request permissions only if a parent actually needs 597 * them). 598 * 599 * This function is only invoked after bdrv_check_perm(), so block drivers 600 * may rely on preparations made in their .bdrv_check_perm implementation. 601 */ 602 void (*bdrv_set_perm)(BlockDriverState *bs, uint64_t perm, uint64_t shared); 603 604 /* 605 * Called to inform the driver that after a previous bdrv_check_perm() 606 * call, the permission update is not performed and any preparations made 607 * for it (e.g. taken file locks) need to be undone. 608 * 609 * This function can be called even for nodes that never saw a 610 * bdrv_check_perm() call. It is a no-op then. 611 */ 612 void (*bdrv_abort_perm_update)(BlockDriverState *bs); 613 614 /** 615 * Returns in @nperm and @nshared the permissions that the driver for @bs 616 * needs on its child @c, based on the cumulative permissions requested by 617 * the parents in @parent_perm and @parent_shared. 618 * 619 * If @c is NULL, return the permissions for attaching a new child for the 620 * given @child_class and @role. 621 * 622 * If @reopen_queue is non-NULL, don't return the currently needed 623 * permissions, but those that will be needed after applying the 624 * @reopen_queue. 625 */ 626 void (*bdrv_child_perm)(BlockDriverState *bs, BdrvChild *c, 627 BdrvChildRole role, 628 BlockReopenQueue *reopen_queue, 629 uint64_t parent_perm, uint64_t parent_shared, 630 uint64_t *nperm, uint64_t *nshared); 631 632 bool (*bdrv_supports_persistent_dirty_bitmap)(BlockDriverState *bs); 633 bool (*bdrv_co_can_store_new_dirty_bitmap)(BlockDriverState *bs, 634 const char *name, 635 uint32_t granularity, 636 Error **errp); 637 int (*bdrv_co_remove_persistent_dirty_bitmap)(BlockDriverState *bs, 638 const char *name, 639 Error **errp); 640 641 /** 642 * Register/unregister a buffer for I/O. For example, when the driver is 643 * interested to know the memory areas that will later be used in iovs, so 644 * that it can do IOMMU mapping with VFIO etc., in order to get better 645 * performance. In the case of VFIO drivers, this callback is used to do 646 * DMA mapping for hot buffers. 647 */ 648 void (*bdrv_register_buf)(BlockDriverState *bs, void *host, size_t size); 649 void (*bdrv_unregister_buf)(BlockDriverState *bs, void *host); 650 QLIST_ENTRY(BlockDriver) list; 651 652 /* Pointer to a NULL-terminated array of names of strong options 653 * that can be specified for bdrv_open(). A strong option is one 654 * that changes the data of a BDS. 655 * If this pointer is NULL, the array is considered empty. 656 * "filename" and "driver" are always considered strong. */ 657 const char *const *strong_runtime_opts; 658 }; 659 660 static inline bool block_driver_can_compress(BlockDriver *drv) 661 { 662 return drv->bdrv_co_pwritev_compressed || 663 drv->bdrv_co_pwritev_compressed_part; 664 } 665 666 typedef struct BlockLimits { 667 /* Alignment requirement, in bytes, for offset/length of I/O 668 * requests. Must be a power of 2 less than INT_MAX; defaults to 669 * 1 for drivers with modern byte interfaces, and to 512 670 * otherwise. */ 671 uint32_t request_alignment; 672 673 /* Maximum number of bytes that can be discarded at once (since it 674 * is signed, it must be < 2G, if set). Must be multiple of 675 * pdiscard_alignment, but need not be power of 2. May be 0 if no 676 * inherent 32-bit limit */ 677 int32_t max_pdiscard; 678 679 /* Optimal alignment for discard requests in bytes. A power of 2 680 * is best but not mandatory. Must be a multiple of 681 * bl.request_alignment, and must be less than max_pdiscard if 682 * that is set. May be 0 if bl.request_alignment is good enough */ 683 uint32_t pdiscard_alignment; 684 685 /* Maximum number of bytes that can zeroized at once (since it is 686 * signed, it must be < 2G, if set). Must be multiple of 687 * pwrite_zeroes_alignment. May be 0 if no inherent 32-bit limit */ 688 int32_t max_pwrite_zeroes; 689 690 /* Optimal alignment for write zeroes requests in bytes. A power 691 * of 2 is best but not mandatory. Must be a multiple of 692 * bl.request_alignment, and must be less than max_pwrite_zeroes 693 * if that is set. May be 0 if bl.request_alignment is good 694 * enough */ 695 uint32_t pwrite_zeroes_alignment; 696 697 /* Optimal transfer length in bytes. A power of 2 is best but not 698 * mandatory. Must be a multiple of bl.request_alignment, or 0 if 699 * no preferred size */ 700 uint32_t opt_transfer; 701 702 /* Maximal transfer length in bytes. Need not be power of 2, but 703 * must be multiple of opt_transfer and bl.request_alignment, or 0 704 * for no 32-bit limit. For now, anything larger than INT_MAX is 705 * clamped down. */ 706 uint32_t max_transfer; 707 708 /* Maximal hardware transfer length in bytes. Applies whenever 709 * transfers to the device bypass the kernel I/O scheduler, for 710 * example with SG_IO. If larger than max_transfer or if zero, 711 * blk_get_max_hw_transfer will fall back to max_transfer. 712 */ 713 uint64_t max_hw_transfer; 714 715 /* memory alignment, in bytes so that no bounce buffer is needed */ 716 size_t min_mem_alignment; 717 718 /* memory alignment, in bytes, for bounce buffer */ 719 size_t opt_mem_alignment; 720 721 /* maximum number of iovec elements */ 722 int max_iov; 723 } BlockLimits; 724 725 typedef struct BdrvOpBlocker BdrvOpBlocker; 726 727 typedef struct BdrvAioNotifier { 728 void (*attached_aio_context)(AioContext *new_context, void *opaque); 729 void (*detach_aio_context)(void *opaque); 730 731 void *opaque; 732 bool deleted; 733 734 QLIST_ENTRY(BdrvAioNotifier) list; 735 } BdrvAioNotifier; 736 737 struct BdrvChildClass { 738 /* If true, bdrv_replace_node() doesn't change the node this BdrvChild 739 * points to. */ 740 bool stay_at_node; 741 742 /* If true, the parent is a BlockDriverState and bdrv_next_all_states() 743 * will return it. This information is used for drain_all, where every node 744 * will be drained separately, so the drain only needs to be propagated to 745 * non-BDS parents. */ 746 bool parent_is_bds; 747 748 void (*inherit_options)(BdrvChildRole role, bool parent_is_format, 749 int *child_flags, QDict *child_options, 750 int parent_flags, QDict *parent_options); 751 752 void (*change_media)(BdrvChild *child, bool load); 753 void (*resize)(BdrvChild *child); 754 755 /* Returns a name that is supposedly more useful for human users than the 756 * node name for identifying the node in question (in particular, a BB 757 * name), or NULL if the parent can't provide a better name. */ 758 const char *(*get_name)(BdrvChild *child); 759 760 /* Returns a malloced string that describes the parent of the child for a 761 * human reader. This could be a node-name, BlockBackend name, qdev ID or 762 * QOM path of the device owning the BlockBackend, job type and ID etc. The 763 * caller is responsible for freeing the memory. */ 764 char *(*get_parent_desc)(BdrvChild *child); 765 766 /* 767 * If this pair of functions is implemented, the parent doesn't issue new 768 * requests after returning from .drained_begin() until .drained_end() is 769 * called. 770 * 771 * These functions must not change the graph (and therefore also must not 772 * call aio_poll(), which could change the graph indirectly). 773 * 774 * If drained_end() schedules background operations, it must atomically 775 * increment *drained_end_counter for each such operation and atomically 776 * decrement it once the operation has settled. 777 * 778 * Note that this can be nested. If drained_begin() was called twice, new 779 * I/O is allowed only after drained_end() was called twice, too. 780 */ 781 void (*drained_begin)(BdrvChild *child); 782 void (*drained_end)(BdrvChild *child, int *drained_end_counter); 783 784 /* 785 * Returns whether the parent has pending requests for the child. This 786 * callback is polled after .drained_begin() has been called until all 787 * activity on the child has stopped. 788 */ 789 bool (*drained_poll)(BdrvChild *child); 790 791 /* Notifies the parent that the child has been activated/inactivated (e.g. 792 * when migration is completing) and it can start/stop requesting 793 * permissions and doing I/O on it. */ 794 void (*activate)(BdrvChild *child, Error **errp); 795 int (*inactivate)(BdrvChild *child); 796 797 void (*attach)(BdrvChild *child); 798 void (*detach)(BdrvChild *child); 799 800 /* Notifies the parent that the filename of its child has changed (e.g. 801 * because the direct child was removed from the backing chain), so that it 802 * can update its reference. */ 803 int (*update_filename)(BdrvChild *child, BlockDriverState *new_base, 804 const char *filename, Error **errp); 805 806 bool (*can_set_aio_ctx)(BdrvChild *child, AioContext *ctx, 807 GSList **ignore, Error **errp); 808 void (*set_aio_ctx)(BdrvChild *child, AioContext *ctx, GSList **ignore); 809 810 AioContext *(*get_parent_aio_context)(BdrvChild *child); 811 }; 812 813 extern const BdrvChildClass child_of_bds; 814 815 struct BdrvChild { 816 BlockDriverState *bs; 817 char *name; 818 const BdrvChildClass *klass; 819 BdrvChildRole role; 820 void *opaque; 821 822 /** 823 * Granted permissions for operating on this BdrvChild (BLK_PERM_* bitmask) 824 */ 825 uint64_t perm; 826 827 /** 828 * Permissions that can still be granted to other users of @bs while this 829 * BdrvChild is still attached to it. (BLK_PERM_* bitmask) 830 */ 831 uint64_t shared_perm; 832 833 /* 834 * This link is frozen: the child can neither be replaced nor 835 * detached from the parent. 836 */ 837 bool frozen; 838 839 /* 840 * How many times the parent of this child has been drained 841 * (through klass->drained_*). 842 * Usually, this is equal to bs->quiesce_counter (potentially 843 * reduced by bdrv_drain_all_count). It may differ while the 844 * child is entering or leaving a drained section. 845 */ 846 int parent_quiesce_counter; 847 848 QLIST_ENTRY(BdrvChild) next; 849 QLIST_ENTRY(BdrvChild) next_parent; 850 }; 851 852 /* 853 * Allows bdrv_co_block_status() to cache one data region for a 854 * protocol node. 855 * 856 * @valid: Whether the cache is valid (should be accessed with atomic 857 * functions so this can be reset by RCU readers) 858 * @data_start: Offset where we know (or strongly assume) is data 859 * @data_end: Offset where the data region ends (which is not necessarily 860 * the start of a zeroed region) 861 */ 862 typedef struct BdrvBlockStatusCache { 863 struct rcu_head rcu; 864 865 bool valid; 866 int64_t data_start; 867 int64_t data_end; 868 } BdrvBlockStatusCache; 869 870 struct BlockDriverState { 871 /* Protected by big QEMU lock or read-only after opening. No special 872 * locking needed during I/O... 873 */ 874 int open_flags; /* flags used to open the file, re-used for re-open */ 875 bool encrypted; /* if true, the media is encrypted */ 876 bool sg; /* if true, the device is a /dev/sg* */ 877 bool probed; /* if true, format was probed rather than specified */ 878 bool force_share; /* if true, always allow all shared permissions */ 879 bool implicit; /* if true, this filter node was automatically inserted */ 880 881 BlockDriver *drv; /* NULL means no media */ 882 void *opaque; 883 884 AioContext *aio_context; /* event loop used for fd handlers, timers, etc */ 885 /* long-running tasks intended to always use the same AioContext as this 886 * BDS may register themselves in this list to be notified of changes 887 * regarding this BDS's context */ 888 QLIST_HEAD(, BdrvAioNotifier) aio_notifiers; 889 bool walking_aio_notifiers; /* to make removal during iteration safe */ 890 891 char filename[PATH_MAX]; 892 /* 893 * If not empty, this image is a diff in relation to backing_file. 894 * Note that this is the name given in the image header and 895 * therefore may or may not be equal to .backing->bs->filename. 896 * If this field contains a relative path, it is to be resolved 897 * relatively to the overlay's location. 898 */ 899 char backing_file[PATH_MAX]; 900 /* 901 * The backing filename indicated by the image header. Contrary 902 * to backing_file, if we ever open this file, auto_backing_file 903 * is replaced by the resulting BDS's filename (i.e. after a 904 * bdrv_refresh_filename() run). 905 */ 906 char auto_backing_file[PATH_MAX]; 907 char backing_format[16]; /* if non-zero and backing_file exists */ 908 909 QDict *full_open_options; 910 char exact_filename[PATH_MAX]; 911 912 BdrvChild *backing; 913 BdrvChild *file; 914 915 /* I/O Limits */ 916 BlockLimits bl; 917 918 /* 919 * Flags honored during pread 920 */ 921 unsigned int supported_read_flags; 922 /* Flags honored during pwrite (so far: BDRV_REQ_FUA, 923 * BDRV_REQ_WRITE_UNCHANGED). 924 * If a driver does not support BDRV_REQ_WRITE_UNCHANGED, those 925 * writes will be issued as normal writes without the flag set. 926 * This is important to note for drivers that do not explicitly 927 * request a WRITE permission for their children and instead take 928 * the same permissions as their parent did (this is commonly what 929 * block filters do). Such drivers have to be aware that the 930 * parent may have taken a WRITE_UNCHANGED permission only and is 931 * issuing such requests. Drivers either must make sure that 932 * these requests do not result in plain WRITE accesses (usually 933 * by supporting BDRV_REQ_WRITE_UNCHANGED, and then forwarding 934 * every incoming write request as-is, including potentially that 935 * flag), or they have to explicitly take the WRITE permission for 936 * their children. */ 937 unsigned int supported_write_flags; 938 /* Flags honored during pwrite_zeroes (so far: BDRV_REQ_FUA, 939 * BDRV_REQ_MAY_UNMAP, BDRV_REQ_WRITE_UNCHANGED) */ 940 unsigned int supported_zero_flags; 941 /* 942 * Flags honoured during truncate (so far: BDRV_REQ_ZERO_WRITE). 943 * 944 * If BDRV_REQ_ZERO_WRITE is given, the truncate operation must make sure 945 * that any added space reads as all zeros. If this can't be guaranteed, 946 * the operation must fail. 947 */ 948 unsigned int supported_truncate_flags; 949 950 /* the following member gives a name to every node on the bs graph. */ 951 char node_name[32]; 952 /* element of the list of named nodes building the graph */ 953 QTAILQ_ENTRY(BlockDriverState) node_list; 954 /* element of the list of all BlockDriverStates (all_bdrv_states) */ 955 QTAILQ_ENTRY(BlockDriverState) bs_list; 956 /* element of the list of monitor-owned BDS */ 957 QTAILQ_ENTRY(BlockDriverState) monitor_list; 958 int refcnt; 959 960 /* operation blockers */ 961 QLIST_HEAD(, BdrvOpBlocker) op_blockers[BLOCK_OP_TYPE_MAX]; 962 963 /* The node that this node inherited default options from (and a reopen on 964 * which can affect this node by changing these defaults). This is always a 965 * parent node of this node. */ 966 BlockDriverState *inherits_from; 967 QLIST_HEAD(, BdrvChild) children; 968 QLIST_HEAD(, BdrvChild) parents; 969 970 QDict *options; 971 QDict *explicit_options; 972 BlockdevDetectZeroesOptions detect_zeroes; 973 974 /* The error object in use for blocking operations on backing_hd */ 975 Error *backing_blocker; 976 977 /* Protected by AioContext lock */ 978 979 /* If we are reading a disk image, give its size in sectors. 980 * Generally read-only; it is written to by load_snapshot and 981 * save_snaphost, but the block layer is quiescent during those. 982 */ 983 int64_t total_sectors; 984 985 /* threshold limit for writes, in bytes. "High water mark". */ 986 uint64_t write_threshold_offset; 987 988 /* Writing to the list requires the BQL _and_ the dirty_bitmap_mutex. 989 * Reading from the list can be done with either the BQL or the 990 * dirty_bitmap_mutex. Modifying a bitmap only requires 991 * dirty_bitmap_mutex. */ 992 QemuMutex dirty_bitmap_mutex; 993 QLIST_HEAD(, BdrvDirtyBitmap) dirty_bitmaps; 994 995 /* Offset after the highest byte written to */ 996 Stat64 wr_highest_offset; 997 998 /* If true, copy read backing sectors into image. Can be >1 if more 999 * than one client has requested copy-on-read. Accessed with atomic 1000 * ops. 1001 */ 1002 int copy_on_read; 1003 1004 /* number of in-flight requests; overall and serialising. 1005 * Accessed with atomic ops. 1006 */ 1007 unsigned int in_flight; 1008 unsigned int serialising_in_flight; 1009 1010 /* counter for nested bdrv_io_plug. 1011 * Accessed with atomic ops. 1012 */ 1013 unsigned io_plugged; 1014 1015 /* do we need to tell the quest if we have a volatile write cache? */ 1016 int enable_write_cache; 1017 1018 /* Accessed with atomic ops. */ 1019 int quiesce_counter; 1020 int recursive_quiesce_counter; 1021 1022 unsigned int write_gen; /* Current data generation */ 1023 1024 /* Protected by reqs_lock. */ 1025 CoMutex reqs_lock; 1026 QLIST_HEAD(, BdrvTrackedRequest) tracked_requests; 1027 CoQueue flush_queue; /* Serializing flush queue */ 1028 bool active_flush_req; /* Flush request in flight? */ 1029 1030 /* Only read/written by whoever has set active_flush_req to true. */ 1031 unsigned int flushed_gen; /* Flushed write generation */ 1032 1033 /* BdrvChild links to this node may never be frozen */ 1034 bool never_freeze; 1035 1036 /* Lock for block-status cache RCU writers */ 1037 CoMutex bsc_modify_lock; 1038 /* Always non-NULL, but must only be dereferenced under an RCU read guard */ 1039 BdrvBlockStatusCache *block_status_cache; 1040 }; 1041 1042 struct BlockBackendRootState { 1043 int open_flags; 1044 BlockdevDetectZeroesOptions detect_zeroes; 1045 }; 1046 1047 typedef enum BlockMirrorBackingMode { 1048 /* Reuse the existing backing chain from the source for the target. 1049 * - sync=full: Set backing BDS to NULL. 1050 * - sync=top: Use source's backing BDS. 1051 * - sync=none: Use source as the backing BDS. */ 1052 MIRROR_SOURCE_BACKING_CHAIN, 1053 1054 /* Open the target's backing chain completely anew */ 1055 MIRROR_OPEN_BACKING_CHAIN, 1056 1057 /* Do not change the target's backing BDS after job completion */ 1058 MIRROR_LEAVE_BACKING_CHAIN, 1059 } BlockMirrorBackingMode; 1060 1061 1062 /* Essential block drivers which must always be statically linked into qemu, and 1063 * which therefore can be accessed without using bdrv_find_format() */ 1064 extern BlockDriver bdrv_file; 1065 extern BlockDriver bdrv_raw; 1066 extern BlockDriver bdrv_qcow2; 1067 1068 int coroutine_fn bdrv_co_preadv(BdrvChild *child, 1069 int64_t offset, int64_t bytes, QEMUIOVector *qiov, 1070 BdrvRequestFlags flags); 1071 int coroutine_fn bdrv_co_preadv_part(BdrvChild *child, 1072 int64_t offset, int64_t bytes, 1073 QEMUIOVector *qiov, size_t qiov_offset, BdrvRequestFlags flags); 1074 int coroutine_fn bdrv_co_pwritev(BdrvChild *child, 1075 int64_t offset, int64_t bytes, QEMUIOVector *qiov, 1076 BdrvRequestFlags flags); 1077 int coroutine_fn bdrv_co_pwritev_part(BdrvChild *child, 1078 int64_t offset, int64_t bytes, 1079 QEMUIOVector *qiov, size_t qiov_offset, BdrvRequestFlags flags); 1080 1081 static inline int coroutine_fn bdrv_co_pread(BdrvChild *child, 1082 int64_t offset, unsigned int bytes, void *buf, BdrvRequestFlags flags) 1083 { 1084 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes); 1085 1086 return bdrv_co_preadv(child, offset, bytes, &qiov, flags); 1087 } 1088 1089 static inline int coroutine_fn bdrv_co_pwrite(BdrvChild *child, 1090 int64_t offset, unsigned int bytes, void *buf, BdrvRequestFlags flags) 1091 { 1092 QEMUIOVector qiov = QEMU_IOVEC_INIT_BUF(qiov, buf, bytes); 1093 1094 return bdrv_co_pwritev(child, offset, bytes, &qiov, flags); 1095 } 1096 1097 extern unsigned int bdrv_drain_all_count; 1098 void bdrv_apply_subtree_drain(BdrvChild *child, BlockDriverState *new_parent); 1099 void bdrv_unapply_subtree_drain(BdrvChild *child, BlockDriverState *old_parent); 1100 1101 bool coroutine_fn bdrv_make_request_serialising(BdrvTrackedRequest *req, 1102 uint64_t align); 1103 BdrvTrackedRequest *coroutine_fn bdrv_co_get_self_request(BlockDriverState *bs); 1104 1105 int get_tmp_filename(char *filename, int size); 1106 BlockDriver *bdrv_probe_all(const uint8_t *buf, int buf_size, 1107 const char *filename); 1108 1109 void bdrv_parse_filename_strip_prefix(const char *filename, const char *prefix, 1110 QDict *options); 1111 1112 bool bdrv_backing_overridden(BlockDriverState *bs); 1113 1114 1115 /** 1116 * bdrv_add_aio_context_notifier: 1117 * 1118 * If a long-running job intends to be always run in the same AioContext as a 1119 * certain BDS, it may use this function to be notified of changes regarding the 1120 * association of the BDS to an AioContext. 1121 * 1122 * attached_aio_context() is called after the target BDS has been attached to a 1123 * new AioContext; detach_aio_context() is called before the target BDS is being 1124 * detached from its old AioContext. 1125 */ 1126 void bdrv_add_aio_context_notifier(BlockDriverState *bs, 1127 void (*attached_aio_context)(AioContext *new_context, void *opaque), 1128 void (*detach_aio_context)(void *opaque), void *opaque); 1129 1130 /** 1131 * bdrv_remove_aio_context_notifier: 1132 * 1133 * Unsubscribe of change notifications regarding the BDS's AioContext. The 1134 * parameters given here have to be the same as those given to 1135 * bdrv_add_aio_context_notifier(). 1136 */ 1137 void bdrv_remove_aio_context_notifier(BlockDriverState *bs, 1138 void (*aio_context_attached)(AioContext *, 1139 void *), 1140 void (*aio_context_detached)(void *), 1141 void *opaque); 1142 1143 /** 1144 * bdrv_wakeup: 1145 * @bs: The BlockDriverState for which an I/O operation has been completed. 1146 * 1147 * Wake up the main thread if it is waiting on BDRV_POLL_WHILE. During 1148 * synchronous I/O on a BlockDriverState that is attached to another 1149 * I/O thread, the main thread lets the I/O thread's event loop run, 1150 * waiting for the I/O operation to complete. A bdrv_wakeup will wake 1151 * up the main thread if necessary. 1152 * 1153 * Manual calls to bdrv_wakeup are rarely necessary, because 1154 * bdrv_dec_in_flight already calls it. 1155 */ 1156 void bdrv_wakeup(BlockDriverState *bs); 1157 1158 #ifdef _WIN32 1159 int is_windows_drive(const char *filename); 1160 #endif 1161 1162 /** 1163 * stream_start: 1164 * @job_id: The id of the newly-created job, or %NULL to use the 1165 * device name of @bs. 1166 * @bs: Block device to operate on. 1167 * @base: Block device that will become the new base, or %NULL to 1168 * flatten the whole backing file chain onto @bs. 1169 * @backing_file_str: The file name that will be written to @bs as the 1170 * the new backing file if the job completes. Ignored if @base is %NULL. 1171 * @creation_flags: Flags that control the behavior of the Job lifetime. 1172 * See @BlockJobCreateFlags 1173 * @speed: The maximum speed, in bytes per second, or 0 for unlimited. 1174 * @on_error: The action to take upon error. 1175 * @filter_node_name: The node name that should be assigned to the filter 1176 * driver that the stream job inserts into the graph above 1177 * @bs. NULL means that a node name should be autogenerated. 1178 * @errp: Error object. 1179 * 1180 * Start a streaming operation on @bs. Clusters that are unallocated 1181 * in @bs, but allocated in any image between @base and @bs (both 1182 * exclusive) will be written to @bs. At the end of a successful 1183 * streaming job, the backing file of @bs will be changed to 1184 * @backing_file_str in the written image and to @base in the live 1185 * BlockDriverState. 1186 */ 1187 void stream_start(const char *job_id, BlockDriverState *bs, 1188 BlockDriverState *base, const char *backing_file_str, 1189 BlockDriverState *bottom, 1190 int creation_flags, int64_t speed, 1191 BlockdevOnError on_error, 1192 const char *filter_node_name, 1193 Error **errp); 1194 1195 /** 1196 * commit_start: 1197 * @job_id: The id of the newly-created job, or %NULL to use the 1198 * device name of @bs. 1199 * @bs: Active block device. 1200 * @top: Top block device to be committed. 1201 * @base: Block device that will be written into, and become the new top. 1202 * @creation_flags: Flags that control the behavior of the Job lifetime. 1203 * See @BlockJobCreateFlags 1204 * @speed: The maximum speed, in bytes per second, or 0 for unlimited. 1205 * @on_error: The action to take upon error. 1206 * @backing_file_str: String to use as the backing file in @top's overlay 1207 * @filter_node_name: The node name that should be assigned to the filter 1208 * driver that the commit job inserts into the graph above @top. NULL means 1209 * that a node name should be autogenerated. 1210 * @errp: Error object. 1211 * 1212 */ 1213 void commit_start(const char *job_id, BlockDriverState *bs, 1214 BlockDriverState *base, BlockDriverState *top, 1215 int creation_flags, int64_t speed, 1216 BlockdevOnError on_error, const char *backing_file_str, 1217 const char *filter_node_name, Error **errp); 1218 /** 1219 * commit_active_start: 1220 * @job_id: The id of the newly-created job, or %NULL to use the 1221 * device name of @bs. 1222 * @bs: Active block device to be committed. 1223 * @base: Block device that will be written into, and become the new top. 1224 * @creation_flags: Flags that control the behavior of the Job lifetime. 1225 * See @BlockJobCreateFlags 1226 * @speed: The maximum speed, in bytes per second, or 0 for unlimited. 1227 * @on_error: The action to take upon error. 1228 * @filter_node_name: The node name that should be assigned to the filter 1229 * driver that the commit job inserts into the graph above @bs. NULL means that 1230 * a node name should be autogenerated. 1231 * @cb: Completion function for the job. 1232 * @opaque: Opaque pointer value passed to @cb. 1233 * @auto_complete: Auto complete the job. 1234 * @errp: Error object. 1235 * 1236 */ 1237 BlockJob *commit_active_start(const char *job_id, BlockDriverState *bs, 1238 BlockDriverState *base, int creation_flags, 1239 int64_t speed, BlockdevOnError on_error, 1240 const char *filter_node_name, 1241 BlockCompletionFunc *cb, void *opaque, 1242 bool auto_complete, Error **errp); 1243 /* 1244 * mirror_start: 1245 * @job_id: The id of the newly-created job, or %NULL to use the 1246 * device name of @bs. 1247 * @bs: Block device to operate on. 1248 * @target: Block device to write to. 1249 * @replaces: Block graph node name to replace once the mirror is done. Can 1250 * only be used when full mirroring is selected. 1251 * @creation_flags: Flags that control the behavior of the Job lifetime. 1252 * See @BlockJobCreateFlags 1253 * @speed: The maximum speed, in bytes per second, or 0 for unlimited. 1254 * @granularity: The chosen granularity for the dirty bitmap. 1255 * @buf_size: The amount of data that can be in flight at one time. 1256 * @mode: Whether to collapse all images in the chain to the target. 1257 * @backing_mode: How to establish the target's backing chain after completion. 1258 * @zero_target: Whether the target should be explicitly zero-initialized 1259 * @on_source_error: The action to take upon error reading from the source. 1260 * @on_target_error: The action to take upon error writing to the target. 1261 * @unmap: Whether to unmap target where source sectors only contain zeroes. 1262 * @filter_node_name: The node name that should be assigned to the filter 1263 * driver that the mirror job inserts into the graph above @bs. NULL means that 1264 * a node name should be autogenerated. 1265 * @copy_mode: When to trigger writes to the target. 1266 * @errp: Error object. 1267 * 1268 * Start a mirroring operation on @bs. Clusters that are allocated 1269 * in @bs will be written to @target until the job is cancelled or 1270 * manually completed. At the end of a successful mirroring job, 1271 * @bs will be switched to read from @target. 1272 */ 1273 void mirror_start(const char *job_id, BlockDriverState *bs, 1274 BlockDriverState *target, const char *replaces, 1275 int creation_flags, int64_t speed, 1276 uint32_t granularity, int64_t buf_size, 1277 MirrorSyncMode mode, BlockMirrorBackingMode backing_mode, 1278 bool zero_target, 1279 BlockdevOnError on_source_error, 1280 BlockdevOnError on_target_error, 1281 bool unmap, const char *filter_node_name, 1282 MirrorCopyMode copy_mode, Error **errp); 1283 1284 /* 1285 * backup_job_create: 1286 * @job_id: The id of the newly-created job, or %NULL to use the 1287 * device name of @bs. 1288 * @bs: Block device to operate on. 1289 * @target: Block device to write to. 1290 * @speed: The maximum speed, in bytes per second, or 0 for unlimited. 1291 * @sync_mode: What parts of the disk image should be copied to the destination. 1292 * @sync_bitmap: The dirty bitmap if sync_mode is 'bitmap' or 'incremental' 1293 * @bitmap_mode: The bitmap synchronization policy to use. 1294 * @perf: Performance options. All actual fields assumed to be present, 1295 * all ".has_*" fields are ignored. 1296 * @on_source_error: The action to take upon error reading from the source. 1297 * @on_target_error: The action to take upon error writing to the target. 1298 * @creation_flags: Flags that control the behavior of the Job lifetime. 1299 * See @BlockJobCreateFlags 1300 * @cb: Completion function for the job. 1301 * @opaque: Opaque pointer value passed to @cb. 1302 * @txn: Transaction that this job is part of (may be NULL). 1303 * 1304 * Create a backup operation on @bs. Clusters in @bs are written to @target 1305 * until the job is cancelled or manually completed. 1306 */ 1307 BlockJob *backup_job_create(const char *job_id, BlockDriverState *bs, 1308 BlockDriverState *target, int64_t speed, 1309 MirrorSyncMode sync_mode, 1310 BdrvDirtyBitmap *sync_bitmap, 1311 BitmapSyncMode bitmap_mode, 1312 bool compress, 1313 const char *filter_node_name, 1314 BackupPerf *perf, 1315 BlockdevOnError on_source_error, 1316 BlockdevOnError on_target_error, 1317 int creation_flags, 1318 BlockCompletionFunc *cb, void *opaque, 1319 JobTxn *txn, Error **errp); 1320 1321 BdrvChild *bdrv_root_attach_child(BlockDriverState *child_bs, 1322 const char *child_name, 1323 const BdrvChildClass *child_class, 1324 BdrvChildRole child_role, 1325 uint64_t perm, uint64_t shared_perm, 1326 void *opaque, Error **errp); 1327 void bdrv_root_unref_child(BdrvChild *child); 1328 1329 void bdrv_get_cumulative_perm(BlockDriverState *bs, uint64_t *perm, 1330 uint64_t *shared_perm); 1331 1332 /** 1333 * Sets a BdrvChild's permissions. Avoid if the parent is a BDS; use 1334 * bdrv_child_refresh_perms() instead and make the parent's 1335 * .bdrv_child_perm() implementation return the correct values. 1336 */ 1337 int bdrv_child_try_set_perm(BdrvChild *c, uint64_t perm, uint64_t shared, 1338 Error **errp); 1339 1340 /** 1341 * Calls bs->drv->bdrv_child_perm() and updates the child's permission 1342 * masks with the result. 1343 * Drivers should invoke this function whenever an event occurs that 1344 * makes their .bdrv_child_perm() implementation return different 1345 * values than before, but which will not result in the block layer 1346 * automatically refreshing the permissions. 1347 */ 1348 int bdrv_child_refresh_perms(BlockDriverState *bs, BdrvChild *c, Error **errp); 1349 1350 bool bdrv_recurse_can_replace(BlockDriverState *bs, 1351 BlockDriverState *to_replace); 1352 1353 /* 1354 * Default implementation for BlockDriver.bdrv_child_perm() that can 1355 * be used by block filters and image formats, as long as they use the 1356 * child_of_bds child class and set an appropriate BdrvChildRole. 1357 */ 1358 void bdrv_default_perms(BlockDriverState *bs, BdrvChild *c, 1359 BdrvChildRole role, BlockReopenQueue *reopen_queue, 1360 uint64_t perm, uint64_t shared, 1361 uint64_t *nperm, uint64_t *nshared); 1362 1363 const char *bdrv_get_parent_name(const BlockDriverState *bs); 1364 void blk_dev_change_media_cb(BlockBackend *blk, bool load, Error **errp); 1365 bool blk_dev_has_removable_media(BlockBackend *blk); 1366 bool blk_dev_has_tray(BlockBackend *blk); 1367 void blk_dev_eject_request(BlockBackend *blk, bool force); 1368 bool blk_dev_is_tray_open(BlockBackend *blk); 1369 bool blk_dev_is_medium_locked(BlockBackend *blk); 1370 1371 void bdrv_set_dirty(BlockDriverState *bs, int64_t offset, int64_t bytes); 1372 1373 void bdrv_clear_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap **out); 1374 void bdrv_restore_dirty_bitmap(BdrvDirtyBitmap *bitmap, HBitmap *backup); 1375 bool bdrv_dirty_bitmap_merge_internal(BdrvDirtyBitmap *dest, 1376 const BdrvDirtyBitmap *src, 1377 HBitmap **backup, bool lock); 1378 1379 void bdrv_inc_in_flight(BlockDriverState *bs); 1380 void bdrv_dec_in_flight(BlockDriverState *bs); 1381 1382 void blockdev_close_all_bdrv_states(void); 1383 1384 int coroutine_fn bdrv_co_copy_range_from(BdrvChild *src, int64_t src_offset, 1385 BdrvChild *dst, int64_t dst_offset, 1386 int64_t bytes, 1387 BdrvRequestFlags read_flags, 1388 BdrvRequestFlags write_flags); 1389 int coroutine_fn bdrv_co_copy_range_to(BdrvChild *src, int64_t src_offset, 1390 BdrvChild *dst, int64_t dst_offset, 1391 int64_t bytes, 1392 BdrvRequestFlags read_flags, 1393 BdrvRequestFlags write_flags); 1394 1395 int refresh_total_sectors(BlockDriverState *bs, int64_t hint); 1396 1397 void bdrv_set_monitor_owned(BlockDriverState *bs); 1398 BlockDriverState *bds_tree_init(QDict *bs_opts, Error **errp); 1399 1400 /** 1401 * Simple implementation of bdrv_co_create_opts for protocol drivers 1402 * which only support creation via opening a file 1403 * (usually existing raw storage device) 1404 */ 1405 int coroutine_fn bdrv_co_create_opts_simple(BlockDriver *drv, 1406 const char *filename, 1407 QemuOpts *opts, 1408 Error **errp); 1409 extern QemuOptsList bdrv_create_opts_simple; 1410 1411 BdrvDirtyBitmap *block_dirty_bitmap_lookup(const char *node, 1412 const char *name, 1413 BlockDriverState **pbs, 1414 Error **errp); 1415 BdrvDirtyBitmap *block_dirty_bitmap_merge(const char *node, const char *target, 1416 BlockDirtyBitmapMergeSourceList *bms, 1417 HBitmap **backup, Error **errp); 1418 BdrvDirtyBitmap *block_dirty_bitmap_remove(const char *node, const char *name, 1419 bool release, 1420 BlockDriverState **bitmap_bs, 1421 Error **errp); 1422 1423 BdrvChild *bdrv_cow_child(BlockDriverState *bs); 1424 BdrvChild *bdrv_filter_child(BlockDriverState *bs); 1425 BdrvChild *bdrv_filter_or_cow_child(BlockDriverState *bs); 1426 BdrvChild *bdrv_primary_child(BlockDriverState *bs); 1427 BlockDriverState *bdrv_skip_implicit_filters(BlockDriverState *bs); 1428 BlockDriverState *bdrv_skip_filters(BlockDriverState *bs); 1429 BlockDriverState *bdrv_backing_chain_next(BlockDriverState *bs); 1430 1431 static inline BlockDriverState *child_bs(BdrvChild *child) 1432 { 1433 return child ? child->bs : NULL; 1434 } 1435 1436 static inline BlockDriverState *bdrv_cow_bs(BlockDriverState *bs) 1437 { 1438 return child_bs(bdrv_cow_child(bs)); 1439 } 1440 1441 static inline BlockDriverState *bdrv_filter_bs(BlockDriverState *bs) 1442 { 1443 return child_bs(bdrv_filter_child(bs)); 1444 } 1445 1446 static inline BlockDriverState *bdrv_filter_or_cow_bs(BlockDriverState *bs) 1447 { 1448 return child_bs(bdrv_filter_or_cow_child(bs)); 1449 } 1450 1451 static inline BlockDriverState *bdrv_primary_bs(BlockDriverState *bs) 1452 { 1453 return child_bs(bdrv_primary_child(bs)); 1454 } 1455 1456 /** 1457 * End all quiescent sections started by bdrv_drain_all_begin(). This is 1458 * needed when deleting a BDS before bdrv_drain_all_end() is called. 1459 * 1460 * NOTE: this is an internal helper for bdrv_close() *only*. No one else 1461 * should call it. 1462 */ 1463 void bdrv_drain_all_end_quiesce(BlockDriverState *bs); 1464 1465 /** 1466 * Check whether the given offset is in the cached block-status data 1467 * region. 1468 * 1469 * If it is, and @pnum is not NULL, *pnum is set to 1470 * `bsc.data_end - offset`, i.e. how many bytes, starting from 1471 * @offset, are data (according to the cache). 1472 * Otherwise, *pnum is not touched. 1473 */ 1474 bool bdrv_bsc_is_data(BlockDriverState *bs, int64_t offset, int64_t *pnum); 1475 1476 /** 1477 * If [offset, offset + bytes) overlaps with the currently cached 1478 * block-status region, invalidate the cache. 1479 * 1480 * (To be used by I/O paths that cause data regions to be zero or 1481 * holes.) 1482 */ 1483 void bdrv_bsc_invalidate_range(BlockDriverState *bs, 1484 int64_t offset, int64_t bytes); 1485 1486 /** 1487 * Mark the range [offset, offset + bytes) as a data region. 1488 */ 1489 void bdrv_bsc_fill(BlockDriverState *bs, int64_t offset, int64_t bytes); 1490 1491 #endif /* BLOCK_INT_H */ 1492