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