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_COMMON_H 25 #define BLOCK_COMMON_H 26 27 #include "block/aio.h" 28 #include "block/aio-wait.h" 29 #include "qemu/iov.h" 30 #include "qemu/coroutine.h" 31 #include "block/accounting.h" 32 #include "qemu/hbitmap.h" 33 #include "qemu/transactions.h" 34 35 /* 36 * co_wrapper{*}: Function specifiers used by block-coroutine-wrapper.py 37 * 38 * Function specifiers, which do nothing but mark functions to be 39 * generated by scripts/block-coroutine-wrapper.py 40 * 41 * Usage: read docs/devel/block-coroutine-wrapper.rst 42 * 43 * There are 2 kind of specifiers: 44 * - co_wrapper functions can be called by only non-coroutine context, because 45 * they always generate a new coroutine. 46 * - co_wrapper_mixed functions can be called by both coroutine and 47 * non-coroutine context. 48 */ 49 #define co_wrapper 50 #define co_wrapper_mixed 51 52 #include "block/dirty-bitmap.h" 53 #include "block/blockjob.h" 54 55 /* block.c */ 56 typedef struct BlockDriver BlockDriver; 57 typedef struct BdrvChild BdrvChild; 58 typedef struct BdrvChildClass BdrvChildClass; 59 60 typedef struct BlockDriverInfo { 61 /* in bytes, 0 if irrelevant */ 62 int cluster_size; 63 /* offset at which the VM state can be saved (0 if not possible) */ 64 int64_t vm_state_offset; 65 bool is_dirty; 66 /* 67 * True if this block driver only supports compressed writes 68 */ 69 bool needs_compressed_writes; 70 } BlockDriverInfo; 71 72 typedef struct BlockFragInfo { 73 uint64_t allocated_clusters; 74 uint64_t total_clusters; 75 uint64_t fragmented_clusters; 76 uint64_t compressed_clusters; 77 } BlockFragInfo; 78 79 typedef enum { 80 BDRV_REQ_COPY_ON_READ = 0x1, 81 BDRV_REQ_ZERO_WRITE = 0x2, 82 83 /* 84 * The BDRV_REQ_MAY_UNMAP flag is used in write_zeroes requests to indicate 85 * that the block driver should unmap (discard) blocks if it is guaranteed 86 * that the result will read back as zeroes. The flag is only passed to the 87 * driver if the block device is opened with BDRV_O_UNMAP. 88 */ 89 BDRV_REQ_MAY_UNMAP = 0x4, 90 91 /* 92 * An optimization hint when all QEMUIOVector elements are within 93 * previously registered bdrv_register_buf() memory ranges. 94 * 95 * Code that replaces the user's QEMUIOVector elements with bounce buffers 96 * must take care to clear this flag. 97 */ 98 BDRV_REQ_REGISTERED_BUF = 0x8, 99 100 BDRV_REQ_FUA = 0x10, 101 BDRV_REQ_WRITE_COMPRESSED = 0x20, 102 103 /* 104 * Signifies that this write request will not change the visible disk 105 * content. 106 */ 107 BDRV_REQ_WRITE_UNCHANGED = 0x40, 108 109 /* 110 * Forces request serialisation. Use only with write requests. 111 */ 112 BDRV_REQ_SERIALISING = 0x80, 113 114 /* 115 * Execute the request only if the operation can be offloaded or otherwise 116 * be executed efficiently, but return an error instead of using a slow 117 * fallback. 118 */ 119 BDRV_REQ_NO_FALLBACK = 0x100, 120 121 /* 122 * BDRV_REQ_PREFETCH makes sense only in the context of copy-on-read 123 * (i.e., together with the BDRV_REQ_COPY_ON_READ flag or when a COR 124 * filter is involved), in which case it signals that the COR operation 125 * need not read the data into memory (qiov) but only ensure they are 126 * copied to the top layer (i.e., that COR operation is done). 127 */ 128 BDRV_REQ_PREFETCH = 0x200, 129 130 /* 131 * If we need to wait for other requests, just fail immediately. Used 132 * only together with BDRV_REQ_SERIALISING. Used only with requests aligned 133 * to request_alignment (corresponding assertions are in block/io.c). 134 */ 135 BDRV_REQ_NO_WAIT = 0x400, 136 137 /* Mask of valid flags */ 138 BDRV_REQ_MASK = 0x7ff, 139 } BdrvRequestFlags; 140 141 #define BDRV_O_NO_SHARE 0x0001 /* don't share permissions */ 142 #define BDRV_O_RDWR 0x0002 143 #define BDRV_O_RESIZE 0x0004 /* request permission for resizing the node */ 144 #define BDRV_O_SNAPSHOT 0x0008 /* open the file read only and save 145 writes in a snapshot */ 146 #define BDRV_O_TEMPORARY 0x0010 /* delete the file after use */ 147 #define BDRV_O_NOCACHE 0x0020 /* do not use the host page cache */ 148 #define BDRV_O_NATIVE_AIO 0x0080 /* use native AIO instead of the 149 thread pool */ 150 #define BDRV_O_NO_BACKING 0x0100 /* don't open the backing file */ 151 #define BDRV_O_NO_FLUSH 0x0200 /* disable flushing on this disk */ 152 #define BDRV_O_COPY_ON_READ 0x0400 /* copy read backing sectors into image */ 153 #define BDRV_O_INACTIVE 0x0800 /* consistency hint for migration handoff */ 154 #define BDRV_O_CHECK 0x1000 /* open solely for consistency check */ 155 #define BDRV_O_ALLOW_RDWR 0x2000 /* allow reopen to change from r/o to r/w */ 156 #define BDRV_O_UNMAP 0x4000 /* execute guest UNMAP/TRIM operations */ 157 #define BDRV_O_PROTOCOL 0x8000 /* if no block driver is explicitly given: 158 select an appropriate protocol driver, 159 ignoring the format layer */ 160 #define BDRV_O_NO_IO 0x10000 /* don't initialize for I/O */ 161 #define BDRV_O_AUTO_RDONLY 0x20000 /* degrade to read-only if opening 162 read-write fails */ 163 #define BDRV_O_IO_URING 0x40000 /* use io_uring instead of the thread pool */ 164 165 #define BDRV_O_CACHE_MASK (BDRV_O_NOCACHE | BDRV_O_NO_FLUSH) 166 167 168 /* Option names of options parsed by the block layer */ 169 170 #define BDRV_OPT_CACHE_WB "cache.writeback" 171 #define BDRV_OPT_CACHE_DIRECT "cache.direct" 172 #define BDRV_OPT_CACHE_NO_FLUSH "cache.no-flush" 173 #define BDRV_OPT_READ_ONLY "read-only" 174 #define BDRV_OPT_AUTO_READ_ONLY "auto-read-only" 175 #define BDRV_OPT_DISCARD "discard" 176 #define BDRV_OPT_FORCE_SHARE "force-share" 177 178 179 #define BDRV_SECTOR_BITS 9 180 #define BDRV_SECTOR_SIZE (1ULL << BDRV_SECTOR_BITS) 181 182 #define BDRV_REQUEST_MAX_SECTORS MIN_CONST(SIZE_MAX >> BDRV_SECTOR_BITS, \ 183 INT_MAX >> BDRV_SECTOR_BITS) 184 #define BDRV_REQUEST_MAX_BYTES (BDRV_REQUEST_MAX_SECTORS << BDRV_SECTOR_BITS) 185 186 /* 187 * We want allow aligning requests and disk length up to any 32bit alignment 188 * and don't afraid of overflow. 189 * To achieve it, and in the same time use some pretty number as maximum disk 190 * size, let's define maximum "length" (a limit for any offset/bytes request and 191 * for disk size) to be the greatest power of 2 less than INT64_MAX. 192 */ 193 #define BDRV_MAX_ALIGNMENT (1L << 30) 194 #define BDRV_MAX_LENGTH (QEMU_ALIGN_DOWN(INT64_MAX, BDRV_MAX_ALIGNMENT)) 195 196 /* 197 * Allocation status flags for bdrv_block_status() and friends. 198 * 199 * Public flags: 200 * BDRV_BLOCK_DATA: allocation for data at offset is tied to this layer 201 * BDRV_BLOCK_ZERO: offset reads as zero 202 * BDRV_BLOCK_OFFSET_VALID: an associated offset exists for accessing raw data 203 * BDRV_BLOCK_ALLOCATED: the content of the block is determined by this 204 * layer rather than any backing, set by block layer 205 * BDRV_BLOCK_EOF: the returned pnum covers through end of file for this 206 * layer, set by block layer 207 * 208 * Internal flags: 209 * BDRV_BLOCK_RAW: for use by passthrough drivers, such as raw, to request 210 * that the block layer recompute the answer from the returned 211 * BDS; must be accompanied by just BDRV_BLOCK_OFFSET_VALID. 212 * BDRV_BLOCK_RECURSE: request that the block layer will recursively search for 213 * zeroes in file child of current block node inside 214 * returned region. Only valid together with both 215 * BDRV_BLOCK_DATA and BDRV_BLOCK_OFFSET_VALID. Should not 216 * appear with BDRV_BLOCK_ZERO. 217 * 218 * If BDRV_BLOCK_OFFSET_VALID is set, the map parameter represents the 219 * host offset within the returned BDS that is allocated for the 220 * corresponding raw guest data. However, whether that offset 221 * actually contains data also depends on BDRV_BLOCK_DATA, as follows: 222 * 223 * DATA ZERO OFFSET_VALID 224 * t t t sectors read as zero, returned file is zero at offset 225 * t f t sectors read as valid from file at offset 226 * f t t sectors preallocated, read as zero, returned file not 227 * necessarily zero at offset 228 * f f t sectors preallocated but read from backing_hd, 229 * returned file contains garbage at offset 230 * t t f sectors preallocated, read as zero, unknown offset 231 * t f f sectors read from unknown file or offset 232 * f t f not allocated or unknown offset, read as zero 233 * f f f not allocated or unknown offset, read from backing_hd 234 */ 235 #define BDRV_BLOCK_DATA 0x01 236 #define BDRV_BLOCK_ZERO 0x02 237 #define BDRV_BLOCK_OFFSET_VALID 0x04 238 #define BDRV_BLOCK_RAW 0x08 239 #define BDRV_BLOCK_ALLOCATED 0x10 240 #define BDRV_BLOCK_EOF 0x20 241 #define BDRV_BLOCK_RECURSE 0x40 242 243 typedef QTAILQ_HEAD(BlockReopenQueue, BlockReopenQueueEntry) BlockReopenQueue; 244 245 typedef struct BDRVReopenState { 246 BlockDriverState *bs; 247 int flags; 248 BlockdevDetectZeroesOptions detect_zeroes; 249 bool backing_missing; 250 BlockDriverState *old_backing_bs; /* keep pointer for permissions update */ 251 BlockDriverState *old_file_bs; /* keep pointer for permissions update */ 252 QDict *options; 253 QDict *explicit_options; 254 void *opaque; 255 } BDRVReopenState; 256 257 /* 258 * Block operation types 259 */ 260 typedef enum BlockOpType { 261 BLOCK_OP_TYPE_BACKUP_SOURCE, 262 BLOCK_OP_TYPE_BACKUP_TARGET, 263 BLOCK_OP_TYPE_CHANGE, 264 BLOCK_OP_TYPE_COMMIT_SOURCE, 265 BLOCK_OP_TYPE_COMMIT_TARGET, 266 BLOCK_OP_TYPE_DATAPLANE, 267 BLOCK_OP_TYPE_DRIVE_DEL, 268 BLOCK_OP_TYPE_EJECT, 269 BLOCK_OP_TYPE_EXTERNAL_SNAPSHOT, 270 BLOCK_OP_TYPE_INTERNAL_SNAPSHOT, 271 BLOCK_OP_TYPE_INTERNAL_SNAPSHOT_DELETE, 272 BLOCK_OP_TYPE_MIRROR_SOURCE, 273 BLOCK_OP_TYPE_MIRROR_TARGET, 274 BLOCK_OP_TYPE_RESIZE, 275 BLOCK_OP_TYPE_STREAM, 276 BLOCK_OP_TYPE_REPLACE, 277 BLOCK_OP_TYPE_MAX, 278 } BlockOpType; 279 280 /* Block node permission constants */ 281 enum { 282 /** 283 * A user that has the "permission" of consistent reads is guaranteed that 284 * their view of the contents of the block device is complete and 285 * self-consistent, representing the contents of a disk at a specific 286 * point. 287 * 288 * For most block devices (including their backing files) this is true, but 289 * the property cannot be maintained in a few situations like for 290 * intermediate nodes of a commit block job. 291 */ 292 BLK_PERM_CONSISTENT_READ = 0x01, 293 294 /** This permission is required to change the visible disk contents. */ 295 BLK_PERM_WRITE = 0x02, 296 297 /** 298 * This permission (which is weaker than BLK_PERM_WRITE) is both enough and 299 * required for writes to the block node when the caller promises that 300 * the visible disk content doesn't change. 301 * 302 * As the BLK_PERM_WRITE permission is strictly stronger, either is 303 * sufficient to perform an unchanging write. 304 */ 305 BLK_PERM_WRITE_UNCHANGED = 0x04, 306 307 /** This permission is required to change the size of a block node. */ 308 BLK_PERM_RESIZE = 0x08, 309 310 /** 311 * There was a now-removed bit BLK_PERM_GRAPH_MOD, with value of 0x10. QEMU 312 * 6.1 and earlier may still lock the corresponding byte in block/file-posix 313 * locking. So, implementing some new permission should be very careful to 314 * not interfere with this old unused thing. 315 */ 316 317 BLK_PERM_ALL = 0x0f, 318 319 DEFAULT_PERM_PASSTHROUGH = BLK_PERM_CONSISTENT_READ 320 | BLK_PERM_WRITE 321 | BLK_PERM_WRITE_UNCHANGED 322 | BLK_PERM_RESIZE, 323 324 DEFAULT_PERM_UNCHANGED = BLK_PERM_ALL & ~DEFAULT_PERM_PASSTHROUGH, 325 }; 326 327 /* 328 * Flags that parent nodes assign to child nodes to specify what kind of 329 * role(s) they take. 330 * 331 * At least one of DATA, METADATA, FILTERED, or COW must be set for 332 * every child. 333 * 334 * 335 * = Connection with bs->children, bs->file and bs->backing fields = 336 * 337 * 1. Filters 338 * 339 * Filter drivers have drv->is_filter = true. 340 * 341 * Filter node has exactly one FILTERED|PRIMARY child, and may have other 342 * children which must not have these bits (one example is the 343 * copy-before-write filter, which also has its target DATA child). 344 * 345 * Filter nodes never have COW children. 346 * 347 * For most filters, the filtered child is linked in bs->file, bs->backing is 348 * NULL. For some filters (as an exception), it is the other way around; those 349 * drivers will have drv->filtered_child_is_backing set to true (see that 350 * field’s documentation for what drivers this concerns) 351 * 352 * 2. "raw" driver (block/raw-format.c) 353 * 354 * Formally it's not a filter (drv->is_filter = false) 355 * 356 * bs->backing is always NULL 357 * 358 * Only has one child, linked in bs->file. Its role is either FILTERED|PRIMARY 359 * (like filter) or DATA|PRIMARY depending on options. 360 * 361 * 3. Other drivers 362 * 363 * Don't have any FILTERED children. 364 * 365 * May have at most one COW child. In this case it's linked in bs->backing. 366 * Otherwise bs->backing is NULL. COW child is never PRIMARY. 367 * 368 * May have at most one PRIMARY child. In this case it's linked in bs->file. 369 * Otherwise bs->file is NULL. 370 * 371 * May also have some other children that don't have the PRIMARY or COW bit set. 372 */ 373 enum BdrvChildRoleBits { 374 /* 375 * This child stores data. 376 * Any node may have an arbitrary number of such children. 377 */ 378 BDRV_CHILD_DATA = (1 << 0), 379 380 /* 381 * This child stores metadata. 382 * Any node may have an arbitrary number of metadata-storing 383 * children. 384 */ 385 BDRV_CHILD_METADATA = (1 << 1), 386 387 /* 388 * A child that always presents exactly the same visible data as 389 * the parent, e.g. by virtue of the parent forwarding all reads 390 * and writes. 391 * This flag is mutually exclusive with DATA, METADATA, and COW. 392 * Any node may have at most one filtered child at a time. 393 */ 394 BDRV_CHILD_FILTERED = (1 << 2), 395 396 /* 397 * Child from which to read all data that isn't allocated in the 398 * parent (i.e., the backing child); such data is copied to the 399 * parent through COW (and optionally COR). 400 * This field is mutually exclusive with DATA, METADATA, and 401 * FILTERED. 402 * Any node may have at most one such backing child at a time. 403 */ 404 BDRV_CHILD_COW = (1 << 3), 405 406 /* 407 * The primary child. For most drivers, this is the child whose 408 * filename applies best to the parent node. 409 * Any node may have at most one primary child at a time. 410 */ 411 BDRV_CHILD_PRIMARY = (1 << 4), 412 413 /* Useful combination of flags */ 414 BDRV_CHILD_IMAGE = BDRV_CHILD_DATA 415 | BDRV_CHILD_METADATA 416 | BDRV_CHILD_PRIMARY, 417 }; 418 419 /* Mask of BdrvChildRoleBits values */ 420 typedef unsigned int BdrvChildRole; 421 422 typedef struct BdrvCheckResult { 423 int corruptions; 424 int leaks; 425 int check_errors; 426 int corruptions_fixed; 427 int leaks_fixed; 428 int64_t image_end_offset; 429 BlockFragInfo bfi; 430 } BdrvCheckResult; 431 432 typedef enum { 433 BDRV_FIX_LEAKS = 1, 434 BDRV_FIX_ERRORS = 2, 435 } BdrvCheckMode; 436 437 typedef struct BlockSizes { 438 uint32_t phys; 439 uint32_t log; 440 } BlockSizes; 441 442 typedef struct HDGeometry { 443 uint32_t heads; 444 uint32_t sectors; 445 uint32_t cylinders; 446 } HDGeometry; 447 448 /* 449 * Common functions that are neither I/O nor Global State. 450 * 451 * These functions must never call any function from other categories 452 * (I/O, "I/O or GS", Global State) except this one, but can be invoked by 453 * all of them. 454 */ 455 456 char *bdrv_perm_names(uint64_t perm); 457 uint64_t bdrv_qapi_perm_to_blk_perm(BlockPermission qapi_perm); 458 459 void bdrv_init_with_whitelist(void); 460 bool bdrv_uses_whitelist(void); 461 int bdrv_is_whitelisted(BlockDriver *drv, bool read_only); 462 463 int bdrv_parse_aio(const char *mode, int *flags); 464 int bdrv_parse_cache_mode(const char *mode, int *flags, bool *writethrough); 465 int bdrv_parse_discard_flags(const char *mode, int *flags); 466 467 int path_has_protocol(const char *path); 468 int path_is_absolute(const char *path); 469 char *path_combine(const char *base_path, const char *filename); 470 471 char *bdrv_get_full_backing_filename_from_filename(const char *backed, 472 const char *backing, 473 Error **errp); 474 475 #endif /* BLOCK_COMMON_H */ 476