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