xref: /openbmc/qemu/block/blklogwrites.c (revision 06d4c71f)
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 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_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"
21 
22 /* Disk format stuff - taken from Linux drivers/md/dm-log-writes.c */
23 
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)
32 
33 #define WRITE_LOG_VERSION 1ULL
34 #define WRITE_LOG_MAGIC 0x6a736677736872ULL
35 
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;
43 
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;
50 
51 /* End of disk format structures. */
52 
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;
61 
62 static QemuOptsList runtime_opts = {
63     .name = "blklogwrites",
64     .head = QTAILQ_HEAD_INITIALIZER(runtime_opts.head),
65     .desc = {
66         {
67             .name = "log-append",
68             .type = QEMU_OPT_BOOL,
69             .help = "Append to an existing log",
70         },
71         {
72             .name = "log-sector-size",
73             .type = QEMU_OPT_SIZE,
74             .help = "Log sector size",
75         },
76         {
77             .name = "log-super-update-interval",
78             .type = QEMU_OPT_NUMBER,
79             .help = "Log superblock update interval (# of write requests)",
80         },
81         { /* end of list */ }
82     },
83 };
84 
85 static inline uint32_t blk_log_writes_log2(uint32_t value)
86 {
87     assert(value > 0);
88     return 31 - clz32(value);
89 }
90 
91 static inline bool blk_log_writes_sector_size_valid(uint32_t sector_size)
92 {
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);
97 }
98 
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)
103 {
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;
108 
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;
116         }
117 
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;
122         }
123 
124         /* Account for the sector of the entry itself */
125         ++cur_sector;
126 
127         /*
128          * Account for the data of the write.
129          * For discards, this data is not present.
130          */
131         if (!(cur_entry.flags & cpu_to_le64(LOG_DISCARD_FLAG))) {
132             cur_sector += le64_to_cpu(cur_entry.nr_sectors);
133         }
134 
135         ++cur_idx;
136     }
137 
138     return cur_sector;
139 }
140 
141 static int blk_log_writes_open(BlockDriverState *bs, QDict *options, int flags,
142                                Error **errp)
143 {
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;
150 
151     opts = qemu_opts_create(&runtime_opts, NULL, 0, &error_abort);
152     qemu_opts_absorb_qdict(opts, options, &local_err);
153     if (local_err) {
154         ret = -EINVAL;
155         error_propagate(errp, local_err);
156         goto fail;
157     }
158 
159     /* Open the file */
160     bs->file = bdrv_open_child(NULL, options, "file", bs, &child_of_bds,
161                                BDRV_CHILD_FILTERED | BDRV_CHILD_PRIMARY, false,
162                                &local_err);
163     if (local_err) {
164         ret = -EINVAL;
165         error_propagate(errp, local_err);
166         goto fail;
167     }
168 
169     /* Open the log file */
170     s->log_file = bdrv_open_child(NULL, options, "log", bs, &child_of_bds,
171                                   BDRV_CHILD_METADATA, false, &local_err);
172     if (local_err) {
173         ret = -EINVAL;
174         error_propagate(errp, local_err);
175         goto fail;
176     }
177 
178     log_append = qemu_opt_get_bool(opts, "log-append", false);
179 
180     if (log_append) {
181         struct log_write_super log_sb = { 0, 0, 0, 0 };
182 
183         if (qemu_opt_find(opts, "log-sector-size")) {
184             ret = -EINVAL;
185             error_setg(errp, "log-append and log-sector-size are mutually "
186                        "exclusive");
187             goto fail_log;
188         }
189 
190         /* Read log superblock or fake one for an empty log */
191         if (!bdrv_getlength(s->log_file->bs)) {
192             log_sb.magic      = cpu_to_le64(WRITE_LOG_MAGIC);
193             log_sb.version    = cpu_to_le64(WRITE_LOG_VERSION);
194             log_sb.nr_entries = cpu_to_le64(0);
195             log_sb.sectorsize = cpu_to_le32(BDRV_SECTOR_SIZE);
196         } else {
197             ret = bdrv_pread(s->log_file, 0, &log_sb, sizeof(log_sb));
198             if (ret < 0) {
199                 error_setg_errno(errp, -ret, "Could not read log superblock");
200                 goto fail_log;
201             }
202         }
203 
204         if (log_sb.magic != cpu_to_le64(WRITE_LOG_MAGIC)) {
205             ret = -EINVAL;
206             error_setg(errp, "Invalid log superblock magic");
207             goto fail_log;
208         }
209 
210         if (log_sb.version != cpu_to_le64(WRITE_LOG_VERSION)) {
211             ret = -EINVAL;
212             error_setg(errp, "Unsupported log version %"PRIu64,
213                        le64_to_cpu(log_sb.version));
214             goto fail_log;
215         }
216 
217         log_sector_size = le32_to_cpu(log_sb.sectorsize);
218         s->cur_log_sector = 1;
219         s->nr_entries = 0;
220 
221         if (blk_log_writes_sector_size_valid(log_sector_size)) {
222             s->cur_log_sector =
223                 blk_log_writes_find_cur_log_sector(s->log_file, log_sector_size,
224                                     le64_to_cpu(log_sb.nr_entries), &local_err);
225             if (local_err) {
226                 ret = -EINVAL;
227                 error_propagate(errp, local_err);
228                 goto fail_log;
229             }
230 
231             s->nr_entries = le64_to_cpu(log_sb.nr_entries);
232         }
233     } else {
234         log_sector_size = qemu_opt_get_size(opts, "log-sector-size",
235                                             BDRV_SECTOR_SIZE);
236         s->cur_log_sector = 1;
237         s->nr_entries = 0;
238     }
239 
240     if (!blk_log_writes_sector_size_valid(log_sector_size)) {
241         ret = -EINVAL;
242         error_setg(errp, "Invalid log sector size %"PRIu64, log_sector_size);
243         goto fail_log;
244     }
245 
246     s->sectorsize = log_sector_size;
247     s->sectorbits = blk_log_writes_log2(log_sector_size);
248     s->update_interval = qemu_opt_get_number(opts, "log-super-update-interval",
249                                              4096);
250     if (!s->update_interval) {
251         ret = -EINVAL;
252         error_setg(errp, "Invalid log superblock update interval %"PRIu64,
253                    s->update_interval);
254         goto fail_log;
255     }
256 
257     ret = 0;
258 fail_log:
259     if (ret < 0) {
260         bdrv_unref_child(bs, s->log_file);
261         s->log_file = NULL;
262     }
263 fail:
264     if (ret < 0) {
265         bdrv_unref_child(bs, bs->file);
266         bs->file = NULL;
267     }
268     qemu_opts_del(opts);
269     return ret;
270 }
271 
272 static void blk_log_writes_close(BlockDriverState *bs)
273 {
274     BDRVBlkLogWritesState *s = bs->opaque;
275 
276     bdrv_unref_child(bs, s->log_file);
277     s->log_file = NULL;
278 }
279 
280 static int64_t blk_log_writes_getlength(BlockDriverState *bs)
281 {
282     return bdrv_getlength(bs->file->bs);
283 }
284 
285 static void blk_log_writes_child_perm(BlockDriverState *bs, BdrvChild *c,
286                                       BdrvChildRole role,
287                                       BlockReopenQueue *ro_q,
288                                       uint64_t perm, uint64_t shrd,
289                                       uint64_t *nperm, uint64_t *nshrd)
290 {
291     if (!c) {
292         *nperm = perm & DEFAULT_PERM_PASSTHROUGH;
293         *nshrd = (shrd & DEFAULT_PERM_PASSTHROUGH) | DEFAULT_PERM_UNCHANGED;
294         return;
295     }
296 
297     bdrv_default_perms(bs, c, role, ro_q, perm, shrd,
298                        nperm, nshrd);
299 }
300 
301 static void blk_log_writes_refresh_limits(BlockDriverState *bs, Error **errp)
302 {
303     BDRVBlkLogWritesState *s = bs->opaque;
304     bs->bl.request_alignment = s->sectorsize;
305 }
306 
307 static int coroutine_fn
308 blk_log_writes_co_preadv(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
309                          QEMUIOVector *qiov, int flags)
310 {
311     return bdrv_co_preadv(bs->file, offset, bytes, qiov, flags);
312 }
313 
314 typedef struct BlkLogWritesFileReq {
315     BlockDriverState *bs;
316     uint64_t offset;
317     uint64_t bytes;
318     int file_flags;
319     QEMUIOVector *qiov;
320     int (*func)(struct BlkLogWritesFileReq *r);
321     int file_ret;
322 } BlkLogWritesFileReq;
323 
324 typedef struct {
325     BlockDriverState *bs;
326     QEMUIOVector *qiov;
327     struct log_write_entry entry;
328     uint64_t zero_size;
329     int log_ret;
330 } BlkLogWritesLogReq;
331 
332 static void coroutine_fn blk_log_writes_co_do_log(BlkLogWritesLogReq *lr)
333 {
334     BDRVBlkLogWritesState *s = lr->bs->opaque;
335     uint64_t cur_log_offset = s->cur_log_sector << s->sectorbits;
336 
337     s->nr_entries++;
338     s->cur_log_sector +=
339             ROUND_UP(lr->qiov->size, s->sectorsize) >> s->sectorbits;
340 
341     lr->log_ret = bdrv_co_pwritev(s->log_file, cur_log_offset, lr->qiov->size,
342                                   lr->qiov, 0);
343 
344     /* Logging for the "write zeroes" operation */
345     if (lr->log_ret == 0 && lr->zero_size) {
346         cur_log_offset = s->cur_log_sector << s->sectorbits;
347         s->cur_log_sector +=
348                 ROUND_UP(lr->zero_size, s->sectorsize) >> s->sectorbits;
349 
350         lr->log_ret = bdrv_co_pwrite_zeroes(s->log_file, cur_log_offset,
351                                             lr->zero_size, 0);
352     }
353 
354     /* Update super block on flush or every update interval */
355     if (lr->log_ret == 0 && ((lr->entry.flags & LOG_FLUSH_FLAG)
356         || (s->nr_entries % s->update_interval == 0)))
357     {
358         struct log_write_super super = {
359             .magic      = cpu_to_le64(WRITE_LOG_MAGIC),
360             .version    = cpu_to_le64(WRITE_LOG_VERSION),
361             .nr_entries = cpu_to_le64(s->nr_entries),
362             .sectorsize = cpu_to_le32(s->sectorsize),
363         };
364         void *zeroes = g_malloc0(s->sectorsize - sizeof(super));
365         QEMUIOVector qiov;
366 
367         qemu_iovec_init(&qiov, 2);
368         qemu_iovec_add(&qiov, &super, sizeof(super));
369         qemu_iovec_add(&qiov, zeroes, s->sectorsize - sizeof(super));
370 
371         lr->log_ret =
372             bdrv_co_pwritev(s->log_file, 0, s->sectorsize, &qiov, 0);
373         if (lr->log_ret == 0) {
374             lr->log_ret = bdrv_co_flush(s->log_file->bs);
375         }
376         qemu_iovec_destroy(&qiov);
377         g_free(zeroes);
378     }
379 }
380 
381 static void coroutine_fn blk_log_writes_co_do_file(BlkLogWritesFileReq *fr)
382 {
383     fr->file_ret = fr->func(fr);
384 }
385 
386 static int coroutine_fn
387 blk_log_writes_co_log(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
388                       QEMUIOVector *qiov, int flags,
389                       int (*file_func)(BlkLogWritesFileReq *r),
390                       uint64_t entry_flags, bool is_zero_write)
391 {
392     QEMUIOVector log_qiov;
393     size_t niov = qiov ? qiov->niov : 0;
394     BDRVBlkLogWritesState *s = bs->opaque;
395     BlkLogWritesFileReq fr = {
396         .bs         = bs,
397         .offset     = offset,
398         .bytes      = bytes,
399         .file_flags = flags,
400         .qiov       = qiov,
401         .func       = file_func,
402     };
403     BlkLogWritesLogReq lr = {
404         .bs             = bs,
405         .qiov           = &log_qiov,
406         .entry = {
407             .sector     = cpu_to_le64(offset >> s->sectorbits),
408             .nr_sectors = cpu_to_le64(bytes >> s->sectorbits),
409             .flags      = cpu_to_le64(entry_flags),
410             .data_len   = 0,
411         },
412         .zero_size = is_zero_write ? bytes : 0,
413     };
414     void *zeroes = g_malloc0(s->sectorsize - sizeof(lr.entry));
415 
416     assert((1 << s->sectorbits) == s->sectorsize);
417     assert(bs->bl.request_alignment == s->sectorsize);
418     assert(QEMU_IS_ALIGNED(offset, bs->bl.request_alignment));
419     assert(QEMU_IS_ALIGNED(bytes, bs->bl.request_alignment));
420 
421     qemu_iovec_init(&log_qiov, niov + 2);
422     qemu_iovec_add(&log_qiov, &lr.entry, sizeof(lr.entry));
423     qemu_iovec_add(&log_qiov, zeroes, s->sectorsize - sizeof(lr.entry));
424     if (qiov) {
425         qemu_iovec_concat(&log_qiov, qiov, 0, qiov->size);
426     }
427 
428     blk_log_writes_co_do_file(&fr);
429     blk_log_writes_co_do_log(&lr);
430 
431     qemu_iovec_destroy(&log_qiov);
432     g_free(zeroes);
433 
434     if (lr.log_ret < 0) {
435         return lr.log_ret;
436     }
437 
438     return fr.file_ret;
439 }
440 
441 static int coroutine_fn
442 blk_log_writes_co_do_file_pwritev(BlkLogWritesFileReq *fr)
443 {
444     return bdrv_co_pwritev(fr->bs->file, fr->offset, fr->bytes,
445                            fr->qiov, fr->file_flags);
446 }
447 
448 static int coroutine_fn
449 blk_log_writes_co_do_file_pwrite_zeroes(BlkLogWritesFileReq *fr)
450 {
451     return bdrv_co_pwrite_zeroes(fr->bs->file, fr->offset, fr->bytes,
452                                  fr->file_flags);
453 }
454 
455 static int coroutine_fn blk_log_writes_co_do_file_flush(BlkLogWritesFileReq *fr)
456 {
457     return bdrv_co_flush(fr->bs->file->bs);
458 }
459 
460 static int coroutine_fn
461 blk_log_writes_co_do_file_pdiscard(BlkLogWritesFileReq *fr)
462 {
463     return bdrv_co_pdiscard(fr->bs->file, fr->offset, fr->bytes);
464 }
465 
466 static int coroutine_fn
467 blk_log_writes_co_pwritev(BlockDriverState *bs, uint64_t offset, uint64_t bytes,
468                           QEMUIOVector *qiov, int flags)
469 {
470     return blk_log_writes_co_log(bs, offset, bytes, qiov, flags,
471                                  blk_log_writes_co_do_file_pwritev, 0, false);
472 }
473 
474 static int coroutine_fn
475 blk_log_writes_co_pwrite_zeroes(BlockDriverState *bs, int64_t offset, int bytes,
476                                 BdrvRequestFlags flags)
477 {
478     return blk_log_writes_co_log(bs, offset, bytes, NULL, flags,
479                                  blk_log_writes_co_do_file_pwrite_zeroes, 0,
480                                  true);
481 }
482 
483 static int coroutine_fn blk_log_writes_co_flush_to_disk(BlockDriverState *bs)
484 {
485     return blk_log_writes_co_log(bs, 0, 0, NULL, 0,
486                                  blk_log_writes_co_do_file_flush,
487                                  LOG_FLUSH_FLAG, false);
488 }
489 
490 static int coroutine_fn
491 blk_log_writes_co_pdiscard(BlockDriverState *bs, int64_t offset, int count)
492 {
493     return blk_log_writes_co_log(bs, offset, count, NULL, 0,
494                                  blk_log_writes_co_do_file_pdiscard,
495                                  LOG_DISCARD_FLAG, false);
496 }
497 
498 static const char *const blk_log_writes_strong_runtime_opts[] = {
499     "log-append",
500     "log-sector-size",
501 
502     NULL
503 };
504 
505 static BlockDriver bdrv_blk_log_writes = {
506     .format_name            = "blklogwrites",
507     .instance_size          = sizeof(BDRVBlkLogWritesState),
508 
509     .bdrv_open              = blk_log_writes_open,
510     .bdrv_close             = blk_log_writes_close,
511     .bdrv_getlength         = blk_log_writes_getlength,
512     .bdrv_child_perm        = blk_log_writes_child_perm,
513     .bdrv_refresh_limits    = blk_log_writes_refresh_limits,
514 
515     .bdrv_co_preadv         = blk_log_writes_co_preadv,
516     .bdrv_co_pwritev        = blk_log_writes_co_pwritev,
517     .bdrv_co_pwrite_zeroes  = blk_log_writes_co_pwrite_zeroes,
518     .bdrv_co_flush_to_disk  = blk_log_writes_co_flush_to_disk,
519     .bdrv_co_pdiscard       = blk_log_writes_co_pdiscard,
520     .bdrv_co_block_status   = bdrv_co_block_status_from_file,
521 
522     .is_filter              = true,
523     .strong_runtime_opts    = blk_log_writes_strong_runtime_opts,
524 };
525 
526 static void bdrv_blk_log_writes_init(void)
527 {
528     bdrv_register(&bdrv_blk_log_writes);
529 }
530 
531 block_init(bdrv_blk_log_writes_init);
532