dm writecache: correct uncommitted_block when discarding uncommitted entry
[linux/fpc-iii.git] / drivers / md / dm-writecache.c
blob856b3515654ff1fb98a518815e95c55719be0853
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
29 #define MAX_AGE_DIV 16
30 #define MAX_AGE_UNSPECIFIED -1UL
32 #define BITMAP_GRANULARITY 65536
33 #if BITMAP_GRANULARITY < PAGE_SIZE
34 #undef BITMAP_GRANULARITY
35 #define BITMAP_GRANULARITY PAGE_SIZE
36 #endif
38 #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_DAX_DRIVER)
39 #define DM_WRITECACHE_HAS_PMEM
40 #endif
42 #ifdef DM_WRITECACHE_HAS_PMEM
43 #define pmem_assign(dest, src) \
44 do { \
45 typeof(dest) uniq = (src); \
46 memcpy_flushcache(&(dest), &uniq, sizeof(dest)); \
47 } while (0)
48 #else
49 #define pmem_assign(dest, src) ((dest) = (src))
50 #endif
52 #if defined(__HAVE_ARCH_MEMCPY_MCSAFE) && defined(DM_WRITECACHE_HAS_PMEM)
53 #define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
54 #endif
56 #define MEMORY_SUPERBLOCK_MAGIC 0x23489321
57 #define MEMORY_SUPERBLOCK_VERSION 1
59 struct wc_memory_entry {
60 __le64 original_sector;
61 __le64 seq_count;
64 struct wc_memory_superblock {
65 union {
66 struct {
67 __le32 magic;
68 __le32 version;
69 __le32 block_size;
70 __le32 pad;
71 __le64 n_blocks;
72 __le64 seq_count;
74 __le64 padding[8];
76 struct wc_memory_entry entries[0];
79 struct wc_entry {
80 struct rb_node rb_node;
81 struct list_head lru;
82 unsigned short wc_list_contiguous;
83 bool write_in_progress
84 #if BITS_PER_LONG == 64
86 #endif
88 unsigned long index
89 #if BITS_PER_LONG == 64
90 :47
91 #endif
93 unsigned long age;
94 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
95 uint64_t original_sector;
96 uint64_t seq_count;
97 #endif
100 #ifdef DM_WRITECACHE_HAS_PMEM
101 #define WC_MODE_PMEM(wc) ((wc)->pmem_mode)
102 #define WC_MODE_FUA(wc) ((wc)->writeback_fua)
103 #else
104 #define WC_MODE_PMEM(wc) false
105 #define WC_MODE_FUA(wc) false
106 #endif
107 #define WC_MODE_SORT_FREELIST(wc) (!WC_MODE_PMEM(wc))
109 struct dm_writecache {
110 struct mutex lock;
111 struct list_head lru;
112 union {
113 struct list_head freelist;
114 struct {
115 struct rb_root freetree;
116 struct wc_entry *current_free;
119 struct rb_root tree;
121 size_t freelist_size;
122 size_t writeback_size;
123 size_t freelist_high_watermark;
124 size_t freelist_low_watermark;
125 unsigned long max_age;
127 unsigned uncommitted_blocks;
128 unsigned autocommit_blocks;
129 unsigned max_writeback_jobs;
131 int error;
133 unsigned long autocommit_jiffies;
134 struct timer_list autocommit_timer;
135 struct wait_queue_head freelist_wait;
137 struct timer_list max_age_timer;
139 atomic_t bio_in_progress[2];
140 struct wait_queue_head bio_in_progress_wait[2];
142 struct dm_target *ti;
143 struct dm_dev *dev;
144 struct dm_dev *ssd_dev;
145 sector_t start_sector;
146 void *memory_map;
147 uint64_t memory_map_size;
148 size_t metadata_sectors;
149 size_t n_blocks;
150 uint64_t seq_count;
151 void *block_start;
152 struct wc_entry *entries;
153 unsigned block_size;
154 unsigned char block_size_bits;
156 bool pmem_mode:1;
157 bool writeback_fua:1;
159 bool overwrote_committed:1;
160 bool memory_vmapped:1;
162 bool high_wm_percent_set:1;
163 bool low_wm_percent_set:1;
164 bool max_writeback_jobs_set:1;
165 bool autocommit_blocks_set:1;
166 bool autocommit_time_set:1;
167 bool writeback_fua_set:1;
168 bool flush_on_suspend:1;
169 bool cleaner:1;
171 unsigned writeback_all;
172 struct workqueue_struct *writeback_wq;
173 struct work_struct writeback_work;
174 struct work_struct flush_work;
176 struct dm_io_client *dm_io;
178 raw_spinlock_t endio_list_lock;
179 struct list_head endio_list;
180 struct task_struct *endio_thread;
182 struct task_struct *flush_thread;
183 struct bio_list flush_list;
185 struct dm_kcopyd_client *dm_kcopyd;
186 unsigned long *dirty_bitmap;
187 unsigned dirty_bitmap_size;
189 struct bio_set bio_set;
190 mempool_t copy_pool;
193 #define WB_LIST_INLINE 16
195 struct writeback_struct {
196 struct list_head endio_entry;
197 struct dm_writecache *wc;
198 struct wc_entry **wc_list;
199 unsigned wc_list_n;
200 struct wc_entry *wc_list_inline[WB_LIST_INLINE];
201 struct bio bio;
204 struct copy_struct {
205 struct list_head endio_entry;
206 struct dm_writecache *wc;
207 struct wc_entry *e;
208 unsigned n_entries;
209 int error;
212 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
213 "A percentage of time allocated for data copying");
215 static void wc_lock(struct dm_writecache *wc)
217 mutex_lock(&wc->lock);
220 static void wc_unlock(struct dm_writecache *wc)
222 mutex_unlock(&wc->lock);
225 #ifdef DM_WRITECACHE_HAS_PMEM
226 static int persistent_memory_claim(struct dm_writecache *wc)
228 int r;
229 loff_t s;
230 long p, da;
231 pfn_t pfn;
232 int id;
233 struct page **pages;
235 wc->memory_vmapped = false;
237 if (!wc->ssd_dev->dax_dev) {
238 r = -EOPNOTSUPP;
239 goto err1;
241 s = wc->memory_map_size;
242 p = s >> PAGE_SHIFT;
243 if (!p) {
244 r = -EINVAL;
245 goto err1;
247 if (p != s >> PAGE_SHIFT) {
248 r = -EOVERFLOW;
249 goto err1;
252 id = dax_read_lock();
254 da = dax_direct_access(wc->ssd_dev->dax_dev, 0, p, &wc->memory_map, &pfn);
255 if (da < 0) {
256 wc->memory_map = NULL;
257 r = da;
258 goto err2;
260 if (!pfn_t_has_page(pfn)) {
261 wc->memory_map = NULL;
262 r = -EOPNOTSUPP;
263 goto err2;
265 if (da != p) {
266 long i;
267 wc->memory_map = NULL;
268 pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
269 if (!pages) {
270 r = -ENOMEM;
271 goto err2;
273 i = 0;
274 do {
275 long daa;
276 daa = dax_direct_access(wc->ssd_dev->dax_dev, i, p - i,
277 NULL, &pfn);
278 if (daa <= 0) {
279 r = daa ? daa : -EINVAL;
280 goto err3;
282 if (!pfn_t_has_page(pfn)) {
283 r = -EOPNOTSUPP;
284 goto err3;
286 while (daa-- && i < p) {
287 pages[i++] = pfn_t_to_page(pfn);
288 pfn.val++;
290 } while (i < p);
291 wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
292 if (!wc->memory_map) {
293 r = -ENOMEM;
294 goto err3;
296 kvfree(pages);
297 wc->memory_vmapped = true;
300 dax_read_unlock(id);
302 wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
303 wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
305 return 0;
306 err3:
307 kvfree(pages);
308 err2:
309 dax_read_unlock(id);
310 err1:
311 return r;
313 #else
314 static int persistent_memory_claim(struct dm_writecache *wc)
316 BUG();
318 #endif
320 static void persistent_memory_release(struct dm_writecache *wc)
322 if (wc->memory_vmapped)
323 vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
326 static struct page *persistent_memory_page(void *addr)
328 if (is_vmalloc_addr(addr))
329 return vmalloc_to_page(addr);
330 else
331 return virt_to_page(addr);
334 static unsigned persistent_memory_page_offset(void *addr)
336 return (unsigned long)addr & (PAGE_SIZE - 1);
339 static void persistent_memory_flush_cache(void *ptr, size_t size)
341 if (is_vmalloc_addr(ptr))
342 flush_kernel_vmap_range(ptr, size);
345 static void persistent_memory_invalidate_cache(void *ptr, size_t size)
347 if (is_vmalloc_addr(ptr))
348 invalidate_kernel_vmap_range(ptr, size);
351 static struct wc_memory_superblock *sb(struct dm_writecache *wc)
353 return wc->memory_map;
356 static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
358 return &sb(wc)->entries[e->index];
361 static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
363 return (char *)wc->block_start + (e->index << wc->block_size_bits);
366 static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
368 return wc->start_sector + wc->metadata_sectors +
369 ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
372 static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
374 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
375 return e->original_sector;
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 ssd_commit_superblock(struct dm_writecache *wc)
514 int r;
515 struct dm_io_region region;
516 struct dm_io_request req;
518 region.bdev = wc->ssd_dev->bdev;
519 region.sector = 0;
520 region.count = PAGE_SIZE;
522 if (unlikely(region.sector + region.count > wc->metadata_sectors))
523 region.count = wc->metadata_sectors - region.sector;
525 region.sector += wc->start_sector;
527 req.bi_op = REQ_OP_WRITE;
528 req.bi_op_flags = REQ_SYNC | REQ_FUA;
529 req.mem.type = DM_IO_VMA;
530 req.mem.ptr.vma = (char *)wc->memory_map;
531 req.client = wc->dm_io;
532 req.notify.fn = NULL;
533 req.notify.context = NULL;
535 r = dm_io(&req, 1, &region, NULL);
536 if (unlikely(r))
537 writecache_error(wc, r, "error writing superblock");
540 static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
542 if (WC_MODE_PMEM(wc))
543 wmb();
544 else
545 ssd_commit_flushed(wc, wait_for_ios);
548 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
550 int r;
551 struct dm_io_region region;
552 struct dm_io_request req;
554 region.bdev = dev->bdev;
555 region.sector = 0;
556 region.count = 0;
557 req.bi_op = REQ_OP_WRITE;
558 req.bi_op_flags = REQ_PREFLUSH;
559 req.mem.type = DM_IO_KMEM;
560 req.mem.ptr.addr = NULL;
561 req.client = wc->dm_io;
562 req.notify.fn = NULL;
564 r = dm_io(&req, 1, &region, NULL);
565 if (unlikely(r))
566 writecache_error(wc, r, "error flushing metadata: %d", r);
569 #define WFE_RETURN_FOLLOWING 1
570 #define WFE_LOWEST_SEQ 2
572 static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
573 uint64_t block, int flags)
575 struct wc_entry *e;
576 struct rb_node *node = wc->tree.rb_node;
578 if (unlikely(!node))
579 return NULL;
581 while (1) {
582 e = container_of(node, struct wc_entry, rb_node);
583 if (read_original_sector(wc, e) == block)
584 break;
586 node = (read_original_sector(wc, e) >= block ?
587 e->rb_node.rb_left : e->rb_node.rb_right);
588 if (unlikely(!node)) {
589 if (!(flags & WFE_RETURN_FOLLOWING))
590 return NULL;
591 if (read_original_sector(wc, e) >= block) {
592 return e;
593 } else {
594 node = rb_next(&e->rb_node);
595 if (unlikely(!node))
596 return NULL;
597 e = container_of(node, struct wc_entry, rb_node);
598 return e;
603 while (1) {
604 struct wc_entry *e2;
605 if (flags & WFE_LOWEST_SEQ)
606 node = rb_prev(&e->rb_node);
607 else
608 node = rb_next(&e->rb_node);
609 if (unlikely(!node))
610 return e;
611 e2 = container_of(node, struct wc_entry, rb_node);
612 if (read_original_sector(wc, e2) != block)
613 return e;
614 e = e2;
618 static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
620 struct wc_entry *e;
621 struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
623 while (*node) {
624 e = container_of(*node, struct wc_entry, rb_node);
625 parent = &e->rb_node;
626 if (read_original_sector(wc, e) > read_original_sector(wc, ins))
627 node = &parent->rb_left;
628 else
629 node = &parent->rb_right;
631 rb_link_node(&ins->rb_node, parent, node);
632 rb_insert_color(&ins->rb_node, &wc->tree);
633 list_add(&ins->lru, &wc->lru);
634 ins->age = jiffies;
637 static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
639 list_del(&e->lru);
640 rb_erase(&e->rb_node, &wc->tree);
643 static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
645 if (WC_MODE_SORT_FREELIST(wc)) {
646 struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
647 if (unlikely(!*node))
648 wc->current_free = e;
649 while (*node) {
650 parent = *node;
651 if (&e->rb_node < *node)
652 node = &parent->rb_left;
653 else
654 node = &parent->rb_right;
656 rb_link_node(&e->rb_node, parent, node);
657 rb_insert_color(&e->rb_node, &wc->freetree);
658 } else {
659 list_add_tail(&e->lru, &wc->freelist);
661 wc->freelist_size++;
664 static inline void writecache_verify_watermark(struct dm_writecache *wc)
666 if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
667 queue_work(wc->writeback_wq, &wc->writeback_work);
670 static void writecache_max_age_timer(struct timer_list *t)
672 struct dm_writecache *wc = from_timer(wc, t, max_age_timer);
674 if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) {
675 queue_work(wc->writeback_wq, &wc->writeback_work);
676 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
680 static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector)
682 struct wc_entry *e;
684 if (WC_MODE_SORT_FREELIST(wc)) {
685 struct rb_node *next;
686 if (unlikely(!wc->current_free))
687 return NULL;
688 e = wc->current_free;
689 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
690 return NULL;
691 next = rb_next(&e->rb_node);
692 rb_erase(&e->rb_node, &wc->freetree);
693 if (unlikely(!next))
694 next = rb_first(&wc->freetree);
695 wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
696 } else {
697 if (unlikely(list_empty(&wc->freelist)))
698 return NULL;
699 e = container_of(wc->freelist.next, struct wc_entry, lru);
700 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
701 return NULL;
702 list_del(&e->lru);
704 wc->freelist_size--;
706 writecache_verify_watermark(wc);
708 return e;
711 static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
713 writecache_unlink(wc, e);
714 writecache_add_to_freelist(wc, e);
715 clear_seq_count(wc, e);
716 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
717 if (unlikely(waitqueue_active(&wc->freelist_wait)))
718 wake_up(&wc->freelist_wait);
721 static void writecache_wait_on_freelist(struct dm_writecache *wc)
723 DEFINE_WAIT(wait);
725 prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
726 wc_unlock(wc);
727 io_schedule();
728 finish_wait(&wc->freelist_wait, &wait);
729 wc_lock(wc);
732 static void writecache_poison_lists(struct dm_writecache *wc)
735 * Catch incorrect access to these values while the device is suspended.
737 memset(&wc->tree, -1, sizeof wc->tree);
738 wc->lru.next = LIST_POISON1;
739 wc->lru.prev = LIST_POISON2;
740 wc->freelist.next = LIST_POISON1;
741 wc->freelist.prev = LIST_POISON2;
744 static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
746 writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
747 if (WC_MODE_PMEM(wc))
748 writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
751 static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
753 return read_seq_count(wc, e) < wc->seq_count;
756 static void writecache_flush(struct dm_writecache *wc)
758 struct wc_entry *e, *e2;
759 bool need_flush_after_free;
761 wc->uncommitted_blocks = 0;
762 del_timer(&wc->autocommit_timer);
764 if (list_empty(&wc->lru))
765 return;
767 e = container_of(wc->lru.next, struct wc_entry, lru);
768 if (writecache_entry_is_committed(wc, e)) {
769 if (wc->overwrote_committed) {
770 writecache_wait_for_ios(wc, WRITE);
771 writecache_disk_flush(wc, wc->ssd_dev);
772 wc->overwrote_committed = false;
774 return;
776 while (1) {
777 writecache_flush_entry(wc, e);
778 if (unlikely(e->lru.next == &wc->lru))
779 break;
780 e2 = container_of(e->lru.next, struct wc_entry, lru);
781 if (writecache_entry_is_committed(wc, e2))
782 break;
783 e = e2;
784 cond_resched();
786 writecache_commit_flushed(wc, true);
788 wc->seq_count++;
789 pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
790 if (WC_MODE_PMEM(wc))
791 writecache_commit_flushed(wc, false);
792 else
793 ssd_commit_superblock(wc);
795 wc->overwrote_committed = false;
797 need_flush_after_free = false;
798 while (1) {
799 /* Free another committed entry with lower seq-count */
800 struct rb_node *rb_node = rb_prev(&e->rb_node);
802 if (rb_node) {
803 e2 = container_of(rb_node, struct wc_entry, rb_node);
804 if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
805 likely(!e2->write_in_progress)) {
806 writecache_free_entry(wc, e2);
807 need_flush_after_free = true;
810 if (unlikely(e->lru.prev == &wc->lru))
811 break;
812 e = container_of(e->lru.prev, struct wc_entry, lru);
813 cond_resched();
816 if (need_flush_after_free)
817 writecache_commit_flushed(wc, false);
820 static void writecache_flush_work(struct work_struct *work)
822 struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
824 wc_lock(wc);
825 writecache_flush(wc);
826 wc_unlock(wc);
829 static void writecache_autocommit_timer(struct timer_list *t)
831 struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
832 if (!writecache_has_error(wc))
833 queue_work(wc->writeback_wq, &wc->flush_work);
836 static void writecache_schedule_autocommit(struct dm_writecache *wc)
838 if (!timer_pending(&wc->autocommit_timer))
839 mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
842 static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
844 struct wc_entry *e;
845 bool discarded_something = false;
847 e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
848 if (unlikely(!e))
849 return;
851 while (read_original_sector(wc, e) < end) {
852 struct rb_node *node = rb_next(&e->rb_node);
854 if (likely(!e->write_in_progress)) {
855 if (!discarded_something) {
856 writecache_wait_for_ios(wc, READ);
857 writecache_wait_for_ios(wc, WRITE);
858 discarded_something = true;
860 if (!writecache_entry_is_committed(wc, e))
861 wc->uncommitted_blocks--;
862 writecache_free_entry(wc, e);
865 if (unlikely(!node))
866 break;
868 e = container_of(node, struct wc_entry, rb_node);
871 if (discarded_something)
872 writecache_commit_flushed(wc, false);
875 static bool writecache_wait_for_writeback(struct dm_writecache *wc)
877 if (wc->writeback_size) {
878 writecache_wait_on_freelist(wc);
879 return true;
881 return false;
884 static void writecache_suspend(struct dm_target *ti)
886 struct dm_writecache *wc = ti->private;
887 bool flush_on_suspend;
889 del_timer_sync(&wc->autocommit_timer);
890 del_timer_sync(&wc->max_age_timer);
892 wc_lock(wc);
893 writecache_flush(wc);
894 flush_on_suspend = wc->flush_on_suspend;
895 if (flush_on_suspend) {
896 wc->flush_on_suspend = false;
897 wc->writeback_all++;
898 queue_work(wc->writeback_wq, &wc->writeback_work);
900 wc_unlock(wc);
902 drain_workqueue(wc->writeback_wq);
904 wc_lock(wc);
905 if (flush_on_suspend)
906 wc->writeback_all--;
907 while (writecache_wait_for_writeback(wc));
909 if (WC_MODE_PMEM(wc))
910 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
912 writecache_poison_lists(wc);
914 wc_unlock(wc);
917 static int writecache_alloc_entries(struct dm_writecache *wc)
919 size_t b;
921 if (wc->entries)
922 return 0;
923 wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
924 if (!wc->entries)
925 return -ENOMEM;
926 for (b = 0; b < wc->n_blocks; b++) {
927 struct wc_entry *e = &wc->entries[b];
928 e->index = b;
929 e->write_in_progress = false;
930 cond_resched();
933 return 0;
936 static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors)
938 struct dm_io_region region;
939 struct dm_io_request req;
941 region.bdev = wc->ssd_dev->bdev;
942 region.sector = wc->start_sector;
943 region.count = n_sectors;
944 req.bi_op = REQ_OP_READ;
945 req.bi_op_flags = REQ_SYNC;
946 req.mem.type = DM_IO_VMA;
947 req.mem.ptr.vma = (char *)wc->memory_map;
948 req.client = wc->dm_io;
949 req.notify.fn = NULL;
951 return dm_io(&req, 1, &region, NULL);
954 static void writecache_resume(struct dm_target *ti)
956 struct dm_writecache *wc = ti->private;
957 size_t b;
958 bool need_flush = false;
959 __le64 sb_seq_count;
960 int r;
962 wc_lock(wc);
964 if (WC_MODE_PMEM(wc)) {
965 persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
966 } else {
967 r = writecache_read_metadata(wc, wc->metadata_sectors);
968 if (r) {
969 size_t sb_entries_offset;
970 writecache_error(wc, r, "unable to read metadata: %d", r);
971 sb_entries_offset = offsetof(struct wc_memory_superblock, entries);
972 memset((char *)wc->memory_map + sb_entries_offset, -1,
973 (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset);
977 wc->tree = RB_ROOT;
978 INIT_LIST_HEAD(&wc->lru);
979 if (WC_MODE_SORT_FREELIST(wc)) {
980 wc->freetree = RB_ROOT;
981 wc->current_free = NULL;
982 } else {
983 INIT_LIST_HEAD(&wc->freelist);
985 wc->freelist_size = 0;
987 r = memcpy_mcsafe(&sb_seq_count, &sb(wc)->seq_count, sizeof(uint64_t));
988 if (r) {
989 writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
990 sb_seq_count = cpu_to_le64(0);
992 wc->seq_count = le64_to_cpu(sb_seq_count);
994 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
995 for (b = 0; b < wc->n_blocks; b++) {
996 struct wc_entry *e = &wc->entries[b];
997 struct wc_memory_entry wme;
998 if (writecache_has_error(wc)) {
999 e->original_sector = -1;
1000 e->seq_count = -1;
1001 continue;
1003 r = memcpy_mcsafe(&wme, memory_entry(wc, e), sizeof(struct wc_memory_entry));
1004 if (r) {
1005 writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
1006 (unsigned long)b, r);
1007 e->original_sector = -1;
1008 e->seq_count = -1;
1009 } else {
1010 e->original_sector = le64_to_cpu(wme.original_sector);
1011 e->seq_count = le64_to_cpu(wme.seq_count);
1013 cond_resched();
1015 #endif
1016 for (b = 0; b < wc->n_blocks; b++) {
1017 struct wc_entry *e = &wc->entries[b];
1018 if (!writecache_entry_is_committed(wc, e)) {
1019 if (read_seq_count(wc, e) != -1) {
1020 erase_this:
1021 clear_seq_count(wc, e);
1022 need_flush = true;
1024 writecache_add_to_freelist(wc, e);
1025 } else {
1026 struct wc_entry *old;
1028 old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
1029 if (!old) {
1030 writecache_insert_entry(wc, e);
1031 } else {
1032 if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
1033 writecache_error(wc, -EINVAL,
1034 "two identical entries, position %llu, sector %llu, sequence %llu",
1035 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
1036 (unsigned long long)read_seq_count(wc, e));
1038 if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
1039 goto erase_this;
1040 } else {
1041 writecache_free_entry(wc, old);
1042 writecache_insert_entry(wc, e);
1043 need_flush = true;
1047 cond_resched();
1050 if (need_flush) {
1051 writecache_flush_all_metadata(wc);
1052 writecache_commit_flushed(wc, false);
1055 writecache_verify_watermark(wc);
1057 if (wc->max_age != MAX_AGE_UNSPECIFIED)
1058 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
1060 wc_unlock(wc);
1063 static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1065 if (argc != 1)
1066 return -EINVAL;
1068 wc_lock(wc);
1069 if (dm_suspended(wc->ti)) {
1070 wc_unlock(wc);
1071 return -EBUSY;
1073 if (writecache_has_error(wc)) {
1074 wc_unlock(wc);
1075 return -EIO;
1078 writecache_flush(wc);
1079 wc->writeback_all++;
1080 queue_work(wc->writeback_wq, &wc->writeback_work);
1081 wc_unlock(wc);
1083 flush_workqueue(wc->writeback_wq);
1085 wc_lock(wc);
1086 wc->writeback_all--;
1087 if (writecache_has_error(wc)) {
1088 wc_unlock(wc);
1089 return -EIO;
1091 wc_unlock(wc);
1093 return 0;
1096 static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1098 if (argc != 1)
1099 return -EINVAL;
1101 wc_lock(wc);
1102 wc->flush_on_suspend = true;
1103 wc_unlock(wc);
1105 return 0;
1108 static void activate_cleaner(struct dm_writecache *wc)
1110 wc->flush_on_suspend = true;
1111 wc->cleaner = true;
1112 wc->freelist_high_watermark = wc->n_blocks;
1113 wc->freelist_low_watermark = wc->n_blocks;
1116 static int process_cleaner_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1118 if (argc != 1)
1119 return -EINVAL;
1121 wc_lock(wc);
1122 activate_cleaner(wc);
1123 if (!dm_suspended(wc->ti))
1124 writecache_verify_watermark(wc);
1125 wc_unlock(wc);
1127 return 0;
1130 static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
1131 char *result, unsigned maxlen)
1133 int r = -EINVAL;
1134 struct dm_writecache *wc = ti->private;
1136 if (!strcasecmp(argv[0], "flush"))
1137 r = process_flush_mesg(argc, argv, wc);
1138 else if (!strcasecmp(argv[0], "flush_on_suspend"))
1139 r = process_flush_on_suspend_mesg(argc, argv, wc);
1140 else if (!strcasecmp(argv[0], "cleaner"))
1141 r = process_cleaner_mesg(argc, argv, wc);
1142 else
1143 DMERR("unrecognised message received: %s", argv[0]);
1145 return r;
1148 static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
1150 void *buf;
1151 unsigned long flags;
1152 unsigned size;
1153 int rw = bio_data_dir(bio);
1154 unsigned remaining_size = wc->block_size;
1156 do {
1157 struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
1158 buf = bvec_kmap_irq(&bv, &flags);
1159 size = bv.bv_len;
1160 if (unlikely(size > remaining_size))
1161 size = remaining_size;
1163 if (rw == READ) {
1164 int r;
1165 r = memcpy_mcsafe(buf, data, size);
1166 flush_dcache_page(bio_page(bio));
1167 if (unlikely(r)) {
1168 writecache_error(wc, r, "hardware memory error when reading data: %d", r);
1169 bio->bi_status = BLK_STS_IOERR;
1171 } else {
1172 flush_dcache_page(bio_page(bio));
1173 memcpy_flushcache(data, buf, size);
1176 bvec_kunmap_irq(buf, &flags);
1178 data = (char *)data + size;
1179 remaining_size -= size;
1180 bio_advance(bio, size);
1181 } while (unlikely(remaining_size));
1184 static int writecache_flush_thread(void *data)
1186 struct dm_writecache *wc = data;
1188 while (1) {
1189 struct bio *bio;
1191 wc_lock(wc);
1192 bio = bio_list_pop(&wc->flush_list);
1193 if (!bio) {
1194 set_current_state(TASK_INTERRUPTIBLE);
1195 wc_unlock(wc);
1197 if (unlikely(kthread_should_stop())) {
1198 set_current_state(TASK_RUNNING);
1199 break;
1202 schedule();
1203 continue;
1206 if (bio_op(bio) == REQ_OP_DISCARD) {
1207 writecache_discard(wc, bio->bi_iter.bi_sector,
1208 bio_end_sector(bio));
1209 wc_unlock(wc);
1210 bio_set_dev(bio, wc->dev->bdev);
1211 generic_make_request(bio);
1212 } else {
1213 writecache_flush(wc);
1214 wc_unlock(wc);
1215 if (writecache_has_error(wc))
1216 bio->bi_status = BLK_STS_IOERR;
1217 bio_endio(bio);
1221 return 0;
1224 static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
1226 if (bio_list_empty(&wc->flush_list))
1227 wake_up_process(wc->flush_thread);
1228 bio_list_add(&wc->flush_list, bio);
1231 static int writecache_map(struct dm_target *ti, struct bio *bio)
1233 struct wc_entry *e;
1234 struct dm_writecache *wc = ti->private;
1236 bio->bi_private = NULL;
1238 wc_lock(wc);
1240 if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1241 if (writecache_has_error(wc))
1242 goto unlock_error;
1243 if (WC_MODE_PMEM(wc)) {
1244 writecache_flush(wc);
1245 if (writecache_has_error(wc))
1246 goto unlock_error;
1247 goto unlock_submit;
1248 } else {
1249 writecache_offload_bio(wc, bio);
1250 goto unlock_return;
1254 bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1256 if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
1257 (wc->block_size / 512 - 1)) != 0)) {
1258 DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
1259 (unsigned long long)bio->bi_iter.bi_sector,
1260 bio->bi_iter.bi_size, wc->block_size);
1261 goto unlock_error;
1264 if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
1265 if (writecache_has_error(wc))
1266 goto unlock_error;
1267 if (WC_MODE_PMEM(wc)) {
1268 writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
1269 goto unlock_remap_origin;
1270 } else {
1271 writecache_offload_bio(wc, bio);
1272 goto unlock_return;
1276 if (bio_data_dir(bio) == READ) {
1277 read_next_block:
1278 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1279 if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1280 if (WC_MODE_PMEM(wc)) {
1281 bio_copy_block(wc, bio, memory_data(wc, e));
1282 if (bio->bi_iter.bi_size)
1283 goto read_next_block;
1284 goto unlock_submit;
1285 } else {
1286 dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1287 bio_set_dev(bio, wc->ssd_dev->bdev);
1288 bio->bi_iter.bi_sector = cache_sector(wc, e);
1289 if (!writecache_entry_is_committed(wc, e))
1290 writecache_wait_for_ios(wc, WRITE);
1291 goto unlock_remap;
1293 } else {
1294 if (e) {
1295 sector_t next_boundary =
1296 read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1297 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
1298 dm_accept_partial_bio(bio, next_boundary);
1301 goto unlock_remap_origin;
1303 } else {
1304 do {
1305 bool found_entry = false;
1306 if (writecache_has_error(wc))
1307 goto unlock_error;
1308 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1309 if (e) {
1310 if (!writecache_entry_is_committed(wc, e))
1311 goto bio_copy;
1312 if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1313 wc->overwrote_committed = true;
1314 goto bio_copy;
1316 found_entry = true;
1317 } else {
1318 if (unlikely(wc->cleaner))
1319 goto direct_write;
1321 e = writecache_pop_from_freelist(wc, (sector_t)-1);
1322 if (unlikely(!e)) {
1323 if (!found_entry) {
1324 direct_write:
1325 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1326 if (e) {
1327 sector_t next_boundary = read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1328 BUG_ON(!next_boundary);
1329 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
1330 dm_accept_partial_bio(bio, next_boundary);
1333 goto unlock_remap_origin;
1335 writecache_wait_on_freelist(wc);
1336 continue;
1338 write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1339 writecache_insert_entry(wc, e);
1340 wc->uncommitted_blocks++;
1341 bio_copy:
1342 if (WC_MODE_PMEM(wc)) {
1343 bio_copy_block(wc, bio, memory_data(wc, e));
1344 } else {
1345 unsigned bio_size = wc->block_size;
1346 sector_t start_cache_sec = cache_sector(wc, e);
1347 sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT);
1349 while (bio_size < bio->bi_iter.bi_size) {
1350 struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec);
1351 if (!f)
1352 break;
1353 write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector +
1354 (bio_size >> SECTOR_SHIFT), wc->seq_count);
1355 writecache_insert_entry(wc, f);
1356 wc->uncommitted_blocks++;
1357 bio_size += wc->block_size;
1358 current_cache_sec += wc->block_size >> SECTOR_SHIFT;
1361 bio_set_dev(bio, wc->ssd_dev->bdev);
1362 bio->bi_iter.bi_sector = start_cache_sec;
1363 dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT);
1365 if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1366 wc->uncommitted_blocks = 0;
1367 queue_work(wc->writeback_wq, &wc->flush_work);
1368 } else {
1369 writecache_schedule_autocommit(wc);
1371 goto unlock_remap;
1373 } while (bio->bi_iter.bi_size);
1375 if (unlikely(bio->bi_opf & REQ_FUA ||
1376 wc->uncommitted_blocks >= wc->autocommit_blocks))
1377 writecache_flush(wc);
1378 else
1379 writecache_schedule_autocommit(wc);
1380 goto unlock_submit;
1383 unlock_remap_origin:
1384 bio_set_dev(bio, wc->dev->bdev);
1385 wc_unlock(wc);
1386 return DM_MAPIO_REMAPPED;
1388 unlock_remap:
1389 /* make sure that writecache_end_io decrements bio_in_progress: */
1390 bio->bi_private = (void *)1;
1391 atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
1392 wc_unlock(wc);
1393 return DM_MAPIO_REMAPPED;
1395 unlock_submit:
1396 wc_unlock(wc);
1397 bio_endio(bio);
1398 return DM_MAPIO_SUBMITTED;
1400 unlock_return:
1401 wc_unlock(wc);
1402 return DM_MAPIO_SUBMITTED;
1404 unlock_error:
1405 wc_unlock(wc);
1406 bio_io_error(bio);
1407 return DM_MAPIO_SUBMITTED;
1410 static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
1412 struct dm_writecache *wc = ti->private;
1414 if (bio->bi_private != NULL) {
1415 int dir = bio_data_dir(bio);
1416 if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
1417 if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
1418 wake_up(&wc->bio_in_progress_wait[dir]);
1420 return 0;
1423 static int writecache_iterate_devices(struct dm_target *ti,
1424 iterate_devices_callout_fn fn, void *data)
1426 struct dm_writecache *wc = ti->private;
1428 return fn(ti, wc->dev, 0, ti->len, data);
1431 static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
1433 struct dm_writecache *wc = ti->private;
1435 if (limits->logical_block_size < wc->block_size)
1436 limits->logical_block_size = wc->block_size;
1438 if (limits->physical_block_size < wc->block_size)
1439 limits->physical_block_size = wc->block_size;
1441 if (limits->io_min < wc->block_size)
1442 limits->io_min = wc->block_size;
1446 static void writecache_writeback_endio(struct bio *bio)
1448 struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
1449 struct dm_writecache *wc = wb->wc;
1450 unsigned long flags;
1452 raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
1453 if (unlikely(list_empty(&wc->endio_list)))
1454 wake_up_process(wc->endio_thread);
1455 list_add_tail(&wb->endio_entry, &wc->endio_list);
1456 raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
1459 static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
1461 struct copy_struct *c = ptr;
1462 struct dm_writecache *wc = c->wc;
1464 c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
1466 raw_spin_lock_irq(&wc->endio_list_lock);
1467 if (unlikely(list_empty(&wc->endio_list)))
1468 wake_up_process(wc->endio_thread);
1469 list_add_tail(&c->endio_entry, &wc->endio_list);
1470 raw_spin_unlock_irq(&wc->endio_list_lock);
1473 static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
1475 unsigned i;
1476 struct writeback_struct *wb;
1477 struct wc_entry *e;
1478 unsigned long n_walked = 0;
1480 do {
1481 wb = list_entry(list->next, struct writeback_struct, endio_entry);
1482 list_del(&wb->endio_entry);
1484 if (unlikely(wb->bio.bi_status != BLK_STS_OK))
1485 writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
1486 "write error %d", wb->bio.bi_status);
1487 i = 0;
1488 do {
1489 e = wb->wc_list[i];
1490 BUG_ON(!e->write_in_progress);
1491 e->write_in_progress = false;
1492 INIT_LIST_HEAD(&e->lru);
1493 if (!writecache_has_error(wc))
1494 writecache_free_entry(wc, e);
1495 BUG_ON(!wc->writeback_size);
1496 wc->writeback_size--;
1497 n_walked++;
1498 if (unlikely(n_walked >= ENDIO_LATENCY)) {
1499 writecache_commit_flushed(wc, false);
1500 wc_unlock(wc);
1501 wc_lock(wc);
1502 n_walked = 0;
1504 } while (++i < wb->wc_list_n);
1506 if (wb->wc_list != wb->wc_list_inline)
1507 kfree(wb->wc_list);
1508 bio_put(&wb->bio);
1509 } while (!list_empty(list));
1512 static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
1514 struct copy_struct *c;
1515 struct wc_entry *e;
1517 do {
1518 c = list_entry(list->next, struct copy_struct, endio_entry);
1519 list_del(&c->endio_entry);
1521 if (unlikely(c->error))
1522 writecache_error(wc, c->error, "copy error");
1524 e = c->e;
1525 do {
1526 BUG_ON(!e->write_in_progress);
1527 e->write_in_progress = false;
1528 INIT_LIST_HEAD(&e->lru);
1529 if (!writecache_has_error(wc))
1530 writecache_free_entry(wc, e);
1532 BUG_ON(!wc->writeback_size);
1533 wc->writeback_size--;
1534 e++;
1535 } while (--c->n_entries);
1536 mempool_free(c, &wc->copy_pool);
1537 } while (!list_empty(list));
1540 static int writecache_endio_thread(void *data)
1542 struct dm_writecache *wc = data;
1544 while (1) {
1545 struct list_head list;
1547 raw_spin_lock_irq(&wc->endio_list_lock);
1548 if (!list_empty(&wc->endio_list))
1549 goto pop_from_list;
1550 set_current_state(TASK_INTERRUPTIBLE);
1551 raw_spin_unlock_irq(&wc->endio_list_lock);
1553 if (unlikely(kthread_should_stop())) {
1554 set_current_state(TASK_RUNNING);
1555 break;
1558 schedule();
1560 continue;
1562 pop_from_list:
1563 list = wc->endio_list;
1564 list.next->prev = list.prev->next = &list;
1565 INIT_LIST_HEAD(&wc->endio_list);
1566 raw_spin_unlock_irq(&wc->endio_list_lock);
1568 if (!WC_MODE_FUA(wc))
1569 writecache_disk_flush(wc, wc->dev);
1571 wc_lock(wc);
1573 if (WC_MODE_PMEM(wc)) {
1574 __writecache_endio_pmem(wc, &list);
1575 } else {
1576 __writecache_endio_ssd(wc, &list);
1577 writecache_wait_for_ios(wc, READ);
1580 writecache_commit_flushed(wc, false);
1582 wc_unlock(wc);
1585 return 0;
1588 static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e, gfp_t gfp)
1590 struct dm_writecache *wc = wb->wc;
1591 unsigned block_size = wc->block_size;
1592 void *address = memory_data(wc, e);
1594 persistent_memory_flush_cache(address, block_size);
1595 return bio_add_page(&wb->bio, persistent_memory_page(address),
1596 block_size, persistent_memory_page_offset(address)) != 0;
1599 struct writeback_list {
1600 struct list_head list;
1601 size_t size;
1604 static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
1606 if (unlikely(wc->max_writeback_jobs)) {
1607 if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
1608 wc_lock(wc);
1609 while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
1610 writecache_wait_on_freelist(wc);
1611 wc_unlock(wc);
1614 cond_resched();
1617 static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
1619 struct wc_entry *e, *f;
1620 struct bio *bio;
1621 struct writeback_struct *wb;
1622 unsigned max_pages;
1624 while (wbl->size) {
1625 wbl->size--;
1626 e = container_of(wbl->list.prev, struct wc_entry, lru);
1627 list_del(&e->lru);
1629 max_pages = e->wc_list_contiguous;
1631 bio = bio_alloc_bioset(GFP_NOIO, max_pages, &wc->bio_set);
1632 wb = container_of(bio, struct writeback_struct, bio);
1633 wb->wc = wc;
1634 bio->bi_end_io = writecache_writeback_endio;
1635 bio_set_dev(bio, wc->dev->bdev);
1636 bio->bi_iter.bi_sector = read_original_sector(wc, e);
1637 if (max_pages <= WB_LIST_INLINE ||
1638 unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
1639 GFP_NOIO | __GFP_NORETRY |
1640 __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
1641 wb->wc_list = wb->wc_list_inline;
1642 max_pages = WB_LIST_INLINE;
1645 BUG_ON(!wc_add_block(wb, e, GFP_NOIO));
1647 wb->wc_list[0] = e;
1648 wb->wc_list_n = 1;
1650 while (wbl->size && wb->wc_list_n < max_pages) {
1651 f = container_of(wbl->list.prev, struct wc_entry, lru);
1652 if (read_original_sector(wc, f) !=
1653 read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1654 break;
1655 if (!wc_add_block(wb, f, GFP_NOWAIT | __GFP_NOWARN))
1656 break;
1657 wbl->size--;
1658 list_del(&f->lru);
1659 wb->wc_list[wb->wc_list_n++] = f;
1660 e = f;
1662 bio_set_op_attrs(bio, REQ_OP_WRITE, WC_MODE_FUA(wc) * REQ_FUA);
1663 if (writecache_has_error(wc)) {
1664 bio->bi_status = BLK_STS_IOERR;
1665 bio_endio(bio);
1666 } else {
1667 submit_bio(bio);
1670 __writeback_throttle(wc, wbl);
1674 static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
1676 struct wc_entry *e, *f;
1677 struct dm_io_region from, to;
1678 struct copy_struct *c;
1680 while (wbl->size) {
1681 unsigned n_sectors;
1683 wbl->size--;
1684 e = container_of(wbl->list.prev, struct wc_entry, lru);
1685 list_del(&e->lru);
1687 n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
1689 from.bdev = wc->ssd_dev->bdev;
1690 from.sector = cache_sector(wc, e);
1691 from.count = n_sectors;
1692 to.bdev = wc->dev->bdev;
1693 to.sector = read_original_sector(wc, e);
1694 to.count = n_sectors;
1696 c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
1697 c->wc = wc;
1698 c->e = e;
1699 c->n_entries = e->wc_list_contiguous;
1701 while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
1702 wbl->size--;
1703 f = container_of(wbl->list.prev, struct wc_entry, lru);
1704 BUG_ON(f != e + 1);
1705 list_del(&f->lru);
1706 e = f;
1709 dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
1711 __writeback_throttle(wc, wbl);
1715 static void writecache_writeback(struct work_struct *work)
1717 struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
1718 struct blk_plug plug;
1719 struct wc_entry *f, *uninitialized_var(g), *e = NULL;
1720 struct rb_node *node, *next_node;
1721 struct list_head skipped;
1722 struct writeback_list wbl;
1723 unsigned long n_walked;
1725 wc_lock(wc);
1726 restart:
1727 if (writecache_has_error(wc)) {
1728 wc_unlock(wc);
1729 return;
1732 if (unlikely(wc->writeback_all)) {
1733 if (writecache_wait_for_writeback(wc))
1734 goto restart;
1737 if (wc->overwrote_committed) {
1738 writecache_wait_for_ios(wc, WRITE);
1741 n_walked = 0;
1742 INIT_LIST_HEAD(&skipped);
1743 INIT_LIST_HEAD(&wbl.list);
1744 wbl.size = 0;
1745 while (!list_empty(&wc->lru) &&
1746 (wc->writeback_all ||
1747 wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark ||
1748 (jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >=
1749 wc->max_age - wc->max_age / MAX_AGE_DIV))) {
1751 n_walked++;
1752 if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1753 likely(!wc->writeback_all) && likely(!dm_suspended(wc->ti))) {
1754 queue_work(wc->writeback_wq, &wc->writeback_work);
1755 break;
1758 if (unlikely(wc->writeback_all)) {
1759 if (unlikely(!e)) {
1760 writecache_flush(wc);
1761 e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
1762 } else
1763 e = g;
1764 } else
1765 e = container_of(wc->lru.prev, struct wc_entry, lru);
1766 BUG_ON(e->write_in_progress);
1767 if (unlikely(!writecache_entry_is_committed(wc, e))) {
1768 writecache_flush(wc);
1770 node = rb_prev(&e->rb_node);
1771 if (node) {
1772 f = container_of(node, struct wc_entry, rb_node);
1773 if (unlikely(read_original_sector(wc, f) ==
1774 read_original_sector(wc, e))) {
1775 BUG_ON(!f->write_in_progress);
1776 list_del(&e->lru);
1777 list_add(&e->lru, &skipped);
1778 cond_resched();
1779 continue;
1782 wc->writeback_size++;
1783 list_del(&e->lru);
1784 list_add(&e->lru, &wbl.list);
1785 wbl.size++;
1786 e->write_in_progress = true;
1787 e->wc_list_contiguous = 1;
1789 f = e;
1791 while (1) {
1792 next_node = rb_next(&f->rb_node);
1793 if (unlikely(!next_node))
1794 break;
1795 g = container_of(next_node, struct wc_entry, rb_node);
1796 if (unlikely(read_original_sector(wc, g) ==
1797 read_original_sector(wc, f))) {
1798 f = g;
1799 continue;
1801 if (read_original_sector(wc, g) !=
1802 read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
1803 break;
1804 if (unlikely(g->write_in_progress))
1805 break;
1806 if (unlikely(!writecache_entry_is_committed(wc, g)))
1807 break;
1809 if (!WC_MODE_PMEM(wc)) {
1810 if (g != f + 1)
1811 break;
1814 n_walked++;
1815 //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
1816 // break;
1818 wc->writeback_size++;
1819 list_del(&g->lru);
1820 list_add(&g->lru, &wbl.list);
1821 wbl.size++;
1822 g->write_in_progress = true;
1823 g->wc_list_contiguous = BIO_MAX_PAGES;
1824 f = g;
1825 e->wc_list_contiguous++;
1826 if (unlikely(e->wc_list_contiguous == BIO_MAX_PAGES)) {
1827 if (unlikely(wc->writeback_all)) {
1828 next_node = rb_next(&f->rb_node);
1829 if (likely(next_node))
1830 g = container_of(next_node, struct wc_entry, rb_node);
1832 break;
1835 cond_resched();
1838 if (!list_empty(&skipped)) {
1839 list_splice_tail(&skipped, &wc->lru);
1841 * If we didn't do any progress, we must wait until some
1842 * writeback finishes to avoid burning CPU in a loop
1844 if (unlikely(!wbl.size))
1845 writecache_wait_for_writeback(wc);
1848 wc_unlock(wc);
1850 blk_start_plug(&plug);
1852 if (WC_MODE_PMEM(wc))
1853 __writecache_writeback_pmem(wc, &wbl);
1854 else
1855 __writecache_writeback_ssd(wc, &wbl);
1857 blk_finish_plug(&plug);
1859 if (unlikely(wc->writeback_all)) {
1860 wc_lock(wc);
1861 while (writecache_wait_for_writeback(wc));
1862 wc_unlock(wc);
1866 static int calculate_memory_size(uint64_t device_size, unsigned block_size,
1867 size_t *n_blocks_p, size_t *n_metadata_blocks_p)
1869 uint64_t n_blocks, offset;
1870 struct wc_entry e;
1872 n_blocks = device_size;
1873 do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
1875 while (1) {
1876 if (!n_blocks)
1877 return -ENOSPC;
1878 /* Verify the following entries[n_blocks] won't overflow */
1879 if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
1880 sizeof(struct wc_memory_entry)))
1881 return -EFBIG;
1882 offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
1883 offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
1884 if (offset + n_blocks * block_size <= device_size)
1885 break;
1886 n_blocks--;
1889 /* check if the bit field overflows */
1890 e.index = n_blocks;
1891 if (e.index != n_blocks)
1892 return -EFBIG;
1894 if (n_blocks_p)
1895 *n_blocks_p = n_blocks;
1896 if (n_metadata_blocks_p)
1897 *n_metadata_blocks_p = offset >> __ffs(block_size);
1898 return 0;
1901 static int init_memory(struct dm_writecache *wc)
1903 size_t b;
1904 int r;
1906 r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
1907 if (r)
1908 return r;
1910 r = writecache_alloc_entries(wc);
1911 if (r)
1912 return r;
1914 for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
1915 pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
1916 pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
1917 pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
1918 pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
1919 pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
1921 for (b = 0; b < wc->n_blocks; b++) {
1922 write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
1923 cond_resched();
1926 writecache_flush_all_metadata(wc);
1927 writecache_commit_flushed(wc, false);
1928 pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
1929 writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
1930 writecache_commit_flushed(wc, false);
1932 return 0;
1935 static void writecache_dtr(struct dm_target *ti)
1937 struct dm_writecache *wc = ti->private;
1939 if (!wc)
1940 return;
1942 if (wc->endio_thread)
1943 kthread_stop(wc->endio_thread);
1945 if (wc->flush_thread)
1946 kthread_stop(wc->flush_thread);
1948 bioset_exit(&wc->bio_set);
1950 mempool_exit(&wc->copy_pool);
1952 if (wc->writeback_wq)
1953 destroy_workqueue(wc->writeback_wq);
1955 if (wc->dev)
1956 dm_put_device(ti, wc->dev);
1958 if (wc->ssd_dev)
1959 dm_put_device(ti, wc->ssd_dev);
1961 if (wc->entries)
1962 vfree(wc->entries);
1964 if (wc->memory_map) {
1965 if (WC_MODE_PMEM(wc))
1966 persistent_memory_release(wc);
1967 else
1968 vfree(wc->memory_map);
1971 if (wc->dm_kcopyd)
1972 dm_kcopyd_client_destroy(wc->dm_kcopyd);
1974 if (wc->dm_io)
1975 dm_io_client_destroy(wc->dm_io);
1977 if (wc->dirty_bitmap)
1978 vfree(wc->dirty_bitmap);
1980 kfree(wc);
1983 static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
1985 struct dm_writecache *wc;
1986 struct dm_arg_set as;
1987 const char *string;
1988 unsigned opt_params;
1989 size_t offset, data_size;
1990 int i, r;
1991 char dummy;
1992 int high_wm_percent = HIGH_WATERMARK;
1993 int low_wm_percent = LOW_WATERMARK;
1994 uint64_t x;
1995 struct wc_memory_superblock s;
1997 static struct dm_arg _args[] = {
1998 {0, 10, "Invalid number of feature args"},
2001 as.argc = argc;
2002 as.argv = argv;
2004 wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
2005 if (!wc) {
2006 ti->error = "Cannot allocate writecache structure";
2007 r = -ENOMEM;
2008 goto bad;
2010 ti->private = wc;
2011 wc->ti = ti;
2013 mutex_init(&wc->lock);
2014 wc->max_age = MAX_AGE_UNSPECIFIED;
2015 writecache_poison_lists(wc);
2016 init_waitqueue_head(&wc->freelist_wait);
2017 timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
2018 timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0);
2020 for (i = 0; i < 2; i++) {
2021 atomic_set(&wc->bio_in_progress[i], 0);
2022 init_waitqueue_head(&wc->bio_in_progress_wait[i]);
2025 wc->dm_io = dm_io_client_create();
2026 if (IS_ERR(wc->dm_io)) {
2027 r = PTR_ERR(wc->dm_io);
2028 ti->error = "Unable to allocate dm-io client";
2029 wc->dm_io = NULL;
2030 goto bad;
2033 wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
2034 if (!wc->writeback_wq) {
2035 r = -ENOMEM;
2036 ti->error = "Could not allocate writeback workqueue";
2037 goto bad;
2039 INIT_WORK(&wc->writeback_work, writecache_writeback);
2040 INIT_WORK(&wc->flush_work, writecache_flush_work);
2042 raw_spin_lock_init(&wc->endio_list_lock);
2043 INIT_LIST_HEAD(&wc->endio_list);
2044 wc->endio_thread = kthread_create(writecache_endio_thread, wc, "writecache_endio");
2045 if (IS_ERR(wc->endio_thread)) {
2046 r = PTR_ERR(wc->endio_thread);
2047 wc->endio_thread = NULL;
2048 ti->error = "Couldn't spawn endio thread";
2049 goto bad;
2051 wake_up_process(wc->endio_thread);
2054 * Parse the mode (pmem or ssd)
2056 string = dm_shift_arg(&as);
2057 if (!string)
2058 goto bad_arguments;
2060 if (!strcasecmp(string, "s")) {
2061 wc->pmem_mode = false;
2062 } else if (!strcasecmp(string, "p")) {
2063 #ifdef DM_WRITECACHE_HAS_PMEM
2064 wc->pmem_mode = true;
2065 wc->writeback_fua = true;
2066 #else
2068 * If the architecture doesn't support persistent memory or
2069 * the kernel doesn't support any DAX drivers, this driver can
2070 * only be used in SSD-only mode.
2072 r = -EOPNOTSUPP;
2073 ti->error = "Persistent memory or DAX not supported on this system";
2074 goto bad;
2075 #endif
2076 } else {
2077 goto bad_arguments;
2080 if (WC_MODE_PMEM(wc)) {
2081 r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
2082 offsetof(struct writeback_struct, bio),
2083 BIOSET_NEED_BVECS);
2084 if (r) {
2085 ti->error = "Could not allocate bio set";
2086 goto bad;
2088 } else {
2089 r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
2090 if (r) {
2091 ti->error = "Could not allocate mempool";
2092 goto bad;
2097 * Parse the origin data device
2099 string = dm_shift_arg(&as);
2100 if (!string)
2101 goto bad_arguments;
2102 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
2103 if (r) {
2104 ti->error = "Origin data device lookup failed";
2105 goto bad;
2109 * Parse cache data device (be it pmem or ssd)
2111 string = dm_shift_arg(&as);
2112 if (!string)
2113 goto bad_arguments;
2115 r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
2116 if (r) {
2117 ti->error = "Cache data device lookup failed";
2118 goto bad;
2120 wc->memory_map_size = i_size_read(wc->ssd_dev->bdev->bd_inode);
2123 * Parse the cache block size
2125 string = dm_shift_arg(&as);
2126 if (!string)
2127 goto bad_arguments;
2128 if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
2129 wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
2130 (wc->block_size & (wc->block_size - 1))) {
2131 r = -EINVAL;
2132 ti->error = "Invalid block size";
2133 goto bad;
2135 if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) ||
2136 wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) {
2137 r = -EINVAL;
2138 ti->error = "Block size is smaller than device logical block size";
2139 goto bad;
2141 wc->block_size_bits = __ffs(wc->block_size);
2143 wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
2144 wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
2145 wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
2148 * Parse optional arguments
2150 r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2151 if (r)
2152 goto bad;
2154 while (opt_params) {
2155 string = dm_shift_arg(&as), opt_params--;
2156 if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2157 unsigned long long start_sector;
2158 string = dm_shift_arg(&as), opt_params--;
2159 if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2160 goto invalid_optional;
2161 wc->start_sector = start_sector;
2162 if (wc->start_sector != start_sector ||
2163 wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2164 goto invalid_optional;
2165 } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
2166 string = dm_shift_arg(&as), opt_params--;
2167 if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
2168 goto invalid_optional;
2169 if (high_wm_percent < 0 || high_wm_percent > 100)
2170 goto invalid_optional;
2171 wc->high_wm_percent_set = true;
2172 } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
2173 string = dm_shift_arg(&as), opt_params--;
2174 if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
2175 goto invalid_optional;
2176 if (low_wm_percent < 0 || low_wm_percent > 100)
2177 goto invalid_optional;
2178 wc->low_wm_percent_set = true;
2179 } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
2180 string = dm_shift_arg(&as), opt_params--;
2181 if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
2182 goto invalid_optional;
2183 wc->max_writeback_jobs_set = true;
2184 } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
2185 string = dm_shift_arg(&as), opt_params--;
2186 if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
2187 goto invalid_optional;
2188 wc->autocommit_blocks_set = true;
2189 } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
2190 unsigned autocommit_msecs;
2191 string = dm_shift_arg(&as), opt_params--;
2192 if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
2193 goto invalid_optional;
2194 if (autocommit_msecs > 3600000)
2195 goto invalid_optional;
2196 wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2197 wc->autocommit_time_set = true;
2198 } else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
2199 unsigned max_age_msecs;
2200 string = dm_shift_arg(&as), opt_params--;
2201 if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
2202 goto invalid_optional;
2203 if (max_age_msecs > 86400000)
2204 goto invalid_optional;
2205 wc->max_age = msecs_to_jiffies(max_age_msecs);
2206 } else if (!strcasecmp(string, "cleaner")) {
2207 wc->cleaner = true;
2208 } else if (!strcasecmp(string, "fua")) {
2209 if (WC_MODE_PMEM(wc)) {
2210 wc->writeback_fua = true;
2211 wc->writeback_fua_set = true;
2212 } else goto invalid_optional;
2213 } else if (!strcasecmp(string, "nofua")) {
2214 if (WC_MODE_PMEM(wc)) {
2215 wc->writeback_fua = false;
2216 wc->writeback_fua_set = true;
2217 } else goto invalid_optional;
2218 } else {
2219 invalid_optional:
2220 r = -EINVAL;
2221 ti->error = "Invalid optional argument";
2222 goto bad;
2226 if (high_wm_percent < low_wm_percent) {
2227 r = -EINVAL;
2228 ti->error = "High watermark must be greater than or equal to low watermark";
2229 goto bad;
2232 if (WC_MODE_PMEM(wc)) {
2233 r = persistent_memory_claim(wc);
2234 if (r) {
2235 ti->error = "Unable to map persistent memory for cache";
2236 goto bad;
2238 } else {
2239 size_t n_blocks, n_metadata_blocks;
2240 uint64_t n_bitmap_bits;
2242 wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2244 bio_list_init(&wc->flush_list);
2245 wc->flush_thread = kthread_create(writecache_flush_thread, wc, "dm_writecache_flush");
2246 if (IS_ERR(wc->flush_thread)) {
2247 r = PTR_ERR(wc->flush_thread);
2248 wc->flush_thread = NULL;
2249 ti->error = "Couldn't spawn flush thread";
2250 goto bad;
2252 wake_up_process(wc->flush_thread);
2254 r = calculate_memory_size(wc->memory_map_size, wc->block_size,
2255 &n_blocks, &n_metadata_blocks);
2256 if (r) {
2257 ti->error = "Invalid device size";
2258 goto bad;
2261 n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
2262 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
2263 /* this is limitation of test_bit functions */
2264 if (n_bitmap_bits > 1U << 31) {
2265 r = -EFBIG;
2266 ti->error = "Invalid device size";
2267 goto bad;
2270 wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
2271 if (!wc->memory_map) {
2272 r = -ENOMEM;
2273 ti->error = "Unable to allocate memory for metadata";
2274 goto bad;
2277 wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2278 if (IS_ERR(wc->dm_kcopyd)) {
2279 r = PTR_ERR(wc->dm_kcopyd);
2280 ti->error = "Unable to allocate dm-kcopyd client";
2281 wc->dm_kcopyd = NULL;
2282 goto bad;
2285 wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
2286 wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
2287 BITS_PER_LONG * sizeof(unsigned long);
2288 wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
2289 if (!wc->dirty_bitmap) {
2290 r = -ENOMEM;
2291 ti->error = "Unable to allocate dirty bitmap";
2292 goto bad;
2295 r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT);
2296 if (r) {
2297 ti->error = "Unable to read first block of metadata";
2298 goto bad;
2302 r = memcpy_mcsafe(&s, sb(wc), sizeof(struct wc_memory_superblock));
2303 if (r) {
2304 ti->error = "Hardware memory error when reading superblock";
2305 goto bad;
2307 if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
2308 r = init_memory(wc);
2309 if (r) {
2310 ti->error = "Unable to initialize device";
2311 goto bad;
2313 r = memcpy_mcsafe(&s, sb(wc), sizeof(struct wc_memory_superblock));
2314 if (r) {
2315 ti->error = "Hardware memory error when reading superblock";
2316 goto bad;
2320 if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
2321 ti->error = "Invalid magic in the superblock";
2322 r = -EINVAL;
2323 goto bad;
2326 if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
2327 ti->error = "Invalid version in the superblock";
2328 r = -EINVAL;
2329 goto bad;
2332 if (le32_to_cpu(s.block_size) != wc->block_size) {
2333 ti->error = "Block size does not match superblock";
2334 r = -EINVAL;
2335 goto bad;
2338 wc->n_blocks = le64_to_cpu(s.n_blocks);
2340 offset = wc->n_blocks * sizeof(struct wc_memory_entry);
2341 if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
2342 overflow:
2343 ti->error = "Overflow in size calculation";
2344 r = -EINVAL;
2345 goto bad;
2347 offset += sizeof(struct wc_memory_superblock);
2348 if (offset < sizeof(struct wc_memory_superblock))
2349 goto overflow;
2350 offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
2351 data_size = wc->n_blocks * (size_t)wc->block_size;
2352 if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
2353 (offset + data_size < offset))
2354 goto overflow;
2355 if (offset + data_size > wc->memory_map_size) {
2356 ti->error = "Memory area is too small";
2357 r = -EINVAL;
2358 goto bad;
2361 wc->metadata_sectors = offset >> SECTOR_SHIFT;
2362 wc->block_start = (char *)sb(wc) + offset;
2364 x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
2365 x += 50;
2366 do_div(x, 100);
2367 wc->freelist_high_watermark = x;
2368 x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
2369 x += 50;
2370 do_div(x, 100);
2371 wc->freelist_low_watermark = x;
2373 if (wc->cleaner)
2374 activate_cleaner(wc);
2376 r = writecache_alloc_entries(wc);
2377 if (r) {
2378 ti->error = "Cannot allocate memory";
2379 goto bad;
2382 ti->num_flush_bios = 1;
2383 ti->flush_supported = true;
2384 ti->num_discard_bios = 1;
2386 if (WC_MODE_PMEM(wc))
2387 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
2389 return 0;
2391 bad_arguments:
2392 r = -EINVAL;
2393 ti->error = "Bad arguments";
2394 bad:
2395 writecache_dtr(ti);
2396 return r;
2399 static void writecache_status(struct dm_target *ti, status_type_t type,
2400 unsigned status_flags, char *result, unsigned maxlen)
2402 struct dm_writecache *wc = ti->private;
2403 unsigned extra_args;
2404 unsigned sz = 0;
2405 uint64_t x;
2407 switch (type) {
2408 case STATUSTYPE_INFO:
2409 DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),
2410 (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2411 (unsigned long long)wc->writeback_size);
2412 break;
2413 case STATUSTYPE_TABLE:
2414 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
2415 wc->dev->name, wc->ssd_dev->name, wc->block_size);
2416 extra_args = 0;
2417 if (wc->start_sector)
2418 extra_args += 2;
2419 if (wc->high_wm_percent_set && !wc->cleaner)
2420 extra_args += 2;
2421 if (wc->low_wm_percent_set && !wc->cleaner)
2422 extra_args += 2;
2423 if (wc->max_writeback_jobs_set)
2424 extra_args += 2;
2425 if (wc->autocommit_blocks_set)
2426 extra_args += 2;
2427 if (wc->autocommit_time_set)
2428 extra_args += 2;
2429 if (wc->cleaner)
2430 extra_args++;
2431 if (wc->writeback_fua_set)
2432 extra_args++;
2434 DMEMIT("%u", extra_args);
2435 if (wc->start_sector)
2436 DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2437 if (wc->high_wm_percent_set && !wc->cleaner) {
2438 x = (uint64_t)wc->freelist_high_watermark * 100;
2439 x += wc->n_blocks / 2;
2440 do_div(x, (size_t)wc->n_blocks);
2441 DMEMIT(" high_watermark %u", 100 - (unsigned)x);
2443 if (wc->low_wm_percent_set && !wc->cleaner) {
2444 x = (uint64_t)wc->freelist_low_watermark * 100;
2445 x += wc->n_blocks / 2;
2446 do_div(x, (size_t)wc->n_blocks);
2447 DMEMIT(" low_watermark %u", 100 - (unsigned)x);
2449 if (wc->max_writeback_jobs_set)
2450 DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
2451 if (wc->autocommit_blocks_set)
2452 DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
2453 if (wc->autocommit_time_set)
2454 DMEMIT(" autocommit_time %u", jiffies_to_msecs(wc->autocommit_jiffies));
2455 if (wc->max_age != MAX_AGE_UNSPECIFIED)
2456 DMEMIT(" max_age %u", jiffies_to_msecs(wc->max_age));
2457 if (wc->cleaner)
2458 DMEMIT(" cleaner");
2459 if (wc->writeback_fua_set)
2460 DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2461 break;
2465 static struct target_type writecache_target = {
2466 .name = "writecache",
2467 .version = {1, 3, 0},
2468 .module = THIS_MODULE,
2469 .ctr = writecache_ctr,
2470 .dtr = writecache_dtr,
2471 .status = writecache_status,
2472 .postsuspend = writecache_suspend,
2473 .resume = writecache_resume,
2474 .message = writecache_message,
2475 .map = writecache_map,
2476 .end_io = writecache_end_io,
2477 .iterate_devices = writecache_iterate_devices,
2478 .io_hints = writecache_io_hints,
2481 static int __init dm_writecache_init(void)
2483 int r;
2485 r = dm_register_target(&writecache_target);
2486 if (r < 0) {
2487 DMERR("register failed %d", r);
2488 return r;
2491 return 0;
2494 static void __exit dm_writecache_exit(void)
2496 dm_unregister_target(&writecache_target);
2499 module_init(dm_writecache_init);
2500 module_exit(dm_writecache_exit);
2502 MODULE_DESCRIPTION(DM_NAME " writecache target");
2503 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2504 MODULE_LICENSE("GPL");