2 * Write logging blk driver based on blkverify and blkdebug.
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>
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.
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 "qapi/qmp/qdict.h"
18 #include "qapi/qmp/qstring.h"
19 #include "qemu/cutils.h"
20 #include "qemu/module.h"
21 #include "qemu/option.h"
23 /* Disk format stuff - taken from Linux drivers/md/dm-log-writes.c */
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 \
34 #define WRITE_LOG_VERSION 1ULL
35 #define WRITE_LOG_MAGIC 0x6a736677736872ULL
37 /* All fields are little-endian. */
38 struct log_write_super
{
45 struct log_write_entry
{
52 /* End of disk format structures. */
58 uint64_t update_interval
;
61 * The mutable state of the driver, consisting of the current log sector
62 * and the number of log entries.
64 * May be read and/or written from multiple threads, and the mutex must be
65 * held when accessing these fields.
67 uint64_t cur_log_sector
;
72 * The super block sequence number. Non-zero if a super block update is in
75 * The mutex must be held when accessing this field.
77 uint64_t super_update_seq
;
80 * A coroutine-aware queue to serialize super block updates.
82 * Used with the mutex to ensure that only one thread be updating the super
85 CoQueue super_update_queue
;
86 } BDRVBlkLogWritesState
;
88 static QemuOptsList runtime_opts
= {
89 .name
= "blklogwrites",
90 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
94 .type
= QEMU_OPT_BOOL
,
95 .help
= "Append to an existing log",
98 .name
= "log-sector-size",
99 .type
= QEMU_OPT_SIZE
,
100 .help
= "Log sector size",
103 .name
= "log-super-update-interval",
104 .type
= QEMU_OPT_NUMBER
,
105 .help
= "Log superblock update interval (# of write requests)",
107 { /* end of list */ }
111 static inline uint32_t blk_log_writes_log2(uint32_t value
)
114 return 31 - clz32(value
);
117 static inline bool blk_log_writes_sector_size_valid(uint32_t sector_size
)
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);
125 static uint64_t blk_log_writes_find_cur_log_sector(BdrvChild
*log
,
126 uint32_t sector_size
,
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
;
135 while (cur_idx
< nr_entries
) {
136 int read_ret
= bdrv_pread(log
, cur_sector
<< sector_bits
,
137 sizeof(cur_entry
), &cur_entry
, 0);
139 error_setg_errno(errp
, -read_ret
,
140 "Failed to read log entry %"PRIu64
, cur_idx
);
141 return (uint64_t)-1ull;
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;
150 /* Account for the sector of the entry itself */
154 * Account for the data of the write.
155 * For discards, this data is not present.
157 if (!(cur_entry
.flags
& cpu_to_le64(LOG_DISCARD_FLAG
))) {
158 cur_sector
+= le64_to_cpu(cur_entry
.nr_sectors
);
167 static int blk_log_writes_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
170 BDRVBlkLogWritesState
*s
= bs
->opaque
;
172 Error
*local_err
= NULL
;
174 uint64_t log_sector_size
;
177 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
178 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
184 ret
= bdrv_open_file_child(NULL
, options
, "file", bs
, errp
);
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
);
197 qemu_mutex_init(&s
->mutex
);
198 qemu_co_queue_init(&s
->super_update_queue
);
200 log_append
= qemu_opt_get_bool(opts
, "log-append", false);
203 struct log_write_super log_sb
= { 0, 0, 0, 0 };
205 if (qemu_opt_find(opts
, "log-sector-size")) {
207 error_setg(errp
, "log-append and log-sector-size are mutually "
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
);
219 ret
= bdrv_pread(s
->log_file
, 0, sizeof(log_sb
), &log_sb
, 0);
221 error_setg_errno(errp
, -ret
, "Could not read log superblock");
226 if (log_sb
.magic
!= cpu_to_le64(WRITE_LOG_MAGIC
)) {
228 error_setg(errp
, "Invalid log superblock magic");
232 if (log_sb
.version
!= cpu_to_le64(WRITE_LOG_VERSION
)) {
234 error_setg(errp
, "Unsupported log version %"PRIu64
,
235 le64_to_cpu(log_sb
.version
));
239 log_sector_size
= le32_to_cpu(log_sb
.sectorsize
);
240 s
->cur_log_sector
= 1;
243 if (blk_log_writes_sector_size_valid(log_sector_size
)) {
245 blk_log_writes_find_cur_log_sector(s
->log_file
, log_sector_size
,
246 le64_to_cpu(log_sb
.nr_entries
), &local_err
);
249 error_propagate(errp
, local_err
);
253 s
->nr_entries
= le64_to_cpu(log_sb
.nr_entries
);
256 log_sector_size
= qemu_opt_get_size(opts
, "log-sector-size",
258 s
->cur_log_sector
= 1;
262 s
->super_update_seq
= 0;
264 if (!blk_log_writes_sector_size_valid(log_sector_size
)) {
266 error_setg(errp
, "Invalid log sector size %"PRIu64
, log_sector_size
);
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",
274 if (!s
->update_interval
) {
276 error_setg(errp
, "Invalid log superblock update interval %"PRIu64
,
285 bdrv_unref_child(bs
, s
->log_file
);
286 bdrv_graph_wrunlock();
288 qemu_mutex_destroy(&s
->mutex
);
295 static void blk_log_writes_close(BlockDriverState
*bs
)
297 BDRVBlkLogWritesState
*s
= bs
->opaque
;
300 bdrv_unref_child(bs
, s
->log_file
);
302 bdrv_graph_wrunlock();
303 qemu_mutex_destroy(&s
->mutex
);
306 static int64_t coroutine_fn GRAPH_RDLOCK
307 blk_log_writes_co_getlength(BlockDriverState
*bs
)
309 return bdrv_co_getlength(bs
->file
->bs
);
312 static void blk_log_writes_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
314 BlockReopenQueue
*ro_q
,
315 uint64_t perm
, uint64_t shrd
,
316 uint64_t *nperm
, uint64_t *nshrd
)
319 *nperm
= perm
& DEFAULT_PERM_PASSTHROUGH
;
320 *nshrd
= (shrd
& DEFAULT_PERM_PASSTHROUGH
) | DEFAULT_PERM_UNCHANGED
;
324 bdrv_default_perms(bs
, c
, role
, ro_q
, perm
, shrd
,
328 static void blk_log_writes_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
330 const BDRVBlkLogWritesState
*s
= bs
->opaque
;
331 bs
->bl
.request_alignment
= s
->sectorsize
;
334 static int coroutine_fn GRAPH_RDLOCK
335 blk_log_writes_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
336 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
338 return bdrv_co_preadv(bs
->file
, offset
, bytes
, qiov
, flags
);
341 typedef struct BlkLogWritesFileReq
{
342 BlockDriverState
*bs
;
347 int GRAPH_RDLOCK_PTR (*func
)(struct BlkLogWritesFileReq
*r
);
349 } BlkLogWritesFileReq
;
352 BlockDriverState
*bs
;
354 struct log_write_entry entry
;
357 } BlkLogWritesLogReq
;
359 static void coroutine_fn GRAPH_RDLOCK
360 blk_log_writes_co_do_log(BlkLogWritesLogReq
*lr
)
362 BDRVBlkLogWritesState
*s
= lr
->bs
->opaque
;
365 * Determine the offsets and sizes of different parts of the entry, and
366 * update the state of the driver.
368 * This needs to be done in one go, before any actual I/O is done, as the
369 * log entry may have to be written in two parts, and the state of the
370 * driver may be modified by other driver operations while waiting for the
373 qemu_mutex_lock(&s
->mutex
);
374 const uint64_t entry_start_sector
= s
->cur_log_sector
;
375 const uint64_t entry_offset
= entry_start_sector
<< s
->sectorbits
;
376 const uint64_t qiov_aligned_size
= ROUND_UP(lr
->qiov
->size
, s
->sectorsize
);
377 const uint64_t entry_aligned_size
= qiov_aligned_size
+
378 ROUND_UP(lr
->zero_size
, s
->sectorsize
);
379 const uint64_t entry_nr_sectors
= entry_aligned_size
>> s
->sectorbits
;
380 const uint64_t entry_seq
= s
->nr_entries
+ 1;
382 s
->nr_entries
= entry_seq
;
383 s
->cur_log_sector
+= entry_nr_sectors
;
384 qemu_mutex_unlock(&s
->mutex
);
387 * Write the log entry. Note that if this is a "write zeroes" operation,
388 * only the entry header is written here, with the zeroing being done
391 lr
->log_ret
= bdrv_co_pwritev(s
->log_file
, entry_offset
, lr
->qiov
->size
,
394 /* Logging for the "write zeroes" operation */
395 if (lr
->log_ret
== 0 && lr
->zero_size
) {
396 const uint64_t zeroes_offset
= entry_offset
+ qiov_aligned_size
;
398 lr
->log_ret
= bdrv_co_pwrite_zeroes(s
->log_file
, zeroes_offset
,
402 /* Update super block on flush or every update interval */
403 if (lr
->log_ret
== 0 && ((lr
->entry
.flags
& LOG_FLUSH_FLAG
)
404 || (entry_seq
% s
->update_interval
== 0)))
406 struct log_write_super super
= {
407 .magic
= cpu_to_le64(WRITE_LOG_MAGIC
),
408 .version
= cpu_to_le64(WRITE_LOG_VERSION
),
409 .nr_entries
= 0, /* updated below */
410 .sectorsize
= cpu_to_le32(s
->sectorsize
),
416 * Wait if a super block update is already in progress.
417 * Bail out if a newer update got its turn before us.
419 WITH_QEMU_LOCK_GUARD(&s
->mutex
) {
420 CoQueueWaitFlags wait_flags
= 0;
421 while (s
->super_update_seq
) {
422 if (entry_seq
< s
->super_update_seq
) {
425 qemu_co_queue_wait_flags(&s
->super_update_queue
,
426 &s
->mutex
, wait_flags
);
429 * In case the wait condition remains true after wakeup,
430 * to avoid starvation, make sure that this request is
431 * scheduled to rerun next by pushing it to the front of the
434 wait_flags
= CO_QUEUE_WAIT_FRONT
;
436 s
->super_update_seq
= entry_seq
;
437 super
.nr_entries
= cpu_to_le64(s
->nr_entries
);
440 zeroes
= g_malloc0(s
->sectorsize
- sizeof(super
));
442 qemu_iovec_init(&qiov
, 2);
443 qemu_iovec_add(&qiov
, &super
, sizeof(super
));
444 qemu_iovec_add(&qiov
, zeroes
, s
->sectorsize
- sizeof(super
));
447 bdrv_co_pwritev(s
->log_file
, 0, s
->sectorsize
, &qiov
, 0);
448 if (lr
->log_ret
== 0) {
449 lr
->log_ret
= bdrv_co_flush(s
->log_file
->bs
);
452 /* The super block has been updated. Let another request have a go. */
453 qemu_mutex_lock(&s
->mutex
);
454 s
->super_update_seq
= 0;
455 (void) qemu_co_queue_next(&s
->super_update_queue
);
456 qemu_mutex_unlock(&s
->mutex
);
458 qemu_iovec_destroy(&qiov
);
463 static void coroutine_fn GRAPH_RDLOCK
464 blk_log_writes_co_do_file(BlkLogWritesFileReq
*fr
)
466 fr
->file_ret
= fr
->func(fr
);
469 static int coroutine_fn GRAPH_RDLOCK
470 blk_log_writes_co_log(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
471 QEMUIOVector
*qiov
, int flags
,
472 int /*GRAPH_RDLOCK*/ (*file_func
)(BlkLogWritesFileReq
*r
),
473 uint64_t entry_flags
, bool is_zero_write
)
475 QEMUIOVector log_qiov
;
476 size_t niov
= qiov
? qiov
->niov
: 0;
477 const BDRVBlkLogWritesState
*s
= bs
->opaque
;
478 BlkLogWritesFileReq fr
= {
486 BlkLogWritesLogReq lr
= {
490 .sector
= cpu_to_le64(offset
>> s
->sectorbits
),
491 .nr_sectors
= cpu_to_le64(bytes
>> s
->sectorbits
),
492 .flags
= cpu_to_le64(entry_flags
),
495 .zero_size
= is_zero_write
? bytes
: 0,
497 void *zeroes
= g_malloc0(s
->sectorsize
- sizeof(lr
.entry
));
499 assert((1 << s
->sectorbits
) == s
->sectorsize
);
500 assert(bs
->bl
.request_alignment
== s
->sectorsize
);
501 assert(QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
));
502 assert(QEMU_IS_ALIGNED(bytes
, bs
->bl
.request_alignment
));
504 qemu_iovec_init(&log_qiov
, niov
+ 2);
505 qemu_iovec_add(&log_qiov
, &lr
.entry
, sizeof(lr
.entry
));
506 qemu_iovec_add(&log_qiov
, zeroes
, s
->sectorsize
- sizeof(lr
.entry
));
508 qemu_iovec_concat(&log_qiov
, qiov
, 0, qiov
->size
);
511 blk_log_writes_co_do_file(&fr
);
512 blk_log_writes_co_do_log(&lr
);
514 qemu_iovec_destroy(&log_qiov
);
517 if (lr
.log_ret
< 0) {
524 static int coroutine_fn GRAPH_RDLOCK
525 blk_log_writes_co_do_file_pwritev(BlkLogWritesFileReq
*fr
)
527 return bdrv_co_pwritev(fr
->bs
->file
, fr
->offset
, fr
->bytes
,
528 fr
->qiov
, fr
->file_flags
);
531 static int coroutine_fn GRAPH_RDLOCK
532 blk_log_writes_co_do_file_pwrite_zeroes(BlkLogWritesFileReq
*fr
)
534 return bdrv_co_pwrite_zeroes(fr
->bs
->file
, fr
->offset
, fr
->bytes
,
538 static int coroutine_fn GRAPH_RDLOCK
539 blk_log_writes_co_do_file_flush(BlkLogWritesFileReq
*fr
)
541 return bdrv_co_flush(fr
->bs
->file
->bs
);
544 static int coroutine_fn GRAPH_RDLOCK
545 blk_log_writes_co_do_file_pdiscard(BlkLogWritesFileReq
*fr
)
547 return bdrv_co_pdiscard(fr
->bs
->file
, fr
->offset
, fr
->bytes
);
550 static int coroutine_fn GRAPH_RDLOCK
551 blk_log_writes_co_pwritev(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
552 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
554 return blk_log_writes_co_log(bs
, offset
, bytes
, qiov
, flags
,
555 blk_log_writes_co_do_file_pwritev
, 0, false);
558 static int coroutine_fn GRAPH_RDLOCK
559 blk_log_writes_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
560 int64_t bytes
, BdrvRequestFlags flags
)
562 return blk_log_writes_co_log(bs
, offset
, bytes
, NULL
, flags
,
563 blk_log_writes_co_do_file_pwrite_zeroes
, 0,
567 static int coroutine_fn GRAPH_RDLOCK
568 blk_log_writes_co_flush_to_disk(BlockDriverState
*bs
)
570 return blk_log_writes_co_log(bs
, 0, 0, NULL
, 0,
571 blk_log_writes_co_do_file_flush
,
572 LOG_FLUSH_FLAG
, false);
575 static int coroutine_fn GRAPH_RDLOCK
576 blk_log_writes_co_pdiscard(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
578 return blk_log_writes_co_log(bs
, offset
, bytes
, NULL
, 0,
579 blk_log_writes_co_do_file_pdiscard
,
580 LOG_DISCARD_FLAG
, false);
583 static const char *const blk_log_writes_strong_runtime_opts
[] = {
590 static BlockDriver bdrv_blk_log_writes
= {
591 .format_name
= "blklogwrites",
592 .instance_size
= sizeof(BDRVBlkLogWritesState
),
594 .bdrv_open
= blk_log_writes_open
,
595 .bdrv_close
= blk_log_writes_close
,
596 .bdrv_co_getlength
= blk_log_writes_co_getlength
,
597 .bdrv_child_perm
= blk_log_writes_child_perm
,
598 .bdrv_refresh_limits
= blk_log_writes_refresh_limits
,
600 .bdrv_co_preadv
= blk_log_writes_co_preadv
,
601 .bdrv_co_pwritev
= blk_log_writes_co_pwritev
,
602 .bdrv_co_pwrite_zeroes
= blk_log_writes_co_pwrite_zeroes
,
603 .bdrv_co_flush_to_disk
= blk_log_writes_co_flush_to_disk
,
604 .bdrv_co_pdiscard
= blk_log_writes_co_pdiscard
,
607 .strong_runtime_opts
= blk_log_writes_strong_runtime_opts
,
610 static void bdrv_blk_log_writes_init(void)
612 bdrv_register(&bdrv_blk_log_writes
);
615 block_init(bdrv_blk_log_writes_init
);