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 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_int.h"
16 #include "qapi/qmp/qdict.h"
17 #include "qapi/qmp/qstring.h"
18 #include "qemu/cutils.h"
19 #include "qemu/option.h"
21 /* Disk format stuff - taken from Linux drivers/md/dm-log-writes.c */
23 #define LOG_FLUSH_FLAG (1 << 0)
24 #define LOG_FUA_FLAG (1 << 1)
25 #define LOG_DISCARD_FLAG (1 << 2)
26 #define LOG_MARK_FLAG (1 << 3)
27 #define LOG_FLAG_MASK (LOG_FLUSH_FLAG \
32 #define WRITE_LOG_VERSION 1ULL
33 #define WRITE_LOG_MAGIC 0x6a736677736872ULL
35 /* All fields are little-endian. */
36 struct log_write_super
{
43 struct log_write_entry
{
50 /* End of disk format structures. */
56 uint64_t cur_log_sector
;
58 uint64_t update_interval
;
59 } BDRVBlkLogWritesState
;
61 static QemuOptsList runtime_opts
= {
62 .name
= "blklogwrites",
63 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
67 .type
= QEMU_OPT_BOOL
,
68 .help
= "Append to an existing log",
71 .name
= "log-sector-size",
72 .type
= QEMU_OPT_SIZE
,
73 .help
= "Log sector size",
76 .name
= "log-super-update-interval",
77 .type
= QEMU_OPT_NUMBER
,
78 .help
= "Log superblock update interval (# of write requests)",
84 static inline uint32_t blk_log_writes_log2(uint32_t value
)
87 return 31 - clz32(value
);
90 static inline bool blk_log_writes_sector_size_valid(uint32_t sector_size
)
92 return is_power_of_2(sector_size
) &&
93 sector_size
>= sizeof(struct log_write_super
) &&
94 sector_size
>= sizeof(struct log_write_entry
) &&
95 sector_size
< (1ull << 24);
98 static uint64_t blk_log_writes_find_cur_log_sector(BdrvChild
*log
,
103 uint64_t cur_sector
= 1;
104 uint64_t cur_idx
= 0;
105 uint32_t sector_bits
= blk_log_writes_log2(sector_size
);
106 struct log_write_entry cur_entry
;
108 while (cur_idx
< nr_entries
) {
109 int read_ret
= bdrv_pread(log
, cur_sector
<< sector_bits
, &cur_entry
,
112 error_setg_errno(errp
, -read_ret
,
113 "Failed to read log entry %"PRIu64
, cur_idx
);
114 return (uint64_t)-1ull;
117 if (cur_entry
.flags
& ~cpu_to_le64(LOG_FLAG_MASK
)) {
118 error_setg(errp
, "Invalid flags 0x%"PRIx64
" in log entry %"PRIu64
,
119 le64_to_cpu(cur_entry
.flags
), cur_idx
);
120 return (uint64_t)-1ull;
123 /* Account for the sector of the entry itself */
127 * Account for the data of the write.
128 * For discards, this data is not present.
130 if (!(cur_entry
.flags
& cpu_to_le64(LOG_DISCARD_FLAG
))) {
131 cur_sector
+= le64_to_cpu(cur_entry
.nr_sectors
);
140 static int blk_log_writes_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
143 BDRVBlkLogWritesState
*s
= bs
->opaque
;
145 Error
*local_err
= NULL
;
147 uint64_t log_sector_size
;
150 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
151 qemu_opts_absorb_qdict(opts
, options
, &local_err
);
154 error_propagate(errp
, local_err
);
159 bs
->file
= bdrv_open_child(NULL
, options
, "file", bs
, &child_file
, false,
163 error_propagate(errp
, local_err
);
167 /* Open the log file */
168 s
->log_file
= bdrv_open_child(NULL
, options
, "log", bs
, &child_file
, false,
172 error_propagate(errp
, local_err
);
176 log_append
= qemu_opt_get_bool(opts
, "log-append", false);
179 struct log_write_super log_sb
= { 0, 0, 0, 0 };
181 if (qemu_opt_find(opts
, "log-sector-size")) {
183 error_setg(errp
, "log-append and log-sector-size are mutually "
188 /* Read log superblock or fake one for an empty log */
189 if (!bdrv_getlength(s
->log_file
->bs
)) {
190 log_sb
.magic
= cpu_to_le64(WRITE_LOG_MAGIC
);
191 log_sb
.version
= cpu_to_le64(WRITE_LOG_VERSION
);
192 log_sb
.nr_entries
= cpu_to_le64(0);
193 log_sb
.sectorsize
= cpu_to_le32(BDRV_SECTOR_SIZE
);
195 ret
= bdrv_pread(s
->log_file
, 0, &log_sb
, sizeof(log_sb
));
197 error_setg_errno(errp
, -ret
, "Could not read log superblock");
202 if (log_sb
.magic
!= cpu_to_le64(WRITE_LOG_MAGIC
)) {
204 error_setg(errp
, "Invalid log superblock magic");
208 if (log_sb
.version
!= cpu_to_le64(WRITE_LOG_VERSION
)) {
210 error_setg(errp
, "Unsupported log version %"PRIu64
,
211 le64_to_cpu(log_sb
.version
));
215 log_sector_size
= le32_to_cpu(log_sb
.sectorsize
);
216 s
->cur_log_sector
= 1;
219 if (blk_log_writes_sector_size_valid(log_sector_size
)) {
221 blk_log_writes_find_cur_log_sector(s
->log_file
, log_sector_size
,
222 le64_to_cpu(log_sb
.nr_entries
), &local_err
);
225 error_propagate(errp
, local_err
);
229 s
->nr_entries
= le64_to_cpu(log_sb
.nr_entries
);
232 log_sector_size
= qemu_opt_get_size(opts
, "log-sector-size",
234 s
->cur_log_sector
= 1;
238 if (!blk_log_writes_sector_size_valid(log_sector_size
)) {
240 error_setg(errp
, "Invalid log sector size %"PRIu64
, log_sector_size
);
244 s
->sectorsize
= log_sector_size
;
245 s
->sectorbits
= blk_log_writes_log2(log_sector_size
);
246 s
->update_interval
= qemu_opt_get_number(opts
, "log-super-update-interval",
248 if (!s
->update_interval
) {
250 error_setg(errp
, "Invalid log superblock update interval %"PRIu64
,
258 bdrv_unref_child(bs
, s
->log_file
);
263 bdrv_unref_child(bs
, bs
->file
);
270 static void blk_log_writes_close(BlockDriverState
*bs
)
272 BDRVBlkLogWritesState
*s
= bs
->opaque
;
274 bdrv_unref_child(bs
, s
->log_file
);
278 static int64_t blk_log_writes_getlength(BlockDriverState
*bs
)
280 return bdrv_getlength(bs
->file
->bs
);
283 static void blk_log_writes_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
284 const BdrvChildRole
*role
,
285 BlockReopenQueue
*ro_q
,
286 uint64_t perm
, uint64_t shrd
,
287 uint64_t *nperm
, uint64_t *nshrd
)
290 *nperm
= perm
& DEFAULT_PERM_PASSTHROUGH
;
291 *nshrd
= (shrd
& DEFAULT_PERM_PASSTHROUGH
) | DEFAULT_PERM_UNCHANGED
;
295 if (!strcmp(c
->name
, "log")) {
296 bdrv_format_default_perms(bs
, c
, role
, ro_q
, perm
, shrd
, nperm
, nshrd
);
298 bdrv_filter_default_perms(bs
, c
, role
, ro_q
, perm
, shrd
, nperm
, nshrd
);
302 static void blk_log_writes_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
304 BDRVBlkLogWritesState
*s
= bs
->opaque
;
305 bs
->bl
.request_alignment
= s
->sectorsize
;
308 static int coroutine_fn
309 blk_log_writes_co_preadv(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
310 QEMUIOVector
*qiov
, int flags
)
312 return bdrv_co_preadv(bs
->file
, offset
, bytes
, qiov
, flags
);
315 typedef struct BlkLogWritesFileReq
{
316 BlockDriverState
*bs
;
321 int (*func
)(struct BlkLogWritesFileReq
*r
);
323 } BlkLogWritesFileReq
;
326 BlockDriverState
*bs
;
328 struct log_write_entry entry
;
331 } BlkLogWritesLogReq
;
333 static void coroutine_fn
blk_log_writes_co_do_log(BlkLogWritesLogReq
*lr
)
335 BDRVBlkLogWritesState
*s
= lr
->bs
->opaque
;
336 uint64_t cur_log_offset
= s
->cur_log_sector
<< s
->sectorbits
;
340 ROUND_UP(lr
->qiov
->size
, s
->sectorsize
) >> s
->sectorbits
;
342 lr
->log_ret
= bdrv_co_pwritev(s
->log_file
, cur_log_offset
, lr
->qiov
->size
,
345 /* Logging for the "write zeroes" operation */
346 if (lr
->log_ret
== 0 && lr
->zero_size
) {
347 cur_log_offset
= s
->cur_log_sector
<< s
->sectorbits
;
349 ROUND_UP(lr
->zero_size
, s
->sectorsize
) >> s
->sectorbits
;
351 lr
->log_ret
= bdrv_co_pwrite_zeroes(s
->log_file
, cur_log_offset
,
355 /* Update super block on flush or every update interval */
356 if (lr
->log_ret
== 0 && ((lr
->entry
.flags
& LOG_FLUSH_FLAG
)
357 || (s
->nr_entries
% s
->update_interval
== 0)))
359 struct log_write_super super
= {
360 .magic
= cpu_to_le64(WRITE_LOG_MAGIC
),
361 .version
= cpu_to_le64(WRITE_LOG_VERSION
),
362 .nr_entries
= cpu_to_le64(s
->nr_entries
),
363 .sectorsize
= cpu_to_le32(s
->sectorsize
),
365 void *zeroes
= g_malloc0(s
->sectorsize
- sizeof(super
));
368 qemu_iovec_init(&qiov
, 2);
369 qemu_iovec_add(&qiov
, &super
, sizeof(super
));
370 qemu_iovec_add(&qiov
, zeroes
, s
->sectorsize
- sizeof(super
));
373 bdrv_co_pwritev(s
->log_file
, 0, s
->sectorsize
, &qiov
, 0);
374 if (lr
->log_ret
== 0) {
375 lr
->log_ret
= bdrv_co_flush(s
->log_file
->bs
);
377 qemu_iovec_destroy(&qiov
);
382 static void coroutine_fn
blk_log_writes_co_do_file(BlkLogWritesFileReq
*fr
)
384 fr
->file_ret
= fr
->func(fr
);
387 static int coroutine_fn
388 blk_log_writes_co_log(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
389 QEMUIOVector
*qiov
, int flags
,
390 int (*file_func
)(BlkLogWritesFileReq
*r
),
391 uint64_t entry_flags
, bool is_zero_write
)
393 QEMUIOVector log_qiov
;
394 size_t niov
= qiov
? qiov
->niov
: 0;
395 BDRVBlkLogWritesState
*s
= bs
->opaque
;
396 BlkLogWritesFileReq fr
= {
404 BlkLogWritesLogReq lr
= {
408 .sector
= cpu_to_le64(offset
>> s
->sectorbits
),
409 .nr_sectors
= cpu_to_le64(bytes
>> s
->sectorbits
),
410 .flags
= cpu_to_le64(entry_flags
),
413 .zero_size
= is_zero_write
? bytes
: 0,
415 void *zeroes
= g_malloc0(s
->sectorsize
- sizeof(lr
.entry
));
417 assert((1 << s
->sectorbits
) == s
->sectorsize
);
418 assert(bs
->bl
.request_alignment
== s
->sectorsize
);
419 assert(QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
));
420 assert(QEMU_IS_ALIGNED(bytes
, bs
->bl
.request_alignment
));
422 qemu_iovec_init(&log_qiov
, niov
+ 2);
423 qemu_iovec_add(&log_qiov
, &lr
.entry
, sizeof(lr
.entry
));
424 qemu_iovec_add(&log_qiov
, zeroes
, s
->sectorsize
- sizeof(lr
.entry
));
426 qemu_iovec_concat(&log_qiov
, qiov
, 0, qiov
->size
);
429 blk_log_writes_co_do_file(&fr
);
430 blk_log_writes_co_do_log(&lr
);
432 qemu_iovec_destroy(&log_qiov
);
435 if (lr
.log_ret
< 0) {
442 static int coroutine_fn
443 blk_log_writes_co_do_file_pwritev(BlkLogWritesFileReq
*fr
)
445 return bdrv_co_pwritev(fr
->bs
->file
, fr
->offset
, fr
->bytes
,
446 fr
->qiov
, fr
->file_flags
);
449 static int coroutine_fn
450 blk_log_writes_co_do_file_pwrite_zeroes(BlkLogWritesFileReq
*fr
)
452 return bdrv_co_pwrite_zeroes(fr
->bs
->file
, fr
->offset
, fr
->bytes
,
456 static int coroutine_fn
blk_log_writes_co_do_file_flush(BlkLogWritesFileReq
*fr
)
458 return bdrv_co_flush(fr
->bs
->file
->bs
);
461 static int coroutine_fn
462 blk_log_writes_co_do_file_pdiscard(BlkLogWritesFileReq
*fr
)
464 return bdrv_co_pdiscard(fr
->bs
->file
, fr
->offset
, fr
->bytes
);
467 static int coroutine_fn
468 blk_log_writes_co_pwritev(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
469 QEMUIOVector
*qiov
, int flags
)
471 return blk_log_writes_co_log(bs
, offset
, bytes
, qiov
, flags
,
472 blk_log_writes_co_do_file_pwritev
, 0, false);
475 static int coroutine_fn
476 blk_log_writes_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
, int bytes
,
477 BdrvRequestFlags flags
)
479 return blk_log_writes_co_log(bs
, offset
, bytes
, NULL
, flags
,
480 blk_log_writes_co_do_file_pwrite_zeroes
, 0,
484 static int coroutine_fn
blk_log_writes_co_flush_to_disk(BlockDriverState
*bs
)
486 return blk_log_writes_co_log(bs
, 0, 0, NULL
, 0,
487 blk_log_writes_co_do_file_flush
,
488 LOG_FLUSH_FLAG
, false);
491 static int coroutine_fn
492 blk_log_writes_co_pdiscard(BlockDriverState
*bs
, int64_t offset
, int count
)
494 return blk_log_writes_co_log(bs
, offset
, count
, NULL
, 0,
495 blk_log_writes_co_do_file_pdiscard
,
496 LOG_DISCARD_FLAG
, false);
499 static const char *const blk_log_writes_strong_runtime_opts
[] = {
506 static BlockDriver bdrv_blk_log_writes
= {
507 .format_name
= "blklogwrites",
508 .instance_size
= sizeof(BDRVBlkLogWritesState
),
510 .bdrv_open
= blk_log_writes_open
,
511 .bdrv_close
= blk_log_writes_close
,
512 .bdrv_getlength
= blk_log_writes_getlength
,
513 .bdrv_child_perm
= blk_log_writes_child_perm
,
514 .bdrv_refresh_limits
= blk_log_writes_refresh_limits
,
516 .bdrv_co_preadv
= blk_log_writes_co_preadv
,
517 .bdrv_co_pwritev
= blk_log_writes_co_pwritev
,
518 .bdrv_co_pwrite_zeroes
= blk_log_writes_co_pwrite_zeroes
,
519 .bdrv_co_flush_to_disk
= blk_log_writes_co_flush_to_disk
,
520 .bdrv_co_pdiscard
= blk_log_writes_co_pdiscard
,
521 .bdrv_co_block_status
= bdrv_co_block_status_from_file
,
524 .strong_runtime_opts
= blk_log_writes_strong_runtime_opts
,
527 static void bdrv_blk_log_writes_init(void)
529 bdrv_register(&bdrv_blk_log_writes
);
532 block_init(bdrv_blk_log_writes_init
);