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 min(0x10000000 / PAGE_SIZE, totalram_pages() / 16)
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_FS_DAX)
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 unsigned long long reads;
212 unsigned long long read_hits;
213 unsigned long long writes;
214 unsigned long long write_hits_uncommitted;
215 unsigned long long write_hits_committed;
216 unsigned long long writes_around;
217 unsigned long long writes_allocate;
218 unsigned long long writes_blocked_on_freelist;
219 unsigned long long flushes;
220 unsigned long long discards;
224 #define WB_LIST_INLINE 16
226 struct writeback_struct {
227 struct list_head endio_entry;
228 struct dm_writecache *wc;
229 struct wc_entry **wc_list;
231 struct wc_entry *wc_list_inline[WB_LIST_INLINE];
236 struct list_head endio_entry;
237 struct dm_writecache *wc;
243 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
244 "A percentage of time allocated for data copying");
246 static void wc_lock(struct dm_writecache *wc)
248 mutex_lock(&wc->lock);
251 static void wc_unlock(struct dm_writecache *wc)
253 mutex_unlock(&wc->lock);
256 #ifdef DM_WRITECACHE_HAS_PMEM
257 static int persistent_memory_claim(struct dm_writecache *wc)
267 wc->memory_vmapped = false;
269 s = wc->memory_map_size;
275 if (p != s >> PAGE_SHIFT) {
280 offset = get_start_sect(wc->ssd_dev->bdev);
281 if (offset & (PAGE_SIZE / 512 - 1)) {
285 offset >>= PAGE_SHIFT - 9;
287 id = dax_read_lock();
289 da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, DAX_ACCESS,
290 &wc->memory_map, &pfn);
292 wc->memory_map = NULL;
296 if (!pfn_t_has_page(pfn)) {
297 wc->memory_map = NULL;
303 wc->memory_map = NULL;
304 pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
312 daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i,
313 p - i, DAX_ACCESS, NULL, &pfn);
315 r = daa ? daa : -EINVAL;
318 if (!pfn_t_has_page(pfn)) {
322 while (daa-- && i < p) {
323 pages[i++] = pfn_t_to_page(pfn);
329 wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
330 if (!wc->memory_map) {
335 wc->memory_vmapped = true;
340 wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
341 wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
352 static int persistent_memory_claim(struct dm_writecache *wc)
358 static void persistent_memory_release(struct dm_writecache *wc)
360 if (wc->memory_vmapped)
361 vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
364 static struct page *persistent_memory_page(void *addr)
366 if (is_vmalloc_addr(addr))
367 return vmalloc_to_page(addr);
369 return virt_to_page(addr);
372 static unsigned persistent_memory_page_offset(void *addr)
374 return (unsigned long)addr & (PAGE_SIZE - 1);
377 static void persistent_memory_flush_cache(void *ptr, size_t size)
379 if (is_vmalloc_addr(ptr))
380 flush_kernel_vmap_range(ptr, size);
383 static void persistent_memory_invalidate_cache(void *ptr, size_t size)
385 if (is_vmalloc_addr(ptr))
386 invalidate_kernel_vmap_range(ptr, size);
389 static struct wc_memory_superblock *sb(struct dm_writecache *wc)
391 return wc->memory_map;
394 static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
396 return &sb(wc)->entries[e->index];
399 static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
401 return (char *)wc->block_start + (e->index << wc->block_size_bits);
404 static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
406 return wc->start_sector + wc->metadata_sectors +
407 ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
410 static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
412 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
413 return e->original_sector;
415 return le64_to_cpu(memory_entry(wc, e)->original_sector);
419 static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
421 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
424 return le64_to_cpu(memory_entry(wc, e)->seq_count);
428 static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
430 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
433 pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
436 static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
437 uint64_t original_sector, uint64_t seq_count)
439 struct wc_memory_entry me;
440 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
441 e->original_sector = original_sector;
442 e->seq_count = seq_count;
444 me.original_sector = cpu_to_le64(original_sector);
445 me.seq_count = cpu_to_le64(seq_count);
446 pmem_assign(*memory_entry(wc, e), me);
449 #define writecache_error(wc, err, msg, arg...) \
451 if (!cmpxchg(&(wc)->error, 0, err)) \
453 wake_up(&(wc)->freelist_wait); \
456 #define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error)))
458 static void writecache_flush_all_metadata(struct dm_writecache *wc)
460 if (!WC_MODE_PMEM(wc))
461 memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
464 static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
466 if (!WC_MODE_PMEM(wc))
467 __set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
471 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
474 struct dm_writecache *wc;
479 static void writecache_notify_io(unsigned long error, void *context)
481 struct io_notify *endio = context;
483 if (unlikely(error != 0))
484 writecache_error(endio->wc, -EIO, "error writing metadata");
485 BUG_ON(atomic_read(&endio->count) <= 0);
486 if (atomic_dec_and_test(&endio->count))
490 static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
492 wait_event(wc->bio_in_progress_wait[direction],
493 !atomic_read(&wc->bio_in_progress[direction]));
496 static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
498 struct dm_io_region region;
499 struct dm_io_request req;
500 struct io_notify endio = {
502 COMPLETION_INITIALIZER_ONSTACK(endio.c),
505 unsigned bitmap_bits = wc->dirty_bitmap_size * 8;
510 i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i);
511 if (unlikely(i == bitmap_bits))
513 j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i);
515 region.bdev = wc->ssd_dev->bdev;
516 region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
517 region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
519 if (unlikely(region.sector >= wc->metadata_sectors))
521 if (unlikely(region.sector + region.count > wc->metadata_sectors))
522 region.count = wc->metadata_sectors - region.sector;
524 region.sector += wc->start_sector;
525 atomic_inc(&endio.count);
526 req.bi_op = REQ_OP_WRITE;
527 req.bi_op_flags = REQ_SYNC;
528 req.mem.type = DM_IO_VMA;
529 req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
530 req.client = wc->dm_io;
531 req.notify.fn = writecache_notify_io;
532 req.notify.context = &endio;
534 /* writing via async dm-io (implied by notify.fn above) won't return an error */
535 (void) dm_io(&req, 1, ®ion, NULL);
539 writecache_notify_io(0, &endio);
540 wait_for_completion_io(&endio.c);
543 writecache_wait_for_ios(wc, WRITE);
545 writecache_disk_flush(wc, wc->ssd_dev);
547 memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
550 static void ssd_commit_superblock(struct dm_writecache *wc)
553 struct dm_io_region region;
554 struct dm_io_request req;
556 region.bdev = wc->ssd_dev->bdev;
558 region.count = max(4096U, wc->block_size) >> SECTOR_SHIFT;
560 if (unlikely(region.sector + region.count > wc->metadata_sectors))
561 region.count = wc->metadata_sectors - region.sector;
563 region.sector += wc->start_sector;
565 req.bi_op = REQ_OP_WRITE;
566 req.bi_op_flags = REQ_SYNC | REQ_FUA;
567 req.mem.type = DM_IO_VMA;
568 req.mem.ptr.vma = (char *)wc->memory_map;
569 req.client = wc->dm_io;
570 req.notify.fn = NULL;
571 req.notify.context = NULL;
573 r = dm_io(&req, 1, ®ion, NULL);
575 writecache_error(wc, r, "error writing superblock");
578 static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
580 if (WC_MODE_PMEM(wc))
583 ssd_commit_flushed(wc, wait_for_ios);
586 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
589 struct dm_io_region region;
590 struct dm_io_request req;
592 region.bdev = dev->bdev;
595 req.bi_op = REQ_OP_WRITE;
596 req.bi_op_flags = REQ_PREFLUSH;
597 req.mem.type = DM_IO_KMEM;
598 req.mem.ptr.addr = NULL;
599 req.client = wc->dm_io;
600 req.notify.fn = NULL;
602 r = dm_io(&req, 1, ®ion, NULL);
604 writecache_error(wc, r, "error flushing metadata: %d", r);
607 #define WFE_RETURN_FOLLOWING 1
608 #define WFE_LOWEST_SEQ 2
610 static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
611 uint64_t block, int flags)
614 struct rb_node *node = wc->tree.rb_node;
620 e = container_of(node, struct wc_entry, rb_node);
621 if (read_original_sector(wc, e) == block)
624 node = (read_original_sector(wc, e) >= block ?
625 e->rb_node.rb_left : e->rb_node.rb_right);
626 if (unlikely(!node)) {
627 if (!(flags & WFE_RETURN_FOLLOWING))
629 if (read_original_sector(wc, e) >= block) {
632 node = rb_next(&e->rb_node);
635 e = container_of(node, struct wc_entry, rb_node);
643 if (flags & WFE_LOWEST_SEQ)
644 node = rb_prev(&e->rb_node);
646 node = rb_next(&e->rb_node);
649 e2 = container_of(node, struct wc_entry, rb_node);
650 if (read_original_sector(wc, e2) != block)
656 static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
659 struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
662 e = container_of(*node, struct wc_entry, rb_node);
663 parent = &e->rb_node;
664 if (read_original_sector(wc, e) > read_original_sector(wc, ins))
665 node = &parent->rb_left;
667 node = &parent->rb_right;
669 rb_link_node(&ins->rb_node, parent, node);
670 rb_insert_color(&ins->rb_node, &wc->tree);
671 list_add(&ins->lru, &wc->lru);
675 static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
678 rb_erase(&e->rb_node, &wc->tree);
681 static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
683 if (WC_MODE_SORT_FREELIST(wc)) {
684 struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
685 if (unlikely(!*node))
686 wc->current_free = e;
689 if (&e->rb_node < *node)
690 node = &parent->rb_left;
692 node = &parent->rb_right;
694 rb_link_node(&e->rb_node, parent, node);
695 rb_insert_color(&e->rb_node, &wc->freetree);
697 list_add_tail(&e->lru, &wc->freelist);
702 static inline void writecache_verify_watermark(struct dm_writecache *wc)
704 if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
705 queue_work(wc->writeback_wq, &wc->writeback_work);
708 static void writecache_max_age_timer(struct timer_list *t)
710 struct dm_writecache *wc = from_timer(wc, t, max_age_timer);
712 if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) {
713 queue_work(wc->writeback_wq, &wc->writeback_work);
714 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
718 static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector)
722 if (WC_MODE_SORT_FREELIST(wc)) {
723 struct rb_node *next;
724 if (unlikely(!wc->current_free))
726 e = wc->current_free;
727 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
729 next = rb_next(&e->rb_node);
730 rb_erase(&e->rb_node, &wc->freetree);
732 next = rb_first(&wc->freetree);
733 wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
735 if (unlikely(list_empty(&wc->freelist)))
737 e = container_of(wc->freelist.next, struct wc_entry, lru);
738 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
744 writecache_verify_watermark(wc);
749 static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
751 writecache_unlink(wc, e);
752 writecache_add_to_freelist(wc, e);
753 clear_seq_count(wc, e);
754 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
755 if (unlikely(waitqueue_active(&wc->freelist_wait)))
756 wake_up(&wc->freelist_wait);
759 static void writecache_wait_on_freelist(struct dm_writecache *wc)
763 prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
766 finish_wait(&wc->freelist_wait, &wait);
770 static void writecache_poison_lists(struct dm_writecache *wc)
773 * Catch incorrect access to these values while the device is suspended.
775 memset(&wc->tree, -1, sizeof wc->tree);
776 wc->lru.next = LIST_POISON1;
777 wc->lru.prev = LIST_POISON2;
778 wc->freelist.next = LIST_POISON1;
779 wc->freelist.prev = LIST_POISON2;
782 static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
784 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
785 if (WC_MODE_PMEM(wc))
786 writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
789 static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
791 return read_seq_count(wc, e) < wc->seq_count;
794 static void writecache_flush(struct dm_writecache *wc)
796 struct wc_entry *e, *e2;
797 bool need_flush_after_free;
799 wc->uncommitted_blocks = 0;
800 del_timer(&wc->autocommit_timer);
802 if (list_empty(&wc->lru))
805 e = container_of(wc->lru.next, struct wc_entry, lru);
806 if (writecache_entry_is_committed(wc, e)) {
807 if (wc->overwrote_committed) {
808 writecache_wait_for_ios(wc, WRITE);
809 writecache_disk_flush(wc, wc->ssd_dev);
810 wc->overwrote_committed = false;
815 writecache_flush_entry(wc, e);
816 if (unlikely(e->lru.next == &wc->lru))
818 e2 = container_of(e->lru.next, struct wc_entry, lru);
819 if (writecache_entry_is_committed(wc, e2))
824 writecache_commit_flushed(wc, true);
827 pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
828 if (WC_MODE_PMEM(wc))
829 writecache_commit_flushed(wc, false);
831 ssd_commit_superblock(wc);
833 wc->overwrote_committed = false;
835 need_flush_after_free = false;
837 /* Free another committed entry with lower seq-count */
838 struct rb_node *rb_node = rb_prev(&e->rb_node);
841 e2 = container_of(rb_node, struct wc_entry, rb_node);
842 if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
843 likely(!e2->write_in_progress)) {
844 writecache_free_entry(wc, e2);
845 need_flush_after_free = true;
848 if (unlikely(e->lru.prev == &wc->lru))
850 e = container_of(e->lru.prev, struct wc_entry, lru);
854 if (need_flush_after_free)
855 writecache_commit_flushed(wc, false);
858 static void writecache_flush_work(struct work_struct *work)
860 struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
863 writecache_flush(wc);
867 static void writecache_autocommit_timer(struct timer_list *t)
869 struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
870 if (!writecache_has_error(wc))
871 queue_work(wc->writeback_wq, &wc->flush_work);
874 static void writecache_schedule_autocommit(struct dm_writecache *wc)
876 if (!timer_pending(&wc->autocommit_timer))
877 mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
880 static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
883 bool discarded_something = false;
885 e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
889 while (read_original_sector(wc, e) < end) {
890 struct rb_node *node = rb_next(&e->rb_node);
892 if (likely(!e->write_in_progress)) {
893 if (!discarded_something) {
894 if (!WC_MODE_PMEM(wc)) {
895 writecache_wait_for_ios(wc, READ);
896 writecache_wait_for_ios(wc, WRITE);
898 discarded_something = true;
900 if (!writecache_entry_is_committed(wc, e))
901 wc->uncommitted_blocks--;
902 writecache_free_entry(wc, e);
908 e = container_of(node, struct wc_entry, rb_node);
911 if (discarded_something)
912 writecache_commit_flushed(wc, false);
915 static bool writecache_wait_for_writeback(struct dm_writecache *wc)
917 if (wc->writeback_size) {
918 writecache_wait_on_freelist(wc);
924 static void writecache_suspend(struct dm_target *ti)
926 struct dm_writecache *wc = ti->private;
927 bool flush_on_suspend;
929 del_timer_sync(&wc->autocommit_timer);
930 del_timer_sync(&wc->max_age_timer);
933 writecache_flush(wc);
934 flush_on_suspend = wc->flush_on_suspend;
935 if (flush_on_suspend) {
936 wc->flush_on_suspend = false;
938 queue_work(wc->writeback_wq, &wc->writeback_work);
942 drain_workqueue(wc->writeback_wq);
945 if (flush_on_suspend)
947 while (writecache_wait_for_writeback(wc));
949 if (WC_MODE_PMEM(wc))
950 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
952 writecache_poison_lists(wc);
957 static int writecache_alloc_entries(struct dm_writecache *wc)
963 wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
966 for (b = 0; b < wc->n_blocks; b++) {
967 struct wc_entry *e = &wc->entries[b];
969 e->write_in_progress = false;
976 static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors)
978 struct dm_io_region region;
979 struct dm_io_request req;
981 region.bdev = wc->ssd_dev->bdev;
982 region.sector = wc->start_sector;
983 region.count = n_sectors;
984 req.bi_op = REQ_OP_READ;
985 req.bi_op_flags = REQ_SYNC;
986 req.mem.type = DM_IO_VMA;
987 req.mem.ptr.vma = (char *)wc->memory_map;
988 req.client = wc->dm_io;
989 req.notify.fn = NULL;
991 return dm_io(&req, 1, ®ion, NULL);
994 static void writecache_resume(struct dm_target *ti)
996 struct dm_writecache *wc = ti->private;
998 bool need_flush = false;
1004 wc->data_device_sectors = bdev_nr_sectors(wc->dev->bdev);
1006 if (WC_MODE_PMEM(wc)) {
1007 persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
1009 r = writecache_read_metadata(wc, wc->metadata_sectors);
1011 size_t sb_entries_offset;
1012 writecache_error(wc, r, "unable to read metadata: %d", r);
1013 sb_entries_offset = offsetof(struct wc_memory_superblock, entries);
1014 memset((char *)wc->memory_map + sb_entries_offset, -1,
1015 (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset);
1020 INIT_LIST_HEAD(&wc->lru);
1021 if (WC_MODE_SORT_FREELIST(wc)) {
1022 wc->freetree = RB_ROOT;
1023 wc->current_free = NULL;
1025 INIT_LIST_HEAD(&wc->freelist);
1027 wc->freelist_size = 0;
1029 r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count,
1032 writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
1033 sb_seq_count = cpu_to_le64(0);
1035 wc->seq_count = le64_to_cpu(sb_seq_count);
1037 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
1038 for (b = 0; b < wc->n_blocks; b++) {
1039 struct wc_entry *e = &wc->entries[b];
1040 struct wc_memory_entry wme;
1041 if (writecache_has_error(wc)) {
1042 e->original_sector = -1;
1046 r = copy_mc_to_kernel(&wme, memory_entry(wc, e),
1047 sizeof(struct wc_memory_entry));
1049 writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
1050 (unsigned long)b, r);
1051 e->original_sector = -1;
1054 e->original_sector = le64_to_cpu(wme.original_sector);
1055 e->seq_count = le64_to_cpu(wme.seq_count);
1060 for (b = 0; b < wc->n_blocks; b++) {
1061 struct wc_entry *e = &wc->entries[b];
1062 if (!writecache_entry_is_committed(wc, e)) {
1063 if (read_seq_count(wc, e) != -1) {
1065 clear_seq_count(wc, e);
1068 writecache_add_to_freelist(wc, e);
1070 struct wc_entry *old;
1072 old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
1074 writecache_insert_entry(wc, e);
1076 if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
1077 writecache_error(wc, -EINVAL,
1078 "two identical entries, position %llu, sector %llu, sequence %llu",
1079 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
1080 (unsigned long long)read_seq_count(wc, e));
1082 if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
1085 writecache_free_entry(wc, old);
1086 writecache_insert_entry(wc, e);
1095 writecache_flush_all_metadata(wc);
1096 writecache_commit_flushed(wc, false);
1099 writecache_verify_watermark(wc);
1101 if (wc->max_age != MAX_AGE_UNSPECIFIED)
1102 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
1107 static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1113 if (dm_suspended(wc->ti)) {
1117 if (writecache_has_error(wc)) {
1122 writecache_flush(wc);
1123 wc->writeback_all++;
1124 queue_work(wc->writeback_wq, &wc->writeback_work);
1127 flush_workqueue(wc->writeback_wq);
1130 wc->writeback_all--;
1131 if (writecache_has_error(wc)) {
1140 static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1146 wc->flush_on_suspend = true;
1152 static void activate_cleaner(struct dm_writecache *wc)
1154 wc->flush_on_suspend = true;
1156 wc->freelist_high_watermark = wc->n_blocks;
1157 wc->freelist_low_watermark = wc->n_blocks;
1160 static int process_cleaner_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1166 activate_cleaner(wc);
1167 if (!dm_suspended(wc->ti))
1168 writecache_verify_watermark(wc);
1174 static int process_clear_stats_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1180 memset(&wc->stats, 0, sizeof wc->stats);
1186 static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
1187 char *result, unsigned maxlen)
1190 struct dm_writecache *wc = ti->private;
1192 if (!strcasecmp(argv[0], "flush"))
1193 r = process_flush_mesg(argc, argv, wc);
1194 else if (!strcasecmp(argv[0], "flush_on_suspend"))
1195 r = process_flush_on_suspend_mesg(argc, argv, wc);
1196 else if (!strcasecmp(argv[0], "cleaner"))
1197 r = process_cleaner_mesg(argc, argv, wc);
1198 else if (!strcasecmp(argv[0], "clear_stats"))
1199 r = process_clear_stats_mesg(argc, argv, wc);
1201 DMERR("unrecognised message received: %s", argv[0]);
1206 static void memcpy_flushcache_optimized(void *dest, void *source, size_t size)
1209 * clflushopt performs better with block size 1024, 2048, 4096
1210 * non-temporal stores perform better with block size 512
1212 * block size 512 1024 2048 4096
1213 * movnti 496 MB/s 642 MB/s 725 MB/s 744 MB/s
1214 * clflushopt 373 MB/s 688 MB/s 1.1 GB/s 1.2 GB/s
1216 * We see that movnti performs better for 512-byte blocks, and
1217 * clflushopt performs better for 1024-byte and larger blocks. So, we
1218 * prefer clflushopt for sizes >= 768.
1220 * NOTE: this happens to be the case now (with dm-writecache's single
1221 * threaded model) but re-evaluate this once memcpy_flushcache() is
1222 * enabled to use movdir64b which might invalidate this performance
1223 * advantage seen with cache-allocating-writes plus flushing.
1226 if (static_cpu_has(X86_FEATURE_CLFLUSHOPT) &&
1227 likely(boot_cpu_data.x86_clflush_size == 64) &&
1228 likely(size >= 768)) {
1230 memcpy((void *)dest, (void *)source, 64);
1231 clflushopt((void *)dest);
1235 } while (size >= 64);
1239 memcpy_flushcache(dest, source, size);
1242 static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
1246 int rw = bio_data_dir(bio);
1247 unsigned remaining_size = wc->block_size;
1250 struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
1251 buf = bvec_kmap_local(&bv);
1253 if (unlikely(size > remaining_size))
1254 size = remaining_size;
1258 r = copy_mc_to_kernel(buf, data, size);
1259 flush_dcache_page(bio_page(bio));
1261 writecache_error(wc, r, "hardware memory error when reading data: %d", r);
1262 bio->bi_status = BLK_STS_IOERR;
1265 flush_dcache_page(bio_page(bio));
1266 memcpy_flushcache_optimized(data, buf, size);
1271 data = (char *)data + size;
1272 remaining_size -= size;
1273 bio_advance(bio, size);
1274 } while (unlikely(remaining_size));
1277 static int writecache_flush_thread(void *data)
1279 struct dm_writecache *wc = data;
1285 bio = bio_list_pop(&wc->flush_list);
1287 set_current_state(TASK_INTERRUPTIBLE);
1290 if (unlikely(kthread_should_stop())) {
1291 set_current_state(TASK_RUNNING);
1299 if (bio_op(bio) == REQ_OP_DISCARD) {
1300 writecache_discard(wc, bio->bi_iter.bi_sector,
1301 bio_end_sector(bio));
1303 bio_set_dev(bio, wc->dev->bdev);
1304 submit_bio_noacct(bio);
1306 writecache_flush(wc);
1308 if (writecache_has_error(wc))
1309 bio->bi_status = BLK_STS_IOERR;
1317 static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
1319 if (bio_list_empty(&wc->flush_list))
1320 wake_up_process(wc->flush_thread);
1321 bio_list_add(&wc->flush_list, bio);
1327 WC_MAP_REMAP_ORIGIN,
1332 static void writecache_map_remap_origin(struct dm_writecache *wc, struct bio *bio,
1336 sector_t next_boundary =
1337 read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1338 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT)
1339 dm_accept_partial_bio(bio, next_boundary);
1343 static enum wc_map_op writecache_map_read(struct dm_writecache *wc, struct bio *bio)
1345 enum wc_map_op map_op;
1350 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1351 if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1352 wc->stats.read_hits++;
1353 if (WC_MODE_PMEM(wc)) {
1354 bio_copy_block(wc, bio, memory_data(wc, e));
1355 if (bio->bi_iter.bi_size)
1356 goto read_next_block;
1357 map_op = WC_MAP_SUBMIT;
1359 dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1360 bio_set_dev(bio, wc->ssd_dev->bdev);
1361 bio->bi_iter.bi_sector = cache_sector(wc, e);
1362 if (!writecache_entry_is_committed(wc, e))
1363 writecache_wait_for_ios(wc, WRITE);
1364 map_op = WC_MAP_REMAP;
1367 writecache_map_remap_origin(wc, bio, e);
1368 wc->stats.reads += (bio->bi_iter.bi_size - wc->block_size) >> wc->block_size_bits;
1369 map_op = WC_MAP_REMAP_ORIGIN;
1375 static void writecache_bio_copy_ssd(struct dm_writecache *wc, struct bio *bio,
1376 struct wc_entry *e, bool search_used)
1378 unsigned bio_size = wc->block_size;
1379 sector_t start_cache_sec = cache_sector(wc, e);
1380 sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT);
1382 while (bio_size < bio->bi_iter.bi_size) {
1384 struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec);
1387 write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector +
1388 (bio_size >> SECTOR_SHIFT), wc->seq_count);
1389 writecache_insert_entry(wc, f);
1390 wc->uncommitted_blocks++;
1393 struct rb_node *next = rb_next(&e->rb_node);
1396 f = container_of(next, struct wc_entry, rb_node);
1399 if (read_original_sector(wc, f) !=
1400 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1402 if (unlikely(f->write_in_progress))
1404 if (writecache_entry_is_committed(wc, f))
1405 wc->overwrote_committed = true;
1408 bio_size += wc->block_size;
1409 current_cache_sec += wc->block_size >> SECTOR_SHIFT;
1412 bio_set_dev(bio, wc->ssd_dev->bdev);
1413 bio->bi_iter.bi_sector = start_cache_sec;
1414 dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT);
1416 wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits;
1417 wc->stats.writes_allocate += (bio->bi_iter.bi_size - wc->block_size) >> wc->block_size_bits;
1419 if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1420 wc->uncommitted_blocks = 0;
1421 queue_work(wc->writeback_wq, &wc->flush_work);
1423 writecache_schedule_autocommit(wc);
1427 static enum wc_map_op writecache_map_write(struct dm_writecache *wc, struct bio *bio)
1432 bool found_entry = false;
1433 bool search_used = false;
1434 if (writecache_has_error(wc)) {
1435 wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits;
1436 return WC_MAP_ERROR;
1438 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1440 if (!writecache_entry_is_committed(wc, e)) {
1441 wc->stats.write_hits_uncommitted++;
1445 wc->stats.write_hits_committed++;
1446 if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1447 wc->overwrote_committed = true;
1453 if (unlikely(wc->cleaner) ||
1454 (wc->metadata_only && !(bio->bi_opf & REQ_META)))
1457 e = writecache_pop_from_freelist(wc, (sector_t)-1);
1459 if (!WC_MODE_PMEM(wc) && !found_entry) {
1461 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1462 writecache_map_remap_origin(wc, bio, e);
1463 wc->stats.writes_around += bio->bi_iter.bi_size >> wc->block_size_bits;
1464 wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits;
1465 return WC_MAP_REMAP_ORIGIN;
1467 wc->stats.writes_blocked_on_freelist++;
1468 writecache_wait_on_freelist(wc);
1471 write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1472 writecache_insert_entry(wc, e);
1473 wc->uncommitted_blocks++;
1474 wc->stats.writes_allocate++;
1476 if (WC_MODE_PMEM(wc)) {
1477 bio_copy_block(wc, bio, memory_data(wc, e));
1480 writecache_bio_copy_ssd(wc, bio, e, search_used);
1481 return WC_MAP_REMAP;
1483 } while (bio->bi_iter.bi_size);
1485 if (unlikely(bio->bi_opf & REQ_FUA || wc->uncommitted_blocks >= wc->autocommit_blocks))
1486 writecache_flush(wc);
1488 writecache_schedule_autocommit(wc);
1490 return WC_MAP_SUBMIT;
1493 static enum wc_map_op writecache_map_flush(struct dm_writecache *wc, struct bio *bio)
1495 if (writecache_has_error(wc))
1496 return WC_MAP_ERROR;
1498 if (WC_MODE_PMEM(wc)) {
1499 wc->stats.flushes++;
1500 writecache_flush(wc);
1501 if (writecache_has_error(wc))
1502 return WC_MAP_ERROR;
1503 else if (unlikely(wc->cleaner) || unlikely(wc->metadata_only))
1504 return WC_MAP_REMAP_ORIGIN;
1505 return WC_MAP_SUBMIT;
1508 if (dm_bio_get_target_bio_nr(bio))
1509 return WC_MAP_REMAP_ORIGIN;
1510 wc->stats.flushes++;
1511 writecache_offload_bio(wc, bio);
1512 return WC_MAP_RETURN;
1515 static enum wc_map_op writecache_map_discard(struct dm_writecache *wc, struct bio *bio)
1517 wc->stats.discards += bio->bi_iter.bi_size >> wc->block_size_bits;
1519 if (writecache_has_error(wc))
1520 return WC_MAP_ERROR;
1522 if (WC_MODE_PMEM(wc)) {
1523 writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
1524 return WC_MAP_REMAP_ORIGIN;
1527 writecache_offload_bio(wc, bio);
1528 return WC_MAP_RETURN;
1531 static int writecache_map(struct dm_target *ti, struct bio *bio)
1533 struct dm_writecache *wc = ti->private;
1534 enum wc_map_op map_op;
1536 bio->bi_private = NULL;
1540 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1541 map_op = writecache_map_flush(wc, bio);
1545 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1547 if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
1548 (wc->block_size / 512 - 1)) != 0)) {
1549 DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
1550 (unsigned long long)bio->bi_iter.bi_sector,
1551 bio->bi_iter.bi_size, wc->block_size);
1552 map_op = WC_MAP_ERROR;
1556 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
1557 map_op = writecache_map_discard(wc, bio);
1561 if (bio_data_dir(bio) == READ)
1562 map_op = writecache_map_read(wc, bio);
1564 map_op = writecache_map_write(wc, bio);
1567 case WC_MAP_REMAP_ORIGIN:
1568 if (likely(wc->pause != 0)) {
1569 if (bio_op(bio) == REQ_OP_WRITE) {
1570 dm_iot_io_begin(&wc->iot, 1);
1571 bio->bi_private = (void *)2;
1574 bio_set_dev(bio, wc->dev->bdev);
1576 return DM_MAPIO_REMAPPED;
1579 /* make sure that writecache_end_io decrements bio_in_progress: */
1580 bio->bi_private = (void *)1;
1581 atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
1583 return DM_MAPIO_REMAPPED;
1588 return DM_MAPIO_SUBMITTED;
1592 return DM_MAPIO_SUBMITTED;
1597 return DM_MAPIO_SUBMITTED;
1605 static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
1607 struct dm_writecache *wc = ti->private;
1609 if (bio->bi_private == (void *)1) {
1610 int dir = bio_data_dir(bio);
1611 if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
1612 if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
1613 wake_up(&wc->bio_in_progress_wait[dir]);
1614 } else if (bio->bi_private == (void *)2) {
1615 dm_iot_io_end(&wc->iot, 1);
1620 static int writecache_iterate_devices(struct dm_target *ti,
1621 iterate_devices_callout_fn fn, void *data)
1623 struct dm_writecache *wc = ti->private;
1625 return fn(ti, wc->dev, 0, ti->len, data);
1628 static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
1630 struct dm_writecache *wc = ti->private;
1632 if (limits->logical_block_size < wc->block_size)
1633 limits->logical_block_size = wc->block_size;
1635 if (limits->physical_block_size < wc->block_size)
1636 limits->physical_block_size = wc->block_size;
1638 if (limits->io_min < wc->block_size)
1639 limits->io_min = wc->block_size;
1643 static void writecache_writeback_endio(struct bio *bio)
1645 struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
1646 struct dm_writecache *wc = wb->wc;
1647 unsigned long flags;
1649 raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
1650 if (unlikely(list_empty(&wc->endio_list)))
1651 wake_up_process(wc->endio_thread);
1652 list_add_tail(&wb->endio_entry, &wc->endio_list);
1653 raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
1656 static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
1658 struct copy_struct *c = ptr;
1659 struct dm_writecache *wc = c->wc;
1661 c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
1663 raw_spin_lock_irq(&wc->endio_list_lock);
1664 if (unlikely(list_empty(&wc->endio_list)))
1665 wake_up_process(wc->endio_thread);
1666 list_add_tail(&c->endio_entry, &wc->endio_list);
1667 raw_spin_unlock_irq(&wc->endio_list_lock);
1670 static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
1673 struct writeback_struct *wb;
1675 unsigned long n_walked = 0;
1678 wb = list_entry(list->next, struct writeback_struct, endio_entry);
1679 list_del(&wb->endio_entry);
1681 if (unlikely(wb->bio.bi_status != BLK_STS_OK))
1682 writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
1683 "write error %d", wb->bio.bi_status);
1687 BUG_ON(!e->write_in_progress);
1688 e->write_in_progress = false;
1689 INIT_LIST_HEAD(&e->lru);
1690 if (!writecache_has_error(wc))
1691 writecache_free_entry(wc, e);
1692 BUG_ON(!wc->writeback_size);
1693 wc->writeback_size--;
1695 if (unlikely(n_walked >= ENDIO_LATENCY)) {
1696 writecache_commit_flushed(wc, false);
1701 } while (++i < wb->wc_list_n);
1703 if (wb->wc_list != wb->wc_list_inline)
1706 } while (!list_empty(list));
1709 static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
1711 struct copy_struct *c;
1715 c = list_entry(list->next, struct copy_struct, endio_entry);
1716 list_del(&c->endio_entry);
1718 if (unlikely(c->error))
1719 writecache_error(wc, c->error, "copy error");
1723 BUG_ON(!e->write_in_progress);
1724 e->write_in_progress = false;
1725 INIT_LIST_HEAD(&e->lru);
1726 if (!writecache_has_error(wc))
1727 writecache_free_entry(wc, e);
1729 BUG_ON(!wc->writeback_size);
1730 wc->writeback_size--;
1732 } while (--c->n_entries);
1733 mempool_free(c, &wc->copy_pool);
1734 } while (!list_empty(list));
1737 static int writecache_endio_thread(void *data)
1739 struct dm_writecache *wc = data;
1742 struct list_head list;
1744 raw_spin_lock_irq(&wc->endio_list_lock);
1745 if (!list_empty(&wc->endio_list))
1747 set_current_state(TASK_INTERRUPTIBLE);
1748 raw_spin_unlock_irq(&wc->endio_list_lock);
1750 if (unlikely(kthread_should_stop())) {
1751 set_current_state(TASK_RUNNING);
1760 list = wc->endio_list;
1761 list.next->prev = list.prev->next = &list;
1762 INIT_LIST_HEAD(&wc->endio_list);
1763 raw_spin_unlock_irq(&wc->endio_list_lock);
1765 if (!WC_MODE_FUA(wc))
1766 writecache_disk_flush(wc, wc->dev);
1770 if (WC_MODE_PMEM(wc)) {
1771 __writecache_endio_pmem(wc, &list);
1773 __writecache_endio_ssd(wc, &list);
1774 writecache_wait_for_ios(wc, READ);
1777 writecache_commit_flushed(wc, false);
1785 static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e)
1787 struct dm_writecache *wc = wb->wc;
1788 unsigned block_size = wc->block_size;
1789 void *address = memory_data(wc, e);
1791 persistent_memory_flush_cache(address, block_size);
1793 if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors))
1796 return bio_add_page(&wb->bio, persistent_memory_page(address),
1797 block_size, persistent_memory_page_offset(address)) != 0;
1800 struct writeback_list {
1801 struct list_head list;
1805 static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
1807 if (unlikely(wc->max_writeback_jobs)) {
1808 if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
1810 while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
1811 writecache_wait_on_freelist(wc);
1818 static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
1820 struct wc_entry *e, *f;
1822 struct writeback_struct *wb;
1827 e = container_of(wbl->list.prev, struct wc_entry, lru);
1830 max_pages = e->wc_list_contiguous;
1832 bio = bio_alloc_bioset(wc->dev->bdev, max_pages, REQ_OP_WRITE,
1833 GFP_NOIO, &wc->bio_set);
1834 wb = container_of(bio, struct writeback_struct, bio);
1836 bio->bi_end_io = writecache_writeback_endio;
1837 bio->bi_iter.bi_sector = read_original_sector(wc, e);
1838 if (max_pages <= WB_LIST_INLINE ||
1839 unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
1840 GFP_NOIO | __GFP_NORETRY |
1841 __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
1842 wb->wc_list = wb->wc_list_inline;
1843 max_pages = WB_LIST_INLINE;
1846 BUG_ON(!wc_add_block(wb, e));
1851 while (wbl->size && wb->wc_list_n < max_pages) {
1852 f = container_of(wbl->list.prev, struct wc_entry, lru);
1853 if (read_original_sector(wc, f) !=
1854 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1856 if (!wc_add_block(wb, f))
1860 wb->wc_list[wb->wc_list_n++] = f;
1863 if (WC_MODE_FUA(wc))
1864 bio->bi_opf |= REQ_FUA;
1865 if (writecache_has_error(wc)) {
1866 bio->bi_status = BLK_STS_IOERR;
1868 } else if (unlikely(!bio_sectors(bio))) {
1869 bio->bi_status = BLK_STS_OK;
1875 __writeback_throttle(wc, wbl);
1879 static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
1881 struct wc_entry *e, *f;
1882 struct dm_io_region from, to;
1883 struct copy_struct *c;
1889 e = container_of(wbl->list.prev, struct wc_entry, lru);
1892 n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
1894 from.bdev = wc->ssd_dev->bdev;
1895 from.sector = cache_sector(wc, e);
1896 from.count = n_sectors;
1897 to.bdev = wc->dev->bdev;
1898 to.sector = read_original_sector(wc, e);
1899 to.count = n_sectors;
1901 c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
1904 c->n_entries = e->wc_list_contiguous;
1906 while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
1908 f = container_of(wbl->list.prev, struct wc_entry, lru);
1914 if (unlikely(to.sector + to.count > wc->data_device_sectors)) {
1915 if (to.sector >= wc->data_device_sectors) {
1916 writecache_copy_endio(0, 0, c);
1919 from.count = to.count = wc->data_device_sectors - to.sector;
1922 dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
1924 __writeback_throttle(wc, wbl);
1928 static void writecache_writeback(struct work_struct *work)
1930 struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
1931 struct blk_plug plug;
1932 struct wc_entry *f, *g, *e = NULL;
1933 struct rb_node *node, *next_node;
1934 struct list_head skipped;
1935 struct writeback_list wbl;
1936 unsigned long n_walked;
1938 if (!WC_MODE_PMEM(wc)) {
1939 /* Wait for any active kcopyd work on behalf of ssd writeback */
1940 dm_kcopyd_client_flush(wc->dm_kcopyd);
1943 if (likely(wc->pause != 0)) {
1946 if (unlikely(wc->cleaner) || unlikely(wc->writeback_all) ||
1947 unlikely(dm_suspended(wc->ti)))
1949 idle = dm_iot_idle_time(&wc->iot);
1950 if (idle >= wc->pause)
1952 idle = wc->pause - idle;
1955 schedule_timeout_idle(idle);
1961 if (writecache_has_error(wc)) {
1966 if (unlikely(wc->writeback_all)) {
1967 if (writecache_wait_for_writeback(wc))
1971 if (wc->overwrote_committed) {
1972 writecache_wait_for_ios(wc, WRITE);
1976 INIT_LIST_HEAD(&skipped);
1977 INIT_LIST_HEAD(&wbl.list);
1979 while (!list_empty(&wc->lru) &&
1980 (wc->writeback_all ||
1981 wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark ||
1982 (jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >=
1983 wc->max_age - wc->max_age / MAX_AGE_DIV))) {
1986 if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1987 likely(!wc->writeback_all)) {
1988 if (likely(!dm_suspended(wc->ti)))
1989 queue_work(wc->writeback_wq, &wc->writeback_work);
1993 if (unlikely(wc->writeback_all)) {
1995 writecache_flush(wc);
1996 e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
2000 e = container_of(wc->lru.prev, struct wc_entry, lru);
2001 BUG_ON(e->write_in_progress);
2002 if (unlikely(!writecache_entry_is_committed(wc, e))) {
2003 writecache_flush(wc);
2005 node = rb_prev(&e->rb_node);
2007 f = container_of(node, struct wc_entry, rb_node);
2008 if (unlikely(read_original_sector(wc, f) ==
2009 read_original_sector(wc, e))) {
2010 BUG_ON(!f->write_in_progress);
2011 list_move(&e->lru, &skipped);
2016 wc->writeback_size++;
2017 list_move(&e->lru, &wbl.list);
2019 e->write_in_progress = true;
2020 e->wc_list_contiguous = 1;
2025 next_node = rb_next(&f->rb_node);
2026 if (unlikely(!next_node))
2028 g = container_of(next_node, struct wc_entry, rb_node);
2029 if (unlikely(read_original_sector(wc, g) ==
2030 read_original_sector(wc, f))) {
2034 if (read_original_sector(wc, g) !=
2035 read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
2037 if (unlikely(g->write_in_progress))
2039 if (unlikely(!writecache_entry_is_committed(wc, g)))
2042 if (!WC_MODE_PMEM(wc)) {
2048 //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
2051 wc->writeback_size++;
2052 list_move(&g->lru, &wbl.list);
2054 g->write_in_progress = true;
2055 g->wc_list_contiguous = BIO_MAX_VECS;
2057 e->wc_list_contiguous++;
2058 if (unlikely(e->wc_list_contiguous == BIO_MAX_VECS)) {
2059 if (unlikely(wc->writeback_all)) {
2060 next_node = rb_next(&f->rb_node);
2061 if (likely(next_node))
2062 g = container_of(next_node, struct wc_entry, rb_node);
2070 if (!list_empty(&skipped)) {
2071 list_splice_tail(&skipped, &wc->lru);
2073 * If we didn't do any progress, we must wait until some
2074 * writeback finishes to avoid burning CPU in a loop
2076 if (unlikely(!wbl.size))
2077 writecache_wait_for_writeback(wc);
2082 blk_start_plug(&plug);
2084 if (WC_MODE_PMEM(wc))
2085 __writecache_writeback_pmem(wc, &wbl);
2087 __writecache_writeback_ssd(wc, &wbl);
2089 blk_finish_plug(&plug);
2091 if (unlikely(wc->writeback_all)) {
2093 while (writecache_wait_for_writeback(wc));
2098 static int calculate_memory_size(uint64_t device_size, unsigned block_size,
2099 size_t *n_blocks_p, size_t *n_metadata_blocks_p)
2101 uint64_t n_blocks, offset;
2104 n_blocks = device_size;
2105 do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
2110 /* Verify the following entries[n_blocks] won't overflow */
2111 if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
2112 sizeof(struct wc_memory_entry)))
2114 offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
2115 offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
2116 if (offset + n_blocks * block_size <= device_size)
2121 /* check if the bit field overflows */
2123 if (e.index != n_blocks)
2127 *n_blocks_p = n_blocks;
2128 if (n_metadata_blocks_p)
2129 *n_metadata_blocks_p = offset >> __ffs(block_size);
2133 static int init_memory(struct dm_writecache *wc)
2138 r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
2142 r = writecache_alloc_entries(wc);
2146 for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
2147 pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
2148 pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
2149 pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
2150 pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
2151 pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
2153 for (b = 0; b < wc->n_blocks; b++) {
2154 write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
2158 writecache_flush_all_metadata(wc);
2159 writecache_commit_flushed(wc, false);
2160 pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
2161 writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
2162 writecache_commit_flushed(wc, false);
2167 static void writecache_dtr(struct dm_target *ti)
2169 struct dm_writecache *wc = ti->private;
2174 if (wc->endio_thread)
2175 kthread_stop(wc->endio_thread);
2177 if (wc->flush_thread)
2178 kthread_stop(wc->flush_thread);
2180 bioset_exit(&wc->bio_set);
2182 mempool_exit(&wc->copy_pool);
2184 if (wc->writeback_wq)
2185 destroy_workqueue(wc->writeback_wq);
2188 dm_put_device(ti, wc->dev);
2191 dm_put_device(ti, wc->ssd_dev);
2195 if (wc->memory_map) {
2196 if (WC_MODE_PMEM(wc))
2197 persistent_memory_release(wc);
2199 vfree(wc->memory_map);
2203 dm_kcopyd_client_destroy(wc->dm_kcopyd);
2206 dm_io_client_destroy(wc->dm_io);
2208 vfree(wc->dirty_bitmap);
2213 static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2215 struct dm_writecache *wc;
2216 struct dm_arg_set as;
2218 unsigned opt_params;
2219 size_t offset, data_size;
2222 int high_wm_percent = HIGH_WATERMARK;
2223 int low_wm_percent = LOW_WATERMARK;
2225 struct wc_memory_superblock s;
2227 static struct dm_arg _args[] = {
2228 {0, 18, "Invalid number of feature args"},
2234 wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
2236 ti->error = "Cannot allocate writecache structure";
2243 mutex_init(&wc->lock);
2244 wc->max_age = MAX_AGE_UNSPECIFIED;
2245 writecache_poison_lists(wc);
2246 init_waitqueue_head(&wc->freelist_wait);
2247 timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
2248 timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0);
2250 for (i = 0; i < 2; i++) {
2251 atomic_set(&wc->bio_in_progress[i], 0);
2252 init_waitqueue_head(&wc->bio_in_progress_wait[i]);
2255 wc->dm_io = dm_io_client_create();
2256 if (IS_ERR(wc->dm_io)) {
2257 r = PTR_ERR(wc->dm_io);
2258 ti->error = "Unable to allocate dm-io client";
2263 wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
2264 if (!wc->writeback_wq) {
2266 ti->error = "Could not allocate writeback workqueue";
2269 INIT_WORK(&wc->writeback_work, writecache_writeback);
2270 INIT_WORK(&wc->flush_work, writecache_flush_work);
2272 dm_iot_init(&wc->iot);
2274 raw_spin_lock_init(&wc->endio_list_lock);
2275 INIT_LIST_HEAD(&wc->endio_list);
2276 wc->endio_thread = kthread_run(writecache_endio_thread, wc, "writecache_endio");
2277 if (IS_ERR(wc->endio_thread)) {
2278 r = PTR_ERR(wc->endio_thread);
2279 wc->endio_thread = NULL;
2280 ti->error = "Couldn't spawn endio thread";
2285 * Parse the mode (pmem or ssd)
2287 string = dm_shift_arg(&as);
2291 if (!strcasecmp(string, "s")) {
2292 wc->pmem_mode = false;
2293 } else if (!strcasecmp(string, "p")) {
2294 #ifdef DM_WRITECACHE_HAS_PMEM
2295 wc->pmem_mode = true;
2296 wc->writeback_fua = true;
2299 * If the architecture doesn't support persistent memory or
2300 * the kernel doesn't support any DAX drivers, this driver can
2301 * only be used in SSD-only mode.
2304 ti->error = "Persistent memory or DAX not supported on this system";
2311 if (WC_MODE_PMEM(wc)) {
2312 r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
2313 offsetof(struct writeback_struct, bio),
2316 ti->error = "Could not allocate bio set";
2320 wc->pause = PAUSE_WRITEBACK;
2321 r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
2323 ti->error = "Could not allocate mempool";
2329 * Parse the origin data device
2331 string = dm_shift_arg(&as);
2334 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
2336 ti->error = "Origin data device lookup failed";
2341 * Parse cache data device (be it pmem or ssd)
2343 string = dm_shift_arg(&as);
2347 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
2349 ti->error = "Cache data device lookup failed";
2352 wc->memory_map_size = bdev_nr_bytes(wc->ssd_dev->bdev);
2355 * Parse the cache block size
2357 string = dm_shift_arg(&as);
2360 if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
2361 wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
2362 (wc->block_size & (wc->block_size - 1))) {
2364 ti->error = "Invalid block size";
2367 if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) ||
2368 wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) {
2370 ti->error = "Block size is smaller than device logical block size";
2373 wc->block_size_bits = __ffs(wc->block_size);
2375 wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
2376 wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
2377 wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
2380 * Parse optional arguments
2382 r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2386 while (opt_params) {
2387 string = dm_shift_arg(&as), opt_params--;
2388 if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2389 unsigned long long start_sector;
2390 string = dm_shift_arg(&as), opt_params--;
2391 if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2392 goto invalid_optional;
2393 wc->start_sector = start_sector;
2394 wc->start_sector_set = true;
2395 if (wc->start_sector != start_sector ||
2396 wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2397 goto invalid_optional;
2398 } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
2399 string = dm_shift_arg(&as), opt_params--;
2400 if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
2401 goto invalid_optional;
2402 if (high_wm_percent < 0 || high_wm_percent > 100)
2403 goto invalid_optional;
2404 wc->high_wm_percent_value = high_wm_percent;
2405 wc->high_wm_percent_set = true;
2406 } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
2407 string = dm_shift_arg(&as), opt_params--;
2408 if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
2409 goto invalid_optional;
2410 if (low_wm_percent < 0 || low_wm_percent > 100)
2411 goto invalid_optional;
2412 wc->low_wm_percent_value = low_wm_percent;
2413 wc->low_wm_percent_set = true;
2414 } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
2415 string = dm_shift_arg(&as), opt_params--;
2416 if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
2417 goto invalid_optional;
2418 wc->max_writeback_jobs_set = true;
2419 } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
2420 string = dm_shift_arg(&as), opt_params--;
2421 if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
2422 goto invalid_optional;
2423 wc->autocommit_blocks_set = true;
2424 } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
2425 unsigned autocommit_msecs;
2426 string = dm_shift_arg(&as), opt_params--;
2427 if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
2428 goto invalid_optional;
2429 if (autocommit_msecs > 3600000)
2430 goto invalid_optional;
2431 wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2432 wc->autocommit_time_value = autocommit_msecs;
2433 wc->autocommit_time_set = true;
2434 } else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
2435 unsigned max_age_msecs;
2436 string = dm_shift_arg(&as), opt_params--;
2437 if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
2438 goto invalid_optional;
2439 if (max_age_msecs > 86400000)
2440 goto invalid_optional;
2441 wc->max_age = msecs_to_jiffies(max_age_msecs);
2442 wc->max_age_set = true;
2443 wc->max_age_value = max_age_msecs;
2444 } else if (!strcasecmp(string, "cleaner")) {
2445 wc->cleaner_set = true;
2447 } else if (!strcasecmp(string, "fua")) {
2448 if (WC_MODE_PMEM(wc)) {
2449 wc->writeback_fua = true;
2450 wc->writeback_fua_set = true;
2451 } else goto invalid_optional;
2452 } else if (!strcasecmp(string, "nofua")) {
2453 if (WC_MODE_PMEM(wc)) {
2454 wc->writeback_fua = false;
2455 wc->writeback_fua_set = true;
2456 } else goto invalid_optional;
2457 } else if (!strcasecmp(string, "metadata_only")) {
2458 wc->metadata_only = true;
2459 } else if (!strcasecmp(string, "pause_writeback") && opt_params >= 1) {
2460 unsigned pause_msecs;
2461 if (WC_MODE_PMEM(wc))
2462 goto invalid_optional;
2463 string = dm_shift_arg(&as), opt_params--;
2464 if (sscanf(string, "%u%c", &pause_msecs, &dummy) != 1)
2465 goto invalid_optional;
2466 if (pause_msecs > 60000)
2467 goto invalid_optional;
2468 wc->pause = msecs_to_jiffies(pause_msecs);
2469 wc->pause_set = true;
2470 wc->pause_value = pause_msecs;
2474 ti->error = "Invalid optional argument";
2479 if (high_wm_percent < low_wm_percent) {
2481 ti->error = "High watermark must be greater than or equal to low watermark";
2485 if (WC_MODE_PMEM(wc)) {
2486 if (!dax_synchronous(wc->ssd_dev->dax_dev)) {
2488 ti->error = "Asynchronous persistent memory not supported as pmem cache";
2492 r = persistent_memory_claim(wc);
2494 ti->error = "Unable to map persistent memory for cache";
2498 size_t n_blocks, n_metadata_blocks;
2499 uint64_t n_bitmap_bits;
2501 wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2503 bio_list_init(&wc->flush_list);
2504 wc->flush_thread = kthread_run(writecache_flush_thread, wc, "dm_writecache_flush");
2505 if (IS_ERR(wc->flush_thread)) {
2506 r = PTR_ERR(wc->flush_thread);
2507 wc->flush_thread = NULL;
2508 ti->error = "Couldn't spawn flush thread";
2512 r = calculate_memory_size(wc->memory_map_size, wc->block_size,
2513 &n_blocks, &n_metadata_blocks);
2515 ti->error = "Invalid device size";
2519 n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
2520 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
2521 /* this is limitation of test_bit functions */
2522 if (n_bitmap_bits > 1U << 31) {
2524 ti->error = "Invalid device size";
2528 wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
2529 if (!wc->memory_map) {
2531 ti->error = "Unable to allocate memory for metadata";
2535 wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2536 if (IS_ERR(wc->dm_kcopyd)) {
2537 r = PTR_ERR(wc->dm_kcopyd);
2538 ti->error = "Unable to allocate dm-kcopyd client";
2539 wc->dm_kcopyd = NULL;
2543 wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
2544 wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
2545 BITS_PER_LONG * sizeof(unsigned long);
2546 wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
2547 if (!wc->dirty_bitmap) {
2549 ti->error = "Unable to allocate dirty bitmap";
2553 r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT);
2555 ti->error = "Unable to read first block of metadata";
2560 r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock));
2562 ti->error = "Hardware memory error when reading superblock";
2565 if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
2566 r = init_memory(wc);
2568 ti->error = "Unable to initialize device";
2571 r = copy_mc_to_kernel(&s, sb(wc),
2572 sizeof(struct wc_memory_superblock));
2574 ti->error = "Hardware memory error when reading superblock";
2579 if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
2580 ti->error = "Invalid magic in the superblock";
2585 if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
2586 ti->error = "Invalid version in the superblock";
2591 if (le32_to_cpu(s.block_size) != wc->block_size) {
2592 ti->error = "Block size does not match superblock";
2597 wc->n_blocks = le64_to_cpu(s.n_blocks);
2599 offset = wc->n_blocks * sizeof(struct wc_memory_entry);
2600 if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
2602 ti->error = "Overflow in size calculation";
2606 offset += sizeof(struct wc_memory_superblock);
2607 if (offset < sizeof(struct wc_memory_superblock))
2609 offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
2610 data_size = wc->n_blocks * (size_t)wc->block_size;
2611 if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
2612 (offset + data_size < offset))
2614 if (offset + data_size > wc->memory_map_size) {
2615 ti->error = "Memory area is too small";
2620 wc->metadata_sectors = offset >> SECTOR_SHIFT;
2621 wc->block_start = (char *)sb(wc) + offset;
2623 x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
2626 wc->freelist_high_watermark = x;
2627 x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
2630 wc->freelist_low_watermark = x;
2633 activate_cleaner(wc);
2635 r = writecache_alloc_entries(wc);
2637 ti->error = "Cannot allocate memory";
2641 ti->num_flush_bios = WC_MODE_PMEM(wc) ? 1 : 2;
2642 ti->flush_supported = true;
2643 ti->num_discard_bios = 1;
2645 if (WC_MODE_PMEM(wc))
2646 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
2652 ti->error = "Bad arguments";
2658 static void writecache_status(struct dm_target *ti, status_type_t type,
2659 unsigned status_flags, char *result, unsigned maxlen)
2661 struct dm_writecache *wc = ti->private;
2662 unsigned extra_args;
2666 case STATUSTYPE_INFO:
2667 DMEMIT("%ld %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
2668 writecache_has_error(wc),
2669 (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2670 (unsigned long long)wc->writeback_size,
2672 wc->stats.read_hits,
2674 wc->stats.write_hits_uncommitted,
2675 wc->stats.write_hits_committed,
2676 wc->stats.writes_around,
2677 wc->stats.writes_allocate,
2678 wc->stats.writes_blocked_on_freelist,
2680 wc->stats.discards);
2682 case STATUSTYPE_TABLE:
2683 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
2684 wc->dev->name, wc->ssd_dev->name, wc->block_size);
2686 if (wc->start_sector_set)
2688 if (wc->high_wm_percent_set)
2690 if (wc->low_wm_percent_set)
2692 if (wc->max_writeback_jobs_set)
2694 if (wc->autocommit_blocks_set)
2696 if (wc->autocommit_time_set)
2698 if (wc->max_age_set)
2700 if (wc->cleaner_set)
2702 if (wc->writeback_fua_set)
2704 if (wc->metadata_only)
2709 DMEMIT("%u", extra_args);
2710 if (wc->start_sector_set)
2711 DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2712 if (wc->high_wm_percent_set)
2713 DMEMIT(" high_watermark %u", wc->high_wm_percent_value);
2714 if (wc->low_wm_percent_set)
2715 DMEMIT(" low_watermark %u", wc->low_wm_percent_value);
2716 if (wc->max_writeback_jobs_set)
2717 DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
2718 if (wc->autocommit_blocks_set)
2719 DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
2720 if (wc->autocommit_time_set)
2721 DMEMIT(" autocommit_time %u", wc->autocommit_time_value);
2722 if (wc->max_age_set)
2723 DMEMIT(" max_age %u", wc->max_age_value);
2724 if (wc->cleaner_set)
2726 if (wc->writeback_fua_set)
2727 DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2728 if (wc->metadata_only)
2729 DMEMIT(" metadata_only");
2731 DMEMIT(" pause_writeback %u", wc->pause_value);
2733 case STATUSTYPE_IMA:
2739 static struct target_type writecache_target = {
2740 .name = "writecache",
2741 .version = {1, 6, 0},
2742 .module = THIS_MODULE,
2743 .ctr = writecache_ctr,
2744 .dtr = writecache_dtr,
2745 .status = writecache_status,
2746 .postsuspend = writecache_suspend,
2747 .resume = writecache_resume,
2748 .message = writecache_message,
2749 .map = writecache_map,
2750 .end_io = writecache_end_io,
2751 .iterate_devices = writecache_iterate_devices,
2752 .io_hints = writecache_io_hints,
2755 static int __init dm_writecache_init(void)
2759 r = dm_register_target(&writecache_target);
2761 DMERR("register failed %d", r);
2768 static void __exit dm_writecache_exit(void)
2770 dm_unregister_target(&writecache_target);
2773 module_init(dm_writecache_init);
2774 module_exit(dm_writecache_exit);
2776 MODULE_DESCRIPTION(DM_NAME " writecache target");
2777 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2778 MODULE_LICENSE("GPL");