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