1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Red Hat. All rights reserved.
5 * This file is released under the GPL.
8 #include <linux/device-mapper.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/vmalloc.h>
12 #include <linux/kthread.h>
13 #include <linux/dm-io.h>
14 #include <linux/dm-kcopyd.h>
15 #include <linux/dax.h>
16 #include <linux/pfn_t.h>
17 #include <linux/libnvdimm.h>
19 #define DM_MSG_PREFIX "writecache"
21 #define HIGH_WATERMARK 50
22 #define LOW_WATERMARK 45
23 #define MAX_WRITEBACK_JOBS 0
24 #define ENDIO_LATENCY 16
25 #define WRITEBACK_LATENCY 64
26 #define AUTOCOMMIT_BLOCKS_SSD 65536
27 #define AUTOCOMMIT_BLOCKS_PMEM 64
28 #define AUTOCOMMIT_MSEC 1000
29 #define MAX_AGE_DIV 16
30 #define MAX_AGE_UNSPECIFIED -1UL
32 #define BITMAP_GRANULARITY 65536
33 #if BITMAP_GRANULARITY < PAGE_SIZE
34 #undef BITMAP_GRANULARITY
35 #define BITMAP_GRANULARITY PAGE_SIZE
38 #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_DAX_DRIVER)
39 #define DM_WRITECACHE_HAS_PMEM
42 #ifdef DM_WRITECACHE_HAS_PMEM
43 #define pmem_assign(dest, src) \
45 typeof(dest) uniq = (src); \
46 memcpy_flushcache(&(dest), &uniq, sizeof(dest)); \
49 #define pmem_assign(dest, src) ((dest) = (src))
52 #if defined(__HAVE_ARCH_MEMCPY_MCSAFE) && defined(DM_WRITECACHE_HAS_PMEM)
53 #define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
56 #define MEMORY_SUPERBLOCK_MAGIC 0x23489321
57 #define MEMORY_SUPERBLOCK_VERSION 1
59 struct wc_memory_entry {
60 __le64 original_sector;
64 struct wc_memory_superblock {
76 struct wc_memory_entry entries[0];
80 struct rb_node rb_node;
82 unsigned short wc_list_contiguous;
83 bool write_in_progress
84 #if BITS_PER_LONG == 64
89 #if BITS_PER_LONG == 64
94 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
95 uint64_t original_sector;
100 #ifdef DM_WRITECACHE_HAS_PMEM
101 #define WC_MODE_PMEM(wc) ((wc)->pmem_mode)
102 #define WC_MODE_FUA(wc) ((wc)->writeback_fua)
104 #define WC_MODE_PMEM(wc) false
105 #define WC_MODE_FUA(wc) false
107 #define WC_MODE_SORT_FREELIST(wc) (!WC_MODE_PMEM(wc))
109 struct dm_writecache {
111 struct list_head lru;
113 struct list_head freelist;
115 struct rb_root freetree;
116 struct wc_entry *current_free;
121 size_t freelist_size;
122 size_t writeback_size;
123 size_t freelist_high_watermark;
124 size_t freelist_low_watermark;
125 unsigned long max_age;
127 unsigned uncommitted_blocks;
128 unsigned autocommit_blocks;
129 unsigned max_writeback_jobs;
133 unsigned long autocommit_jiffies;
134 struct timer_list autocommit_timer;
135 struct wait_queue_head freelist_wait;
137 struct timer_list max_age_timer;
139 atomic_t bio_in_progress[2];
140 struct wait_queue_head bio_in_progress_wait[2];
142 struct dm_target *ti;
144 struct dm_dev *ssd_dev;
145 sector_t start_sector;
147 uint64_t memory_map_size;
148 size_t metadata_sectors;
152 struct wc_entry *entries;
154 unsigned char block_size_bits;
157 bool writeback_fua:1;
159 bool overwrote_committed:1;
160 bool memory_vmapped:1;
162 bool high_wm_percent_set:1;
163 bool low_wm_percent_set:1;
164 bool max_writeback_jobs_set:1;
165 bool autocommit_blocks_set:1;
166 bool autocommit_time_set:1;
167 bool writeback_fua_set:1;
168 bool flush_on_suspend:1;
171 unsigned writeback_all;
172 struct workqueue_struct *writeback_wq;
173 struct work_struct writeback_work;
174 struct work_struct flush_work;
176 struct dm_io_client *dm_io;
178 raw_spinlock_t endio_list_lock;
179 struct list_head endio_list;
180 struct task_struct *endio_thread;
182 struct task_struct *flush_thread;
183 struct bio_list flush_list;
185 struct dm_kcopyd_client *dm_kcopyd;
186 unsigned long *dirty_bitmap;
187 unsigned dirty_bitmap_size;
189 struct bio_set bio_set;
193 #define WB_LIST_INLINE 16
195 struct writeback_struct {
196 struct list_head endio_entry;
197 struct dm_writecache *wc;
198 struct wc_entry **wc_list;
200 struct wc_entry *wc_list_inline[WB_LIST_INLINE];
205 struct list_head endio_entry;
206 struct dm_writecache *wc;
212 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
213 "A percentage of time allocated for data copying");
215 static void wc_lock(struct dm_writecache *wc)
217 mutex_lock(&wc->lock);
220 static void wc_unlock(struct dm_writecache *wc)
222 mutex_unlock(&wc->lock);
225 #ifdef DM_WRITECACHE_HAS_PMEM
226 static int persistent_memory_claim(struct dm_writecache *wc)
235 wc->memory_vmapped = false;
237 if (!wc->ssd_dev->dax_dev) {
241 s = wc->memory_map_size;
247 if (p != s >> PAGE_SHIFT) {
252 id = dax_read_lock();
254 da = dax_direct_access(wc->ssd_dev->dax_dev, 0, p, &wc->memory_map, &pfn);
256 wc->memory_map = NULL;
260 if (!pfn_t_has_page(pfn)) {
261 wc->memory_map = NULL;
267 wc->memory_map = NULL;
268 pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
276 daa = dax_direct_access(wc->ssd_dev->dax_dev, i, p - i,
279 r = daa ? daa : -EINVAL;
282 if (!pfn_t_has_page(pfn)) {
286 while (daa-- && i < p) {
287 pages[i++] = pfn_t_to_page(pfn);
291 wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
292 if (!wc->memory_map) {
297 wc->memory_vmapped = true;
302 wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
303 wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
314 static int persistent_memory_claim(struct dm_writecache *wc)
320 static void persistent_memory_release(struct dm_writecache *wc)
322 if (wc->memory_vmapped)
323 vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
326 static struct page *persistent_memory_page(void *addr)
328 if (is_vmalloc_addr(addr))
329 return vmalloc_to_page(addr);
331 return virt_to_page(addr);
334 static unsigned persistent_memory_page_offset(void *addr)
336 return (unsigned long)addr & (PAGE_SIZE - 1);
339 static void persistent_memory_flush_cache(void *ptr, size_t size)
341 if (is_vmalloc_addr(ptr))
342 flush_kernel_vmap_range(ptr, size);
345 static void persistent_memory_invalidate_cache(void *ptr, size_t size)
347 if (is_vmalloc_addr(ptr))
348 invalidate_kernel_vmap_range(ptr, size);
351 static struct wc_memory_superblock *sb(struct dm_writecache *wc)
353 return wc->memory_map;
356 static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
358 return &sb(wc)->entries[e->index];
361 static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
363 return (char *)wc->block_start + (e->index << wc->block_size_bits);
366 static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
368 return wc->start_sector + wc->metadata_sectors +
369 ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
372 static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
374 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
375 return e->original_sector;
377 return le64_to_cpu(memory_entry(wc, e)->original_sector);
381 static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
383 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
386 return le64_to_cpu(memory_entry(wc, e)->seq_count);
390 static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
392 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
395 pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
398 static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
399 uint64_t original_sector, uint64_t seq_count)
401 struct wc_memory_entry me;
402 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
403 e->original_sector = original_sector;
404 e->seq_count = seq_count;
406 me.original_sector = cpu_to_le64(original_sector);
407 me.seq_count = cpu_to_le64(seq_count);
408 pmem_assign(*memory_entry(wc, e), me);
411 #define writecache_error(wc, err, msg, arg...) \
413 if (!cmpxchg(&(wc)->error, 0, err)) \
415 wake_up(&(wc)->freelist_wait); \
418 #define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error)))
420 static void writecache_flush_all_metadata(struct dm_writecache *wc)
422 if (!WC_MODE_PMEM(wc))
423 memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
426 static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
428 if (!WC_MODE_PMEM(wc))
429 __set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
433 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
436 struct dm_writecache *wc;
441 static void writecache_notify_io(unsigned long error, void *context)
443 struct io_notify *endio = context;
445 if (unlikely(error != 0))
446 writecache_error(endio->wc, -EIO, "error writing metadata");
447 BUG_ON(atomic_read(&endio->count) <= 0);
448 if (atomic_dec_and_test(&endio->count))
452 static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
454 wait_event(wc->bio_in_progress_wait[direction],
455 !atomic_read(&wc->bio_in_progress[direction]));
458 static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
460 struct dm_io_region region;
461 struct dm_io_request req;
462 struct io_notify endio = {
464 COMPLETION_INITIALIZER_ONSTACK(endio.c),
467 unsigned bitmap_bits = wc->dirty_bitmap_size * 8;
472 i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i);
473 if (unlikely(i == bitmap_bits))
475 j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i);
477 region.bdev = wc->ssd_dev->bdev;
478 region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
479 region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
481 if (unlikely(region.sector >= wc->metadata_sectors))
483 if (unlikely(region.sector + region.count > wc->metadata_sectors))
484 region.count = wc->metadata_sectors - region.sector;
486 region.sector += wc->start_sector;
487 atomic_inc(&endio.count);
488 req.bi_op = REQ_OP_WRITE;
489 req.bi_op_flags = REQ_SYNC;
490 req.mem.type = DM_IO_VMA;
491 req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
492 req.client = wc->dm_io;
493 req.notify.fn = writecache_notify_io;
494 req.notify.context = &endio;
496 /* writing via async dm-io (implied by notify.fn above) won't return an error */
497 (void) dm_io(&req, 1, ®ion, NULL);
501 writecache_notify_io(0, &endio);
502 wait_for_completion_io(&endio.c);
505 writecache_wait_for_ios(wc, WRITE);
507 writecache_disk_flush(wc, wc->ssd_dev);
509 memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
512 static void ssd_commit_superblock(struct dm_writecache *wc)
515 struct dm_io_region region;
516 struct dm_io_request req;
518 region.bdev = wc->ssd_dev->bdev;
520 region.count = PAGE_SIZE;
522 if (unlikely(region.sector + region.count > wc->metadata_sectors))
523 region.count = wc->metadata_sectors - region.sector;
525 region.sector += wc->start_sector;
527 req.bi_op = REQ_OP_WRITE;
528 req.bi_op_flags = REQ_SYNC | REQ_FUA;
529 req.mem.type = DM_IO_VMA;
530 req.mem.ptr.vma = (char *)wc->memory_map;
531 req.client = wc->dm_io;
532 req.notify.fn = NULL;
533 req.notify.context = NULL;
535 r = dm_io(&req, 1, ®ion, NULL);
537 writecache_error(wc, r, "error writing superblock");
540 static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
542 if (WC_MODE_PMEM(wc))
545 ssd_commit_flushed(wc, wait_for_ios);
548 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
551 struct dm_io_region region;
552 struct dm_io_request req;
554 region.bdev = dev->bdev;
557 req.bi_op = REQ_OP_WRITE;
558 req.bi_op_flags = REQ_PREFLUSH;
559 req.mem.type = DM_IO_KMEM;
560 req.mem.ptr.addr = NULL;
561 req.client = wc->dm_io;
562 req.notify.fn = NULL;
564 r = dm_io(&req, 1, ®ion, NULL);
566 writecache_error(wc, r, "error flushing metadata: %d", r);
569 #define WFE_RETURN_FOLLOWING 1
570 #define WFE_LOWEST_SEQ 2
572 static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
573 uint64_t block, int flags)
576 struct rb_node *node = wc->tree.rb_node;
582 e = container_of(node, struct wc_entry, rb_node);
583 if (read_original_sector(wc, e) == block)
586 node = (read_original_sector(wc, e) >= block ?
587 e->rb_node.rb_left : e->rb_node.rb_right);
588 if (unlikely(!node)) {
589 if (!(flags & WFE_RETURN_FOLLOWING))
591 if (read_original_sector(wc, e) >= block) {
594 node = rb_next(&e->rb_node);
597 e = container_of(node, struct wc_entry, rb_node);
605 if (flags & WFE_LOWEST_SEQ)
606 node = rb_prev(&e->rb_node);
608 node = rb_next(&e->rb_node);
611 e2 = container_of(node, struct wc_entry, rb_node);
612 if (read_original_sector(wc, e2) != block)
618 static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
621 struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
624 e = container_of(*node, struct wc_entry, rb_node);
625 parent = &e->rb_node;
626 if (read_original_sector(wc, e) > read_original_sector(wc, ins))
627 node = &parent->rb_left;
629 node = &parent->rb_right;
631 rb_link_node(&ins->rb_node, parent, node);
632 rb_insert_color(&ins->rb_node, &wc->tree);
633 list_add(&ins->lru, &wc->lru);
637 static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
640 rb_erase(&e->rb_node, &wc->tree);
643 static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
645 if (WC_MODE_SORT_FREELIST(wc)) {
646 struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
647 if (unlikely(!*node))
648 wc->current_free = e;
651 if (&e->rb_node < *node)
652 node = &parent->rb_left;
654 node = &parent->rb_right;
656 rb_link_node(&e->rb_node, parent, node);
657 rb_insert_color(&e->rb_node, &wc->freetree);
659 list_add_tail(&e->lru, &wc->freelist);
664 static inline void writecache_verify_watermark(struct dm_writecache *wc)
666 if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
667 queue_work(wc->writeback_wq, &wc->writeback_work);
670 static void writecache_max_age_timer(struct timer_list *t)
672 struct dm_writecache *wc = from_timer(wc, t, max_age_timer);
674 if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) {
675 queue_work(wc->writeback_wq, &wc->writeback_work);
676 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
680 static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector)
684 if (WC_MODE_SORT_FREELIST(wc)) {
685 struct rb_node *next;
686 if (unlikely(!wc->current_free))
688 e = wc->current_free;
689 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
691 next = rb_next(&e->rb_node);
692 rb_erase(&e->rb_node, &wc->freetree);
694 next = rb_first(&wc->freetree);
695 wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
697 if (unlikely(list_empty(&wc->freelist)))
699 e = container_of(wc->freelist.next, struct wc_entry, lru);
700 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
706 writecache_verify_watermark(wc);
711 static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
713 writecache_unlink(wc, e);
714 writecache_add_to_freelist(wc, e);
715 clear_seq_count(wc, e);
716 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
717 if (unlikely(waitqueue_active(&wc->freelist_wait)))
718 wake_up(&wc->freelist_wait);
721 static void writecache_wait_on_freelist(struct dm_writecache *wc)
725 prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
728 finish_wait(&wc->freelist_wait, &wait);
732 static void writecache_poison_lists(struct dm_writecache *wc)
735 * Catch incorrect access to these values while the device is suspended.
737 memset(&wc->tree, -1, sizeof wc->tree);
738 wc->lru.next = LIST_POISON1;
739 wc->lru.prev = LIST_POISON2;
740 wc->freelist.next = LIST_POISON1;
741 wc->freelist.prev = LIST_POISON2;
744 static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
746 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
747 if (WC_MODE_PMEM(wc))
748 writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
751 static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
753 return read_seq_count(wc, e) < wc->seq_count;
756 static void writecache_flush(struct dm_writecache *wc)
758 struct wc_entry *e, *e2;
759 bool need_flush_after_free;
761 wc->uncommitted_blocks = 0;
762 del_timer(&wc->autocommit_timer);
764 if (list_empty(&wc->lru))
767 e = container_of(wc->lru.next, struct wc_entry, lru);
768 if (writecache_entry_is_committed(wc, e)) {
769 if (wc->overwrote_committed) {
770 writecache_wait_for_ios(wc, WRITE);
771 writecache_disk_flush(wc, wc->ssd_dev);
772 wc->overwrote_committed = false;
777 writecache_flush_entry(wc, e);
778 if (unlikely(e->lru.next == &wc->lru))
780 e2 = container_of(e->lru.next, struct wc_entry, lru);
781 if (writecache_entry_is_committed(wc, e2))
786 writecache_commit_flushed(wc, true);
789 pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
790 if (WC_MODE_PMEM(wc))
791 writecache_commit_flushed(wc, false);
793 ssd_commit_superblock(wc);
795 wc->overwrote_committed = false;
797 need_flush_after_free = false;
799 /* Free another committed entry with lower seq-count */
800 struct rb_node *rb_node = rb_prev(&e->rb_node);
803 e2 = container_of(rb_node, struct wc_entry, rb_node);
804 if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
805 likely(!e2->write_in_progress)) {
806 writecache_free_entry(wc, e2);
807 need_flush_after_free = true;
810 if (unlikely(e->lru.prev == &wc->lru))
812 e = container_of(e->lru.prev, struct wc_entry, lru);
816 if (need_flush_after_free)
817 writecache_commit_flushed(wc, false);
820 static void writecache_flush_work(struct work_struct *work)
822 struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
825 writecache_flush(wc);
829 static void writecache_autocommit_timer(struct timer_list *t)
831 struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
832 if (!writecache_has_error(wc))
833 queue_work(wc->writeback_wq, &wc->flush_work);
836 static void writecache_schedule_autocommit(struct dm_writecache *wc)
838 if (!timer_pending(&wc->autocommit_timer))
839 mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
842 static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
845 bool discarded_something = false;
847 e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
851 while (read_original_sector(wc, e) < end) {
852 struct rb_node *node = rb_next(&e->rb_node);
854 if (likely(!e->write_in_progress)) {
855 if (!discarded_something) {
856 writecache_wait_for_ios(wc, READ);
857 writecache_wait_for_ios(wc, WRITE);
858 discarded_something = true;
860 writecache_free_entry(wc, e);
866 e = container_of(node, struct wc_entry, rb_node);
869 if (discarded_something)
870 writecache_commit_flushed(wc, false);
873 static bool writecache_wait_for_writeback(struct dm_writecache *wc)
875 if (wc->writeback_size) {
876 writecache_wait_on_freelist(wc);
882 static void writecache_suspend(struct dm_target *ti)
884 struct dm_writecache *wc = ti->private;
885 bool flush_on_suspend;
887 del_timer_sync(&wc->autocommit_timer);
888 del_timer_sync(&wc->max_age_timer);
891 writecache_flush(wc);
892 flush_on_suspend = wc->flush_on_suspend;
893 if (flush_on_suspend) {
894 wc->flush_on_suspend = false;
896 queue_work(wc->writeback_wq, &wc->writeback_work);
900 drain_workqueue(wc->writeback_wq);
903 if (flush_on_suspend)
905 while (writecache_wait_for_writeback(wc));
907 if (WC_MODE_PMEM(wc))
908 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
910 writecache_poison_lists(wc);
915 static int writecache_alloc_entries(struct dm_writecache *wc)
921 wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
924 for (b = 0; b < wc->n_blocks; b++) {
925 struct wc_entry *e = &wc->entries[b];
927 e->write_in_progress = false;
933 static void writecache_resume(struct dm_target *ti)
935 struct dm_writecache *wc = ti->private;
937 bool need_flush = false;
943 if (WC_MODE_PMEM(wc))
944 persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
947 INIT_LIST_HEAD(&wc->lru);
948 if (WC_MODE_SORT_FREELIST(wc)) {
949 wc->freetree = RB_ROOT;
950 wc->current_free = NULL;
952 INIT_LIST_HEAD(&wc->freelist);
954 wc->freelist_size = 0;
956 r = memcpy_mcsafe(&sb_seq_count, &sb(wc)->seq_count, sizeof(uint64_t));
958 writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
959 sb_seq_count = cpu_to_le64(0);
961 wc->seq_count = le64_to_cpu(sb_seq_count);
963 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
964 for (b = 0; b < wc->n_blocks; b++) {
965 struct wc_entry *e = &wc->entries[b];
966 struct wc_memory_entry wme;
967 if (writecache_has_error(wc)) {
968 e->original_sector = -1;
972 r = memcpy_mcsafe(&wme, memory_entry(wc, e), sizeof(struct wc_memory_entry));
974 writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
975 (unsigned long)b, r);
976 e->original_sector = -1;
979 e->original_sector = le64_to_cpu(wme.original_sector);
980 e->seq_count = le64_to_cpu(wme.seq_count);
984 for (b = 0; b < wc->n_blocks; b++) {
985 struct wc_entry *e = &wc->entries[b];
986 if (!writecache_entry_is_committed(wc, e)) {
987 if (read_seq_count(wc, e) != -1) {
989 clear_seq_count(wc, e);
992 writecache_add_to_freelist(wc, e);
994 struct wc_entry *old;
996 old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
998 writecache_insert_entry(wc, e);
1000 if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
1001 writecache_error(wc, -EINVAL,
1002 "two identical entries, position %llu, sector %llu, sequence %llu",
1003 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
1004 (unsigned long long)read_seq_count(wc, e));
1006 if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
1009 writecache_free_entry(wc, old);
1010 writecache_insert_entry(wc, e);
1019 writecache_flush_all_metadata(wc);
1020 writecache_commit_flushed(wc, false);
1023 writecache_verify_watermark(wc);
1025 if (wc->max_age != MAX_AGE_UNSPECIFIED)
1026 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
1031 static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1037 if (dm_suspended(wc->ti)) {
1041 if (writecache_has_error(wc)) {
1046 writecache_flush(wc);
1047 wc->writeback_all++;
1048 queue_work(wc->writeback_wq, &wc->writeback_work);
1051 flush_workqueue(wc->writeback_wq);
1054 wc->writeback_all--;
1055 if (writecache_has_error(wc)) {
1064 static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1070 wc->flush_on_suspend = true;
1076 static void activate_cleaner(struct dm_writecache *wc)
1078 wc->flush_on_suspend = true;
1080 wc->freelist_high_watermark = wc->n_blocks;
1081 wc->freelist_low_watermark = wc->n_blocks;
1084 static int process_cleaner_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1090 activate_cleaner(wc);
1091 if (!dm_suspended(wc->ti))
1092 writecache_verify_watermark(wc);
1098 static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
1099 char *result, unsigned maxlen)
1102 struct dm_writecache *wc = ti->private;
1104 if (!strcasecmp(argv[0], "flush"))
1105 r = process_flush_mesg(argc, argv, wc);
1106 else if (!strcasecmp(argv[0], "flush_on_suspend"))
1107 r = process_flush_on_suspend_mesg(argc, argv, wc);
1108 else if (!strcasecmp(argv[0], "cleaner"))
1109 r = process_cleaner_mesg(argc, argv, wc);
1111 DMERR("unrecognised message received: %s", argv[0]);
1116 static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
1119 unsigned long flags;
1121 int rw = bio_data_dir(bio);
1122 unsigned remaining_size = wc->block_size;
1125 struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
1126 buf = bvec_kmap_irq(&bv, &flags);
1128 if (unlikely(size > remaining_size))
1129 size = remaining_size;
1133 r = memcpy_mcsafe(buf, data, size);
1134 flush_dcache_page(bio_page(bio));
1136 writecache_error(wc, r, "hardware memory error when reading data: %d", r);
1137 bio->bi_status = BLK_STS_IOERR;
1140 flush_dcache_page(bio_page(bio));
1141 memcpy_flushcache(data, buf, size);
1144 bvec_kunmap_irq(buf, &flags);
1146 data = (char *)data + size;
1147 remaining_size -= size;
1148 bio_advance(bio, size);
1149 } while (unlikely(remaining_size));
1152 static int writecache_flush_thread(void *data)
1154 struct dm_writecache *wc = data;
1160 bio = bio_list_pop(&wc->flush_list);
1162 set_current_state(TASK_INTERRUPTIBLE);
1165 if (unlikely(kthread_should_stop())) {
1166 set_current_state(TASK_RUNNING);
1174 if (bio_op(bio) == REQ_OP_DISCARD) {
1175 writecache_discard(wc, bio->bi_iter.bi_sector,
1176 bio_end_sector(bio));
1178 bio_set_dev(bio, wc->dev->bdev);
1179 generic_make_request(bio);
1181 writecache_flush(wc);
1183 if (writecache_has_error(wc))
1184 bio->bi_status = BLK_STS_IOERR;
1192 static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
1194 if (bio_list_empty(&wc->flush_list))
1195 wake_up_process(wc->flush_thread);
1196 bio_list_add(&wc->flush_list, bio);
1199 static int writecache_map(struct dm_target *ti, struct bio *bio)
1202 struct dm_writecache *wc = ti->private;
1204 bio->bi_private = NULL;
1208 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1209 if (writecache_has_error(wc))
1211 if (WC_MODE_PMEM(wc)) {
1212 writecache_flush(wc);
1213 if (writecache_has_error(wc))
1217 writecache_offload_bio(wc, bio);
1222 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1224 if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
1225 (wc->block_size / 512 - 1)) != 0)) {
1226 DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
1227 (unsigned long long)bio->bi_iter.bi_sector,
1228 bio->bi_iter.bi_size, wc->block_size);
1232 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
1233 if (writecache_has_error(wc))
1235 if (WC_MODE_PMEM(wc)) {
1236 writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
1237 goto unlock_remap_origin;
1239 writecache_offload_bio(wc, bio);
1244 if (bio_data_dir(bio) == READ) {
1246 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1247 if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1248 if (WC_MODE_PMEM(wc)) {
1249 bio_copy_block(wc, bio, memory_data(wc, e));
1250 if (bio->bi_iter.bi_size)
1251 goto read_next_block;
1254 dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1255 bio_set_dev(bio, wc->ssd_dev->bdev);
1256 bio->bi_iter.bi_sector = cache_sector(wc, e);
1257 if (!writecache_entry_is_committed(wc, e))
1258 writecache_wait_for_ios(wc, WRITE);
1263 sector_t next_boundary =
1264 read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1265 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
1266 dm_accept_partial_bio(bio, next_boundary);
1269 goto unlock_remap_origin;
1273 bool found_entry = false;
1274 if (writecache_has_error(wc))
1276 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1278 if (!writecache_entry_is_committed(wc, e))
1280 if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1281 wc->overwrote_committed = true;
1286 if (unlikely(wc->cleaner))
1289 e = writecache_pop_from_freelist(wc, (sector_t)-1);
1293 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1295 sector_t next_boundary = read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1296 BUG_ON(!next_boundary);
1297 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
1298 dm_accept_partial_bio(bio, next_boundary);
1301 goto unlock_remap_origin;
1303 writecache_wait_on_freelist(wc);
1306 write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1307 writecache_insert_entry(wc, e);
1308 wc->uncommitted_blocks++;
1310 if (WC_MODE_PMEM(wc)) {
1311 bio_copy_block(wc, bio, memory_data(wc, e));
1313 unsigned bio_size = wc->block_size;
1314 sector_t start_cache_sec = cache_sector(wc, e);
1315 sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT);
1317 while (bio_size < bio->bi_iter.bi_size) {
1318 struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec);
1321 write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector +
1322 (bio_size >> SECTOR_SHIFT), wc->seq_count);
1323 writecache_insert_entry(wc, f);
1324 wc->uncommitted_blocks++;
1325 bio_size += wc->block_size;
1326 current_cache_sec += wc->block_size >> SECTOR_SHIFT;
1329 bio_set_dev(bio, wc->ssd_dev->bdev);
1330 bio->bi_iter.bi_sector = start_cache_sec;
1331 dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT);
1333 if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1334 wc->uncommitted_blocks = 0;
1335 queue_work(wc->writeback_wq, &wc->flush_work);
1337 writecache_schedule_autocommit(wc);
1341 } while (bio->bi_iter.bi_size);
1343 if (unlikely(bio->bi_opf & REQ_FUA ||
1344 wc->uncommitted_blocks >= wc->autocommit_blocks))
1345 writecache_flush(wc);
1347 writecache_schedule_autocommit(wc);
1351 unlock_remap_origin:
1352 bio_set_dev(bio, wc->dev->bdev);
1354 return DM_MAPIO_REMAPPED;
1357 /* make sure that writecache_end_io decrements bio_in_progress: */
1358 bio->bi_private = (void *)1;
1359 atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
1361 return DM_MAPIO_REMAPPED;
1366 return DM_MAPIO_SUBMITTED;
1370 return DM_MAPIO_SUBMITTED;
1375 return DM_MAPIO_SUBMITTED;
1378 static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
1380 struct dm_writecache *wc = ti->private;
1382 if (bio->bi_private != NULL) {
1383 int dir = bio_data_dir(bio);
1384 if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
1385 if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
1386 wake_up(&wc->bio_in_progress_wait[dir]);
1391 static int writecache_iterate_devices(struct dm_target *ti,
1392 iterate_devices_callout_fn fn, void *data)
1394 struct dm_writecache *wc = ti->private;
1396 return fn(ti, wc->dev, 0, ti->len, data);
1399 static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
1401 struct dm_writecache *wc = ti->private;
1403 if (limits->logical_block_size < wc->block_size)
1404 limits->logical_block_size = wc->block_size;
1406 if (limits->physical_block_size < wc->block_size)
1407 limits->physical_block_size = wc->block_size;
1409 if (limits->io_min < wc->block_size)
1410 limits->io_min = wc->block_size;
1414 static void writecache_writeback_endio(struct bio *bio)
1416 struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
1417 struct dm_writecache *wc = wb->wc;
1418 unsigned long flags;
1420 raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
1421 if (unlikely(list_empty(&wc->endio_list)))
1422 wake_up_process(wc->endio_thread);
1423 list_add_tail(&wb->endio_entry, &wc->endio_list);
1424 raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
1427 static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
1429 struct copy_struct *c = ptr;
1430 struct dm_writecache *wc = c->wc;
1432 c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
1434 raw_spin_lock_irq(&wc->endio_list_lock);
1435 if (unlikely(list_empty(&wc->endio_list)))
1436 wake_up_process(wc->endio_thread);
1437 list_add_tail(&c->endio_entry, &wc->endio_list);
1438 raw_spin_unlock_irq(&wc->endio_list_lock);
1441 static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
1444 struct writeback_struct *wb;
1446 unsigned long n_walked = 0;
1449 wb = list_entry(list->next, struct writeback_struct, endio_entry);
1450 list_del(&wb->endio_entry);
1452 if (unlikely(wb->bio.bi_status != BLK_STS_OK))
1453 writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
1454 "write error %d", wb->bio.bi_status);
1458 BUG_ON(!e->write_in_progress);
1459 e->write_in_progress = false;
1460 INIT_LIST_HEAD(&e->lru);
1461 if (!writecache_has_error(wc))
1462 writecache_free_entry(wc, e);
1463 BUG_ON(!wc->writeback_size);
1464 wc->writeback_size--;
1466 if (unlikely(n_walked >= ENDIO_LATENCY)) {
1467 writecache_commit_flushed(wc, false);
1472 } while (++i < wb->wc_list_n);
1474 if (wb->wc_list != wb->wc_list_inline)
1477 } while (!list_empty(list));
1480 static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
1482 struct copy_struct *c;
1486 c = list_entry(list->next, struct copy_struct, endio_entry);
1487 list_del(&c->endio_entry);
1489 if (unlikely(c->error))
1490 writecache_error(wc, c->error, "copy error");
1494 BUG_ON(!e->write_in_progress);
1495 e->write_in_progress = false;
1496 INIT_LIST_HEAD(&e->lru);
1497 if (!writecache_has_error(wc))
1498 writecache_free_entry(wc, e);
1500 BUG_ON(!wc->writeback_size);
1501 wc->writeback_size--;
1503 } while (--c->n_entries);
1504 mempool_free(c, &wc->copy_pool);
1505 } while (!list_empty(list));
1508 static int writecache_endio_thread(void *data)
1510 struct dm_writecache *wc = data;
1513 struct list_head list;
1515 raw_spin_lock_irq(&wc->endio_list_lock);
1516 if (!list_empty(&wc->endio_list))
1518 set_current_state(TASK_INTERRUPTIBLE);
1519 raw_spin_unlock_irq(&wc->endio_list_lock);
1521 if (unlikely(kthread_should_stop())) {
1522 set_current_state(TASK_RUNNING);
1531 list = wc->endio_list;
1532 list.next->prev = list.prev->next = &list;
1533 INIT_LIST_HEAD(&wc->endio_list);
1534 raw_spin_unlock_irq(&wc->endio_list_lock);
1536 if (!WC_MODE_FUA(wc))
1537 writecache_disk_flush(wc, wc->dev);
1541 if (WC_MODE_PMEM(wc)) {
1542 __writecache_endio_pmem(wc, &list);
1544 __writecache_endio_ssd(wc, &list);
1545 writecache_wait_for_ios(wc, READ);
1548 writecache_commit_flushed(wc, false);
1556 static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e, gfp_t gfp)
1558 struct dm_writecache *wc = wb->wc;
1559 unsigned block_size = wc->block_size;
1560 void *address = memory_data(wc, e);
1562 persistent_memory_flush_cache(address, block_size);
1563 return bio_add_page(&wb->bio, persistent_memory_page(address),
1564 block_size, persistent_memory_page_offset(address)) != 0;
1567 struct writeback_list {
1568 struct list_head list;
1572 static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
1574 if (unlikely(wc->max_writeback_jobs)) {
1575 if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
1577 while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
1578 writecache_wait_on_freelist(wc);
1585 static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
1587 struct wc_entry *e, *f;
1589 struct writeback_struct *wb;
1594 e = container_of(wbl->list.prev, struct wc_entry, lru);
1597 max_pages = e->wc_list_contiguous;
1599 bio = bio_alloc_bioset(GFP_NOIO, max_pages, &wc->bio_set);
1600 wb = container_of(bio, struct writeback_struct, bio);
1602 bio->bi_end_io = writecache_writeback_endio;
1603 bio_set_dev(bio, wc->dev->bdev);
1604 bio->bi_iter.bi_sector = read_original_sector(wc, e);
1605 if (max_pages <= WB_LIST_INLINE ||
1606 unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
1607 GFP_NOIO | __GFP_NORETRY |
1608 __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
1609 wb->wc_list = wb->wc_list_inline;
1610 max_pages = WB_LIST_INLINE;
1613 BUG_ON(!wc_add_block(wb, e, GFP_NOIO));
1618 while (wbl->size && wb->wc_list_n < max_pages) {
1619 f = container_of(wbl->list.prev, struct wc_entry, lru);
1620 if (read_original_sector(wc, f) !=
1621 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1623 if (!wc_add_block(wb, f, GFP_NOWAIT | __GFP_NOWARN))
1627 wb->wc_list[wb->wc_list_n++] = f;
1630 bio_set_op_attrs(bio, REQ_OP_WRITE, WC_MODE_FUA(wc) * REQ_FUA);
1631 if (writecache_has_error(wc)) {
1632 bio->bi_status = BLK_STS_IOERR;
1638 __writeback_throttle(wc, wbl);
1642 static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
1644 struct wc_entry *e, *f;
1645 struct dm_io_region from, to;
1646 struct copy_struct *c;
1652 e = container_of(wbl->list.prev, struct wc_entry, lru);
1655 n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
1657 from.bdev = wc->ssd_dev->bdev;
1658 from.sector = cache_sector(wc, e);
1659 from.count = n_sectors;
1660 to.bdev = wc->dev->bdev;
1661 to.sector = read_original_sector(wc, e);
1662 to.count = n_sectors;
1664 c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
1667 c->n_entries = e->wc_list_contiguous;
1669 while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
1671 f = container_of(wbl->list.prev, struct wc_entry, lru);
1677 dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
1679 __writeback_throttle(wc, wbl);
1683 static void writecache_writeback(struct work_struct *work)
1685 struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
1686 struct blk_plug plug;
1687 struct wc_entry *f, *uninitialized_var(g), *e = NULL;
1688 struct rb_node *node, *next_node;
1689 struct list_head skipped;
1690 struct writeback_list wbl;
1691 unsigned long n_walked;
1695 if (writecache_has_error(wc)) {
1700 if (unlikely(wc->writeback_all)) {
1701 if (writecache_wait_for_writeback(wc))
1705 if (wc->overwrote_committed) {
1706 writecache_wait_for_ios(wc, WRITE);
1710 INIT_LIST_HEAD(&skipped);
1711 INIT_LIST_HEAD(&wbl.list);
1713 while (!list_empty(&wc->lru) &&
1714 (wc->writeback_all ||
1715 wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark ||
1716 (jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >=
1717 wc->max_age - wc->max_age / MAX_AGE_DIV))) {
1720 if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1721 likely(!wc->writeback_all) && likely(!dm_suspended(wc->ti))) {
1722 queue_work(wc->writeback_wq, &wc->writeback_work);
1726 if (unlikely(wc->writeback_all)) {
1728 writecache_flush(wc);
1729 e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
1733 e = container_of(wc->lru.prev, struct wc_entry, lru);
1734 BUG_ON(e->write_in_progress);
1735 if (unlikely(!writecache_entry_is_committed(wc, e))) {
1736 writecache_flush(wc);
1738 node = rb_prev(&e->rb_node);
1740 f = container_of(node, struct wc_entry, rb_node);
1741 if (unlikely(read_original_sector(wc, f) ==
1742 read_original_sector(wc, e))) {
1743 BUG_ON(!f->write_in_progress);
1745 list_add(&e->lru, &skipped);
1750 wc->writeback_size++;
1752 list_add(&e->lru, &wbl.list);
1754 e->write_in_progress = true;
1755 e->wc_list_contiguous = 1;
1760 next_node = rb_next(&f->rb_node);
1761 if (unlikely(!next_node))
1763 g = container_of(next_node, struct wc_entry, rb_node);
1764 if (unlikely(read_original_sector(wc, g) ==
1765 read_original_sector(wc, f))) {
1769 if (read_original_sector(wc, g) !=
1770 read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
1772 if (unlikely(g->write_in_progress))
1774 if (unlikely(!writecache_entry_is_committed(wc, g)))
1777 if (!WC_MODE_PMEM(wc)) {
1783 //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
1786 wc->writeback_size++;
1788 list_add(&g->lru, &wbl.list);
1790 g->write_in_progress = true;
1791 g->wc_list_contiguous = BIO_MAX_PAGES;
1793 e->wc_list_contiguous++;
1794 if (unlikely(e->wc_list_contiguous == BIO_MAX_PAGES)) {
1795 if (unlikely(wc->writeback_all)) {
1796 next_node = rb_next(&f->rb_node);
1797 if (likely(next_node))
1798 g = container_of(next_node, struct wc_entry, rb_node);
1806 if (!list_empty(&skipped)) {
1807 list_splice_tail(&skipped, &wc->lru);
1809 * If we didn't do any progress, we must wait until some
1810 * writeback finishes to avoid burning CPU in a loop
1812 if (unlikely(!wbl.size))
1813 writecache_wait_for_writeback(wc);
1818 blk_start_plug(&plug);
1820 if (WC_MODE_PMEM(wc))
1821 __writecache_writeback_pmem(wc, &wbl);
1823 __writecache_writeback_ssd(wc, &wbl);
1825 blk_finish_plug(&plug);
1827 if (unlikely(wc->writeback_all)) {
1829 while (writecache_wait_for_writeback(wc));
1834 static int calculate_memory_size(uint64_t device_size, unsigned block_size,
1835 size_t *n_blocks_p, size_t *n_metadata_blocks_p)
1837 uint64_t n_blocks, offset;
1840 n_blocks = device_size;
1841 do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
1846 /* Verify the following entries[n_blocks] won't overflow */
1847 if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
1848 sizeof(struct wc_memory_entry)))
1850 offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
1851 offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
1852 if (offset + n_blocks * block_size <= device_size)
1857 /* check if the bit field overflows */
1859 if (e.index != n_blocks)
1863 *n_blocks_p = n_blocks;
1864 if (n_metadata_blocks_p)
1865 *n_metadata_blocks_p = offset >> __ffs(block_size);
1869 static int init_memory(struct dm_writecache *wc)
1874 r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
1878 r = writecache_alloc_entries(wc);
1882 for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
1883 pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
1884 pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
1885 pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
1886 pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
1887 pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
1889 for (b = 0; b < wc->n_blocks; b++)
1890 write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
1892 writecache_flush_all_metadata(wc);
1893 writecache_commit_flushed(wc, false);
1894 pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
1895 writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
1896 writecache_commit_flushed(wc, false);
1901 static void writecache_dtr(struct dm_target *ti)
1903 struct dm_writecache *wc = ti->private;
1908 if (wc->endio_thread)
1909 kthread_stop(wc->endio_thread);
1911 if (wc->flush_thread)
1912 kthread_stop(wc->flush_thread);
1914 bioset_exit(&wc->bio_set);
1916 mempool_exit(&wc->copy_pool);
1918 if (wc->writeback_wq)
1919 destroy_workqueue(wc->writeback_wq);
1922 dm_put_device(ti, wc->dev);
1925 dm_put_device(ti, wc->ssd_dev);
1930 if (wc->memory_map) {
1931 if (WC_MODE_PMEM(wc))
1932 persistent_memory_release(wc);
1934 vfree(wc->memory_map);
1938 dm_kcopyd_client_destroy(wc->dm_kcopyd);
1941 dm_io_client_destroy(wc->dm_io);
1943 if (wc->dirty_bitmap)
1944 vfree(wc->dirty_bitmap);
1949 static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
1951 struct dm_writecache *wc;
1952 struct dm_arg_set as;
1954 unsigned opt_params;
1955 size_t offset, data_size;
1958 int high_wm_percent = HIGH_WATERMARK;
1959 int low_wm_percent = LOW_WATERMARK;
1961 struct wc_memory_superblock s;
1963 static struct dm_arg _args[] = {
1964 {0, 10, "Invalid number of feature args"},
1970 wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
1972 ti->error = "Cannot allocate writecache structure";
1979 mutex_init(&wc->lock);
1980 wc->max_age = MAX_AGE_UNSPECIFIED;
1981 writecache_poison_lists(wc);
1982 init_waitqueue_head(&wc->freelist_wait);
1983 timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
1984 timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0);
1986 for (i = 0; i < 2; i++) {
1987 atomic_set(&wc->bio_in_progress[i], 0);
1988 init_waitqueue_head(&wc->bio_in_progress_wait[i]);
1991 wc->dm_io = dm_io_client_create();
1992 if (IS_ERR(wc->dm_io)) {
1993 r = PTR_ERR(wc->dm_io);
1994 ti->error = "Unable to allocate dm-io client";
1999 wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
2000 if (!wc->writeback_wq) {
2002 ti->error = "Could not allocate writeback workqueue";
2005 INIT_WORK(&wc->writeback_work, writecache_writeback);
2006 INIT_WORK(&wc->flush_work, writecache_flush_work);
2008 raw_spin_lock_init(&wc->endio_list_lock);
2009 INIT_LIST_HEAD(&wc->endio_list);
2010 wc->endio_thread = kthread_create(writecache_endio_thread, wc, "writecache_endio");
2011 if (IS_ERR(wc->endio_thread)) {
2012 r = PTR_ERR(wc->endio_thread);
2013 wc->endio_thread = NULL;
2014 ti->error = "Couldn't spawn endio thread";
2017 wake_up_process(wc->endio_thread);
2020 * Parse the mode (pmem or ssd)
2022 string = dm_shift_arg(&as);
2026 if (!strcasecmp(string, "s")) {
2027 wc->pmem_mode = false;
2028 } else if (!strcasecmp(string, "p")) {
2029 #ifdef DM_WRITECACHE_HAS_PMEM
2030 wc->pmem_mode = true;
2031 wc->writeback_fua = true;
2034 * If the architecture doesn't support persistent memory or
2035 * the kernel doesn't support any DAX drivers, this driver can
2036 * only be used in SSD-only mode.
2039 ti->error = "Persistent memory or DAX not supported on this system";
2046 if (WC_MODE_PMEM(wc)) {
2047 r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
2048 offsetof(struct writeback_struct, bio),
2051 ti->error = "Could not allocate bio set";
2055 r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
2057 ti->error = "Could not allocate mempool";
2063 * Parse the origin data device
2065 string = dm_shift_arg(&as);
2068 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
2070 ti->error = "Origin data device lookup failed";
2075 * Parse cache data device (be it pmem or ssd)
2077 string = dm_shift_arg(&as);
2081 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
2083 ti->error = "Cache data device lookup failed";
2086 wc->memory_map_size = i_size_read(wc->ssd_dev->bdev->bd_inode);
2089 * Parse the cache block size
2091 string = dm_shift_arg(&as);
2094 if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
2095 wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
2096 (wc->block_size & (wc->block_size - 1))) {
2098 ti->error = "Invalid block size";
2101 wc->block_size_bits = __ffs(wc->block_size);
2103 wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
2104 wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
2105 wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
2108 * Parse optional arguments
2110 r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2114 while (opt_params) {
2115 string = dm_shift_arg(&as), opt_params--;
2116 if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2117 unsigned long long start_sector;
2118 string = dm_shift_arg(&as), opt_params--;
2119 if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2120 goto invalid_optional;
2121 wc->start_sector = start_sector;
2122 if (wc->start_sector != start_sector ||
2123 wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2124 goto invalid_optional;
2125 } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
2126 string = dm_shift_arg(&as), opt_params--;
2127 if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
2128 goto invalid_optional;
2129 if (high_wm_percent < 0 || high_wm_percent > 100)
2130 goto invalid_optional;
2131 wc->high_wm_percent_set = true;
2132 } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
2133 string = dm_shift_arg(&as), opt_params--;
2134 if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
2135 goto invalid_optional;
2136 if (low_wm_percent < 0 || low_wm_percent > 100)
2137 goto invalid_optional;
2138 wc->low_wm_percent_set = true;
2139 } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
2140 string = dm_shift_arg(&as), opt_params--;
2141 if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
2142 goto invalid_optional;
2143 wc->max_writeback_jobs_set = true;
2144 } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
2145 string = dm_shift_arg(&as), opt_params--;
2146 if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
2147 goto invalid_optional;
2148 wc->autocommit_blocks_set = true;
2149 } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
2150 unsigned autocommit_msecs;
2151 string = dm_shift_arg(&as), opt_params--;
2152 if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
2153 goto invalid_optional;
2154 if (autocommit_msecs > 3600000)
2155 goto invalid_optional;
2156 wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2157 wc->autocommit_time_set = true;
2158 } else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
2159 unsigned max_age_msecs;
2160 string = dm_shift_arg(&as), opt_params--;
2161 if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
2162 goto invalid_optional;
2163 if (max_age_msecs > 86400000)
2164 goto invalid_optional;
2165 wc->max_age = msecs_to_jiffies(max_age_msecs);
2166 } else if (!strcasecmp(string, "cleaner")) {
2168 } else if (!strcasecmp(string, "fua")) {
2169 if (WC_MODE_PMEM(wc)) {
2170 wc->writeback_fua = true;
2171 wc->writeback_fua_set = true;
2172 } else goto invalid_optional;
2173 } else if (!strcasecmp(string, "nofua")) {
2174 if (WC_MODE_PMEM(wc)) {
2175 wc->writeback_fua = false;
2176 wc->writeback_fua_set = true;
2177 } else goto invalid_optional;
2181 ti->error = "Invalid optional argument";
2186 if (high_wm_percent < low_wm_percent) {
2188 ti->error = "High watermark must be greater than or equal to low watermark";
2192 if (WC_MODE_PMEM(wc)) {
2193 r = persistent_memory_claim(wc);
2195 ti->error = "Unable to map persistent memory for cache";
2199 struct dm_io_region region;
2200 struct dm_io_request req;
2201 size_t n_blocks, n_metadata_blocks;
2202 uint64_t n_bitmap_bits;
2204 wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2206 bio_list_init(&wc->flush_list);
2207 wc->flush_thread = kthread_create(writecache_flush_thread, wc, "dm_writecache_flush");
2208 if (IS_ERR(wc->flush_thread)) {
2209 r = PTR_ERR(wc->flush_thread);
2210 wc->flush_thread = NULL;
2211 ti->error = "Couldn't spawn flush thread";
2214 wake_up_process(wc->flush_thread);
2216 r = calculate_memory_size(wc->memory_map_size, wc->block_size,
2217 &n_blocks, &n_metadata_blocks);
2219 ti->error = "Invalid device size";
2223 n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
2224 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
2225 /* this is limitation of test_bit functions */
2226 if (n_bitmap_bits > 1U << 31) {
2228 ti->error = "Invalid device size";
2232 wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
2233 if (!wc->memory_map) {
2235 ti->error = "Unable to allocate memory for metadata";
2239 wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2240 if (IS_ERR(wc->dm_kcopyd)) {
2241 r = PTR_ERR(wc->dm_kcopyd);
2242 ti->error = "Unable to allocate dm-kcopyd client";
2243 wc->dm_kcopyd = NULL;
2247 wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
2248 wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
2249 BITS_PER_LONG * sizeof(unsigned long);
2250 wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
2251 if (!wc->dirty_bitmap) {
2253 ti->error = "Unable to allocate dirty bitmap";
2257 region.bdev = wc->ssd_dev->bdev;
2258 region.sector = wc->start_sector;
2259 region.count = wc->metadata_sectors;
2260 req.bi_op = REQ_OP_READ;
2261 req.bi_op_flags = REQ_SYNC;
2262 req.mem.type = DM_IO_VMA;
2263 req.mem.ptr.vma = (char *)wc->memory_map;
2264 req.client = wc->dm_io;
2265 req.notify.fn = NULL;
2267 r = dm_io(&req, 1, ®ion, NULL);
2269 ti->error = "Unable to read metadata";
2274 r = memcpy_mcsafe(&s, sb(wc), sizeof(struct wc_memory_superblock));
2276 ti->error = "Hardware memory error when reading superblock";
2279 if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
2280 r = init_memory(wc);
2282 ti->error = "Unable to initialize device";
2285 r = memcpy_mcsafe(&s, sb(wc), sizeof(struct wc_memory_superblock));
2287 ti->error = "Hardware memory error when reading superblock";
2292 if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
2293 ti->error = "Invalid magic in the superblock";
2298 if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
2299 ti->error = "Invalid version in the superblock";
2304 if (le32_to_cpu(s.block_size) != wc->block_size) {
2305 ti->error = "Block size does not match superblock";
2310 wc->n_blocks = le64_to_cpu(s.n_blocks);
2312 offset = wc->n_blocks * sizeof(struct wc_memory_entry);
2313 if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
2315 ti->error = "Overflow in size calculation";
2319 offset += sizeof(struct wc_memory_superblock);
2320 if (offset < sizeof(struct wc_memory_superblock))
2322 offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
2323 data_size = wc->n_blocks * (size_t)wc->block_size;
2324 if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
2325 (offset + data_size < offset))
2327 if (offset + data_size > wc->memory_map_size) {
2328 ti->error = "Memory area is too small";
2333 wc->metadata_sectors = offset >> SECTOR_SHIFT;
2334 wc->block_start = (char *)sb(wc) + offset;
2336 x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
2339 wc->freelist_high_watermark = x;
2340 x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
2343 wc->freelist_low_watermark = x;
2346 activate_cleaner(wc);
2348 r = writecache_alloc_entries(wc);
2350 ti->error = "Cannot allocate memory";
2354 ti->num_flush_bios = 1;
2355 ti->flush_supported = true;
2356 ti->num_discard_bios = 1;
2358 if (WC_MODE_PMEM(wc))
2359 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
2365 ti->error = "Bad arguments";
2371 static void writecache_status(struct dm_target *ti, status_type_t type,
2372 unsigned status_flags, char *result, unsigned maxlen)
2374 struct dm_writecache *wc = ti->private;
2375 unsigned extra_args;
2380 case STATUSTYPE_INFO:
2381 DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),
2382 (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2383 (unsigned long long)wc->writeback_size);
2385 case STATUSTYPE_TABLE:
2386 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
2387 wc->dev->name, wc->ssd_dev->name, wc->block_size);
2389 if (wc->start_sector)
2391 if (wc->high_wm_percent_set && !wc->cleaner)
2393 if (wc->low_wm_percent_set && !wc->cleaner)
2395 if (wc->max_writeback_jobs_set)
2397 if (wc->autocommit_blocks_set)
2399 if (wc->autocommit_time_set)
2403 if (wc->writeback_fua_set)
2406 DMEMIT("%u", extra_args);
2407 if (wc->start_sector)
2408 DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2409 if (wc->high_wm_percent_set && !wc->cleaner) {
2410 x = (uint64_t)wc->freelist_high_watermark * 100;
2411 x += wc->n_blocks / 2;
2412 do_div(x, (size_t)wc->n_blocks);
2413 DMEMIT(" high_watermark %u", 100 - (unsigned)x);
2415 if (wc->low_wm_percent_set && !wc->cleaner) {
2416 x = (uint64_t)wc->freelist_low_watermark * 100;
2417 x += wc->n_blocks / 2;
2418 do_div(x, (size_t)wc->n_blocks);
2419 DMEMIT(" low_watermark %u", 100 - (unsigned)x);
2421 if (wc->max_writeback_jobs_set)
2422 DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
2423 if (wc->autocommit_blocks_set)
2424 DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
2425 if (wc->autocommit_time_set)
2426 DMEMIT(" autocommit_time %u", jiffies_to_msecs(wc->autocommit_jiffies));
2427 if (wc->max_age != MAX_AGE_UNSPECIFIED)
2428 DMEMIT(" max_age %u", jiffies_to_msecs(wc->max_age));
2431 if (wc->writeback_fua_set)
2432 DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2437 static struct target_type writecache_target = {
2438 .name = "writecache",
2439 .version = {1, 3, 0},
2440 .module = THIS_MODULE,
2441 .ctr = writecache_ctr,
2442 .dtr = writecache_dtr,
2443 .status = writecache_status,
2444 .postsuspend = writecache_suspend,
2445 .resume = writecache_resume,
2446 .message = writecache_message,
2447 .map = writecache_map,
2448 .end_io = writecache_end_io,
2449 .iterate_devices = writecache_iterate_devices,
2450 .io_hints = writecache_io_hints,
2453 static int __init dm_writecache_init(void)
2457 r = dm_register_target(&writecache_target);
2459 DMERR("register failed %d", r);
2466 static void __exit dm_writecache_exit(void)
2468 dm_unregister_target(&writecache_target);
2471 module_init(dm_writecache_init);
2472 module_exit(dm_writecache_exit);
2474 MODULE_DESCRIPTION(DM_NAME " writecache target");
2475 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2476 MODULE_LICENSE("GPL");