1 // SPDX-License-Identifier: GPL-2.0
3 * Copyright (C) 2018 Red Hat. All rights reserved.
5 * This file is released under the GPL.
8 #include <linux/device-mapper.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/vmalloc.h>
12 #include <linux/kthread.h>
13 #include <linux/dm-io.h>
14 #include <linux/dm-kcopyd.h>
15 #include <linux/dax.h>
16 #include <linux/pfn_t.h>
17 #include <linux/libnvdimm.h>
18 #include <linux/delay.h>
19 #include "dm-io-tracker.h"
21 #define DM_MSG_PREFIX "writecache"
23 #define HIGH_WATERMARK 50
24 #define LOW_WATERMARK 45
25 #define MAX_WRITEBACK_JOBS 0
26 #define ENDIO_LATENCY 16
27 #define WRITEBACK_LATENCY 64
28 #define AUTOCOMMIT_BLOCKS_SSD 65536
29 #define AUTOCOMMIT_BLOCKS_PMEM 64
30 #define AUTOCOMMIT_MSEC 1000
31 #define MAX_AGE_DIV 16
32 #define MAX_AGE_UNSPECIFIED -1UL
33 #define PAUSE_WRITEBACK (HZ * 3)
35 #define BITMAP_GRANULARITY 65536
36 #if BITMAP_GRANULARITY < PAGE_SIZE
37 #undef BITMAP_GRANULARITY
38 #define BITMAP_GRANULARITY PAGE_SIZE
41 #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_DAX_DRIVER)
42 #define DM_WRITECACHE_HAS_PMEM
45 #ifdef DM_WRITECACHE_HAS_PMEM
46 #define pmem_assign(dest, src) \
48 typeof(dest) uniq = (src); \
49 memcpy_flushcache(&(dest), &uniq, sizeof(dest)); \
52 #define pmem_assign(dest, src) ((dest) = (src))
55 #if IS_ENABLED(CONFIG_ARCH_HAS_COPY_MC) && defined(DM_WRITECACHE_HAS_PMEM)
56 #define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
59 #define MEMORY_SUPERBLOCK_MAGIC 0x23489321
60 #define MEMORY_SUPERBLOCK_VERSION 1
62 struct wc_memory_entry {
63 __le64 original_sector;
67 struct wc_memory_superblock {
79 struct wc_memory_entry entries[];
83 struct rb_node rb_node;
85 unsigned short wc_list_contiguous;
86 bool write_in_progress
87 #if BITS_PER_LONG == 64
92 #if BITS_PER_LONG == 64
97 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
98 uint64_t original_sector;
103 #ifdef DM_WRITECACHE_HAS_PMEM
104 #define WC_MODE_PMEM(wc) ((wc)->pmem_mode)
105 #define WC_MODE_FUA(wc) ((wc)->writeback_fua)
107 #define WC_MODE_PMEM(wc) false
108 #define WC_MODE_FUA(wc) false
110 #define WC_MODE_SORT_FREELIST(wc) (!WC_MODE_PMEM(wc))
112 struct dm_writecache {
114 struct list_head lru;
116 struct list_head freelist;
118 struct rb_root freetree;
119 struct wc_entry *current_free;
124 size_t freelist_size;
125 size_t writeback_size;
126 size_t freelist_high_watermark;
127 size_t freelist_low_watermark;
128 unsigned long max_age;
131 unsigned uncommitted_blocks;
132 unsigned autocommit_blocks;
133 unsigned max_writeback_jobs;
137 unsigned long autocommit_jiffies;
138 struct timer_list autocommit_timer;
139 struct wait_queue_head freelist_wait;
141 struct timer_list max_age_timer;
143 atomic_t bio_in_progress[2];
144 struct wait_queue_head bio_in_progress_wait[2];
146 struct dm_target *ti;
148 struct dm_dev *ssd_dev;
149 sector_t start_sector;
151 uint64_t memory_map_size;
152 size_t metadata_sectors;
155 sector_t data_device_sectors;
157 struct wc_entry *entries;
159 unsigned char block_size_bits;
162 bool writeback_fua:1;
164 bool overwrote_committed:1;
165 bool memory_vmapped:1;
167 bool start_sector_set:1;
168 bool high_wm_percent_set:1;
169 bool low_wm_percent_set:1;
170 bool max_writeback_jobs_set:1;
171 bool autocommit_blocks_set:1;
172 bool autocommit_time_set:1;
174 bool writeback_fua_set:1;
175 bool flush_on_suspend:1;
178 bool metadata_only:1;
181 unsigned high_wm_percent_value;
182 unsigned low_wm_percent_value;
183 unsigned autocommit_time_value;
184 unsigned max_age_value;
185 unsigned pause_value;
187 unsigned writeback_all;
188 struct workqueue_struct *writeback_wq;
189 struct work_struct writeback_work;
190 struct work_struct flush_work;
192 struct dm_io_tracker iot;
194 struct dm_io_client *dm_io;
196 raw_spinlock_t endio_list_lock;
197 struct list_head endio_list;
198 struct task_struct *endio_thread;
200 struct task_struct *flush_thread;
201 struct bio_list flush_list;
203 struct dm_kcopyd_client *dm_kcopyd;
204 unsigned long *dirty_bitmap;
205 unsigned dirty_bitmap_size;
207 struct bio_set bio_set;
211 unsigned long long reads;
212 unsigned long long read_hits;
213 unsigned long long writes;
214 unsigned long long write_hits_uncommitted;
215 unsigned long long write_hits_committed;
216 unsigned long long writes_around;
217 unsigned long long writes_allocate;
218 unsigned long long writes_blocked_on_freelist;
219 unsigned long long flushes;
220 unsigned long long discards;
224 #define WB_LIST_INLINE 16
226 struct writeback_struct {
227 struct list_head endio_entry;
228 struct dm_writecache *wc;
229 struct wc_entry **wc_list;
231 struct wc_entry *wc_list_inline[WB_LIST_INLINE];
236 struct list_head endio_entry;
237 struct dm_writecache *wc;
243 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
244 "A percentage of time allocated for data copying");
246 static void wc_lock(struct dm_writecache *wc)
248 mutex_lock(&wc->lock);
251 static void wc_unlock(struct dm_writecache *wc)
253 mutex_unlock(&wc->lock);
256 #ifdef DM_WRITECACHE_HAS_PMEM
257 static int persistent_memory_claim(struct dm_writecache *wc)
267 wc->memory_vmapped = false;
269 s = wc->memory_map_size;
275 if (p != s >> PAGE_SHIFT) {
280 offset = get_start_sect(wc->ssd_dev->bdev);
281 if (offset & (PAGE_SIZE / 512 - 1)) {
285 offset >>= PAGE_SHIFT - 9;
287 id = dax_read_lock();
289 da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, &wc->memory_map, &pfn);
291 wc->memory_map = NULL;
295 if (!pfn_t_has_page(pfn)) {
296 wc->memory_map = NULL;
302 wc->memory_map = NULL;
303 pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
311 daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i, p - i,
314 r = daa ? daa : -EINVAL;
317 if (!pfn_t_has_page(pfn)) {
321 while (daa-- && i < p) {
322 pages[i++] = pfn_t_to_page(pfn);
328 wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
329 if (!wc->memory_map) {
334 wc->memory_vmapped = true;
339 wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
340 wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
351 static int persistent_memory_claim(struct dm_writecache *wc)
357 static void persistent_memory_release(struct dm_writecache *wc)
359 if (wc->memory_vmapped)
360 vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
363 static struct page *persistent_memory_page(void *addr)
365 if (is_vmalloc_addr(addr))
366 return vmalloc_to_page(addr);
368 return virt_to_page(addr);
371 static unsigned persistent_memory_page_offset(void *addr)
373 return (unsigned long)addr & (PAGE_SIZE - 1);
376 static void persistent_memory_flush_cache(void *ptr, size_t size)
378 if (is_vmalloc_addr(ptr))
379 flush_kernel_vmap_range(ptr, size);
382 static void persistent_memory_invalidate_cache(void *ptr, size_t size)
384 if (is_vmalloc_addr(ptr))
385 invalidate_kernel_vmap_range(ptr, size);
388 static struct wc_memory_superblock *sb(struct dm_writecache *wc)
390 return wc->memory_map;
393 static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
395 return &sb(wc)->entries[e->index];
398 static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
400 return (char *)wc->block_start + (e->index << wc->block_size_bits);
403 static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
405 return wc->start_sector + wc->metadata_sectors +
406 ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
409 static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
411 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
412 return e->original_sector;
414 return le64_to_cpu(memory_entry(wc, e)->original_sector);
418 static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
420 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
423 return le64_to_cpu(memory_entry(wc, e)->seq_count);
427 static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
429 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
432 pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
435 static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
436 uint64_t original_sector, uint64_t seq_count)
438 struct wc_memory_entry me;
439 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
440 e->original_sector = original_sector;
441 e->seq_count = seq_count;
443 me.original_sector = cpu_to_le64(original_sector);
444 me.seq_count = cpu_to_le64(seq_count);
445 pmem_assign(*memory_entry(wc, e), me);
448 #define writecache_error(wc, err, msg, arg...) \
450 if (!cmpxchg(&(wc)->error, 0, err)) \
452 wake_up(&(wc)->freelist_wait); \
455 #define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error)))
457 static void writecache_flush_all_metadata(struct dm_writecache *wc)
459 if (!WC_MODE_PMEM(wc))
460 memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
463 static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
465 if (!WC_MODE_PMEM(wc))
466 __set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
470 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
473 struct dm_writecache *wc;
478 static void writecache_notify_io(unsigned long error, void *context)
480 struct io_notify *endio = context;
482 if (unlikely(error != 0))
483 writecache_error(endio->wc, -EIO, "error writing metadata");
484 BUG_ON(atomic_read(&endio->count) <= 0);
485 if (atomic_dec_and_test(&endio->count))
489 static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
491 wait_event(wc->bio_in_progress_wait[direction],
492 !atomic_read(&wc->bio_in_progress[direction]));
495 static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
497 struct dm_io_region region;
498 struct dm_io_request req;
499 struct io_notify endio = {
501 COMPLETION_INITIALIZER_ONSTACK(endio.c),
504 unsigned bitmap_bits = wc->dirty_bitmap_size * 8;
509 i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i);
510 if (unlikely(i == bitmap_bits))
512 j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i);
514 region.bdev = wc->ssd_dev->bdev;
515 region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
516 region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
518 if (unlikely(region.sector >= wc->metadata_sectors))
520 if (unlikely(region.sector + region.count > wc->metadata_sectors))
521 region.count = wc->metadata_sectors - region.sector;
523 region.sector += wc->start_sector;
524 atomic_inc(&endio.count);
525 req.bi_op = REQ_OP_WRITE;
526 req.bi_op_flags = REQ_SYNC;
527 req.mem.type = DM_IO_VMA;
528 req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
529 req.client = wc->dm_io;
530 req.notify.fn = writecache_notify_io;
531 req.notify.context = &endio;
533 /* writing via async dm-io (implied by notify.fn above) won't return an error */
534 (void) dm_io(&req, 1, ®ion, NULL);
538 writecache_notify_io(0, &endio);
539 wait_for_completion_io(&endio.c);
542 writecache_wait_for_ios(wc, WRITE);
544 writecache_disk_flush(wc, wc->ssd_dev);
546 memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
549 static void ssd_commit_superblock(struct dm_writecache *wc)
552 struct dm_io_region region;
553 struct dm_io_request req;
555 region.bdev = wc->ssd_dev->bdev;
557 region.count = max(4096U, wc->block_size) >> SECTOR_SHIFT;
559 if (unlikely(region.sector + region.count > wc->metadata_sectors))
560 region.count = wc->metadata_sectors - region.sector;
562 region.sector += wc->start_sector;
564 req.bi_op = REQ_OP_WRITE;
565 req.bi_op_flags = REQ_SYNC | REQ_FUA;
566 req.mem.type = DM_IO_VMA;
567 req.mem.ptr.vma = (char *)wc->memory_map;
568 req.client = wc->dm_io;
569 req.notify.fn = NULL;
570 req.notify.context = NULL;
572 r = dm_io(&req, 1, ®ion, NULL);
574 writecache_error(wc, r, "error writing superblock");
577 static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
579 if (WC_MODE_PMEM(wc))
582 ssd_commit_flushed(wc, wait_for_ios);
585 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
588 struct dm_io_region region;
589 struct dm_io_request req;
591 region.bdev = dev->bdev;
594 req.bi_op = REQ_OP_WRITE;
595 req.bi_op_flags = REQ_PREFLUSH;
596 req.mem.type = DM_IO_KMEM;
597 req.mem.ptr.addr = NULL;
598 req.client = wc->dm_io;
599 req.notify.fn = NULL;
601 r = dm_io(&req, 1, ®ion, NULL);
603 writecache_error(wc, r, "error flushing metadata: %d", r);
606 #define WFE_RETURN_FOLLOWING 1
607 #define WFE_LOWEST_SEQ 2
609 static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
610 uint64_t block, int flags)
613 struct rb_node *node = wc->tree.rb_node;
619 e = container_of(node, struct wc_entry, rb_node);
620 if (read_original_sector(wc, e) == block)
623 node = (read_original_sector(wc, e) >= block ?
624 e->rb_node.rb_left : e->rb_node.rb_right);
625 if (unlikely(!node)) {
626 if (!(flags & WFE_RETURN_FOLLOWING))
628 if (read_original_sector(wc, e) >= block) {
631 node = rb_next(&e->rb_node);
634 e = container_of(node, struct wc_entry, rb_node);
642 if (flags & WFE_LOWEST_SEQ)
643 node = rb_prev(&e->rb_node);
645 node = rb_next(&e->rb_node);
648 e2 = container_of(node, struct wc_entry, rb_node);
649 if (read_original_sector(wc, e2) != block)
655 static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
658 struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
661 e = container_of(*node, struct wc_entry, rb_node);
662 parent = &e->rb_node;
663 if (read_original_sector(wc, e) > read_original_sector(wc, ins))
664 node = &parent->rb_left;
666 node = &parent->rb_right;
668 rb_link_node(&ins->rb_node, parent, node);
669 rb_insert_color(&ins->rb_node, &wc->tree);
670 list_add(&ins->lru, &wc->lru);
674 static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
677 rb_erase(&e->rb_node, &wc->tree);
680 static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
682 if (WC_MODE_SORT_FREELIST(wc)) {
683 struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
684 if (unlikely(!*node))
685 wc->current_free = e;
688 if (&e->rb_node < *node)
689 node = &parent->rb_left;
691 node = &parent->rb_right;
693 rb_link_node(&e->rb_node, parent, node);
694 rb_insert_color(&e->rb_node, &wc->freetree);
696 list_add_tail(&e->lru, &wc->freelist);
701 static inline void writecache_verify_watermark(struct dm_writecache *wc)
703 if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
704 queue_work(wc->writeback_wq, &wc->writeback_work);
707 static void writecache_max_age_timer(struct timer_list *t)
709 struct dm_writecache *wc = from_timer(wc, t, max_age_timer);
711 if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) {
712 queue_work(wc->writeback_wq, &wc->writeback_work);
713 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
717 static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector)
721 if (WC_MODE_SORT_FREELIST(wc)) {
722 struct rb_node *next;
723 if (unlikely(!wc->current_free))
725 e = wc->current_free;
726 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
728 next = rb_next(&e->rb_node);
729 rb_erase(&e->rb_node, &wc->freetree);
731 next = rb_first(&wc->freetree);
732 wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
734 if (unlikely(list_empty(&wc->freelist)))
736 e = container_of(wc->freelist.next, struct wc_entry, lru);
737 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
743 writecache_verify_watermark(wc);
748 static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
750 writecache_unlink(wc, e);
751 writecache_add_to_freelist(wc, e);
752 clear_seq_count(wc, e);
753 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
754 if (unlikely(waitqueue_active(&wc->freelist_wait)))
755 wake_up(&wc->freelist_wait);
758 static void writecache_wait_on_freelist(struct dm_writecache *wc)
762 prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
765 finish_wait(&wc->freelist_wait, &wait);
769 static void writecache_poison_lists(struct dm_writecache *wc)
772 * Catch incorrect access to these values while the device is suspended.
774 memset(&wc->tree, -1, sizeof wc->tree);
775 wc->lru.next = LIST_POISON1;
776 wc->lru.prev = LIST_POISON2;
777 wc->freelist.next = LIST_POISON1;
778 wc->freelist.prev = LIST_POISON2;
781 static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
783 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
784 if (WC_MODE_PMEM(wc))
785 writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
788 static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
790 return read_seq_count(wc, e) < wc->seq_count;
793 static void writecache_flush(struct dm_writecache *wc)
795 struct wc_entry *e, *e2;
796 bool need_flush_after_free;
798 wc->uncommitted_blocks = 0;
799 del_timer(&wc->autocommit_timer);
801 if (list_empty(&wc->lru))
804 e = container_of(wc->lru.next, struct wc_entry, lru);
805 if (writecache_entry_is_committed(wc, e)) {
806 if (wc->overwrote_committed) {
807 writecache_wait_for_ios(wc, WRITE);
808 writecache_disk_flush(wc, wc->ssd_dev);
809 wc->overwrote_committed = false;
814 writecache_flush_entry(wc, e);
815 if (unlikely(e->lru.next == &wc->lru))
817 e2 = container_of(e->lru.next, struct wc_entry, lru);
818 if (writecache_entry_is_committed(wc, e2))
823 writecache_commit_flushed(wc, true);
826 pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
827 if (WC_MODE_PMEM(wc))
828 writecache_commit_flushed(wc, false);
830 ssd_commit_superblock(wc);
832 wc->overwrote_committed = false;
834 need_flush_after_free = false;
836 /* Free another committed entry with lower seq-count */
837 struct rb_node *rb_node = rb_prev(&e->rb_node);
840 e2 = container_of(rb_node, struct wc_entry, rb_node);
841 if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
842 likely(!e2->write_in_progress)) {
843 writecache_free_entry(wc, e2);
844 need_flush_after_free = true;
847 if (unlikely(e->lru.prev == &wc->lru))
849 e = container_of(e->lru.prev, struct wc_entry, lru);
853 if (need_flush_after_free)
854 writecache_commit_flushed(wc, false);
857 static void writecache_flush_work(struct work_struct *work)
859 struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
862 writecache_flush(wc);
866 static void writecache_autocommit_timer(struct timer_list *t)
868 struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
869 if (!writecache_has_error(wc))
870 queue_work(wc->writeback_wq, &wc->flush_work);
873 static void writecache_schedule_autocommit(struct dm_writecache *wc)
875 if (!timer_pending(&wc->autocommit_timer))
876 mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
879 static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
882 bool discarded_something = false;
884 e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
888 while (read_original_sector(wc, e) < end) {
889 struct rb_node *node = rb_next(&e->rb_node);
891 if (likely(!e->write_in_progress)) {
892 if (!discarded_something) {
893 if (!WC_MODE_PMEM(wc)) {
894 writecache_wait_for_ios(wc, READ);
895 writecache_wait_for_ios(wc, WRITE);
897 discarded_something = true;
899 if (!writecache_entry_is_committed(wc, e))
900 wc->uncommitted_blocks--;
901 writecache_free_entry(wc, e);
907 e = container_of(node, struct wc_entry, rb_node);
910 if (discarded_something)
911 writecache_commit_flushed(wc, false);
914 static bool writecache_wait_for_writeback(struct dm_writecache *wc)
916 if (wc->writeback_size) {
917 writecache_wait_on_freelist(wc);
923 static void writecache_suspend(struct dm_target *ti)
925 struct dm_writecache *wc = ti->private;
926 bool flush_on_suspend;
928 del_timer_sync(&wc->autocommit_timer);
929 del_timer_sync(&wc->max_age_timer);
932 writecache_flush(wc);
933 flush_on_suspend = wc->flush_on_suspend;
934 if (flush_on_suspend) {
935 wc->flush_on_suspend = false;
937 queue_work(wc->writeback_wq, &wc->writeback_work);
941 drain_workqueue(wc->writeback_wq);
944 if (flush_on_suspend)
946 while (writecache_wait_for_writeback(wc));
948 if (WC_MODE_PMEM(wc))
949 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
951 writecache_poison_lists(wc);
956 static int writecache_alloc_entries(struct dm_writecache *wc)
962 wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
965 for (b = 0; b < wc->n_blocks; b++) {
966 struct wc_entry *e = &wc->entries[b];
968 e->write_in_progress = false;
975 static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors)
977 struct dm_io_region region;
978 struct dm_io_request req;
980 region.bdev = wc->ssd_dev->bdev;
981 region.sector = wc->start_sector;
982 region.count = n_sectors;
983 req.bi_op = REQ_OP_READ;
984 req.bi_op_flags = REQ_SYNC;
985 req.mem.type = DM_IO_VMA;
986 req.mem.ptr.vma = (char *)wc->memory_map;
987 req.client = wc->dm_io;
988 req.notify.fn = NULL;
990 return dm_io(&req, 1, ®ion, NULL);
993 static void writecache_resume(struct dm_target *ti)
995 struct dm_writecache *wc = ti->private;
997 bool need_flush = false;
1003 wc->data_device_sectors = bdev_nr_sectors(wc->dev->bdev);
1005 if (WC_MODE_PMEM(wc)) {
1006 persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
1008 r = writecache_read_metadata(wc, wc->metadata_sectors);
1010 size_t sb_entries_offset;
1011 writecache_error(wc, r, "unable to read metadata: %d", r);
1012 sb_entries_offset = offsetof(struct wc_memory_superblock, entries);
1013 memset((char *)wc->memory_map + sb_entries_offset, -1,
1014 (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset);
1019 INIT_LIST_HEAD(&wc->lru);
1020 if (WC_MODE_SORT_FREELIST(wc)) {
1021 wc->freetree = RB_ROOT;
1022 wc->current_free = NULL;
1024 INIT_LIST_HEAD(&wc->freelist);
1026 wc->freelist_size = 0;
1028 r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count,
1031 writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
1032 sb_seq_count = cpu_to_le64(0);
1034 wc->seq_count = le64_to_cpu(sb_seq_count);
1036 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
1037 for (b = 0; b < wc->n_blocks; b++) {
1038 struct wc_entry *e = &wc->entries[b];
1039 struct wc_memory_entry wme;
1040 if (writecache_has_error(wc)) {
1041 e->original_sector = -1;
1045 r = copy_mc_to_kernel(&wme, memory_entry(wc, e),
1046 sizeof(struct wc_memory_entry));
1048 writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
1049 (unsigned long)b, r);
1050 e->original_sector = -1;
1053 e->original_sector = le64_to_cpu(wme.original_sector);
1054 e->seq_count = le64_to_cpu(wme.seq_count);
1059 for (b = 0; b < wc->n_blocks; b++) {
1060 struct wc_entry *e = &wc->entries[b];
1061 if (!writecache_entry_is_committed(wc, e)) {
1062 if (read_seq_count(wc, e) != -1) {
1064 clear_seq_count(wc, e);
1067 writecache_add_to_freelist(wc, e);
1069 struct wc_entry *old;
1071 old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
1073 writecache_insert_entry(wc, e);
1075 if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
1076 writecache_error(wc, -EINVAL,
1077 "two identical entries, position %llu, sector %llu, sequence %llu",
1078 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
1079 (unsigned long long)read_seq_count(wc, e));
1081 if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
1084 writecache_free_entry(wc, old);
1085 writecache_insert_entry(wc, e);
1094 writecache_flush_all_metadata(wc);
1095 writecache_commit_flushed(wc, false);
1098 writecache_verify_watermark(wc);
1100 if (wc->max_age != MAX_AGE_UNSPECIFIED)
1101 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
1106 static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1112 if (dm_suspended(wc->ti)) {
1116 if (writecache_has_error(wc)) {
1121 writecache_flush(wc);
1122 wc->writeback_all++;
1123 queue_work(wc->writeback_wq, &wc->writeback_work);
1126 flush_workqueue(wc->writeback_wq);
1129 wc->writeback_all--;
1130 if (writecache_has_error(wc)) {
1139 static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1145 wc->flush_on_suspend = true;
1151 static void activate_cleaner(struct dm_writecache *wc)
1153 wc->flush_on_suspend = true;
1155 wc->freelist_high_watermark = wc->n_blocks;
1156 wc->freelist_low_watermark = wc->n_blocks;
1159 static int process_cleaner_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1165 activate_cleaner(wc);
1166 if (!dm_suspended(wc->ti))
1167 writecache_verify_watermark(wc);
1173 static int process_clear_stats_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1179 memset(&wc->stats, 0, sizeof wc->stats);
1185 static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
1186 char *result, unsigned maxlen)
1189 struct dm_writecache *wc = ti->private;
1191 if (!strcasecmp(argv[0], "flush"))
1192 r = process_flush_mesg(argc, argv, wc);
1193 else if (!strcasecmp(argv[0], "flush_on_suspend"))
1194 r = process_flush_on_suspend_mesg(argc, argv, wc);
1195 else if (!strcasecmp(argv[0], "cleaner"))
1196 r = process_cleaner_mesg(argc, argv, wc);
1197 else if (!strcasecmp(argv[0], "clear_stats"))
1198 r = process_clear_stats_mesg(argc, argv, wc);
1200 DMERR("unrecognised message received: %s", argv[0]);
1205 static void memcpy_flushcache_optimized(void *dest, void *source, size_t size)
1208 * clflushopt performs better with block size 1024, 2048, 4096
1209 * non-temporal stores perform better with block size 512
1211 * block size 512 1024 2048 4096
1212 * movnti 496 MB/s 642 MB/s 725 MB/s 744 MB/s
1213 * clflushopt 373 MB/s 688 MB/s 1.1 GB/s 1.2 GB/s
1215 * We see that movnti performs better for 512-byte blocks, and
1216 * clflushopt performs better for 1024-byte and larger blocks. So, we
1217 * prefer clflushopt for sizes >= 768.
1219 * NOTE: this happens to be the case now (with dm-writecache's single
1220 * threaded model) but re-evaluate this once memcpy_flushcache() is
1221 * enabled to use movdir64b which might invalidate this performance
1222 * advantage seen with cache-allocating-writes plus flushing.
1225 if (static_cpu_has(X86_FEATURE_CLFLUSHOPT) &&
1226 likely(boot_cpu_data.x86_clflush_size == 64) &&
1227 likely(size >= 768)) {
1229 memcpy((void *)dest, (void *)source, 64);
1230 clflushopt((void *)dest);
1234 } while (size >= 64);
1238 memcpy_flushcache(dest, source, size);
1241 static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
1245 int rw = bio_data_dir(bio);
1246 unsigned remaining_size = wc->block_size;
1249 struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
1250 buf = bvec_kmap_local(&bv);
1252 if (unlikely(size > remaining_size))
1253 size = remaining_size;
1257 r = copy_mc_to_kernel(buf, data, size);
1258 flush_dcache_page(bio_page(bio));
1260 writecache_error(wc, r, "hardware memory error when reading data: %d", r);
1261 bio->bi_status = BLK_STS_IOERR;
1264 flush_dcache_page(bio_page(bio));
1265 memcpy_flushcache_optimized(data, buf, size);
1270 data = (char *)data + size;
1271 remaining_size -= size;
1272 bio_advance(bio, size);
1273 } while (unlikely(remaining_size));
1276 static int writecache_flush_thread(void *data)
1278 struct dm_writecache *wc = data;
1284 bio = bio_list_pop(&wc->flush_list);
1286 set_current_state(TASK_INTERRUPTIBLE);
1289 if (unlikely(kthread_should_stop())) {
1290 set_current_state(TASK_RUNNING);
1298 if (bio_op(bio) == REQ_OP_DISCARD) {
1299 writecache_discard(wc, bio->bi_iter.bi_sector,
1300 bio_end_sector(bio));
1302 bio_set_dev(bio, wc->dev->bdev);
1303 submit_bio_noacct(bio);
1305 writecache_flush(wc);
1307 if (writecache_has_error(wc))
1308 bio->bi_status = BLK_STS_IOERR;
1316 static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
1318 if (bio_list_empty(&wc->flush_list))
1319 wake_up_process(wc->flush_thread);
1320 bio_list_add(&wc->flush_list, bio);
1326 WC_MAP_REMAP_ORIGIN,
1331 static enum wc_map_op writecache_map_remap_origin(struct dm_writecache *wc, struct bio *bio,
1335 sector_t next_boundary =
1336 read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1337 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT)
1338 dm_accept_partial_bio(bio, next_boundary);
1341 return WC_MAP_REMAP_ORIGIN;
1344 static enum wc_map_op writecache_map_read(struct dm_writecache *wc, struct bio *bio)
1346 enum wc_map_op map_op;
1351 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1352 if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1353 wc->stats.read_hits++;
1354 if (WC_MODE_PMEM(wc)) {
1355 bio_copy_block(wc, bio, memory_data(wc, e));
1356 if (bio->bi_iter.bi_size)
1357 goto read_next_block;
1358 map_op = WC_MAP_SUBMIT;
1360 dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1361 bio_set_dev(bio, wc->ssd_dev->bdev);
1362 bio->bi_iter.bi_sector = cache_sector(wc, e);
1363 if (!writecache_entry_is_committed(wc, e))
1364 writecache_wait_for_ios(wc, WRITE);
1365 map_op = WC_MAP_REMAP;
1368 map_op = writecache_map_remap_origin(wc, bio, e);
1374 static enum wc_map_op writecache_bio_copy_ssd(struct dm_writecache *wc, struct bio *bio,
1375 struct wc_entry *e, bool search_used)
1377 unsigned bio_size = wc->block_size;
1378 sector_t start_cache_sec = cache_sector(wc, e);
1379 sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT);
1381 while (bio_size < bio->bi_iter.bi_size) {
1383 struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec);
1386 write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector +
1387 (bio_size >> SECTOR_SHIFT), wc->seq_count);
1388 writecache_insert_entry(wc, f);
1389 wc->uncommitted_blocks++;
1392 struct rb_node *next = rb_next(&e->rb_node);
1395 f = container_of(next, struct wc_entry, rb_node);
1398 if (read_original_sector(wc, f) !=
1399 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1401 if (unlikely(f->write_in_progress))
1403 if (writecache_entry_is_committed(wc, f))
1404 wc->overwrote_committed = true;
1407 bio_size += wc->block_size;
1408 current_cache_sec += wc->block_size >> SECTOR_SHIFT;
1411 bio_set_dev(bio, wc->ssd_dev->bdev);
1412 bio->bi_iter.bi_sector = start_cache_sec;
1413 dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT);
1415 if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1416 wc->uncommitted_blocks = 0;
1417 queue_work(wc->writeback_wq, &wc->flush_work);
1419 writecache_schedule_autocommit(wc);
1422 return WC_MAP_REMAP;
1425 static enum wc_map_op writecache_map_write(struct dm_writecache *wc, struct bio *bio)
1430 bool found_entry = false;
1431 bool search_used = false;
1433 if (writecache_has_error(wc))
1434 return WC_MAP_ERROR;
1435 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1437 if (!writecache_entry_is_committed(wc, e)) {
1438 wc->stats.write_hits_uncommitted++;
1442 wc->stats.write_hits_committed++;
1443 if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1444 wc->overwrote_committed = true;
1450 if (unlikely(wc->cleaner) ||
1451 (wc->metadata_only && !(bio->bi_opf & REQ_META)))
1454 e = writecache_pop_from_freelist(wc, (sector_t)-1);
1456 if (!WC_MODE_PMEM(wc) && !found_entry) {
1458 wc->stats.writes_around++;
1459 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1460 return writecache_map_remap_origin(wc, bio, e);
1462 wc->stats.writes_blocked_on_freelist++;
1463 writecache_wait_on_freelist(wc);
1466 write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1467 writecache_insert_entry(wc, e);
1468 wc->uncommitted_blocks++;
1469 wc->stats.writes_allocate++;
1471 if (WC_MODE_PMEM(wc))
1472 bio_copy_block(wc, bio, memory_data(wc, e));
1474 return writecache_bio_copy_ssd(wc, bio, e, search_used);
1475 } while (bio->bi_iter.bi_size);
1477 if (unlikely(bio->bi_opf & REQ_FUA || wc->uncommitted_blocks >= wc->autocommit_blocks))
1478 writecache_flush(wc);
1480 writecache_schedule_autocommit(wc);
1482 return WC_MAP_SUBMIT;
1485 static enum wc_map_op writecache_map_flush(struct dm_writecache *wc, struct bio *bio)
1487 if (writecache_has_error(wc))
1488 return WC_MAP_ERROR;
1490 if (WC_MODE_PMEM(wc)) {
1491 wc->stats.flushes++;
1492 writecache_flush(wc);
1493 if (writecache_has_error(wc))
1494 return WC_MAP_ERROR;
1495 else if (unlikely(wc->cleaner) || unlikely(wc->metadata_only))
1496 return WC_MAP_REMAP_ORIGIN;
1497 return WC_MAP_SUBMIT;
1500 if (dm_bio_get_target_bio_nr(bio))
1501 return WC_MAP_REMAP_ORIGIN;
1502 wc->stats.flushes++;
1503 writecache_offload_bio(wc, bio);
1504 return WC_MAP_RETURN;
1507 static enum wc_map_op writecache_map_discard(struct dm_writecache *wc, struct bio *bio)
1509 wc->stats.discards++;
1511 if (writecache_has_error(wc))
1512 return WC_MAP_ERROR;
1514 if (WC_MODE_PMEM(wc)) {
1515 writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
1516 return WC_MAP_REMAP_ORIGIN;
1519 writecache_offload_bio(wc, bio);
1520 return WC_MAP_RETURN;
1523 static int writecache_map(struct dm_target *ti, struct bio *bio)
1525 struct dm_writecache *wc = ti->private;
1526 enum wc_map_op map_op;
1528 bio->bi_private = NULL;
1532 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1533 map_op = writecache_map_flush(wc, bio);
1537 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1539 if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
1540 (wc->block_size / 512 - 1)) != 0)) {
1541 DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
1542 (unsigned long long)bio->bi_iter.bi_sector,
1543 bio->bi_iter.bi_size, wc->block_size);
1544 map_op = WC_MAP_ERROR;
1548 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
1549 map_op = writecache_map_discard(wc, bio);
1553 if (bio_data_dir(bio) == READ)
1554 map_op = writecache_map_read(wc, bio);
1556 map_op = writecache_map_write(wc, bio);
1559 case WC_MAP_REMAP_ORIGIN:
1560 if (likely(wc->pause != 0)) {
1561 if (bio_op(bio) == REQ_OP_WRITE) {
1562 dm_iot_io_begin(&wc->iot, 1);
1563 bio->bi_private = (void *)2;
1566 bio_set_dev(bio, wc->dev->bdev);
1568 return DM_MAPIO_REMAPPED;
1571 /* make sure that writecache_end_io decrements bio_in_progress: */
1572 bio->bi_private = (void *)1;
1573 atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
1575 return DM_MAPIO_REMAPPED;
1580 return DM_MAPIO_SUBMITTED;
1584 return DM_MAPIO_SUBMITTED;
1589 return DM_MAPIO_SUBMITTED;
1597 static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
1599 struct dm_writecache *wc = ti->private;
1601 if (bio->bi_private == (void *)1) {
1602 int dir = bio_data_dir(bio);
1603 if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
1604 if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
1605 wake_up(&wc->bio_in_progress_wait[dir]);
1606 } else if (bio->bi_private == (void *)2) {
1607 dm_iot_io_end(&wc->iot, 1);
1612 static int writecache_iterate_devices(struct dm_target *ti,
1613 iterate_devices_callout_fn fn, void *data)
1615 struct dm_writecache *wc = ti->private;
1617 return fn(ti, wc->dev, 0, ti->len, data);
1620 static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
1622 struct dm_writecache *wc = ti->private;
1624 if (limits->logical_block_size < wc->block_size)
1625 limits->logical_block_size = wc->block_size;
1627 if (limits->physical_block_size < wc->block_size)
1628 limits->physical_block_size = wc->block_size;
1630 if (limits->io_min < wc->block_size)
1631 limits->io_min = wc->block_size;
1635 static void writecache_writeback_endio(struct bio *bio)
1637 struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
1638 struct dm_writecache *wc = wb->wc;
1639 unsigned long flags;
1641 raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
1642 if (unlikely(list_empty(&wc->endio_list)))
1643 wake_up_process(wc->endio_thread);
1644 list_add_tail(&wb->endio_entry, &wc->endio_list);
1645 raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
1648 static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
1650 struct copy_struct *c = ptr;
1651 struct dm_writecache *wc = c->wc;
1653 c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
1655 raw_spin_lock_irq(&wc->endio_list_lock);
1656 if (unlikely(list_empty(&wc->endio_list)))
1657 wake_up_process(wc->endio_thread);
1658 list_add_tail(&c->endio_entry, &wc->endio_list);
1659 raw_spin_unlock_irq(&wc->endio_list_lock);
1662 static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
1665 struct writeback_struct *wb;
1667 unsigned long n_walked = 0;
1670 wb = list_entry(list->next, struct writeback_struct, endio_entry);
1671 list_del(&wb->endio_entry);
1673 if (unlikely(wb->bio.bi_status != BLK_STS_OK))
1674 writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
1675 "write error %d", wb->bio.bi_status);
1679 BUG_ON(!e->write_in_progress);
1680 e->write_in_progress = false;
1681 INIT_LIST_HEAD(&e->lru);
1682 if (!writecache_has_error(wc))
1683 writecache_free_entry(wc, e);
1684 BUG_ON(!wc->writeback_size);
1685 wc->writeback_size--;
1687 if (unlikely(n_walked >= ENDIO_LATENCY)) {
1688 writecache_commit_flushed(wc, false);
1693 } while (++i < wb->wc_list_n);
1695 if (wb->wc_list != wb->wc_list_inline)
1698 } while (!list_empty(list));
1701 static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
1703 struct copy_struct *c;
1707 c = list_entry(list->next, struct copy_struct, endio_entry);
1708 list_del(&c->endio_entry);
1710 if (unlikely(c->error))
1711 writecache_error(wc, c->error, "copy error");
1715 BUG_ON(!e->write_in_progress);
1716 e->write_in_progress = false;
1717 INIT_LIST_HEAD(&e->lru);
1718 if (!writecache_has_error(wc))
1719 writecache_free_entry(wc, e);
1721 BUG_ON(!wc->writeback_size);
1722 wc->writeback_size--;
1724 } while (--c->n_entries);
1725 mempool_free(c, &wc->copy_pool);
1726 } while (!list_empty(list));
1729 static int writecache_endio_thread(void *data)
1731 struct dm_writecache *wc = data;
1734 struct list_head list;
1736 raw_spin_lock_irq(&wc->endio_list_lock);
1737 if (!list_empty(&wc->endio_list))
1739 set_current_state(TASK_INTERRUPTIBLE);
1740 raw_spin_unlock_irq(&wc->endio_list_lock);
1742 if (unlikely(kthread_should_stop())) {
1743 set_current_state(TASK_RUNNING);
1752 list = wc->endio_list;
1753 list.next->prev = list.prev->next = &list;
1754 INIT_LIST_HEAD(&wc->endio_list);
1755 raw_spin_unlock_irq(&wc->endio_list_lock);
1757 if (!WC_MODE_FUA(wc))
1758 writecache_disk_flush(wc, wc->dev);
1762 if (WC_MODE_PMEM(wc)) {
1763 __writecache_endio_pmem(wc, &list);
1765 __writecache_endio_ssd(wc, &list);
1766 writecache_wait_for_ios(wc, READ);
1769 writecache_commit_flushed(wc, false);
1777 static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e)
1779 struct dm_writecache *wc = wb->wc;
1780 unsigned block_size = wc->block_size;
1781 void *address = memory_data(wc, e);
1783 persistent_memory_flush_cache(address, block_size);
1785 if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors))
1788 return bio_add_page(&wb->bio, persistent_memory_page(address),
1789 block_size, persistent_memory_page_offset(address)) != 0;
1792 struct writeback_list {
1793 struct list_head list;
1797 static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
1799 if (unlikely(wc->max_writeback_jobs)) {
1800 if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
1802 while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
1803 writecache_wait_on_freelist(wc);
1810 static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
1812 struct wc_entry *e, *f;
1814 struct writeback_struct *wb;
1819 e = container_of(wbl->list.prev, struct wc_entry, lru);
1822 max_pages = e->wc_list_contiguous;
1824 bio = bio_alloc_bioset(GFP_NOIO, max_pages, &wc->bio_set);
1825 wb = container_of(bio, struct writeback_struct, bio);
1827 bio->bi_end_io = writecache_writeback_endio;
1828 bio_set_dev(bio, wc->dev->bdev);
1829 bio->bi_iter.bi_sector = read_original_sector(wc, e);
1830 if (max_pages <= WB_LIST_INLINE ||
1831 unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
1832 GFP_NOIO | __GFP_NORETRY |
1833 __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
1834 wb->wc_list = wb->wc_list_inline;
1835 max_pages = WB_LIST_INLINE;
1838 BUG_ON(!wc_add_block(wb, e));
1843 while (wbl->size && wb->wc_list_n < max_pages) {
1844 f = container_of(wbl->list.prev, struct wc_entry, lru);
1845 if (read_original_sector(wc, f) !=
1846 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1848 if (!wc_add_block(wb, f))
1852 wb->wc_list[wb->wc_list_n++] = f;
1855 bio_set_op_attrs(bio, REQ_OP_WRITE, WC_MODE_FUA(wc) * REQ_FUA);
1856 if (writecache_has_error(wc)) {
1857 bio->bi_status = BLK_STS_IOERR;
1859 } else if (unlikely(!bio_sectors(bio))) {
1860 bio->bi_status = BLK_STS_OK;
1866 __writeback_throttle(wc, wbl);
1870 static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
1872 struct wc_entry *e, *f;
1873 struct dm_io_region from, to;
1874 struct copy_struct *c;
1880 e = container_of(wbl->list.prev, struct wc_entry, lru);
1883 n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
1885 from.bdev = wc->ssd_dev->bdev;
1886 from.sector = cache_sector(wc, e);
1887 from.count = n_sectors;
1888 to.bdev = wc->dev->bdev;
1889 to.sector = read_original_sector(wc, e);
1890 to.count = n_sectors;
1892 c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
1895 c->n_entries = e->wc_list_contiguous;
1897 while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
1899 f = container_of(wbl->list.prev, struct wc_entry, lru);
1905 if (unlikely(to.sector + to.count > wc->data_device_sectors)) {
1906 if (to.sector >= wc->data_device_sectors) {
1907 writecache_copy_endio(0, 0, c);
1910 from.count = to.count = wc->data_device_sectors - to.sector;
1913 dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
1915 __writeback_throttle(wc, wbl);
1919 static void writecache_writeback(struct work_struct *work)
1921 struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
1922 struct blk_plug plug;
1923 struct wc_entry *f, *g, *e = NULL;
1924 struct rb_node *node, *next_node;
1925 struct list_head skipped;
1926 struct writeback_list wbl;
1927 unsigned long n_walked;
1929 if (!WC_MODE_PMEM(wc)) {
1930 /* Wait for any active kcopyd work on behalf of ssd writeback */
1931 dm_kcopyd_client_flush(wc->dm_kcopyd);
1934 if (likely(wc->pause != 0)) {
1937 if (unlikely(wc->cleaner) || unlikely(wc->writeback_all) ||
1938 unlikely(dm_suspended(wc->ti)))
1940 idle = dm_iot_idle_time(&wc->iot);
1941 if (idle >= wc->pause)
1943 idle = wc->pause - idle;
1946 schedule_timeout_idle(idle);
1952 if (writecache_has_error(wc)) {
1957 if (unlikely(wc->writeback_all)) {
1958 if (writecache_wait_for_writeback(wc))
1962 if (wc->overwrote_committed) {
1963 writecache_wait_for_ios(wc, WRITE);
1967 INIT_LIST_HEAD(&skipped);
1968 INIT_LIST_HEAD(&wbl.list);
1970 while (!list_empty(&wc->lru) &&
1971 (wc->writeback_all ||
1972 wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark ||
1973 (jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >=
1974 wc->max_age - wc->max_age / MAX_AGE_DIV))) {
1977 if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1978 likely(!wc->writeback_all)) {
1979 if (likely(!dm_suspended(wc->ti)))
1980 queue_work(wc->writeback_wq, &wc->writeback_work);
1984 if (unlikely(wc->writeback_all)) {
1986 writecache_flush(wc);
1987 e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
1991 e = container_of(wc->lru.prev, struct wc_entry, lru);
1992 BUG_ON(e->write_in_progress);
1993 if (unlikely(!writecache_entry_is_committed(wc, e))) {
1994 writecache_flush(wc);
1996 node = rb_prev(&e->rb_node);
1998 f = container_of(node, struct wc_entry, rb_node);
1999 if (unlikely(read_original_sector(wc, f) ==
2000 read_original_sector(wc, e))) {
2001 BUG_ON(!f->write_in_progress);
2002 list_move(&e->lru, &skipped);
2007 wc->writeback_size++;
2008 list_move(&e->lru, &wbl.list);
2010 e->write_in_progress = true;
2011 e->wc_list_contiguous = 1;
2016 next_node = rb_next(&f->rb_node);
2017 if (unlikely(!next_node))
2019 g = container_of(next_node, struct wc_entry, rb_node);
2020 if (unlikely(read_original_sector(wc, g) ==
2021 read_original_sector(wc, f))) {
2025 if (read_original_sector(wc, g) !=
2026 read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
2028 if (unlikely(g->write_in_progress))
2030 if (unlikely(!writecache_entry_is_committed(wc, g)))
2033 if (!WC_MODE_PMEM(wc)) {
2039 //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
2042 wc->writeback_size++;
2043 list_move(&g->lru, &wbl.list);
2045 g->write_in_progress = true;
2046 g->wc_list_contiguous = BIO_MAX_VECS;
2048 e->wc_list_contiguous++;
2049 if (unlikely(e->wc_list_contiguous == BIO_MAX_VECS)) {
2050 if (unlikely(wc->writeback_all)) {
2051 next_node = rb_next(&f->rb_node);
2052 if (likely(next_node))
2053 g = container_of(next_node, struct wc_entry, rb_node);
2061 if (!list_empty(&skipped)) {
2062 list_splice_tail(&skipped, &wc->lru);
2064 * If we didn't do any progress, we must wait until some
2065 * writeback finishes to avoid burning CPU in a loop
2067 if (unlikely(!wbl.size))
2068 writecache_wait_for_writeback(wc);
2073 blk_start_plug(&plug);
2075 if (WC_MODE_PMEM(wc))
2076 __writecache_writeback_pmem(wc, &wbl);
2078 __writecache_writeback_ssd(wc, &wbl);
2080 blk_finish_plug(&plug);
2082 if (unlikely(wc->writeback_all)) {
2084 while (writecache_wait_for_writeback(wc));
2089 static int calculate_memory_size(uint64_t device_size, unsigned block_size,
2090 size_t *n_blocks_p, size_t *n_metadata_blocks_p)
2092 uint64_t n_blocks, offset;
2095 n_blocks = device_size;
2096 do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
2101 /* Verify the following entries[n_blocks] won't overflow */
2102 if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
2103 sizeof(struct wc_memory_entry)))
2105 offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
2106 offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
2107 if (offset + n_blocks * block_size <= device_size)
2112 /* check if the bit field overflows */
2114 if (e.index != n_blocks)
2118 *n_blocks_p = n_blocks;
2119 if (n_metadata_blocks_p)
2120 *n_metadata_blocks_p = offset >> __ffs(block_size);
2124 static int init_memory(struct dm_writecache *wc)
2129 r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
2133 r = writecache_alloc_entries(wc);
2137 for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
2138 pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
2139 pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
2140 pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
2141 pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
2142 pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
2144 for (b = 0; b < wc->n_blocks; b++) {
2145 write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
2149 writecache_flush_all_metadata(wc);
2150 writecache_commit_flushed(wc, false);
2151 pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
2152 writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
2153 writecache_commit_flushed(wc, false);
2158 static void writecache_dtr(struct dm_target *ti)
2160 struct dm_writecache *wc = ti->private;
2165 if (wc->endio_thread)
2166 kthread_stop(wc->endio_thread);
2168 if (wc->flush_thread)
2169 kthread_stop(wc->flush_thread);
2171 bioset_exit(&wc->bio_set);
2173 mempool_exit(&wc->copy_pool);
2175 if (wc->writeback_wq)
2176 destroy_workqueue(wc->writeback_wq);
2179 dm_put_device(ti, wc->dev);
2182 dm_put_device(ti, wc->ssd_dev);
2186 if (wc->memory_map) {
2187 if (WC_MODE_PMEM(wc))
2188 persistent_memory_release(wc);
2190 vfree(wc->memory_map);
2194 dm_kcopyd_client_destroy(wc->dm_kcopyd);
2197 dm_io_client_destroy(wc->dm_io);
2199 vfree(wc->dirty_bitmap);
2204 static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2206 struct dm_writecache *wc;
2207 struct dm_arg_set as;
2209 unsigned opt_params;
2210 size_t offset, data_size;
2213 int high_wm_percent = HIGH_WATERMARK;
2214 int low_wm_percent = LOW_WATERMARK;
2216 struct wc_memory_superblock s;
2218 static struct dm_arg _args[] = {
2219 {0, 18, "Invalid number of feature args"},
2225 wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
2227 ti->error = "Cannot allocate writecache structure";
2234 mutex_init(&wc->lock);
2235 wc->max_age = MAX_AGE_UNSPECIFIED;
2236 writecache_poison_lists(wc);
2237 init_waitqueue_head(&wc->freelist_wait);
2238 timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
2239 timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0);
2241 for (i = 0; i < 2; i++) {
2242 atomic_set(&wc->bio_in_progress[i], 0);
2243 init_waitqueue_head(&wc->bio_in_progress_wait[i]);
2246 wc->dm_io = dm_io_client_create();
2247 if (IS_ERR(wc->dm_io)) {
2248 r = PTR_ERR(wc->dm_io);
2249 ti->error = "Unable to allocate dm-io client";
2254 wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
2255 if (!wc->writeback_wq) {
2257 ti->error = "Could not allocate writeback workqueue";
2260 INIT_WORK(&wc->writeback_work, writecache_writeback);
2261 INIT_WORK(&wc->flush_work, writecache_flush_work);
2263 dm_iot_init(&wc->iot);
2265 raw_spin_lock_init(&wc->endio_list_lock);
2266 INIT_LIST_HEAD(&wc->endio_list);
2267 wc->endio_thread = kthread_create(writecache_endio_thread, wc, "writecache_endio");
2268 if (IS_ERR(wc->endio_thread)) {
2269 r = PTR_ERR(wc->endio_thread);
2270 wc->endio_thread = NULL;
2271 ti->error = "Couldn't spawn endio thread";
2274 wake_up_process(wc->endio_thread);
2277 * Parse the mode (pmem or ssd)
2279 string = dm_shift_arg(&as);
2283 if (!strcasecmp(string, "s")) {
2284 wc->pmem_mode = false;
2285 } else if (!strcasecmp(string, "p")) {
2286 #ifdef DM_WRITECACHE_HAS_PMEM
2287 wc->pmem_mode = true;
2288 wc->writeback_fua = true;
2291 * If the architecture doesn't support persistent memory or
2292 * the kernel doesn't support any DAX drivers, this driver can
2293 * only be used in SSD-only mode.
2296 ti->error = "Persistent memory or DAX not supported on this system";
2303 if (WC_MODE_PMEM(wc)) {
2304 r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
2305 offsetof(struct writeback_struct, bio),
2308 ti->error = "Could not allocate bio set";
2312 wc->pause = PAUSE_WRITEBACK;
2313 r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
2315 ti->error = "Could not allocate mempool";
2321 * Parse the origin data device
2323 string = dm_shift_arg(&as);
2326 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
2328 ti->error = "Origin data device lookup failed";
2333 * Parse cache data device (be it pmem or ssd)
2335 string = dm_shift_arg(&as);
2339 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
2341 ti->error = "Cache data device lookup failed";
2344 wc->memory_map_size = i_size_read(wc->ssd_dev->bdev->bd_inode);
2347 * Parse the cache block size
2349 string = dm_shift_arg(&as);
2352 if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
2353 wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
2354 (wc->block_size & (wc->block_size - 1))) {
2356 ti->error = "Invalid block size";
2359 if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) ||
2360 wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) {
2362 ti->error = "Block size is smaller than device logical block size";
2365 wc->block_size_bits = __ffs(wc->block_size);
2367 wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
2368 wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
2369 wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
2372 * Parse optional arguments
2374 r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2378 while (opt_params) {
2379 string = dm_shift_arg(&as), opt_params--;
2380 if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2381 unsigned long long start_sector;
2382 string = dm_shift_arg(&as), opt_params--;
2383 if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2384 goto invalid_optional;
2385 wc->start_sector = start_sector;
2386 wc->start_sector_set = true;
2387 if (wc->start_sector != start_sector ||
2388 wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2389 goto invalid_optional;
2390 } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
2391 string = dm_shift_arg(&as), opt_params--;
2392 if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
2393 goto invalid_optional;
2394 if (high_wm_percent < 0 || high_wm_percent > 100)
2395 goto invalid_optional;
2396 wc->high_wm_percent_value = high_wm_percent;
2397 wc->high_wm_percent_set = true;
2398 } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
2399 string = dm_shift_arg(&as), opt_params--;
2400 if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
2401 goto invalid_optional;
2402 if (low_wm_percent < 0 || low_wm_percent > 100)
2403 goto invalid_optional;
2404 wc->low_wm_percent_value = low_wm_percent;
2405 wc->low_wm_percent_set = true;
2406 } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
2407 string = dm_shift_arg(&as), opt_params--;
2408 if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
2409 goto invalid_optional;
2410 wc->max_writeback_jobs_set = true;
2411 } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
2412 string = dm_shift_arg(&as), opt_params--;
2413 if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
2414 goto invalid_optional;
2415 wc->autocommit_blocks_set = true;
2416 } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
2417 unsigned autocommit_msecs;
2418 string = dm_shift_arg(&as), opt_params--;
2419 if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
2420 goto invalid_optional;
2421 if (autocommit_msecs > 3600000)
2422 goto invalid_optional;
2423 wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2424 wc->autocommit_time_value = autocommit_msecs;
2425 wc->autocommit_time_set = true;
2426 } else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
2427 unsigned max_age_msecs;
2428 string = dm_shift_arg(&as), opt_params--;
2429 if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
2430 goto invalid_optional;
2431 if (max_age_msecs > 86400000)
2432 goto invalid_optional;
2433 wc->max_age = msecs_to_jiffies(max_age_msecs);
2434 wc->max_age_set = true;
2435 wc->max_age_value = max_age_msecs;
2436 } else if (!strcasecmp(string, "cleaner")) {
2437 wc->cleaner_set = true;
2439 } else if (!strcasecmp(string, "fua")) {
2440 if (WC_MODE_PMEM(wc)) {
2441 wc->writeback_fua = true;
2442 wc->writeback_fua_set = true;
2443 } else goto invalid_optional;
2444 } else if (!strcasecmp(string, "nofua")) {
2445 if (WC_MODE_PMEM(wc)) {
2446 wc->writeback_fua = false;
2447 wc->writeback_fua_set = true;
2448 } else goto invalid_optional;
2449 } else if (!strcasecmp(string, "metadata_only")) {
2450 wc->metadata_only = true;
2451 } else if (!strcasecmp(string, "pause_writeback") && opt_params >= 1) {
2452 unsigned pause_msecs;
2453 if (WC_MODE_PMEM(wc))
2454 goto invalid_optional;
2455 string = dm_shift_arg(&as), opt_params--;
2456 if (sscanf(string, "%u%c", &pause_msecs, &dummy) != 1)
2457 goto invalid_optional;
2458 if (pause_msecs > 60000)
2459 goto invalid_optional;
2460 wc->pause = msecs_to_jiffies(pause_msecs);
2461 wc->pause_set = true;
2462 wc->pause_value = pause_msecs;
2466 ti->error = "Invalid optional argument";
2471 if (high_wm_percent < low_wm_percent) {
2473 ti->error = "High watermark must be greater than or equal to low watermark";
2477 if (WC_MODE_PMEM(wc)) {
2478 if (!dax_synchronous(wc->ssd_dev->dax_dev)) {
2480 ti->error = "Asynchronous persistent memory not supported as pmem cache";
2484 r = persistent_memory_claim(wc);
2486 ti->error = "Unable to map persistent memory for cache";
2490 size_t n_blocks, n_metadata_blocks;
2491 uint64_t n_bitmap_bits;
2493 wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2495 bio_list_init(&wc->flush_list);
2496 wc->flush_thread = kthread_create(writecache_flush_thread, wc, "dm_writecache_flush");
2497 if (IS_ERR(wc->flush_thread)) {
2498 r = PTR_ERR(wc->flush_thread);
2499 wc->flush_thread = NULL;
2500 ti->error = "Couldn't spawn flush thread";
2503 wake_up_process(wc->flush_thread);
2505 r = calculate_memory_size(wc->memory_map_size, wc->block_size,
2506 &n_blocks, &n_metadata_blocks);
2508 ti->error = "Invalid device size";
2512 n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
2513 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
2514 /* this is limitation of test_bit functions */
2515 if (n_bitmap_bits > 1U << 31) {
2517 ti->error = "Invalid device size";
2521 wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
2522 if (!wc->memory_map) {
2524 ti->error = "Unable to allocate memory for metadata";
2528 wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2529 if (IS_ERR(wc->dm_kcopyd)) {
2530 r = PTR_ERR(wc->dm_kcopyd);
2531 ti->error = "Unable to allocate dm-kcopyd client";
2532 wc->dm_kcopyd = NULL;
2536 wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
2537 wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
2538 BITS_PER_LONG * sizeof(unsigned long);
2539 wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
2540 if (!wc->dirty_bitmap) {
2542 ti->error = "Unable to allocate dirty bitmap";
2546 r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT);
2548 ti->error = "Unable to read first block of metadata";
2553 r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock));
2555 ti->error = "Hardware memory error when reading superblock";
2558 if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
2559 r = init_memory(wc);
2561 ti->error = "Unable to initialize device";
2564 r = copy_mc_to_kernel(&s, sb(wc),
2565 sizeof(struct wc_memory_superblock));
2567 ti->error = "Hardware memory error when reading superblock";
2572 if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
2573 ti->error = "Invalid magic in the superblock";
2578 if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
2579 ti->error = "Invalid version in the superblock";
2584 if (le32_to_cpu(s.block_size) != wc->block_size) {
2585 ti->error = "Block size does not match superblock";
2590 wc->n_blocks = le64_to_cpu(s.n_blocks);
2592 offset = wc->n_blocks * sizeof(struct wc_memory_entry);
2593 if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
2595 ti->error = "Overflow in size calculation";
2599 offset += sizeof(struct wc_memory_superblock);
2600 if (offset < sizeof(struct wc_memory_superblock))
2602 offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
2603 data_size = wc->n_blocks * (size_t)wc->block_size;
2604 if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
2605 (offset + data_size < offset))
2607 if (offset + data_size > wc->memory_map_size) {
2608 ti->error = "Memory area is too small";
2613 wc->metadata_sectors = offset >> SECTOR_SHIFT;
2614 wc->block_start = (char *)sb(wc) + offset;
2616 x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
2619 wc->freelist_high_watermark = x;
2620 x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
2623 wc->freelist_low_watermark = x;
2626 activate_cleaner(wc);
2628 r = writecache_alloc_entries(wc);
2630 ti->error = "Cannot allocate memory";
2634 ti->num_flush_bios = WC_MODE_PMEM(wc) ? 1 : 2;
2635 ti->flush_supported = true;
2636 ti->num_discard_bios = 1;
2638 if (WC_MODE_PMEM(wc))
2639 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
2645 ti->error = "Bad arguments";
2651 static void writecache_status(struct dm_target *ti, status_type_t type,
2652 unsigned status_flags, char *result, unsigned maxlen)
2654 struct dm_writecache *wc = ti->private;
2655 unsigned extra_args;
2659 case STATUSTYPE_INFO:
2660 DMEMIT("%ld %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
2661 writecache_has_error(wc),
2662 (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2663 (unsigned long long)wc->writeback_size,
2665 wc->stats.read_hits,
2667 wc->stats.write_hits_uncommitted,
2668 wc->stats.write_hits_committed,
2669 wc->stats.writes_around,
2670 wc->stats.writes_allocate,
2671 wc->stats.writes_blocked_on_freelist,
2673 wc->stats.discards);
2675 case STATUSTYPE_TABLE:
2676 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
2677 wc->dev->name, wc->ssd_dev->name, wc->block_size);
2679 if (wc->start_sector_set)
2681 if (wc->high_wm_percent_set)
2683 if (wc->low_wm_percent_set)
2685 if (wc->max_writeback_jobs_set)
2687 if (wc->autocommit_blocks_set)
2689 if (wc->autocommit_time_set)
2691 if (wc->max_age_set)
2693 if (wc->cleaner_set)
2695 if (wc->writeback_fua_set)
2697 if (wc->metadata_only)
2702 DMEMIT("%u", extra_args);
2703 if (wc->start_sector_set)
2704 DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2705 if (wc->high_wm_percent_set)
2706 DMEMIT(" high_watermark %u", wc->high_wm_percent_value);
2707 if (wc->low_wm_percent_set)
2708 DMEMIT(" low_watermark %u", wc->low_wm_percent_value);
2709 if (wc->max_writeback_jobs_set)
2710 DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
2711 if (wc->autocommit_blocks_set)
2712 DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
2713 if (wc->autocommit_time_set)
2714 DMEMIT(" autocommit_time %u", wc->autocommit_time_value);
2715 if (wc->max_age_set)
2716 DMEMIT(" max_age %u", wc->max_age_value);
2717 if (wc->cleaner_set)
2719 if (wc->writeback_fua_set)
2720 DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2721 if (wc->metadata_only)
2722 DMEMIT(" metadata_only");
2724 DMEMIT(" pause_writeback %u", wc->pause_value);
2726 case STATUSTYPE_IMA:
2732 static struct target_type writecache_target = {
2733 .name = "writecache",
2734 .version = {1, 6, 0},
2735 .module = THIS_MODULE,
2736 .ctr = writecache_ctr,
2737 .dtr = writecache_dtr,
2738 .status = writecache_status,
2739 .postsuspend = writecache_suspend,
2740 .resume = writecache_resume,
2741 .message = writecache_message,
2742 .map = writecache_map,
2743 .end_io = writecache_end_io,
2744 .iterate_devices = writecache_iterate_devices,
2745 .io_hints = writecache_io_hints,
2748 static int __init dm_writecache_init(void)
2752 r = dm_register_target(&writecache_target);
2754 DMERR("register failed %d", r);
2761 static void __exit dm_writecache_exit(void)
2763 dm_unregister_target(&writecache_target);
2766 module_init(dm_writecache_init);
2767 module_exit(dm_writecache_exit);
2769 MODULE_DESCRIPTION(DM_NAME " writecache target");
2770 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2771 MODULE_LICENSE("GPL");