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>
18 #include <linux/delay.h>
19 #include "dm-io-tracker.h"
21 #define DM_MSG_PREFIX "writecache"
23 #define HIGH_WATERMARK 50
24 #define LOW_WATERMARK 45
25 #define MAX_WRITEBACK_JOBS 0
26 #define ENDIO_LATENCY 16
27 #define WRITEBACK_LATENCY 64
28 #define AUTOCOMMIT_BLOCKS_SSD 65536
29 #define AUTOCOMMIT_BLOCKS_PMEM 64
30 #define AUTOCOMMIT_MSEC 1000
31 #define MAX_AGE_DIV 16
32 #define MAX_AGE_UNSPECIFIED -1UL
33 #define PAUSE_WRITEBACK (HZ * 3)
35 #define BITMAP_GRANULARITY 65536
36 #if BITMAP_GRANULARITY < PAGE_SIZE
37 #undef BITMAP_GRANULARITY
38 #define BITMAP_GRANULARITY PAGE_SIZE
41 #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_DAX_DRIVER)
42 #define DM_WRITECACHE_HAS_PMEM
45 #ifdef DM_WRITECACHE_HAS_PMEM
46 #define pmem_assign(dest, src) \
48 typeof(dest) uniq = (src); \
49 memcpy_flushcache(&(dest), &uniq, sizeof(dest)); \
52 #define pmem_assign(dest, src) ((dest) = (src))
55 #if IS_ENABLED(CONFIG_ARCH_HAS_COPY_MC) && defined(DM_WRITECACHE_HAS_PMEM)
56 #define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
59 #define MEMORY_SUPERBLOCK_MAGIC 0x23489321
60 #define MEMORY_SUPERBLOCK_VERSION 1
62 struct wc_memory_entry {
63 __le64 original_sector;
67 struct wc_memory_superblock {
79 struct wc_memory_entry entries[];
83 struct rb_node rb_node;
85 unsigned short wc_list_contiguous;
86 bool write_in_progress
87 #if BITS_PER_LONG == 64
92 #if BITS_PER_LONG == 64
97 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
98 uint64_t original_sector;
103 #ifdef DM_WRITECACHE_HAS_PMEM
104 #define WC_MODE_PMEM(wc) ((wc)->pmem_mode)
105 #define WC_MODE_FUA(wc) ((wc)->writeback_fua)
107 #define WC_MODE_PMEM(wc) false
108 #define WC_MODE_FUA(wc) false
110 #define WC_MODE_SORT_FREELIST(wc) (!WC_MODE_PMEM(wc))
112 struct dm_writecache {
114 struct list_head lru;
116 struct list_head freelist;
118 struct rb_root freetree;
119 struct wc_entry *current_free;
124 size_t freelist_size;
125 size_t writeback_size;
126 size_t freelist_high_watermark;
127 size_t freelist_low_watermark;
128 unsigned long max_age;
131 unsigned uncommitted_blocks;
132 unsigned autocommit_blocks;
133 unsigned max_writeback_jobs;
137 unsigned long autocommit_jiffies;
138 struct timer_list autocommit_timer;
139 struct wait_queue_head freelist_wait;
141 struct timer_list max_age_timer;
143 atomic_t bio_in_progress[2];
144 struct wait_queue_head bio_in_progress_wait[2];
146 struct dm_target *ti;
148 struct dm_dev *ssd_dev;
149 sector_t start_sector;
151 uint64_t memory_map_size;
152 size_t metadata_sectors;
155 sector_t data_device_sectors;
157 struct wc_entry *entries;
159 unsigned char block_size_bits;
162 bool writeback_fua:1;
164 bool overwrote_committed:1;
165 bool memory_vmapped:1;
167 bool start_sector_set:1;
168 bool high_wm_percent_set:1;
169 bool low_wm_percent_set:1;
170 bool max_writeback_jobs_set:1;
171 bool autocommit_blocks_set:1;
172 bool autocommit_time_set:1;
174 bool writeback_fua_set:1;
175 bool flush_on_suspend:1;
178 bool metadata_only:1;
181 unsigned high_wm_percent_value;
182 unsigned low_wm_percent_value;
183 unsigned autocommit_time_value;
184 unsigned max_age_value;
185 unsigned pause_value;
187 unsigned writeback_all;
188 struct workqueue_struct *writeback_wq;
189 struct work_struct writeback_work;
190 struct work_struct flush_work;
192 struct dm_io_tracker iot;
194 struct dm_io_client *dm_io;
196 raw_spinlock_t endio_list_lock;
197 struct list_head endio_list;
198 struct task_struct *endio_thread;
200 struct task_struct *flush_thread;
201 struct bio_list flush_list;
203 struct dm_kcopyd_client *dm_kcopyd;
204 unsigned long *dirty_bitmap;
205 unsigned dirty_bitmap_size;
207 struct bio_set bio_set;
211 #define WB_LIST_INLINE 16
213 struct writeback_struct {
214 struct list_head endio_entry;
215 struct dm_writecache *wc;
216 struct wc_entry **wc_list;
218 struct wc_entry *wc_list_inline[WB_LIST_INLINE];
223 struct list_head endio_entry;
224 struct dm_writecache *wc;
230 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
231 "A percentage of time allocated for data copying");
233 static void wc_lock(struct dm_writecache *wc)
235 mutex_lock(&wc->lock);
238 static void wc_unlock(struct dm_writecache *wc)
240 mutex_unlock(&wc->lock);
243 #ifdef DM_WRITECACHE_HAS_PMEM
244 static int persistent_memory_claim(struct dm_writecache *wc)
254 wc->memory_vmapped = false;
256 s = wc->memory_map_size;
262 if (p != s >> PAGE_SHIFT) {
267 offset = get_start_sect(wc->ssd_dev->bdev);
268 if (offset & (PAGE_SIZE / 512 - 1)) {
272 offset >>= PAGE_SHIFT - 9;
274 id = dax_read_lock();
276 da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, &wc->memory_map, &pfn);
278 wc->memory_map = NULL;
282 if (!pfn_t_has_page(pfn)) {
283 wc->memory_map = NULL;
289 wc->memory_map = NULL;
290 pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
298 daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i, p - i,
301 r = daa ? daa : -EINVAL;
304 if (!pfn_t_has_page(pfn)) {
308 while (daa-- && i < p) {
309 pages[i++] = pfn_t_to_page(pfn);
315 wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
316 if (!wc->memory_map) {
321 wc->memory_vmapped = true;
326 wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
327 wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
338 static int persistent_memory_claim(struct dm_writecache *wc)
344 static void persistent_memory_release(struct dm_writecache *wc)
346 if (wc->memory_vmapped)
347 vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
350 static struct page *persistent_memory_page(void *addr)
352 if (is_vmalloc_addr(addr))
353 return vmalloc_to_page(addr);
355 return virt_to_page(addr);
358 static unsigned persistent_memory_page_offset(void *addr)
360 return (unsigned long)addr & (PAGE_SIZE - 1);
363 static void persistent_memory_flush_cache(void *ptr, size_t size)
365 if (is_vmalloc_addr(ptr))
366 flush_kernel_vmap_range(ptr, size);
369 static void persistent_memory_invalidate_cache(void *ptr, size_t size)
371 if (is_vmalloc_addr(ptr))
372 invalidate_kernel_vmap_range(ptr, size);
375 static struct wc_memory_superblock *sb(struct dm_writecache *wc)
377 return wc->memory_map;
380 static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
382 return &sb(wc)->entries[e->index];
385 static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
387 return (char *)wc->block_start + (e->index << wc->block_size_bits);
390 static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
392 return wc->start_sector + wc->metadata_sectors +
393 ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
396 static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
398 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
399 return e->original_sector;
401 return le64_to_cpu(memory_entry(wc, e)->original_sector);
405 static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
407 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
410 return le64_to_cpu(memory_entry(wc, e)->seq_count);
414 static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
416 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
419 pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
422 static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
423 uint64_t original_sector, uint64_t seq_count)
425 struct wc_memory_entry me;
426 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
427 e->original_sector = original_sector;
428 e->seq_count = seq_count;
430 me.original_sector = cpu_to_le64(original_sector);
431 me.seq_count = cpu_to_le64(seq_count);
432 pmem_assign(*memory_entry(wc, e), me);
435 #define writecache_error(wc, err, msg, arg...) \
437 if (!cmpxchg(&(wc)->error, 0, err)) \
439 wake_up(&(wc)->freelist_wait); \
442 #define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error)))
444 static void writecache_flush_all_metadata(struct dm_writecache *wc)
446 if (!WC_MODE_PMEM(wc))
447 memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
450 static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
452 if (!WC_MODE_PMEM(wc))
453 __set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
457 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
460 struct dm_writecache *wc;
465 static void writecache_notify_io(unsigned long error, void *context)
467 struct io_notify *endio = context;
469 if (unlikely(error != 0))
470 writecache_error(endio->wc, -EIO, "error writing metadata");
471 BUG_ON(atomic_read(&endio->count) <= 0);
472 if (atomic_dec_and_test(&endio->count))
476 static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
478 wait_event(wc->bio_in_progress_wait[direction],
479 !atomic_read(&wc->bio_in_progress[direction]));
482 static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
484 struct dm_io_region region;
485 struct dm_io_request req;
486 struct io_notify endio = {
488 COMPLETION_INITIALIZER_ONSTACK(endio.c),
491 unsigned bitmap_bits = wc->dirty_bitmap_size * 8;
496 i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i);
497 if (unlikely(i == bitmap_bits))
499 j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i);
501 region.bdev = wc->ssd_dev->bdev;
502 region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
503 region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
505 if (unlikely(region.sector >= wc->metadata_sectors))
507 if (unlikely(region.sector + region.count > wc->metadata_sectors))
508 region.count = wc->metadata_sectors - region.sector;
510 region.sector += wc->start_sector;
511 atomic_inc(&endio.count);
512 req.bi_op = REQ_OP_WRITE;
513 req.bi_op_flags = REQ_SYNC;
514 req.mem.type = DM_IO_VMA;
515 req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
516 req.client = wc->dm_io;
517 req.notify.fn = writecache_notify_io;
518 req.notify.context = &endio;
520 /* writing via async dm-io (implied by notify.fn above) won't return an error */
521 (void) dm_io(&req, 1, ®ion, NULL);
525 writecache_notify_io(0, &endio);
526 wait_for_completion_io(&endio.c);
529 writecache_wait_for_ios(wc, WRITE);
531 writecache_disk_flush(wc, wc->ssd_dev);
533 memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
536 static void ssd_commit_superblock(struct dm_writecache *wc)
539 struct dm_io_region region;
540 struct dm_io_request req;
542 region.bdev = wc->ssd_dev->bdev;
544 region.count = max(4096U, wc->block_size) >> SECTOR_SHIFT;
546 if (unlikely(region.sector + region.count > wc->metadata_sectors))
547 region.count = wc->metadata_sectors - region.sector;
549 region.sector += wc->start_sector;
551 req.bi_op = REQ_OP_WRITE;
552 req.bi_op_flags = REQ_SYNC | REQ_FUA;
553 req.mem.type = DM_IO_VMA;
554 req.mem.ptr.vma = (char *)wc->memory_map;
555 req.client = wc->dm_io;
556 req.notify.fn = NULL;
557 req.notify.context = NULL;
559 r = dm_io(&req, 1, ®ion, NULL);
561 writecache_error(wc, r, "error writing superblock");
564 static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
566 if (WC_MODE_PMEM(wc))
569 ssd_commit_flushed(wc, wait_for_ios);
572 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
575 struct dm_io_region region;
576 struct dm_io_request req;
578 region.bdev = dev->bdev;
581 req.bi_op = REQ_OP_WRITE;
582 req.bi_op_flags = REQ_PREFLUSH;
583 req.mem.type = DM_IO_KMEM;
584 req.mem.ptr.addr = NULL;
585 req.client = wc->dm_io;
586 req.notify.fn = NULL;
588 r = dm_io(&req, 1, ®ion, NULL);
590 writecache_error(wc, r, "error flushing metadata: %d", r);
593 #define WFE_RETURN_FOLLOWING 1
594 #define WFE_LOWEST_SEQ 2
596 static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
597 uint64_t block, int flags)
600 struct rb_node *node = wc->tree.rb_node;
606 e = container_of(node, struct wc_entry, rb_node);
607 if (read_original_sector(wc, e) == block)
610 node = (read_original_sector(wc, e) >= block ?
611 e->rb_node.rb_left : e->rb_node.rb_right);
612 if (unlikely(!node)) {
613 if (!(flags & WFE_RETURN_FOLLOWING))
615 if (read_original_sector(wc, e) >= block) {
618 node = rb_next(&e->rb_node);
621 e = container_of(node, struct wc_entry, rb_node);
629 if (flags & WFE_LOWEST_SEQ)
630 node = rb_prev(&e->rb_node);
632 node = rb_next(&e->rb_node);
635 e2 = container_of(node, struct wc_entry, rb_node);
636 if (read_original_sector(wc, e2) != block)
642 static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
645 struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
648 e = container_of(*node, struct wc_entry, rb_node);
649 parent = &e->rb_node;
650 if (read_original_sector(wc, e) > read_original_sector(wc, ins))
651 node = &parent->rb_left;
653 node = &parent->rb_right;
655 rb_link_node(&ins->rb_node, parent, node);
656 rb_insert_color(&ins->rb_node, &wc->tree);
657 list_add(&ins->lru, &wc->lru);
661 static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
664 rb_erase(&e->rb_node, &wc->tree);
667 static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
669 if (WC_MODE_SORT_FREELIST(wc)) {
670 struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
671 if (unlikely(!*node))
672 wc->current_free = e;
675 if (&e->rb_node < *node)
676 node = &parent->rb_left;
678 node = &parent->rb_right;
680 rb_link_node(&e->rb_node, parent, node);
681 rb_insert_color(&e->rb_node, &wc->freetree);
683 list_add_tail(&e->lru, &wc->freelist);
688 static inline void writecache_verify_watermark(struct dm_writecache *wc)
690 if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
691 queue_work(wc->writeback_wq, &wc->writeback_work);
694 static void writecache_max_age_timer(struct timer_list *t)
696 struct dm_writecache *wc = from_timer(wc, t, max_age_timer);
698 if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) {
699 queue_work(wc->writeback_wq, &wc->writeback_work);
700 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
704 static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector)
708 if (WC_MODE_SORT_FREELIST(wc)) {
709 struct rb_node *next;
710 if (unlikely(!wc->current_free))
712 e = wc->current_free;
713 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
715 next = rb_next(&e->rb_node);
716 rb_erase(&e->rb_node, &wc->freetree);
718 next = rb_first(&wc->freetree);
719 wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
721 if (unlikely(list_empty(&wc->freelist)))
723 e = container_of(wc->freelist.next, struct wc_entry, lru);
724 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
730 writecache_verify_watermark(wc);
735 static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
737 writecache_unlink(wc, e);
738 writecache_add_to_freelist(wc, e);
739 clear_seq_count(wc, e);
740 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
741 if (unlikely(waitqueue_active(&wc->freelist_wait)))
742 wake_up(&wc->freelist_wait);
745 static void writecache_wait_on_freelist(struct dm_writecache *wc)
749 prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
752 finish_wait(&wc->freelist_wait, &wait);
756 static void writecache_poison_lists(struct dm_writecache *wc)
759 * Catch incorrect access to these values while the device is suspended.
761 memset(&wc->tree, -1, sizeof wc->tree);
762 wc->lru.next = LIST_POISON1;
763 wc->lru.prev = LIST_POISON2;
764 wc->freelist.next = LIST_POISON1;
765 wc->freelist.prev = LIST_POISON2;
768 static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
770 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
771 if (WC_MODE_PMEM(wc))
772 writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
775 static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
777 return read_seq_count(wc, e) < wc->seq_count;
780 static void writecache_flush(struct dm_writecache *wc)
782 struct wc_entry *e, *e2;
783 bool need_flush_after_free;
785 wc->uncommitted_blocks = 0;
786 del_timer(&wc->autocommit_timer);
788 if (list_empty(&wc->lru))
791 e = container_of(wc->lru.next, struct wc_entry, lru);
792 if (writecache_entry_is_committed(wc, e)) {
793 if (wc->overwrote_committed) {
794 writecache_wait_for_ios(wc, WRITE);
795 writecache_disk_flush(wc, wc->ssd_dev);
796 wc->overwrote_committed = false;
801 writecache_flush_entry(wc, e);
802 if (unlikely(e->lru.next == &wc->lru))
804 e2 = container_of(e->lru.next, struct wc_entry, lru);
805 if (writecache_entry_is_committed(wc, e2))
810 writecache_commit_flushed(wc, true);
813 pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
814 if (WC_MODE_PMEM(wc))
815 writecache_commit_flushed(wc, false);
817 ssd_commit_superblock(wc);
819 wc->overwrote_committed = false;
821 need_flush_after_free = false;
823 /* Free another committed entry with lower seq-count */
824 struct rb_node *rb_node = rb_prev(&e->rb_node);
827 e2 = container_of(rb_node, struct wc_entry, rb_node);
828 if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
829 likely(!e2->write_in_progress)) {
830 writecache_free_entry(wc, e2);
831 need_flush_after_free = true;
834 if (unlikely(e->lru.prev == &wc->lru))
836 e = container_of(e->lru.prev, struct wc_entry, lru);
840 if (need_flush_after_free)
841 writecache_commit_flushed(wc, false);
844 static void writecache_flush_work(struct work_struct *work)
846 struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
849 writecache_flush(wc);
853 static void writecache_autocommit_timer(struct timer_list *t)
855 struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
856 if (!writecache_has_error(wc))
857 queue_work(wc->writeback_wq, &wc->flush_work);
860 static void writecache_schedule_autocommit(struct dm_writecache *wc)
862 if (!timer_pending(&wc->autocommit_timer))
863 mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
866 static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
869 bool discarded_something = false;
871 e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
875 while (read_original_sector(wc, e) < end) {
876 struct rb_node *node = rb_next(&e->rb_node);
878 if (likely(!e->write_in_progress)) {
879 if (!discarded_something) {
880 if (!WC_MODE_PMEM(wc)) {
881 writecache_wait_for_ios(wc, READ);
882 writecache_wait_for_ios(wc, WRITE);
884 discarded_something = true;
886 if (!writecache_entry_is_committed(wc, e))
887 wc->uncommitted_blocks--;
888 writecache_free_entry(wc, e);
894 e = container_of(node, struct wc_entry, rb_node);
897 if (discarded_something)
898 writecache_commit_flushed(wc, false);
901 static bool writecache_wait_for_writeback(struct dm_writecache *wc)
903 if (wc->writeback_size) {
904 writecache_wait_on_freelist(wc);
910 static void writecache_suspend(struct dm_target *ti)
912 struct dm_writecache *wc = ti->private;
913 bool flush_on_suspend;
915 del_timer_sync(&wc->autocommit_timer);
916 del_timer_sync(&wc->max_age_timer);
919 writecache_flush(wc);
920 flush_on_suspend = wc->flush_on_suspend;
921 if (flush_on_suspend) {
922 wc->flush_on_suspend = false;
924 queue_work(wc->writeback_wq, &wc->writeback_work);
928 drain_workqueue(wc->writeback_wq);
931 if (flush_on_suspend)
933 while (writecache_wait_for_writeback(wc));
935 if (WC_MODE_PMEM(wc))
936 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
938 writecache_poison_lists(wc);
943 static int writecache_alloc_entries(struct dm_writecache *wc)
949 wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
952 for (b = 0; b < wc->n_blocks; b++) {
953 struct wc_entry *e = &wc->entries[b];
955 e->write_in_progress = false;
962 static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors)
964 struct dm_io_region region;
965 struct dm_io_request req;
967 region.bdev = wc->ssd_dev->bdev;
968 region.sector = wc->start_sector;
969 region.count = n_sectors;
970 req.bi_op = REQ_OP_READ;
971 req.bi_op_flags = REQ_SYNC;
972 req.mem.type = DM_IO_VMA;
973 req.mem.ptr.vma = (char *)wc->memory_map;
974 req.client = wc->dm_io;
975 req.notify.fn = NULL;
977 return dm_io(&req, 1, ®ion, NULL);
980 static void writecache_resume(struct dm_target *ti)
982 struct dm_writecache *wc = ti->private;
984 bool need_flush = false;
990 wc->data_device_sectors = bdev_nr_sectors(wc->dev->bdev);
992 if (WC_MODE_PMEM(wc)) {
993 persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
995 r = writecache_read_metadata(wc, wc->metadata_sectors);
997 size_t sb_entries_offset;
998 writecache_error(wc, r, "unable to read metadata: %d", r);
999 sb_entries_offset = offsetof(struct wc_memory_superblock, entries);
1000 memset((char *)wc->memory_map + sb_entries_offset, -1,
1001 (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset);
1006 INIT_LIST_HEAD(&wc->lru);
1007 if (WC_MODE_SORT_FREELIST(wc)) {
1008 wc->freetree = RB_ROOT;
1009 wc->current_free = NULL;
1011 INIT_LIST_HEAD(&wc->freelist);
1013 wc->freelist_size = 0;
1015 r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count,
1018 writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
1019 sb_seq_count = cpu_to_le64(0);
1021 wc->seq_count = le64_to_cpu(sb_seq_count);
1023 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
1024 for (b = 0; b < wc->n_blocks; b++) {
1025 struct wc_entry *e = &wc->entries[b];
1026 struct wc_memory_entry wme;
1027 if (writecache_has_error(wc)) {
1028 e->original_sector = -1;
1032 r = copy_mc_to_kernel(&wme, memory_entry(wc, e),
1033 sizeof(struct wc_memory_entry));
1035 writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
1036 (unsigned long)b, r);
1037 e->original_sector = -1;
1040 e->original_sector = le64_to_cpu(wme.original_sector);
1041 e->seq_count = le64_to_cpu(wme.seq_count);
1046 for (b = 0; b < wc->n_blocks; b++) {
1047 struct wc_entry *e = &wc->entries[b];
1048 if (!writecache_entry_is_committed(wc, e)) {
1049 if (read_seq_count(wc, e) != -1) {
1051 clear_seq_count(wc, e);
1054 writecache_add_to_freelist(wc, e);
1056 struct wc_entry *old;
1058 old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
1060 writecache_insert_entry(wc, e);
1062 if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
1063 writecache_error(wc, -EINVAL,
1064 "two identical entries, position %llu, sector %llu, sequence %llu",
1065 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
1066 (unsigned long long)read_seq_count(wc, e));
1068 if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
1071 writecache_free_entry(wc, old);
1072 writecache_insert_entry(wc, e);
1081 writecache_flush_all_metadata(wc);
1082 writecache_commit_flushed(wc, false);
1085 writecache_verify_watermark(wc);
1087 if (wc->max_age != MAX_AGE_UNSPECIFIED)
1088 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
1093 static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1099 if (dm_suspended(wc->ti)) {
1103 if (writecache_has_error(wc)) {
1108 writecache_flush(wc);
1109 wc->writeback_all++;
1110 queue_work(wc->writeback_wq, &wc->writeback_work);
1113 flush_workqueue(wc->writeback_wq);
1116 wc->writeback_all--;
1117 if (writecache_has_error(wc)) {
1126 static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1132 wc->flush_on_suspend = true;
1138 static void activate_cleaner(struct dm_writecache *wc)
1140 wc->flush_on_suspend = true;
1142 wc->freelist_high_watermark = wc->n_blocks;
1143 wc->freelist_low_watermark = wc->n_blocks;
1146 static int process_cleaner_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1152 activate_cleaner(wc);
1153 if (!dm_suspended(wc->ti))
1154 writecache_verify_watermark(wc);
1160 static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
1161 char *result, unsigned maxlen)
1164 struct dm_writecache *wc = ti->private;
1166 if (!strcasecmp(argv[0], "flush"))
1167 r = process_flush_mesg(argc, argv, wc);
1168 else if (!strcasecmp(argv[0], "flush_on_suspend"))
1169 r = process_flush_on_suspend_mesg(argc, argv, wc);
1170 else if (!strcasecmp(argv[0], "cleaner"))
1171 r = process_cleaner_mesg(argc, argv, wc);
1173 DMERR("unrecognised message received: %s", argv[0]);
1178 static void memcpy_flushcache_optimized(void *dest, void *source, size_t size)
1181 * clflushopt performs better with block size 1024, 2048, 4096
1182 * non-temporal stores perform better with block size 512
1184 * block size 512 1024 2048 4096
1185 * movnti 496 MB/s 642 MB/s 725 MB/s 744 MB/s
1186 * clflushopt 373 MB/s 688 MB/s 1.1 GB/s 1.2 GB/s
1188 * We see that movnti performs better for 512-byte blocks, and
1189 * clflushopt performs better for 1024-byte and larger blocks. So, we
1190 * prefer clflushopt for sizes >= 768.
1192 * NOTE: this happens to be the case now (with dm-writecache's single
1193 * threaded model) but re-evaluate this once memcpy_flushcache() is
1194 * enabled to use movdir64b which might invalidate this performance
1195 * advantage seen with cache-allocating-writes plus flushing.
1198 if (static_cpu_has(X86_FEATURE_CLFLUSHOPT) &&
1199 likely(boot_cpu_data.x86_clflush_size == 64) &&
1200 likely(size >= 768)) {
1202 memcpy((void *)dest, (void *)source, 64);
1203 clflushopt((void *)dest);
1207 } while (size >= 64);
1211 memcpy_flushcache(dest, source, size);
1214 static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
1217 unsigned long flags;
1219 int rw = bio_data_dir(bio);
1220 unsigned remaining_size = wc->block_size;
1223 struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
1224 buf = bvec_kmap_irq(&bv, &flags);
1226 if (unlikely(size > remaining_size))
1227 size = remaining_size;
1231 r = copy_mc_to_kernel(buf, data, size);
1232 flush_dcache_page(bio_page(bio));
1234 writecache_error(wc, r, "hardware memory error when reading data: %d", r);
1235 bio->bi_status = BLK_STS_IOERR;
1238 flush_dcache_page(bio_page(bio));
1239 memcpy_flushcache_optimized(data, buf, size);
1242 bvec_kunmap_irq(buf, &flags);
1244 data = (char *)data + size;
1245 remaining_size -= size;
1246 bio_advance(bio, size);
1247 } while (unlikely(remaining_size));
1250 static int writecache_flush_thread(void *data)
1252 struct dm_writecache *wc = data;
1258 bio = bio_list_pop(&wc->flush_list);
1260 set_current_state(TASK_INTERRUPTIBLE);
1263 if (unlikely(kthread_should_stop())) {
1264 set_current_state(TASK_RUNNING);
1272 if (bio_op(bio) == REQ_OP_DISCARD) {
1273 writecache_discard(wc, bio->bi_iter.bi_sector,
1274 bio_end_sector(bio));
1276 bio_set_dev(bio, wc->dev->bdev);
1277 submit_bio_noacct(bio);
1279 writecache_flush(wc);
1281 if (writecache_has_error(wc))
1282 bio->bi_status = BLK_STS_IOERR;
1290 static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
1292 if (bio_list_empty(&wc->flush_list))
1293 wake_up_process(wc->flush_thread);
1294 bio_list_add(&wc->flush_list, bio);
1297 static int writecache_map(struct dm_target *ti, struct bio *bio)
1300 struct dm_writecache *wc = ti->private;
1302 bio->bi_private = NULL;
1306 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1307 if (writecache_has_error(wc))
1309 if (WC_MODE_PMEM(wc)) {
1310 writecache_flush(wc);
1311 if (writecache_has_error(wc))
1313 if (unlikely(wc->cleaner) || unlikely(wc->metadata_only))
1314 goto unlock_remap_origin;
1317 if (dm_bio_get_target_bio_nr(bio))
1318 goto unlock_remap_origin;
1319 writecache_offload_bio(wc, bio);
1324 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1326 if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
1327 (wc->block_size / 512 - 1)) != 0)) {
1328 DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
1329 (unsigned long long)bio->bi_iter.bi_sector,
1330 bio->bi_iter.bi_size, wc->block_size);
1334 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
1335 if (writecache_has_error(wc))
1337 if (WC_MODE_PMEM(wc)) {
1338 writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
1339 goto unlock_remap_origin;
1341 writecache_offload_bio(wc, bio);
1346 if (bio_data_dir(bio) == READ) {
1348 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1349 if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1350 if (WC_MODE_PMEM(wc)) {
1351 bio_copy_block(wc, bio, memory_data(wc, e));
1352 if (bio->bi_iter.bi_size)
1353 goto read_next_block;
1356 dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1357 bio_set_dev(bio, wc->ssd_dev->bdev);
1358 bio->bi_iter.bi_sector = cache_sector(wc, e);
1359 if (!writecache_entry_is_committed(wc, e))
1360 writecache_wait_for_ios(wc, WRITE);
1365 sector_t next_boundary =
1366 read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1367 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
1368 dm_accept_partial_bio(bio, next_boundary);
1371 goto unlock_remap_origin;
1375 bool found_entry = false;
1376 bool search_used = false;
1377 if (writecache_has_error(wc))
1379 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1381 if (!writecache_entry_is_committed(wc, e)) {
1385 if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1386 wc->overwrote_committed = true;
1392 if (unlikely(wc->cleaner) ||
1393 (wc->metadata_only && !(bio->bi_opf & REQ_META)))
1396 e = writecache_pop_from_freelist(wc, (sector_t)-1);
1398 if (!WC_MODE_PMEM(wc) && !found_entry) {
1400 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1402 sector_t next_boundary = read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1403 BUG_ON(!next_boundary);
1404 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
1405 dm_accept_partial_bio(bio, next_boundary);
1408 goto unlock_remap_origin;
1410 writecache_wait_on_freelist(wc);
1413 write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1414 writecache_insert_entry(wc, e);
1415 wc->uncommitted_blocks++;
1417 if (WC_MODE_PMEM(wc)) {
1418 bio_copy_block(wc, bio, memory_data(wc, e));
1420 unsigned bio_size = wc->block_size;
1421 sector_t start_cache_sec = cache_sector(wc, e);
1422 sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT);
1424 while (bio_size < bio->bi_iter.bi_size) {
1426 struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec);
1429 write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector +
1430 (bio_size >> SECTOR_SHIFT), wc->seq_count);
1431 writecache_insert_entry(wc, f);
1432 wc->uncommitted_blocks++;
1435 struct rb_node *next = rb_next(&e->rb_node);
1438 f = container_of(next, struct wc_entry, rb_node);
1441 if (read_original_sector(wc, f) !=
1442 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1444 if (unlikely(f->write_in_progress))
1446 if (writecache_entry_is_committed(wc, f))
1447 wc->overwrote_committed = true;
1450 bio_size += wc->block_size;
1451 current_cache_sec += wc->block_size >> SECTOR_SHIFT;
1454 bio_set_dev(bio, wc->ssd_dev->bdev);
1455 bio->bi_iter.bi_sector = start_cache_sec;
1456 dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT);
1458 if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1459 wc->uncommitted_blocks = 0;
1460 queue_work(wc->writeback_wq, &wc->flush_work);
1462 writecache_schedule_autocommit(wc);
1466 } while (bio->bi_iter.bi_size);
1468 if (unlikely(bio->bi_opf & REQ_FUA ||
1469 wc->uncommitted_blocks >= wc->autocommit_blocks))
1470 writecache_flush(wc);
1472 writecache_schedule_autocommit(wc);
1476 unlock_remap_origin:
1477 if (likely(wc->pause != 0)) {
1478 if (bio_op(bio) == REQ_OP_WRITE) {
1479 dm_iot_io_begin(&wc->iot, 1);
1480 bio->bi_private = (void *)2;
1483 bio_set_dev(bio, wc->dev->bdev);
1485 return DM_MAPIO_REMAPPED;
1488 /* make sure that writecache_end_io decrements bio_in_progress: */
1489 bio->bi_private = (void *)1;
1490 atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
1492 return DM_MAPIO_REMAPPED;
1497 return DM_MAPIO_SUBMITTED;
1501 return DM_MAPIO_SUBMITTED;
1506 return DM_MAPIO_SUBMITTED;
1509 static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
1511 struct dm_writecache *wc = ti->private;
1513 if (bio->bi_private == (void *)1) {
1514 int dir = bio_data_dir(bio);
1515 if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
1516 if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
1517 wake_up(&wc->bio_in_progress_wait[dir]);
1518 } else if (bio->bi_private == (void *)2) {
1519 dm_iot_io_end(&wc->iot, 1);
1524 static int writecache_iterate_devices(struct dm_target *ti,
1525 iterate_devices_callout_fn fn, void *data)
1527 struct dm_writecache *wc = ti->private;
1529 return fn(ti, wc->dev, 0, ti->len, data);
1532 static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
1534 struct dm_writecache *wc = ti->private;
1536 if (limits->logical_block_size < wc->block_size)
1537 limits->logical_block_size = wc->block_size;
1539 if (limits->physical_block_size < wc->block_size)
1540 limits->physical_block_size = wc->block_size;
1542 if (limits->io_min < wc->block_size)
1543 limits->io_min = wc->block_size;
1547 static void writecache_writeback_endio(struct bio *bio)
1549 struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
1550 struct dm_writecache *wc = wb->wc;
1551 unsigned long flags;
1553 raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
1554 if (unlikely(list_empty(&wc->endio_list)))
1555 wake_up_process(wc->endio_thread);
1556 list_add_tail(&wb->endio_entry, &wc->endio_list);
1557 raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
1560 static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
1562 struct copy_struct *c = ptr;
1563 struct dm_writecache *wc = c->wc;
1565 c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
1567 raw_spin_lock_irq(&wc->endio_list_lock);
1568 if (unlikely(list_empty(&wc->endio_list)))
1569 wake_up_process(wc->endio_thread);
1570 list_add_tail(&c->endio_entry, &wc->endio_list);
1571 raw_spin_unlock_irq(&wc->endio_list_lock);
1574 static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
1577 struct writeback_struct *wb;
1579 unsigned long n_walked = 0;
1582 wb = list_entry(list->next, struct writeback_struct, endio_entry);
1583 list_del(&wb->endio_entry);
1585 if (unlikely(wb->bio.bi_status != BLK_STS_OK))
1586 writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
1587 "write error %d", wb->bio.bi_status);
1591 BUG_ON(!e->write_in_progress);
1592 e->write_in_progress = false;
1593 INIT_LIST_HEAD(&e->lru);
1594 if (!writecache_has_error(wc))
1595 writecache_free_entry(wc, e);
1596 BUG_ON(!wc->writeback_size);
1597 wc->writeback_size--;
1599 if (unlikely(n_walked >= ENDIO_LATENCY)) {
1600 writecache_commit_flushed(wc, false);
1605 } while (++i < wb->wc_list_n);
1607 if (wb->wc_list != wb->wc_list_inline)
1610 } while (!list_empty(list));
1613 static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
1615 struct copy_struct *c;
1619 c = list_entry(list->next, struct copy_struct, endio_entry);
1620 list_del(&c->endio_entry);
1622 if (unlikely(c->error))
1623 writecache_error(wc, c->error, "copy error");
1627 BUG_ON(!e->write_in_progress);
1628 e->write_in_progress = false;
1629 INIT_LIST_HEAD(&e->lru);
1630 if (!writecache_has_error(wc))
1631 writecache_free_entry(wc, e);
1633 BUG_ON(!wc->writeback_size);
1634 wc->writeback_size--;
1636 } while (--c->n_entries);
1637 mempool_free(c, &wc->copy_pool);
1638 } while (!list_empty(list));
1641 static int writecache_endio_thread(void *data)
1643 struct dm_writecache *wc = data;
1646 struct list_head list;
1648 raw_spin_lock_irq(&wc->endio_list_lock);
1649 if (!list_empty(&wc->endio_list))
1651 set_current_state(TASK_INTERRUPTIBLE);
1652 raw_spin_unlock_irq(&wc->endio_list_lock);
1654 if (unlikely(kthread_should_stop())) {
1655 set_current_state(TASK_RUNNING);
1664 list = wc->endio_list;
1665 list.next->prev = list.prev->next = &list;
1666 INIT_LIST_HEAD(&wc->endio_list);
1667 raw_spin_unlock_irq(&wc->endio_list_lock);
1669 if (!WC_MODE_FUA(wc))
1670 writecache_disk_flush(wc, wc->dev);
1674 if (WC_MODE_PMEM(wc)) {
1675 __writecache_endio_pmem(wc, &list);
1677 __writecache_endio_ssd(wc, &list);
1678 writecache_wait_for_ios(wc, READ);
1681 writecache_commit_flushed(wc, false);
1689 static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e)
1691 struct dm_writecache *wc = wb->wc;
1692 unsigned block_size = wc->block_size;
1693 void *address = memory_data(wc, e);
1695 persistent_memory_flush_cache(address, block_size);
1697 if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors))
1700 return bio_add_page(&wb->bio, persistent_memory_page(address),
1701 block_size, persistent_memory_page_offset(address)) != 0;
1704 struct writeback_list {
1705 struct list_head list;
1709 static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
1711 if (unlikely(wc->max_writeback_jobs)) {
1712 if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
1714 while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
1715 writecache_wait_on_freelist(wc);
1722 static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
1724 struct wc_entry *e, *f;
1726 struct writeback_struct *wb;
1731 e = container_of(wbl->list.prev, struct wc_entry, lru);
1734 max_pages = e->wc_list_contiguous;
1736 bio = bio_alloc_bioset(GFP_NOIO, max_pages, &wc->bio_set);
1737 wb = container_of(bio, struct writeback_struct, bio);
1739 bio->bi_end_io = writecache_writeback_endio;
1740 bio_set_dev(bio, wc->dev->bdev);
1741 bio->bi_iter.bi_sector = read_original_sector(wc, e);
1742 if (max_pages <= WB_LIST_INLINE ||
1743 unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
1744 GFP_NOIO | __GFP_NORETRY |
1745 __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
1746 wb->wc_list = wb->wc_list_inline;
1747 max_pages = WB_LIST_INLINE;
1750 BUG_ON(!wc_add_block(wb, e));
1755 while (wbl->size && wb->wc_list_n < max_pages) {
1756 f = container_of(wbl->list.prev, struct wc_entry, lru);
1757 if (read_original_sector(wc, f) !=
1758 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1760 if (!wc_add_block(wb, f))
1764 wb->wc_list[wb->wc_list_n++] = f;
1767 bio_set_op_attrs(bio, REQ_OP_WRITE, WC_MODE_FUA(wc) * REQ_FUA);
1768 if (writecache_has_error(wc)) {
1769 bio->bi_status = BLK_STS_IOERR;
1771 } else if (unlikely(!bio_sectors(bio))) {
1772 bio->bi_status = BLK_STS_OK;
1778 __writeback_throttle(wc, wbl);
1782 static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
1784 struct wc_entry *e, *f;
1785 struct dm_io_region from, to;
1786 struct copy_struct *c;
1792 e = container_of(wbl->list.prev, struct wc_entry, lru);
1795 n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
1797 from.bdev = wc->ssd_dev->bdev;
1798 from.sector = cache_sector(wc, e);
1799 from.count = n_sectors;
1800 to.bdev = wc->dev->bdev;
1801 to.sector = read_original_sector(wc, e);
1802 to.count = n_sectors;
1804 c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
1807 c->n_entries = e->wc_list_contiguous;
1809 while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
1811 f = container_of(wbl->list.prev, struct wc_entry, lru);
1817 if (unlikely(to.sector + to.count > wc->data_device_sectors)) {
1818 if (to.sector >= wc->data_device_sectors) {
1819 writecache_copy_endio(0, 0, c);
1822 from.count = to.count = wc->data_device_sectors - to.sector;
1825 dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
1827 __writeback_throttle(wc, wbl);
1831 static void writecache_writeback(struct work_struct *work)
1833 struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
1834 struct blk_plug plug;
1835 struct wc_entry *f, *g, *e = NULL;
1836 struct rb_node *node, *next_node;
1837 struct list_head skipped;
1838 struct writeback_list wbl;
1839 unsigned long n_walked;
1841 if (!WC_MODE_PMEM(wc)) {
1842 /* Wait for any active kcopyd work on behalf of ssd writeback */
1843 dm_kcopyd_client_flush(wc->dm_kcopyd);
1846 if (likely(wc->pause != 0)) {
1849 if (unlikely(wc->cleaner) || unlikely(wc->writeback_all) ||
1850 unlikely(dm_suspended(wc->ti)))
1852 idle = dm_iot_idle_time(&wc->iot);
1853 if (idle >= wc->pause)
1855 idle = wc->pause - idle;
1858 schedule_timeout_idle(idle);
1864 if (writecache_has_error(wc)) {
1869 if (unlikely(wc->writeback_all)) {
1870 if (writecache_wait_for_writeback(wc))
1874 if (wc->overwrote_committed) {
1875 writecache_wait_for_ios(wc, WRITE);
1879 INIT_LIST_HEAD(&skipped);
1880 INIT_LIST_HEAD(&wbl.list);
1882 while (!list_empty(&wc->lru) &&
1883 (wc->writeback_all ||
1884 wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark ||
1885 (jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >=
1886 wc->max_age - wc->max_age / MAX_AGE_DIV))) {
1889 if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1890 likely(!wc->writeback_all)) {
1891 if (likely(!dm_suspended(wc->ti)))
1892 queue_work(wc->writeback_wq, &wc->writeback_work);
1896 if (unlikely(wc->writeback_all)) {
1898 writecache_flush(wc);
1899 e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
1903 e = container_of(wc->lru.prev, struct wc_entry, lru);
1904 BUG_ON(e->write_in_progress);
1905 if (unlikely(!writecache_entry_is_committed(wc, e))) {
1906 writecache_flush(wc);
1908 node = rb_prev(&e->rb_node);
1910 f = container_of(node, struct wc_entry, rb_node);
1911 if (unlikely(read_original_sector(wc, f) ==
1912 read_original_sector(wc, e))) {
1913 BUG_ON(!f->write_in_progress);
1914 list_move(&e->lru, &skipped);
1919 wc->writeback_size++;
1920 list_move(&e->lru, &wbl.list);
1922 e->write_in_progress = true;
1923 e->wc_list_contiguous = 1;
1928 next_node = rb_next(&f->rb_node);
1929 if (unlikely(!next_node))
1931 g = container_of(next_node, struct wc_entry, rb_node);
1932 if (unlikely(read_original_sector(wc, g) ==
1933 read_original_sector(wc, f))) {
1937 if (read_original_sector(wc, g) !=
1938 read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
1940 if (unlikely(g->write_in_progress))
1942 if (unlikely(!writecache_entry_is_committed(wc, g)))
1945 if (!WC_MODE_PMEM(wc)) {
1951 //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
1954 wc->writeback_size++;
1955 list_move(&g->lru, &wbl.list);
1957 g->write_in_progress = true;
1958 g->wc_list_contiguous = BIO_MAX_VECS;
1960 e->wc_list_contiguous++;
1961 if (unlikely(e->wc_list_contiguous == BIO_MAX_VECS)) {
1962 if (unlikely(wc->writeback_all)) {
1963 next_node = rb_next(&f->rb_node);
1964 if (likely(next_node))
1965 g = container_of(next_node, struct wc_entry, rb_node);
1973 if (!list_empty(&skipped)) {
1974 list_splice_tail(&skipped, &wc->lru);
1976 * If we didn't do any progress, we must wait until some
1977 * writeback finishes to avoid burning CPU in a loop
1979 if (unlikely(!wbl.size))
1980 writecache_wait_for_writeback(wc);
1985 blk_start_plug(&plug);
1987 if (WC_MODE_PMEM(wc))
1988 __writecache_writeback_pmem(wc, &wbl);
1990 __writecache_writeback_ssd(wc, &wbl);
1992 blk_finish_plug(&plug);
1994 if (unlikely(wc->writeback_all)) {
1996 while (writecache_wait_for_writeback(wc));
2001 static int calculate_memory_size(uint64_t device_size, unsigned block_size,
2002 size_t *n_blocks_p, size_t *n_metadata_blocks_p)
2004 uint64_t n_blocks, offset;
2007 n_blocks = device_size;
2008 do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
2013 /* Verify the following entries[n_blocks] won't overflow */
2014 if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
2015 sizeof(struct wc_memory_entry)))
2017 offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
2018 offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
2019 if (offset + n_blocks * block_size <= device_size)
2024 /* check if the bit field overflows */
2026 if (e.index != n_blocks)
2030 *n_blocks_p = n_blocks;
2031 if (n_metadata_blocks_p)
2032 *n_metadata_blocks_p = offset >> __ffs(block_size);
2036 static int init_memory(struct dm_writecache *wc)
2041 r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
2045 r = writecache_alloc_entries(wc);
2049 for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
2050 pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
2051 pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
2052 pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
2053 pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
2054 pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
2056 for (b = 0; b < wc->n_blocks; b++) {
2057 write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
2061 writecache_flush_all_metadata(wc);
2062 writecache_commit_flushed(wc, false);
2063 pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
2064 writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
2065 writecache_commit_flushed(wc, false);
2070 static void writecache_dtr(struct dm_target *ti)
2072 struct dm_writecache *wc = ti->private;
2077 if (wc->endio_thread)
2078 kthread_stop(wc->endio_thread);
2080 if (wc->flush_thread)
2081 kthread_stop(wc->flush_thread);
2083 bioset_exit(&wc->bio_set);
2085 mempool_exit(&wc->copy_pool);
2087 if (wc->writeback_wq)
2088 destroy_workqueue(wc->writeback_wq);
2091 dm_put_device(ti, wc->dev);
2094 dm_put_device(ti, wc->ssd_dev);
2098 if (wc->memory_map) {
2099 if (WC_MODE_PMEM(wc))
2100 persistent_memory_release(wc);
2102 vfree(wc->memory_map);
2106 dm_kcopyd_client_destroy(wc->dm_kcopyd);
2109 dm_io_client_destroy(wc->dm_io);
2111 vfree(wc->dirty_bitmap);
2116 static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2118 struct dm_writecache *wc;
2119 struct dm_arg_set as;
2121 unsigned opt_params;
2122 size_t offset, data_size;
2125 int high_wm_percent = HIGH_WATERMARK;
2126 int low_wm_percent = LOW_WATERMARK;
2128 struct wc_memory_superblock s;
2130 static struct dm_arg _args[] = {
2131 {0, 18, "Invalid number of feature args"},
2137 wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
2139 ti->error = "Cannot allocate writecache structure";
2146 mutex_init(&wc->lock);
2147 wc->max_age = MAX_AGE_UNSPECIFIED;
2148 writecache_poison_lists(wc);
2149 init_waitqueue_head(&wc->freelist_wait);
2150 timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
2151 timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0);
2153 for (i = 0; i < 2; i++) {
2154 atomic_set(&wc->bio_in_progress[i], 0);
2155 init_waitqueue_head(&wc->bio_in_progress_wait[i]);
2158 wc->dm_io = dm_io_client_create();
2159 if (IS_ERR(wc->dm_io)) {
2160 r = PTR_ERR(wc->dm_io);
2161 ti->error = "Unable to allocate dm-io client";
2166 wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
2167 if (!wc->writeback_wq) {
2169 ti->error = "Could not allocate writeback workqueue";
2172 INIT_WORK(&wc->writeback_work, writecache_writeback);
2173 INIT_WORK(&wc->flush_work, writecache_flush_work);
2175 dm_iot_init(&wc->iot);
2177 raw_spin_lock_init(&wc->endio_list_lock);
2178 INIT_LIST_HEAD(&wc->endio_list);
2179 wc->endio_thread = kthread_create(writecache_endio_thread, wc, "writecache_endio");
2180 if (IS_ERR(wc->endio_thread)) {
2181 r = PTR_ERR(wc->endio_thread);
2182 wc->endio_thread = NULL;
2183 ti->error = "Couldn't spawn endio thread";
2186 wake_up_process(wc->endio_thread);
2189 * Parse the mode (pmem or ssd)
2191 string = dm_shift_arg(&as);
2195 if (!strcasecmp(string, "s")) {
2196 wc->pmem_mode = false;
2197 } else if (!strcasecmp(string, "p")) {
2198 #ifdef DM_WRITECACHE_HAS_PMEM
2199 wc->pmem_mode = true;
2200 wc->writeback_fua = true;
2203 * If the architecture doesn't support persistent memory or
2204 * the kernel doesn't support any DAX drivers, this driver can
2205 * only be used in SSD-only mode.
2208 ti->error = "Persistent memory or DAX not supported on this system";
2215 if (WC_MODE_PMEM(wc)) {
2216 r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
2217 offsetof(struct writeback_struct, bio),
2220 ti->error = "Could not allocate bio set";
2224 wc->pause = PAUSE_WRITEBACK;
2225 r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
2227 ti->error = "Could not allocate mempool";
2233 * Parse the origin data device
2235 string = dm_shift_arg(&as);
2238 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
2240 ti->error = "Origin data device lookup failed";
2245 * Parse cache data device (be it pmem or ssd)
2247 string = dm_shift_arg(&as);
2251 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
2253 ti->error = "Cache data device lookup failed";
2256 wc->memory_map_size = i_size_read(wc->ssd_dev->bdev->bd_inode);
2259 * Parse the cache block size
2261 string = dm_shift_arg(&as);
2264 if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
2265 wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
2266 (wc->block_size & (wc->block_size - 1))) {
2268 ti->error = "Invalid block size";
2271 if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) ||
2272 wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) {
2274 ti->error = "Block size is smaller than device logical block size";
2277 wc->block_size_bits = __ffs(wc->block_size);
2279 wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
2280 wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
2281 wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
2284 * Parse optional arguments
2286 r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2290 while (opt_params) {
2291 string = dm_shift_arg(&as), opt_params--;
2292 if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2293 unsigned long long start_sector;
2294 string = dm_shift_arg(&as), opt_params--;
2295 if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2296 goto invalid_optional;
2297 wc->start_sector = start_sector;
2298 wc->start_sector_set = true;
2299 if (wc->start_sector != start_sector ||
2300 wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2301 goto invalid_optional;
2302 } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
2303 string = dm_shift_arg(&as), opt_params--;
2304 if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
2305 goto invalid_optional;
2306 if (high_wm_percent < 0 || high_wm_percent > 100)
2307 goto invalid_optional;
2308 wc->high_wm_percent_value = high_wm_percent;
2309 wc->high_wm_percent_set = true;
2310 } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
2311 string = dm_shift_arg(&as), opt_params--;
2312 if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
2313 goto invalid_optional;
2314 if (low_wm_percent < 0 || low_wm_percent > 100)
2315 goto invalid_optional;
2316 wc->low_wm_percent_value = low_wm_percent;
2317 wc->low_wm_percent_set = true;
2318 } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
2319 string = dm_shift_arg(&as), opt_params--;
2320 if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
2321 goto invalid_optional;
2322 wc->max_writeback_jobs_set = true;
2323 } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
2324 string = dm_shift_arg(&as), opt_params--;
2325 if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
2326 goto invalid_optional;
2327 wc->autocommit_blocks_set = true;
2328 } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
2329 unsigned autocommit_msecs;
2330 string = dm_shift_arg(&as), opt_params--;
2331 if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
2332 goto invalid_optional;
2333 if (autocommit_msecs > 3600000)
2334 goto invalid_optional;
2335 wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2336 wc->autocommit_time_value = autocommit_msecs;
2337 wc->autocommit_time_set = true;
2338 } else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
2339 unsigned max_age_msecs;
2340 string = dm_shift_arg(&as), opt_params--;
2341 if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
2342 goto invalid_optional;
2343 if (max_age_msecs > 86400000)
2344 goto invalid_optional;
2345 wc->max_age = msecs_to_jiffies(max_age_msecs);
2346 wc->max_age_set = true;
2347 wc->max_age_value = max_age_msecs;
2348 } else if (!strcasecmp(string, "cleaner")) {
2349 wc->cleaner_set = true;
2351 } else if (!strcasecmp(string, "fua")) {
2352 if (WC_MODE_PMEM(wc)) {
2353 wc->writeback_fua = true;
2354 wc->writeback_fua_set = true;
2355 } else goto invalid_optional;
2356 } else if (!strcasecmp(string, "nofua")) {
2357 if (WC_MODE_PMEM(wc)) {
2358 wc->writeback_fua = false;
2359 wc->writeback_fua_set = true;
2360 } else goto invalid_optional;
2361 } else if (!strcasecmp(string, "metadata_only")) {
2362 wc->metadata_only = true;
2363 } else if (!strcasecmp(string, "pause_writeback") && opt_params >= 1) {
2364 unsigned pause_msecs;
2365 if (WC_MODE_PMEM(wc))
2366 goto invalid_optional;
2367 string = dm_shift_arg(&as), opt_params--;
2368 if (sscanf(string, "%u%c", &pause_msecs, &dummy) != 1)
2369 goto invalid_optional;
2370 if (pause_msecs > 60000)
2371 goto invalid_optional;
2372 wc->pause = msecs_to_jiffies(pause_msecs);
2373 wc->pause_set = true;
2374 wc->pause_value = pause_msecs;
2378 ti->error = "Invalid optional argument";
2383 if (high_wm_percent < low_wm_percent) {
2385 ti->error = "High watermark must be greater than or equal to low watermark";
2389 if (WC_MODE_PMEM(wc)) {
2390 if (!dax_synchronous(wc->ssd_dev->dax_dev)) {
2392 ti->error = "Asynchronous persistent memory not supported as pmem cache";
2396 r = persistent_memory_claim(wc);
2398 ti->error = "Unable to map persistent memory for cache";
2402 size_t n_blocks, n_metadata_blocks;
2403 uint64_t n_bitmap_bits;
2405 wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2407 bio_list_init(&wc->flush_list);
2408 wc->flush_thread = kthread_create(writecache_flush_thread, wc, "dm_writecache_flush");
2409 if (IS_ERR(wc->flush_thread)) {
2410 r = PTR_ERR(wc->flush_thread);
2411 wc->flush_thread = NULL;
2412 ti->error = "Couldn't spawn flush thread";
2415 wake_up_process(wc->flush_thread);
2417 r = calculate_memory_size(wc->memory_map_size, wc->block_size,
2418 &n_blocks, &n_metadata_blocks);
2420 ti->error = "Invalid device size";
2424 n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
2425 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
2426 /* this is limitation of test_bit functions */
2427 if (n_bitmap_bits > 1U << 31) {
2429 ti->error = "Invalid device size";
2433 wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
2434 if (!wc->memory_map) {
2436 ti->error = "Unable to allocate memory for metadata";
2440 wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2441 if (IS_ERR(wc->dm_kcopyd)) {
2442 r = PTR_ERR(wc->dm_kcopyd);
2443 ti->error = "Unable to allocate dm-kcopyd client";
2444 wc->dm_kcopyd = NULL;
2448 wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
2449 wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
2450 BITS_PER_LONG * sizeof(unsigned long);
2451 wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
2452 if (!wc->dirty_bitmap) {
2454 ti->error = "Unable to allocate dirty bitmap";
2458 r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT);
2460 ti->error = "Unable to read first block of metadata";
2465 r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock));
2467 ti->error = "Hardware memory error when reading superblock";
2470 if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
2471 r = init_memory(wc);
2473 ti->error = "Unable to initialize device";
2476 r = copy_mc_to_kernel(&s, sb(wc),
2477 sizeof(struct wc_memory_superblock));
2479 ti->error = "Hardware memory error when reading superblock";
2484 if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
2485 ti->error = "Invalid magic in the superblock";
2490 if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
2491 ti->error = "Invalid version in the superblock";
2496 if (le32_to_cpu(s.block_size) != wc->block_size) {
2497 ti->error = "Block size does not match superblock";
2502 wc->n_blocks = le64_to_cpu(s.n_blocks);
2504 offset = wc->n_blocks * sizeof(struct wc_memory_entry);
2505 if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
2507 ti->error = "Overflow in size calculation";
2511 offset += sizeof(struct wc_memory_superblock);
2512 if (offset < sizeof(struct wc_memory_superblock))
2514 offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
2515 data_size = wc->n_blocks * (size_t)wc->block_size;
2516 if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
2517 (offset + data_size < offset))
2519 if (offset + data_size > wc->memory_map_size) {
2520 ti->error = "Memory area is too small";
2525 wc->metadata_sectors = offset >> SECTOR_SHIFT;
2526 wc->block_start = (char *)sb(wc) + offset;
2528 x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
2531 wc->freelist_high_watermark = x;
2532 x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
2535 wc->freelist_low_watermark = x;
2538 activate_cleaner(wc);
2540 r = writecache_alloc_entries(wc);
2542 ti->error = "Cannot allocate memory";
2546 ti->num_flush_bios = WC_MODE_PMEM(wc) ? 1 : 2;
2547 ti->flush_supported = true;
2548 ti->num_discard_bios = 1;
2550 if (WC_MODE_PMEM(wc))
2551 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
2557 ti->error = "Bad arguments";
2563 static void writecache_status(struct dm_target *ti, status_type_t type,
2564 unsigned status_flags, char *result, unsigned maxlen)
2566 struct dm_writecache *wc = ti->private;
2567 unsigned extra_args;
2571 case STATUSTYPE_INFO:
2572 DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),
2573 (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2574 (unsigned long long)wc->writeback_size);
2576 case STATUSTYPE_TABLE:
2577 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
2578 wc->dev->name, wc->ssd_dev->name, wc->block_size);
2580 if (wc->start_sector_set)
2582 if (wc->high_wm_percent_set)
2584 if (wc->low_wm_percent_set)
2586 if (wc->max_writeback_jobs_set)
2588 if (wc->autocommit_blocks_set)
2590 if (wc->autocommit_time_set)
2592 if (wc->max_age_set)
2594 if (wc->cleaner_set)
2596 if (wc->writeback_fua_set)
2598 if (wc->metadata_only)
2603 DMEMIT("%u", extra_args);
2604 if (wc->start_sector_set)
2605 DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2606 if (wc->high_wm_percent_set)
2607 DMEMIT(" high_watermark %u", wc->high_wm_percent_value);
2608 if (wc->low_wm_percent_set)
2609 DMEMIT(" low_watermark %u", wc->low_wm_percent_value);
2610 if (wc->max_writeback_jobs_set)
2611 DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
2612 if (wc->autocommit_blocks_set)
2613 DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
2614 if (wc->autocommit_time_set)
2615 DMEMIT(" autocommit_time %u", wc->autocommit_time_value);
2616 if (wc->max_age_set)
2617 DMEMIT(" max_age %u", wc->max_age_value);
2618 if (wc->cleaner_set)
2620 if (wc->writeback_fua_set)
2621 DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2622 if (wc->metadata_only)
2623 DMEMIT(" metadata_only");
2625 DMEMIT(" pause_writeback %u", wc->pause_value);
2630 static struct target_type writecache_target = {
2631 .name = "writecache",
2632 .version = {1, 5, 0},
2633 .module = THIS_MODULE,
2634 .ctr = writecache_ctr,
2635 .dtr = writecache_dtr,
2636 .status = writecache_status,
2637 .postsuspend = writecache_suspend,
2638 .resume = writecache_resume,
2639 .message = writecache_message,
2640 .map = writecache_map,
2641 .end_io = writecache_end_io,
2642 .iterate_devices = writecache_iterate_devices,
2643 .io_hints = writecache_io_hints,
2646 static int __init dm_writecache_init(void)
2650 r = dm_register_target(&writecache_target);
2652 DMERR("register failed %d", r);
2659 static void __exit dm_writecache_exit(void)
2661 dm_unregister_target(&writecache_target);
2664 module_init(dm_writecache_init);
2665 module_exit(dm_writecache_exit);
2667 MODULE_DESCRIPTION(DM_NAME " writecache target");
2668 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2669 MODULE_LICENSE("GPL");