xref: /linux/drivers/md/dm-writecache.c (revision ca7dc242e358e46d963b32f9d9dd829785a9e957)
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
25*ca7dc242SMikulas Patocka #define MAX_WRITEBACK_JOBS		min(0x10000000 / PAGE_SIZE, totalram_pages() / 16)
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 
415d2a228bSChristoph Hellwig #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_FS_DAX)
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;
209e3a35d03SMikulas Patocka 
210e3a35d03SMikulas Patocka 	struct {
211e3a35d03SMikulas Patocka 		unsigned long long reads;
212e3a35d03SMikulas Patocka 		unsigned long long read_hits;
213e3a35d03SMikulas Patocka 		unsigned long long writes;
214e3a35d03SMikulas Patocka 		unsigned long long write_hits_uncommitted;
215e3a35d03SMikulas Patocka 		unsigned long long write_hits_committed;
216e3a35d03SMikulas Patocka 		unsigned long long writes_around;
217e3a35d03SMikulas Patocka 		unsigned long long writes_allocate;
218e3a35d03SMikulas Patocka 		unsigned long long writes_blocked_on_freelist;
219e3a35d03SMikulas Patocka 		unsigned long long flushes;
220e3a35d03SMikulas Patocka 		unsigned long long discards;
221e3a35d03SMikulas Patocka 	} stats;
22248debafeSMikulas Patocka };
22348debafeSMikulas Patocka 
22448debafeSMikulas Patocka #define WB_LIST_INLINE		16
22548debafeSMikulas Patocka 
22648debafeSMikulas Patocka struct writeback_struct {
22748debafeSMikulas Patocka 	struct list_head endio_entry;
22848debafeSMikulas Patocka 	struct dm_writecache *wc;
22948debafeSMikulas Patocka 	struct wc_entry **wc_list;
23048debafeSMikulas Patocka 	unsigned wc_list_n;
23148debafeSMikulas Patocka 	struct wc_entry *wc_list_inline[WB_LIST_INLINE];
23248debafeSMikulas Patocka 	struct bio bio;
23348debafeSMikulas Patocka };
23448debafeSMikulas Patocka 
23548debafeSMikulas Patocka struct copy_struct {
23648debafeSMikulas Patocka 	struct list_head endio_entry;
23748debafeSMikulas Patocka 	struct dm_writecache *wc;
23848debafeSMikulas Patocka 	struct wc_entry *e;
23948debafeSMikulas Patocka 	unsigned n_entries;
24048debafeSMikulas Patocka 	int error;
24148debafeSMikulas Patocka };
24248debafeSMikulas Patocka 
24348debafeSMikulas Patocka DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
24448debafeSMikulas Patocka 					    "A percentage of time allocated for data copying");
24548debafeSMikulas Patocka 
24648debafeSMikulas Patocka static void wc_lock(struct dm_writecache *wc)
24748debafeSMikulas Patocka {
24848debafeSMikulas Patocka 	mutex_lock(&wc->lock);
24948debafeSMikulas Patocka }
25048debafeSMikulas Patocka 
25148debafeSMikulas Patocka static void wc_unlock(struct dm_writecache *wc)
25248debafeSMikulas Patocka {
25348debafeSMikulas Patocka 	mutex_unlock(&wc->lock);
25448debafeSMikulas Patocka }
25548debafeSMikulas Patocka 
25648debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM
25748debafeSMikulas Patocka static int persistent_memory_claim(struct dm_writecache *wc)
25848debafeSMikulas Patocka {
25948debafeSMikulas Patocka 	int r;
26048debafeSMikulas Patocka 	loff_t s;
26148debafeSMikulas Patocka 	long p, da;
26248debafeSMikulas Patocka 	pfn_t pfn;
26348debafeSMikulas Patocka 	int id;
26448debafeSMikulas Patocka 	struct page **pages;
265f9e040efSMikulas Patocka 	sector_t offset;
26648debafeSMikulas Patocka 
26748debafeSMikulas Patocka 	wc->memory_vmapped = false;
26848debafeSMikulas Patocka 
26948debafeSMikulas Patocka 	s = wc->memory_map_size;
27048debafeSMikulas Patocka 	p = s >> PAGE_SHIFT;
27148debafeSMikulas Patocka 	if (!p) {
27248debafeSMikulas Patocka 		r = -EINVAL;
27348debafeSMikulas Patocka 		goto err1;
27448debafeSMikulas Patocka 	}
27548debafeSMikulas Patocka 	if (p != s >> PAGE_SHIFT) {
27648debafeSMikulas Patocka 		r = -EOVERFLOW;
27748debafeSMikulas Patocka 		goto err1;
27848debafeSMikulas Patocka 	}
27948debafeSMikulas Patocka 
280f9e040efSMikulas Patocka 	offset = get_start_sect(wc->ssd_dev->bdev);
281f9e040efSMikulas Patocka 	if (offset & (PAGE_SIZE / 512 - 1)) {
282f9e040efSMikulas Patocka 		r = -EINVAL;
283f9e040efSMikulas Patocka 		goto err1;
284f9e040efSMikulas Patocka 	}
285f9e040efSMikulas Patocka 	offset >>= PAGE_SHIFT - 9;
286f9e040efSMikulas Patocka 
28748debafeSMikulas Patocka 	id = dax_read_lock();
28848debafeSMikulas Patocka 
289e511c4a3SJane Chu 	da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, DAX_ACCESS,
290e511c4a3SJane Chu 			&wc->memory_map, &pfn);
29148debafeSMikulas Patocka 	if (da < 0) {
29248debafeSMikulas Patocka 		wc->memory_map = NULL;
29348debafeSMikulas Patocka 		r = da;
29448debafeSMikulas Patocka 		goto err2;
29548debafeSMikulas Patocka 	}
29648debafeSMikulas Patocka 	if (!pfn_t_has_page(pfn)) {
29748debafeSMikulas Patocka 		wc->memory_map = NULL;
29848debafeSMikulas Patocka 		r = -EOPNOTSUPP;
29948debafeSMikulas Patocka 		goto err2;
30048debafeSMikulas Patocka 	}
30148debafeSMikulas Patocka 	if (da != p) {
30248debafeSMikulas Patocka 		long i;
30348debafeSMikulas Patocka 		wc->memory_map = NULL;
30450a7d3baSKees Cook 		pages = kvmalloc_array(p, sizeof(struct page *), GFP_KERNEL);
30548debafeSMikulas Patocka 		if (!pages) {
30648debafeSMikulas Patocka 			r = -ENOMEM;
30748debafeSMikulas Patocka 			goto err2;
30848debafeSMikulas Patocka 		}
30948debafeSMikulas Patocka 		i = 0;
31048debafeSMikulas Patocka 		do {
31148debafeSMikulas Patocka 			long daa;
312e511c4a3SJane Chu 			daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i,
313e511c4a3SJane Chu 					p - i, DAX_ACCESS, NULL, &pfn);
31448debafeSMikulas Patocka 			if (daa <= 0) {
31548debafeSMikulas Patocka 				r = daa ? daa : -EINVAL;
31648debafeSMikulas Patocka 				goto err3;
31748debafeSMikulas Patocka 			}
31848debafeSMikulas Patocka 			if (!pfn_t_has_page(pfn)) {
31948debafeSMikulas Patocka 				r = -EOPNOTSUPP;
32048debafeSMikulas Patocka 				goto err3;
32148debafeSMikulas Patocka 			}
32248debafeSMikulas Patocka 			while (daa-- && i < p) {
32348debafeSMikulas Patocka 				pages[i++] = pfn_t_to_page(pfn);
32448debafeSMikulas Patocka 				pfn.val++;
325d35bd764SMikulas Patocka 				if (!(i & 15))
326d35bd764SMikulas Patocka 					cond_resched();
32748debafeSMikulas Patocka 			}
32848debafeSMikulas Patocka 		} while (i < p);
32948debafeSMikulas Patocka 		wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
33048debafeSMikulas Patocka 		if (!wc->memory_map) {
33148debafeSMikulas Patocka 			r = -ENOMEM;
33248debafeSMikulas Patocka 			goto err3;
33348debafeSMikulas Patocka 		}
33448debafeSMikulas Patocka 		kvfree(pages);
33548debafeSMikulas Patocka 		wc->memory_vmapped = true;
33648debafeSMikulas Patocka 	}
33748debafeSMikulas Patocka 
33848debafeSMikulas Patocka 	dax_read_unlock(id);
339d284f824SMikulas Patocka 
340d284f824SMikulas Patocka 	wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
341d284f824SMikulas Patocka 	wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
342d284f824SMikulas Patocka 
34348debafeSMikulas Patocka 	return 0;
34448debafeSMikulas Patocka err3:
34548debafeSMikulas Patocka 	kvfree(pages);
34648debafeSMikulas Patocka err2:
34748debafeSMikulas Patocka 	dax_read_unlock(id);
34848debafeSMikulas Patocka err1:
34948debafeSMikulas Patocka 	return r;
35048debafeSMikulas Patocka }
35148debafeSMikulas Patocka #else
35248debafeSMikulas Patocka static int persistent_memory_claim(struct dm_writecache *wc)
35348debafeSMikulas Patocka {
354857c4c0aSMike Snitzer 	return -EOPNOTSUPP;
35548debafeSMikulas Patocka }
35648debafeSMikulas Patocka #endif
35748debafeSMikulas Patocka 
35848debafeSMikulas Patocka static void persistent_memory_release(struct dm_writecache *wc)
35948debafeSMikulas Patocka {
36048debafeSMikulas Patocka 	if (wc->memory_vmapped)
361d284f824SMikulas Patocka 		vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
36248debafeSMikulas Patocka }
36348debafeSMikulas Patocka 
36448debafeSMikulas Patocka static struct page *persistent_memory_page(void *addr)
36548debafeSMikulas Patocka {
36648debafeSMikulas Patocka 	if (is_vmalloc_addr(addr))
36748debafeSMikulas Patocka 		return vmalloc_to_page(addr);
36848debafeSMikulas Patocka 	else
36948debafeSMikulas Patocka 		return virt_to_page(addr);
37048debafeSMikulas Patocka }
37148debafeSMikulas Patocka 
37248debafeSMikulas Patocka static unsigned persistent_memory_page_offset(void *addr)
37348debafeSMikulas Patocka {
37448debafeSMikulas Patocka 	return (unsigned long)addr & (PAGE_SIZE - 1);
37548debafeSMikulas Patocka }
37648debafeSMikulas Patocka 
37748debafeSMikulas Patocka static void persistent_memory_flush_cache(void *ptr, size_t size)
37848debafeSMikulas Patocka {
37948debafeSMikulas Patocka 	if (is_vmalloc_addr(ptr))
38048debafeSMikulas Patocka 		flush_kernel_vmap_range(ptr, size);
38148debafeSMikulas Patocka }
38248debafeSMikulas Patocka 
38348debafeSMikulas Patocka static void persistent_memory_invalidate_cache(void *ptr, size_t size)
38448debafeSMikulas Patocka {
38548debafeSMikulas Patocka 	if (is_vmalloc_addr(ptr))
38648debafeSMikulas Patocka 		invalidate_kernel_vmap_range(ptr, size);
38748debafeSMikulas Patocka }
38848debafeSMikulas Patocka 
38948debafeSMikulas Patocka static struct wc_memory_superblock *sb(struct dm_writecache *wc)
39048debafeSMikulas Patocka {
39148debafeSMikulas Patocka 	return wc->memory_map;
39248debafeSMikulas Patocka }
39348debafeSMikulas Patocka 
39448debafeSMikulas Patocka static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
39548debafeSMikulas Patocka {
39648debafeSMikulas Patocka 	return &sb(wc)->entries[e->index];
39748debafeSMikulas Patocka }
39848debafeSMikulas Patocka 
39948debafeSMikulas Patocka static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
40048debafeSMikulas Patocka {
40148debafeSMikulas Patocka 	return (char *)wc->block_start + (e->index << wc->block_size_bits);
40248debafeSMikulas Patocka }
40348debafeSMikulas Patocka 
40448debafeSMikulas Patocka static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
40548debafeSMikulas Patocka {
406d284f824SMikulas Patocka 	return wc->start_sector + wc->metadata_sectors +
40748debafeSMikulas Patocka 		((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
40848debafeSMikulas Patocka }
40948debafeSMikulas Patocka 
41048debafeSMikulas Patocka static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
41148debafeSMikulas Patocka {
41248debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
41348debafeSMikulas Patocka 	return e->original_sector;
41448debafeSMikulas Patocka #else
41548debafeSMikulas Patocka 	return le64_to_cpu(memory_entry(wc, e)->original_sector);
41648debafeSMikulas Patocka #endif
41748debafeSMikulas Patocka }
41848debafeSMikulas Patocka 
41948debafeSMikulas Patocka static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
42048debafeSMikulas Patocka {
42148debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
42248debafeSMikulas Patocka 	return e->seq_count;
42348debafeSMikulas Patocka #else
42448debafeSMikulas Patocka 	return le64_to_cpu(memory_entry(wc, e)->seq_count);
42548debafeSMikulas Patocka #endif
42648debafeSMikulas Patocka }
42748debafeSMikulas Patocka 
42848debafeSMikulas Patocka static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
42948debafeSMikulas Patocka {
43048debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
43148debafeSMikulas Patocka 	e->seq_count = -1;
43248debafeSMikulas Patocka #endif
43348debafeSMikulas Patocka 	pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
43448debafeSMikulas Patocka }
43548debafeSMikulas Patocka 
43648debafeSMikulas Patocka static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
43748debafeSMikulas Patocka 					    uint64_t original_sector, uint64_t seq_count)
43848debafeSMikulas Patocka {
43948debafeSMikulas Patocka 	struct wc_memory_entry me;
44048debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
44148debafeSMikulas Patocka 	e->original_sector = original_sector;
44248debafeSMikulas Patocka 	e->seq_count = seq_count;
44348debafeSMikulas Patocka #endif
44448debafeSMikulas Patocka 	me.original_sector = cpu_to_le64(original_sector);
44548debafeSMikulas Patocka 	me.seq_count = cpu_to_le64(seq_count);
44648debafeSMikulas Patocka 	pmem_assign(*memory_entry(wc, e), me);
44748debafeSMikulas Patocka }
44848debafeSMikulas Patocka 
44948debafeSMikulas Patocka #define writecache_error(wc, err, msg, arg...)				\
45048debafeSMikulas Patocka do {									\
45148debafeSMikulas Patocka 	if (!cmpxchg(&(wc)->error, 0, err))				\
45248debafeSMikulas Patocka 		DMERR(msg, ##arg);					\
45348debafeSMikulas Patocka 	wake_up(&(wc)->freelist_wait);					\
45448debafeSMikulas Patocka } while (0)
45548debafeSMikulas Patocka 
45648debafeSMikulas Patocka #define writecache_has_error(wc)	(unlikely(READ_ONCE((wc)->error)))
45748debafeSMikulas Patocka 
45848debafeSMikulas Patocka static void writecache_flush_all_metadata(struct dm_writecache *wc)
45948debafeSMikulas Patocka {
46048debafeSMikulas Patocka 	if (!WC_MODE_PMEM(wc))
46148debafeSMikulas Patocka 		memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
46248debafeSMikulas Patocka }
46348debafeSMikulas Patocka 
46448debafeSMikulas Patocka static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
46548debafeSMikulas Patocka {
46648debafeSMikulas Patocka 	if (!WC_MODE_PMEM(wc))
46748debafeSMikulas Patocka 		__set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
46848debafeSMikulas Patocka 			  wc->dirty_bitmap);
46948debafeSMikulas Patocka }
47048debafeSMikulas Patocka 
47148debafeSMikulas Patocka static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
47248debafeSMikulas Patocka 
47348debafeSMikulas Patocka struct io_notify {
47448debafeSMikulas Patocka 	struct dm_writecache *wc;
47548debafeSMikulas Patocka 	struct completion c;
47648debafeSMikulas Patocka 	atomic_t count;
47748debafeSMikulas Patocka };
47848debafeSMikulas Patocka 
47948debafeSMikulas Patocka static void writecache_notify_io(unsigned long error, void *context)
48048debafeSMikulas Patocka {
48148debafeSMikulas Patocka 	struct io_notify *endio = context;
48248debafeSMikulas Patocka 
48348debafeSMikulas Patocka 	if (unlikely(error != 0))
48448debafeSMikulas Patocka 		writecache_error(endio->wc, -EIO, "error writing metadata");
48548debafeSMikulas Patocka 	BUG_ON(atomic_read(&endio->count) <= 0);
48648debafeSMikulas Patocka 	if (atomic_dec_and_test(&endio->count))
48748debafeSMikulas Patocka 		complete(&endio->c);
48848debafeSMikulas Patocka }
48948debafeSMikulas Patocka 
490aa950920SMikulas Patocka static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
491aa950920SMikulas Patocka {
492aa950920SMikulas Patocka 	wait_event(wc->bio_in_progress_wait[direction],
493aa950920SMikulas Patocka 		   !atomic_read(&wc->bio_in_progress[direction]));
494aa950920SMikulas Patocka }
495aa950920SMikulas Patocka 
496aa950920SMikulas Patocka static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
49748debafeSMikulas Patocka {
49848debafeSMikulas Patocka 	struct dm_io_region region;
49948debafeSMikulas Patocka 	struct dm_io_request req;
50048debafeSMikulas Patocka 	struct io_notify endio = {
50148debafeSMikulas Patocka 		wc,
50248debafeSMikulas Patocka 		COMPLETION_INITIALIZER_ONSTACK(endio.c),
50348debafeSMikulas Patocka 		ATOMIC_INIT(1),
50448debafeSMikulas Patocka 	};
5051e1132eaSMikulas Patocka 	unsigned bitmap_bits = wc->dirty_bitmap_size * 8;
50648debafeSMikulas Patocka 	unsigned i = 0;
50748debafeSMikulas Patocka 
50848debafeSMikulas Patocka 	while (1) {
50948debafeSMikulas Patocka 		unsigned j;
51048debafeSMikulas Patocka 		i = find_next_bit(wc->dirty_bitmap, bitmap_bits, i);
51148debafeSMikulas Patocka 		if (unlikely(i == bitmap_bits))
51248debafeSMikulas Patocka 			break;
51348debafeSMikulas Patocka 		j = find_next_zero_bit(wc->dirty_bitmap, bitmap_bits, i);
51448debafeSMikulas Patocka 
51548debafeSMikulas Patocka 		region.bdev = wc->ssd_dev->bdev;
51648debafeSMikulas Patocka 		region.sector = (sector_t)i * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
51748debafeSMikulas Patocka 		region.count = (sector_t)(j - i) * (BITMAP_GRANULARITY >> SECTOR_SHIFT);
51848debafeSMikulas Patocka 
51948debafeSMikulas Patocka 		if (unlikely(region.sector >= wc->metadata_sectors))
52048debafeSMikulas Patocka 			break;
52148debafeSMikulas Patocka 		if (unlikely(region.sector + region.count > wc->metadata_sectors))
52248debafeSMikulas Patocka 			region.count = wc->metadata_sectors - region.sector;
52348debafeSMikulas Patocka 
524d284f824SMikulas Patocka 		region.sector += wc->start_sector;
52548debafeSMikulas Patocka 		atomic_inc(&endio.count);
52648debafeSMikulas Patocka 		req.bi_op = REQ_OP_WRITE;
52748debafeSMikulas Patocka 		req.bi_op_flags = REQ_SYNC;
52848debafeSMikulas Patocka 		req.mem.type = DM_IO_VMA;
52948debafeSMikulas Patocka 		req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
53048debafeSMikulas Patocka 		req.client = wc->dm_io;
53148debafeSMikulas Patocka 		req.notify.fn = writecache_notify_io;
53248debafeSMikulas Patocka 		req.notify.context = &endio;
53348debafeSMikulas Patocka 
53448debafeSMikulas Patocka 		/* writing via async dm-io (implied by notify.fn above) won't return an error */
53548debafeSMikulas Patocka 	        (void) dm_io(&req, 1, &region, NULL);
53648debafeSMikulas Patocka 		i = j;
53748debafeSMikulas Patocka 	}
53848debafeSMikulas Patocka 
53948debafeSMikulas Patocka 	writecache_notify_io(0, &endio);
54048debafeSMikulas Patocka 	wait_for_completion_io(&endio.c);
54148debafeSMikulas Patocka 
542aa950920SMikulas Patocka 	if (wait_for_ios)
543aa950920SMikulas Patocka 		writecache_wait_for_ios(wc, WRITE);
544aa950920SMikulas Patocka 
54548debafeSMikulas Patocka 	writecache_disk_flush(wc, wc->ssd_dev);
54648debafeSMikulas Patocka 
54748debafeSMikulas Patocka 	memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
54848debafeSMikulas Patocka }
54948debafeSMikulas Patocka 
550dc8a01aeSMikulas Patocka static void ssd_commit_superblock(struct dm_writecache *wc)
551dc8a01aeSMikulas Patocka {
552dc8a01aeSMikulas Patocka 	int r;
553dc8a01aeSMikulas Patocka 	struct dm_io_region region;
554dc8a01aeSMikulas Patocka 	struct dm_io_request req;
555dc8a01aeSMikulas Patocka 
556dc8a01aeSMikulas Patocka 	region.bdev = wc->ssd_dev->bdev;
557dc8a01aeSMikulas Patocka 	region.sector = 0;
558867de40cSMikulas Patocka 	region.count = max(4096U, wc->block_size) >> SECTOR_SHIFT;
559867de40cSMikulas Patocka 
560867de40cSMikulas Patocka 	if (unlikely(region.sector + region.count > wc->metadata_sectors))
561867de40cSMikulas Patocka 		region.count = wc->metadata_sectors - region.sector;
562867de40cSMikulas Patocka 
563dc8a01aeSMikulas Patocka 	region.sector += wc->start_sector;
564dc8a01aeSMikulas Patocka 
565dc8a01aeSMikulas Patocka 	req.bi_op = REQ_OP_WRITE;
566dc8a01aeSMikulas Patocka 	req.bi_op_flags = REQ_SYNC | REQ_FUA;
567dc8a01aeSMikulas Patocka 	req.mem.type = DM_IO_VMA;
568dc8a01aeSMikulas Patocka 	req.mem.ptr.vma = (char *)wc->memory_map;
569dc8a01aeSMikulas Patocka 	req.client = wc->dm_io;
570dc8a01aeSMikulas Patocka 	req.notify.fn = NULL;
571dc8a01aeSMikulas Patocka 	req.notify.context = NULL;
572dc8a01aeSMikulas Patocka 
573dc8a01aeSMikulas Patocka 	r = dm_io(&req, 1, &region, NULL);
574dc8a01aeSMikulas Patocka 	if (unlikely(r))
575dc8a01aeSMikulas Patocka 		writecache_error(wc, r, "error writing superblock");
576dc8a01aeSMikulas Patocka }
577dc8a01aeSMikulas Patocka 
578aa950920SMikulas Patocka static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
57948debafeSMikulas Patocka {
58048debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
5813e79f082SAneesh Kumar K.V 		pmem_wmb();
58248debafeSMikulas Patocka 	else
583aa950920SMikulas Patocka 		ssd_commit_flushed(wc, wait_for_ios);
58448debafeSMikulas Patocka }
58548debafeSMikulas Patocka 
58648debafeSMikulas Patocka static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
58748debafeSMikulas Patocka {
58848debafeSMikulas Patocka 	int r;
58948debafeSMikulas Patocka 	struct dm_io_region region;
59048debafeSMikulas Patocka 	struct dm_io_request req;
59148debafeSMikulas Patocka 
59248debafeSMikulas Patocka 	region.bdev = dev->bdev;
59348debafeSMikulas Patocka 	region.sector = 0;
59448debafeSMikulas Patocka 	region.count = 0;
59548debafeSMikulas Patocka 	req.bi_op = REQ_OP_WRITE;
59648debafeSMikulas Patocka 	req.bi_op_flags = REQ_PREFLUSH;
59748debafeSMikulas Patocka 	req.mem.type = DM_IO_KMEM;
59848debafeSMikulas Patocka 	req.mem.ptr.addr = NULL;
59948debafeSMikulas Patocka 	req.client = wc->dm_io;
60048debafeSMikulas Patocka 	req.notify.fn = NULL;
60148debafeSMikulas Patocka 
60248debafeSMikulas Patocka 	r = dm_io(&req, 1, &region, NULL);
60348debafeSMikulas Patocka 	if (unlikely(r))
60448debafeSMikulas Patocka 		writecache_error(wc, r, "error flushing metadata: %d", r);
60548debafeSMikulas Patocka }
60648debafeSMikulas Patocka 
60748debafeSMikulas Patocka #define WFE_RETURN_FOLLOWING	1
60848debafeSMikulas Patocka #define WFE_LOWEST_SEQ		2
60948debafeSMikulas Patocka 
61048debafeSMikulas Patocka static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
61148debafeSMikulas Patocka 					      uint64_t block, int flags)
61248debafeSMikulas Patocka {
61348debafeSMikulas Patocka 	struct wc_entry *e;
61448debafeSMikulas Patocka 	struct rb_node *node = wc->tree.rb_node;
61548debafeSMikulas Patocka 
61648debafeSMikulas Patocka 	if (unlikely(!node))
61748debafeSMikulas Patocka 		return NULL;
61848debafeSMikulas Patocka 
61948debafeSMikulas Patocka 	while (1) {
62048debafeSMikulas Patocka 		e = container_of(node, struct wc_entry, rb_node);
62148debafeSMikulas Patocka 		if (read_original_sector(wc, e) == block)
62248debafeSMikulas Patocka 			break;
623f8011d33SMikulas Patocka 
62448debafeSMikulas Patocka 		node = (read_original_sector(wc, e) >= block ?
62548debafeSMikulas Patocka 			e->rb_node.rb_left : e->rb_node.rb_right);
62648debafeSMikulas Patocka 		if (unlikely(!node)) {
627f8011d33SMikulas Patocka 			if (!(flags & WFE_RETURN_FOLLOWING))
62848debafeSMikulas Patocka 				return NULL;
62948debafeSMikulas Patocka 			if (read_original_sector(wc, e) >= block) {
630f8011d33SMikulas Patocka 				return e;
63148debafeSMikulas Patocka 			} else {
63248debafeSMikulas Patocka 				node = rb_next(&e->rb_node);
633f8011d33SMikulas Patocka 				if (unlikely(!node))
63448debafeSMikulas Patocka 					return NULL;
63548debafeSMikulas Patocka 				e = container_of(node, struct wc_entry, rb_node);
636f8011d33SMikulas Patocka 				return e;
63748debafeSMikulas Patocka 			}
63848debafeSMikulas Patocka 		}
63948debafeSMikulas Patocka 	}
64048debafeSMikulas Patocka 
64148debafeSMikulas Patocka 	while (1) {
64248debafeSMikulas Patocka 		struct wc_entry *e2;
64348debafeSMikulas Patocka 		if (flags & WFE_LOWEST_SEQ)
64448debafeSMikulas Patocka 			node = rb_prev(&e->rb_node);
64548debafeSMikulas Patocka 		else
64648debafeSMikulas Patocka 			node = rb_next(&e->rb_node);
64784420b1eSHuaisheng Ye 		if (unlikely(!node))
64848debafeSMikulas Patocka 			return e;
64948debafeSMikulas Patocka 		e2 = container_of(node, struct wc_entry, rb_node);
65048debafeSMikulas Patocka 		if (read_original_sector(wc, e2) != block)
65148debafeSMikulas Patocka 			return e;
65248debafeSMikulas Patocka 		e = e2;
65348debafeSMikulas Patocka 	}
65448debafeSMikulas Patocka }
65548debafeSMikulas Patocka 
65648debafeSMikulas Patocka static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
65748debafeSMikulas Patocka {
65848debafeSMikulas Patocka 	struct wc_entry *e;
65948debafeSMikulas Patocka 	struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
66048debafeSMikulas Patocka 
66148debafeSMikulas Patocka 	while (*node) {
66248debafeSMikulas Patocka 		e = container_of(*node, struct wc_entry, rb_node);
66348debafeSMikulas Patocka 		parent = &e->rb_node;
66448debafeSMikulas Patocka 		if (read_original_sector(wc, e) > read_original_sector(wc, ins))
66548debafeSMikulas Patocka 			node = &parent->rb_left;
66648debafeSMikulas Patocka 		else
66748debafeSMikulas Patocka 			node = &parent->rb_right;
66848debafeSMikulas Patocka 	}
66948debafeSMikulas Patocka 	rb_link_node(&ins->rb_node, parent, node);
67048debafeSMikulas Patocka 	rb_insert_color(&ins->rb_node, &wc->tree);
67148debafeSMikulas Patocka 	list_add(&ins->lru, &wc->lru);
6723923d485SMikulas Patocka 	ins->age = jiffies;
67348debafeSMikulas Patocka }
67448debafeSMikulas Patocka 
67548debafeSMikulas Patocka static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
67648debafeSMikulas Patocka {
67748debafeSMikulas Patocka 	list_del(&e->lru);
67848debafeSMikulas Patocka 	rb_erase(&e->rb_node, &wc->tree);
67948debafeSMikulas Patocka }
68048debafeSMikulas Patocka 
68148debafeSMikulas Patocka static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
68248debafeSMikulas Patocka {
68348debafeSMikulas Patocka 	if (WC_MODE_SORT_FREELIST(wc)) {
68448debafeSMikulas Patocka 		struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
68548debafeSMikulas Patocka 		if (unlikely(!*node))
68648debafeSMikulas Patocka 			wc->current_free = e;
68748debafeSMikulas Patocka 		while (*node) {
68848debafeSMikulas Patocka 			parent = *node;
68948debafeSMikulas Patocka 			if (&e->rb_node < *node)
69048debafeSMikulas Patocka 				node = &parent->rb_left;
69148debafeSMikulas Patocka 			else
69248debafeSMikulas Patocka 				node = &parent->rb_right;
69348debafeSMikulas Patocka 		}
69448debafeSMikulas Patocka 		rb_link_node(&e->rb_node, parent, node);
69548debafeSMikulas Patocka 		rb_insert_color(&e->rb_node, &wc->freetree);
69648debafeSMikulas Patocka 	} else {
69748debafeSMikulas Patocka 		list_add_tail(&e->lru, &wc->freelist);
69848debafeSMikulas Patocka 	}
69948debafeSMikulas Patocka 	wc->freelist_size++;
70048debafeSMikulas Patocka }
70148debafeSMikulas Patocka 
70241c526c5SMikulas Patocka static inline void writecache_verify_watermark(struct dm_writecache *wc)
70341c526c5SMikulas Patocka {
70441c526c5SMikulas Patocka 	if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
70541c526c5SMikulas Patocka 		queue_work(wc->writeback_wq, &wc->writeback_work);
70641c526c5SMikulas Patocka }
70741c526c5SMikulas Patocka 
7083923d485SMikulas Patocka static void writecache_max_age_timer(struct timer_list *t)
7093923d485SMikulas Patocka {
7103923d485SMikulas Patocka 	struct dm_writecache *wc = from_timer(wc, t, max_age_timer);
7113923d485SMikulas Patocka 
7123923d485SMikulas Patocka 	if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) {
7133923d485SMikulas Patocka 		queue_work(wc->writeback_wq, &wc->writeback_work);
7143923d485SMikulas Patocka 		mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
7153923d485SMikulas Patocka 	}
7163923d485SMikulas Patocka }
7173923d485SMikulas Patocka 
718dcd19507SMikulas Patocka static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector)
71948debafeSMikulas Patocka {
72048debafeSMikulas Patocka 	struct wc_entry *e;
72148debafeSMikulas Patocka 
72248debafeSMikulas Patocka 	if (WC_MODE_SORT_FREELIST(wc)) {
72348debafeSMikulas Patocka 		struct rb_node *next;
72448debafeSMikulas Patocka 		if (unlikely(!wc->current_free))
72548debafeSMikulas Patocka 			return NULL;
72648debafeSMikulas Patocka 		e = wc->current_free;
727dcd19507SMikulas Patocka 		if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
728dcd19507SMikulas Patocka 			return NULL;
72948debafeSMikulas Patocka 		next = rb_next(&e->rb_node);
73048debafeSMikulas Patocka 		rb_erase(&e->rb_node, &wc->freetree);
73148debafeSMikulas Patocka 		if (unlikely(!next))
73248debafeSMikulas Patocka 			next = rb_first(&wc->freetree);
73348debafeSMikulas Patocka 		wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
73448debafeSMikulas Patocka 	} else {
73548debafeSMikulas Patocka 		if (unlikely(list_empty(&wc->freelist)))
73648debafeSMikulas Patocka 			return NULL;
73748debafeSMikulas Patocka 		e = container_of(wc->freelist.next, struct wc_entry, lru);
738dcd19507SMikulas Patocka 		if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
739dcd19507SMikulas Patocka 			return NULL;
74048debafeSMikulas Patocka 		list_del(&e->lru);
74148debafeSMikulas Patocka 	}
74248debafeSMikulas Patocka 	wc->freelist_size--;
74341c526c5SMikulas Patocka 
74441c526c5SMikulas Patocka 	writecache_verify_watermark(wc);
74548debafeSMikulas Patocka 
74648debafeSMikulas Patocka 	return e;
74748debafeSMikulas Patocka }
74848debafeSMikulas Patocka 
74948debafeSMikulas Patocka static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
75048debafeSMikulas Patocka {
75148debafeSMikulas Patocka 	writecache_unlink(wc, e);
75248debafeSMikulas Patocka 	writecache_add_to_freelist(wc, e);
75348debafeSMikulas Patocka 	clear_seq_count(wc, e);
75448debafeSMikulas Patocka 	writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
75548debafeSMikulas Patocka 	if (unlikely(waitqueue_active(&wc->freelist_wait)))
75648debafeSMikulas Patocka 		wake_up(&wc->freelist_wait);
75748debafeSMikulas Patocka }
75848debafeSMikulas Patocka 
75948debafeSMikulas Patocka static void writecache_wait_on_freelist(struct dm_writecache *wc)
76048debafeSMikulas Patocka {
76148debafeSMikulas Patocka 	DEFINE_WAIT(wait);
76248debafeSMikulas Patocka 
76348debafeSMikulas Patocka 	prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
76448debafeSMikulas Patocka 	wc_unlock(wc);
76548debafeSMikulas Patocka 	io_schedule();
76648debafeSMikulas Patocka 	finish_wait(&wc->freelist_wait, &wait);
76748debafeSMikulas Patocka 	wc_lock(wc);
76848debafeSMikulas Patocka }
76948debafeSMikulas Patocka 
77048debafeSMikulas Patocka static void writecache_poison_lists(struct dm_writecache *wc)
77148debafeSMikulas Patocka {
77248debafeSMikulas Patocka 	/*
77348debafeSMikulas Patocka 	 * Catch incorrect access to these values while the device is suspended.
77448debafeSMikulas Patocka 	 */
77548debafeSMikulas Patocka 	memset(&wc->tree, -1, sizeof wc->tree);
77648debafeSMikulas Patocka 	wc->lru.next = LIST_POISON1;
77748debafeSMikulas Patocka 	wc->lru.prev = LIST_POISON2;
77848debafeSMikulas Patocka 	wc->freelist.next = LIST_POISON1;
77948debafeSMikulas Patocka 	wc->freelist.prev = LIST_POISON2;
78048debafeSMikulas Patocka }
78148debafeSMikulas Patocka 
78248debafeSMikulas Patocka static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
78348debafeSMikulas Patocka {
78448debafeSMikulas Patocka 	writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
78548debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
78648debafeSMikulas Patocka 		writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
78748debafeSMikulas Patocka }
78848debafeSMikulas Patocka 
78948debafeSMikulas Patocka static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
79048debafeSMikulas Patocka {
79148debafeSMikulas Patocka 	return read_seq_count(wc, e) < wc->seq_count;
79248debafeSMikulas Patocka }
79348debafeSMikulas Patocka 
79448debafeSMikulas Patocka static void writecache_flush(struct dm_writecache *wc)
79548debafeSMikulas Patocka {
79648debafeSMikulas Patocka 	struct wc_entry *e, *e2;
79748debafeSMikulas Patocka 	bool need_flush_after_free;
79848debafeSMikulas Patocka 
79948debafeSMikulas Patocka 	wc->uncommitted_blocks = 0;
80048debafeSMikulas Patocka 	del_timer(&wc->autocommit_timer);
80148debafeSMikulas Patocka 
80248debafeSMikulas Patocka 	if (list_empty(&wc->lru))
80348debafeSMikulas Patocka 		return;
80448debafeSMikulas Patocka 
80548debafeSMikulas Patocka 	e = container_of(wc->lru.next, struct wc_entry, lru);
80648debafeSMikulas Patocka 	if (writecache_entry_is_committed(wc, e)) {
80748debafeSMikulas Patocka 		if (wc->overwrote_committed) {
80848debafeSMikulas Patocka 			writecache_wait_for_ios(wc, WRITE);
80948debafeSMikulas Patocka 			writecache_disk_flush(wc, wc->ssd_dev);
81048debafeSMikulas Patocka 			wc->overwrote_committed = false;
81148debafeSMikulas Patocka 		}
81248debafeSMikulas Patocka 		return;
81348debafeSMikulas Patocka 	}
81448debafeSMikulas Patocka 	while (1) {
81548debafeSMikulas Patocka 		writecache_flush_entry(wc, e);
81648debafeSMikulas Patocka 		if (unlikely(e->lru.next == &wc->lru))
81748debafeSMikulas Patocka 			break;
81848debafeSMikulas Patocka 		e2 = container_of(e->lru.next, struct wc_entry, lru);
81948debafeSMikulas Patocka 		if (writecache_entry_is_committed(wc, e2))
82048debafeSMikulas Patocka 			break;
82148debafeSMikulas Patocka 		e = e2;
82248debafeSMikulas Patocka 		cond_resched();
82348debafeSMikulas Patocka 	}
824aa950920SMikulas Patocka 	writecache_commit_flushed(wc, true);
82548debafeSMikulas Patocka 
82648debafeSMikulas Patocka 	wc->seq_count++;
82748debafeSMikulas Patocka 	pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
828dc8a01aeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
829aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
830dc8a01aeSMikulas Patocka 	else
831dc8a01aeSMikulas Patocka 		ssd_commit_superblock(wc);
83248debafeSMikulas Patocka 
83348debafeSMikulas Patocka 	wc->overwrote_committed = false;
83448debafeSMikulas Patocka 
83548debafeSMikulas Patocka 	need_flush_after_free = false;
83648debafeSMikulas Patocka 	while (1) {
83748debafeSMikulas Patocka 		/* Free another committed entry with lower seq-count */
83848debafeSMikulas Patocka 		struct rb_node *rb_node = rb_prev(&e->rb_node);
83948debafeSMikulas Patocka 
84048debafeSMikulas Patocka 		if (rb_node) {
84148debafeSMikulas Patocka 			e2 = container_of(rb_node, struct wc_entry, rb_node);
84248debafeSMikulas Patocka 			if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
84348debafeSMikulas Patocka 			    likely(!e2->write_in_progress)) {
84448debafeSMikulas Patocka 				writecache_free_entry(wc, e2);
84548debafeSMikulas Patocka 				need_flush_after_free = true;
84648debafeSMikulas Patocka 			}
84748debafeSMikulas Patocka 		}
84848debafeSMikulas Patocka 		if (unlikely(e->lru.prev == &wc->lru))
84948debafeSMikulas Patocka 			break;
85048debafeSMikulas Patocka 		e = container_of(e->lru.prev, struct wc_entry, lru);
85148debafeSMikulas Patocka 		cond_resched();
85248debafeSMikulas Patocka 	}
85348debafeSMikulas Patocka 
85448debafeSMikulas Patocka 	if (need_flush_after_free)
855aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
85648debafeSMikulas Patocka }
85748debafeSMikulas Patocka 
85848debafeSMikulas Patocka static void writecache_flush_work(struct work_struct *work)
85948debafeSMikulas Patocka {
86048debafeSMikulas Patocka 	struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
86148debafeSMikulas Patocka 
86248debafeSMikulas Patocka 	wc_lock(wc);
86348debafeSMikulas Patocka 	writecache_flush(wc);
86448debafeSMikulas Patocka 	wc_unlock(wc);
86548debafeSMikulas Patocka }
86648debafeSMikulas Patocka 
86748debafeSMikulas Patocka static void writecache_autocommit_timer(struct timer_list *t)
86848debafeSMikulas Patocka {
86948debafeSMikulas Patocka 	struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
87048debafeSMikulas Patocka 	if (!writecache_has_error(wc))
87148debafeSMikulas Patocka 		queue_work(wc->writeback_wq, &wc->flush_work);
87248debafeSMikulas Patocka }
87348debafeSMikulas Patocka 
87448debafeSMikulas Patocka static void writecache_schedule_autocommit(struct dm_writecache *wc)
87548debafeSMikulas Patocka {
87648debafeSMikulas Patocka 	if (!timer_pending(&wc->autocommit_timer))
87748debafeSMikulas Patocka 		mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
87848debafeSMikulas Patocka }
87948debafeSMikulas Patocka 
88048debafeSMikulas Patocka static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
88148debafeSMikulas Patocka {
88248debafeSMikulas Patocka 	struct wc_entry *e;
88348debafeSMikulas Patocka 	bool discarded_something = false;
88448debafeSMikulas Patocka 
88548debafeSMikulas Patocka 	e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
88648debafeSMikulas Patocka 	if (unlikely(!e))
88748debafeSMikulas Patocka 		return;
88848debafeSMikulas Patocka 
88948debafeSMikulas Patocka 	while (read_original_sector(wc, e) < end) {
89048debafeSMikulas Patocka 		struct rb_node *node = rb_next(&e->rb_node);
89148debafeSMikulas Patocka 
89248debafeSMikulas Patocka 		if (likely(!e->write_in_progress)) {
89348debafeSMikulas Patocka 			if (!discarded_something) {
894a143e172SHuaisheng Ye 				if (!WC_MODE_PMEM(wc)) {
89548debafeSMikulas Patocka 					writecache_wait_for_ios(wc, READ);
89648debafeSMikulas Patocka 					writecache_wait_for_ios(wc, WRITE);
897a143e172SHuaisheng Ye 				}
89848debafeSMikulas Patocka 				discarded_something = true;
89948debafeSMikulas Patocka 			}
90039495b12SHuaisheng Ye 			if (!writecache_entry_is_committed(wc, e))
90139495b12SHuaisheng Ye 				wc->uncommitted_blocks--;
90248debafeSMikulas Patocka 			writecache_free_entry(wc, e);
90348debafeSMikulas Patocka 		}
90448debafeSMikulas Patocka 
90584420b1eSHuaisheng Ye 		if (unlikely(!node))
90648debafeSMikulas Patocka 			break;
90748debafeSMikulas Patocka 
90848debafeSMikulas Patocka 		e = container_of(node, struct wc_entry, rb_node);
90948debafeSMikulas Patocka 	}
91048debafeSMikulas Patocka 
91148debafeSMikulas Patocka 	if (discarded_something)
912aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
91348debafeSMikulas Patocka }
91448debafeSMikulas Patocka 
91548debafeSMikulas Patocka static bool writecache_wait_for_writeback(struct dm_writecache *wc)
91648debafeSMikulas Patocka {
91748debafeSMikulas Patocka 	if (wc->writeback_size) {
91848debafeSMikulas Patocka 		writecache_wait_on_freelist(wc);
91948debafeSMikulas Patocka 		return true;
92048debafeSMikulas Patocka 	}
92148debafeSMikulas Patocka 	return false;
92248debafeSMikulas Patocka }
92348debafeSMikulas Patocka 
92448debafeSMikulas Patocka static void writecache_suspend(struct dm_target *ti)
92548debafeSMikulas Patocka {
92648debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
92748debafeSMikulas Patocka 	bool flush_on_suspend;
92848debafeSMikulas Patocka 
92948debafeSMikulas Patocka 	del_timer_sync(&wc->autocommit_timer);
9303923d485SMikulas Patocka 	del_timer_sync(&wc->max_age_timer);
93148debafeSMikulas Patocka 
93248debafeSMikulas Patocka 	wc_lock(wc);
93348debafeSMikulas Patocka 	writecache_flush(wc);
93448debafeSMikulas Patocka 	flush_on_suspend = wc->flush_on_suspend;
93548debafeSMikulas Patocka 	if (flush_on_suspend) {
93648debafeSMikulas Patocka 		wc->flush_on_suspend = false;
93748debafeSMikulas Patocka 		wc->writeback_all++;
93848debafeSMikulas Patocka 		queue_work(wc->writeback_wq, &wc->writeback_work);
93948debafeSMikulas Patocka 	}
94048debafeSMikulas Patocka 	wc_unlock(wc);
94148debafeSMikulas Patocka 
942adc0daadSMikulas Patocka 	drain_workqueue(wc->writeback_wq);
94348debafeSMikulas Patocka 
94448debafeSMikulas Patocka 	wc_lock(wc);
94548debafeSMikulas Patocka 	if (flush_on_suspend)
94648debafeSMikulas Patocka 		wc->writeback_all--;
94748debafeSMikulas Patocka 	while (writecache_wait_for_writeback(wc));
94848debafeSMikulas Patocka 
94948debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
95048debafeSMikulas Patocka 		persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
95148debafeSMikulas Patocka 
95248debafeSMikulas Patocka 	writecache_poison_lists(wc);
95348debafeSMikulas Patocka 
95448debafeSMikulas Patocka 	wc_unlock(wc);
95548debafeSMikulas Patocka }
95648debafeSMikulas Patocka 
95748debafeSMikulas Patocka static int writecache_alloc_entries(struct dm_writecache *wc)
95848debafeSMikulas Patocka {
95948debafeSMikulas Patocka 	size_t b;
96048debafeSMikulas Patocka 
96148debafeSMikulas Patocka 	if (wc->entries)
96248debafeSMikulas Patocka 		return 0;
96350a7d3baSKees Cook 	wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
96448debafeSMikulas Patocka 	if (!wc->entries)
96548debafeSMikulas Patocka 		return -ENOMEM;
96648debafeSMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
96748debafeSMikulas Patocka 		struct wc_entry *e = &wc->entries[b];
96848debafeSMikulas Patocka 		e->index = b;
96948debafeSMikulas Patocka 		e->write_in_progress = false;
9701edaa447SMikulas Patocka 		cond_resched();
97148debafeSMikulas Patocka 	}
97248debafeSMikulas Patocka 
97348debafeSMikulas Patocka 	return 0;
97448debafeSMikulas Patocka }
97548debafeSMikulas Patocka 
97631b22120SMikulas Patocka static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors)
97731b22120SMikulas Patocka {
97831b22120SMikulas Patocka 	struct dm_io_region region;
97931b22120SMikulas Patocka 	struct dm_io_request req;
98031b22120SMikulas Patocka 
98131b22120SMikulas Patocka 	region.bdev = wc->ssd_dev->bdev;
98231b22120SMikulas Patocka 	region.sector = wc->start_sector;
98331b22120SMikulas Patocka 	region.count = n_sectors;
98431b22120SMikulas Patocka 	req.bi_op = REQ_OP_READ;
98531b22120SMikulas Patocka 	req.bi_op_flags = REQ_SYNC;
98631b22120SMikulas Patocka 	req.mem.type = DM_IO_VMA;
98731b22120SMikulas Patocka 	req.mem.ptr.vma = (char *)wc->memory_map;
98831b22120SMikulas Patocka 	req.client = wc->dm_io;
98931b22120SMikulas Patocka 	req.notify.fn = NULL;
99031b22120SMikulas Patocka 
99131b22120SMikulas Patocka 	return dm_io(&req, 1, &region, NULL);
99231b22120SMikulas Patocka }
99331b22120SMikulas Patocka 
99448debafeSMikulas Patocka static void writecache_resume(struct dm_target *ti)
99548debafeSMikulas Patocka {
99648debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
99748debafeSMikulas Patocka 	size_t b;
99848debafeSMikulas Patocka 	bool need_flush = false;
99948debafeSMikulas Patocka 	__le64 sb_seq_count;
100048debafeSMikulas Patocka 	int r;
100148debafeSMikulas Patocka 
100248debafeSMikulas Patocka 	wc_lock(wc);
100348debafeSMikulas Patocka 
1004d9928ac5SMike Snitzer 	wc->data_device_sectors = bdev_nr_sectors(wc->dev->bdev);
10054134455fSMikulas Patocka 
100631b22120SMikulas Patocka 	if (WC_MODE_PMEM(wc)) {
100748debafeSMikulas Patocka 		persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
100831b22120SMikulas Patocka 	} else {
100931b22120SMikulas Patocka 		r = writecache_read_metadata(wc, wc->metadata_sectors);
101031b22120SMikulas Patocka 		if (r) {
101131b22120SMikulas Patocka 			size_t sb_entries_offset;
101231b22120SMikulas Patocka 			writecache_error(wc, r, "unable to read metadata: %d", r);
101331b22120SMikulas Patocka 			sb_entries_offset = offsetof(struct wc_memory_superblock, entries);
101431b22120SMikulas Patocka 			memset((char *)wc->memory_map + sb_entries_offset, -1,
101531b22120SMikulas Patocka 			       (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset);
101631b22120SMikulas Patocka 		}
101731b22120SMikulas Patocka 	}
101848debafeSMikulas Patocka 
101948debafeSMikulas Patocka 	wc->tree = RB_ROOT;
102048debafeSMikulas Patocka 	INIT_LIST_HEAD(&wc->lru);
102148debafeSMikulas Patocka 	if (WC_MODE_SORT_FREELIST(wc)) {
102248debafeSMikulas Patocka 		wc->freetree = RB_ROOT;
102348debafeSMikulas Patocka 		wc->current_free = NULL;
102448debafeSMikulas Patocka 	} else {
102548debafeSMikulas Patocka 		INIT_LIST_HEAD(&wc->freelist);
102648debafeSMikulas Patocka 	}
102748debafeSMikulas Patocka 	wc->freelist_size = 0;
102848debafeSMikulas Patocka 
1029ec6347bbSDan Williams 	r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count,
1030ec6347bbSDan Williams 			      sizeof(uint64_t));
103148debafeSMikulas Patocka 	if (r) {
103248debafeSMikulas Patocka 		writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
103348debafeSMikulas Patocka 		sb_seq_count = cpu_to_le64(0);
103448debafeSMikulas Patocka 	}
103548debafeSMikulas Patocka 	wc->seq_count = le64_to_cpu(sb_seq_count);
103648debafeSMikulas Patocka 
103748debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
103848debafeSMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
103948debafeSMikulas Patocka 		struct wc_entry *e = &wc->entries[b];
104048debafeSMikulas Patocka 		struct wc_memory_entry wme;
104148debafeSMikulas Patocka 		if (writecache_has_error(wc)) {
104248debafeSMikulas Patocka 			e->original_sector = -1;
104348debafeSMikulas Patocka 			e->seq_count = -1;
104448debafeSMikulas Patocka 			continue;
104548debafeSMikulas Patocka 		}
1046ec6347bbSDan Williams 		r = copy_mc_to_kernel(&wme, memory_entry(wc, e),
1047ec6347bbSDan Williams 				      sizeof(struct wc_memory_entry));
104848debafeSMikulas Patocka 		if (r) {
104948debafeSMikulas Patocka 			writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
105048debafeSMikulas Patocka 					 (unsigned long)b, r);
105148debafeSMikulas Patocka 			e->original_sector = -1;
105248debafeSMikulas Patocka 			e->seq_count = -1;
105348debafeSMikulas Patocka 		} else {
105448debafeSMikulas Patocka 			e->original_sector = le64_to_cpu(wme.original_sector);
105548debafeSMikulas Patocka 			e->seq_count = le64_to_cpu(wme.seq_count);
105648debafeSMikulas Patocka 		}
10571edaa447SMikulas Patocka 		cond_resched();
105848debafeSMikulas Patocka 	}
105948debafeSMikulas Patocka #endif
106048debafeSMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
106148debafeSMikulas Patocka 		struct wc_entry *e = &wc->entries[b];
106248debafeSMikulas Patocka 		if (!writecache_entry_is_committed(wc, e)) {
106348debafeSMikulas Patocka 			if (read_seq_count(wc, e) != -1) {
106448debafeSMikulas Patocka erase_this:
106548debafeSMikulas Patocka 				clear_seq_count(wc, e);
106648debafeSMikulas Patocka 				need_flush = true;
106748debafeSMikulas Patocka 			}
106848debafeSMikulas Patocka 			writecache_add_to_freelist(wc, e);
106948debafeSMikulas Patocka 		} else {
107048debafeSMikulas Patocka 			struct wc_entry *old;
107148debafeSMikulas Patocka 
107248debafeSMikulas Patocka 			old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
107348debafeSMikulas Patocka 			if (!old) {
107448debafeSMikulas Patocka 				writecache_insert_entry(wc, e);
107548debafeSMikulas Patocka 			} else {
107648debafeSMikulas Patocka 				if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
107748debafeSMikulas Patocka 					writecache_error(wc, -EINVAL,
107848debafeSMikulas Patocka 						 "two identical entries, position %llu, sector %llu, sequence %llu",
107948debafeSMikulas Patocka 						 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
108048debafeSMikulas Patocka 						 (unsigned long long)read_seq_count(wc, e));
108148debafeSMikulas Patocka 				}
108248debafeSMikulas Patocka 				if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
108348debafeSMikulas Patocka 					goto erase_this;
108448debafeSMikulas Patocka 				} else {
108548debafeSMikulas Patocka 					writecache_free_entry(wc, old);
108648debafeSMikulas Patocka 					writecache_insert_entry(wc, e);
108748debafeSMikulas Patocka 					need_flush = true;
108848debafeSMikulas Patocka 				}
108948debafeSMikulas Patocka 			}
109048debafeSMikulas Patocka 		}
109148debafeSMikulas Patocka 		cond_resched();
109248debafeSMikulas Patocka 	}
109348debafeSMikulas Patocka 
109448debafeSMikulas Patocka 	if (need_flush) {
109548debafeSMikulas Patocka 		writecache_flush_all_metadata(wc);
1096aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
109748debafeSMikulas Patocka 	}
109848debafeSMikulas Patocka 
109941c526c5SMikulas Patocka 	writecache_verify_watermark(wc);
110041c526c5SMikulas Patocka 
11013923d485SMikulas Patocka 	if (wc->max_age != MAX_AGE_UNSPECIFIED)
11023923d485SMikulas Patocka 		mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
11033923d485SMikulas Patocka 
110448debafeSMikulas Patocka 	wc_unlock(wc);
110548debafeSMikulas Patocka }
110648debafeSMikulas Patocka 
110748debafeSMikulas Patocka static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
110848debafeSMikulas Patocka {
110948debafeSMikulas Patocka 	if (argc != 1)
111048debafeSMikulas Patocka 		return -EINVAL;
111148debafeSMikulas Patocka 
111248debafeSMikulas Patocka 	wc_lock(wc);
111348debafeSMikulas Patocka 	if (dm_suspended(wc->ti)) {
111448debafeSMikulas Patocka 		wc_unlock(wc);
111548debafeSMikulas Patocka 		return -EBUSY;
111648debafeSMikulas Patocka 	}
111748debafeSMikulas Patocka 	if (writecache_has_error(wc)) {
111848debafeSMikulas Patocka 		wc_unlock(wc);
111948debafeSMikulas Patocka 		return -EIO;
112048debafeSMikulas Patocka 	}
112148debafeSMikulas Patocka 
112248debafeSMikulas Patocka 	writecache_flush(wc);
112348debafeSMikulas Patocka 	wc->writeback_all++;
112448debafeSMikulas Patocka 	queue_work(wc->writeback_wq, &wc->writeback_work);
112548debafeSMikulas Patocka 	wc_unlock(wc);
112648debafeSMikulas Patocka 
112748debafeSMikulas Patocka 	flush_workqueue(wc->writeback_wq);
112848debafeSMikulas Patocka 
112948debafeSMikulas Patocka 	wc_lock(wc);
113048debafeSMikulas Patocka 	wc->writeback_all--;
113148debafeSMikulas Patocka 	if (writecache_has_error(wc)) {
113248debafeSMikulas Patocka 		wc_unlock(wc);
113348debafeSMikulas Patocka 		return -EIO;
113448debafeSMikulas Patocka 	}
113548debafeSMikulas Patocka 	wc_unlock(wc);
113648debafeSMikulas Patocka 
113748debafeSMikulas Patocka 	return 0;
113848debafeSMikulas Patocka }
113948debafeSMikulas Patocka 
114048debafeSMikulas Patocka static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
114148debafeSMikulas Patocka {
114248debafeSMikulas Patocka 	if (argc != 1)
114348debafeSMikulas Patocka 		return -EINVAL;
114448debafeSMikulas Patocka 
114548debafeSMikulas Patocka 	wc_lock(wc);
114648debafeSMikulas Patocka 	wc->flush_on_suspend = true;
114748debafeSMikulas Patocka 	wc_unlock(wc);
114848debafeSMikulas Patocka 
114948debafeSMikulas Patocka 	return 0;
115048debafeSMikulas Patocka }
115148debafeSMikulas Patocka 
115293de44ebSMikulas Patocka static void activate_cleaner(struct dm_writecache *wc)
115393de44ebSMikulas Patocka {
115493de44ebSMikulas Patocka 	wc->flush_on_suspend = true;
115593de44ebSMikulas Patocka 	wc->cleaner = true;
115693de44ebSMikulas Patocka 	wc->freelist_high_watermark = wc->n_blocks;
115793de44ebSMikulas Patocka 	wc->freelist_low_watermark = wc->n_blocks;
115893de44ebSMikulas Patocka }
115993de44ebSMikulas Patocka 
116093de44ebSMikulas Patocka static int process_cleaner_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
116193de44ebSMikulas Patocka {
116293de44ebSMikulas Patocka 	if (argc != 1)
116393de44ebSMikulas Patocka 		return -EINVAL;
116493de44ebSMikulas Patocka 
116593de44ebSMikulas Patocka 	wc_lock(wc);
116693de44ebSMikulas Patocka 	activate_cleaner(wc);
116793de44ebSMikulas Patocka 	if (!dm_suspended(wc->ti))
116893de44ebSMikulas Patocka 		writecache_verify_watermark(wc);
116993de44ebSMikulas Patocka 	wc_unlock(wc);
117093de44ebSMikulas Patocka 
117193de44ebSMikulas Patocka 	return 0;
117293de44ebSMikulas Patocka }
117393de44ebSMikulas Patocka 
1174e3a35d03SMikulas Patocka static int process_clear_stats_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1175e3a35d03SMikulas Patocka {
1176e3a35d03SMikulas Patocka 	if (argc != 1)
1177e3a35d03SMikulas Patocka 		return -EINVAL;
1178e3a35d03SMikulas Patocka 
1179e3a35d03SMikulas Patocka 	wc_lock(wc);
1180e3a35d03SMikulas Patocka 	memset(&wc->stats, 0, sizeof wc->stats);
1181e3a35d03SMikulas Patocka 	wc_unlock(wc);
1182e3a35d03SMikulas Patocka 
1183e3a35d03SMikulas Patocka 	return 0;
1184e3a35d03SMikulas Patocka }
1185e3a35d03SMikulas Patocka 
118648debafeSMikulas Patocka static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
118748debafeSMikulas Patocka 			      char *result, unsigned maxlen)
118848debafeSMikulas Patocka {
118948debafeSMikulas Patocka 	int r = -EINVAL;
119048debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
119148debafeSMikulas Patocka 
119248debafeSMikulas Patocka 	if (!strcasecmp(argv[0], "flush"))
119348debafeSMikulas Patocka 		r = process_flush_mesg(argc, argv, wc);
119448debafeSMikulas Patocka 	else if (!strcasecmp(argv[0], "flush_on_suspend"))
119548debafeSMikulas Patocka 		r = process_flush_on_suspend_mesg(argc, argv, wc);
119693de44ebSMikulas Patocka 	else if (!strcasecmp(argv[0], "cleaner"))
119793de44ebSMikulas Patocka 		r = process_cleaner_mesg(argc, argv, wc);
1198e3a35d03SMikulas Patocka 	else if (!strcasecmp(argv[0], "clear_stats"))
1199e3a35d03SMikulas Patocka 		r = process_clear_stats_mesg(argc, argv, wc);
120048debafeSMikulas Patocka 	else
120148debafeSMikulas Patocka 		DMERR("unrecognised message received: %s", argv[0]);
120248debafeSMikulas Patocka 
120348debafeSMikulas Patocka 	return r;
120448debafeSMikulas Patocka }
120548debafeSMikulas Patocka 
120648338daaSMikulas Patocka static void memcpy_flushcache_optimized(void *dest, void *source, size_t size)
120748338daaSMikulas Patocka {
120848338daaSMikulas Patocka 	/*
120948338daaSMikulas Patocka 	 * clflushopt performs better with block size 1024, 2048, 4096
121048338daaSMikulas Patocka 	 * non-temporal stores perform better with block size 512
121148338daaSMikulas Patocka 	 *
121248338daaSMikulas Patocka 	 * block size   512             1024            2048            4096
121348338daaSMikulas Patocka 	 * movnti       496 MB/s        642 MB/s        725 MB/s        744 MB/s
121448338daaSMikulas Patocka 	 * clflushopt   373 MB/s        688 MB/s        1.1 GB/s        1.2 GB/s
121548338daaSMikulas Patocka 	 *
121648338daaSMikulas Patocka 	 * We see that movnti performs better for 512-byte blocks, and
121748338daaSMikulas Patocka 	 * clflushopt performs better for 1024-byte and larger blocks. So, we
121848338daaSMikulas Patocka 	 * prefer clflushopt for sizes >= 768.
121948338daaSMikulas Patocka 	 *
122048338daaSMikulas Patocka 	 * NOTE: this happens to be the case now (with dm-writecache's single
122148338daaSMikulas Patocka 	 * threaded model) but re-evaluate this once memcpy_flushcache() is
122248338daaSMikulas Patocka 	 * enabled to use movdir64b which might invalidate this performance
122348338daaSMikulas Patocka 	 * advantage seen with cache-allocating-writes plus flushing.
122448338daaSMikulas Patocka 	 */
122548338daaSMikulas Patocka #ifdef CONFIG_X86
122648338daaSMikulas Patocka 	if (static_cpu_has(X86_FEATURE_CLFLUSHOPT) &&
122748338daaSMikulas Patocka 	    likely(boot_cpu_data.x86_clflush_size == 64) &&
122848338daaSMikulas Patocka 	    likely(size >= 768)) {
122948338daaSMikulas Patocka 		do {
123048338daaSMikulas Patocka 			memcpy((void *)dest, (void *)source, 64);
123148338daaSMikulas Patocka 			clflushopt((void *)dest);
123248338daaSMikulas Patocka 			dest += 64;
123348338daaSMikulas Patocka 			source += 64;
123448338daaSMikulas Patocka 			size -= 64;
123548338daaSMikulas Patocka 		} while (size >= 64);
123648338daaSMikulas Patocka 		return;
123748338daaSMikulas Patocka 	}
123848338daaSMikulas Patocka #endif
123948338daaSMikulas Patocka 	memcpy_flushcache(dest, source, size);
124048338daaSMikulas Patocka }
124148338daaSMikulas Patocka 
124248debafeSMikulas Patocka static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
124348debafeSMikulas Patocka {
124448debafeSMikulas Patocka 	void *buf;
124548debafeSMikulas Patocka 	unsigned size;
124648debafeSMikulas Patocka 	int rw = bio_data_dir(bio);
124748debafeSMikulas Patocka 	unsigned remaining_size = wc->block_size;
124848debafeSMikulas Patocka 
124948debafeSMikulas Patocka 	do {
125048debafeSMikulas Patocka 		struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
125118a6234cSChristoph Hellwig 		buf = bvec_kmap_local(&bv);
125248debafeSMikulas Patocka 		size = bv.bv_len;
125348debafeSMikulas Patocka 		if (unlikely(size > remaining_size))
125448debafeSMikulas Patocka 			size = remaining_size;
125548debafeSMikulas Patocka 
125648debafeSMikulas Patocka 		if (rw == READ) {
125748debafeSMikulas Patocka 			int r;
1258ec6347bbSDan Williams 			r = copy_mc_to_kernel(buf, data, size);
125948debafeSMikulas Patocka 			flush_dcache_page(bio_page(bio));
126048debafeSMikulas Patocka 			if (unlikely(r)) {
126148debafeSMikulas Patocka 				writecache_error(wc, r, "hardware memory error when reading data: %d", r);
126248debafeSMikulas Patocka 				bio->bi_status = BLK_STS_IOERR;
126348debafeSMikulas Patocka 			}
126448debafeSMikulas Patocka 		} else {
126548debafeSMikulas Patocka 			flush_dcache_page(bio_page(bio));
126648338daaSMikulas Patocka 			memcpy_flushcache_optimized(data, buf, size);
126748debafeSMikulas Patocka 		}
126848debafeSMikulas Patocka 
126918a6234cSChristoph Hellwig 		kunmap_local(buf);
127048debafeSMikulas Patocka 
127148debafeSMikulas Patocka 		data = (char *)data + size;
127248debafeSMikulas Patocka 		remaining_size -= size;
127348debafeSMikulas Patocka 		bio_advance(bio, size);
127448debafeSMikulas Patocka 	} while (unlikely(remaining_size));
127548debafeSMikulas Patocka }
127648debafeSMikulas Patocka 
127748debafeSMikulas Patocka static int writecache_flush_thread(void *data)
127848debafeSMikulas Patocka {
127948debafeSMikulas Patocka 	struct dm_writecache *wc = data;
128048debafeSMikulas Patocka 
128148debafeSMikulas Patocka 	while (1) {
128248debafeSMikulas Patocka 		struct bio *bio;
128348debafeSMikulas Patocka 
128448debafeSMikulas Patocka 		wc_lock(wc);
128548debafeSMikulas Patocka 		bio = bio_list_pop(&wc->flush_list);
128648debafeSMikulas Patocka 		if (!bio) {
128748debafeSMikulas Patocka 			set_current_state(TASK_INTERRUPTIBLE);
128848debafeSMikulas Patocka 			wc_unlock(wc);
128948debafeSMikulas Patocka 
129048debafeSMikulas Patocka 			if (unlikely(kthread_should_stop())) {
129148debafeSMikulas Patocka 				set_current_state(TASK_RUNNING);
129248debafeSMikulas Patocka 				break;
129348debafeSMikulas Patocka 			}
129448debafeSMikulas Patocka 
129548debafeSMikulas Patocka 			schedule();
129648debafeSMikulas Patocka 			continue;
129748debafeSMikulas Patocka 		}
129848debafeSMikulas Patocka 
129948debafeSMikulas Patocka 		if (bio_op(bio) == REQ_OP_DISCARD) {
130048debafeSMikulas Patocka 			writecache_discard(wc, bio->bi_iter.bi_sector,
130148debafeSMikulas Patocka 					   bio_end_sector(bio));
130248debafeSMikulas Patocka 			wc_unlock(wc);
130348debafeSMikulas Patocka 			bio_set_dev(bio, wc->dev->bdev);
1304ed00aabdSChristoph Hellwig 			submit_bio_noacct(bio);
130548debafeSMikulas Patocka 		} else {
130648debafeSMikulas Patocka 			writecache_flush(wc);
130748debafeSMikulas Patocka 			wc_unlock(wc);
130848debafeSMikulas Patocka 			if (writecache_has_error(wc))
130948debafeSMikulas Patocka 				bio->bi_status = BLK_STS_IOERR;
131048debafeSMikulas Patocka 			bio_endio(bio);
131148debafeSMikulas Patocka 		}
131248debafeSMikulas Patocka 	}
131348debafeSMikulas Patocka 
131448debafeSMikulas Patocka 	return 0;
131548debafeSMikulas Patocka }
131648debafeSMikulas Patocka 
131748debafeSMikulas Patocka static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
131848debafeSMikulas Patocka {
131948debafeSMikulas Patocka 	if (bio_list_empty(&wc->flush_list))
132048debafeSMikulas Patocka 		wake_up_process(wc->flush_thread);
132148debafeSMikulas Patocka 	bio_list_add(&wc->flush_list, bio);
132248debafeSMikulas Patocka }
132348debafeSMikulas Patocka 
1324cdd4d783SMike Snitzer enum wc_map_op {
1325cdd4d783SMike Snitzer 	WC_MAP_SUBMIT,
1326cdd4d783SMike Snitzer 	WC_MAP_REMAP,
1327cdd4d783SMike Snitzer 	WC_MAP_REMAP_ORIGIN,
1328cdd4d783SMike Snitzer 	WC_MAP_RETURN,
1329cdd4d783SMike Snitzer 	WC_MAP_ERROR,
1330cdd4d783SMike Snitzer };
1331cdd4d783SMike Snitzer 
13324d020b3aSMike Snitzer static enum wc_map_op writecache_map_remap_origin(struct dm_writecache *wc, struct bio *bio,
13334d020b3aSMike Snitzer 						  struct wc_entry *e)
13344d020b3aSMike Snitzer {
13354d020b3aSMike Snitzer 	if (e) {
13364d020b3aSMike Snitzer 		sector_t next_boundary =
13374d020b3aSMike Snitzer 			read_original_sector(wc, e) - bio->bi_iter.bi_sector;
13384d020b3aSMike Snitzer 		if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT)
13394d020b3aSMike Snitzer 			dm_accept_partial_bio(bio, next_boundary);
13404d020b3aSMike Snitzer 	}
13414d020b3aSMike Snitzer 
13424d020b3aSMike Snitzer 	return WC_MAP_REMAP_ORIGIN;
13434d020b3aSMike Snitzer }
13444d020b3aSMike Snitzer 
1345cdd4d783SMike Snitzer static enum wc_map_op writecache_map_read(struct dm_writecache *wc, struct bio *bio)
1346cdd4d783SMike Snitzer {
1347cdd4d783SMike Snitzer 	enum wc_map_op map_op;
1348cdd4d783SMike Snitzer 	struct wc_entry *e;
1349cdd4d783SMike Snitzer 
1350cdd4d783SMike Snitzer read_next_block:
1351e3a35d03SMikulas Patocka 	wc->stats.reads++;
1352cdd4d783SMike Snitzer 	e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1353cdd4d783SMike Snitzer 	if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1354e3a35d03SMikulas Patocka 		wc->stats.read_hits++;
1355cdd4d783SMike Snitzer 		if (WC_MODE_PMEM(wc)) {
1356cdd4d783SMike Snitzer 			bio_copy_block(wc, bio, memory_data(wc, e));
1357cdd4d783SMike Snitzer 			if (bio->bi_iter.bi_size)
1358cdd4d783SMike Snitzer 				goto read_next_block;
1359cdd4d783SMike Snitzer 			map_op = WC_MAP_SUBMIT;
1360cdd4d783SMike Snitzer 		} else {
1361cdd4d783SMike Snitzer 			dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1362cdd4d783SMike Snitzer 			bio_set_dev(bio, wc->ssd_dev->bdev);
1363cdd4d783SMike Snitzer 			bio->bi_iter.bi_sector = cache_sector(wc, e);
1364cdd4d783SMike Snitzer 			if (!writecache_entry_is_committed(wc, e))
1365cdd4d783SMike Snitzer 				writecache_wait_for_ios(wc, WRITE);
1366cdd4d783SMike Snitzer 			map_op = WC_MAP_REMAP;
1367cdd4d783SMike Snitzer 		}
1368cdd4d783SMike Snitzer 	} else {
13694d020b3aSMike Snitzer 		map_op = writecache_map_remap_origin(wc, bio, e);
1370cdd4d783SMike Snitzer 	}
1371cdd4d783SMike Snitzer 
1372cdd4d783SMike Snitzer 	return map_op;
1373cdd4d783SMike Snitzer }
1374cdd4d783SMike Snitzer 
1375cdd4d783SMike Snitzer static enum wc_map_op writecache_bio_copy_ssd(struct dm_writecache *wc, struct bio *bio,
1376cdd4d783SMike Snitzer 					      struct wc_entry *e, bool search_used)
1377cdd4d783SMike Snitzer {
1378cdd4d783SMike Snitzer 	unsigned bio_size = wc->block_size;
1379cdd4d783SMike Snitzer 	sector_t start_cache_sec = cache_sector(wc, e);
1380cdd4d783SMike Snitzer 	sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT);
1381cdd4d783SMike Snitzer 
1382cdd4d783SMike Snitzer 	while (bio_size < bio->bi_iter.bi_size) {
1383cdd4d783SMike Snitzer 		if (!search_used) {
1384cdd4d783SMike Snitzer 			struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec);
1385cdd4d783SMike Snitzer 			if (!f)
1386cdd4d783SMike Snitzer 				break;
1387cdd4d783SMike Snitzer 			write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector +
1388cdd4d783SMike Snitzer 							(bio_size >> SECTOR_SHIFT), wc->seq_count);
1389cdd4d783SMike Snitzer 			writecache_insert_entry(wc, f);
1390cdd4d783SMike Snitzer 			wc->uncommitted_blocks++;
1391cdd4d783SMike Snitzer 		} else {
1392cdd4d783SMike Snitzer 			struct wc_entry *f;
1393cdd4d783SMike Snitzer 			struct rb_node *next = rb_next(&e->rb_node);
1394cdd4d783SMike Snitzer 			if (!next)
1395cdd4d783SMike Snitzer 				break;
1396cdd4d783SMike Snitzer 			f = container_of(next, struct wc_entry, rb_node);
1397cdd4d783SMike Snitzer 			if (f != e + 1)
1398cdd4d783SMike Snitzer 				break;
1399cdd4d783SMike Snitzer 			if (read_original_sector(wc, f) !=
1400cdd4d783SMike Snitzer 			    read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1401cdd4d783SMike Snitzer 				break;
1402cdd4d783SMike Snitzer 			if (unlikely(f->write_in_progress))
1403cdd4d783SMike Snitzer 				break;
1404cdd4d783SMike Snitzer 			if (writecache_entry_is_committed(wc, f))
1405cdd4d783SMike Snitzer 				wc->overwrote_committed = true;
1406cdd4d783SMike Snitzer 			e = f;
1407cdd4d783SMike Snitzer 		}
1408cdd4d783SMike Snitzer 		bio_size += wc->block_size;
1409cdd4d783SMike Snitzer 		current_cache_sec += wc->block_size >> SECTOR_SHIFT;
1410cdd4d783SMike Snitzer 	}
1411cdd4d783SMike Snitzer 
1412cdd4d783SMike Snitzer 	bio_set_dev(bio, wc->ssd_dev->bdev);
1413cdd4d783SMike Snitzer 	bio->bi_iter.bi_sector = start_cache_sec;
1414cdd4d783SMike Snitzer 	dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT);
1415cdd4d783SMike Snitzer 
1416cdd4d783SMike Snitzer 	if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1417cdd4d783SMike Snitzer 		wc->uncommitted_blocks = 0;
1418cdd4d783SMike Snitzer 		queue_work(wc->writeback_wq, &wc->flush_work);
1419cdd4d783SMike Snitzer 	} else {
1420cdd4d783SMike Snitzer 		writecache_schedule_autocommit(wc);
1421cdd4d783SMike Snitzer 	}
1422cdd4d783SMike Snitzer 
1423cdd4d783SMike Snitzer 	return WC_MAP_REMAP;
1424cdd4d783SMike Snitzer }
1425cdd4d783SMike Snitzer 
1426cdd4d783SMike Snitzer static enum wc_map_op writecache_map_write(struct dm_writecache *wc, struct bio *bio)
142748debafeSMikulas Patocka {
142848debafeSMikulas Patocka 	struct wc_entry *e;
1429cdd4d783SMike Snitzer 
1430cdd4d783SMike Snitzer 	do {
1431cdd4d783SMike Snitzer 		bool found_entry = false;
1432cdd4d783SMike Snitzer 		bool search_used = false;
1433e3a35d03SMikulas Patocka 		wc->stats.writes++;
1434cdd4d783SMike Snitzer 		if (writecache_has_error(wc))
1435cdd4d783SMike Snitzer 			return WC_MAP_ERROR;
1436cdd4d783SMike Snitzer 		e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1437cdd4d783SMike Snitzer 		if (e) {
1438cdd4d783SMike Snitzer 			if (!writecache_entry_is_committed(wc, e)) {
1439e3a35d03SMikulas Patocka 				wc->stats.write_hits_uncommitted++;
1440cdd4d783SMike Snitzer 				search_used = true;
1441cdd4d783SMike Snitzer 				goto bio_copy;
1442cdd4d783SMike Snitzer 			}
1443e3a35d03SMikulas Patocka 			wc->stats.write_hits_committed++;
1444cdd4d783SMike Snitzer 			if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1445cdd4d783SMike Snitzer 				wc->overwrote_committed = true;
1446cdd4d783SMike Snitzer 				search_used = true;
1447cdd4d783SMike Snitzer 				goto bio_copy;
1448cdd4d783SMike Snitzer 			}
1449cdd4d783SMike Snitzer 			found_entry = true;
1450cdd4d783SMike Snitzer 		} else {
1451cdd4d783SMike Snitzer 			if (unlikely(wc->cleaner) ||
1452cdd4d783SMike Snitzer 			    (wc->metadata_only && !(bio->bi_opf & REQ_META)))
1453cdd4d783SMike Snitzer 				goto direct_write;
1454cdd4d783SMike Snitzer 		}
1455cdd4d783SMike Snitzer 		e = writecache_pop_from_freelist(wc, (sector_t)-1);
1456cdd4d783SMike Snitzer 		if (unlikely(!e)) {
1457cdd4d783SMike Snitzer 			if (!WC_MODE_PMEM(wc) && !found_entry) {
1458cdd4d783SMike Snitzer direct_write:
1459e3a35d03SMikulas Patocka 				wc->stats.writes_around++;
1460cdd4d783SMike Snitzer 				e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
14614d020b3aSMike Snitzer 				return writecache_map_remap_origin(wc, bio, e);
1462cdd4d783SMike Snitzer 			}
1463e3a35d03SMikulas Patocka 			wc->stats.writes_blocked_on_freelist++;
1464cdd4d783SMike Snitzer 			writecache_wait_on_freelist(wc);
1465cdd4d783SMike Snitzer 			continue;
1466cdd4d783SMike Snitzer 		}
1467cdd4d783SMike Snitzer 		write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1468cdd4d783SMike Snitzer 		writecache_insert_entry(wc, e);
1469cdd4d783SMike Snitzer 		wc->uncommitted_blocks++;
1470e3a35d03SMikulas Patocka 		wc->stats.writes_allocate++;
1471cdd4d783SMike Snitzer bio_copy:
1472cdd4d783SMike Snitzer 		if (WC_MODE_PMEM(wc))
1473cdd4d783SMike Snitzer 			bio_copy_block(wc, bio, memory_data(wc, e));
1474cdd4d783SMike Snitzer 		else
1475cdd4d783SMike Snitzer 			return writecache_bio_copy_ssd(wc, bio, e, search_used);
1476cdd4d783SMike Snitzer 	} while (bio->bi_iter.bi_size);
1477cdd4d783SMike Snitzer 
1478cdd4d783SMike Snitzer 	if (unlikely(bio->bi_opf & REQ_FUA || wc->uncommitted_blocks >= wc->autocommit_blocks))
1479cdd4d783SMike Snitzer 		writecache_flush(wc);
1480cdd4d783SMike Snitzer 	else
1481cdd4d783SMike Snitzer 		writecache_schedule_autocommit(wc);
1482cdd4d783SMike Snitzer 
1483cdd4d783SMike Snitzer 	return WC_MAP_SUBMIT;
1484cdd4d783SMike Snitzer }
1485cdd4d783SMike Snitzer 
148615cb6f39SMike Snitzer static enum wc_map_op writecache_map_flush(struct dm_writecache *wc, struct bio *bio)
148715cb6f39SMike Snitzer {
148815cb6f39SMike Snitzer 	if (writecache_has_error(wc))
148915cb6f39SMike Snitzer 		return WC_MAP_ERROR;
149015cb6f39SMike Snitzer 
149115cb6f39SMike Snitzer 	if (WC_MODE_PMEM(wc)) {
1492e3a35d03SMikulas Patocka 		wc->stats.flushes++;
149315cb6f39SMike Snitzer 		writecache_flush(wc);
149415cb6f39SMike Snitzer 		if (writecache_has_error(wc))
149515cb6f39SMike Snitzer 			return WC_MAP_ERROR;
149615cb6f39SMike Snitzer 		else if (unlikely(wc->cleaner) || unlikely(wc->metadata_only))
149715cb6f39SMike Snitzer 			return WC_MAP_REMAP_ORIGIN;
149815cb6f39SMike Snitzer 		return WC_MAP_SUBMIT;
149915cb6f39SMike Snitzer 	}
150015cb6f39SMike Snitzer 	/* SSD: */
150115cb6f39SMike Snitzer 	if (dm_bio_get_target_bio_nr(bio))
150215cb6f39SMike Snitzer 		return WC_MAP_REMAP_ORIGIN;
1503e3a35d03SMikulas Patocka 	wc->stats.flushes++;
150415cb6f39SMike Snitzer 	writecache_offload_bio(wc, bio);
150515cb6f39SMike Snitzer 	return WC_MAP_RETURN;
150615cb6f39SMike Snitzer }
150715cb6f39SMike Snitzer 
150815cb6f39SMike Snitzer static enum wc_map_op writecache_map_discard(struct dm_writecache *wc, struct bio *bio)
150915cb6f39SMike Snitzer {
1510e3a35d03SMikulas Patocka 	wc->stats.discards++;
1511e3a35d03SMikulas Patocka 
151215cb6f39SMike Snitzer 	if (writecache_has_error(wc))
151315cb6f39SMike Snitzer 		return WC_MAP_ERROR;
151415cb6f39SMike Snitzer 
151515cb6f39SMike Snitzer 	if (WC_MODE_PMEM(wc)) {
151615cb6f39SMike Snitzer 		writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
151715cb6f39SMike Snitzer 		return WC_MAP_REMAP_ORIGIN;
151815cb6f39SMike Snitzer 	}
151915cb6f39SMike Snitzer 	/* SSD: */
152015cb6f39SMike Snitzer 	writecache_offload_bio(wc, bio);
152115cb6f39SMike Snitzer 	return WC_MAP_RETURN;
152215cb6f39SMike Snitzer }
152315cb6f39SMike Snitzer 
1524cdd4d783SMike Snitzer static int writecache_map(struct dm_target *ti, struct bio *bio)
1525cdd4d783SMike Snitzer {
152648debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
152715cb6f39SMike Snitzer 	enum wc_map_op map_op;
152848debafeSMikulas Patocka 
152948debafeSMikulas Patocka 	bio->bi_private = NULL;
153048debafeSMikulas Patocka 
153148debafeSMikulas Patocka 	wc_lock(wc);
153248debafeSMikulas Patocka 
153348debafeSMikulas Patocka 	if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
153415cb6f39SMike Snitzer 		map_op = writecache_map_flush(wc, bio);
153515cb6f39SMike Snitzer 		goto done;
153648debafeSMikulas Patocka 	}
153748debafeSMikulas Patocka 
153848debafeSMikulas Patocka 	bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
153948debafeSMikulas Patocka 
154048debafeSMikulas Patocka 	if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
154148debafeSMikulas Patocka 				(wc->block_size / 512 - 1)) != 0)) {
154248debafeSMikulas Patocka 		DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
154348debafeSMikulas Patocka 		      (unsigned long long)bio->bi_iter.bi_sector,
154448debafeSMikulas Patocka 		      bio->bi_iter.bi_size, wc->block_size);
154515cb6f39SMike Snitzer 		map_op = WC_MAP_ERROR;
154615cb6f39SMike Snitzer 		goto done;
154748debafeSMikulas Patocka 	}
154848debafeSMikulas Patocka 
154948debafeSMikulas Patocka 	if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
155015cb6f39SMike Snitzer 		map_op = writecache_map_discard(wc, bio);
155115cb6f39SMike Snitzer 		goto done;
155248debafeSMikulas Patocka 	}
155348debafeSMikulas Patocka 
1554cdd4d783SMike Snitzer 	if (bio_data_dir(bio) == READ)
1555cdd4d783SMike Snitzer 		map_op = writecache_map_read(wc, bio);
155648debafeSMikulas Patocka 	else
1557cdd4d783SMike Snitzer 		map_op = writecache_map_write(wc, bio);
155815cb6f39SMike Snitzer done:
1559cdd4d783SMike Snitzer 	switch (map_op) {
1560cdd4d783SMike Snitzer 	case WC_MAP_REMAP_ORIGIN:
15615c0de3d7SMikulas Patocka 		if (likely(wc->pause != 0)) {
15625c0de3d7SMikulas Patocka 			if (bio_op(bio) == REQ_OP_WRITE) {
156395b88f4dSMikulas Patocka 				dm_iot_io_begin(&wc->iot, 1);
156495b88f4dSMikulas Patocka 				bio->bi_private = (void *)2;
156595b88f4dSMikulas Patocka 			}
15665c0de3d7SMikulas Patocka 		}
156748debafeSMikulas Patocka 		bio_set_dev(bio, wc->dev->bdev);
156848debafeSMikulas Patocka 		wc_unlock(wc);
156948debafeSMikulas Patocka 		return DM_MAPIO_REMAPPED;
157048debafeSMikulas Patocka 
1571cdd4d783SMike Snitzer 	case WC_MAP_REMAP:
157248debafeSMikulas Patocka 		/* make sure that writecache_end_io decrements bio_in_progress: */
157348debafeSMikulas Patocka 		bio->bi_private = (void *)1;
157448debafeSMikulas Patocka 		atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
157548debafeSMikulas Patocka 		wc_unlock(wc);
157648debafeSMikulas Patocka 		return DM_MAPIO_REMAPPED;
157748debafeSMikulas Patocka 
1578cdd4d783SMike Snitzer 	case WC_MAP_SUBMIT:
157948debafeSMikulas Patocka 		wc_unlock(wc);
158048debafeSMikulas Patocka 		bio_endio(bio);
158148debafeSMikulas Patocka 		return DM_MAPIO_SUBMITTED;
158248debafeSMikulas Patocka 
1583cdd4d783SMike Snitzer 	case WC_MAP_RETURN:
158448debafeSMikulas Patocka 		wc_unlock(wc);
158548debafeSMikulas Patocka 		return DM_MAPIO_SUBMITTED;
158648debafeSMikulas Patocka 
1587cdd4d783SMike Snitzer 	case WC_MAP_ERROR:
158848debafeSMikulas Patocka 		wc_unlock(wc);
158948debafeSMikulas Patocka 		bio_io_error(bio);
159048debafeSMikulas Patocka 		return DM_MAPIO_SUBMITTED;
1591df699cc1SMikulas Patocka 
1592df699cc1SMikulas Patocka 	default:
1593df699cc1SMikulas Patocka 		BUG();
1594df699cc1SMikulas Patocka 		return -1;
159548debafeSMikulas Patocka 	}
1596cdd4d783SMike Snitzer }
159748debafeSMikulas Patocka 
159848debafeSMikulas Patocka static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
159948debafeSMikulas Patocka {
160048debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
160148debafeSMikulas Patocka 
160295b88f4dSMikulas Patocka 	if (bio->bi_private == (void *)1) {
160348debafeSMikulas Patocka 		int dir = bio_data_dir(bio);
160448debafeSMikulas Patocka 		if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
160548debafeSMikulas Patocka 			if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
160648debafeSMikulas Patocka 				wake_up(&wc->bio_in_progress_wait[dir]);
160795b88f4dSMikulas Patocka 	} else if (bio->bi_private == (void *)2) {
160895b88f4dSMikulas Patocka 		dm_iot_io_end(&wc->iot, 1);
160948debafeSMikulas Patocka 	}
161048debafeSMikulas Patocka 	return 0;
161148debafeSMikulas Patocka }
161248debafeSMikulas Patocka 
161348debafeSMikulas Patocka static int writecache_iterate_devices(struct dm_target *ti,
161448debafeSMikulas Patocka 				      iterate_devices_callout_fn fn, void *data)
161548debafeSMikulas Patocka {
161648debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
161748debafeSMikulas Patocka 
161848debafeSMikulas Patocka 	return fn(ti, wc->dev, 0, ti->len, data);
161948debafeSMikulas Patocka }
162048debafeSMikulas Patocka 
162148debafeSMikulas Patocka static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
162248debafeSMikulas Patocka {
162348debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
162448debafeSMikulas Patocka 
162548debafeSMikulas Patocka 	if (limits->logical_block_size < wc->block_size)
162648debafeSMikulas Patocka 		limits->logical_block_size = wc->block_size;
162748debafeSMikulas Patocka 
162848debafeSMikulas Patocka 	if (limits->physical_block_size < wc->block_size)
162948debafeSMikulas Patocka 		limits->physical_block_size = wc->block_size;
163048debafeSMikulas Patocka 
163148debafeSMikulas Patocka 	if (limits->io_min < wc->block_size)
163248debafeSMikulas Patocka 		limits->io_min = wc->block_size;
163348debafeSMikulas Patocka }
163448debafeSMikulas Patocka 
163548debafeSMikulas Patocka 
163648debafeSMikulas Patocka static void writecache_writeback_endio(struct bio *bio)
163748debafeSMikulas Patocka {
163848debafeSMikulas Patocka 	struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
163948debafeSMikulas Patocka 	struct dm_writecache *wc = wb->wc;
164048debafeSMikulas Patocka 	unsigned long flags;
164148debafeSMikulas Patocka 
164248debafeSMikulas Patocka 	raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
164348debafeSMikulas Patocka 	if (unlikely(list_empty(&wc->endio_list)))
164448debafeSMikulas Patocka 		wake_up_process(wc->endio_thread);
164548debafeSMikulas Patocka 	list_add_tail(&wb->endio_entry, &wc->endio_list);
164648debafeSMikulas Patocka 	raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
164748debafeSMikulas Patocka }
164848debafeSMikulas Patocka 
164948debafeSMikulas Patocka static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
165048debafeSMikulas Patocka {
165148debafeSMikulas Patocka 	struct copy_struct *c = ptr;
165248debafeSMikulas Patocka 	struct dm_writecache *wc = c->wc;
165348debafeSMikulas Patocka 
165448debafeSMikulas Patocka 	c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
165548debafeSMikulas Patocka 
165648debafeSMikulas Patocka 	raw_spin_lock_irq(&wc->endio_list_lock);
165748debafeSMikulas Patocka 	if (unlikely(list_empty(&wc->endio_list)))
165848debafeSMikulas Patocka 		wake_up_process(wc->endio_thread);
165948debafeSMikulas Patocka 	list_add_tail(&c->endio_entry, &wc->endio_list);
166048debafeSMikulas Patocka 	raw_spin_unlock_irq(&wc->endio_list_lock);
166148debafeSMikulas Patocka }
166248debafeSMikulas Patocka 
166348debafeSMikulas Patocka static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
166448debafeSMikulas Patocka {
166548debafeSMikulas Patocka 	unsigned i;
166648debafeSMikulas Patocka 	struct writeback_struct *wb;
166748debafeSMikulas Patocka 	struct wc_entry *e;
166848debafeSMikulas Patocka 	unsigned long n_walked = 0;
166948debafeSMikulas Patocka 
167048debafeSMikulas Patocka 	do {
167148debafeSMikulas Patocka 		wb = list_entry(list->next, struct writeback_struct, endio_entry);
167248debafeSMikulas Patocka 		list_del(&wb->endio_entry);
167348debafeSMikulas Patocka 
167448debafeSMikulas Patocka 		if (unlikely(wb->bio.bi_status != BLK_STS_OK))
167548debafeSMikulas Patocka 			writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
167648debafeSMikulas Patocka 					"write error %d", wb->bio.bi_status);
167748debafeSMikulas Patocka 		i = 0;
167848debafeSMikulas Patocka 		do {
167948debafeSMikulas Patocka 			e = wb->wc_list[i];
168048debafeSMikulas Patocka 			BUG_ON(!e->write_in_progress);
168148debafeSMikulas Patocka 			e->write_in_progress = false;
168248debafeSMikulas Patocka 			INIT_LIST_HEAD(&e->lru);
168348debafeSMikulas Patocka 			if (!writecache_has_error(wc))
168448debafeSMikulas Patocka 				writecache_free_entry(wc, e);
168548debafeSMikulas Patocka 			BUG_ON(!wc->writeback_size);
168648debafeSMikulas Patocka 			wc->writeback_size--;
168748debafeSMikulas Patocka 			n_walked++;
168848debafeSMikulas Patocka 			if (unlikely(n_walked >= ENDIO_LATENCY)) {
1689aa950920SMikulas Patocka 				writecache_commit_flushed(wc, false);
169048debafeSMikulas Patocka 				wc_unlock(wc);
169148debafeSMikulas Patocka 				wc_lock(wc);
169248debafeSMikulas Patocka 				n_walked = 0;
169348debafeSMikulas Patocka 			}
169448debafeSMikulas Patocka 		} while (++i < wb->wc_list_n);
169548debafeSMikulas Patocka 
169648debafeSMikulas Patocka 		if (wb->wc_list != wb->wc_list_inline)
169748debafeSMikulas Patocka 			kfree(wb->wc_list);
169848debafeSMikulas Patocka 		bio_put(&wb->bio);
169948debafeSMikulas Patocka 	} while (!list_empty(list));
170048debafeSMikulas Patocka }
170148debafeSMikulas Patocka 
170248debafeSMikulas Patocka static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
170348debafeSMikulas Patocka {
170448debafeSMikulas Patocka 	struct copy_struct *c;
170548debafeSMikulas Patocka 	struct wc_entry *e;
170648debafeSMikulas Patocka 
170748debafeSMikulas Patocka 	do {
170848debafeSMikulas Patocka 		c = list_entry(list->next, struct copy_struct, endio_entry);
170948debafeSMikulas Patocka 		list_del(&c->endio_entry);
171048debafeSMikulas Patocka 
171148debafeSMikulas Patocka 		if (unlikely(c->error))
171248debafeSMikulas Patocka 			writecache_error(wc, c->error, "copy error");
171348debafeSMikulas Patocka 
171448debafeSMikulas Patocka 		e = c->e;
171548debafeSMikulas Patocka 		do {
171648debafeSMikulas Patocka 			BUG_ON(!e->write_in_progress);
171748debafeSMikulas Patocka 			e->write_in_progress = false;
171848debafeSMikulas Patocka 			INIT_LIST_HEAD(&e->lru);
171948debafeSMikulas Patocka 			if (!writecache_has_error(wc))
172048debafeSMikulas Patocka 				writecache_free_entry(wc, e);
172148debafeSMikulas Patocka 
172248debafeSMikulas Patocka 			BUG_ON(!wc->writeback_size);
172348debafeSMikulas Patocka 			wc->writeback_size--;
172448debafeSMikulas Patocka 			e++;
172548debafeSMikulas Patocka 		} while (--c->n_entries);
172648debafeSMikulas Patocka 		mempool_free(c, &wc->copy_pool);
172748debafeSMikulas Patocka 	} while (!list_empty(list));
172848debafeSMikulas Patocka }
172948debafeSMikulas Patocka 
173048debafeSMikulas Patocka static int writecache_endio_thread(void *data)
173148debafeSMikulas Patocka {
173248debafeSMikulas Patocka 	struct dm_writecache *wc = data;
173348debafeSMikulas Patocka 
173448debafeSMikulas Patocka 	while (1) {
173548debafeSMikulas Patocka 		struct list_head list;
173648debafeSMikulas Patocka 
173748debafeSMikulas Patocka 		raw_spin_lock_irq(&wc->endio_list_lock);
173848debafeSMikulas Patocka 		if (!list_empty(&wc->endio_list))
173948debafeSMikulas Patocka 			goto pop_from_list;
174048debafeSMikulas Patocka 		set_current_state(TASK_INTERRUPTIBLE);
174148debafeSMikulas Patocka 		raw_spin_unlock_irq(&wc->endio_list_lock);
174248debafeSMikulas Patocka 
174348debafeSMikulas Patocka 		if (unlikely(kthread_should_stop())) {
174448debafeSMikulas Patocka 			set_current_state(TASK_RUNNING);
174548debafeSMikulas Patocka 			break;
174648debafeSMikulas Patocka 		}
174748debafeSMikulas Patocka 
174848debafeSMikulas Patocka 		schedule();
174948debafeSMikulas Patocka 
175048debafeSMikulas Patocka 		continue;
175148debafeSMikulas Patocka 
175248debafeSMikulas Patocka pop_from_list:
175348debafeSMikulas Patocka 		list = wc->endio_list;
175448debafeSMikulas Patocka 		list.next->prev = list.prev->next = &list;
175548debafeSMikulas Patocka 		INIT_LIST_HEAD(&wc->endio_list);
175648debafeSMikulas Patocka 		raw_spin_unlock_irq(&wc->endio_list_lock);
175748debafeSMikulas Patocka 
175848debafeSMikulas Patocka 		if (!WC_MODE_FUA(wc))
175948debafeSMikulas Patocka 			writecache_disk_flush(wc, wc->dev);
176048debafeSMikulas Patocka 
176148debafeSMikulas Patocka 		wc_lock(wc);
176248debafeSMikulas Patocka 
176348debafeSMikulas Patocka 		if (WC_MODE_PMEM(wc)) {
176448debafeSMikulas Patocka 			__writecache_endio_pmem(wc, &list);
176548debafeSMikulas Patocka 		} else {
176648debafeSMikulas Patocka 			__writecache_endio_ssd(wc, &list);
176748debafeSMikulas Patocka 			writecache_wait_for_ios(wc, READ);
176848debafeSMikulas Patocka 		}
176948debafeSMikulas Patocka 
1770aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
177148debafeSMikulas Patocka 
177248debafeSMikulas Patocka 		wc_unlock(wc);
177348debafeSMikulas Patocka 	}
177448debafeSMikulas Patocka 
177548debafeSMikulas Patocka 	return 0;
177648debafeSMikulas Patocka }
177748debafeSMikulas Patocka 
1778620cbe40SMikulas Patocka static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e)
177948debafeSMikulas Patocka {
178048debafeSMikulas Patocka 	struct dm_writecache *wc = wb->wc;
178148debafeSMikulas Patocka 	unsigned block_size = wc->block_size;
178248debafeSMikulas Patocka 	void *address = memory_data(wc, e);
178348debafeSMikulas Patocka 
178448debafeSMikulas Patocka 	persistent_memory_flush_cache(address, block_size);
17854134455fSMikulas Patocka 
17864134455fSMikulas Patocka 	if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors))
17874134455fSMikulas Patocka 		return true;
17884134455fSMikulas Patocka 
178948debafeSMikulas Patocka 	return bio_add_page(&wb->bio, persistent_memory_page(address),
179048debafeSMikulas Patocka 			    block_size, persistent_memory_page_offset(address)) != 0;
179148debafeSMikulas Patocka }
179248debafeSMikulas Patocka 
179348debafeSMikulas Patocka struct writeback_list {
179448debafeSMikulas Patocka 	struct list_head list;
179548debafeSMikulas Patocka 	size_t size;
179648debafeSMikulas Patocka };
179748debafeSMikulas Patocka 
179848debafeSMikulas Patocka static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
179948debafeSMikulas Patocka {
180048debafeSMikulas Patocka 	if (unlikely(wc->max_writeback_jobs)) {
180148debafeSMikulas Patocka 		if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
180248debafeSMikulas Patocka 			wc_lock(wc);
180348debafeSMikulas Patocka 			while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
180448debafeSMikulas Patocka 				writecache_wait_on_freelist(wc);
180548debafeSMikulas Patocka 			wc_unlock(wc);
180648debafeSMikulas Patocka 		}
180748debafeSMikulas Patocka 	}
180848debafeSMikulas Patocka 	cond_resched();
180948debafeSMikulas Patocka }
181048debafeSMikulas Patocka 
181148debafeSMikulas Patocka static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
181248debafeSMikulas Patocka {
181348debafeSMikulas Patocka 	struct wc_entry *e, *f;
181448debafeSMikulas Patocka 	struct bio *bio;
181548debafeSMikulas Patocka 	struct writeback_struct *wb;
181648debafeSMikulas Patocka 	unsigned max_pages;
181748debafeSMikulas Patocka 
181848debafeSMikulas Patocka 	while (wbl->size) {
181948debafeSMikulas Patocka 		wbl->size--;
182048debafeSMikulas Patocka 		e = container_of(wbl->list.prev, struct wc_entry, lru);
182148debafeSMikulas Patocka 		list_del(&e->lru);
182248debafeSMikulas Patocka 
182348debafeSMikulas Patocka 		max_pages = e->wc_list_contiguous;
182448debafeSMikulas Patocka 
1825609be106SChristoph Hellwig 		bio = bio_alloc_bioset(wc->dev->bdev, max_pages, REQ_OP_WRITE,
1826609be106SChristoph Hellwig 				       GFP_NOIO, &wc->bio_set);
182748debafeSMikulas Patocka 		wb = container_of(bio, struct writeback_struct, bio);
182848debafeSMikulas Patocka 		wb->wc = wc;
182909f2d656SHuaisheng Ye 		bio->bi_end_io = writecache_writeback_endio;
183009f2d656SHuaisheng Ye 		bio->bi_iter.bi_sector = read_original_sector(wc, e);
183148debafeSMikulas Patocka 		if (max_pages <= WB_LIST_INLINE ||
183250a7d3baSKees Cook 		    unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
183348debafeSMikulas Patocka 							   GFP_NOIO | __GFP_NORETRY |
183448debafeSMikulas Patocka 							   __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
183548debafeSMikulas Patocka 			wb->wc_list = wb->wc_list_inline;
183648debafeSMikulas Patocka 			max_pages = WB_LIST_INLINE;
183748debafeSMikulas Patocka 		}
183848debafeSMikulas Patocka 
1839620cbe40SMikulas Patocka 		BUG_ON(!wc_add_block(wb, e));
184048debafeSMikulas Patocka 
184148debafeSMikulas Patocka 		wb->wc_list[0] = e;
184248debafeSMikulas Patocka 		wb->wc_list_n = 1;
184348debafeSMikulas Patocka 
184448debafeSMikulas Patocka 		while (wbl->size && wb->wc_list_n < max_pages) {
184548debafeSMikulas Patocka 			f = container_of(wbl->list.prev, struct wc_entry, lru);
184648debafeSMikulas Patocka 			if (read_original_sector(wc, f) !=
184748debafeSMikulas Patocka 			    read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
184848debafeSMikulas Patocka 				break;
1849620cbe40SMikulas Patocka 			if (!wc_add_block(wb, f))
185048debafeSMikulas Patocka 				break;
185148debafeSMikulas Patocka 			wbl->size--;
185248debafeSMikulas Patocka 			list_del(&f->lru);
185348debafeSMikulas Patocka 			wb->wc_list[wb->wc_list_n++] = f;
185448debafeSMikulas Patocka 			e = f;
185548debafeSMikulas Patocka 		}
1856609be106SChristoph Hellwig 		if (WC_MODE_FUA(wc))
1857609be106SChristoph Hellwig 			bio->bi_opf |= REQ_FUA;
185848debafeSMikulas Patocka 		if (writecache_has_error(wc)) {
185948debafeSMikulas Patocka 			bio->bi_status = BLK_STS_IOERR;
186009f2d656SHuaisheng Ye 			bio_endio(bio);
18614134455fSMikulas Patocka 		} else if (unlikely(!bio_sectors(bio))) {
18624134455fSMikulas Patocka 			bio->bi_status = BLK_STS_OK;
18634134455fSMikulas Patocka 			bio_endio(bio);
186448debafeSMikulas Patocka 		} else {
186509f2d656SHuaisheng Ye 			submit_bio(bio);
186648debafeSMikulas Patocka 		}
186748debafeSMikulas Patocka 
186848debafeSMikulas Patocka 		__writeback_throttle(wc, wbl);
186948debafeSMikulas Patocka 	}
187048debafeSMikulas Patocka }
187148debafeSMikulas Patocka 
187248debafeSMikulas Patocka static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
187348debafeSMikulas Patocka {
187448debafeSMikulas Patocka 	struct wc_entry *e, *f;
187548debafeSMikulas Patocka 	struct dm_io_region from, to;
187648debafeSMikulas Patocka 	struct copy_struct *c;
187748debafeSMikulas Patocka 
187848debafeSMikulas Patocka 	while (wbl->size) {
187948debafeSMikulas Patocka 		unsigned n_sectors;
188048debafeSMikulas Patocka 
188148debafeSMikulas Patocka 		wbl->size--;
188248debafeSMikulas Patocka 		e = container_of(wbl->list.prev, struct wc_entry, lru);
188348debafeSMikulas Patocka 		list_del(&e->lru);
188448debafeSMikulas Patocka 
188548debafeSMikulas Patocka 		n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
188648debafeSMikulas Patocka 
188748debafeSMikulas Patocka 		from.bdev = wc->ssd_dev->bdev;
188848debafeSMikulas Patocka 		from.sector = cache_sector(wc, e);
188948debafeSMikulas Patocka 		from.count = n_sectors;
189048debafeSMikulas Patocka 		to.bdev = wc->dev->bdev;
189148debafeSMikulas Patocka 		to.sector = read_original_sector(wc, e);
189248debafeSMikulas Patocka 		to.count = n_sectors;
189348debafeSMikulas Patocka 
189448debafeSMikulas Patocka 		c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
189548debafeSMikulas Patocka 		c->wc = wc;
189648debafeSMikulas Patocka 		c->e = e;
189748debafeSMikulas Patocka 		c->n_entries = e->wc_list_contiguous;
189848debafeSMikulas Patocka 
189948debafeSMikulas Patocka 		while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
190048debafeSMikulas Patocka 			wbl->size--;
190148debafeSMikulas Patocka 			f = container_of(wbl->list.prev, struct wc_entry, lru);
190248debafeSMikulas Patocka 			BUG_ON(f != e + 1);
190348debafeSMikulas Patocka 			list_del(&f->lru);
190448debafeSMikulas Patocka 			e = f;
190548debafeSMikulas Patocka 		}
190648debafeSMikulas Patocka 
19074134455fSMikulas Patocka 		if (unlikely(to.sector + to.count > wc->data_device_sectors)) {
19084134455fSMikulas Patocka 			if (to.sector >= wc->data_device_sectors) {
19094134455fSMikulas Patocka 				writecache_copy_endio(0, 0, c);
19104134455fSMikulas Patocka 				continue;
19114134455fSMikulas Patocka 			}
19124134455fSMikulas Patocka 			from.count = to.count = wc->data_device_sectors - to.sector;
19134134455fSMikulas Patocka 		}
19144134455fSMikulas Patocka 
191548debafeSMikulas Patocka 		dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
191648debafeSMikulas Patocka 
191748debafeSMikulas Patocka 		__writeback_throttle(wc, wbl);
191848debafeSMikulas Patocka 	}
191948debafeSMikulas Patocka }
192048debafeSMikulas Patocka 
192148debafeSMikulas Patocka static void writecache_writeback(struct work_struct *work)
192248debafeSMikulas Patocka {
192348debafeSMikulas Patocka 	struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
192448debafeSMikulas Patocka 	struct blk_plug plug;
19253f649ab7SKees Cook 	struct wc_entry *f, *g, *e = NULL;
192648debafeSMikulas Patocka 	struct rb_node *node, *next_node;
192748debafeSMikulas Patocka 	struct list_head skipped;
192848debafeSMikulas Patocka 	struct writeback_list wbl;
192948debafeSMikulas Patocka 	unsigned long n_walked;
193048debafeSMikulas Patocka 
1931293128b1SMikulas Patocka 	if (!WC_MODE_PMEM(wc)) {
1932293128b1SMikulas Patocka 		/* Wait for any active kcopyd work on behalf of ssd writeback */
1933293128b1SMikulas Patocka 		dm_kcopyd_client_flush(wc->dm_kcopyd);
1934293128b1SMikulas Patocka 	}
1935293128b1SMikulas Patocka 
19365c0de3d7SMikulas Patocka 	if (likely(wc->pause != 0)) {
19375c0de3d7SMikulas Patocka 		while (1) {
19385c0de3d7SMikulas Patocka 			unsigned long idle;
19395c0de3d7SMikulas Patocka 			if (unlikely(wc->cleaner) || unlikely(wc->writeback_all) ||
19405c0de3d7SMikulas Patocka 			    unlikely(dm_suspended(wc->ti)))
19415c0de3d7SMikulas Patocka 				break;
19425c0de3d7SMikulas Patocka 			idle = dm_iot_idle_time(&wc->iot);
19435c0de3d7SMikulas Patocka 			if (idle >= wc->pause)
19445c0de3d7SMikulas Patocka 				break;
19455c0de3d7SMikulas Patocka 			idle = wc->pause - idle;
19465c0de3d7SMikulas Patocka 			if (idle > HZ)
19475c0de3d7SMikulas Patocka 				idle = HZ;
19485c0de3d7SMikulas Patocka 			schedule_timeout_idle(idle);
194995b88f4dSMikulas Patocka 		}
195095b88f4dSMikulas Patocka 	}
195195b88f4dSMikulas Patocka 
195248debafeSMikulas Patocka 	wc_lock(wc);
195348debafeSMikulas Patocka restart:
195448debafeSMikulas Patocka 	if (writecache_has_error(wc)) {
195548debafeSMikulas Patocka 		wc_unlock(wc);
195648debafeSMikulas Patocka 		return;
195748debafeSMikulas Patocka 	}
195848debafeSMikulas Patocka 
195948debafeSMikulas Patocka 	if (unlikely(wc->writeback_all)) {
196048debafeSMikulas Patocka 		if (writecache_wait_for_writeback(wc))
196148debafeSMikulas Patocka 			goto restart;
196248debafeSMikulas Patocka 	}
196348debafeSMikulas Patocka 
196448debafeSMikulas Patocka 	if (wc->overwrote_committed) {
196548debafeSMikulas Patocka 		writecache_wait_for_ios(wc, WRITE);
196648debafeSMikulas Patocka 	}
196748debafeSMikulas Patocka 
196848debafeSMikulas Patocka 	n_walked = 0;
196948debafeSMikulas Patocka 	INIT_LIST_HEAD(&skipped);
197048debafeSMikulas Patocka 	INIT_LIST_HEAD(&wbl.list);
197148debafeSMikulas Patocka 	wbl.size = 0;
197248debafeSMikulas Patocka 	while (!list_empty(&wc->lru) &&
197348debafeSMikulas Patocka 	       (wc->writeback_all ||
19743923d485SMikulas Patocka 		wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark ||
19753923d485SMikulas Patocka 		(jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >=
19763923d485SMikulas Patocka 		 wc->max_age - wc->max_age / MAX_AGE_DIV))) {
197748debafeSMikulas Patocka 
197848debafeSMikulas Patocka 		n_walked++;
197948debafeSMikulas Patocka 		if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1980af4f6cabSMikulas Patocka 		    likely(!wc->writeback_all)) {
1981af4f6cabSMikulas Patocka 			if (likely(!dm_suspended(wc->ti)))
198248debafeSMikulas Patocka 				queue_work(wc->writeback_wq, &wc->writeback_work);
198348debafeSMikulas Patocka 			break;
198448debafeSMikulas Patocka 		}
198548debafeSMikulas Patocka 
19865229b489SHuaisheng Ye 		if (unlikely(wc->writeback_all)) {
19875229b489SHuaisheng Ye 			if (unlikely(!e)) {
19885229b489SHuaisheng Ye 				writecache_flush(wc);
19895229b489SHuaisheng Ye 				e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
19905229b489SHuaisheng Ye 			} else
19915229b489SHuaisheng Ye 				e = g;
19925229b489SHuaisheng Ye 		} else
199348debafeSMikulas Patocka 			e = container_of(wc->lru.prev, struct wc_entry, lru);
199448debafeSMikulas Patocka 		BUG_ON(e->write_in_progress);
199548debafeSMikulas Patocka 		if (unlikely(!writecache_entry_is_committed(wc, e))) {
199648debafeSMikulas Patocka 			writecache_flush(wc);
199748debafeSMikulas Patocka 		}
199848debafeSMikulas Patocka 		node = rb_prev(&e->rb_node);
199948debafeSMikulas Patocka 		if (node) {
200048debafeSMikulas Patocka 			f = container_of(node, struct wc_entry, rb_node);
200148debafeSMikulas Patocka 			if (unlikely(read_original_sector(wc, f) ==
200248debafeSMikulas Patocka 				     read_original_sector(wc, e))) {
200348debafeSMikulas Patocka 				BUG_ON(!f->write_in_progress);
20048c77f1cbSBaokun Li 				list_move(&e->lru, &skipped);
200548debafeSMikulas Patocka 				cond_resched();
200648debafeSMikulas Patocka 				continue;
200748debafeSMikulas Patocka 			}
200848debafeSMikulas Patocka 		}
200948debafeSMikulas Patocka 		wc->writeback_size++;
20108c77f1cbSBaokun Li 		list_move(&e->lru, &wbl.list);
201148debafeSMikulas Patocka 		wbl.size++;
201248debafeSMikulas Patocka 		e->write_in_progress = true;
201348debafeSMikulas Patocka 		e->wc_list_contiguous = 1;
201448debafeSMikulas Patocka 
201548debafeSMikulas Patocka 		f = e;
201648debafeSMikulas Patocka 
201748debafeSMikulas Patocka 		while (1) {
201848debafeSMikulas Patocka 			next_node = rb_next(&f->rb_node);
201948debafeSMikulas Patocka 			if (unlikely(!next_node))
202048debafeSMikulas Patocka 				break;
202148debafeSMikulas Patocka 			g = container_of(next_node, struct wc_entry, rb_node);
202262421b38SHuaisheng Ye 			if (unlikely(read_original_sector(wc, g) ==
202362421b38SHuaisheng Ye 			    read_original_sector(wc, f))) {
202448debafeSMikulas Patocka 				f = g;
202548debafeSMikulas Patocka 				continue;
202648debafeSMikulas Patocka 			}
202748debafeSMikulas Patocka 			if (read_original_sector(wc, g) !=
202848debafeSMikulas Patocka 			    read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
202948debafeSMikulas Patocka 				break;
203048debafeSMikulas Patocka 			if (unlikely(g->write_in_progress))
203148debafeSMikulas Patocka 				break;
203248debafeSMikulas Patocka 			if (unlikely(!writecache_entry_is_committed(wc, g)))
203348debafeSMikulas Patocka 				break;
203448debafeSMikulas Patocka 
203548debafeSMikulas Patocka 			if (!WC_MODE_PMEM(wc)) {
203648debafeSMikulas Patocka 				if (g != f + 1)
203748debafeSMikulas Patocka 					break;
203848debafeSMikulas Patocka 			}
203948debafeSMikulas Patocka 
204048debafeSMikulas Patocka 			n_walked++;
204148debafeSMikulas Patocka 			//if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
204248debafeSMikulas Patocka 			//	break;
204348debafeSMikulas Patocka 
204448debafeSMikulas Patocka 			wc->writeback_size++;
20458c77f1cbSBaokun Li 			list_move(&g->lru, &wbl.list);
204648debafeSMikulas Patocka 			wbl.size++;
204748debafeSMikulas Patocka 			g->write_in_progress = true;
2048a8affc03SChristoph Hellwig 			g->wc_list_contiguous = BIO_MAX_VECS;
204948debafeSMikulas Patocka 			f = g;
205048debafeSMikulas Patocka 			e->wc_list_contiguous++;
2051a8affc03SChristoph Hellwig 			if (unlikely(e->wc_list_contiguous == BIO_MAX_VECS)) {
20525229b489SHuaisheng Ye 				if (unlikely(wc->writeback_all)) {
20535229b489SHuaisheng Ye 					next_node = rb_next(&f->rb_node);
20545229b489SHuaisheng Ye 					if (likely(next_node))
20555229b489SHuaisheng Ye 						g = container_of(next_node, struct wc_entry, rb_node);
20565229b489SHuaisheng Ye 				}
205748debafeSMikulas Patocka 				break;
205848debafeSMikulas Patocka 			}
20595229b489SHuaisheng Ye 		}
206048debafeSMikulas Patocka 		cond_resched();
206148debafeSMikulas Patocka 	}
206248debafeSMikulas Patocka 
206348debafeSMikulas Patocka 	if (!list_empty(&skipped)) {
206448debafeSMikulas Patocka 		list_splice_tail(&skipped, &wc->lru);
206548debafeSMikulas Patocka 		/*
206648debafeSMikulas Patocka 		 * If we didn't do any progress, we must wait until some
206748debafeSMikulas Patocka 		 * writeback finishes to avoid burning CPU in a loop
206848debafeSMikulas Patocka 		 */
206948debafeSMikulas Patocka 		if (unlikely(!wbl.size))
207048debafeSMikulas Patocka 			writecache_wait_for_writeback(wc);
207148debafeSMikulas Patocka 	}
207248debafeSMikulas Patocka 
207348debafeSMikulas Patocka 	wc_unlock(wc);
207448debafeSMikulas Patocka 
207548debafeSMikulas Patocka 	blk_start_plug(&plug);
207648debafeSMikulas Patocka 
207748debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
207848debafeSMikulas Patocka 		__writecache_writeback_pmem(wc, &wbl);
207948debafeSMikulas Patocka 	else
208048debafeSMikulas Patocka 		__writecache_writeback_ssd(wc, &wbl);
208148debafeSMikulas Patocka 
208248debafeSMikulas Patocka 	blk_finish_plug(&plug);
208348debafeSMikulas Patocka 
208448debafeSMikulas Patocka 	if (unlikely(wc->writeback_all)) {
208548debafeSMikulas Patocka 		wc_lock(wc);
208648debafeSMikulas Patocka 		while (writecache_wait_for_writeback(wc));
208748debafeSMikulas Patocka 		wc_unlock(wc);
208848debafeSMikulas Patocka 	}
208948debafeSMikulas Patocka }
209048debafeSMikulas Patocka 
209148debafeSMikulas Patocka static int calculate_memory_size(uint64_t device_size, unsigned block_size,
209248debafeSMikulas Patocka 				 size_t *n_blocks_p, size_t *n_metadata_blocks_p)
209348debafeSMikulas Patocka {
209448debafeSMikulas Patocka 	uint64_t n_blocks, offset;
209548debafeSMikulas Patocka 	struct wc_entry e;
209648debafeSMikulas Patocka 
209748debafeSMikulas Patocka 	n_blocks = device_size;
209848debafeSMikulas Patocka 	do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
209948debafeSMikulas Patocka 
210048debafeSMikulas Patocka 	while (1) {
210148debafeSMikulas Patocka 		if (!n_blocks)
210248debafeSMikulas Patocka 			return -ENOSPC;
210348debafeSMikulas Patocka 		/* Verify the following entries[n_blocks] won't overflow */
210448debafeSMikulas Patocka 		if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
210548debafeSMikulas Patocka 				 sizeof(struct wc_memory_entry)))
210648debafeSMikulas Patocka 			return -EFBIG;
210748debafeSMikulas Patocka 		offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
210848debafeSMikulas Patocka 		offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
210948debafeSMikulas Patocka 		if (offset + n_blocks * block_size <= device_size)
211048debafeSMikulas Patocka 			break;
211148debafeSMikulas Patocka 		n_blocks--;
211248debafeSMikulas Patocka 	}
211348debafeSMikulas Patocka 
211448debafeSMikulas Patocka 	/* check if the bit field overflows */
211548debafeSMikulas Patocka 	e.index = n_blocks;
211648debafeSMikulas Patocka 	if (e.index != n_blocks)
211748debafeSMikulas Patocka 		return -EFBIG;
211848debafeSMikulas Patocka 
211948debafeSMikulas Patocka 	if (n_blocks_p)
212048debafeSMikulas Patocka 		*n_blocks_p = n_blocks;
212148debafeSMikulas Patocka 	if (n_metadata_blocks_p)
212248debafeSMikulas Patocka 		*n_metadata_blocks_p = offset >> __ffs(block_size);
212348debafeSMikulas Patocka 	return 0;
212448debafeSMikulas Patocka }
212548debafeSMikulas Patocka 
212648debafeSMikulas Patocka static int init_memory(struct dm_writecache *wc)
212748debafeSMikulas Patocka {
212848debafeSMikulas Patocka 	size_t b;
212948debafeSMikulas Patocka 	int r;
213048debafeSMikulas Patocka 
213148debafeSMikulas Patocka 	r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
213248debafeSMikulas Patocka 	if (r)
213348debafeSMikulas Patocka 		return r;
213448debafeSMikulas Patocka 
213548debafeSMikulas Patocka 	r = writecache_alloc_entries(wc);
213648debafeSMikulas Patocka 	if (r)
213748debafeSMikulas Patocka 		return r;
213848debafeSMikulas Patocka 
213948debafeSMikulas Patocka 	for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
214048debafeSMikulas Patocka 		pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
214148debafeSMikulas Patocka 	pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
214248debafeSMikulas Patocka 	pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
214348debafeSMikulas Patocka 	pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
214448debafeSMikulas Patocka 	pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
214548debafeSMikulas Patocka 
21461edaa447SMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
214748debafeSMikulas Patocka 		write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
21481edaa447SMikulas Patocka 		cond_resched();
21491edaa447SMikulas Patocka 	}
215048debafeSMikulas Patocka 
215148debafeSMikulas Patocka 	writecache_flush_all_metadata(wc);
2152aa950920SMikulas Patocka 	writecache_commit_flushed(wc, false);
215348debafeSMikulas Patocka 	pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
215448debafeSMikulas Patocka 	writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
2155aa950920SMikulas Patocka 	writecache_commit_flushed(wc, false);
215648debafeSMikulas Patocka 
215748debafeSMikulas Patocka 	return 0;
215848debafeSMikulas Patocka }
215948debafeSMikulas Patocka 
216048debafeSMikulas Patocka static void writecache_dtr(struct dm_target *ti)
216148debafeSMikulas Patocka {
216248debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
216348debafeSMikulas Patocka 
216448debafeSMikulas Patocka 	if (!wc)
216548debafeSMikulas Patocka 		return;
216648debafeSMikulas Patocka 
216748debafeSMikulas Patocka 	if (wc->endio_thread)
216848debafeSMikulas Patocka 		kthread_stop(wc->endio_thread);
216948debafeSMikulas Patocka 
217048debafeSMikulas Patocka 	if (wc->flush_thread)
217148debafeSMikulas Patocka 		kthread_stop(wc->flush_thread);
217248debafeSMikulas Patocka 
217348debafeSMikulas Patocka 	bioset_exit(&wc->bio_set);
217448debafeSMikulas Patocka 
217548debafeSMikulas Patocka 	mempool_exit(&wc->copy_pool);
217648debafeSMikulas Patocka 
217748debafeSMikulas Patocka 	if (wc->writeback_wq)
217848debafeSMikulas Patocka 		destroy_workqueue(wc->writeback_wq);
217948debafeSMikulas Patocka 
218048debafeSMikulas Patocka 	if (wc->dev)
218148debafeSMikulas Patocka 		dm_put_device(ti, wc->dev);
218248debafeSMikulas Patocka 
218348debafeSMikulas Patocka 	if (wc->ssd_dev)
218448debafeSMikulas Patocka 		dm_put_device(ti, wc->ssd_dev);
218548debafeSMikulas Patocka 
218648debafeSMikulas Patocka 	vfree(wc->entries);
218748debafeSMikulas Patocka 
218848debafeSMikulas Patocka 	if (wc->memory_map) {
218948debafeSMikulas Patocka 		if (WC_MODE_PMEM(wc))
219048debafeSMikulas Patocka 			persistent_memory_release(wc);
219148debafeSMikulas Patocka 		else
219248debafeSMikulas Patocka 			vfree(wc->memory_map);
219348debafeSMikulas Patocka 	}
219448debafeSMikulas Patocka 
219548debafeSMikulas Patocka 	if (wc->dm_kcopyd)
219648debafeSMikulas Patocka 		dm_kcopyd_client_destroy(wc->dm_kcopyd);
219748debafeSMikulas Patocka 
219848debafeSMikulas Patocka 	if (wc->dm_io)
219948debafeSMikulas Patocka 		dm_io_client_destroy(wc->dm_io);
220048debafeSMikulas Patocka 
220148debafeSMikulas Patocka 	vfree(wc->dirty_bitmap);
220248debafeSMikulas Patocka 
220348debafeSMikulas Patocka 	kfree(wc);
220448debafeSMikulas Patocka }
220548debafeSMikulas Patocka 
220648debafeSMikulas Patocka static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
220748debafeSMikulas Patocka {
220848debafeSMikulas Patocka 	struct dm_writecache *wc;
220948debafeSMikulas Patocka 	struct dm_arg_set as;
221048debafeSMikulas Patocka 	const char *string;
221148debafeSMikulas Patocka 	unsigned opt_params;
221248debafeSMikulas Patocka 	size_t offset, data_size;
221348debafeSMikulas Patocka 	int i, r;
221448debafeSMikulas Patocka 	char dummy;
221548debafeSMikulas Patocka 	int high_wm_percent = HIGH_WATERMARK;
221648debafeSMikulas Patocka 	int low_wm_percent = LOW_WATERMARK;
221748debafeSMikulas Patocka 	uint64_t x;
221848debafeSMikulas Patocka 	struct wc_memory_superblock s;
221948debafeSMikulas Patocka 
222048debafeSMikulas Patocka 	static struct dm_arg _args[] = {
22215c0de3d7SMikulas Patocka 		{0, 18, "Invalid number of feature args"},
222248debafeSMikulas Patocka 	};
222348debafeSMikulas Patocka 
222448debafeSMikulas Patocka 	as.argc = argc;
222548debafeSMikulas Patocka 	as.argv = argv;
222648debafeSMikulas Patocka 
222748debafeSMikulas Patocka 	wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
222848debafeSMikulas Patocka 	if (!wc) {
222948debafeSMikulas Patocka 		ti->error = "Cannot allocate writecache structure";
223048debafeSMikulas Patocka 		r = -ENOMEM;
223148debafeSMikulas Patocka 		goto bad;
223248debafeSMikulas Patocka 	}
223348debafeSMikulas Patocka 	ti->private = wc;
223448debafeSMikulas Patocka 	wc->ti = ti;
223548debafeSMikulas Patocka 
223648debafeSMikulas Patocka 	mutex_init(&wc->lock);
22373923d485SMikulas Patocka 	wc->max_age = MAX_AGE_UNSPECIFIED;
223848debafeSMikulas Patocka 	writecache_poison_lists(wc);
223948debafeSMikulas Patocka 	init_waitqueue_head(&wc->freelist_wait);
224048debafeSMikulas Patocka 	timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
22413923d485SMikulas Patocka 	timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0);
224248debafeSMikulas Patocka 
224348debafeSMikulas Patocka 	for (i = 0; i < 2; i++) {
224448debafeSMikulas Patocka 		atomic_set(&wc->bio_in_progress[i], 0);
224548debafeSMikulas Patocka 		init_waitqueue_head(&wc->bio_in_progress_wait[i]);
224648debafeSMikulas Patocka 	}
224748debafeSMikulas Patocka 
224848debafeSMikulas Patocka 	wc->dm_io = dm_io_client_create();
224948debafeSMikulas Patocka 	if (IS_ERR(wc->dm_io)) {
225048debafeSMikulas Patocka 		r = PTR_ERR(wc->dm_io);
225148debafeSMikulas Patocka 		ti->error = "Unable to allocate dm-io client";
225248debafeSMikulas Patocka 		wc->dm_io = NULL;
225348debafeSMikulas Patocka 		goto bad;
225448debafeSMikulas Patocka 	}
225548debafeSMikulas Patocka 
2256f87e033bSHuaisheng Ye 	wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
225748debafeSMikulas Patocka 	if (!wc->writeback_wq) {
225848debafeSMikulas Patocka 		r = -ENOMEM;
225948debafeSMikulas Patocka 		ti->error = "Could not allocate writeback workqueue";
226048debafeSMikulas Patocka 		goto bad;
226148debafeSMikulas Patocka 	}
226248debafeSMikulas Patocka 	INIT_WORK(&wc->writeback_work, writecache_writeback);
226348debafeSMikulas Patocka 	INIT_WORK(&wc->flush_work, writecache_flush_work);
226448debafeSMikulas Patocka 
226595b88f4dSMikulas Patocka 	dm_iot_init(&wc->iot);
226695b88f4dSMikulas Patocka 
226748debafeSMikulas Patocka 	raw_spin_lock_init(&wc->endio_list_lock);
226848debafeSMikulas Patocka 	INIT_LIST_HEAD(&wc->endio_list);
2269f635237aSCai Huoqing 	wc->endio_thread = kthread_run(writecache_endio_thread, wc, "writecache_endio");
227048debafeSMikulas Patocka 	if (IS_ERR(wc->endio_thread)) {
227148debafeSMikulas Patocka 		r = PTR_ERR(wc->endio_thread);
227248debafeSMikulas Patocka 		wc->endio_thread = NULL;
227348debafeSMikulas Patocka 		ti->error = "Couldn't spawn endio thread";
227448debafeSMikulas Patocka 		goto bad;
227548debafeSMikulas Patocka 	}
227648debafeSMikulas Patocka 
227748debafeSMikulas Patocka 	/*
227848debafeSMikulas Patocka 	 * Parse the mode (pmem or ssd)
227948debafeSMikulas Patocka 	 */
228048debafeSMikulas Patocka 	string = dm_shift_arg(&as);
228148debafeSMikulas Patocka 	if (!string)
228248debafeSMikulas Patocka 		goto bad_arguments;
228348debafeSMikulas Patocka 
228448debafeSMikulas Patocka 	if (!strcasecmp(string, "s")) {
228548debafeSMikulas Patocka 		wc->pmem_mode = false;
228648debafeSMikulas Patocka 	} else if (!strcasecmp(string, "p")) {
228748debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM
228848debafeSMikulas Patocka 		wc->pmem_mode = true;
228948debafeSMikulas Patocka 		wc->writeback_fua = true;
229048debafeSMikulas Patocka #else
229148debafeSMikulas Patocka 		/*
229248debafeSMikulas Patocka 		 * If the architecture doesn't support persistent memory or
229348debafeSMikulas Patocka 		 * the kernel doesn't support any DAX drivers, this driver can
229448debafeSMikulas Patocka 		 * only be used in SSD-only mode.
229548debafeSMikulas Patocka 		 */
229648debafeSMikulas Patocka 		r = -EOPNOTSUPP;
229748debafeSMikulas Patocka 		ti->error = "Persistent memory or DAX not supported on this system";
229848debafeSMikulas Patocka 		goto bad;
229948debafeSMikulas Patocka #endif
230048debafeSMikulas Patocka 	} else {
230148debafeSMikulas Patocka 		goto bad_arguments;
230248debafeSMikulas Patocka 	}
230348debafeSMikulas Patocka 
230448debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc)) {
230548debafeSMikulas Patocka 		r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
230648debafeSMikulas Patocka 				offsetof(struct writeback_struct, bio),
230748debafeSMikulas Patocka 				BIOSET_NEED_BVECS);
230848debafeSMikulas Patocka 		if (r) {
230948debafeSMikulas Patocka 			ti->error = "Could not allocate bio set";
231048debafeSMikulas Patocka 			goto bad;
231148debafeSMikulas Patocka 		}
231248debafeSMikulas Patocka 	} else {
23135c0de3d7SMikulas Patocka 		wc->pause = PAUSE_WRITEBACK;
231448debafeSMikulas Patocka 		r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
231548debafeSMikulas Patocka 		if (r) {
231648debafeSMikulas Patocka 			ti->error = "Could not allocate mempool";
231748debafeSMikulas Patocka 			goto bad;
231848debafeSMikulas Patocka 		}
231948debafeSMikulas Patocka 	}
232048debafeSMikulas Patocka 
232148debafeSMikulas Patocka 	/*
232248debafeSMikulas Patocka 	 * Parse the origin data device
232348debafeSMikulas Patocka 	 */
232448debafeSMikulas Patocka 	string = dm_shift_arg(&as);
232548debafeSMikulas Patocka 	if (!string)
232648debafeSMikulas Patocka 		goto bad_arguments;
232748debafeSMikulas Patocka 	r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
232848debafeSMikulas Patocka 	if (r) {
232948debafeSMikulas Patocka 		ti->error = "Origin data device lookup failed";
233048debafeSMikulas Patocka 		goto bad;
233148debafeSMikulas Patocka 	}
233248debafeSMikulas Patocka 
233348debafeSMikulas Patocka 	/*
233448debafeSMikulas Patocka 	 * Parse cache data device (be it pmem or ssd)
233548debafeSMikulas Patocka 	 */
233648debafeSMikulas Patocka 	string = dm_shift_arg(&as);
233748debafeSMikulas Patocka 	if (!string)
233848debafeSMikulas Patocka 		goto bad_arguments;
233948debafeSMikulas Patocka 
234048debafeSMikulas Patocka 	r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
234148debafeSMikulas Patocka 	if (r) {
234248debafeSMikulas Patocka 		ti->error = "Cache data device lookup failed";
234348debafeSMikulas Patocka 		goto bad;
234448debafeSMikulas Patocka 	}
23456dcbb52cSChristoph Hellwig 	wc->memory_map_size = bdev_nr_bytes(wc->ssd_dev->bdev);
234648debafeSMikulas Patocka 
234748debafeSMikulas Patocka 	/*
234848debafeSMikulas Patocka 	 * Parse the cache block size
234948debafeSMikulas Patocka 	 */
235048debafeSMikulas Patocka 	string = dm_shift_arg(&as);
235148debafeSMikulas Patocka 	if (!string)
235248debafeSMikulas Patocka 		goto bad_arguments;
235348debafeSMikulas Patocka 	if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
235448debafeSMikulas Patocka 	    wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
235548debafeSMikulas Patocka 	    (wc->block_size & (wc->block_size - 1))) {
235648debafeSMikulas Patocka 		r = -EINVAL;
235748debafeSMikulas Patocka 		ti->error = "Invalid block size";
235848debafeSMikulas Patocka 		goto bad;
235948debafeSMikulas Patocka 	}
236031b22120SMikulas Patocka 	if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) ||
236131b22120SMikulas Patocka 	    wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) {
236231b22120SMikulas Patocka 		r = -EINVAL;
236331b22120SMikulas Patocka 		ti->error = "Block size is smaller than device logical block size";
236431b22120SMikulas Patocka 		goto bad;
236531b22120SMikulas Patocka 	}
236648debafeSMikulas Patocka 	wc->block_size_bits = __ffs(wc->block_size);
236748debafeSMikulas Patocka 
236848debafeSMikulas Patocka 	wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
236948debafeSMikulas Patocka 	wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
237048debafeSMikulas Patocka 	wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
237148debafeSMikulas Patocka 
237248debafeSMikulas Patocka 	/*
237348debafeSMikulas Patocka 	 * Parse optional arguments
237448debafeSMikulas Patocka 	 */
237548debafeSMikulas Patocka 	r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
237648debafeSMikulas Patocka 	if (r)
237748debafeSMikulas Patocka 		goto bad;
237848debafeSMikulas Patocka 
237948debafeSMikulas Patocka 	while (opt_params) {
238048debafeSMikulas Patocka 		string = dm_shift_arg(&as), opt_params--;
2381d284f824SMikulas Patocka 		if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2382d284f824SMikulas Patocka 			unsigned long long start_sector;
2383d284f824SMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
2384d284f824SMikulas Patocka 			if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2385d284f824SMikulas Patocka 				goto invalid_optional;
2386d284f824SMikulas Patocka 			wc->start_sector = start_sector;
2387054bee16SMikulas Patocka 			wc->start_sector_set = true;
2388d284f824SMikulas Patocka 			if (wc->start_sector != start_sector ||
2389d284f824SMikulas Patocka 			    wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2390d284f824SMikulas Patocka 				goto invalid_optional;
2391d284f824SMikulas Patocka 		} else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
239248debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
239348debafeSMikulas Patocka 			if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
239448debafeSMikulas Patocka 				goto invalid_optional;
239548debafeSMikulas Patocka 			if (high_wm_percent < 0 || high_wm_percent > 100)
239648debafeSMikulas Patocka 				goto invalid_optional;
2397054bee16SMikulas Patocka 			wc->high_wm_percent_value = high_wm_percent;
239848debafeSMikulas Patocka 			wc->high_wm_percent_set = true;
239948debafeSMikulas Patocka 		} else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
240048debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
240148debafeSMikulas Patocka 			if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
240248debafeSMikulas Patocka 				goto invalid_optional;
240348debafeSMikulas Patocka 			if (low_wm_percent < 0 || low_wm_percent > 100)
240448debafeSMikulas Patocka 				goto invalid_optional;
2405054bee16SMikulas Patocka 			wc->low_wm_percent_value = low_wm_percent;
240648debafeSMikulas Patocka 			wc->low_wm_percent_set = true;
240748debafeSMikulas Patocka 		} else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
240848debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
240948debafeSMikulas Patocka 			if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
241048debafeSMikulas Patocka 				goto invalid_optional;
241148debafeSMikulas Patocka 			wc->max_writeback_jobs_set = true;
241248debafeSMikulas Patocka 		} else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
241348debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
241448debafeSMikulas Patocka 			if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
241548debafeSMikulas Patocka 				goto invalid_optional;
241648debafeSMikulas Patocka 			wc->autocommit_blocks_set = true;
241748debafeSMikulas Patocka 		} else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
241848debafeSMikulas Patocka 			unsigned autocommit_msecs;
241948debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
242048debafeSMikulas Patocka 			if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
242148debafeSMikulas Patocka 				goto invalid_optional;
242248debafeSMikulas Patocka 			if (autocommit_msecs > 3600000)
242348debafeSMikulas Patocka 				goto invalid_optional;
242448debafeSMikulas Patocka 			wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2425054bee16SMikulas Patocka 			wc->autocommit_time_value = autocommit_msecs;
242648debafeSMikulas Patocka 			wc->autocommit_time_set = true;
24273923d485SMikulas Patocka 		} else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
24283923d485SMikulas Patocka 			unsigned max_age_msecs;
24293923d485SMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
24303923d485SMikulas Patocka 			if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
24313923d485SMikulas Patocka 				goto invalid_optional;
24323923d485SMikulas Patocka 			if (max_age_msecs > 86400000)
24333923d485SMikulas Patocka 				goto invalid_optional;
24343923d485SMikulas Patocka 			wc->max_age = msecs_to_jiffies(max_age_msecs);
2435054bee16SMikulas Patocka 			wc->max_age_set = true;
2436054bee16SMikulas Patocka 			wc->max_age_value = max_age_msecs;
243793de44ebSMikulas Patocka 		} else if (!strcasecmp(string, "cleaner")) {
2438054bee16SMikulas Patocka 			wc->cleaner_set = true;
243993de44ebSMikulas Patocka 			wc->cleaner = true;
244048debafeSMikulas Patocka 		} else if (!strcasecmp(string, "fua")) {
244148debafeSMikulas Patocka 			if (WC_MODE_PMEM(wc)) {
244248debafeSMikulas Patocka 				wc->writeback_fua = true;
244348debafeSMikulas Patocka 				wc->writeback_fua_set = true;
244448debafeSMikulas Patocka 			} else goto invalid_optional;
244548debafeSMikulas Patocka 		} else if (!strcasecmp(string, "nofua")) {
244648debafeSMikulas Patocka 			if (WC_MODE_PMEM(wc)) {
244748debafeSMikulas Patocka 				wc->writeback_fua = false;
244848debafeSMikulas Patocka 				wc->writeback_fua_set = true;
244948debafeSMikulas Patocka 			} else goto invalid_optional;
2450611c3e16SMikulas Patocka 		} else if (!strcasecmp(string, "metadata_only")) {
2451611c3e16SMikulas Patocka 			wc->metadata_only = true;
24525c0de3d7SMikulas Patocka 		} else if (!strcasecmp(string, "pause_writeback") && opt_params >= 1) {
24535c0de3d7SMikulas Patocka 			unsigned pause_msecs;
24545c0de3d7SMikulas Patocka 			if (WC_MODE_PMEM(wc))
24555c0de3d7SMikulas Patocka 				goto invalid_optional;
24565c0de3d7SMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
24575c0de3d7SMikulas Patocka 			if (sscanf(string, "%u%c", &pause_msecs, &dummy) != 1)
24585c0de3d7SMikulas Patocka 				goto invalid_optional;
24595c0de3d7SMikulas Patocka 			if (pause_msecs > 60000)
24605c0de3d7SMikulas Patocka 				goto invalid_optional;
24615c0de3d7SMikulas Patocka 			wc->pause = msecs_to_jiffies(pause_msecs);
24625c0de3d7SMikulas Patocka 			wc->pause_set = true;
24635c0de3d7SMikulas Patocka 			wc->pause_value = pause_msecs;
246448debafeSMikulas Patocka 		} else {
246548debafeSMikulas Patocka invalid_optional:
246648debafeSMikulas Patocka 			r = -EINVAL;
246748debafeSMikulas Patocka 			ti->error = "Invalid optional argument";
246848debafeSMikulas Patocka 			goto bad;
246948debafeSMikulas Patocka 		}
247048debafeSMikulas Patocka 	}
247148debafeSMikulas Patocka 
247248debafeSMikulas Patocka 	if (high_wm_percent < low_wm_percent) {
247348debafeSMikulas Patocka 		r = -EINVAL;
247448debafeSMikulas Patocka 		ti->error = "High watermark must be greater than or equal to low watermark";
247548debafeSMikulas Patocka 		goto bad;
247648debafeSMikulas Patocka 	}
247748debafeSMikulas Patocka 
2478d284f824SMikulas Patocka 	if (WC_MODE_PMEM(wc)) {
2479a4662458SMichal Suchanek 		if (!dax_synchronous(wc->ssd_dev->dax_dev)) {
2480a4662458SMichal Suchanek 			r = -EOPNOTSUPP;
2481a4662458SMichal Suchanek 			ti->error = "Asynchronous persistent memory not supported as pmem cache";
2482a4662458SMichal Suchanek 			goto bad;
2483a4662458SMichal Suchanek 		}
2484a4662458SMichal Suchanek 
2485d284f824SMikulas Patocka 		r = persistent_memory_claim(wc);
2486d284f824SMikulas Patocka 		if (r) {
2487d284f824SMikulas Patocka 			ti->error = "Unable to map persistent memory for cache";
2488d284f824SMikulas Patocka 			goto bad;
2489d284f824SMikulas Patocka 		}
2490d284f824SMikulas Patocka 	} else {
249148debafeSMikulas Patocka 		size_t n_blocks, n_metadata_blocks;
249248debafeSMikulas Patocka 		uint64_t n_bitmap_bits;
249348debafeSMikulas Patocka 
2494d284f824SMikulas Patocka 		wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2495d284f824SMikulas Patocka 
249648debafeSMikulas Patocka 		bio_list_init(&wc->flush_list);
2497f635237aSCai Huoqing 		wc->flush_thread = kthread_run(writecache_flush_thread, wc, "dm_writecache_flush");
249848debafeSMikulas Patocka 		if (IS_ERR(wc->flush_thread)) {
249948debafeSMikulas Patocka 			r = PTR_ERR(wc->flush_thread);
250048debafeSMikulas Patocka 			wc->flush_thread = NULL;
2501e8ea141aSShenghui Wang 			ti->error = "Couldn't spawn flush thread";
250248debafeSMikulas Patocka 			goto bad;
250348debafeSMikulas Patocka 		}
250448debafeSMikulas Patocka 
250548debafeSMikulas Patocka 		r = calculate_memory_size(wc->memory_map_size, wc->block_size,
250648debafeSMikulas Patocka 					  &n_blocks, &n_metadata_blocks);
250748debafeSMikulas Patocka 		if (r) {
250848debafeSMikulas Patocka 			ti->error = "Invalid device size";
250948debafeSMikulas Patocka 			goto bad;
251048debafeSMikulas Patocka 		}
251148debafeSMikulas Patocka 
251248debafeSMikulas Patocka 		n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
251348debafeSMikulas Patocka 				 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
251448debafeSMikulas Patocka 		/* this is limitation of test_bit functions */
251548debafeSMikulas Patocka 		if (n_bitmap_bits > 1U << 31) {
251648debafeSMikulas Patocka 			r = -EFBIG;
251748debafeSMikulas Patocka 			ti->error = "Invalid device size";
251848debafeSMikulas Patocka 			goto bad;
251948debafeSMikulas Patocka 		}
252048debafeSMikulas Patocka 
252148debafeSMikulas Patocka 		wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
252248debafeSMikulas Patocka 		if (!wc->memory_map) {
252348debafeSMikulas Patocka 			r = -ENOMEM;
252448debafeSMikulas Patocka 			ti->error = "Unable to allocate memory for metadata";
252548debafeSMikulas Patocka 			goto bad;
252648debafeSMikulas Patocka 		}
252748debafeSMikulas Patocka 
252848debafeSMikulas Patocka 		wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
252948debafeSMikulas Patocka 		if (IS_ERR(wc->dm_kcopyd)) {
253048debafeSMikulas Patocka 			r = PTR_ERR(wc->dm_kcopyd);
253148debafeSMikulas Patocka 			ti->error = "Unable to allocate dm-kcopyd client";
253248debafeSMikulas Patocka 			wc->dm_kcopyd = NULL;
253348debafeSMikulas Patocka 			goto bad;
253448debafeSMikulas Patocka 		}
253548debafeSMikulas Patocka 
253648debafeSMikulas Patocka 		wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
253748debafeSMikulas Patocka 		wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
253848debafeSMikulas Patocka 			BITS_PER_LONG * sizeof(unsigned long);
253948debafeSMikulas Patocka 		wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
254048debafeSMikulas Patocka 		if (!wc->dirty_bitmap) {
254148debafeSMikulas Patocka 			r = -ENOMEM;
254248debafeSMikulas Patocka 			ti->error = "Unable to allocate dirty bitmap";
254348debafeSMikulas Patocka 			goto bad;
254448debafeSMikulas Patocka 		}
254548debafeSMikulas Patocka 
254631b22120SMikulas Patocka 		r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT);
254748debafeSMikulas Patocka 		if (r) {
254831b22120SMikulas Patocka 			ti->error = "Unable to read first block of metadata";
254948debafeSMikulas Patocka 			goto bad;
255048debafeSMikulas Patocka 		}
255148debafeSMikulas Patocka 	}
255248debafeSMikulas Patocka 
2553ec6347bbSDan Williams 	r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock));
255448debafeSMikulas Patocka 	if (r) {
255548debafeSMikulas Patocka 		ti->error = "Hardware memory error when reading superblock";
255648debafeSMikulas Patocka 		goto bad;
255748debafeSMikulas Patocka 	}
255848debafeSMikulas Patocka 	if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
255948debafeSMikulas Patocka 		r = init_memory(wc);
256048debafeSMikulas Patocka 		if (r) {
256148debafeSMikulas Patocka 			ti->error = "Unable to initialize device";
256248debafeSMikulas Patocka 			goto bad;
256348debafeSMikulas Patocka 		}
2564ec6347bbSDan Williams 		r = copy_mc_to_kernel(&s, sb(wc),
2565ec6347bbSDan Williams 				      sizeof(struct wc_memory_superblock));
256648debafeSMikulas Patocka 		if (r) {
256748debafeSMikulas Patocka 			ti->error = "Hardware memory error when reading superblock";
256848debafeSMikulas Patocka 			goto bad;
256948debafeSMikulas Patocka 		}
257048debafeSMikulas Patocka 	}
257148debafeSMikulas Patocka 
257248debafeSMikulas Patocka 	if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
257348debafeSMikulas Patocka 		ti->error = "Invalid magic in the superblock";
257448debafeSMikulas Patocka 		r = -EINVAL;
257548debafeSMikulas Patocka 		goto bad;
257648debafeSMikulas Patocka 	}
257748debafeSMikulas Patocka 
257848debafeSMikulas Patocka 	if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
257948debafeSMikulas Patocka 		ti->error = "Invalid version in the superblock";
258048debafeSMikulas Patocka 		r = -EINVAL;
258148debafeSMikulas Patocka 		goto bad;
258248debafeSMikulas Patocka 	}
258348debafeSMikulas Patocka 
258448debafeSMikulas Patocka 	if (le32_to_cpu(s.block_size) != wc->block_size) {
258548debafeSMikulas Patocka 		ti->error = "Block size does not match superblock";
258648debafeSMikulas Patocka 		r = -EINVAL;
258748debafeSMikulas Patocka 		goto bad;
258848debafeSMikulas Patocka 	}
258948debafeSMikulas Patocka 
259048debafeSMikulas Patocka 	wc->n_blocks = le64_to_cpu(s.n_blocks);
259148debafeSMikulas Patocka 
259248debafeSMikulas Patocka 	offset = wc->n_blocks * sizeof(struct wc_memory_entry);
259348debafeSMikulas Patocka 	if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
259448debafeSMikulas Patocka overflow:
259548debafeSMikulas Patocka 		ti->error = "Overflow in size calculation";
259648debafeSMikulas Patocka 		r = -EINVAL;
259748debafeSMikulas Patocka 		goto bad;
259848debafeSMikulas Patocka 	}
259948debafeSMikulas Patocka 	offset += sizeof(struct wc_memory_superblock);
260048debafeSMikulas Patocka 	if (offset < sizeof(struct wc_memory_superblock))
260148debafeSMikulas Patocka 		goto overflow;
260248debafeSMikulas Patocka 	offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
260348debafeSMikulas Patocka 	data_size = wc->n_blocks * (size_t)wc->block_size;
260448debafeSMikulas Patocka 	if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
260548debafeSMikulas Patocka 	    (offset + data_size < offset))
260648debafeSMikulas Patocka 		goto overflow;
260748debafeSMikulas Patocka 	if (offset + data_size > wc->memory_map_size) {
260848debafeSMikulas Patocka 		ti->error = "Memory area is too small";
260948debafeSMikulas Patocka 		r = -EINVAL;
261048debafeSMikulas Patocka 		goto bad;
261148debafeSMikulas Patocka 	}
261248debafeSMikulas Patocka 
261348debafeSMikulas Patocka 	wc->metadata_sectors = offset >> SECTOR_SHIFT;
261448debafeSMikulas Patocka 	wc->block_start = (char *)sb(wc) + offset;
261548debafeSMikulas Patocka 
261648debafeSMikulas Patocka 	x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
261748debafeSMikulas Patocka 	x += 50;
261848debafeSMikulas Patocka 	do_div(x, 100);
261948debafeSMikulas Patocka 	wc->freelist_high_watermark = x;
262048debafeSMikulas Patocka 	x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
262148debafeSMikulas Patocka 	x += 50;
262248debafeSMikulas Patocka 	do_div(x, 100);
262348debafeSMikulas Patocka 	wc->freelist_low_watermark = x;
262448debafeSMikulas Patocka 
262593de44ebSMikulas Patocka 	if (wc->cleaner)
262693de44ebSMikulas Patocka 		activate_cleaner(wc);
262793de44ebSMikulas Patocka 
262848debafeSMikulas Patocka 	r = writecache_alloc_entries(wc);
262948debafeSMikulas Patocka 	if (r) {
263048debafeSMikulas Patocka 		ti->error = "Cannot allocate memory";
263148debafeSMikulas Patocka 		goto bad;
263248debafeSMikulas Patocka 	}
263348debafeSMikulas Patocka 
2634ee55b92aSMikulas Patocka 	ti->num_flush_bios = WC_MODE_PMEM(wc) ? 1 : 2;
263548debafeSMikulas Patocka 	ti->flush_supported = true;
263648debafeSMikulas Patocka 	ti->num_discard_bios = 1;
263748debafeSMikulas Patocka 
263848debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
263948debafeSMikulas Patocka 		persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
264048debafeSMikulas Patocka 
264148debafeSMikulas Patocka 	return 0;
264248debafeSMikulas Patocka 
264348debafeSMikulas Patocka bad_arguments:
264448debafeSMikulas Patocka 	r = -EINVAL;
264548debafeSMikulas Patocka 	ti->error = "Bad arguments";
264648debafeSMikulas Patocka bad:
264748debafeSMikulas Patocka 	writecache_dtr(ti);
264848debafeSMikulas Patocka 	return r;
264948debafeSMikulas Patocka }
265048debafeSMikulas Patocka 
265148debafeSMikulas Patocka static void writecache_status(struct dm_target *ti, status_type_t type,
265248debafeSMikulas Patocka 			      unsigned status_flags, char *result, unsigned maxlen)
265348debafeSMikulas Patocka {
265448debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
265548debafeSMikulas Patocka 	unsigned extra_args;
265648debafeSMikulas Patocka 	unsigned sz = 0;
265748debafeSMikulas Patocka 
265848debafeSMikulas Patocka 	switch (type) {
265948debafeSMikulas Patocka 	case STATUSTYPE_INFO:
2660e3a35d03SMikulas Patocka 		DMEMIT("%ld %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
2661e3a35d03SMikulas Patocka 		       writecache_has_error(wc),
266248debafeSMikulas Patocka 		       (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2663e3a35d03SMikulas Patocka 		       (unsigned long long)wc->writeback_size,
2664e3a35d03SMikulas Patocka 		       wc->stats.reads,
2665e3a35d03SMikulas Patocka 		       wc->stats.read_hits,
2666e3a35d03SMikulas Patocka 		       wc->stats.writes,
2667e3a35d03SMikulas Patocka 		       wc->stats.write_hits_uncommitted,
2668e3a35d03SMikulas Patocka 		       wc->stats.write_hits_committed,
2669e3a35d03SMikulas Patocka 		       wc->stats.writes_around,
2670e3a35d03SMikulas Patocka 		       wc->stats.writes_allocate,
2671e3a35d03SMikulas Patocka 		       wc->stats.writes_blocked_on_freelist,
2672e3a35d03SMikulas Patocka 		       wc->stats.flushes,
2673e3a35d03SMikulas Patocka 		       wc->stats.discards);
267448debafeSMikulas Patocka 		break;
267548debafeSMikulas Patocka 	case STATUSTYPE_TABLE:
267648debafeSMikulas Patocka 		DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
267748debafeSMikulas Patocka 				wc->dev->name, wc->ssd_dev->name, wc->block_size);
267848debafeSMikulas Patocka 		extra_args = 0;
2679054bee16SMikulas Patocka 		if (wc->start_sector_set)
26809ff07e7dSMikulas Patocka 			extra_args += 2;
2681054bee16SMikulas Patocka 		if (wc->high_wm_percent_set)
268248debafeSMikulas Patocka 			extra_args += 2;
2683054bee16SMikulas Patocka 		if (wc->low_wm_percent_set)
268448debafeSMikulas Patocka 			extra_args += 2;
268548debafeSMikulas Patocka 		if (wc->max_writeback_jobs_set)
268648debafeSMikulas Patocka 			extra_args += 2;
268748debafeSMikulas Patocka 		if (wc->autocommit_blocks_set)
268848debafeSMikulas Patocka 			extra_args += 2;
268948debafeSMikulas Patocka 		if (wc->autocommit_time_set)
269048debafeSMikulas Patocka 			extra_args += 2;
2691054bee16SMikulas Patocka 		if (wc->max_age_set)
2692e5d41cbcSMikulas Patocka 			extra_args += 2;
2693054bee16SMikulas Patocka 		if (wc->cleaner_set)
269493de44ebSMikulas Patocka 			extra_args++;
269548debafeSMikulas Patocka 		if (wc->writeback_fua_set)
269648debafeSMikulas Patocka 			extra_args++;
2697611c3e16SMikulas Patocka 		if (wc->metadata_only)
2698611c3e16SMikulas Patocka 			extra_args++;
26995c0de3d7SMikulas Patocka 		if (wc->pause_set)
27005c0de3d7SMikulas Patocka 			extra_args += 2;
270148debafeSMikulas Patocka 
270248debafeSMikulas Patocka 		DMEMIT("%u", extra_args);
2703054bee16SMikulas Patocka 		if (wc->start_sector_set)
27049ff07e7dSMikulas Patocka 			DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2705054bee16SMikulas Patocka 		if (wc->high_wm_percent_set)
2706054bee16SMikulas Patocka 			DMEMIT(" high_watermark %u", wc->high_wm_percent_value);
2707054bee16SMikulas Patocka 		if (wc->low_wm_percent_set)
2708054bee16SMikulas Patocka 			DMEMIT(" low_watermark %u", wc->low_wm_percent_value);
270948debafeSMikulas Patocka 		if (wc->max_writeback_jobs_set)
271048debafeSMikulas Patocka 			DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
271148debafeSMikulas Patocka 		if (wc->autocommit_blocks_set)
271248debafeSMikulas Patocka 			DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
271348debafeSMikulas Patocka 		if (wc->autocommit_time_set)
2714054bee16SMikulas Patocka 			DMEMIT(" autocommit_time %u", wc->autocommit_time_value);
2715054bee16SMikulas Patocka 		if (wc->max_age_set)
2716054bee16SMikulas Patocka 			DMEMIT(" max_age %u", wc->max_age_value);
2717054bee16SMikulas Patocka 		if (wc->cleaner_set)
271893de44ebSMikulas Patocka 			DMEMIT(" cleaner");
271948debafeSMikulas Patocka 		if (wc->writeback_fua_set)
272048debafeSMikulas Patocka 			DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2721611c3e16SMikulas Patocka 		if (wc->metadata_only)
2722611c3e16SMikulas Patocka 			DMEMIT(" metadata_only");
27235c0de3d7SMikulas Patocka 		if (wc->pause_set)
27245c0de3d7SMikulas Patocka 			DMEMIT(" pause_writeback %u", wc->pause_value);
272548debafeSMikulas Patocka 		break;
27268ec45662STushar Sugandhi 	case STATUSTYPE_IMA:
27278ec45662STushar Sugandhi 		*result = '\0';
27288ec45662STushar Sugandhi 		break;
272948debafeSMikulas Patocka 	}
273048debafeSMikulas Patocka }
273148debafeSMikulas Patocka 
273248debafeSMikulas Patocka static struct target_type writecache_target = {
273348debafeSMikulas Patocka 	.name			= "writecache",
2734e3a35d03SMikulas Patocka 	.version		= {1, 6, 0},
273548debafeSMikulas Patocka 	.module			= THIS_MODULE,
273648debafeSMikulas Patocka 	.ctr			= writecache_ctr,
273748debafeSMikulas Patocka 	.dtr			= writecache_dtr,
273848debafeSMikulas Patocka 	.status			= writecache_status,
273948debafeSMikulas Patocka 	.postsuspend		= writecache_suspend,
274048debafeSMikulas Patocka 	.resume			= writecache_resume,
274148debafeSMikulas Patocka 	.message		= writecache_message,
274248debafeSMikulas Patocka 	.map			= writecache_map,
274348debafeSMikulas Patocka 	.end_io			= writecache_end_io,
274448debafeSMikulas Patocka 	.iterate_devices	= writecache_iterate_devices,
274548debafeSMikulas Patocka 	.io_hints		= writecache_io_hints,
274648debafeSMikulas Patocka };
274748debafeSMikulas Patocka 
274848debafeSMikulas Patocka static int __init dm_writecache_init(void)
274948debafeSMikulas Patocka {
275048debafeSMikulas Patocka 	int r;
275148debafeSMikulas Patocka 
275248debafeSMikulas Patocka 	r = dm_register_target(&writecache_target);
275348debafeSMikulas Patocka 	if (r < 0) {
275448debafeSMikulas Patocka 		DMERR("register failed %d", r);
275548debafeSMikulas Patocka 		return r;
275648debafeSMikulas Patocka 	}
275748debafeSMikulas Patocka 
275848debafeSMikulas Patocka 	return 0;
275948debafeSMikulas Patocka }
276048debafeSMikulas Patocka 
276148debafeSMikulas Patocka static void __exit dm_writecache_exit(void)
276248debafeSMikulas Patocka {
276348debafeSMikulas Patocka 	dm_unregister_target(&writecache_target);
276448debafeSMikulas Patocka }
276548debafeSMikulas Patocka 
276648debafeSMikulas Patocka module_init(dm_writecache_init);
276748debafeSMikulas Patocka module_exit(dm_writecache_exit);
276848debafeSMikulas Patocka 
276948debafeSMikulas Patocka MODULE_DESCRIPTION(DM_NAME " writecache target");
277048debafeSMikulas Patocka MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
277148debafeSMikulas Patocka MODULE_LICENSE("GPL");
2772