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
30 #define BITMAP_GRANULARITY 65536
31 #if BITMAP_GRANULARITY < PAGE_SIZE
32 #undef BITMAP_GRANULARITY
33 #define BITMAP_GRANULARITY PAGE_SIZE
36 #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_DAX_DRIVER)
37 #define DM_WRITECACHE_HAS_PMEM
40 #ifdef DM_WRITECACHE_HAS_PMEM
41 #define pmem_assign(dest, src) \
43 typeof(dest) uniq = (src); \
44 memcpy_flushcache(&(dest), &uniq, sizeof(dest)); \
47 #define pmem_assign(dest, src) ((dest) = (src))
50 #if defined(__HAVE_ARCH_MEMCPY_MCSAFE) && defined(DM_WRITECACHE_HAS_PMEM)
51 #define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
54 #define MEMORY_SUPERBLOCK_MAGIC 0x23489321
55 #define MEMORY_SUPERBLOCK_VERSION 1
57 struct wc_memory_entry {
58 __le64 original_sector;
62 struct wc_memory_superblock {
74 struct wc_memory_entry entries[0];
78 struct rb_node rb_node;
80 unsigned short wc_list_contiguous;
81 bool write_in_progress
82 #if BITS_PER_LONG == 64
87 #if BITS_PER_LONG == 64
91 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
92 uint64_t original_sector;
97 #ifdef DM_WRITECACHE_HAS_PMEM
98 #define WC_MODE_PMEM(wc) ((wc)->pmem_mode)
99 #define WC_MODE_FUA(wc) ((wc)->writeback_fua)
101 #define WC_MODE_PMEM(wc) false
102 #define WC_MODE_FUA(wc) false
104 #define WC_MODE_SORT_FREELIST(wc) (!WC_MODE_PMEM(wc))
106 struct dm_writecache {
108 struct list_head lru;
110 struct list_head freelist;
112 struct rb_root freetree;
113 struct wc_entry *current_free;
118 size_t freelist_size;
119 size_t writeback_size;
120 size_t freelist_high_watermark;
121 size_t freelist_low_watermark;
123 unsigned uncommitted_blocks;
124 unsigned autocommit_blocks;
125 unsigned max_writeback_jobs;
129 unsigned long autocommit_jiffies;
130 struct timer_list autocommit_timer;
131 struct wait_queue_head freelist_wait;
133 atomic_t bio_in_progress[2];
134 struct wait_queue_head bio_in_progress_wait[2];
136 struct dm_target *ti;
138 struct dm_dev *ssd_dev;
139 sector_t start_sector;
141 uint64_t memory_map_size;
142 size_t metadata_sectors;
146 struct wc_entry *entries;
148 unsigned char block_size_bits;
151 bool writeback_fua:1;
153 bool overwrote_committed:1;
154 bool memory_vmapped:1;
156 bool high_wm_percent_set:1;
157 bool low_wm_percent_set:1;
158 bool max_writeback_jobs_set:1;
159 bool autocommit_blocks_set:1;
160 bool autocommit_time_set:1;
161 bool writeback_fua_set:1;
162 bool flush_on_suspend:1;
164 unsigned writeback_all;
165 struct workqueue_struct *writeback_wq;
166 struct work_struct writeback_work;
167 struct work_struct flush_work;
169 struct dm_io_client *dm_io;
171 raw_spinlock_t endio_list_lock;
172 struct list_head endio_list;
173 struct task_struct *endio_thread;
175 struct task_struct *flush_thread;
176 struct bio_list flush_list;
178 struct dm_kcopyd_client *dm_kcopyd;
179 unsigned long *dirty_bitmap;
180 unsigned dirty_bitmap_size;
182 struct bio_set bio_set;
186 #define WB_LIST_INLINE 16
188 struct writeback_struct {
189 struct list_head endio_entry;
190 struct dm_writecache *wc;
191 struct wc_entry **wc_list;
193 unsigned page_offset;
195 struct wc_entry *wc_list_inline[WB_LIST_INLINE];
200 struct list_head endio_entry;
201 struct dm_writecache *wc;
207 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
208 "A percentage of time allocated for data copying");
210 static void wc_lock(struct dm_writecache *wc)
212 mutex_lock(&wc->lock);
215 static void wc_unlock(struct dm_writecache *wc)
217 mutex_unlock(&wc->lock);
220 #ifdef DM_WRITECACHE_HAS_PMEM
221 static int persistent_memory_claim(struct dm_writecache *wc)
230 wc->memory_vmapped = false;
232 if (!wc->ssd_dev->dax_dev) {
236 s = wc->memory_map_size;
242 if (p != s >> PAGE_SHIFT) {
247 id = dax_read_lock();
249 da = dax_direct_access(wc->ssd_dev->dax_dev, 0, p, &wc->memory_map, &pfn);
251 wc->memory_map = NULL;
255 if (!pfn_t_has_page(pfn)) {
256 wc->memory_map = NULL;
262 wc->memory_map = NULL;
263 pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
271 daa = dax_direct_access(wc->ssd_dev->dax_dev, i, p - i,
274 r = daa ? daa : -EINVAL;
277 if (!pfn_t_has_page(pfn)) {
281 while (daa-- && i < p) {
282 pages[i++] = pfn_t_to_page(pfn);
286 wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
287 if (!wc->memory_map) {
292 wc->memory_vmapped = true;
297 wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
298 wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
309 static int persistent_memory_claim(struct dm_writecache *wc)
315 static void persistent_memory_release(struct dm_writecache *wc)
317 if (wc->memory_vmapped)
318 vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
321 static struct page *persistent_memory_page(void *addr)
323 if (is_vmalloc_addr(addr))
324 return vmalloc_to_page(addr);
326 return virt_to_page(addr);
329 static unsigned persistent_memory_page_offset(void *addr)
331 return (unsigned long)addr & (PAGE_SIZE - 1);
334 static void persistent_memory_flush_cache(void *ptr, size_t size)
336 if (is_vmalloc_addr(ptr))
337 flush_kernel_vmap_range(ptr, size);
340 static void persistent_memory_invalidate_cache(void *ptr, size_t size)
342 if (is_vmalloc_addr(ptr))
343 invalidate_kernel_vmap_range(ptr, size);
346 static struct wc_memory_superblock *sb(struct dm_writecache *wc)
348 return wc->memory_map;
351 static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
353 if (is_power_of_2(sizeof(struct wc_entry)) && 0)
354 return &sb(wc)->entries[e - wc->entries];
356 return &sb(wc)->entries[e->index];
359 static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
361 return (char *)wc->block_start + (e->index << wc->block_size_bits);
364 static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
366 return wc->start_sector + wc->metadata_sectors +
367 ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
370 static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
372 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
373 return e->original_sector;
375 return le64_to_cpu(memory_entry(wc, e)->original_sector);
379 static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
381 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
384 return le64_to_cpu(memory_entry(wc, e)->seq_count);
388 static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
390 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
393 pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
396 static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
397 uint64_t original_sector, uint64_t seq_count)
399 struct wc_memory_entry me;
400 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
401 e->original_sector = original_sector;
402 e->seq_count = seq_count;
404 me.original_sector = cpu_to_le64(original_sector);
405 me.seq_count = cpu_to_le64(seq_count);
406 pmem_assign(*memory_entry(wc, e), me);
409 #define writecache_error(wc, err, msg, arg...) \
411 if (!cmpxchg(&(wc)->error, 0, err)) \
413 wake_up(&(wc)->freelist_wait); \
416 #define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error)))
418 static void writecache_flush_all_metadata(struct dm_writecache *wc)
420 if (!WC_MODE_PMEM(wc))
421 memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
424 static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
426 if (!WC_MODE_PMEM(wc))
427 __set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
431 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
434 struct dm_writecache *wc;
439 static void writecache_notify_io(unsigned long error, void *context)
441 struct io_notify *endio = context;
443 if (unlikely(error != 0))
444 writecache_error(endio->wc, -EIO, "error writing metadata");
445 BUG_ON(atomic_read(&endio->count) <= 0);
446 if (atomic_dec_and_test(&endio->count))
450 static void ssd_commit_flushed(struct dm_writecache *wc)
452 struct dm_io_region region;
453 struct dm_io_request req;
454 struct io_notify endio = {
456 COMPLETION_INITIALIZER_ONSTACK(endio.c),
459 unsigned bitmap_bits = wc->dirty_bitmap_size * 8;
464 i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i);
465 if (unlikely(i == bitmap_bits))
467 j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i);
469 region.bdev = wc->ssd_dev->bdev;
470 region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
471 region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
473 if (unlikely(region.sector >= wc->metadata_sectors))
475 if (unlikely(region.sector + region.count > wc->metadata_sectors))
476 region.count = wc->metadata_sectors - region.sector;
478 region.sector += wc->start_sector;
479 atomic_inc(&endio.count);
480 req.bi_op = REQ_OP_WRITE;
481 req.bi_op_flags = REQ_SYNC;
482 req.mem.type = DM_IO_VMA;
483 req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
484 req.client = wc->dm_io;
485 req.notify.fn = writecache_notify_io;
486 req.notify.context = &endio;
488 /* writing via async dm-io (implied by notify.fn above) won't return an error */
489 (void) dm_io(&req, 1, ®ion, NULL);
493 writecache_notify_io(0, &endio);
494 wait_for_completion_io(&endio.c);
496 writecache_disk_flush(wc, wc->ssd_dev);
498 memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
501 static void writecache_commit_flushed(struct dm_writecache *wc)
503 if (WC_MODE_PMEM(wc))
506 ssd_commit_flushed(wc);
509 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
512 struct dm_io_region region;
513 struct dm_io_request req;
515 region.bdev = dev->bdev;
518 req.bi_op = REQ_OP_WRITE;
519 req.bi_op_flags = REQ_PREFLUSH;
520 req.mem.type = DM_IO_KMEM;
521 req.mem.ptr.addr = NULL;
522 req.client = wc->dm_io;
523 req.notify.fn = NULL;
525 r = dm_io(&req, 1, ®ion, NULL);
527 writecache_error(wc, r, "error flushing metadata: %d", r);
530 static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
532 wait_event(wc->bio_in_progress_wait[direction],
533 !atomic_read(&wc->bio_in_progress[direction]));
536 #define WFE_RETURN_FOLLOWING 1
537 #define WFE_LOWEST_SEQ 2
539 static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
540 uint64_t block, int flags)
543 struct rb_node *node = wc->tree.rb_node;
549 e = container_of(node, struct wc_entry, rb_node);
550 if (read_original_sector(wc, e) == block)
552 node = (read_original_sector(wc, e) >= block ?
553 e->rb_node.rb_left : e->rb_node.rb_right);
554 if (unlikely(!node)) {
555 if (!(flags & WFE_RETURN_FOLLOWING)) {
558 if (read_original_sector(wc, e) >= block) {
561 node = rb_next(&e->rb_node);
562 if (unlikely(!node)) {
565 e = container_of(node, struct wc_entry, rb_node);
573 if (flags & WFE_LOWEST_SEQ)
574 node = rb_prev(&e->rb_node);
576 node = rb_next(&e->rb_node);
579 e2 = container_of(node, struct wc_entry, rb_node);
580 if (read_original_sector(wc, e2) != block)
586 static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
589 struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
592 e = container_of(*node, struct wc_entry, rb_node);
593 parent = &e->rb_node;
594 if (read_original_sector(wc, e) > read_original_sector(wc, ins))
595 node = &parent->rb_left;
597 node = &parent->rb_right;
599 rb_link_node(&ins->rb_node, parent, node);
600 rb_insert_color(&ins->rb_node, &wc->tree);
601 list_add(&ins->lru, &wc->lru);
604 static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
607 rb_erase(&e->rb_node, &wc->tree);
610 static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
612 if (WC_MODE_SORT_FREELIST(wc)) {
613 struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
614 if (unlikely(!*node))
615 wc->current_free = e;
618 if (&e->rb_node < *node)
619 node = &parent->rb_left;
621 node = &parent->rb_right;
623 rb_link_node(&e->rb_node, parent, node);
624 rb_insert_color(&e->rb_node, &wc->freetree);
626 list_add_tail(&e->lru, &wc->freelist);
631 static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc)
635 if (WC_MODE_SORT_FREELIST(wc)) {
636 struct rb_node *next;
637 if (unlikely(!wc->current_free))
639 e = wc->current_free;
640 next = rb_next(&e->rb_node);
641 rb_erase(&e->rb_node, &wc->freetree);
643 next = rb_first(&wc->freetree);
644 wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
646 if (unlikely(list_empty(&wc->freelist)))
648 e = container_of(wc->freelist.next, struct wc_entry, lru);
652 if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
653 queue_work(wc->writeback_wq, &wc->writeback_work);
658 static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
660 writecache_unlink(wc, e);
661 writecache_add_to_freelist(wc, e);
662 clear_seq_count(wc, e);
663 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
664 if (unlikely(waitqueue_active(&wc->freelist_wait)))
665 wake_up(&wc->freelist_wait);
668 static void writecache_wait_on_freelist(struct dm_writecache *wc)
672 prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
675 finish_wait(&wc->freelist_wait, &wait);
679 static void writecache_poison_lists(struct dm_writecache *wc)
682 * Catch incorrect access to these values while the device is suspended.
684 memset(&wc->tree, -1, sizeof wc->tree);
685 wc->lru.next = LIST_POISON1;
686 wc->lru.prev = LIST_POISON2;
687 wc->freelist.next = LIST_POISON1;
688 wc->freelist.prev = LIST_POISON2;
691 static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
693 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
694 if (WC_MODE_PMEM(wc))
695 writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
698 static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
700 return read_seq_count(wc, e) < wc->seq_count;
703 static void writecache_flush(struct dm_writecache *wc)
705 struct wc_entry *e, *e2;
706 bool need_flush_after_free;
708 wc->uncommitted_blocks = 0;
709 del_timer(&wc->autocommit_timer);
711 if (list_empty(&wc->lru))
714 e = container_of(wc->lru.next, struct wc_entry, lru);
715 if (writecache_entry_is_committed(wc, e)) {
716 if (wc->overwrote_committed) {
717 writecache_wait_for_ios(wc, WRITE);
718 writecache_disk_flush(wc, wc->ssd_dev);
719 wc->overwrote_committed = false;
724 writecache_flush_entry(wc, e);
725 if (unlikely(e->lru.next == &wc->lru))
727 e2 = container_of(e->lru.next, struct wc_entry, lru);
728 if (writecache_entry_is_committed(wc, e2))
733 writecache_commit_flushed(wc);
735 writecache_wait_for_ios(wc, WRITE);
738 pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
739 writecache_flush_region(wc, &sb(wc)->seq_count, sizeof sb(wc)->seq_count);
740 writecache_commit_flushed(wc);
742 wc->overwrote_committed = false;
744 need_flush_after_free = false;
746 /* Free another committed entry with lower seq-count */
747 struct rb_node *rb_node = rb_prev(&e->rb_node);
750 e2 = container_of(rb_node, struct wc_entry, rb_node);
751 if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
752 likely(!e2->write_in_progress)) {
753 writecache_free_entry(wc, e2);
754 need_flush_after_free = true;
757 if (unlikely(e->lru.prev == &wc->lru))
759 e = container_of(e->lru.prev, struct wc_entry, lru);
763 if (need_flush_after_free)
764 writecache_commit_flushed(wc);
767 static void writecache_flush_work(struct work_struct *work)
769 struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
772 writecache_flush(wc);
776 static void writecache_autocommit_timer(struct timer_list *t)
778 struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
779 if (!writecache_has_error(wc))
780 queue_work(wc->writeback_wq, &wc->flush_work);
783 static void writecache_schedule_autocommit(struct dm_writecache *wc)
785 if (!timer_pending(&wc->autocommit_timer))
786 mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
789 static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
792 bool discarded_something = false;
794 e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
798 while (read_original_sector(wc, e) < end) {
799 struct rb_node *node = rb_next(&e->rb_node);
801 if (likely(!e->write_in_progress)) {
802 if (!discarded_something) {
803 writecache_wait_for_ios(wc, READ);
804 writecache_wait_for_ios(wc, WRITE);
805 discarded_something = true;
807 writecache_free_entry(wc, e);
813 e = container_of(node, struct wc_entry, rb_node);
816 if (discarded_something)
817 writecache_commit_flushed(wc);
820 static bool writecache_wait_for_writeback(struct dm_writecache *wc)
822 if (wc->writeback_size) {
823 writecache_wait_on_freelist(wc);
829 static void writecache_suspend(struct dm_target *ti)
831 struct dm_writecache *wc = ti->private;
832 bool flush_on_suspend;
834 del_timer_sync(&wc->autocommit_timer);
837 writecache_flush(wc);
838 flush_on_suspend = wc->flush_on_suspend;
839 if (flush_on_suspend) {
840 wc->flush_on_suspend = false;
842 queue_work(wc->writeback_wq, &wc->writeback_work);
846 flush_workqueue(wc->writeback_wq);
849 if (flush_on_suspend)
851 while (writecache_wait_for_writeback(wc));
853 if (WC_MODE_PMEM(wc))
854 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
856 writecache_poison_lists(wc);
861 static int writecache_alloc_entries(struct dm_writecache *wc)
867 wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
870 for (b = 0; b < wc->n_blocks; b++) {
871 struct wc_entry *e = &wc->entries[b];
873 e->write_in_progress = false;
879 static void writecache_resume(struct dm_target *ti)
881 struct dm_writecache *wc = ti->private;
883 bool need_flush = false;
889 if (WC_MODE_PMEM(wc))
890 persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
893 INIT_LIST_HEAD(&wc->lru);
894 if (WC_MODE_SORT_FREELIST(wc)) {
895 wc->freetree = RB_ROOT;
896 wc->current_free = NULL;
898 INIT_LIST_HEAD(&wc->freelist);
900 wc->freelist_size = 0;
902 r = memcpy_mcsafe(&sb_seq_count, &sb(wc)->seq_count, sizeof(uint64_t));
904 writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
905 sb_seq_count = cpu_to_le64(0);
907 wc->seq_count = le64_to_cpu(sb_seq_count);
909 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
910 for (b = 0; b < wc->n_blocks; b++) {
911 struct wc_entry *e = &wc->entries[b];
912 struct wc_memory_entry wme;
913 if (writecache_has_error(wc)) {
914 e->original_sector = -1;
918 r = memcpy_mcsafe(&wme, memory_entry(wc, e), sizeof(struct wc_memory_entry));
920 writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
921 (unsigned long)b, r);
922 e->original_sector = -1;
925 e->original_sector = le64_to_cpu(wme.original_sector);
926 e->seq_count = le64_to_cpu(wme.seq_count);
930 for (b = 0; b < wc->n_blocks; b++) {
931 struct wc_entry *e = &wc->entries[b];
932 if (!writecache_entry_is_committed(wc, e)) {
933 if (read_seq_count(wc, e) != -1) {
935 clear_seq_count(wc, e);
938 writecache_add_to_freelist(wc, e);
940 struct wc_entry *old;
942 old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
944 writecache_insert_entry(wc, e);
946 if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
947 writecache_error(wc, -EINVAL,
948 "two identical entries, position %llu, sector %llu, sequence %llu",
949 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
950 (unsigned long long)read_seq_count(wc, e));
952 if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
955 writecache_free_entry(wc, old);
956 writecache_insert_entry(wc, e);
965 writecache_flush_all_metadata(wc);
966 writecache_commit_flushed(wc);
972 static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
978 if (dm_suspended(wc->ti)) {
982 if (writecache_has_error(wc)) {
987 writecache_flush(wc);
989 queue_work(wc->writeback_wq, &wc->writeback_work);
992 flush_workqueue(wc->writeback_wq);
996 if (writecache_has_error(wc)) {
1005 static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1011 wc->flush_on_suspend = true;
1017 static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
1018 char *result, unsigned maxlen)
1021 struct dm_writecache *wc = ti->private;
1023 if (!strcasecmp(argv[0], "flush"))
1024 r = process_flush_mesg(argc, argv, wc);
1025 else if (!strcasecmp(argv[0], "flush_on_suspend"))
1026 r = process_flush_on_suspend_mesg(argc, argv, wc);
1028 DMERR("unrecognised message received: %s", argv[0]);
1033 static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
1036 unsigned long flags;
1038 int rw = bio_data_dir(bio);
1039 unsigned remaining_size = wc->block_size;
1042 struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
1043 buf = bvec_kmap_irq(&bv, &flags);
1045 if (unlikely(size > remaining_size))
1046 size = remaining_size;
1050 r = memcpy_mcsafe(buf, data, size);
1051 flush_dcache_page(bio_page(bio));
1053 writecache_error(wc, r, "hardware memory error when reading data: %d", r);
1054 bio->bi_status = BLK_STS_IOERR;
1057 flush_dcache_page(bio_page(bio));
1058 memcpy_flushcache(data, buf, size);
1061 bvec_kunmap_irq(buf, &flags);
1063 data = (char *)data + size;
1064 remaining_size -= size;
1065 bio_advance(bio, size);
1066 } while (unlikely(remaining_size));
1069 static int writecache_flush_thread(void *data)
1071 struct dm_writecache *wc = data;
1077 bio = bio_list_pop(&wc->flush_list);
1079 set_current_state(TASK_INTERRUPTIBLE);
1082 if (unlikely(kthread_should_stop())) {
1083 set_current_state(TASK_RUNNING);
1091 if (bio_op(bio) == REQ_OP_DISCARD) {
1092 writecache_discard(wc, bio->bi_iter.bi_sector,
1093 bio_end_sector(bio));
1095 bio_set_dev(bio, wc->dev->bdev);
1096 generic_make_request(bio);
1098 writecache_flush(wc);
1100 if (writecache_has_error(wc))
1101 bio->bi_status = BLK_STS_IOERR;
1109 static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
1111 if (bio_list_empty(&wc->flush_list))
1112 wake_up_process(wc->flush_thread);
1113 bio_list_add(&wc->flush_list, bio);
1116 static int writecache_map(struct dm_target *ti, struct bio *bio)
1119 struct dm_writecache *wc = ti->private;
1121 bio->bi_private = NULL;
1125 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1126 if (writecache_has_error(wc))
1128 if (WC_MODE_PMEM(wc)) {
1129 writecache_flush(wc);
1130 if (writecache_has_error(wc))
1134 writecache_offload_bio(wc, bio);
1139 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1141 if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
1142 (wc->block_size / 512 - 1)) != 0)) {
1143 DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
1144 (unsigned long long)bio->bi_iter.bi_sector,
1145 bio->bi_iter.bi_size, wc->block_size);
1149 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
1150 if (writecache_has_error(wc))
1152 if (WC_MODE_PMEM(wc)) {
1153 writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
1154 goto unlock_remap_origin;
1156 writecache_offload_bio(wc, bio);
1161 if (bio_data_dir(bio) == READ) {
1163 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1164 if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1165 if (WC_MODE_PMEM(wc)) {
1166 bio_copy_block(wc, bio, memory_data(wc, e));
1167 if (bio->bi_iter.bi_size)
1168 goto read_next_block;
1171 dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1172 bio_set_dev(bio, wc->ssd_dev->bdev);
1173 bio->bi_iter.bi_sector = cache_sector(wc, e);
1174 if (!writecache_entry_is_committed(wc, e))
1175 writecache_wait_for_ios(wc, WRITE);
1180 sector_t next_boundary =
1181 read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1182 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
1183 dm_accept_partial_bio(bio, next_boundary);
1186 goto unlock_remap_origin;
1190 if (writecache_has_error(wc))
1192 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1194 if (!writecache_entry_is_committed(wc, e))
1196 if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1197 wc->overwrote_committed = true;
1201 e = writecache_pop_from_freelist(wc);
1203 writecache_wait_on_freelist(wc);
1206 write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1207 writecache_insert_entry(wc, e);
1208 wc->uncommitted_blocks++;
1210 if (WC_MODE_PMEM(wc)) {
1211 bio_copy_block(wc, bio, memory_data(wc, e));
1213 dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1214 bio_set_dev(bio, wc->ssd_dev->bdev);
1215 bio->bi_iter.bi_sector = cache_sector(wc, e);
1216 if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1217 wc->uncommitted_blocks = 0;
1218 queue_work(wc->writeback_wq, &wc->flush_work);
1220 writecache_schedule_autocommit(wc);
1224 } while (bio->bi_iter.bi_size);
1226 if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks))
1227 writecache_flush(wc);
1229 writecache_schedule_autocommit(wc);
1233 unlock_remap_origin:
1234 bio_set_dev(bio, wc->dev->bdev);
1236 return DM_MAPIO_REMAPPED;
1239 /* make sure that writecache_end_io decrements bio_in_progress: */
1240 bio->bi_private = (void *)1;
1241 atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
1243 return DM_MAPIO_REMAPPED;
1248 return DM_MAPIO_SUBMITTED;
1252 return DM_MAPIO_SUBMITTED;
1257 return DM_MAPIO_SUBMITTED;
1260 static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
1262 struct dm_writecache *wc = ti->private;
1264 if (bio->bi_private != NULL) {
1265 int dir = bio_data_dir(bio);
1266 if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
1267 if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
1268 wake_up(&wc->bio_in_progress_wait[dir]);
1273 static int writecache_iterate_devices(struct dm_target *ti,
1274 iterate_devices_callout_fn fn, void *data)
1276 struct dm_writecache *wc = ti->private;
1278 return fn(ti, wc->dev, 0, ti->len, data);
1281 static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
1283 struct dm_writecache *wc = ti->private;
1285 if (limits->logical_block_size < wc->block_size)
1286 limits->logical_block_size = wc->block_size;
1288 if (limits->physical_block_size < wc->block_size)
1289 limits->physical_block_size = wc->block_size;
1291 if (limits->io_min < wc->block_size)
1292 limits->io_min = wc->block_size;
1296 static void writecache_writeback_endio(struct bio *bio)
1298 struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
1299 struct dm_writecache *wc = wb->wc;
1300 unsigned long flags;
1302 raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
1303 if (unlikely(list_empty(&wc->endio_list)))
1304 wake_up_process(wc->endio_thread);
1305 list_add_tail(&wb->endio_entry, &wc->endio_list);
1306 raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
1309 static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
1311 struct copy_struct *c = ptr;
1312 struct dm_writecache *wc = c->wc;
1314 c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
1316 raw_spin_lock_irq(&wc->endio_list_lock);
1317 if (unlikely(list_empty(&wc->endio_list)))
1318 wake_up_process(wc->endio_thread);
1319 list_add_tail(&c->endio_entry, &wc->endio_list);
1320 raw_spin_unlock_irq(&wc->endio_list_lock);
1323 static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
1326 struct writeback_struct *wb;
1328 unsigned long n_walked = 0;
1331 wb = list_entry(list->next, struct writeback_struct, endio_entry);
1332 list_del(&wb->endio_entry);
1334 if (unlikely(wb->bio.bi_status != BLK_STS_OK))
1335 writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
1336 "write error %d", wb->bio.bi_status);
1340 BUG_ON(!e->write_in_progress);
1341 e->write_in_progress = false;
1342 INIT_LIST_HEAD(&e->lru);
1343 if (!writecache_has_error(wc))
1344 writecache_free_entry(wc, e);
1345 BUG_ON(!wc->writeback_size);
1346 wc->writeback_size--;
1348 if (unlikely(n_walked >= ENDIO_LATENCY)) {
1349 writecache_commit_flushed(wc);
1354 } while (++i < wb->wc_list_n);
1356 if (wb->wc_list != wb->wc_list_inline)
1359 } while (!list_empty(list));
1362 static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
1364 struct copy_struct *c;
1368 c = list_entry(list->next, struct copy_struct, endio_entry);
1369 list_del(&c->endio_entry);
1371 if (unlikely(c->error))
1372 writecache_error(wc, c->error, "copy error");
1376 BUG_ON(!e->write_in_progress);
1377 e->write_in_progress = false;
1378 INIT_LIST_HEAD(&e->lru);
1379 if (!writecache_has_error(wc))
1380 writecache_free_entry(wc, e);
1382 BUG_ON(!wc->writeback_size);
1383 wc->writeback_size--;
1385 } while (--c->n_entries);
1386 mempool_free(c, &wc->copy_pool);
1387 } while (!list_empty(list));
1390 static int writecache_endio_thread(void *data)
1392 struct dm_writecache *wc = data;
1395 struct list_head list;
1397 raw_spin_lock_irq(&wc->endio_list_lock);
1398 if (!list_empty(&wc->endio_list))
1400 set_current_state(TASK_INTERRUPTIBLE);
1401 raw_spin_unlock_irq(&wc->endio_list_lock);
1403 if (unlikely(kthread_should_stop())) {
1404 set_current_state(TASK_RUNNING);
1413 list = wc->endio_list;
1414 list.next->prev = list.prev->next = &list;
1415 INIT_LIST_HEAD(&wc->endio_list);
1416 raw_spin_unlock_irq(&wc->endio_list_lock);
1418 if (!WC_MODE_FUA(wc))
1419 writecache_disk_flush(wc, wc->dev);
1423 if (WC_MODE_PMEM(wc)) {
1424 __writecache_endio_pmem(wc, &list);
1426 __writecache_endio_ssd(wc, &list);
1427 writecache_wait_for_ios(wc, READ);
1430 writecache_commit_flushed(wc);
1438 static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e, gfp_t gfp)
1440 struct dm_writecache *wc = wb->wc;
1441 unsigned block_size = wc->block_size;
1442 void *address = memory_data(wc, e);
1444 persistent_memory_flush_cache(address, block_size);
1445 return bio_add_page(&wb->bio, persistent_memory_page(address),
1446 block_size, persistent_memory_page_offset(address)) != 0;
1449 struct writeback_list {
1450 struct list_head list;
1454 static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
1456 if (unlikely(wc->max_writeback_jobs)) {
1457 if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
1459 while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
1460 writecache_wait_on_freelist(wc);
1467 static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
1469 struct wc_entry *e, *f;
1471 struct writeback_struct *wb;
1476 e = container_of(wbl->list.prev, struct wc_entry, lru);
1479 max_pages = e->wc_list_contiguous;
1481 bio = bio_alloc_bioset(GFP_NOIO, max_pages, &wc->bio_set);
1482 wb = container_of(bio, struct writeback_struct, bio);
1484 wb->bio.bi_end_io = writecache_writeback_endio;
1485 bio_set_dev(&wb->bio, wc->dev->bdev);
1486 wb->bio.bi_iter.bi_sector = read_original_sector(wc, e);
1487 wb->page_offset = PAGE_SIZE;
1488 if (max_pages <= WB_LIST_INLINE ||
1489 unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
1490 GFP_NOIO | __GFP_NORETRY |
1491 __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
1492 wb->wc_list = wb->wc_list_inline;
1493 max_pages = WB_LIST_INLINE;
1496 BUG_ON(!wc_add_block(wb, e, GFP_NOIO));
1501 while (wbl->size && wb->wc_list_n < max_pages) {
1502 f = container_of(wbl->list.prev, struct wc_entry, lru);
1503 if (read_original_sector(wc, f) !=
1504 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1506 if (!wc_add_block(wb, f, GFP_NOWAIT | __GFP_NOWARN))
1510 wb->wc_list[wb->wc_list_n++] = f;
1513 bio_set_op_attrs(&wb->bio, REQ_OP_WRITE, WC_MODE_FUA(wc) * REQ_FUA);
1514 if (writecache_has_error(wc)) {
1515 bio->bi_status = BLK_STS_IOERR;
1516 bio_endio(&wb->bio);
1518 submit_bio(&wb->bio);
1521 __writeback_throttle(wc, wbl);
1525 static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
1527 struct wc_entry *e, *f;
1528 struct dm_io_region from, to;
1529 struct copy_struct *c;
1535 e = container_of(wbl->list.prev, struct wc_entry, lru);
1538 n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
1540 from.bdev = wc->ssd_dev->bdev;
1541 from.sector = cache_sector(wc, e);
1542 from.count = n_sectors;
1543 to.bdev = wc->dev->bdev;
1544 to.sector = read_original_sector(wc, e);
1545 to.count = n_sectors;
1547 c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
1550 c->n_entries = e->wc_list_contiguous;
1552 while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
1554 f = container_of(wbl->list.prev, struct wc_entry, lru);
1560 dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
1562 __writeback_throttle(wc, wbl);
1566 static void writecache_writeback(struct work_struct *work)
1568 struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
1569 struct blk_plug plug;
1570 struct wc_entry *e, *f, *g;
1571 struct rb_node *node, *next_node;
1572 struct list_head skipped;
1573 struct writeback_list wbl;
1574 unsigned long n_walked;
1578 if (writecache_has_error(wc)) {
1583 if (unlikely(wc->writeback_all)) {
1584 if (writecache_wait_for_writeback(wc))
1588 if (wc->overwrote_committed) {
1589 writecache_wait_for_ios(wc, WRITE);
1593 INIT_LIST_HEAD(&skipped);
1594 INIT_LIST_HEAD(&wbl.list);
1596 while (!list_empty(&wc->lru) &&
1597 (wc->writeback_all ||
1598 wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark)) {
1601 if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1602 likely(!wc->writeback_all) && likely(!dm_suspended(wc->ti))) {
1603 queue_work(wc->writeback_wq, &wc->writeback_work);
1607 e = container_of(wc->lru.prev, struct wc_entry, lru);
1608 BUG_ON(e->write_in_progress);
1609 if (unlikely(!writecache_entry_is_committed(wc, e))) {
1610 writecache_flush(wc);
1612 node = rb_prev(&e->rb_node);
1614 f = container_of(node, struct wc_entry, rb_node);
1615 if (unlikely(read_original_sector(wc, f) ==
1616 read_original_sector(wc, e))) {
1617 BUG_ON(!f->write_in_progress);
1619 list_add(&e->lru, &skipped);
1624 wc->writeback_size++;
1626 list_add(&e->lru, &wbl.list);
1628 e->write_in_progress = true;
1629 e->wc_list_contiguous = 1;
1634 next_node = rb_next(&f->rb_node);
1635 if (unlikely(!next_node))
1637 g = container_of(next_node, struct wc_entry, rb_node);
1638 if (read_original_sector(wc, g) ==
1639 read_original_sector(wc, f)) {
1643 if (read_original_sector(wc, g) !=
1644 read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
1646 if (unlikely(g->write_in_progress))
1648 if (unlikely(!writecache_entry_is_committed(wc, g)))
1651 if (!WC_MODE_PMEM(wc)) {
1657 //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
1660 wc->writeback_size++;
1662 list_add(&g->lru, &wbl.list);
1664 g->write_in_progress = true;
1665 g->wc_list_contiguous = BIO_MAX_PAGES;
1667 e->wc_list_contiguous++;
1668 if (unlikely(e->wc_list_contiguous == BIO_MAX_PAGES))
1674 if (!list_empty(&skipped)) {
1675 list_splice_tail(&skipped, &wc->lru);
1677 * If we didn't do any progress, we must wait until some
1678 * writeback finishes to avoid burning CPU in a loop
1680 if (unlikely(!wbl.size))
1681 writecache_wait_for_writeback(wc);
1686 blk_start_plug(&plug);
1688 if (WC_MODE_PMEM(wc))
1689 __writecache_writeback_pmem(wc, &wbl);
1691 __writecache_writeback_ssd(wc, &wbl);
1693 blk_finish_plug(&plug);
1695 if (unlikely(wc->writeback_all)) {
1697 while (writecache_wait_for_writeback(wc));
1702 static int calculate_memory_size(uint64_t device_size, unsigned block_size,
1703 size_t *n_blocks_p, size_t *n_metadata_blocks_p)
1705 uint64_t n_blocks, offset;
1708 n_blocks = device_size;
1709 do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
1714 /* Verify the following entries[n_blocks] won't overflow */
1715 if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
1716 sizeof(struct wc_memory_entry)))
1718 offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
1719 offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
1720 if (offset + n_blocks * block_size <= device_size)
1725 /* check if the bit field overflows */
1727 if (e.index != n_blocks)
1731 *n_blocks_p = n_blocks;
1732 if (n_metadata_blocks_p)
1733 *n_metadata_blocks_p = offset >> __ffs(block_size);
1737 static int init_memory(struct dm_writecache *wc)
1742 r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
1746 r = writecache_alloc_entries(wc);
1750 for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
1751 pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
1752 pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
1753 pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
1754 pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
1755 pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
1757 for (b = 0; b < wc->n_blocks; b++)
1758 write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
1760 writecache_flush_all_metadata(wc);
1761 writecache_commit_flushed(wc);
1762 pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
1763 writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
1764 writecache_commit_flushed(wc);
1769 static void writecache_dtr(struct dm_target *ti)
1771 struct dm_writecache *wc = ti->private;
1776 if (wc->endio_thread)
1777 kthread_stop(wc->endio_thread);
1779 if (wc->flush_thread)
1780 kthread_stop(wc->flush_thread);
1782 bioset_exit(&wc->bio_set);
1784 mempool_exit(&wc->copy_pool);
1786 if (wc->writeback_wq)
1787 destroy_workqueue(wc->writeback_wq);
1790 dm_put_device(ti, wc->dev);
1793 dm_put_device(ti, wc->ssd_dev);
1798 if (wc->memory_map) {
1799 if (WC_MODE_PMEM(wc))
1800 persistent_memory_release(wc);
1802 vfree(wc->memory_map);
1806 dm_kcopyd_client_destroy(wc->dm_kcopyd);
1809 dm_io_client_destroy(wc->dm_io);
1811 if (wc->dirty_bitmap)
1812 vfree(wc->dirty_bitmap);
1817 static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
1819 struct dm_writecache *wc;
1820 struct dm_arg_set as;
1822 unsigned opt_params;
1823 size_t offset, data_size;
1826 int high_wm_percent = HIGH_WATERMARK;
1827 int low_wm_percent = LOW_WATERMARK;
1829 struct wc_memory_superblock s;
1831 static struct dm_arg _args[] = {
1832 {0, 10, "Invalid number of feature args"},
1838 wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
1840 ti->error = "Cannot allocate writecache structure";
1847 mutex_init(&wc->lock);
1848 writecache_poison_lists(wc);
1849 init_waitqueue_head(&wc->freelist_wait);
1850 timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
1852 for (i = 0; i < 2; i++) {
1853 atomic_set(&wc->bio_in_progress[i], 0);
1854 init_waitqueue_head(&wc->bio_in_progress_wait[i]);
1857 wc->dm_io = dm_io_client_create();
1858 if (IS_ERR(wc->dm_io)) {
1859 r = PTR_ERR(wc->dm_io);
1860 ti->error = "Unable to allocate dm-io client";
1865 wc->writeback_wq = alloc_workqueue("writecache-writeabck", WQ_MEM_RECLAIM, 1);
1866 if (!wc->writeback_wq) {
1868 ti->error = "Could not allocate writeback workqueue";
1871 INIT_WORK(&wc->writeback_work, writecache_writeback);
1872 INIT_WORK(&wc->flush_work, writecache_flush_work);
1874 raw_spin_lock_init(&wc->endio_list_lock);
1875 INIT_LIST_HEAD(&wc->endio_list);
1876 wc->endio_thread = kthread_create(writecache_endio_thread, wc, "writecache_endio");
1877 if (IS_ERR(wc->endio_thread)) {
1878 r = PTR_ERR(wc->endio_thread);
1879 wc->endio_thread = NULL;
1880 ti->error = "Couldn't spawn endio thread";
1883 wake_up_process(wc->endio_thread);
1886 * Parse the mode (pmem or ssd)
1888 string = dm_shift_arg(&as);
1892 if (!strcasecmp(string, "s")) {
1893 wc->pmem_mode = false;
1894 } else if (!strcasecmp(string, "p")) {
1895 #ifdef DM_WRITECACHE_HAS_PMEM
1896 wc->pmem_mode = true;
1897 wc->writeback_fua = true;
1900 * If the architecture doesn't support persistent memory or
1901 * the kernel doesn't support any DAX drivers, this driver can
1902 * only be used in SSD-only mode.
1905 ti->error = "Persistent memory or DAX not supported on this system";
1912 if (WC_MODE_PMEM(wc)) {
1913 r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
1914 offsetof(struct writeback_struct, bio),
1917 ti->error = "Could not allocate bio set";
1921 r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
1923 ti->error = "Could not allocate mempool";
1929 * Parse the origin data device
1931 string = dm_shift_arg(&as);
1934 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
1936 ti->error = "Origin data device lookup failed";
1941 * Parse cache data device (be it pmem or ssd)
1943 string = dm_shift_arg(&as);
1947 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
1949 ti->error = "Cache data device lookup failed";
1952 wc->memory_map_size = i_size_read(wc->ssd_dev->bdev->bd_inode);
1955 * Parse the cache block size
1957 string = dm_shift_arg(&as);
1960 if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
1961 wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
1962 (wc->block_size & (wc->block_size - 1))) {
1964 ti->error = "Invalid block size";
1967 wc->block_size_bits = __ffs(wc->block_size);
1969 wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
1970 wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
1971 wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
1974 * Parse optional arguments
1976 r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
1980 while (opt_params) {
1981 string = dm_shift_arg(&as), opt_params--;
1982 if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
1983 unsigned long long start_sector;
1984 string = dm_shift_arg(&as), opt_params--;
1985 if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
1986 goto invalid_optional;
1987 wc->start_sector = start_sector;
1988 if (wc->start_sector != start_sector ||
1989 wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
1990 goto invalid_optional;
1991 } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
1992 string = dm_shift_arg(&as), opt_params--;
1993 if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
1994 goto invalid_optional;
1995 if (high_wm_percent < 0 || high_wm_percent > 100)
1996 goto invalid_optional;
1997 wc->high_wm_percent_set = true;
1998 } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
1999 string = dm_shift_arg(&as), opt_params--;
2000 if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
2001 goto invalid_optional;
2002 if (low_wm_percent < 0 || low_wm_percent > 100)
2003 goto invalid_optional;
2004 wc->low_wm_percent_set = true;
2005 } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
2006 string = dm_shift_arg(&as), opt_params--;
2007 if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
2008 goto invalid_optional;
2009 wc->max_writeback_jobs_set = true;
2010 } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
2011 string = dm_shift_arg(&as), opt_params--;
2012 if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
2013 goto invalid_optional;
2014 wc->autocommit_blocks_set = true;
2015 } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
2016 unsigned autocommit_msecs;
2017 string = dm_shift_arg(&as), opt_params--;
2018 if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
2019 goto invalid_optional;
2020 if (autocommit_msecs > 3600000)
2021 goto invalid_optional;
2022 wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2023 wc->autocommit_time_set = true;
2024 } else if (!strcasecmp(string, "fua")) {
2025 if (WC_MODE_PMEM(wc)) {
2026 wc->writeback_fua = true;
2027 wc->writeback_fua_set = true;
2028 } else goto invalid_optional;
2029 } else if (!strcasecmp(string, "nofua")) {
2030 if (WC_MODE_PMEM(wc)) {
2031 wc->writeback_fua = false;
2032 wc->writeback_fua_set = true;
2033 } else goto invalid_optional;
2037 ti->error = "Invalid optional argument";
2042 if (high_wm_percent < low_wm_percent) {
2044 ti->error = "High watermark must be greater than or equal to low watermark";
2048 if (WC_MODE_PMEM(wc)) {
2049 r = persistent_memory_claim(wc);
2051 ti->error = "Unable to map persistent memory for cache";
2055 struct dm_io_region region;
2056 struct dm_io_request req;
2057 size_t n_blocks, n_metadata_blocks;
2058 uint64_t n_bitmap_bits;
2060 wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2062 bio_list_init(&wc->flush_list);
2063 wc->flush_thread = kthread_create(writecache_flush_thread, wc, "dm_writecache_flush");
2064 if (IS_ERR(wc->flush_thread)) {
2065 r = PTR_ERR(wc->flush_thread);
2066 wc->flush_thread = NULL;
2067 ti->error = "Couldn't spawn endio thread";
2070 wake_up_process(wc->flush_thread);
2072 r = calculate_memory_size(wc->memory_map_size, wc->block_size,
2073 &n_blocks, &n_metadata_blocks);
2075 ti->error = "Invalid device size";
2079 n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
2080 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
2081 /* this is limitation of test_bit functions */
2082 if (n_bitmap_bits > 1U << 31) {
2084 ti->error = "Invalid device size";
2088 wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
2089 if (!wc->memory_map) {
2091 ti->error = "Unable to allocate memory for metadata";
2095 wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2096 if (IS_ERR(wc->dm_kcopyd)) {
2097 r = PTR_ERR(wc->dm_kcopyd);
2098 ti->error = "Unable to allocate dm-kcopyd client";
2099 wc->dm_kcopyd = NULL;
2103 wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
2104 wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
2105 BITS_PER_LONG * sizeof(unsigned long);
2106 wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
2107 if (!wc->dirty_bitmap) {
2109 ti->error = "Unable to allocate dirty bitmap";
2113 region.bdev = wc->ssd_dev->bdev;
2114 region.sector = wc->start_sector;
2115 region.count = wc->metadata_sectors;
2116 req.bi_op = REQ_OP_READ;
2117 req.bi_op_flags = REQ_SYNC;
2118 req.mem.type = DM_IO_VMA;
2119 req.mem.ptr.vma = (char *)wc->memory_map;
2120 req.client = wc->dm_io;
2121 req.notify.fn = NULL;
2123 r = dm_io(&req, 1, ®ion, NULL);
2125 ti->error = "Unable to read metadata";
2130 r = memcpy_mcsafe(&s, sb(wc), sizeof(struct wc_memory_superblock));
2132 ti->error = "Hardware memory error when reading superblock";
2135 if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
2136 r = init_memory(wc);
2138 ti->error = "Unable to initialize device";
2141 r = memcpy_mcsafe(&s, sb(wc), sizeof(struct wc_memory_superblock));
2143 ti->error = "Hardware memory error when reading superblock";
2148 if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
2149 ti->error = "Invalid magic in the superblock";
2154 if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
2155 ti->error = "Invalid version in the superblock";
2160 if (le32_to_cpu(s.block_size) != wc->block_size) {
2161 ti->error = "Block size does not match superblock";
2166 wc->n_blocks = le64_to_cpu(s.n_blocks);
2168 offset = wc->n_blocks * sizeof(struct wc_memory_entry);
2169 if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
2171 ti->error = "Overflow in size calculation";
2175 offset += sizeof(struct wc_memory_superblock);
2176 if (offset < sizeof(struct wc_memory_superblock))
2178 offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
2179 data_size = wc->n_blocks * (size_t)wc->block_size;
2180 if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
2181 (offset + data_size < offset))
2183 if (offset + data_size > wc->memory_map_size) {
2184 ti->error = "Memory area is too small";
2189 wc->metadata_sectors = offset >> SECTOR_SHIFT;
2190 wc->block_start = (char *)sb(wc) + offset;
2192 x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
2195 wc->freelist_high_watermark = x;
2196 x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
2199 wc->freelist_low_watermark = x;
2201 r = writecache_alloc_entries(wc);
2203 ti->error = "Cannot allocate memory";
2207 ti->num_flush_bios = 1;
2208 ti->flush_supported = true;
2209 ti->num_discard_bios = 1;
2211 if (WC_MODE_PMEM(wc))
2212 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
2218 ti->error = "Bad arguments";
2224 static void writecache_status(struct dm_target *ti, status_type_t type,
2225 unsigned status_flags, char *result, unsigned maxlen)
2227 struct dm_writecache *wc = ti->private;
2228 unsigned extra_args;
2233 case STATUSTYPE_INFO:
2234 DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),
2235 (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2236 (unsigned long long)wc->writeback_size);
2238 case STATUSTYPE_TABLE:
2239 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
2240 wc->dev->name, wc->ssd_dev->name, wc->block_size);
2242 if (wc->start_sector)
2244 if (wc->high_wm_percent_set)
2246 if (wc->low_wm_percent_set)
2248 if (wc->max_writeback_jobs_set)
2250 if (wc->autocommit_blocks_set)
2252 if (wc->autocommit_time_set)
2254 if (wc->writeback_fua_set)
2257 DMEMIT("%u", extra_args);
2258 if (wc->start_sector)
2259 DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2260 if (wc->high_wm_percent_set) {
2261 x = (uint64_t)wc->freelist_high_watermark * 100;
2262 x += wc->n_blocks / 2;
2263 do_div(x, (size_t)wc->n_blocks);
2264 DMEMIT(" high_watermark %u", 100 - (unsigned)x);
2266 if (wc->low_wm_percent_set) {
2267 x = (uint64_t)wc->freelist_low_watermark * 100;
2268 x += wc->n_blocks / 2;
2269 do_div(x, (size_t)wc->n_blocks);
2270 DMEMIT(" low_watermark %u", 100 - (unsigned)x);
2272 if (wc->max_writeback_jobs_set)
2273 DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
2274 if (wc->autocommit_blocks_set)
2275 DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
2276 if (wc->autocommit_time_set)
2277 DMEMIT(" autocommit_time %u", jiffies_to_msecs(wc->autocommit_jiffies));
2278 if (wc->writeback_fua_set)
2279 DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2284 static struct target_type writecache_target = {
2285 .name = "writecache",
2286 .version = {1, 1, 1},
2287 .module = THIS_MODULE,
2288 .ctr = writecache_ctr,
2289 .dtr = writecache_dtr,
2290 .status = writecache_status,
2291 .postsuspend = writecache_suspend,
2292 .resume = writecache_resume,
2293 .message = writecache_message,
2294 .map = writecache_map,
2295 .end_io = writecache_end_io,
2296 .iterate_devices = writecache_iterate_devices,
2297 .io_hints = writecache_io_hints,
2300 static int __init dm_writecache_init(void)
2304 r = dm_register_target(&writecache_target);
2306 DMERR("register failed %d", r);
2313 static void __exit dm_writecache_exit(void)
2315 dm_unregister_target(&writecache_target);
2318 module_init(dm_writecache_init);
2319 module_exit(dm_writecache_exit);
2321 MODULE_DESCRIPTION(DM_NAME " writecache target");
2322 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2323 MODULE_LICENSE("GPL");