xref: /linux/drivers/md/dm-writecache.c (revision 581075e4f6475bb97c73ecccf68636a9453a31fd)
148debafeSMikulas Patocka // SPDX-License-Identifier: GPL-2.0
248debafeSMikulas Patocka /*
348debafeSMikulas Patocka  * Copyright (C) 2018 Red Hat. All rights reserved.
448debafeSMikulas Patocka  *
548debafeSMikulas Patocka  * This file is released under the GPL.
648debafeSMikulas Patocka  */
748debafeSMikulas Patocka 
848debafeSMikulas Patocka #include <linux/device-mapper.h>
948debafeSMikulas Patocka #include <linux/module.h>
1048debafeSMikulas Patocka #include <linux/init.h>
1148debafeSMikulas Patocka #include <linux/vmalloc.h>
1248debafeSMikulas Patocka #include <linux/kthread.h>
1348debafeSMikulas Patocka #include <linux/dm-io.h>
1448debafeSMikulas Patocka #include <linux/dm-kcopyd.h>
1548debafeSMikulas Patocka #include <linux/dax.h>
1648debafeSMikulas Patocka #include <linux/pfn_t.h>
1748debafeSMikulas Patocka #include <linux/libnvdimm.h>
1895b88f4dSMikulas Patocka #include <linux/delay.h>
1995b88f4dSMikulas Patocka #include "dm-io-tracker.h"
2048debafeSMikulas Patocka 
2148debafeSMikulas Patocka #define DM_MSG_PREFIX "writecache"
2248debafeSMikulas Patocka 
2348debafeSMikulas Patocka #define HIGH_WATERMARK			50
2448debafeSMikulas Patocka #define LOW_WATERMARK			45
2548debafeSMikulas Patocka #define MAX_WRITEBACK_JOBS		0
2648debafeSMikulas Patocka #define ENDIO_LATENCY			16
2748debafeSMikulas Patocka #define WRITEBACK_LATENCY		64
2848debafeSMikulas Patocka #define AUTOCOMMIT_BLOCKS_SSD		65536
2948debafeSMikulas Patocka #define AUTOCOMMIT_BLOCKS_PMEM		64
3048debafeSMikulas Patocka #define AUTOCOMMIT_MSEC			1000
313923d485SMikulas Patocka #define MAX_AGE_DIV			16
323923d485SMikulas Patocka #define MAX_AGE_UNSPECIFIED		-1UL
335c0de3d7SMikulas Patocka #define PAUSE_WRITEBACK			(HZ * 3)
3448debafeSMikulas Patocka 
3548debafeSMikulas Patocka #define BITMAP_GRANULARITY	65536
3648debafeSMikulas Patocka #if BITMAP_GRANULARITY < PAGE_SIZE
3748debafeSMikulas Patocka #undef BITMAP_GRANULARITY
3848debafeSMikulas Patocka #define BITMAP_GRANULARITY	PAGE_SIZE
3948debafeSMikulas Patocka #endif
4048debafeSMikulas Patocka 
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);
526*581075e4SBart Van Assche 		req.bi_opf = REQ_OP_WRITE | REQ_SYNC;
52748debafeSMikulas Patocka 		req.mem.type = DM_IO_VMA;
52848debafeSMikulas Patocka 		req.mem.ptr.vma = (char *)wc->memory_map + (size_t)i * BITMAP_GRANULARITY;
52948debafeSMikulas Patocka 		req.client = wc->dm_io;
53048debafeSMikulas Patocka 		req.notify.fn = writecache_notify_io;
53148debafeSMikulas Patocka 		req.notify.context = &endio;
53248debafeSMikulas Patocka 
53348debafeSMikulas Patocka 		/* writing via async dm-io (implied by notify.fn above) won't return an error */
53448debafeSMikulas Patocka 	        (void) dm_io(&req, 1, &region, NULL);
53548debafeSMikulas Patocka 		i = j;
53648debafeSMikulas Patocka 	}
53748debafeSMikulas Patocka 
53848debafeSMikulas Patocka 	writecache_notify_io(0, &endio);
53948debafeSMikulas Patocka 	wait_for_completion_io(&endio.c);
54048debafeSMikulas Patocka 
541aa950920SMikulas Patocka 	if (wait_for_ios)
542aa950920SMikulas Patocka 		writecache_wait_for_ios(wc, WRITE);
543aa950920SMikulas Patocka 
54448debafeSMikulas Patocka 	writecache_disk_flush(wc, wc->ssd_dev);
54548debafeSMikulas Patocka 
54648debafeSMikulas Patocka 	memset(wc->dirty_bitmap, 0, wc->dirty_bitmap_size);
54748debafeSMikulas Patocka }
54848debafeSMikulas Patocka 
549dc8a01aeSMikulas Patocka static void ssd_commit_superblock(struct dm_writecache *wc)
550dc8a01aeSMikulas Patocka {
551dc8a01aeSMikulas Patocka 	int r;
552dc8a01aeSMikulas Patocka 	struct dm_io_region region;
553dc8a01aeSMikulas Patocka 	struct dm_io_request req;
554dc8a01aeSMikulas Patocka 
555dc8a01aeSMikulas Patocka 	region.bdev = wc->ssd_dev->bdev;
556dc8a01aeSMikulas Patocka 	region.sector = 0;
557867de40cSMikulas Patocka 	region.count = max(4096U, wc->block_size) >> SECTOR_SHIFT;
558867de40cSMikulas Patocka 
559867de40cSMikulas Patocka 	if (unlikely(region.sector + region.count > wc->metadata_sectors))
560867de40cSMikulas Patocka 		region.count = wc->metadata_sectors - region.sector;
561867de40cSMikulas Patocka 
562dc8a01aeSMikulas Patocka 	region.sector += wc->start_sector;
563dc8a01aeSMikulas Patocka 
564*581075e4SBart Van Assche 	req.bi_opf = REQ_OP_WRITE | REQ_SYNC | REQ_FUA;
565dc8a01aeSMikulas Patocka 	req.mem.type = DM_IO_VMA;
566dc8a01aeSMikulas Patocka 	req.mem.ptr.vma = (char *)wc->memory_map;
567dc8a01aeSMikulas Patocka 	req.client = wc->dm_io;
568dc8a01aeSMikulas Patocka 	req.notify.fn = NULL;
569dc8a01aeSMikulas Patocka 	req.notify.context = NULL;
570dc8a01aeSMikulas Patocka 
571dc8a01aeSMikulas Patocka 	r = dm_io(&req, 1, &region, NULL);
572dc8a01aeSMikulas Patocka 	if (unlikely(r))
573dc8a01aeSMikulas Patocka 		writecache_error(wc, r, "error writing superblock");
574dc8a01aeSMikulas Patocka }
575dc8a01aeSMikulas Patocka 
576aa950920SMikulas Patocka static void writecache_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
57748debafeSMikulas Patocka {
57848debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
5793e79f082SAneesh Kumar K.V 		pmem_wmb();
58048debafeSMikulas Patocka 	else
581aa950920SMikulas Patocka 		ssd_commit_flushed(wc, wait_for_ios);
58248debafeSMikulas Patocka }
58348debafeSMikulas Patocka 
58448debafeSMikulas Patocka static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev)
58548debafeSMikulas Patocka {
58648debafeSMikulas Patocka 	int r;
58748debafeSMikulas Patocka 	struct dm_io_region region;
58848debafeSMikulas Patocka 	struct dm_io_request req;
58948debafeSMikulas Patocka 
59048debafeSMikulas Patocka 	region.bdev = dev->bdev;
59148debafeSMikulas Patocka 	region.sector = 0;
59248debafeSMikulas Patocka 	region.count = 0;
593*581075e4SBart Van Assche 	req.bi_opf = REQ_OP_WRITE | REQ_PREFLUSH;
59448debafeSMikulas Patocka 	req.mem.type = DM_IO_KMEM;
59548debafeSMikulas Patocka 	req.mem.ptr.addr = NULL;
59648debafeSMikulas Patocka 	req.client = wc->dm_io;
59748debafeSMikulas Patocka 	req.notify.fn = NULL;
59848debafeSMikulas Patocka 
59948debafeSMikulas Patocka 	r = dm_io(&req, 1, &region, NULL);
60048debafeSMikulas Patocka 	if (unlikely(r))
60148debafeSMikulas Patocka 		writecache_error(wc, r, "error flushing metadata: %d", r);
60248debafeSMikulas Patocka }
60348debafeSMikulas Patocka 
60448debafeSMikulas Patocka #define WFE_RETURN_FOLLOWING	1
60548debafeSMikulas Patocka #define WFE_LOWEST_SEQ		2
60648debafeSMikulas Patocka 
60748debafeSMikulas Patocka static struct wc_entry *writecache_find_entry(struct dm_writecache *wc,
60848debafeSMikulas Patocka 					      uint64_t block, int flags)
60948debafeSMikulas Patocka {
61048debafeSMikulas Patocka 	struct wc_entry *e;
61148debafeSMikulas Patocka 	struct rb_node *node = wc->tree.rb_node;
61248debafeSMikulas Patocka 
61348debafeSMikulas Patocka 	if (unlikely(!node))
61448debafeSMikulas Patocka 		return NULL;
61548debafeSMikulas Patocka 
61648debafeSMikulas Patocka 	while (1) {
61748debafeSMikulas Patocka 		e = container_of(node, struct wc_entry, rb_node);
61848debafeSMikulas Patocka 		if (read_original_sector(wc, e) == block)
61948debafeSMikulas Patocka 			break;
620f8011d33SMikulas Patocka 
62148debafeSMikulas Patocka 		node = (read_original_sector(wc, e) >= block ?
62248debafeSMikulas Patocka 			e->rb_node.rb_left : e->rb_node.rb_right);
62348debafeSMikulas Patocka 		if (unlikely(!node)) {
624f8011d33SMikulas Patocka 			if (!(flags & WFE_RETURN_FOLLOWING))
62548debafeSMikulas Patocka 				return NULL;
62648debafeSMikulas Patocka 			if (read_original_sector(wc, e) >= block) {
627f8011d33SMikulas Patocka 				return e;
62848debafeSMikulas Patocka 			} else {
62948debafeSMikulas Patocka 				node = rb_next(&e->rb_node);
630f8011d33SMikulas Patocka 				if (unlikely(!node))
63148debafeSMikulas Patocka 					return NULL;
63248debafeSMikulas Patocka 				e = container_of(node, struct wc_entry, rb_node);
633f8011d33SMikulas Patocka 				return e;
63448debafeSMikulas Patocka 			}
63548debafeSMikulas Patocka 		}
63648debafeSMikulas Patocka 	}
63748debafeSMikulas Patocka 
63848debafeSMikulas Patocka 	while (1) {
63948debafeSMikulas Patocka 		struct wc_entry *e2;
64048debafeSMikulas Patocka 		if (flags & WFE_LOWEST_SEQ)
64148debafeSMikulas Patocka 			node = rb_prev(&e->rb_node);
64248debafeSMikulas Patocka 		else
64348debafeSMikulas Patocka 			node = rb_next(&e->rb_node);
64484420b1eSHuaisheng Ye 		if (unlikely(!node))
64548debafeSMikulas Patocka 			return e;
64648debafeSMikulas Patocka 		e2 = container_of(node, struct wc_entry, rb_node);
64748debafeSMikulas Patocka 		if (read_original_sector(wc, e2) != block)
64848debafeSMikulas Patocka 			return e;
64948debafeSMikulas Patocka 		e = e2;
65048debafeSMikulas Patocka 	}
65148debafeSMikulas Patocka }
65248debafeSMikulas Patocka 
65348debafeSMikulas Patocka static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
65448debafeSMikulas Patocka {
65548debafeSMikulas Patocka 	struct wc_entry *e;
65648debafeSMikulas Patocka 	struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
65748debafeSMikulas Patocka 
65848debafeSMikulas Patocka 	while (*node) {
65948debafeSMikulas Patocka 		e = container_of(*node, struct wc_entry, rb_node);
66048debafeSMikulas Patocka 		parent = &e->rb_node;
66148debafeSMikulas Patocka 		if (read_original_sector(wc, e) > read_original_sector(wc, ins))
66248debafeSMikulas Patocka 			node = &parent->rb_left;
66348debafeSMikulas Patocka 		else
66448debafeSMikulas Patocka 			node = &parent->rb_right;
66548debafeSMikulas Patocka 	}
66648debafeSMikulas Patocka 	rb_link_node(&ins->rb_node, parent, node);
66748debafeSMikulas Patocka 	rb_insert_color(&ins->rb_node, &wc->tree);
66848debafeSMikulas Patocka 	list_add(&ins->lru, &wc->lru);
6693923d485SMikulas Patocka 	ins->age = jiffies;
67048debafeSMikulas Patocka }
67148debafeSMikulas Patocka 
67248debafeSMikulas Patocka static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
67348debafeSMikulas Patocka {
67448debafeSMikulas Patocka 	list_del(&e->lru);
67548debafeSMikulas Patocka 	rb_erase(&e->rb_node, &wc->tree);
67648debafeSMikulas Patocka }
67748debafeSMikulas Patocka 
67848debafeSMikulas Patocka static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
67948debafeSMikulas Patocka {
68048debafeSMikulas Patocka 	if (WC_MODE_SORT_FREELIST(wc)) {
68148debafeSMikulas Patocka 		struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
68248debafeSMikulas Patocka 		if (unlikely(!*node))
68348debafeSMikulas Patocka 			wc->current_free = e;
68448debafeSMikulas Patocka 		while (*node) {
68548debafeSMikulas Patocka 			parent = *node;
68648debafeSMikulas Patocka 			if (&e->rb_node < *node)
68748debafeSMikulas Patocka 				node = &parent->rb_left;
68848debafeSMikulas Patocka 			else
68948debafeSMikulas Patocka 				node = &parent->rb_right;
69048debafeSMikulas Patocka 		}
69148debafeSMikulas Patocka 		rb_link_node(&e->rb_node, parent, node);
69248debafeSMikulas Patocka 		rb_insert_color(&e->rb_node, &wc->freetree);
69348debafeSMikulas Patocka 	} else {
69448debafeSMikulas Patocka 		list_add_tail(&e->lru, &wc->freelist);
69548debafeSMikulas Patocka 	}
69648debafeSMikulas Patocka 	wc->freelist_size++;
69748debafeSMikulas Patocka }
69848debafeSMikulas Patocka 
69941c526c5SMikulas Patocka static inline void writecache_verify_watermark(struct dm_writecache *wc)
70041c526c5SMikulas Patocka {
70141c526c5SMikulas Patocka 	if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
70241c526c5SMikulas Patocka 		queue_work(wc->writeback_wq, &wc->writeback_work);
70341c526c5SMikulas Patocka }
70441c526c5SMikulas Patocka 
7053923d485SMikulas Patocka static void writecache_max_age_timer(struct timer_list *t)
7063923d485SMikulas Patocka {
7073923d485SMikulas Patocka 	struct dm_writecache *wc = from_timer(wc, t, max_age_timer);
7083923d485SMikulas Patocka 
7093923d485SMikulas Patocka 	if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) {
7103923d485SMikulas Patocka 		queue_work(wc->writeback_wq, &wc->writeback_work);
7113923d485SMikulas Patocka 		mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
7123923d485SMikulas Patocka 	}
7133923d485SMikulas Patocka }
7143923d485SMikulas Patocka 
715dcd19507SMikulas Patocka static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector)
71648debafeSMikulas Patocka {
71748debafeSMikulas Patocka 	struct wc_entry *e;
71848debafeSMikulas Patocka 
71948debafeSMikulas Patocka 	if (WC_MODE_SORT_FREELIST(wc)) {
72048debafeSMikulas Patocka 		struct rb_node *next;
72148debafeSMikulas Patocka 		if (unlikely(!wc->current_free))
72248debafeSMikulas Patocka 			return NULL;
72348debafeSMikulas Patocka 		e = wc->current_free;
724dcd19507SMikulas Patocka 		if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
725dcd19507SMikulas Patocka 			return NULL;
72648debafeSMikulas Patocka 		next = rb_next(&e->rb_node);
72748debafeSMikulas Patocka 		rb_erase(&e->rb_node, &wc->freetree);
72848debafeSMikulas Patocka 		if (unlikely(!next))
72948debafeSMikulas Patocka 			next = rb_first(&wc->freetree);
73048debafeSMikulas Patocka 		wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
73148debafeSMikulas Patocka 	} else {
73248debafeSMikulas Patocka 		if (unlikely(list_empty(&wc->freelist)))
73348debafeSMikulas Patocka 			return NULL;
73448debafeSMikulas Patocka 		e = container_of(wc->freelist.next, struct wc_entry, lru);
735dcd19507SMikulas Patocka 		if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
736dcd19507SMikulas Patocka 			return NULL;
73748debafeSMikulas Patocka 		list_del(&e->lru);
73848debafeSMikulas Patocka 	}
73948debafeSMikulas Patocka 	wc->freelist_size--;
74041c526c5SMikulas Patocka 
74141c526c5SMikulas Patocka 	writecache_verify_watermark(wc);
74248debafeSMikulas Patocka 
74348debafeSMikulas Patocka 	return e;
74448debafeSMikulas Patocka }
74548debafeSMikulas Patocka 
74648debafeSMikulas Patocka static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
74748debafeSMikulas Patocka {
74848debafeSMikulas Patocka 	writecache_unlink(wc, e);
74948debafeSMikulas Patocka 	writecache_add_to_freelist(wc, e);
75048debafeSMikulas Patocka 	clear_seq_count(wc, e);
75148debafeSMikulas Patocka 	writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
75248debafeSMikulas Patocka 	if (unlikely(waitqueue_active(&wc->freelist_wait)))
75348debafeSMikulas Patocka 		wake_up(&wc->freelist_wait);
75448debafeSMikulas Patocka }
75548debafeSMikulas Patocka 
75648debafeSMikulas Patocka static void writecache_wait_on_freelist(struct dm_writecache *wc)
75748debafeSMikulas Patocka {
75848debafeSMikulas Patocka 	DEFINE_WAIT(wait);
75948debafeSMikulas Patocka 
76048debafeSMikulas Patocka 	prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
76148debafeSMikulas Patocka 	wc_unlock(wc);
76248debafeSMikulas Patocka 	io_schedule();
76348debafeSMikulas Patocka 	finish_wait(&wc->freelist_wait, &wait);
76448debafeSMikulas Patocka 	wc_lock(wc);
76548debafeSMikulas Patocka }
76648debafeSMikulas Patocka 
76748debafeSMikulas Patocka static void writecache_poison_lists(struct dm_writecache *wc)
76848debafeSMikulas Patocka {
76948debafeSMikulas Patocka 	/*
77048debafeSMikulas Patocka 	 * Catch incorrect access to these values while the device is suspended.
77148debafeSMikulas Patocka 	 */
77248debafeSMikulas Patocka 	memset(&wc->tree, -1, sizeof wc->tree);
77348debafeSMikulas Patocka 	wc->lru.next = LIST_POISON1;
77448debafeSMikulas Patocka 	wc->lru.prev = LIST_POISON2;
77548debafeSMikulas Patocka 	wc->freelist.next = LIST_POISON1;
77648debafeSMikulas Patocka 	wc->freelist.prev = LIST_POISON2;
77748debafeSMikulas Patocka }
77848debafeSMikulas Patocka 
77948debafeSMikulas Patocka static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
78048debafeSMikulas Patocka {
78148debafeSMikulas Patocka 	writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
78248debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
78348debafeSMikulas Patocka 		writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
78448debafeSMikulas Patocka }
78548debafeSMikulas Patocka 
78648debafeSMikulas Patocka static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
78748debafeSMikulas Patocka {
78848debafeSMikulas Patocka 	return read_seq_count(wc, e) < wc->seq_count;
78948debafeSMikulas Patocka }
79048debafeSMikulas Patocka 
79148debafeSMikulas Patocka static void writecache_flush(struct dm_writecache *wc)
79248debafeSMikulas Patocka {
79348debafeSMikulas Patocka 	struct wc_entry *e, *e2;
79448debafeSMikulas Patocka 	bool need_flush_after_free;
79548debafeSMikulas Patocka 
79648debafeSMikulas Patocka 	wc->uncommitted_blocks = 0;
79748debafeSMikulas Patocka 	del_timer(&wc->autocommit_timer);
79848debafeSMikulas Patocka 
79948debafeSMikulas Patocka 	if (list_empty(&wc->lru))
80048debafeSMikulas Patocka 		return;
80148debafeSMikulas Patocka 
80248debafeSMikulas Patocka 	e = container_of(wc->lru.next, struct wc_entry, lru);
80348debafeSMikulas Patocka 	if (writecache_entry_is_committed(wc, e)) {
80448debafeSMikulas Patocka 		if (wc->overwrote_committed) {
80548debafeSMikulas Patocka 			writecache_wait_for_ios(wc, WRITE);
80648debafeSMikulas Patocka 			writecache_disk_flush(wc, wc->ssd_dev);
80748debafeSMikulas Patocka 			wc->overwrote_committed = false;
80848debafeSMikulas Patocka 		}
80948debafeSMikulas Patocka 		return;
81048debafeSMikulas Patocka 	}
81148debafeSMikulas Patocka 	while (1) {
81248debafeSMikulas Patocka 		writecache_flush_entry(wc, e);
81348debafeSMikulas Patocka 		if (unlikely(e->lru.next == &wc->lru))
81448debafeSMikulas Patocka 			break;
81548debafeSMikulas Patocka 		e2 = container_of(e->lru.next, struct wc_entry, lru);
81648debafeSMikulas Patocka 		if (writecache_entry_is_committed(wc, e2))
81748debafeSMikulas Patocka 			break;
81848debafeSMikulas Patocka 		e = e2;
81948debafeSMikulas Patocka 		cond_resched();
82048debafeSMikulas Patocka 	}
821aa950920SMikulas Patocka 	writecache_commit_flushed(wc, true);
82248debafeSMikulas Patocka 
82348debafeSMikulas Patocka 	wc->seq_count++;
82448debafeSMikulas Patocka 	pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
825dc8a01aeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
826aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
827dc8a01aeSMikulas Patocka 	else
828dc8a01aeSMikulas Patocka 		ssd_commit_superblock(wc);
82948debafeSMikulas Patocka 
83048debafeSMikulas Patocka 	wc->overwrote_committed = false;
83148debafeSMikulas Patocka 
83248debafeSMikulas Patocka 	need_flush_after_free = false;
83348debafeSMikulas Patocka 	while (1) {
83448debafeSMikulas Patocka 		/* Free another committed entry with lower seq-count */
83548debafeSMikulas Patocka 		struct rb_node *rb_node = rb_prev(&e->rb_node);
83648debafeSMikulas Patocka 
83748debafeSMikulas Patocka 		if (rb_node) {
83848debafeSMikulas Patocka 			e2 = container_of(rb_node, struct wc_entry, rb_node);
83948debafeSMikulas Patocka 			if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
84048debafeSMikulas Patocka 			    likely(!e2->write_in_progress)) {
84148debafeSMikulas Patocka 				writecache_free_entry(wc, e2);
84248debafeSMikulas Patocka 				need_flush_after_free = true;
84348debafeSMikulas Patocka 			}
84448debafeSMikulas Patocka 		}
84548debafeSMikulas Patocka 		if (unlikely(e->lru.prev == &wc->lru))
84648debafeSMikulas Patocka 			break;
84748debafeSMikulas Patocka 		e = container_of(e->lru.prev, struct wc_entry, lru);
84848debafeSMikulas Patocka 		cond_resched();
84948debafeSMikulas Patocka 	}
85048debafeSMikulas Patocka 
85148debafeSMikulas Patocka 	if (need_flush_after_free)
852aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
85348debafeSMikulas Patocka }
85448debafeSMikulas Patocka 
85548debafeSMikulas Patocka static void writecache_flush_work(struct work_struct *work)
85648debafeSMikulas Patocka {
85748debafeSMikulas Patocka 	struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
85848debafeSMikulas Patocka 
85948debafeSMikulas Patocka 	wc_lock(wc);
86048debafeSMikulas Patocka 	writecache_flush(wc);
86148debafeSMikulas Patocka 	wc_unlock(wc);
86248debafeSMikulas Patocka }
86348debafeSMikulas Patocka 
86448debafeSMikulas Patocka static void writecache_autocommit_timer(struct timer_list *t)
86548debafeSMikulas Patocka {
86648debafeSMikulas Patocka 	struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
86748debafeSMikulas Patocka 	if (!writecache_has_error(wc))
86848debafeSMikulas Patocka 		queue_work(wc->writeback_wq, &wc->flush_work);
86948debafeSMikulas Patocka }
87048debafeSMikulas Patocka 
87148debafeSMikulas Patocka static void writecache_schedule_autocommit(struct dm_writecache *wc)
87248debafeSMikulas Patocka {
87348debafeSMikulas Patocka 	if (!timer_pending(&wc->autocommit_timer))
87448debafeSMikulas Patocka 		mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
87548debafeSMikulas Patocka }
87648debafeSMikulas Patocka 
87748debafeSMikulas Patocka static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
87848debafeSMikulas Patocka {
87948debafeSMikulas Patocka 	struct wc_entry *e;
88048debafeSMikulas Patocka 	bool discarded_something = false;
88148debafeSMikulas Patocka 
88248debafeSMikulas Patocka 	e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
88348debafeSMikulas Patocka 	if (unlikely(!e))
88448debafeSMikulas Patocka 		return;
88548debafeSMikulas Patocka 
88648debafeSMikulas Patocka 	while (read_original_sector(wc, e) < end) {
88748debafeSMikulas Patocka 		struct rb_node *node = rb_next(&e->rb_node);
88848debafeSMikulas Patocka 
88948debafeSMikulas Patocka 		if (likely(!e->write_in_progress)) {
89048debafeSMikulas Patocka 			if (!discarded_something) {
891a143e172SHuaisheng Ye 				if (!WC_MODE_PMEM(wc)) {
89248debafeSMikulas Patocka 					writecache_wait_for_ios(wc, READ);
89348debafeSMikulas Patocka 					writecache_wait_for_ios(wc, WRITE);
894a143e172SHuaisheng Ye 				}
89548debafeSMikulas Patocka 				discarded_something = true;
89648debafeSMikulas Patocka 			}
89739495b12SHuaisheng Ye 			if (!writecache_entry_is_committed(wc, e))
89839495b12SHuaisheng Ye 				wc->uncommitted_blocks--;
89948debafeSMikulas Patocka 			writecache_free_entry(wc, e);
90048debafeSMikulas Patocka 		}
90148debafeSMikulas Patocka 
90284420b1eSHuaisheng Ye 		if (unlikely(!node))
90348debafeSMikulas Patocka 			break;
90448debafeSMikulas Patocka 
90548debafeSMikulas Patocka 		e = container_of(node, struct wc_entry, rb_node);
90648debafeSMikulas Patocka 	}
90748debafeSMikulas Patocka 
90848debafeSMikulas Patocka 	if (discarded_something)
909aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
91048debafeSMikulas Patocka }
91148debafeSMikulas Patocka 
91248debafeSMikulas Patocka static bool writecache_wait_for_writeback(struct dm_writecache *wc)
91348debafeSMikulas Patocka {
91448debafeSMikulas Patocka 	if (wc->writeback_size) {
91548debafeSMikulas Patocka 		writecache_wait_on_freelist(wc);
91648debafeSMikulas Patocka 		return true;
91748debafeSMikulas Patocka 	}
91848debafeSMikulas Patocka 	return false;
91948debafeSMikulas Patocka }
92048debafeSMikulas Patocka 
92148debafeSMikulas Patocka static void writecache_suspend(struct dm_target *ti)
92248debafeSMikulas Patocka {
92348debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
92448debafeSMikulas Patocka 	bool flush_on_suspend;
92548debafeSMikulas Patocka 
92648debafeSMikulas Patocka 	del_timer_sync(&wc->autocommit_timer);
9273923d485SMikulas Patocka 	del_timer_sync(&wc->max_age_timer);
92848debafeSMikulas Patocka 
92948debafeSMikulas Patocka 	wc_lock(wc);
93048debafeSMikulas Patocka 	writecache_flush(wc);
93148debafeSMikulas Patocka 	flush_on_suspend = wc->flush_on_suspend;
93248debafeSMikulas Patocka 	if (flush_on_suspend) {
93348debafeSMikulas Patocka 		wc->flush_on_suspend = false;
93448debafeSMikulas Patocka 		wc->writeback_all++;
93548debafeSMikulas Patocka 		queue_work(wc->writeback_wq, &wc->writeback_work);
93648debafeSMikulas Patocka 	}
93748debafeSMikulas Patocka 	wc_unlock(wc);
93848debafeSMikulas Patocka 
939adc0daadSMikulas Patocka 	drain_workqueue(wc->writeback_wq);
94048debafeSMikulas Patocka 
94148debafeSMikulas Patocka 	wc_lock(wc);
94248debafeSMikulas Patocka 	if (flush_on_suspend)
94348debafeSMikulas Patocka 		wc->writeback_all--;
94448debafeSMikulas Patocka 	while (writecache_wait_for_writeback(wc));
94548debafeSMikulas Patocka 
94648debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
94748debafeSMikulas Patocka 		persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
94848debafeSMikulas Patocka 
94948debafeSMikulas Patocka 	writecache_poison_lists(wc);
95048debafeSMikulas Patocka 
95148debafeSMikulas Patocka 	wc_unlock(wc);
95248debafeSMikulas Patocka }
95348debafeSMikulas Patocka 
95448debafeSMikulas Patocka static int writecache_alloc_entries(struct dm_writecache *wc)
95548debafeSMikulas Patocka {
95648debafeSMikulas Patocka 	size_t b;
95748debafeSMikulas Patocka 
95848debafeSMikulas Patocka 	if (wc->entries)
95948debafeSMikulas Patocka 		return 0;
96050a7d3baSKees Cook 	wc->entries = vmalloc(array_size(sizeof(struct wc_entry), wc->n_blocks));
96148debafeSMikulas Patocka 	if (!wc->entries)
96248debafeSMikulas Patocka 		return -ENOMEM;
96348debafeSMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
96448debafeSMikulas Patocka 		struct wc_entry *e = &wc->entries[b];
96548debafeSMikulas Patocka 		e->index = b;
96648debafeSMikulas Patocka 		e->write_in_progress = false;
9671edaa447SMikulas Patocka 		cond_resched();
96848debafeSMikulas Patocka 	}
96948debafeSMikulas Patocka 
97048debafeSMikulas Patocka 	return 0;
97148debafeSMikulas Patocka }
97248debafeSMikulas Patocka 
97331b22120SMikulas Patocka static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors)
97431b22120SMikulas Patocka {
97531b22120SMikulas Patocka 	struct dm_io_region region;
97631b22120SMikulas Patocka 	struct dm_io_request req;
97731b22120SMikulas Patocka 
97831b22120SMikulas Patocka 	region.bdev = wc->ssd_dev->bdev;
97931b22120SMikulas Patocka 	region.sector = wc->start_sector;
98031b22120SMikulas Patocka 	region.count = n_sectors;
981*581075e4SBart Van Assche 	req.bi_opf = REQ_OP_READ | REQ_SYNC;
98231b22120SMikulas Patocka 	req.mem.type = DM_IO_VMA;
98331b22120SMikulas Patocka 	req.mem.ptr.vma = (char *)wc->memory_map;
98431b22120SMikulas Patocka 	req.client = wc->dm_io;
98531b22120SMikulas Patocka 	req.notify.fn = NULL;
98631b22120SMikulas Patocka 
98731b22120SMikulas Patocka 	return dm_io(&req, 1, &region, NULL);
98831b22120SMikulas Patocka }
98931b22120SMikulas Patocka 
99048debafeSMikulas Patocka static void writecache_resume(struct dm_target *ti)
99148debafeSMikulas Patocka {
99248debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
99348debafeSMikulas Patocka 	size_t b;
99448debafeSMikulas Patocka 	bool need_flush = false;
99548debafeSMikulas Patocka 	__le64 sb_seq_count;
99648debafeSMikulas Patocka 	int r;
99748debafeSMikulas Patocka 
99848debafeSMikulas Patocka 	wc_lock(wc);
99948debafeSMikulas Patocka 
1000d9928ac5SMike Snitzer 	wc->data_device_sectors = bdev_nr_sectors(wc->dev->bdev);
10014134455fSMikulas Patocka 
100231b22120SMikulas Patocka 	if (WC_MODE_PMEM(wc)) {
100348debafeSMikulas Patocka 		persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
100431b22120SMikulas Patocka 	} else {
100531b22120SMikulas Patocka 		r = writecache_read_metadata(wc, wc->metadata_sectors);
100631b22120SMikulas Patocka 		if (r) {
100731b22120SMikulas Patocka 			size_t sb_entries_offset;
100831b22120SMikulas Patocka 			writecache_error(wc, r, "unable to read metadata: %d", r);
100931b22120SMikulas Patocka 			sb_entries_offset = offsetof(struct wc_memory_superblock, entries);
101031b22120SMikulas Patocka 			memset((char *)wc->memory_map + sb_entries_offset, -1,
101131b22120SMikulas Patocka 			       (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset);
101231b22120SMikulas Patocka 		}
101331b22120SMikulas Patocka 	}
101448debafeSMikulas Patocka 
101548debafeSMikulas Patocka 	wc->tree = RB_ROOT;
101648debafeSMikulas Patocka 	INIT_LIST_HEAD(&wc->lru);
101748debafeSMikulas Patocka 	if (WC_MODE_SORT_FREELIST(wc)) {
101848debafeSMikulas Patocka 		wc->freetree = RB_ROOT;
101948debafeSMikulas Patocka 		wc->current_free = NULL;
102048debafeSMikulas Patocka 	} else {
102148debafeSMikulas Patocka 		INIT_LIST_HEAD(&wc->freelist);
102248debafeSMikulas Patocka 	}
102348debafeSMikulas Patocka 	wc->freelist_size = 0;
102448debafeSMikulas Patocka 
1025ec6347bbSDan Williams 	r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count,
1026ec6347bbSDan Williams 			      sizeof(uint64_t));
102748debafeSMikulas Patocka 	if (r) {
102848debafeSMikulas Patocka 		writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
102948debafeSMikulas Patocka 		sb_seq_count = cpu_to_le64(0);
103048debafeSMikulas Patocka 	}
103148debafeSMikulas Patocka 	wc->seq_count = le64_to_cpu(sb_seq_count);
103248debafeSMikulas Patocka 
103348debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
103448debafeSMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
103548debafeSMikulas Patocka 		struct wc_entry *e = &wc->entries[b];
103648debafeSMikulas Patocka 		struct wc_memory_entry wme;
103748debafeSMikulas Patocka 		if (writecache_has_error(wc)) {
103848debafeSMikulas Patocka 			e->original_sector = -1;
103948debafeSMikulas Patocka 			e->seq_count = -1;
104048debafeSMikulas Patocka 			continue;
104148debafeSMikulas Patocka 		}
1042ec6347bbSDan Williams 		r = copy_mc_to_kernel(&wme, memory_entry(wc, e),
1043ec6347bbSDan Williams 				      sizeof(struct wc_memory_entry));
104448debafeSMikulas Patocka 		if (r) {
104548debafeSMikulas Patocka 			writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
104648debafeSMikulas Patocka 					 (unsigned long)b, r);
104748debafeSMikulas Patocka 			e->original_sector = -1;
104848debafeSMikulas Patocka 			e->seq_count = -1;
104948debafeSMikulas Patocka 		} else {
105048debafeSMikulas Patocka 			e->original_sector = le64_to_cpu(wme.original_sector);
105148debafeSMikulas Patocka 			e->seq_count = le64_to_cpu(wme.seq_count);
105248debafeSMikulas Patocka 		}
10531edaa447SMikulas Patocka 		cond_resched();
105448debafeSMikulas Patocka 	}
105548debafeSMikulas Patocka #endif
105648debafeSMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
105748debafeSMikulas Patocka 		struct wc_entry *e = &wc->entries[b];
105848debafeSMikulas Patocka 		if (!writecache_entry_is_committed(wc, e)) {
105948debafeSMikulas Patocka 			if (read_seq_count(wc, e) != -1) {
106048debafeSMikulas Patocka erase_this:
106148debafeSMikulas Patocka 				clear_seq_count(wc, e);
106248debafeSMikulas Patocka 				need_flush = true;
106348debafeSMikulas Patocka 			}
106448debafeSMikulas Patocka 			writecache_add_to_freelist(wc, e);
106548debafeSMikulas Patocka 		} else {
106648debafeSMikulas Patocka 			struct wc_entry *old;
106748debafeSMikulas Patocka 
106848debafeSMikulas Patocka 			old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
106948debafeSMikulas Patocka 			if (!old) {
107048debafeSMikulas Patocka 				writecache_insert_entry(wc, e);
107148debafeSMikulas Patocka 			} else {
107248debafeSMikulas Patocka 				if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
107348debafeSMikulas Patocka 					writecache_error(wc, -EINVAL,
107448debafeSMikulas Patocka 						 "two identical entries, position %llu, sector %llu, sequence %llu",
107548debafeSMikulas Patocka 						 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
107648debafeSMikulas Patocka 						 (unsigned long long)read_seq_count(wc, e));
107748debafeSMikulas Patocka 				}
107848debafeSMikulas Patocka 				if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
107948debafeSMikulas Patocka 					goto erase_this;
108048debafeSMikulas Patocka 				} else {
108148debafeSMikulas Patocka 					writecache_free_entry(wc, old);
108248debafeSMikulas Patocka 					writecache_insert_entry(wc, e);
108348debafeSMikulas Patocka 					need_flush = true;
108448debafeSMikulas Patocka 				}
108548debafeSMikulas Patocka 			}
108648debafeSMikulas Patocka 		}
108748debafeSMikulas Patocka 		cond_resched();
108848debafeSMikulas Patocka 	}
108948debafeSMikulas Patocka 
109048debafeSMikulas Patocka 	if (need_flush) {
109148debafeSMikulas Patocka 		writecache_flush_all_metadata(wc);
1092aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
109348debafeSMikulas Patocka 	}
109448debafeSMikulas Patocka 
109541c526c5SMikulas Patocka 	writecache_verify_watermark(wc);
109641c526c5SMikulas Patocka 
10973923d485SMikulas Patocka 	if (wc->max_age != MAX_AGE_UNSPECIFIED)
10983923d485SMikulas Patocka 		mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
10993923d485SMikulas Patocka 
110048debafeSMikulas Patocka 	wc_unlock(wc);
110148debafeSMikulas Patocka }
110248debafeSMikulas Patocka 
110348debafeSMikulas Patocka static int process_flush_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
110448debafeSMikulas Patocka {
110548debafeSMikulas Patocka 	if (argc != 1)
110648debafeSMikulas Patocka 		return -EINVAL;
110748debafeSMikulas Patocka 
110848debafeSMikulas Patocka 	wc_lock(wc);
110948debafeSMikulas Patocka 	if (dm_suspended(wc->ti)) {
111048debafeSMikulas Patocka 		wc_unlock(wc);
111148debafeSMikulas Patocka 		return -EBUSY;
111248debafeSMikulas Patocka 	}
111348debafeSMikulas Patocka 	if (writecache_has_error(wc)) {
111448debafeSMikulas Patocka 		wc_unlock(wc);
111548debafeSMikulas Patocka 		return -EIO;
111648debafeSMikulas Patocka 	}
111748debafeSMikulas Patocka 
111848debafeSMikulas Patocka 	writecache_flush(wc);
111948debafeSMikulas Patocka 	wc->writeback_all++;
112048debafeSMikulas Patocka 	queue_work(wc->writeback_wq, &wc->writeback_work);
112148debafeSMikulas Patocka 	wc_unlock(wc);
112248debafeSMikulas Patocka 
112348debafeSMikulas Patocka 	flush_workqueue(wc->writeback_wq);
112448debafeSMikulas Patocka 
112548debafeSMikulas Patocka 	wc_lock(wc);
112648debafeSMikulas Patocka 	wc->writeback_all--;
112748debafeSMikulas Patocka 	if (writecache_has_error(wc)) {
112848debafeSMikulas Patocka 		wc_unlock(wc);
112948debafeSMikulas Patocka 		return -EIO;
113048debafeSMikulas Patocka 	}
113148debafeSMikulas Patocka 	wc_unlock(wc);
113248debafeSMikulas Patocka 
113348debafeSMikulas Patocka 	return 0;
113448debafeSMikulas Patocka }
113548debafeSMikulas Patocka 
113648debafeSMikulas Patocka static int process_flush_on_suspend_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
113748debafeSMikulas Patocka {
113848debafeSMikulas Patocka 	if (argc != 1)
113948debafeSMikulas Patocka 		return -EINVAL;
114048debafeSMikulas Patocka 
114148debafeSMikulas Patocka 	wc_lock(wc);
114248debafeSMikulas Patocka 	wc->flush_on_suspend = true;
114348debafeSMikulas Patocka 	wc_unlock(wc);
114448debafeSMikulas Patocka 
114548debafeSMikulas Patocka 	return 0;
114648debafeSMikulas Patocka }
114748debafeSMikulas Patocka 
114893de44ebSMikulas Patocka static void activate_cleaner(struct dm_writecache *wc)
114993de44ebSMikulas Patocka {
115093de44ebSMikulas Patocka 	wc->flush_on_suspend = true;
115193de44ebSMikulas Patocka 	wc->cleaner = true;
115293de44ebSMikulas Patocka 	wc->freelist_high_watermark = wc->n_blocks;
115393de44ebSMikulas Patocka 	wc->freelist_low_watermark = wc->n_blocks;
115493de44ebSMikulas Patocka }
115593de44ebSMikulas Patocka 
115693de44ebSMikulas Patocka static int process_cleaner_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
115793de44ebSMikulas Patocka {
115893de44ebSMikulas Patocka 	if (argc != 1)
115993de44ebSMikulas Patocka 		return -EINVAL;
116093de44ebSMikulas Patocka 
116193de44ebSMikulas Patocka 	wc_lock(wc);
116293de44ebSMikulas Patocka 	activate_cleaner(wc);
116393de44ebSMikulas Patocka 	if (!dm_suspended(wc->ti))
116493de44ebSMikulas Patocka 		writecache_verify_watermark(wc);
116593de44ebSMikulas Patocka 	wc_unlock(wc);
116693de44ebSMikulas Patocka 
116793de44ebSMikulas Patocka 	return 0;
116893de44ebSMikulas Patocka }
116993de44ebSMikulas Patocka 
1170e3a35d03SMikulas Patocka static int process_clear_stats_mesg(unsigned argc, char **argv, struct dm_writecache *wc)
1171e3a35d03SMikulas Patocka {
1172e3a35d03SMikulas Patocka 	if (argc != 1)
1173e3a35d03SMikulas Patocka 		return -EINVAL;
1174e3a35d03SMikulas Patocka 
1175e3a35d03SMikulas Patocka 	wc_lock(wc);
1176e3a35d03SMikulas Patocka 	memset(&wc->stats, 0, sizeof wc->stats);
1177e3a35d03SMikulas Patocka 	wc_unlock(wc);
1178e3a35d03SMikulas Patocka 
1179e3a35d03SMikulas Patocka 	return 0;
1180e3a35d03SMikulas Patocka }
1181e3a35d03SMikulas Patocka 
118248debafeSMikulas Patocka static int writecache_message(struct dm_target *ti, unsigned argc, char **argv,
118348debafeSMikulas Patocka 			      char *result, unsigned maxlen)
118448debafeSMikulas Patocka {
118548debafeSMikulas Patocka 	int r = -EINVAL;
118648debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
118748debafeSMikulas Patocka 
118848debafeSMikulas Patocka 	if (!strcasecmp(argv[0], "flush"))
118948debafeSMikulas Patocka 		r = process_flush_mesg(argc, argv, wc);
119048debafeSMikulas Patocka 	else if (!strcasecmp(argv[0], "flush_on_suspend"))
119148debafeSMikulas Patocka 		r = process_flush_on_suspend_mesg(argc, argv, wc);
119293de44ebSMikulas Patocka 	else if (!strcasecmp(argv[0], "cleaner"))
119393de44ebSMikulas Patocka 		r = process_cleaner_mesg(argc, argv, wc);
1194e3a35d03SMikulas Patocka 	else if (!strcasecmp(argv[0], "clear_stats"))
1195e3a35d03SMikulas Patocka 		r = process_clear_stats_mesg(argc, argv, wc);
119648debafeSMikulas Patocka 	else
119748debafeSMikulas Patocka 		DMERR("unrecognised message received: %s", argv[0]);
119848debafeSMikulas Patocka 
119948debafeSMikulas Patocka 	return r;
120048debafeSMikulas Patocka }
120148debafeSMikulas Patocka 
120248338daaSMikulas Patocka static void memcpy_flushcache_optimized(void *dest, void *source, size_t size)
120348338daaSMikulas Patocka {
120448338daaSMikulas Patocka 	/*
120548338daaSMikulas Patocka 	 * clflushopt performs better with block size 1024, 2048, 4096
120648338daaSMikulas Patocka 	 * non-temporal stores perform better with block size 512
120748338daaSMikulas Patocka 	 *
120848338daaSMikulas Patocka 	 * block size   512             1024            2048            4096
120948338daaSMikulas Patocka 	 * movnti       496 MB/s        642 MB/s        725 MB/s        744 MB/s
121048338daaSMikulas Patocka 	 * clflushopt   373 MB/s        688 MB/s        1.1 GB/s        1.2 GB/s
121148338daaSMikulas Patocka 	 *
121248338daaSMikulas Patocka 	 * We see that movnti performs better for 512-byte blocks, and
121348338daaSMikulas Patocka 	 * clflushopt performs better for 1024-byte and larger blocks. So, we
121448338daaSMikulas Patocka 	 * prefer clflushopt for sizes >= 768.
121548338daaSMikulas Patocka 	 *
121648338daaSMikulas Patocka 	 * NOTE: this happens to be the case now (with dm-writecache's single
121748338daaSMikulas Patocka 	 * threaded model) but re-evaluate this once memcpy_flushcache() is
121848338daaSMikulas Patocka 	 * enabled to use movdir64b which might invalidate this performance
121948338daaSMikulas Patocka 	 * advantage seen with cache-allocating-writes plus flushing.
122048338daaSMikulas Patocka 	 */
122148338daaSMikulas Patocka #ifdef CONFIG_X86
122248338daaSMikulas Patocka 	if (static_cpu_has(X86_FEATURE_CLFLUSHOPT) &&
122348338daaSMikulas Patocka 	    likely(boot_cpu_data.x86_clflush_size == 64) &&
122448338daaSMikulas Patocka 	    likely(size >= 768)) {
122548338daaSMikulas Patocka 		do {
122648338daaSMikulas Patocka 			memcpy((void *)dest, (void *)source, 64);
122748338daaSMikulas Patocka 			clflushopt((void *)dest);
122848338daaSMikulas Patocka 			dest += 64;
122948338daaSMikulas Patocka 			source += 64;
123048338daaSMikulas Patocka 			size -= 64;
123148338daaSMikulas Patocka 		} while (size >= 64);
123248338daaSMikulas Patocka 		return;
123348338daaSMikulas Patocka 	}
123448338daaSMikulas Patocka #endif
123548338daaSMikulas Patocka 	memcpy_flushcache(dest, source, size);
123648338daaSMikulas Patocka }
123748338daaSMikulas Patocka 
123848debafeSMikulas Patocka static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
123948debafeSMikulas Patocka {
124048debafeSMikulas Patocka 	void *buf;
124148debafeSMikulas Patocka 	unsigned size;
124248debafeSMikulas Patocka 	int rw = bio_data_dir(bio);
124348debafeSMikulas Patocka 	unsigned remaining_size = wc->block_size;
124448debafeSMikulas Patocka 
124548debafeSMikulas Patocka 	do {
124648debafeSMikulas Patocka 		struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
124718a6234cSChristoph Hellwig 		buf = bvec_kmap_local(&bv);
124848debafeSMikulas Patocka 		size = bv.bv_len;
124948debafeSMikulas Patocka 		if (unlikely(size > remaining_size))
125048debafeSMikulas Patocka 			size = remaining_size;
125148debafeSMikulas Patocka 
125248debafeSMikulas Patocka 		if (rw == READ) {
125348debafeSMikulas Patocka 			int r;
1254ec6347bbSDan Williams 			r = copy_mc_to_kernel(buf, data, size);
125548debafeSMikulas Patocka 			flush_dcache_page(bio_page(bio));
125648debafeSMikulas Patocka 			if (unlikely(r)) {
125748debafeSMikulas Patocka 				writecache_error(wc, r, "hardware memory error when reading data: %d", r);
125848debafeSMikulas Patocka 				bio->bi_status = BLK_STS_IOERR;
125948debafeSMikulas Patocka 			}
126048debafeSMikulas Patocka 		} else {
126148debafeSMikulas Patocka 			flush_dcache_page(bio_page(bio));
126248338daaSMikulas Patocka 			memcpy_flushcache_optimized(data, buf, size);
126348debafeSMikulas Patocka 		}
126448debafeSMikulas Patocka 
126518a6234cSChristoph Hellwig 		kunmap_local(buf);
126648debafeSMikulas Patocka 
126748debafeSMikulas Patocka 		data = (char *)data + size;
126848debafeSMikulas Patocka 		remaining_size -= size;
126948debafeSMikulas Patocka 		bio_advance(bio, size);
127048debafeSMikulas Patocka 	} while (unlikely(remaining_size));
127148debafeSMikulas Patocka }
127248debafeSMikulas Patocka 
127348debafeSMikulas Patocka static int writecache_flush_thread(void *data)
127448debafeSMikulas Patocka {
127548debafeSMikulas Patocka 	struct dm_writecache *wc = data;
127648debafeSMikulas Patocka 
127748debafeSMikulas Patocka 	while (1) {
127848debafeSMikulas Patocka 		struct bio *bio;
127948debafeSMikulas Patocka 
128048debafeSMikulas Patocka 		wc_lock(wc);
128148debafeSMikulas Patocka 		bio = bio_list_pop(&wc->flush_list);
128248debafeSMikulas Patocka 		if (!bio) {
128348debafeSMikulas Patocka 			set_current_state(TASK_INTERRUPTIBLE);
128448debafeSMikulas Patocka 			wc_unlock(wc);
128548debafeSMikulas Patocka 
128648debafeSMikulas Patocka 			if (unlikely(kthread_should_stop())) {
128748debafeSMikulas Patocka 				set_current_state(TASK_RUNNING);
128848debafeSMikulas Patocka 				break;
128948debafeSMikulas Patocka 			}
129048debafeSMikulas Patocka 
129148debafeSMikulas Patocka 			schedule();
129248debafeSMikulas Patocka 			continue;
129348debafeSMikulas Patocka 		}
129448debafeSMikulas Patocka 
129548debafeSMikulas Patocka 		if (bio_op(bio) == REQ_OP_DISCARD) {
129648debafeSMikulas Patocka 			writecache_discard(wc, bio->bi_iter.bi_sector,
129748debafeSMikulas Patocka 					   bio_end_sector(bio));
129848debafeSMikulas Patocka 			wc_unlock(wc);
129948debafeSMikulas Patocka 			bio_set_dev(bio, wc->dev->bdev);
1300ed00aabdSChristoph Hellwig 			submit_bio_noacct(bio);
130148debafeSMikulas Patocka 		} else {
130248debafeSMikulas Patocka 			writecache_flush(wc);
130348debafeSMikulas Patocka 			wc_unlock(wc);
130448debafeSMikulas Patocka 			if (writecache_has_error(wc))
130548debafeSMikulas Patocka 				bio->bi_status = BLK_STS_IOERR;
130648debafeSMikulas Patocka 			bio_endio(bio);
130748debafeSMikulas Patocka 		}
130848debafeSMikulas Patocka 	}
130948debafeSMikulas Patocka 
131048debafeSMikulas Patocka 	return 0;
131148debafeSMikulas Patocka }
131248debafeSMikulas Patocka 
131348debafeSMikulas Patocka static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
131448debafeSMikulas Patocka {
131548debafeSMikulas Patocka 	if (bio_list_empty(&wc->flush_list))
131648debafeSMikulas Patocka 		wake_up_process(wc->flush_thread);
131748debafeSMikulas Patocka 	bio_list_add(&wc->flush_list, bio);
131848debafeSMikulas Patocka }
131948debafeSMikulas Patocka 
1320cdd4d783SMike Snitzer enum wc_map_op {
1321cdd4d783SMike Snitzer 	WC_MAP_SUBMIT,
1322cdd4d783SMike Snitzer 	WC_MAP_REMAP,
1323cdd4d783SMike Snitzer 	WC_MAP_REMAP_ORIGIN,
1324cdd4d783SMike Snitzer 	WC_MAP_RETURN,
1325cdd4d783SMike Snitzer 	WC_MAP_ERROR,
1326cdd4d783SMike Snitzer };
1327cdd4d783SMike Snitzer 
13284d020b3aSMike Snitzer static enum wc_map_op writecache_map_remap_origin(struct dm_writecache *wc, struct bio *bio,
13294d020b3aSMike Snitzer 						  struct wc_entry *e)
13304d020b3aSMike Snitzer {
13314d020b3aSMike Snitzer 	if (e) {
13324d020b3aSMike Snitzer 		sector_t next_boundary =
13334d020b3aSMike Snitzer 			read_original_sector(wc, e) - bio->bi_iter.bi_sector;
13344d020b3aSMike Snitzer 		if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT)
13354d020b3aSMike Snitzer 			dm_accept_partial_bio(bio, next_boundary);
13364d020b3aSMike Snitzer 	}
13374d020b3aSMike Snitzer 
13384d020b3aSMike Snitzer 	return WC_MAP_REMAP_ORIGIN;
13394d020b3aSMike Snitzer }
13404d020b3aSMike Snitzer 
1341cdd4d783SMike Snitzer static enum wc_map_op writecache_map_read(struct dm_writecache *wc, struct bio *bio)
1342cdd4d783SMike Snitzer {
1343cdd4d783SMike Snitzer 	enum wc_map_op map_op;
1344cdd4d783SMike Snitzer 	struct wc_entry *e;
1345cdd4d783SMike Snitzer 
1346cdd4d783SMike Snitzer read_next_block:
1347e3a35d03SMikulas Patocka 	wc->stats.reads++;
1348cdd4d783SMike Snitzer 	e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1349cdd4d783SMike Snitzer 	if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1350e3a35d03SMikulas Patocka 		wc->stats.read_hits++;
1351cdd4d783SMike Snitzer 		if (WC_MODE_PMEM(wc)) {
1352cdd4d783SMike Snitzer 			bio_copy_block(wc, bio, memory_data(wc, e));
1353cdd4d783SMike Snitzer 			if (bio->bi_iter.bi_size)
1354cdd4d783SMike Snitzer 				goto read_next_block;
1355cdd4d783SMike Snitzer 			map_op = WC_MAP_SUBMIT;
1356cdd4d783SMike Snitzer 		} else {
1357cdd4d783SMike Snitzer 			dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1358cdd4d783SMike Snitzer 			bio_set_dev(bio, wc->ssd_dev->bdev);
1359cdd4d783SMike Snitzer 			bio->bi_iter.bi_sector = cache_sector(wc, e);
1360cdd4d783SMike Snitzer 			if (!writecache_entry_is_committed(wc, e))
1361cdd4d783SMike Snitzer 				writecache_wait_for_ios(wc, WRITE);
1362cdd4d783SMike Snitzer 			map_op = WC_MAP_REMAP;
1363cdd4d783SMike Snitzer 		}
1364cdd4d783SMike Snitzer 	} else {
13654d020b3aSMike Snitzer 		map_op = writecache_map_remap_origin(wc, bio, e);
1366cdd4d783SMike Snitzer 	}
1367cdd4d783SMike Snitzer 
1368cdd4d783SMike Snitzer 	return map_op;
1369cdd4d783SMike Snitzer }
1370cdd4d783SMike Snitzer 
1371cdd4d783SMike Snitzer static enum wc_map_op writecache_bio_copy_ssd(struct dm_writecache *wc, struct bio *bio,
1372cdd4d783SMike Snitzer 					      struct wc_entry *e, bool search_used)
1373cdd4d783SMike Snitzer {
1374cdd4d783SMike Snitzer 	unsigned bio_size = wc->block_size;
1375cdd4d783SMike Snitzer 	sector_t start_cache_sec = cache_sector(wc, e);
1376cdd4d783SMike Snitzer 	sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT);
1377cdd4d783SMike Snitzer 
1378cdd4d783SMike Snitzer 	while (bio_size < bio->bi_iter.bi_size) {
1379cdd4d783SMike Snitzer 		if (!search_used) {
1380cdd4d783SMike Snitzer 			struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec);
1381cdd4d783SMike Snitzer 			if (!f)
1382cdd4d783SMike Snitzer 				break;
1383cdd4d783SMike Snitzer 			write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector +
1384cdd4d783SMike Snitzer 							(bio_size >> SECTOR_SHIFT), wc->seq_count);
1385cdd4d783SMike Snitzer 			writecache_insert_entry(wc, f);
1386cdd4d783SMike Snitzer 			wc->uncommitted_blocks++;
1387cdd4d783SMike Snitzer 		} else {
1388cdd4d783SMike Snitzer 			struct wc_entry *f;
1389cdd4d783SMike Snitzer 			struct rb_node *next = rb_next(&e->rb_node);
1390cdd4d783SMike Snitzer 			if (!next)
1391cdd4d783SMike Snitzer 				break;
1392cdd4d783SMike Snitzer 			f = container_of(next, struct wc_entry, rb_node);
1393cdd4d783SMike Snitzer 			if (f != e + 1)
1394cdd4d783SMike Snitzer 				break;
1395cdd4d783SMike Snitzer 			if (read_original_sector(wc, f) !=
1396cdd4d783SMike Snitzer 			    read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1397cdd4d783SMike Snitzer 				break;
1398cdd4d783SMike Snitzer 			if (unlikely(f->write_in_progress))
1399cdd4d783SMike Snitzer 				break;
1400cdd4d783SMike Snitzer 			if (writecache_entry_is_committed(wc, f))
1401cdd4d783SMike Snitzer 				wc->overwrote_committed = true;
1402cdd4d783SMike Snitzer 			e = f;
1403cdd4d783SMike Snitzer 		}
1404cdd4d783SMike Snitzer 		bio_size += wc->block_size;
1405cdd4d783SMike Snitzer 		current_cache_sec += wc->block_size >> SECTOR_SHIFT;
1406cdd4d783SMike Snitzer 	}
1407cdd4d783SMike Snitzer 
1408cdd4d783SMike Snitzer 	bio_set_dev(bio, wc->ssd_dev->bdev);
1409cdd4d783SMike Snitzer 	bio->bi_iter.bi_sector = start_cache_sec;
1410cdd4d783SMike Snitzer 	dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT);
1411cdd4d783SMike Snitzer 
1412cdd4d783SMike Snitzer 	if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1413cdd4d783SMike Snitzer 		wc->uncommitted_blocks = 0;
1414cdd4d783SMike Snitzer 		queue_work(wc->writeback_wq, &wc->flush_work);
1415cdd4d783SMike Snitzer 	} else {
1416cdd4d783SMike Snitzer 		writecache_schedule_autocommit(wc);
1417cdd4d783SMike Snitzer 	}
1418cdd4d783SMike Snitzer 
1419cdd4d783SMike Snitzer 	return WC_MAP_REMAP;
1420cdd4d783SMike Snitzer }
1421cdd4d783SMike Snitzer 
1422cdd4d783SMike Snitzer static enum wc_map_op writecache_map_write(struct dm_writecache *wc, struct bio *bio)
142348debafeSMikulas Patocka {
142448debafeSMikulas Patocka 	struct wc_entry *e;
1425cdd4d783SMike Snitzer 
1426cdd4d783SMike Snitzer 	do {
1427cdd4d783SMike Snitzer 		bool found_entry = false;
1428cdd4d783SMike Snitzer 		bool search_used = false;
1429e3a35d03SMikulas Patocka 		wc->stats.writes++;
1430cdd4d783SMike Snitzer 		if (writecache_has_error(wc))
1431cdd4d783SMike Snitzer 			return WC_MAP_ERROR;
1432cdd4d783SMike Snitzer 		e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1433cdd4d783SMike Snitzer 		if (e) {
1434cdd4d783SMike Snitzer 			if (!writecache_entry_is_committed(wc, e)) {
1435e3a35d03SMikulas Patocka 				wc->stats.write_hits_uncommitted++;
1436cdd4d783SMike Snitzer 				search_used = true;
1437cdd4d783SMike Snitzer 				goto bio_copy;
1438cdd4d783SMike Snitzer 			}
1439e3a35d03SMikulas Patocka 			wc->stats.write_hits_committed++;
1440cdd4d783SMike Snitzer 			if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1441cdd4d783SMike Snitzer 				wc->overwrote_committed = true;
1442cdd4d783SMike Snitzer 				search_used = true;
1443cdd4d783SMike Snitzer 				goto bio_copy;
1444cdd4d783SMike Snitzer 			}
1445cdd4d783SMike Snitzer 			found_entry = true;
1446cdd4d783SMike Snitzer 		} else {
1447cdd4d783SMike Snitzer 			if (unlikely(wc->cleaner) ||
1448cdd4d783SMike Snitzer 			    (wc->metadata_only && !(bio->bi_opf & REQ_META)))
1449cdd4d783SMike Snitzer 				goto direct_write;
1450cdd4d783SMike Snitzer 		}
1451cdd4d783SMike Snitzer 		e = writecache_pop_from_freelist(wc, (sector_t)-1);
1452cdd4d783SMike Snitzer 		if (unlikely(!e)) {
1453cdd4d783SMike Snitzer 			if (!WC_MODE_PMEM(wc) && !found_entry) {
1454cdd4d783SMike Snitzer direct_write:
1455e3a35d03SMikulas Patocka 				wc->stats.writes_around++;
1456cdd4d783SMike Snitzer 				e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
14574d020b3aSMike Snitzer 				return writecache_map_remap_origin(wc, bio, e);
1458cdd4d783SMike Snitzer 			}
1459e3a35d03SMikulas Patocka 			wc->stats.writes_blocked_on_freelist++;
1460cdd4d783SMike Snitzer 			writecache_wait_on_freelist(wc);
1461cdd4d783SMike Snitzer 			continue;
1462cdd4d783SMike Snitzer 		}
1463cdd4d783SMike Snitzer 		write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1464cdd4d783SMike Snitzer 		writecache_insert_entry(wc, e);
1465cdd4d783SMike Snitzer 		wc->uncommitted_blocks++;
1466e3a35d03SMikulas Patocka 		wc->stats.writes_allocate++;
1467cdd4d783SMike Snitzer bio_copy:
1468cdd4d783SMike Snitzer 		if (WC_MODE_PMEM(wc))
1469cdd4d783SMike Snitzer 			bio_copy_block(wc, bio, memory_data(wc, e));
1470cdd4d783SMike Snitzer 		else
1471cdd4d783SMike Snitzer 			return writecache_bio_copy_ssd(wc, bio, e, search_used);
1472cdd4d783SMike Snitzer 	} while (bio->bi_iter.bi_size);
1473cdd4d783SMike Snitzer 
1474cdd4d783SMike Snitzer 	if (unlikely(bio->bi_opf & REQ_FUA || wc->uncommitted_blocks >= wc->autocommit_blocks))
1475cdd4d783SMike Snitzer 		writecache_flush(wc);
1476cdd4d783SMike Snitzer 	else
1477cdd4d783SMike Snitzer 		writecache_schedule_autocommit(wc);
1478cdd4d783SMike Snitzer 
1479cdd4d783SMike Snitzer 	return WC_MAP_SUBMIT;
1480cdd4d783SMike Snitzer }
1481cdd4d783SMike Snitzer 
148215cb6f39SMike Snitzer static enum wc_map_op writecache_map_flush(struct dm_writecache *wc, struct bio *bio)
148315cb6f39SMike Snitzer {
148415cb6f39SMike Snitzer 	if (writecache_has_error(wc))
148515cb6f39SMike Snitzer 		return WC_MAP_ERROR;
148615cb6f39SMike Snitzer 
148715cb6f39SMike Snitzer 	if (WC_MODE_PMEM(wc)) {
1488e3a35d03SMikulas Patocka 		wc->stats.flushes++;
148915cb6f39SMike Snitzer 		writecache_flush(wc);
149015cb6f39SMike Snitzer 		if (writecache_has_error(wc))
149115cb6f39SMike Snitzer 			return WC_MAP_ERROR;
149215cb6f39SMike Snitzer 		else if (unlikely(wc->cleaner) || unlikely(wc->metadata_only))
149315cb6f39SMike Snitzer 			return WC_MAP_REMAP_ORIGIN;
149415cb6f39SMike Snitzer 		return WC_MAP_SUBMIT;
149515cb6f39SMike Snitzer 	}
149615cb6f39SMike Snitzer 	/* SSD: */
149715cb6f39SMike Snitzer 	if (dm_bio_get_target_bio_nr(bio))
149815cb6f39SMike Snitzer 		return WC_MAP_REMAP_ORIGIN;
1499e3a35d03SMikulas Patocka 	wc->stats.flushes++;
150015cb6f39SMike Snitzer 	writecache_offload_bio(wc, bio);
150115cb6f39SMike Snitzer 	return WC_MAP_RETURN;
150215cb6f39SMike Snitzer }
150315cb6f39SMike Snitzer 
150415cb6f39SMike Snitzer static enum wc_map_op writecache_map_discard(struct dm_writecache *wc, struct bio *bio)
150515cb6f39SMike Snitzer {
1506e3a35d03SMikulas Patocka 	wc->stats.discards++;
1507e3a35d03SMikulas Patocka 
150815cb6f39SMike Snitzer 	if (writecache_has_error(wc))
150915cb6f39SMike Snitzer 		return WC_MAP_ERROR;
151015cb6f39SMike Snitzer 
151115cb6f39SMike Snitzer 	if (WC_MODE_PMEM(wc)) {
151215cb6f39SMike Snitzer 		writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
151315cb6f39SMike Snitzer 		return WC_MAP_REMAP_ORIGIN;
151415cb6f39SMike Snitzer 	}
151515cb6f39SMike Snitzer 	/* SSD: */
151615cb6f39SMike Snitzer 	writecache_offload_bio(wc, bio);
151715cb6f39SMike Snitzer 	return WC_MAP_RETURN;
151815cb6f39SMike Snitzer }
151915cb6f39SMike Snitzer 
1520cdd4d783SMike Snitzer static int writecache_map(struct dm_target *ti, struct bio *bio)
1521cdd4d783SMike Snitzer {
152248debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
152315cb6f39SMike Snitzer 	enum wc_map_op map_op;
152448debafeSMikulas Patocka 
152548debafeSMikulas Patocka 	bio->bi_private = NULL;
152648debafeSMikulas Patocka 
152748debafeSMikulas Patocka 	wc_lock(wc);
152848debafeSMikulas Patocka 
152948debafeSMikulas Patocka 	if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
153015cb6f39SMike Snitzer 		map_op = writecache_map_flush(wc, bio);
153115cb6f39SMike Snitzer 		goto done;
153248debafeSMikulas Patocka 	}
153348debafeSMikulas Patocka 
153448debafeSMikulas Patocka 	bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
153548debafeSMikulas Patocka 
153648debafeSMikulas Patocka 	if (unlikely((((unsigned)bio->bi_iter.bi_sector | bio_sectors(bio)) &
153748debafeSMikulas Patocka 				(wc->block_size / 512 - 1)) != 0)) {
153848debafeSMikulas Patocka 		DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
153948debafeSMikulas Patocka 		      (unsigned long long)bio->bi_iter.bi_sector,
154048debafeSMikulas Patocka 		      bio->bi_iter.bi_size, wc->block_size);
154115cb6f39SMike Snitzer 		map_op = WC_MAP_ERROR;
154215cb6f39SMike Snitzer 		goto done;
154348debafeSMikulas Patocka 	}
154448debafeSMikulas Patocka 
154548debafeSMikulas Patocka 	if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
154615cb6f39SMike Snitzer 		map_op = writecache_map_discard(wc, bio);
154715cb6f39SMike Snitzer 		goto done;
154848debafeSMikulas Patocka 	}
154948debafeSMikulas Patocka 
1550cdd4d783SMike Snitzer 	if (bio_data_dir(bio) == READ)
1551cdd4d783SMike Snitzer 		map_op = writecache_map_read(wc, bio);
155248debafeSMikulas Patocka 	else
1553cdd4d783SMike Snitzer 		map_op = writecache_map_write(wc, bio);
155415cb6f39SMike Snitzer done:
1555cdd4d783SMike Snitzer 	switch (map_op) {
1556cdd4d783SMike Snitzer 	case WC_MAP_REMAP_ORIGIN:
15575c0de3d7SMikulas Patocka 		if (likely(wc->pause != 0)) {
15585c0de3d7SMikulas Patocka 			if (bio_op(bio) == REQ_OP_WRITE) {
155995b88f4dSMikulas Patocka 				dm_iot_io_begin(&wc->iot, 1);
156095b88f4dSMikulas Patocka 				bio->bi_private = (void *)2;
156195b88f4dSMikulas Patocka 			}
15625c0de3d7SMikulas Patocka 		}
156348debafeSMikulas Patocka 		bio_set_dev(bio, wc->dev->bdev);
156448debafeSMikulas Patocka 		wc_unlock(wc);
156548debafeSMikulas Patocka 		return DM_MAPIO_REMAPPED;
156648debafeSMikulas Patocka 
1567cdd4d783SMike Snitzer 	case WC_MAP_REMAP:
156848debafeSMikulas Patocka 		/* make sure that writecache_end_io decrements bio_in_progress: */
156948debafeSMikulas Patocka 		bio->bi_private = (void *)1;
157048debafeSMikulas Patocka 		atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
157148debafeSMikulas Patocka 		wc_unlock(wc);
157248debafeSMikulas Patocka 		return DM_MAPIO_REMAPPED;
157348debafeSMikulas Patocka 
1574cdd4d783SMike Snitzer 	case WC_MAP_SUBMIT:
157548debafeSMikulas Patocka 		wc_unlock(wc);
157648debafeSMikulas Patocka 		bio_endio(bio);
157748debafeSMikulas Patocka 		return DM_MAPIO_SUBMITTED;
157848debafeSMikulas Patocka 
1579cdd4d783SMike Snitzer 	case WC_MAP_RETURN:
158048debafeSMikulas Patocka 		wc_unlock(wc);
158148debafeSMikulas Patocka 		return DM_MAPIO_SUBMITTED;
158248debafeSMikulas Patocka 
1583cdd4d783SMike Snitzer 	case WC_MAP_ERROR:
158448debafeSMikulas Patocka 		wc_unlock(wc);
158548debafeSMikulas Patocka 		bio_io_error(bio);
158648debafeSMikulas Patocka 		return DM_MAPIO_SUBMITTED;
1587df699cc1SMikulas Patocka 
1588df699cc1SMikulas Patocka 	default:
1589df699cc1SMikulas Patocka 		BUG();
1590df699cc1SMikulas Patocka 		return -1;
159148debafeSMikulas Patocka 	}
1592cdd4d783SMike Snitzer }
159348debafeSMikulas Patocka 
159448debafeSMikulas Patocka static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
159548debafeSMikulas Patocka {
159648debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
159748debafeSMikulas Patocka 
159895b88f4dSMikulas Patocka 	if (bio->bi_private == (void *)1) {
159948debafeSMikulas Patocka 		int dir = bio_data_dir(bio);
160048debafeSMikulas Patocka 		if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
160148debafeSMikulas Patocka 			if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
160248debafeSMikulas Patocka 				wake_up(&wc->bio_in_progress_wait[dir]);
160395b88f4dSMikulas Patocka 	} else if (bio->bi_private == (void *)2) {
160495b88f4dSMikulas Patocka 		dm_iot_io_end(&wc->iot, 1);
160548debafeSMikulas Patocka 	}
160648debafeSMikulas Patocka 	return 0;
160748debafeSMikulas Patocka }
160848debafeSMikulas Patocka 
160948debafeSMikulas Patocka static int writecache_iterate_devices(struct dm_target *ti,
161048debafeSMikulas Patocka 				      iterate_devices_callout_fn fn, void *data)
161148debafeSMikulas Patocka {
161248debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
161348debafeSMikulas Patocka 
161448debafeSMikulas Patocka 	return fn(ti, wc->dev, 0, ti->len, data);
161548debafeSMikulas Patocka }
161648debafeSMikulas Patocka 
161748debafeSMikulas Patocka static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
161848debafeSMikulas Patocka {
161948debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
162048debafeSMikulas Patocka 
162148debafeSMikulas Patocka 	if (limits->logical_block_size < wc->block_size)
162248debafeSMikulas Patocka 		limits->logical_block_size = wc->block_size;
162348debafeSMikulas Patocka 
162448debafeSMikulas Patocka 	if (limits->physical_block_size < wc->block_size)
162548debafeSMikulas Patocka 		limits->physical_block_size = wc->block_size;
162648debafeSMikulas Patocka 
162748debafeSMikulas Patocka 	if (limits->io_min < wc->block_size)
162848debafeSMikulas Patocka 		limits->io_min = wc->block_size;
162948debafeSMikulas Patocka }
163048debafeSMikulas Patocka 
163148debafeSMikulas Patocka 
163248debafeSMikulas Patocka static void writecache_writeback_endio(struct bio *bio)
163348debafeSMikulas Patocka {
163448debafeSMikulas Patocka 	struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
163548debafeSMikulas Patocka 	struct dm_writecache *wc = wb->wc;
163648debafeSMikulas Patocka 	unsigned long flags;
163748debafeSMikulas Patocka 
163848debafeSMikulas Patocka 	raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
163948debafeSMikulas Patocka 	if (unlikely(list_empty(&wc->endio_list)))
164048debafeSMikulas Patocka 		wake_up_process(wc->endio_thread);
164148debafeSMikulas Patocka 	list_add_tail(&wb->endio_entry, &wc->endio_list);
164248debafeSMikulas Patocka 	raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
164348debafeSMikulas Patocka }
164448debafeSMikulas Patocka 
164548debafeSMikulas Patocka static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
164648debafeSMikulas Patocka {
164748debafeSMikulas Patocka 	struct copy_struct *c = ptr;
164848debafeSMikulas Patocka 	struct dm_writecache *wc = c->wc;
164948debafeSMikulas Patocka 
165048debafeSMikulas Patocka 	c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
165148debafeSMikulas Patocka 
165248debafeSMikulas Patocka 	raw_spin_lock_irq(&wc->endio_list_lock);
165348debafeSMikulas Patocka 	if (unlikely(list_empty(&wc->endio_list)))
165448debafeSMikulas Patocka 		wake_up_process(wc->endio_thread);
165548debafeSMikulas Patocka 	list_add_tail(&c->endio_entry, &wc->endio_list);
165648debafeSMikulas Patocka 	raw_spin_unlock_irq(&wc->endio_list_lock);
165748debafeSMikulas Patocka }
165848debafeSMikulas Patocka 
165948debafeSMikulas Patocka static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
166048debafeSMikulas Patocka {
166148debafeSMikulas Patocka 	unsigned i;
166248debafeSMikulas Patocka 	struct writeback_struct *wb;
166348debafeSMikulas Patocka 	struct wc_entry *e;
166448debafeSMikulas Patocka 	unsigned long n_walked = 0;
166548debafeSMikulas Patocka 
166648debafeSMikulas Patocka 	do {
166748debafeSMikulas Patocka 		wb = list_entry(list->next, struct writeback_struct, endio_entry);
166848debafeSMikulas Patocka 		list_del(&wb->endio_entry);
166948debafeSMikulas Patocka 
167048debafeSMikulas Patocka 		if (unlikely(wb->bio.bi_status != BLK_STS_OK))
167148debafeSMikulas Patocka 			writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
167248debafeSMikulas Patocka 					"write error %d", wb->bio.bi_status);
167348debafeSMikulas Patocka 		i = 0;
167448debafeSMikulas Patocka 		do {
167548debafeSMikulas Patocka 			e = wb->wc_list[i];
167648debafeSMikulas Patocka 			BUG_ON(!e->write_in_progress);
167748debafeSMikulas Patocka 			e->write_in_progress = false;
167848debafeSMikulas Patocka 			INIT_LIST_HEAD(&e->lru);
167948debafeSMikulas Patocka 			if (!writecache_has_error(wc))
168048debafeSMikulas Patocka 				writecache_free_entry(wc, e);
168148debafeSMikulas Patocka 			BUG_ON(!wc->writeback_size);
168248debafeSMikulas Patocka 			wc->writeback_size--;
168348debafeSMikulas Patocka 			n_walked++;
168448debafeSMikulas Patocka 			if (unlikely(n_walked >= ENDIO_LATENCY)) {
1685aa950920SMikulas Patocka 				writecache_commit_flushed(wc, false);
168648debafeSMikulas Patocka 				wc_unlock(wc);
168748debafeSMikulas Patocka 				wc_lock(wc);
168848debafeSMikulas Patocka 				n_walked = 0;
168948debafeSMikulas Patocka 			}
169048debafeSMikulas Patocka 		} while (++i < wb->wc_list_n);
169148debafeSMikulas Patocka 
169248debafeSMikulas Patocka 		if (wb->wc_list != wb->wc_list_inline)
169348debafeSMikulas Patocka 			kfree(wb->wc_list);
169448debafeSMikulas Patocka 		bio_put(&wb->bio);
169548debafeSMikulas Patocka 	} while (!list_empty(list));
169648debafeSMikulas Patocka }
169748debafeSMikulas Patocka 
169848debafeSMikulas Patocka static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
169948debafeSMikulas Patocka {
170048debafeSMikulas Patocka 	struct copy_struct *c;
170148debafeSMikulas Patocka 	struct wc_entry *e;
170248debafeSMikulas Patocka 
170348debafeSMikulas Patocka 	do {
170448debafeSMikulas Patocka 		c = list_entry(list->next, struct copy_struct, endio_entry);
170548debafeSMikulas Patocka 		list_del(&c->endio_entry);
170648debafeSMikulas Patocka 
170748debafeSMikulas Patocka 		if (unlikely(c->error))
170848debafeSMikulas Patocka 			writecache_error(wc, c->error, "copy error");
170948debafeSMikulas Patocka 
171048debafeSMikulas Patocka 		e = c->e;
171148debafeSMikulas Patocka 		do {
171248debafeSMikulas Patocka 			BUG_ON(!e->write_in_progress);
171348debafeSMikulas Patocka 			e->write_in_progress = false;
171448debafeSMikulas Patocka 			INIT_LIST_HEAD(&e->lru);
171548debafeSMikulas Patocka 			if (!writecache_has_error(wc))
171648debafeSMikulas Patocka 				writecache_free_entry(wc, e);
171748debafeSMikulas Patocka 
171848debafeSMikulas Patocka 			BUG_ON(!wc->writeback_size);
171948debafeSMikulas Patocka 			wc->writeback_size--;
172048debafeSMikulas Patocka 			e++;
172148debafeSMikulas Patocka 		} while (--c->n_entries);
172248debafeSMikulas Patocka 		mempool_free(c, &wc->copy_pool);
172348debafeSMikulas Patocka 	} while (!list_empty(list));
172448debafeSMikulas Patocka }
172548debafeSMikulas Patocka 
172648debafeSMikulas Patocka static int writecache_endio_thread(void *data)
172748debafeSMikulas Patocka {
172848debafeSMikulas Patocka 	struct dm_writecache *wc = data;
172948debafeSMikulas Patocka 
173048debafeSMikulas Patocka 	while (1) {
173148debafeSMikulas Patocka 		struct list_head list;
173248debafeSMikulas Patocka 
173348debafeSMikulas Patocka 		raw_spin_lock_irq(&wc->endio_list_lock);
173448debafeSMikulas Patocka 		if (!list_empty(&wc->endio_list))
173548debafeSMikulas Patocka 			goto pop_from_list;
173648debafeSMikulas Patocka 		set_current_state(TASK_INTERRUPTIBLE);
173748debafeSMikulas Patocka 		raw_spin_unlock_irq(&wc->endio_list_lock);
173848debafeSMikulas Patocka 
173948debafeSMikulas Patocka 		if (unlikely(kthread_should_stop())) {
174048debafeSMikulas Patocka 			set_current_state(TASK_RUNNING);
174148debafeSMikulas Patocka 			break;
174248debafeSMikulas Patocka 		}
174348debafeSMikulas Patocka 
174448debafeSMikulas Patocka 		schedule();
174548debafeSMikulas Patocka 
174648debafeSMikulas Patocka 		continue;
174748debafeSMikulas Patocka 
174848debafeSMikulas Patocka pop_from_list:
174948debafeSMikulas Patocka 		list = wc->endio_list;
175048debafeSMikulas Patocka 		list.next->prev = list.prev->next = &list;
175148debafeSMikulas Patocka 		INIT_LIST_HEAD(&wc->endio_list);
175248debafeSMikulas Patocka 		raw_spin_unlock_irq(&wc->endio_list_lock);
175348debafeSMikulas Patocka 
175448debafeSMikulas Patocka 		if (!WC_MODE_FUA(wc))
175548debafeSMikulas Patocka 			writecache_disk_flush(wc, wc->dev);
175648debafeSMikulas Patocka 
175748debafeSMikulas Patocka 		wc_lock(wc);
175848debafeSMikulas Patocka 
175948debafeSMikulas Patocka 		if (WC_MODE_PMEM(wc)) {
176048debafeSMikulas Patocka 			__writecache_endio_pmem(wc, &list);
176148debafeSMikulas Patocka 		} else {
176248debafeSMikulas Patocka 			__writecache_endio_ssd(wc, &list);
176348debafeSMikulas Patocka 			writecache_wait_for_ios(wc, READ);
176448debafeSMikulas Patocka 		}
176548debafeSMikulas Patocka 
1766aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
176748debafeSMikulas Patocka 
176848debafeSMikulas Patocka 		wc_unlock(wc);
176948debafeSMikulas Patocka 	}
177048debafeSMikulas Patocka 
177148debafeSMikulas Patocka 	return 0;
177248debafeSMikulas Patocka }
177348debafeSMikulas Patocka 
1774620cbe40SMikulas Patocka static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e)
177548debafeSMikulas Patocka {
177648debafeSMikulas Patocka 	struct dm_writecache *wc = wb->wc;
177748debafeSMikulas Patocka 	unsigned block_size = wc->block_size;
177848debafeSMikulas Patocka 	void *address = memory_data(wc, e);
177948debafeSMikulas Patocka 
178048debafeSMikulas Patocka 	persistent_memory_flush_cache(address, block_size);
17814134455fSMikulas Patocka 
17824134455fSMikulas Patocka 	if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors))
17834134455fSMikulas Patocka 		return true;
17844134455fSMikulas Patocka 
178548debafeSMikulas Patocka 	return bio_add_page(&wb->bio, persistent_memory_page(address),
178648debafeSMikulas Patocka 			    block_size, persistent_memory_page_offset(address)) != 0;
178748debafeSMikulas Patocka }
178848debafeSMikulas Patocka 
178948debafeSMikulas Patocka struct writeback_list {
179048debafeSMikulas Patocka 	struct list_head list;
179148debafeSMikulas Patocka 	size_t size;
179248debafeSMikulas Patocka };
179348debafeSMikulas Patocka 
179448debafeSMikulas Patocka static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
179548debafeSMikulas Patocka {
179648debafeSMikulas Patocka 	if (unlikely(wc->max_writeback_jobs)) {
179748debafeSMikulas Patocka 		if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
179848debafeSMikulas Patocka 			wc_lock(wc);
179948debafeSMikulas Patocka 			while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
180048debafeSMikulas Patocka 				writecache_wait_on_freelist(wc);
180148debafeSMikulas Patocka 			wc_unlock(wc);
180248debafeSMikulas Patocka 		}
180348debafeSMikulas Patocka 	}
180448debafeSMikulas Patocka 	cond_resched();
180548debafeSMikulas Patocka }
180648debafeSMikulas Patocka 
180748debafeSMikulas Patocka static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
180848debafeSMikulas Patocka {
180948debafeSMikulas Patocka 	struct wc_entry *e, *f;
181048debafeSMikulas Patocka 	struct bio *bio;
181148debafeSMikulas Patocka 	struct writeback_struct *wb;
181248debafeSMikulas Patocka 	unsigned max_pages;
181348debafeSMikulas Patocka 
181448debafeSMikulas Patocka 	while (wbl->size) {
181548debafeSMikulas Patocka 		wbl->size--;
181648debafeSMikulas Patocka 		e = container_of(wbl->list.prev, struct wc_entry, lru);
181748debafeSMikulas Patocka 		list_del(&e->lru);
181848debafeSMikulas Patocka 
181948debafeSMikulas Patocka 		max_pages = e->wc_list_contiguous;
182048debafeSMikulas Patocka 
1821609be106SChristoph Hellwig 		bio = bio_alloc_bioset(wc->dev->bdev, max_pages, REQ_OP_WRITE,
1822609be106SChristoph Hellwig 				       GFP_NOIO, &wc->bio_set);
182348debafeSMikulas Patocka 		wb = container_of(bio, struct writeback_struct, bio);
182448debafeSMikulas Patocka 		wb->wc = wc;
182509f2d656SHuaisheng Ye 		bio->bi_end_io = writecache_writeback_endio;
182609f2d656SHuaisheng Ye 		bio->bi_iter.bi_sector = read_original_sector(wc, e);
182748debafeSMikulas Patocka 		if (max_pages <= WB_LIST_INLINE ||
182850a7d3baSKees Cook 		    unlikely(!(wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
182948debafeSMikulas Patocka 							   GFP_NOIO | __GFP_NORETRY |
183048debafeSMikulas Patocka 							   __GFP_NOMEMALLOC | __GFP_NOWARN)))) {
183148debafeSMikulas Patocka 			wb->wc_list = wb->wc_list_inline;
183248debafeSMikulas Patocka 			max_pages = WB_LIST_INLINE;
183348debafeSMikulas Patocka 		}
183448debafeSMikulas Patocka 
1835620cbe40SMikulas Patocka 		BUG_ON(!wc_add_block(wb, e));
183648debafeSMikulas Patocka 
183748debafeSMikulas Patocka 		wb->wc_list[0] = e;
183848debafeSMikulas Patocka 		wb->wc_list_n = 1;
183948debafeSMikulas Patocka 
184048debafeSMikulas Patocka 		while (wbl->size && wb->wc_list_n < max_pages) {
184148debafeSMikulas Patocka 			f = container_of(wbl->list.prev, struct wc_entry, lru);
184248debafeSMikulas Patocka 			if (read_original_sector(wc, f) !=
184348debafeSMikulas Patocka 			    read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
184448debafeSMikulas Patocka 				break;
1845620cbe40SMikulas Patocka 			if (!wc_add_block(wb, f))
184648debafeSMikulas Patocka 				break;
184748debafeSMikulas Patocka 			wbl->size--;
184848debafeSMikulas Patocka 			list_del(&f->lru);
184948debafeSMikulas Patocka 			wb->wc_list[wb->wc_list_n++] = f;
185048debafeSMikulas Patocka 			e = f;
185148debafeSMikulas Patocka 		}
1852609be106SChristoph Hellwig 		if (WC_MODE_FUA(wc))
1853609be106SChristoph Hellwig 			bio->bi_opf |= REQ_FUA;
185448debafeSMikulas Patocka 		if (writecache_has_error(wc)) {
185548debafeSMikulas Patocka 			bio->bi_status = BLK_STS_IOERR;
185609f2d656SHuaisheng Ye 			bio_endio(bio);
18574134455fSMikulas Patocka 		} else if (unlikely(!bio_sectors(bio))) {
18584134455fSMikulas Patocka 			bio->bi_status = BLK_STS_OK;
18594134455fSMikulas Patocka 			bio_endio(bio);
186048debafeSMikulas Patocka 		} else {
186109f2d656SHuaisheng Ye 			submit_bio(bio);
186248debafeSMikulas Patocka 		}
186348debafeSMikulas Patocka 
186448debafeSMikulas Patocka 		__writeback_throttle(wc, wbl);
186548debafeSMikulas Patocka 	}
186648debafeSMikulas Patocka }
186748debafeSMikulas Patocka 
186848debafeSMikulas Patocka static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
186948debafeSMikulas Patocka {
187048debafeSMikulas Patocka 	struct wc_entry *e, *f;
187148debafeSMikulas Patocka 	struct dm_io_region from, to;
187248debafeSMikulas Patocka 	struct copy_struct *c;
187348debafeSMikulas Patocka 
187448debafeSMikulas Patocka 	while (wbl->size) {
187548debafeSMikulas Patocka 		unsigned n_sectors;
187648debafeSMikulas Patocka 
187748debafeSMikulas Patocka 		wbl->size--;
187848debafeSMikulas Patocka 		e = container_of(wbl->list.prev, struct wc_entry, lru);
187948debafeSMikulas Patocka 		list_del(&e->lru);
188048debafeSMikulas Patocka 
188148debafeSMikulas Patocka 		n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
188248debafeSMikulas Patocka 
188348debafeSMikulas Patocka 		from.bdev = wc->ssd_dev->bdev;
188448debafeSMikulas Patocka 		from.sector = cache_sector(wc, e);
188548debafeSMikulas Patocka 		from.count = n_sectors;
188648debafeSMikulas Patocka 		to.bdev = wc->dev->bdev;
188748debafeSMikulas Patocka 		to.sector = read_original_sector(wc, e);
188848debafeSMikulas Patocka 		to.count = n_sectors;
188948debafeSMikulas Patocka 
189048debafeSMikulas Patocka 		c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
189148debafeSMikulas Patocka 		c->wc = wc;
189248debafeSMikulas Patocka 		c->e = e;
189348debafeSMikulas Patocka 		c->n_entries = e->wc_list_contiguous;
189448debafeSMikulas Patocka 
189548debafeSMikulas Patocka 		while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
189648debafeSMikulas Patocka 			wbl->size--;
189748debafeSMikulas Patocka 			f = container_of(wbl->list.prev, struct wc_entry, lru);
189848debafeSMikulas Patocka 			BUG_ON(f != e + 1);
189948debafeSMikulas Patocka 			list_del(&f->lru);
190048debafeSMikulas Patocka 			e = f;
190148debafeSMikulas Patocka 		}
190248debafeSMikulas Patocka 
19034134455fSMikulas Patocka 		if (unlikely(to.sector + to.count > wc->data_device_sectors)) {
19044134455fSMikulas Patocka 			if (to.sector >= wc->data_device_sectors) {
19054134455fSMikulas Patocka 				writecache_copy_endio(0, 0, c);
19064134455fSMikulas Patocka 				continue;
19074134455fSMikulas Patocka 			}
19084134455fSMikulas Patocka 			from.count = to.count = wc->data_device_sectors - to.sector;
19094134455fSMikulas Patocka 		}
19104134455fSMikulas Patocka 
191148debafeSMikulas Patocka 		dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
191248debafeSMikulas Patocka 
191348debafeSMikulas Patocka 		__writeback_throttle(wc, wbl);
191448debafeSMikulas Patocka 	}
191548debafeSMikulas Patocka }
191648debafeSMikulas Patocka 
191748debafeSMikulas Patocka static void writecache_writeback(struct work_struct *work)
191848debafeSMikulas Patocka {
191948debafeSMikulas Patocka 	struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
192048debafeSMikulas Patocka 	struct blk_plug plug;
19213f649ab7SKees Cook 	struct wc_entry *f, *g, *e = NULL;
192248debafeSMikulas Patocka 	struct rb_node *node, *next_node;
192348debafeSMikulas Patocka 	struct list_head skipped;
192448debafeSMikulas Patocka 	struct writeback_list wbl;
192548debafeSMikulas Patocka 	unsigned long n_walked;
192648debafeSMikulas Patocka 
1927293128b1SMikulas Patocka 	if (!WC_MODE_PMEM(wc)) {
1928293128b1SMikulas Patocka 		/* Wait for any active kcopyd work on behalf of ssd writeback */
1929293128b1SMikulas Patocka 		dm_kcopyd_client_flush(wc->dm_kcopyd);
1930293128b1SMikulas Patocka 	}
1931293128b1SMikulas Patocka 
19325c0de3d7SMikulas Patocka 	if (likely(wc->pause != 0)) {
19335c0de3d7SMikulas Patocka 		while (1) {
19345c0de3d7SMikulas Patocka 			unsigned long idle;
19355c0de3d7SMikulas Patocka 			if (unlikely(wc->cleaner) || unlikely(wc->writeback_all) ||
19365c0de3d7SMikulas Patocka 			    unlikely(dm_suspended(wc->ti)))
19375c0de3d7SMikulas Patocka 				break;
19385c0de3d7SMikulas Patocka 			idle = dm_iot_idle_time(&wc->iot);
19395c0de3d7SMikulas Patocka 			if (idle >= wc->pause)
19405c0de3d7SMikulas Patocka 				break;
19415c0de3d7SMikulas Patocka 			idle = wc->pause - idle;
19425c0de3d7SMikulas Patocka 			if (idle > HZ)
19435c0de3d7SMikulas Patocka 				idle = HZ;
19445c0de3d7SMikulas Patocka 			schedule_timeout_idle(idle);
194595b88f4dSMikulas Patocka 		}
194695b88f4dSMikulas Patocka 	}
194795b88f4dSMikulas Patocka 
194848debafeSMikulas Patocka 	wc_lock(wc);
194948debafeSMikulas Patocka restart:
195048debafeSMikulas Patocka 	if (writecache_has_error(wc)) {
195148debafeSMikulas Patocka 		wc_unlock(wc);
195248debafeSMikulas Patocka 		return;
195348debafeSMikulas Patocka 	}
195448debafeSMikulas Patocka 
195548debafeSMikulas Patocka 	if (unlikely(wc->writeback_all)) {
195648debafeSMikulas Patocka 		if (writecache_wait_for_writeback(wc))
195748debafeSMikulas Patocka 			goto restart;
195848debafeSMikulas Patocka 	}
195948debafeSMikulas Patocka 
196048debafeSMikulas Patocka 	if (wc->overwrote_committed) {
196148debafeSMikulas Patocka 		writecache_wait_for_ios(wc, WRITE);
196248debafeSMikulas Patocka 	}
196348debafeSMikulas Patocka 
196448debafeSMikulas Patocka 	n_walked = 0;
196548debafeSMikulas Patocka 	INIT_LIST_HEAD(&skipped);
196648debafeSMikulas Patocka 	INIT_LIST_HEAD(&wbl.list);
196748debafeSMikulas Patocka 	wbl.size = 0;
196848debafeSMikulas Patocka 	while (!list_empty(&wc->lru) &&
196948debafeSMikulas Patocka 	       (wc->writeback_all ||
19703923d485SMikulas Patocka 		wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark ||
19713923d485SMikulas Patocka 		(jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >=
19723923d485SMikulas Patocka 		 wc->max_age - wc->max_age / MAX_AGE_DIV))) {
197348debafeSMikulas Patocka 
197448debafeSMikulas Patocka 		n_walked++;
197548debafeSMikulas Patocka 		if (unlikely(n_walked > WRITEBACK_LATENCY) &&
1976af4f6cabSMikulas Patocka 		    likely(!wc->writeback_all)) {
1977af4f6cabSMikulas Patocka 			if (likely(!dm_suspended(wc->ti)))
197848debafeSMikulas Patocka 				queue_work(wc->writeback_wq, &wc->writeback_work);
197948debafeSMikulas Patocka 			break;
198048debafeSMikulas Patocka 		}
198148debafeSMikulas Patocka 
19825229b489SHuaisheng Ye 		if (unlikely(wc->writeback_all)) {
19835229b489SHuaisheng Ye 			if (unlikely(!e)) {
19845229b489SHuaisheng Ye 				writecache_flush(wc);
19855229b489SHuaisheng Ye 				e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
19865229b489SHuaisheng Ye 			} else
19875229b489SHuaisheng Ye 				e = g;
19885229b489SHuaisheng Ye 		} else
198948debafeSMikulas Patocka 			e = container_of(wc->lru.prev, struct wc_entry, lru);
199048debafeSMikulas Patocka 		BUG_ON(e->write_in_progress);
199148debafeSMikulas Patocka 		if (unlikely(!writecache_entry_is_committed(wc, e))) {
199248debafeSMikulas Patocka 			writecache_flush(wc);
199348debafeSMikulas Patocka 		}
199448debafeSMikulas Patocka 		node = rb_prev(&e->rb_node);
199548debafeSMikulas Patocka 		if (node) {
199648debafeSMikulas Patocka 			f = container_of(node, struct wc_entry, rb_node);
199748debafeSMikulas Patocka 			if (unlikely(read_original_sector(wc, f) ==
199848debafeSMikulas Patocka 				     read_original_sector(wc, e))) {
199948debafeSMikulas Patocka 				BUG_ON(!f->write_in_progress);
20008c77f1cbSBaokun Li 				list_move(&e->lru, &skipped);
200148debafeSMikulas Patocka 				cond_resched();
200248debafeSMikulas Patocka 				continue;
200348debafeSMikulas Patocka 			}
200448debafeSMikulas Patocka 		}
200548debafeSMikulas Patocka 		wc->writeback_size++;
20068c77f1cbSBaokun Li 		list_move(&e->lru, &wbl.list);
200748debafeSMikulas Patocka 		wbl.size++;
200848debafeSMikulas Patocka 		e->write_in_progress = true;
200948debafeSMikulas Patocka 		e->wc_list_contiguous = 1;
201048debafeSMikulas Patocka 
201148debafeSMikulas Patocka 		f = e;
201248debafeSMikulas Patocka 
201348debafeSMikulas Patocka 		while (1) {
201448debafeSMikulas Patocka 			next_node = rb_next(&f->rb_node);
201548debafeSMikulas Patocka 			if (unlikely(!next_node))
201648debafeSMikulas Patocka 				break;
201748debafeSMikulas Patocka 			g = container_of(next_node, struct wc_entry, rb_node);
201862421b38SHuaisheng Ye 			if (unlikely(read_original_sector(wc, g) ==
201962421b38SHuaisheng Ye 			    read_original_sector(wc, f))) {
202048debafeSMikulas Patocka 				f = g;
202148debafeSMikulas Patocka 				continue;
202248debafeSMikulas Patocka 			}
202348debafeSMikulas Patocka 			if (read_original_sector(wc, g) !=
202448debafeSMikulas Patocka 			    read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
202548debafeSMikulas Patocka 				break;
202648debafeSMikulas Patocka 			if (unlikely(g->write_in_progress))
202748debafeSMikulas Patocka 				break;
202848debafeSMikulas Patocka 			if (unlikely(!writecache_entry_is_committed(wc, g)))
202948debafeSMikulas Patocka 				break;
203048debafeSMikulas Patocka 
203148debafeSMikulas Patocka 			if (!WC_MODE_PMEM(wc)) {
203248debafeSMikulas Patocka 				if (g != f + 1)
203348debafeSMikulas Patocka 					break;
203448debafeSMikulas Patocka 			}
203548debafeSMikulas Patocka 
203648debafeSMikulas Patocka 			n_walked++;
203748debafeSMikulas Patocka 			//if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
203848debafeSMikulas Patocka 			//	break;
203948debafeSMikulas Patocka 
204048debafeSMikulas Patocka 			wc->writeback_size++;
20418c77f1cbSBaokun Li 			list_move(&g->lru, &wbl.list);
204248debafeSMikulas Patocka 			wbl.size++;
204348debafeSMikulas Patocka 			g->write_in_progress = true;
2044a8affc03SChristoph Hellwig 			g->wc_list_contiguous = BIO_MAX_VECS;
204548debafeSMikulas Patocka 			f = g;
204648debafeSMikulas Patocka 			e->wc_list_contiguous++;
2047a8affc03SChristoph Hellwig 			if (unlikely(e->wc_list_contiguous == BIO_MAX_VECS)) {
20485229b489SHuaisheng Ye 				if (unlikely(wc->writeback_all)) {
20495229b489SHuaisheng Ye 					next_node = rb_next(&f->rb_node);
20505229b489SHuaisheng Ye 					if (likely(next_node))
20515229b489SHuaisheng Ye 						g = container_of(next_node, struct wc_entry, rb_node);
20525229b489SHuaisheng Ye 				}
205348debafeSMikulas Patocka 				break;
205448debafeSMikulas Patocka 			}
20555229b489SHuaisheng Ye 		}
205648debafeSMikulas Patocka 		cond_resched();
205748debafeSMikulas Patocka 	}
205848debafeSMikulas Patocka 
205948debafeSMikulas Patocka 	if (!list_empty(&skipped)) {
206048debafeSMikulas Patocka 		list_splice_tail(&skipped, &wc->lru);
206148debafeSMikulas Patocka 		/*
206248debafeSMikulas Patocka 		 * If we didn't do any progress, we must wait until some
206348debafeSMikulas Patocka 		 * writeback finishes to avoid burning CPU in a loop
206448debafeSMikulas Patocka 		 */
206548debafeSMikulas Patocka 		if (unlikely(!wbl.size))
206648debafeSMikulas Patocka 			writecache_wait_for_writeback(wc);
206748debafeSMikulas Patocka 	}
206848debafeSMikulas Patocka 
206948debafeSMikulas Patocka 	wc_unlock(wc);
207048debafeSMikulas Patocka 
207148debafeSMikulas Patocka 	blk_start_plug(&plug);
207248debafeSMikulas Patocka 
207348debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
207448debafeSMikulas Patocka 		__writecache_writeback_pmem(wc, &wbl);
207548debafeSMikulas Patocka 	else
207648debafeSMikulas Patocka 		__writecache_writeback_ssd(wc, &wbl);
207748debafeSMikulas Patocka 
207848debafeSMikulas Patocka 	blk_finish_plug(&plug);
207948debafeSMikulas Patocka 
208048debafeSMikulas Patocka 	if (unlikely(wc->writeback_all)) {
208148debafeSMikulas Patocka 		wc_lock(wc);
208248debafeSMikulas Patocka 		while (writecache_wait_for_writeback(wc));
208348debafeSMikulas Patocka 		wc_unlock(wc);
208448debafeSMikulas Patocka 	}
208548debafeSMikulas Patocka }
208648debafeSMikulas Patocka 
208748debafeSMikulas Patocka static int calculate_memory_size(uint64_t device_size, unsigned block_size,
208848debafeSMikulas Patocka 				 size_t *n_blocks_p, size_t *n_metadata_blocks_p)
208948debafeSMikulas Patocka {
209048debafeSMikulas Patocka 	uint64_t n_blocks, offset;
209148debafeSMikulas Patocka 	struct wc_entry e;
209248debafeSMikulas Patocka 
209348debafeSMikulas Patocka 	n_blocks = device_size;
209448debafeSMikulas Patocka 	do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
209548debafeSMikulas Patocka 
209648debafeSMikulas Patocka 	while (1) {
209748debafeSMikulas Patocka 		if (!n_blocks)
209848debafeSMikulas Patocka 			return -ENOSPC;
209948debafeSMikulas Patocka 		/* Verify the following entries[n_blocks] won't overflow */
210048debafeSMikulas Patocka 		if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
210148debafeSMikulas Patocka 				 sizeof(struct wc_memory_entry)))
210248debafeSMikulas Patocka 			return -EFBIG;
210348debafeSMikulas Patocka 		offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
210448debafeSMikulas Patocka 		offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
210548debafeSMikulas Patocka 		if (offset + n_blocks * block_size <= device_size)
210648debafeSMikulas Patocka 			break;
210748debafeSMikulas Patocka 		n_blocks--;
210848debafeSMikulas Patocka 	}
210948debafeSMikulas Patocka 
211048debafeSMikulas Patocka 	/* check if the bit field overflows */
211148debafeSMikulas Patocka 	e.index = n_blocks;
211248debafeSMikulas Patocka 	if (e.index != n_blocks)
211348debafeSMikulas Patocka 		return -EFBIG;
211448debafeSMikulas Patocka 
211548debafeSMikulas Patocka 	if (n_blocks_p)
211648debafeSMikulas Patocka 		*n_blocks_p = n_blocks;
211748debafeSMikulas Patocka 	if (n_metadata_blocks_p)
211848debafeSMikulas Patocka 		*n_metadata_blocks_p = offset >> __ffs(block_size);
211948debafeSMikulas Patocka 	return 0;
212048debafeSMikulas Patocka }
212148debafeSMikulas Patocka 
212248debafeSMikulas Patocka static int init_memory(struct dm_writecache *wc)
212348debafeSMikulas Patocka {
212448debafeSMikulas Patocka 	size_t b;
212548debafeSMikulas Patocka 	int r;
212648debafeSMikulas Patocka 
212748debafeSMikulas Patocka 	r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
212848debafeSMikulas Patocka 	if (r)
212948debafeSMikulas Patocka 		return r;
213048debafeSMikulas Patocka 
213148debafeSMikulas Patocka 	r = writecache_alloc_entries(wc);
213248debafeSMikulas Patocka 	if (r)
213348debafeSMikulas Patocka 		return r;
213448debafeSMikulas Patocka 
213548debafeSMikulas Patocka 	for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
213648debafeSMikulas Patocka 		pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
213748debafeSMikulas Patocka 	pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
213848debafeSMikulas Patocka 	pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
213948debafeSMikulas Patocka 	pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
214048debafeSMikulas Patocka 	pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
214148debafeSMikulas Patocka 
21421edaa447SMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
214348debafeSMikulas Patocka 		write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
21441edaa447SMikulas Patocka 		cond_resched();
21451edaa447SMikulas Patocka 	}
214648debafeSMikulas Patocka 
214748debafeSMikulas Patocka 	writecache_flush_all_metadata(wc);
2148aa950920SMikulas Patocka 	writecache_commit_flushed(wc, false);
214948debafeSMikulas Patocka 	pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
215048debafeSMikulas Patocka 	writecache_flush_region(wc, &sb(wc)->magic, sizeof sb(wc)->magic);
2151aa950920SMikulas Patocka 	writecache_commit_flushed(wc, false);
215248debafeSMikulas Patocka 
215348debafeSMikulas Patocka 	return 0;
215448debafeSMikulas Patocka }
215548debafeSMikulas Patocka 
215648debafeSMikulas Patocka static void writecache_dtr(struct dm_target *ti)
215748debafeSMikulas Patocka {
215848debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
215948debafeSMikulas Patocka 
216048debafeSMikulas Patocka 	if (!wc)
216148debafeSMikulas Patocka 		return;
216248debafeSMikulas Patocka 
216348debafeSMikulas Patocka 	if (wc->endio_thread)
216448debafeSMikulas Patocka 		kthread_stop(wc->endio_thread);
216548debafeSMikulas Patocka 
216648debafeSMikulas Patocka 	if (wc->flush_thread)
216748debafeSMikulas Patocka 		kthread_stop(wc->flush_thread);
216848debafeSMikulas Patocka 
216948debafeSMikulas Patocka 	bioset_exit(&wc->bio_set);
217048debafeSMikulas Patocka 
217148debafeSMikulas Patocka 	mempool_exit(&wc->copy_pool);
217248debafeSMikulas Patocka 
217348debafeSMikulas Patocka 	if (wc->writeback_wq)
217448debafeSMikulas Patocka 		destroy_workqueue(wc->writeback_wq);
217548debafeSMikulas Patocka 
217648debafeSMikulas Patocka 	if (wc->dev)
217748debafeSMikulas Patocka 		dm_put_device(ti, wc->dev);
217848debafeSMikulas Patocka 
217948debafeSMikulas Patocka 	if (wc->ssd_dev)
218048debafeSMikulas Patocka 		dm_put_device(ti, wc->ssd_dev);
218148debafeSMikulas Patocka 
218248debafeSMikulas Patocka 	vfree(wc->entries);
218348debafeSMikulas Patocka 
218448debafeSMikulas Patocka 	if (wc->memory_map) {
218548debafeSMikulas Patocka 		if (WC_MODE_PMEM(wc))
218648debafeSMikulas Patocka 			persistent_memory_release(wc);
218748debafeSMikulas Patocka 		else
218848debafeSMikulas Patocka 			vfree(wc->memory_map);
218948debafeSMikulas Patocka 	}
219048debafeSMikulas Patocka 
219148debafeSMikulas Patocka 	if (wc->dm_kcopyd)
219248debafeSMikulas Patocka 		dm_kcopyd_client_destroy(wc->dm_kcopyd);
219348debafeSMikulas Patocka 
219448debafeSMikulas Patocka 	if (wc->dm_io)
219548debafeSMikulas Patocka 		dm_io_client_destroy(wc->dm_io);
219648debafeSMikulas Patocka 
219748debafeSMikulas Patocka 	vfree(wc->dirty_bitmap);
219848debafeSMikulas Patocka 
219948debafeSMikulas Patocka 	kfree(wc);
220048debafeSMikulas Patocka }
220148debafeSMikulas Patocka 
220248debafeSMikulas Patocka static int writecache_ctr(struct dm_target *ti, unsigned argc, char **argv)
220348debafeSMikulas Patocka {
220448debafeSMikulas Patocka 	struct dm_writecache *wc;
220548debafeSMikulas Patocka 	struct dm_arg_set as;
220648debafeSMikulas Patocka 	const char *string;
220748debafeSMikulas Patocka 	unsigned opt_params;
220848debafeSMikulas Patocka 	size_t offset, data_size;
220948debafeSMikulas Patocka 	int i, r;
221048debafeSMikulas Patocka 	char dummy;
221148debafeSMikulas Patocka 	int high_wm_percent = HIGH_WATERMARK;
221248debafeSMikulas Patocka 	int low_wm_percent = LOW_WATERMARK;
221348debafeSMikulas Patocka 	uint64_t x;
221448debafeSMikulas Patocka 	struct wc_memory_superblock s;
221548debafeSMikulas Patocka 
221648debafeSMikulas Patocka 	static struct dm_arg _args[] = {
22175c0de3d7SMikulas Patocka 		{0, 18, "Invalid number of feature args"},
221848debafeSMikulas Patocka 	};
221948debafeSMikulas Patocka 
222048debafeSMikulas Patocka 	as.argc = argc;
222148debafeSMikulas Patocka 	as.argv = argv;
222248debafeSMikulas Patocka 
222348debafeSMikulas Patocka 	wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
222448debafeSMikulas Patocka 	if (!wc) {
222548debafeSMikulas Patocka 		ti->error = "Cannot allocate writecache structure";
222648debafeSMikulas Patocka 		r = -ENOMEM;
222748debafeSMikulas Patocka 		goto bad;
222848debafeSMikulas Patocka 	}
222948debafeSMikulas Patocka 	ti->private = wc;
223048debafeSMikulas Patocka 	wc->ti = ti;
223148debafeSMikulas Patocka 
223248debafeSMikulas Patocka 	mutex_init(&wc->lock);
22333923d485SMikulas Patocka 	wc->max_age = MAX_AGE_UNSPECIFIED;
223448debafeSMikulas Patocka 	writecache_poison_lists(wc);
223548debafeSMikulas Patocka 	init_waitqueue_head(&wc->freelist_wait);
223648debafeSMikulas Patocka 	timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
22373923d485SMikulas Patocka 	timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0);
223848debafeSMikulas Patocka 
223948debafeSMikulas Patocka 	for (i = 0; i < 2; i++) {
224048debafeSMikulas Patocka 		atomic_set(&wc->bio_in_progress[i], 0);
224148debafeSMikulas Patocka 		init_waitqueue_head(&wc->bio_in_progress_wait[i]);
224248debafeSMikulas Patocka 	}
224348debafeSMikulas Patocka 
224448debafeSMikulas Patocka 	wc->dm_io = dm_io_client_create();
224548debafeSMikulas Patocka 	if (IS_ERR(wc->dm_io)) {
224648debafeSMikulas Patocka 		r = PTR_ERR(wc->dm_io);
224748debafeSMikulas Patocka 		ti->error = "Unable to allocate dm-io client";
224848debafeSMikulas Patocka 		wc->dm_io = NULL;
224948debafeSMikulas Patocka 		goto bad;
225048debafeSMikulas Patocka 	}
225148debafeSMikulas Patocka 
2252f87e033bSHuaisheng Ye 	wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
225348debafeSMikulas Patocka 	if (!wc->writeback_wq) {
225448debafeSMikulas Patocka 		r = -ENOMEM;
225548debafeSMikulas Patocka 		ti->error = "Could not allocate writeback workqueue";
225648debafeSMikulas Patocka 		goto bad;
225748debafeSMikulas Patocka 	}
225848debafeSMikulas Patocka 	INIT_WORK(&wc->writeback_work, writecache_writeback);
225948debafeSMikulas Patocka 	INIT_WORK(&wc->flush_work, writecache_flush_work);
226048debafeSMikulas Patocka 
226195b88f4dSMikulas Patocka 	dm_iot_init(&wc->iot);
226295b88f4dSMikulas Patocka 
226348debafeSMikulas Patocka 	raw_spin_lock_init(&wc->endio_list_lock);
226448debafeSMikulas Patocka 	INIT_LIST_HEAD(&wc->endio_list);
2265f635237aSCai Huoqing 	wc->endio_thread = kthread_run(writecache_endio_thread, wc, "writecache_endio");
226648debafeSMikulas Patocka 	if (IS_ERR(wc->endio_thread)) {
226748debafeSMikulas Patocka 		r = PTR_ERR(wc->endio_thread);
226848debafeSMikulas Patocka 		wc->endio_thread = NULL;
226948debafeSMikulas Patocka 		ti->error = "Couldn't spawn endio thread";
227048debafeSMikulas Patocka 		goto bad;
227148debafeSMikulas Patocka 	}
227248debafeSMikulas Patocka 
227348debafeSMikulas Patocka 	/*
227448debafeSMikulas Patocka 	 * Parse the mode (pmem or ssd)
227548debafeSMikulas Patocka 	 */
227648debafeSMikulas Patocka 	string = dm_shift_arg(&as);
227748debafeSMikulas Patocka 	if (!string)
227848debafeSMikulas Patocka 		goto bad_arguments;
227948debafeSMikulas Patocka 
228048debafeSMikulas Patocka 	if (!strcasecmp(string, "s")) {
228148debafeSMikulas Patocka 		wc->pmem_mode = false;
228248debafeSMikulas Patocka 	} else if (!strcasecmp(string, "p")) {
228348debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM
228448debafeSMikulas Patocka 		wc->pmem_mode = true;
228548debafeSMikulas Patocka 		wc->writeback_fua = true;
228648debafeSMikulas Patocka #else
228748debafeSMikulas Patocka 		/*
228848debafeSMikulas Patocka 		 * If the architecture doesn't support persistent memory or
228948debafeSMikulas Patocka 		 * the kernel doesn't support any DAX drivers, this driver can
229048debafeSMikulas Patocka 		 * only be used in SSD-only mode.
229148debafeSMikulas Patocka 		 */
229248debafeSMikulas Patocka 		r = -EOPNOTSUPP;
229348debafeSMikulas Patocka 		ti->error = "Persistent memory or DAX not supported on this system";
229448debafeSMikulas Patocka 		goto bad;
229548debafeSMikulas Patocka #endif
229648debafeSMikulas Patocka 	} else {
229748debafeSMikulas Patocka 		goto bad_arguments;
229848debafeSMikulas Patocka 	}
229948debafeSMikulas Patocka 
230048debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc)) {
230148debafeSMikulas Patocka 		r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
230248debafeSMikulas Patocka 				offsetof(struct writeback_struct, bio),
230348debafeSMikulas Patocka 				BIOSET_NEED_BVECS);
230448debafeSMikulas Patocka 		if (r) {
230548debafeSMikulas Patocka 			ti->error = "Could not allocate bio set";
230648debafeSMikulas Patocka 			goto bad;
230748debafeSMikulas Patocka 		}
230848debafeSMikulas Patocka 	} else {
23095c0de3d7SMikulas Patocka 		wc->pause = PAUSE_WRITEBACK;
231048debafeSMikulas Patocka 		r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
231148debafeSMikulas Patocka 		if (r) {
231248debafeSMikulas Patocka 			ti->error = "Could not allocate mempool";
231348debafeSMikulas Patocka 			goto bad;
231448debafeSMikulas Patocka 		}
231548debafeSMikulas Patocka 	}
231648debafeSMikulas Patocka 
231748debafeSMikulas Patocka 	/*
231848debafeSMikulas Patocka 	 * Parse the origin data device
231948debafeSMikulas Patocka 	 */
232048debafeSMikulas Patocka 	string = dm_shift_arg(&as);
232148debafeSMikulas Patocka 	if (!string)
232248debafeSMikulas Patocka 		goto bad_arguments;
232348debafeSMikulas Patocka 	r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
232448debafeSMikulas Patocka 	if (r) {
232548debafeSMikulas Patocka 		ti->error = "Origin data device lookup failed";
232648debafeSMikulas Patocka 		goto bad;
232748debafeSMikulas Patocka 	}
232848debafeSMikulas Patocka 
232948debafeSMikulas Patocka 	/*
233048debafeSMikulas Patocka 	 * Parse cache data device (be it pmem or ssd)
233148debafeSMikulas Patocka 	 */
233248debafeSMikulas Patocka 	string = dm_shift_arg(&as);
233348debafeSMikulas Patocka 	if (!string)
233448debafeSMikulas Patocka 		goto bad_arguments;
233548debafeSMikulas Patocka 
233648debafeSMikulas Patocka 	r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
233748debafeSMikulas Patocka 	if (r) {
233848debafeSMikulas Patocka 		ti->error = "Cache data device lookup failed";
233948debafeSMikulas Patocka 		goto bad;
234048debafeSMikulas Patocka 	}
23416dcbb52cSChristoph Hellwig 	wc->memory_map_size = bdev_nr_bytes(wc->ssd_dev->bdev);
234248debafeSMikulas Patocka 
234348debafeSMikulas Patocka 	/*
234448debafeSMikulas Patocka 	 * Parse the cache block size
234548debafeSMikulas Patocka 	 */
234648debafeSMikulas Patocka 	string = dm_shift_arg(&as);
234748debafeSMikulas Patocka 	if (!string)
234848debafeSMikulas Patocka 		goto bad_arguments;
234948debafeSMikulas Patocka 	if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
235048debafeSMikulas Patocka 	    wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
235148debafeSMikulas Patocka 	    (wc->block_size & (wc->block_size - 1))) {
235248debafeSMikulas Patocka 		r = -EINVAL;
235348debafeSMikulas Patocka 		ti->error = "Invalid block size";
235448debafeSMikulas Patocka 		goto bad;
235548debafeSMikulas Patocka 	}
235631b22120SMikulas Patocka 	if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) ||
235731b22120SMikulas Patocka 	    wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) {
235831b22120SMikulas Patocka 		r = -EINVAL;
235931b22120SMikulas Patocka 		ti->error = "Block size is smaller than device logical block size";
236031b22120SMikulas Patocka 		goto bad;
236131b22120SMikulas Patocka 	}
236248debafeSMikulas Patocka 	wc->block_size_bits = __ffs(wc->block_size);
236348debafeSMikulas Patocka 
236448debafeSMikulas Patocka 	wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
236548debafeSMikulas Patocka 	wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
236648debafeSMikulas Patocka 	wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
236748debafeSMikulas Patocka 
236848debafeSMikulas Patocka 	/*
236948debafeSMikulas Patocka 	 * Parse optional arguments
237048debafeSMikulas Patocka 	 */
237148debafeSMikulas Patocka 	r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
237248debafeSMikulas Patocka 	if (r)
237348debafeSMikulas Patocka 		goto bad;
237448debafeSMikulas Patocka 
237548debafeSMikulas Patocka 	while (opt_params) {
237648debafeSMikulas Patocka 		string = dm_shift_arg(&as), opt_params--;
2377d284f824SMikulas Patocka 		if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2378d284f824SMikulas Patocka 			unsigned long long start_sector;
2379d284f824SMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
2380d284f824SMikulas Patocka 			if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2381d284f824SMikulas Patocka 				goto invalid_optional;
2382d284f824SMikulas Patocka 			wc->start_sector = start_sector;
2383054bee16SMikulas Patocka 			wc->start_sector_set = true;
2384d284f824SMikulas Patocka 			if (wc->start_sector != start_sector ||
2385d284f824SMikulas Patocka 			    wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2386d284f824SMikulas Patocka 				goto invalid_optional;
2387d284f824SMikulas Patocka 		} else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
238848debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
238948debafeSMikulas Patocka 			if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
239048debafeSMikulas Patocka 				goto invalid_optional;
239148debafeSMikulas Patocka 			if (high_wm_percent < 0 || high_wm_percent > 100)
239248debafeSMikulas Patocka 				goto invalid_optional;
2393054bee16SMikulas Patocka 			wc->high_wm_percent_value = high_wm_percent;
239448debafeSMikulas Patocka 			wc->high_wm_percent_set = true;
239548debafeSMikulas Patocka 		} else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
239648debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
239748debafeSMikulas Patocka 			if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
239848debafeSMikulas Patocka 				goto invalid_optional;
239948debafeSMikulas Patocka 			if (low_wm_percent < 0 || low_wm_percent > 100)
240048debafeSMikulas Patocka 				goto invalid_optional;
2401054bee16SMikulas Patocka 			wc->low_wm_percent_value = low_wm_percent;
240248debafeSMikulas Patocka 			wc->low_wm_percent_set = true;
240348debafeSMikulas Patocka 		} else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
240448debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
240548debafeSMikulas Patocka 			if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
240648debafeSMikulas Patocka 				goto invalid_optional;
240748debafeSMikulas Patocka 			wc->max_writeback_jobs_set = true;
240848debafeSMikulas Patocka 		} else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
240948debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
241048debafeSMikulas Patocka 			if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
241148debafeSMikulas Patocka 				goto invalid_optional;
241248debafeSMikulas Patocka 			wc->autocommit_blocks_set = true;
241348debafeSMikulas Patocka 		} else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
241448debafeSMikulas Patocka 			unsigned autocommit_msecs;
241548debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
241648debafeSMikulas Patocka 			if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
241748debafeSMikulas Patocka 				goto invalid_optional;
241848debafeSMikulas Patocka 			if (autocommit_msecs > 3600000)
241948debafeSMikulas Patocka 				goto invalid_optional;
242048debafeSMikulas Patocka 			wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2421054bee16SMikulas Patocka 			wc->autocommit_time_value = autocommit_msecs;
242248debafeSMikulas Patocka 			wc->autocommit_time_set = true;
24233923d485SMikulas Patocka 		} else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
24243923d485SMikulas Patocka 			unsigned max_age_msecs;
24253923d485SMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
24263923d485SMikulas Patocka 			if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
24273923d485SMikulas Patocka 				goto invalid_optional;
24283923d485SMikulas Patocka 			if (max_age_msecs > 86400000)
24293923d485SMikulas Patocka 				goto invalid_optional;
24303923d485SMikulas Patocka 			wc->max_age = msecs_to_jiffies(max_age_msecs);
2431054bee16SMikulas Patocka 			wc->max_age_set = true;
2432054bee16SMikulas Patocka 			wc->max_age_value = max_age_msecs;
243393de44ebSMikulas Patocka 		} else if (!strcasecmp(string, "cleaner")) {
2434054bee16SMikulas Patocka 			wc->cleaner_set = true;
243593de44ebSMikulas Patocka 			wc->cleaner = true;
243648debafeSMikulas Patocka 		} else if (!strcasecmp(string, "fua")) {
243748debafeSMikulas Patocka 			if (WC_MODE_PMEM(wc)) {
243848debafeSMikulas Patocka 				wc->writeback_fua = true;
243948debafeSMikulas Patocka 				wc->writeback_fua_set = true;
244048debafeSMikulas Patocka 			} else goto invalid_optional;
244148debafeSMikulas Patocka 		} else if (!strcasecmp(string, "nofua")) {
244248debafeSMikulas Patocka 			if (WC_MODE_PMEM(wc)) {
244348debafeSMikulas Patocka 				wc->writeback_fua = false;
244448debafeSMikulas Patocka 				wc->writeback_fua_set = true;
244548debafeSMikulas Patocka 			} else goto invalid_optional;
2446611c3e16SMikulas Patocka 		} else if (!strcasecmp(string, "metadata_only")) {
2447611c3e16SMikulas Patocka 			wc->metadata_only = true;
24485c0de3d7SMikulas Patocka 		} else if (!strcasecmp(string, "pause_writeback") && opt_params >= 1) {
24495c0de3d7SMikulas Patocka 			unsigned pause_msecs;
24505c0de3d7SMikulas Patocka 			if (WC_MODE_PMEM(wc))
24515c0de3d7SMikulas Patocka 				goto invalid_optional;
24525c0de3d7SMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
24535c0de3d7SMikulas Patocka 			if (sscanf(string, "%u%c", &pause_msecs, &dummy) != 1)
24545c0de3d7SMikulas Patocka 				goto invalid_optional;
24555c0de3d7SMikulas Patocka 			if (pause_msecs > 60000)
24565c0de3d7SMikulas Patocka 				goto invalid_optional;
24575c0de3d7SMikulas Patocka 			wc->pause = msecs_to_jiffies(pause_msecs);
24585c0de3d7SMikulas Patocka 			wc->pause_set = true;
24595c0de3d7SMikulas Patocka 			wc->pause_value = pause_msecs;
246048debafeSMikulas Patocka 		} else {
246148debafeSMikulas Patocka invalid_optional:
246248debafeSMikulas Patocka 			r = -EINVAL;
246348debafeSMikulas Patocka 			ti->error = "Invalid optional argument";
246448debafeSMikulas Patocka 			goto bad;
246548debafeSMikulas Patocka 		}
246648debafeSMikulas Patocka 	}
246748debafeSMikulas Patocka 
246848debafeSMikulas Patocka 	if (high_wm_percent < low_wm_percent) {
246948debafeSMikulas Patocka 		r = -EINVAL;
247048debafeSMikulas Patocka 		ti->error = "High watermark must be greater than or equal to low watermark";
247148debafeSMikulas Patocka 		goto bad;
247248debafeSMikulas Patocka 	}
247348debafeSMikulas Patocka 
2474d284f824SMikulas Patocka 	if (WC_MODE_PMEM(wc)) {
2475a4662458SMichal Suchanek 		if (!dax_synchronous(wc->ssd_dev->dax_dev)) {
2476a4662458SMichal Suchanek 			r = -EOPNOTSUPP;
2477a4662458SMichal Suchanek 			ti->error = "Asynchronous persistent memory not supported as pmem cache";
2478a4662458SMichal Suchanek 			goto bad;
2479a4662458SMichal Suchanek 		}
2480a4662458SMichal Suchanek 
2481d284f824SMikulas Patocka 		r = persistent_memory_claim(wc);
2482d284f824SMikulas Patocka 		if (r) {
2483d284f824SMikulas Patocka 			ti->error = "Unable to map persistent memory for cache";
2484d284f824SMikulas Patocka 			goto bad;
2485d284f824SMikulas Patocka 		}
2486d284f824SMikulas Patocka 	} else {
248748debafeSMikulas Patocka 		size_t n_blocks, n_metadata_blocks;
248848debafeSMikulas Patocka 		uint64_t n_bitmap_bits;
248948debafeSMikulas Patocka 
2490d284f824SMikulas Patocka 		wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2491d284f824SMikulas Patocka 
249248debafeSMikulas Patocka 		bio_list_init(&wc->flush_list);
2493f635237aSCai Huoqing 		wc->flush_thread = kthread_run(writecache_flush_thread, wc, "dm_writecache_flush");
249448debafeSMikulas Patocka 		if (IS_ERR(wc->flush_thread)) {
249548debafeSMikulas Patocka 			r = PTR_ERR(wc->flush_thread);
249648debafeSMikulas Patocka 			wc->flush_thread = NULL;
2497e8ea141aSShenghui Wang 			ti->error = "Couldn't spawn flush thread";
249848debafeSMikulas Patocka 			goto bad;
249948debafeSMikulas Patocka 		}
250048debafeSMikulas Patocka 
250148debafeSMikulas Patocka 		r = calculate_memory_size(wc->memory_map_size, wc->block_size,
250248debafeSMikulas Patocka 					  &n_blocks, &n_metadata_blocks);
250348debafeSMikulas Patocka 		if (r) {
250448debafeSMikulas Patocka 			ti->error = "Invalid device size";
250548debafeSMikulas Patocka 			goto bad;
250648debafeSMikulas Patocka 		}
250748debafeSMikulas Patocka 
250848debafeSMikulas Patocka 		n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
250948debafeSMikulas Patocka 				 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
251048debafeSMikulas Patocka 		/* this is limitation of test_bit functions */
251148debafeSMikulas Patocka 		if (n_bitmap_bits > 1U << 31) {
251248debafeSMikulas Patocka 			r = -EFBIG;
251348debafeSMikulas Patocka 			ti->error = "Invalid device size";
251448debafeSMikulas Patocka 			goto bad;
251548debafeSMikulas Patocka 		}
251648debafeSMikulas Patocka 
251748debafeSMikulas Patocka 		wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
251848debafeSMikulas Patocka 		if (!wc->memory_map) {
251948debafeSMikulas Patocka 			r = -ENOMEM;
252048debafeSMikulas Patocka 			ti->error = "Unable to allocate memory for metadata";
252148debafeSMikulas Patocka 			goto bad;
252248debafeSMikulas Patocka 		}
252348debafeSMikulas Patocka 
252448debafeSMikulas Patocka 		wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
252548debafeSMikulas Patocka 		if (IS_ERR(wc->dm_kcopyd)) {
252648debafeSMikulas Patocka 			r = PTR_ERR(wc->dm_kcopyd);
252748debafeSMikulas Patocka 			ti->error = "Unable to allocate dm-kcopyd client";
252848debafeSMikulas Patocka 			wc->dm_kcopyd = NULL;
252948debafeSMikulas Patocka 			goto bad;
253048debafeSMikulas Patocka 		}
253148debafeSMikulas Patocka 
253248debafeSMikulas Patocka 		wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
253348debafeSMikulas Patocka 		wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
253448debafeSMikulas Patocka 			BITS_PER_LONG * sizeof(unsigned long);
253548debafeSMikulas Patocka 		wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
253648debafeSMikulas Patocka 		if (!wc->dirty_bitmap) {
253748debafeSMikulas Patocka 			r = -ENOMEM;
253848debafeSMikulas Patocka 			ti->error = "Unable to allocate dirty bitmap";
253948debafeSMikulas Patocka 			goto bad;
254048debafeSMikulas Patocka 		}
254148debafeSMikulas Patocka 
254231b22120SMikulas Patocka 		r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT);
254348debafeSMikulas Patocka 		if (r) {
254431b22120SMikulas Patocka 			ti->error = "Unable to read first block of metadata";
254548debafeSMikulas Patocka 			goto bad;
254648debafeSMikulas Patocka 		}
254748debafeSMikulas Patocka 	}
254848debafeSMikulas Patocka 
2549ec6347bbSDan Williams 	r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock));
255048debafeSMikulas Patocka 	if (r) {
255148debafeSMikulas Patocka 		ti->error = "Hardware memory error when reading superblock";
255248debafeSMikulas Patocka 		goto bad;
255348debafeSMikulas Patocka 	}
255448debafeSMikulas Patocka 	if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
255548debafeSMikulas Patocka 		r = init_memory(wc);
255648debafeSMikulas Patocka 		if (r) {
255748debafeSMikulas Patocka 			ti->error = "Unable to initialize device";
255848debafeSMikulas Patocka 			goto bad;
255948debafeSMikulas Patocka 		}
2560ec6347bbSDan Williams 		r = copy_mc_to_kernel(&s, sb(wc),
2561ec6347bbSDan Williams 				      sizeof(struct wc_memory_superblock));
256248debafeSMikulas Patocka 		if (r) {
256348debafeSMikulas Patocka 			ti->error = "Hardware memory error when reading superblock";
256448debafeSMikulas Patocka 			goto bad;
256548debafeSMikulas Patocka 		}
256648debafeSMikulas Patocka 	}
256748debafeSMikulas Patocka 
256848debafeSMikulas Patocka 	if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
256948debafeSMikulas Patocka 		ti->error = "Invalid magic in the superblock";
257048debafeSMikulas Patocka 		r = -EINVAL;
257148debafeSMikulas Patocka 		goto bad;
257248debafeSMikulas Patocka 	}
257348debafeSMikulas Patocka 
257448debafeSMikulas Patocka 	if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
257548debafeSMikulas Patocka 		ti->error = "Invalid version in the superblock";
257648debafeSMikulas Patocka 		r = -EINVAL;
257748debafeSMikulas Patocka 		goto bad;
257848debafeSMikulas Patocka 	}
257948debafeSMikulas Patocka 
258048debafeSMikulas Patocka 	if (le32_to_cpu(s.block_size) != wc->block_size) {
258148debafeSMikulas Patocka 		ti->error = "Block size does not match superblock";
258248debafeSMikulas Patocka 		r = -EINVAL;
258348debafeSMikulas Patocka 		goto bad;
258448debafeSMikulas Patocka 	}
258548debafeSMikulas Patocka 
258648debafeSMikulas Patocka 	wc->n_blocks = le64_to_cpu(s.n_blocks);
258748debafeSMikulas Patocka 
258848debafeSMikulas Patocka 	offset = wc->n_blocks * sizeof(struct wc_memory_entry);
258948debafeSMikulas Patocka 	if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
259048debafeSMikulas Patocka overflow:
259148debafeSMikulas Patocka 		ti->error = "Overflow in size calculation";
259248debafeSMikulas Patocka 		r = -EINVAL;
259348debafeSMikulas Patocka 		goto bad;
259448debafeSMikulas Patocka 	}
259548debafeSMikulas Patocka 	offset += sizeof(struct wc_memory_superblock);
259648debafeSMikulas Patocka 	if (offset < sizeof(struct wc_memory_superblock))
259748debafeSMikulas Patocka 		goto overflow;
259848debafeSMikulas Patocka 	offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
259948debafeSMikulas Patocka 	data_size = wc->n_blocks * (size_t)wc->block_size;
260048debafeSMikulas Patocka 	if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
260148debafeSMikulas Patocka 	    (offset + data_size < offset))
260248debafeSMikulas Patocka 		goto overflow;
260348debafeSMikulas Patocka 	if (offset + data_size > wc->memory_map_size) {
260448debafeSMikulas Patocka 		ti->error = "Memory area is too small";
260548debafeSMikulas Patocka 		r = -EINVAL;
260648debafeSMikulas Patocka 		goto bad;
260748debafeSMikulas Patocka 	}
260848debafeSMikulas Patocka 
260948debafeSMikulas Patocka 	wc->metadata_sectors = offset >> SECTOR_SHIFT;
261048debafeSMikulas Patocka 	wc->block_start = (char *)sb(wc) + offset;
261148debafeSMikulas Patocka 
261248debafeSMikulas Patocka 	x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
261348debafeSMikulas Patocka 	x += 50;
261448debafeSMikulas Patocka 	do_div(x, 100);
261548debafeSMikulas Patocka 	wc->freelist_high_watermark = x;
261648debafeSMikulas Patocka 	x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
261748debafeSMikulas Patocka 	x += 50;
261848debafeSMikulas Patocka 	do_div(x, 100);
261948debafeSMikulas Patocka 	wc->freelist_low_watermark = x;
262048debafeSMikulas Patocka 
262193de44ebSMikulas Patocka 	if (wc->cleaner)
262293de44ebSMikulas Patocka 		activate_cleaner(wc);
262393de44ebSMikulas Patocka 
262448debafeSMikulas Patocka 	r = writecache_alloc_entries(wc);
262548debafeSMikulas Patocka 	if (r) {
262648debafeSMikulas Patocka 		ti->error = "Cannot allocate memory";
262748debafeSMikulas Patocka 		goto bad;
262848debafeSMikulas Patocka 	}
262948debafeSMikulas Patocka 
2630ee55b92aSMikulas Patocka 	ti->num_flush_bios = WC_MODE_PMEM(wc) ? 1 : 2;
263148debafeSMikulas Patocka 	ti->flush_supported = true;
263248debafeSMikulas Patocka 	ti->num_discard_bios = 1;
263348debafeSMikulas Patocka 
263448debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
263548debafeSMikulas Patocka 		persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
263648debafeSMikulas Patocka 
263748debafeSMikulas Patocka 	return 0;
263848debafeSMikulas Patocka 
263948debafeSMikulas Patocka bad_arguments:
264048debafeSMikulas Patocka 	r = -EINVAL;
264148debafeSMikulas Patocka 	ti->error = "Bad arguments";
264248debafeSMikulas Patocka bad:
264348debafeSMikulas Patocka 	writecache_dtr(ti);
264448debafeSMikulas Patocka 	return r;
264548debafeSMikulas Patocka }
264648debafeSMikulas Patocka 
264748debafeSMikulas Patocka static void writecache_status(struct dm_target *ti, status_type_t type,
264848debafeSMikulas Patocka 			      unsigned status_flags, char *result, unsigned maxlen)
264948debafeSMikulas Patocka {
265048debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
265148debafeSMikulas Patocka 	unsigned extra_args;
265248debafeSMikulas Patocka 	unsigned sz = 0;
265348debafeSMikulas Patocka 
265448debafeSMikulas Patocka 	switch (type) {
265548debafeSMikulas Patocka 	case STATUSTYPE_INFO:
2656e3a35d03SMikulas Patocka 		DMEMIT("%ld %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
2657e3a35d03SMikulas Patocka 		       writecache_has_error(wc),
265848debafeSMikulas Patocka 		       (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2659e3a35d03SMikulas Patocka 		       (unsigned long long)wc->writeback_size,
2660e3a35d03SMikulas Patocka 		       wc->stats.reads,
2661e3a35d03SMikulas Patocka 		       wc->stats.read_hits,
2662e3a35d03SMikulas Patocka 		       wc->stats.writes,
2663e3a35d03SMikulas Patocka 		       wc->stats.write_hits_uncommitted,
2664e3a35d03SMikulas Patocka 		       wc->stats.write_hits_committed,
2665e3a35d03SMikulas Patocka 		       wc->stats.writes_around,
2666e3a35d03SMikulas Patocka 		       wc->stats.writes_allocate,
2667e3a35d03SMikulas Patocka 		       wc->stats.writes_blocked_on_freelist,
2668e3a35d03SMikulas Patocka 		       wc->stats.flushes,
2669e3a35d03SMikulas Patocka 		       wc->stats.discards);
267048debafeSMikulas Patocka 		break;
267148debafeSMikulas Patocka 	case STATUSTYPE_TABLE:
267248debafeSMikulas Patocka 		DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
267348debafeSMikulas Patocka 				wc->dev->name, wc->ssd_dev->name, wc->block_size);
267448debafeSMikulas Patocka 		extra_args = 0;
2675054bee16SMikulas Patocka 		if (wc->start_sector_set)
26769ff07e7dSMikulas Patocka 			extra_args += 2;
2677054bee16SMikulas Patocka 		if (wc->high_wm_percent_set)
267848debafeSMikulas Patocka 			extra_args += 2;
2679054bee16SMikulas Patocka 		if (wc->low_wm_percent_set)
268048debafeSMikulas Patocka 			extra_args += 2;
268148debafeSMikulas Patocka 		if (wc->max_writeback_jobs_set)
268248debafeSMikulas Patocka 			extra_args += 2;
268348debafeSMikulas Patocka 		if (wc->autocommit_blocks_set)
268448debafeSMikulas Patocka 			extra_args += 2;
268548debafeSMikulas Patocka 		if (wc->autocommit_time_set)
268648debafeSMikulas Patocka 			extra_args += 2;
2687054bee16SMikulas Patocka 		if (wc->max_age_set)
2688e5d41cbcSMikulas Patocka 			extra_args += 2;
2689054bee16SMikulas Patocka 		if (wc->cleaner_set)
269093de44ebSMikulas Patocka 			extra_args++;
269148debafeSMikulas Patocka 		if (wc->writeback_fua_set)
269248debafeSMikulas Patocka 			extra_args++;
2693611c3e16SMikulas Patocka 		if (wc->metadata_only)
2694611c3e16SMikulas Patocka 			extra_args++;
26955c0de3d7SMikulas Patocka 		if (wc->pause_set)
26965c0de3d7SMikulas Patocka 			extra_args += 2;
269748debafeSMikulas Patocka 
269848debafeSMikulas Patocka 		DMEMIT("%u", extra_args);
2699054bee16SMikulas Patocka 		if (wc->start_sector_set)
27009ff07e7dSMikulas Patocka 			DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2701054bee16SMikulas Patocka 		if (wc->high_wm_percent_set)
2702054bee16SMikulas Patocka 			DMEMIT(" high_watermark %u", wc->high_wm_percent_value);
2703054bee16SMikulas Patocka 		if (wc->low_wm_percent_set)
2704054bee16SMikulas Patocka 			DMEMIT(" low_watermark %u", wc->low_wm_percent_value);
270548debafeSMikulas Patocka 		if (wc->max_writeback_jobs_set)
270648debafeSMikulas Patocka 			DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
270748debafeSMikulas Patocka 		if (wc->autocommit_blocks_set)
270848debafeSMikulas Patocka 			DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
270948debafeSMikulas Patocka 		if (wc->autocommit_time_set)
2710054bee16SMikulas Patocka 			DMEMIT(" autocommit_time %u", wc->autocommit_time_value);
2711054bee16SMikulas Patocka 		if (wc->max_age_set)
2712054bee16SMikulas Patocka 			DMEMIT(" max_age %u", wc->max_age_value);
2713054bee16SMikulas Patocka 		if (wc->cleaner_set)
271493de44ebSMikulas Patocka 			DMEMIT(" cleaner");
271548debafeSMikulas Patocka 		if (wc->writeback_fua_set)
271648debafeSMikulas Patocka 			DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2717611c3e16SMikulas Patocka 		if (wc->metadata_only)
2718611c3e16SMikulas Patocka 			DMEMIT(" metadata_only");
27195c0de3d7SMikulas Patocka 		if (wc->pause_set)
27205c0de3d7SMikulas Patocka 			DMEMIT(" pause_writeback %u", wc->pause_value);
272148debafeSMikulas Patocka 		break;
27228ec45662STushar Sugandhi 	case STATUSTYPE_IMA:
27238ec45662STushar Sugandhi 		*result = '\0';
27248ec45662STushar Sugandhi 		break;
272548debafeSMikulas Patocka 	}
272648debafeSMikulas Patocka }
272748debafeSMikulas Patocka 
272848debafeSMikulas Patocka static struct target_type writecache_target = {
272948debafeSMikulas Patocka 	.name			= "writecache",
2730e3a35d03SMikulas Patocka 	.version		= {1, 6, 0},
273148debafeSMikulas Patocka 	.module			= THIS_MODULE,
273248debafeSMikulas Patocka 	.ctr			= writecache_ctr,
273348debafeSMikulas Patocka 	.dtr			= writecache_dtr,
273448debafeSMikulas Patocka 	.status			= writecache_status,
273548debafeSMikulas Patocka 	.postsuspend		= writecache_suspend,
273648debafeSMikulas Patocka 	.resume			= writecache_resume,
273748debafeSMikulas Patocka 	.message		= writecache_message,
273848debafeSMikulas Patocka 	.map			= writecache_map,
273948debafeSMikulas Patocka 	.end_io			= writecache_end_io,
274048debafeSMikulas Patocka 	.iterate_devices	= writecache_iterate_devices,
274148debafeSMikulas Patocka 	.io_hints		= writecache_io_hints,
274248debafeSMikulas Patocka };
274348debafeSMikulas Patocka 
274448debafeSMikulas Patocka static int __init dm_writecache_init(void)
274548debafeSMikulas Patocka {
274648debafeSMikulas Patocka 	int r;
274748debafeSMikulas Patocka 
274848debafeSMikulas Patocka 	r = dm_register_target(&writecache_target);
274948debafeSMikulas Patocka 	if (r < 0) {
275048debafeSMikulas Patocka 		DMERR("register failed %d", r);
275148debafeSMikulas Patocka 		return r;
275248debafeSMikulas Patocka 	}
275348debafeSMikulas Patocka 
275448debafeSMikulas Patocka 	return 0;
275548debafeSMikulas Patocka }
275648debafeSMikulas Patocka 
275748debafeSMikulas Patocka static void __exit dm_writecache_exit(void)
275848debafeSMikulas Patocka {
275948debafeSMikulas Patocka 	dm_unregister_target(&writecache_target);
276048debafeSMikulas Patocka }
276148debafeSMikulas Patocka 
276248debafeSMikulas Patocka module_init(dm_writecache_init);
276348debafeSMikulas Patocka module_exit(dm_writecache_exit);
276448debafeSMikulas Patocka 
276548debafeSMikulas Patocka MODULE_DESCRIPTION(DM_NAME " writecache target");
276648debafeSMikulas Patocka MODULE_AUTHOR("Mikulas Patocka <dm-devel@redhat.com>");
276748debafeSMikulas Patocka MODULE_LICENSE("GPL");
2768