Linux 4.19.133
[linux/fpc-iii.git] / drivers / md / dm-writecache.c
blobcc028353f9d5594a7cf0766407671be8e561efb1
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3 * Copyright (C) 2018 Red Hat. All rights reserved.
5 * This file is released under the GPL.
6 */
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
34 #endif
36 #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_DAX_DRIVER)
37 #define DM_WRITECACHE_HAS_PMEM
38 #endif
40 #ifdef DM_WRITECACHE_HAS_PMEM
41 #define pmem_assign(dest, src) \
42 do { \
43 typeof(dest) uniq = (src); \
44 memcpy_flushcache(&(dest), &uniq, sizeof(dest)); \
45 } while (0)
46 #else
47 #define pmem_assign(dest, src) ((dest) = (src))
48 #endif
50 #if defined(__HAVE_ARCH_MEMCPY_MCSAFE) && defined(DM_WRITECACHE_HAS_PMEM)
51 #define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
52 #endif
54 #define MEMORY_SUPERBLOCK_MAGIC 0x23489321
55 #define MEMORY_SUPERBLOCK_VERSION 1
57 struct wc_memory_entry {
58 __le64 original_sector;
59 __le64 seq_count;
62 struct wc_memory_superblock {
63 union {
64 struct {
65 __le32 magic;
66 __le32 version;
67 __le32 block_size;
68 __le32 pad;
69 __le64 n_blocks;
70 __le64 seq_count;
72 __le64 padding[8];
74 struct wc_memory_entry entries[0];
77 struct wc_entry {
78 struct rb_node rb_node;
79 struct list_head lru;
80 unsigned short wc_list_contiguous;
81 bool write_in_progress
82 #if BITS_PER_LONG == 64
84 #endif
86 unsigned long index
87 #if BITS_PER_LONG == 64
88 :47
89 #endif
91 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
92 uint64_t original_sector;
93 uint64_t seq_count;
94 #endif
97 #ifdef DM_WRITECACHE_HAS_PMEM
98 #define WC_MODE_PMEM(wc) ((wc)->pmem_mode)
99 #define WC_MODE_FUA(wc) ((wc)->writeback_fua)
100 #else
101 #define WC_MODE_PMEM(wc) false
102 #define WC_MODE_FUA(wc) false
103 #endif
104 #define WC_MODE_SORT_FREELIST(wc) (!WC_MODE_PMEM(wc))
106 struct dm_writecache {
107 struct mutex lock;
108 struct list_head lru;
109 union {
110 struct list_head freelist;
111 struct {
112 struct rb_root freetree;
113 struct wc_entry *current_free;
116 struct rb_root tree;
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;
127 int error;
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;
137 struct dm_dev *dev;
138 struct dm_dev *ssd_dev;
139 sector_t start_sector;
140 void *memory_map;
141 uint64_t memory_map_size;
142 size_t metadata_sectors;
143 size_t n_blocks;
144 uint64_t seq_count;
145 void *block_start;
146 struct wc_entry *entries;
147 unsigned block_size;
148 unsigned char block_size_bits;
150 bool pmem_mode:1;
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;
183 mempool_t copy_pool;
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;
192 unsigned wc_list_n;
193 unsigned page_offset;
194 struct page *page;
195 struct wc_entry *wc_list_inline[WB_LIST_INLINE];
196 struct bio bio;
199 struct copy_struct {
200 struct list_head endio_entry;
201 struct dm_writecache *wc;
202 struct wc_entry *e;
203 unsigned n_entries;
204 int error;
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)
223 int r;
224 loff_t s;
225 long p, da;
226 pfn_t pfn;
227 int id;
228 struct page **pages;
230 wc->memory_vmapped = false;
232 if (!wc->ssd_dev->dax_dev) {
233 r = -EOPNOTSUPP;
234 goto err1;
236 s = wc->memory_map_size;
237 p = s >> PAGE_SHIFT;
238 if (!p) {
239 r = -EINVAL;
240 goto err1;
242 if (p != s >> PAGE_SHIFT) {
243 r = -EOVERFLOW;
244 goto err1;
247 id = dax_read_lock();
249 da = dax_direct_access(wc->ssd_dev->dax_dev, 0, p, &wc->memory_map, &pfn);
250 if (da < 0) {
251 wc->memory_map = NULL;
252 r = da;
253 goto err2;
255 if (!pfn_t_has_page(pfn)) {
256 wc->memory_map = NULL;
257 r = -EOPNOTSUPP;
258 goto err2;
260 if (da != p) {
261 long i;
262 wc->memory_map = NULL;
263 pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
264 if (!pages) {
265 r = -ENOMEM;
266 goto err2;
268 i = 0;
269 do {
270 long daa;
271 daa = dax_direct_access(wc->ssd_dev->dax_dev, i, p - i,
272 NULL, &pfn);
273 if (daa <= 0) {
274 r = daa ? daa : -EINVAL;
275 goto err3;
277 if (!pfn_t_has_page(pfn)) {
278 r = -EOPNOTSUPP;
279 goto err3;
281 while (daa-- && i < p) {
282 pages[i++] = pfn_t_to_page(pfn);
283 pfn.val++;
284 if (!(i & 15))
285 cond_resched();
287 } while (i < p);
288 wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
289 if (!wc->memory_map) {
290 r = -ENOMEM;
291 goto err3;
293 kvfree(pages);
294 wc->memory_vmapped = true;
297 dax_read_unlock(id);
299 wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
300 wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
302 return 0;
303 err3:
304 kvfree(pages);
305 err2:
306 dax_read_unlock(id);
307 err1:
308 return r;
310 #else
311 static int persistent_memory_claim(struct dm_writecache *wc)
313 BUG();
315 #endif
317 static void persistent_memory_release(struct dm_writecache *wc)
319 if (wc->memory_vmapped)
320 vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
323 static struct page *persistent_memory_page(void *addr)
325 if (is_vmalloc_addr(addr))
326 return vmalloc_to_page(addr);
327 else
328 return virt_to_page(addr);
331 static unsigned persistent_memory_page_offset(void *addr)
333 return (unsigned long)addr & (PAGE_SIZE - 1);
336 static void persistent_memory_flush_cache(void *ptr, size_t size)
338 if (is_vmalloc_addr(ptr))
339 flush_kernel_vmap_range(ptr, size);
342 static void persistent_memory_invalidate_cache(void *ptr, size_t size)
344 if (is_vmalloc_addr(ptr))
345 invalidate_kernel_vmap_range(ptr, size);
348 static struct wc_memory_superblock *sb(struct dm_writecache *wc)
350 return wc->memory_map;
353 static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
355 if (is_power_of_2(sizeof(struct wc_entry)) && 0)
356 return &sb(wc)->entries[e - wc->entries];
357 else
358 return &sb(wc)->entries[e->index];
361 static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
363 return (char *)wc->block_start + (e->index << wc->block_size_bits);
366 static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
368 return wc->start_sector + wc->metadata_sectors +
369 ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
372 static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
374 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
375 return e->original_sector;
376 #else
377 return le64_to_cpu(memory_entry(wc, e)->original_sector);
378 #endif
381 static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
383 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
384 return e->seq_count;
385 #else
386 return le64_to_cpu(memory_entry(wc, e)->seq_count);
387 #endif
390 static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
392 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
393 e->seq_count = -1;
394 #endif
395 pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
398 static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
399 uint64_t original_sector, uint64_t seq_count)
401 struct wc_memory_entry me;
402 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
403 e->original_sector = original_sector;
404 e->seq_count = seq_count;
405 #endif
406 me.original_sector = cpu_to_le64(original_sector);
407 me.seq_count = cpu_to_le64(seq_count);
408 pmem_assign(*memory_entry(wc, e), me);
411 #define writecache_error(wc, err, msg, arg...) \
412 do { \
413 if (!cmpxchg(&(wc)->error, 0, err)) \
414 DMERR(msg, ##arg); \
415 wake_up(&(wc)->freelist_wait); \
416 } while (0)
418 #define writecache_has_error(wc) (unlikely(READ_ONCE((wc)->error)))
420 static void writecache_flush_all_metadata(struct dm_writecache *wc)
422 if (!WC_MODE_PMEM(wc))
423 memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
426 static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
428 if (!WC_MODE_PMEM(wc))
429 __set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
430 wc->dirty_bitmap);
433 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
435 struct io_notify {
436 struct dm_writecache *wc;
437 struct completion c;
438 atomic_t count;
441 static void writecache_notify_io(unsigned long error, void *context)
443 struct io_notify *endio = context;
445 if (unlikely(error != 0))
446 writecache_error(endio->wc, -EIO, "error writing metadata");
447 BUG_ON(atomic_read(&endio->count) <= 0);
448 if (atomic_dec_and_test(&endio->count))
449 complete(&endio->c);
452 static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
454 wait_event(wc->bio_in_progress_wait[direction],
455 !atomic_read(&wc->bio_in_progress[direction]));
458 static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
460 struct dm_io_region region;
461 struct dm_io_request req;
462 struct io_notify endio = {
464 COMPLETION_INITIALIZER_ONSTACK(endio.c),
465 ATOMIC_INIT(1),
467 unsigned bitmap_bits = wc->dirty_bitmap_size * 8;
468 unsigned i = 0;
470 while (1) {
471 unsigned j;
472 i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i);
473 if (unlikely(i == bitmap_bits))
474 break;
475 j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i);
477 region.bdev = wc->ssd_dev->bdev;
478 region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
479 region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
481 if (unlikely(region.sector >= wc->metadata_sectors))
482 break;
483 if (unlikely(region.sector + region.count > wc->metadata_sectors))
484 region.count = wc->metadata_sectors - region.sector;
486 region.sector += wc->start_sector;
487 atomic_inc(&endio.count);
488 req.bi_op = REQ_OP_WRITE;
489 req.bi_op_flags = REQ_SYNC;
490 req.mem.type = DM_IO_VMA;
491 req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
492 req.client = wc->dm_io;
493 req.notify.fn = writecache_notify_io;
494 req.notify.context = &endio;
496 /* writing via async dm-io (implied by notify.fn above) won't return an error */
497 (void) dm_io(&req, 1, &region, NULL);
498 i = j;
501 writecache_notify_io(0, &endio);
502 wait_for_completion_io(&endio.c);
504 if (wait_for_ios)
505 writecache_wait_for_ios(wc, WRITE);
507 writecache_disk_flush(wc, wc->ssd_dev);
509 memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
512 static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
514 if (WC_MODE_PMEM(wc))
515 wmb();
516 else
517 ssd_commit_flushed(wc, wait_for_ios);
520 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
522 int r;
523 struct dm_io_region region;
524 struct dm_io_request req;
526 region.bdev = dev->bdev;
527 region.sector = 0;
528 region.count = 0;
529 req.bi_op = REQ_OP_WRITE;
530 req.bi_op_flags = REQ_PREFLUSH;
531 req.mem.type = DM_IO_KMEM;
532 req.mem.ptr.addr = NULL;
533 req.client = wc->dm_io;
534 req.notify.fn = NULL;
536 r = dm_io(&req, 1, &region, NULL);
537 if (unlikely(r))
538 writecache_error(wc, r, "error flushing metadata: %d", r);
541 #define WFE_RETURN_FOLLOWING 1
542 #define WFE_LOWEST_SEQ 2
544 static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
545 uint64_t block, int flags)
547 struct wc_entry *e;
548 struct rb_node *node = wc->tree.rb_node;
550 if (unlikely(!node))
551 return NULL;
553 while (1) {
554 e = container_of(node, struct wc_entry, rb_node);
555 if (read_original_sector(wc, e) == block)
556 break;
557 node = (read_original_sector(wc, e) >= block ?
558 e->rb_node.rb_left : e->rb_node.rb_right);
559 if (unlikely(!node)) {
560 if (!(flags & WFE_RETURN_FOLLOWING)) {
561 return NULL;
563 if (read_original_sector(wc, e) >= block) {
564 break;
565 } else {
566 node = rb_next(&e->rb_node);
567 if (unlikely(!node)) {
568 return NULL;
570 e = container_of(node, struct wc_entry, rb_node);
571 break;
576 while (1) {
577 struct wc_entry *e2;
578 if (flags & WFE_LOWEST_SEQ)
579 node = rb_prev(&e->rb_node);
580 else
581 node = rb_next(&e->rb_node);
582 if (!node)
583 return e;
584 e2 = container_of(node, struct wc_entry, rb_node);
585 if (read_original_sector(wc, e2) != block)
586 return e;
587 e = e2;
591 static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
593 struct wc_entry *e;
594 struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
596 while (*node) {
597 e = container_of(*node, struct wc_entry, rb_node);
598 parent = &e->rb_node;
599 if (read_original_sector(wc, e) > read_original_sector(wc, ins))
600 node = &parent->rb_left;
601 else
602 node = &parent->rb_right;
604 rb_link_node(&ins->rb_node, parent, node);
605 rb_insert_color(&ins->rb_node, &wc->tree);
606 list_add(&ins->lru, &wc->lru);
609 static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
611 list_del(&e->lru);
612 rb_erase(&e->rb_node, &wc->tree);
615 static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
617 if (WC_MODE_SORT_FREELIST(wc)) {
618 struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
619 if (unlikely(!*node))
620 wc->current_free = e;
621 while (*node) {
622 parent = *node;
623 if (&e->rb_node < *node)
624 node = &parent->rb_left;
625 else
626 node = &parent->rb_right;
628 rb_link_node(&e->rb_node, parent, node);
629 rb_insert_color(&e->rb_node, &wc->freetree);
630 } else {
631 list_add_tail(&e->lru, &wc->freelist);
633 wc->freelist_size++;
636 static inline void writecache_verify_watermark(struct dm_writecache *wc)
638 if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
639 queue_work(wc->writeback_wq, &wc->writeback_work);
642 static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc)
644 struct wc_entry *e;
646 if (WC_MODE_SORT_FREELIST(wc)) {
647 struct rb_node *next;
648 if (unlikely(!wc->current_free))
649 return NULL;
650 e = wc->current_free;
651 next = rb_next(&e->rb_node);
652 rb_erase(&e->rb_node, &wc->freetree);
653 if (unlikely(!next))
654 next = rb_first(&wc->freetree);
655 wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
656 } else {
657 if (unlikely(list_empty(&wc->freelist)))
658 return NULL;
659 e = container_of(wc->freelist.next, struct wc_entry, lru);
660 list_del(&e->lru);
662 wc->freelist_size--;
664 writecache_verify_watermark(wc);
666 return e;
669 static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
671 writecache_unlink(wc, e);
672 writecache_add_to_freelist(wc, e);
673 clear_seq_count(wc, e);
674 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
675 if (unlikely(waitqueue_active(&wc->freelist_wait)))
676 wake_up(&wc->freelist_wait);
679 static void writecache_wait_on_freelist(struct dm_writecache *wc)
681 DEFINE_WAIT(wait);
683 prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
684 wc_unlock(wc);
685 io_schedule();
686 finish_wait(&wc->freelist_wait, &wait);
687 wc_lock(wc);
690 static void writecache_poison_lists(struct dm_writecache *wc)
693 * Catch incorrect access to these values while the device is suspended.
695 memset(&wc->tree, -1, sizeof wc->tree);
696 wc->lru.next = LIST_POISON1;
697 wc->lru.prev = LIST_POISON2;
698 wc->freelist.next = LIST_POISON1;
699 wc->freelist.prev = LIST_POISON2;
702 static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
704 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
705 if (WC_MODE_PMEM(wc))
706 writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
709 static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
711 return read_seq_count(wc, e) < wc->seq_count;
714 static void writecache_flush(struct dm_writecache *wc)
716 struct wc_entry *e, *e2;
717 bool need_flush_after_free;
719 wc->uncommitted_blocks = 0;
720 del_timer(&wc->autocommit_timer);
722 if (list_empty(&wc->lru))
723 return;
725 e = container_of(wc->lru.next, struct wc_entry, lru);
726 if (writecache_entry_is_committed(wc, e)) {
727 if (wc->overwrote_committed) {
728 writecache_wait_for_ios(wc, WRITE);
729 writecache_disk_flush(wc, wc->ssd_dev);
730 wc->overwrote_committed = false;
732 return;
734 while (1) {
735 writecache_flush_entry(wc, e);
736 if (unlikely(e->lru.next == &wc->lru))
737 break;
738 e2 = container_of(e->lru.next, struct wc_entry, lru);
739 if (writecache_entry_is_committed(wc, e2))
740 break;
741 e = e2;
742 cond_resched();
744 writecache_commit_flushed(wc, true);
746 wc->seq_count++;
747 pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
748 writecache_flush_region(wc, &sb(wc)->seq_count, sizeof sb(wc)->seq_count);
749 writecache_commit_flushed(wc, false);
751 wc->overwrote_committed = false;
753 need_flush_after_free = false;
754 while (1) {
755 /* Free another committed entry with lower seq-count */
756 struct rb_node *rb_node = rb_prev(&e->rb_node);
758 if (rb_node) {
759 e2 = container_of(rb_node, struct wc_entry, rb_node);
760 if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
761 likely(!e2->write_in_progress)) {
762 writecache_free_entry(wc, e2);
763 need_flush_after_free = true;
766 if (unlikely(e->lru.prev == &wc->lru))
767 break;
768 e = container_of(e->lru.prev, struct wc_entry, lru);
769 cond_resched();
772 if (need_flush_after_free)
773 writecache_commit_flushed(wc, false);
776 static void writecache_flush_work(struct work_struct *work)
778 struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
780 wc_lock(wc);
781 writecache_flush(wc);
782 wc_unlock(wc);
785 static void writecache_autocommit_timer(struct timer_list *t)
787 struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
788 if (!writecache_has_error(wc))
789 queue_work(wc->writeback_wq, &wc->flush_work);
792 static void writecache_schedule_autocommit(struct dm_writecache *wc)
794 if (!timer_pending(&wc->autocommit_timer))
795 mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
798 static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
800 struct wc_entry *e;
801 bool discarded_something = false;
803 e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
804 if (unlikely(!e))
805 return;
807 while (read_original_sector(wc, e) < end) {
808 struct rb_node *node = rb_next(&e->rb_node);
810 if (likely(!e->write_in_progress)) {
811 if (!discarded_something) {
812 writecache_wait_for_ios(wc, READ);
813 writecache_wait_for_ios(wc, WRITE);
814 discarded_something = true;
816 if (!writecache_entry_is_committed(wc, e))
817 wc->uncommitted_blocks--;
818 writecache_free_entry(wc, e);
821 if (!node)
822 break;
824 e = container_of(node, struct wc_entry, rb_node);
827 if (discarded_something)
828 writecache_commit_flushed(wc, false);
831 static bool writecache_wait_for_writeback(struct dm_writecache *wc)
833 if (wc->writeback_size) {
834 writecache_wait_on_freelist(wc);
835 return true;
837 return false;
840 static void writecache_suspend(struct dm_target *ti)
842 struct dm_writecache *wc = ti->private;
843 bool flush_on_suspend;
845 del_timer_sync(&wc->autocommit_timer);
847 wc_lock(wc);
848 writecache_flush(wc);
849 flush_on_suspend = wc->flush_on_suspend;
850 if (flush_on_suspend) {
851 wc->flush_on_suspend = false;
852 wc->writeback_all++;
853 queue_work(wc->writeback_wq, &wc->writeback_work);
855 wc_unlock(wc);
857 drain_workqueue(wc->writeback_wq);
859 wc_lock(wc);
860 if (flush_on_suspend)
861 wc->writeback_all--;
862 while (writecache_wait_for_writeback(wc));
864 if (WC_MODE_PMEM(wc))
865 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
867 writecache_poison_lists(wc);
869 wc_unlock(wc);
872 static int writecache_alloc_entries(struct dm_writecache *wc)
874 size_t b;
876 if (wc->entries)
877 return 0;
878 wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
879 if (!wc->entries)
880 return -ENOMEM;
881 for (b = 0; b < wc->n_blocks; b++) {
882 struct wc_entry *e = &wc->entries[b];
883 e->index = b;
884 e->write_in_progress = false;
885 cond_resched();
888 return 0;
891 static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors)
893 struct dm_io_region region;
894 struct dm_io_request req;
896 region.bdev = wc->ssd_dev->bdev;
897 region.sector = wc->start_sector;
898 region.count = n_sectors;
899 req.bi_op = REQ_OP_READ;
900 req.bi_op_flags = REQ_SYNC;
901 req.mem.type = DM_IO_VMA;
902 req.mem.ptr.vma = (char *)wc->memory_map;
903 req.client = wc->dm_io;
904 req.notify.fn = NULL;
906 return dm_io(&req, 1, &region, NULL);
909 static void writecache_resume(struct dm_target *ti)
911 struct dm_writecache *wc = ti->private;
912 size_t b;
913 bool need_flush = false;
914 __le64 sb_seq_count;
915 int r;
917 wc_lock(wc);
919 if (WC_MODE_PMEM(wc)) {
920 persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
921 } else {
922 r = writecache_read_metadata(wc, wc->metadata_sectors);
923 if (r) {
924 size_t sb_entries_offset;
925 writecache_error(wc, r, "unable to read metadata: %d", r);
926 sb_entries_offset = offsetof(struct wc_memory_superblock, entries);
927 memset((char *)wc->memory_map + sb_entries_offset, -1,
928 (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset);
932 wc->tree = RB_ROOT;
933 INIT_LIST_HEAD(&wc->lru);
934 if (WC_MODE_SORT_FREELIST(wc)) {
935 wc->freetree = RB_ROOT;
936 wc->current_free = NULL;
937 } else {
938 INIT_LIST_HEAD(&wc->freelist);
940 wc->freelist_size = 0;
942 r = memcpy_mcsafe(&sb_seq_count, &sb(wc)->seq_count, sizeof(uint64_t));
943 if (r) {
944 writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
945 sb_seq_count = cpu_to_le64(0);
947 wc->seq_count = le64_to_cpu(sb_seq_count);
949 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
950 for (b = 0; b < wc->n_blocks; b++) {
951 struct wc_entry *e = &wc->entries[b];
952 struct wc_memory_entry wme;
953 if (writecache_has_error(wc)) {
954 e->original_sector = -1;
955 e->seq_count = -1;
956 continue;
958 r = memcpy_mcsafe(&wme, memory_entry(wc, e), sizeof(struct wc_memory_entry));
959 if (r) {
960 writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
961 (unsigned long)b, r);
962 e->original_sector = -1;
963 e->seq_count = -1;
964 } else {
965 e->original_sector = le64_to_cpu(wme.original_sector);
966 e->seq_count = le64_to_cpu(wme.seq_count);
968 cond_resched();
970 #endif
971 for (b = 0; b < wc->n_blocks; b++) {
972 struct wc_entry *e = &wc->entries[b];
973 if (!writecache_entry_is_committed(wc, e)) {
974 if (read_seq_count(wc, e) != -1) {
975 erase_this:
976 clear_seq_count(wc, e);
977 need_flush = true;
979 writecache_add_to_freelist(wc, e);
980 } else {
981 struct wc_entry *old;
983 old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
984 if (!old) {
985 writecache_insert_entry(wc, e);
986 } else {
987 if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
988 writecache_error(wc, -EINVAL,
989 "two identical entries, position %llu, sector %llu, sequence %llu",
990 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
991 (unsigned long long)read_seq_count(wc, e));
993 if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
994 goto erase_this;
995 } else {
996 writecache_free_entry(wc, old);
997 writecache_insert_entry(wc, e);
998 need_flush = true;
1002 cond_resched();
1005 if (need_flush) {
1006 writecache_flush_all_metadata(wc);
1007 writecache_commit_flushed(wc, false);
1010 writecache_verify_watermark(wc);
1012 wc_unlock(wc);
1015 static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1017 if (argc != 1)
1018 return -EINVAL;
1020 wc_lock(wc);
1021 if (dm_suspended(wc->ti)) {
1022 wc_unlock(wc);
1023 return -EBUSY;
1025 if (writecache_has_error(wc)) {
1026 wc_unlock(wc);
1027 return -EIO;
1030 writecache_flush(wc);
1031 wc->writeback_all++;
1032 queue_work(wc->writeback_wq, &wc->writeback_work);
1033 wc_unlock(wc);
1035 flush_workqueue(wc->writeback_wq);
1037 wc_lock(wc);
1038 wc->writeback_all--;
1039 if (writecache_has_error(wc)) {
1040 wc_unlock(wc);
1041 return -EIO;
1043 wc_unlock(wc);
1045 return 0;
1048 static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1050 if (argc != 1)
1051 return -EINVAL;
1053 wc_lock(wc);
1054 wc->flush_on_suspend = true;
1055 wc_unlock(wc);
1057 return 0;
1060 static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
1061 char *result, unsigned maxlen)
1063 int r = -EINVAL;
1064 struct dm_writecache *wc = ti->private;
1066 if (!strcasecmp(argv[0], "flush"))
1067 r = process_flush_mesg(argc, argv, wc);
1068 else if (!strcasecmp(argv[0], "flush_on_suspend"))
1069 r = process_flush_on_suspend_mesg(argc, argv, wc);
1070 else
1071 DMERR("unrecognised message received: %s", argv[0]);
1073 return r;
1076 static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
1078 void *buf;
1079 unsigned long flags;
1080 unsigned size;
1081 int rw = bio_data_dir(bio);
1082 unsigned remaining_size = wc->block_size;
1084 do {
1085 struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
1086 buf = bvec_kmap_irq(&bv, &flags);
1087 size = bv.bv_len;
1088 if (unlikely(size > remaining_size))
1089 size = remaining_size;
1091 if (rw == READ) {
1092 int r;
1093 r = memcpy_mcsafe(buf, data, size);
1094 flush_dcache_page(bio_page(bio));
1095 if (unlikely(r)) {
1096 writecache_error(wc, r, "hardware memory error when reading data: %d", r);
1097 bio->bi_status = BLK_STS_IOERR;
1099 } else {
1100 flush_dcache_page(bio_page(bio));
1101 memcpy_flushcache(data, buf, size);
1104 bvec_kunmap_irq(buf, &flags);
1106 data = (char *)data + size;
1107 remaining_size -= size;
1108 bio_advance(bio, size);
1109 } while (unlikely(remaining_size));
1112 static int writecache_flush_thread(void *data)
1114 struct dm_writecache *wc = data;
1116 while (1) {
1117 struct bio *bio;
1119 wc_lock(wc);
1120 bio = bio_list_pop(&wc->flush_list);
1121 if (!bio) {
1122 set_current_state(TASK_INTERRUPTIBLE);
1123 wc_unlock(wc);
1125 if (unlikely(kthread_should_stop())) {
1126 set_current_state(TASK_RUNNING);
1127 break;
1130 schedule();
1131 continue;
1134 if (bio_op(bio) == REQ_OP_DISCARD) {
1135 writecache_discard(wc, bio->bi_iter.bi_sector,
1136 bio_end_sector(bio));
1137 wc_unlock(wc);
1138 bio_set_dev(bio, wc->dev->bdev);
1139 generic_make_request(bio);
1140 } else {
1141 writecache_flush(wc);
1142 wc_unlock(wc);
1143 if (writecache_has_error(wc))
1144 bio->bi_status = BLK_STS_IOERR;
1145 bio_endio(bio);
1149 return 0;
1152 static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
1154 if (bio_list_empty(&wc->flush_list))
1155 wake_up_process(wc->flush_thread);
1156 bio_list_add(&wc->flush_list, bio);
1159 static int writecache_map(struct dm_target *ti, struct bio *bio)
1161 struct wc_entry *e;
1162 struct dm_writecache *wc = ti->private;
1164 bio->bi_private = NULL;
1166 wc_lock(wc);
1168 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1169 if (writecache_has_error(wc))
1170 goto unlock_error;
1171 if (WC_MODE_PMEM(wc)) {
1172 writecache_flush(wc);
1173 if (writecache_has_error(wc))
1174 goto unlock_error;
1175 goto unlock_submit;
1176 } else {
1177 writecache_offload_bio(wc, bio);
1178 goto unlock_return;
1182 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1184 if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
1185 (wc->block_size / 512 - 1)) != 0)) {
1186 DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
1187 (unsigned long long)bio->bi_iter.bi_sector,
1188 bio->bi_iter.bi_size, wc->block_size);
1189 goto unlock_error;
1192 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
1193 if (writecache_has_error(wc))
1194 goto unlock_error;
1195 if (WC_MODE_PMEM(wc)) {
1196 writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
1197 goto unlock_remap_origin;
1198 } else {
1199 writecache_offload_bio(wc, bio);
1200 goto unlock_return;
1204 if (bio_data_dir(bio) == READ) {
1205 read_next_block:
1206 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1207 if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1208 if (WC_MODE_PMEM(wc)) {
1209 bio_copy_block(wc, bio, memory_data(wc, e));
1210 if (bio->bi_iter.bi_size)
1211 goto read_next_block;
1212 goto unlock_submit;
1213 } else {
1214 dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1215 bio_set_dev(bio, wc->ssd_dev->bdev);
1216 bio->bi_iter.bi_sector = cache_sector(wc, e);
1217 if (!writecache_entry_is_committed(wc, e))
1218 writecache_wait_for_ios(wc, WRITE);
1219 goto unlock_remap;
1221 } else {
1222 if (e) {
1223 sector_t next_boundary =
1224 read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1225 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
1226 dm_accept_partial_bio(bio, next_boundary);
1229 goto unlock_remap_origin;
1231 } else {
1232 do {
1233 if (writecache_has_error(wc))
1234 goto unlock_error;
1235 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1236 if (e) {
1237 if (!writecache_entry_is_committed(wc, e))
1238 goto bio_copy;
1239 if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1240 wc->overwrote_committed = true;
1241 goto bio_copy;
1244 e = writecache_pop_from_freelist(wc);
1245 if (unlikely(!e)) {
1246 writecache_wait_on_freelist(wc);
1247 continue;
1249 write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1250 writecache_insert_entry(wc, e);
1251 wc->uncommitted_blocks++;
1252 bio_copy:
1253 if (WC_MODE_PMEM(wc)) {
1254 bio_copy_block(wc, bio, memory_data(wc, e));
1255 } else {
1256 dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1257 bio_set_dev(bio, wc->ssd_dev->bdev);
1258 bio->bi_iter.bi_sector = cache_sector(wc, e);
1259 if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1260 wc->uncommitted_blocks = 0;
1261 queue_work(wc->writeback_wq, &wc->flush_work);
1262 } else {
1263 writecache_schedule_autocommit(wc);
1265 goto unlock_remap;
1267 } while (bio->bi_iter.bi_size);
1269 if (unlikely(bio->bi_opf & REQ_FUA ||
1270 wc->uncommitted_blocks >= wc->autocommit_blocks))
1271 writecache_flush(wc);
1272 else
1273 writecache_schedule_autocommit(wc);
1274 goto unlock_submit;
1277 unlock_remap_origin:
1278 bio_set_dev(bio, wc->dev->bdev);
1279 wc_unlock(wc);
1280 return DM_MAPIO_REMAPPED;
1282 unlock_remap:
1283 /* make sure that writecache_end_io decrements bio_in_progress: */
1284 bio->bi_private = (void *)1;
1285 atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
1286 wc_unlock(wc);
1287 return DM_MAPIO_REMAPPED;
1289 unlock_submit:
1290 wc_unlock(wc);
1291 bio_endio(bio);
1292 return DM_MAPIO_SUBMITTED;
1294 unlock_return:
1295 wc_unlock(wc);
1296 return DM_MAPIO_SUBMITTED;
1298 unlock_error:
1299 wc_unlock(wc);
1300 bio_io_error(bio);
1301 return DM_MAPIO_SUBMITTED;
1304 static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
1306 struct dm_writecache *wc = ti->private;
1308 if (bio->bi_private != NULL) {
1309 int dir = bio_data_dir(bio);
1310 if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
1311 if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
1312 wake_up(&wc->bio_in_progress_wait[dir]);
1314 return 0;
1317 static int writecache_iterate_devices(struct dm_target *ti,
1318 iterate_devices_callout_fn fn, void *data)
1320 struct dm_writecache *wc = ti->private;
1322 return fn(ti, wc->dev, 0, ti->len, data);
1325 static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
1327 struct dm_writecache *wc = ti->private;
1329 if (limits->logical_block_size < wc->block_size)
1330 limits->logical_block_size = wc->block_size;
1332 if (limits->physical_block_size < wc->block_size)
1333 limits->physical_block_size = wc->block_size;
1335 if (limits->io_min < wc->block_size)
1336 limits->io_min = wc->block_size;
1340 static void writecache_writeback_endio(struct bio *bio)
1342 struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
1343 struct dm_writecache *wc = wb->wc;
1344 unsigned long flags;
1346 raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
1347 if (unlikely(list_empty(&wc->endio_list)))
1348 wake_up_process(wc->endio_thread);
1349 list_add_tail(&wb->endio_entry, &wc->endio_list);
1350 raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
1353 static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
1355 struct copy_struct *c = ptr;
1356 struct dm_writecache *wc = c->wc;
1358 c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
1360 raw_spin_lock_irq(&wc->endio_list_lock);
1361 if (unlikely(list_empty(&wc->endio_list)))
1362 wake_up_process(wc->endio_thread);
1363 list_add_tail(&c->endio_entry, &wc->endio_list);
1364 raw_spin_unlock_irq(&wc->endio_list_lock);
1367 static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
1369 unsigned i;
1370 struct writeback_struct *wb;
1371 struct wc_entry *e;
1372 unsigned long n_walked = 0;
1374 do {
1375 wb = list_entry(list->next, struct writeback_struct, endio_entry);
1376 list_del(&wb->endio_entry);
1378 if (unlikely(wb->bio.bi_status != BLK_STS_OK))
1379 writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
1380 "write error %d", wb->bio.bi_status);
1381 i = 0;
1382 do {
1383 e = wb->wc_list[i];
1384 BUG_ON(!e->write_in_progress);
1385 e->write_in_progress = false;
1386 INIT_LIST_HEAD(&e->lru);
1387 if (!writecache_has_error(wc))
1388 writecache_free_entry(wc, e);
1389 BUG_ON(!wc->writeback_size);
1390 wc->writeback_size--;
1391 n_walked++;
1392 if (unlikely(n_walked >= ENDIO_LATENCY)) {
1393 writecache_commit_flushed(wc, false);
1394 wc_unlock(wc);
1395 wc_lock(wc);
1396 n_walked = 0;
1398 } while (++i < wb->wc_list_n);
1400 if (wb->wc_list != wb->wc_list_inline)
1401 kfree(wb->wc_list);
1402 bio_put(&wb->bio);
1403 } while (!list_empty(list));
1406 static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
1408 struct copy_struct *c;
1409 struct wc_entry *e;
1411 do {
1412 c = list_entry(list->next, struct copy_struct, endio_entry);
1413 list_del(&c->endio_entry);
1415 if (unlikely(c->error))
1416 writecache_error(wc, c->error, "copy error");
1418 e = c->e;
1419 do {
1420 BUG_ON(!e->write_in_progress);
1421 e->write_in_progress = false;
1422 INIT_LIST_HEAD(&e->lru);
1423 if (!writecache_has_error(wc))
1424 writecache_free_entry(wc, e);
1426 BUG_ON(!wc->writeback_size);
1427 wc->writeback_size--;
1428 e++;
1429 } while (--c->n_entries);
1430 mempool_free(c, &wc->copy_pool);
1431 } while (!list_empty(list));
1434 static int writecache_endio_thread(void *data)
1436 struct dm_writecache *wc = data;
1438 while (1) {
1439 struct list_head list;
1441 raw_spin_lock_irq(&wc->endio_list_lock);
1442 if (!list_empty(&wc->endio_list))
1443 goto pop_from_list;
1444 set_current_state(TASK_INTERRUPTIBLE);
1445 raw_spin_unlock_irq(&wc->endio_list_lock);
1447 if (unlikely(kthread_should_stop())) {
1448 set_current_state(TASK_RUNNING);
1449 break;
1452 schedule();
1454 continue;
1456 pop_from_list:
1457 list = wc->endio_list;
1458 list.next->prev = list.prev->next = &list;
1459 INIT_LIST_HEAD(&wc->endio_list);
1460 raw_spin_unlock_irq(&wc->endio_list_lock);
1462 if (!WC_MODE_FUA(wc))
1463 writecache_disk_flush(wc, wc->dev);
1465 wc_lock(wc);
1467 if (WC_MODE_PMEM(wc)) {
1468 __writecache_endio_pmem(wc, &list);
1469 } else {
1470 __writecache_endio_ssd(wc, &list);
1471 writecache_wait_for_ios(wc, READ);
1474 writecache_commit_flushed(wc, false);
1476 wc_unlock(wc);
1479 return 0;
1482 static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e, gfp_t gfp)
1484 struct dm_writecache *wc = wb->wc;
1485 unsigned block_size = wc->block_size;
1486 void *address = memory_data(wc, e);
1488 persistent_memory_flush_cache(address, block_size);
1489 return bio_add_page(&wb->bio, persistent_memory_page(address),
1490 block_size, persistent_memory_page_offset(address)) != 0;
1493 struct writeback_list {
1494 struct list_head list;
1495 size_t size;
1498 static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
1500 if (unlikely(wc->max_writeback_jobs)) {
1501 if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
1502 wc_lock(wc);
1503 while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
1504 writecache_wait_on_freelist(wc);
1505 wc_unlock(wc);
1508 cond_resched();
1511 static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
1513 struct wc_entry *e, *f;
1514 struct bio *bio;
1515 struct writeback_struct *wb;
1516 unsigned max_pages;
1518 while (wbl->size) {
1519 wbl->size--;
1520 e = container_of(wbl->list.prev, struct wc_entry, lru);
1521 list_del(&e->lru);
1523 max_pages = e->wc_list_contiguous;
1525 bio = bio_alloc_bioset(GFP_NOIO, max_pages, &wc->bio_set);
1526 wb = container_of(bio, struct writeback_struct, bio);
1527 wb->wc = wc;
1528 wb->bio.bi_end_io = writecache_writeback_endio;
1529 bio_set_dev(&wb->bio, wc->dev->bdev);
1530 wb->bio.bi_iter.bi_sector = read_original_sector(wc, e);
1531 wb->page_offset = PAGE_SIZE;
1532 if (max_pages <= WB_LIST_INLINE ||
1533 unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
1534 GFP_NOIO | __GFP_NORETRY |
1535 __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
1536 wb->wc_list = wb->wc_list_inline;
1537 max_pages = WB_LIST_INLINE;
1540 BUG_ON(!wc_add_block(wb, e, GFP_NOIO));
1542 wb->wc_list[0] = e;
1543 wb->wc_list_n = 1;
1545 while (wbl->size && wb->wc_list_n < max_pages) {
1546 f = container_of(wbl->list.prev, struct wc_entry, lru);
1547 if (read_original_sector(wc, f) !=
1548 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1549 break;
1550 if (!wc_add_block(wb, f, GFP_NOWAIT | __GFP_NOWARN))
1551 break;
1552 wbl->size--;
1553 list_del(&f->lru);
1554 wb->wc_list[wb->wc_list_n++] = f;
1555 e = f;
1557 bio_set_op_attrs(&wb->bio, REQ_OP_WRITE, WC_MODE_FUA(wc) * REQ_FUA);
1558 if (writecache_has_error(wc)) {
1559 bio->bi_status = BLK_STS_IOERR;
1560 bio_endio(&wb->bio);
1561 } else {
1562 submit_bio(&wb->bio);
1565 __writeback_throttle(wc, wbl);
1569 static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
1571 struct wc_entry *e, *f;
1572 struct dm_io_region from, to;
1573 struct copy_struct *c;
1575 while (wbl->size) {
1576 unsigned n_sectors;
1578 wbl->size--;
1579 e = container_of(wbl->list.prev, struct wc_entry, lru);
1580 list_del(&e->lru);
1582 n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
1584 from.bdev = wc->ssd_dev->bdev;
1585 from.sector = cache_sector(wc, e);
1586 from.count = n_sectors;
1587 to.bdev = wc->dev->bdev;
1588 to.sector = read_original_sector(wc, e);
1589 to.count = n_sectors;
1591 c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
1592 c->wc = wc;
1593 c->e = e;
1594 c->n_entries = e->wc_list_contiguous;
1596 while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
1597 wbl->size--;
1598 f = container_of(wbl->list.prev, struct wc_entry, lru);
1599 BUG_ON(f != e + 1);
1600 list_del(&f->lru);
1601 e = f;
1604 dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
1606 __writeback_throttle(wc, wbl);
1610 static void writecache_writeback(struct work_struct *work)
1612 struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
1613 struct blk_plug plug;
1614 struct wc_entry *e, *f, *g;
1615 struct rb_node *node, *next_node;
1616 struct list_head skipped;
1617 struct writeback_list wbl;
1618 unsigned long n_walked;
1620 wc_lock(wc);
1621 restart:
1622 if (writecache_has_error(wc)) {
1623 wc_unlock(wc);
1624 return;
1627 if (unlikely(wc->writeback_all)) {
1628 if (writecache_wait_for_writeback(wc))
1629 goto restart;
1632 if (wc->overwrote_committed) {
1633 writecache_wait_for_ios(wc, WRITE);
1636 n_walked = 0;
1637 INIT_LIST_HEAD(&skipped);
1638 INIT_LIST_HEAD(&wbl.list);
1639 wbl.size = 0;
1640 while (!list_empty(&wc->lru) &&
1641 (wc->writeback_all ||
1642 wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark)) {
1644 n_walked++;
1645 if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1646 likely(!wc->writeback_all) && likely(!dm_suspended(wc->ti))) {
1647 queue_work(wc->writeback_wq, &wc->writeback_work);
1648 break;
1651 e = container_of(wc->lru.prev, struct wc_entry, lru);
1652 BUG_ON(e->write_in_progress);
1653 if (unlikely(!writecache_entry_is_committed(wc, e))) {
1654 writecache_flush(wc);
1656 node = rb_prev(&e->rb_node);
1657 if (node) {
1658 f = container_of(node, struct wc_entry, rb_node);
1659 if (unlikely(read_original_sector(wc, f) ==
1660 read_original_sector(wc, e))) {
1661 BUG_ON(!f->write_in_progress);
1662 list_del(&e->lru);
1663 list_add(&e->lru, &skipped);
1664 cond_resched();
1665 continue;
1668 wc->writeback_size++;
1669 list_del(&e->lru);
1670 list_add(&e->lru, &wbl.list);
1671 wbl.size++;
1672 e->write_in_progress = true;
1673 e->wc_list_contiguous = 1;
1675 f = e;
1677 while (1) {
1678 next_node = rb_next(&f->rb_node);
1679 if (unlikely(!next_node))
1680 break;
1681 g = container_of(next_node, struct wc_entry, rb_node);
1682 if (read_original_sector(wc, g) ==
1683 read_original_sector(wc, f)) {
1684 f = g;
1685 continue;
1687 if (read_original_sector(wc, g) !=
1688 read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
1689 break;
1690 if (unlikely(g->write_in_progress))
1691 break;
1692 if (unlikely(!writecache_entry_is_committed(wc, g)))
1693 break;
1695 if (!WC_MODE_PMEM(wc)) {
1696 if (g != f + 1)
1697 break;
1700 n_walked++;
1701 //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
1702 // break;
1704 wc->writeback_size++;
1705 list_del(&g->lru);
1706 list_add(&g->lru, &wbl.list);
1707 wbl.size++;
1708 g->write_in_progress = true;
1709 g->wc_list_contiguous = BIO_MAX_PAGES;
1710 f = g;
1711 e->wc_list_contiguous++;
1712 if (unlikely(e->wc_list_contiguous == BIO_MAX_PAGES))
1713 break;
1715 cond_resched();
1718 if (!list_empty(&skipped)) {
1719 list_splice_tail(&skipped, &wc->lru);
1721 * If we didn't do any progress, we must wait until some
1722 * writeback finishes to avoid burning CPU in a loop
1724 if (unlikely(!wbl.size))
1725 writecache_wait_for_writeback(wc);
1728 wc_unlock(wc);
1730 blk_start_plug(&plug);
1732 if (WC_MODE_PMEM(wc))
1733 __writecache_writeback_pmem(wc, &wbl);
1734 else
1735 __writecache_writeback_ssd(wc, &wbl);
1737 blk_finish_plug(&plug);
1739 if (unlikely(wc->writeback_all)) {
1740 wc_lock(wc);
1741 while (writecache_wait_for_writeback(wc));
1742 wc_unlock(wc);
1746 static int calculate_memory_size(uint64_t device_size, unsigned block_size,
1747 size_t *n_blocks_p, size_t *n_metadata_blocks_p)
1749 uint64_t n_blocks, offset;
1750 struct wc_entry e;
1752 n_blocks = device_size;
1753 do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
1755 while (1) {
1756 if (!n_blocks)
1757 return -ENOSPC;
1758 /* Verify the following entries[n_blocks] won't overflow */
1759 if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
1760 sizeof(struct wc_memory_entry)))
1761 return -EFBIG;
1762 offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
1763 offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
1764 if (offset + n_blocks * block_size <= device_size)
1765 break;
1766 n_blocks--;
1769 /* check if the bit field overflows */
1770 e.index = n_blocks;
1771 if (e.index != n_blocks)
1772 return -EFBIG;
1774 if (n_blocks_p)
1775 *n_blocks_p = n_blocks;
1776 if (n_metadata_blocks_p)
1777 *n_metadata_blocks_p = offset >> __ffs(block_size);
1778 return 0;
1781 static int init_memory(struct dm_writecache *wc)
1783 size_t b;
1784 int r;
1786 r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
1787 if (r)
1788 return r;
1790 r = writecache_alloc_entries(wc);
1791 if (r)
1792 return r;
1794 for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
1795 pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
1796 pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
1797 pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
1798 pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
1799 pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
1801 for (b = 0; b < wc->n_blocks; b++) {
1802 write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
1803 cond_resched();
1806 writecache_flush_all_metadata(wc);
1807 writecache_commit_flushed(wc, false);
1808 pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
1809 writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
1810 writecache_commit_flushed(wc, false);
1812 return 0;
1815 static void writecache_dtr(struct dm_target *ti)
1817 struct dm_writecache *wc = ti->private;
1819 if (!wc)
1820 return;
1822 if (wc->endio_thread)
1823 kthread_stop(wc->endio_thread);
1825 if (wc->flush_thread)
1826 kthread_stop(wc->flush_thread);
1828 bioset_exit(&wc->bio_set);
1830 mempool_exit(&wc->copy_pool);
1832 if (wc->writeback_wq)
1833 destroy_workqueue(wc->writeback_wq);
1835 if (wc->dev)
1836 dm_put_device(ti, wc->dev);
1838 if (wc->ssd_dev)
1839 dm_put_device(ti, wc->ssd_dev);
1841 if (wc->entries)
1842 vfree(wc->entries);
1844 if (wc->memory_map) {
1845 if (WC_MODE_PMEM(wc))
1846 persistent_memory_release(wc);
1847 else
1848 vfree(wc->memory_map);
1851 if (wc->dm_kcopyd)
1852 dm_kcopyd_client_destroy(wc->dm_kcopyd);
1854 if (wc->dm_io)
1855 dm_io_client_destroy(wc->dm_io);
1857 if (wc->dirty_bitmap)
1858 vfree(wc->dirty_bitmap);
1860 kfree(wc);
1863 static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
1865 struct dm_writecache *wc;
1866 struct dm_arg_set as;
1867 const char *string;
1868 unsigned opt_params;
1869 size_t offset, data_size;
1870 int i, r;
1871 char dummy;
1872 int high_wm_percent = HIGH_WATERMARK;
1873 int low_wm_percent = LOW_WATERMARK;
1874 uint64_t x;
1875 struct wc_memory_superblock s;
1877 static struct dm_arg _args[] = {
1878 {0, 10, "Invalid number of feature args"},
1881 as.argc = argc;
1882 as.argv = argv;
1884 wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
1885 if (!wc) {
1886 ti->error = "Cannot allocate writecache structure";
1887 r = -ENOMEM;
1888 goto bad;
1890 ti->private = wc;
1891 wc->ti = ti;
1893 mutex_init(&wc->lock);
1894 writecache_poison_lists(wc);
1895 init_waitqueue_head(&wc->freelist_wait);
1896 timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
1898 for (i = 0; i < 2; i++) {
1899 atomic_set(&wc->bio_in_progress[i], 0);
1900 init_waitqueue_head(&wc->bio_in_progress_wait[i]);
1903 wc->dm_io = dm_io_client_create();
1904 if (IS_ERR(wc->dm_io)) {
1905 r = PTR_ERR(wc->dm_io);
1906 ti->error = "Unable to allocate dm-io client";
1907 wc->dm_io = NULL;
1908 goto bad;
1911 wc->writeback_wq = alloc_workqueue("writecache-writeabck", WQ_MEM_RECLAIM, 1);
1912 if (!wc->writeback_wq) {
1913 r = -ENOMEM;
1914 ti->error = "Could not allocate writeback workqueue";
1915 goto bad;
1917 INIT_WORK(&wc->writeback_work, writecache_writeback);
1918 INIT_WORK(&wc->flush_work, writecache_flush_work);
1920 raw_spin_lock_init(&wc->endio_list_lock);
1921 INIT_LIST_HEAD(&wc->endio_list);
1922 wc->endio_thread = kthread_create(writecache_endio_thread, wc, "writecache_endio");
1923 if (IS_ERR(wc->endio_thread)) {
1924 r = PTR_ERR(wc->endio_thread);
1925 wc->endio_thread = NULL;
1926 ti->error = "Couldn't spawn endio thread";
1927 goto bad;
1929 wake_up_process(wc->endio_thread);
1932 * Parse the mode (pmem or ssd)
1934 string = dm_shift_arg(&as);
1935 if (!string)
1936 goto bad_arguments;
1938 if (!strcasecmp(string, "s")) {
1939 wc->pmem_mode = false;
1940 } else if (!strcasecmp(string, "p")) {
1941 #ifdef DM_WRITECACHE_HAS_PMEM
1942 wc->pmem_mode = true;
1943 wc->writeback_fua = true;
1944 #else
1946 * If the architecture doesn't support persistent memory or
1947 * the kernel doesn't support any DAX drivers, this driver can
1948 * only be used in SSD-only mode.
1950 r = -EOPNOTSUPP;
1951 ti->error = "Persistent memory or DAX not supported on this system";
1952 goto bad;
1953 #endif
1954 } else {
1955 goto bad_arguments;
1958 if (WC_MODE_PMEM(wc)) {
1959 r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
1960 offsetof(struct writeback_struct, bio),
1961 BIOSET_NEED_BVECS);
1962 if (r) {
1963 ti->error = "Could not allocate bio set";
1964 goto bad;
1966 } else {
1967 r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
1968 if (r) {
1969 ti->error = "Could not allocate mempool";
1970 goto bad;
1975 * Parse the origin data device
1977 string = dm_shift_arg(&as);
1978 if (!string)
1979 goto bad_arguments;
1980 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
1981 if (r) {
1982 ti->error = "Origin data device lookup failed";
1983 goto bad;
1987 * Parse cache data device (be it pmem or ssd)
1989 string = dm_shift_arg(&as);
1990 if (!string)
1991 goto bad_arguments;
1993 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
1994 if (r) {
1995 ti->error = "Cache data device lookup failed";
1996 goto bad;
1998 wc->memory_map_size = i_size_read(wc->ssd_dev->bdev->bd_inode);
2001 * Parse the cache block size
2003 string = dm_shift_arg(&as);
2004 if (!string)
2005 goto bad_arguments;
2006 if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
2007 wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
2008 (wc->block_size & (wc->block_size - 1))) {
2009 r = -EINVAL;
2010 ti->error = "Invalid block size";
2011 goto bad;
2013 if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) ||
2014 wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) {
2015 r = -EINVAL;
2016 ti->error = "Block size is smaller than device logical block size";
2017 goto bad;
2019 wc->block_size_bits = __ffs(wc->block_size);
2021 wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
2022 wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
2023 wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
2026 * Parse optional arguments
2028 r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2029 if (r)
2030 goto bad;
2032 while (opt_params) {
2033 string = dm_shift_arg(&as), opt_params--;
2034 if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2035 unsigned long long start_sector;
2036 string = dm_shift_arg(&as), opt_params--;
2037 if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2038 goto invalid_optional;
2039 wc->start_sector = start_sector;
2040 if (wc->start_sector != start_sector ||
2041 wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2042 goto invalid_optional;
2043 } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
2044 string = dm_shift_arg(&as), opt_params--;
2045 if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
2046 goto invalid_optional;
2047 if (high_wm_percent < 0 || high_wm_percent > 100)
2048 goto invalid_optional;
2049 wc->high_wm_percent_set = true;
2050 } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
2051 string = dm_shift_arg(&as), opt_params--;
2052 if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
2053 goto invalid_optional;
2054 if (low_wm_percent < 0 || low_wm_percent > 100)
2055 goto invalid_optional;
2056 wc->low_wm_percent_set = true;
2057 } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
2058 string = dm_shift_arg(&as), opt_params--;
2059 if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
2060 goto invalid_optional;
2061 wc->max_writeback_jobs_set = true;
2062 } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
2063 string = dm_shift_arg(&as), opt_params--;
2064 if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
2065 goto invalid_optional;
2066 wc->autocommit_blocks_set = true;
2067 } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
2068 unsigned autocommit_msecs;
2069 string = dm_shift_arg(&as), opt_params--;
2070 if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
2071 goto invalid_optional;
2072 if (autocommit_msecs > 3600000)
2073 goto invalid_optional;
2074 wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2075 wc->autocommit_time_set = true;
2076 } else if (!strcasecmp(string, "fua")) {
2077 if (WC_MODE_PMEM(wc)) {
2078 wc->writeback_fua = true;
2079 wc->writeback_fua_set = true;
2080 } else goto invalid_optional;
2081 } else if (!strcasecmp(string, "nofua")) {
2082 if (WC_MODE_PMEM(wc)) {
2083 wc->writeback_fua = false;
2084 wc->writeback_fua_set = true;
2085 } else goto invalid_optional;
2086 } else {
2087 invalid_optional:
2088 r = -EINVAL;
2089 ti->error = "Invalid optional argument";
2090 goto bad;
2094 if (high_wm_percent < low_wm_percent) {
2095 r = -EINVAL;
2096 ti->error = "High watermark must be greater than or equal to low watermark";
2097 goto bad;
2100 if (WC_MODE_PMEM(wc)) {
2101 r = persistent_memory_claim(wc);
2102 if (r) {
2103 ti->error = "Unable to map persistent memory for cache";
2104 goto bad;
2106 } else {
2107 size_t n_blocks, n_metadata_blocks;
2108 uint64_t n_bitmap_bits;
2110 wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2112 bio_list_init(&wc->flush_list);
2113 wc->flush_thread = kthread_create(writecache_flush_thread, wc, "dm_writecache_flush");
2114 if (IS_ERR(wc->flush_thread)) {
2115 r = PTR_ERR(wc->flush_thread);
2116 wc->flush_thread = NULL;
2117 ti->error = "Couldn't spawn endio thread";
2118 goto bad;
2120 wake_up_process(wc->flush_thread);
2122 r = calculate_memory_size(wc->memory_map_size, wc->block_size,
2123 &n_blocks, &n_metadata_blocks);
2124 if (r) {
2125 ti->error = "Invalid device size";
2126 goto bad;
2129 n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
2130 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
2131 /* this is limitation of test_bit functions */
2132 if (n_bitmap_bits > 1U << 31) {
2133 r = -EFBIG;
2134 ti->error = "Invalid device size";
2135 goto bad;
2138 wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
2139 if (!wc->memory_map) {
2140 r = -ENOMEM;
2141 ti->error = "Unable to allocate memory for metadata";
2142 goto bad;
2145 wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2146 if (IS_ERR(wc->dm_kcopyd)) {
2147 r = PTR_ERR(wc->dm_kcopyd);
2148 ti->error = "Unable to allocate dm-kcopyd client";
2149 wc->dm_kcopyd = NULL;
2150 goto bad;
2153 wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
2154 wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
2155 BITS_PER_LONG * sizeof(unsigned long);
2156 wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
2157 if (!wc->dirty_bitmap) {
2158 r = -ENOMEM;
2159 ti->error = "Unable to allocate dirty bitmap";
2160 goto bad;
2163 r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT);
2164 if (r) {
2165 ti->error = "Unable to read first block of metadata";
2166 goto bad;
2170 r = memcpy_mcsafe(&s, sb(wc), sizeof(struct wc_memory_superblock));
2171 if (r) {
2172 ti->error = "Hardware memory error when reading superblock";
2173 goto bad;
2175 if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
2176 r = init_memory(wc);
2177 if (r) {
2178 ti->error = "Unable to initialize device";
2179 goto bad;
2181 r = memcpy_mcsafe(&s, sb(wc), sizeof(struct wc_memory_superblock));
2182 if (r) {
2183 ti->error = "Hardware memory error when reading superblock";
2184 goto bad;
2188 if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
2189 ti->error = "Invalid magic in the superblock";
2190 r = -EINVAL;
2191 goto bad;
2194 if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
2195 ti->error = "Invalid version in the superblock";
2196 r = -EINVAL;
2197 goto bad;
2200 if (le32_to_cpu(s.block_size) != wc->block_size) {
2201 ti->error = "Block size does not match superblock";
2202 r = -EINVAL;
2203 goto bad;
2206 wc->n_blocks = le64_to_cpu(s.n_blocks);
2208 offset = wc->n_blocks * sizeof(struct wc_memory_entry);
2209 if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
2210 overflow:
2211 ti->error = "Overflow in size calculation";
2212 r = -EINVAL;
2213 goto bad;
2215 offset += sizeof(struct wc_memory_superblock);
2216 if (offset < sizeof(struct wc_memory_superblock))
2217 goto overflow;
2218 offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
2219 data_size = wc->n_blocks * (size_t)wc->block_size;
2220 if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
2221 (offset + data_size < offset))
2222 goto overflow;
2223 if (offset + data_size > wc->memory_map_size) {
2224 ti->error = "Memory area is too small";
2225 r = -EINVAL;
2226 goto bad;
2229 wc->metadata_sectors = offset >> SECTOR_SHIFT;
2230 wc->block_start = (char *)sb(wc) + offset;
2232 x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
2233 x += 50;
2234 do_div(x, 100);
2235 wc->freelist_high_watermark = x;
2236 x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
2237 x += 50;
2238 do_div(x, 100);
2239 wc->freelist_low_watermark = x;
2241 r = writecache_alloc_entries(wc);
2242 if (r) {
2243 ti->error = "Cannot allocate memory";
2244 goto bad;
2247 ti->num_flush_bios = 1;
2248 ti->flush_supported = true;
2249 ti->num_discard_bios = 1;
2251 if (WC_MODE_PMEM(wc))
2252 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
2254 return 0;
2256 bad_arguments:
2257 r = -EINVAL;
2258 ti->error = "Bad arguments";
2259 bad:
2260 writecache_dtr(ti);
2261 return r;
2264 static void writecache_status(struct dm_target *ti, status_type_t type,
2265 unsigned status_flags, char *result, unsigned maxlen)
2267 struct dm_writecache *wc = ti->private;
2268 unsigned extra_args;
2269 unsigned sz = 0;
2270 uint64_t x;
2272 switch (type) {
2273 case STATUSTYPE_INFO:
2274 DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),
2275 (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2276 (unsigned long long)wc->writeback_size);
2277 break;
2278 case STATUSTYPE_TABLE:
2279 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
2280 wc->dev->name, wc->ssd_dev->name, wc->block_size);
2281 extra_args = 0;
2282 if (wc->start_sector)
2283 extra_args += 2;
2284 if (wc->high_wm_percent_set)
2285 extra_args += 2;
2286 if (wc->low_wm_percent_set)
2287 extra_args += 2;
2288 if (wc->max_writeback_jobs_set)
2289 extra_args += 2;
2290 if (wc->autocommit_blocks_set)
2291 extra_args += 2;
2292 if (wc->autocommit_time_set)
2293 extra_args += 2;
2294 if (wc->writeback_fua_set)
2295 extra_args++;
2297 DMEMIT("%u", extra_args);
2298 if (wc->start_sector)
2299 DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2300 if (wc->high_wm_percent_set) {
2301 x = (uint64_t)wc->freelist_high_watermark * 100;
2302 x += wc->n_blocks / 2;
2303 do_div(x, (size_t)wc->n_blocks);
2304 DMEMIT(" high_watermark %u", 100 - (unsigned)x);
2306 if (wc->low_wm_percent_set) {
2307 x = (uint64_t)wc->freelist_low_watermark * 100;
2308 x += wc->n_blocks / 2;
2309 do_div(x, (size_t)wc->n_blocks);
2310 DMEMIT(" low_watermark %u", 100 - (unsigned)x);
2312 if (wc->max_writeback_jobs_set)
2313 DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
2314 if (wc->autocommit_blocks_set)
2315 DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
2316 if (wc->autocommit_time_set)
2317 DMEMIT(" autocommit_time %u", jiffies_to_msecs(wc->autocommit_jiffies));
2318 if (wc->writeback_fua_set)
2319 DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2320 break;
2324 static struct target_type writecache_target = {
2325 .name = "writecache",
2326 .version = {1, 1, 1},
2327 .module = THIS_MODULE,
2328 .ctr = writecache_ctr,
2329 .dtr = writecache_dtr,
2330 .status = writecache_status,
2331 .postsuspend = writecache_suspend,
2332 .resume = writecache_resume,
2333 .message = writecache_message,
2334 .map = writecache_map,
2335 .end_io = writecache_end_io,
2336 .iterate_devices = writecache_iterate_devices,
2337 .io_hints = writecache_io_hints,
2340 static int __init dm_writecache_init(void)
2342 int r;
2344 r = dm_register_target(&writecache_target);
2345 if (r < 0) {
2346 DMERR("register failed %d", r);
2347 return r;
2350 return 0;
2353 static void __exit dm_writecache_exit(void)
2355 dm_unregister_target(&writecache_target);
2358 module_init(dm_writecache_init);
2359 module_exit(dm_writecache_exit);
2361 MODULE_DESCRIPTION(DM_NAME " writecache target");
2362 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2363 MODULE_LICENSE("GPL");