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-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 cur_log_sector
;
60 uint64_t update_interval
;
61 } BDRVBlkLogWritesState
;
63 static QemuOptsList runtime_opts
= {
64 .name
= "blklogwrites",
65 .head
= QTAILQ_HEAD_INITIALIZER(runtime_opts
.head
),
69 .type
= QEMU_OPT_BOOL
,
70 .help
= "Append to an existing log",
73 .name
= "log-sector-size",
74 .type
= QEMU_OPT_SIZE
,
75 .help
= "Log sector size",
78 .name
= "log-super-update-interval",
79 .type
= QEMU_OPT_NUMBER
,
80 .help
= "Log superblock update interval (# of write requests)",
86 static inline uint32_t blk_log_writes_log2(uint32_t value
)
89 return 31 - clz32(value
);
92 static inline bool blk_log_writes_sector_size_valid(uint32_t sector_size
)
94 return is_power_of_2(sector_size
) &&
95 sector_size
>= sizeof(struct log_write_super
) &&
96 sector_size
>= sizeof(struct log_write_entry
) &&
97 sector_size
< (1ull << 24);
100 static uint64_t blk_log_writes_find_cur_log_sector(BdrvChild
*log
,
101 uint32_t sector_size
,
105 uint64_t cur_sector
= 1;
106 uint64_t cur_idx
= 0;
107 uint32_t sector_bits
= blk_log_writes_log2(sector_size
);
108 struct log_write_entry cur_entry
;
110 while (cur_idx
< nr_entries
) {
111 int read_ret
= bdrv_pread(log
, cur_sector
<< sector_bits
,
112 sizeof(cur_entry
), &cur_entry
, 0);
114 error_setg_errno(errp
, -read_ret
,
115 "Failed to read log entry %"PRIu64
, cur_idx
);
116 return (uint64_t)-1ull;
119 if (cur_entry
.flags
& ~cpu_to_le64(LOG_FLAG_MASK
)) {
120 error_setg(errp
, "Invalid flags 0x%"PRIx64
" in log entry %"PRIu64
,
121 le64_to_cpu(cur_entry
.flags
), cur_idx
);
122 return (uint64_t)-1ull;
125 /* Account for the sector of the entry itself */
129 * Account for the data of the write.
130 * For discards, this data is not present.
132 if (!(cur_entry
.flags
& cpu_to_le64(LOG_DISCARD_FLAG
))) {
133 cur_sector
+= le64_to_cpu(cur_entry
.nr_sectors
);
142 static int blk_log_writes_open(BlockDriverState
*bs
, QDict
*options
, int flags
,
145 BDRVBlkLogWritesState
*s
= bs
->opaque
;
147 Error
*local_err
= NULL
;
149 uint64_t log_sector_size
;
152 opts
= qemu_opts_create(&runtime_opts
, NULL
, 0, &error_abort
);
153 if (!qemu_opts_absorb_qdict(opts
, options
, errp
)) {
159 ret
= bdrv_open_file_child(NULL
, options
, "file", bs
, errp
);
164 /* Open the log file */
165 s
->log_file
= bdrv_open_child(NULL
, options
, "log", bs
, &child_of_bds
,
166 BDRV_CHILD_METADATA
, false, errp
);
172 log_append
= qemu_opt_get_bool(opts
, "log-append", false);
175 struct log_write_super log_sb
= { 0, 0, 0, 0 };
177 if (qemu_opt_find(opts
, "log-sector-size")) {
179 error_setg(errp
, "log-append and log-sector-size are mutually "
184 /* Read log superblock or fake one for an empty log */
185 if (!bdrv_getlength(s
->log_file
->bs
)) {
186 log_sb
.magic
= cpu_to_le64(WRITE_LOG_MAGIC
);
187 log_sb
.version
= cpu_to_le64(WRITE_LOG_VERSION
);
188 log_sb
.nr_entries
= cpu_to_le64(0);
189 log_sb
.sectorsize
= cpu_to_le32(BDRV_SECTOR_SIZE
);
191 ret
= bdrv_pread(s
->log_file
, 0, sizeof(log_sb
), &log_sb
, 0);
193 error_setg_errno(errp
, -ret
, "Could not read log superblock");
198 if (log_sb
.magic
!= cpu_to_le64(WRITE_LOG_MAGIC
)) {
200 error_setg(errp
, "Invalid log superblock magic");
204 if (log_sb
.version
!= cpu_to_le64(WRITE_LOG_VERSION
)) {
206 error_setg(errp
, "Unsupported log version %"PRIu64
,
207 le64_to_cpu(log_sb
.version
));
211 log_sector_size
= le32_to_cpu(log_sb
.sectorsize
);
212 s
->cur_log_sector
= 1;
215 if (blk_log_writes_sector_size_valid(log_sector_size
)) {
217 blk_log_writes_find_cur_log_sector(s
->log_file
, log_sector_size
,
218 le64_to_cpu(log_sb
.nr_entries
), &local_err
);
221 error_propagate(errp
, local_err
);
225 s
->nr_entries
= le64_to_cpu(log_sb
.nr_entries
);
228 log_sector_size
= qemu_opt_get_size(opts
, "log-sector-size",
230 s
->cur_log_sector
= 1;
234 if (!blk_log_writes_sector_size_valid(log_sector_size
)) {
236 error_setg(errp
, "Invalid log sector size %"PRIu64
, log_sector_size
);
240 s
->sectorsize
= log_sector_size
;
241 s
->sectorbits
= blk_log_writes_log2(log_sector_size
);
242 s
->update_interval
= qemu_opt_get_number(opts
, "log-super-update-interval",
244 if (!s
->update_interval
) {
246 error_setg(errp
, "Invalid log superblock update interval %"PRIu64
,
254 bdrv_unref_child(bs
, s
->log_file
);
262 static void blk_log_writes_close(BlockDriverState
*bs
)
264 BDRVBlkLogWritesState
*s
= bs
->opaque
;
266 bdrv_unref_child(bs
, s
->log_file
);
270 static int64_t coroutine_fn
blk_log_writes_co_getlength(BlockDriverState
*bs
)
272 return bdrv_co_getlength(bs
->file
->bs
);
275 static void blk_log_writes_child_perm(BlockDriverState
*bs
, BdrvChild
*c
,
277 BlockReopenQueue
*ro_q
,
278 uint64_t perm
, uint64_t shrd
,
279 uint64_t *nperm
, uint64_t *nshrd
)
282 *nperm
= perm
& DEFAULT_PERM_PASSTHROUGH
;
283 *nshrd
= (shrd
& DEFAULT_PERM_PASSTHROUGH
) | DEFAULT_PERM_UNCHANGED
;
287 bdrv_default_perms(bs
, c
, role
, ro_q
, perm
, shrd
,
291 static void blk_log_writes_refresh_limits(BlockDriverState
*bs
, Error
**errp
)
293 BDRVBlkLogWritesState
*s
= bs
->opaque
;
294 bs
->bl
.request_alignment
= s
->sectorsize
;
297 static int coroutine_fn
298 blk_log_writes_co_preadv(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
299 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
301 return bdrv_co_preadv(bs
->file
, offset
, bytes
, qiov
, flags
);
304 typedef struct BlkLogWritesFileReq
{
305 BlockDriverState
*bs
;
310 int (*func
)(struct BlkLogWritesFileReq
*r
);
312 } BlkLogWritesFileReq
;
315 BlockDriverState
*bs
;
317 struct log_write_entry entry
;
320 } BlkLogWritesLogReq
;
322 static void coroutine_fn
blk_log_writes_co_do_log(BlkLogWritesLogReq
*lr
)
324 BDRVBlkLogWritesState
*s
= lr
->bs
->opaque
;
325 uint64_t cur_log_offset
= s
->cur_log_sector
<< s
->sectorbits
;
329 ROUND_UP(lr
->qiov
->size
, s
->sectorsize
) >> s
->sectorbits
;
331 lr
->log_ret
= bdrv_co_pwritev(s
->log_file
, cur_log_offset
, lr
->qiov
->size
,
334 /* Logging for the "write zeroes" operation */
335 if (lr
->log_ret
== 0 && lr
->zero_size
) {
336 cur_log_offset
= s
->cur_log_sector
<< s
->sectorbits
;
338 ROUND_UP(lr
->zero_size
, s
->sectorsize
) >> s
->sectorbits
;
340 lr
->log_ret
= bdrv_co_pwrite_zeroes(s
->log_file
, cur_log_offset
,
344 /* Update super block on flush or every update interval */
345 if (lr
->log_ret
== 0 && ((lr
->entry
.flags
& LOG_FLUSH_FLAG
)
346 || (s
->nr_entries
% s
->update_interval
== 0)))
348 struct log_write_super super
= {
349 .magic
= cpu_to_le64(WRITE_LOG_MAGIC
),
350 .version
= cpu_to_le64(WRITE_LOG_VERSION
),
351 .nr_entries
= cpu_to_le64(s
->nr_entries
),
352 .sectorsize
= cpu_to_le32(s
->sectorsize
),
354 void *zeroes
= g_malloc0(s
->sectorsize
- sizeof(super
));
357 qemu_iovec_init(&qiov
, 2);
358 qemu_iovec_add(&qiov
, &super
, sizeof(super
));
359 qemu_iovec_add(&qiov
, zeroes
, s
->sectorsize
- sizeof(super
));
362 bdrv_co_pwritev(s
->log_file
, 0, s
->sectorsize
, &qiov
, 0);
363 if (lr
->log_ret
== 0) {
364 lr
->log_ret
= bdrv_co_flush(s
->log_file
->bs
);
366 qemu_iovec_destroy(&qiov
);
371 static void coroutine_fn
blk_log_writes_co_do_file(BlkLogWritesFileReq
*fr
)
373 fr
->file_ret
= fr
->func(fr
);
376 static int coroutine_fn
377 blk_log_writes_co_log(BlockDriverState
*bs
, uint64_t offset
, uint64_t bytes
,
378 QEMUIOVector
*qiov
, int flags
,
379 int (*file_func
)(BlkLogWritesFileReq
*r
),
380 uint64_t entry_flags
, bool is_zero_write
)
382 QEMUIOVector log_qiov
;
383 size_t niov
= qiov
? qiov
->niov
: 0;
384 BDRVBlkLogWritesState
*s
= bs
->opaque
;
385 BlkLogWritesFileReq fr
= {
393 BlkLogWritesLogReq lr
= {
397 .sector
= cpu_to_le64(offset
>> s
->sectorbits
),
398 .nr_sectors
= cpu_to_le64(bytes
>> s
->sectorbits
),
399 .flags
= cpu_to_le64(entry_flags
),
402 .zero_size
= is_zero_write
? bytes
: 0,
404 void *zeroes
= g_malloc0(s
->sectorsize
- sizeof(lr
.entry
));
406 assert((1 << s
->sectorbits
) == s
->sectorsize
);
407 assert(bs
->bl
.request_alignment
== s
->sectorsize
);
408 assert(QEMU_IS_ALIGNED(offset
, bs
->bl
.request_alignment
));
409 assert(QEMU_IS_ALIGNED(bytes
, bs
->bl
.request_alignment
));
411 qemu_iovec_init(&log_qiov
, niov
+ 2);
412 qemu_iovec_add(&log_qiov
, &lr
.entry
, sizeof(lr
.entry
));
413 qemu_iovec_add(&log_qiov
, zeroes
, s
->sectorsize
- sizeof(lr
.entry
));
415 qemu_iovec_concat(&log_qiov
, qiov
, 0, qiov
->size
);
418 blk_log_writes_co_do_file(&fr
);
419 blk_log_writes_co_do_log(&lr
);
421 qemu_iovec_destroy(&log_qiov
);
424 if (lr
.log_ret
< 0) {
431 static int coroutine_fn
432 blk_log_writes_co_do_file_pwritev(BlkLogWritesFileReq
*fr
)
434 return bdrv_co_pwritev(fr
->bs
->file
, fr
->offset
, fr
->bytes
,
435 fr
->qiov
, fr
->file_flags
);
438 static int coroutine_fn
439 blk_log_writes_co_do_file_pwrite_zeroes(BlkLogWritesFileReq
*fr
)
441 return bdrv_co_pwrite_zeroes(fr
->bs
->file
, fr
->offset
, fr
->bytes
,
445 static int coroutine_fn
blk_log_writes_co_do_file_flush(BlkLogWritesFileReq
*fr
)
447 return bdrv_co_flush(fr
->bs
->file
->bs
);
450 static int coroutine_fn
451 blk_log_writes_co_do_file_pdiscard(BlkLogWritesFileReq
*fr
)
453 return bdrv_co_pdiscard(fr
->bs
->file
, fr
->offset
, fr
->bytes
);
456 static int coroutine_fn
457 blk_log_writes_co_pwritev(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
,
458 QEMUIOVector
*qiov
, BdrvRequestFlags flags
)
460 return blk_log_writes_co_log(bs
, offset
, bytes
, qiov
, flags
,
461 blk_log_writes_co_do_file_pwritev
, 0, false);
464 static int coroutine_fn
465 blk_log_writes_co_pwrite_zeroes(BlockDriverState
*bs
, int64_t offset
,
466 int64_t bytes
, BdrvRequestFlags flags
)
468 return blk_log_writes_co_log(bs
, offset
, bytes
, NULL
, flags
,
469 blk_log_writes_co_do_file_pwrite_zeroes
, 0,
473 static int coroutine_fn
blk_log_writes_co_flush_to_disk(BlockDriverState
*bs
)
475 return blk_log_writes_co_log(bs
, 0, 0, NULL
, 0,
476 blk_log_writes_co_do_file_flush
,
477 LOG_FLUSH_FLAG
, false);
480 static int coroutine_fn
481 blk_log_writes_co_pdiscard(BlockDriverState
*bs
, int64_t offset
, int64_t bytes
)
483 return blk_log_writes_co_log(bs
, offset
, bytes
, NULL
, 0,
484 blk_log_writes_co_do_file_pdiscard
,
485 LOG_DISCARD_FLAG
, false);
488 static const char *const blk_log_writes_strong_runtime_opts
[] = {
495 static BlockDriver bdrv_blk_log_writes
= {
496 .format_name
= "blklogwrites",
497 .instance_size
= sizeof(BDRVBlkLogWritesState
),
499 .bdrv_open
= blk_log_writes_open
,
500 .bdrv_close
= blk_log_writes_close
,
501 .bdrv_co_getlength
= blk_log_writes_co_getlength
,
502 .bdrv_child_perm
= blk_log_writes_child_perm
,
503 .bdrv_refresh_limits
= blk_log_writes_refresh_limits
,
505 .bdrv_co_preadv
= blk_log_writes_co_preadv
,
506 .bdrv_co_pwritev
= blk_log_writes_co_pwritev
,
507 .bdrv_co_pwrite_zeroes
= blk_log_writes_co_pwrite_zeroes
,
508 .bdrv_co_flush_to_disk
= blk_log_writes_co_flush_to_disk
,
509 .bdrv_co_pdiscard
= blk_log_writes_co_pdiscard
,
512 .strong_runtime_opts
= blk_log_writes_strong_runtime_opts
,
515 static void bdrv_blk_log_writes_init(void)
517 bdrv_register(&bdrv_blk_log_writes
);
520 block_init(bdrv_blk_log_writes_init
);