dm writecache: remove unused gfp_t argument from wc_add_block()
[linux-2.6-block.git] / drivers / md / dm-writecache.c
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (C) 2018 Red Hat. All rights reserved.
4  *
5  * This file is released under the GPL.
6  */
7
8 #include <linux/device-mapper.h>
9 #include <linux/module.h>
10 #include <linux/init.h>
11 #include <linux/vmalloc.h>
12 #include <linux/kthread.h>
13 #include <linux/dm-io.h>
14 #include <linux/dm-kcopyd.h>
15 #include <linux/dax.h>
16 #include <linux/pfn_t.h>
17 #include <linux/libnvdimm.h>
18
19 #define DM_MSG_PREFIX "writecache"
20
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
31
32 #define BITMAP_GRANULARITY      65536
33 #if BITMAP_GRANULARITY < PAGE_SIZE
34 #undef BITMAP_GRANULARITY
35 #define BITMAP_GRANULARITY      PAGE_SIZE
36 #endif
37
38 #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_DAX_DRIVER)
39 #define DM_WRITECACHE_HAS_PMEM
40 #endif
41
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
51
52 #if IS_ENABLED(CONFIG_ARCH_HAS_COPY_MC) && defined(DM_WRITECACHE_HAS_PMEM)
53 #define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
54 #endif
55
56 #define MEMORY_SUPERBLOCK_MAGIC         0x23489321
57 #define MEMORY_SUPERBLOCK_VERSION       1
58
59 struct wc_memory_entry {
60         __le64 original_sector;
61         __le64 seq_count;
62 };
63
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;
73                 };
74                 __le64 padding[8];
75         };
76         struct wc_memory_entry entries[];
77 };
78
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
85                 :1
86 #endif
87         ;
88         unsigned long index
89 #if BITS_PER_LONG == 64
90                 :47
91 #endif
92         ;
93         unsigned long age;
94 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
95         uint64_t original_sector;
96         uint64_t seq_count;
97 #endif
98 };
99
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))
108
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;
117                 };
118         };
119         struct rb_root tree;
120
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;
126
127         unsigned uncommitted_blocks;
128         unsigned autocommit_blocks;
129         unsigned max_writeback_jobs;
130
131         int error;
132
133         unsigned long autocommit_jiffies;
134         struct timer_list autocommit_timer;
135         struct wait_queue_head freelist_wait;
136
137         struct timer_list max_age_timer;
138
139         atomic_t bio_in_progress[2];
140         struct wait_queue_head bio_in_progress_wait[2];
141
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         sector_t data_device_sectors;
152         void *block_start;
153         struct wc_entry *entries;
154         unsigned block_size;
155         unsigned char block_size_bits;
156
157         bool pmem_mode:1;
158         bool writeback_fua:1;
159
160         bool overwrote_committed:1;
161         bool memory_vmapped:1;
162
163         bool start_sector_set:1;
164         bool high_wm_percent_set:1;
165         bool low_wm_percent_set:1;
166         bool max_writeback_jobs_set:1;
167         bool autocommit_blocks_set:1;
168         bool autocommit_time_set:1;
169         bool max_age_set:1;
170         bool writeback_fua_set:1;
171         bool flush_on_suspend:1;
172         bool cleaner:1;
173         bool cleaner_set:1;
174
175         unsigned high_wm_percent_value;
176         unsigned low_wm_percent_value;
177         unsigned autocommit_time_value;
178         unsigned max_age_value;
179
180         unsigned writeback_all;
181         struct workqueue_struct *writeback_wq;
182         struct work_struct writeback_work;
183         struct work_struct flush_work;
184
185         struct dm_io_client *dm_io;
186
187         raw_spinlock_t endio_list_lock;
188         struct list_head endio_list;
189         struct task_struct *endio_thread;
190
191         struct task_struct *flush_thread;
192         struct bio_list flush_list;
193
194         struct dm_kcopyd_client *dm_kcopyd;
195         unsigned long *dirty_bitmap;
196         unsigned dirty_bitmap_size;
197
198         struct bio_set bio_set;
199         mempool_t copy_pool;
200 };
201
202 #define WB_LIST_INLINE          16
203
204 struct writeback_struct {
205         struct list_head endio_entry;
206         struct dm_writecache *wc;
207         struct wc_entry **wc_list;
208         unsigned wc_list_n;
209         struct wc_entry *wc_list_inline[WB_LIST_INLINE];
210         struct bio bio;
211 };
212
213 struct copy_struct {
214         struct list_head endio_entry;
215         struct dm_writecache *wc;
216         struct wc_entry *e;
217         unsigned n_entries;
218         int error;
219 };
220
221 DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
222                                             "A percentage of time allocated for data copying");
223
224 static void wc_lock(struct dm_writecache *wc)
225 {
226         mutex_lock(&wc->lock);
227 }
228
229 static void wc_unlock(struct dm_writecache *wc)
230 {
231         mutex_unlock(&wc->lock);
232 }
233
234 #ifdef DM_WRITECACHE_HAS_PMEM
235 static int persistent_memory_claim(struct dm_writecache *wc)
236 {
237         int r;
238         loff_t s;
239         long p, da;
240         pfn_t pfn;
241         int id;
242         struct page **pages;
243         sector_t offset;
244
245         wc->memory_vmapped = false;
246
247         s = wc->memory_map_size;
248         p = s >> PAGE_SHIFT;
249         if (!p) {
250                 r = -EINVAL;
251                 goto err1;
252         }
253         if (p != s >> PAGE_SHIFT) {
254                 r = -EOVERFLOW;
255                 goto err1;
256         }
257
258         offset = get_start_sect(wc->ssd_dev->bdev);
259         if (offset & (PAGE_SIZE / 512 - 1)) {
260                 r = -EINVAL;
261                 goto err1;
262         }
263         offset >>= PAGE_SHIFT - 9;
264
265         id = dax_read_lock();
266
267         da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, &wc->memory_map, &pfn);
268         if (da < 0) {
269                 wc->memory_map = NULL;
270                 r = da;
271                 goto err2;
272         }
273         if (!pfn_t_has_page(pfn)) {
274                 wc->memory_map = NULL;
275                 r = -EOPNOTSUPP;
276                 goto err2;
277         }
278         if (da != p) {
279                 long i;
280                 wc->memory_map = NULL;
281                 pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
282                 if (!pages) {
283                         r = -ENOMEM;
284                         goto err2;
285                 }
286                 i = 0;
287                 do {
288                         long daa;
289                         daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i, p - i,
290                                                 NULL, &pfn);
291                         if (daa <= 0) {
292                                 r = daa ? daa : -EINVAL;
293                                 goto err3;
294                         }
295                         if (!pfn_t_has_page(pfn)) {
296                                 r = -EOPNOTSUPP;
297                                 goto err3;
298                         }
299                         while (daa-- && i < p) {
300                                 pages[i++] = pfn_t_to_page(pfn);
301                                 pfn.val++;
302                                 if (!(i & 15))
303                                         cond_resched();
304                         }
305                 } while (i < p);
306                 wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
307                 if (!wc->memory_map) {
308                         r = -ENOMEM;
309                         goto err3;
310                 }
311                 kvfree(pages);
312                 wc->memory_vmapped = true;
313         }
314
315         dax_read_unlock(id);
316
317         wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
318         wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
319
320         return 0;
321 err3:
322         kvfree(pages);
323 err2:
324         dax_read_unlock(id);
325 err1:
326         return r;
327 }
328 #else
329 static int persistent_memory_claim(struct dm_writecache *wc)
330 {
331         return -EOPNOTSUPP;
332 }
333 #endif
334
335 static void persistent_memory_release(struct dm_writecache *wc)
336 {
337         if (wc->memory_vmapped)
338                 vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
339 }
340
341 static struct page *persistent_memory_page(void *addr)
342 {
343         if (is_vmalloc_addr(addr))
344                 return vmalloc_to_page(addr);
345         else
346                 return virt_to_page(addr);
347 }
348
349 static unsigned persistent_memory_page_offset(void *addr)
350 {
351         return (unsigned long)addr & (PAGE_SIZE - 1);
352 }
353
354 static void persistent_memory_flush_cache(void *ptr, size_t size)
355 {
356         if (is_vmalloc_addr(ptr))
357                 flush_kernel_vmap_range(ptr, size);
358 }
359
360 static void persistent_memory_invalidate_cache(void *ptr, size_t size)
361 {
362         if (is_vmalloc_addr(ptr))
363                 invalidate_kernel_vmap_range(ptr, size);
364 }
365
366 static struct wc_memory_superblock *sb(struct dm_writecache *wc)
367 {
368         return wc->memory_map;
369 }
370
371 static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
372 {
373         return &sb(wc)->entries[e->index];
374 }
375
376 static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
377 {
378         return (char *)wc->block_start + (e->index << wc->block_size_bits);
379 }
380
381 static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
382 {
383         return wc->start_sector + wc->metadata_sectors +
384                 ((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
385 }
386
387 static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
388 {
389 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
390         return e->original_sector;
391 #else
392         return le64_to_cpu(memory_entry(wc, e)->original_sector);
393 #endif
394 }
395
396 static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
397 {
398 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
399         return e->seq_count;
400 #else
401         return le64_to_cpu(memory_entry(wc, e)->seq_count);
402 #endif
403 }
404
405 static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
406 {
407 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
408         e->seq_count = -1;
409 #endif
410         pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
411 }
412
413 static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
414                                             uint64_t original_sector, uint64_t seq_count)
415 {
416         struct wc_memory_entry me;
417 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
418         e->original_sector = original_sector;
419         e->seq_count = seq_count;
420 #endif
421         me.original_sector = cpu_to_le64(original_sector);
422         me.seq_count = cpu_to_le64(seq_count);
423         pmem_assign(*memory_entry(wc, e), me);
424 }
425
426 #define writecache_error(wc, err, msg, arg...)                          \
427 do {                                                                    \
428         if (!cmpxchg(&(wc)->error, 0, err))                             \
429                 DMERR(msg, ##arg);                                      \
430         wake_up(&(wc)->freelist_wait);                                  \
431 } while (0)
432
433 #define writecache_has_error(wc)        (unlikely(READ_ONCE((wc)->error)))
434
435 static void writecache_flush_all_metadata(struct dm_writecache *wc)
436 {
437         if (!WC_MODE_PMEM(wc))
438                 memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
439 }
440
441 static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
442 {
443         if (!WC_MODE_PMEM(wc))
444                 __set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
445                           wc->dirty_bitmap);
446 }
447
448 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
449
450 struct io_notify {
451         struct dm_writecache *wc;
452         struct completion c;
453         atomic_t count;
454 };
455
456 static void writecache_notify_io(unsigned long error, void *context)
457 {
458         struct io_notify *endio = context;
459
460         if (unlikely(error != 0))
461                 writecache_error(endio->wc, -EIO, "error writing metadata");
462         BUG_ON(atomic_read(&endio->count) <= 0);
463         if (atomic_dec_and_test(&endio->count))
464                 complete(&endio->c);
465 }
466
467 static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
468 {
469         wait_event(wc->bio_in_progress_wait[direction],
470                    !atomic_read(&wc->bio_in_progress[direction]));
471 }
472
473 static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
474 {
475         struct dm_io_region region;
476         struct dm_io_request req;
477         struct io_notify endio = {
478                 wc,
479                 COMPLETION_INITIALIZER_ONSTACK(endio.c),
480                 ATOMIC_INIT(1),
481         };
482         unsigned bitmap_bits = wc->dirty_bitmap_size * 8;
483         unsigned i = 0;
484
485         while (1) {
486                 unsigned j;
487                 i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i);
488                 if (unlikely(i == bitmap_bits))
489                         break;
490                 j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i);
491
492                 region.bdev = wc->ssd_dev->bdev;
493                 region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
494                 region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
495
496                 if (unlikely(region.sector >= wc->metadata_sectors))
497                         break;
498                 if (unlikely(region.sector + region.count > wc->metadata_sectors))
499                         region.count = wc->metadata_sectors - region.sector;
500
501                 region.sector += wc->start_sector;
502                 atomic_inc(&endio.count);
503                 req.bi_op = REQ_OP_WRITE;
504                 req.bi_op_flags = REQ_SYNC;
505                 req.mem.type = DM_IO_VMA;
506                 req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
507                 req.client = wc->dm_io;
508                 req.notify.fn = writecache_notify_io;
509                 req.notify.context = &endio;
510
511                 /* writing via async dm-io (implied by notify.fn above) won't return an error */
512                 (void) dm_io(&req, 1, &region, NULL);
513                 i = j;
514         }
515
516         writecache_notify_io(0, &endio);
517         wait_for_completion_io(&endio.c);
518
519         if (wait_for_ios)
520                 writecache_wait_for_ios(wc, WRITE);
521
522         writecache_disk_flush(wc, wc->ssd_dev);
523
524         memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
525 }
526
527 static void ssd_commit_superblock(struct dm_writecache *wc)
528 {
529         int r;
530         struct dm_io_region region;
531         struct dm_io_request req;
532
533         region.bdev = wc->ssd_dev->bdev;
534         region.sector = 0;
535         region.count = PAGE_SIZE >> SECTOR_SHIFT;
536
537         if (unlikely(region.sector + region.count > wc->metadata_sectors))
538                 region.count = wc->metadata_sectors - region.sector;
539
540         region.sector += wc->start_sector;
541
542         req.bi_op = REQ_OP_WRITE;
543         req.bi_op_flags = REQ_SYNC | REQ_FUA;
544         req.mem.type = DM_IO_VMA;
545         req.mem.ptr.vma = (char *)wc->memory_map;
546         req.client = wc->dm_io;
547         req.notify.fn = NULL;
548         req.notify.context = NULL;
549
550         r = dm_io(&req, 1, &region, NULL);
551         if (unlikely(r))
552                 writecache_error(wc, r, "error writing superblock");
553 }
554
555 static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
556 {
557         if (WC_MODE_PMEM(wc))
558                 pmem_wmb();
559         else
560                 ssd_commit_flushed(wc, wait_for_ios);
561 }
562
563 static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
564 {
565         int r;
566         struct dm_io_region region;
567         struct dm_io_request req;
568
569         region.bdev = dev->bdev;
570         region.sector = 0;
571         region.count = 0;
572         req.bi_op = REQ_OP_WRITE;
573         req.bi_op_flags = REQ_PREFLUSH;
574         req.mem.type = DM_IO_KMEM;
575         req.mem.ptr.addr = NULL;
576         req.client = wc->dm_io;
577         req.notify.fn = NULL;
578
579         r = dm_io(&req, 1, &region, NULL);
580         if (unlikely(r))
581                 writecache_error(wc, r, "error flushing metadata: %d", r);
582 }
583
584 #define WFE_RETURN_FOLLOWING    1
585 #define WFE_LOWEST_SEQ          2
586
587 static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
588                                               uint64_t block, int flags)
589 {
590         struct wc_entry *e;
591         struct rb_node *node = wc->tree.rb_node;
592
593         if (unlikely(!node))
594                 return NULL;
595
596         while (1) {
597                 e = container_of(node, struct wc_entry, rb_node);
598                 if (read_original_sector(wc, e) == block)
599                         break;
600
601                 node = (read_original_sector(wc, e) >= block ?
602                         e->rb_node.rb_left : e->rb_node.rb_right);
603                 if (unlikely(!node)) {
604                         if (!(flags & WFE_RETURN_FOLLOWING))
605                                 return NULL;
606                         if (read_original_sector(wc, e) >= block) {
607                                 return e;
608                         } else {
609                                 node = rb_next(&e->rb_node);
610                                 if (unlikely(!node))
611                                         return NULL;
612                                 e = container_of(node, struct wc_entry, rb_node);
613                                 return e;
614                         }
615                 }
616         }
617
618         while (1) {
619                 struct wc_entry *e2;
620                 if (flags & WFE_LOWEST_SEQ)
621                         node = rb_prev(&e->rb_node);
622                 else
623                         node = rb_next(&e->rb_node);
624                 if (unlikely(!node))
625                         return e;
626                 e2 = container_of(node, struct wc_entry, rb_node);
627                 if (read_original_sector(wc, e2) != block)
628                         return e;
629                 e = e2;
630         }
631 }
632
633 static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
634 {
635         struct wc_entry *e;
636         struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
637
638         while (*node) {
639                 e = container_of(*node, struct wc_entry, rb_node);
640                 parent = &e->rb_node;
641                 if (read_original_sector(wc, e) > read_original_sector(wc, ins))
642                         node = &parent->rb_left;
643                 else
644                         node = &parent->rb_right;
645         }
646         rb_link_node(&ins->rb_node, parent, node);
647         rb_insert_color(&ins->rb_node, &wc->tree);
648         list_add(&ins->lru, &wc->lru);
649         ins->age = jiffies;
650 }
651
652 static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
653 {
654         list_del(&e->lru);
655         rb_erase(&e->rb_node, &wc->tree);
656 }
657
658 static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
659 {
660         if (WC_MODE_SORT_FREELIST(wc)) {
661                 struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
662                 if (unlikely(!*node))
663                         wc->current_free = e;
664                 while (*node) {
665                         parent = *node;
666                         if (&e->rb_node < *node)
667                                 node = &parent->rb_left;
668                         else
669                                 node = &parent->rb_right;
670                 }
671                 rb_link_node(&e->rb_node, parent, node);
672                 rb_insert_color(&e->rb_node, &wc->freetree);
673         } else {
674                 list_add_tail(&e->lru, &wc->freelist);
675         }
676         wc->freelist_size++;
677 }
678
679 static inline void writecache_verify_watermark(struct dm_writecache *wc)
680 {
681         if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
682                 queue_work(wc->writeback_wq, &wc->writeback_work);
683 }
684
685 static void writecache_max_age_timer(struct timer_list *t)
686 {
687         struct dm_writecache *wc = from_timer(wc, t, max_age_timer);
688
689         if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) {
690                 queue_work(wc->writeback_wq, &wc->writeback_work);
691                 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
692         }
693 }
694
695 static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector)
696 {
697         struct wc_entry *e;
698
699         if (WC_MODE_SORT_FREELIST(wc)) {
700                 struct rb_node *next;
701                 if (unlikely(!wc->current_free))
702                         return NULL;
703                 e = wc->current_free;
704                 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
705                         return NULL;
706                 next = rb_next(&e->rb_node);
707                 rb_erase(&e->rb_node, &wc->freetree);
708                 if (unlikely(!next))
709                         next = rb_first(&wc->freetree);
710                 wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
711         } else {
712                 if (unlikely(list_empty(&wc->freelist)))
713                         return NULL;
714                 e = container_of(wc->freelist.next, struct wc_entry, lru);
715                 if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
716                         return NULL;
717                 list_del(&e->lru);
718         }
719         wc->freelist_size--;
720
721         writecache_verify_watermark(wc);
722
723         return e;
724 }
725
726 static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
727 {
728         writecache_unlink(wc, e);
729         writecache_add_to_freelist(wc, e);
730         clear_seq_count(wc, e);
731         writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
732         if (unlikely(waitqueue_active(&wc->freelist_wait)))
733                 wake_up(&wc->freelist_wait);
734 }
735
736 static void writecache_wait_on_freelist(struct dm_writecache *wc)
737 {
738         DEFINE_WAIT(wait);
739
740         prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
741         wc_unlock(wc);
742         io_schedule();
743         finish_wait(&wc->freelist_wait, &wait);
744         wc_lock(wc);
745 }
746
747 static void writecache_poison_lists(struct dm_writecache *wc)
748 {
749         /*
750          * Catch incorrect access to these values while the device is suspended.
751          */
752         memset(&wc->tree, -1, sizeof wc->tree);
753         wc->lru.next = LIST_POISON1;
754         wc->lru.prev = LIST_POISON2;
755         wc->freelist.next = LIST_POISON1;
756         wc->freelist.prev = LIST_POISON2;
757 }
758
759 static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
760 {
761         writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
762         if (WC_MODE_PMEM(wc))
763                 writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
764 }
765
766 static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
767 {
768         return read_seq_count(wc, e) < wc->seq_count;
769 }
770
771 static void writecache_flush(struct dm_writecache *wc)
772 {
773         struct wc_entry *e, *e2;
774         bool need_flush_after_free;
775
776         wc->uncommitted_blocks = 0;
777         del_timer(&wc->autocommit_timer);
778
779         if (list_empty(&wc->lru))
780                 return;
781
782         e = container_of(wc->lru.next, struct wc_entry, lru);
783         if (writecache_entry_is_committed(wc, e)) {
784                 if (wc->overwrote_committed) {
785                         writecache_wait_for_ios(wc, WRITE);
786                         writecache_disk_flush(wc, wc->ssd_dev);
787                         wc->overwrote_committed = false;
788                 }
789                 return;
790         }
791         while (1) {
792                 writecache_flush_entry(wc, e);
793                 if (unlikely(e->lru.next == &wc->lru))
794                         break;
795                 e2 = container_of(e->lru.next, struct wc_entry, lru);
796                 if (writecache_entry_is_committed(wc, e2))
797                         break;
798                 e = e2;
799                 cond_resched();
800         }
801         writecache_commit_flushed(wc, true);
802
803         wc->seq_count++;
804         pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
805         if (WC_MODE_PMEM(wc))
806                 writecache_commit_flushed(wc, false);
807         else
808                 ssd_commit_superblock(wc);
809
810         wc->overwrote_committed = false;
811
812         need_flush_after_free = false;
813         while (1) {
814                 /* Free another committed entry with lower seq-count */
815                 struct rb_node *rb_node = rb_prev(&e->rb_node);
816
817                 if (rb_node) {
818                         e2 = container_of(rb_node, struct wc_entry, rb_node);
819                         if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
820                             likely(!e2->write_in_progress)) {
821                                 writecache_free_entry(wc, e2);
822                                 need_flush_after_free = true;
823                         }
824                 }
825                 if (unlikely(e->lru.prev == &wc->lru))
826                         break;
827                 e = container_of(e->lru.prev, struct wc_entry, lru);
828                 cond_resched();
829         }
830
831         if (need_flush_after_free)
832                 writecache_commit_flushed(wc, false);
833 }
834
835 static void writecache_flush_work(struct work_struct *work)
836 {
837         struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
838
839         wc_lock(wc);
840         writecache_flush(wc);
841         wc_unlock(wc);
842 }
843
844 static void writecache_autocommit_timer(struct timer_list *t)
845 {
846         struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
847         if (!writecache_has_error(wc))
848                 queue_work(wc->writeback_wq, &wc->flush_work);
849 }
850
851 static void writecache_schedule_autocommit(struct dm_writecache *wc)
852 {
853         if (!timer_pending(&wc->autocommit_timer))
854                 mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
855 }
856
857 static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
858 {
859         struct wc_entry *e;
860         bool discarded_something = false;
861
862         e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
863         if (unlikely(!e))
864                 return;
865
866         while (read_original_sector(wc, e) < end) {
867                 struct rb_node *node = rb_next(&e->rb_node);
868
869                 if (likely(!e->write_in_progress)) {
870                         if (!discarded_something) {
871                                 if (!WC_MODE_PMEM(wc)) {
872                                         writecache_wait_for_ios(wc, READ);
873                                         writecache_wait_for_ios(wc, WRITE);
874                                 }
875                                 discarded_something = true;
876                         }
877                         if (!writecache_entry_is_committed(wc, e))
878                                 wc->uncommitted_blocks--;
879                         writecache_free_entry(wc, e);
880                 }
881
882                 if (unlikely(!node))
883                         break;
884
885                 e = container_of(node, struct wc_entry, rb_node);
886         }
887
888         if (discarded_something)
889                 writecache_commit_flushed(wc, false);
890 }
891
892 static bool writecache_wait_for_writeback(struct dm_writecache *wc)
893 {
894         if (wc->writeback_size) {
895                 writecache_wait_on_freelist(wc);
896                 return true;
897         }
898         return false;
899 }
900
901 static void writecache_suspend(struct dm_target *ti)
902 {
903         struct dm_writecache *wc = ti->private;
904         bool flush_on_suspend;
905
906         del_timer_sync(&wc->autocommit_timer);
907         del_timer_sync(&wc->max_age_timer);
908
909         wc_lock(wc);
910         writecache_flush(wc);
911         flush_on_suspend = wc->flush_on_suspend;
912         if (flush_on_suspend) {
913                 wc->flush_on_suspend = false;
914                 wc->writeback_all++;
915                 queue_work(wc->writeback_wq, &wc->writeback_work);
916         }
917         wc_unlock(wc);
918
919         drain_workqueue(wc->writeback_wq);
920
921         wc_lock(wc);
922         if (flush_on_suspend)
923                 wc->writeback_all--;
924         while (writecache_wait_for_writeback(wc));
925
926         if (WC_MODE_PMEM(wc))
927                 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
928
929         writecache_poison_lists(wc);
930
931         wc_unlock(wc);
932 }
933
934 static int writecache_alloc_entries(struct dm_writecache *wc)
935 {
936         size_t b;
937
938         if (wc->entries)
939                 return 0;
940         wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
941         if (!wc->entries)
942                 return -ENOMEM;
943         for (b = 0; b < wc->n_blocks; b++) {
944                 struct wc_entry *e = &wc->entries[b];
945                 e->index = b;
946                 e->write_in_progress = false;
947                 cond_resched();
948         }
949
950         return 0;
951 }
952
953 static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors)
954 {
955         struct dm_io_region region;
956         struct dm_io_request req;
957
958         region.bdev = wc->ssd_dev->bdev;
959         region.sector = wc->start_sector;
960         region.count = n_sectors;
961         req.bi_op = REQ_OP_READ;
962         req.bi_op_flags = REQ_SYNC;
963         req.mem.type = DM_IO_VMA;
964         req.mem.ptr.vma = (char *)wc->memory_map;
965         req.client = wc->dm_io;
966         req.notify.fn = NULL;
967
968         return dm_io(&req, 1, &region, NULL);
969 }
970
971 static void writecache_resume(struct dm_target *ti)
972 {
973         struct dm_writecache *wc = ti->private;
974         size_t b;
975         bool need_flush = false;
976         __le64 sb_seq_count;
977         int r;
978
979         wc_lock(wc);
980
981         wc->data_device_sectors = bdev_nr_sectors(wc->dev->bdev);
982
983         if (WC_MODE_PMEM(wc)) {
984                 persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
985         } else {
986                 r = writecache_read_metadata(wc, wc->metadata_sectors);
987                 if (r) {
988                         size_t sb_entries_offset;
989                         writecache_error(wc, r, "unable to read metadata: %d", r);
990                         sb_entries_offset = offsetof(struct wc_memory_superblock, entries);
991                         memset((char *)wc->memory_map + sb_entries_offset, -1,
992                                (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset);
993                 }
994         }
995
996         wc->tree = RB_ROOT;
997         INIT_LIST_HEAD(&wc->lru);
998         if (WC_MODE_SORT_FREELIST(wc)) {
999                 wc->freetree = RB_ROOT;
1000                 wc->current_free = NULL;
1001         } else {
1002                 INIT_LIST_HEAD(&wc->freelist);
1003         }
1004         wc->freelist_size = 0;
1005
1006         r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count,
1007                               sizeof(uint64_t));
1008         if (r) {
1009                 writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
1010                 sb_seq_count = cpu_to_le64(0);
1011         }
1012         wc->seq_count = le64_to_cpu(sb_seq_count);
1013
1014 #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
1015         for (b = 0; b < wc->n_blocks; b++) {
1016                 struct wc_entry *e = &wc->entries[b];
1017                 struct wc_memory_entry wme;
1018                 if (writecache_has_error(wc)) {
1019                         e->original_sector = -1;
1020                         e->seq_count = -1;
1021                         continue;
1022                 }
1023                 r = copy_mc_to_kernel(&wme, memory_entry(wc, e),
1024                                       sizeof(struct wc_memory_entry));
1025                 if (r) {
1026                         writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
1027                                          (unsigned long)b, r);
1028                         e->original_sector = -1;
1029                         e->seq_count = -1;
1030                 } else {
1031                         e->original_sector = le64_to_cpu(wme.original_sector);
1032                         e->seq_count = le64_to_cpu(wme.seq_count);
1033                 }
1034                 cond_resched();
1035         }
1036 #endif
1037         for (b = 0; b < wc->n_blocks; b++) {
1038                 struct wc_entry *e = &wc->entries[b];
1039                 if (!writecache_entry_is_committed(wc, e)) {
1040                         if (read_seq_count(wc, e) != -1) {
1041 erase_this:
1042                                 clear_seq_count(wc, e);
1043                                 need_flush = true;
1044                         }
1045                         writecache_add_to_freelist(wc, e);
1046                 } else {
1047                         struct wc_entry *old;
1048
1049                         old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
1050                         if (!old) {
1051                                 writecache_insert_entry(wc, e);
1052                         } else {
1053                                 if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
1054                                         writecache_error(wc, -EINVAL,
1055                                                  "two identical entries, position %llu, sector %llu, sequence %llu",
1056                                                  (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
1057                                                  (unsigned long long)read_seq_count(wc, e));
1058                                 }
1059                                 if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
1060                                         goto erase_this;
1061                                 } else {
1062                                         writecache_free_entry(wc, old);
1063                                         writecache_insert_entry(wc, e);
1064                                         need_flush = true;
1065                                 }
1066                         }
1067                 }
1068                 cond_resched();
1069         }
1070
1071         if (need_flush) {
1072                 writecache_flush_all_metadata(wc);
1073                 writecache_commit_flushed(wc, false);
1074         }
1075
1076         writecache_verify_watermark(wc);
1077
1078         if (wc->max_age != MAX_AGE_UNSPECIFIED)
1079                 mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
1080
1081         wc_unlock(wc);
1082 }
1083
1084 static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1085 {
1086         if (argc != 1)
1087                 return -EINVAL;
1088
1089         wc_lock(wc);
1090         if (dm_suspended(wc->ti)) {
1091                 wc_unlock(wc);
1092                 return -EBUSY;
1093         }
1094         if (writecache_has_error(wc)) {
1095                 wc_unlock(wc);
1096                 return -EIO;
1097         }
1098
1099         writecache_flush(wc);
1100         wc->writeback_all++;
1101         queue_work(wc->writeback_wq, &wc->writeback_work);
1102         wc_unlock(wc);
1103
1104         flush_workqueue(wc->writeback_wq);
1105
1106         wc_lock(wc);
1107         wc->writeback_all--;
1108         if (writecache_has_error(wc)) {
1109                 wc_unlock(wc);
1110                 return -EIO;
1111         }
1112         wc_unlock(wc);
1113
1114         return 0;
1115 }
1116
1117 static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1118 {
1119         if (argc != 1)
1120                 return -EINVAL;
1121
1122         wc_lock(wc);
1123         wc->flush_on_suspend = true;
1124         wc_unlock(wc);
1125
1126         return 0;
1127 }
1128
1129 static void activate_cleaner(struct dm_writecache *wc)
1130 {
1131         wc->flush_on_suspend = true;
1132         wc->cleaner = true;
1133         wc->freelist_high_watermark = wc->n_blocks;
1134         wc->freelist_low_watermark = wc->n_blocks;
1135 }
1136
1137 static int process_cleaner_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1138 {
1139         if (argc != 1)
1140                 return -EINVAL;
1141
1142         wc_lock(wc);
1143         activate_cleaner(wc);
1144         if (!dm_suspended(wc->ti))
1145                 writecache_verify_watermark(wc);
1146         wc_unlock(wc);
1147
1148         return 0;
1149 }
1150
1151 static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
1152                               char *result, unsigned maxlen)
1153 {
1154         int r = -EINVAL;
1155         struct dm_writecache *wc = ti->private;
1156
1157         if (!strcasecmp(argv[0], "flush"))
1158                 r = process_flush_mesg(argc, argv, wc);
1159         else if (!strcasecmp(argv[0], "flush_on_suspend"))
1160                 r = process_flush_on_suspend_mesg(argc, argv, wc);
1161         else if (!strcasecmp(argv[0], "cleaner"))
1162                 r = process_cleaner_mesg(argc, argv, wc);
1163         else
1164                 DMERR("unrecognised message received: %s", argv[0]);
1165
1166         return r;
1167 }
1168
1169 static void memcpy_flushcache_optimized(void *dest, void *source, size_t size)
1170 {
1171         /*
1172          * clflushopt performs better with block size 1024, 2048, 4096
1173          * non-temporal stores perform better with block size 512
1174          *
1175          * block size   512             1024            2048            4096
1176          * movnti       496 MB/s        642 MB/s        725 MB/s        744 MB/s
1177          * clflushopt   373 MB/s        688 MB/s        1.1 GB/s        1.2 GB/s
1178          *
1179          * We see that movnti performs better for 512-byte blocks, and
1180          * clflushopt performs better for 1024-byte and larger blocks. So, we
1181          * prefer clflushopt for sizes >= 768.
1182          *
1183          * NOTE: this happens to be the case now (with dm-writecache's single
1184          * threaded model) but re-evaluate this once memcpy_flushcache() is
1185          * enabled to use movdir64b which might invalidate this performance
1186          * advantage seen with cache-allocating-writes plus flushing.
1187          */
1188 #ifdef CONFIG_X86
1189         if (static_cpu_has(X86_FEATURE_CLFLUSHOPT) &&
1190             likely(boot_cpu_data.x86_clflush_size == 64) &&
1191             likely(size >= 768)) {
1192                 do {
1193                         memcpy((void *)dest, (void *)source, 64);
1194                         clflushopt((void *)dest);
1195                         dest += 64;
1196                         source += 64;
1197                         size -= 64;
1198                 } while (size >= 64);
1199                 return;
1200         }
1201 #endif
1202         memcpy_flushcache(dest, source, size);
1203 }
1204
1205 static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
1206 {
1207         void *buf;
1208         unsigned long flags;
1209         unsigned size;
1210         int rw = bio_data_dir(bio);
1211         unsigned remaining_size = wc->block_size;
1212
1213         do {
1214                 struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
1215                 buf = bvec_kmap_irq(&bv, &flags);
1216                 size = bv.bv_len;
1217                 if (unlikely(size > remaining_size))
1218                         size = remaining_size;
1219
1220                 if (rw == READ) {
1221                         int r;
1222                         r = copy_mc_to_kernel(buf, data, size);
1223                         flush_dcache_page(bio_page(bio));
1224                         if (unlikely(r)) {
1225                                 writecache_error(wc, r, "hardware memory error when reading data: %d", r);
1226                                 bio->bi_status = BLK_STS_IOERR;
1227                         }
1228                 } else {
1229                         flush_dcache_page(bio_page(bio));
1230                         memcpy_flushcache_optimized(data, buf, size);
1231                 }
1232
1233                 bvec_kunmap_irq(buf, &flags);
1234
1235                 data = (char *)data + size;
1236                 remaining_size -= size;
1237                 bio_advance(bio, size);
1238         } while (unlikely(remaining_size));
1239 }
1240
1241 static int writecache_flush_thread(void *data)
1242 {
1243         struct dm_writecache *wc = data;
1244
1245         while (1) {
1246                 struct bio *bio;
1247
1248                 wc_lock(wc);
1249                 bio = bio_list_pop(&wc->flush_list);
1250                 if (!bio) {
1251                         set_current_state(TASK_INTERRUPTIBLE);
1252                         wc_unlock(wc);
1253
1254                         if (unlikely(kthread_should_stop())) {
1255                                 set_current_state(TASK_RUNNING);
1256                                 break;
1257                         }
1258
1259                         schedule();
1260                         continue;
1261                 }
1262
1263                 if (bio_op(bio) == REQ_OP_DISCARD) {
1264                         writecache_discard(wc, bio->bi_iter.bi_sector,
1265                                            bio_end_sector(bio));
1266                         wc_unlock(wc);
1267                         bio_set_dev(bio, wc->dev->bdev);
1268                         submit_bio_noacct(bio);
1269                 } else {
1270                         writecache_flush(wc);
1271                         wc_unlock(wc);
1272                         if (writecache_has_error(wc))
1273                                 bio->bi_status = BLK_STS_IOERR;
1274                         bio_endio(bio);
1275                 }
1276         }
1277
1278         return 0;
1279 }
1280
1281 static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
1282 {
1283         if (bio_list_empty(&wc->flush_list))
1284                 wake_up_process(wc->flush_thread);
1285         bio_list_add(&wc->flush_list, bio);
1286 }
1287
1288 static int writecache_map(struct dm_target *ti, struct bio *bio)
1289 {
1290         struct wc_entry *e;
1291         struct dm_writecache *wc = ti->private;
1292
1293         bio->bi_private = NULL;
1294
1295         wc_lock(wc);
1296
1297         if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
1298                 if (writecache_has_error(wc))
1299                         goto unlock_error;
1300                 if (WC_MODE_PMEM(wc)) {
1301                         writecache_flush(wc);
1302                         if (writecache_has_error(wc))
1303                                 goto unlock_error;
1304                         goto unlock_submit;
1305                 } else {
1306                         writecache_offload_bio(wc, bio);
1307                         goto unlock_return;
1308                 }
1309         }
1310
1311         bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
1312
1313         if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
1314                                 (wc->block_size / 512 - 1)) != 0)) {
1315                 DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
1316                       (unsigned long long)bio->bi_iter.bi_sector,
1317                       bio->bi_iter.bi_size, wc->block_size);
1318                 goto unlock_error;
1319         }
1320
1321         if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
1322                 if (writecache_has_error(wc))
1323                         goto unlock_error;
1324                 if (WC_MODE_PMEM(wc)) {
1325                         writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
1326                         goto unlock_remap_origin;
1327                 } else {
1328                         writecache_offload_bio(wc, bio);
1329                         goto unlock_return;
1330                 }
1331         }
1332
1333         if (bio_data_dir(bio) == READ) {
1334 read_next_block:
1335                 e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1336                 if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1337                         if (WC_MODE_PMEM(wc)) {
1338                                 bio_copy_block(wc, bio, memory_data(wc, e));
1339                                 if (bio->bi_iter.bi_size)
1340                                         goto read_next_block;
1341                                 goto unlock_submit;
1342                         } else {
1343                                 dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1344                                 bio_set_dev(bio, wc->ssd_dev->bdev);
1345                                 bio->bi_iter.bi_sector = cache_sector(wc, e);
1346                                 if (!writecache_entry_is_committed(wc, e))
1347                                         writecache_wait_for_ios(wc, WRITE);
1348                                 goto unlock_remap;
1349                         }
1350                 } else {
1351                         if (e) {
1352                                 sector_t next_boundary =
1353                                         read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1354                                 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
1355                                         dm_accept_partial_bio(bio, next_boundary);
1356                                 }
1357                         }
1358                         goto unlock_remap_origin;
1359                 }
1360         } else {
1361                 do {
1362                         bool found_entry = false;
1363                         bool search_used = false;
1364                         if (writecache_has_error(wc))
1365                                 goto unlock_error;
1366                         e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1367                         if (e) {
1368                                 if (!writecache_entry_is_committed(wc, e)) {
1369                                         search_used = true;
1370                                         goto bio_copy;
1371                                 }
1372                                 if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1373                                         wc->overwrote_committed = true;
1374                                         search_used = true;
1375                                         goto bio_copy;
1376                                 }
1377                                 found_entry = true;
1378                         } else {
1379                                 if (unlikely(wc->cleaner))
1380                                         goto direct_write;
1381                         }
1382                         e = writecache_pop_from_freelist(wc, (sector_t)-1);
1383                         if (unlikely(!e)) {
1384                                 if (!found_entry) {
1385 direct_write:
1386                                         e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1387                                         if (e) {
1388                                                 sector_t next_boundary = read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1389                                                 BUG_ON(!next_boundary);
1390                                                 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
1391                                                         dm_accept_partial_bio(bio, next_boundary);
1392                                                 }
1393                                         }
1394                                         goto unlock_remap_origin;
1395                                 }
1396                                 writecache_wait_on_freelist(wc);
1397                                 continue;
1398                         }
1399                         write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1400                         writecache_insert_entry(wc, e);
1401                         wc->uncommitted_blocks++;
1402 bio_copy:
1403                         if (WC_MODE_PMEM(wc)) {
1404                                 bio_copy_block(wc, bio, memory_data(wc, e));
1405                         } else {
1406                                 unsigned bio_size = wc->block_size;
1407                                 sector_t start_cache_sec = cache_sector(wc, e);
1408                                 sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT);
1409
1410                                 while (bio_size < bio->bi_iter.bi_size) {
1411                                         if (!search_used) {
1412                                                 struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec);
1413                                                 if (!f)
1414                                                         break;
1415                                                 write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector +
1416                                                                                 (bio_size >> SECTOR_SHIFT), wc->seq_count);
1417                                                 writecache_insert_entry(wc, f);
1418                                                 wc->uncommitted_blocks++;
1419                                         } else {
1420                                                 struct wc_entry *f;
1421                                                 struct rb_node *next = rb_next(&e->rb_node);
1422                                                 if (!next)
1423                                                         break;
1424                                                 f = container_of(next, struct wc_entry, rb_node);
1425                                                 if (f != e + 1)
1426                                                         break;
1427                                                 if (read_original_sector(wc, f) !=
1428                                                     read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1429                                                         break;
1430                                                 if (unlikely(f->write_in_progress))
1431                                                         break;
1432                                                 if (writecache_entry_is_committed(wc, f))
1433                                                         wc->overwrote_committed = true;
1434                                                 e = f;
1435                                         }
1436                                         bio_size += wc->block_size;
1437                                         current_cache_sec += wc->block_size >> SECTOR_SHIFT;
1438                                 }
1439
1440                                 bio_set_dev(bio, wc->ssd_dev->bdev);
1441                                 bio->bi_iter.bi_sector = start_cache_sec;
1442                                 dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT);
1443
1444                                 if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1445                                         wc->uncommitted_blocks = 0;
1446                                         queue_work(wc->writeback_wq, &wc->flush_work);
1447                                 } else {
1448                                         writecache_schedule_autocommit(wc);
1449                                 }
1450                                 goto unlock_remap;
1451                         }
1452                 } while (bio->bi_iter.bi_size);
1453
1454                 if (unlikely(bio->bi_opf & REQ_FUA ||
1455                              wc->uncommitted_blocks >= wc->autocommit_blocks))
1456                         writecache_flush(wc);
1457                 else
1458                         writecache_schedule_autocommit(wc);
1459                 goto unlock_submit;
1460         }
1461
1462 unlock_remap_origin:
1463         bio_set_dev(bio, wc->dev->bdev);
1464         wc_unlock(wc);
1465         return DM_MAPIO_REMAPPED;
1466
1467 unlock_remap:
1468         /* make sure that writecache_end_io decrements bio_in_progress: */
1469         bio->bi_private = (void *)1;
1470         atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
1471         wc_unlock(wc);
1472         return DM_MAPIO_REMAPPED;
1473
1474 unlock_submit:
1475         wc_unlock(wc);
1476         bio_endio(bio);
1477         return DM_MAPIO_SUBMITTED;
1478
1479 unlock_return:
1480         wc_unlock(wc);
1481         return DM_MAPIO_SUBMITTED;
1482
1483 unlock_error:
1484         wc_unlock(wc);
1485         bio_io_error(bio);
1486         return DM_MAPIO_SUBMITTED;
1487 }
1488
1489 static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
1490 {
1491         struct dm_writecache *wc = ti->private;
1492
1493         if (bio->bi_private != NULL) {
1494                 int dir = bio_data_dir(bio);
1495                 if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
1496                         if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
1497                                 wake_up(&wc->bio_in_progress_wait[dir]);
1498         }
1499         return 0;
1500 }
1501
1502 static int writecache_iterate_devices(struct dm_target *ti,
1503                                       iterate_devices_callout_fn fn, void *data)
1504 {
1505         struct dm_writecache *wc = ti->private;
1506
1507         return fn(ti, wc->dev, 0, ti->len, data);
1508 }
1509
1510 static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
1511 {
1512         struct dm_writecache *wc = ti->private;
1513
1514         if (limits->logical_block_size < wc->block_size)
1515                 limits->logical_block_size = wc->block_size;
1516
1517         if (limits->physical_block_size < wc->block_size)
1518                 limits->physical_block_size = wc->block_size;
1519
1520         if (limits->io_min < wc->block_size)
1521                 limits->io_min = wc->block_size;
1522 }
1523
1524
1525 static void writecache_writeback_endio(struct bio *bio)
1526 {
1527         struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
1528         struct dm_writecache *wc = wb->wc;
1529         unsigned long flags;
1530
1531         raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
1532         if (unlikely(list_empty(&wc->endio_list)))
1533                 wake_up_process(wc->endio_thread);
1534         list_add_tail(&wb->endio_entry, &wc->endio_list);
1535         raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
1536 }
1537
1538 static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
1539 {
1540         struct copy_struct *c = ptr;
1541         struct dm_writecache *wc = c->wc;
1542
1543         c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
1544
1545         raw_spin_lock_irq(&wc->endio_list_lock);
1546         if (unlikely(list_empty(&wc->endio_list)))
1547                 wake_up_process(wc->endio_thread);
1548         list_add_tail(&c->endio_entry, &wc->endio_list);
1549         raw_spin_unlock_irq(&wc->endio_list_lock);
1550 }
1551
1552 static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
1553 {
1554         unsigned i;
1555         struct writeback_struct *wb;
1556         struct wc_entry *e;
1557         unsigned long n_walked = 0;
1558
1559         do {
1560                 wb = list_entry(list->next, struct writeback_struct, endio_entry);
1561                 list_del(&wb->endio_entry);
1562
1563                 if (unlikely(wb->bio.bi_status != BLK_STS_OK))
1564                         writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
1565                                         "write error %d", wb->bio.bi_status);
1566                 i = 0;
1567                 do {
1568                         e = wb->wc_list[i];
1569                         BUG_ON(!e->write_in_progress);
1570                         e->write_in_progress = false;
1571                         INIT_LIST_HEAD(&e->lru);
1572                         if (!writecache_has_error(wc))
1573                                 writecache_free_entry(wc, e);
1574                         BUG_ON(!wc->writeback_size);
1575                         wc->writeback_size--;
1576                         n_walked++;
1577                         if (unlikely(n_walked >= ENDIO_LATENCY)) {
1578                                 writecache_commit_flushed(wc, false);
1579                                 wc_unlock(wc);
1580                                 wc_lock(wc);
1581                                 n_walked = 0;
1582                         }
1583                 } while (++i < wb->wc_list_n);
1584
1585                 if (wb->wc_list != wb->wc_list_inline)
1586                         kfree(wb->wc_list);
1587                 bio_put(&wb->bio);
1588         } while (!list_empty(list));
1589 }
1590
1591 static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
1592 {
1593         struct copy_struct *c;
1594         struct wc_entry *e;
1595
1596         do {
1597                 c = list_entry(list->next, struct copy_struct, endio_entry);
1598                 list_del(&c->endio_entry);
1599
1600                 if (unlikely(c->error))
1601                         writecache_error(wc, c->error, "copy error");
1602
1603                 e = c->e;
1604                 do {
1605                         BUG_ON(!e->write_in_progress);
1606                         e->write_in_progress = false;
1607                         INIT_LIST_HEAD(&e->lru);
1608                         if (!writecache_has_error(wc))
1609                                 writecache_free_entry(wc, e);
1610
1611                         BUG_ON(!wc->writeback_size);
1612                         wc->writeback_size--;
1613                         e++;
1614                 } while (--c->n_entries);
1615                 mempool_free(c, &wc->copy_pool);
1616         } while (!list_empty(list));
1617 }
1618
1619 static int writecache_endio_thread(void *data)
1620 {
1621         struct dm_writecache *wc = data;
1622
1623         while (1) {
1624                 struct list_head list;
1625
1626                 raw_spin_lock_irq(&wc->endio_list_lock);
1627                 if (!list_empty(&wc->endio_list))
1628                         goto pop_from_list;
1629                 set_current_state(TASK_INTERRUPTIBLE);
1630                 raw_spin_unlock_irq(&wc->endio_list_lock);
1631
1632                 if (unlikely(kthread_should_stop())) {
1633                         set_current_state(TASK_RUNNING);
1634                         break;
1635                 }
1636
1637                 schedule();
1638
1639                 continue;
1640
1641 pop_from_list:
1642                 list = wc->endio_list;
1643                 list.next->prev = list.prev->next = &list;
1644                 INIT_LIST_HEAD(&wc->endio_list);
1645                 raw_spin_unlock_irq(&wc->endio_list_lock);
1646
1647                 if (!WC_MODE_FUA(wc))
1648                         writecache_disk_flush(wc, wc->dev);
1649
1650                 wc_lock(wc);
1651
1652                 if (WC_MODE_PMEM(wc)) {
1653                         __writecache_endio_pmem(wc, &list);
1654                 } else {
1655                         __writecache_endio_ssd(wc, &list);
1656                         writecache_wait_for_ios(wc, READ);
1657                 }
1658
1659                 writecache_commit_flushed(wc, false);
1660
1661                 wc_unlock(wc);
1662         }
1663
1664         return 0;
1665 }
1666
1667 static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e)
1668 {
1669         struct dm_writecache *wc = wb->wc;
1670         unsigned block_size = wc->block_size;
1671         void *address = memory_data(wc, e);
1672
1673         persistent_memory_flush_cache(address, block_size);
1674
1675         if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors))
1676                 return true;
1677
1678         return bio_add_page(&wb->bio, persistent_memory_page(address),
1679                             block_size, persistent_memory_page_offset(address)) != 0;
1680 }
1681
1682 struct writeback_list {
1683         struct list_head list;
1684         size_t size;
1685 };
1686
1687 static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
1688 {
1689         if (unlikely(wc->max_writeback_jobs)) {
1690                 if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
1691                         wc_lock(wc);
1692                         while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
1693                                 writecache_wait_on_freelist(wc);
1694                         wc_unlock(wc);
1695                 }
1696         }
1697         cond_resched();
1698 }
1699
1700 static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
1701 {
1702         struct wc_entry *e, *f;
1703         struct bio *bio;
1704         struct writeback_struct *wb;
1705         unsigned max_pages;
1706
1707         while (wbl->size) {
1708                 wbl->size--;
1709                 e = container_of(wbl->list.prev, struct wc_entry, lru);
1710                 list_del(&e->lru);
1711
1712                 max_pages = e->wc_list_contiguous;
1713
1714                 bio = bio_alloc_bioset(GFP_NOIO, max_pages, &wc->bio_set);
1715                 wb = container_of(bio, struct writeback_struct, bio);
1716                 wb->wc = wc;
1717                 bio->bi_end_io = writecache_writeback_endio;
1718                 bio_set_dev(bio, wc->dev->bdev);
1719                 bio->bi_iter.bi_sector = read_original_sector(wc, e);
1720                 if (max_pages <= WB_LIST_INLINE ||
1721                     unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
1722                                                            GFP_NOIO | __GFP_NORETRY |
1723                                                            __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
1724                         wb->wc_list = wb->wc_list_inline;
1725                         max_pages = WB_LIST_INLINE;
1726                 }
1727
1728                 BUG_ON(!wc_add_block(wb, e));
1729
1730                 wb->wc_list[0] = e;
1731                 wb->wc_list_n = 1;
1732
1733                 while (wbl->size && wb->wc_list_n < max_pages) {
1734                         f = container_of(wbl->list.prev, struct wc_entry, lru);
1735                         if (read_original_sector(wc, f) !=
1736                             read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1737                                 break;
1738                         if (!wc_add_block(wb, f))
1739                                 break;
1740                         wbl->size--;
1741                         list_del(&f->lru);
1742                         wb->wc_list[wb->wc_list_n++] = f;
1743                         e = f;
1744                 }
1745                 bio_set_op_attrs(bio, REQ_OP_WRITE, WC_MODE_FUA(wc) * REQ_FUA);
1746                 if (writecache_has_error(wc)) {
1747                         bio->bi_status = BLK_STS_IOERR;
1748                         bio_endio(bio);
1749                 } else if (unlikely(!bio_sectors(bio))) {
1750                         bio->bi_status = BLK_STS_OK;
1751                         bio_endio(bio);
1752                 } else {
1753                         submit_bio(bio);
1754                 }
1755
1756                 __writeback_throttle(wc, wbl);
1757         }
1758 }
1759
1760 static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
1761 {
1762         struct wc_entry *e, *f;
1763         struct dm_io_region from, to;
1764         struct copy_struct *c;
1765
1766         while (wbl->size) {
1767                 unsigned n_sectors;
1768
1769                 wbl->size--;
1770                 e = container_of(wbl->list.prev, struct wc_entry, lru);
1771                 list_del(&e->lru);
1772
1773                 n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
1774
1775                 from.bdev = wc->ssd_dev->bdev;
1776                 from.sector = cache_sector(wc, e);
1777                 from.count = n_sectors;
1778                 to.bdev = wc->dev->bdev;
1779                 to.sector = read_original_sector(wc, e);
1780                 to.count = n_sectors;
1781
1782                 c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
1783                 c->wc = wc;
1784                 c->e = e;
1785                 c->n_entries = e->wc_list_contiguous;
1786
1787                 while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
1788                         wbl->size--;
1789                         f = container_of(wbl->list.prev, struct wc_entry, lru);
1790                         BUG_ON(f != e + 1);
1791                         list_del(&f->lru);
1792                         e = f;
1793                 }
1794
1795                 if (unlikely(to.sector + to.count > wc->data_device_sectors)) {
1796                         if (to.sector >= wc->data_device_sectors) {
1797                                 writecache_copy_endio(0, 0, c);
1798                                 continue;
1799                         }
1800                         from.count = to.count = wc->data_device_sectors - to.sector;
1801                 }
1802
1803                 dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
1804
1805                 __writeback_throttle(wc, wbl);
1806         }
1807 }
1808
1809 static void writecache_writeback(struct work_struct *work)
1810 {
1811         struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
1812         struct blk_plug plug;
1813         struct wc_entry *f, *g, *e = NULL;
1814         struct rb_node *node, *next_node;
1815         struct list_head skipped;
1816         struct writeback_list wbl;
1817         unsigned long n_walked;
1818
1819         wc_lock(wc);
1820 restart:
1821         if (writecache_has_error(wc)) {
1822                 wc_unlock(wc);
1823                 return;
1824         }
1825
1826         if (unlikely(wc->writeback_all)) {
1827                 if (writecache_wait_for_writeback(wc))
1828                         goto restart;
1829         }
1830
1831         if (wc->overwrote_committed) {
1832                 writecache_wait_for_ios(wc, WRITE);
1833         }
1834
1835         n_walked = 0;
1836         INIT_LIST_HEAD(&skipped);
1837         INIT_LIST_HEAD(&wbl.list);
1838         wbl.size = 0;
1839         while (!list_empty(&wc->lru) &&
1840                (wc->writeback_all ||
1841                 wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark ||
1842                 (jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >=
1843                  wc->max_age - wc->max_age / MAX_AGE_DIV))) {
1844
1845                 n_walked++;
1846                 if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1847                     likely(!wc->writeback_all)) {
1848                         if (likely(!dm_suspended(wc->ti)))
1849                                 queue_work(wc->writeback_wq, &wc->writeback_work);
1850                         break;
1851                 }
1852
1853                 if (unlikely(wc->writeback_all)) {
1854                         if (unlikely(!e)) {
1855                                 writecache_flush(wc);
1856                                 e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
1857                         } else
1858                                 e = g;
1859                 } else
1860                         e = container_of(wc->lru.prev, struct wc_entry, lru);
1861                 BUG_ON(e->write_in_progress);
1862                 if (unlikely(!writecache_entry_is_committed(wc, e))) {
1863                         writecache_flush(wc);
1864                 }
1865                 node = rb_prev(&e->rb_node);
1866                 if (node) {
1867                         f = container_of(node, struct wc_entry, rb_node);
1868                         if (unlikely(read_original_sector(wc, f) ==
1869                                      read_original_sector(wc, e))) {
1870                                 BUG_ON(!f->write_in_progress);
1871                                 list_del(&e->lru);
1872                                 list_add(&e->lru, &skipped);
1873                                 cond_resched();
1874                                 continue;
1875                         }
1876                 }
1877                 wc->writeback_size++;
1878                 list_del(&e->lru);
1879                 list_add(&e->lru, &wbl.list);
1880                 wbl.size++;
1881                 e->write_in_progress = true;
1882                 e->wc_list_contiguous = 1;
1883
1884                 f = e;
1885
1886                 while (1) {
1887                         next_node = rb_next(&f->rb_node);
1888                         if (unlikely(!next_node))
1889                                 break;
1890                         g = container_of(next_node, struct wc_entry, rb_node);
1891                         if (unlikely(read_original_sector(wc, g) ==
1892                             read_original_sector(wc, f))) {
1893                                 f = g;
1894                                 continue;
1895                         }
1896                         if (read_original_sector(wc, g) !=
1897                             read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
1898                                 break;
1899                         if (unlikely(g->write_in_progress))
1900                                 break;
1901                         if (unlikely(!writecache_entry_is_committed(wc, g)))
1902                                 break;
1903
1904                         if (!WC_MODE_PMEM(wc)) {
1905                                 if (g != f + 1)
1906                                         break;
1907                         }
1908
1909                         n_walked++;
1910                         //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
1911                         //      break;
1912
1913                         wc->writeback_size++;
1914                         list_del(&g->lru);
1915                         list_add(&g->lru, &wbl.list);
1916                         wbl.size++;
1917                         g->write_in_progress = true;
1918                         g->wc_list_contiguous = BIO_MAX_VECS;
1919                         f = g;
1920                         e->wc_list_contiguous++;
1921                         if (unlikely(e->wc_list_contiguous == BIO_MAX_VECS)) {
1922                                 if (unlikely(wc->writeback_all)) {
1923                                         next_node = rb_next(&f->rb_node);
1924                                         if (likely(next_node))
1925                                                 g = container_of(next_node, struct wc_entry, rb_node);
1926                                 }
1927                                 break;
1928                         }
1929                 }
1930                 cond_resched();
1931         }
1932
1933         if (!list_empty(&skipped)) {
1934                 list_splice_tail(&skipped, &wc->lru);
1935                 /*
1936                  * If we didn't do any progress, we must wait until some
1937                  * writeback finishes to avoid burning CPU in a loop
1938                  */
1939                 if (unlikely(!wbl.size))
1940                         writecache_wait_for_writeback(wc);
1941         }
1942
1943         wc_unlock(wc);
1944
1945         blk_start_plug(&plug);
1946
1947         if (WC_MODE_PMEM(wc))
1948                 __writecache_writeback_pmem(wc, &wbl);
1949         else
1950                 __writecache_writeback_ssd(wc, &wbl);
1951
1952         blk_finish_plug(&plug);
1953
1954         if (unlikely(wc->writeback_all)) {
1955                 wc_lock(wc);
1956                 while (writecache_wait_for_writeback(wc));
1957                 wc_unlock(wc);
1958         }
1959 }
1960
1961 static int calculate_memory_size(uint64_t device_size, unsigned block_size,
1962                                  size_t *n_blocks_p, size_t *n_metadata_blocks_p)
1963 {
1964         uint64_t n_blocks, offset;
1965         struct wc_entry e;
1966
1967         n_blocks = device_size;
1968         do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
1969
1970         while (1) {
1971                 if (!n_blocks)
1972                         return -ENOSPC;
1973                 /* Verify the following entries[n_blocks] won't overflow */
1974                 if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
1975                                  sizeof(struct wc_memory_entry)))
1976                         return -EFBIG;
1977                 offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
1978                 offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
1979                 if (offset + n_blocks * block_size <= device_size)
1980                         break;
1981                 n_blocks--;
1982         }
1983
1984         /* check if the bit field overflows */
1985         e.index = n_blocks;
1986         if (e.index != n_blocks)
1987                 return -EFBIG;
1988
1989         if (n_blocks_p)
1990                 *n_blocks_p = n_blocks;
1991         if (n_metadata_blocks_p)
1992                 *n_metadata_blocks_p = offset >> __ffs(block_size);
1993         return 0;
1994 }
1995
1996 static int init_memory(struct dm_writecache *wc)
1997 {
1998         size_t b;
1999         int r;
2000
2001         r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
2002         if (r)
2003                 return r;
2004
2005         r = writecache_alloc_entries(wc);
2006         if (r)
2007                 return r;
2008
2009         for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
2010                 pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
2011         pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
2012         pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
2013         pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
2014         pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
2015
2016         for (b = 0; b < wc->n_blocks; b++) {
2017                 write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
2018                 cond_resched();
2019         }
2020
2021         writecache_flush_all_metadata(wc);
2022         writecache_commit_flushed(wc, false);
2023         pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
2024         writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
2025         writecache_commit_flushed(wc, false);
2026
2027         return 0;
2028 }
2029
2030 static void writecache_dtr(struct dm_target *ti)
2031 {
2032         struct dm_writecache *wc = ti->private;
2033
2034         if (!wc)
2035                 return;
2036
2037         if (wc->endio_thread)
2038                 kthread_stop(wc->endio_thread);
2039
2040         if (wc->flush_thread)
2041                 kthread_stop(wc->flush_thread);
2042
2043         bioset_exit(&wc->bio_set);
2044
2045         mempool_exit(&wc->copy_pool);
2046
2047         if (wc->writeback_wq)
2048                 destroy_workqueue(wc->writeback_wq);
2049
2050         if (wc->dev)
2051                 dm_put_device(ti, wc->dev);
2052
2053         if (wc->ssd_dev)
2054                 dm_put_device(ti, wc->ssd_dev);
2055
2056         vfree(wc->entries);
2057
2058         if (wc->memory_map) {
2059                 if (WC_MODE_PMEM(wc))
2060                         persistent_memory_release(wc);
2061                 else
2062                         vfree(wc->memory_map);
2063         }
2064
2065         if (wc->dm_kcopyd)
2066                 dm_kcopyd_client_destroy(wc->dm_kcopyd);
2067
2068         if (wc->dm_io)
2069                 dm_io_client_destroy(wc->dm_io);
2070
2071         vfree(wc->dirty_bitmap);
2072
2073         kfree(wc);
2074 }
2075
2076 static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2077 {
2078         struct dm_writecache *wc;
2079         struct dm_arg_set as;
2080         const char *string;
2081         unsigned opt_params;
2082         size_t offset, data_size;
2083         int i, r;
2084         char dummy;
2085         int high_wm_percent = HIGH_WATERMARK;
2086         int low_wm_percent = LOW_WATERMARK;
2087         uint64_t x;
2088         struct wc_memory_superblock s;
2089
2090         static struct dm_arg _args[] = {
2091                 {0, 16, "Invalid number of feature args"},
2092         };
2093
2094         as.argc = argc;
2095         as.argv = argv;
2096
2097         wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
2098         if (!wc) {
2099                 ti->error = "Cannot allocate writecache structure";
2100                 r = -ENOMEM;
2101                 goto bad;
2102         }
2103         ti->private = wc;
2104         wc->ti = ti;
2105
2106         mutex_init(&wc->lock);
2107         wc->max_age = MAX_AGE_UNSPECIFIED;
2108         writecache_poison_lists(wc);
2109         init_waitqueue_head(&wc->freelist_wait);
2110         timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
2111         timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0);
2112
2113         for (i = 0; i < 2; i++) {
2114                 atomic_set(&wc->bio_in_progress[i], 0);
2115                 init_waitqueue_head(&wc->bio_in_progress_wait[i]);
2116         }
2117
2118         wc->dm_io = dm_io_client_create();
2119         if (IS_ERR(wc->dm_io)) {
2120                 r = PTR_ERR(wc->dm_io);
2121                 ti->error = "Unable to allocate dm-io client";
2122                 wc->dm_io = NULL;
2123                 goto bad;
2124         }
2125
2126         wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
2127         if (!wc->writeback_wq) {
2128                 r = -ENOMEM;
2129                 ti->error = "Could not allocate writeback workqueue";
2130                 goto bad;
2131         }
2132         INIT_WORK(&wc->writeback_work, writecache_writeback);
2133         INIT_WORK(&wc->flush_work, writecache_flush_work);
2134
2135         raw_spin_lock_init(&wc->endio_list_lock);
2136         INIT_LIST_HEAD(&wc->endio_list);
2137         wc->endio_thread = kthread_create(writecache_endio_thread, wc, "writecache_endio");
2138         if (IS_ERR(wc->endio_thread)) {
2139                 r = PTR_ERR(wc->endio_thread);
2140                 wc->endio_thread = NULL;
2141                 ti->error = "Couldn't spawn endio thread";
2142                 goto bad;
2143         }
2144         wake_up_process(wc->endio_thread);
2145
2146         /*
2147          * Parse the mode (pmem or ssd)
2148          */
2149         string = dm_shift_arg(&as);
2150         if (!string)
2151                 goto bad_arguments;
2152
2153         if (!strcasecmp(string, "s")) {
2154                 wc->pmem_mode = false;
2155         } else if (!strcasecmp(string, "p")) {
2156 #ifdef DM_WRITECACHE_HAS_PMEM
2157                 wc->pmem_mode = true;
2158                 wc->writeback_fua = true;
2159 #else
2160                 /*
2161                  * If the architecture doesn't support persistent memory or
2162                  * the kernel doesn't support any DAX drivers, this driver can
2163                  * only be used in SSD-only mode.
2164                  */
2165                 r = -EOPNOTSUPP;
2166                 ti->error = "Persistent memory or DAX not supported on this system";
2167                 goto bad;
2168 #endif
2169         } else {
2170                 goto bad_arguments;
2171         }
2172
2173         if (WC_MODE_PMEM(wc)) {
2174                 r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
2175                                 offsetof(struct writeback_struct, bio),
2176                                 BIOSET_NEED_BVECS);
2177                 if (r) {
2178                         ti->error = "Could not allocate bio set";
2179                         goto bad;
2180                 }
2181         } else {
2182                 r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
2183                 if (r) {
2184                         ti->error = "Could not allocate mempool";
2185                         goto bad;
2186                 }
2187         }
2188
2189         /*
2190          * Parse the origin data device
2191          */
2192         string = dm_shift_arg(&as);
2193         if (!string)
2194                 goto bad_arguments;
2195         r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
2196         if (r) {
2197                 ti->error = "Origin data device lookup failed";
2198                 goto bad;
2199         }
2200
2201         /*
2202          * Parse cache data device (be it pmem or ssd)
2203          */
2204         string = dm_shift_arg(&as);
2205         if (!string)
2206                 goto bad_arguments;
2207
2208         r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
2209         if (r) {
2210                 ti->error = "Cache data device lookup failed";
2211                 goto bad;
2212         }
2213         wc->memory_map_size = i_size_read(wc->ssd_dev->bdev->bd_inode);
2214
2215         /*
2216          * Parse the cache block size
2217          */
2218         string = dm_shift_arg(&as);
2219         if (!string)
2220                 goto bad_arguments;
2221         if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
2222             wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
2223             (wc->block_size & (wc->block_size - 1))) {
2224                 r = -EINVAL;
2225                 ti->error = "Invalid block size";
2226                 goto bad;
2227         }
2228         if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) ||
2229             wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) {
2230                 r = -EINVAL;
2231                 ti->error = "Block size is smaller than device logical block size";
2232                 goto bad;
2233         }
2234         wc->block_size_bits = __ffs(wc->block_size);
2235
2236         wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
2237         wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
2238         wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
2239
2240         /*
2241          * Parse optional arguments
2242          */
2243         r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2244         if (r)
2245                 goto bad;
2246
2247         while (opt_params) {
2248                 string = dm_shift_arg(&as), opt_params--;
2249                 if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2250                         unsigned long long start_sector;
2251                         string = dm_shift_arg(&as), opt_params--;
2252                         if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2253                                 goto invalid_optional;
2254                         wc->start_sector = start_sector;
2255                         wc->start_sector_set = true;
2256                         if (wc->start_sector != start_sector ||
2257                             wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2258                                 goto invalid_optional;
2259                 } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
2260                         string = dm_shift_arg(&as), opt_params--;
2261                         if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
2262                                 goto invalid_optional;
2263                         if (high_wm_percent < 0 || high_wm_percent > 100)
2264                                 goto invalid_optional;
2265                         wc->high_wm_percent_value = high_wm_percent;
2266                         wc->high_wm_percent_set = true;
2267                 } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
2268                         string = dm_shift_arg(&as), opt_params--;
2269                         if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
2270                                 goto invalid_optional;
2271                         if (low_wm_percent < 0 || low_wm_percent > 100)
2272                                 goto invalid_optional;
2273                         wc->low_wm_percent_value = low_wm_percent;
2274                         wc->low_wm_percent_set = true;
2275                 } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
2276                         string = dm_shift_arg(&as), opt_params--;
2277                         if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
2278                                 goto invalid_optional;
2279                         wc->max_writeback_jobs_set = true;
2280                 } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
2281                         string = dm_shift_arg(&as), opt_params--;
2282                         if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
2283                                 goto invalid_optional;
2284                         wc->autocommit_blocks_set = true;
2285                 } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
2286                         unsigned autocommit_msecs;
2287                         string = dm_shift_arg(&as), opt_params--;
2288                         if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
2289                                 goto invalid_optional;
2290                         if (autocommit_msecs > 3600000)
2291                                 goto invalid_optional;
2292                         wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2293                         wc->autocommit_time_value = autocommit_msecs;
2294                         wc->autocommit_time_set = true;
2295                 } else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
2296                         unsigned max_age_msecs;
2297                         string = dm_shift_arg(&as), opt_params--;
2298                         if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
2299                                 goto invalid_optional;
2300                         if (max_age_msecs > 86400000)
2301                                 goto invalid_optional;
2302                         wc->max_age = msecs_to_jiffies(max_age_msecs);
2303                         wc->max_age_set = true;
2304                         wc->max_age_value = max_age_msecs;
2305                 } else if (!strcasecmp(string, "cleaner")) {
2306                         wc->cleaner_set = true;
2307                         wc->cleaner = true;
2308                 } else if (!strcasecmp(string, "fua")) {
2309                         if (WC_MODE_PMEM(wc)) {
2310                                 wc->writeback_fua = true;
2311                                 wc->writeback_fua_set = true;
2312                         } else goto invalid_optional;
2313                 } else if (!strcasecmp(string, "nofua")) {
2314                         if (WC_MODE_PMEM(wc)) {
2315                                 wc->writeback_fua = false;
2316                                 wc->writeback_fua_set = true;
2317                         } else goto invalid_optional;
2318                 } else {
2319 invalid_optional:
2320                         r = -EINVAL;
2321                         ti->error = "Invalid optional argument";
2322                         goto bad;
2323                 }
2324         }
2325
2326         if (high_wm_percent < low_wm_percent) {
2327                 r = -EINVAL;
2328                 ti->error = "High watermark must be greater than or equal to low watermark";
2329                 goto bad;
2330         }
2331
2332         if (WC_MODE_PMEM(wc)) {
2333                 if (!dax_synchronous(wc->ssd_dev->dax_dev)) {
2334                         r = -EOPNOTSUPP;
2335                         ti->error = "Asynchronous persistent memory not supported as pmem cache";
2336                         goto bad;
2337                 }
2338
2339                 r = persistent_memory_claim(wc);
2340                 if (r) {
2341                         ti->error = "Unable to map persistent memory for cache";
2342                         goto bad;
2343                 }
2344         } else {
2345                 size_t n_blocks, n_metadata_blocks;
2346                 uint64_t n_bitmap_bits;
2347
2348                 wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2349
2350                 bio_list_init(&wc->flush_list);
2351                 wc->flush_thread = kthread_create(writecache_flush_thread, wc, "dm_writecache_flush");
2352                 if (IS_ERR(wc->flush_thread)) {
2353                         r = PTR_ERR(wc->flush_thread);
2354                         wc->flush_thread = NULL;
2355                         ti->error = "Couldn't spawn flush thread";
2356                         goto bad;
2357                 }
2358                 wake_up_process(wc->flush_thread);
2359
2360                 r = calculate_memory_size(wc->memory_map_size, wc->block_size,
2361                                           &n_blocks, &n_metadata_blocks);
2362                 if (r) {
2363                         ti->error = "Invalid device size";
2364                         goto bad;
2365                 }
2366
2367                 n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
2368                                  BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
2369                 /* this is limitation of test_bit functions */
2370                 if (n_bitmap_bits > 1U << 31) {
2371                         r = -EFBIG;
2372                         ti->error = "Invalid device size";
2373                         goto bad;
2374                 }
2375
2376                 wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
2377                 if (!wc->memory_map) {
2378                         r = -ENOMEM;
2379                         ti->error = "Unable to allocate memory for metadata";
2380                         goto bad;
2381                 }
2382
2383                 wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2384                 if (IS_ERR(wc->dm_kcopyd)) {
2385                         r = PTR_ERR(wc->dm_kcopyd);
2386                         ti->error = "Unable to allocate dm-kcopyd client";
2387                         wc->dm_kcopyd = NULL;
2388                         goto bad;
2389                 }
2390
2391                 wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
2392                 wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
2393                         BITS_PER_LONG * sizeof(unsigned long);
2394                 wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
2395                 if (!wc->dirty_bitmap) {
2396                         r = -ENOMEM;
2397                         ti->error = "Unable to allocate dirty bitmap";
2398                         goto bad;
2399                 }
2400
2401                 r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT);
2402                 if (r) {
2403                         ti->error = "Unable to read first block of metadata";
2404                         goto bad;
2405                 }
2406         }
2407
2408         r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock));
2409         if (r) {
2410                 ti->error = "Hardware memory error when reading superblock";
2411                 goto bad;
2412         }
2413         if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
2414                 r = init_memory(wc);
2415                 if (r) {
2416                         ti->error = "Unable to initialize device";
2417                         goto bad;
2418                 }
2419                 r = copy_mc_to_kernel(&s, sb(wc),
2420                                       sizeof(struct wc_memory_superblock));
2421                 if (r) {
2422                         ti->error = "Hardware memory error when reading superblock";
2423                         goto bad;
2424                 }
2425         }
2426
2427         if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
2428                 ti->error = "Invalid magic in the superblock";
2429                 r = -EINVAL;
2430                 goto bad;
2431         }
2432
2433         if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
2434                 ti->error = "Invalid version in the superblock";
2435                 r = -EINVAL;
2436                 goto bad;
2437         }
2438
2439         if (le32_to_cpu(s.block_size) != wc->block_size) {
2440                 ti->error = "Block size does not match superblock";
2441                 r = -EINVAL;
2442                 goto bad;
2443         }
2444
2445         wc->n_blocks = le64_to_cpu(s.n_blocks);
2446
2447         offset = wc->n_blocks * sizeof(struct wc_memory_entry);
2448         if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
2449 overflow:
2450                 ti->error = "Overflow in size calculation";
2451                 r = -EINVAL;
2452                 goto bad;
2453         }
2454         offset += sizeof(struct wc_memory_superblock);
2455         if (offset < sizeof(struct wc_memory_superblock))
2456                 goto overflow;
2457         offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
2458         data_size = wc->n_blocks * (size_t)wc->block_size;
2459         if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
2460             (offset + data_size < offset))
2461                 goto overflow;
2462         if (offset + data_size > wc->memory_map_size) {
2463                 ti->error = "Memory area is too small";
2464                 r = -EINVAL;
2465                 goto bad;
2466         }
2467
2468         wc->metadata_sectors = offset >> SECTOR_SHIFT;
2469         wc->block_start = (char *)sb(wc) + offset;
2470
2471         x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
2472         x += 50;
2473         do_div(x, 100);
2474         wc->freelist_high_watermark = x;
2475         x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
2476         x += 50;
2477         do_div(x, 100);
2478         wc->freelist_low_watermark = x;
2479
2480         if (wc->cleaner)
2481                 activate_cleaner(wc);
2482
2483         r = writecache_alloc_entries(wc);
2484         if (r) {
2485                 ti->error = "Cannot allocate memory";
2486                 goto bad;
2487         }
2488
2489         ti->num_flush_bios = 1;
2490         ti->flush_supported = true;
2491         ti->num_discard_bios = 1;
2492
2493         if (WC_MODE_PMEM(wc))
2494                 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
2495
2496         return 0;
2497
2498 bad_arguments:
2499         r = -EINVAL;
2500         ti->error = "Bad arguments";
2501 bad:
2502         writecache_dtr(ti);
2503         return r;
2504 }
2505
2506 static void writecache_status(struct dm_target *ti, status_type_t type,
2507                               unsigned status_flags, char *result, unsigned maxlen)
2508 {
2509         struct dm_writecache *wc = ti->private;
2510         unsigned extra_args;
2511         unsigned sz = 0;
2512
2513         switch (type) {
2514         case STATUSTYPE_INFO:
2515                 DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),
2516                        (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2517                        (unsigned long long)wc->writeback_size);
2518                 break;
2519         case STATUSTYPE_TABLE:
2520                 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
2521                                 wc->dev->name, wc->ssd_dev->name, wc->block_size);
2522                 extra_args = 0;
2523                 if (wc->start_sector_set)
2524                         extra_args += 2;
2525                 if (wc->high_wm_percent_set)
2526                         extra_args += 2;
2527                 if (wc->low_wm_percent_set)
2528                         extra_args += 2;
2529                 if (wc->max_writeback_jobs_set)
2530                         extra_args += 2;
2531                 if (wc->autocommit_blocks_set)
2532                         extra_args += 2;
2533                 if (wc->autocommit_time_set)
2534                         extra_args += 2;
2535                 if (wc->max_age_set)
2536                         extra_args += 2;
2537                 if (wc->cleaner_set)
2538                         extra_args++;
2539                 if (wc->writeback_fua_set)
2540                         extra_args++;
2541
2542                 DMEMIT("%u", extra_args);
2543                 if (wc->start_sector_set)
2544                         DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2545                 if (wc->high_wm_percent_set)
2546                         DMEMIT(" high_watermark %u", wc->high_wm_percent_value);
2547                 if (wc->low_wm_percent_set)
2548                         DMEMIT(" low_watermark %u", wc->low_wm_percent_value);
2549                 if (wc->max_writeback_jobs_set)
2550                         DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
2551                 if (wc->autocommit_blocks_set)
2552                         DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
2553                 if (wc->autocommit_time_set)
2554                         DMEMIT(" autocommit_time %u", wc->autocommit_time_value);
2555                 if (wc->max_age_set)
2556                         DMEMIT(" max_age %u", wc->max_age_value);
2557                 if (wc->cleaner_set)
2558                         DMEMIT(" cleaner");
2559                 if (wc->writeback_fua_set)
2560                         DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2561                 break;
2562         }
2563 }
2564
2565 static struct target_type writecache_target = {
2566         .name                   = "writecache",
2567         .version                = {1, 4, 0},
2568         .module                 = THIS_MODULE,
2569         .ctr                    = writecache_ctr,
2570         .dtr                    = writecache_dtr,
2571         .status                 = writecache_status,
2572         .postsuspend            = writecache_suspend,
2573         .resume                 = writecache_resume,
2574         .message                = writecache_message,
2575         .map                    = writecache_map,
2576         .end_io                 = writecache_end_io,
2577         .iterate_devices        = writecache_iterate_devices,
2578         .io_hints               = writecache_io_hints,
2579 };
2580
2581 static int __init dm_writecache_init(void)
2582 {
2583         int r;
2584
2585         r = dm_register_target(&writecache_target);
2586         if (r < 0) {
2587                 DMERR("register failed %d", r);
2588                 return r;
2589         }
2590
2591         return 0;
2592 }
2593
2594 static void __exit dm_writecache_exit(void)
2595 {
2596         dm_unregister_target(&writecache_target);
2597 }
2598
2599 module_init(dm_writecache_init);
2600 module_exit(dm_writecache_exit);
2601
2602 MODULE_DESCRIPTION(DM_NAME " writecache target");
2603 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2604 MODULE_LICENSE("GPL");