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/dax.h>
14 #include <linux/slab.h>
15 #include <linux/kthread.h>
16 #include <linux/freezer.h>
17 #include <linux/uio.h>
19 #define DM_MSG_PREFIX "log-writes"
22 * This target will sequentially log all writes to the target device onto the
23 * log device. This is helpful for replaying writes to check for fs consistency
24 * at all times. This target provides a mechanism to mark specific events to
25 * check data at a later time. So for example you would:
29 * dmsetup message /dev/whatever mark mymark
32 * Then replay the log up to mymark and check the contents of the replay to
33 * verify it matches what was written.
35 * We log writes only after they have been flushed, this makes the log describe
36 * close to the order in which the data hits the actual disk, not its cache. So
37 * for example the following sequence (W means write, C means complete)
39 * Wa,Wb,Wc,Cc,Ca,FLUSH,FUAd,Cb,CFLUSH,CFUAd
41 * Would result in the log looking like this:
43 * c,a,flush,fuad,b,<other writes>,<next flush>
45 * This is meant to help expose problems where file systems do not properly wait
46 * on data being written before invoking a FLUSH. FUA bypasses cache so once it
47 * completes it is added to the log as it should be on disk.
49 * We treat DISCARDs as if they don't bypass cache so that they are logged in
50 * order of completion along with the normal writes. If we didn't do it this
51 * way we would process all the discards first and then write all the data, when
52 * in fact we want to do the data and the discard in the order that they
55 #define LOG_FLUSH_FLAG (1 << 0)
56 #define LOG_FUA_FLAG (1 << 1)
57 #define LOG_DISCARD_FLAG (1 << 2)
58 #define LOG_MARK_FLAG (1 << 3)
60 #define WRITE_LOG_VERSION 1ULL
61 #define WRITE_LOG_MAGIC 0x6a736677736872ULL
64 * The disk format for this is braindead simple.
66 * At byte 0 we have our super, followed by the following sequence for
69 * [ 1 sector ][ entry->nr_sectors ]
70 * [log_write_entry][ data written ]
72 * The log_write_entry takes up a full sector so we can have arbitrary length
73 * marks and it leaves us room for extra content in the future.
77 * Basic info about the log for userspace.
79 struct log_write_super {
87 * sector - the sector we wrote.
88 * nr_sectors - the number of sectors we wrote.
89 * flags - flags for this log entry.
90 * data_len - the size of the data in this log entry, this is for private log
91 * entry stuff, the MARK data provided by userspace for example.
93 struct log_write_entry {
100 struct log_writes_c {
102 struct dm_dev *logdev;
107 atomic_t pending_blocks;
108 sector_t next_sector;
110 bool logging_enabled;
111 bool device_supports_discard;
112 spinlock_t blocks_lock;
113 struct list_head unflushed_blocks;
114 struct list_head logging_blocks;
115 wait_queue_head_t wait;
116 struct task_struct *log_kthread;
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);
183 * Meant to be called if there is an error, it will free all the pages
184 * associated with the block.
186 static void free_pending_block(struct log_writes_c *lc,
187 struct pending_block *block)
191 for (i = 0; i < block->vec_cnt; i++) {
192 if (block->vecs[i].bv_page)
193 __free_page(block->vecs[i].bv_page);
197 put_pending_block(lc);
200 static int write_metadata(struct log_writes_c *lc, void *entry,
201 size_t entrylen, void *data, size_t datalen,
209 bio = bio_alloc(GFP_KERNEL, 1);
211 DMERR("Couldn't alloc log bio");
214 bio->bi_iter.bi_size = 0;
215 bio->bi_iter.bi_sector = sector;
216 bio_set_dev(bio, lc->logdev->bdev);
217 bio->bi_end_io = log_end_io;
218 bio->bi_private = lc;
219 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
221 page = alloc_page(GFP_KERNEL);
223 DMERR("Couldn't alloc log page");
228 ptr = kmap_atomic(page);
229 memcpy(ptr, entry, entrylen);
231 memcpy(ptr + entrylen, data, datalen);
232 memset(ptr + entrylen + datalen, 0,
233 lc->sectorsize - entrylen - datalen);
236 ret = bio_add_page(bio, page, lc->sectorsize, 0);
237 if (ret != lc->sectorsize) {
238 DMERR("Couldn't add page to the log block");
251 static int write_inline_data(struct log_writes_c *lc, void *entry,
252 size_t entrylen, void *data, size_t datalen,
255 int num_pages, bio_pages, pg_datalen, pg_sectorlen, i;
262 num_pages = ALIGN(datalen, PAGE_SIZE) >> PAGE_SHIFT;
263 bio_pages = min(num_pages, BIO_MAX_PAGES);
265 atomic_inc(&lc->io_blocks);
267 bio = bio_alloc(GFP_KERNEL, bio_pages);
269 DMERR("Couldn't alloc inline data bio");
273 bio->bi_iter.bi_size = 0;
274 bio->bi_iter.bi_sector = sector;
275 bio_set_dev(bio, lc->logdev->bdev);
276 bio->bi_end_io = log_end_io;
277 bio->bi_private = lc;
278 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
280 for (i = 0; i < bio_pages; i++) {
281 pg_datalen = min_t(int, datalen, PAGE_SIZE);
282 pg_sectorlen = ALIGN(pg_datalen, lc->sectorsize);
284 page = alloc_page(GFP_KERNEL);
286 DMERR("Couldn't alloc inline data page");
290 ptr = kmap_atomic(page);
291 memcpy(ptr, data, pg_datalen);
292 if (pg_sectorlen > pg_datalen)
293 memset(ptr + pg_datalen, 0, pg_sectorlen - pg_datalen);
296 ret = bio_add_page(bio, page, pg_sectorlen, 0);
297 if (ret != pg_sectorlen) {
298 DMERR("Couldn't add page of inline data");
303 datalen -= pg_datalen;
308 sector += bio_pages * PAGE_SECTORS;
319 static int log_one_block(struct log_writes_c *lc,
320 struct pending_block *block, sector_t sector)
323 struct log_write_entry entry;
324 size_t metadatalen, ret;
327 entry.sector = cpu_to_le64(block->sector);
328 entry.nr_sectors = cpu_to_le64(block->nr_sectors);
329 entry.flags = cpu_to_le64(block->flags);
330 entry.data_len = cpu_to_le64(block->datalen);
332 metadatalen = (block->flags & LOG_MARK_FLAG) ? block->datalen : 0;
333 if (write_metadata(lc, &entry, sizeof(entry), block->data,
334 metadatalen, sector)) {
335 free_pending_block(lc, block);
339 sector += dev_to_bio_sectors(lc, 1);
341 if (block->datalen && metadatalen == 0) {
342 if (write_inline_data(lc, &entry, sizeof(entry), block->data,
343 block->datalen, sector)) {
344 free_pending_block(lc, block);
347 /* we don't support both inline data & bio data */
354 atomic_inc(&lc->io_blocks);
355 bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt, BIO_MAX_PAGES));
357 DMERR("Couldn't alloc log bio");
360 bio->bi_iter.bi_size = 0;
361 bio->bi_iter.bi_sector = sector;
362 bio_set_dev(bio, lc->logdev->bdev);
363 bio->bi_end_io = log_end_io;
364 bio->bi_private = lc;
365 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
367 for (i = 0; i < block->vec_cnt; i++) {
369 * The page offset is always 0 because we allocate a new page
370 * for every bvec in the original bio for simplicity sake.
372 ret = bio_add_page(bio, block->vecs[i].bv_page,
373 block->vecs[i].bv_len, 0);
374 if (ret != block->vecs[i].bv_len) {
375 atomic_inc(&lc->io_blocks);
377 bio = bio_alloc(GFP_KERNEL, min(block->vec_cnt - i, BIO_MAX_PAGES));
379 DMERR("Couldn't alloc log bio");
382 bio->bi_iter.bi_size = 0;
383 bio->bi_iter.bi_sector = sector;
384 bio_set_dev(bio, lc->logdev->bdev);
385 bio->bi_end_io = log_end_io;
386 bio->bi_private = lc;
387 bio_set_op_attrs(bio, REQ_OP_WRITE, 0);
389 ret = bio_add_page(bio, block->vecs[i].bv_page,
390 block->vecs[i].bv_len, 0);
391 if (ret != block->vecs[i].bv_len) {
392 DMERR("Couldn't add page on new bio?");
397 sector += block->vecs[i].bv_len >> SECTOR_SHIFT;
403 put_pending_block(lc);
406 free_pending_block(lc, block);
411 static int log_super(struct log_writes_c *lc)
413 struct log_write_super super;
415 super.magic = cpu_to_le64(WRITE_LOG_MAGIC);
416 super.version = cpu_to_le64(WRITE_LOG_VERSION);
417 super.nr_entries = cpu_to_le64(lc->logged_entries);
418 super.sectorsize = cpu_to_le32(lc->sectorsize);
420 if (write_metadata(lc, &super, sizeof(super), NULL, 0, 0)) {
421 DMERR("Couldn't write super");
428 static inline sector_t logdev_last_sector(struct log_writes_c *lc)
430 return i_size_read(lc->logdev->bdev->bd_inode) >> SECTOR_SHIFT;
433 static int log_writes_kthread(void *arg)
435 struct log_writes_c *lc = (struct log_writes_c *)arg;
438 while (!kthread_should_stop()) {
440 bool logging_enabled;
441 struct pending_block *block = NULL;
444 spin_lock_irq(&lc->blocks_lock);
445 if (!list_empty(&lc->logging_blocks)) {
446 block = list_first_entry(&lc->logging_blocks,
447 struct pending_block, list);
448 list_del_init(&block->list);
449 if (!lc->logging_enabled)
452 sector = lc->next_sector;
453 if (!(block->flags & LOG_DISCARD_FLAG))
454 lc->next_sector += dev_to_bio_sectors(lc, block->nr_sectors);
455 lc->next_sector += dev_to_bio_sectors(lc, 1);
458 * Apparently the size of the device may not be known
459 * right away, so handle this properly.
462 lc->end_sector = logdev_last_sector(lc);
463 if (lc->end_sector &&
464 lc->next_sector >= lc->end_sector) {
465 DMERR("Ran out of space on the logdev");
466 lc->logging_enabled = false;
469 lc->logged_entries++;
470 atomic_inc(&lc->io_blocks);
472 super = (block->flags & (LOG_FUA_FLAG | LOG_MARK_FLAG));
474 atomic_inc(&lc->io_blocks);
477 logging_enabled = lc->logging_enabled;
478 spin_unlock_irq(&lc->blocks_lock);
480 if (logging_enabled) {
481 ret = log_one_block(lc, block, sector);
485 spin_lock_irq(&lc->blocks_lock);
486 lc->logging_enabled = false;
487 spin_unlock_irq(&lc->blocks_lock);
490 free_pending_block(lc, block);
494 if (!try_to_freeze()) {
495 set_current_state(TASK_INTERRUPTIBLE);
496 if (!kthread_should_stop() &&
497 list_empty(&lc->logging_blocks))
499 __set_current_state(TASK_RUNNING);
506 * Construct a log-writes mapping:
507 * log-writes <dev_path> <log_dev_path>
509 static int log_writes_ctr(struct dm_target *ti, unsigned int argc, char **argv)
511 struct log_writes_c *lc;
512 struct dm_arg_set as;
513 const char *devname, *logdevname;
520 ti->error = "Invalid argument count";
524 lc = kzalloc(sizeof(struct log_writes_c), GFP_KERNEL);
526 ti->error = "Cannot allocate context";
529 spin_lock_init(&lc->blocks_lock);
530 INIT_LIST_HEAD(&lc->unflushed_blocks);
531 INIT_LIST_HEAD(&lc->logging_blocks);
532 init_waitqueue_head(&lc->wait);
533 atomic_set(&lc->io_blocks, 0);
534 atomic_set(&lc->pending_blocks, 0);
536 devname = dm_shift_arg(&as);
537 ret = dm_get_device(ti, devname, dm_table_get_mode(ti->table), &lc->dev);
539 ti->error = "Device lookup failed";
543 logdevname = dm_shift_arg(&as);
544 ret = dm_get_device(ti, logdevname, dm_table_get_mode(ti->table),
547 ti->error = "Log device lookup failed";
548 dm_put_device(ti, lc->dev);
552 lc->sectorsize = bdev_logical_block_size(lc->dev->bdev);
553 lc->sectorshift = ilog2(lc->sectorsize);
554 lc->log_kthread = kthread_run(log_writes_kthread, lc, "log-write");
555 if (IS_ERR(lc->log_kthread)) {
556 ret = PTR_ERR(lc->log_kthread);
557 ti->error = "Couldn't alloc kthread";
558 dm_put_device(ti, lc->dev);
559 dm_put_device(ti, lc->logdev);
564 * next_sector is in 512b sectors to correspond to what bi_sector expects.
565 * The super starts at sector 0, and the next_sector is the next logical
566 * one based on the sectorsize of the device.
568 lc->next_sector = lc->sectorsize >> SECTOR_SHIFT;
569 lc->logging_enabled = true;
570 lc->end_sector = logdev_last_sector(lc);
571 lc->device_supports_discard = true;
573 ti->num_flush_bios = 1;
574 ti->flush_supported = true;
575 ti->num_discard_bios = 1;
576 ti->discards_supported = true;
577 ti->per_io_data_size = sizeof(struct per_bio_data);
586 static int log_mark(struct log_writes_c *lc, char *data)
588 struct pending_block *block;
589 size_t maxsize = lc->sectorsize - sizeof(struct log_write_entry);
591 block = kzalloc(sizeof(struct pending_block), GFP_KERNEL);
593 DMERR("Error allocating pending block");
597 block->data = kstrndup(data, maxsize - 1, GFP_KERNEL);
599 DMERR("Error copying mark data");
603 atomic_inc(&lc->pending_blocks);
604 block->datalen = strlen(block->data);
605 block->flags |= LOG_MARK_FLAG;
606 spin_lock_irq(&lc->blocks_lock);
607 list_add_tail(&block->list, &lc->logging_blocks);
608 spin_unlock_irq(&lc->blocks_lock);
609 wake_up_process(lc->log_kthread);
613 static int log_dax(struct log_writes_c *lc, sector_t sector, size_t bytes,
616 struct pending_block *block;
621 block = kzalloc(sizeof(struct pending_block), GFP_KERNEL);
623 DMERR("Error allocating dax pending block");
627 block->data = kzalloc(bytes, GFP_KERNEL);
629 DMERR("Error allocating dax data space");
634 /* write data provided via the iterator */
635 if (!copy_from_iter(block->data, bytes, i)) {
636 DMERR("Error copying dax data");
642 /* rewind the iterator so that the block driver can use it */
643 iov_iter_revert(i, bytes);
645 block->datalen = bytes;
646 block->sector = bio_to_dev_sectors(lc, sector);
647 block->nr_sectors = ALIGN(bytes, lc->sectorsize) >> lc->sectorshift;
649 atomic_inc(&lc->pending_blocks);
650 spin_lock_irq(&lc->blocks_lock);
651 list_add_tail(&block->list, &lc->unflushed_blocks);
652 spin_unlock_irq(&lc->blocks_lock);
653 wake_up_process(lc->log_kthread);
658 static void log_writes_dtr(struct dm_target *ti)
660 struct log_writes_c *lc = ti->private;
662 spin_lock_irq(&lc->blocks_lock);
663 list_splice_init(&lc->unflushed_blocks, &lc->logging_blocks);
664 spin_unlock_irq(&lc->blocks_lock);
667 * This is just nice to have since it'll update the super to include the
668 * unflushed blocks, if it fails we don't really care.
670 log_mark(lc, "dm-log-writes-end");
671 wake_up_process(lc->log_kthread);
672 wait_event(lc->wait, !atomic_read(&lc->io_blocks) &&
673 !atomic_read(&lc->pending_blocks));
674 kthread_stop(lc->log_kthread);
676 WARN_ON(!list_empty(&lc->logging_blocks));
677 WARN_ON(!list_empty(&lc->unflushed_blocks));
678 dm_put_device(ti, lc->dev);
679 dm_put_device(ti, lc->logdev);
683 static void normal_map_bio(struct dm_target *ti, struct bio *bio)
685 struct log_writes_c *lc = ti->private;
687 bio_set_dev(bio, lc->dev->bdev);
690 static int log_writes_map(struct dm_target *ti, struct bio *bio)
692 struct log_writes_c *lc = ti->private;
693 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
694 struct pending_block *block;
695 struct bvec_iter iter;
699 bool flush_bio = (bio->bi_opf & REQ_PREFLUSH);
700 bool fua_bio = (bio->bi_opf & REQ_FUA);
701 bool discard_bio = (bio_op(bio) == REQ_OP_DISCARD);
705 /* Don't bother doing anything if logging has been disabled */
706 if (!lc->logging_enabled)
710 * Map reads as normal.
712 if (bio_data_dir(bio) == READ)
715 /* No sectors and not a flush? Don't care */
716 if (!bio_sectors(bio) && !flush_bio)
720 * Discards will have bi_size set but there's no actual data, so just
721 * allocate the size of the pending block.
724 alloc_size = sizeof(struct pending_block);
726 alloc_size = sizeof(struct pending_block) + sizeof(struct bio_vec) * bio_segments(bio);
728 block = kzalloc(alloc_size, GFP_NOIO);
730 DMERR("Error allocating pending block");
731 spin_lock_irq(&lc->blocks_lock);
732 lc->logging_enabled = false;
733 spin_unlock_irq(&lc->blocks_lock);
734 return DM_MAPIO_KILL;
736 INIT_LIST_HEAD(&block->list);
738 atomic_inc(&lc->pending_blocks);
741 block->flags |= LOG_FLUSH_FLAG;
743 block->flags |= LOG_FUA_FLAG;
745 block->flags |= LOG_DISCARD_FLAG;
747 block->sector = bio_to_dev_sectors(lc, bio->bi_iter.bi_sector);
748 block->nr_sectors = bio_to_dev_sectors(lc, bio_sectors(bio));
750 /* We don't need the data, just submit */
752 WARN_ON(flush_bio || fua_bio);
753 if (lc->device_supports_discard)
756 return DM_MAPIO_SUBMITTED;
759 /* Flush bio, splice the unflushed blocks onto this list and submit */
760 if (flush_bio && !bio_sectors(bio)) {
761 spin_lock_irq(&lc->blocks_lock);
762 list_splice_init(&lc->unflushed_blocks, &block->list);
763 spin_unlock_irq(&lc->blocks_lock);
768 * We will write this bio somewhere else way later so we need to copy
769 * the actual contents into new pages so we know the data will always be
772 * We do this because this could be a bio from O_DIRECT in which case we
773 * can't just hold onto the page until some later point, we have to
774 * manually copy the contents.
776 bio_for_each_segment(bv, bio, iter) {
780 page = alloc_page(GFP_NOIO);
782 DMERR("Error allocing page");
783 free_pending_block(lc, block);
784 spin_lock_irq(&lc->blocks_lock);
785 lc->logging_enabled = false;
786 spin_unlock_irq(&lc->blocks_lock);
787 return DM_MAPIO_KILL;
790 src = kmap_atomic(bv.bv_page);
791 dst = kmap_atomic(page);
792 memcpy(dst, src + bv.bv_offset, bv.bv_len);
795 block->vecs[i].bv_page = page;
796 block->vecs[i].bv_len = bv.bv_len;
801 /* Had a flush with data in it, weird */
803 spin_lock_irq(&lc->blocks_lock);
804 list_splice_init(&lc->unflushed_blocks, &block->list);
805 spin_unlock_irq(&lc->blocks_lock);
808 normal_map_bio(ti, bio);
809 return DM_MAPIO_REMAPPED;
812 static int normal_end_io(struct dm_target *ti, struct bio *bio,
815 struct log_writes_c *lc = ti->private;
816 struct per_bio_data *pb = dm_per_bio_data(bio, sizeof(struct per_bio_data));
818 if (bio_data_dir(bio) == WRITE && pb->block) {
819 struct pending_block *block = pb->block;
822 spin_lock_irqsave(&lc->blocks_lock, flags);
823 if (block->flags & LOG_FLUSH_FLAG) {
824 list_splice_tail_init(&block->list, &lc->logging_blocks);
825 list_add_tail(&block->list, &lc->logging_blocks);
826 wake_up_process(lc->log_kthread);
827 } else if (block->flags & LOG_FUA_FLAG) {
828 list_add_tail(&block->list, &lc->logging_blocks);
829 wake_up_process(lc->log_kthread);
831 list_add_tail(&block->list, &lc->unflushed_blocks);
832 spin_unlock_irqrestore(&lc->blocks_lock, flags);
835 return DM_ENDIO_DONE;
839 * INFO format: <logged entries> <highest allocated sector>
841 static void log_writes_status(struct dm_target *ti, status_type_t type,
842 unsigned status_flags, char *result,
846 struct log_writes_c *lc = ti->private;
849 case STATUSTYPE_INFO:
850 DMEMIT("%llu %llu", lc->logged_entries,
851 (unsigned long long)lc->next_sector - 1);
852 if (!lc->logging_enabled)
853 DMEMIT(" logging_disabled");
856 case STATUSTYPE_TABLE:
857 DMEMIT("%s %s", lc->dev->name, lc->logdev->name);
862 static int log_writes_prepare_ioctl(struct dm_target *ti,
863 struct block_device **bdev, fmode_t *mode)
865 struct log_writes_c *lc = ti->private;
866 struct dm_dev *dev = lc->dev;
870 * Only pass ioctls through if the device sizes match exactly.
872 if (ti->len != i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT)
877 static int log_writes_iterate_devices(struct dm_target *ti,
878 iterate_devices_callout_fn fn,
881 struct log_writes_c *lc = ti->private;
883 return fn(ti, lc->dev, 0, ti->len, data);
887 * Messages supported:
888 * mark <mark data> - specify the marked data.
890 static int log_writes_message(struct dm_target *ti, unsigned argc, char **argv)
893 struct log_writes_c *lc = ti->private;
896 DMWARN("Invalid log-writes message arguments, expect 2 arguments, got %d", argc);
900 if (!strcasecmp(argv[0], "mark"))
901 r = log_mark(lc, argv[1]);
903 DMWARN("Unrecognised log writes target message received: %s", argv[0]);
908 static void log_writes_io_hints(struct dm_target *ti, struct queue_limits *limits)
910 struct log_writes_c *lc = ti->private;
911 struct request_queue *q = bdev_get_queue(lc->dev->bdev);
913 if (!q || !blk_queue_discard(q)) {
914 lc->device_supports_discard = false;
915 limits->discard_granularity = lc->sectorsize;
916 limits->max_discard_sectors = (UINT_MAX >> SECTOR_SHIFT);
918 limits->logical_block_size = bdev_logical_block_size(lc->dev->bdev);
919 limits->physical_block_size = bdev_physical_block_size(lc->dev->bdev);
920 limits->io_min = limits->physical_block_size;
923 static long log_writes_dax_direct_access(struct dm_target *ti, pgoff_t pgoff,
924 long nr_pages, void **kaddr, pfn_t *pfn)
926 struct log_writes_c *lc = ti->private;
927 sector_t sector = pgoff * PAGE_SECTORS;
930 ret = bdev_dax_pgoff(lc->dev->bdev, sector, nr_pages * PAGE_SIZE, &pgoff);
933 return dax_direct_access(lc->dev->dax_dev, pgoff, nr_pages, kaddr, pfn);
936 static size_t log_writes_dax_copy_from_iter(struct dm_target *ti,
937 pgoff_t pgoff, void *addr, size_t bytes,
940 struct log_writes_c *lc = ti->private;
941 sector_t sector = pgoff * PAGE_SECTORS;
944 if (bdev_dax_pgoff(lc->dev->bdev, sector, ALIGN(bytes, PAGE_SIZE), &pgoff))
947 /* Don't bother doing anything if logging has been disabled */
948 if (!lc->logging_enabled)
951 err = log_dax(lc, sector, bytes, i);
953 DMWARN("Error %d logging DAX write", err);
957 return dax_copy_from_iter(lc->dev->dax_dev, pgoff, addr, bytes, i);
960 static struct target_type log_writes_target = {
961 .name = "log-writes",
962 .version = {1, 1, 0},
963 .module = THIS_MODULE,
964 .ctr = log_writes_ctr,
965 .dtr = log_writes_dtr,
966 .map = log_writes_map,
967 .end_io = normal_end_io,
968 .status = log_writes_status,
969 .prepare_ioctl = log_writes_prepare_ioctl,
970 .message = log_writes_message,
971 .iterate_devices = log_writes_iterate_devices,
972 .io_hints = log_writes_io_hints,
973 .direct_access = log_writes_dax_direct_access,
974 .dax_copy_from_iter = log_writes_dax_copy_from_iter,
977 static int __init dm_log_writes_init(void)
979 int r = dm_register_target(&log_writes_target);
982 DMERR("register failed %d", r);
987 static void __exit dm_log_writes_exit(void)
989 dm_unregister_target(&log_writes_target);
992 module_init(dm_log_writes_init);
993 module_exit(dm_log_writes_exit);
995 MODULE_DESCRIPTION(DM_NAME " log writes target");
996 MODULE_AUTHOR("Josef Bacik <jbacik@fb.com>");
997 MODULE_LICENSE("GPL");