xref: /linux/drivers/md/dm-writecache.c (revision df699cc16ea5ef93a917676dcdd4376e02860ad5)
148debafeSMikulas Patocka // SPDX-License-Identifier: GPL-2.0
248debafeSMikulas Patocka /*
348debafeSMikulas Patocka  * Copyright (C) 2018 Red Hat. All rights reserved.
448debafeSMikulas Patocka  *
548debafeSMikulas Patocka  * This file is released under the GPL.
648debafeSMikulas Patocka  */
748debafeSMikulas Patocka 
848debafeSMikulas Patocka #include <linux/device-mapper.h>
948debafeSMikulas Patocka #include <linux/module.h>
1048debafeSMikulas Patocka #include <linux/init.h>
1148debafeSMikulas Patocka #include <linux/vmalloc.h>
1248debafeSMikulas Patocka #include <linux/kthread.h>
1348debafeSMikulas Patocka #include <linux/dm-io.h>
1448debafeSMikulas Patocka #include <linux/dm-kcopyd.h>
1548debafeSMikulas Patocka #include <linux/dax.h>
1648debafeSMikulas Patocka #include <linux/pfn_t.h>
1748debafeSMikulas Patocka #include <linux/libnvdimm.h>
1895b88f4dSMikulas Patocka #include <linux/delay.h>
1995b88f4dSMikulas Patocka #include "dm-io-tracker.h"
2048debafeSMikulas Patocka 
2148debafeSMikulas Patocka #define DM_MSG_PREFIX "writecache"
2248debafeSMikulas Patocka 
2348debafeSMikulas Patocka #define HIGH_WATERMARK			50
2448debafeSMikulas Patocka #define LOW_WATERMARK			45
2548debafeSMikulas Patocka #define MAX_WRITEBACK_JOBS		0
2648debafeSMikulas Patocka #define ENDIO_LATENCY			16
2748debafeSMikulas Patocka #define WRITEBACK_LATENCY		64
2848debafeSMikulas Patocka #define AUTOCOMMIT_BLOCKS_SSD		65536
2948debafeSMikulas Patocka #define AUTOCOMMIT_BLOCKS_PMEM		64
3048debafeSMikulas Patocka #define AUTOCOMMIT_MSEC			1000
313923d485SMikulas Patocka #define MAX_AGE_DIV			16
323923d485SMikulas Patocka #define MAX_AGE_UNSPECIFIED		-1UL
335c0de3d7SMikulas Patocka #define PAUSE_WRITEBACK			(HZ * 3)
3448debafeSMikulas Patocka 
3548debafeSMikulas Patocka #define BITMAP_GRANULARITY	65536
3648debafeSMikulas Patocka #if BITMAP_GRANULARITY < PAGE_SIZE
3748debafeSMikulas Patocka #undef BITMAP_GRANULARITY
3848debafeSMikulas Patocka #define BITMAP_GRANULARITY	PAGE_SIZE
3948debafeSMikulas Patocka #endif
4048debafeSMikulas Patocka 
4148debafeSMikulas Patocka #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_DAX_DRIVER)
4248debafeSMikulas Patocka #define DM_WRITECACHE_HAS_PMEM
4348debafeSMikulas Patocka #endif
4448debafeSMikulas Patocka 
4548debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM
4648debafeSMikulas Patocka #define pmem_assign(dest, src)					\
4748debafeSMikulas Patocka do {								\
4848debafeSMikulas Patocka 	typeof(dest) uniq = (src);				\
4948debafeSMikulas Patocka 	memcpy_flushcache(&(dest), &uniq, sizeof(dest));	\
5048debafeSMikulas Patocka } while (0)
5148debafeSMikulas Patocka #else
5248debafeSMikulas Patocka #define pmem_assign(dest, src)	((dest) = (src))
5348debafeSMikulas Patocka #endif
5448debafeSMikulas Patocka 
55ec6347bbSDan Williams #if IS_ENABLED(CONFIG_ARCH_HAS_COPY_MC) && defined(DM_WRITECACHE_HAS_PMEM)
5648debafeSMikulas Patocka #define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
5748debafeSMikulas Patocka #endif
5848debafeSMikulas Patocka 
5948debafeSMikulas Patocka #define MEMORY_SUPERBLOCK_MAGIC		0x23489321
6048debafeSMikulas Patocka #define MEMORY_SUPERBLOCK_VERSION	1
6148debafeSMikulas Patocka 
6248debafeSMikulas Patocka struct wc_memory_entry {
6348debafeSMikulas Patocka 	__le64 original_sector;
6448debafeSMikulas Patocka 	__le64 seq_count;
6548debafeSMikulas Patocka };
6648debafeSMikulas Patocka 
6748debafeSMikulas Patocka struct wc_memory_superblock {
6848debafeSMikulas Patocka 	union {
6948debafeSMikulas Patocka 		struct {
7048debafeSMikulas Patocka 			__le32 magic;
7148debafeSMikulas Patocka 			__le32 version;
7248debafeSMikulas Patocka 			__le32 block_size;
7348debafeSMikulas Patocka 			__le32 pad;
7448debafeSMikulas Patocka 			__le64 n_blocks;
7548debafeSMikulas Patocka 			__le64 seq_count;
7648debafeSMikulas Patocka 		};
7748debafeSMikulas Patocka 		__le64 padding[8];
7848debafeSMikulas Patocka 	};
79c40819f2SJulia Lawall 	struct wc_memory_entry entries[];
8048debafeSMikulas Patocka };
8148debafeSMikulas Patocka 
8248debafeSMikulas Patocka struct wc_entry {
8348debafeSMikulas Patocka 	struct rb_node rb_node;
8448debafeSMikulas Patocka 	struct list_head lru;
8548debafeSMikulas Patocka 	unsigned short wc_list_contiguous;
8648debafeSMikulas Patocka 	bool write_in_progress
8748debafeSMikulas Patocka #if BITS_PER_LONG == 64
8848debafeSMikulas Patocka 		:1
8948debafeSMikulas Patocka #endif
9048debafeSMikulas Patocka 	;
9148debafeSMikulas Patocka 	unsigned long index
9248debafeSMikulas Patocka #if BITS_PER_LONG == 64
9348debafeSMikulas Patocka 		:47
9448debafeSMikulas Patocka #endif
9548debafeSMikulas Patocka 	;
963923d485SMikulas Patocka 	unsigned long age;
9748debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
9848debafeSMikulas Patocka 	uint64_t original_sector;
9948debafeSMikulas Patocka 	uint64_t seq_count;
10048debafeSMikulas Patocka #endif
10148debafeSMikulas Patocka };
10248debafeSMikulas Patocka 
10348debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM
10448debafeSMikulas Patocka #define WC_MODE_PMEM(wc)			((wc)->pmem_mode)
10548debafeSMikulas Patocka #define WC_MODE_FUA(wc)				((wc)->writeback_fua)
10648debafeSMikulas Patocka #else
10748debafeSMikulas Patocka #define WC_MODE_PMEM(wc)			false
10848debafeSMikulas Patocka #define WC_MODE_FUA(wc)				false
10948debafeSMikulas Patocka #endif
11048debafeSMikulas Patocka #define WC_MODE_SORT_FREELIST(wc)		(!WC_MODE_PMEM(wc))
11148debafeSMikulas Patocka 
11248debafeSMikulas Patocka struct dm_writecache {
11348debafeSMikulas Patocka 	struct mutex lock;
11448debafeSMikulas Patocka 	struct list_head lru;
11548debafeSMikulas Patocka 	union {
11648debafeSMikulas Patocka 		struct list_head freelist;
11748debafeSMikulas Patocka 		struct {
11848debafeSMikulas Patocka 			struct rb_root freetree;
11948debafeSMikulas Patocka 			struct wc_entry *current_free;
12048debafeSMikulas Patocka 		};
12148debafeSMikulas Patocka 	};
12248debafeSMikulas Patocka 	struct rb_root tree;
12348debafeSMikulas Patocka 
12448debafeSMikulas Patocka 	size_t freelist_size;
12548debafeSMikulas Patocka 	size_t writeback_size;
12648debafeSMikulas Patocka 	size_t freelist_high_watermark;
12748debafeSMikulas Patocka 	size_t freelist_low_watermark;
1283923d485SMikulas Patocka 	unsigned long max_age;
1295c0de3d7SMikulas Patocka 	unsigned long pause;
13048debafeSMikulas Patocka 
13148debafeSMikulas Patocka 	unsigned uncommitted_blocks;
13248debafeSMikulas Patocka 	unsigned autocommit_blocks;
13348debafeSMikulas Patocka 	unsigned max_writeback_jobs;
13448debafeSMikulas Patocka 
13548debafeSMikulas Patocka 	int error;
13648debafeSMikulas Patocka 
13748debafeSMikulas Patocka 	unsigned long autocommit_jiffies;
13848debafeSMikulas Patocka 	struct timer_list autocommit_timer;
13948debafeSMikulas Patocka 	struct wait_queue_head freelist_wait;
14048debafeSMikulas Patocka 
1413923d485SMikulas Patocka 	struct timer_list max_age_timer;
1423923d485SMikulas Patocka 
14348debafeSMikulas Patocka 	atomic_t bio_in_progress[2];
14448debafeSMikulas Patocka 	struct wait_queue_head bio_in_progress_wait[2];
14548debafeSMikulas Patocka 
14648debafeSMikulas Patocka 	struct dm_target *ti;
14748debafeSMikulas Patocka 	struct dm_dev *dev;
14848debafeSMikulas Patocka 	struct dm_dev *ssd_dev;
149d284f824SMikulas Patocka 	sector_t start_sector;
15048debafeSMikulas Patocka 	void *memory_map;
15148debafeSMikulas Patocka 	uint64_t memory_map_size;
15248debafeSMikulas Patocka 	size_t metadata_sectors;
15348debafeSMikulas Patocka 	size_t n_blocks;
15448debafeSMikulas Patocka 	uint64_t seq_count;
1554134455fSMikulas Patocka 	sector_t data_device_sectors;
15648debafeSMikulas Patocka 	void *block_start;
15748debafeSMikulas Patocka 	struct wc_entry *entries;
15848debafeSMikulas Patocka 	unsigned block_size;
15948debafeSMikulas Patocka 	unsigned char block_size_bits;
16048debafeSMikulas Patocka 
16148debafeSMikulas Patocka 	bool pmem_mode:1;
16248debafeSMikulas Patocka 	bool writeback_fua:1;
16348debafeSMikulas Patocka 
16448debafeSMikulas Patocka 	bool overwrote_committed:1;
16548debafeSMikulas Patocka 	bool memory_vmapped:1;
16648debafeSMikulas Patocka 
167054bee16SMikulas Patocka 	bool start_sector_set:1;
16848debafeSMikulas Patocka 	bool high_wm_percent_set:1;
16948debafeSMikulas Patocka 	bool low_wm_percent_set:1;
17048debafeSMikulas Patocka 	bool max_writeback_jobs_set:1;
17148debafeSMikulas Patocka 	bool autocommit_blocks_set:1;
17248debafeSMikulas Patocka 	bool autocommit_time_set:1;
173054bee16SMikulas Patocka 	bool max_age_set:1;
17448debafeSMikulas Patocka 	bool writeback_fua_set:1;
17548debafeSMikulas Patocka 	bool flush_on_suspend:1;
17693de44ebSMikulas Patocka 	bool cleaner:1;
177054bee16SMikulas Patocka 	bool cleaner_set:1;
178611c3e16SMikulas Patocka 	bool metadata_only:1;
1795c0de3d7SMikulas Patocka 	bool pause_set:1;
180054bee16SMikulas Patocka 
181054bee16SMikulas Patocka 	unsigned high_wm_percent_value;
182054bee16SMikulas Patocka 	unsigned low_wm_percent_value;
183054bee16SMikulas Patocka 	unsigned autocommit_time_value;
184054bee16SMikulas Patocka 	unsigned max_age_value;
1855c0de3d7SMikulas Patocka 	unsigned pause_value;
18648debafeSMikulas Patocka 
18748debafeSMikulas Patocka 	unsigned writeback_all;
18848debafeSMikulas Patocka 	struct workqueue_struct *writeback_wq;
18948debafeSMikulas Patocka 	struct work_struct writeback_work;
19048debafeSMikulas Patocka 	struct work_struct flush_work;
19148debafeSMikulas Patocka 
19295b88f4dSMikulas Patocka 	struct dm_io_tracker iot;
19395b88f4dSMikulas Patocka 
19448debafeSMikulas Patocka 	struct dm_io_client *dm_io;
19548debafeSMikulas Patocka 
19648debafeSMikulas Patocka 	raw_spinlock_t endio_list_lock;
19748debafeSMikulas Patocka 	struct list_head endio_list;
19848debafeSMikulas Patocka 	struct task_struct *endio_thread;
19948debafeSMikulas Patocka 
20048debafeSMikulas Patocka 	struct task_struct *flush_thread;
20148debafeSMikulas Patocka 	struct bio_list flush_list;
20248debafeSMikulas Patocka 
20348debafeSMikulas Patocka 	struct dm_kcopyd_client *dm_kcopyd;
20448debafeSMikulas Patocka 	unsigned long *dirty_bitmap;
20548debafeSMikulas Patocka 	unsigned dirty_bitmap_size;
20648debafeSMikulas Patocka 
20748debafeSMikulas Patocka 	struct bio_set bio_set;
20848debafeSMikulas Patocka 	mempool_t copy_pool;
20948debafeSMikulas Patocka };
21048debafeSMikulas Patocka 
21148debafeSMikulas Patocka #define WB_LIST_INLINE		16
21248debafeSMikulas Patocka 
21348debafeSMikulas Patocka struct writeback_struct {
21448debafeSMikulas Patocka 	struct list_head endio_entry;
21548debafeSMikulas Patocka 	struct dm_writecache *wc;
21648debafeSMikulas Patocka 	struct wc_entry **wc_list;
21748debafeSMikulas Patocka 	unsigned wc_list_n;
21848debafeSMikulas Patocka 	struct wc_entry *wc_list_inline[WB_LIST_INLINE];
21948debafeSMikulas Patocka 	struct bio bio;
22048debafeSMikulas Patocka };
22148debafeSMikulas Patocka 
22248debafeSMikulas Patocka struct copy_struct {
22348debafeSMikulas Patocka 	struct list_head endio_entry;
22448debafeSMikulas Patocka 	struct dm_writecache *wc;
22548debafeSMikulas Patocka 	struct wc_entry *e;
22648debafeSMikulas Patocka 	unsigned n_entries;
22748debafeSMikulas Patocka 	int error;
22848debafeSMikulas Patocka };
22948debafeSMikulas Patocka 
23048debafeSMikulas Patocka DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
23148debafeSMikulas Patocka 					    "A percentage of time allocated for data copying");
23248debafeSMikulas Patocka 
23348debafeSMikulas Patocka static void wc_lock(struct dm_writecache *wc)
23448debafeSMikulas Patocka {
23548debafeSMikulas Patocka 	mutex_lock(&wc->lock);
23648debafeSMikulas Patocka }
23748debafeSMikulas Patocka 
23848debafeSMikulas Patocka static void wc_unlock(struct dm_writecache *wc)
23948debafeSMikulas Patocka {
24048debafeSMikulas Patocka 	mutex_unlock(&wc->lock);
24148debafeSMikulas Patocka }
24248debafeSMikulas Patocka 
24348debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM
24448debafeSMikulas Patocka static int persistent_memory_claim(struct dm_writecache *wc)
24548debafeSMikulas Patocka {
24648debafeSMikulas Patocka 	int r;
24748debafeSMikulas Patocka 	loff_t s;
24848debafeSMikulas Patocka 	long p, da;
24948debafeSMikulas Patocka 	pfn_t pfn;
25048debafeSMikulas Patocka 	int id;
25148debafeSMikulas Patocka 	struct page **pages;
252f9e040efSMikulas Patocka 	sector_t offset;
25348debafeSMikulas Patocka 
25448debafeSMikulas Patocka 	wc->memory_vmapped = false;
25548debafeSMikulas Patocka 
25648debafeSMikulas Patocka 	s = wc->memory_map_size;
25748debafeSMikulas Patocka 	p = s >> PAGE_SHIFT;
25848debafeSMikulas Patocka 	if (!p) {
25948debafeSMikulas Patocka 		r = -EINVAL;
26048debafeSMikulas Patocka 		goto err1;
26148debafeSMikulas Patocka 	}
26248debafeSMikulas Patocka 	if (p != s >> PAGE_SHIFT) {
26348debafeSMikulas Patocka 		r = -EOVERFLOW;
26448debafeSMikulas Patocka 		goto err1;
26548debafeSMikulas Patocka 	}
26648debafeSMikulas Patocka 
267f9e040efSMikulas Patocka 	offset = get_start_sect(wc->ssd_dev->bdev);
268f9e040efSMikulas Patocka 	if (offset & (PAGE_SIZE / 512 - 1)) {
269f9e040efSMikulas Patocka 		r = -EINVAL;
270f9e040efSMikulas Patocka 		goto err1;
271f9e040efSMikulas Patocka 	}
272f9e040efSMikulas Patocka 	offset >>= PAGE_SHIFT - 9;
273f9e040efSMikulas Patocka 
27448debafeSMikulas Patocka 	id = dax_read_lock();
27548debafeSMikulas Patocka 
276f9e040efSMikulas Patocka 	da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, &wc->memory_map, &pfn);
27748debafeSMikulas Patocka 	if (da < 0) {
27848debafeSMikulas Patocka 		wc->memory_map = NULL;
27948debafeSMikulas Patocka 		r = da;
28048debafeSMikulas Patocka 		goto err2;
28148debafeSMikulas Patocka 	}
28248debafeSMikulas Patocka 	if (!pfn_t_has_page(pfn)) {
28348debafeSMikulas Patocka 		wc->memory_map = NULL;
28448debafeSMikulas Patocka 		r = -EOPNOTSUPP;
28548debafeSMikulas Patocka 		goto err2;
28648debafeSMikulas Patocka 	}
28748debafeSMikulas Patocka 	if (da != p) {
28848debafeSMikulas Patocka 		long i;
28948debafeSMikulas Patocka 		wc->memory_map = NULL;
29050a7d3baSKees Cook 		pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
29148debafeSMikulas Patocka 		if (!pages) {
29248debafeSMikulas Patocka 			r = -ENOMEM;
29348debafeSMikulas Patocka 			goto err2;
29448debafeSMikulas Patocka 		}
29548debafeSMikulas Patocka 		i = 0;
29648debafeSMikulas Patocka 		do {
29748debafeSMikulas Patocka 			long daa;
298f9e040efSMikulas Patocka 			daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i, p - i,
299f742267aSHuaisheng Ye 						NULL, &pfn);
30048debafeSMikulas Patocka 			if (daa <= 0) {
30148debafeSMikulas Patocka 				r = daa ? daa : -EINVAL;
30248debafeSMikulas Patocka 				goto err3;
30348debafeSMikulas Patocka 			}
30448debafeSMikulas Patocka 			if (!pfn_t_has_page(pfn)) {
30548debafeSMikulas Patocka 				r = -EOPNOTSUPP;
30648debafeSMikulas Patocka 				goto err3;
30748debafeSMikulas Patocka 			}
30848debafeSMikulas Patocka 			while (daa-- && i < p) {
30948debafeSMikulas Patocka 				pages[i++] = pfn_t_to_page(pfn);
31048debafeSMikulas Patocka 				pfn.val++;
311d35bd764SMikulas Patocka 				if (!(i & 15))
312d35bd764SMikulas Patocka 					cond_resched();
31348debafeSMikulas Patocka 			}
31448debafeSMikulas Patocka 		} while (i < p);
31548debafeSMikulas Patocka 		wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
31648debafeSMikulas Patocka 		if (!wc->memory_map) {
31748debafeSMikulas Patocka 			r = -ENOMEM;
31848debafeSMikulas Patocka 			goto err3;
31948debafeSMikulas Patocka 		}
32048debafeSMikulas Patocka 		kvfree(pages);
32148debafeSMikulas Patocka 		wc->memory_vmapped = true;
32248debafeSMikulas Patocka 	}
32348debafeSMikulas Patocka 
32448debafeSMikulas Patocka 	dax_read_unlock(id);
325d284f824SMikulas Patocka 
326d284f824SMikulas Patocka 	wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
327d284f824SMikulas Patocka 	wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
328d284f824SMikulas Patocka 
32948debafeSMikulas Patocka 	return 0;
33048debafeSMikulas Patocka err3:
33148debafeSMikulas Patocka 	kvfree(pages);
33248debafeSMikulas Patocka err2:
33348debafeSMikulas Patocka 	dax_read_unlock(id);
33448debafeSMikulas Patocka err1:
33548debafeSMikulas Patocka 	return r;
33648debafeSMikulas Patocka }
33748debafeSMikulas Patocka #else
33848debafeSMikulas Patocka static int persistent_memory_claim(struct dm_writecache *wc)
33948debafeSMikulas Patocka {
340857c4c0aSMike Snitzer 	return -EOPNOTSUPP;
34148debafeSMikulas Patocka }
34248debafeSMikulas Patocka #endif
34348debafeSMikulas Patocka 
34448debafeSMikulas Patocka static void persistent_memory_release(struct dm_writecache *wc)
34548debafeSMikulas Patocka {
34648debafeSMikulas Patocka 	if (wc->memory_vmapped)
347d284f824SMikulas Patocka 		vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
34848debafeSMikulas Patocka }
34948debafeSMikulas Patocka 
35048debafeSMikulas Patocka static struct page *persistent_memory_page(void *addr)
35148debafeSMikulas Patocka {
35248debafeSMikulas Patocka 	if (is_vmalloc_addr(addr))
35348debafeSMikulas Patocka 		return vmalloc_to_page(addr);
35448debafeSMikulas Patocka 	else
35548debafeSMikulas Patocka 		return virt_to_page(addr);
35648debafeSMikulas Patocka }
35748debafeSMikulas Patocka 
35848debafeSMikulas Patocka static unsigned persistent_memory_page_offset(void *addr)
35948debafeSMikulas Patocka {
36048debafeSMikulas Patocka 	return (unsigned long)addr & (PAGE_SIZE - 1);
36148debafeSMikulas Patocka }
36248debafeSMikulas Patocka 
36348debafeSMikulas Patocka static void persistent_memory_flush_cache(void *ptr, size_t size)
36448debafeSMikulas Patocka {
36548debafeSMikulas Patocka 	if (is_vmalloc_addr(ptr))
36648debafeSMikulas Patocka 		flush_kernel_vmap_range(ptr, size);
36748debafeSMikulas Patocka }
36848debafeSMikulas Patocka 
36948debafeSMikulas Patocka static void persistent_memory_invalidate_cache(void *ptr, size_t size)
37048debafeSMikulas Patocka {
37148debafeSMikulas Patocka 	if (is_vmalloc_addr(ptr))
37248debafeSMikulas Patocka 		invalidate_kernel_vmap_range(ptr, size);
37348debafeSMikulas Patocka }
37448debafeSMikulas Patocka 
37548debafeSMikulas Patocka static struct wc_memory_superblock *sb(struct dm_writecache *wc)
37648debafeSMikulas Patocka {
37748debafeSMikulas Patocka 	return wc->memory_map;
37848debafeSMikulas Patocka }
37948debafeSMikulas Patocka 
38048debafeSMikulas Patocka static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
38148debafeSMikulas Patocka {
38248debafeSMikulas Patocka 	return &sb(wc)->entries[e->index];
38348debafeSMikulas Patocka }
38448debafeSMikulas Patocka 
38548debafeSMikulas Patocka static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
38648debafeSMikulas Patocka {
38748debafeSMikulas Patocka 	return (char *)wc->block_start + (e->index << wc->block_size_bits);
38848debafeSMikulas Patocka }
38948debafeSMikulas Patocka 
39048debafeSMikulas Patocka static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
39148debafeSMikulas Patocka {
392d284f824SMikulas Patocka 	return wc->start_sector + wc->metadata_sectors +
39348debafeSMikulas Patocka 		((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
39448debafeSMikulas Patocka }
39548debafeSMikulas Patocka 
39648debafeSMikulas Patocka static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
39748debafeSMikulas Patocka {
39848debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
39948debafeSMikulas Patocka 	return e->original_sector;
40048debafeSMikulas Patocka #else
40148debafeSMikulas Patocka 	return le64_to_cpu(memory_entry(wc, e)->original_sector);
40248debafeSMikulas Patocka #endif
40348debafeSMikulas Patocka }
40448debafeSMikulas Patocka 
40548debafeSMikulas Patocka static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
40648debafeSMikulas Patocka {
40748debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
40848debafeSMikulas Patocka 	return e->seq_count;
40948debafeSMikulas Patocka #else
41048debafeSMikulas Patocka 	return le64_to_cpu(memory_entry(wc, e)->seq_count);
41148debafeSMikulas Patocka #endif
41248debafeSMikulas Patocka }
41348debafeSMikulas Patocka 
41448debafeSMikulas Patocka static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
41548debafeSMikulas Patocka {
41648debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
41748debafeSMikulas Patocka 	e->seq_count = -1;
41848debafeSMikulas Patocka #endif
41948debafeSMikulas Patocka 	pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
42048debafeSMikulas Patocka }
42148debafeSMikulas Patocka 
42248debafeSMikulas Patocka static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
42348debafeSMikulas Patocka 					    uint64_t original_sector, uint64_t seq_count)
42448debafeSMikulas Patocka {
42548debafeSMikulas Patocka 	struct wc_memory_entry me;
42648debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
42748debafeSMikulas Patocka 	e->original_sector = original_sector;
42848debafeSMikulas Patocka 	e->seq_count = seq_count;
42948debafeSMikulas Patocka #endif
43048debafeSMikulas Patocka 	me.original_sector = cpu_to_le64(original_sector);
43148debafeSMikulas Patocka 	me.seq_count = cpu_to_le64(seq_count);
43248debafeSMikulas Patocka 	pmem_assign(*memory_entry(wc, e), me);
43348debafeSMikulas Patocka }
43448debafeSMikulas Patocka 
43548debafeSMikulas Patocka #define writecache_error(wc, err, msg, arg...)				\
43648debafeSMikulas Patocka do {									\
43748debafeSMikulas Patocka 	if (!cmpxchg(&(wc)->error, 0, err))				\
43848debafeSMikulas Patocka 		DMERR(msg, ##arg);					\
43948debafeSMikulas Patocka 	wake_up(&(wc)->freelist_wait);					\
44048debafeSMikulas Patocka } while (0)
44148debafeSMikulas Patocka 
44248debafeSMikulas Patocka #define writecache_has_error(wc)	(unlikely(READ_ONCE((wc)->error)))
44348debafeSMikulas Patocka 
44448debafeSMikulas Patocka static void writecache_flush_all_metadata(struct dm_writecache *wc)
44548debafeSMikulas Patocka {
44648debafeSMikulas Patocka 	if (!WC_MODE_PMEM(wc))
44748debafeSMikulas Patocka 		memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
44848debafeSMikulas Patocka }
44948debafeSMikulas Patocka 
45048debafeSMikulas Patocka static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
45148debafeSMikulas Patocka {
45248debafeSMikulas Patocka 	if (!WC_MODE_PMEM(wc))
45348debafeSMikulas Patocka 		__set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
45448debafeSMikulas Patocka 			  wc->dirty_bitmap);
45548debafeSMikulas Patocka }
45648debafeSMikulas Patocka 
45748debafeSMikulas Patocka static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
45848debafeSMikulas Patocka 
45948debafeSMikulas Patocka struct io_notify {
46048debafeSMikulas Patocka 	struct dm_writecache *wc;
46148debafeSMikulas Patocka 	struct completion c;
46248debafeSMikulas Patocka 	atomic_t count;
46348debafeSMikulas Patocka };
46448debafeSMikulas Patocka 
46548debafeSMikulas Patocka static void writecache_notify_io(unsigned long error, void *context)
46648debafeSMikulas Patocka {
46748debafeSMikulas Patocka 	struct io_notify *endio = context;
46848debafeSMikulas Patocka 
46948debafeSMikulas Patocka 	if (unlikely(error != 0))
47048debafeSMikulas Patocka 		writecache_error(endio->wc, -EIO, "error writing metadata");
47148debafeSMikulas Patocka 	BUG_ON(atomic_read(&endio->count) <= 0);
47248debafeSMikulas Patocka 	if (atomic_dec_and_test(&endio->count))
47348debafeSMikulas Patocka 		complete(&endio->c);
47448debafeSMikulas Patocka }
47548debafeSMikulas Patocka 
476aa950920SMikulas Patocka static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
477aa950920SMikulas Patocka {
478aa950920SMikulas Patocka 	wait_event(wc->bio_in_progress_wait[direction],
479aa950920SMikulas Patocka 		   !atomic_read(&wc->bio_in_progress[direction]));
480aa950920SMikulas Patocka }
481aa950920SMikulas Patocka 
482aa950920SMikulas Patocka static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
48348debafeSMikulas Patocka {
48448debafeSMikulas Patocka 	struct dm_io_region region;
48548debafeSMikulas Patocka 	struct dm_io_request req;
48648debafeSMikulas Patocka 	struct io_notify endio = {
48748debafeSMikulas Patocka 		wc,
48848debafeSMikulas Patocka 		COMPLETION_INITIALIZER_ONSTACK(endio.c),
48948debafeSMikulas Patocka 		ATOMIC_INIT(1),
49048debafeSMikulas Patocka 	};
4911e1132eaSMikulas Patocka 	unsigned bitmap_bits = wc->dirty_bitmap_size * 8;
49248debafeSMikulas Patocka 	unsigned i = 0;
49348debafeSMikulas Patocka 
49448debafeSMikulas Patocka 	while (1) {
49548debafeSMikulas Patocka 		unsigned j;
49648debafeSMikulas Patocka 		i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i);
49748debafeSMikulas Patocka 		if (unlikely(i == bitmap_bits))
49848debafeSMikulas Patocka 			break;
49948debafeSMikulas Patocka 		j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i);
50048debafeSMikulas Patocka 
50148debafeSMikulas Patocka 		region.bdev = wc->ssd_dev->bdev;
50248debafeSMikulas Patocka 		region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
50348debafeSMikulas Patocka 		region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
50448debafeSMikulas Patocka 
50548debafeSMikulas Patocka 		if (unlikely(region.sector >= wc->metadata_sectors))
50648debafeSMikulas Patocka 			break;
50748debafeSMikulas Patocka 		if (unlikely(region.sector + region.count > wc->metadata_sectors))
50848debafeSMikulas Patocka 			region.count = wc->metadata_sectors - region.sector;
50948debafeSMikulas Patocka 
510d284f824SMikulas Patocka 		region.sector += wc->start_sector;
51148debafeSMikulas Patocka 		atomic_inc(&endio.count);
51248debafeSMikulas Patocka 		req.bi_op = REQ_OP_WRITE;
51348debafeSMikulas Patocka 		req.bi_op_flags = REQ_SYNC;
51448debafeSMikulas Patocka 		req.mem.type = DM_IO_VMA;
51548debafeSMikulas Patocka 		req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
51648debafeSMikulas Patocka 		req.client = wc->dm_io;
51748debafeSMikulas Patocka 		req.notify.fn = writecache_notify_io;
51848debafeSMikulas Patocka 		req.notify.context = &endio;
51948debafeSMikulas Patocka 
52048debafeSMikulas Patocka 		/* writing via async dm-io (implied by notify.fn above) won't return an error */
52148debafeSMikulas Patocka 	        (void) dm_io(&req, 1, &region, NULL);
52248debafeSMikulas Patocka 		i = j;
52348debafeSMikulas Patocka 	}
52448debafeSMikulas Patocka 
52548debafeSMikulas Patocka 	writecache_notify_io(0, &endio);
52648debafeSMikulas Patocka 	wait_for_completion_io(&endio.c);
52748debafeSMikulas Patocka 
528aa950920SMikulas Patocka 	if (wait_for_ios)
529aa950920SMikulas Patocka 		writecache_wait_for_ios(wc, WRITE);
530aa950920SMikulas Patocka 
53148debafeSMikulas Patocka 	writecache_disk_flush(wc, wc->ssd_dev);
53248debafeSMikulas Patocka 
53348debafeSMikulas Patocka 	memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
53448debafeSMikulas Patocka }
53548debafeSMikulas Patocka 
536dc8a01aeSMikulas Patocka static void ssd_commit_superblock(struct dm_writecache *wc)
537dc8a01aeSMikulas Patocka {
538dc8a01aeSMikulas Patocka 	int r;
539dc8a01aeSMikulas Patocka 	struct dm_io_region region;
540dc8a01aeSMikulas Patocka 	struct dm_io_request req;
541dc8a01aeSMikulas Patocka 
542dc8a01aeSMikulas Patocka 	region.bdev = wc->ssd_dev->bdev;
543dc8a01aeSMikulas Patocka 	region.sector = 0;
544867de40cSMikulas Patocka 	region.count = max(4096U, wc->block_size) >> SECTOR_SHIFT;
545867de40cSMikulas Patocka 
546867de40cSMikulas Patocka 	if (unlikely(region.sector + region.count > wc->metadata_sectors))
547867de40cSMikulas Patocka 		region.count = wc->metadata_sectors - region.sector;
548867de40cSMikulas Patocka 
549dc8a01aeSMikulas Patocka 	region.sector += wc->start_sector;
550dc8a01aeSMikulas Patocka 
551dc8a01aeSMikulas Patocka 	req.bi_op = REQ_OP_WRITE;
552dc8a01aeSMikulas Patocka 	req.bi_op_flags = REQ_SYNC | REQ_FUA;
553dc8a01aeSMikulas Patocka 	req.mem.type = DM_IO_VMA;
554dc8a01aeSMikulas Patocka 	req.mem.ptr.vma = (char *)wc->memory_map;
555dc8a01aeSMikulas Patocka 	req.client = wc->dm_io;
556dc8a01aeSMikulas Patocka 	req.notify.fn = NULL;
557dc8a01aeSMikulas Patocka 	req.notify.context = NULL;
558dc8a01aeSMikulas Patocka 
559dc8a01aeSMikulas Patocka 	r = dm_io(&req, 1, &region, NULL);
560dc8a01aeSMikulas Patocka 	if (unlikely(r))
561dc8a01aeSMikulas Patocka 		writecache_error(wc, r, "error writing superblock");
562dc8a01aeSMikulas Patocka }
563dc8a01aeSMikulas Patocka 
564aa950920SMikulas Patocka static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
56548debafeSMikulas Patocka {
56648debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
5673e79f082SAneesh Kumar K.V 		pmem_wmb();
56848debafeSMikulas Patocka 	else
569aa950920SMikulas Patocka 		ssd_commit_flushed(wc, wait_for_ios);
57048debafeSMikulas Patocka }
57148debafeSMikulas Patocka 
57248debafeSMikulas Patocka static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
57348debafeSMikulas Patocka {
57448debafeSMikulas Patocka 	int r;
57548debafeSMikulas Patocka 	struct dm_io_region region;
57648debafeSMikulas Patocka 	struct dm_io_request req;
57748debafeSMikulas Patocka 
57848debafeSMikulas Patocka 	region.bdev = dev->bdev;
57948debafeSMikulas Patocka 	region.sector = 0;
58048debafeSMikulas Patocka 	region.count = 0;
58148debafeSMikulas Patocka 	req.bi_op = REQ_OP_WRITE;
58248debafeSMikulas Patocka 	req.bi_op_flags = REQ_PREFLUSH;
58348debafeSMikulas Patocka 	req.mem.type = DM_IO_KMEM;
58448debafeSMikulas Patocka 	req.mem.ptr.addr = NULL;
58548debafeSMikulas Patocka 	req.client = wc->dm_io;
58648debafeSMikulas Patocka 	req.notify.fn = NULL;
58748debafeSMikulas Patocka 
58848debafeSMikulas Patocka 	r = dm_io(&req, 1, &region, NULL);
58948debafeSMikulas Patocka 	if (unlikely(r))
59048debafeSMikulas Patocka 		writecache_error(wc, r, "error flushing metadata: %d", r);
59148debafeSMikulas Patocka }
59248debafeSMikulas Patocka 
59348debafeSMikulas Patocka #define WFE_RETURN_FOLLOWING	1
59448debafeSMikulas Patocka #define WFE_LOWEST_SEQ		2
59548debafeSMikulas Patocka 
59648debafeSMikulas Patocka static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
59748debafeSMikulas Patocka 					      uint64_t block, int flags)
59848debafeSMikulas Patocka {
59948debafeSMikulas Patocka 	struct wc_entry *e;
60048debafeSMikulas Patocka 	struct rb_node *node = wc->tree.rb_node;
60148debafeSMikulas Patocka 
60248debafeSMikulas Patocka 	if (unlikely(!node))
60348debafeSMikulas Patocka 		return NULL;
60448debafeSMikulas Patocka 
60548debafeSMikulas Patocka 	while (1) {
60648debafeSMikulas Patocka 		e = container_of(node, struct wc_entry, rb_node);
60748debafeSMikulas Patocka 		if (read_original_sector(wc, e) == block)
60848debafeSMikulas Patocka 			break;
609f8011d33SMikulas Patocka 
61048debafeSMikulas Patocka 		node = (read_original_sector(wc, e) >= block ?
61148debafeSMikulas Patocka 			e->rb_node.rb_left : e->rb_node.rb_right);
61248debafeSMikulas Patocka 		if (unlikely(!node)) {
613f8011d33SMikulas Patocka 			if (!(flags & WFE_RETURN_FOLLOWING))
61448debafeSMikulas Patocka 				return NULL;
61548debafeSMikulas Patocka 			if (read_original_sector(wc, e) >= block) {
616f8011d33SMikulas Patocka 				return e;
61748debafeSMikulas Patocka 			} else {
61848debafeSMikulas Patocka 				node = rb_next(&e->rb_node);
619f8011d33SMikulas Patocka 				if (unlikely(!node))
62048debafeSMikulas Patocka 					return NULL;
62148debafeSMikulas Patocka 				e = container_of(node, struct wc_entry, rb_node);
622f8011d33SMikulas Patocka 				return e;
62348debafeSMikulas Patocka 			}
62448debafeSMikulas Patocka 		}
62548debafeSMikulas Patocka 	}
62648debafeSMikulas Patocka 
62748debafeSMikulas Patocka 	while (1) {
62848debafeSMikulas Patocka 		struct wc_entry *e2;
62948debafeSMikulas Patocka 		if (flags & WFE_LOWEST_SEQ)
63048debafeSMikulas Patocka 			node = rb_prev(&e->rb_node);
63148debafeSMikulas Patocka 		else
63248debafeSMikulas Patocka 			node = rb_next(&e->rb_node);
63384420b1eSHuaisheng Ye 		if (unlikely(!node))
63448debafeSMikulas Patocka 			return e;
63548debafeSMikulas Patocka 		e2 = container_of(node, struct wc_entry, rb_node);
63648debafeSMikulas Patocka 		if (read_original_sector(wc, e2) != block)
63748debafeSMikulas Patocka 			return e;
63848debafeSMikulas Patocka 		e = e2;
63948debafeSMikulas Patocka 	}
64048debafeSMikulas Patocka }
64148debafeSMikulas Patocka 
64248debafeSMikulas Patocka static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
64348debafeSMikulas Patocka {
64448debafeSMikulas Patocka 	struct wc_entry *e;
64548debafeSMikulas Patocka 	struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
64648debafeSMikulas Patocka 
64748debafeSMikulas Patocka 	while (*node) {
64848debafeSMikulas Patocka 		e = container_of(*node, struct wc_entry, rb_node);
64948debafeSMikulas Patocka 		parent = &e->rb_node;
65048debafeSMikulas Patocka 		if (read_original_sector(wc, e) > read_original_sector(wc, ins))
65148debafeSMikulas Patocka 			node = &parent->rb_left;
65248debafeSMikulas Patocka 		else
65348debafeSMikulas Patocka 			node = &parent->rb_right;
65448debafeSMikulas Patocka 	}
65548debafeSMikulas Patocka 	rb_link_node(&ins->rb_node, parent, node);
65648debafeSMikulas Patocka 	rb_insert_color(&ins->rb_node, &wc->tree);
65748debafeSMikulas Patocka 	list_add(&ins->lru, &wc->lru);
6583923d485SMikulas Patocka 	ins->age = jiffies;
65948debafeSMikulas Patocka }
66048debafeSMikulas Patocka 
66148debafeSMikulas Patocka static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
66248debafeSMikulas Patocka {
66348debafeSMikulas Patocka 	list_del(&e->lru);
66448debafeSMikulas Patocka 	rb_erase(&e->rb_node, &wc->tree);
66548debafeSMikulas Patocka }
66648debafeSMikulas Patocka 
66748debafeSMikulas Patocka static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
66848debafeSMikulas Patocka {
66948debafeSMikulas Patocka 	if (WC_MODE_SORT_FREELIST(wc)) {
67048debafeSMikulas Patocka 		struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
67148debafeSMikulas Patocka 		if (unlikely(!*node))
67248debafeSMikulas Patocka 			wc->current_free = e;
67348debafeSMikulas Patocka 		while (*node) {
67448debafeSMikulas Patocka 			parent = *node;
67548debafeSMikulas Patocka 			if (&e->rb_node < *node)
67648debafeSMikulas Patocka 				node = &parent->rb_left;
67748debafeSMikulas Patocka 			else
67848debafeSMikulas Patocka 				node = &parent->rb_right;
67948debafeSMikulas Patocka 		}
68048debafeSMikulas Patocka 		rb_link_node(&e->rb_node, parent, node);
68148debafeSMikulas Patocka 		rb_insert_color(&e->rb_node, &wc->freetree);
68248debafeSMikulas Patocka 	} else {
68348debafeSMikulas Patocka 		list_add_tail(&e->lru, &wc->freelist);
68448debafeSMikulas Patocka 	}
68548debafeSMikulas Patocka 	wc->freelist_size++;
68648debafeSMikulas Patocka }
68748debafeSMikulas Patocka 
68841c526c5SMikulas Patocka static inline void writecache_verify_watermark(struct dm_writecache *wc)
68941c526c5SMikulas Patocka {
69041c526c5SMikulas Patocka 	if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
69141c526c5SMikulas Patocka 		queue_work(wc->writeback_wq, &wc->writeback_work);
69241c526c5SMikulas Patocka }
69341c526c5SMikulas Patocka 
6943923d485SMikulas Patocka static void writecache_max_age_timer(struct timer_list *t)
6953923d485SMikulas Patocka {
6963923d485SMikulas Patocka 	struct dm_writecache *wc = from_timer(wc, t, max_age_timer);
6973923d485SMikulas Patocka 
6983923d485SMikulas Patocka 	if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) {
6993923d485SMikulas Patocka 		queue_work(wc->writeback_wq, &wc->writeback_work);
7003923d485SMikulas Patocka 		mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
7013923d485SMikulas Patocka 	}
7023923d485SMikulas Patocka }
7033923d485SMikulas Patocka 
704dcd19507SMikulas Patocka static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector)
70548debafeSMikulas Patocka {
70648debafeSMikulas Patocka 	struct wc_entry *e;
70748debafeSMikulas Patocka 
70848debafeSMikulas Patocka 	if (WC_MODE_SORT_FREELIST(wc)) {
70948debafeSMikulas Patocka 		struct rb_node *next;
71048debafeSMikulas Patocka 		if (unlikely(!wc->current_free))
71148debafeSMikulas Patocka 			return NULL;
71248debafeSMikulas Patocka 		e = wc->current_free;
713dcd19507SMikulas Patocka 		if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
714dcd19507SMikulas Patocka 			return NULL;
71548debafeSMikulas Patocka 		next = rb_next(&e->rb_node);
71648debafeSMikulas Patocka 		rb_erase(&e->rb_node, &wc->freetree);
71748debafeSMikulas Patocka 		if (unlikely(!next))
71848debafeSMikulas Patocka 			next = rb_first(&wc->freetree);
71948debafeSMikulas Patocka 		wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
72048debafeSMikulas Patocka 	} else {
72148debafeSMikulas Patocka 		if (unlikely(list_empty(&wc->freelist)))
72248debafeSMikulas Patocka 			return NULL;
72348debafeSMikulas Patocka 		e = container_of(wc->freelist.next, struct wc_entry, lru);
724dcd19507SMikulas Patocka 		if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
725dcd19507SMikulas Patocka 			return NULL;
72648debafeSMikulas Patocka 		list_del(&e->lru);
72748debafeSMikulas Patocka 	}
72848debafeSMikulas Patocka 	wc->freelist_size--;
72941c526c5SMikulas Patocka 
73041c526c5SMikulas Patocka 	writecache_verify_watermark(wc);
73148debafeSMikulas Patocka 
73248debafeSMikulas Patocka 	return e;
73348debafeSMikulas Patocka }
73448debafeSMikulas Patocka 
73548debafeSMikulas Patocka static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
73648debafeSMikulas Patocka {
73748debafeSMikulas Patocka 	writecache_unlink(wc, e);
73848debafeSMikulas Patocka 	writecache_add_to_freelist(wc, e);
73948debafeSMikulas Patocka 	clear_seq_count(wc, e);
74048debafeSMikulas Patocka 	writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
74148debafeSMikulas Patocka 	if (unlikely(waitqueue_active(&wc->freelist_wait)))
74248debafeSMikulas Patocka 		wake_up(&wc->freelist_wait);
74348debafeSMikulas Patocka }
74448debafeSMikulas Patocka 
74548debafeSMikulas Patocka static void writecache_wait_on_freelist(struct dm_writecache *wc)
74648debafeSMikulas Patocka {
74748debafeSMikulas Patocka 	DEFINE_WAIT(wait);
74848debafeSMikulas Patocka 
74948debafeSMikulas Patocka 	prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
75048debafeSMikulas Patocka 	wc_unlock(wc);
75148debafeSMikulas Patocka 	io_schedule();
75248debafeSMikulas Patocka 	finish_wait(&wc->freelist_wait, &wait);
75348debafeSMikulas Patocka 	wc_lock(wc);
75448debafeSMikulas Patocka }
75548debafeSMikulas Patocka 
75648debafeSMikulas Patocka static void writecache_poison_lists(struct dm_writecache *wc)
75748debafeSMikulas Patocka {
75848debafeSMikulas Patocka 	/*
75948debafeSMikulas Patocka 	 * Catch incorrect access to these values while the device is suspended.
76048debafeSMikulas Patocka 	 */
76148debafeSMikulas Patocka 	memset(&wc->tree, -1, sizeof wc->tree);
76248debafeSMikulas Patocka 	wc->lru.next = LIST_POISON1;
76348debafeSMikulas Patocka 	wc->lru.prev = LIST_POISON2;
76448debafeSMikulas Patocka 	wc->freelist.next = LIST_POISON1;
76548debafeSMikulas Patocka 	wc->freelist.prev = LIST_POISON2;
76648debafeSMikulas Patocka }
76748debafeSMikulas Patocka 
76848debafeSMikulas Patocka static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
76948debafeSMikulas Patocka {
77048debafeSMikulas Patocka 	writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
77148debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
77248debafeSMikulas Patocka 		writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
77348debafeSMikulas Patocka }
77448debafeSMikulas Patocka 
77548debafeSMikulas Patocka static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
77648debafeSMikulas Patocka {
77748debafeSMikulas Patocka 	return read_seq_count(wc, e) < wc->seq_count;
77848debafeSMikulas Patocka }
77948debafeSMikulas Patocka 
78048debafeSMikulas Patocka static void writecache_flush(struct dm_writecache *wc)
78148debafeSMikulas Patocka {
78248debafeSMikulas Patocka 	struct wc_entry *e, *e2;
78348debafeSMikulas Patocka 	bool need_flush_after_free;
78448debafeSMikulas Patocka 
78548debafeSMikulas Patocka 	wc->uncommitted_blocks = 0;
78648debafeSMikulas Patocka 	del_timer(&wc->autocommit_timer);
78748debafeSMikulas Patocka 
78848debafeSMikulas Patocka 	if (list_empty(&wc->lru))
78948debafeSMikulas Patocka 		return;
79048debafeSMikulas Patocka 
79148debafeSMikulas Patocka 	e = container_of(wc->lru.next, struct wc_entry, lru);
79248debafeSMikulas Patocka 	if (writecache_entry_is_committed(wc, e)) {
79348debafeSMikulas Patocka 		if (wc->overwrote_committed) {
79448debafeSMikulas Patocka 			writecache_wait_for_ios(wc, WRITE);
79548debafeSMikulas Patocka 			writecache_disk_flush(wc, wc->ssd_dev);
79648debafeSMikulas Patocka 			wc->overwrote_committed = false;
79748debafeSMikulas Patocka 		}
79848debafeSMikulas Patocka 		return;
79948debafeSMikulas Patocka 	}
80048debafeSMikulas Patocka 	while (1) {
80148debafeSMikulas Patocka 		writecache_flush_entry(wc, e);
80248debafeSMikulas Patocka 		if (unlikely(e->lru.next == &wc->lru))
80348debafeSMikulas Patocka 			break;
80448debafeSMikulas Patocka 		e2 = container_of(e->lru.next, struct wc_entry, lru);
80548debafeSMikulas Patocka 		if (writecache_entry_is_committed(wc, e2))
80648debafeSMikulas Patocka 			break;
80748debafeSMikulas Patocka 		e = e2;
80848debafeSMikulas Patocka 		cond_resched();
80948debafeSMikulas Patocka 	}
810aa950920SMikulas Patocka 	writecache_commit_flushed(wc, true);
81148debafeSMikulas Patocka 
81248debafeSMikulas Patocka 	wc->seq_count++;
81348debafeSMikulas Patocka 	pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
814dc8a01aeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
815aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
816dc8a01aeSMikulas Patocka 	else
817dc8a01aeSMikulas Patocka 		ssd_commit_superblock(wc);
81848debafeSMikulas Patocka 
81948debafeSMikulas Patocka 	wc->overwrote_committed = false;
82048debafeSMikulas Patocka 
82148debafeSMikulas Patocka 	need_flush_after_free = false;
82248debafeSMikulas Patocka 	while (1) {
82348debafeSMikulas Patocka 		/* Free another committed entry with lower seq-count */
82448debafeSMikulas Patocka 		struct rb_node *rb_node = rb_prev(&e->rb_node);
82548debafeSMikulas Patocka 
82648debafeSMikulas Patocka 		if (rb_node) {
82748debafeSMikulas Patocka 			e2 = container_of(rb_node, struct wc_entry, rb_node);
82848debafeSMikulas Patocka 			if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
82948debafeSMikulas Patocka 			    likely(!e2->write_in_progress)) {
83048debafeSMikulas Patocka 				writecache_free_entry(wc, e2);
83148debafeSMikulas Patocka 				need_flush_after_free = true;
83248debafeSMikulas Patocka 			}
83348debafeSMikulas Patocka 		}
83448debafeSMikulas Patocka 		if (unlikely(e->lru.prev == &wc->lru))
83548debafeSMikulas Patocka 			break;
83648debafeSMikulas Patocka 		e = container_of(e->lru.prev, struct wc_entry, lru);
83748debafeSMikulas Patocka 		cond_resched();
83848debafeSMikulas Patocka 	}
83948debafeSMikulas Patocka 
84048debafeSMikulas Patocka 	if (need_flush_after_free)
841aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
84248debafeSMikulas Patocka }
84348debafeSMikulas Patocka 
84448debafeSMikulas Patocka static void writecache_flush_work(struct work_struct *work)
84548debafeSMikulas Patocka {
84648debafeSMikulas Patocka 	struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
84748debafeSMikulas Patocka 
84848debafeSMikulas Patocka 	wc_lock(wc);
84948debafeSMikulas Patocka 	writecache_flush(wc);
85048debafeSMikulas Patocka 	wc_unlock(wc);
85148debafeSMikulas Patocka }
85248debafeSMikulas Patocka 
85348debafeSMikulas Patocka static void writecache_autocommit_timer(struct timer_list *t)
85448debafeSMikulas Patocka {
85548debafeSMikulas Patocka 	struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
85648debafeSMikulas Patocka 	if (!writecache_has_error(wc))
85748debafeSMikulas Patocka 		queue_work(wc->writeback_wq, &wc->flush_work);
85848debafeSMikulas Patocka }
85948debafeSMikulas Patocka 
86048debafeSMikulas Patocka static void writecache_schedule_autocommit(struct dm_writecache *wc)
86148debafeSMikulas Patocka {
86248debafeSMikulas Patocka 	if (!timer_pending(&wc->autocommit_timer))
86348debafeSMikulas Patocka 		mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
86448debafeSMikulas Patocka }
86548debafeSMikulas Patocka 
86648debafeSMikulas Patocka static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
86748debafeSMikulas Patocka {
86848debafeSMikulas Patocka 	struct wc_entry *e;
86948debafeSMikulas Patocka 	bool discarded_something = false;
87048debafeSMikulas Patocka 
87148debafeSMikulas Patocka 	e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
87248debafeSMikulas Patocka 	if (unlikely(!e))
87348debafeSMikulas Patocka 		return;
87448debafeSMikulas Patocka 
87548debafeSMikulas Patocka 	while (read_original_sector(wc, e) < end) {
87648debafeSMikulas Patocka 		struct rb_node *node = rb_next(&e->rb_node);
87748debafeSMikulas Patocka 
87848debafeSMikulas Patocka 		if (likely(!e->write_in_progress)) {
87948debafeSMikulas Patocka 			if (!discarded_something) {
880a143e172SHuaisheng Ye 				if (!WC_MODE_PMEM(wc)) {
88148debafeSMikulas Patocka 					writecache_wait_for_ios(wc, READ);
88248debafeSMikulas Patocka 					writecache_wait_for_ios(wc, WRITE);
883a143e172SHuaisheng Ye 				}
88448debafeSMikulas Patocka 				discarded_something = true;
88548debafeSMikulas Patocka 			}
88639495b12SHuaisheng Ye 			if (!writecache_entry_is_committed(wc, e))
88739495b12SHuaisheng Ye 				wc->uncommitted_blocks--;
88848debafeSMikulas Patocka 			writecache_free_entry(wc, e);
88948debafeSMikulas Patocka 		}
89048debafeSMikulas Patocka 
89184420b1eSHuaisheng Ye 		if (unlikely(!node))
89248debafeSMikulas Patocka 			break;
89348debafeSMikulas Patocka 
89448debafeSMikulas Patocka 		e = container_of(node, struct wc_entry, rb_node);
89548debafeSMikulas Patocka 	}
89648debafeSMikulas Patocka 
89748debafeSMikulas Patocka 	if (discarded_something)
898aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
89948debafeSMikulas Patocka }
90048debafeSMikulas Patocka 
90148debafeSMikulas Patocka static bool writecache_wait_for_writeback(struct dm_writecache *wc)
90248debafeSMikulas Patocka {
90348debafeSMikulas Patocka 	if (wc->writeback_size) {
90448debafeSMikulas Patocka 		writecache_wait_on_freelist(wc);
90548debafeSMikulas Patocka 		return true;
90648debafeSMikulas Patocka 	}
90748debafeSMikulas Patocka 	return false;
90848debafeSMikulas Patocka }
90948debafeSMikulas Patocka 
91048debafeSMikulas Patocka static void writecache_suspend(struct dm_target *ti)
91148debafeSMikulas Patocka {
91248debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
91348debafeSMikulas Patocka 	bool flush_on_suspend;
91448debafeSMikulas Patocka 
91548debafeSMikulas Patocka 	del_timer_sync(&wc->autocommit_timer);
9163923d485SMikulas Patocka 	del_timer_sync(&wc->max_age_timer);
91748debafeSMikulas Patocka 
91848debafeSMikulas Patocka 	wc_lock(wc);
91948debafeSMikulas Patocka 	writecache_flush(wc);
92048debafeSMikulas Patocka 	flush_on_suspend = wc->flush_on_suspend;
92148debafeSMikulas Patocka 	if (flush_on_suspend) {
92248debafeSMikulas Patocka 		wc->flush_on_suspend = false;
92348debafeSMikulas Patocka 		wc->writeback_all++;
92448debafeSMikulas Patocka 		queue_work(wc->writeback_wq, &wc->writeback_work);
92548debafeSMikulas Patocka 	}
92648debafeSMikulas Patocka 	wc_unlock(wc);
92748debafeSMikulas Patocka 
928adc0daadSMikulas Patocka 	drain_workqueue(wc->writeback_wq);
92948debafeSMikulas Patocka 
93048debafeSMikulas Patocka 	wc_lock(wc);
93148debafeSMikulas Patocka 	if (flush_on_suspend)
93248debafeSMikulas Patocka 		wc->writeback_all--;
93348debafeSMikulas Patocka 	while (writecache_wait_for_writeback(wc));
93448debafeSMikulas Patocka 
93548debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
93648debafeSMikulas Patocka 		persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
93748debafeSMikulas Patocka 
93848debafeSMikulas Patocka 	writecache_poison_lists(wc);
93948debafeSMikulas Patocka 
94048debafeSMikulas Patocka 	wc_unlock(wc);
94148debafeSMikulas Patocka }
94248debafeSMikulas Patocka 
94348debafeSMikulas Patocka static int writecache_alloc_entries(struct dm_writecache *wc)
94448debafeSMikulas Patocka {
94548debafeSMikulas Patocka 	size_t b;
94648debafeSMikulas Patocka 
94748debafeSMikulas Patocka 	if (wc->entries)
94848debafeSMikulas Patocka 		return 0;
94950a7d3baSKees Cook 	wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
95048debafeSMikulas Patocka 	if (!wc->entries)
95148debafeSMikulas Patocka 		return -ENOMEM;
95248debafeSMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
95348debafeSMikulas Patocka 		struct wc_entry *e = &wc->entries[b];
95448debafeSMikulas Patocka 		e->index = b;
95548debafeSMikulas Patocka 		e->write_in_progress = false;
9561edaa447SMikulas Patocka 		cond_resched();
95748debafeSMikulas Patocka 	}
95848debafeSMikulas Patocka 
95948debafeSMikulas Patocka 	return 0;
96048debafeSMikulas Patocka }
96148debafeSMikulas Patocka 
96231b22120SMikulas Patocka static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors)
96331b22120SMikulas Patocka {
96431b22120SMikulas Patocka 	struct dm_io_region region;
96531b22120SMikulas Patocka 	struct dm_io_request req;
96631b22120SMikulas Patocka 
96731b22120SMikulas Patocka 	region.bdev = wc->ssd_dev->bdev;
96831b22120SMikulas Patocka 	region.sector = wc->start_sector;
96931b22120SMikulas Patocka 	region.count = n_sectors;
97031b22120SMikulas Patocka 	req.bi_op = REQ_OP_READ;
97131b22120SMikulas Patocka 	req.bi_op_flags = REQ_SYNC;
97231b22120SMikulas Patocka 	req.mem.type = DM_IO_VMA;
97331b22120SMikulas Patocka 	req.mem.ptr.vma = (char *)wc->memory_map;
97431b22120SMikulas Patocka 	req.client = wc->dm_io;
97531b22120SMikulas Patocka 	req.notify.fn = NULL;
97631b22120SMikulas Patocka 
97731b22120SMikulas Patocka 	return dm_io(&req, 1, &region, NULL);
97831b22120SMikulas Patocka }
97931b22120SMikulas Patocka 
98048debafeSMikulas Patocka static void writecache_resume(struct dm_target *ti)
98148debafeSMikulas Patocka {
98248debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
98348debafeSMikulas Patocka 	size_t b;
98448debafeSMikulas Patocka 	bool need_flush = false;
98548debafeSMikulas Patocka 	__le64 sb_seq_count;
98648debafeSMikulas Patocka 	int r;
98748debafeSMikulas Patocka 
98848debafeSMikulas Patocka 	wc_lock(wc);
98948debafeSMikulas Patocka 
990d9928ac5SMike Snitzer 	wc->data_device_sectors = bdev_nr_sectors(wc->dev->bdev);
9914134455fSMikulas Patocka 
99231b22120SMikulas Patocka 	if (WC_MODE_PMEM(wc)) {
99348debafeSMikulas Patocka 		persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
99431b22120SMikulas Patocka 	} else {
99531b22120SMikulas Patocka 		r = writecache_read_metadata(wc, wc->metadata_sectors);
99631b22120SMikulas Patocka 		if (r) {
99731b22120SMikulas Patocka 			size_t sb_entries_offset;
99831b22120SMikulas Patocka 			writecache_error(wc, r, "unable to read metadata: %d", r);
99931b22120SMikulas Patocka 			sb_entries_offset = offsetof(struct wc_memory_superblock, entries);
100031b22120SMikulas Patocka 			memset((char *)wc->memory_map + sb_entries_offset, -1,
100131b22120SMikulas Patocka 			       (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset);
100231b22120SMikulas Patocka 		}
100331b22120SMikulas Patocka 	}
100448debafeSMikulas Patocka 
100548debafeSMikulas Patocka 	wc->tree = RB_ROOT;
100648debafeSMikulas Patocka 	INIT_LIST_HEAD(&wc->lru);
100748debafeSMikulas Patocka 	if (WC_MODE_SORT_FREELIST(wc)) {
100848debafeSMikulas Patocka 		wc->freetree = RB_ROOT;
100948debafeSMikulas Patocka 		wc->current_free = NULL;
101048debafeSMikulas Patocka 	} else {
101148debafeSMikulas Patocka 		INIT_LIST_HEAD(&wc->freelist);
101248debafeSMikulas Patocka 	}
101348debafeSMikulas Patocka 	wc->freelist_size = 0;
101448debafeSMikulas Patocka 
1015ec6347bbSDan Williams 	r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count,
1016ec6347bbSDan Williams 			      sizeof(uint64_t));
101748debafeSMikulas Patocka 	if (r) {
101848debafeSMikulas Patocka 		writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
101948debafeSMikulas Patocka 		sb_seq_count = cpu_to_le64(0);
102048debafeSMikulas Patocka 	}
102148debafeSMikulas Patocka 	wc->seq_count = le64_to_cpu(sb_seq_count);
102248debafeSMikulas Patocka 
102348debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
102448debafeSMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
102548debafeSMikulas Patocka 		struct wc_entry *e = &wc->entries[b];
102648debafeSMikulas Patocka 		struct wc_memory_entry wme;
102748debafeSMikulas Patocka 		if (writecache_has_error(wc)) {
102848debafeSMikulas Patocka 			e->original_sector = -1;
102948debafeSMikulas Patocka 			e->seq_count = -1;
103048debafeSMikulas Patocka 			continue;
103148debafeSMikulas Patocka 		}
1032ec6347bbSDan Williams 		r = copy_mc_to_kernel(&wme, memory_entry(wc, e),
1033ec6347bbSDan Williams 				      sizeof(struct wc_memory_entry));
103448debafeSMikulas Patocka 		if (r) {
103548debafeSMikulas Patocka 			writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
103648debafeSMikulas Patocka 					 (unsigned long)b, r);
103748debafeSMikulas Patocka 			e->original_sector = -1;
103848debafeSMikulas Patocka 			e->seq_count = -1;
103948debafeSMikulas Patocka 		} else {
104048debafeSMikulas Patocka 			e->original_sector = le64_to_cpu(wme.original_sector);
104148debafeSMikulas Patocka 			e->seq_count = le64_to_cpu(wme.seq_count);
104248debafeSMikulas Patocka 		}
10431edaa447SMikulas Patocka 		cond_resched();
104448debafeSMikulas Patocka 	}
104548debafeSMikulas Patocka #endif
104648debafeSMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
104748debafeSMikulas Patocka 		struct wc_entry *e = &wc->entries[b];
104848debafeSMikulas Patocka 		if (!writecache_entry_is_committed(wc, e)) {
104948debafeSMikulas Patocka 			if (read_seq_count(wc, e) != -1) {
105048debafeSMikulas Patocka erase_this:
105148debafeSMikulas Patocka 				clear_seq_count(wc, e);
105248debafeSMikulas Patocka 				need_flush = true;
105348debafeSMikulas Patocka 			}
105448debafeSMikulas Patocka 			writecache_add_to_freelist(wc, e);
105548debafeSMikulas Patocka 		} else {
105648debafeSMikulas Patocka 			struct wc_entry *old;
105748debafeSMikulas Patocka 
105848debafeSMikulas Patocka 			old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
105948debafeSMikulas Patocka 			if (!old) {
106048debafeSMikulas Patocka 				writecache_insert_entry(wc, e);
106148debafeSMikulas Patocka 			} else {
106248debafeSMikulas Patocka 				if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
106348debafeSMikulas Patocka 					writecache_error(wc, -EINVAL,
106448debafeSMikulas Patocka 						 "two identical entries, position %llu, sector %llu, sequence %llu",
106548debafeSMikulas Patocka 						 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
106648debafeSMikulas Patocka 						 (unsigned long long)read_seq_count(wc, e));
106748debafeSMikulas Patocka 				}
106848debafeSMikulas Patocka 				if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
106948debafeSMikulas Patocka 					goto erase_this;
107048debafeSMikulas Patocka 				} else {
107148debafeSMikulas Patocka 					writecache_free_entry(wc, old);
107248debafeSMikulas Patocka 					writecache_insert_entry(wc, e);
107348debafeSMikulas Patocka 					need_flush = true;
107448debafeSMikulas Patocka 				}
107548debafeSMikulas Patocka 			}
107648debafeSMikulas Patocka 		}
107748debafeSMikulas Patocka 		cond_resched();
107848debafeSMikulas Patocka 	}
107948debafeSMikulas Patocka 
108048debafeSMikulas Patocka 	if (need_flush) {
108148debafeSMikulas Patocka 		writecache_flush_all_metadata(wc);
1082aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
108348debafeSMikulas Patocka 	}
108448debafeSMikulas Patocka 
108541c526c5SMikulas Patocka 	writecache_verify_watermark(wc);
108641c526c5SMikulas Patocka 
10873923d485SMikulas Patocka 	if (wc->max_age != MAX_AGE_UNSPECIFIED)
10883923d485SMikulas Patocka 		mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
10893923d485SMikulas Patocka 
109048debafeSMikulas Patocka 	wc_unlock(wc);
109148debafeSMikulas Patocka }
109248debafeSMikulas Patocka 
109348debafeSMikulas Patocka static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
109448debafeSMikulas Patocka {
109548debafeSMikulas Patocka 	if (argc != 1)
109648debafeSMikulas Patocka 		return -EINVAL;
109748debafeSMikulas Patocka 
109848debafeSMikulas Patocka 	wc_lock(wc);
109948debafeSMikulas Patocka 	if (dm_suspended(wc->ti)) {
110048debafeSMikulas Patocka 		wc_unlock(wc);
110148debafeSMikulas Patocka 		return -EBUSY;
110248debafeSMikulas Patocka 	}
110348debafeSMikulas Patocka 	if (writecache_has_error(wc)) {
110448debafeSMikulas Patocka 		wc_unlock(wc);
110548debafeSMikulas Patocka 		return -EIO;
110648debafeSMikulas Patocka 	}
110748debafeSMikulas Patocka 
110848debafeSMikulas Patocka 	writecache_flush(wc);
110948debafeSMikulas Patocka 	wc->writeback_all++;
111048debafeSMikulas Patocka 	queue_work(wc->writeback_wq, &wc->writeback_work);
111148debafeSMikulas Patocka 	wc_unlock(wc);
111248debafeSMikulas Patocka 
111348debafeSMikulas Patocka 	flush_workqueue(wc->writeback_wq);
111448debafeSMikulas Patocka 
111548debafeSMikulas Patocka 	wc_lock(wc);
111648debafeSMikulas Patocka 	wc->writeback_all--;
111748debafeSMikulas Patocka 	if (writecache_has_error(wc)) {
111848debafeSMikulas Patocka 		wc_unlock(wc);
111948debafeSMikulas Patocka 		return -EIO;
112048debafeSMikulas Patocka 	}
112148debafeSMikulas Patocka 	wc_unlock(wc);
112248debafeSMikulas Patocka 
112348debafeSMikulas Patocka 	return 0;
112448debafeSMikulas Patocka }
112548debafeSMikulas Patocka 
112648debafeSMikulas Patocka static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
112748debafeSMikulas Patocka {
112848debafeSMikulas Patocka 	if (argc != 1)
112948debafeSMikulas Patocka 		return -EINVAL;
113048debafeSMikulas Patocka 
113148debafeSMikulas Patocka 	wc_lock(wc);
113248debafeSMikulas Patocka 	wc->flush_on_suspend = true;
113348debafeSMikulas Patocka 	wc_unlock(wc);
113448debafeSMikulas Patocka 
113548debafeSMikulas Patocka 	return 0;
113648debafeSMikulas Patocka }
113748debafeSMikulas Patocka 
113893de44ebSMikulas Patocka static void activate_cleaner(struct dm_writecache *wc)
113993de44ebSMikulas Patocka {
114093de44ebSMikulas Patocka 	wc->flush_on_suspend = true;
114193de44ebSMikulas Patocka 	wc->cleaner = true;
114293de44ebSMikulas Patocka 	wc->freelist_high_watermark = wc->n_blocks;
114393de44ebSMikulas Patocka 	wc->freelist_low_watermark = wc->n_blocks;
114493de44ebSMikulas Patocka }
114593de44ebSMikulas Patocka 
114693de44ebSMikulas Patocka static int process_cleaner_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
114793de44ebSMikulas Patocka {
114893de44ebSMikulas Patocka 	if (argc != 1)
114993de44ebSMikulas Patocka 		return -EINVAL;
115093de44ebSMikulas Patocka 
115193de44ebSMikulas Patocka 	wc_lock(wc);
115293de44ebSMikulas Patocka 	activate_cleaner(wc);
115393de44ebSMikulas Patocka 	if (!dm_suspended(wc->ti))
115493de44ebSMikulas Patocka 		writecache_verify_watermark(wc);
115593de44ebSMikulas Patocka 	wc_unlock(wc);
115693de44ebSMikulas Patocka 
115793de44ebSMikulas Patocka 	return 0;
115893de44ebSMikulas Patocka }
115993de44ebSMikulas Patocka 
116048debafeSMikulas Patocka static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
116148debafeSMikulas Patocka 			      char *result, unsigned maxlen)
116248debafeSMikulas Patocka {
116348debafeSMikulas Patocka 	int r = -EINVAL;
116448debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
116548debafeSMikulas Patocka 
116648debafeSMikulas Patocka 	if (!strcasecmp(argv[0], "flush"))
116748debafeSMikulas Patocka 		r = process_flush_mesg(argc, argv, wc);
116848debafeSMikulas Patocka 	else if (!strcasecmp(argv[0], "flush_on_suspend"))
116948debafeSMikulas Patocka 		r = process_flush_on_suspend_mesg(argc, argv, wc);
117093de44ebSMikulas Patocka 	else if (!strcasecmp(argv[0], "cleaner"))
117193de44ebSMikulas Patocka 		r = process_cleaner_mesg(argc, argv, wc);
117248debafeSMikulas Patocka 	else
117348debafeSMikulas Patocka 		DMERR("unrecognised message received: %s", argv[0]);
117448debafeSMikulas Patocka 
117548debafeSMikulas Patocka 	return r;
117648debafeSMikulas Patocka }
117748debafeSMikulas Patocka 
117848338daaSMikulas Patocka static void memcpy_flushcache_optimized(void *dest, void *source, size_t size)
117948338daaSMikulas Patocka {
118048338daaSMikulas Patocka 	/*
118148338daaSMikulas Patocka 	 * clflushopt performs better with block size 1024, 2048, 4096
118248338daaSMikulas Patocka 	 * non-temporal stores perform better with block size 512
118348338daaSMikulas Patocka 	 *
118448338daaSMikulas Patocka 	 * block size   512             1024            2048            4096
118548338daaSMikulas Patocka 	 * movnti       496 MB/s        642 MB/s        725 MB/s        744 MB/s
118648338daaSMikulas Patocka 	 * clflushopt   373 MB/s        688 MB/s        1.1 GB/s        1.2 GB/s
118748338daaSMikulas Patocka 	 *
118848338daaSMikulas Patocka 	 * We see that movnti performs better for 512-byte blocks, and
118948338daaSMikulas Patocka 	 * clflushopt performs better for 1024-byte and larger blocks. So, we
119048338daaSMikulas Patocka 	 * prefer clflushopt for sizes >= 768.
119148338daaSMikulas Patocka 	 *
119248338daaSMikulas Patocka 	 * NOTE: this happens to be the case now (with dm-writecache's single
119348338daaSMikulas Patocka 	 * threaded model) but re-evaluate this once memcpy_flushcache() is
119448338daaSMikulas Patocka 	 * enabled to use movdir64b which might invalidate this performance
119548338daaSMikulas Patocka 	 * advantage seen with cache-allocating-writes plus flushing.
119648338daaSMikulas Patocka 	 */
119748338daaSMikulas Patocka #ifdef CONFIG_X86
119848338daaSMikulas Patocka 	if (static_cpu_has(X86_FEATURE_CLFLUSHOPT) &&
119948338daaSMikulas Patocka 	    likely(boot_cpu_data.x86_clflush_size == 64) &&
120048338daaSMikulas Patocka 	    likely(size >= 768)) {
120148338daaSMikulas Patocka 		do {
120248338daaSMikulas Patocka 			memcpy((void *)dest, (void *)source, 64);
120348338daaSMikulas Patocka 			clflushopt((void *)dest);
120448338daaSMikulas Patocka 			dest += 64;
120548338daaSMikulas Patocka 			source += 64;
120648338daaSMikulas Patocka 			size -= 64;
120748338daaSMikulas Patocka 		} while (size >= 64);
120848338daaSMikulas Patocka 		return;
120948338daaSMikulas Patocka 	}
121048338daaSMikulas Patocka #endif
121148338daaSMikulas Patocka 	memcpy_flushcache(dest, source, size);
121248338daaSMikulas Patocka }
121348338daaSMikulas Patocka 
121448debafeSMikulas Patocka static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
121548debafeSMikulas Patocka {
121648debafeSMikulas Patocka 	void *buf;
121748debafeSMikulas Patocka 	unsigned size;
121848debafeSMikulas Patocka 	int rw = bio_data_dir(bio);
121948debafeSMikulas Patocka 	unsigned remaining_size = wc->block_size;
122048debafeSMikulas Patocka 
122148debafeSMikulas Patocka 	do {
122248debafeSMikulas Patocka 		struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
122318a6234cSChristoph Hellwig 		buf = bvec_kmap_local(&bv);
122448debafeSMikulas Patocka 		size = bv.bv_len;
122548debafeSMikulas Patocka 		if (unlikely(size > remaining_size))
122648debafeSMikulas Patocka 			size = remaining_size;
122748debafeSMikulas Patocka 
122848debafeSMikulas Patocka 		if (rw == READ) {
122948debafeSMikulas Patocka 			int r;
1230ec6347bbSDan Williams 			r = copy_mc_to_kernel(buf, data, size);
123148debafeSMikulas Patocka 			flush_dcache_page(bio_page(bio));
123248debafeSMikulas Patocka 			if (unlikely(r)) {
123348debafeSMikulas Patocka 				writecache_error(wc, r, "hardware memory error when reading data: %d", r);
123448debafeSMikulas Patocka 				bio->bi_status = BLK_STS_IOERR;
123548debafeSMikulas Patocka 			}
123648debafeSMikulas Patocka 		} else {
123748debafeSMikulas Patocka 			flush_dcache_page(bio_page(bio));
123848338daaSMikulas Patocka 			memcpy_flushcache_optimized(data, buf, size);
123948debafeSMikulas Patocka 		}
124048debafeSMikulas Patocka 
124118a6234cSChristoph Hellwig 		kunmap_local(buf);
124248debafeSMikulas Patocka 
124348debafeSMikulas Patocka 		data = (char *)data + size;
124448debafeSMikulas Patocka 		remaining_size -= size;
124548debafeSMikulas Patocka 		bio_advance(bio, size);
124648debafeSMikulas Patocka 	} while (unlikely(remaining_size));
124748debafeSMikulas Patocka }
124848debafeSMikulas Patocka 
124948debafeSMikulas Patocka static int writecache_flush_thread(void *data)
125048debafeSMikulas Patocka {
125148debafeSMikulas Patocka 	struct dm_writecache *wc = data;
125248debafeSMikulas Patocka 
125348debafeSMikulas Patocka 	while (1) {
125448debafeSMikulas Patocka 		struct bio *bio;
125548debafeSMikulas Patocka 
125648debafeSMikulas Patocka 		wc_lock(wc);
125748debafeSMikulas Patocka 		bio = bio_list_pop(&wc->flush_list);
125848debafeSMikulas Patocka 		if (!bio) {
125948debafeSMikulas Patocka 			set_current_state(TASK_INTERRUPTIBLE);
126048debafeSMikulas Patocka 			wc_unlock(wc);
126148debafeSMikulas Patocka 
126248debafeSMikulas Patocka 			if (unlikely(kthread_should_stop())) {
126348debafeSMikulas Patocka 				set_current_state(TASK_RUNNING);
126448debafeSMikulas Patocka 				break;
126548debafeSMikulas Patocka 			}
126648debafeSMikulas Patocka 
126748debafeSMikulas Patocka 			schedule();
126848debafeSMikulas Patocka 			continue;
126948debafeSMikulas Patocka 		}
127048debafeSMikulas Patocka 
127148debafeSMikulas Patocka 		if (bio_op(bio) == REQ_OP_DISCARD) {
127248debafeSMikulas Patocka 			writecache_discard(wc, bio->bi_iter.bi_sector,
127348debafeSMikulas Patocka 					   bio_end_sector(bio));
127448debafeSMikulas Patocka 			wc_unlock(wc);
127548debafeSMikulas Patocka 			bio_set_dev(bio, wc->dev->bdev);
1276ed00aabdSChristoph Hellwig 			submit_bio_noacct(bio);
127748debafeSMikulas Patocka 		} else {
127848debafeSMikulas Patocka 			writecache_flush(wc);
127948debafeSMikulas Patocka 			wc_unlock(wc);
128048debafeSMikulas Patocka 			if (writecache_has_error(wc))
128148debafeSMikulas Patocka 				bio->bi_status = BLK_STS_IOERR;
128248debafeSMikulas Patocka 			bio_endio(bio);
128348debafeSMikulas Patocka 		}
128448debafeSMikulas Patocka 	}
128548debafeSMikulas Patocka 
128648debafeSMikulas Patocka 	return 0;
128748debafeSMikulas Patocka }
128848debafeSMikulas Patocka 
128948debafeSMikulas Patocka static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
129048debafeSMikulas Patocka {
129148debafeSMikulas Patocka 	if (bio_list_empty(&wc->flush_list))
129248debafeSMikulas Patocka 		wake_up_process(wc->flush_thread);
129348debafeSMikulas Patocka 	bio_list_add(&wc->flush_list, bio);
129448debafeSMikulas Patocka }
129548debafeSMikulas Patocka 
1296cdd4d783SMike Snitzer enum wc_map_op {
1297cdd4d783SMike Snitzer 	WC_MAP_SUBMIT,
1298cdd4d783SMike Snitzer 	WC_MAP_REMAP,
1299cdd4d783SMike Snitzer 	WC_MAP_REMAP_ORIGIN,
1300cdd4d783SMike Snitzer 	WC_MAP_RETURN,
1301cdd4d783SMike Snitzer 	WC_MAP_ERROR,
1302cdd4d783SMike Snitzer };
1303cdd4d783SMike Snitzer 
13044d020b3aSMike Snitzer static enum wc_map_op writecache_map_remap_origin(struct dm_writecache *wc, struct bio *bio,
13054d020b3aSMike Snitzer 						  struct wc_entry *e)
13064d020b3aSMike Snitzer {
13074d020b3aSMike Snitzer 	if (e) {
13084d020b3aSMike Snitzer 		sector_t next_boundary =
13094d020b3aSMike Snitzer 			read_original_sector(wc, e) - bio->bi_iter.bi_sector;
13104d020b3aSMike Snitzer 		if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT)
13114d020b3aSMike Snitzer 			dm_accept_partial_bio(bio, next_boundary);
13124d020b3aSMike Snitzer 	}
13134d020b3aSMike Snitzer 
13144d020b3aSMike Snitzer 	return WC_MAP_REMAP_ORIGIN;
13154d020b3aSMike Snitzer }
13164d020b3aSMike Snitzer 
1317cdd4d783SMike Snitzer static enum wc_map_op writecache_map_read(struct dm_writecache *wc, struct bio *bio)
1318cdd4d783SMike Snitzer {
1319cdd4d783SMike Snitzer 	enum wc_map_op map_op;
1320cdd4d783SMike Snitzer 	struct wc_entry *e;
1321cdd4d783SMike Snitzer 
1322cdd4d783SMike Snitzer read_next_block:
1323cdd4d783SMike Snitzer 	e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1324cdd4d783SMike Snitzer 	if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1325cdd4d783SMike Snitzer 		if (WC_MODE_PMEM(wc)) {
1326cdd4d783SMike Snitzer 			bio_copy_block(wc, bio, memory_data(wc, e));
1327cdd4d783SMike Snitzer 			if (bio->bi_iter.bi_size)
1328cdd4d783SMike Snitzer 				goto read_next_block;
1329cdd4d783SMike Snitzer 			map_op = WC_MAP_SUBMIT;
1330cdd4d783SMike Snitzer 		} else {
1331cdd4d783SMike Snitzer 			dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1332cdd4d783SMike Snitzer 			bio_set_dev(bio, wc->ssd_dev->bdev);
1333cdd4d783SMike Snitzer 			bio->bi_iter.bi_sector = cache_sector(wc, e);
1334cdd4d783SMike Snitzer 			if (!writecache_entry_is_committed(wc, e))
1335cdd4d783SMike Snitzer 				writecache_wait_for_ios(wc, WRITE);
1336cdd4d783SMike Snitzer 			map_op = WC_MAP_REMAP;
1337cdd4d783SMike Snitzer 		}
1338cdd4d783SMike Snitzer 	} else {
13394d020b3aSMike Snitzer 		map_op = writecache_map_remap_origin(wc, bio, e);
1340cdd4d783SMike Snitzer 	}
1341cdd4d783SMike Snitzer 
1342cdd4d783SMike Snitzer 	return map_op;
1343cdd4d783SMike Snitzer }
1344cdd4d783SMike Snitzer 
1345cdd4d783SMike Snitzer static enum wc_map_op writecache_bio_copy_ssd(struct dm_writecache *wc, struct bio *bio,
1346cdd4d783SMike Snitzer 					      struct wc_entry *e, bool search_used)
1347cdd4d783SMike Snitzer {
1348cdd4d783SMike Snitzer 	unsigned bio_size = wc->block_size;
1349cdd4d783SMike Snitzer 	sector_t start_cache_sec = cache_sector(wc, e);
1350cdd4d783SMike Snitzer 	sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT);
1351cdd4d783SMike Snitzer 
1352cdd4d783SMike Snitzer 	while (bio_size < bio->bi_iter.bi_size) {
1353cdd4d783SMike Snitzer 		if (!search_used) {
1354cdd4d783SMike Snitzer 			struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec);
1355cdd4d783SMike Snitzer 			if (!f)
1356cdd4d783SMike Snitzer 				break;
1357cdd4d783SMike Snitzer 			write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector +
1358cdd4d783SMike Snitzer 							(bio_size >> SECTOR_SHIFT), wc->seq_count);
1359cdd4d783SMike Snitzer 			writecache_insert_entry(wc, f);
1360cdd4d783SMike Snitzer 			wc->uncommitted_blocks++;
1361cdd4d783SMike Snitzer 		} else {
1362cdd4d783SMike Snitzer 			struct wc_entry *f;
1363cdd4d783SMike Snitzer 			struct rb_node *next = rb_next(&e->rb_node);
1364cdd4d783SMike Snitzer 			if (!next)
1365cdd4d783SMike Snitzer 				break;
1366cdd4d783SMike Snitzer 			f = container_of(next, struct wc_entry, rb_node);
1367cdd4d783SMike Snitzer 			if (f != e + 1)
1368cdd4d783SMike Snitzer 				break;
1369cdd4d783SMike Snitzer 			if (read_original_sector(wc, f) !=
1370cdd4d783SMike Snitzer 			    read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1371cdd4d783SMike Snitzer 				break;
1372cdd4d783SMike Snitzer 			if (unlikely(f->write_in_progress))
1373cdd4d783SMike Snitzer 				break;
1374cdd4d783SMike Snitzer 			if (writecache_entry_is_committed(wc, f))
1375cdd4d783SMike Snitzer 				wc->overwrote_committed = true;
1376cdd4d783SMike Snitzer 			e = f;
1377cdd4d783SMike Snitzer 		}
1378cdd4d783SMike Snitzer 		bio_size += wc->block_size;
1379cdd4d783SMike Snitzer 		current_cache_sec += wc->block_size >> SECTOR_SHIFT;
1380cdd4d783SMike Snitzer 	}
1381cdd4d783SMike Snitzer 
1382cdd4d783SMike Snitzer 	bio_set_dev(bio, wc->ssd_dev->bdev);
1383cdd4d783SMike Snitzer 	bio->bi_iter.bi_sector = start_cache_sec;
1384cdd4d783SMike Snitzer 	dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT);
1385cdd4d783SMike Snitzer 
1386cdd4d783SMike Snitzer 	if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1387cdd4d783SMike Snitzer 		wc->uncommitted_blocks = 0;
1388cdd4d783SMike Snitzer 		queue_work(wc->writeback_wq, &wc->flush_work);
1389cdd4d783SMike Snitzer 	} else {
1390cdd4d783SMike Snitzer 		writecache_schedule_autocommit(wc);
1391cdd4d783SMike Snitzer 	}
1392cdd4d783SMike Snitzer 
1393cdd4d783SMike Snitzer 	return WC_MAP_REMAP;
1394cdd4d783SMike Snitzer }
1395cdd4d783SMike Snitzer 
1396cdd4d783SMike Snitzer static enum wc_map_op writecache_map_write(struct dm_writecache *wc, struct bio *bio)
139748debafeSMikulas Patocka {
139848debafeSMikulas Patocka 	struct wc_entry *e;
1399cdd4d783SMike Snitzer 
1400cdd4d783SMike Snitzer 	do {
1401cdd4d783SMike Snitzer 		bool found_entry = false;
1402cdd4d783SMike Snitzer 		bool search_used = false;
1403cdd4d783SMike Snitzer 		if (writecache_has_error(wc))
1404cdd4d783SMike Snitzer 			return WC_MAP_ERROR;
1405cdd4d783SMike Snitzer 		e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1406cdd4d783SMike Snitzer 		if (e) {
1407cdd4d783SMike Snitzer 			if (!writecache_entry_is_committed(wc, e)) {
1408cdd4d783SMike Snitzer 				search_used = true;
1409cdd4d783SMike Snitzer 				goto bio_copy;
1410cdd4d783SMike Snitzer 			}
1411cdd4d783SMike Snitzer 			if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1412cdd4d783SMike Snitzer 				wc->overwrote_committed = true;
1413cdd4d783SMike Snitzer 				search_used = true;
1414cdd4d783SMike Snitzer 				goto bio_copy;
1415cdd4d783SMike Snitzer 			}
1416cdd4d783SMike Snitzer 			found_entry = true;
1417cdd4d783SMike Snitzer 		} else {
1418cdd4d783SMike Snitzer 			if (unlikely(wc->cleaner) ||
1419cdd4d783SMike Snitzer 			    (wc->metadata_only && !(bio->bi_opf & REQ_META)))
1420cdd4d783SMike Snitzer 				goto direct_write;
1421cdd4d783SMike Snitzer 		}
1422cdd4d783SMike Snitzer 		e = writecache_pop_from_freelist(wc, (sector_t)-1);
1423cdd4d783SMike Snitzer 		if (unlikely(!e)) {
1424cdd4d783SMike Snitzer 			if (!WC_MODE_PMEM(wc) && !found_entry) {
1425cdd4d783SMike Snitzer direct_write:
1426cdd4d783SMike Snitzer 				e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
14274d020b3aSMike Snitzer 				return writecache_map_remap_origin(wc, bio, e);
1428cdd4d783SMike Snitzer 			}
1429cdd4d783SMike Snitzer 			writecache_wait_on_freelist(wc);
1430cdd4d783SMike Snitzer 			continue;
1431cdd4d783SMike Snitzer 		}
1432cdd4d783SMike Snitzer 		write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1433cdd4d783SMike Snitzer 		writecache_insert_entry(wc, e);
1434cdd4d783SMike Snitzer 		wc->uncommitted_blocks++;
1435cdd4d783SMike Snitzer bio_copy:
1436cdd4d783SMike Snitzer 		if (WC_MODE_PMEM(wc))
1437cdd4d783SMike Snitzer 			bio_copy_block(wc, bio, memory_data(wc, e));
1438cdd4d783SMike Snitzer 		else
1439cdd4d783SMike Snitzer 			return writecache_bio_copy_ssd(wc, bio, e, search_used);
1440cdd4d783SMike Snitzer 	} while (bio->bi_iter.bi_size);
1441cdd4d783SMike Snitzer 
1442cdd4d783SMike Snitzer 	if (unlikely(bio->bi_opf & REQ_FUA || wc->uncommitted_blocks >= wc->autocommit_blocks))
1443cdd4d783SMike Snitzer 		writecache_flush(wc);
1444cdd4d783SMike Snitzer 	else
1445cdd4d783SMike Snitzer 		writecache_schedule_autocommit(wc);
1446cdd4d783SMike Snitzer 
1447cdd4d783SMike Snitzer 	return WC_MAP_SUBMIT;
1448cdd4d783SMike Snitzer }
1449cdd4d783SMike Snitzer 
145015cb6f39SMike Snitzer static enum wc_map_op writecache_map_flush(struct dm_writecache *wc, struct bio *bio)
145115cb6f39SMike Snitzer {
145215cb6f39SMike Snitzer 	if (writecache_has_error(wc))
145315cb6f39SMike Snitzer 		return WC_MAP_ERROR;
145415cb6f39SMike Snitzer 
145515cb6f39SMike Snitzer 	if (WC_MODE_PMEM(wc)) {
145615cb6f39SMike Snitzer 		writecache_flush(wc);
145715cb6f39SMike Snitzer 		if (writecache_has_error(wc))
145815cb6f39SMike Snitzer 			return WC_MAP_ERROR;
145915cb6f39SMike Snitzer 		else if (unlikely(wc->cleaner) || unlikely(wc->metadata_only))
146015cb6f39SMike Snitzer 			return WC_MAP_REMAP_ORIGIN;
146115cb6f39SMike Snitzer 		return WC_MAP_SUBMIT;
146215cb6f39SMike Snitzer 	}
146315cb6f39SMike Snitzer 	/* SSD: */
146415cb6f39SMike Snitzer 	if (dm_bio_get_target_bio_nr(bio))
146515cb6f39SMike Snitzer 		return WC_MAP_REMAP_ORIGIN;
146615cb6f39SMike Snitzer 	writecache_offload_bio(wc, bio);
146715cb6f39SMike Snitzer 	return WC_MAP_RETURN;
146815cb6f39SMike Snitzer }
146915cb6f39SMike Snitzer 
147015cb6f39SMike Snitzer static enum wc_map_op writecache_map_discard(struct dm_writecache *wc, struct bio *bio)
147115cb6f39SMike Snitzer {
147215cb6f39SMike Snitzer 	if (writecache_has_error(wc))
147315cb6f39SMike Snitzer 		return WC_MAP_ERROR;
147415cb6f39SMike Snitzer 
147515cb6f39SMike Snitzer 	if (WC_MODE_PMEM(wc)) {
147615cb6f39SMike Snitzer 		writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
147715cb6f39SMike Snitzer 		return WC_MAP_REMAP_ORIGIN;
147815cb6f39SMike Snitzer 	}
147915cb6f39SMike Snitzer 	/* SSD: */
148015cb6f39SMike Snitzer 	writecache_offload_bio(wc, bio);
148115cb6f39SMike Snitzer 	return WC_MAP_RETURN;
148215cb6f39SMike Snitzer }
148315cb6f39SMike Snitzer 
1484cdd4d783SMike Snitzer static int writecache_map(struct dm_target *ti, struct bio *bio)
1485cdd4d783SMike Snitzer {
148648debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
148715cb6f39SMike Snitzer 	enum wc_map_op map_op;
148848debafeSMikulas Patocka 
148948debafeSMikulas Patocka 	bio->bi_private = NULL;
149048debafeSMikulas Patocka 
149148debafeSMikulas Patocka 	wc_lock(wc);
149248debafeSMikulas Patocka 
149348debafeSMikulas Patocka 	if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
149415cb6f39SMike Snitzer 		map_op = writecache_map_flush(wc, bio);
149515cb6f39SMike Snitzer 		goto done;
149648debafeSMikulas Patocka 	}
149748debafeSMikulas Patocka 
149848debafeSMikulas Patocka 	bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
149948debafeSMikulas Patocka 
150048debafeSMikulas Patocka 	if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
150148debafeSMikulas Patocka 				(wc->block_size / 512 - 1)) != 0)) {
150248debafeSMikulas Patocka 		DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
150348debafeSMikulas Patocka 		      (unsigned long long)bio->bi_iter.bi_sector,
150448debafeSMikulas Patocka 		      bio->bi_iter.bi_size, wc->block_size);
150515cb6f39SMike Snitzer 		map_op = WC_MAP_ERROR;
150615cb6f39SMike Snitzer 		goto done;
150748debafeSMikulas Patocka 	}
150848debafeSMikulas Patocka 
150948debafeSMikulas Patocka 	if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
151015cb6f39SMike Snitzer 		map_op = writecache_map_discard(wc, bio);
151115cb6f39SMike Snitzer 		goto done;
151248debafeSMikulas Patocka 	}
151348debafeSMikulas Patocka 
1514cdd4d783SMike Snitzer 	if (bio_data_dir(bio) == READ)
1515cdd4d783SMike Snitzer 		map_op = writecache_map_read(wc, bio);
151648debafeSMikulas Patocka 	else
1517cdd4d783SMike Snitzer 		map_op = writecache_map_write(wc, bio);
151815cb6f39SMike Snitzer done:
1519cdd4d783SMike Snitzer 	switch (map_op) {
1520cdd4d783SMike Snitzer 	case WC_MAP_REMAP_ORIGIN:
15215c0de3d7SMikulas Patocka 		if (likely(wc->pause != 0)) {
15225c0de3d7SMikulas Patocka 			if (bio_op(bio) == REQ_OP_WRITE) {
152395b88f4dSMikulas Patocka 				dm_iot_io_begin(&wc->iot, 1);
152495b88f4dSMikulas Patocka 				bio->bi_private = (void *)2;
152595b88f4dSMikulas Patocka 			}
15265c0de3d7SMikulas Patocka 		}
152748debafeSMikulas Patocka 		bio_set_dev(bio, wc->dev->bdev);
152848debafeSMikulas Patocka 		wc_unlock(wc);
152948debafeSMikulas Patocka 		return DM_MAPIO_REMAPPED;
153048debafeSMikulas Patocka 
1531cdd4d783SMike Snitzer 	case WC_MAP_REMAP:
153248debafeSMikulas Patocka 		/* make sure that writecache_end_io decrements bio_in_progress: */
153348debafeSMikulas Patocka 		bio->bi_private = (void *)1;
153448debafeSMikulas Patocka 		atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
153548debafeSMikulas Patocka 		wc_unlock(wc);
153648debafeSMikulas Patocka 		return DM_MAPIO_REMAPPED;
153748debafeSMikulas Patocka 
1538cdd4d783SMike Snitzer 	case WC_MAP_SUBMIT:
153948debafeSMikulas Patocka 		wc_unlock(wc);
154048debafeSMikulas Patocka 		bio_endio(bio);
154148debafeSMikulas Patocka 		return DM_MAPIO_SUBMITTED;
154248debafeSMikulas Patocka 
1543cdd4d783SMike Snitzer 	case WC_MAP_RETURN:
154448debafeSMikulas Patocka 		wc_unlock(wc);
154548debafeSMikulas Patocka 		return DM_MAPIO_SUBMITTED;
154648debafeSMikulas Patocka 
1547cdd4d783SMike Snitzer 	case WC_MAP_ERROR:
154848debafeSMikulas Patocka 		wc_unlock(wc);
154948debafeSMikulas Patocka 		bio_io_error(bio);
155048debafeSMikulas Patocka 		return DM_MAPIO_SUBMITTED;
1551*df699cc1SMikulas Patocka 
1552*df699cc1SMikulas Patocka 	default:
1553*df699cc1SMikulas Patocka 		BUG();
1554*df699cc1SMikulas Patocka 		return -1;
155548debafeSMikulas Patocka 	}
1556cdd4d783SMike Snitzer }
155748debafeSMikulas Patocka 
155848debafeSMikulas Patocka static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
155948debafeSMikulas Patocka {
156048debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
156148debafeSMikulas Patocka 
156295b88f4dSMikulas Patocka 	if (bio->bi_private == (void *)1) {
156348debafeSMikulas Patocka 		int dir = bio_data_dir(bio);
156448debafeSMikulas Patocka 		if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
156548debafeSMikulas Patocka 			if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
156648debafeSMikulas Patocka 				wake_up(&wc->bio_in_progress_wait[dir]);
156795b88f4dSMikulas Patocka 	} else if (bio->bi_private == (void *)2) {
156895b88f4dSMikulas Patocka 		dm_iot_io_end(&wc->iot, 1);
156948debafeSMikulas Patocka 	}
157048debafeSMikulas Patocka 	return 0;
157148debafeSMikulas Patocka }
157248debafeSMikulas Patocka 
157348debafeSMikulas Patocka static int writecache_iterate_devices(struct dm_target *ti,
157448debafeSMikulas Patocka 				      iterate_devices_callout_fn fn, void *data)
157548debafeSMikulas Patocka {
157648debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
157748debafeSMikulas Patocka 
157848debafeSMikulas Patocka 	return fn(ti, wc->dev, 0, ti->len, data);
157948debafeSMikulas Patocka }
158048debafeSMikulas Patocka 
158148debafeSMikulas Patocka static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
158248debafeSMikulas Patocka {
158348debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
158448debafeSMikulas Patocka 
158548debafeSMikulas Patocka 	if (limits->logical_block_size < wc->block_size)
158648debafeSMikulas Patocka 		limits->logical_block_size = wc->block_size;
158748debafeSMikulas Patocka 
158848debafeSMikulas Patocka 	if (limits->physical_block_size < wc->block_size)
158948debafeSMikulas Patocka 		limits->physical_block_size = wc->block_size;
159048debafeSMikulas Patocka 
159148debafeSMikulas Patocka 	if (limits->io_min < wc->block_size)
159248debafeSMikulas Patocka 		limits->io_min = wc->block_size;
159348debafeSMikulas Patocka }
159448debafeSMikulas Patocka 
159548debafeSMikulas Patocka 
159648debafeSMikulas Patocka static void writecache_writeback_endio(struct bio *bio)
159748debafeSMikulas Patocka {
159848debafeSMikulas Patocka 	struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
159948debafeSMikulas Patocka 	struct dm_writecache *wc = wb->wc;
160048debafeSMikulas Patocka 	unsigned long flags;
160148debafeSMikulas Patocka 
160248debafeSMikulas Patocka 	raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
160348debafeSMikulas Patocka 	if (unlikely(list_empty(&wc->endio_list)))
160448debafeSMikulas Patocka 		wake_up_process(wc->endio_thread);
160548debafeSMikulas Patocka 	list_add_tail(&wb->endio_entry, &wc->endio_list);
160648debafeSMikulas Patocka 	raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
160748debafeSMikulas Patocka }
160848debafeSMikulas Patocka 
160948debafeSMikulas Patocka static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
161048debafeSMikulas Patocka {
161148debafeSMikulas Patocka 	struct copy_struct *c = ptr;
161248debafeSMikulas Patocka 	struct dm_writecache *wc = c->wc;
161348debafeSMikulas Patocka 
161448debafeSMikulas Patocka 	c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
161548debafeSMikulas Patocka 
161648debafeSMikulas Patocka 	raw_spin_lock_irq(&wc->endio_list_lock);
161748debafeSMikulas Patocka 	if (unlikely(list_empty(&wc->endio_list)))
161848debafeSMikulas Patocka 		wake_up_process(wc->endio_thread);
161948debafeSMikulas Patocka 	list_add_tail(&c->endio_entry, &wc->endio_list);
162048debafeSMikulas Patocka 	raw_spin_unlock_irq(&wc->endio_list_lock);
162148debafeSMikulas Patocka }
162248debafeSMikulas Patocka 
162348debafeSMikulas Patocka static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
162448debafeSMikulas Patocka {
162548debafeSMikulas Patocka 	unsigned i;
162648debafeSMikulas Patocka 	struct writeback_struct *wb;
162748debafeSMikulas Patocka 	struct wc_entry *e;
162848debafeSMikulas Patocka 	unsigned long n_walked = 0;
162948debafeSMikulas Patocka 
163048debafeSMikulas Patocka 	do {
163148debafeSMikulas Patocka 		wb = list_entry(list->next, struct writeback_struct, endio_entry);
163248debafeSMikulas Patocka 		list_del(&wb->endio_entry);
163348debafeSMikulas Patocka 
163448debafeSMikulas Patocka 		if (unlikely(wb->bio.bi_status != BLK_STS_OK))
163548debafeSMikulas Patocka 			writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
163648debafeSMikulas Patocka 					"write error %d", wb->bio.bi_status);
163748debafeSMikulas Patocka 		i = 0;
163848debafeSMikulas Patocka 		do {
163948debafeSMikulas Patocka 			e = wb->wc_list[i];
164048debafeSMikulas Patocka 			BUG_ON(!e->write_in_progress);
164148debafeSMikulas Patocka 			e->write_in_progress = false;
164248debafeSMikulas Patocka 			INIT_LIST_HEAD(&e->lru);
164348debafeSMikulas Patocka 			if (!writecache_has_error(wc))
164448debafeSMikulas Patocka 				writecache_free_entry(wc, e);
164548debafeSMikulas Patocka 			BUG_ON(!wc->writeback_size);
164648debafeSMikulas Patocka 			wc->writeback_size--;
164748debafeSMikulas Patocka 			n_walked++;
164848debafeSMikulas Patocka 			if (unlikely(n_walked >= ENDIO_LATENCY)) {
1649aa950920SMikulas Patocka 				writecache_commit_flushed(wc, false);
165048debafeSMikulas Patocka 				wc_unlock(wc);
165148debafeSMikulas Patocka 				wc_lock(wc);
165248debafeSMikulas Patocka 				n_walked = 0;
165348debafeSMikulas Patocka 			}
165448debafeSMikulas Patocka 		} while (++i < wb->wc_list_n);
165548debafeSMikulas Patocka 
165648debafeSMikulas Patocka 		if (wb->wc_list != wb->wc_list_inline)
165748debafeSMikulas Patocka 			kfree(wb->wc_list);
165848debafeSMikulas Patocka 		bio_put(&wb->bio);
165948debafeSMikulas Patocka 	} while (!list_empty(list));
166048debafeSMikulas Patocka }
166148debafeSMikulas Patocka 
166248debafeSMikulas Patocka static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
166348debafeSMikulas Patocka {
166448debafeSMikulas Patocka 	struct copy_struct *c;
166548debafeSMikulas Patocka 	struct wc_entry *e;
166648debafeSMikulas Patocka 
166748debafeSMikulas Patocka 	do {
166848debafeSMikulas Patocka 		c = list_entry(list->next, struct copy_struct, endio_entry);
166948debafeSMikulas Patocka 		list_del(&c->endio_entry);
167048debafeSMikulas Patocka 
167148debafeSMikulas Patocka 		if (unlikely(c->error))
167248debafeSMikulas Patocka 			writecache_error(wc, c->error, "copy error");
167348debafeSMikulas Patocka 
167448debafeSMikulas Patocka 		e = c->e;
167548debafeSMikulas Patocka 		do {
167648debafeSMikulas Patocka 			BUG_ON(!e->write_in_progress);
167748debafeSMikulas Patocka 			e->write_in_progress = false;
167848debafeSMikulas Patocka 			INIT_LIST_HEAD(&e->lru);
167948debafeSMikulas Patocka 			if (!writecache_has_error(wc))
168048debafeSMikulas Patocka 				writecache_free_entry(wc, e);
168148debafeSMikulas Patocka 
168248debafeSMikulas Patocka 			BUG_ON(!wc->writeback_size);
168348debafeSMikulas Patocka 			wc->writeback_size--;
168448debafeSMikulas Patocka 			e++;
168548debafeSMikulas Patocka 		} while (--c->n_entries);
168648debafeSMikulas Patocka 		mempool_free(c, &wc->copy_pool);
168748debafeSMikulas Patocka 	} while (!list_empty(list));
168848debafeSMikulas Patocka }
168948debafeSMikulas Patocka 
169048debafeSMikulas Patocka static int writecache_endio_thread(void *data)
169148debafeSMikulas Patocka {
169248debafeSMikulas Patocka 	struct dm_writecache *wc = data;
169348debafeSMikulas Patocka 
169448debafeSMikulas Patocka 	while (1) {
169548debafeSMikulas Patocka 		struct list_head list;
169648debafeSMikulas Patocka 
169748debafeSMikulas Patocka 		raw_spin_lock_irq(&wc->endio_list_lock);
169848debafeSMikulas Patocka 		if (!list_empty(&wc->endio_list))
169948debafeSMikulas Patocka 			goto pop_from_list;
170048debafeSMikulas Patocka 		set_current_state(TASK_INTERRUPTIBLE);
170148debafeSMikulas Patocka 		raw_spin_unlock_irq(&wc->endio_list_lock);
170248debafeSMikulas Patocka 
170348debafeSMikulas Patocka 		if (unlikely(kthread_should_stop())) {
170448debafeSMikulas Patocka 			set_current_state(TASK_RUNNING);
170548debafeSMikulas Patocka 			break;
170648debafeSMikulas Patocka 		}
170748debafeSMikulas Patocka 
170848debafeSMikulas Patocka 		schedule();
170948debafeSMikulas Patocka 
171048debafeSMikulas Patocka 		continue;
171148debafeSMikulas Patocka 
171248debafeSMikulas Patocka pop_from_list:
171348debafeSMikulas Patocka 		list = wc->endio_list;
171448debafeSMikulas Patocka 		list.next->prev = list.prev->next = &list;
171548debafeSMikulas Patocka 		INIT_LIST_HEAD(&wc->endio_list);
171648debafeSMikulas Patocka 		raw_spin_unlock_irq(&wc->endio_list_lock);
171748debafeSMikulas Patocka 
171848debafeSMikulas Patocka 		if (!WC_MODE_FUA(wc))
171948debafeSMikulas Patocka 			writecache_disk_flush(wc, wc->dev);
172048debafeSMikulas Patocka 
172148debafeSMikulas Patocka 		wc_lock(wc);
172248debafeSMikulas Patocka 
172348debafeSMikulas Patocka 		if (WC_MODE_PMEM(wc)) {
172448debafeSMikulas Patocka 			__writecache_endio_pmem(wc, &list);
172548debafeSMikulas Patocka 		} else {
172648debafeSMikulas Patocka 			__writecache_endio_ssd(wc, &list);
172748debafeSMikulas Patocka 			writecache_wait_for_ios(wc, READ);
172848debafeSMikulas Patocka 		}
172948debafeSMikulas Patocka 
1730aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
173148debafeSMikulas Patocka 
173248debafeSMikulas Patocka 		wc_unlock(wc);
173348debafeSMikulas Patocka 	}
173448debafeSMikulas Patocka 
173548debafeSMikulas Patocka 	return 0;
173648debafeSMikulas Patocka }
173748debafeSMikulas Patocka 
1738620cbe40SMikulas Patocka static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e)
173948debafeSMikulas Patocka {
174048debafeSMikulas Patocka 	struct dm_writecache *wc = wb->wc;
174148debafeSMikulas Patocka 	unsigned block_size = wc->block_size;
174248debafeSMikulas Patocka 	void *address = memory_data(wc, e);
174348debafeSMikulas Patocka 
174448debafeSMikulas Patocka 	persistent_memory_flush_cache(address, block_size);
17454134455fSMikulas Patocka 
17464134455fSMikulas Patocka 	if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors))
17474134455fSMikulas Patocka 		return true;
17484134455fSMikulas Patocka 
174948debafeSMikulas Patocka 	return bio_add_page(&wb->bio, persistent_memory_page(address),
175048debafeSMikulas Patocka 			    block_size, persistent_memory_page_offset(address)) != 0;
175148debafeSMikulas Patocka }
175248debafeSMikulas Patocka 
175348debafeSMikulas Patocka struct writeback_list {
175448debafeSMikulas Patocka 	struct list_head list;
175548debafeSMikulas Patocka 	size_t size;
175648debafeSMikulas Patocka };
175748debafeSMikulas Patocka 
175848debafeSMikulas Patocka static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
175948debafeSMikulas Patocka {
176048debafeSMikulas Patocka 	if (unlikely(wc->max_writeback_jobs)) {
176148debafeSMikulas Patocka 		if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
176248debafeSMikulas Patocka 			wc_lock(wc);
176348debafeSMikulas Patocka 			while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
176448debafeSMikulas Patocka 				writecache_wait_on_freelist(wc);
176548debafeSMikulas Patocka 			wc_unlock(wc);
176648debafeSMikulas Patocka 		}
176748debafeSMikulas Patocka 	}
176848debafeSMikulas Patocka 	cond_resched();
176948debafeSMikulas Patocka }
177048debafeSMikulas Patocka 
177148debafeSMikulas Patocka static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
177248debafeSMikulas Patocka {
177348debafeSMikulas Patocka 	struct wc_entry *e, *f;
177448debafeSMikulas Patocka 	struct bio *bio;
177548debafeSMikulas Patocka 	struct writeback_struct *wb;
177648debafeSMikulas Patocka 	unsigned max_pages;
177748debafeSMikulas Patocka 
177848debafeSMikulas Patocka 	while (wbl->size) {
177948debafeSMikulas Patocka 		wbl->size--;
178048debafeSMikulas Patocka 		e = container_of(wbl->list.prev, struct wc_entry, lru);
178148debafeSMikulas Patocka 		list_del(&e->lru);
178248debafeSMikulas Patocka 
178348debafeSMikulas Patocka 		max_pages = e->wc_list_contiguous;
178448debafeSMikulas Patocka 
178548debafeSMikulas Patocka 		bio = bio_alloc_bioset(GFP_NOIO, max_pages, &wc->bio_set);
178648debafeSMikulas Patocka 		wb = container_of(bio, struct writeback_struct, bio);
178748debafeSMikulas Patocka 		wb->wc = wc;
178809f2d656SHuaisheng Ye 		bio->bi_end_io = writecache_writeback_endio;
178909f2d656SHuaisheng Ye 		bio_set_dev(bio, wc->dev->bdev);
179009f2d656SHuaisheng Ye 		bio->bi_iter.bi_sector = read_original_sector(wc, e);
179148debafeSMikulas Patocka 		if (max_pages <= WB_LIST_INLINE ||
179250a7d3baSKees Cook 		    unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
179348debafeSMikulas Patocka 							   GFP_NOIO | __GFP_NORETRY |
179448debafeSMikulas Patocka 							   __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
179548debafeSMikulas Patocka 			wb->wc_list = wb->wc_list_inline;
179648debafeSMikulas Patocka 			max_pages = WB_LIST_INLINE;
179748debafeSMikulas Patocka 		}
179848debafeSMikulas Patocka 
1799620cbe40SMikulas Patocka 		BUG_ON(!wc_add_block(wb, e));
180048debafeSMikulas Patocka 
180148debafeSMikulas Patocka 		wb->wc_list[0] = e;
180248debafeSMikulas Patocka 		wb->wc_list_n = 1;
180348debafeSMikulas Patocka 
180448debafeSMikulas Patocka 		while (wbl->size && wb->wc_list_n < max_pages) {
180548debafeSMikulas Patocka 			f = container_of(wbl->list.prev, struct wc_entry, lru);
180648debafeSMikulas Patocka 			if (read_original_sector(wc, f) !=
180748debafeSMikulas Patocka 			    read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
180848debafeSMikulas Patocka 				break;
1809620cbe40SMikulas Patocka 			if (!wc_add_block(wb, f))
181048debafeSMikulas Patocka 				break;
181148debafeSMikulas Patocka 			wbl->size--;
181248debafeSMikulas Patocka 			list_del(&f->lru);
181348debafeSMikulas Patocka 			wb->wc_list[wb->wc_list_n++] = f;
181448debafeSMikulas Patocka 			e = f;
181548debafeSMikulas Patocka 		}
181609f2d656SHuaisheng Ye 		bio_set_op_attrs(bio, REQ_OP_WRITE, WC_MODE_FUA(wc) * REQ_FUA);
181748debafeSMikulas Patocka 		if (writecache_has_error(wc)) {
181848debafeSMikulas Patocka 			bio->bi_status = BLK_STS_IOERR;
181909f2d656SHuaisheng Ye 			bio_endio(bio);
18204134455fSMikulas Patocka 		} else if (unlikely(!bio_sectors(bio))) {
18214134455fSMikulas Patocka 			bio->bi_status = BLK_STS_OK;
18224134455fSMikulas Patocka 			bio_endio(bio);
182348debafeSMikulas Patocka 		} else {
182409f2d656SHuaisheng Ye 			submit_bio(bio);
182548debafeSMikulas Patocka 		}
182648debafeSMikulas Patocka 
182748debafeSMikulas Patocka 		__writeback_throttle(wc, wbl);
182848debafeSMikulas Patocka 	}
182948debafeSMikulas Patocka }
183048debafeSMikulas Patocka 
183148debafeSMikulas Patocka static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
183248debafeSMikulas Patocka {
183348debafeSMikulas Patocka 	struct wc_entry *e, *f;
183448debafeSMikulas Patocka 	struct dm_io_region from, to;
183548debafeSMikulas Patocka 	struct copy_struct *c;
183648debafeSMikulas Patocka 
183748debafeSMikulas Patocka 	while (wbl->size) {
183848debafeSMikulas Patocka 		unsigned n_sectors;
183948debafeSMikulas Patocka 
184048debafeSMikulas Patocka 		wbl->size--;
184148debafeSMikulas Patocka 		e = container_of(wbl->list.prev, struct wc_entry, lru);
184248debafeSMikulas Patocka 		list_del(&e->lru);
184348debafeSMikulas Patocka 
184448debafeSMikulas Patocka 		n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
184548debafeSMikulas Patocka 
184648debafeSMikulas Patocka 		from.bdev = wc->ssd_dev->bdev;
184748debafeSMikulas Patocka 		from.sector = cache_sector(wc, e);
184848debafeSMikulas Patocka 		from.count = n_sectors;
184948debafeSMikulas Patocka 		to.bdev = wc->dev->bdev;
185048debafeSMikulas Patocka 		to.sector = read_original_sector(wc, e);
185148debafeSMikulas Patocka 		to.count = n_sectors;
185248debafeSMikulas Patocka 
185348debafeSMikulas Patocka 		c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
185448debafeSMikulas Patocka 		c->wc = wc;
185548debafeSMikulas Patocka 		c->e = e;
185648debafeSMikulas Patocka 		c->n_entries = e->wc_list_contiguous;
185748debafeSMikulas Patocka 
185848debafeSMikulas Patocka 		while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
185948debafeSMikulas Patocka 			wbl->size--;
186048debafeSMikulas Patocka 			f = container_of(wbl->list.prev, struct wc_entry, lru);
186148debafeSMikulas Patocka 			BUG_ON(f != e + 1);
186248debafeSMikulas Patocka 			list_del(&f->lru);
186348debafeSMikulas Patocka 			e = f;
186448debafeSMikulas Patocka 		}
186548debafeSMikulas Patocka 
18664134455fSMikulas Patocka 		if (unlikely(to.sector + to.count > wc->data_device_sectors)) {
18674134455fSMikulas Patocka 			if (to.sector >= wc->data_device_sectors) {
18684134455fSMikulas Patocka 				writecache_copy_endio(0, 0, c);
18694134455fSMikulas Patocka 				continue;
18704134455fSMikulas Patocka 			}
18714134455fSMikulas Patocka 			from.count = to.count = wc->data_device_sectors - to.sector;
18724134455fSMikulas Patocka 		}
18734134455fSMikulas Patocka 
187448debafeSMikulas Patocka 		dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
187548debafeSMikulas Patocka 
187648debafeSMikulas Patocka 		__writeback_throttle(wc, wbl);
187748debafeSMikulas Patocka 	}
187848debafeSMikulas Patocka }
187948debafeSMikulas Patocka 
188048debafeSMikulas Patocka static void writecache_writeback(struct work_struct *work)
188148debafeSMikulas Patocka {
188248debafeSMikulas Patocka 	struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
188348debafeSMikulas Patocka 	struct blk_plug plug;
18843f649ab7SKees Cook 	struct wc_entry *f, *g, *e = NULL;
188548debafeSMikulas Patocka 	struct rb_node *node, *next_node;
188648debafeSMikulas Patocka 	struct list_head skipped;
188748debafeSMikulas Patocka 	struct writeback_list wbl;
188848debafeSMikulas Patocka 	unsigned long n_walked;
188948debafeSMikulas Patocka 
1890293128b1SMikulas Patocka 	if (!WC_MODE_PMEM(wc)) {
1891293128b1SMikulas Patocka 		/* Wait for any active kcopyd work on behalf of ssd writeback */
1892293128b1SMikulas Patocka 		dm_kcopyd_client_flush(wc->dm_kcopyd);
1893293128b1SMikulas Patocka 	}
1894293128b1SMikulas Patocka 
18955c0de3d7SMikulas Patocka 	if (likely(wc->pause != 0)) {
18965c0de3d7SMikulas Patocka 		while (1) {
18975c0de3d7SMikulas Patocka 			unsigned long idle;
18985c0de3d7SMikulas Patocka 			if (unlikely(wc->cleaner) || unlikely(wc->writeback_all) ||
18995c0de3d7SMikulas Patocka 			    unlikely(dm_suspended(wc->ti)))
19005c0de3d7SMikulas Patocka 				break;
19015c0de3d7SMikulas Patocka 			idle = dm_iot_idle_time(&wc->iot);
19025c0de3d7SMikulas Patocka 			if (idle >= wc->pause)
19035c0de3d7SMikulas Patocka 				break;
19045c0de3d7SMikulas Patocka 			idle = wc->pause - idle;
19055c0de3d7SMikulas Patocka 			if (idle > HZ)
19065c0de3d7SMikulas Patocka 				idle = HZ;
19075c0de3d7SMikulas Patocka 			schedule_timeout_idle(idle);
190895b88f4dSMikulas Patocka 		}
190995b88f4dSMikulas Patocka 	}
191095b88f4dSMikulas Patocka 
191148debafeSMikulas Patocka 	wc_lock(wc);
191248debafeSMikulas Patocka restart:
191348debafeSMikulas Patocka 	if (writecache_has_error(wc)) {
191448debafeSMikulas Patocka 		wc_unlock(wc);
191548debafeSMikulas Patocka 		return;
191648debafeSMikulas Patocka 	}
191748debafeSMikulas Patocka 
191848debafeSMikulas Patocka 	if (unlikely(wc->writeback_all)) {
191948debafeSMikulas Patocka 		if (writecache_wait_for_writeback(wc))
192048debafeSMikulas Patocka 			goto restart;
192148debafeSMikulas Patocka 	}
192248debafeSMikulas Patocka 
192348debafeSMikulas Patocka 	if (wc->overwrote_committed) {
192448debafeSMikulas Patocka 		writecache_wait_for_ios(wc, WRITE);
192548debafeSMikulas Patocka 	}
192648debafeSMikulas Patocka 
192748debafeSMikulas Patocka 	n_walked = 0;
192848debafeSMikulas Patocka 	INIT_LIST_HEAD(&skipped);
192948debafeSMikulas Patocka 	INIT_LIST_HEAD(&wbl.list);
193048debafeSMikulas Patocka 	wbl.size = 0;
193148debafeSMikulas Patocka 	while (!list_empty(&wc->lru) &&
193248debafeSMikulas Patocka 	       (wc->writeback_all ||
19333923d485SMikulas Patocka 		wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark ||
19343923d485SMikulas Patocka 		(jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >=
19353923d485SMikulas Patocka 		 wc->max_age - wc->max_age / MAX_AGE_DIV))) {
193648debafeSMikulas Patocka 
193748debafeSMikulas Patocka 		n_walked++;
193848debafeSMikulas Patocka 		if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1939af4f6cabSMikulas Patocka 		    likely(!wc->writeback_all)) {
1940af4f6cabSMikulas Patocka 			if (likely(!dm_suspended(wc->ti)))
194148debafeSMikulas Patocka 				queue_work(wc->writeback_wq, &wc->writeback_work);
194248debafeSMikulas Patocka 			break;
194348debafeSMikulas Patocka 		}
194448debafeSMikulas Patocka 
19455229b489SHuaisheng Ye 		if (unlikely(wc->writeback_all)) {
19465229b489SHuaisheng Ye 			if (unlikely(!e)) {
19475229b489SHuaisheng Ye 				writecache_flush(wc);
19485229b489SHuaisheng Ye 				e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
19495229b489SHuaisheng Ye 			} else
19505229b489SHuaisheng Ye 				e = g;
19515229b489SHuaisheng Ye 		} else
195248debafeSMikulas Patocka 			e = container_of(wc->lru.prev, struct wc_entry, lru);
195348debafeSMikulas Patocka 		BUG_ON(e->write_in_progress);
195448debafeSMikulas Patocka 		if (unlikely(!writecache_entry_is_committed(wc, e))) {
195548debafeSMikulas Patocka 			writecache_flush(wc);
195648debafeSMikulas Patocka 		}
195748debafeSMikulas Patocka 		node = rb_prev(&e->rb_node);
195848debafeSMikulas Patocka 		if (node) {
195948debafeSMikulas Patocka 			f = container_of(node, struct wc_entry, rb_node);
196048debafeSMikulas Patocka 			if (unlikely(read_original_sector(wc, f) ==
196148debafeSMikulas Patocka 				     read_original_sector(wc, e))) {
196248debafeSMikulas Patocka 				BUG_ON(!f->write_in_progress);
19638c77f1cbSBaokun Li 				list_move(&e->lru, &skipped);
196448debafeSMikulas Patocka 				cond_resched();
196548debafeSMikulas Patocka 				continue;
196648debafeSMikulas Patocka 			}
196748debafeSMikulas Patocka 		}
196848debafeSMikulas Patocka 		wc->writeback_size++;
19698c77f1cbSBaokun Li 		list_move(&e->lru, &wbl.list);
197048debafeSMikulas Patocka 		wbl.size++;
197148debafeSMikulas Patocka 		e->write_in_progress = true;
197248debafeSMikulas Patocka 		e->wc_list_contiguous = 1;
197348debafeSMikulas Patocka 
197448debafeSMikulas Patocka 		f = e;
197548debafeSMikulas Patocka 
197648debafeSMikulas Patocka 		while (1) {
197748debafeSMikulas Patocka 			next_node = rb_next(&f->rb_node);
197848debafeSMikulas Patocka 			if (unlikely(!next_node))
197948debafeSMikulas Patocka 				break;
198048debafeSMikulas Patocka 			g = container_of(next_node, struct wc_entry, rb_node);
198162421b38SHuaisheng Ye 			if (unlikely(read_original_sector(wc, g) ==
198262421b38SHuaisheng Ye 			    read_original_sector(wc, f))) {
198348debafeSMikulas Patocka 				f = g;
198448debafeSMikulas Patocka 				continue;
198548debafeSMikulas Patocka 			}
198648debafeSMikulas Patocka 			if (read_original_sector(wc, g) !=
198748debafeSMikulas Patocka 			    read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
198848debafeSMikulas Patocka 				break;
198948debafeSMikulas Patocka 			if (unlikely(g->write_in_progress))
199048debafeSMikulas Patocka 				break;
199148debafeSMikulas Patocka 			if (unlikely(!writecache_entry_is_committed(wc, g)))
199248debafeSMikulas Patocka 				break;
199348debafeSMikulas Patocka 
199448debafeSMikulas Patocka 			if (!WC_MODE_PMEM(wc)) {
199548debafeSMikulas Patocka 				if (g != f + 1)
199648debafeSMikulas Patocka 					break;
199748debafeSMikulas Patocka 			}
199848debafeSMikulas Patocka 
199948debafeSMikulas Patocka 			n_walked++;
200048debafeSMikulas Patocka 			//if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
200148debafeSMikulas Patocka 			//	break;
200248debafeSMikulas Patocka 
200348debafeSMikulas Patocka 			wc->writeback_size++;
20048c77f1cbSBaokun Li 			list_move(&g->lru, &wbl.list);
200548debafeSMikulas Patocka 			wbl.size++;
200648debafeSMikulas Patocka 			g->write_in_progress = true;
2007a8affc03SChristoph Hellwig 			g->wc_list_contiguous = BIO_MAX_VECS;
200848debafeSMikulas Patocka 			f = g;
200948debafeSMikulas Patocka 			e->wc_list_contiguous++;
2010a8affc03SChristoph Hellwig 			if (unlikely(e->wc_list_contiguous == BIO_MAX_VECS)) {
20115229b489SHuaisheng Ye 				if (unlikely(wc->writeback_all)) {
20125229b489SHuaisheng Ye 					next_node = rb_next(&f->rb_node);
20135229b489SHuaisheng Ye 					if (likely(next_node))
20145229b489SHuaisheng Ye 						g = container_of(next_node, struct wc_entry, rb_node);
20155229b489SHuaisheng Ye 				}
201648debafeSMikulas Patocka 				break;
201748debafeSMikulas Patocka 			}
20185229b489SHuaisheng Ye 		}
201948debafeSMikulas Patocka 		cond_resched();
202048debafeSMikulas Patocka 	}
202148debafeSMikulas Patocka 
202248debafeSMikulas Patocka 	if (!list_empty(&skipped)) {
202348debafeSMikulas Patocka 		list_splice_tail(&skipped, &wc->lru);
202448debafeSMikulas Patocka 		/*
202548debafeSMikulas Patocka 		 * If we didn't do any progress, we must wait until some
202648debafeSMikulas Patocka 		 * writeback finishes to avoid burning CPU in a loop
202748debafeSMikulas Patocka 		 */
202848debafeSMikulas Patocka 		if (unlikely(!wbl.size))
202948debafeSMikulas Patocka 			writecache_wait_for_writeback(wc);
203048debafeSMikulas Patocka 	}
203148debafeSMikulas Patocka 
203248debafeSMikulas Patocka 	wc_unlock(wc);
203348debafeSMikulas Patocka 
203448debafeSMikulas Patocka 	blk_start_plug(&plug);
203548debafeSMikulas Patocka 
203648debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
203748debafeSMikulas Patocka 		__writecache_writeback_pmem(wc, &wbl);
203848debafeSMikulas Patocka 	else
203948debafeSMikulas Patocka 		__writecache_writeback_ssd(wc, &wbl);
204048debafeSMikulas Patocka 
204148debafeSMikulas Patocka 	blk_finish_plug(&plug);
204248debafeSMikulas Patocka 
204348debafeSMikulas Patocka 	if (unlikely(wc->writeback_all)) {
204448debafeSMikulas Patocka 		wc_lock(wc);
204548debafeSMikulas Patocka 		while (writecache_wait_for_writeback(wc));
204648debafeSMikulas Patocka 		wc_unlock(wc);
204748debafeSMikulas Patocka 	}
204848debafeSMikulas Patocka }
204948debafeSMikulas Patocka 
205048debafeSMikulas Patocka static int calculate_memory_size(uint64_t device_size, unsigned block_size,
205148debafeSMikulas Patocka 				 size_t *n_blocks_p, size_t *n_metadata_blocks_p)
205248debafeSMikulas Patocka {
205348debafeSMikulas Patocka 	uint64_t n_blocks, offset;
205448debafeSMikulas Patocka 	struct wc_entry e;
205548debafeSMikulas Patocka 
205648debafeSMikulas Patocka 	n_blocks = device_size;
205748debafeSMikulas Patocka 	do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
205848debafeSMikulas Patocka 
205948debafeSMikulas Patocka 	while (1) {
206048debafeSMikulas Patocka 		if (!n_blocks)
206148debafeSMikulas Patocka 			return -ENOSPC;
206248debafeSMikulas Patocka 		/* Verify the following entries[n_blocks] won't overflow */
206348debafeSMikulas Patocka 		if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
206448debafeSMikulas Patocka 				 sizeof(struct wc_memory_entry)))
206548debafeSMikulas Patocka 			return -EFBIG;
206648debafeSMikulas Patocka 		offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
206748debafeSMikulas Patocka 		offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
206848debafeSMikulas Patocka 		if (offset + n_blocks * block_size <= device_size)
206948debafeSMikulas Patocka 			break;
207048debafeSMikulas Patocka 		n_blocks--;
207148debafeSMikulas Patocka 	}
207248debafeSMikulas Patocka 
207348debafeSMikulas Patocka 	/* check if the bit field overflows */
207448debafeSMikulas Patocka 	e.index = n_blocks;
207548debafeSMikulas Patocka 	if (e.index != n_blocks)
207648debafeSMikulas Patocka 		return -EFBIG;
207748debafeSMikulas Patocka 
207848debafeSMikulas Patocka 	if (n_blocks_p)
207948debafeSMikulas Patocka 		*n_blocks_p = n_blocks;
208048debafeSMikulas Patocka 	if (n_metadata_blocks_p)
208148debafeSMikulas Patocka 		*n_metadata_blocks_p = offset >> __ffs(block_size);
208248debafeSMikulas Patocka 	return 0;
208348debafeSMikulas Patocka }
208448debafeSMikulas Patocka 
208548debafeSMikulas Patocka static int init_memory(struct dm_writecache *wc)
208648debafeSMikulas Patocka {
208748debafeSMikulas Patocka 	size_t b;
208848debafeSMikulas Patocka 	int r;
208948debafeSMikulas Patocka 
209048debafeSMikulas Patocka 	r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
209148debafeSMikulas Patocka 	if (r)
209248debafeSMikulas Patocka 		return r;
209348debafeSMikulas Patocka 
209448debafeSMikulas Patocka 	r = writecache_alloc_entries(wc);
209548debafeSMikulas Patocka 	if (r)
209648debafeSMikulas Patocka 		return r;
209748debafeSMikulas Patocka 
209848debafeSMikulas Patocka 	for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
209948debafeSMikulas Patocka 		pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
210048debafeSMikulas Patocka 	pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
210148debafeSMikulas Patocka 	pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
210248debafeSMikulas Patocka 	pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
210348debafeSMikulas Patocka 	pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
210448debafeSMikulas Patocka 
21051edaa447SMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
210648debafeSMikulas Patocka 		write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
21071edaa447SMikulas Patocka 		cond_resched();
21081edaa447SMikulas Patocka 	}
210948debafeSMikulas Patocka 
211048debafeSMikulas Patocka 	writecache_flush_all_metadata(wc);
2111aa950920SMikulas Patocka 	writecache_commit_flushed(wc, false);
211248debafeSMikulas Patocka 	pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
211348debafeSMikulas Patocka 	writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
2114aa950920SMikulas Patocka 	writecache_commit_flushed(wc, false);
211548debafeSMikulas Patocka 
211648debafeSMikulas Patocka 	return 0;
211748debafeSMikulas Patocka }
211848debafeSMikulas Patocka 
211948debafeSMikulas Patocka static void writecache_dtr(struct dm_target *ti)
212048debafeSMikulas Patocka {
212148debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
212248debafeSMikulas Patocka 
212348debafeSMikulas Patocka 	if (!wc)
212448debafeSMikulas Patocka 		return;
212548debafeSMikulas Patocka 
212648debafeSMikulas Patocka 	if (wc->endio_thread)
212748debafeSMikulas Patocka 		kthread_stop(wc->endio_thread);
212848debafeSMikulas Patocka 
212948debafeSMikulas Patocka 	if (wc->flush_thread)
213048debafeSMikulas Patocka 		kthread_stop(wc->flush_thread);
213148debafeSMikulas Patocka 
213248debafeSMikulas Patocka 	bioset_exit(&wc->bio_set);
213348debafeSMikulas Patocka 
213448debafeSMikulas Patocka 	mempool_exit(&wc->copy_pool);
213548debafeSMikulas Patocka 
213648debafeSMikulas Patocka 	if (wc->writeback_wq)
213748debafeSMikulas Patocka 		destroy_workqueue(wc->writeback_wq);
213848debafeSMikulas Patocka 
213948debafeSMikulas Patocka 	if (wc->dev)
214048debafeSMikulas Patocka 		dm_put_device(ti, wc->dev);
214148debafeSMikulas Patocka 
214248debafeSMikulas Patocka 	if (wc->ssd_dev)
214348debafeSMikulas Patocka 		dm_put_device(ti, wc->ssd_dev);
214448debafeSMikulas Patocka 
214548debafeSMikulas Patocka 	vfree(wc->entries);
214648debafeSMikulas Patocka 
214748debafeSMikulas Patocka 	if (wc->memory_map) {
214848debafeSMikulas Patocka 		if (WC_MODE_PMEM(wc))
214948debafeSMikulas Patocka 			persistent_memory_release(wc);
215048debafeSMikulas Patocka 		else
215148debafeSMikulas Patocka 			vfree(wc->memory_map);
215248debafeSMikulas Patocka 	}
215348debafeSMikulas Patocka 
215448debafeSMikulas Patocka 	if (wc->dm_kcopyd)
215548debafeSMikulas Patocka 		dm_kcopyd_client_destroy(wc->dm_kcopyd);
215648debafeSMikulas Patocka 
215748debafeSMikulas Patocka 	if (wc->dm_io)
215848debafeSMikulas Patocka 		dm_io_client_destroy(wc->dm_io);
215948debafeSMikulas Patocka 
216048debafeSMikulas Patocka 	vfree(wc->dirty_bitmap);
216148debafeSMikulas Patocka 
216248debafeSMikulas Patocka 	kfree(wc);
216348debafeSMikulas Patocka }
216448debafeSMikulas Patocka 
216548debafeSMikulas Patocka static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
216648debafeSMikulas Patocka {
216748debafeSMikulas Patocka 	struct dm_writecache *wc;
216848debafeSMikulas Patocka 	struct dm_arg_set as;
216948debafeSMikulas Patocka 	const char *string;
217048debafeSMikulas Patocka 	unsigned opt_params;
217148debafeSMikulas Patocka 	size_t offset, data_size;
217248debafeSMikulas Patocka 	int i, r;
217348debafeSMikulas Patocka 	char dummy;
217448debafeSMikulas Patocka 	int high_wm_percent = HIGH_WATERMARK;
217548debafeSMikulas Patocka 	int low_wm_percent = LOW_WATERMARK;
217648debafeSMikulas Patocka 	uint64_t x;
217748debafeSMikulas Patocka 	struct wc_memory_superblock s;
217848debafeSMikulas Patocka 
217948debafeSMikulas Patocka 	static struct dm_arg _args[] = {
21805c0de3d7SMikulas Patocka 		{0, 18, "Invalid number of feature args"},
218148debafeSMikulas Patocka 	};
218248debafeSMikulas Patocka 
218348debafeSMikulas Patocka 	as.argc = argc;
218448debafeSMikulas Patocka 	as.argv = argv;
218548debafeSMikulas Patocka 
218648debafeSMikulas Patocka 	wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
218748debafeSMikulas Patocka 	if (!wc) {
218848debafeSMikulas Patocka 		ti->error = "Cannot allocate writecache structure";
218948debafeSMikulas Patocka 		r = -ENOMEM;
219048debafeSMikulas Patocka 		goto bad;
219148debafeSMikulas Patocka 	}
219248debafeSMikulas Patocka 	ti->private = wc;
219348debafeSMikulas Patocka 	wc->ti = ti;
219448debafeSMikulas Patocka 
219548debafeSMikulas Patocka 	mutex_init(&wc->lock);
21963923d485SMikulas Patocka 	wc->max_age = MAX_AGE_UNSPECIFIED;
219748debafeSMikulas Patocka 	writecache_poison_lists(wc);
219848debafeSMikulas Patocka 	init_waitqueue_head(&wc->freelist_wait);
219948debafeSMikulas Patocka 	timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
22003923d485SMikulas Patocka 	timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0);
220148debafeSMikulas Patocka 
220248debafeSMikulas Patocka 	for (i = 0; i < 2; i++) {
220348debafeSMikulas Patocka 		atomic_set(&wc->bio_in_progress[i], 0);
220448debafeSMikulas Patocka 		init_waitqueue_head(&wc->bio_in_progress_wait[i]);
220548debafeSMikulas Patocka 	}
220648debafeSMikulas Patocka 
220748debafeSMikulas Patocka 	wc->dm_io = dm_io_client_create();
220848debafeSMikulas Patocka 	if (IS_ERR(wc->dm_io)) {
220948debafeSMikulas Patocka 		r = PTR_ERR(wc->dm_io);
221048debafeSMikulas Patocka 		ti->error = "Unable to allocate dm-io client";
221148debafeSMikulas Patocka 		wc->dm_io = NULL;
221248debafeSMikulas Patocka 		goto bad;
221348debafeSMikulas Patocka 	}
221448debafeSMikulas Patocka 
2215f87e033bSHuaisheng Ye 	wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
221648debafeSMikulas Patocka 	if (!wc->writeback_wq) {
221748debafeSMikulas Patocka 		r = -ENOMEM;
221848debafeSMikulas Patocka 		ti->error = "Could not allocate writeback workqueue";
221948debafeSMikulas Patocka 		goto bad;
222048debafeSMikulas Patocka 	}
222148debafeSMikulas Patocka 	INIT_WORK(&wc->writeback_work, writecache_writeback);
222248debafeSMikulas Patocka 	INIT_WORK(&wc->flush_work, writecache_flush_work);
222348debafeSMikulas Patocka 
222495b88f4dSMikulas Patocka 	dm_iot_init(&wc->iot);
222595b88f4dSMikulas Patocka 
222648debafeSMikulas Patocka 	raw_spin_lock_init(&wc->endio_list_lock);
222748debafeSMikulas Patocka 	INIT_LIST_HEAD(&wc->endio_list);
222848debafeSMikulas Patocka 	wc->endio_thread = kthread_create(writecache_endio_thread, wc, "writecache_endio");
222948debafeSMikulas Patocka 	if (IS_ERR(wc->endio_thread)) {
223048debafeSMikulas Patocka 		r = PTR_ERR(wc->endio_thread);
223148debafeSMikulas Patocka 		wc->endio_thread = NULL;
223248debafeSMikulas Patocka 		ti->error = "Couldn't spawn endio thread";
223348debafeSMikulas Patocka 		goto bad;
223448debafeSMikulas Patocka 	}
223548debafeSMikulas Patocka 	wake_up_process(wc->endio_thread);
223648debafeSMikulas Patocka 
223748debafeSMikulas Patocka 	/*
223848debafeSMikulas Patocka 	 * Parse the mode (pmem or ssd)
223948debafeSMikulas Patocka 	 */
224048debafeSMikulas Patocka 	string = dm_shift_arg(&as);
224148debafeSMikulas Patocka 	if (!string)
224248debafeSMikulas Patocka 		goto bad_arguments;
224348debafeSMikulas Patocka 
224448debafeSMikulas Patocka 	if (!strcasecmp(string, "s")) {
224548debafeSMikulas Patocka 		wc->pmem_mode = false;
224648debafeSMikulas Patocka 	} else if (!strcasecmp(string, "p")) {
224748debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM
224848debafeSMikulas Patocka 		wc->pmem_mode = true;
224948debafeSMikulas Patocka 		wc->writeback_fua = true;
225048debafeSMikulas Patocka #else
225148debafeSMikulas Patocka 		/*
225248debafeSMikulas Patocka 		 * If the architecture doesn't support persistent memory or
225348debafeSMikulas Patocka 		 * the kernel doesn't support any DAX drivers, this driver can
225448debafeSMikulas Patocka 		 * only be used in SSD-only mode.
225548debafeSMikulas Patocka 		 */
225648debafeSMikulas Patocka 		r = -EOPNOTSUPP;
225748debafeSMikulas Patocka 		ti->error = "Persistent memory or DAX not supported on this system";
225848debafeSMikulas Patocka 		goto bad;
225948debafeSMikulas Patocka #endif
226048debafeSMikulas Patocka 	} else {
226148debafeSMikulas Patocka 		goto bad_arguments;
226248debafeSMikulas Patocka 	}
226348debafeSMikulas Patocka 
226448debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc)) {
226548debafeSMikulas Patocka 		r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
226648debafeSMikulas Patocka 				offsetof(struct writeback_struct, bio),
226748debafeSMikulas Patocka 				BIOSET_NEED_BVECS);
226848debafeSMikulas Patocka 		if (r) {
226948debafeSMikulas Patocka 			ti->error = "Could not allocate bio set";
227048debafeSMikulas Patocka 			goto bad;
227148debafeSMikulas Patocka 		}
227248debafeSMikulas Patocka 	} else {
22735c0de3d7SMikulas Patocka 		wc->pause = PAUSE_WRITEBACK;
227448debafeSMikulas Patocka 		r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
227548debafeSMikulas Patocka 		if (r) {
227648debafeSMikulas Patocka 			ti->error = "Could not allocate mempool";
227748debafeSMikulas Patocka 			goto bad;
227848debafeSMikulas Patocka 		}
227948debafeSMikulas Patocka 	}
228048debafeSMikulas Patocka 
228148debafeSMikulas Patocka 	/*
228248debafeSMikulas Patocka 	 * Parse the origin data device
228348debafeSMikulas Patocka 	 */
228448debafeSMikulas Patocka 	string = dm_shift_arg(&as);
228548debafeSMikulas Patocka 	if (!string)
228648debafeSMikulas Patocka 		goto bad_arguments;
228748debafeSMikulas Patocka 	r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
228848debafeSMikulas Patocka 	if (r) {
228948debafeSMikulas Patocka 		ti->error = "Origin data device lookup failed";
229048debafeSMikulas Patocka 		goto bad;
229148debafeSMikulas Patocka 	}
229248debafeSMikulas Patocka 
229348debafeSMikulas Patocka 	/*
229448debafeSMikulas Patocka 	 * Parse cache data device (be it pmem or ssd)
229548debafeSMikulas Patocka 	 */
229648debafeSMikulas Patocka 	string = dm_shift_arg(&as);
229748debafeSMikulas Patocka 	if (!string)
229848debafeSMikulas Patocka 		goto bad_arguments;
229948debafeSMikulas Patocka 
230048debafeSMikulas Patocka 	r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
230148debafeSMikulas Patocka 	if (r) {
230248debafeSMikulas Patocka 		ti->error = "Cache data device lookup failed";
230348debafeSMikulas Patocka 		goto bad;
230448debafeSMikulas Patocka 	}
230548debafeSMikulas Patocka 	wc->memory_map_size = i_size_read(wc->ssd_dev->bdev->bd_inode);
230648debafeSMikulas Patocka 
230748debafeSMikulas Patocka 	/*
230848debafeSMikulas Patocka 	 * Parse the cache block size
230948debafeSMikulas Patocka 	 */
231048debafeSMikulas Patocka 	string = dm_shift_arg(&as);
231148debafeSMikulas Patocka 	if (!string)
231248debafeSMikulas Patocka 		goto bad_arguments;
231348debafeSMikulas Patocka 	if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
231448debafeSMikulas Patocka 	    wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
231548debafeSMikulas Patocka 	    (wc->block_size & (wc->block_size - 1))) {
231648debafeSMikulas Patocka 		r = -EINVAL;
231748debafeSMikulas Patocka 		ti->error = "Invalid block size";
231848debafeSMikulas Patocka 		goto bad;
231948debafeSMikulas Patocka 	}
232031b22120SMikulas Patocka 	if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) ||
232131b22120SMikulas Patocka 	    wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) {
232231b22120SMikulas Patocka 		r = -EINVAL;
232331b22120SMikulas Patocka 		ti->error = "Block size is smaller than device logical block size";
232431b22120SMikulas Patocka 		goto bad;
232531b22120SMikulas Patocka 	}
232648debafeSMikulas Patocka 	wc->block_size_bits = __ffs(wc->block_size);
232748debafeSMikulas Patocka 
232848debafeSMikulas Patocka 	wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
232948debafeSMikulas Patocka 	wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
233048debafeSMikulas Patocka 	wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
233148debafeSMikulas Patocka 
233248debafeSMikulas Patocka 	/*
233348debafeSMikulas Patocka 	 * Parse optional arguments
233448debafeSMikulas Patocka 	 */
233548debafeSMikulas Patocka 	r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
233648debafeSMikulas Patocka 	if (r)
233748debafeSMikulas Patocka 		goto bad;
233848debafeSMikulas Patocka 
233948debafeSMikulas Patocka 	while (opt_params) {
234048debafeSMikulas Patocka 		string = dm_shift_arg(&as), opt_params--;
2341d284f824SMikulas Patocka 		if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2342d284f824SMikulas Patocka 			unsigned long long start_sector;
2343d284f824SMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
2344d284f824SMikulas Patocka 			if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2345d284f824SMikulas Patocka 				goto invalid_optional;
2346d284f824SMikulas Patocka 			wc->start_sector = start_sector;
2347054bee16SMikulas Patocka 			wc->start_sector_set = true;
2348d284f824SMikulas Patocka 			if (wc->start_sector != start_sector ||
2349d284f824SMikulas Patocka 			    wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2350d284f824SMikulas Patocka 				goto invalid_optional;
2351d284f824SMikulas Patocka 		} else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
235248debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
235348debafeSMikulas Patocka 			if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
235448debafeSMikulas Patocka 				goto invalid_optional;
235548debafeSMikulas Patocka 			if (high_wm_percent < 0 || high_wm_percent > 100)
235648debafeSMikulas Patocka 				goto invalid_optional;
2357054bee16SMikulas Patocka 			wc->high_wm_percent_value = high_wm_percent;
235848debafeSMikulas Patocka 			wc->high_wm_percent_set = true;
235948debafeSMikulas Patocka 		} else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
236048debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
236148debafeSMikulas Patocka 			if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
236248debafeSMikulas Patocka 				goto invalid_optional;
236348debafeSMikulas Patocka 			if (low_wm_percent < 0 || low_wm_percent > 100)
236448debafeSMikulas Patocka 				goto invalid_optional;
2365054bee16SMikulas Patocka 			wc->low_wm_percent_value = low_wm_percent;
236648debafeSMikulas Patocka 			wc->low_wm_percent_set = true;
236748debafeSMikulas Patocka 		} else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
236848debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
236948debafeSMikulas Patocka 			if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
237048debafeSMikulas Patocka 				goto invalid_optional;
237148debafeSMikulas Patocka 			wc->max_writeback_jobs_set = true;
237248debafeSMikulas Patocka 		} else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
237348debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
237448debafeSMikulas Patocka 			if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
237548debafeSMikulas Patocka 				goto invalid_optional;
237648debafeSMikulas Patocka 			wc->autocommit_blocks_set = true;
237748debafeSMikulas Patocka 		} else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
237848debafeSMikulas Patocka 			unsigned autocommit_msecs;
237948debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
238048debafeSMikulas Patocka 			if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
238148debafeSMikulas Patocka 				goto invalid_optional;
238248debafeSMikulas Patocka 			if (autocommit_msecs > 3600000)
238348debafeSMikulas Patocka 				goto invalid_optional;
238448debafeSMikulas Patocka 			wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2385054bee16SMikulas Patocka 			wc->autocommit_time_value = autocommit_msecs;
238648debafeSMikulas Patocka 			wc->autocommit_time_set = true;
23873923d485SMikulas Patocka 		} else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
23883923d485SMikulas Patocka 			unsigned max_age_msecs;
23893923d485SMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
23903923d485SMikulas Patocka 			if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
23913923d485SMikulas Patocka 				goto invalid_optional;
23923923d485SMikulas Patocka 			if (max_age_msecs > 86400000)
23933923d485SMikulas Patocka 				goto invalid_optional;
23943923d485SMikulas Patocka 			wc->max_age = msecs_to_jiffies(max_age_msecs);
2395054bee16SMikulas Patocka 			wc->max_age_set = true;
2396054bee16SMikulas Patocka 			wc->max_age_value = max_age_msecs;
239793de44ebSMikulas Patocka 		} else if (!strcasecmp(string, "cleaner")) {
2398054bee16SMikulas Patocka 			wc->cleaner_set = true;
239993de44ebSMikulas Patocka 			wc->cleaner = true;
240048debafeSMikulas Patocka 		} else if (!strcasecmp(string, "fua")) {
240148debafeSMikulas Patocka 			if (WC_MODE_PMEM(wc)) {
240248debafeSMikulas Patocka 				wc->writeback_fua = true;
240348debafeSMikulas Patocka 				wc->writeback_fua_set = true;
240448debafeSMikulas Patocka 			} else goto invalid_optional;
240548debafeSMikulas Patocka 		} else if (!strcasecmp(string, "nofua")) {
240648debafeSMikulas Patocka 			if (WC_MODE_PMEM(wc)) {
240748debafeSMikulas Patocka 				wc->writeback_fua = false;
240848debafeSMikulas Patocka 				wc->writeback_fua_set = true;
240948debafeSMikulas Patocka 			} else goto invalid_optional;
2410611c3e16SMikulas Patocka 		} else if (!strcasecmp(string, "metadata_only")) {
2411611c3e16SMikulas Patocka 			wc->metadata_only = true;
24125c0de3d7SMikulas Patocka 		} else if (!strcasecmp(string, "pause_writeback") && opt_params >= 1) {
24135c0de3d7SMikulas Patocka 			unsigned pause_msecs;
24145c0de3d7SMikulas Patocka 			if (WC_MODE_PMEM(wc))
24155c0de3d7SMikulas Patocka 				goto invalid_optional;
24165c0de3d7SMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
24175c0de3d7SMikulas Patocka 			if (sscanf(string, "%u%c", &pause_msecs, &dummy) != 1)
24185c0de3d7SMikulas Patocka 				goto invalid_optional;
24195c0de3d7SMikulas Patocka 			if (pause_msecs > 60000)
24205c0de3d7SMikulas Patocka 				goto invalid_optional;
24215c0de3d7SMikulas Patocka 			wc->pause = msecs_to_jiffies(pause_msecs);
24225c0de3d7SMikulas Patocka 			wc->pause_set = true;
24235c0de3d7SMikulas Patocka 			wc->pause_value = pause_msecs;
242448debafeSMikulas Patocka 		} else {
242548debafeSMikulas Patocka invalid_optional:
242648debafeSMikulas Patocka 			r = -EINVAL;
242748debafeSMikulas Patocka 			ti->error = "Invalid optional argument";
242848debafeSMikulas Patocka 			goto bad;
242948debafeSMikulas Patocka 		}
243048debafeSMikulas Patocka 	}
243148debafeSMikulas Patocka 
243248debafeSMikulas Patocka 	if (high_wm_percent < low_wm_percent) {
243348debafeSMikulas Patocka 		r = -EINVAL;
243448debafeSMikulas Patocka 		ti->error = "High watermark must be greater than or equal to low watermark";
243548debafeSMikulas Patocka 		goto bad;
243648debafeSMikulas Patocka 	}
243748debafeSMikulas Patocka 
2438d284f824SMikulas Patocka 	if (WC_MODE_PMEM(wc)) {
2439a4662458SMichal Suchanek 		if (!dax_synchronous(wc->ssd_dev->dax_dev)) {
2440a4662458SMichal Suchanek 			r = -EOPNOTSUPP;
2441a4662458SMichal Suchanek 			ti->error = "Asynchronous persistent memory not supported as pmem cache";
2442a4662458SMichal Suchanek 			goto bad;
2443a4662458SMichal Suchanek 		}
2444a4662458SMichal Suchanek 
2445d284f824SMikulas Patocka 		r = persistent_memory_claim(wc);
2446d284f824SMikulas Patocka 		if (r) {
2447d284f824SMikulas Patocka 			ti->error = "Unable to map persistent memory for cache";
2448d284f824SMikulas Patocka 			goto bad;
2449d284f824SMikulas Patocka 		}
2450d284f824SMikulas Patocka 	} else {
245148debafeSMikulas Patocka 		size_t n_blocks, n_metadata_blocks;
245248debafeSMikulas Patocka 		uint64_t n_bitmap_bits;
245348debafeSMikulas Patocka 
2454d284f824SMikulas Patocka 		wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2455d284f824SMikulas Patocka 
245648debafeSMikulas Patocka 		bio_list_init(&wc->flush_list);
245748debafeSMikulas Patocka 		wc->flush_thread = kthread_create(writecache_flush_thread, wc, "dm_writecache_flush");
245848debafeSMikulas Patocka 		if (IS_ERR(wc->flush_thread)) {
245948debafeSMikulas Patocka 			r = PTR_ERR(wc->flush_thread);
246048debafeSMikulas Patocka 			wc->flush_thread = NULL;
2461e8ea141aSShenghui Wang 			ti->error = "Couldn't spawn flush thread";
246248debafeSMikulas Patocka 			goto bad;
246348debafeSMikulas Patocka 		}
246448debafeSMikulas Patocka 		wake_up_process(wc->flush_thread);
246548debafeSMikulas Patocka 
246648debafeSMikulas Patocka 		r = calculate_memory_size(wc->memory_map_size, wc->block_size,
246748debafeSMikulas Patocka 					  &n_blocks, &n_metadata_blocks);
246848debafeSMikulas Patocka 		if (r) {
246948debafeSMikulas Patocka 			ti->error = "Invalid device size";
247048debafeSMikulas Patocka 			goto bad;
247148debafeSMikulas Patocka 		}
247248debafeSMikulas Patocka 
247348debafeSMikulas Patocka 		n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
247448debafeSMikulas Patocka 				 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
247548debafeSMikulas Patocka 		/* this is limitation of test_bit functions */
247648debafeSMikulas Patocka 		if (n_bitmap_bits > 1U << 31) {
247748debafeSMikulas Patocka 			r = -EFBIG;
247848debafeSMikulas Patocka 			ti->error = "Invalid device size";
247948debafeSMikulas Patocka 			goto bad;
248048debafeSMikulas Patocka 		}
248148debafeSMikulas Patocka 
248248debafeSMikulas Patocka 		wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
248348debafeSMikulas Patocka 		if (!wc->memory_map) {
248448debafeSMikulas Patocka 			r = -ENOMEM;
248548debafeSMikulas Patocka 			ti->error = "Unable to allocate memory for metadata";
248648debafeSMikulas Patocka 			goto bad;
248748debafeSMikulas Patocka 		}
248848debafeSMikulas Patocka 
248948debafeSMikulas Patocka 		wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
249048debafeSMikulas Patocka 		if (IS_ERR(wc->dm_kcopyd)) {
249148debafeSMikulas Patocka 			r = PTR_ERR(wc->dm_kcopyd);
249248debafeSMikulas Patocka 			ti->error = "Unable to allocate dm-kcopyd client";
249348debafeSMikulas Patocka 			wc->dm_kcopyd = NULL;
249448debafeSMikulas Patocka 			goto bad;
249548debafeSMikulas Patocka 		}
249648debafeSMikulas Patocka 
249748debafeSMikulas Patocka 		wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
249848debafeSMikulas Patocka 		wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
249948debafeSMikulas Patocka 			BITS_PER_LONG * sizeof(unsigned long);
250048debafeSMikulas Patocka 		wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
250148debafeSMikulas Patocka 		if (!wc->dirty_bitmap) {
250248debafeSMikulas Patocka 			r = -ENOMEM;
250348debafeSMikulas Patocka 			ti->error = "Unable to allocate dirty bitmap";
250448debafeSMikulas Patocka 			goto bad;
250548debafeSMikulas Patocka 		}
250648debafeSMikulas Patocka 
250731b22120SMikulas Patocka 		r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT);
250848debafeSMikulas Patocka 		if (r) {
250931b22120SMikulas Patocka 			ti->error = "Unable to read first block of metadata";
251048debafeSMikulas Patocka 			goto bad;
251148debafeSMikulas Patocka 		}
251248debafeSMikulas Patocka 	}
251348debafeSMikulas Patocka 
2514ec6347bbSDan Williams 	r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock));
251548debafeSMikulas Patocka 	if (r) {
251648debafeSMikulas Patocka 		ti->error = "Hardware memory error when reading superblock";
251748debafeSMikulas Patocka 		goto bad;
251848debafeSMikulas Patocka 	}
251948debafeSMikulas Patocka 	if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
252048debafeSMikulas Patocka 		r = init_memory(wc);
252148debafeSMikulas Patocka 		if (r) {
252248debafeSMikulas Patocka 			ti->error = "Unable to initialize device";
252348debafeSMikulas Patocka 			goto bad;
252448debafeSMikulas Patocka 		}
2525ec6347bbSDan Williams 		r = copy_mc_to_kernel(&s, sb(wc),
2526ec6347bbSDan Williams 				      sizeof(struct wc_memory_superblock));
252748debafeSMikulas Patocka 		if (r) {
252848debafeSMikulas Patocka 			ti->error = "Hardware memory error when reading superblock";
252948debafeSMikulas Patocka 			goto bad;
253048debafeSMikulas Patocka 		}
253148debafeSMikulas Patocka 	}
253248debafeSMikulas Patocka 
253348debafeSMikulas Patocka 	if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
253448debafeSMikulas Patocka 		ti->error = "Invalid magic in the superblock";
253548debafeSMikulas Patocka 		r = -EINVAL;
253648debafeSMikulas Patocka 		goto bad;
253748debafeSMikulas Patocka 	}
253848debafeSMikulas Patocka 
253948debafeSMikulas Patocka 	if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
254048debafeSMikulas Patocka 		ti->error = "Invalid version in the superblock";
254148debafeSMikulas Patocka 		r = -EINVAL;
254248debafeSMikulas Patocka 		goto bad;
254348debafeSMikulas Patocka 	}
254448debafeSMikulas Patocka 
254548debafeSMikulas Patocka 	if (le32_to_cpu(s.block_size) != wc->block_size) {
254648debafeSMikulas Patocka 		ti->error = "Block size does not match superblock";
254748debafeSMikulas Patocka 		r = -EINVAL;
254848debafeSMikulas Patocka 		goto bad;
254948debafeSMikulas Patocka 	}
255048debafeSMikulas Patocka 
255148debafeSMikulas Patocka 	wc->n_blocks = le64_to_cpu(s.n_blocks);
255248debafeSMikulas Patocka 
255348debafeSMikulas Patocka 	offset = wc->n_blocks * sizeof(struct wc_memory_entry);
255448debafeSMikulas Patocka 	if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
255548debafeSMikulas Patocka overflow:
255648debafeSMikulas Patocka 		ti->error = "Overflow in size calculation";
255748debafeSMikulas Patocka 		r = -EINVAL;
255848debafeSMikulas Patocka 		goto bad;
255948debafeSMikulas Patocka 	}
256048debafeSMikulas Patocka 	offset += sizeof(struct wc_memory_superblock);
256148debafeSMikulas Patocka 	if (offset < sizeof(struct wc_memory_superblock))
256248debafeSMikulas Patocka 		goto overflow;
256348debafeSMikulas Patocka 	offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
256448debafeSMikulas Patocka 	data_size = wc->n_blocks * (size_t)wc->block_size;
256548debafeSMikulas Patocka 	if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
256648debafeSMikulas Patocka 	    (offset + data_size < offset))
256748debafeSMikulas Patocka 		goto overflow;
256848debafeSMikulas Patocka 	if (offset + data_size > wc->memory_map_size) {
256948debafeSMikulas Patocka 		ti->error = "Memory area is too small";
257048debafeSMikulas Patocka 		r = -EINVAL;
257148debafeSMikulas Patocka 		goto bad;
257248debafeSMikulas Patocka 	}
257348debafeSMikulas Patocka 
257448debafeSMikulas Patocka 	wc->metadata_sectors = offset >> SECTOR_SHIFT;
257548debafeSMikulas Patocka 	wc->block_start = (char *)sb(wc) + offset;
257648debafeSMikulas Patocka 
257748debafeSMikulas Patocka 	x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
257848debafeSMikulas Patocka 	x += 50;
257948debafeSMikulas Patocka 	do_div(x, 100);
258048debafeSMikulas Patocka 	wc->freelist_high_watermark = x;
258148debafeSMikulas Patocka 	x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
258248debafeSMikulas Patocka 	x += 50;
258348debafeSMikulas Patocka 	do_div(x, 100);
258448debafeSMikulas Patocka 	wc->freelist_low_watermark = x;
258548debafeSMikulas Patocka 
258693de44ebSMikulas Patocka 	if (wc->cleaner)
258793de44ebSMikulas Patocka 		activate_cleaner(wc);
258893de44ebSMikulas Patocka 
258948debafeSMikulas Patocka 	r = writecache_alloc_entries(wc);
259048debafeSMikulas Patocka 	if (r) {
259148debafeSMikulas Patocka 		ti->error = "Cannot allocate memory";
259248debafeSMikulas Patocka 		goto bad;
259348debafeSMikulas Patocka 	}
259448debafeSMikulas Patocka 
2595ee55b92aSMikulas Patocka 	ti->num_flush_bios = WC_MODE_PMEM(wc) ? 1 : 2;
259648debafeSMikulas Patocka 	ti->flush_supported = true;
259748debafeSMikulas Patocka 	ti->num_discard_bios = 1;
259848debafeSMikulas Patocka 
259948debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
260048debafeSMikulas Patocka 		persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
260148debafeSMikulas Patocka 
260248debafeSMikulas Patocka 	return 0;
260348debafeSMikulas Patocka 
260448debafeSMikulas Patocka bad_arguments:
260548debafeSMikulas Patocka 	r = -EINVAL;
260648debafeSMikulas Patocka 	ti->error = "Bad arguments";
260748debafeSMikulas Patocka bad:
260848debafeSMikulas Patocka 	writecache_dtr(ti);
260948debafeSMikulas Patocka 	return r;
261048debafeSMikulas Patocka }
261148debafeSMikulas Patocka 
261248debafeSMikulas Patocka static void writecache_status(struct dm_target *ti, status_type_t type,
261348debafeSMikulas Patocka 			      unsigned status_flags, char *result, unsigned maxlen)
261448debafeSMikulas Patocka {
261548debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
261648debafeSMikulas Patocka 	unsigned extra_args;
261748debafeSMikulas Patocka 	unsigned sz = 0;
261848debafeSMikulas Patocka 
261948debafeSMikulas Patocka 	switch (type) {
262048debafeSMikulas Patocka 	case STATUSTYPE_INFO:
262148debafeSMikulas Patocka 		DMEMIT("%ld %llu %llu %llu", writecache_has_error(wc),
262248debafeSMikulas Patocka 		       (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
262348debafeSMikulas Patocka 		       (unsigned long long)wc->writeback_size);
262448debafeSMikulas Patocka 		break;
262548debafeSMikulas Patocka 	case STATUSTYPE_TABLE:
262648debafeSMikulas Patocka 		DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
262748debafeSMikulas Patocka 				wc->dev->name, wc->ssd_dev->name, wc->block_size);
262848debafeSMikulas Patocka 		extra_args = 0;
2629054bee16SMikulas Patocka 		if (wc->start_sector_set)
26309ff07e7dSMikulas Patocka 			extra_args += 2;
2631054bee16SMikulas Patocka 		if (wc->high_wm_percent_set)
263248debafeSMikulas Patocka 			extra_args += 2;
2633054bee16SMikulas Patocka 		if (wc->low_wm_percent_set)
263448debafeSMikulas Patocka 			extra_args += 2;
263548debafeSMikulas Patocka 		if (wc->max_writeback_jobs_set)
263648debafeSMikulas Patocka 			extra_args += 2;
263748debafeSMikulas Patocka 		if (wc->autocommit_blocks_set)
263848debafeSMikulas Patocka 			extra_args += 2;
263948debafeSMikulas Patocka 		if (wc->autocommit_time_set)
264048debafeSMikulas Patocka 			extra_args += 2;
2641054bee16SMikulas Patocka 		if (wc->max_age_set)
2642e5d41cbcSMikulas Patocka 			extra_args += 2;
2643054bee16SMikulas Patocka 		if (wc->cleaner_set)
264493de44ebSMikulas Patocka 			extra_args++;
264548debafeSMikulas Patocka 		if (wc->writeback_fua_set)
264648debafeSMikulas Patocka 			extra_args++;
2647611c3e16SMikulas Patocka 		if (wc->metadata_only)
2648611c3e16SMikulas Patocka 			extra_args++;
26495c0de3d7SMikulas Patocka 		if (wc->pause_set)
26505c0de3d7SMikulas Patocka 			extra_args += 2;
265148debafeSMikulas Patocka 
265248debafeSMikulas Patocka 		DMEMIT("%u", extra_args);
2653054bee16SMikulas Patocka 		if (wc->start_sector_set)
26549ff07e7dSMikulas Patocka 			DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2655054bee16SMikulas Patocka 		if (wc->high_wm_percent_set)
2656054bee16SMikulas Patocka 			DMEMIT(" high_watermark %u", wc->high_wm_percent_value);
2657054bee16SMikulas Patocka 		if (wc->low_wm_percent_set)
2658054bee16SMikulas Patocka 			DMEMIT(" low_watermark %u", wc->low_wm_percent_value);
265948debafeSMikulas Patocka 		if (wc->max_writeback_jobs_set)
266048debafeSMikulas Patocka 			DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
266148debafeSMikulas Patocka 		if (wc->autocommit_blocks_set)
266248debafeSMikulas Patocka 			DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
266348debafeSMikulas Patocka 		if (wc->autocommit_time_set)
2664054bee16SMikulas Patocka 			DMEMIT(" autocommit_time %u", wc->autocommit_time_value);
2665054bee16SMikulas Patocka 		if (wc->max_age_set)
2666054bee16SMikulas Patocka 			DMEMIT(" max_age %u", wc->max_age_value);
2667054bee16SMikulas Patocka 		if (wc->cleaner_set)
266893de44ebSMikulas Patocka 			DMEMIT(" cleaner");
266948debafeSMikulas Patocka 		if (wc->writeback_fua_set)
267048debafeSMikulas Patocka 			DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2671611c3e16SMikulas Patocka 		if (wc->metadata_only)
2672611c3e16SMikulas Patocka 			DMEMIT(" metadata_only");
26735c0de3d7SMikulas Patocka 		if (wc->pause_set)
26745c0de3d7SMikulas Patocka 			DMEMIT(" pause_writeback %u", wc->pause_value);
267548debafeSMikulas Patocka 		break;
267648debafeSMikulas Patocka 	}
267748debafeSMikulas Patocka }
267848debafeSMikulas Patocka 
267948debafeSMikulas Patocka static struct target_type writecache_target = {
268048debafeSMikulas Patocka 	.name			= "writecache",
2681611c3e16SMikulas Patocka 	.version		= {1, 5, 0},
268248debafeSMikulas Patocka 	.module			= THIS_MODULE,
268348debafeSMikulas Patocka 	.ctr			= writecache_ctr,
268448debafeSMikulas Patocka 	.dtr			= writecache_dtr,
268548debafeSMikulas Patocka 	.status			= writecache_status,
268648debafeSMikulas Patocka 	.postsuspend		= writecache_suspend,
268748debafeSMikulas Patocka 	.resume			= writecache_resume,
268848debafeSMikulas Patocka 	.message		= writecache_message,
268948debafeSMikulas Patocka 	.map			= writecache_map,
269048debafeSMikulas Patocka 	.end_io			= writecache_end_io,
269148debafeSMikulas Patocka 	.iterate_devices	= writecache_iterate_devices,
269248debafeSMikulas Patocka 	.io_hints		= writecache_io_hints,
269348debafeSMikulas Patocka };
269448debafeSMikulas Patocka 
269548debafeSMikulas Patocka static int __init dm_writecache_init(void)
269648debafeSMikulas Patocka {
269748debafeSMikulas Patocka 	int r;
269848debafeSMikulas Patocka 
269948debafeSMikulas Patocka 	r = dm_register_target(&writecache_target);
270048debafeSMikulas Patocka 	if (r < 0) {
270148debafeSMikulas Patocka 		DMERR("register failed %d", r);
270248debafeSMikulas Patocka 		return r;
270348debafeSMikulas Patocka 	}
270448debafeSMikulas Patocka 
270548debafeSMikulas Patocka 	return 0;
270648debafeSMikulas Patocka }
270748debafeSMikulas Patocka 
270848debafeSMikulas Patocka static void __exit dm_writecache_exit(void)
270948debafeSMikulas Patocka {
271048debafeSMikulas Patocka 	dm_unregister_target(&writecache_target);
271148debafeSMikulas Patocka }
271248debafeSMikulas Patocka 
271348debafeSMikulas Patocka module_init(dm_writecache_init);
271448debafeSMikulas Patocka module_exit(dm_writecache_exit);
271548debafeSMikulas Patocka 
271648debafeSMikulas Patocka MODULE_DESCRIPTION(DM_NAME " writecache target");
271748debafeSMikulas Patocka MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
271848debafeSMikulas Patocka MODULE_LICENSE("GPL");
2719