1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Red Hat. All rights reserved.
5 * This file is released under the GPL.
8 #include <linux/device-mapper.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/vmalloc.h>
12 #include <linux/kthread.h>
13 #include <linux/dm-io.h>
14 #include <linux/dm-kcopyd.h>
15 #include <linux/dax.h>
16 #include <linux/pfn_t.h>
17 #include <linux/libnvdimm.h>
19 #define DM_MSG_PREFIX "writecache"
21 #define HIGH_WATERMARK 50
22 #define LOW_WATERMARK 45
23 #define MAX_WRITEBACK_JOBS min(0x10000000 / PAGE_SIZE, totalram_pages() / 16)
24 #define ENDIO_LATENCY 16
25 #define WRITEBACK_LATENCY 64
26 #define AUTOCOMMIT_BLOCKS_SSD 65536
27 #define AUTOCOMMIT_BLOCKS_PMEM 64
28 #define AUTOCOMMIT_MSEC 1000
29 #define MAX_AGE_DIV 16
30 #define MAX_AGE_UNSPECIFIED -1UL
32 #define BITMAP_GRANULARITY 65536
33 #if BITMAP_GRANULARITY < PAGE_SIZE
34 #undef BITMAP_GRANULARITY
35 #define BITMAP_GRANULARITY PAGE_SIZE
38 #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_DAX_DRIVER)
39 #define DM_WRITECACHE_HAS_PMEM
42 #ifdef DM_WRITECACHE_HAS_PMEM
43 #define pmem_assign(dest, src) \
45 typeof(dest) uniq = (src); \
46 memcpy_flushcache(&(dest), &uniq, sizeof(dest)); \
49 #define pmem_assign(dest, src) ((dest) = (src))
52 #if IS_ENABLED(CONFIG_ARCH_HAS_COPY_MC) && defined(DM_WRITECACHE_HAS_PMEM)
53 #define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
56 #define MEMORY_SUPERBLOCK_MAGIC 0x23489321
57 #define MEMORY_SUPERBLOCK_VERSION 1
59 struct wc_memory_entry {
60 __le64 original_sector;
64 struct wc_memory_superblock {
76 struct wc_memory_entry entries[0];
80 struct rb_node rb_node;
82 unsigned short wc_list_contiguous;
83 bool write_in_progress
84 #if BITS_PER_LONG == 64
89 #if BITS_PER_LONG == 64
94 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
95 uint64_t original_sector;
100 #ifdef DM_WRITECACHE_HAS_PMEM
101 #define WC_MODE_PMEM(wc) ((wc)->pmem_mode)
102 #define WC_MODE_FUA(wc) ((wc)->writeback_fua)
104 #define WC_MODE_PMEM(wc) false
105 #define WC_MODE_FUA(wc) false
107 #define WC_MODE_SORT_FREELIST(wc) (!WC_MODE_PMEM(wc))
109 struct dm_writecache {
111 struct list_head lru;
113 struct list_head freelist;
115 struct rb_root freetree;
116 struct wc_entry *current_free;
121 size_t freelist_size;
122 size_t writeback_size;
123 size_t freelist_high_watermark;
124 size_t freelist_low_watermark;
125 unsigned long max_age;
127 unsigned uncommitted_blocks;
128 unsigned autocommit_blocks;
129 unsigned max_writeback_jobs;
133 unsigned long autocommit_jiffies;
134 struct timer_list autocommit_timer;
135 struct wait_queue_head freelist_wait;
137 struct timer_list max_age_timer;
139 atomic_t bio_in_progress[2];
140 struct wait_queue_head bio_in_progress_wait[2];
142 struct dm_target *ti;
144 struct dm_dev *ssd_dev;
145 sector_t start_sector;
147 uint64_t memory_map_size;
148 size_t metadata_sectors;
151 sector_t data_device_sectors;
153 struct wc_entry *entries;
155 unsigned char block_size_bits;
158 bool writeback_fua:1;
160 bool overwrote_committed:1;
161 bool memory_vmapped:1;
163 bool start_sector_set:1;
164 bool high_wm_percent_set:1;
165 bool low_wm_percent_set:1;
166 bool max_writeback_jobs_set:1;
167 bool autocommit_blocks_set:1;
168 bool autocommit_time_set:1;
170 bool writeback_fua_set:1;
171 bool flush_on_suspend:1;
175 unsigned high_wm_percent_value;
176 unsigned low_wm_percent_value;
177 unsigned autocommit_time_value;
178 unsigned max_age_value;
180 unsigned writeback_all;
181 struct workqueue_struct *writeback_wq;
182 struct work_struct writeback_work;
183 struct work_struct flush_work;
185 struct dm_io_client *dm_io;
187 raw_spinlock_t endio_list_lock;
188 struct list_head endio_list;
189 struct task_struct *endio_thread;
191 struct task_struct *flush_thread;
192 struct bio_list flush_list;
194 struct dm_kcopyd_client *dm_kcopyd;
195 unsigned long *dirty_bitmap;
196 unsigned dirty_bitmap_size;
198 struct bio_set bio_set;
202 #define WB_LIST_INLINE 16
204 struct writeback_struct {
205 struct list_head endio_entry;
206 struct dm_writecache *wc;
207 struct wc_entry **wc_list;
209 struct wc_entry *wc_list_inline[WB_LIST_INLINE];
214 struct list_head endio_entry;
215 struct dm_writecache *wc;
221 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
222 "A percentage of time allocated for data copying");
224 static void wc_lock(struct dm_writecache *wc)
226 mutex_lock(&wc->lock);
229 static void wc_unlock(struct dm_writecache *wc)
231 mutex_unlock(&wc->lock);
234 #ifdef DM_WRITECACHE_HAS_PMEM
235 static int persistent_memory_claim(struct dm_writecache *wc)
245 wc->memory_vmapped = false;
247 s = wc->memory_map_size;
253 if (p != s >> PAGE_SHIFT) {
258 offset = get_start_sect(wc->ssd_dev->bdev);
259 if (offset & (PAGE_SIZE / 512 - 1)) {
263 offset >>= PAGE_SHIFT - 9;
265 id = dax_read_lock();
267 da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, &wc->memory_map, &pfn);
269 wc->memory_map = NULL;
273 if (!pfn_t_has_page(pfn)) {
274 wc->memory_map = NULL;
280 wc->memory_map = NULL;
281 pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
289 daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i, p - i,
292 r = daa ? daa : -EINVAL;
295 if (!pfn_t_has_page(pfn)) {
299 while (daa-- && i < p) {
300 pages[i++] = pfn_t_to_page(pfn);
306 wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
307 if (!wc->memory_map) {
312 wc->memory_vmapped = true;
317 wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
318 wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
329 static int persistent_memory_claim(struct dm_writecache *wc)
335 static void persistent_memory_release(struct dm_writecache *wc)
337 if (wc->memory_vmapped)
338 vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
341 static struct page *persistent_memory_page(void *addr)
343 if (is_vmalloc_addr(addr))
344 return vmalloc_to_page(addr);
346 return virt_to_page(addr);
349 static unsigned persistent_memory_page_offset(void *addr)
351 return (unsigned long)addr & (PAGE_SIZE - 1);
354 static void persistent_memory_flush_cache(void *ptr, size_t size)
356 if (is_vmalloc_addr(ptr))
357 flush_kernel_vmap_range(ptr, size);
360 static void persistent_memory_invalidate_cache(void *ptr, size_t size)
362 if (is_vmalloc_addr(ptr))
363 invalidate_kernel_vmap_range(ptr, size);
366 static struct wc_memory_superblock *sb(struct dm_writecache *wc)
368 return wc->memory_map;
371 static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
373 return &sb(wc)->entries[e->index];
376 static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
378 return (char *)wc->block_start + (e->index << wc->block_size_bits);
381 static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
383 return wc->start_sector + wc->metadata_sectors +
384 ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
387 static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
389 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
390 return e->original_sector;
392 return le64_to_cpu(memory_entry(wc, e)->original_sector);
396 static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
398 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
401 return le64_to_cpu(memory_entry(wc, e)->seq_count);
405 static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
407 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
410 pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
413 static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
414 uint64_t original_sector, uint64_t seq_count)
416 struct wc_memory_entry me;
417 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
418 e->original_sector = original_sector;
419 e->seq_count = seq_count;
421 me.original_sector = cpu_to_le64(original_sector);
422 me.seq_count = cpu_to_le64(seq_count);
423 pmem_assign(*memory_entry(wc, e), me);
426 #define writecache_error(wc, err, msg, arg...) \
428 if (!cmpxchg(&(wc)->error, 0, err)) \
430 wake_up(&(wc)->freelist_wait); \
433 #define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error)))
435 static void writecache_flush_all_metadata(struct dm_writecache *wc)
437 if (!WC_MODE_PMEM(wc))
438 memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
441 static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
443 if (!WC_MODE_PMEM(wc))
444 __set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
448 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
451 struct dm_writecache *wc;
456 static void writecache_notify_io(unsigned long error, void *context)
458 struct io_notify *endio = context;
460 if (unlikely(error != 0))
461 writecache_error(endio->wc, -EIO, "error writing metadata");
462 BUG_ON(atomic_read(&endio->count) <= 0);
463 if (atomic_dec_and_test(&endio->count))
467 static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
469 wait_event(wc->bio_in_progress_wait[direction],
470 !atomic_read(&wc->bio_in_progress[direction]));
473 static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
475 struct dm_io_region region;
476 struct dm_io_request req;
477 struct io_notify endio = {
479 COMPLETION_INITIALIZER_ONSTACK(endio.c),
482 unsigned bitmap_bits = wc->dirty_bitmap_size * 8;
487 i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i);
488 if (unlikely(i == bitmap_bits))
490 j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i);
492 region.bdev = wc->ssd_dev->bdev;
493 region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
494 region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
496 if (unlikely(region.sector >= wc->metadata_sectors))
498 if (unlikely(region.sector + region.count > wc->metadata_sectors))
499 region.count = wc->metadata_sectors - region.sector;
501 region.sector += wc->start_sector;
502 atomic_inc(&endio.count);
503 req.bi_op = REQ_OP_WRITE;
504 req.bi_op_flags = REQ_SYNC;
505 req.mem.type = DM_IO_VMA;
506 req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
507 req.client = wc->dm_io;
508 req.notify.fn = writecache_notify_io;
509 req.notify.context = &endio;
511 /* writing via async dm-io (implied by notify.fn above) won't return an error */
512 (void) dm_io(&req, 1, ®ion, NULL);
516 writecache_notify_io(0, &endio);
517 wait_for_completion_io(&endio.c);
520 writecache_wait_for_ios(wc, WRITE);
522 writecache_disk_flush(wc, wc->ssd_dev);
524 memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
527 static void ssd_commit_superblock(struct dm_writecache *wc)
530 struct dm_io_region region;
531 struct dm_io_request req;
533 region.bdev = wc->ssd_dev->bdev;
535 region.count = max(4096U, wc->block_size) >> SECTOR_SHIFT;
537 if (unlikely(region.sector + region.count > wc->metadata_sectors))
538 region.count = wc->metadata_sectors - region.sector;
540 region.sector += wc->start_sector;
542 req.bi_op = REQ_OP_WRITE;
543 req.bi_op_flags = REQ_SYNC | REQ_FUA;
544 req.mem.type = DM_IO_VMA;
545 req.mem.ptr.vma = (char *)wc->memory_map;
546 req.client = wc->dm_io;
547 req.notify.fn = NULL;
548 req.notify.context = NULL;
550 r = dm_io(&req, 1, ®ion, NULL);
552 writecache_error(wc, r, "error writing superblock");
555 static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
557 if (WC_MODE_PMEM(wc))
560 ssd_commit_flushed(wc, wait_for_ios);
563 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
566 struct dm_io_region region;
567 struct dm_io_request req;
569 region.bdev = dev->bdev;
572 req.bi_op = REQ_OP_WRITE;
573 req.bi_op_flags = REQ_PREFLUSH;
574 req.mem.type = DM_IO_KMEM;
575 req.mem.ptr.addr = NULL;
576 req.client = wc->dm_io;
577 req.notify.fn = NULL;
579 r = dm_io(&req, 1, ®ion, NULL);
581 writecache_error(wc, r, "error flushing metadata: %d", r);
584 #define WFE_RETURN_FOLLOWING 1
585 #define WFE_LOWEST_SEQ 2
587 static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
588 uint64_t block, int flags)
591 struct rb_node *node = wc->tree.rb_node;
597 e = container_of(node, struct wc_entry, rb_node);
598 if (read_original_sector(wc, e) == block)
601 node = (read_original_sector(wc, e) >= block ?
602 e->rb_node.rb_left : e->rb_node.rb_right);
603 if (unlikely(!node)) {
604 if (!(flags & WFE_RETURN_FOLLOWING))
606 if (read_original_sector(wc, e) >= block) {
609 node = rb_next(&e->rb_node);
612 e = container_of(node, struct wc_entry, rb_node);
620 if (flags & WFE_LOWEST_SEQ)
621 node = rb_prev(&e->rb_node);
623 node = rb_next(&e->rb_node);
626 e2 = container_of(node, struct wc_entry, rb_node);
627 if (read_original_sector(wc, e2) != block)
633 static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
636 struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
639 e = container_of(*node, struct wc_entry, rb_node);
640 parent = &e->rb_node;
641 if (read_original_sector(wc, e) > read_original_sector(wc, ins))
642 node = &parent->rb_left;
644 node = &parent->rb_right;
646 rb_link_node(&ins->rb_node, parent, node);
647 rb_insert_color(&ins->rb_node, &wc->tree);
648 list_add(&ins->lru, &wc->lru);
652 static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
655 rb_erase(&e->rb_node, &wc->tree);
658 static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
660 if (WC_MODE_SORT_FREELIST(wc)) {
661 struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
662 if (unlikely(!*node))
663 wc->current_free = e;
666 if (&e->rb_node < *node)
667 node = &parent->rb_left;
669 node = &parent->rb_right;
671 rb_link_node(&e->rb_node, parent, node);
672 rb_insert_color(&e->rb_node, &wc->freetree);
674 list_add_tail(&e->lru, &wc->freelist);
679 static inline void writecache_verify_watermark(struct dm_writecache *wc)
681 if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
682 queue_work(wc->writeback_wq, &wc->writeback_work);
685 static void writecache_max_age_timer(struct timer_list *t)
687 struct dm_writecache *wc = from_timer(wc, t, max_age_timer);
689 if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) {
690 queue_work(wc->writeback_wq, &wc->writeback_work);
691 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
695 static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector)
699 if (WC_MODE_SORT_FREELIST(wc)) {
700 struct rb_node *next;
701 if (unlikely(!wc->current_free))
703 e = wc->current_free;
704 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
706 next = rb_next(&e->rb_node);
707 rb_erase(&e->rb_node, &wc->freetree);
709 next = rb_first(&wc->freetree);
710 wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
712 if (unlikely(list_empty(&wc->freelist)))
714 e = container_of(wc->freelist.next, struct wc_entry, lru);
715 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
721 writecache_verify_watermark(wc);
726 static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
728 writecache_unlink(wc, e);
729 writecache_add_to_freelist(wc, e);
730 clear_seq_count(wc, e);
731 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
732 if (unlikely(waitqueue_active(&wc->freelist_wait)))
733 wake_up(&wc->freelist_wait);
736 static void writecache_wait_on_freelist(struct dm_writecache *wc)
740 prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
743 finish_wait(&wc->freelist_wait, &wait);
747 static void writecache_poison_lists(struct dm_writecache *wc)
750 * Catch incorrect access to these values while the device is suspended.
752 memset(&wc->tree, -1, sizeof wc->tree);
753 wc->lru.next = LIST_POISON1;
754 wc->lru.prev = LIST_POISON2;
755 wc->freelist.next = LIST_POISON1;
756 wc->freelist.prev = LIST_POISON2;
759 static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
761 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
762 if (WC_MODE_PMEM(wc))
763 writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
766 static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
768 return read_seq_count(wc, e) < wc->seq_count;
771 static void writecache_flush(struct dm_writecache *wc)
773 struct wc_entry *e, *e2;
774 bool need_flush_after_free;
776 wc->uncommitted_blocks = 0;
777 del_timer(&wc->autocommit_timer);
779 if (list_empty(&wc->lru))
782 e = container_of(wc->lru.next, struct wc_entry, lru);
783 if (writecache_entry_is_committed(wc, e)) {
784 if (wc->overwrote_committed) {
785 writecache_wait_for_ios(wc, WRITE);
786 writecache_disk_flush(wc, wc->ssd_dev);
787 wc->overwrote_committed = false;
792 writecache_flush_entry(wc, e);
793 if (unlikely(e->lru.next == &wc->lru))
795 e2 = container_of(e->lru.next, struct wc_entry, lru);
796 if (writecache_entry_is_committed(wc, e2))
801 writecache_commit_flushed(wc, true);
804 pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
805 if (WC_MODE_PMEM(wc))
806 writecache_commit_flushed(wc, false);
808 ssd_commit_superblock(wc);
810 wc->overwrote_committed = false;
812 need_flush_after_free = false;
814 /* Free another committed entry with lower seq-count */
815 struct rb_node *rb_node = rb_prev(&e->rb_node);
818 e2 = container_of(rb_node, struct wc_entry, rb_node);
819 if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
820 likely(!e2->write_in_progress)) {
821 writecache_free_entry(wc, e2);
822 need_flush_after_free = true;
825 if (unlikely(e->lru.prev == &wc->lru))
827 e = container_of(e->lru.prev, struct wc_entry, lru);
831 if (need_flush_after_free)
832 writecache_commit_flushed(wc, false);
835 static void writecache_flush_work(struct work_struct *work)
837 struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
840 writecache_flush(wc);
844 static void writecache_autocommit_timer(struct timer_list *t)
846 struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
847 if (!writecache_has_error(wc))
848 queue_work(wc->writeback_wq, &wc->flush_work);
851 static void writecache_schedule_autocommit(struct dm_writecache *wc)
853 if (!timer_pending(&wc->autocommit_timer))
854 mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
857 static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
860 bool discarded_something = false;
862 e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
866 while (read_original_sector(wc, e) < end) {
867 struct rb_node *node = rb_next(&e->rb_node);
869 if (likely(!e->write_in_progress)) {
870 if (!discarded_something) {
871 if (!WC_MODE_PMEM(wc)) {
872 writecache_wait_for_ios(wc, READ);
873 writecache_wait_for_ios(wc, WRITE);
875 discarded_something = true;
877 if (!writecache_entry_is_committed(wc, e))
878 wc->uncommitted_blocks--;
879 writecache_free_entry(wc, e);
885 e = container_of(node, struct wc_entry, rb_node);
888 if (discarded_something)
889 writecache_commit_flushed(wc, false);
892 static bool writecache_wait_for_writeback(struct dm_writecache *wc)
894 if (wc->writeback_size) {
895 writecache_wait_on_freelist(wc);
901 static void writecache_suspend(struct dm_target *ti)
903 struct dm_writecache *wc = ti->private;
904 bool flush_on_suspend;
906 del_timer_sync(&wc->autocommit_timer);
907 del_timer_sync(&wc->max_age_timer);
910 writecache_flush(wc);
911 flush_on_suspend = wc->flush_on_suspend;
912 if (flush_on_suspend) {
913 wc->flush_on_suspend = false;
915 queue_work(wc->writeback_wq, &wc->writeback_work);
919 drain_workqueue(wc->writeback_wq);
922 if (flush_on_suspend)
924 while (writecache_wait_for_writeback(wc));
926 if (WC_MODE_PMEM(wc))
927 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
929 writecache_poison_lists(wc);
934 static int writecache_alloc_entries(struct dm_writecache *wc)
940 wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
943 for (b = 0; b < wc->n_blocks; b++) {
944 struct wc_entry *e = &wc->entries[b];
946 e->write_in_progress = false;
953 static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors)
955 struct dm_io_region region;
956 struct dm_io_request req;
958 region.bdev = wc->ssd_dev->bdev;
959 region.sector = wc->start_sector;
960 region.count = n_sectors;
961 req.bi_op = REQ_OP_READ;
962 req.bi_op_flags = REQ_SYNC;
963 req.mem.type = DM_IO_VMA;
964 req.mem.ptr.vma = (char *)wc->memory_map;
965 req.client = wc->dm_io;
966 req.notify.fn = NULL;
968 return dm_io(&req, 1, ®ion, NULL);
971 static void writecache_resume(struct dm_target *ti)
973 struct dm_writecache *wc = ti->private;
975 bool need_flush = false;
981 wc->data_device_sectors = i_size_read(wc->dev->bdev->bd_inode) >> SECTOR_SHIFT;
983 if (WC_MODE_PMEM(wc)) {
984 persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
986 r = writecache_read_metadata(wc, wc->metadata_sectors);
988 size_t sb_entries_offset;
989 writecache_error(wc, r, "unable to read metadata: %d", r);
990 sb_entries_offset = offsetof(struct wc_memory_superblock, entries);
991 memset((char *)wc->memory_map + sb_entries_offset, -1,
992 (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset);
997 INIT_LIST_HEAD(&wc->lru);
998 if (WC_MODE_SORT_FREELIST(wc)) {
999 wc->freetree = RB_ROOT;
1000 wc->current_free = NULL;
1002 INIT_LIST_HEAD(&wc->freelist);
1004 wc->freelist_size = 0;
1006 r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count,
1009 writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
1010 sb_seq_count = cpu_to_le64(0);
1012 wc->seq_count = le64_to_cpu(sb_seq_count);
1014 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
1015 for (b = 0; b < wc->n_blocks; b++) {
1016 struct wc_entry *e = &wc->entries[b];
1017 struct wc_memory_entry wme;
1018 if (writecache_has_error(wc)) {
1019 e->original_sector = -1;
1023 r = copy_mc_to_kernel(&wme, memory_entry(wc, e),
1024 sizeof(struct wc_memory_entry));
1026 writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
1027 (unsigned long)b, r);
1028 e->original_sector = -1;
1031 e->original_sector = le64_to_cpu(wme.original_sector);
1032 e->seq_count = le64_to_cpu(wme.seq_count);
1037 for (b = 0; b < wc->n_blocks; b++) {
1038 struct wc_entry *e = &wc->entries[b];
1039 if (!writecache_entry_is_committed(wc, e)) {
1040 if (read_seq_count(wc, e) != -1) {
1042 clear_seq_count(wc, e);
1045 writecache_add_to_freelist(wc, e);
1047 struct wc_entry *old;
1049 old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
1051 writecache_insert_entry(wc, e);
1053 if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
1054 writecache_error(wc, -EINVAL,
1055 "two identical entries, position %llu, sector %llu, sequence %llu",
1056 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
1057 (unsigned long long)read_seq_count(wc, e));
1059 if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
1062 writecache_free_entry(wc, old);
1063 writecache_insert_entry(wc, e);
1072 writecache_flush_all_metadata(wc);
1073 writecache_commit_flushed(wc, false);
1076 writecache_verify_watermark(wc);
1078 if (wc->max_age != MAX_AGE_UNSPECIFIED)
1079 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
1084 static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1090 if (dm_suspended(wc->ti)) {
1094 if (writecache_has_error(wc)) {
1099 writecache_flush(wc);
1100 wc->writeback_all++;
1101 queue_work(wc->writeback_wq, &wc->writeback_work);
1104 flush_workqueue(wc->writeback_wq);
1107 wc->writeback_all--;
1108 if (writecache_has_error(wc)) {
1117 static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1123 wc->flush_on_suspend = true;
1129 static void activate_cleaner(struct dm_writecache *wc)
1131 wc->flush_on_suspend = true;
1133 wc->freelist_high_watermark = wc->n_blocks;
1134 wc->freelist_low_watermark = wc->n_blocks;
1137 static int process_cleaner_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1143 activate_cleaner(wc);
1144 if (!dm_suspended(wc->ti))
1145 writecache_verify_watermark(wc);
1151 static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
1152 char *result, unsigned maxlen)
1155 struct dm_writecache *wc = ti->private;
1157 if (!strcasecmp(argv[0], "flush"))
1158 r = process_flush_mesg(argc, argv, wc);
1159 else if (!strcasecmp(argv[0], "flush_on_suspend"))
1160 r = process_flush_on_suspend_mesg(argc, argv, wc);
1161 else if (!strcasecmp(argv[0], "cleaner"))
1162 r = process_cleaner_mesg(argc, argv, wc);
1164 DMERR("unrecognised message received: %s", argv[0]);
1169 static void memcpy_flushcache_optimized(void *dest, void *source, size_t size)
1172 * clflushopt performs better with block size 1024, 2048, 4096
1173 * non-temporal stores perform better with block size 512
1175 * block size 512 1024 2048 4096
1176 * movnti 496 MB/s 642 MB/s 725 MB/s 744 MB/s
1177 * clflushopt 373 MB/s 688 MB/s 1.1 GB/s 1.2 GB/s
1179 * We see that movnti performs better for 512-byte blocks, and
1180 * clflushopt performs better for 1024-byte and larger blocks. So, we
1181 * prefer clflushopt for sizes >= 768.
1183 * NOTE: this happens to be the case now (with dm-writecache's single
1184 * threaded model) but re-evaluate this once memcpy_flushcache() is
1185 * enabled to use movdir64b which might invalidate this performance
1186 * advantage seen with cache-allocating-writes plus flushing.
1189 if (static_cpu_has(X86_FEATURE_CLFLUSHOPT) &&
1190 likely(boot_cpu_data.x86_clflush_size == 64) &&
1191 likely(size >= 768)) {
1193 memcpy((void *)dest, (void *)source, 64);
1194 clflushopt((void *)dest);
1198 } while (size >= 64);
1202 memcpy_flushcache(dest, source, size);
1205 static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
1208 unsigned long flags;
1210 int rw = bio_data_dir(bio);
1211 unsigned remaining_size = wc->block_size;
1214 struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
1215 buf = bvec_kmap_irq(&bv, &flags);
1217 if (unlikely(size > remaining_size))
1218 size = remaining_size;
1222 r = copy_mc_to_kernel(buf, data, size);
1223 flush_dcache_page(bio_page(bio));
1225 writecache_error(wc, r, "hardware memory error when reading data: %d", r);
1226 bio->bi_status = BLK_STS_IOERR;
1229 flush_dcache_page(bio_page(bio));
1230 memcpy_flushcache_optimized(data, buf, size);
1233 bvec_kunmap_irq(buf, &flags);
1235 data = (char *)data + size;
1236 remaining_size -= size;
1237 bio_advance(bio, size);
1238 } while (unlikely(remaining_size));
1241 static int writecache_flush_thread(void *data)
1243 struct dm_writecache *wc = data;
1249 bio = bio_list_pop(&wc->flush_list);
1251 set_current_state(TASK_INTERRUPTIBLE);
1254 if (unlikely(kthread_should_stop())) {
1255 set_current_state(TASK_RUNNING);
1263 if (bio_op(bio) == REQ_OP_DISCARD) {
1264 writecache_discard(wc, bio->bi_iter.bi_sector,
1265 bio_end_sector(bio));
1267 bio_set_dev(bio, wc->dev->bdev);
1268 submit_bio_noacct(bio);
1270 writecache_flush(wc);
1272 if (writecache_has_error(wc))
1273 bio->bi_status = BLK_STS_IOERR;
1281 static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
1283 if (bio_list_empty(&wc->flush_list))
1284 wake_up_process(wc->flush_thread);
1285 bio_list_add(&wc->flush_list, bio);
1288 static int writecache_map(struct dm_target *ti, struct bio *bio)
1291 struct dm_writecache *wc = ti->private;
1293 bio->bi_private = NULL;
1297 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1298 if (writecache_has_error(wc))
1300 if (WC_MODE_PMEM(wc)) {
1301 writecache_flush(wc);
1302 if (writecache_has_error(wc))
1304 if (unlikely(wc->cleaner))
1305 goto unlock_remap_origin;
1308 if (dm_bio_get_target_bio_nr(bio))
1309 goto unlock_remap_origin;
1310 writecache_offload_bio(wc, bio);
1315 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1317 if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
1318 (wc->block_size / 512 - 1)) != 0)) {
1319 DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
1320 (unsigned long long)bio->bi_iter.bi_sector,
1321 bio->bi_iter.bi_size, wc->block_size);
1325 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
1326 if (writecache_has_error(wc))
1328 if (WC_MODE_PMEM(wc)) {
1329 writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
1330 goto unlock_remap_origin;
1332 writecache_offload_bio(wc, bio);
1337 if (bio_data_dir(bio) == READ) {
1339 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1340 if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1341 if (WC_MODE_PMEM(wc)) {
1342 bio_copy_block(wc, bio, memory_data(wc, e));
1343 if (bio->bi_iter.bi_size)
1344 goto read_next_block;
1347 dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1348 bio_set_dev(bio, wc->ssd_dev->bdev);
1349 bio->bi_iter.bi_sector = cache_sector(wc, e);
1350 if (!writecache_entry_is_committed(wc, e))
1351 writecache_wait_for_ios(wc, WRITE);
1356 sector_t next_boundary =
1357 read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1358 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
1359 dm_accept_partial_bio(bio, next_boundary);
1362 goto unlock_remap_origin;
1366 bool found_entry = false;
1367 bool search_used = false;
1368 if (writecache_has_error(wc))
1370 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1372 if (!writecache_entry_is_committed(wc, e)) {
1376 if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1377 wc->overwrote_committed = true;
1383 if (unlikely(wc->cleaner))
1386 e = writecache_pop_from_freelist(wc, (sector_t)-1);
1388 if (!WC_MODE_PMEM(wc) && !found_entry) {
1390 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1392 sector_t next_boundary = read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1393 BUG_ON(!next_boundary);
1394 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
1395 dm_accept_partial_bio(bio, next_boundary);
1398 goto unlock_remap_origin;
1400 writecache_wait_on_freelist(wc);
1403 write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1404 writecache_insert_entry(wc, e);
1405 wc->uncommitted_blocks++;
1407 if (WC_MODE_PMEM(wc)) {
1408 bio_copy_block(wc, bio, memory_data(wc, e));
1410 unsigned bio_size = wc->block_size;
1411 sector_t start_cache_sec = cache_sector(wc, e);
1412 sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT);
1414 while (bio_size < bio->bi_iter.bi_size) {
1416 struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec);
1419 write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector +
1420 (bio_size >> SECTOR_SHIFT), wc->seq_count);
1421 writecache_insert_entry(wc, f);
1422 wc->uncommitted_blocks++;
1425 struct rb_node *next = rb_next(&e->rb_node);
1428 f = container_of(next, struct wc_entry, rb_node);
1431 if (read_original_sector(wc, f) !=
1432 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1434 if (unlikely(f->write_in_progress))
1436 if (writecache_entry_is_committed(wc, f))
1437 wc->overwrote_committed = true;
1440 bio_size += wc->block_size;
1441 current_cache_sec += wc->block_size >> SECTOR_SHIFT;
1444 bio_set_dev(bio, wc->ssd_dev->bdev);
1445 bio->bi_iter.bi_sector = start_cache_sec;
1446 dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT);
1448 if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1449 wc->uncommitted_blocks = 0;
1450 queue_work(wc->writeback_wq, &wc->flush_work);
1452 writecache_schedule_autocommit(wc);
1456 } while (bio->bi_iter.bi_size);
1458 if (unlikely(bio->bi_opf & REQ_FUA ||
1459 wc->uncommitted_blocks >= wc->autocommit_blocks))
1460 writecache_flush(wc);
1462 writecache_schedule_autocommit(wc);
1466 unlock_remap_origin:
1467 bio_set_dev(bio, wc->dev->bdev);
1469 return DM_MAPIO_REMAPPED;
1472 /* make sure that writecache_end_io decrements bio_in_progress: */
1473 bio->bi_private = (void *)1;
1474 atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
1476 return DM_MAPIO_REMAPPED;
1481 return DM_MAPIO_SUBMITTED;
1485 return DM_MAPIO_SUBMITTED;
1490 return DM_MAPIO_SUBMITTED;
1493 static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
1495 struct dm_writecache *wc = ti->private;
1497 if (bio->bi_private != NULL) {
1498 int dir = bio_data_dir(bio);
1499 if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
1500 if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
1501 wake_up(&wc->bio_in_progress_wait[dir]);
1506 static int writecache_iterate_devices(struct dm_target *ti,
1507 iterate_devices_callout_fn fn, void *data)
1509 struct dm_writecache *wc = ti->private;
1511 return fn(ti, wc->dev, 0, ti->len, data);
1514 static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
1516 struct dm_writecache *wc = ti->private;
1518 if (limits->logical_block_size < wc->block_size)
1519 limits->logical_block_size = wc->block_size;
1521 if (limits->physical_block_size < wc->block_size)
1522 limits->physical_block_size = wc->block_size;
1524 if (limits->io_min < wc->block_size)
1525 limits->io_min = wc->block_size;
1529 static void writecache_writeback_endio(struct bio *bio)
1531 struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
1532 struct dm_writecache *wc = wb->wc;
1533 unsigned long flags;
1535 raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
1536 if (unlikely(list_empty(&wc->endio_list)))
1537 wake_up_process(wc->endio_thread);
1538 list_add_tail(&wb->endio_entry, &wc->endio_list);
1539 raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
1542 static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
1544 struct copy_struct *c = ptr;
1545 struct dm_writecache *wc = c->wc;
1547 c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
1549 raw_spin_lock_irq(&wc->endio_list_lock);
1550 if (unlikely(list_empty(&wc->endio_list)))
1551 wake_up_process(wc->endio_thread);
1552 list_add_tail(&c->endio_entry, &wc->endio_list);
1553 raw_spin_unlock_irq(&wc->endio_list_lock);
1556 static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
1559 struct writeback_struct *wb;
1561 unsigned long n_walked = 0;
1564 wb = list_entry(list->next, struct writeback_struct, endio_entry);
1565 list_del(&wb->endio_entry);
1567 if (unlikely(wb->bio.bi_status != BLK_STS_OK))
1568 writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
1569 "write error %d", wb->bio.bi_status);
1573 BUG_ON(!e->write_in_progress);
1574 e->write_in_progress = false;
1575 INIT_LIST_HEAD(&e->lru);
1576 if (!writecache_has_error(wc))
1577 writecache_free_entry(wc, e);
1578 BUG_ON(!wc->writeback_size);
1579 wc->writeback_size--;
1581 if (unlikely(n_walked >= ENDIO_LATENCY)) {
1582 writecache_commit_flushed(wc, false);
1587 } while (++i < wb->wc_list_n);
1589 if (wb->wc_list != wb->wc_list_inline)
1592 } while (!list_empty(list));
1595 static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
1597 struct copy_struct *c;
1601 c = list_entry(list->next, struct copy_struct, endio_entry);
1602 list_del(&c->endio_entry);
1604 if (unlikely(c->error))
1605 writecache_error(wc, c->error, "copy error");
1609 BUG_ON(!e->write_in_progress);
1610 e->write_in_progress = false;
1611 INIT_LIST_HEAD(&e->lru);
1612 if (!writecache_has_error(wc))
1613 writecache_free_entry(wc, e);
1615 BUG_ON(!wc->writeback_size);
1616 wc->writeback_size--;
1618 } while (--c->n_entries);
1619 mempool_free(c, &wc->copy_pool);
1620 } while (!list_empty(list));
1623 static int writecache_endio_thread(void *data)
1625 struct dm_writecache *wc = data;
1628 struct list_head list;
1630 raw_spin_lock_irq(&wc->endio_list_lock);
1631 if (!list_empty(&wc->endio_list))
1633 set_current_state(TASK_INTERRUPTIBLE);
1634 raw_spin_unlock_irq(&wc->endio_list_lock);
1636 if (unlikely(kthread_should_stop())) {
1637 set_current_state(TASK_RUNNING);
1646 list = wc->endio_list;
1647 list.next->prev = list.prev->next = &list;
1648 INIT_LIST_HEAD(&wc->endio_list);
1649 raw_spin_unlock_irq(&wc->endio_list_lock);
1651 if (!WC_MODE_FUA(wc))
1652 writecache_disk_flush(wc, wc->dev);
1656 if (WC_MODE_PMEM(wc)) {
1657 __writecache_endio_pmem(wc, &list);
1659 __writecache_endio_ssd(wc, &list);
1660 writecache_wait_for_ios(wc, READ);
1663 writecache_commit_flushed(wc, false);
1671 static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e, gfp_t gfp)
1673 struct dm_writecache *wc = wb->wc;
1674 unsigned block_size = wc->block_size;
1675 void *address = memory_data(wc, e);
1677 persistent_memory_flush_cache(address, block_size);
1679 if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors))
1682 return bio_add_page(&wb->bio, persistent_memory_page(address),
1683 block_size, persistent_memory_page_offset(address)) != 0;
1686 struct writeback_list {
1687 struct list_head list;
1691 static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
1693 if (unlikely(wc->max_writeback_jobs)) {
1694 if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
1696 while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
1697 writecache_wait_on_freelist(wc);
1704 static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
1706 struct wc_entry *e, *f;
1708 struct writeback_struct *wb;
1713 e = container_of(wbl->list.prev, struct wc_entry, lru);
1716 max_pages = e->wc_list_contiguous;
1718 bio = bio_alloc_bioset(GFP_NOIO, max_pages, &wc->bio_set);
1719 wb = container_of(bio, struct writeback_struct, bio);
1721 bio->bi_end_io = writecache_writeback_endio;
1722 bio_set_dev(bio, wc->dev->bdev);
1723 bio->bi_iter.bi_sector = read_original_sector(wc, e);
1724 if (max_pages <= WB_LIST_INLINE ||
1725 unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
1726 GFP_NOIO | __GFP_NORETRY |
1727 __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
1728 wb->wc_list = wb->wc_list_inline;
1729 max_pages = WB_LIST_INLINE;
1732 BUG_ON(!wc_add_block(wb, e, GFP_NOIO));
1737 while (wbl->size && wb->wc_list_n < max_pages) {
1738 f = container_of(wbl->list.prev, struct wc_entry, lru);
1739 if (read_original_sector(wc, f) !=
1740 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1742 if (!wc_add_block(wb, f, GFP_NOWAIT | __GFP_NOWARN))
1746 wb->wc_list[wb->wc_list_n++] = f;
1749 bio_set_op_attrs(bio, REQ_OP_WRITE, WC_MODE_FUA(wc) * REQ_FUA);
1750 if (writecache_has_error(wc)) {
1751 bio->bi_status = BLK_STS_IOERR;
1753 } else if (unlikely(!bio_sectors(bio))) {
1754 bio->bi_status = BLK_STS_OK;
1760 __writeback_throttle(wc, wbl);
1764 static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
1766 struct wc_entry *e, *f;
1767 struct dm_io_region from, to;
1768 struct copy_struct *c;
1774 e = container_of(wbl->list.prev, struct wc_entry, lru);
1777 n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
1779 from.bdev = wc->ssd_dev->bdev;
1780 from.sector = cache_sector(wc, e);
1781 from.count = n_sectors;
1782 to.bdev = wc->dev->bdev;
1783 to.sector = read_original_sector(wc, e);
1784 to.count = n_sectors;
1786 c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
1789 c->n_entries = e->wc_list_contiguous;
1791 while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
1793 f = container_of(wbl->list.prev, struct wc_entry, lru);
1799 if (unlikely(to.sector + to.count > wc->data_device_sectors)) {
1800 if (to.sector >= wc->data_device_sectors) {
1801 writecache_copy_endio(0, 0, c);
1804 from.count = to.count = wc->data_device_sectors - to.sector;
1807 dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
1809 __writeback_throttle(wc, wbl);
1813 static void writecache_writeback(struct work_struct *work)
1815 struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
1816 struct blk_plug plug;
1817 struct wc_entry *f, *g, *e = NULL;
1818 struct rb_node *node, *next_node;
1819 struct list_head skipped;
1820 struct writeback_list wbl;
1821 unsigned long n_walked;
1825 if (writecache_has_error(wc)) {
1830 if (unlikely(wc->writeback_all)) {
1831 if (writecache_wait_for_writeback(wc))
1835 if (wc->overwrote_committed) {
1836 writecache_wait_for_ios(wc, WRITE);
1840 INIT_LIST_HEAD(&skipped);
1841 INIT_LIST_HEAD(&wbl.list);
1843 while (!list_empty(&wc->lru) &&
1844 (wc->writeback_all ||
1845 wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark ||
1846 (jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >=
1847 wc->max_age - wc->max_age / MAX_AGE_DIV))) {
1850 if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1851 likely(!wc->writeback_all) && likely(!dm_suspended(wc->ti))) {
1852 queue_work(wc->writeback_wq, &wc->writeback_work);
1856 if (unlikely(wc->writeback_all)) {
1858 writecache_flush(wc);
1859 e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
1863 e = container_of(wc->lru.prev, struct wc_entry, lru);
1864 BUG_ON(e->write_in_progress);
1865 if (unlikely(!writecache_entry_is_committed(wc, e))) {
1866 writecache_flush(wc);
1868 node = rb_prev(&e->rb_node);
1870 f = container_of(node, struct wc_entry, rb_node);
1871 if (unlikely(read_original_sector(wc, f) ==
1872 read_original_sector(wc, e))) {
1873 BUG_ON(!f->write_in_progress);
1875 list_add(&e->lru, &skipped);
1880 wc->writeback_size++;
1882 list_add(&e->lru, &wbl.list);
1884 e->write_in_progress = true;
1885 e->wc_list_contiguous = 1;
1890 next_node = rb_next(&f->rb_node);
1891 if (unlikely(!next_node))
1893 g = container_of(next_node, struct wc_entry, rb_node);
1894 if (unlikely(read_original_sector(wc, g) ==
1895 read_original_sector(wc, f))) {
1899 if (read_original_sector(wc, g) !=
1900 read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
1902 if (unlikely(g->write_in_progress))
1904 if (unlikely(!writecache_entry_is_committed(wc, g)))
1907 if (!WC_MODE_PMEM(wc)) {
1913 //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
1916 wc->writeback_size++;
1918 list_add(&g->lru, &wbl.list);
1920 g->write_in_progress = true;
1921 g->wc_list_contiguous = BIO_MAX_PAGES;
1923 e->wc_list_contiguous++;
1924 if (unlikely(e->wc_list_contiguous == BIO_MAX_PAGES)) {
1925 if (unlikely(wc->writeback_all)) {
1926 next_node = rb_next(&f->rb_node);
1927 if (likely(next_node))
1928 g = container_of(next_node, struct wc_entry, rb_node);
1936 if (!list_empty(&skipped)) {
1937 list_splice_tail(&skipped, &wc->lru);
1939 * If we didn't do any progress, we must wait until some
1940 * writeback finishes to avoid burning CPU in a loop
1942 if (unlikely(!wbl.size))
1943 writecache_wait_for_writeback(wc);
1948 blk_start_plug(&plug);
1950 if (WC_MODE_PMEM(wc))
1951 __writecache_writeback_pmem(wc, &wbl);
1953 __writecache_writeback_ssd(wc, &wbl);
1955 blk_finish_plug(&plug);
1957 if (unlikely(wc->writeback_all)) {
1959 while (writecache_wait_for_writeback(wc));
1964 static int calculate_memory_size(uint64_t device_size, unsigned block_size,
1965 size_t *n_blocks_p, size_t *n_metadata_blocks_p)
1967 uint64_t n_blocks, offset;
1970 n_blocks = device_size;
1971 do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
1976 /* Verify the following entries[n_blocks] won't overflow */
1977 if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
1978 sizeof(struct wc_memory_entry)))
1980 offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
1981 offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
1982 if (offset + n_blocks * block_size <= device_size)
1987 /* check if the bit field overflows */
1989 if (e.index != n_blocks)
1993 *n_blocks_p = n_blocks;
1994 if (n_metadata_blocks_p)
1995 *n_metadata_blocks_p = offset >> __ffs(block_size);
1999 static int init_memory(struct dm_writecache *wc)
2004 r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
2008 r = writecache_alloc_entries(wc);
2012 for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
2013 pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
2014 pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
2015 pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
2016 pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
2017 pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
2019 for (b = 0; b < wc->n_blocks; b++) {
2020 write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
2024 writecache_flush_all_metadata(wc);
2025 writecache_commit_flushed(wc, false);
2026 pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
2027 writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
2028 writecache_commit_flushed(wc, false);
2033 static void writecache_dtr(struct dm_target *ti)
2035 struct dm_writecache *wc = ti->private;
2040 if (wc->endio_thread)
2041 kthread_stop(wc->endio_thread);
2043 if (wc->flush_thread)
2044 kthread_stop(wc->flush_thread);
2046 bioset_exit(&wc->bio_set);
2048 mempool_exit(&wc->copy_pool);
2050 if (wc->writeback_wq)
2051 destroy_workqueue(wc->writeback_wq);
2054 dm_put_device(ti, wc->dev);
2057 dm_put_device(ti, wc->ssd_dev);
2062 if (wc->memory_map) {
2063 if (WC_MODE_PMEM(wc))
2064 persistent_memory_release(wc);
2066 vfree(wc->memory_map);
2070 dm_kcopyd_client_destroy(wc->dm_kcopyd);
2073 dm_io_client_destroy(wc->dm_io);
2075 if (wc->dirty_bitmap)
2076 vfree(wc->dirty_bitmap);
2081 static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2083 struct dm_writecache *wc;
2084 struct dm_arg_set as;
2086 unsigned opt_params;
2087 size_t offset, data_size;
2090 int high_wm_percent = HIGH_WATERMARK;
2091 int low_wm_percent = LOW_WATERMARK;
2093 struct wc_memory_superblock s;
2095 static struct dm_arg _args[] = {
2096 {0, 16, "Invalid number of feature args"},
2102 wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
2104 ti->error = "Cannot allocate writecache structure";
2111 mutex_init(&wc->lock);
2112 wc->max_age = MAX_AGE_UNSPECIFIED;
2113 writecache_poison_lists(wc);
2114 init_waitqueue_head(&wc->freelist_wait);
2115 timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
2116 timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0);
2118 for (i = 0; i < 2; i++) {
2119 atomic_set(&wc->bio_in_progress[i], 0);
2120 init_waitqueue_head(&wc->bio_in_progress_wait[i]);
2123 wc->dm_io = dm_io_client_create();
2124 if (IS_ERR(wc->dm_io)) {
2125 r = PTR_ERR(wc->dm_io);
2126 ti->error = "Unable to allocate dm-io client";
2131 wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
2132 if (!wc->writeback_wq) {
2134 ti->error = "Could not allocate writeback workqueue";
2137 INIT_WORK(&wc->writeback_work, writecache_writeback);
2138 INIT_WORK(&wc->flush_work, writecache_flush_work);
2140 raw_spin_lock_init(&wc->endio_list_lock);
2141 INIT_LIST_HEAD(&wc->endio_list);
2142 wc->endio_thread = kthread_create(writecache_endio_thread, wc, "writecache_endio");
2143 if (IS_ERR(wc->endio_thread)) {
2144 r = PTR_ERR(wc->endio_thread);
2145 wc->endio_thread = NULL;
2146 ti->error = "Couldn't spawn endio thread";
2149 wake_up_process(wc->endio_thread);
2152 * Parse the mode (pmem or ssd)
2154 string = dm_shift_arg(&as);
2158 if (!strcasecmp(string, "s")) {
2159 wc->pmem_mode = false;
2160 } else if (!strcasecmp(string, "p")) {
2161 #ifdef DM_WRITECACHE_HAS_PMEM
2162 wc->pmem_mode = true;
2163 wc->writeback_fua = true;
2166 * If the architecture doesn't support persistent memory or
2167 * the kernel doesn't support any DAX drivers, this driver can
2168 * only be used in SSD-only mode.
2171 ti->error = "Persistent memory or DAX not supported on this system";
2178 if (WC_MODE_PMEM(wc)) {
2179 r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
2180 offsetof(struct writeback_struct, bio),
2183 ti->error = "Could not allocate bio set";
2187 r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
2189 ti->error = "Could not allocate mempool";
2195 * Parse the origin data device
2197 string = dm_shift_arg(&as);
2200 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
2202 ti->error = "Origin data device lookup failed";
2207 * Parse cache data device (be it pmem or ssd)
2209 string = dm_shift_arg(&as);
2213 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
2215 ti->error = "Cache data device lookup failed";
2218 wc->memory_map_size = i_size_read(wc->ssd_dev->bdev->bd_inode);
2221 * Parse the cache block size
2223 string = dm_shift_arg(&as);
2226 if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
2227 wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
2228 (wc->block_size & (wc->block_size - 1))) {
2230 ti->error = "Invalid block size";
2233 if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) ||
2234 wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) {
2236 ti->error = "Block size is smaller than device logical block size";
2239 wc->block_size_bits = __ffs(wc->block_size);
2241 wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
2242 wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
2243 wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
2246 * Parse optional arguments
2248 r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2252 while (opt_params) {
2253 string = dm_shift_arg(&as), opt_params--;
2254 if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2255 unsigned long long start_sector;
2256 string = dm_shift_arg(&as), opt_params--;
2257 if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2258 goto invalid_optional;
2259 wc->start_sector = start_sector;
2260 wc->start_sector_set = true;
2261 if (wc->start_sector != start_sector ||
2262 wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2263 goto invalid_optional;
2264 } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
2265 string = dm_shift_arg(&as), opt_params--;
2266 if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
2267 goto invalid_optional;
2268 if (high_wm_percent < 0 || high_wm_percent > 100)
2269 goto invalid_optional;
2270 wc->high_wm_percent_value = high_wm_percent;
2271 wc->high_wm_percent_set = true;
2272 } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
2273 string = dm_shift_arg(&as), opt_params--;
2274 if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
2275 goto invalid_optional;
2276 if (low_wm_percent < 0 || low_wm_percent > 100)
2277 goto invalid_optional;
2278 wc->low_wm_percent_value = low_wm_percent;
2279 wc->low_wm_percent_set = true;
2280 } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
2281 string = dm_shift_arg(&as), opt_params--;
2282 if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
2283 goto invalid_optional;
2284 wc->max_writeback_jobs_set = true;
2285 } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
2286 string = dm_shift_arg(&as), opt_params--;
2287 if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
2288 goto invalid_optional;
2289 wc->autocommit_blocks_set = true;
2290 } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
2291 unsigned autocommit_msecs;
2292 string = dm_shift_arg(&as), opt_params--;
2293 if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
2294 goto invalid_optional;
2295 if (autocommit_msecs > 3600000)
2296 goto invalid_optional;
2297 wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2298 wc->autocommit_time_value = autocommit_msecs;
2299 wc->autocommit_time_set = true;
2300 } else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
2301 unsigned max_age_msecs;
2302 string = dm_shift_arg(&as), opt_params--;
2303 if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
2304 goto invalid_optional;
2305 if (max_age_msecs > 86400000)
2306 goto invalid_optional;
2307 wc->max_age = msecs_to_jiffies(max_age_msecs);
2308 wc->max_age_set = true;
2309 wc->max_age_value = max_age_msecs;
2310 } else if (!strcasecmp(string, "cleaner")) {
2311 wc->cleaner_set = true;
2313 } else if (!strcasecmp(string, "fua")) {
2314 if (WC_MODE_PMEM(wc)) {
2315 wc->writeback_fua = true;
2316 wc->writeback_fua_set = true;
2317 } else goto invalid_optional;
2318 } else if (!strcasecmp(string, "nofua")) {
2319 if (WC_MODE_PMEM(wc)) {
2320 wc->writeback_fua = false;
2321 wc->writeback_fua_set = true;
2322 } else goto invalid_optional;
2326 ti->error = "Invalid optional argument";
2331 if (high_wm_percent < low_wm_percent) {
2333 ti->error = "High watermark must be greater than or equal to low watermark";
2337 if (WC_MODE_PMEM(wc)) {
2338 if (!dax_synchronous(wc->ssd_dev->dax_dev)) {
2340 ti->error = "Asynchronous persistent memory not supported as pmem cache";
2344 r = persistent_memory_claim(wc);
2346 ti->error = "Unable to map persistent memory for cache";
2350 size_t n_blocks, n_metadata_blocks;
2351 uint64_t n_bitmap_bits;
2353 wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2355 bio_list_init(&wc->flush_list);
2356 wc->flush_thread = kthread_create(writecache_flush_thread, wc, "dm_writecache_flush");
2357 if (IS_ERR(wc->flush_thread)) {
2358 r = PTR_ERR(wc->flush_thread);
2359 wc->flush_thread = NULL;
2360 ti->error = "Couldn't spawn flush thread";
2363 wake_up_process(wc->flush_thread);
2365 r = calculate_memory_size(wc->memory_map_size, wc->block_size,
2366 &n_blocks, &n_metadata_blocks);
2368 ti->error = "Invalid device size";
2372 n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
2373 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
2374 /* this is limitation of test_bit functions */
2375 if (n_bitmap_bits > 1U << 31) {
2377 ti->error = "Invalid device size";
2381 wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
2382 if (!wc->memory_map) {
2384 ti->error = "Unable to allocate memory for metadata";
2388 wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2389 if (IS_ERR(wc->dm_kcopyd)) {
2390 r = PTR_ERR(wc->dm_kcopyd);
2391 ti->error = "Unable to allocate dm-kcopyd client";
2392 wc->dm_kcopyd = NULL;
2396 wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
2397 wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
2398 BITS_PER_LONG * sizeof(unsigned long);
2399 wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
2400 if (!wc->dirty_bitmap) {
2402 ti->error = "Unable to allocate dirty bitmap";
2406 r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT);
2408 ti->error = "Unable to read first block of metadata";
2413 r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock));
2415 ti->error = "Hardware memory error when reading superblock";
2418 if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
2419 r = init_memory(wc);
2421 ti->error = "Unable to initialize device";
2424 r = copy_mc_to_kernel(&s, sb(wc),
2425 sizeof(struct wc_memory_superblock));
2427 ti->error = "Hardware memory error when reading superblock";
2432 if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
2433 ti->error = "Invalid magic in the superblock";
2438 if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
2439 ti->error = "Invalid version in the superblock";
2444 if (le32_to_cpu(s.block_size) != wc->block_size) {
2445 ti->error = "Block size does not match superblock";
2450 wc->n_blocks = le64_to_cpu(s.n_blocks);
2452 offset = wc->n_blocks * sizeof(struct wc_memory_entry);
2453 if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
2455 ti->error = "Overflow in size calculation";
2459 offset += sizeof(struct wc_memory_superblock);
2460 if (offset < sizeof(struct wc_memory_superblock))
2462 offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
2463 data_size = wc->n_blocks * (size_t)wc->block_size;
2464 if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
2465 (offset + data_size < offset))
2467 if (offset + data_size > wc->memory_map_size) {
2468 ti->error = "Memory area is too small";
2473 wc->metadata_sectors = offset >> SECTOR_SHIFT;
2474 wc->block_start = (char *)sb(wc) + offset;
2476 x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
2479 wc->freelist_high_watermark = x;
2480 x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
2483 wc->freelist_low_watermark = x;
2486 activate_cleaner(wc);
2488 r = writecache_alloc_entries(wc);
2490 ti->error = "Cannot allocate memory";
2494 ti->num_flush_bios = WC_MODE_PMEM(wc) ? 1 : 2;
2495 ti->flush_supported = true;
2496 ti->num_discard_bios = 1;
2498 if (WC_MODE_PMEM(wc))
2499 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
2505 ti->error = "Bad arguments";
2511 static void writecache_status(struct dm_target *ti, status_type_t type,
2512 unsigned status_flags, char *result, unsigned maxlen)
2514 struct dm_writecache *wc = ti->private;
2515 unsigned extra_args;
2519 case STATUSTYPE_INFO:
2520 DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),
2521 (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2522 (unsigned long long)wc->writeback_size);
2524 case STATUSTYPE_TABLE:
2525 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
2526 wc->dev->name, wc->ssd_dev->name, wc->block_size);
2528 if (wc->start_sector_set)
2530 if (wc->high_wm_percent_set)
2532 if (wc->low_wm_percent_set)
2534 if (wc->max_writeback_jobs_set)
2536 if (wc->autocommit_blocks_set)
2538 if (wc->autocommit_time_set)
2540 if (wc->max_age_set)
2542 if (wc->cleaner_set)
2544 if (wc->writeback_fua_set)
2547 DMEMIT("%u", extra_args);
2548 if (wc->start_sector_set)
2549 DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2550 if (wc->high_wm_percent_set)
2551 DMEMIT(" high_watermark %u", wc->high_wm_percent_value);
2552 if (wc->low_wm_percent_set)
2553 DMEMIT(" low_watermark %u", wc->low_wm_percent_value);
2554 if (wc->max_writeback_jobs_set)
2555 DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
2556 if (wc->autocommit_blocks_set)
2557 DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
2558 if (wc->autocommit_time_set)
2559 DMEMIT(" autocommit_time %u", wc->autocommit_time_value);
2560 if (wc->max_age_set)
2561 DMEMIT(" max_age %u", wc->max_age_value);
2562 if (wc->cleaner_set)
2564 if (wc->writeback_fua_set)
2565 DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2570 static struct target_type writecache_target = {
2571 .name = "writecache",
2572 .version = {1, 4, 0},
2573 .module = THIS_MODULE,
2574 .ctr = writecache_ctr,
2575 .dtr = writecache_dtr,
2576 .status = writecache_status,
2577 .postsuspend = writecache_suspend,
2578 .resume = writecache_resume,
2579 .message = writecache_message,
2580 .map = writecache_map,
2581 .end_io = writecache_end_io,
2582 .iterate_devices = writecache_iterate_devices,
2583 .io_hints = writecache_io_hints,
2586 static int __init dm_writecache_init(void)
2590 r = dm_register_target(&writecache_target);
2592 DMERR("register failed %d", r);
2599 static void __exit dm_writecache_exit(void)
2601 dm_unregister_target(&writecache_target);
2604 module_init(dm_writecache_init);
2605 module_exit(dm_writecache_exit);
2607 MODULE_DESCRIPTION(DM_NAME " writecache target");
2608 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2609 MODULE_LICENSE("GPL");