block-backend: Retain permissions after migration
[qemu.git] / block / blklogwrites.c
blobf7a251e91f9eff96c434672711a74c7df0d56084
1 /*
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/module.h"
20 #include "qemu/option.h"
22 /* Disk format stuff - taken from Linux drivers/md/dm-log-writes.c */
24 #define LOG_FLUSH_FLAG (1 << 0)
25 #define LOG_FUA_FLAG (1 << 1)
26 #define LOG_DISCARD_FLAG (1 << 2)
27 #define LOG_MARK_FLAG (1 << 3)
28 #define LOG_FLAG_MASK (LOG_FLUSH_FLAG \
29 | LOG_FUA_FLAG \
30 | LOG_DISCARD_FLAG \
31 | LOG_MARK_FLAG)
33 #define WRITE_LOG_VERSION 1ULL
34 #define WRITE_LOG_MAGIC 0x6a736677736872ULL
36 /* All fields are little-endian. */
37 struct log_write_super {
38 uint64_t magic;
39 uint64_t version;
40 uint64_t nr_entries;
41 uint32_t sectorsize;
42 } QEMU_PACKED;
44 struct log_write_entry {
45 uint64_t sector;
46 uint64_t nr_sectors;
47 uint64_t flags;
48 uint64_t data_len;
49 } QEMU_PACKED;
51 /* End of disk format structures. */
53 typedef struct {
54 BdrvChild *log_file;
55 uint32_t sectorsize;
56 uint32_t sectorbits;
57 uint64_t cur_log_sector;
58 uint64_t nr_entries;
59 uint64_t update_interval;
60 } BDRVBlkLogWritesState;
62 static QemuOptsList runtime_opts = {
63 .name = "blklogwrites",
64 .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
65 .desc = {
67 .name = "log-append",
68 .type = QEMU_OPT_BOOL,
69 .help = "Append to an existing log",
72 .name = "log-sector-size",
73 .type = QEMU_OPT_SIZE,
74 .help = "Log sector size",
77 .name = "log-super-update-interval",
78 .type = QEMU_OPT_NUMBER,
79 .help = "Log superblock update interval (# of write requests)",
81 { /* end of list */ }
85 static inline uint32_t blk_log_writes_log2(uint32_t value)
87 assert(value > 0);
88 return 31 - clz32(value);
91 static inline bool blk_log_writes_sector_size_valid(uint32_t sector_size)
93 return is_power_of_2(sector_size) &&
94 sector_size >= sizeof(struct log_write_super) &&
95 sector_size >= sizeof(struct log_write_entry) &&
96 sector_size < (1ull << 24);
99 static uint64_t blk_log_writes_find_cur_log_sector(BdrvChild *log,
100 uint32_t sector_size,
101 uint64_t nr_entries,
102 Error **errp)
104 uint64_t cur_sector = 1;
105 uint64_t cur_idx = 0;
106 uint32_t sector_bits = blk_log_writes_log2(sector_size);
107 struct log_write_entry cur_entry;
109 while (cur_idx < nr_entries) {
110 int read_ret = bdrv_pread(log, cur_sector << sector_bits, &cur_entry,
111 sizeof(cur_entry));
112 if (read_ret < 0) {
113 error_setg_errno(errp, -read_ret,
114 "Failed to read log entry %"PRIu64, cur_idx);
115 return (uint64_t)-1ull;
118 if (cur_entry.flags & ~cpu_to_le64(LOG_FLAG_MASK)) {
119 error_setg(errp, "Invalid flags 0x%"PRIx64" in log entry %"PRIu64,
120 le64_to_cpu(cur_entry.flags), cur_idx);
121 return (uint64_t)-1ull;
124 /* Account for the sector of the entry itself */
125 ++cur_sector;
128 * Account for the data of the write.
129 * For discards, this data is not present.
131 if (!(cur_entry.flags & cpu_to_le64(LOG_DISCARD_FLAG))) {
132 cur_sector += le64_to_cpu(cur_entry.nr_sectors);
135 ++cur_idx;
138 return cur_sector;
141 static int blk_log_writes_open(BlockDriverState *bs, QDict *options, int flags,
142 Error **errp)
144 BDRVBlkLogWritesState *s = bs->opaque;
145 QemuOpts *opts;
146 Error *local_err = NULL;
147 int ret;
148 uint64_t log_sector_size;
149 bool log_append;
151 opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
152 if (!qemu_opts_absorb_qdict(opts, options, errp)) {
153 ret = -EINVAL;
154 goto fail;
157 /* Open the file */
158 bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
159 BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, false,
160 errp);
161 if (!bs->file) {
162 ret = -EINVAL;
163 goto fail;
166 /* Open the log file */
167 s->log_file = bdrv_open_child(NULL, options, "log", bs, &child_of_bds,
168 BDRV_CHILD_METADATA, false, errp);
169 if (!s->log_file) {
170 ret = -EINVAL;
171 goto fail;
174 log_append = qemu_opt_get_bool(opts, "log-append", false);
176 if (log_append) {
177 struct log_write_super log_sb = { 0, 0, 0, 0 };
179 if (qemu_opt_find(opts, "log-sector-size")) {
180 ret = -EINVAL;
181 error_setg(errp, "log-append and log-sector-size are mutually "
182 "exclusive");
183 goto fail_log;
186 /* Read log superblock or fake one for an empty log */
187 if (!bdrv_getlength(s->log_file->bs)) {
188 log_sb.magic = cpu_to_le64(WRITE_LOG_MAGIC);
189 log_sb.version = cpu_to_le64(WRITE_LOG_VERSION);
190 log_sb.nr_entries = cpu_to_le64(0);
191 log_sb.sectorsize = cpu_to_le32(BDRV_SECTOR_SIZE);
192 } else {
193 ret = bdrv_pread(s->log_file, 0, &log_sb, sizeof(log_sb));
194 if (ret < 0) {
195 error_setg_errno(errp, -ret, "Could not read log superblock");
196 goto fail_log;
200 if (log_sb.magic != cpu_to_le64(WRITE_LOG_MAGIC)) {
201 ret = -EINVAL;
202 error_setg(errp, "Invalid log superblock magic");
203 goto fail_log;
206 if (log_sb.version != cpu_to_le64(WRITE_LOG_VERSION)) {
207 ret = -EINVAL;
208 error_setg(errp, "Unsupported log version %"PRIu64,
209 le64_to_cpu(log_sb.version));
210 goto fail_log;
213 log_sector_size = le32_to_cpu(log_sb.sectorsize);
214 s->cur_log_sector = 1;
215 s->nr_entries = 0;
217 if (blk_log_writes_sector_size_valid(log_sector_size)) {
218 s->cur_log_sector =
219 blk_log_writes_find_cur_log_sector(s->log_file, log_sector_size,
220 le64_to_cpu(log_sb.nr_entries), &local_err);
221 if (local_err) {
222 ret = -EINVAL;
223 error_propagate(errp, local_err);
224 goto fail_log;
227 s->nr_entries = le64_to_cpu(log_sb.nr_entries);
229 } else {
230 log_sector_size = qemu_opt_get_size(opts, "log-sector-size",
231 BDRV_SECTOR_SIZE);
232 s->cur_log_sector = 1;
233 s->nr_entries = 0;
236 if (!blk_log_writes_sector_size_valid(log_sector_size)) {
237 ret = -EINVAL;
238 error_setg(errp, "Invalid log sector size %"PRIu64, log_sector_size);
239 goto fail_log;
242 s->sectorsize = log_sector_size;
243 s->sectorbits = blk_log_writes_log2(log_sector_size);
244 s->update_interval = qemu_opt_get_number(opts, "log-super-update-interval",
245 4096);
246 if (!s->update_interval) {
247 ret = -EINVAL;
248 error_setg(errp, "Invalid log superblock update interval %"PRIu64,
249 s->update_interval);
250 goto fail_log;
253 ret = 0;
254 fail_log:
255 if (ret < 0) {
256 bdrv_unref_child(bs, s->log_file);
257 s->log_file = NULL;
259 fail:
260 if (ret < 0) {
261 bdrv_unref_child(bs, bs->file);
262 bs->file = NULL;
264 qemu_opts_del(opts);
265 return ret;
268 static void blk_log_writes_close(BlockDriverState *bs)
270 BDRVBlkLogWritesState *s = bs->opaque;
272 bdrv_unref_child(bs, s->log_file);
273 s->log_file = NULL;
276 static int64_t blk_log_writes_getlength(BlockDriverState *bs)
278 return bdrv_getlength(bs->file->bs);
281 static void blk_log_writes_child_perm(BlockDriverState *bs, BdrvChild *c,
282 BdrvChildRole role,
283 BlockReopenQueue *ro_q,
284 uint64_t perm, uint64_t shrd,
285 uint64_t *nperm, uint64_t *nshrd)
287 if (!c) {
288 *nperm = perm & DEFAULT_PERM_PASSTHROUGH;
289 *nshrd = (shrd & DEFAULT_PERM_PASSTHROUGH) | DEFAULT_PERM_UNCHANGED;
290 return;
293 bdrv_default_perms(bs, c, role, ro_q, perm, shrd,
294 nperm, nshrd);
297 static void blk_log_writes_refresh_limits(BlockDriverState *bs, Error **errp)
299 BDRVBlkLogWritesState *s = bs->opaque;
300 bs->bl.request_alignment = s->sectorsize;
303 static int coroutine_fn
304 blk_log_writes_co_preadv(BlockDriverState *bs, int64_t offset, int64_t bytes,
305 QEMUIOVector *qiov, BdrvRequestFlags flags)
307 return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
310 typedef struct BlkLogWritesFileReq {
311 BlockDriverState *bs;
312 uint64_t offset;
313 uint64_t bytes;
314 int file_flags;
315 QEMUIOVector *qiov;
316 int (*func)(struct BlkLogWritesFileReq *r);
317 int file_ret;
318 } BlkLogWritesFileReq;
320 typedef struct {
321 BlockDriverState *bs;
322 QEMUIOVector *qiov;
323 struct log_write_entry entry;
324 uint64_t zero_size;
325 int log_ret;
326 } BlkLogWritesLogReq;
328 static void coroutine_fn blk_log_writes_co_do_log(BlkLogWritesLogReq *lr)
330 BDRVBlkLogWritesState *s = lr->bs->opaque;
331 uint64_t cur_log_offset = s->cur_log_sector << s->sectorbits;
333 s->nr_entries++;
334 s->cur_log_sector +=
335 ROUND_UP(lr->qiov->size, s->sectorsize) >> s->sectorbits;
337 lr->log_ret = bdrv_co_pwritev(s->log_file, cur_log_offset, lr->qiov->size,
338 lr->qiov, 0);
340 /* Logging for the "write zeroes" operation */
341 if (lr->log_ret == 0 && lr->zero_size) {
342 cur_log_offset = s->cur_log_sector << s->sectorbits;
343 s->cur_log_sector +=
344 ROUND_UP(lr->zero_size, s->sectorsize) >> s->sectorbits;
346 lr->log_ret = bdrv_co_pwrite_zeroes(s->log_file, cur_log_offset,
347 lr->zero_size, 0);
350 /* Update super block on flush or every update interval */
351 if (lr->log_ret == 0 && ((lr->entry.flags & LOG_FLUSH_FLAG)
352 || (s->nr_entries % s->update_interval == 0)))
354 struct log_write_super super = {
355 .magic = cpu_to_le64(WRITE_LOG_MAGIC),
356 .version = cpu_to_le64(WRITE_LOG_VERSION),
357 .nr_entries = cpu_to_le64(s->nr_entries),
358 .sectorsize = cpu_to_le32(s->sectorsize),
360 void *zeroes = g_malloc0(s->sectorsize - sizeof(super));
361 QEMUIOVector qiov;
363 qemu_iovec_init(&qiov, 2);
364 qemu_iovec_add(&qiov, &super, sizeof(super));
365 qemu_iovec_add(&qiov, zeroes, s->sectorsize - sizeof(super));
367 lr->log_ret =
368 bdrv_co_pwritev(s->log_file, 0, s->sectorsize, &qiov, 0);
369 if (lr->log_ret == 0) {
370 lr->log_ret = bdrv_co_flush(s->log_file->bs);
372 qemu_iovec_destroy(&qiov);
373 g_free(zeroes);
377 static void coroutine_fn blk_log_writes_co_do_file(BlkLogWritesFileReq *fr)
379 fr->file_ret = fr->func(fr);
382 static int coroutine_fn
383 blk_log_writes_co_log(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
384 QEMUIOVector *qiov, int flags,
385 int (*file_func)(BlkLogWritesFileReq *r),
386 uint64_t entry_flags, bool is_zero_write)
388 QEMUIOVector log_qiov;
389 size_t niov = qiov ? qiov->niov : 0;
390 BDRVBlkLogWritesState *s = bs->opaque;
391 BlkLogWritesFileReq fr = {
392 .bs = bs,
393 .offset = offset,
394 .bytes = bytes,
395 .file_flags = flags,
396 .qiov = qiov,
397 .func = file_func,
399 BlkLogWritesLogReq lr = {
400 .bs = bs,
401 .qiov = &log_qiov,
402 .entry = {
403 .sector = cpu_to_le64(offset >> s->sectorbits),
404 .nr_sectors = cpu_to_le64(bytes >> s->sectorbits),
405 .flags = cpu_to_le64(entry_flags),
406 .data_len = 0,
408 .zero_size = is_zero_write ? bytes : 0,
410 void *zeroes = g_malloc0(s->sectorsize - sizeof(lr.entry));
412 assert((1 << s->sectorbits) == s->sectorsize);
413 assert(bs->bl.request_alignment == s->sectorsize);
414 assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
415 assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
417 qemu_iovec_init(&log_qiov, niov + 2);
418 qemu_iovec_add(&log_qiov, &lr.entry, sizeof(lr.entry));
419 qemu_iovec_add(&log_qiov, zeroes, s->sectorsize - sizeof(lr.entry));
420 if (qiov) {
421 qemu_iovec_concat(&log_qiov, qiov, 0, qiov->size);
424 blk_log_writes_co_do_file(&fr);
425 blk_log_writes_co_do_log(&lr);
427 qemu_iovec_destroy(&log_qiov);
428 g_free(zeroes);
430 if (lr.log_ret < 0) {
431 return lr.log_ret;
434 return fr.file_ret;
437 static int coroutine_fn
438 blk_log_writes_co_do_file_pwritev(BlkLogWritesFileReq *fr)
440 return bdrv_co_pwritev(fr->bs->file, fr->offset, fr->bytes,
441 fr->qiov, fr->file_flags);
444 static int coroutine_fn
445 blk_log_writes_co_do_file_pwrite_zeroes(BlkLogWritesFileReq *fr)
447 return bdrv_co_pwrite_zeroes(fr->bs->file, fr->offset, fr->bytes,
448 fr->file_flags);
451 static int coroutine_fn blk_log_writes_co_do_file_flush(BlkLogWritesFileReq *fr)
453 return bdrv_co_flush(fr->bs->file->bs);
456 static int coroutine_fn
457 blk_log_writes_co_do_file_pdiscard(BlkLogWritesFileReq *fr)
459 return bdrv_co_pdiscard(fr->bs->file, fr->offset, fr->bytes);
462 static int coroutine_fn
463 blk_log_writes_co_pwritev(BlockDriverState *bs, int64_t offset, int64_t bytes,
464 QEMUIOVector *qiov, BdrvRequestFlags flags)
466 return blk_log_writes_co_log(bs, offset, bytes, qiov, flags,
467 blk_log_writes_co_do_file_pwritev, 0, false);
470 static int coroutine_fn
471 blk_log_writes_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset,
472 int64_t bytes, BdrvRequestFlags flags)
474 return blk_log_writes_co_log(bs, offset, bytes, NULL, flags,
475 blk_log_writes_co_do_file_pwrite_zeroes, 0,
476 true);
479 static int coroutine_fn blk_log_writes_co_flush_to_disk(BlockDriverState *bs)
481 return blk_log_writes_co_log(bs, 0, 0, NULL, 0,
482 blk_log_writes_co_do_file_flush,
483 LOG_FLUSH_FLAG, false);
486 static int coroutine_fn
487 blk_log_writes_co_pdiscard(BlockDriverState *bs, int64_t offset, int64_t bytes)
489 return blk_log_writes_co_log(bs, offset, bytes, NULL, 0,
490 blk_log_writes_co_do_file_pdiscard,
491 LOG_DISCARD_FLAG, false);
494 static const char *const blk_log_writes_strong_runtime_opts[] = {
495 "log-append",
496 "log-sector-size",
498 NULL
501 static BlockDriver bdrv_blk_log_writes = {
502 .format_name = "blklogwrites",
503 .instance_size = sizeof(BDRVBlkLogWritesState),
505 .bdrv_open = blk_log_writes_open,
506 .bdrv_close = blk_log_writes_close,
507 .bdrv_getlength = blk_log_writes_getlength,
508 .bdrv_child_perm = blk_log_writes_child_perm,
509 .bdrv_refresh_limits = blk_log_writes_refresh_limits,
511 .bdrv_co_preadv = blk_log_writes_co_preadv,
512 .bdrv_co_pwritev = blk_log_writes_co_pwritev,
513 .bdrv_co_pwrite_zeroes = blk_log_writes_co_pwrite_zeroes,
514 .bdrv_co_flush_to_disk = blk_log_writes_co_flush_to_disk,
515 .bdrv_co_pdiscard = blk_log_writes_co_pdiscard,
517 .is_filter = true,
518 .strong_runtime_opts = blk_log_writes_strong_runtime_opts,
521 static void bdrv_blk_log_writes_init(void)
523 bdrv_register(&bdrv_blk_log_writes);
526 block_init(bdrv_blk_log_writes_init);