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