2 * Copyright (C) 2014 Facebook. All rights reserved.
4 * This file is released under the GPL.
7 #include <linux/device-mapper.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/blkdev.h>
12 #include <linux/bio.h>
13 #include <linux/slab.h>
14 #include <linux/kthread.h>
15 #include <linux/freezer.h>
17 #define DM_MSG_PREFIX "log-writes"
20 * This target will sequentially log all writes to the target device onto the
21 * log device. This is helpful for replaying writes to check for fs consistency
22 * at all times. This target provides a mechanism to mark specific events to
23 * check data at a later time. So for example you would:
27 * dmsetup message /dev/whatever mark mymark
30 * Then replay the log up to mymark and check the contents of the replay to
31 * verify it matches what was written.
33 * We log writes only after they have been flushed, this makes the log describe
34 * close to the order in which the data hits the actual disk, not its cache. So
35 * for example the following sequence (W means write, C means complete)
37 * Wa,Wb,Wc,Cc,Ca,FLUSH,FUAd,Cb,CFLUSH,CFUAd
39 * Would result in the log looking like this:
41 * c,a,flush,fuad,b,<other writes>,<next flush>
43 * This is meant to help expose problems where file systems do not properly wait
44 * on data being written before invoking a FLUSH. FUA bypasses cache so once it
45 * completes it is added to the log as it should be on disk.
47 * We treat DISCARDs as if they don't bypass cache so that they are logged in
48 * order of completion along with the normal writes. If we didn't do it this
49 * way we would process all the discards first and then write all the data, when
50 * in fact we want to do the data and the discard in the order that they
53 #define LOG_FLUSH_FLAG (1 << 0)
54 #define LOG_FUA_FLAG (1 << 1)
55 #define LOG_DISCARD_FLAG (1 << 2)
56 #define LOG_MARK_FLAG (1 << 3)
58 #define WRITE_LOG_VERSION 1ULL
59 #define WRITE_LOG_MAGIC 0x6a736677736872ULL
60 #define WRITE_LOG_SUPER_SECTOR 0
63 * The disk format for this is braindead simple.
65 * At byte 0 we have our super, followed by the following sequence for
68 * [ 1 sector ][ entry->nr_sectors ]
69 * [log_write_entry][ data written ]
71 * The log_write_entry takes up a full sector so we can have arbitrary length
72 * marks and it leaves us room for extra content in the future.
76 * Basic info about the log for userspace.
78 struct log_write_super {
86 * sector - the sector we wrote.
87 * nr_sectors - the number of sectors we wrote.
88 * flags - flags for this log entry.
89 * data_len - the size of the data in this log entry, this is for private log
90 * entry stuff, the MARK data provided by userspace for example.
92 struct log_write_entry {
101 struct dm_dev *logdev;
106 atomic_t pending_blocks;
107 sector_t next_sector;
109 bool logging_enabled;
110 bool device_supports_discard;
111 spinlock_t blocks_lock;
112 struct list_head unflushed_blocks;
113 struct list_head logging_blocks;
114 wait_queue_head_t wait;
115 struct task_struct *log_kthread;
116 struct completion super_done;
119 struct pending_block {
126 struct list_head list;
127 struct bio_vec vecs[0];
130 struct per_bio_data {
131 struct pending_block *block;
134 static inline sector_t bio_to_dev_sectors(struct log_writes_c *lc,
137 return sectors >> (lc->sectorshift - SECTOR_SHIFT);
140 static inline sector_t dev_to_bio_sectors(struct log_writes_c *lc,
143 return sectors << (lc->sectorshift - SECTOR_SHIFT);
146 static void put_pending_block(struct log_writes_c *lc)
148 if (atomic_dec_and_test(&lc->pending_blocks)) {
149 smp_mb__after_atomic();
150 if (waitqueue_active(&lc->wait))
155 static void put_io_block(struct log_writes_c *lc)
157 if (atomic_dec_and_test(&lc->io_blocks)) {
158 smp_mb__after_atomic();
159 if (waitqueue_active(&lc->wait))
164 static void log_end_io(struct bio *bio)
166 struct log_writes_c *lc = bio->bi_private;
168 if (bio->bi_status) {
171 DMERR("Error writing log block, error=%d", bio->bi_status);
172 spin_lock_irqsave(&lc->blocks_lock, flags);
173 lc->logging_enabled = false;
174 spin_unlock_irqrestore(&lc->blocks_lock, flags);
182 static void log_end_super(struct bio *bio)
184 struct log_writes_c *lc = bio->bi_private;
186 complete(&lc->super_done);
191 * Meant to be called if there is an error, it will free all the pages
192 * associated with the block.
194 static void free_pending_block(struct log_writes_c *lc,
195 struct pending_block *block)
199 for (i = 0; i < block->vec_cnt; i++) {
200 if (block->vecs[i].bv_page)
201 __free_page(block->vecs[i].bv_page);
205 put_pending_block(lc);
208 static int write_metadata(struct log_writes_c *lc, void *entry,
209 size_t entrylen, void *data, size_t datalen,
217 bio = bio_alloc(GFP_KERNEL, 1);
219 DMERR("Couldn't alloc log bio");
222 bio->bi_iter.bi_size = 0;
223 bio->bi_iter.bi_sector = sector;
224 bio_set_dev(bio, lc->logdev->bdev);
225 bio->bi_end_io = (sector == WRITE_LOG_SUPER_SECTOR) ?
226 log_end_super : log_end_io;
227 bio->bi_private = lc;
228 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
230 page = alloc_page(GFP_KERNEL);
232 DMERR("Couldn't alloc log page");
237 ptr = kmap_atomic(page);
238 memcpy(ptr, entry, entrylen);
240 memcpy(ptr + entrylen, data, datalen);
241 memset(ptr + entrylen + datalen, 0,
242 lc->sectorsize - entrylen - datalen);
245 ret = bio_add_page(bio, page, lc->sectorsize, 0);
246 if (ret != lc->sectorsize) {
247 DMERR("Couldn't add page to the log block");
260 static int log_one_block(struct log_writes_c *lc,
261 struct pending_block *block, sector_t sector)
264 struct log_write_entry entry;
268 entry.sector = cpu_to_le64(block->sector);
269 entry.nr_sectors = cpu_to_le64(block->nr_sectors);
270 entry.flags = cpu_to_le64(block->flags);
271 entry.data_len = cpu_to_le64(block->datalen);
272 if (write_metadata(lc, &entry, sizeof(entry), block->data,
273 block->datalen, sector)) {
274 free_pending_block(lc, block);
280 sector += dev_to_bio_sectors(lc, 1);
282 atomic_inc(&lc->io_blocks);
283 bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt, BIO_MAX_PAGES));
285 DMERR("Couldn't alloc log bio");
288 bio->bi_iter.bi_size = 0;
289 bio->bi_iter.bi_sector = sector;
290 bio_set_dev(bio, lc->logdev->bdev);
291 bio->bi_end_io = log_end_io;
292 bio->bi_private = lc;
293 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
295 for (i = 0; i < block->vec_cnt; i++) {
297 * The page offset is always 0 because we allocate a new page
298 * for every bvec in the original bio for simplicity sake.
300 ret = bio_add_page(bio, block->vecs[i].bv_page,
301 block->vecs[i].bv_len, 0);
302 if (ret != block->vecs[i].bv_len) {
303 atomic_inc(&lc->io_blocks);
305 bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt - i, BIO_MAX_PAGES));
307 DMERR("Couldn't alloc log bio");
310 bio->bi_iter.bi_size = 0;
311 bio->bi_iter.bi_sector = sector;
312 bio_set_dev(bio, lc->logdev->bdev);
313 bio->bi_end_io = log_end_io;
314 bio->bi_private = lc;
315 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
317 ret = bio_add_page(bio, block->vecs[i].bv_page,
318 block->vecs[i].bv_len, 0);
319 if (ret != block->vecs[i].bv_len) {
320 DMERR("Couldn't add page on new bio?");
325 sector += block->vecs[i].bv_len >> SECTOR_SHIFT;
331 put_pending_block(lc);
334 free_pending_block(lc, block);
339 static int log_super(struct log_writes_c *lc)
341 struct log_write_super super;
343 super.magic = cpu_to_le64(WRITE_LOG_MAGIC);
344 super.version = cpu_to_le64(WRITE_LOG_VERSION);
345 super.nr_entries = cpu_to_le64(lc->logged_entries);
346 super.sectorsize = cpu_to_le32(lc->sectorsize);
348 if (write_metadata(lc, &super, sizeof(super), NULL, 0,
349 WRITE_LOG_SUPER_SECTOR)) {
350 DMERR("Couldn't write super");
355 * Super sector should be writen in-order, otherwise the
356 * nr_entries could be rewritten incorrectly by an old bio.
358 wait_for_completion_io(&lc->super_done);
363 static inline sector_t logdev_last_sector(struct log_writes_c *lc)
365 return i_size_read(lc->logdev->bdev->bd_inode) >> SECTOR_SHIFT;
368 static int log_writes_kthread(void *arg)
370 struct log_writes_c *lc = (struct log_writes_c *)arg;
373 while (!kthread_should_stop()) {
375 bool logging_enabled;
376 struct pending_block *block = NULL;
379 spin_lock_irq(&lc->blocks_lock);
380 if (!list_empty(&lc->logging_blocks)) {
381 block = list_first_entry(&lc->logging_blocks,
382 struct pending_block, list);
383 list_del_init(&block->list);
384 if (!lc->logging_enabled)
387 sector = lc->next_sector;
388 if (!(block->flags & LOG_DISCARD_FLAG))
389 lc->next_sector += dev_to_bio_sectors(lc, block->nr_sectors);
390 lc->next_sector += dev_to_bio_sectors(lc, 1);
393 * Apparently the size of the device may not be known
394 * right away, so handle this properly.
397 lc->end_sector = logdev_last_sector(lc);
398 if (lc->end_sector &&
399 lc->next_sector >= lc->end_sector) {
400 DMERR("Ran out of space on the logdev");
401 lc->logging_enabled = false;
404 lc->logged_entries++;
405 atomic_inc(&lc->io_blocks);
407 super = (block->flags & (LOG_FUA_FLAG | LOG_MARK_FLAG));
409 atomic_inc(&lc->io_blocks);
412 logging_enabled = lc->logging_enabled;
413 spin_unlock_irq(&lc->blocks_lock);
415 if (logging_enabled) {
416 ret = log_one_block(lc, block, sector);
420 spin_lock_irq(&lc->blocks_lock);
421 lc->logging_enabled = false;
422 spin_unlock_irq(&lc->blocks_lock);
425 free_pending_block(lc, block);
429 if (!try_to_freeze()) {
430 set_current_state(TASK_INTERRUPTIBLE);
431 if (!kthread_should_stop() &&
432 list_empty(&lc->logging_blocks))
434 __set_current_state(TASK_RUNNING);
441 * Construct a log-writes mapping:
442 * log-writes <dev_path> <log_dev_path>
444 static int log_writes_ctr(struct dm_target *ti, unsigned int argc, char **argv)
446 struct log_writes_c *lc;
447 struct dm_arg_set as;
448 const char *devname, *logdevname;
455 ti->error = "Invalid argument count";
459 lc = kzalloc(sizeof(struct log_writes_c), GFP_KERNEL);
461 ti->error = "Cannot allocate context";
464 spin_lock_init(&lc->blocks_lock);
465 INIT_LIST_HEAD(&lc->unflushed_blocks);
466 INIT_LIST_HEAD(&lc->logging_blocks);
467 init_waitqueue_head(&lc->wait);
468 init_completion(&lc->super_done);
469 atomic_set(&lc->io_blocks, 0);
470 atomic_set(&lc->pending_blocks, 0);
472 devname = dm_shift_arg(&as);
473 ret = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &lc->dev);
475 ti->error = "Device lookup failed";
479 logdevname = dm_shift_arg(&as);
480 ret = dm_get_device(ti, logdevname, dm_table_get_mode(ti->table),
483 ti->error = "Log device lookup failed";
484 dm_put_device(ti, lc->dev);
488 lc->sectorsize = bdev_logical_block_size(lc->dev->bdev);
489 lc->sectorshift = ilog2(lc->sectorsize);
490 lc->log_kthread = kthread_run(log_writes_kthread, lc, "log-write");
491 if (IS_ERR(lc->log_kthread)) {
492 ret = PTR_ERR(lc->log_kthread);
493 ti->error = "Couldn't alloc kthread";
494 dm_put_device(ti, lc->dev);
495 dm_put_device(ti, lc->logdev);
500 * next_sector is in 512b sectors to correspond to what bi_sector expects.
501 * The super starts at sector 0, and the next_sector is the next logical
502 * one based on the sectorsize of the device.
504 lc->next_sector = lc->sectorsize >> SECTOR_SHIFT;
505 lc->logging_enabled = true;
506 lc->end_sector = logdev_last_sector(lc);
507 lc->device_supports_discard = true;
509 ti->num_flush_bios = 1;
510 ti->flush_supported = true;
511 ti->num_discard_bios = 1;
512 ti->discards_supported = true;
513 ti->per_io_data_size = sizeof(struct per_bio_data);
522 static int log_mark(struct log_writes_c *lc, char *data)
524 struct pending_block *block;
525 size_t maxsize = lc->sectorsize - sizeof(struct log_write_entry);
527 block = kzalloc(sizeof(struct pending_block), GFP_KERNEL);
529 DMERR("Error allocating pending block");
533 block->data = kstrndup(data, maxsize, GFP_KERNEL);
535 DMERR("Error copying mark data");
539 atomic_inc(&lc->pending_blocks);
540 block->datalen = strlen(block->data);
541 block->flags |= LOG_MARK_FLAG;
542 spin_lock_irq(&lc->blocks_lock);
543 list_add_tail(&block->list, &lc->logging_blocks);
544 spin_unlock_irq(&lc->blocks_lock);
545 wake_up_process(lc->log_kthread);
549 static void log_writes_dtr(struct dm_target *ti)
551 struct log_writes_c *lc = ti->private;
553 spin_lock_irq(&lc->blocks_lock);
554 list_splice_init(&lc->unflushed_blocks, &lc->logging_blocks);
555 spin_unlock_irq(&lc->blocks_lock);
558 * This is just nice to have since it'll update the super to include the
559 * unflushed blocks, if it fails we don't really care.
561 log_mark(lc, "dm-log-writes-end");
562 wake_up_process(lc->log_kthread);
563 wait_event(lc->wait, !atomic_read(&lc->io_blocks) &&
564 !atomic_read(&lc->pending_blocks));
565 kthread_stop(lc->log_kthread);
567 WARN_ON(!list_empty(&lc->logging_blocks));
568 WARN_ON(!list_empty(&lc->unflushed_blocks));
569 dm_put_device(ti, lc->dev);
570 dm_put_device(ti, lc->logdev);
574 static void normal_map_bio(struct dm_target *ti, struct bio *bio)
576 struct log_writes_c *lc = ti->private;
578 bio_set_dev(bio, lc->dev->bdev);
581 static int log_writes_map(struct dm_target *ti, struct bio *bio)
583 struct log_writes_c *lc = ti->private;
584 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
585 struct pending_block *block;
586 struct bvec_iter iter;
590 bool flush_bio = (bio->bi_opf & REQ_PREFLUSH);
591 bool fua_bio = (bio->bi_opf & REQ_FUA);
592 bool discard_bio = (bio_op(bio) == REQ_OP_DISCARD);
596 /* Don't bother doing anything if logging has been disabled */
597 if (!lc->logging_enabled)
601 * Map reads as normal.
603 if (bio_data_dir(bio) == READ)
606 /* No sectors and not a flush? Don't care */
607 if (!bio_sectors(bio) && !flush_bio)
611 * Discards will have bi_size set but there's no actual data, so just
612 * allocate the size of the pending block.
615 alloc_size = sizeof(struct pending_block);
617 alloc_size = sizeof(struct pending_block) + sizeof(struct bio_vec) * bio_segments(bio);
619 block = kzalloc(alloc_size, GFP_NOIO);
621 DMERR("Error allocating pending block");
622 spin_lock_irq(&lc->blocks_lock);
623 lc->logging_enabled = false;
624 spin_unlock_irq(&lc->blocks_lock);
625 return DM_MAPIO_KILL;
627 INIT_LIST_HEAD(&block->list);
629 atomic_inc(&lc->pending_blocks);
632 block->flags |= LOG_FLUSH_FLAG;
634 block->flags |= LOG_FUA_FLAG;
636 block->flags |= LOG_DISCARD_FLAG;
638 block->sector = bio_to_dev_sectors(lc, bio->bi_iter.bi_sector);
639 block->nr_sectors = bio_to_dev_sectors(lc, bio_sectors(bio));
641 /* We don't need the data, just submit */
643 WARN_ON(flush_bio || fua_bio);
644 if (lc->device_supports_discard)
647 return DM_MAPIO_SUBMITTED;
650 /* Flush bio, splice the unflushed blocks onto this list and submit */
651 if (flush_bio && !bio_sectors(bio)) {
652 spin_lock_irq(&lc->blocks_lock);
653 list_splice_init(&lc->unflushed_blocks, &block->list);
654 spin_unlock_irq(&lc->blocks_lock);
659 * We will write this bio somewhere else way later so we need to copy
660 * the actual contents into new pages so we know the data will always be
663 * We do this because this could be a bio from O_DIRECT in which case we
664 * can't just hold onto the page until some later point, we have to
665 * manually copy the contents.
667 bio_for_each_segment(bv, bio, iter) {
671 page = alloc_page(GFP_NOIO);
673 DMERR("Error allocing page");
674 free_pending_block(lc, block);
675 spin_lock_irq(&lc->blocks_lock);
676 lc->logging_enabled = false;
677 spin_unlock_irq(&lc->blocks_lock);
678 return DM_MAPIO_KILL;
681 src = kmap_atomic(bv.bv_page);
682 dst = kmap_atomic(page);
683 memcpy(dst, src + bv.bv_offset, bv.bv_len);
686 block->vecs[i].bv_page = page;
687 block->vecs[i].bv_len = bv.bv_len;
692 /* Had a flush with data in it, weird */
694 spin_lock_irq(&lc->blocks_lock);
695 list_splice_init(&lc->unflushed_blocks, &block->list);
696 spin_unlock_irq(&lc->blocks_lock);
699 normal_map_bio(ti, bio);
700 return DM_MAPIO_REMAPPED;
703 static int normal_end_io(struct dm_target *ti, struct bio *bio,
706 struct log_writes_c *lc = ti->private;
707 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
709 if (bio_data_dir(bio) == WRITE && pb->block) {
710 struct pending_block *block = pb->block;
713 spin_lock_irqsave(&lc->blocks_lock, flags);
714 if (block->flags & LOG_FLUSH_FLAG) {
715 list_splice_tail_init(&block->list, &lc->logging_blocks);
716 list_add_tail(&block->list, &lc->logging_blocks);
717 wake_up_process(lc->log_kthread);
718 } else if (block->flags & LOG_FUA_FLAG) {
719 list_add_tail(&block->list, &lc->logging_blocks);
720 wake_up_process(lc->log_kthread);
722 list_add_tail(&block->list, &lc->unflushed_blocks);
723 spin_unlock_irqrestore(&lc->blocks_lock, flags);
726 return DM_ENDIO_DONE;
730 * INFO format: <logged entries> <highest allocated sector>
732 static void log_writes_status(struct dm_target *ti, status_type_t type,
733 unsigned status_flags, char *result,
737 struct log_writes_c *lc = ti->private;
740 case STATUSTYPE_INFO:
741 DMEMIT("%llu %llu", lc->logged_entries,
742 (unsigned long long)lc->next_sector - 1);
743 if (!lc->logging_enabled)
744 DMEMIT(" logging_disabled");
747 case STATUSTYPE_TABLE:
748 DMEMIT("%s %s", lc->dev->name, lc->logdev->name);
753 static int log_writes_prepare_ioctl(struct dm_target *ti,
754 struct block_device **bdev, fmode_t *mode)
756 struct log_writes_c *lc = ti->private;
757 struct dm_dev *dev = lc->dev;
761 * Only pass ioctls through if the device sizes match exactly.
763 if (ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
768 static int log_writes_iterate_devices(struct dm_target *ti,
769 iterate_devices_callout_fn fn,
772 struct log_writes_c *lc = ti->private;
774 return fn(ti, lc->dev, 0, ti->len, data);
778 * Messages supported:
779 * mark <mark data> - specify the marked data.
781 static int log_writes_message(struct dm_target *ti, unsigned argc, char **argv)
784 struct log_writes_c *lc = ti->private;
787 DMWARN("Invalid log-writes message arguments, expect 2 arguments, got %d", argc);
791 if (!strcasecmp(argv[0], "mark"))
792 r = log_mark(lc, argv[1]);
794 DMWARN("Unrecognised log writes target message received: %s", argv[0]);
799 static void log_writes_io_hints(struct dm_target *ti, struct queue_limits *limits)
801 struct log_writes_c *lc = ti->private;
802 struct request_queue *q = bdev_get_queue(lc->dev->bdev);
804 if (!q || !blk_queue_discard(q)) {
805 lc->device_supports_discard = false;
806 limits->discard_granularity = lc->sectorsize;
807 limits->max_discard_sectors = (UINT_MAX >> SECTOR_SHIFT);
809 limits->logical_block_size = bdev_logical_block_size(lc->dev->bdev);
810 limits->physical_block_size = bdev_physical_block_size(lc->dev->bdev);
811 limits->io_min = limits->physical_block_size;
814 static struct target_type log_writes_target = {
815 .name = "log-writes",
816 .version = {1, 0, 0},
817 .module = THIS_MODULE,
818 .ctr = log_writes_ctr,
819 .dtr = log_writes_dtr,
820 .map = log_writes_map,
821 .end_io = normal_end_io,
822 .status = log_writes_status,
823 .prepare_ioctl = log_writes_prepare_ioctl,
824 .message = log_writes_message,
825 .iterate_devices = log_writes_iterate_devices,
826 .io_hints = log_writes_io_hints,
829 static int __init dm_log_writes_init(void)
831 int r = dm_register_target(&log_writes_target);
834 DMERR("register failed %d", r);
839 static void __exit dm_log_writes_exit(void)
841 dm_unregister_target(&log_writes_target);
844 module_init(dm_log_writes_init);
845 module_exit(dm_log_writes_exit);
847 MODULE_DESCRIPTION(DM_NAME " log writes target");
848 MODULE_AUTHOR("Josef Bacik <jbacik@fb.com>");
849 MODULE_LICENSE("GPL");