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