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)
59 #define LOG_METADATA_FLAG (1 << 4)
61 #define WRITE_LOG_VERSION 1ULL
62 #define WRITE_LOG_MAGIC 0x6a736677736872ULL
65 * The disk format for this is braindead simple.
67 * At byte 0 we have our super, followed by the following sequence for
70 * [ 1 sector ][ entry->nr_sectors ]
71 * [log_write_entry][ data written ]
73 * The log_write_entry takes up a full sector so we can have arbitrary length
74 * marks and it leaves us room for extra content in the future.
78 * Basic info about the log for userspace.
80 struct log_write_super
{
88 * sector - the sector we wrote.
89 * nr_sectors - the number of sectors we wrote.
90 * flags - flags for this log entry.
91 * data_len - the size of the data in this log entry, this is for private log
92 * entry stuff, the MARK data provided by userspace for example.
94 struct log_write_entry
{
101 struct log_writes_c
{
103 struct dm_dev
*logdev
;
108 atomic_t pending_blocks
;
109 sector_t next_sector
;
111 bool logging_enabled
;
112 bool device_supports_discard
;
113 spinlock_t blocks_lock
;
114 struct list_head unflushed_blocks
;
115 struct list_head logging_blocks
;
116 wait_queue_head_t wait
;
117 struct task_struct
*log_kthread
;
120 struct pending_block
{
127 struct list_head list
;
128 struct bio_vec vecs
[0];
131 struct per_bio_data
{
132 struct pending_block
*block
;
135 static inline sector_t
bio_to_dev_sectors(struct log_writes_c
*lc
,
138 return sectors
>> (lc
->sectorshift
- SECTOR_SHIFT
);
141 static inline sector_t
dev_to_bio_sectors(struct log_writes_c
*lc
,
144 return sectors
<< (lc
->sectorshift
- SECTOR_SHIFT
);
147 static void put_pending_block(struct log_writes_c
*lc
)
149 if (atomic_dec_and_test(&lc
->pending_blocks
)) {
150 smp_mb__after_atomic();
151 if (waitqueue_active(&lc
->wait
))
156 static void put_io_block(struct log_writes_c
*lc
)
158 if (atomic_dec_and_test(&lc
->io_blocks
)) {
159 smp_mb__after_atomic();
160 if (waitqueue_active(&lc
->wait
))
165 static void log_end_io(struct bio
*bio
)
167 struct log_writes_c
*lc
= bio
->bi_private
;
169 if (bio
->bi_status
) {
172 DMERR("Error writing log block, error=%d", bio
->bi_status
);
173 spin_lock_irqsave(&lc
->blocks_lock
, flags
);
174 lc
->logging_enabled
= false;
175 spin_unlock_irqrestore(&lc
->blocks_lock
, flags
);
184 * Meant to be called if there is an error, it will free all the pages
185 * associated with the block.
187 static void free_pending_block(struct log_writes_c
*lc
,
188 struct pending_block
*block
)
192 for (i
= 0; i
< block
->vec_cnt
; i
++) {
193 if (block
->vecs
[i
].bv_page
)
194 __free_page(block
->vecs
[i
].bv_page
);
198 put_pending_block(lc
);
201 static int write_metadata(struct log_writes_c
*lc
, void *entry
,
202 size_t entrylen
, void *data
, size_t datalen
,
210 bio
= bio_alloc(GFP_KERNEL
, 1);
212 DMERR("Couldn't alloc log bio");
215 bio
->bi_iter
.bi_size
= 0;
216 bio
->bi_iter
.bi_sector
= sector
;
217 bio_set_dev(bio
, lc
->logdev
->bdev
);
218 bio
->bi_end_io
= log_end_io
;
219 bio
->bi_private
= lc
;
220 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
222 page
= alloc_page(GFP_KERNEL
);
224 DMERR("Couldn't alloc log page");
229 ptr
= kmap_atomic(page
);
230 memcpy(ptr
, entry
, entrylen
);
232 memcpy(ptr
+ entrylen
, data
, datalen
);
233 memset(ptr
+ entrylen
+ datalen
, 0,
234 lc
->sectorsize
- entrylen
- datalen
);
237 ret
= bio_add_page(bio
, page
, lc
->sectorsize
, 0);
238 if (ret
!= lc
->sectorsize
) {
239 DMERR("Couldn't add page to the log block");
252 static int write_inline_data(struct log_writes_c
*lc
, void *entry
,
253 size_t entrylen
, void *data
, size_t datalen
,
256 int num_pages
, bio_pages
, pg_datalen
, pg_sectorlen
, i
;
263 num_pages
= ALIGN(datalen
, PAGE_SIZE
) >> PAGE_SHIFT
;
264 bio_pages
= min(num_pages
, BIO_MAX_PAGES
);
266 atomic_inc(&lc
->io_blocks
);
268 bio
= bio_alloc(GFP_KERNEL
, bio_pages
);
270 DMERR("Couldn't alloc inline data bio");
274 bio
->bi_iter
.bi_size
= 0;
275 bio
->bi_iter
.bi_sector
= sector
;
276 bio_set_dev(bio
, lc
->logdev
->bdev
);
277 bio
->bi_end_io
= log_end_io
;
278 bio
->bi_private
= lc
;
279 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
281 for (i
= 0; i
< bio_pages
; i
++) {
282 pg_datalen
= min_t(int, datalen
, PAGE_SIZE
);
283 pg_sectorlen
= ALIGN(pg_datalen
, lc
->sectorsize
);
285 page
= alloc_page(GFP_KERNEL
);
287 DMERR("Couldn't alloc inline data page");
291 ptr
= kmap_atomic(page
);
292 memcpy(ptr
, data
, pg_datalen
);
293 if (pg_sectorlen
> pg_datalen
)
294 memset(ptr
+ pg_datalen
, 0, pg_sectorlen
- pg_datalen
);
297 ret
= bio_add_page(bio
, page
, pg_sectorlen
, 0);
298 if (ret
!= pg_sectorlen
) {
299 DMERR("Couldn't add page of inline data");
304 datalen
-= pg_datalen
;
309 sector
+= bio_pages
* PAGE_SECTORS
;
320 static int log_one_block(struct log_writes_c
*lc
,
321 struct pending_block
*block
, sector_t sector
)
324 struct log_write_entry entry
;
325 size_t metadatalen
, ret
;
328 entry
.sector
= cpu_to_le64(block
->sector
);
329 entry
.nr_sectors
= cpu_to_le64(block
->nr_sectors
);
330 entry
.flags
= cpu_to_le64(block
->flags
);
331 entry
.data_len
= cpu_to_le64(block
->datalen
);
333 metadatalen
= (block
->flags
& LOG_MARK_FLAG
) ? block
->datalen
: 0;
334 if (write_metadata(lc
, &entry
, sizeof(entry
), block
->data
,
335 metadatalen
, sector
)) {
336 free_pending_block(lc
, block
);
340 sector
+= dev_to_bio_sectors(lc
, 1);
342 if (block
->datalen
&& metadatalen
== 0) {
343 if (write_inline_data(lc
, &entry
, sizeof(entry
), block
->data
,
344 block
->datalen
, sector
)) {
345 free_pending_block(lc
, block
);
348 /* we don't support both inline data & bio data */
355 atomic_inc(&lc
->io_blocks
);
356 bio
= bio_alloc(GFP_KERNEL
, min(block
->vec_cnt
, BIO_MAX_PAGES
));
358 DMERR("Couldn't alloc log bio");
361 bio
->bi_iter
.bi_size
= 0;
362 bio
->bi_iter
.bi_sector
= sector
;
363 bio_set_dev(bio
, lc
->logdev
->bdev
);
364 bio
->bi_end_io
= log_end_io
;
365 bio
->bi_private
= lc
;
366 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
368 for (i
= 0; i
< block
->vec_cnt
; i
++) {
370 * The page offset is always 0 because we allocate a new page
371 * for every bvec in the original bio for simplicity sake.
373 ret
= bio_add_page(bio
, block
->vecs
[i
].bv_page
,
374 block
->vecs
[i
].bv_len
, 0);
375 if (ret
!= block
->vecs
[i
].bv_len
) {
376 atomic_inc(&lc
->io_blocks
);
378 bio
= bio_alloc(GFP_KERNEL
, min(block
->vec_cnt
- i
, BIO_MAX_PAGES
));
380 DMERR("Couldn't alloc log bio");
383 bio
->bi_iter
.bi_size
= 0;
384 bio
->bi_iter
.bi_sector
= sector
;
385 bio_set_dev(bio
, lc
->logdev
->bdev
);
386 bio
->bi_end_io
= log_end_io
;
387 bio
->bi_private
= lc
;
388 bio_set_op_attrs(bio
, REQ_OP_WRITE
, 0);
390 ret
= bio_add_page(bio
, block
->vecs
[i
].bv_page
,
391 block
->vecs
[i
].bv_len
, 0);
392 if (ret
!= block
->vecs
[i
].bv_len
) {
393 DMERR("Couldn't add page on new bio?");
398 sector
+= block
->vecs
[i
].bv_len
>> SECTOR_SHIFT
;
404 put_pending_block(lc
);
407 free_pending_block(lc
, block
);
412 static int log_super(struct log_writes_c
*lc
)
414 struct log_write_super super
;
416 super
.magic
= cpu_to_le64(WRITE_LOG_MAGIC
);
417 super
.version
= cpu_to_le64(WRITE_LOG_VERSION
);
418 super
.nr_entries
= cpu_to_le64(lc
->logged_entries
);
419 super
.sectorsize
= cpu_to_le32(lc
->sectorsize
);
421 if (write_metadata(lc
, &super
, sizeof(super
), NULL
, 0, 0)) {
422 DMERR("Couldn't write super");
429 static inline sector_t
logdev_last_sector(struct log_writes_c
*lc
)
431 return i_size_read(lc
->logdev
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
434 static int log_writes_kthread(void *arg
)
436 struct log_writes_c
*lc
= (struct log_writes_c
*)arg
;
439 while (!kthread_should_stop()) {
441 bool logging_enabled
;
442 struct pending_block
*block
= NULL
;
445 spin_lock_irq(&lc
->blocks_lock
);
446 if (!list_empty(&lc
->logging_blocks
)) {
447 block
= list_first_entry(&lc
->logging_blocks
,
448 struct pending_block
, list
);
449 list_del_init(&block
->list
);
450 if (!lc
->logging_enabled
)
453 sector
= lc
->next_sector
;
454 if (!(block
->flags
& LOG_DISCARD_FLAG
))
455 lc
->next_sector
+= dev_to_bio_sectors(lc
, block
->nr_sectors
);
456 lc
->next_sector
+= dev_to_bio_sectors(lc
, 1);
459 * Apparently the size of the device may not be known
460 * right away, so handle this properly.
463 lc
->end_sector
= logdev_last_sector(lc
);
464 if (lc
->end_sector
&&
465 lc
->next_sector
>= lc
->end_sector
) {
466 DMERR("Ran out of space on the logdev");
467 lc
->logging_enabled
= false;
470 lc
->logged_entries
++;
471 atomic_inc(&lc
->io_blocks
);
473 super
= (block
->flags
& (LOG_FUA_FLAG
| LOG_MARK_FLAG
));
475 atomic_inc(&lc
->io_blocks
);
478 logging_enabled
= lc
->logging_enabled
;
479 spin_unlock_irq(&lc
->blocks_lock
);
481 if (logging_enabled
) {
482 ret
= log_one_block(lc
, block
, sector
);
486 spin_lock_irq(&lc
->blocks_lock
);
487 lc
->logging_enabled
= false;
488 spin_unlock_irq(&lc
->blocks_lock
);
491 free_pending_block(lc
, block
);
495 if (!try_to_freeze()) {
496 set_current_state(TASK_INTERRUPTIBLE
);
497 if (!kthread_should_stop() &&
498 list_empty(&lc
->logging_blocks
))
500 __set_current_state(TASK_RUNNING
);
507 * Construct a log-writes mapping:
508 * log-writes <dev_path> <log_dev_path>
510 static int log_writes_ctr(struct dm_target
*ti
, unsigned int argc
, char **argv
)
512 struct log_writes_c
*lc
;
513 struct dm_arg_set as
;
514 const char *devname
, *logdevname
;
521 ti
->error
= "Invalid argument count";
525 lc
= kzalloc(sizeof(struct log_writes_c
), GFP_KERNEL
);
527 ti
->error
= "Cannot allocate context";
530 spin_lock_init(&lc
->blocks_lock
);
531 INIT_LIST_HEAD(&lc
->unflushed_blocks
);
532 INIT_LIST_HEAD(&lc
->logging_blocks
);
533 init_waitqueue_head(&lc
->wait
);
534 atomic_set(&lc
->io_blocks
, 0);
535 atomic_set(&lc
->pending_blocks
, 0);
537 devname
= dm_shift_arg(&as
);
538 ret
= dm_get_device(ti
, devname
, dm_table_get_mode(ti
->table
), &lc
->dev
);
540 ti
->error
= "Device lookup failed";
544 logdevname
= dm_shift_arg(&as
);
545 ret
= dm_get_device(ti
, logdevname
, dm_table_get_mode(ti
->table
),
548 ti
->error
= "Log device lookup failed";
549 dm_put_device(ti
, lc
->dev
);
553 lc
->sectorsize
= bdev_logical_block_size(lc
->dev
->bdev
);
554 lc
->sectorshift
= ilog2(lc
->sectorsize
);
555 lc
->log_kthread
= kthread_run(log_writes_kthread
, lc
, "log-write");
556 if (IS_ERR(lc
->log_kthread
)) {
557 ret
= PTR_ERR(lc
->log_kthread
);
558 ti
->error
= "Couldn't alloc kthread";
559 dm_put_device(ti
, lc
->dev
);
560 dm_put_device(ti
, lc
->logdev
);
565 * next_sector is in 512b sectors to correspond to what bi_sector expects.
566 * The super starts at sector 0, and the next_sector is the next logical
567 * one based on the sectorsize of the device.
569 lc
->next_sector
= lc
->sectorsize
>> SECTOR_SHIFT
;
570 lc
->logging_enabled
= true;
571 lc
->end_sector
= logdev_last_sector(lc
);
572 lc
->device_supports_discard
= true;
574 ti
->num_flush_bios
= 1;
575 ti
->flush_supported
= true;
576 ti
->num_discard_bios
= 1;
577 ti
->discards_supported
= true;
578 ti
->per_io_data_size
= sizeof(struct per_bio_data
);
587 static int log_mark(struct log_writes_c
*lc
, char *data
)
589 struct pending_block
*block
;
590 size_t maxsize
= lc
->sectorsize
- sizeof(struct log_write_entry
);
592 block
= kzalloc(sizeof(struct pending_block
), GFP_KERNEL
);
594 DMERR("Error allocating pending block");
598 block
->data
= kstrndup(data
, maxsize
- 1, GFP_KERNEL
);
600 DMERR("Error copying mark data");
604 atomic_inc(&lc
->pending_blocks
);
605 block
->datalen
= strlen(block
->data
);
606 block
->flags
|= LOG_MARK_FLAG
;
607 spin_lock_irq(&lc
->blocks_lock
);
608 list_add_tail(&block
->list
, &lc
->logging_blocks
);
609 spin_unlock_irq(&lc
->blocks_lock
);
610 wake_up_process(lc
->log_kthread
);
614 static void log_writes_dtr(struct dm_target
*ti
)
616 struct log_writes_c
*lc
= ti
->private;
618 spin_lock_irq(&lc
->blocks_lock
);
619 list_splice_init(&lc
->unflushed_blocks
, &lc
->logging_blocks
);
620 spin_unlock_irq(&lc
->blocks_lock
);
623 * This is just nice to have since it'll update the super to include the
624 * unflushed blocks, if it fails we don't really care.
626 log_mark(lc
, "dm-log-writes-end");
627 wake_up_process(lc
->log_kthread
);
628 wait_event(lc
->wait
, !atomic_read(&lc
->io_blocks
) &&
629 !atomic_read(&lc
->pending_blocks
));
630 kthread_stop(lc
->log_kthread
);
632 WARN_ON(!list_empty(&lc
->logging_blocks
));
633 WARN_ON(!list_empty(&lc
->unflushed_blocks
));
634 dm_put_device(ti
, lc
->dev
);
635 dm_put_device(ti
, lc
->logdev
);
639 static void normal_map_bio(struct dm_target
*ti
, struct bio
*bio
)
641 struct log_writes_c
*lc
= ti
->private;
643 bio_set_dev(bio
, lc
->dev
->bdev
);
646 static int log_writes_map(struct dm_target
*ti
, struct bio
*bio
)
648 struct log_writes_c
*lc
= ti
->private;
649 struct per_bio_data
*pb
= dm_per_bio_data(bio
, sizeof(struct per_bio_data
));
650 struct pending_block
*block
;
651 struct bvec_iter iter
;
655 bool flush_bio
= (bio
->bi_opf
& REQ_PREFLUSH
);
656 bool fua_bio
= (bio
->bi_opf
& REQ_FUA
);
657 bool discard_bio
= (bio_op(bio
) == REQ_OP_DISCARD
);
658 bool meta_bio
= (bio
->bi_opf
& REQ_META
);
662 /* Don't bother doing anything if logging has been disabled */
663 if (!lc
->logging_enabled
)
667 * Map reads as normal.
669 if (bio_data_dir(bio
) == READ
)
672 /* No sectors and not a flush? Don't care */
673 if (!bio_sectors(bio
) && !flush_bio
)
677 * Discards will have bi_size set but there's no actual data, so just
678 * allocate the size of the pending block.
681 alloc_size
= sizeof(struct pending_block
);
683 alloc_size
= sizeof(struct pending_block
) + sizeof(struct bio_vec
) * bio_segments(bio
);
685 block
= kzalloc(alloc_size
, GFP_NOIO
);
687 DMERR("Error allocating pending block");
688 spin_lock_irq(&lc
->blocks_lock
);
689 lc
->logging_enabled
= false;
690 spin_unlock_irq(&lc
->blocks_lock
);
691 return DM_MAPIO_KILL
;
693 INIT_LIST_HEAD(&block
->list
);
695 atomic_inc(&lc
->pending_blocks
);
698 block
->flags
|= LOG_FLUSH_FLAG
;
700 block
->flags
|= LOG_FUA_FLAG
;
702 block
->flags
|= LOG_DISCARD_FLAG
;
704 block
->flags
|= LOG_METADATA_FLAG
;
706 block
->sector
= bio_to_dev_sectors(lc
, bio
->bi_iter
.bi_sector
);
707 block
->nr_sectors
= bio_to_dev_sectors(lc
, bio_sectors(bio
));
709 /* We don't need the data, just submit */
711 WARN_ON(flush_bio
|| fua_bio
);
712 if (lc
->device_supports_discard
)
715 return DM_MAPIO_SUBMITTED
;
718 /* Flush bio, splice the unflushed blocks onto this list and submit */
719 if (flush_bio
&& !bio_sectors(bio
)) {
720 spin_lock_irq(&lc
->blocks_lock
);
721 list_splice_init(&lc
->unflushed_blocks
, &block
->list
);
722 spin_unlock_irq(&lc
->blocks_lock
);
727 * We will write this bio somewhere else way later so we need to copy
728 * the actual contents into new pages so we know the data will always be
731 * We do this because this could be a bio from O_DIRECT in which case we
732 * can't just hold onto the page until some later point, we have to
733 * manually copy the contents.
735 bio_for_each_segment(bv
, bio
, iter
) {
739 page
= alloc_page(GFP_NOIO
);
741 DMERR("Error allocing page");
742 free_pending_block(lc
, block
);
743 spin_lock_irq(&lc
->blocks_lock
);
744 lc
->logging_enabled
= false;
745 spin_unlock_irq(&lc
->blocks_lock
);
746 return DM_MAPIO_KILL
;
749 src
= kmap_atomic(bv
.bv_page
);
750 dst
= kmap_atomic(page
);
751 memcpy(dst
, src
+ bv
.bv_offset
, bv
.bv_len
);
754 block
->vecs
[i
].bv_page
= page
;
755 block
->vecs
[i
].bv_len
= bv
.bv_len
;
760 /* Had a flush with data in it, weird */
762 spin_lock_irq(&lc
->blocks_lock
);
763 list_splice_init(&lc
->unflushed_blocks
, &block
->list
);
764 spin_unlock_irq(&lc
->blocks_lock
);
767 normal_map_bio(ti
, bio
);
768 return DM_MAPIO_REMAPPED
;
771 static int normal_end_io(struct dm_target
*ti
, struct bio
*bio
,
774 struct log_writes_c
*lc
= ti
->private;
775 struct per_bio_data
*pb
= dm_per_bio_data(bio
, sizeof(struct per_bio_data
));
777 if (bio_data_dir(bio
) == WRITE
&& pb
->block
) {
778 struct pending_block
*block
= pb
->block
;
781 spin_lock_irqsave(&lc
->blocks_lock
, flags
);
782 if (block
->flags
& LOG_FLUSH_FLAG
) {
783 list_splice_tail_init(&block
->list
, &lc
->logging_blocks
);
784 list_add_tail(&block
->list
, &lc
->logging_blocks
);
785 wake_up_process(lc
->log_kthread
);
786 } else if (block
->flags
& LOG_FUA_FLAG
) {
787 list_add_tail(&block
->list
, &lc
->logging_blocks
);
788 wake_up_process(lc
->log_kthread
);
790 list_add_tail(&block
->list
, &lc
->unflushed_blocks
);
791 spin_unlock_irqrestore(&lc
->blocks_lock
, flags
);
794 return DM_ENDIO_DONE
;
798 * INFO format: <logged entries> <highest allocated sector>
800 static void log_writes_status(struct dm_target
*ti
, status_type_t type
,
801 unsigned status_flags
, char *result
,
805 struct log_writes_c
*lc
= ti
->private;
808 case STATUSTYPE_INFO
:
809 DMEMIT("%llu %llu", lc
->logged_entries
,
810 (unsigned long long)lc
->next_sector
- 1);
811 if (!lc
->logging_enabled
)
812 DMEMIT(" logging_disabled");
815 case STATUSTYPE_TABLE
:
816 DMEMIT("%s %s", lc
->dev
->name
, lc
->logdev
->name
);
821 static int log_writes_prepare_ioctl(struct dm_target
*ti
,
822 struct block_device
**bdev
)
824 struct log_writes_c
*lc
= ti
->private;
825 struct dm_dev
*dev
= lc
->dev
;
829 * Only pass ioctls through if the device sizes match exactly.
831 if (ti
->len
!= i_size_read(dev
->bdev
->bd_inode
) >> SECTOR_SHIFT
)
836 static int log_writes_iterate_devices(struct dm_target
*ti
,
837 iterate_devices_callout_fn fn
,
840 struct log_writes_c
*lc
= ti
->private;
842 return fn(ti
, lc
->dev
, 0, ti
->len
, data
);
846 * Messages supported:
847 * mark <mark data> - specify the marked data.
849 static int log_writes_message(struct dm_target
*ti
, unsigned argc
, char **argv
,
850 char *result
, unsigned maxlen
)
853 struct log_writes_c
*lc
= ti
->private;
856 DMWARN("Invalid log-writes message arguments, expect 2 arguments, got %d", argc
);
860 if (!strcasecmp(argv
[0], "mark"))
861 r
= log_mark(lc
, argv
[1]);
863 DMWARN("Unrecognised log writes target message received: %s", argv
[0]);
868 static void log_writes_io_hints(struct dm_target
*ti
, struct queue_limits
*limits
)
870 struct log_writes_c
*lc
= ti
->private;
871 struct request_queue
*q
= bdev_get_queue(lc
->dev
->bdev
);
873 if (!q
|| !blk_queue_discard(q
)) {
874 lc
->device_supports_discard
= false;
875 limits
->discard_granularity
= lc
->sectorsize
;
876 limits
->max_discard_sectors
= (UINT_MAX
>> SECTOR_SHIFT
);
878 limits
->logical_block_size
= bdev_logical_block_size(lc
->dev
->bdev
);
879 limits
->physical_block_size
= bdev_physical_block_size(lc
->dev
->bdev
);
880 limits
->io_min
= limits
->physical_block_size
;
883 #if IS_ENABLED(CONFIG_DAX_DRIVER)
884 static int log_dax(struct log_writes_c
*lc
, sector_t sector
, size_t bytes
,
887 struct pending_block
*block
;
892 block
= kzalloc(sizeof(struct pending_block
), GFP_KERNEL
);
894 DMERR("Error allocating dax pending block");
898 block
->data
= kzalloc(bytes
, GFP_KERNEL
);
900 DMERR("Error allocating dax data space");
905 /* write data provided via the iterator */
906 if (!copy_from_iter(block
->data
, bytes
, i
)) {
907 DMERR("Error copying dax data");
913 /* rewind the iterator so that the block driver can use it */
914 iov_iter_revert(i
, bytes
);
916 block
->datalen
= bytes
;
917 block
->sector
= bio_to_dev_sectors(lc
, sector
);
918 block
->nr_sectors
= ALIGN(bytes
, lc
->sectorsize
) >> lc
->sectorshift
;
920 atomic_inc(&lc
->pending_blocks
);
921 spin_lock_irq(&lc
->blocks_lock
);
922 list_add_tail(&block
->list
, &lc
->unflushed_blocks
);
923 spin_unlock_irq(&lc
->blocks_lock
);
924 wake_up_process(lc
->log_kthread
);
929 static long log_writes_dax_direct_access(struct dm_target
*ti
, pgoff_t pgoff
,
930 long nr_pages
, void **kaddr
, pfn_t
*pfn
)
932 struct log_writes_c
*lc
= ti
->private;
933 sector_t sector
= pgoff
* PAGE_SECTORS
;
936 ret
= bdev_dax_pgoff(lc
->dev
->bdev
, sector
, nr_pages
* PAGE_SIZE
, &pgoff
);
939 return dax_direct_access(lc
->dev
->dax_dev
, pgoff
, nr_pages
, kaddr
, pfn
);
942 static size_t log_writes_dax_copy_from_iter(struct dm_target
*ti
,
943 pgoff_t pgoff
, void *addr
, size_t bytes
,
946 struct log_writes_c
*lc
= ti
->private;
947 sector_t sector
= pgoff
* PAGE_SECTORS
;
950 if (bdev_dax_pgoff(lc
->dev
->bdev
, sector
, ALIGN(bytes
, PAGE_SIZE
), &pgoff
))
953 /* Don't bother doing anything if logging has been disabled */
954 if (!lc
->logging_enabled
)
957 err
= log_dax(lc
, sector
, bytes
, i
);
959 DMWARN("Error %d logging DAX write", err
);
963 return dax_copy_from_iter(lc
->dev
->dax_dev
, pgoff
, addr
, bytes
, i
);
966 static size_t log_writes_dax_copy_to_iter(struct dm_target
*ti
,
967 pgoff_t pgoff
, void *addr
, size_t bytes
,
970 struct log_writes_c
*lc
= ti
->private;
971 sector_t sector
= pgoff
* PAGE_SECTORS
;
973 if (bdev_dax_pgoff(lc
->dev
->bdev
, sector
, ALIGN(bytes
, PAGE_SIZE
), &pgoff
))
975 return dax_copy_to_iter(lc
->dev
->dax_dev
, pgoff
, addr
, bytes
, i
);
979 #define log_writes_dax_direct_access NULL
980 #define log_writes_dax_copy_from_iter NULL
981 #define log_writes_dax_copy_to_iter NULL
984 static struct target_type log_writes_target
= {
985 .name
= "log-writes",
986 .version
= {1, 1, 0},
987 .module
= THIS_MODULE
,
988 .ctr
= log_writes_ctr
,
989 .dtr
= log_writes_dtr
,
990 .map
= log_writes_map
,
991 .end_io
= normal_end_io
,
992 .status
= log_writes_status
,
993 .prepare_ioctl
= log_writes_prepare_ioctl
,
994 .message
= log_writes_message
,
995 .iterate_devices
= log_writes_iterate_devices
,
996 .io_hints
= log_writes_io_hints
,
997 .direct_access
= log_writes_dax_direct_access
,
998 .dax_copy_from_iter
= log_writes_dax_copy_from_iter
,
999 .dax_copy_to_iter
= log_writes_dax_copy_to_iter
,
1002 static int __init
dm_log_writes_init(void)
1004 int r
= dm_register_target(&log_writes_target
);
1007 DMERR("register failed %d", r
);
1012 static void __exit
dm_log_writes_exit(void)
1014 dm_unregister_target(&log_writes_target
);
1017 module_init(dm_log_writes_init
);
1018 module_exit(dm_log_writes_exit
);
1020 MODULE_DESCRIPTION(DM_NAME
" log writes target");
1021 MODULE_AUTHOR("Josef Bacik <jbacik@fb.com>");
1022 MODULE_LICENSE("GPL");