1 /* 2 * Write logging blk driver based on blkverify and blkdebug. 3 * 4 * Copyright (c) 2017 Tuomas Tynkkynen <tuomas@tuxera.com> 5 * Copyright (c) 2018 Aapo Vienamo <aapo@tuxera.com> 6 * Copyright (c) 2018-2024 Ari Sundholm <ari@tuxera.com> 7 * 8 * This work is licensed under the terms of the GNU GPL, version 2 or later. 9 * See the COPYING file in the top-level directory. 10 */ 11 12 #include "qemu/osdep.h" 13 #include "qapi/error.h" 14 #include "qemu/sockets.h" /* for EINPROGRESS on Windows */ 15 #include "block/block-io.h" 16 #include "block/block_int.h" 17 #include "qobject/qdict.h" 18 #include "qobject/qstring.h" 19 #include "qemu/cutils.h" 20 #include "qemu/module.h" 21 #include "qemu/option.h" 22 23 /* Disk format stuff - taken from Linux drivers/md/dm-log-writes.c */ 24 25 #define LOG_FLUSH_FLAG (1 << 0) 26 #define LOG_FUA_FLAG (1 << 1) 27 #define LOG_DISCARD_FLAG (1 << 2) 28 #define LOG_MARK_FLAG (1 << 3) 29 #define LOG_FLAG_MASK (LOG_FLUSH_FLAG \ 30 | LOG_FUA_FLAG \ 31 | LOG_DISCARD_FLAG \ 32 | LOG_MARK_FLAG) 33 34 #define WRITE_LOG_VERSION 1ULL 35 #define WRITE_LOG_MAGIC 0x6a736677736872ULL 36 37 /* All fields are little-endian. */ 38 struct log_write_super { 39 uint64_t magic; 40 uint64_t version; 41 uint64_t nr_entries; 42 uint32_t sectorsize; 43 } QEMU_PACKED; 44 45 struct log_write_entry { 46 uint64_t sector; 47 uint64_t nr_sectors; 48 uint64_t flags; 49 uint64_t data_len; 50 } QEMU_PACKED; 51 52 /* End of disk format structures. */ 53 54 typedef struct { 55 BdrvChild *log_file; 56 uint32_t sectorsize; 57 uint32_t sectorbits; 58 uint64_t update_interval; 59 60 /* 61 * The mutable state of the driver, consisting of the current log sector 62 * and the number of log entries. 63 * 64 * May be read and/or written from multiple threads, and the mutex must be 65 * held when accessing these fields. 66 */ 67 uint64_t cur_log_sector; 68 uint64_t nr_entries; 69 QemuMutex mutex; 70 71 /* 72 * The super block sequence number. Non-zero if a super block update is in 73 * progress. 74 * 75 * The mutex must be held when accessing this field. 76 */ 77 uint64_t super_update_seq; 78 79 /* 80 * A coroutine-aware queue to serialize super block updates. 81 * 82 * Used with the mutex to ensure that only one thread be updating the super 83 * block at a time. 84 */ 85 CoQueue super_update_queue; 86 } BDRVBlkLogWritesState; 87 88 static QemuOptsList runtime_opts = { 89 .name = "blklogwrites", 90 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head), 91 .desc = { 92 { 93 .name = "log-append", 94 .type = QEMU_OPT_BOOL, 95 .help = "Append to an existing log", 96 }, 97 { 98 .name = "log-sector-size", 99 .type = QEMU_OPT_SIZE, 100 .help = "Log sector size", 101 }, 102 { 103 .name = "log-super-update-interval", 104 .type = QEMU_OPT_NUMBER, 105 .help = "Log superblock update interval (# of write requests)", 106 }, 107 { /* end of list */ } 108 }, 109 }; 110 111 static inline uint32_t blk_log_writes_log2(uint32_t value) 112 { 113 assert(value > 0); 114 return 31 - clz32(value); 115 } 116 117 static inline bool blk_log_writes_sector_size_valid(uint32_t sector_size) 118 { 119 return is_power_of_2(sector_size) && 120 sector_size >= sizeof(struct log_write_super) && 121 sector_size >= sizeof(struct log_write_entry) && 122 sector_size < (1ull << 24); 123 } 124 125 static uint64_t blk_log_writes_find_cur_log_sector(BdrvChild *log, 126 uint32_t sector_size, 127 uint64_t nr_entries, 128 Error **errp) 129 { 130 uint64_t cur_sector = 1; 131 uint64_t cur_idx = 0; 132 uint32_t sector_bits = blk_log_writes_log2(sector_size); 133 struct log_write_entry cur_entry; 134 135 while (cur_idx < nr_entries) { 136 int read_ret = bdrv_pread(log, cur_sector << sector_bits, 137 sizeof(cur_entry), &cur_entry, 0); 138 if (read_ret < 0) { 139 error_setg_errno(errp, -read_ret, 140 "Failed to read log entry %"PRIu64, cur_idx); 141 return (uint64_t)-1ull; 142 } 143 144 if (cur_entry.flags & ~cpu_to_le64(LOG_FLAG_MASK)) { 145 error_setg(errp, "Invalid flags 0x%"PRIx64" in log entry %"PRIu64, 146 le64_to_cpu(cur_entry.flags), cur_idx); 147 return (uint64_t)-1ull; 148 } 149 150 /* Account for the sector of the entry itself */ 151 ++cur_sector; 152 153 /* 154 * Account for the data of the write. 155 * For discards, this data is not present. 156 */ 157 if (!(cur_entry.flags & cpu_to_le64(LOG_DISCARD_FLAG))) { 158 cur_sector += le64_to_cpu(cur_entry.nr_sectors); 159 } 160 161 ++cur_idx; 162 } 163 164 return cur_sector; 165 } 166 167 static int blk_log_writes_open(BlockDriverState *bs, QDict *options, int flags, 168 Error **errp) 169 { 170 BDRVBlkLogWritesState *s = bs->opaque; 171 QemuOpts *opts; 172 Error *local_err = NULL; 173 int ret; 174 uint64_t log_sector_size; 175 bool log_append; 176 177 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort); 178 if (!qemu_opts_absorb_qdict(opts, options, errp)) { 179 ret = -EINVAL; 180 goto fail; 181 } 182 183 /* Open the file */ 184 ret = bdrv_open_file_child(NULL, options, "file", bs, errp); 185 if (ret < 0) { 186 goto fail; 187 } 188 189 /* Open the log file */ 190 s->log_file = bdrv_open_child(NULL, options, "log", bs, &child_of_bds, 191 BDRV_CHILD_METADATA, false, errp); 192 if (!s->log_file) { 193 ret = -EINVAL; 194 goto fail; 195 } 196 197 qemu_mutex_init(&s->mutex); 198 qemu_co_queue_init(&s->super_update_queue); 199 200 log_append = qemu_opt_get_bool(opts, "log-append", false); 201 202 if (log_append) { 203 struct log_write_super log_sb = { 0, 0, 0, 0 }; 204 205 if (qemu_opt_find(opts, "log-sector-size")) { 206 ret = -EINVAL; 207 error_setg(errp, "log-append and log-sector-size are mutually " 208 "exclusive"); 209 goto fail_log; 210 } 211 212 /* Read log superblock or fake one for an empty log */ 213 if (!bdrv_getlength(s->log_file->bs)) { 214 log_sb.magic = cpu_to_le64(WRITE_LOG_MAGIC); 215 log_sb.version = cpu_to_le64(WRITE_LOG_VERSION); 216 log_sb.nr_entries = cpu_to_le64(0); 217 log_sb.sectorsize = cpu_to_le32(BDRV_SECTOR_SIZE); 218 } else { 219 ret = bdrv_pread(s->log_file, 0, sizeof(log_sb), &log_sb, 0); 220 if (ret < 0) { 221 error_setg_errno(errp, -ret, "Could not read log superblock"); 222 goto fail_log; 223 } 224 } 225 226 if (log_sb.magic != cpu_to_le64(WRITE_LOG_MAGIC)) { 227 ret = -EINVAL; 228 error_setg(errp, "Invalid log superblock magic"); 229 goto fail_log; 230 } 231 232 if (log_sb.version != cpu_to_le64(WRITE_LOG_VERSION)) { 233 ret = -EINVAL; 234 error_setg(errp, "Unsupported log version %"PRIu64, 235 le64_to_cpu(log_sb.version)); 236 goto fail_log; 237 } 238 239 log_sector_size = le32_to_cpu(log_sb.sectorsize); 240 s->cur_log_sector = 1; 241 s->nr_entries = 0; 242 243 if (blk_log_writes_sector_size_valid(log_sector_size)) { 244 s->cur_log_sector = 245 blk_log_writes_find_cur_log_sector(s->log_file, log_sector_size, 246 le64_to_cpu(log_sb.nr_entries), &local_err); 247 if (local_err) { 248 ret = -EINVAL; 249 error_propagate(errp, local_err); 250 goto fail_log; 251 } 252 253 s->nr_entries = le64_to_cpu(log_sb.nr_entries); 254 } 255 } else { 256 log_sector_size = qemu_opt_get_size(opts, "log-sector-size", 257 BDRV_SECTOR_SIZE); 258 s->cur_log_sector = 1; 259 s->nr_entries = 0; 260 } 261 262 s->super_update_seq = 0; 263 264 if (!blk_log_writes_sector_size_valid(log_sector_size)) { 265 ret = -EINVAL; 266 error_setg(errp, "Invalid log sector size %"PRIu64, log_sector_size); 267 goto fail_log; 268 } 269 270 s->sectorsize = log_sector_size; 271 s->sectorbits = blk_log_writes_log2(log_sector_size); 272 s->update_interval = qemu_opt_get_number(opts, "log-super-update-interval", 273 4096); 274 if (!s->update_interval) { 275 ret = -EINVAL; 276 error_setg(errp, "Invalid log superblock update interval %"PRIu64, 277 s->update_interval); 278 goto fail_log; 279 } 280 281 ret = 0; 282 fail_log: 283 if (ret < 0) { 284 bdrv_drain_all_begin(); 285 bdrv_graph_wrlock(); 286 bdrv_unref_child(bs, s->log_file); 287 bdrv_graph_wrunlock(); 288 bdrv_drain_all_end(); 289 s->log_file = NULL; 290 qemu_mutex_destroy(&s->mutex); 291 } 292 fail: 293 qemu_opts_del(opts); 294 return ret; 295 } 296 297 static void blk_log_writes_close(BlockDriverState *bs) 298 { 299 BDRVBlkLogWritesState *s = bs->opaque; 300 301 bdrv_drain_all_begin(); 302 bdrv_graph_wrlock(); 303 bdrv_unref_child(bs, s->log_file); 304 s->log_file = NULL; 305 bdrv_graph_wrunlock(); 306 bdrv_drain_all_end(); 307 qemu_mutex_destroy(&s->mutex); 308 } 309 310 static int64_t coroutine_fn GRAPH_RDLOCK 311 blk_log_writes_co_getlength(BlockDriverState *bs) 312 { 313 return bdrv_co_getlength(bs->file->bs); 314 } 315 316 static void blk_log_writes_child_perm(BlockDriverState *bs, BdrvChild *c, 317 BdrvChildRole role, 318 BlockReopenQueue *ro_q, 319 uint64_t perm, uint64_t shrd, 320 uint64_t *nperm, uint64_t *nshrd) 321 { 322 if (!c) { 323 *nperm = perm & DEFAULT_PERM_PASSTHROUGH; 324 *nshrd = (shrd & DEFAULT_PERM_PASSTHROUGH) | DEFAULT_PERM_UNCHANGED; 325 return; 326 } 327 328 bdrv_default_perms(bs, c, role, ro_q, perm, shrd, 329 nperm, nshrd); 330 } 331 332 static void blk_log_writes_refresh_limits(BlockDriverState *bs, Error **errp) 333 { 334 const BDRVBlkLogWritesState *s = bs->opaque; 335 bs->bl.request_alignment = s->sectorsize; 336 } 337 338 static int coroutine_fn GRAPH_RDLOCK 339 blk_log_writes_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes, 340 QEMUIOVector *qiov, BdrvRequestFlags flags) 341 { 342 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags); 343 } 344 345 typedef struct BlkLogWritesFileReq { 346 BlockDriverState *bs; 347 uint64_t offset; 348 uint64_t bytes; 349 int file_flags; 350 QEMUIOVector *qiov; 351 int GRAPH_RDLOCK_PTR (*func)(struct BlkLogWritesFileReq *r); 352 int file_ret; 353 } BlkLogWritesFileReq; 354 355 typedef struct { 356 BlockDriverState *bs; 357 QEMUIOVector *qiov; 358 struct log_write_entry entry; 359 uint64_t zero_size; 360 int log_ret; 361 } BlkLogWritesLogReq; 362 363 static void coroutine_fn GRAPH_RDLOCK 364 blk_log_writes_co_do_log(BlkLogWritesLogReq *lr) 365 { 366 BDRVBlkLogWritesState *s = lr->bs->opaque; 367 368 /* 369 * Determine the offsets and sizes of different parts of the entry, and 370 * update the state of the driver. 371 * 372 * This needs to be done in one go, before any actual I/O is done, as the 373 * log entry may have to be written in two parts, and the state of the 374 * driver may be modified by other driver operations while waiting for the 375 * I/O to complete. 376 */ 377 qemu_mutex_lock(&s->mutex); 378 const uint64_t entry_start_sector = s->cur_log_sector; 379 const uint64_t entry_offset = entry_start_sector << s->sectorbits; 380 const uint64_t qiov_aligned_size = ROUND_UP(lr->qiov->size, s->sectorsize); 381 const uint64_t entry_aligned_size = qiov_aligned_size + 382 ROUND_UP(lr->zero_size, s->sectorsize); 383 const uint64_t entry_nr_sectors = entry_aligned_size >> s->sectorbits; 384 const uint64_t entry_seq = s->nr_entries + 1; 385 386 s->nr_entries = entry_seq; 387 s->cur_log_sector += entry_nr_sectors; 388 qemu_mutex_unlock(&s->mutex); 389 390 /* 391 * Write the log entry. Note that if this is a "write zeroes" operation, 392 * only the entry header is written here, with the zeroing being done 393 * separately below. 394 */ 395 lr->log_ret = bdrv_co_pwritev(s->log_file, entry_offset, lr->qiov->size, 396 lr->qiov, 0); 397 398 /* Logging for the "write zeroes" operation */ 399 if (lr->log_ret == 0 && lr->zero_size) { 400 const uint64_t zeroes_offset = entry_offset + qiov_aligned_size; 401 402 lr->log_ret = bdrv_co_pwrite_zeroes(s->log_file, zeroes_offset, 403 lr->zero_size, 0); 404 } 405 406 /* Update super block on flush or every update interval */ 407 if (lr->log_ret == 0 && ((lr->entry.flags & LOG_FLUSH_FLAG) 408 || (entry_seq % s->update_interval == 0))) 409 { 410 struct log_write_super super = { 411 .magic = cpu_to_le64(WRITE_LOG_MAGIC), 412 .version = cpu_to_le64(WRITE_LOG_VERSION), 413 .nr_entries = 0, /* updated below */ 414 .sectorsize = cpu_to_le32(s->sectorsize), 415 }; 416 void *zeroes; 417 QEMUIOVector qiov; 418 419 /* 420 * Wait if a super block update is already in progress. 421 * Bail out if a newer update got its turn before us. 422 */ 423 WITH_QEMU_LOCK_GUARD(&s->mutex) { 424 CoQueueWaitFlags wait_flags = 0; 425 while (s->super_update_seq) { 426 if (entry_seq < s->super_update_seq) { 427 return; 428 } 429 qemu_co_queue_wait_flags(&s->super_update_queue, 430 &s->mutex, wait_flags); 431 432 /* 433 * In case the wait condition remains true after wakeup, 434 * to avoid starvation, make sure that this request is 435 * scheduled to rerun next by pushing it to the front of the 436 * queue. 437 */ 438 wait_flags = CO_QUEUE_WAIT_FRONT; 439 } 440 s->super_update_seq = entry_seq; 441 super.nr_entries = cpu_to_le64(s->nr_entries); 442 } 443 444 zeroes = g_malloc0(s->sectorsize - sizeof(super)); 445 446 qemu_iovec_init(&qiov, 2); 447 qemu_iovec_add(&qiov, &super, sizeof(super)); 448 qemu_iovec_add(&qiov, zeroes, s->sectorsize - sizeof(super)); 449 450 lr->log_ret = 451 bdrv_co_pwritev(s->log_file, 0, s->sectorsize, &qiov, 0); 452 if (lr->log_ret == 0) { 453 lr->log_ret = bdrv_co_flush(s->log_file->bs); 454 } 455 456 /* The super block has been updated. Let another request have a go. */ 457 qemu_mutex_lock(&s->mutex); 458 s->super_update_seq = 0; 459 (void) qemu_co_queue_next(&s->super_update_queue); 460 qemu_mutex_unlock(&s->mutex); 461 462 qemu_iovec_destroy(&qiov); 463 g_free(zeroes); 464 } 465 } 466 467 static void coroutine_fn GRAPH_RDLOCK 468 blk_log_writes_co_do_file(BlkLogWritesFileReq *fr) 469 { 470 fr->file_ret = fr->func(fr); 471 } 472 473 static int coroutine_fn GRAPH_RDLOCK 474 blk_log_writes_co_log(BlockDriverState *bs, uint64_t offset, uint64_t bytes, 475 QEMUIOVector *qiov, int flags, 476 int /*GRAPH_RDLOCK*/ (*file_func)(BlkLogWritesFileReq *r), 477 uint64_t entry_flags, bool is_zero_write) 478 { 479 QEMUIOVector log_qiov; 480 size_t niov = qiov ? qiov->niov : 0; 481 const BDRVBlkLogWritesState *s = bs->opaque; 482 BlkLogWritesFileReq fr = { 483 .bs = bs, 484 .offset = offset, 485 .bytes = bytes, 486 .file_flags = flags, 487 .qiov = qiov, 488 .func = file_func, 489 }; 490 BlkLogWritesLogReq lr = { 491 .bs = bs, 492 .qiov = &log_qiov, 493 .entry = { 494 .sector = cpu_to_le64(offset >> s->sectorbits), 495 .nr_sectors = cpu_to_le64(bytes >> s->sectorbits), 496 .flags = cpu_to_le64(entry_flags), 497 .data_len = 0, 498 }, 499 .zero_size = is_zero_write ? bytes : 0, 500 }; 501 void *zeroes = g_malloc0(s->sectorsize - sizeof(lr.entry)); 502 503 assert((1 << s->sectorbits) == s->sectorsize); 504 assert(bs->bl.request_alignment == s->sectorsize); 505 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment)); 506 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment)); 507 508 qemu_iovec_init(&log_qiov, niov + 2); 509 qemu_iovec_add(&log_qiov, &lr.entry, sizeof(lr.entry)); 510 qemu_iovec_add(&log_qiov, zeroes, s->sectorsize - sizeof(lr.entry)); 511 if (qiov) { 512 qemu_iovec_concat(&log_qiov, qiov, 0, qiov->size); 513 } 514 515 blk_log_writes_co_do_file(&fr); 516 blk_log_writes_co_do_log(&lr); 517 518 qemu_iovec_destroy(&log_qiov); 519 g_free(zeroes); 520 521 if (lr.log_ret < 0) { 522 return lr.log_ret; 523 } 524 525 return fr.file_ret; 526 } 527 528 static int coroutine_fn GRAPH_RDLOCK 529 blk_log_writes_co_do_file_pwritev(BlkLogWritesFileReq *fr) 530 { 531 return bdrv_co_pwritev(fr->bs->file, fr->offset, fr->bytes, 532 fr->qiov, fr->file_flags); 533 } 534 535 static int coroutine_fn GRAPH_RDLOCK 536 blk_log_writes_co_do_file_pwrite_zeroes(BlkLogWritesFileReq *fr) 537 { 538 return bdrv_co_pwrite_zeroes(fr->bs->file, fr->offset, fr->bytes, 539 fr->file_flags); 540 } 541 542 static int coroutine_fn GRAPH_RDLOCK 543 blk_log_writes_co_do_file_flush(BlkLogWritesFileReq *fr) 544 { 545 return bdrv_co_flush(fr->bs->file->bs); 546 } 547 548 static int coroutine_fn GRAPH_RDLOCK 549 blk_log_writes_co_do_file_pdiscard(BlkLogWritesFileReq *fr) 550 { 551 return bdrv_co_pdiscard(fr->bs->file, fr->offset, fr->bytes); 552 } 553 554 static int coroutine_fn GRAPH_RDLOCK 555 blk_log_writes_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes, 556 QEMUIOVector *qiov, BdrvRequestFlags flags) 557 { 558 return blk_log_writes_co_log(bs, offset, bytes, qiov, flags, 559 blk_log_writes_co_do_file_pwritev, 0, false); 560 } 561 562 static int coroutine_fn GRAPH_RDLOCK 563 blk_log_writes_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, 564 int64_t bytes, BdrvRequestFlags flags) 565 { 566 return blk_log_writes_co_log(bs, offset, bytes, NULL, flags, 567 blk_log_writes_co_do_file_pwrite_zeroes, 0, 568 true); 569 } 570 571 static int coroutine_fn GRAPH_RDLOCK 572 blk_log_writes_co_flush_to_disk(BlockDriverState *bs) 573 { 574 return blk_log_writes_co_log(bs, 0, 0, NULL, 0, 575 blk_log_writes_co_do_file_flush, 576 LOG_FLUSH_FLAG, false); 577 } 578 579 static int coroutine_fn GRAPH_RDLOCK 580 blk_log_writes_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes) 581 { 582 return blk_log_writes_co_log(bs, offset, bytes, NULL, 0, 583 blk_log_writes_co_do_file_pdiscard, 584 LOG_DISCARD_FLAG, false); 585 } 586 587 static const char *const blk_log_writes_strong_runtime_opts[] = { 588 "log-append", 589 "log-sector-size", 590 591 NULL 592 }; 593 594 static BlockDriver bdrv_blk_log_writes = { 595 .format_name = "blklogwrites", 596 .instance_size = sizeof(BDRVBlkLogWritesState), 597 598 .bdrv_open = blk_log_writes_open, 599 .bdrv_close = blk_log_writes_close, 600 .bdrv_co_getlength = blk_log_writes_co_getlength, 601 .bdrv_child_perm = blk_log_writes_child_perm, 602 .bdrv_refresh_limits = blk_log_writes_refresh_limits, 603 604 .bdrv_co_preadv = blk_log_writes_co_preadv, 605 .bdrv_co_pwritev = blk_log_writes_co_pwritev, 606 .bdrv_co_pwrite_zeroes = blk_log_writes_co_pwrite_zeroes, 607 .bdrv_co_flush_to_disk = blk_log_writes_co_flush_to_disk, 608 .bdrv_co_pdiscard = blk_log_writes_co_pdiscard, 609 610 .is_filter = true, 611 .strong_runtime_opts = blk_log_writes_strong_runtime_opts, 612 }; 613 614 static void bdrv_blk_log_writes_init(void) 615 { 616 bdrv_register(&bdrv_blk_log_writes); 617 } 618 619 block_init(bdrv_blk_log_writes_init); 620