drm/etnaviv: Implement mmap as GEM object function
[linux-2.6-microblaze.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                         if (writecache_has_error(wc))
1364                                 goto unlock_error;
1365                         e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1366                         if (e) {
1367                                 if (!writecache_entry_is_committed(wc, e))
1368                                         goto bio_copy;
1369                                 if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1370                                         wc->overwrote_committed = true;
1371                                         goto bio_copy;
1372                                 }
1373                                 found_entry = true;
1374                         } else {
1375                                 if (unlikely(wc->cleaner))
1376                                         goto direct_write;
1377                         }
1378                         e = writecache_pop_from_freelist(wc, (sector_t)-1);
1379                         if (unlikely(!e)) {
1380                                 if (!found_entry) {
1381 direct_write:
1382                                         e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1383                                         if (e) {
1384                                                 sector_t next_boundary = read_original_sector(wc, e) - bio->bi_iter.bi_sector;
1385                                                 BUG_ON(!next_boundary);
1386                                                 if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT) {
1387                                                         dm_accept_partial_bio(bio, next_boundary);
1388                                                 }
1389                                         }
1390                                         goto unlock_remap_origin;
1391                                 }
1392                                 writecache_wait_on_freelist(wc);
1393                                 continue;
1394                         }
1395                         write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1396                         writecache_insert_entry(wc, e);
1397                         wc->uncommitted_blocks++;
1398 bio_copy:
1399                         if (WC_MODE_PMEM(wc)) {
1400                                 bio_copy_block(wc, bio, memory_data(wc, e));
1401                         } else {
1402                                 unsigned bio_size = wc->block_size;
1403                                 sector_t start_cache_sec = cache_sector(wc, e);
1404                                 sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT);
1405
1406                                 while (bio_size < bio->bi_iter.bi_size) {
1407                                         struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec);
1408                                         if (!f)
1409                                                 break;
1410                                         write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector +
1411                                                                         (bio_size >> SECTOR_SHIFT), wc->seq_count);
1412                                         writecache_insert_entry(wc, f);
1413                                         wc->uncommitted_blocks++;
1414                                         bio_size += wc->block_size;
1415                                         current_cache_sec += wc->block_size >> SECTOR_SHIFT;
1416                                 }
1417
1418                                 bio_set_dev(bio, wc->ssd_dev->bdev);
1419                                 bio->bi_iter.bi_sector = start_cache_sec;
1420                                 dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT);
1421
1422                                 if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1423                                         wc->uncommitted_blocks = 0;
1424                                         queue_work(wc->writeback_wq, &wc->flush_work);
1425                                 } else {
1426                                         writecache_schedule_autocommit(wc);
1427                                 }
1428                                 goto unlock_remap;
1429                         }
1430                 } while (bio->bi_iter.bi_size);
1431
1432                 if (unlikely(bio->bi_opf & REQ_FUA ||
1433                              wc->uncommitted_blocks >= wc->autocommit_blocks))
1434                         writecache_flush(wc);
1435                 else
1436                         writecache_schedule_autocommit(wc);
1437                 goto unlock_submit;
1438         }
1439
1440 unlock_remap_origin:
1441         bio_set_dev(bio, wc->dev->bdev);
1442         wc_unlock(wc);
1443         return DM_MAPIO_REMAPPED;
1444
1445 unlock_remap:
1446         /* make sure that writecache_end_io decrements bio_in_progress: */
1447         bio->bi_private = (void *)1;
1448         atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
1449         wc_unlock(wc);
1450         return DM_MAPIO_REMAPPED;
1451
1452 unlock_submit:
1453         wc_unlock(wc);
1454         bio_endio(bio);
1455         return DM_MAPIO_SUBMITTED;
1456
1457 unlock_return:
1458         wc_unlock(wc);
1459         return DM_MAPIO_SUBMITTED;
1460
1461 unlock_error:
1462         wc_unlock(wc);
1463         bio_io_error(bio);
1464         return DM_MAPIO_SUBMITTED;
1465 }
1466
1467 static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
1468 {
1469         struct dm_writecache *wc = ti->private;
1470
1471         if (bio->bi_private != NULL) {
1472                 int dir = bio_data_dir(bio);
1473                 if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
1474                         if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
1475                                 wake_up(&wc->bio_in_progress_wait[dir]);
1476         }
1477         return 0;
1478 }
1479
1480 static int writecache_iterate_devices(struct dm_target *ti,
1481                                       iterate_devices_callout_fn fn, void *data)
1482 {
1483         struct dm_writecache *wc = ti->private;
1484
1485         return fn(ti, wc->dev, 0, ti->len, data);
1486 }
1487
1488 static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
1489 {
1490         struct dm_writecache *wc = ti->private;
1491
1492         if (limits->logical_block_size < wc->block_size)
1493                 limits->logical_block_size = wc->block_size;
1494
1495         if (limits->physical_block_size < wc->block_size)
1496                 limits->physical_block_size = wc->block_size;
1497
1498         if (limits->io_min < wc->block_size)
1499                 limits->io_min = wc->block_size;
1500 }
1501
1502
1503 static void writecache_writeback_endio(struct bio *bio)
1504 {
1505         struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
1506         struct dm_writecache *wc = wb->wc;
1507         unsigned long flags;
1508
1509         raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
1510         if (unlikely(list_empty(&wc->endio_list)))
1511                 wake_up_process(wc->endio_thread);
1512         list_add_tail(&wb->endio_entry, &wc->endio_list);
1513         raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
1514 }
1515
1516 static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
1517 {
1518         struct copy_struct *c = ptr;
1519         struct dm_writecache *wc = c->wc;
1520
1521         c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
1522
1523         raw_spin_lock_irq(&wc->endio_list_lock);
1524         if (unlikely(list_empty(&wc->endio_list)))
1525                 wake_up_process(wc->endio_thread);
1526         list_add_tail(&c->endio_entry, &wc->endio_list);
1527         raw_spin_unlock_irq(&wc->endio_list_lock);
1528 }
1529
1530 static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
1531 {
1532         unsigned i;
1533         struct writeback_struct *wb;
1534         struct wc_entry *e;
1535         unsigned long n_walked = 0;
1536
1537         do {
1538                 wb = list_entry(list->next, struct writeback_struct, endio_entry);
1539                 list_del(&wb->endio_entry);
1540
1541                 if (unlikely(wb->bio.bi_status != BLK_STS_OK))
1542                         writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
1543                                         "write error %d", wb->bio.bi_status);
1544                 i = 0;
1545                 do {
1546                         e = wb->wc_list[i];
1547                         BUG_ON(!e->write_in_progress);
1548                         e->write_in_progress = false;
1549                         INIT_LIST_HEAD(&e->lru);
1550                         if (!writecache_has_error(wc))
1551                                 writecache_free_entry(wc, e);
1552                         BUG_ON(!wc->writeback_size);
1553                         wc->writeback_size--;
1554                         n_walked++;
1555                         if (unlikely(n_walked >= ENDIO_LATENCY)) {
1556                                 writecache_commit_flushed(wc, false);
1557                                 wc_unlock(wc);
1558                                 wc_lock(wc);
1559                                 n_walked = 0;
1560                         }
1561                 } while (++i < wb->wc_list_n);
1562
1563                 if (wb->wc_list != wb->wc_list_inline)
1564                         kfree(wb->wc_list);
1565                 bio_put(&wb->bio);
1566         } while (!list_empty(list));
1567 }
1568
1569 static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
1570 {
1571         struct copy_struct *c;
1572         struct wc_entry *e;
1573
1574         do {
1575                 c = list_entry(list->next, struct copy_struct, endio_entry);
1576                 list_del(&c->endio_entry);
1577
1578                 if (unlikely(c->error))
1579                         writecache_error(wc, c->error, "copy error");
1580
1581                 e = c->e;
1582                 do {
1583                         BUG_ON(!e->write_in_progress);
1584                         e->write_in_progress = false;
1585                         INIT_LIST_HEAD(&e->lru);
1586                         if (!writecache_has_error(wc))
1587                                 writecache_free_entry(wc, e);
1588
1589                         BUG_ON(!wc->writeback_size);
1590                         wc->writeback_size--;
1591                         e++;
1592                 } while (--c->n_entries);
1593                 mempool_free(c, &wc->copy_pool);
1594         } while (!list_empty(list));
1595 }
1596
1597 static int writecache_endio_thread(void *data)
1598 {
1599         struct dm_writecache *wc = data;
1600
1601         while (1) {
1602                 struct list_head list;
1603
1604                 raw_spin_lock_irq(&wc->endio_list_lock);
1605                 if (!list_empty(&wc->endio_list))
1606                         goto pop_from_list;
1607                 set_current_state(TASK_INTERRUPTIBLE);
1608                 raw_spin_unlock_irq(&wc->endio_list_lock);
1609
1610                 if (unlikely(kthread_should_stop())) {
1611                         set_current_state(TASK_RUNNING);
1612                         break;
1613                 }
1614
1615                 schedule();
1616
1617                 continue;
1618
1619 pop_from_list:
1620                 list = wc->endio_list;
1621                 list.next->prev = list.prev->next = &list;
1622                 INIT_LIST_HEAD(&wc->endio_list);
1623                 raw_spin_unlock_irq(&wc->endio_list_lock);
1624
1625                 if (!WC_MODE_FUA(wc))
1626                         writecache_disk_flush(wc, wc->dev);
1627
1628                 wc_lock(wc);
1629
1630                 if (WC_MODE_PMEM(wc)) {
1631                         __writecache_endio_pmem(wc, &list);
1632                 } else {
1633                         __writecache_endio_ssd(wc, &list);
1634                         writecache_wait_for_ios(wc, READ);
1635                 }
1636
1637                 writecache_commit_flushed(wc, false);
1638
1639                 wc_unlock(wc);
1640         }
1641
1642         return 0;
1643 }
1644
1645 static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e, gfp_t gfp)
1646 {
1647         struct dm_writecache *wc = wb->wc;
1648         unsigned block_size = wc->block_size;
1649         void *address = memory_data(wc, e);
1650
1651         persistent_memory_flush_cache(address, block_size);
1652
1653         if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors))
1654                 return true;
1655
1656         return bio_add_page(&wb->bio, persistent_memory_page(address),
1657                             block_size, persistent_memory_page_offset(address)) != 0;
1658 }
1659
1660 struct writeback_list {
1661         struct list_head list;
1662         size_t size;
1663 };
1664
1665 static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
1666 {
1667         if (unlikely(wc->max_writeback_jobs)) {
1668                 if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
1669                         wc_lock(wc);
1670                         while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
1671                                 writecache_wait_on_freelist(wc);
1672                         wc_unlock(wc);
1673                 }
1674         }
1675         cond_resched();
1676 }
1677
1678 static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
1679 {
1680         struct wc_entry *e, *f;
1681         struct bio *bio;
1682         struct writeback_struct *wb;
1683         unsigned max_pages;
1684
1685         while (wbl->size) {
1686                 wbl->size--;
1687                 e = container_of(wbl->list.prev, struct wc_entry, lru);
1688                 list_del(&e->lru);
1689
1690                 max_pages = e->wc_list_contiguous;
1691
1692                 bio = bio_alloc_bioset(GFP_NOIO, max_pages, &wc->bio_set);
1693                 wb = container_of(bio, struct writeback_struct, bio);
1694                 wb->wc = wc;
1695                 bio->bi_end_io = writecache_writeback_endio;
1696                 bio_set_dev(bio, wc->dev->bdev);
1697                 bio->bi_iter.bi_sector = read_original_sector(wc, e);
1698                 if (max_pages <= WB_LIST_INLINE ||
1699                     unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
1700                                                            GFP_NOIO | __GFP_NORETRY |
1701                                                            __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
1702                         wb->wc_list = wb->wc_list_inline;
1703                         max_pages = WB_LIST_INLINE;
1704                 }
1705
1706                 BUG_ON(!wc_add_block(wb, e, GFP_NOIO));
1707
1708                 wb->wc_list[0] = e;
1709                 wb->wc_list_n = 1;
1710
1711                 while (wbl->size && wb->wc_list_n < max_pages) {
1712                         f = container_of(wbl->list.prev, struct wc_entry, lru);
1713                         if (read_original_sector(wc, f) !=
1714                             read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1715                                 break;
1716                         if (!wc_add_block(wb, f, GFP_NOWAIT | __GFP_NOWARN))
1717                                 break;
1718                         wbl->size--;
1719                         list_del(&f->lru);
1720                         wb->wc_list[wb->wc_list_n++] = f;
1721                         e = f;
1722                 }
1723                 bio_set_op_attrs(bio, REQ_OP_WRITE, WC_MODE_FUA(wc) * REQ_FUA);
1724                 if (writecache_has_error(wc)) {
1725                         bio->bi_status = BLK_STS_IOERR;
1726                         bio_endio(bio);
1727                 } else if (unlikely(!bio_sectors(bio))) {
1728                         bio->bi_status = BLK_STS_OK;
1729                         bio_endio(bio);
1730                 } else {
1731                         submit_bio(bio);
1732                 }
1733
1734                 __writeback_throttle(wc, wbl);
1735         }
1736 }
1737
1738 static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
1739 {
1740         struct wc_entry *e, *f;
1741         struct dm_io_region from, to;
1742         struct copy_struct *c;
1743
1744         while (wbl->size) {
1745                 unsigned n_sectors;
1746
1747                 wbl->size--;
1748                 e = container_of(wbl->list.prev, struct wc_entry, lru);
1749                 list_del(&e->lru);
1750
1751                 n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
1752
1753                 from.bdev = wc->ssd_dev->bdev;
1754                 from.sector = cache_sector(wc, e);
1755                 from.count = n_sectors;
1756                 to.bdev = wc->dev->bdev;
1757                 to.sector = read_original_sector(wc, e);
1758                 to.count = n_sectors;
1759
1760                 c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
1761                 c->wc = wc;
1762                 c->e = e;
1763                 c->n_entries = e->wc_list_contiguous;
1764
1765                 while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
1766                         wbl->size--;
1767                         f = container_of(wbl->list.prev, struct wc_entry, lru);
1768                         BUG_ON(f != e + 1);
1769                         list_del(&f->lru);
1770                         e = f;
1771                 }
1772
1773                 if (unlikely(to.sector + to.count > wc->data_device_sectors)) {
1774                         if (to.sector >= wc->data_device_sectors) {
1775                                 writecache_copy_endio(0, 0, c);
1776                                 continue;
1777                         }
1778                         from.count = to.count = wc->data_device_sectors - to.sector;
1779                 }
1780
1781                 dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
1782
1783                 __writeback_throttle(wc, wbl);
1784         }
1785 }
1786
1787 static void writecache_writeback(struct work_struct *work)
1788 {
1789         struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
1790         struct blk_plug plug;
1791         struct wc_entry *f, *g, *e = NULL;
1792         struct rb_node *node, *next_node;
1793         struct list_head skipped;
1794         struct writeback_list wbl;
1795         unsigned long n_walked;
1796
1797         wc_lock(wc);
1798 restart:
1799         if (writecache_has_error(wc)) {
1800                 wc_unlock(wc);
1801                 return;
1802         }
1803
1804         if (unlikely(wc->writeback_all)) {
1805                 if (writecache_wait_for_writeback(wc))
1806                         goto restart;
1807         }
1808
1809         if (wc->overwrote_committed) {
1810                 writecache_wait_for_ios(wc, WRITE);
1811         }
1812
1813         n_walked = 0;
1814         INIT_LIST_HEAD(&skipped);
1815         INIT_LIST_HEAD(&wbl.list);
1816         wbl.size = 0;
1817         while (!list_empty(&wc->lru) &&
1818                (wc->writeback_all ||
1819                 wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark ||
1820                 (jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >=
1821                  wc->max_age - wc->max_age / MAX_AGE_DIV))) {
1822
1823                 n_walked++;
1824                 if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1825                     likely(!wc->writeback_all) && likely(!dm_suspended(wc->ti))) {
1826                         queue_work(wc->writeback_wq, &wc->writeback_work);
1827                         break;
1828                 }
1829
1830                 if (unlikely(wc->writeback_all)) {
1831                         if (unlikely(!e)) {
1832                                 writecache_flush(wc);
1833                                 e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
1834                         } else
1835                                 e = g;
1836                 } else
1837                         e = container_of(wc->lru.prev, struct wc_entry, lru);
1838                 BUG_ON(e->write_in_progress);
1839                 if (unlikely(!writecache_entry_is_committed(wc, e))) {
1840                         writecache_flush(wc);
1841                 }
1842                 node = rb_prev(&e->rb_node);
1843                 if (node) {
1844                         f = container_of(node, struct wc_entry, rb_node);
1845                         if (unlikely(read_original_sector(wc, f) ==
1846                                      read_original_sector(wc, e))) {
1847                                 BUG_ON(!f->write_in_progress);
1848                                 list_del(&e->lru);
1849                                 list_add(&e->lru, &skipped);
1850                                 cond_resched();
1851                                 continue;
1852                         }
1853                 }
1854                 wc->writeback_size++;
1855                 list_del(&e->lru);
1856                 list_add(&e->lru, &wbl.list);
1857                 wbl.size++;
1858                 e->write_in_progress = true;
1859                 e->wc_list_contiguous = 1;
1860
1861                 f = e;
1862
1863                 while (1) {
1864                         next_node = rb_next(&f->rb_node);
1865                         if (unlikely(!next_node))
1866                                 break;
1867                         g = container_of(next_node, struct wc_entry, rb_node);
1868                         if (unlikely(read_original_sector(wc, g) ==
1869                             read_original_sector(wc, f))) {
1870                                 f = g;
1871                                 continue;
1872                         }
1873                         if (read_original_sector(wc, g) !=
1874                             read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
1875                                 break;
1876                         if (unlikely(g->write_in_progress))
1877                                 break;
1878                         if (unlikely(!writecache_entry_is_committed(wc, g)))
1879                                 break;
1880
1881                         if (!WC_MODE_PMEM(wc)) {
1882                                 if (g != f + 1)
1883                                         break;
1884                         }
1885
1886                         n_walked++;
1887                         //if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
1888                         //      break;
1889
1890                         wc->writeback_size++;
1891                         list_del(&g->lru);
1892                         list_add(&g->lru, &wbl.list);
1893                         wbl.size++;
1894                         g->write_in_progress = true;
1895                         g->wc_list_contiguous = BIO_MAX_VECS;
1896                         f = g;
1897                         e->wc_list_contiguous++;
1898                         if (unlikely(e->wc_list_contiguous == BIO_MAX_VECS)) {
1899                                 if (unlikely(wc->writeback_all)) {
1900                                         next_node = rb_next(&f->rb_node);
1901                                         if (likely(next_node))
1902                                                 g = container_of(next_node, struct wc_entry, rb_node);
1903                                 }
1904                                 break;
1905                         }
1906                 }
1907                 cond_resched();
1908         }
1909
1910         if (!list_empty(&skipped)) {
1911                 list_splice_tail(&skipped, &wc->lru);
1912                 /*
1913                  * If we didn't do any progress, we must wait until some
1914                  * writeback finishes to avoid burning CPU in a loop
1915                  */
1916                 if (unlikely(!wbl.size))
1917                         writecache_wait_for_writeback(wc);
1918         }
1919
1920         wc_unlock(wc);
1921
1922         blk_start_plug(&plug);
1923
1924         if (WC_MODE_PMEM(wc))
1925                 __writecache_writeback_pmem(wc, &wbl);
1926         else
1927                 __writecache_writeback_ssd(wc, &wbl);
1928
1929         blk_finish_plug(&plug);
1930
1931         if (unlikely(wc->writeback_all)) {
1932                 wc_lock(wc);
1933                 while (writecache_wait_for_writeback(wc));
1934                 wc_unlock(wc);
1935         }
1936 }
1937
1938 static int calculate_memory_size(uint64_t device_size, unsigned block_size,
1939                                  size_t *n_blocks_p, size_t *n_metadata_blocks_p)
1940 {
1941         uint64_t n_blocks, offset;
1942         struct wc_entry e;
1943
1944         n_blocks = device_size;
1945         do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
1946
1947         while (1) {
1948                 if (!n_blocks)
1949                         return -ENOSPC;
1950                 /* Verify the following entries[n_blocks] won't overflow */
1951                 if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
1952                                  sizeof(struct wc_memory_entry)))
1953                         return -EFBIG;
1954                 offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
1955                 offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
1956                 if (offset + n_blocks * block_size <= device_size)
1957                         break;
1958                 n_blocks--;
1959         }
1960
1961         /* check if the bit field overflows */
1962         e.index = n_blocks;
1963         if (e.index != n_blocks)
1964                 return -EFBIG;
1965
1966         if (n_blocks_p)
1967                 *n_blocks_p = n_blocks;
1968         if (n_metadata_blocks_p)
1969                 *n_metadata_blocks_p = offset >> __ffs(block_size);
1970         return 0;
1971 }
1972
1973 static int init_memory(struct dm_writecache *wc)
1974 {
1975         size_t b;
1976         int r;
1977
1978         r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
1979         if (r)
1980                 return r;
1981
1982         r = writecache_alloc_entries(wc);
1983         if (r)
1984                 return r;
1985
1986         for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
1987                 pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
1988         pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
1989         pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
1990         pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
1991         pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
1992
1993         for (b = 0; b < wc->n_blocks; b++) {
1994                 write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
1995                 cond_resched();
1996         }
1997
1998         writecache_flush_all_metadata(wc);
1999         writecache_commit_flushed(wc, false);
2000         pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
2001         writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
2002         writecache_commit_flushed(wc, false);
2003
2004         return 0;
2005 }
2006
2007 static void writecache_dtr(struct dm_target *ti)
2008 {
2009         struct dm_writecache *wc = ti->private;
2010
2011         if (!wc)
2012                 return;
2013
2014         if (wc->endio_thread)
2015                 kthread_stop(wc->endio_thread);
2016
2017         if (wc->flush_thread)
2018                 kthread_stop(wc->flush_thread);
2019
2020         bioset_exit(&wc->bio_set);
2021
2022         mempool_exit(&wc->copy_pool);
2023
2024         if (wc->writeback_wq)
2025                 destroy_workqueue(wc->writeback_wq);
2026
2027         if (wc->dev)
2028                 dm_put_device(ti, wc->dev);
2029
2030         if (wc->ssd_dev)
2031                 dm_put_device(ti, wc->ssd_dev);
2032
2033         vfree(wc->entries);
2034
2035         if (wc->memory_map) {
2036                 if (WC_MODE_PMEM(wc))
2037                         persistent_memory_release(wc);
2038                 else
2039                         vfree(wc->memory_map);
2040         }
2041
2042         if (wc->dm_kcopyd)
2043                 dm_kcopyd_client_destroy(wc->dm_kcopyd);
2044
2045         if (wc->dm_io)
2046                 dm_io_client_destroy(wc->dm_io);
2047
2048         vfree(wc->dirty_bitmap);
2049
2050         kfree(wc);
2051 }
2052
2053 static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
2054 {
2055         struct dm_writecache *wc;
2056         struct dm_arg_set as;
2057         const char *string;
2058         unsigned opt_params;
2059         size_t offset, data_size;
2060         int i, r;
2061         char dummy;
2062         int high_wm_percent = HIGH_WATERMARK;
2063         int low_wm_percent = LOW_WATERMARK;
2064         uint64_t x;
2065         struct wc_memory_superblock s;
2066
2067         static struct dm_arg _args[] = {
2068                 {0, 16, "Invalid number of feature args"},
2069         };
2070
2071         as.argc = argc;
2072         as.argv = argv;
2073
2074         wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
2075         if (!wc) {
2076                 ti->error = "Cannot allocate writecache structure";
2077                 r = -ENOMEM;
2078                 goto bad;
2079         }
2080         ti->private = wc;
2081         wc->ti = ti;
2082
2083         mutex_init(&wc->lock);
2084         wc->max_age = MAX_AGE_UNSPECIFIED;
2085         writecache_poison_lists(wc);
2086         init_waitqueue_head(&wc->freelist_wait);
2087         timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
2088         timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0);
2089
2090         for (i = 0; i < 2; i++) {
2091                 atomic_set(&wc->bio_in_progress[i], 0);
2092                 init_waitqueue_head(&wc->bio_in_progress_wait[i]);
2093         }
2094
2095         wc->dm_io = dm_io_client_create();
2096         if (IS_ERR(wc->dm_io)) {
2097                 r = PTR_ERR(wc->dm_io);
2098                 ti->error = "Unable to allocate dm-io client";
2099                 wc->dm_io = NULL;
2100                 goto bad;
2101         }
2102
2103         wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
2104         if (!wc->writeback_wq) {
2105                 r = -ENOMEM;
2106                 ti->error = "Could not allocate writeback workqueue";
2107                 goto bad;
2108         }
2109         INIT_WORK(&wc->writeback_work, writecache_writeback);
2110         INIT_WORK(&wc->flush_work, writecache_flush_work);
2111
2112         raw_spin_lock_init(&wc->endio_list_lock);
2113         INIT_LIST_HEAD(&wc->endio_list);
2114         wc->endio_thread = kthread_create(writecache_endio_thread, wc, "writecache_endio");
2115         if (IS_ERR(wc->endio_thread)) {
2116                 r = PTR_ERR(wc->endio_thread);
2117                 wc->endio_thread = NULL;
2118                 ti->error = "Couldn't spawn endio thread";
2119                 goto bad;
2120         }
2121         wake_up_process(wc->endio_thread);
2122
2123         /*
2124          * Parse the mode (pmem or ssd)
2125          */
2126         string = dm_shift_arg(&as);
2127         if (!string)
2128                 goto bad_arguments;
2129
2130         if (!strcasecmp(string, "s")) {
2131                 wc->pmem_mode = false;
2132         } else if (!strcasecmp(string, "p")) {
2133 #ifdef DM_WRITECACHE_HAS_PMEM
2134                 wc->pmem_mode = true;
2135                 wc->writeback_fua = true;
2136 #else
2137                 /*
2138                  * If the architecture doesn't support persistent memory or
2139                  * the kernel doesn't support any DAX drivers, this driver can
2140                  * only be used in SSD-only mode.
2141                  */
2142                 r = -EOPNOTSUPP;
2143                 ti->error = "Persistent memory or DAX not supported on this system";
2144                 goto bad;
2145 #endif
2146         } else {
2147                 goto bad_arguments;
2148         }
2149
2150         if (WC_MODE_PMEM(wc)) {
2151                 r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
2152                                 offsetof(struct writeback_struct, bio),
2153                                 BIOSET_NEED_BVECS);
2154                 if (r) {
2155                         ti->error = "Could not allocate bio set";
2156                         goto bad;
2157                 }
2158         } else {
2159                 r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
2160                 if (r) {
2161                         ti->error = "Could not allocate mempool";
2162                         goto bad;
2163                 }
2164         }
2165
2166         /*
2167          * Parse the origin data device
2168          */
2169         string = dm_shift_arg(&as);
2170         if (!string)
2171                 goto bad_arguments;
2172         r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
2173         if (r) {
2174                 ti->error = "Origin data device lookup failed";
2175                 goto bad;
2176         }
2177
2178         /*
2179          * Parse cache data device (be it pmem or ssd)
2180          */
2181         string = dm_shift_arg(&as);
2182         if (!string)
2183                 goto bad_arguments;
2184
2185         r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
2186         if (r) {
2187                 ti->error = "Cache data device lookup failed";
2188                 goto bad;
2189         }
2190         wc->memory_map_size = i_size_read(wc->ssd_dev->bdev->bd_inode);
2191
2192         /*
2193          * Parse the cache block size
2194          */
2195         string = dm_shift_arg(&as);
2196         if (!string)
2197                 goto bad_arguments;
2198         if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
2199             wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
2200             (wc->block_size & (wc->block_size - 1))) {
2201                 r = -EINVAL;
2202                 ti->error = "Invalid block size";
2203                 goto bad;
2204         }
2205         if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) ||
2206             wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) {
2207                 r = -EINVAL;
2208                 ti->error = "Block size is smaller than device logical block size";
2209                 goto bad;
2210         }
2211         wc->block_size_bits = __ffs(wc->block_size);
2212
2213         wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
2214         wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
2215         wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
2216
2217         /*
2218          * Parse optional arguments
2219          */
2220         r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
2221         if (r)
2222                 goto bad;
2223
2224         while (opt_params) {
2225                 string = dm_shift_arg(&as), opt_params--;
2226                 if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2227                         unsigned long long start_sector;
2228                         string = dm_shift_arg(&as), opt_params--;
2229                         if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2230                                 goto invalid_optional;
2231                         wc->start_sector = start_sector;
2232                         wc->start_sector_set = true;
2233                         if (wc->start_sector != start_sector ||
2234                             wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2235                                 goto invalid_optional;
2236                 } else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
2237                         string = dm_shift_arg(&as), opt_params--;
2238                         if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
2239                                 goto invalid_optional;
2240                         if (high_wm_percent < 0 || high_wm_percent > 100)
2241                                 goto invalid_optional;
2242                         wc->high_wm_percent_value = high_wm_percent;
2243                         wc->high_wm_percent_set = true;
2244                 } else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
2245                         string = dm_shift_arg(&as), opt_params--;
2246                         if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
2247                                 goto invalid_optional;
2248                         if (low_wm_percent < 0 || low_wm_percent > 100)
2249                                 goto invalid_optional;
2250                         wc->low_wm_percent_value = low_wm_percent;
2251                         wc->low_wm_percent_set = true;
2252                 } else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
2253                         string = dm_shift_arg(&as), opt_params--;
2254                         if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
2255                                 goto invalid_optional;
2256                         wc->max_writeback_jobs_set = true;
2257                 } else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
2258                         string = dm_shift_arg(&as), opt_params--;
2259                         if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
2260                                 goto invalid_optional;
2261                         wc->autocommit_blocks_set = true;
2262                 } else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
2263                         unsigned autocommit_msecs;
2264                         string = dm_shift_arg(&as), opt_params--;
2265                         if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
2266                                 goto invalid_optional;
2267                         if (autocommit_msecs > 3600000)
2268                                 goto invalid_optional;
2269                         wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2270                         wc->autocommit_time_value = autocommit_msecs;
2271                         wc->autocommit_time_set = true;
2272                 } else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
2273                         unsigned max_age_msecs;
2274                         string = dm_shift_arg(&as), opt_params--;
2275                         if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
2276                                 goto invalid_optional;
2277                         if (max_age_msecs > 86400000)
2278                                 goto invalid_optional;
2279                         wc->max_age = msecs_to_jiffies(max_age_msecs);
2280                         wc->max_age_set = true;
2281                         wc->max_age_value = max_age_msecs;
2282                 } else if (!strcasecmp(string, "cleaner")) {
2283                         wc->cleaner_set = true;
2284                         wc->cleaner = true;
2285                 } else if (!strcasecmp(string, "fua")) {
2286                         if (WC_MODE_PMEM(wc)) {
2287                                 wc->writeback_fua = true;
2288                                 wc->writeback_fua_set = true;
2289                         } else goto invalid_optional;
2290                 } else if (!strcasecmp(string, "nofua")) {
2291                         if (WC_MODE_PMEM(wc)) {
2292                                 wc->writeback_fua = false;
2293                                 wc->writeback_fua_set = true;
2294                         } else goto invalid_optional;
2295                 } else {
2296 invalid_optional:
2297                         r = -EINVAL;
2298                         ti->error = "Invalid optional argument";
2299                         goto bad;
2300                 }
2301         }
2302
2303         if (high_wm_percent < low_wm_percent) {
2304                 r = -EINVAL;
2305                 ti->error = "High watermark must be greater than or equal to low watermark";
2306                 goto bad;
2307         }
2308
2309         if (WC_MODE_PMEM(wc)) {
2310                 if (!dax_synchronous(wc->ssd_dev->dax_dev)) {
2311                         r = -EOPNOTSUPP;
2312                         ti->error = "Asynchronous persistent memory not supported as pmem cache";
2313                         goto bad;
2314                 }
2315
2316                 r = persistent_memory_claim(wc);
2317                 if (r) {
2318                         ti->error = "Unable to map persistent memory for cache";
2319                         goto bad;
2320                 }
2321         } else {
2322                 size_t n_blocks, n_metadata_blocks;
2323                 uint64_t n_bitmap_bits;
2324
2325                 wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2326
2327                 bio_list_init(&wc->flush_list);
2328                 wc->flush_thread = kthread_create(writecache_flush_thread, wc, "dm_writecache_flush");
2329                 if (IS_ERR(wc->flush_thread)) {
2330                         r = PTR_ERR(wc->flush_thread);
2331                         wc->flush_thread = NULL;
2332                         ti->error = "Couldn't spawn flush thread";
2333                         goto bad;
2334                 }
2335                 wake_up_process(wc->flush_thread);
2336
2337                 r = calculate_memory_size(wc->memory_map_size, wc->block_size,
2338                                           &n_blocks, &n_metadata_blocks);
2339                 if (r) {
2340                         ti->error = "Invalid device size";
2341                         goto bad;
2342                 }
2343
2344                 n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
2345                                  BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
2346                 /* this is limitation of test_bit functions */
2347                 if (n_bitmap_bits > 1U << 31) {
2348                         r = -EFBIG;
2349                         ti->error = "Invalid device size";
2350                         goto bad;
2351                 }
2352
2353                 wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
2354                 if (!wc->memory_map) {
2355                         r = -ENOMEM;
2356                         ti->error = "Unable to allocate memory for metadata";
2357                         goto bad;
2358                 }
2359
2360                 wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
2361                 if (IS_ERR(wc->dm_kcopyd)) {
2362                         r = PTR_ERR(wc->dm_kcopyd);
2363                         ti->error = "Unable to allocate dm-kcopyd client";
2364                         wc->dm_kcopyd = NULL;
2365                         goto bad;
2366                 }
2367
2368                 wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
2369                 wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
2370                         BITS_PER_LONG * sizeof(unsigned long);
2371                 wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
2372                 if (!wc->dirty_bitmap) {
2373                         r = -ENOMEM;
2374                         ti->error = "Unable to allocate dirty bitmap";
2375                         goto bad;
2376                 }
2377
2378                 r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT);
2379                 if (r) {
2380                         ti->error = "Unable to read first block of metadata";
2381                         goto bad;
2382                 }
2383         }
2384
2385         r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock));
2386         if (r) {
2387                 ti->error = "Hardware memory error when reading superblock";
2388                 goto bad;
2389         }
2390         if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
2391                 r = init_memory(wc);
2392                 if (r) {
2393                         ti->error = "Unable to initialize device";
2394                         goto bad;
2395                 }
2396                 r = copy_mc_to_kernel(&s, sb(wc),
2397                                       sizeof(struct wc_memory_superblock));
2398                 if (r) {
2399                         ti->error = "Hardware memory error when reading superblock";
2400                         goto bad;
2401                 }
2402         }
2403
2404         if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
2405                 ti->error = "Invalid magic in the superblock";
2406                 r = -EINVAL;
2407                 goto bad;
2408         }
2409
2410         if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
2411                 ti->error = "Invalid version in the superblock";
2412                 r = -EINVAL;
2413                 goto bad;
2414         }
2415
2416         if (le32_to_cpu(s.block_size) != wc->block_size) {
2417                 ti->error = "Block size does not match superblock";
2418                 r = -EINVAL;
2419                 goto bad;
2420         }
2421
2422         wc->n_blocks = le64_to_cpu(s.n_blocks);
2423
2424         offset = wc->n_blocks * sizeof(struct wc_memory_entry);
2425         if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
2426 overflow:
2427                 ti->error = "Overflow in size calculation";
2428                 r = -EINVAL;
2429                 goto bad;
2430         }
2431         offset += sizeof(struct wc_memory_superblock);
2432         if (offset < sizeof(struct wc_memory_superblock))
2433                 goto overflow;
2434         offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
2435         data_size = wc->n_blocks * (size_t)wc->block_size;
2436         if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
2437             (offset + data_size < offset))
2438                 goto overflow;
2439         if (offset + data_size > wc->memory_map_size) {
2440                 ti->error = "Memory area is too small";
2441                 r = -EINVAL;
2442                 goto bad;
2443         }
2444
2445         wc->metadata_sectors = offset >> SECTOR_SHIFT;
2446         wc->block_start = (char *)sb(wc) + offset;
2447
2448         x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
2449         x += 50;
2450         do_div(x, 100);
2451         wc->freelist_high_watermark = x;
2452         x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
2453         x += 50;
2454         do_div(x, 100);
2455         wc->freelist_low_watermark = x;
2456
2457         if (wc->cleaner)
2458                 activate_cleaner(wc);
2459
2460         r = writecache_alloc_entries(wc);
2461         if (r) {
2462                 ti->error = "Cannot allocate memory";
2463                 goto bad;
2464         }
2465
2466         ti->num_flush_bios = 1;
2467         ti->flush_supported = true;
2468         ti->num_discard_bios = 1;
2469
2470         if (WC_MODE_PMEM(wc))
2471                 persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
2472
2473         return 0;
2474
2475 bad_arguments:
2476         r = -EINVAL;
2477         ti->error = "Bad arguments";
2478 bad:
2479         writecache_dtr(ti);
2480         return r;
2481 }
2482
2483 static void writecache_status(struct dm_target *ti, status_type_t type,
2484                               unsigned status_flags, char *result, unsigned maxlen)
2485 {
2486         struct dm_writecache *wc = ti->private;
2487         unsigned extra_args;
2488         unsigned sz = 0;
2489
2490         switch (type) {
2491         case STATUSTYPE_INFO:
2492                 DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),
2493                        (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2494                        (unsigned long long)wc->writeback_size);
2495                 break;
2496         case STATUSTYPE_TABLE:
2497                 DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
2498                                 wc->dev->name, wc->ssd_dev->name, wc->block_size);
2499                 extra_args = 0;
2500                 if (wc->start_sector_set)
2501                         extra_args += 2;
2502                 if (wc->high_wm_percent_set)
2503                         extra_args += 2;
2504                 if (wc->low_wm_percent_set)
2505                         extra_args += 2;
2506                 if (wc->max_writeback_jobs_set)
2507                         extra_args += 2;
2508                 if (wc->autocommit_blocks_set)
2509                         extra_args += 2;
2510                 if (wc->autocommit_time_set)
2511                         extra_args += 2;
2512                 if (wc->max_age_set)
2513                         extra_args += 2;
2514                 if (wc->cleaner_set)
2515                         extra_args++;
2516                 if (wc->writeback_fua_set)
2517                         extra_args++;
2518
2519                 DMEMIT("%u", extra_args);
2520                 if (wc->start_sector_set)
2521                         DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2522                 if (wc->high_wm_percent_set)
2523                         DMEMIT(" high_watermark %u", wc->high_wm_percent_value);
2524                 if (wc->low_wm_percent_set)
2525                         DMEMIT(" low_watermark %u", wc->low_wm_percent_value);
2526                 if (wc->max_writeback_jobs_set)
2527                         DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
2528                 if (wc->autocommit_blocks_set)
2529                         DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
2530                 if (wc->autocommit_time_set)
2531                         DMEMIT(" autocommit_time %u", wc->autocommit_time_value);
2532                 if (wc->max_age_set)
2533                         DMEMIT(" max_age %u", wc->max_age_value);
2534                 if (wc->cleaner_set)
2535                         DMEMIT(" cleaner");
2536                 if (wc->writeback_fua_set)
2537                         DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2538                 break;
2539         }
2540 }
2541
2542 static struct target_type writecache_target = {
2543         .name                   = "writecache",
2544         .version                = {1, 4, 0},
2545         .module                 = THIS_MODULE,
2546         .ctr                    = writecache_ctr,
2547         .dtr                    = writecache_dtr,
2548         .status                 = writecache_status,
2549         .postsuspend            = writecache_suspend,
2550         .resume                 = writecache_resume,
2551         .message                = writecache_message,
2552         .map                    = writecache_map,
2553         .end_io                 = writecache_end_io,
2554         .iterate_devices        = writecache_iterate_devices,
2555         .io_hints               = writecache_io_hints,
2556 };
2557
2558 static int __init dm_writecache_init(void)
2559 {
2560         int r;
2561
2562         r = dm_register_target(&writecache_target);
2563         if (r < 0) {
2564                 DMERR("register failed %d", r);
2565                 return r;
2566         }
2567
2568         return 0;
2569 }
2570
2571 static void __exit dm_writecache_exit(void)
2572 {
2573         dm_unregister_target(&writecache_target);
2574 }
2575
2576 module_init(dm_writecache_init);
2577 module_exit(dm_writecache_exit);
2578
2579 MODULE_DESCRIPTION(DM_NAME " writecache target");
2580 MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
2581 MODULE_LICENSE("GPL");