xref: /linux/drivers/md/dm-writecache.c (revision 79790b6818e96c58fe2bffee1b418c16e64e7b80)
13bd94003SHeinz Mauelshagen // SPDX-License-Identifier: GPL-2.0-only
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
25ca7dc242SMikulas Patocka #define MAX_WRITEBACK_JOBS		min(0x10000000 / PAGE_SIZE, totalram_pages() / 16)
2648debafeSMikulas Patocka #define ENDIO_LATENCY			16
2748debafeSMikulas Patocka #define WRITEBACK_LATENCY		64
2848debafeSMikulas Patocka #define AUTOCOMMIT_BLOCKS_SSD		65536
2948debafeSMikulas Patocka #define AUTOCOMMIT_BLOCKS_PMEM		64
3048debafeSMikulas Patocka #define AUTOCOMMIT_MSEC			1000
313923d485SMikulas Patocka #define MAX_AGE_DIV			16
323923d485SMikulas Patocka #define MAX_AGE_UNSPECIFIED		-1UL
335c0de3d7SMikulas Patocka #define PAUSE_WRITEBACK			(HZ * 3)
3448debafeSMikulas Patocka 
3548debafeSMikulas Patocka #define BITMAP_GRANULARITY	65536
3648debafeSMikulas Patocka #if BITMAP_GRANULARITY < PAGE_SIZE
3748debafeSMikulas Patocka #undef BITMAP_GRANULARITY
3848debafeSMikulas Patocka #define BITMAP_GRANULARITY	PAGE_SIZE
3948debafeSMikulas Patocka #endif
4048debafeSMikulas Patocka 
415d2a228bSChristoph Hellwig #if IS_ENABLED(CONFIG_ARCH_HAS_PMEM_API) && IS_ENABLED(CONFIG_FS_DAX)
4248debafeSMikulas Patocka #define DM_WRITECACHE_HAS_PMEM
4348debafeSMikulas Patocka #endif
4448debafeSMikulas Patocka 
4548debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM
4648debafeSMikulas Patocka #define pmem_assign(dest, src)					\
4748debafeSMikulas Patocka do {								\
4848debafeSMikulas Patocka 	typeof(dest) uniq = (src);				\
4948debafeSMikulas Patocka 	memcpy_flushcache(&(dest), &uniq, sizeof(dest));	\
5048debafeSMikulas Patocka } while (0)
5148debafeSMikulas Patocka #else
5248debafeSMikulas Patocka #define pmem_assign(dest, src)	((dest) = (src))
5348debafeSMikulas Patocka #endif
5448debafeSMikulas Patocka 
55ec6347bbSDan Williams #if IS_ENABLED(CONFIG_ARCH_HAS_COPY_MC) && defined(DM_WRITECACHE_HAS_PMEM)
5648debafeSMikulas Patocka #define DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
5748debafeSMikulas Patocka #endif
5848debafeSMikulas Patocka 
5948debafeSMikulas Patocka #define MEMORY_SUPERBLOCK_MAGIC		0x23489321
6048debafeSMikulas Patocka #define MEMORY_SUPERBLOCK_VERSION	1
6148debafeSMikulas Patocka 
6248debafeSMikulas Patocka struct wc_memory_entry {
6348debafeSMikulas Patocka 	__le64 original_sector;
6448debafeSMikulas Patocka 	__le64 seq_count;
6548debafeSMikulas Patocka };
6648debafeSMikulas Patocka 
6748debafeSMikulas Patocka struct wc_memory_superblock {
6848debafeSMikulas Patocka 	union {
6948debafeSMikulas Patocka 		struct {
7048debafeSMikulas Patocka 			__le32 magic;
7148debafeSMikulas Patocka 			__le32 version;
7248debafeSMikulas Patocka 			__le32 block_size;
7348debafeSMikulas Patocka 			__le32 pad;
7448debafeSMikulas Patocka 			__le64 n_blocks;
7548debafeSMikulas Patocka 			__le64 seq_count;
7648debafeSMikulas Patocka 		};
7748debafeSMikulas Patocka 		__le64 padding[8];
7848debafeSMikulas Patocka 	};
79c40819f2SJulia Lawall 	struct wc_memory_entry entries[];
8048debafeSMikulas Patocka };
8148debafeSMikulas Patocka 
8248debafeSMikulas Patocka struct wc_entry {
8348debafeSMikulas Patocka 	struct rb_node rb_node;
8448debafeSMikulas Patocka 	struct list_head lru;
8548debafeSMikulas Patocka 	unsigned short wc_list_contiguous;
8648debafeSMikulas Patocka #if BITS_PER_LONG == 64
870ef0b471SHeinz Mauelshagen 	bool write_in_progress : 1;
880ef0b471SHeinz Mauelshagen 	unsigned long index : 47;
890ef0b471SHeinz Mauelshagen #else
900ef0b471SHeinz Mauelshagen 	bool write_in_progress;
910ef0b471SHeinz Mauelshagen 	unsigned long index;
9248debafeSMikulas Patocka #endif
933923d485SMikulas Patocka 	unsigned long age;
9448debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
9548debafeSMikulas Patocka 	uint64_t original_sector;
9648debafeSMikulas Patocka 	uint64_t seq_count;
9748debafeSMikulas Patocka #endif
9848debafeSMikulas Patocka };
9948debafeSMikulas Patocka 
10048debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM
10148debafeSMikulas Patocka #define WC_MODE_PMEM(wc)			((wc)->pmem_mode)
10248debafeSMikulas Patocka #define WC_MODE_FUA(wc)				((wc)->writeback_fua)
10348debafeSMikulas Patocka #else
10448debafeSMikulas Patocka #define WC_MODE_PMEM(wc)			false
10548debafeSMikulas Patocka #define WC_MODE_FUA(wc)				false
10648debafeSMikulas Patocka #endif
10748debafeSMikulas Patocka #define WC_MODE_SORT_FREELIST(wc)		(!WC_MODE_PMEM(wc))
10848debafeSMikulas Patocka 
10948debafeSMikulas Patocka struct dm_writecache {
11048debafeSMikulas Patocka 	struct mutex lock;
11148debafeSMikulas Patocka 	struct list_head lru;
11248debafeSMikulas Patocka 	union {
11348debafeSMikulas Patocka 		struct list_head freelist;
11448debafeSMikulas Patocka 		struct {
11548debafeSMikulas Patocka 			struct rb_root freetree;
11648debafeSMikulas Patocka 			struct wc_entry *current_free;
11748debafeSMikulas Patocka 		};
11848debafeSMikulas Patocka 	};
11948debafeSMikulas Patocka 	struct rb_root tree;
12048debafeSMikulas Patocka 
12148debafeSMikulas Patocka 	size_t freelist_size;
12248debafeSMikulas Patocka 	size_t writeback_size;
12348debafeSMikulas Patocka 	size_t freelist_high_watermark;
12448debafeSMikulas Patocka 	size_t freelist_low_watermark;
1253923d485SMikulas Patocka 	unsigned long max_age;
1265c0de3d7SMikulas Patocka 	unsigned long pause;
12748debafeSMikulas Patocka 
12886a3238cSHeinz Mauelshagen 	unsigned int uncommitted_blocks;
12986a3238cSHeinz Mauelshagen 	unsigned int autocommit_blocks;
13086a3238cSHeinz Mauelshagen 	unsigned int max_writeback_jobs;
13148debafeSMikulas Patocka 
13248debafeSMikulas Patocka 	int error;
13348debafeSMikulas Patocka 
13448debafeSMikulas Patocka 	unsigned long autocommit_jiffies;
13548debafeSMikulas Patocka 	struct timer_list autocommit_timer;
13648debafeSMikulas Patocka 	struct wait_queue_head freelist_wait;
13748debafeSMikulas Patocka 
1383923d485SMikulas Patocka 	struct timer_list max_age_timer;
1393923d485SMikulas Patocka 
14048debafeSMikulas Patocka 	atomic_t bio_in_progress[2];
14148debafeSMikulas Patocka 	struct wait_queue_head bio_in_progress_wait[2];
14248debafeSMikulas Patocka 
14348debafeSMikulas Patocka 	struct dm_target *ti;
14448debafeSMikulas Patocka 	struct dm_dev *dev;
14548debafeSMikulas Patocka 	struct dm_dev *ssd_dev;
146d284f824SMikulas Patocka 	sector_t start_sector;
14748debafeSMikulas Patocka 	void *memory_map;
14848debafeSMikulas Patocka 	uint64_t memory_map_size;
14948debafeSMikulas Patocka 	size_t metadata_sectors;
15048debafeSMikulas Patocka 	size_t n_blocks;
15148debafeSMikulas Patocka 	uint64_t seq_count;
1524134455fSMikulas Patocka 	sector_t data_device_sectors;
15348debafeSMikulas Patocka 	void *block_start;
15448debafeSMikulas Patocka 	struct wc_entry *entries;
15586a3238cSHeinz Mauelshagen 	unsigned int block_size;
15648debafeSMikulas Patocka 	unsigned char block_size_bits;
15748debafeSMikulas Patocka 
15848debafeSMikulas Patocka 	bool pmem_mode:1;
15948debafeSMikulas Patocka 	bool writeback_fua:1;
16048debafeSMikulas Patocka 
16148debafeSMikulas Patocka 	bool overwrote_committed:1;
16248debafeSMikulas Patocka 	bool memory_vmapped:1;
16348debafeSMikulas Patocka 
164054bee16SMikulas Patocka 	bool start_sector_set:1;
16548debafeSMikulas Patocka 	bool high_wm_percent_set:1;
16648debafeSMikulas Patocka 	bool low_wm_percent_set:1;
16748debafeSMikulas Patocka 	bool max_writeback_jobs_set:1;
16848debafeSMikulas Patocka 	bool autocommit_blocks_set:1;
16948debafeSMikulas Patocka 	bool autocommit_time_set:1;
170054bee16SMikulas Patocka 	bool max_age_set:1;
17148debafeSMikulas Patocka 	bool writeback_fua_set:1;
17248debafeSMikulas Patocka 	bool flush_on_suspend:1;
17393de44ebSMikulas Patocka 	bool cleaner:1;
174054bee16SMikulas Patocka 	bool cleaner_set:1;
175611c3e16SMikulas Patocka 	bool metadata_only:1;
1765c0de3d7SMikulas Patocka 	bool pause_set:1;
177054bee16SMikulas Patocka 
17886a3238cSHeinz Mauelshagen 	unsigned int high_wm_percent_value;
17986a3238cSHeinz Mauelshagen 	unsigned int low_wm_percent_value;
18086a3238cSHeinz Mauelshagen 	unsigned int autocommit_time_value;
18186a3238cSHeinz Mauelshagen 	unsigned int max_age_value;
18286a3238cSHeinz Mauelshagen 	unsigned int pause_value;
18348debafeSMikulas Patocka 
18486a3238cSHeinz Mauelshagen 	unsigned int writeback_all;
18548debafeSMikulas Patocka 	struct workqueue_struct *writeback_wq;
18648debafeSMikulas Patocka 	struct work_struct writeback_work;
18748debafeSMikulas Patocka 	struct work_struct flush_work;
18848debafeSMikulas Patocka 
18995b88f4dSMikulas Patocka 	struct dm_io_tracker iot;
19095b88f4dSMikulas Patocka 
19148debafeSMikulas Patocka 	struct dm_io_client *dm_io;
19248debafeSMikulas Patocka 
19348debafeSMikulas Patocka 	raw_spinlock_t endio_list_lock;
19448debafeSMikulas Patocka 	struct list_head endio_list;
19548debafeSMikulas Patocka 	struct task_struct *endio_thread;
19648debafeSMikulas Patocka 
19748debafeSMikulas Patocka 	struct task_struct *flush_thread;
19848debafeSMikulas Patocka 	struct bio_list flush_list;
19948debafeSMikulas Patocka 
20048debafeSMikulas Patocka 	struct dm_kcopyd_client *dm_kcopyd;
20148debafeSMikulas Patocka 	unsigned long *dirty_bitmap;
20286a3238cSHeinz Mauelshagen 	unsigned int dirty_bitmap_size;
20348debafeSMikulas Patocka 
20448debafeSMikulas Patocka 	struct bio_set bio_set;
20548debafeSMikulas Patocka 	mempool_t copy_pool;
206e3a35d03SMikulas Patocka 
207e3a35d03SMikulas Patocka 	struct {
208e3a35d03SMikulas Patocka 		unsigned long long reads;
209e3a35d03SMikulas Patocka 		unsigned long long read_hits;
210e3a35d03SMikulas Patocka 		unsigned long long writes;
211e3a35d03SMikulas Patocka 		unsigned long long write_hits_uncommitted;
212e3a35d03SMikulas Patocka 		unsigned long long write_hits_committed;
213e3a35d03SMikulas Patocka 		unsigned long long writes_around;
214e3a35d03SMikulas Patocka 		unsigned long long writes_allocate;
215e3a35d03SMikulas Patocka 		unsigned long long writes_blocked_on_freelist;
216e3a35d03SMikulas Patocka 		unsigned long long flushes;
217e3a35d03SMikulas Patocka 		unsigned long long discards;
218e3a35d03SMikulas Patocka 	} stats;
21948debafeSMikulas Patocka };
22048debafeSMikulas Patocka 
22148debafeSMikulas Patocka #define WB_LIST_INLINE		16
22248debafeSMikulas Patocka 
22348debafeSMikulas Patocka struct writeback_struct {
22448debafeSMikulas Patocka 	struct list_head endio_entry;
22548debafeSMikulas Patocka 	struct dm_writecache *wc;
22648debafeSMikulas Patocka 	struct wc_entry **wc_list;
22786a3238cSHeinz Mauelshagen 	unsigned int wc_list_n;
22848debafeSMikulas Patocka 	struct wc_entry *wc_list_inline[WB_LIST_INLINE];
22948debafeSMikulas Patocka 	struct bio bio;
23048debafeSMikulas Patocka };
23148debafeSMikulas Patocka 
23248debafeSMikulas Patocka struct copy_struct {
23348debafeSMikulas Patocka 	struct list_head endio_entry;
23448debafeSMikulas Patocka 	struct dm_writecache *wc;
23548debafeSMikulas Patocka 	struct wc_entry *e;
23686a3238cSHeinz Mauelshagen 	unsigned int n_entries;
23748debafeSMikulas Patocka 	int error;
23848debafeSMikulas Patocka };
23948debafeSMikulas Patocka 
24048debafeSMikulas Patocka DECLARE_DM_KCOPYD_THROTTLE_WITH_MODULE_PARM(dm_writecache_throttle,
24148debafeSMikulas Patocka 					    "A percentage of time allocated for data copying");
24248debafeSMikulas Patocka 
wc_lock(struct dm_writecache * wc)24348debafeSMikulas Patocka static void wc_lock(struct dm_writecache *wc)
24448debafeSMikulas Patocka {
24548debafeSMikulas Patocka 	mutex_lock(&wc->lock);
24648debafeSMikulas Patocka }
24748debafeSMikulas Patocka 
wc_unlock(struct dm_writecache * wc)24848debafeSMikulas Patocka static void wc_unlock(struct dm_writecache *wc)
24948debafeSMikulas Patocka {
25048debafeSMikulas Patocka 	mutex_unlock(&wc->lock);
25148debafeSMikulas Patocka }
25248debafeSMikulas Patocka 
25348debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM
persistent_memory_claim(struct dm_writecache * wc)25448debafeSMikulas Patocka static int persistent_memory_claim(struct dm_writecache *wc)
25548debafeSMikulas Patocka {
25648debafeSMikulas Patocka 	int r;
25748debafeSMikulas Patocka 	loff_t s;
25848debafeSMikulas Patocka 	long p, da;
25948debafeSMikulas Patocka 	pfn_t pfn;
26048debafeSMikulas Patocka 	int id;
26148debafeSMikulas Patocka 	struct page **pages;
262f9e040efSMikulas Patocka 	sector_t offset;
26348debafeSMikulas Patocka 
26448debafeSMikulas Patocka 	wc->memory_vmapped = false;
26548debafeSMikulas Patocka 
26648debafeSMikulas Patocka 	s = wc->memory_map_size;
26748debafeSMikulas Patocka 	p = s >> PAGE_SHIFT;
26848debafeSMikulas Patocka 	if (!p) {
26948debafeSMikulas Patocka 		r = -EINVAL;
27048debafeSMikulas Patocka 		goto err1;
27148debafeSMikulas Patocka 	}
27248debafeSMikulas Patocka 	if (p != s >> PAGE_SHIFT) {
27348debafeSMikulas Patocka 		r = -EOVERFLOW;
27448debafeSMikulas Patocka 		goto err1;
27548debafeSMikulas Patocka 	}
27648debafeSMikulas Patocka 
277f9e040efSMikulas Patocka 	offset = get_start_sect(wc->ssd_dev->bdev);
278f9e040efSMikulas Patocka 	if (offset & (PAGE_SIZE / 512 - 1)) {
279f9e040efSMikulas Patocka 		r = -EINVAL;
280f9e040efSMikulas Patocka 		goto err1;
281f9e040efSMikulas Patocka 	}
282f9e040efSMikulas Patocka 	offset >>= PAGE_SHIFT - 9;
283f9e040efSMikulas Patocka 
28448debafeSMikulas Patocka 	id = dax_read_lock();
28548debafeSMikulas Patocka 
286e511c4a3SJane Chu 	da = dax_direct_access(wc->ssd_dev->dax_dev, offset, p, DAX_ACCESS,
287e511c4a3SJane Chu 			&wc->memory_map, &pfn);
28848debafeSMikulas Patocka 	if (da < 0) {
28948debafeSMikulas Patocka 		wc->memory_map = NULL;
29048debafeSMikulas Patocka 		r = da;
29148debafeSMikulas Patocka 		goto err2;
29248debafeSMikulas Patocka 	}
29348debafeSMikulas Patocka 	if (!pfn_t_has_page(pfn)) {
29448debafeSMikulas Patocka 		wc->memory_map = NULL;
29548debafeSMikulas Patocka 		r = -EOPNOTSUPP;
29648debafeSMikulas Patocka 		goto err2;
29748debafeSMikulas Patocka 	}
29848debafeSMikulas Patocka 	if (da != p) {
29948debafeSMikulas Patocka 		long i;
3000ef0b471SHeinz Mauelshagen 
30148debafeSMikulas Patocka 		wc->memory_map = NULL;
30240ef8756SMikulas Patocka 		pages = vmalloc_array(p, sizeof(struct page *));
30348debafeSMikulas Patocka 		if (!pages) {
30448debafeSMikulas Patocka 			r = -ENOMEM;
30548debafeSMikulas Patocka 			goto err2;
30648debafeSMikulas Patocka 		}
30748debafeSMikulas Patocka 		i = 0;
30848debafeSMikulas Patocka 		do {
30948debafeSMikulas Patocka 			long daa;
3100ef0b471SHeinz Mauelshagen 
311e511c4a3SJane Chu 			daa = dax_direct_access(wc->ssd_dev->dax_dev, offset + i,
312e511c4a3SJane Chu 					p - i, DAX_ACCESS, NULL, &pfn);
31348debafeSMikulas Patocka 			if (daa <= 0) {
31448debafeSMikulas Patocka 				r = daa ? daa : -EINVAL;
31548debafeSMikulas Patocka 				goto err3;
31648debafeSMikulas Patocka 			}
31748debafeSMikulas Patocka 			if (!pfn_t_has_page(pfn)) {
31848debafeSMikulas Patocka 				r = -EOPNOTSUPP;
31948debafeSMikulas Patocka 				goto err3;
32048debafeSMikulas Patocka 			}
32148debafeSMikulas Patocka 			while (daa-- && i < p) {
32248debafeSMikulas Patocka 				pages[i++] = pfn_t_to_page(pfn);
32348debafeSMikulas Patocka 				pfn.val++;
324d35bd764SMikulas Patocka 				if (!(i & 15))
325d35bd764SMikulas Patocka 					cond_resched();
32648debafeSMikulas Patocka 			}
32748debafeSMikulas Patocka 		} while (i < p);
32848debafeSMikulas Patocka 		wc->memory_map = vmap(pages, p, VM_MAP, PAGE_KERNEL);
32948debafeSMikulas Patocka 		if (!wc->memory_map) {
33048debafeSMikulas Patocka 			r = -ENOMEM;
33148debafeSMikulas Patocka 			goto err3;
33248debafeSMikulas Patocka 		}
33340ef8756SMikulas Patocka 		vfree(pages);
33448debafeSMikulas Patocka 		wc->memory_vmapped = true;
33548debafeSMikulas Patocka 	}
33648debafeSMikulas Patocka 
33748debafeSMikulas Patocka 	dax_read_unlock(id);
338d284f824SMikulas Patocka 
339d284f824SMikulas Patocka 	wc->memory_map += (size_t)wc->start_sector << SECTOR_SHIFT;
340d284f824SMikulas Patocka 	wc->memory_map_size -= (size_t)wc->start_sector << SECTOR_SHIFT;
341d284f824SMikulas Patocka 
34248debafeSMikulas Patocka 	return 0;
34348debafeSMikulas Patocka err3:
34440ef8756SMikulas Patocka 	vfree(pages);
34548debafeSMikulas Patocka err2:
34648debafeSMikulas Patocka 	dax_read_unlock(id);
34748debafeSMikulas Patocka err1:
34848debafeSMikulas Patocka 	return r;
34948debafeSMikulas Patocka }
35048debafeSMikulas Patocka #else
persistent_memory_claim(struct dm_writecache * wc)35148debafeSMikulas Patocka static int persistent_memory_claim(struct dm_writecache *wc)
35248debafeSMikulas Patocka {
353857c4c0aSMike Snitzer 	return -EOPNOTSUPP;
35448debafeSMikulas Patocka }
35548debafeSMikulas Patocka #endif
35648debafeSMikulas Patocka 
persistent_memory_release(struct dm_writecache * wc)35748debafeSMikulas Patocka static void persistent_memory_release(struct dm_writecache *wc)
35848debafeSMikulas Patocka {
35948debafeSMikulas Patocka 	if (wc->memory_vmapped)
360d284f824SMikulas Patocka 		vunmap(wc->memory_map - ((size_t)wc->start_sector << SECTOR_SHIFT));
36148debafeSMikulas Patocka }
36248debafeSMikulas Patocka 
persistent_memory_page(void * addr)36348debafeSMikulas Patocka static struct page *persistent_memory_page(void *addr)
36448debafeSMikulas Patocka {
36548debafeSMikulas Patocka 	if (is_vmalloc_addr(addr))
36648debafeSMikulas Patocka 		return vmalloc_to_page(addr);
36748debafeSMikulas Patocka 	else
36848debafeSMikulas Patocka 		return virt_to_page(addr);
36948debafeSMikulas Patocka }
37048debafeSMikulas Patocka 
persistent_memory_page_offset(void * addr)37186a3238cSHeinz Mauelshagen static unsigned int persistent_memory_page_offset(void *addr)
37248debafeSMikulas Patocka {
37348debafeSMikulas Patocka 	return (unsigned long)addr & (PAGE_SIZE - 1);
37448debafeSMikulas Patocka }
37548debafeSMikulas Patocka 
persistent_memory_flush_cache(void * ptr,size_t size)37648debafeSMikulas Patocka static void persistent_memory_flush_cache(void *ptr, size_t size)
37748debafeSMikulas Patocka {
37848debafeSMikulas Patocka 	if (is_vmalloc_addr(ptr))
37948debafeSMikulas Patocka 		flush_kernel_vmap_range(ptr, size);
38048debafeSMikulas Patocka }
38148debafeSMikulas Patocka 
persistent_memory_invalidate_cache(void * ptr,size_t size)38248debafeSMikulas Patocka static void persistent_memory_invalidate_cache(void *ptr, size_t size)
38348debafeSMikulas Patocka {
38448debafeSMikulas Patocka 	if (is_vmalloc_addr(ptr))
38548debafeSMikulas Patocka 		invalidate_kernel_vmap_range(ptr, size);
38648debafeSMikulas Patocka }
38748debafeSMikulas Patocka 
sb(struct dm_writecache * wc)38848debafeSMikulas Patocka static struct wc_memory_superblock *sb(struct dm_writecache *wc)
38948debafeSMikulas Patocka {
39048debafeSMikulas Patocka 	return wc->memory_map;
39148debafeSMikulas Patocka }
39248debafeSMikulas Patocka 
memory_entry(struct dm_writecache * wc,struct wc_entry * e)39348debafeSMikulas Patocka static struct wc_memory_entry *memory_entry(struct dm_writecache *wc, struct wc_entry *e)
39448debafeSMikulas Patocka {
39548debafeSMikulas Patocka 	return &sb(wc)->entries[e->index];
39648debafeSMikulas Patocka }
39748debafeSMikulas Patocka 
memory_data(struct dm_writecache * wc,struct wc_entry * e)39848debafeSMikulas Patocka static void *memory_data(struct dm_writecache *wc, struct wc_entry *e)
39948debafeSMikulas Patocka {
40048debafeSMikulas Patocka 	return (char *)wc->block_start + (e->index << wc->block_size_bits);
40148debafeSMikulas Patocka }
40248debafeSMikulas Patocka 
cache_sector(struct dm_writecache * wc,struct wc_entry * e)40348debafeSMikulas Patocka static sector_t cache_sector(struct dm_writecache *wc, struct wc_entry *e)
40448debafeSMikulas Patocka {
405d284f824SMikulas Patocka 	return wc->start_sector + wc->metadata_sectors +
40648debafeSMikulas Patocka 		((sector_t)e->index << (wc->block_size_bits - SECTOR_SHIFT));
40748debafeSMikulas Patocka }
40848debafeSMikulas Patocka 
read_original_sector(struct dm_writecache * wc,struct wc_entry * e)40948debafeSMikulas Patocka static uint64_t read_original_sector(struct dm_writecache *wc, struct wc_entry *e)
41048debafeSMikulas Patocka {
41148debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
41248debafeSMikulas Patocka 	return e->original_sector;
41348debafeSMikulas Patocka #else
41448debafeSMikulas Patocka 	return le64_to_cpu(memory_entry(wc, e)->original_sector);
41548debafeSMikulas Patocka #endif
41648debafeSMikulas Patocka }
41748debafeSMikulas Patocka 
read_seq_count(struct dm_writecache * wc,struct wc_entry * e)41848debafeSMikulas Patocka static uint64_t read_seq_count(struct dm_writecache *wc, struct wc_entry *e)
41948debafeSMikulas Patocka {
42048debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
42148debafeSMikulas Patocka 	return e->seq_count;
42248debafeSMikulas Patocka #else
42348debafeSMikulas Patocka 	return le64_to_cpu(memory_entry(wc, e)->seq_count);
42448debafeSMikulas Patocka #endif
42548debafeSMikulas Patocka }
42648debafeSMikulas Patocka 
clear_seq_count(struct dm_writecache * wc,struct wc_entry * e)42748debafeSMikulas Patocka static void clear_seq_count(struct dm_writecache *wc, struct wc_entry *e)
42848debafeSMikulas Patocka {
42948debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
43048debafeSMikulas Patocka 	e->seq_count = -1;
43148debafeSMikulas Patocka #endif
43248debafeSMikulas Patocka 	pmem_assign(memory_entry(wc, e)->seq_count, cpu_to_le64(-1));
43348debafeSMikulas Patocka }
43448debafeSMikulas Patocka 
write_original_sector_seq_count(struct dm_writecache * wc,struct wc_entry * e,uint64_t original_sector,uint64_t seq_count)43548debafeSMikulas Patocka static void write_original_sector_seq_count(struct dm_writecache *wc, struct wc_entry *e,
43648debafeSMikulas Patocka 					    uint64_t original_sector, uint64_t seq_count)
43748debafeSMikulas Patocka {
43848debafeSMikulas Patocka 	struct wc_memory_entry me;
43948debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
44048debafeSMikulas Patocka 	e->original_sector = original_sector;
44148debafeSMikulas Patocka 	e->seq_count = seq_count;
44248debafeSMikulas Patocka #endif
44348debafeSMikulas Patocka 	me.original_sector = cpu_to_le64(original_sector);
44448debafeSMikulas Patocka 	me.seq_count = cpu_to_le64(seq_count);
44548debafeSMikulas Patocka 	pmem_assign(*memory_entry(wc, e), me);
44648debafeSMikulas Patocka }
44748debafeSMikulas Patocka 
44848debafeSMikulas Patocka #define writecache_error(wc, err, msg, arg...)				\
44948debafeSMikulas Patocka do {									\
45048debafeSMikulas Patocka 	if (!cmpxchg(&(wc)->error, 0, err))				\
45148debafeSMikulas Patocka 		DMERR(msg, ##arg);					\
45248debafeSMikulas Patocka 	wake_up(&(wc)->freelist_wait);					\
45348debafeSMikulas Patocka } while (0)
45448debafeSMikulas Patocka 
45548debafeSMikulas Patocka #define writecache_has_error(wc)	(unlikely(READ_ONCE((wc)->error)))
45648debafeSMikulas Patocka 
writecache_flush_all_metadata(struct dm_writecache * wc)45748debafeSMikulas Patocka static void writecache_flush_all_metadata(struct dm_writecache *wc)
45848debafeSMikulas Patocka {
45948debafeSMikulas Patocka 	if (!WC_MODE_PMEM(wc))
46048debafeSMikulas Patocka 		memset(wc->dirty_bitmap, -1, wc->dirty_bitmap_size);
46148debafeSMikulas Patocka }
46248debafeSMikulas Patocka 
writecache_flush_region(struct dm_writecache * wc,void * ptr,size_t size)46348debafeSMikulas Patocka static void writecache_flush_region(struct dm_writecache *wc, void *ptr, size_t size)
46448debafeSMikulas Patocka {
46548debafeSMikulas Patocka 	if (!WC_MODE_PMEM(wc))
46648debafeSMikulas Patocka 		__set_bit(((char *)ptr - (char *)wc->memory_map) / BITMAP_GRANULARITY,
46748debafeSMikulas Patocka 			  wc->dirty_bitmap);
46848debafeSMikulas Patocka }
46948debafeSMikulas Patocka 
47048debafeSMikulas Patocka static void writecache_disk_flush(struct dm_writecache *wc, struct dm_dev *dev);
47148debafeSMikulas Patocka 
47248debafeSMikulas Patocka struct io_notify {
47348debafeSMikulas Patocka 	struct dm_writecache *wc;
47448debafeSMikulas Patocka 	struct completion c;
47548debafeSMikulas Patocka 	atomic_t count;
47648debafeSMikulas Patocka };
47748debafeSMikulas Patocka 
writecache_notify_io(unsigned long error,void * context)47848debafeSMikulas Patocka static void writecache_notify_io(unsigned long error, void *context)
47948debafeSMikulas Patocka {
48048debafeSMikulas Patocka 	struct io_notify *endio = context;
48148debafeSMikulas Patocka 
48248debafeSMikulas Patocka 	if (unlikely(error != 0))
48348debafeSMikulas Patocka 		writecache_error(endio->wc, -EIO, "error writing metadata");
48448debafeSMikulas Patocka 	BUG_ON(atomic_read(&endio->count) <= 0);
48548debafeSMikulas Patocka 	if (atomic_dec_and_test(&endio->count))
48648debafeSMikulas Patocka 		complete(&endio->c);
48748debafeSMikulas Patocka }
48848debafeSMikulas Patocka 
writecache_wait_for_ios(struct dm_writecache * wc,int direction)489aa950920SMikulas Patocka static void writecache_wait_for_ios(struct dm_writecache *wc, int direction)
490aa950920SMikulas Patocka {
491aa950920SMikulas Patocka 	wait_event(wc->bio_in_progress_wait[direction],
492aa950920SMikulas Patocka 		   !atomic_read(&wc->bio_in_progress[direction]));
493aa950920SMikulas Patocka }
494aa950920SMikulas Patocka 
ssd_commit_flushed(struct dm_writecache * wc,bool wait_for_ios)495aa950920SMikulas Patocka static void ssd_commit_flushed(struct dm_writecache *wc, bool wait_for_ios)
49648debafeSMikulas Patocka {
49748debafeSMikulas Patocka 	struct dm_io_region region;
49848debafeSMikulas Patocka 	struct dm_io_request req;
49948debafeSMikulas Patocka 	struct io_notify endio = {
50048debafeSMikulas Patocka 		wc,
50148debafeSMikulas Patocka 		COMPLETION_INITIALIZER_ONSTACK(endio.c),
50248debafeSMikulas Patocka 		ATOMIC_INIT(1),
50348debafeSMikulas Patocka 	};
50486a3238cSHeinz Mauelshagen 	unsigned int bitmap_bits = wc->dirty_bitmap_size * 8;
50586a3238cSHeinz Mauelshagen 	unsigned int i = 0;
50648debafeSMikulas Patocka 
50748debafeSMikulas Patocka 	while (1) {
50886a3238cSHeinz Mauelshagen 		unsigned int j;
5090ef0b471SHeinz Mauelshagen 
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);
526581075e4SBart 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 */
5346e5f0f63SHongyu Jin 		(void) dm_io(&req, 1, &region, NULL, IOPRIO_DEFAULT);
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 
ssd_commit_superblock(struct dm_writecache * wc)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 
564581075e4SBart 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 
5716e5f0f63SHongyu Jin 	r = dm_io(&req, 1, &region, NULL, IOPRIO_DEFAULT);
572dc8a01aeSMikulas Patocka 	if (unlikely(r))
573dc8a01aeSMikulas Patocka 		writecache_error(wc, r, "error writing superblock");
574dc8a01aeSMikulas Patocka }
575dc8a01aeSMikulas Patocka 
writecache_commit_flushed(struct dm_writecache * wc,bool wait_for_ios)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 
writecache_disk_flush(struct dm_writecache * wc,struct dm_dev * dev)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;
593581075e4SBart 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 
5996e5f0f63SHongyu Jin 	r = dm_io(&req, 1, &region, NULL, IOPRIO_DEFAULT);
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 
writecache_find_entry(struct dm_writecache * wc,uint64_t block,int flags)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;
6261c3fe2faSHeinz Mauelshagen 			if (read_original_sector(wc, e) >= block)
627f8011d33SMikulas Patocka 				return e;
6281c3fe2faSHeinz Mauelshagen 
62948debafeSMikulas Patocka 			node = rb_next(&e->rb_node);
630f8011d33SMikulas Patocka 			if (unlikely(!node))
63148debafeSMikulas Patocka 				return NULL;
6321c3fe2faSHeinz Mauelshagen 
63348debafeSMikulas Patocka 			e = container_of(node, struct wc_entry, rb_node);
634f8011d33SMikulas Patocka 			return e;
63548debafeSMikulas Patocka 		}
63648debafeSMikulas Patocka 	}
63748debafeSMikulas Patocka 
63848debafeSMikulas Patocka 	while (1) {
63948debafeSMikulas Patocka 		struct wc_entry *e2;
6400ef0b471SHeinz Mauelshagen 
64148debafeSMikulas Patocka 		if (flags & WFE_LOWEST_SEQ)
64248debafeSMikulas Patocka 			node = rb_prev(&e->rb_node);
64348debafeSMikulas Patocka 		else
64448debafeSMikulas Patocka 			node = rb_next(&e->rb_node);
64584420b1eSHuaisheng Ye 		if (unlikely(!node))
64648debafeSMikulas Patocka 			return e;
64748debafeSMikulas Patocka 		e2 = container_of(node, struct wc_entry, rb_node);
64848debafeSMikulas Patocka 		if (read_original_sector(wc, e2) != block)
64948debafeSMikulas Patocka 			return e;
65048debafeSMikulas Patocka 		e = e2;
65148debafeSMikulas Patocka 	}
65248debafeSMikulas Patocka }
65348debafeSMikulas Patocka 
writecache_insert_entry(struct dm_writecache * wc,struct wc_entry * ins)65448debafeSMikulas Patocka static void writecache_insert_entry(struct dm_writecache *wc, struct wc_entry *ins)
65548debafeSMikulas Patocka {
65648debafeSMikulas Patocka 	struct wc_entry *e;
65748debafeSMikulas Patocka 	struct rb_node **node = &wc->tree.rb_node, *parent = NULL;
65848debafeSMikulas Patocka 
65948debafeSMikulas Patocka 	while (*node) {
66048debafeSMikulas Patocka 		e = container_of(*node, struct wc_entry, rb_node);
66148debafeSMikulas Patocka 		parent = &e->rb_node;
66248debafeSMikulas Patocka 		if (read_original_sector(wc, e) > read_original_sector(wc, ins))
66348debafeSMikulas Patocka 			node = &parent->rb_left;
66448debafeSMikulas Patocka 		else
66548debafeSMikulas Patocka 			node = &parent->rb_right;
66648debafeSMikulas Patocka 	}
66748debafeSMikulas Patocka 	rb_link_node(&ins->rb_node, parent, node);
66848debafeSMikulas Patocka 	rb_insert_color(&ins->rb_node, &wc->tree);
66948debafeSMikulas Patocka 	list_add(&ins->lru, &wc->lru);
6703923d485SMikulas Patocka 	ins->age = jiffies;
67148debafeSMikulas Patocka }
67248debafeSMikulas Patocka 
writecache_unlink(struct dm_writecache * wc,struct wc_entry * e)67348debafeSMikulas Patocka static void writecache_unlink(struct dm_writecache *wc, struct wc_entry *e)
67448debafeSMikulas Patocka {
67548debafeSMikulas Patocka 	list_del(&e->lru);
67648debafeSMikulas Patocka 	rb_erase(&e->rb_node, &wc->tree);
67748debafeSMikulas Patocka }
67848debafeSMikulas Patocka 
writecache_add_to_freelist(struct dm_writecache * wc,struct wc_entry * e)67948debafeSMikulas Patocka static void writecache_add_to_freelist(struct dm_writecache *wc, struct wc_entry *e)
68048debafeSMikulas Patocka {
68148debafeSMikulas Patocka 	if (WC_MODE_SORT_FREELIST(wc)) {
68248debafeSMikulas Patocka 		struct rb_node **node = &wc->freetree.rb_node, *parent = NULL;
6830ef0b471SHeinz Mauelshagen 
68448debafeSMikulas Patocka 		if (unlikely(!*node))
68548debafeSMikulas Patocka 			wc->current_free = e;
68648debafeSMikulas Patocka 		while (*node) {
68748debafeSMikulas Patocka 			parent = *node;
68848debafeSMikulas Patocka 			if (&e->rb_node < *node)
68948debafeSMikulas Patocka 				node = &parent->rb_left;
69048debafeSMikulas Patocka 			else
69148debafeSMikulas Patocka 				node = &parent->rb_right;
69248debafeSMikulas Patocka 		}
69348debafeSMikulas Patocka 		rb_link_node(&e->rb_node, parent, node);
69448debafeSMikulas Patocka 		rb_insert_color(&e->rb_node, &wc->freetree);
69548debafeSMikulas Patocka 	} else {
69648debafeSMikulas Patocka 		list_add_tail(&e->lru, &wc->freelist);
69748debafeSMikulas Patocka 	}
69848debafeSMikulas Patocka 	wc->freelist_size++;
69948debafeSMikulas Patocka }
70048debafeSMikulas Patocka 
writecache_verify_watermark(struct dm_writecache * wc)70141c526c5SMikulas Patocka static inline void writecache_verify_watermark(struct dm_writecache *wc)
70241c526c5SMikulas Patocka {
70341c526c5SMikulas Patocka 	if (unlikely(wc->freelist_size + wc->writeback_size <= wc->freelist_high_watermark))
70441c526c5SMikulas Patocka 		queue_work(wc->writeback_wq, &wc->writeback_work);
70541c526c5SMikulas Patocka }
70641c526c5SMikulas Patocka 
writecache_max_age_timer(struct timer_list * t)7073923d485SMikulas Patocka static void writecache_max_age_timer(struct timer_list *t)
7083923d485SMikulas Patocka {
7093923d485SMikulas Patocka 	struct dm_writecache *wc = from_timer(wc, t, max_age_timer);
7103923d485SMikulas Patocka 
7113923d485SMikulas Patocka 	if (!dm_suspended(wc->ti) && !writecache_has_error(wc)) {
7123923d485SMikulas Patocka 		queue_work(wc->writeback_wq, &wc->writeback_work);
7133923d485SMikulas Patocka 		mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
7143923d485SMikulas Patocka 	}
7153923d485SMikulas Patocka }
7163923d485SMikulas Patocka 
writecache_pop_from_freelist(struct dm_writecache * wc,sector_t expected_sector)717dcd19507SMikulas Patocka static struct wc_entry *writecache_pop_from_freelist(struct dm_writecache *wc, sector_t expected_sector)
71848debafeSMikulas Patocka {
71948debafeSMikulas Patocka 	struct wc_entry *e;
72048debafeSMikulas Patocka 
72148debafeSMikulas Patocka 	if (WC_MODE_SORT_FREELIST(wc)) {
72248debafeSMikulas Patocka 		struct rb_node *next;
7230ef0b471SHeinz Mauelshagen 
72448debafeSMikulas Patocka 		if (unlikely(!wc->current_free))
72548debafeSMikulas Patocka 			return NULL;
72648debafeSMikulas Patocka 		e = wc->current_free;
727dcd19507SMikulas Patocka 		if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
728dcd19507SMikulas Patocka 			return NULL;
72948debafeSMikulas Patocka 		next = rb_next(&e->rb_node);
73048debafeSMikulas Patocka 		rb_erase(&e->rb_node, &wc->freetree);
73148debafeSMikulas Patocka 		if (unlikely(!next))
73248debafeSMikulas Patocka 			next = rb_first(&wc->freetree);
73348debafeSMikulas Patocka 		wc->current_free = next ? container_of(next, struct wc_entry, rb_node) : NULL;
73448debafeSMikulas Patocka 	} else {
73548debafeSMikulas Patocka 		if (unlikely(list_empty(&wc->freelist)))
73648debafeSMikulas Patocka 			return NULL;
73748debafeSMikulas Patocka 		e = container_of(wc->freelist.next, struct wc_entry, lru);
738dcd19507SMikulas Patocka 		if (expected_sector != (sector_t)-1 && unlikely(cache_sector(wc, e) != expected_sector))
739dcd19507SMikulas Patocka 			return NULL;
74048debafeSMikulas Patocka 		list_del(&e->lru);
74148debafeSMikulas Patocka 	}
74248debafeSMikulas Patocka 	wc->freelist_size--;
74341c526c5SMikulas Patocka 
74441c526c5SMikulas Patocka 	writecache_verify_watermark(wc);
74548debafeSMikulas Patocka 
74648debafeSMikulas Patocka 	return e;
74748debafeSMikulas Patocka }
74848debafeSMikulas Patocka 
writecache_free_entry(struct dm_writecache * wc,struct wc_entry * e)74948debafeSMikulas Patocka static void writecache_free_entry(struct dm_writecache *wc, struct wc_entry *e)
75048debafeSMikulas Patocka {
75148debafeSMikulas Patocka 	writecache_unlink(wc, e);
75248debafeSMikulas Patocka 	writecache_add_to_freelist(wc, e);
75348debafeSMikulas Patocka 	clear_seq_count(wc, e);
75448debafeSMikulas Patocka 	writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
75548debafeSMikulas Patocka 	if (unlikely(waitqueue_active(&wc->freelist_wait)))
75648debafeSMikulas Patocka 		wake_up(&wc->freelist_wait);
75748debafeSMikulas Patocka }
75848debafeSMikulas Patocka 
writecache_wait_on_freelist(struct dm_writecache * wc)75948debafeSMikulas Patocka static void writecache_wait_on_freelist(struct dm_writecache *wc)
76048debafeSMikulas Patocka {
76148debafeSMikulas Patocka 	DEFINE_WAIT(wait);
76248debafeSMikulas Patocka 
76348debafeSMikulas Patocka 	prepare_to_wait(&wc->freelist_wait, &wait, TASK_UNINTERRUPTIBLE);
76448debafeSMikulas Patocka 	wc_unlock(wc);
76548debafeSMikulas Patocka 	io_schedule();
76648debafeSMikulas Patocka 	finish_wait(&wc->freelist_wait, &wait);
76748debafeSMikulas Patocka 	wc_lock(wc);
76848debafeSMikulas Patocka }
76948debafeSMikulas Patocka 
writecache_poison_lists(struct dm_writecache * wc)77048debafeSMikulas Patocka static void writecache_poison_lists(struct dm_writecache *wc)
77148debafeSMikulas Patocka {
77248debafeSMikulas Patocka 	/*
77348debafeSMikulas Patocka 	 * Catch incorrect access to these values while the device is suspended.
77448debafeSMikulas Patocka 	 */
7758d1058fbSHeinz Mauelshagen 	memset(&wc->tree, -1, sizeof(wc->tree));
77648debafeSMikulas Patocka 	wc->lru.next = LIST_POISON1;
77748debafeSMikulas Patocka 	wc->lru.prev = LIST_POISON2;
77848debafeSMikulas Patocka 	wc->freelist.next = LIST_POISON1;
77948debafeSMikulas Patocka 	wc->freelist.prev = LIST_POISON2;
78048debafeSMikulas Patocka }
78148debafeSMikulas Patocka 
writecache_flush_entry(struct dm_writecache * wc,struct wc_entry * e)78248debafeSMikulas Patocka static void writecache_flush_entry(struct dm_writecache *wc, struct wc_entry *e)
78348debafeSMikulas Patocka {
78448debafeSMikulas Patocka 	writecache_flush_region(wc, memory_entry(wc, e), sizeof(struct wc_memory_entry));
78548debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
78648debafeSMikulas Patocka 		writecache_flush_region(wc, memory_data(wc, e), wc->block_size);
78748debafeSMikulas Patocka }
78848debafeSMikulas Patocka 
writecache_entry_is_committed(struct dm_writecache * wc,struct wc_entry * e)78948debafeSMikulas Patocka static bool writecache_entry_is_committed(struct dm_writecache *wc, struct wc_entry *e)
79048debafeSMikulas Patocka {
79148debafeSMikulas Patocka 	return read_seq_count(wc, e) < wc->seq_count;
79248debafeSMikulas Patocka }
79348debafeSMikulas Patocka 
writecache_flush(struct dm_writecache * wc)79448debafeSMikulas Patocka static void writecache_flush(struct dm_writecache *wc)
79548debafeSMikulas Patocka {
79648debafeSMikulas Patocka 	struct wc_entry *e, *e2;
79748debafeSMikulas Patocka 	bool need_flush_after_free;
79848debafeSMikulas Patocka 
79948debafeSMikulas Patocka 	wc->uncommitted_blocks = 0;
80048debafeSMikulas Patocka 	del_timer(&wc->autocommit_timer);
80148debafeSMikulas Patocka 
80248debafeSMikulas Patocka 	if (list_empty(&wc->lru))
80348debafeSMikulas Patocka 		return;
80448debafeSMikulas Patocka 
80548debafeSMikulas Patocka 	e = container_of(wc->lru.next, struct wc_entry, lru);
80648debafeSMikulas Patocka 	if (writecache_entry_is_committed(wc, e)) {
80748debafeSMikulas Patocka 		if (wc->overwrote_committed) {
80848debafeSMikulas Patocka 			writecache_wait_for_ios(wc, WRITE);
80948debafeSMikulas Patocka 			writecache_disk_flush(wc, wc->ssd_dev);
81048debafeSMikulas Patocka 			wc->overwrote_committed = false;
81148debafeSMikulas Patocka 		}
81248debafeSMikulas Patocka 		return;
81348debafeSMikulas Patocka 	}
81448debafeSMikulas Patocka 	while (1) {
81548debafeSMikulas Patocka 		writecache_flush_entry(wc, e);
81648debafeSMikulas Patocka 		if (unlikely(e->lru.next == &wc->lru))
81748debafeSMikulas Patocka 			break;
81848debafeSMikulas Patocka 		e2 = container_of(e->lru.next, struct wc_entry, lru);
81948debafeSMikulas Patocka 		if (writecache_entry_is_committed(wc, e2))
82048debafeSMikulas Patocka 			break;
82148debafeSMikulas Patocka 		e = e2;
82248debafeSMikulas Patocka 		cond_resched();
82348debafeSMikulas Patocka 	}
824aa950920SMikulas Patocka 	writecache_commit_flushed(wc, true);
82548debafeSMikulas Patocka 
82648debafeSMikulas Patocka 	wc->seq_count++;
82748debafeSMikulas Patocka 	pmem_assign(sb(wc)->seq_count, cpu_to_le64(wc->seq_count));
828dc8a01aeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
829aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
830dc8a01aeSMikulas Patocka 	else
831dc8a01aeSMikulas Patocka 		ssd_commit_superblock(wc);
83248debafeSMikulas Patocka 
83348debafeSMikulas Patocka 	wc->overwrote_committed = false;
83448debafeSMikulas Patocka 
83548debafeSMikulas Patocka 	need_flush_after_free = false;
83648debafeSMikulas Patocka 	while (1) {
83748debafeSMikulas Patocka 		/* Free another committed entry with lower seq-count */
83848debafeSMikulas Patocka 		struct rb_node *rb_node = rb_prev(&e->rb_node);
83948debafeSMikulas Patocka 
84048debafeSMikulas Patocka 		if (rb_node) {
84148debafeSMikulas Patocka 			e2 = container_of(rb_node, struct wc_entry, rb_node);
84248debafeSMikulas Patocka 			if (read_original_sector(wc, e2) == read_original_sector(wc, e) &&
84348debafeSMikulas Patocka 			    likely(!e2->write_in_progress)) {
84448debafeSMikulas Patocka 				writecache_free_entry(wc, e2);
84548debafeSMikulas Patocka 				need_flush_after_free = true;
84648debafeSMikulas Patocka 			}
84748debafeSMikulas Patocka 		}
84848debafeSMikulas Patocka 		if (unlikely(e->lru.prev == &wc->lru))
84948debafeSMikulas Patocka 			break;
85048debafeSMikulas Patocka 		e = container_of(e->lru.prev, struct wc_entry, lru);
85148debafeSMikulas Patocka 		cond_resched();
85248debafeSMikulas Patocka 	}
85348debafeSMikulas Patocka 
85448debafeSMikulas Patocka 	if (need_flush_after_free)
855aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
85648debafeSMikulas Patocka }
85748debafeSMikulas Patocka 
writecache_flush_work(struct work_struct * work)85848debafeSMikulas Patocka static void writecache_flush_work(struct work_struct *work)
85948debafeSMikulas Patocka {
86048debafeSMikulas Patocka 	struct dm_writecache *wc = container_of(work, struct dm_writecache, flush_work);
86148debafeSMikulas Patocka 
86248debafeSMikulas Patocka 	wc_lock(wc);
86348debafeSMikulas Patocka 	writecache_flush(wc);
86448debafeSMikulas Patocka 	wc_unlock(wc);
86548debafeSMikulas Patocka }
86648debafeSMikulas Patocka 
writecache_autocommit_timer(struct timer_list * t)86748debafeSMikulas Patocka static void writecache_autocommit_timer(struct timer_list *t)
86848debafeSMikulas Patocka {
86948debafeSMikulas Patocka 	struct dm_writecache *wc = from_timer(wc, t, autocommit_timer);
8700ef0b471SHeinz Mauelshagen 
87148debafeSMikulas Patocka 	if (!writecache_has_error(wc))
87248debafeSMikulas Patocka 		queue_work(wc->writeback_wq, &wc->flush_work);
87348debafeSMikulas Patocka }
87448debafeSMikulas Patocka 
writecache_schedule_autocommit(struct dm_writecache * wc)87548debafeSMikulas Patocka static void writecache_schedule_autocommit(struct dm_writecache *wc)
87648debafeSMikulas Patocka {
87748debafeSMikulas Patocka 	if (!timer_pending(&wc->autocommit_timer))
87848debafeSMikulas Patocka 		mod_timer(&wc->autocommit_timer, jiffies + wc->autocommit_jiffies);
87948debafeSMikulas Patocka }
88048debafeSMikulas Patocka 
writecache_discard(struct dm_writecache * wc,sector_t start,sector_t end)88148debafeSMikulas Patocka static void writecache_discard(struct dm_writecache *wc, sector_t start, sector_t end)
88248debafeSMikulas Patocka {
88348debafeSMikulas Patocka 	struct wc_entry *e;
88448debafeSMikulas Patocka 	bool discarded_something = false;
88548debafeSMikulas Patocka 
88648debafeSMikulas Patocka 	e = writecache_find_entry(wc, start, WFE_RETURN_FOLLOWING | WFE_LOWEST_SEQ);
88748debafeSMikulas Patocka 	if (unlikely(!e))
88848debafeSMikulas Patocka 		return;
88948debafeSMikulas Patocka 
89048debafeSMikulas Patocka 	while (read_original_sector(wc, e) < end) {
89148debafeSMikulas Patocka 		struct rb_node *node = rb_next(&e->rb_node);
89248debafeSMikulas Patocka 
89348debafeSMikulas Patocka 		if (likely(!e->write_in_progress)) {
89448debafeSMikulas Patocka 			if (!discarded_something) {
895a143e172SHuaisheng Ye 				if (!WC_MODE_PMEM(wc)) {
89648debafeSMikulas Patocka 					writecache_wait_for_ios(wc, READ);
89748debafeSMikulas Patocka 					writecache_wait_for_ios(wc, WRITE);
898a143e172SHuaisheng Ye 				}
89948debafeSMikulas Patocka 				discarded_something = true;
90048debafeSMikulas Patocka 			}
90139495b12SHuaisheng Ye 			if (!writecache_entry_is_committed(wc, e))
90239495b12SHuaisheng Ye 				wc->uncommitted_blocks--;
90348debafeSMikulas Patocka 			writecache_free_entry(wc, e);
90448debafeSMikulas Patocka 		}
90548debafeSMikulas Patocka 
90684420b1eSHuaisheng Ye 		if (unlikely(!node))
90748debafeSMikulas Patocka 			break;
90848debafeSMikulas Patocka 
90948debafeSMikulas Patocka 		e = container_of(node, struct wc_entry, rb_node);
91048debafeSMikulas Patocka 	}
91148debafeSMikulas Patocka 
91248debafeSMikulas Patocka 	if (discarded_something)
913aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
91448debafeSMikulas Patocka }
91548debafeSMikulas Patocka 
writecache_wait_for_writeback(struct dm_writecache * wc)91648debafeSMikulas Patocka static bool writecache_wait_for_writeback(struct dm_writecache *wc)
91748debafeSMikulas Patocka {
91848debafeSMikulas Patocka 	if (wc->writeback_size) {
91948debafeSMikulas Patocka 		writecache_wait_on_freelist(wc);
92048debafeSMikulas Patocka 		return true;
92148debafeSMikulas Patocka 	}
92248debafeSMikulas Patocka 	return false;
92348debafeSMikulas Patocka }
92448debafeSMikulas Patocka 
writecache_suspend(struct dm_target * ti)92548debafeSMikulas Patocka static void writecache_suspend(struct dm_target *ti)
92648debafeSMikulas Patocka {
92748debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
92848debafeSMikulas Patocka 	bool flush_on_suspend;
92948debafeSMikulas Patocka 
93048debafeSMikulas Patocka 	del_timer_sync(&wc->autocommit_timer);
9313923d485SMikulas Patocka 	del_timer_sync(&wc->max_age_timer);
93248debafeSMikulas Patocka 
93348debafeSMikulas Patocka 	wc_lock(wc);
93448debafeSMikulas Patocka 	writecache_flush(wc);
93548debafeSMikulas Patocka 	flush_on_suspend = wc->flush_on_suspend;
93648debafeSMikulas Patocka 	if (flush_on_suspend) {
93748debafeSMikulas Patocka 		wc->flush_on_suspend = false;
93848debafeSMikulas Patocka 		wc->writeback_all++;
93948debafeSMikulas Patocka 		queue_work(wc->writeback_wq, &wc->writeback_work);
94048debafeSMikulas Patocka 	}
94148debafeSMikulas Patocka 	wc_unlock(wc);
94248debafeSMikulas Patocka 
943adc0daadSMikulas Patocka 	drain_workqueue(wc->writeback_wq);
94448debafeSMikulas Patocka 
94548debafeSMikulas Patocka 	wc_lock(wc);
94648debafeSMikulas Patocka 	if (flush_on_suspend)
94748debafeSMikulas Patocka 		wc->writeback_all--;
94803b18887SHeinz Mauelshagen 	while (writecache_wait_for_writeback(wc))
94903b18887SHeinz Mauelshagen 		;
95048debafeSMikulas Patocka 
95148debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
95248debafeSMikulas Patocka 		persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
95348debafeSMikulas Patocka 
95448debafeSMikulas Patocka 	writecache_poison_lists(wc);
95548debafeSMikulas Patocka 
95648debafeSMikulas Patocka 	wc_unlock(wc);
95748debafeSMikulas Patocka }
95848debafeSMikulas Patocka 
writecache_alloc_entries(struct dm_writecache * wc)95948debafeSMikulas Patocka static int writecache_alloc_entries(struct dm_writecache *wc)
96048debafeSMikulas Patocka {
96148debafeSMikulas Patocka 	size_t b;
96248debafeSMikulas Patocka 
96348debafeSMikulas Patocka 	if (wc->entries)
96448debafeSMikulas Patocka 		return 0;
96540ef8756SMikulas Patocka 	wc->entries = vmalloc_array(wc->n_blocks, sizeof(struct wc_entry));
96648debafeSMikulas Patocka 	if (!wc->entries)
96748debafeSMikulas Patocka 		return -ENOMEM;
96848debafeSMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
96948debafeSMikulas Patocka 		struct wc_entry *e = &wc->entries[b];
9700ef0b471SHeinz Mauelshagen 
97148debafeSMikulas Patocka 		e->index = b;
97248debafeSMikulas Patocka 		e->write_in_progress = false;
9731edaa447SMikulas Patocka 		cond_resched();
97448debafeSMikulas Patocka 	}
97548debafeSMikulas Patocka 
97648debafeSMikulas Patocka 	return 0;
97748debafeSMikulas Patocka }
97848debafeSMikulas Patocka 
writecache_read_metadata(struct dm_writecache * wc,sector_t n_sectors)97931b22120SMikulas Patocka static int writecache_read_metadata(struct dm_writecache *wc, sector_t n_sectors)
98031b22120SMikulas Patocka {
98131b22120SMikulas Patocka 	struct dm_io_region region;
98231b22120SMikulas Patocka 	struct dm_io_request req;
98331b22120SMikulas Patocka 
98431b22120SMikulas Patocka 	region.bdev = wc->ssd_dev->bdev;
98531b22120SMikulas Patocka 	region.sector = wc->start_sector;
98631b22120SMikulas Patocka 	region.count = n_sectors;
987581075e4SBart Van Assche 	req.bi_opf = REQ_OP_READ | REQ_SYNC;
98831b22120SMikulas Patocka 	req.mem.type = DM_IO_VMA;
98931b22120SMikulas Patocka 	req.mem.ptr.vma = (char *)wc->memory_map;
99031b22120SMikulas Patocka 	req.client = wc->dm_io;
99131b22120SMikulas Patocka 	req.notify.fn = NULL;
99231b22120SMikulas Patocka 
9936e5f0f63SHongyu Jin 	return dm_io(&req, 1, &region, NULL, IOPRIO_DEFAULT);
99431b22120SMikulas Patocka }
99531b22120SMikulas Patocka 
writecache_resume(struct dm_target * ti)99648debafeSMikulas Patocka static void writecache_resume(struct dm_target *ti)
99748debafeSMikulas Patocka {
99848debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
99948debafeSMikulas Patocka 	size_t b;
100048debafeSMikulas Patocka 	bool need_flush = false;
100148debafeSMikulas Patocka 	__le64 sb_seq_count;
100248debafeSMikulas Patocka 	int r;
100348debafeSMikulas Patocka 
100448debafeSMikulas Patocka 	wc_lock(wc);
100548debafeSMikulas Patocka 
1006d9928ac5SMike Snitzer 	wc->data_device_sectors = bdev_nr_sectors(wc->dev->bdev);
10074134455fSMikulas Patocka 
100831b22120SMikulas Patocka 	if (WC_MODE_PMEM(wc)) {
100948debafeSMikulas Patocka 		persistent_memory_invalidate_cache(wc->memory_map, wc->memory_map_size);
101031b22120SMikulas Patocka 	} else {
101131b22120SMikulas Patocka 		r = writecache_read_metadata(wc, wc->metadata_sectors);
101231b22120SMikulas Patocka 		if (r) {
101331b22120SMikulas Patocka 			size_t sb_entries_offset;
10140ef0b471SHeinz Mauelshagen 
101531b22120SMikulas Patocka 			writecache_error(wc, r, "unable to read metadata: %d", r);
101631b22120SMikulas Patocka 			sb_entries_offset = offsetof(struct wc_memory_superblock, entries);
101731b22120SMikulas Patocka 			memset((char *)wc->memory_map + sb_entries_offset, -1,
101831b22120SMikulas Patocka 			       (wc->metadata_sectors << SECTOR_SHIFT) - sb_entries_offset);
101931b22120SMikulas Patocka 		}
102031b22120SMikulas Patocka 	}
102148debafeSMikulas Patocka 
102248debafeSMikulas Patocka 	wc->tree = RB_ROOT;
102348debafeSMikulas Patocka 	INIT_LIST_HEAD(&wc->lru);
102448debafeSMikulas Patocka 	if (WC_MODE_SORT_FREELIST(wc)) {
102548debafeSMikulas Patocka 		wc->freetree = RB_ROOT;
102648debafeSMikulas Patocka 		wc->current_free = NULL;
102748debafeSMikulas Patocka 	} else {
102848debafeSMikulas Patocka 		INIT_LIST_HEAD(&wc->freelist);
102948debafeSMikulas Patocka 	}
103048debafeSMikulas Patocka 	wc->freelist_size = 0;
103148debafeSMikulas Patocka 
1032ec6347bbSDan Williams 	r = copy_mc_to_kernel(&sb_seq_count, &sb(wc)->seq_count,
1033ec6347bbSDan Williams 			      sizeof(uint64_t));
103448debafeSMikulas Patocka 	if (r) {
103548debafeSMikulas Patocka 		writecache_error(wc, r, "hardware memory error when reading superblock: %d", r);
103648debafeSMikulas Patocka 		sb_seq_count = cpu_to_le64(0);
103748debafeSMikulas Patocka 	}
103848debafeSMikulas Patocka 	wc->seq_count = le64_to_cpu(sb_seq_count);
103948debafeSMikulas Patocka 
104048debafeSMikulas Patocka #ifdef DM_WRITECACHE_HANDLE_HARDWARE_ERRORS
104148debafeSMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
104248debafeSMikulas Patocka 		struct wc_entry *e = &wc->entries[b];
104348debafeSMikulas Patocka 		struct wc_memory_entry wme;
10440ef0b471SHeinz Mauelshagen 
104548debafeSMikulas Patocka 		if (writecache_has_error(wc)) {
104648debafeSMikulas Patocka 			e->original_sector = -1;
104748debafeSMikulas Patocka 			e->seq_count = -1;
104848debafeSMikulas Patocka 			continue;
104948debafeSMikulas Patocka 		}
1050ec6347bbSDan Williams 		r = copy_mc_to_kernel(&wme, memory_entry(wc, e),
1051ec6347bbSDan Williams 				      sizeof(struct wc_memory_entry));
105248debafeSMikulas Patocka 		if (r) {
105348debafeSMikulas Patocka 			writecache_error(wc, r, "hardware memory error when reading metadata entry %lu: %d",
105448debafeSMikulas Patocka 					 (unsigned long)b, r);
105548debafeSMikulas Patocka 			e->original_sector = -1;
105648debafeSMikulas Patocka 			e->seq_count = -1;
105748debafeSMikulas Patocka 		} else {
105848debafeSMikulas Patocka 			e->original_sector = le64_to_cpu(wme.original_sector);
105948debafeSMikulas Patocka 			e->seq_count = le64_to_cpu(wme.seq_count);
106048debafeSMikulas Patocka 		}
10611edaa447SMikulas Patocka 		cond_resched();
106248debafeSMikulas Patocka 	}
106348debafeSMikulas Patocka #endif
106448debafeSMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
106548debafeSMikulas Patocka 		struct wc_entry *e = &wc->entries[b];
10660ef0b471SHeinz Mauelshagen 
106748debafeSMikulas Patocka 		if (!writecache_entry_is_committed(wc, e)) {
106848debafeSMikulas Patocka 			if (read_seq_count(wc, e) != -1) {
106948debafeSMikulas Patocka erase_this:
107048debafeSMikulas Patocka 				clear_seq_count(wc, e);
107148debafeSMikulas Patocka 				need_flush = true;
107248debafeSMikulas Patocka 			}
107348debafeSMikulas Patocka 			writecache_add_to_freelist(wc, e);
107448debafeSMikulas Patocka 		} else {
107548debafeSMikulas Patocka 			struct wc_entry *old;
107648debafeSMikulas Patocka 
107748debafeSMikulas Patocka 			old = writecache_find_entry(wc, read_original_sector(wc, e), 0);
107848debafeSMikulas Patocka 			if (!old) {
107948debafeSMikulas Patocka 				writecache_insert_entry(wc, e);
108048debafeSMikulas Patocka 			} else {
108148debafeSMikulas Patocka 				if (read_seq_count(wc, old) == read_seq_count(wc, e)) {
108248debafeSMikulas Patocka 					writecache_error(wc, -EINVAL,
108348debafeSMikulas Patocka 						 "two identical entries, position %llu, sector %llu, sequence %llu",
108448debafeSMikulas Patocka 						 (unsigned long long)b, (unsigned long long)read_original_sector(wc, e),
108548debafeSMikulas Patocka 						 (unsigned long long)read_seq_count(wc, e));
108648debafeSMikulas Patocka 				}
108748debafeSMikulas Patocka 				if (read_seq_count(wc, old) > read_seq_count(wc, e)) {
108848debafeSMikulas Patocka 					goto erase_this;
108948debafeSMikulas Patocka 				} else {
109048debafeSMikulas Patocka 					writecache_free_entry(wc, old);
109148debafeSMikulas Patocka 					writecache_insert_entry(wc, e);
109248debafeSMikulas Patocka 					need_flush = true;
109348debafeSMikulas Patocka 				}
109448debafeSMikulas Patocka 			}
109548debafeSMikulas Patocka 		}
109648debafeSMikulas Patocka 		cond_resched();
109748debafeSMikulas Patocka 	}
109848debafeSMikulas Patocka 
109948debafeSMikulas Patocka 	if (need_flush) {
110048debafeSMikulas Patocka 		writecache_flush_all_metadata(wc);
1101aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
110248debafeSMikulas Patocka 	}
110348debafeSMikulas Patocka 
110441c526c5SMikulas Patocka 	writecache_verify_watermark(wc);
110541c526c5SMikulas Patocka 
11063923d485SMikulas Patocka 	if (wc->max_age != MAX_AGE_UNSPECIFIED)
11073923d485SMikulas Patocka 		mod_timer(&wc->max_age_timer, jiffies + wc->max_age / MAX_AGE_DIV);
11083923d485SMikulas Patocka 
110948debafeSMikulas Patocka 	wc_unlock(wc);
111048debafeSMikulas Patocka }
111148debafeSMikulas Patocka 
process_flush_mesg(unsigned int argc,char ** argv,struct dm_writecache * wc)111286a3238cSHeinz Mauelshagen static int process_flush_mesg(unsigned int argc, char **argv, struct dm_writecache *wc)
111348debafeSMikulas Patocka {
111448debafeSMikulas Patocka 	if (argc != 1)
111548debafeSMikulas Patocka 		return -EINVAL;
111648debafeSMikulas Patocka 
111748debafeSMikulas Patocka 	wc_lock(wc);
111848debafeSMikulas Patocka 	if (dm_suspended(wc->ti)) {
111948debafeSMikulas Patocka 		wc_unlock(wc);
112048debafeSMikulas Patocka 		return -EBUSY;
112148debafeSMikulas Patocka 	}
112248debafeSMikulas Patocka 	if (writecache_has_error(wc)) {
112348debafeSMikulas Patocka 		wc_unlock(wc);
112448debafeSMikulas Patocka 		return -EIO;
112548debafeSMikulas Patocka 	}
112648debafeSMikulas Patocka 
112748debafeSMikulas Patocka 	writecache_flush(wc);
112848debafeSMikulas Patocka 	wc->writeback_all++;
112948debafeSMikulas Patocka 	queue_work(wc->writeback_wq, &wc->writeback_work);
113048debafeSMikulas Patocka 	wc_unlock(wc);
113148debafeSMikulas Patocka 
113248debafeSMikulas Patocka 	flush_workqueue(wc->writeback_wq);
113348debafeSMikulas Patocka 
113448debafeSMikulas Patocka 	wc_lock(wc);
113548debafeSMikulas Patocka 	wc->writeback_all--;
113648debafeSMikulas Patocka 	if (writecache_has_error(wc)) {
113748debafeSMikulas Patocka 		wc_unlock(wc);
113848debafeSMikulas Patocka 		return -EIO;
113948debafeSMikulas Patocka 	}
114048debafeSMikulas Patocka 	wc_unlock(wc);
114148debafeSMikulas Patocka 
114248debafeSMikulas Patocka 	return 0;
114348debafeSMikulas Patocka }
114448debafeSMikulas Patocka 
process_flush_on_suspend_mesg(unsigned int argc,char ** argv,struct dm_writecache * wc)114586a3238cSHeinz Mauelshagen static int process_flush_on_suspend_mesg(unsigned int argc, char **argv, struct dm_writecache *wc)
114648debafeSMikulas Patocka {
114748debafeSMikulas Patocka 	if (argc != 1)
114848debafeSMikulas Patocka 		return -EINVAL;
114948debafeSMikulas Patocka 
115048debafeSMikulas Patocka 	wc_lock(wc);
115148debafeSMikulas Patocka 	wc->flush_on_suspend = true;
115248debafeSMikulas Patocka 	wc_unlock(wc);
115348debafeSMikulas Patocka 
115448debafeSMikulas Patocka 	return 0;
115548debafeSMikulas Patocka }
115648debafeSMikulas Patocka 
activate_cleaner(struct dm_writecache * wc)115793de44ebSMikulas Patocka static void activate_cleaner(struct dm_writecache *wc)
115893de44ebSMikulas Patocka {
115993de44ebSMikulas Patocka 	wc->flush_on_suspend = true;
116093de44ebSMikulas Patocka 	wc->cleaner = true;
116193de44ebSMikulas Patocka 	wc->freelist_high_watermark = wc->n_blocks;
116293de44ebSMikulas Patocka 	wc->freelist_low_watermark = wc->n_blocks;
116393de44ebSMikulas Patocka }
116493de44ebSMikulas Patocka 
process_cleaner_mesg(unsigned int argc,char ** argv,struct dm_writecache * wc)116586a3238cSHeinz Mauelshagen static int process_cleaner_mesg(unsigned int argc, char **argv, struct dm_writecache *wc)
116693de44ebSMikulas Patocka {
116793de44ebSMikulas Patocka 	if (argc != 1)
116893de44ebSMikulas Patocka 		return -EINVAL;
116993de44ebSMikulas Patocka 
117093de44ebSMikulas Patocka 	wc_lock(wc);
117193de44ebSMikulas Patocka 	activate_cleaner(wc);
117293de44ebSMikulas Patocka 	if (!dm_suspended(wc->ti))
117393de44ebSMikulas Patocka 		writecache_verify_watermark(wc);
117493de44ebSMikulas Patocka 	wc_unlock(wc);
117593de44ebSMikulas Patocka 
117693de44ebSMikulas Patocka 	return 0;
117793de44ebSMikulas Patocka }
117893de44ebSMikulas Patocka 
process_clear_stats_mesg(unsigned int argc,char ** argv,struct dm_writecache * wc)117986a3238cSHeinz Mauelshagen static int process_clear_stats_mesg(unsigned int argc, char **argv, struct dm_writecache *wc)
1180e3a35d03SMikulas Patocka {
1181e3a35d03SMikulas Patocka 	if (argc != 1)
1182e3a35d03SMikulas Patocka 		return -EINVAL;
1183e3a35d03SMikulas Patocka 
1184e3a35d03SMikulas Patocka 	wc_lock(wc);
11858d1058fbSHeinz Mauelshagen 	memset(&wc->stats, 0, sizeof(wc->stats));
1186e3a35d03SMikulas Patocka 	wc_unlock(wc);
1187e3a35d03SMikulas Patocka 
1188e3a35d03SMikulas Patocka 	return 0;
1189e3a35d03SMikulas Patocka }
1190e3a35d03SMikulas Patocka 
writecache_message(struct dm_target * ti,unsigned int argc,char ** argv,char * result,unsigned int maxlen)119186a3238cSHeinz Mauelshagen static int writecache_message(struct dm_target *ti, unsigned int argc, char **argv,
119286a3238cSHeinz Mauelshagen 			      char *result, unsigned int maxlen)
119348debafeSMikulas Patocka {
119448debafeSMikulas Patocka 	int r = -EINVAL;
119548debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
119648debafeSMikulas Patocka 
119748debafeSMikulas Patocka 	if (!strcasecmp(argv[0], "flush"))
119848debafeSMikulas Patocka 		r = process_flush_mesg(argc, argv, wc);
119948debafeSMikulas Patocka 	else if (!strcasecmp(argv[0], "flush_on_suspend"))
120048debafeSMikulas Patocka 		r = process_flush_on_suspend_mesg(argc, argv, wc);
120193de44ebSMikulas Patocka 	else if (!strcasecmp(argv[0], "cleaner"))
120293de44ebSMikulas Patocka 		r = process_cleaner_mesg(argc, argv, wc);
1203e3a35d03SMikulas Patocka 	else if (!strcasecmp(argv[0], "clear_stats"))
1204e3a35d03SMikulas Patocka 		r = process_clear_stats_mesg(argc, argv, wc);
120548debafeSMikulas Patocka 	else
120648debafeSMikulas Patocka 		DMERR("unrecognised message received: %s", argv[0]);
120748debafeSMikulas Patocka 
120848debafeSMikulas Patocka 	return r;
120948debafeSMikulas Patocka }
121048debafeSMikulas Patocka 
memcpy_flushcache_optimized(void * dest,void * source,size_t size)121148338daaSMikulas Patocka static void memcpy_flushcache_optimized(void *dest, void *source, size_t size)
121248338daaSMikulas Patocka {
121348338daaSMikulas Patocka 	/*
121448338daaSMikulas Patocka 	 * clflushopt performs better with block size 1024, 2048, 4096
121548338daaSMikulas Patocka 	 * non-temporal stores perform better with block size 512
121648338daaSMikulas Patocka 	 *
121748338daaSMikulas Patocka 	 * block size   512             1024            2048            4096
121848338daaSMikulas Patocka 	 * movnti       496 MB/s        642 MB/s        725 MB/s        744 MB/s
121948338daaSMikulas Patocka 	 * clflushopt   373 MB/s        688 MB/s        1.1 GB/s        1.2 GB/s
122048338daaSMikulas Patocka 	 *
122148338daaSMikulas Patocka 	 * We see that movnti performs better for 512-byte blocks, and
122248338daaSMikulas Patocka 	 * clflushopt performs better for 1024-byte and larger blocks. So, we
122348338daaSMikulas Patocka 	 * prefer clflushopt for sizes >= 768.
122448338daaSMikulas Patocka 	 *
122548338daaSMikulas Patocka 	 * NOTE: this happens to be the case now (with dm-writecache's single
122648338daaSMikulas Patocka 	 * threaded model) but re-evaluate this once memcpy_flushcache() is
122748338daaSMikulas Patocka 	 * enabled to use movdir64b which might invalidate this performance
122848338daaSMikulas Patocka 	 * advantage seen with cache-allocating-writes plus flushing.
122948338daaSMikulas Patocka 	 */
123048338daaSMikulas Patocka #ifdef CONFIG_X86
123148338daaSMikulas Patocka 	if (static_cpu_has(X86_FEATURE_CLFLUSHOPT) &&
123248338daaSMikulas Patocka 	    likely(boot_cpu_data.x86_clflush_size == 64) &&
123348338daaSMikulas Patocka 	    likely(size >= 768)) {
123448338daaSMikulas Patocka 		do {
123548338daaSMikulas Patocka 			memcpy((void *)dest, (void *)source, 64);
123648338daaSMikulas Patocka 			clflushopt((void *)dest);
123748338daaSMikulas Patocka 			dest += 64;
123848338daaSMikulas Patocka 			source += 64;
123948338daaSMikulas Patocka 			size -= 64;
124048338daaSMikulas Patocka 		} while (size >= 64);
124148338daaSMikulas Patocka 		return;
124248338daaSMikulas Patocka 	}
124348338daaSMikulas Patocka #endif
124448338daaSMikulas Patocka 	memcpy_flushcache(dest, source, size);
124548338daaSMikulas Patocka }
124648338daaSMikulas Patocka 
bio_copy_block(struct dm_writecache * wc,struct bio * bio,void * data)124748debafeSMikulas Patocka static void bio_copy_block(struct dm_writecache *wc, struct bio *bio, void *data)
124848debafeSMikulas Patocka {
124948debafeSMikulas Patocka 	void *buf;
125086a3238cSHeinz Mauelshagen 	unsigned int size;
125148debafeSMikulas Patocka 	int rw = bio_data_dir(bio);
125286a3238cSHeinz Mauelshagen 	unsigned int remaining_size = wc->block_size;
125348debafeSMikulas Patocka 
125448debafeSMikulas Patocka 	do {
125548debafeSMikulas Patocka 		struct bio_vec bv = bio_iter_iovec(bio, bio->bi_iter);
12560ef0b471SHeinz Mauelshagen 
125718a6234cSChristoph Hellwig 		buf = bvec_kmap_local(&bv);
125848debafeSMikulas Patocka 		size = bv.bv_len;
125948debafeSMikulas Patocka 		if (unlikely(size > remaining_size))
126048debafeSMikulas Patocka 			size = remaining_size;
126148debafeSMikulas Patocka 
126248debafeSMikulas Patocka 		if (rw == READ) {
126348debafeSMikulas Patocka 			int r;
12640ef0b471SHeinz Mauelshagen 
1265ec6347bbSDan Williams 			r = copy_mc_to_kernel(buf, data, size);
126648debafeSMikulas Patocka 			flush_dcache_page(bio_page(bio));
126748debafeSMikulas Patocka 			if (unlikely(r)) {
126848debafeSMikulas Patocka 				writecache_error(wc, r, "hardware memory error when reading data: %d", r);
126948debafeSMikulas Patocka 				bio->bi_status = BLK_STS_IOERR;
127048debafeSMikulas Patocka 			}
127148debafeSMikulas Patocka 		} else {
127248debafeSMikulas Patocka 			flush_dcache_page(bio_page(bio));
127348338daaSMikulas Patocka 			memcpy_flushcache_optimized(data, buf, size);
127448debafeSMikulas Patocka 		}
127548debafeSMikulas Patocka 
127618a6234cSChristoph Hellwig 		kunmap_local(buf);
127748debafeSMikulas Patocka 
127848debafeSMikulas Patocka 		data = (char *)data + size;
127948debafeSMikulas Patocka 		remaining_size -= size;
128048debafeSMikulas Patocka 		bio_advance(bio, size);
128148debafeSMikulas Patocka 	} while (unlikely(remaining_size));
128248debafeSMikulas Patocka }
128348debafeSMikulas Patocka 
writecache_flush_thread(void * data)128448debafeSMikulas Patocka static int writecache_flush_thread(void *data)
128548debafeSMikulas Patocka {
128648debafeSMikulas Patocka 	struct dm_writecache *wc = data;
128748debafeSMikulas Patocka 
128848debafeSMikulas Patocka 	while (1) {
128948debafeSMikulas Patocka 		struct bio *bio;
129048debafeSMikulas Patocka 
129148debafeSMikulas Patocka 		wc_lock(wc);
129248debafeSMikulas Patocka 		bio = bio_list_pop(&wc->flush_list);
129348debafeSMikulas Patocka 		if (!bio) {
129448debafeSMikulas Patocka 			set_current_state(TASK_INTERRUPTIBLE);
129548debafeSMikulas Patocka 			wc_unlock(wc);
129648debafeSMikulas Patocka 
129748debafeSMikulas Patocka 			if (unlikely(kthread_should_stop())) {
129848debafeSMikulas Patocka 				set_current_state(TASK_RUNNING);
129948debafeSMikulas Patocka 				break;
130048debafeSMikulas Patocka 			}
130148debafeSMikulas Patocka 
130248debafeSMikulas Patocka 			schedule();
130348debafeSMikulas Patocka 			continue;
130448debafeSMikulas Patocka 		}
130548debafeSMikulas Patocka 
130648debafeSMikulas Patocka 		if (bio_op(bio) == REQ_OP_DISCARD) {
130748debafeSMikulas Patocka 			writecache_discard(wc, bio->bi_iter.bi_sector,
130848debafeSMikulas Patocka 					   bio_end_sector(bio));
130948debafeSMikulas Patocka 			wc_unlock(wc);
131048debafeSMikulas Patocka 			bio_set_dev(bio, wc->dev->bdev);
1311ed00aabdSChristoph Hellwig 			submit_bio_noacct(bio);
131248debafeSMikulas Patocka 		} else {
131348debafeSMikulas Patocka 			writecache_flush(wc);
131448debafeSMikulas Patocka 			wc_unlock(wc);
131548debafeSMikulas Patocka 			if (writecache_has_error(wc))
131648debafeSMikulas Patocka 				bio->bi_status = BLK_STS_IOERR;
131748debafeSMikulas Patocka 			bio_endio(bio);
131848debafeSMikulas Patocka 		}
131948debafeSMikulas Patocka 	}
132048debafeSMikulas Patocka 
132148debafeSMikulas Patocka 	return 0;
132248debafeSMikulas Patocka }
132348debafeSMikulas Patocka 
writecache_offload_bio(struct dm_writecache * wc,struct bio * bio)132448debafeSMikulas Patocka static void writecache_offload_bio(struct dm_writecache *wc, struct bio *bio)
132548debafeSMikulas Patocka {
132648debafeSMikulas Patocka 	if (bio_list_empty(&wc->flush_list))
132748debafeSMikulas Patocka 		wake_up_process(wc->flush_thread);
132848debafeSMikulas Patocka 	bio_list_add(&wc->flush_list, bio);
132948debafeSMikulas Patocka }
133048debafeSMikulas Patocka 
1331cdd4d783SMike Snitzer enum wc_map_op {
1332cdd4d783SMike Snitzer 	WC_MAP_SUBMIT,
1333cdd4d783SMike Snitzer 	WC_MAP_REMAP,
1334cdd4d783SMike Snitzer 	WC_MAP_REMAP_ORIGIN,
1335cdd4d783SMike Snitzer 	WC_MAP_RETURN,
1336cdd4d783SMike Snitzer 	WC_MAP_ERROR,
1337cdd4d783SMike Snitzer };
1338cdd4d783SMike Snitzer 
writecache_map_remap_origin(struct dm_writecache * wc,struct bio * bio,struct wc_entry * e)13399bc0c92eSMikulas Patocka static void writecache_map_remap_origin(struct dm_writecache *wc, struct bio *bio,
13404d020b3aSMike Snitzer 					struct wc_entry *e)
13414d020b3aSMike Snitzer {
13424d020b3aSMike Snitzer 	if (e) {
13434d020b3aSMike Snitzer 		sector_t next_boundary =
13444d020b3aSMike Snitzer 			read_original_sector(wc, e) - bio->bi_iter.bi_sector;
13454d020b3aSMike Snitzer 		if (next_boundary < bio->bi_iter.bi_size >> SECTOR_SHIFT)
13464d020b3aSMike Snitzer 			dm_accept_partial_bio(bio, next_boundary);
13474d020b3aSMike Snitzer 	}
13484d020b3aSMike Snitzer }
13494d020b3aSMike Snitzer 
writecache_map_read(struct dm_writecache * wc,struct bio * bio)1350cdd4d783SMike Snitzer static enum wc_map_op writecache_map_read(struct dm_writecache *wc, struct bio *bio)
1351cdd4d783SMike Snitzer {
1352cdd4d783SMike Snitzer 	enum wc_map_op map_op;
1353cdd4d783SMike Snitzer 	struct wc_entry *e;
1354cdd4d783SMike Snitzer 
1355cdd4d783SMike Snitzer read_next_block:
1356e3a35d03SMikulas Patocka 	wc->stats.reads++;
1357cdd4d783SMike Snitzer 	e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
1358cdd4d783SMike Snitzer 	if (e && read_original_sector(wc, e) == bio->bi_iter.bi_sector) {
1359e3a35d03SMikulas Patocka 		wc->stats.read_hits++;
1360cdd4d783SMike Snitzer 		if (WC_MODE_PMEM(wc)) {
1361cdd4d783SMike Snitzer 			bio_copy_block(wc, bio, memory_data(wc, e));
1362cdd4d783SMike Snitzer 			if (bio->bi_iter.bi_size)
1363cdd4d783SMike Snitzer 				goto read_next_block;
1364cdd4d783SMike Snitzer 			map_op = WC_MAP_SUBMIT;
1365cdd4d783SMike Snitzer 		} else {
1366cdd4d783SMike Snitzer 			dm_accept_partial_bio(bio, wc->block_size >> SECTOR_SHIFT);
1367cdd4d783SMike Snitzer 			bio_set_dev(bio, wc->ssd_dev->bdev);
1368cdd4d783SMike Snitzer 			bio->bi_iter.bi_sector = cache_sector(wc, e);
1369cdd4d783SMike Snitzer 			if (!writecache_entry_is_committed(wc, e))
1370cdd4d783SMike Snitzer 				writecache_wait_for_ios(wc, WRITE);
1371cdd4d783SMike Snitzer 			map_op = WC_MAP_REMAP;
1372cdd4d783SMike Snitzer 		}
1373cdd4d783SMike Snitzer 	} else {
13749bc0c92eSMikulas Patocka 		writecache_map_remap_origin(wc, bio, e);
13752c6e755bSMikulas Patocka 		wc->stats.reads += (bio->bi_iter.bi_size - wc->block_size) >> wc->block_size_bits;
13769bc0c92eSMikulas Patocka 		map_op = WC_MAP_REMAP_ORIGIN;
1377cdd4d783SMike Snitzer 	}
1378cdd4d783SMike Snitzer 
1379cdd4d783SMike Snitzer 	return map_op;
1380cdd4d783SMike Snitzer }
1381cdd4d783SMike Snitzer 
writecache_bio_copy_ssd(struct dm_writecache * wc,struct bio * bio,struct wc_entry * e,bool search_used)13829bc0c92eSMikulas Patocka static void writecache_bio_copy_ssd(struct dm_writecache *wc, struct bio *bio,
1383cdd4d783SMike Snitzer 				    struct wc_entry *e, bool search_used)
1384cdd4d783SMike Snitzer {
138586a3238cSHeinz Mauelshagen 	unsigned int bio_size = wc->block_size;
1386cdd4d783SMike Snitzer 	sector_t start_cache_sec = cache_sector(wc, e);
1387cdd4d783SMike Snitzer 	sector_t current_cache_sec = start_cache_sec + (bio_size >> SECTOR_SHIFT);
1388cdd4d783SMike Snitzer 
1389cdd4d783SMike Snitzer 	while (bio_size < bio->bi_iter.bi_size) {
1390cdd4d783SMike Snitzer 		if (!search_used) {
1391cdd4d783SMike Snitzer 			struct wc_entry *f = writecache_pop_from_freelist(wc, current_cache_sec);
13920ef0b471SHeinz Mauelshagen 
1393cdd4d783SMike Snitzer 			if (!f)
1394cdd4d783SMike Snitzer 				break;
1395cdd4d783SMike Snitzer 			write_original_sector_seq_count(wc, f, bio->bi_iter.bi_sector +
1396cdd4d783SMike Snitzer 							(bio_size >> SECTOR_SHIFT), wc->seq_count);
1397cdd4d783SMike Snitzer 			writecache_insert_entry(wc, f);
1398cdd4d783SMike Snitzer 			wc->uncommitted_blocks++;
1399cdd4d783SMike Snitzer 		} else {
1400cdd4d783SMike Snitzer 			struct wc_entry *f;
1401cdd4d783SMike Snitzer 			struct rb_node *next = rb_next(&e->rb_node);
14020ef0b471SHeinz Mauelshagen 
1403cdd4d783SMike Snitzer 			if (!next)
1404cdd4d783SMike Snitzer 				break;
1405cdd4d783SMike Snitzer 			f = container_of(next, struct wc_entry, rb_node);
1406cdd4d783SMike Snitzer 			if (f != e + 1)
1407cdd4d783SMike Snitzer 				break;
1408cdd4d783SMike Snitzer 			if (read_original_sector(wc, f) !=
1409cdd4d783SMike Snitzer 			    read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
1410cdd4d783SMike Snitzer 				break;
1411cdd4d783SMike Snitzer 			if (unlikely(f->write_in_progress))
1412cdd4d783SMike Snitzer 				break;
1413cdd4d783SMike Snitzer 			if (writecache_entry_is_committed(wc, f))
1414cdd4d783SMike Snitzer 				wc->overwrote_committed = true;
1415cdd4d783SMike Snitzer 			e = f;
1416cdd4d783SMike Snitzer 		}
1417cdd4d783SMike Snitzer 		bio_size += wc->block_size;
1418cdd4d783SMike Snitzer 		current_cache_sec += wc->block_size >> SECTOR_SHIFT;
1419cdd4d783SMike Snitzer 	}
1420cdd4d783SMike Snitzer 
1421cdd4d783SMike Snitzer 	bio_set_dev(bio, wc->ssd_dev->bdev);
1422cdd4d783SMike Snitzer 	bio->bi_iter.bi_sector = start_cache_sec;
1423cdd4d783SMike Snitzer 	dm_accept_partial_bio(bio, bio_size >> SECTOR_SHIFT);
1424cdd4d783SMike Snitzer 
1425b2676e14SMikulas Patocka 	wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits;
1426b2676e14SMikulas Patocka 	wc->stats.writes_allocate += (bio->bi_iter.bi_size - wc->block_size) >> wc->block_size_bits;
1427b2676e14SMikulas Patocka 
1428cdd4d783SMike Snitzer 	if (unlikely(wc->uncommitted_blocks >= wc->autocommit_blocks)) {
1429cdd4d783SMike Snitzer 		wc->uncommitted_blocks = 0;
1430cdd4d783SMike Snitzer 		queue_work(wc->writeback_wq, &wc->flush_work);
1431cdd4d783SMike Snitzer 	} else {
1432cdd4d783SMike Snitzer 		writecache_schedule_autocommit(wc);
1433cdd4d783SMike Snitzer 	}
1434cdd4d783SMike Snitzer }
1435cdd4d783SMike Snitzer 
writecache_map_write(struct dm_writecache * wc,struct bio * bio)1436cdd4d783SMike Snitzer static enum wc_map_op writecache_map_write(struct dm_writecache *wc, struct bio *bio)
143748debafeSMikulas Patocka {
143848debafeSMikulas Patocka 	struct wc_entry *e;
1439cdd4d783SMike Snitzer 
1440cdd4d783SMike Snitzer 	do {
1441cdd4d783SMike Snitzer 		bool found_entry = false;
1442cdd4d783SMike Snitzer 		bool search_used = false;
14430ef0b471SHeinz Mauelshagen 
1444b2676e14SMikulas Patocka 		if (writecache_has_error(wc)) {
1445b2676e14SMikulas Patocka 			wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits;
1446cdd4d783SMike Snitzer 			return WC_MAP_ERROR;
1447b2676e14SMikulas Patocka 		}
1448cdd4d783SMike Snitzer 		e = writecache_find_entry(wc, bio->bi_iter.bi_sector, 0);
1449cdd4d783SMike Snitzer 		if (e) {
1450cdd4d783SMike Snitzer 			if (!writecache_entry_is_committed(wc, e)) {
1451e3a35d03SMikulas Patocka 				wc->stats.write_hits_uncommitted++;
1452cdd4d783SMike Snitzer 				search_used = true;
1453cdd4d783SMike Snitzer 				goto bio_copy;
1454cdd4d783SMike Snitzer 			}
1455e3a35d03SMikulas Patocka 			wc->stats.write_hits_committed++;
1456cdd4d783SMike Snitzer 			if (!WC_MODE_PMEM(wc) && !e->write_in_progress) {
1457cdd4d783SMike Snitzer 				wc->overwrote_committed = true;
1458cdd4d783SMike Snitzer 				search_used = true;
1459cdd4d783SMike Snitzer 				goto bio_copy;
1460cdd4d783SMike Snitzer 			}
1461cdd4d783SMike Snitzer 			found_entry = true;
1462cdd4d783SMike Snitzer 		} else {
1463cdd4d783SMike Snitzer 			if (unlikely(wc->cleaner) ||
1464cdd4d783SMike Snitzer 			    (wc->metadata_only && !(bio->bi_opf & REQ_META)))
1465cdd4d783SMike Snitzer 				goto direct_write;
1466cdd4d783SMike Snitzer 		}
1467cdd4d783SMike Snitzer 		e = writecache_pop_from_freelist(wc, (sector_t)-1);
1468cdd4d783SMike Snitzer 		if (unlikely(!e)) {
1469cdd4d783SMike Snitzer 			if (!WC_MODE_PMEM(wc) && !found_entry) {
1470cdd4d783SMike Snitzer direct_write:
1471cdd4d783SMike Snitzer 				e = writecache_find_entry(wc, bio->bi_iter.bi_sector, WFE_RETURN_FOLLOWING);
14729bc0c92eSMikulas Patocka 				writecache_map_remap_origin(wc, bio, e);
1473b2676e14SMikulas Patocka 				wc->stats.writes_around += bio->bi_iter.bi_size >> wc->block_size_bits;
1474b2676e14SMikulas Patocka 				wc->stats.writes += bio->bi_iter.bi_size >> wc->block_size_bits;
14759bc0c92eSMikulas Patocka 				return WC_MAP_REMAP_ORIGIN;
1476cdd4d783SMike Snitzer 			}
1477e3a35d03SMikulas Patocka 			wc->stats.writes_blocked_on_freelist++;
1478cdd4d783SMike Snitzer 			writecache_wait_on_freelist(wc);
1479cdd4d783SMike Snitzer 			continue;
1480cdd4d783SMike Snitzer 		}
1481cdd4d783SMike Snitzer 		write_original_sector_seq_count(wc, e, bio->bi_iter.bi_sector, wc->seq_count);
1482cdd4d783SMike Snitzer 		writecache_insert_entry(wc, e);
1483cdd4d783SMike Snitzer 		wc->uncommitted_blocks++;
1484e3a35d03SMikulas Patocka 		wc->stats.writes_allocate++;
1485cdd4d783SMike Snitzer bio_copy:
14869bc0c92eSMikulas Patocka 		if (WC_MODE_PMEM(wc)) {
1487cdd4d783SMike Snitzer 			bio_copy_block(wc, bio, memory_data(wc, e));
1488b2676e14SMikulas Patocka 			wc->stats.writes++;
14899bc0c92eSMikulas Patocka 		} else {
14909bc0c92eSMikulas Patocka 			writecache_bio_copy_ssd(wc, bio, e, search_used);
14919bc0c92eSMikulas Patocka 			return WC_MAP_REMAP;
14929bc0c92eSMikulas Patocka 		}
1493cdd4d783SMike Snitzer 	} while (bio->bi_iter.bi_size);
1494cdd4d783SMike Snitzer 
1495cdd4d783SMike Snitzer 	if (unlikely(bio->bi_opf & REQ_FUA || wc->uncommitted_blocks >= wc->autocommit_blocks))
1496cdd4d783SMike Snitzer 		writecache_flush(wc);
1497cdd4d783SMike Snitzer 	else
1498cdd4d783SMike Snitzer 		writecache_schedule_autocommit(wc);
1499cdd4d783SMike Snitzer 
1500cdd4d783SMike Snitzer 	return WC_MAP_SUBMIT;
1501cdd4d783SMike Snitzer }
1502cdd4d783SMike Snitzer 
writecache_map_flush(struct dm_writecache * wc,struct bio * bio)150315cb6f39SMike Snitzer static enum wc_map_op writecache_map_flush(struct dm_writecache *wc, struct bio *bio)
150415cb6f39SMike Snitzer {
150515cb6f39SMike Snitzer 	if (writecache_has_error(wc))
150615cb6f39SMike Snitzer 		return WC_MAP_ERROR;
150715cb6f39SMike Snitzer 
150815cb6f39SMike Snitzer 	if (WC_MODE_PMEM(wc)) {
1509e3a35d03SMikulas Patocka 		wc->stats.flushes++;
151015cb6f39SMike Snitzer 		writecache_flush(wc);
151115cb6f39SMike Snitzer 		if (writecache_has_error(wc))
151215cb6f39SMike Snitzer 			return WC_MAP_ERROR;
151315cb6f39SMike Snitzer 		else if (unlikely(wc->cleaner) || unlikely(wc->metadata_only))
151415cb6f39SMike Snitzer 			return WC_MAP_REMAP_ORIGIN;
151515cb6f39SMike Snitzer 		return WC_MAP_SUBMIT;
151615cb6f39SMike Snitzer 	}
151715cb6f39SMike Snitzer 	/* SSD: */
151815cb6f39SMike Snitzer 	if (dm_bio_get_target_bio_nr(bio))
151915cb6f39SMike Snitzer 		return WC_MAP_REMAP_ORIGIN;
1520e3a35d03SMikulas Patocka 	wc->stats.flushes++;
152115cb6f39SMike Snitzer 	writecache_offload_bio(wc, bio);
152215cb6f39SMike Snitzer 	return WC_MAP_RETURN;
152315cb6f39SMike Snitzer }
152415cb6f39SMike Snitzer 
writecache_map_discard(struct dm_writecache * wc,struct bio * bio)152515cb6f39SMike Snitzer static enum wc_map_op writecache_map_discard(struct dm_writecache *wc, struct bio *bio)
152615cb6f39SMike Snitzer {
15272ee73ef6SMikulas Patocka 	wc->stats.discards += bio->bi_iter.bi_size >> wc->block_size_bits;
1528e3a35d03SMikulas Patocka 
152915cb6f39SMike Snitzer 	if (writecache_has_error(wc))
153015cb6f39SMike Snitzer 		return WC_MAP_ERROR;
153115cb6f39SMike Snitzer 
153215cb6f39SMike Snitzer 	if (WC_MODE_PMEM(wc)) {
153315cb6f39SMike Snitzer 		writecache_discard(wc, bio->bi_iter.bi_sector, bio_end_sector(bio));
153415cb6f39SMike Snitzer 		return WC_MAP_REMAP_ORIGIN;
153515cb6f39SMike Snitzer 	}
153615cb6f39SMike Snitzer 	/* SSD: */
153715cb6f39SMike Snitzer 	writecache_offload_bio(wc, bio);
153815cb6f39SMike Snitzer 	return WC_MAP_RETURN;
153915cb6f39SMike Snitzer }
154015cb6f39SMike Snitzer 
writecache_map(struct dm_target * ti,struct bio * bio)1541cdd4d783SMike Snitzer static int writecache_map(struct dm_target *ti, struct bio *bio)
1542cdd4d783SMike Snitzer {
154348debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
154415cb6f39SMike Snitzer 	enum wc_map_op map_op;
154548debafeSMikulas Patocka 
154648debafeSMikulas Patocka 	bio->bi_private = NULL;
154748debafeSMikulas Patocka 
154848debafeSMikulas Patocka 	wc_lock(wc);
154948debafeSMikulas Patocka 
155048debafeSMikulas Patocka 	if (unlikely(bio->bi_opf & REQ_PREFLUSH)) {
155115cb6f39SMike Snitzer 		map_op = writecache_map_flush(wc, bio);
155215cb6f39SMike Snitzer 		goto done;
155348debafeSMikulas Patocka 	}
155448debafeSMikulas Patocka 
155548debafeSMikulas Patocka 	bio->bi_iter.bi_sector = dm_target_offset(ti, bio->bi_iter.bi_sector);
155648debafeSMikulas Patocka 
155786a3238cSHeinz Mauelshagen 	if (unlikely((((unsigned int)bio->bi_iter.bi_sector | bio_sectors(bio)) &
155848debafeSMikulas Patocka 				(wc->block_size / 512 - 1)) != 0)) {
155948debafeSMikulas Patocka 		DMERR("I/O is not aligned, sector %llu, size %u, block size %u",
156048debafeSMikulas Patocka 		      (unsigned long long)bio->bi_iter.bi_sector,
156148debafeSMikulas Patocka 		      bio->bi_iter.bi_size, wc->block_size);
156215cb6f39SMike Snitzer 		map_op = WC_MAP_ERROR;
156315cb6f39SMike Snitzer 		goto done;
156448debafeSMikulas Patocka 	}
156548debafeSMikulas Patocka 
156648debafeSMikulas Patocka 	if (unlikely(bio_op(bio) == REQ_OP_DISCARD)) {
156715cb6f39SMike Snitzer 		map_op = writecache_map_discard(wc, bio);
156815cb6f39SMike Snitzer 		goto done;
156948debafeSMikulas Patocka 	}
157048debafeSMikulas Patocka 
1571cdd4d783SMike Snitzer 	if (bio_data_dir(bio) == READ)
1572cdd4d783SMike Snitzer 		map_op = writecache_map_read(wc, bio);
157348debafeSMikulas Patocka 	else
1574cdd4d783SMike Snitzer 		map_op = writecache_map_write(wc, bio);
157515cb6f39SMike Snitzer done:
1576cdd4d783SMike Snitzer 	switch (map_op) {
1577cdd4d783SMike Snitzer 	case WC_MAP_REMAP_ORIGIN:
15785c0de3d7SMikulas Patocka 		if (likely(wc->pause != 0)) {
15795c0de3d7SMikulas Patocka 			if (bio_op(bio) == REQ_OP_WRITE) {
158095b88f4dSMikulas Patocka 				dm_iot_io_begin(&wc->iot, 1);
158195b88f4dSMikulas Patocka 				bio->bi_private = (void *)2;
158295b88f4dSMikulas Patocka 			}
15835c0de3d7SMikulas Patocka 		}
158448debafeSMikulas Patocka 		bio_set_dev(bio, wc->dev->bdev);
158548debafeSMikulas Patocka 		wc_unlock(wc);
158648debafeSMikulas Patocka 		return DM_MAPIO_REMAPPED;
158748debafeSMikulas Patocka 
1588cdd4d783SMike Snitzer 	case WC_MAP_REMAP:
158948debafeSMikulas Patocka 		/* make sure that writecache_end_io decrements bio_in_progress: */
159048debafeSMikulas Patocka 		bio->bi_private = (void *)1;
159148debafeSMikulas Patocka 		atomic_inc(&wc->bio_in_progress[bio_data_dir(bio)]);
159248debafeSMikulas Patocka 		wc_unlock(wc);
159348debafeSMikulas Patocka 		return DM_MAPIO_REMAPPED;
159448debafeSMikulas Patocka 
1595cdd4d783SMike Snitzer 	case WC_MAP_SUBMIT:
159648debafeSMikulas Patocka 		wc_unlock(wc);
159748debafeSMikulas Patocka 		bio_endio(bio);
159848debafeSMikulas Patocka 		return DM_MAPIO_SUBMITTED;
159948debafeSMikulas Patocka 
1600cdd4d783SMike Snitzer 	case WC_MAP_RETURN:
160148debafeSMikulas Patocka 		wc_unlock(wc);
160248debafeSMikulas Patocka 		return DM_MAPIO_SUBMITTED;
160348debafeSMikulas Patocka 
1604cdd4d783SMike Snitzer 	case WC_MAP_ERROR:
160548debafeSMikulas Patocka 		wc_unlock(wc);
160648debafeSMikulas Patocka 		bio_io_error(bio);
160748debafeSMikulas Patocka 		return DM_MAPIO_SUBMITTED;
1608df699cc1SMikulas Patocka 
1609df699cc1SMikulas Patocka 	default:
1610df699cc1SMikulas Patocka 		BUG();
1611b7f362d6SMikulas Patocka 		wc_unlock(wc);
1612b7f362d6SMikulas Patocka 		return DM_MAPIO_KILL;
161348debafeSMikulas Patocka 	}
1614cdd4d783SMike Snitzer }
161548debafeSMikulas Patocka 
writecache_end_io(struct dm_target * ti,struct bio * bio,blk_status_t * status)161648debafeSMikulas Patocka static int writecache_end_io(struct dm_target *ti, struct bio *bio, blk_status_t *status)
161748debafeSMikulas Patocka {
161848debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
161948debafeSMikulas Patocka 
162095b88f4dSMikulas Patocka 	if (bio->bi_private == (void *)1) {
162148debafeSMikulas Patocka 		int dir = bio_data_dir(bio);
16220ef0b471SHeinz Mauelshagen 
162348debafeSMikulas Patocka 		if (atomic_dec_and_test(&wc->bio_in_progress[dir]))
162448debafeSMikulas Patocka 			if (unlikely(waitqueue_active(&wc->bio_in_progress_wait[dir])))
162548debafeSMikulas Patocka 				wake_up(&wc->bio_in_progress_wait[dir]);
162695b88f4dSMikulas Patocka 	} else if (bio->bi_private == (void *)2) {
162795b88f4dSMikulas Patocka 		dm_iot_io_end(&wc->iot, 1);
162848debafeSMikulas Patocka 	}
162948debafeSMikulas Patocka 	return 0;
163048debafeSMikulas Patocka }
163148debafeSMikulas Patocka 
writecache_iterate_devices(struct dm_target * ti,iterate_devices_callout_fn fn,void * data)163248debafeSMikulas Patocka static int writecache_iterate_devices(struct dm_target *ti,
163348debafeSMikulas Patocka 				      iterate_devices_callout_fn fn, void *data)
163448debafeSMikulas Patocka {
163548debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
163648debafeSMikulas Patocka 
163748debafeSMikulas Patocka 	return fn(ti, wc->dev, 0, ti->len, data);
163848debafeSMikulas Patocka }
163948debafeSMikulas Patocka 
writecache_io_hints(struct dm_target * ti,struct queue_limits * limits)164048debafeSMikulas Patocka static void writecache_io_hints(struct dm_target *ti, struct queue_limits *limits)
164148debafeSMikulas Patocka {
164248debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
164348debafeSMikulas Patocka 
164448debafeSMikulas Patocka 	if (limits->logical_block_size < wc->block_size)
164548debafeSMikulas Patocka 		limits->logical_block_size = wc->block_size;
164648debafeSMikulas Patocka 
164748debafeSMikulas Patocka 	if (limits->physical_block_size < wc->block_size)
164848debafeSMikulas Patocka 		limits->physical_block_size = wc->block_size;
164948debafeSMikulas Patocka 
165048debafeSMikulas Patocka 	if (limits->io_min < wc->block_size)
165148debafeSMikulas Patocka 		limits->io_min = wc->block_size;
165248debafeSMikulas Patocka }
165348debafeSMikulas Patocka 
165448debafeSMikulas Patocka 
writecache_writeback_endio(struct bio * bio)165548debafeSMikulas Patocka static void writecache_writeback_endio(struct bio *bio)
165648debafeSMikulas Patocka {
165748debafeSMikulas Patocka 	struct writeback_struct *wb = container_of(bio, struct writeback_struct, bio);
165848debafeSMikulas Patocka 	struct dm_writecache *wc = wb->wc;
165948debafeSMikulas Patocka 	unsigned long flags;
166048debafeSMikulas Patocka 
166148debafeSMikulas Patocka 	raw_spin_lock_irqsave(&wc->endio_list_lock, flags);
166248debafeSMikulas Patocka 	if (unlikely(list_empty(&wc->endio_list)))
166348debafeSMikulas Patocka 		wake_up_process(wc->endio_thread);
166448debafeSMikulas Patocka 	list_add_tail(&wb->endio_entry, &wc->endio_list);
166548debafeSMikulas Patocka 	raw_spin_unlock_irqrestore(&wc->endio_list_lock, flags);
166648debafeSMikulas Patocka }
166748debafeSMikulas Patocka 
writecache_copy_endio(int read_err,unsigned long write_err,void * ptr)166848debafeSMikulas Patocka static void writecache_copy_endio(int read_err, unsigned long write_err, void *ptr)
166948debafeSMikulas Patocka {
167048debafeSMikulas Patocka 	struct copy_struct *c = ptr;
167148debafeSMikulas Patocka 	struct dm_writecache *wc = c->wc;
167248debafeSMikulas Patocka 
167348debafeSMikulas Patocka 	c->error = likely(!(read_err | write_err)) ? 0 : -EIO;
167448debafeSMikulas Patocka 
167548debafeSMikulas Patocka 	raw_spin_lock_irq(&wc->endio_list_lock);
167648debafeSMikulas Patocka 	if (unlikely(list_empty(&wc->endio_list)))
167748debafeSMikulas Patocka 		wake_up_process(wc->endio_thread);
167848debafeSMikulas Patocka 	list_add_tail(&c->endio_entry, &wc->endio_list);
167948debafeSMikulas Patocka 	raw_spin_unlock_irq(&wc->endio_list_lock);
168048debafeSMikulas Patocka }
168148debafeSMikulas Patocka 
__writecache_endio_pmem(struct dm_writecache * wc,struct list_head * list)168248debafeSMikulas Patocka static void __writecache_endio_pmem(struct dm_writecache *wc, struct list_head *list)
168348debafeSMikulas Patocka {
168486a3238cSHeinz Mauelshagen 	unsigned int i;
168548debafeSMikulas Patocka 	struct writeback_struct *wb;
168648debafeSMikulas Patocka 	struct wc_entry *e;
168748debafeSMikulas Patocka 	unsigned long n_walked = 0;
168848debafeSMikulas Patocka 
168948debafeSMikulas Patocka 	do {
169048debafeSMikulas Patocka 		wb = list_entry(list->next, struct writeback_struct, endio_entry);
169148debafeSMikulas Patocka 		list_del(&wb->endio_entry);
169248debafeSMikulas Patocka 
169348debafeSMikulas Patocka 		if (unlikely(wb->bio.bi_status != BLK_STS_OK))
169448debafeSMikulas Patocka 			writecache_error(wc, blk_status_to_errno(wb->bio.bi_status),
169548debafeSMikulas Patocka 					"write error %d", wb->bio.bi_status);
169648debafeSMikulas Patocka 		i = 0;
169748debafeSMikulas Patocka 		do {
169848debafeSMikulas Patocka 			e = wb->wc_list[i];
169948debafeSMikulas Patocka 			BUG_ON(!e->write_in_progress);
170048debafeSMikulas Patocka 			e->write_in_progress = false;
170148debafeSMikulas Patocka 			INIT_LIST_HEAD(&e->lru);
170248debafeSMikulas Patocka 			if (!writecache_has_error(wc))
170348debafeSMikulas Patocka 				writecache_free_entry(wc, e);
170448debafeSMikulas Patocka 			BUG_ON(!wc->writeback_size);
170548debafeSMikulas Patocka 			wc->writeback_size--;
170648debafeSMikulas Patocka 			n_walked++;
170748debafeSMikulas Patocka 			if (unlikely(n_walked >= ENDIO_LATENCY)) {
1708aa950920SMikulas Patocka 				writecache_commit_flushed(wc, false);
170948debafeSMikulas Patocka 				wc_unlock(wc);
171048debafeSMikulas Patocka 				wc_lock(wc);
171148debafeSMikulas Patocka 				n_walked = 0;
171248debafeSMikulas Patocka 			}
171348debafeSMikulas Patocka 		} while (++i < wb->wc_list_n);
171448debafeSMikulas Patocka 
171548debafeSMikulas Patocka 		if (wb->wc_list != wb->wc_list_inline)
171648debafeSMikulas Patocka 			kfree(wb->wc_list);
171748debafeSMikulas Patocka 		bio_put(&wb->bio);
171848debafeSMikulas Patocka 	} while (!list_empty(list));
171948debafeSMikulas Patocka }
172048debafeSMikulas Patocka 
__writecache_endio_ssd(struct dm_writecache * wc,struct list_head * list)172148debafeSMikulas Patocka static void __writecache_endio_ssd(struct dm_writecache *wc, struct list_head *list)
172248debafeSMikulas Patocka {
172348debafeSMikulas Patocka 	struct copy_struct *c;
172448debafeSMikulas Patocka 	struct wc_entry *e;
172548debafeSMikulas Patocka 
172648debafeSMikulas Patocka 	do {
172748debafeSMikulas Patocka 		c = list_entry(list->next, struct copy_struct, endio_entry);
172848debafeSMikulas Patocka 		list_del(&c->endio_entry);
172948debafeSMikulas Patocka 
173048debafeSMikulas Patocka 		if (unlikely(c->error))
173148debafeSMikulas Patocka 			writecache_error(wc, c->error, "copy error");
173248debafeSMikulas Patocka 
173348debafeSMikulas Patocka 		e = c->e;
173448debafeSMikulas Patocka 		do {
173548debafeSMikulas Patocka 			BUG_ON(!e->write_in_progress);
173648debafeSMikulas Patocka 			e->write_in_progress = false;
173748debafeSMikulas Patocka 			INIT_LIST_HEAD(&e->lru);
173848debafeSMikulas Patocka 			if (!writecache_has_error(wc))
173948debafeSMikulas Patocka 				writecache_free_entry(wc, e);
174048debafeSMikulas Patocka 
174148debafeSMikulas Patocka 			BUG_ON(!wc->writeback_size);
174248debafeSMikulas Patocka 			wc->writeback_size--;
174348debafeSMikulas Patocka 			e++;
174448debafeSMikulas Patocka 		} while (--c->n_entries);
174548debafeSMikulas Patocka 		mempool_free(c, &wc->copy_pool);
174648debafeSMikulas Patocka 	} while (!list_empty(list));
174748debafeSMikulas Patocka }
174848debafeSMikulas Patocka 
writecache_endio_thread(void * data)174948debafeSMikulas Patocka static int writecache_endio_thread(void *data)
175048debafeSMikulas Patocka {
175148debafeSMikulas Patocka 	struct dm_writecache *wc = data;
175248debafeSMikulas Patocka 
175348debafeSMikulas Patocka 	while (1) {
175448debafeSMikulas Patocka 		struct list_head list;
175548debafeSMikulas Patocka 
175648debafeSMikulas Patocka 		raw_spin_lock_irq(&wc->endio_list_lock);
175748debafeSMikulas Patocka 		if (!list_empty(&wc->endio_list))
175848debafeSMikulas Patocka 			goto pop_from_list;
175948debafeSMikulas Patocka 		set_current_state(TASK_INTERRUPTIBLE);
176048debafeSMikulas Patocka 		raw_spin_unlock_irq(&wc->endio_list_lock);
176148debafeSMikulas Patocka 
176248debafeSMikulas Patocka 		if (unlikely(kthread_should_stop())) {
176348debafeSMikulas Patocka 			set_current_state(TASK_RUNNING);
176448debafeSMikulas Patocka 			break;
176548debafeSMikulas Patocka 		}
176648debafeSMikulas Patocka 
176748debafeSMikulas Patocka 		schedule();
176848debafeSMikulas Patocka 
176948debafeSMikulas Patocka 		continue;
177048debafeSMikulas Patocka 
177148debafeSMikulas Patocka pop_from_list:
177248debafeSMikulas Patocka 		list = wc->endio_list;
177348debafeSMikulas Patocka 		list.next->prev = list.prev->next = &list;
177448debafeSMikulas Patocka 		INIT_LIST_HEAD(&wc->endio_list);
177548debafeSMikulas Patocka 		raw_spin_unlock_irq(&wc->endio_list_lock);
177648debafeSMikulas Patocka 
177748debafeSMikulas Patocka 		if (!WC_MODE_FUA(wc))
177848debafeSMikulas Patocka 			writecache_disk_flush(wc, wc->dev);
177948debafeSMikulas Patocka 
178048debafeSMikulas Patocka 		wc_lock(wc);
178148debafeSMikulas Patocka 
178248debafeSMikulas Patocka 		if (WC_MODE_PMEM(wc)) {
178348debafeSMikulas Patocka 			__writecache_endio_pmem(wc, &list);
178448debafeSMikulas Patocka 		} else {
178548debafeSMikulas Patocka 			__writecache_endio_ssd(wc, &list);
178648debafeSMikulas Patocka 			writecache_wait_for_ios(wc, READ);
178748debafeSMikulas Patocka 		}
178848debafeSMikulas Patocka 
1789aa950920SMikulas Patocka 		writecache_commit_flushed(wc, false);
179048debafeSMikulas Patocka 
179148debafeSMikulas Patocka 		wc_unlock(wc);
179248debafeSMikulas Patocka 	}
179348debafeSMikulas Patocka 
179448debafeSMikulas Patocka 	return 0;
179548debafeSMikulas Patocka }
179648debafeSMikulas Patocka 
wc_add_block(struct writeback_struct * wb,struct wc_entry * e)1797620cbe40SMikulas Patocka static bool wc_add_block(struct writeback_struct *wb, struct wc_entry *e)
179848debafeSMikulas Patocka {
179948debafeSMikulas Patocka 	struct dm_writecache *wc = wb->wc;
180086a3238cSHeinz Mauelshagen 	unsigned int block_size = wc->block_size;
180148debafeSMikulas Patocka 	void *address = memory_data(wc, e);
180248debafeSMikulas Patocka 
180348debafeSMikulas Patocka 	persistent_memory_flush_cache(address, block_size);
18044134455fSMikulas Patocka 
18054134455fSMikulas Patocka 	if (unlikely(bio_end_sector(&wb->bio) >= wc->data_device_sectors))
18064134455fSMikulas Patocka 		return true;
18074134455fSMikulas Patocka 
180848debafeSMikulas Patocka 	return bio_add_page(&wb->bio, persistent_memory_page(address),
180948debafeSMikulas Patocka 			    block_size, persistent_memory_page_offset(address)) != 0;
181048debafeSMikulas Patocka }
181148debafeSMikulas Patocka 
181248debafeSMikulas Patocka struct writeback_list {
181348debafeSMikulas Patocka 	struct list_head list;
181448debafeSMikulas Patocka 	size_t size;
181548debafeSMikulas Patocka };
181648debafeSMikulas Patocka 
__writeback_throttle(struct dm_writecache * wc,struct writeback_list * wbl)181748debafeSMikulas Patocka static void __writeback_throttle(struct dm_writecache *wc, struct writeback_list *wbl)
181848debafeSMikulas Patocka {
181948debafeSMikulas Patocka 	if (unlikely(wc->max_writeback_jobs)) {
182048debafeSMikulas Patocka 		if (READ_ONCE(wc->writeback_size) - wbl->size >= wc->max_writeback_jobs) {
182148debafeSMikulas Patocka 			wc_lock(wc);
182248debafeSMikulas Patocka 			while (wc->writeback_size - wbl->size >= wc->max_writeback_jobs)
182348debafeSMikulas Patocka 				writecache_wait_on_freelist(wc);
182448debafeSMikulas Patocka 			wc_unlock(wc);
182548debafeSMikulas Patocka 		}
182648debafeSMikulas Patocka 	}
182748debafeSMikulas Patocka 	cond_resched();
182848debafeSMikulas Patocka }
182948debafeSMikulas Patocka 
__writecache_writeback_pmem(struct dm_writecache * wc,struct writeback_list * wbl)183048debafeSMikulas Patocka static void __writecache_writeback_pmem(struct dm_writecache *wc, struct writeback_list *wbl)
183148debafeSMikulas Patocka {
183248debafeSMikulas Patocka 	struct wc_entry *e, *f;
183348debafeSMikulas Patocka 	struct bio *bio;
183448debafeSMikulas Patocka 	struct writeback_struct *wb;
183586a3238cSHeinz Mauelshagen 	unsigned int max_pages;
183648debafeSMikulas Patocka 
183748debafeSMikulas Patocka 	while (wbl->size) {
183848debafeSMikulas Patocka 		wbl->size--;
183948debafeSMikulas Patocka 		e = container_of(wbl->list.prev, struct wc_entry, lru);
184048debafeSMikulas Patocka 		list_del(&e->lru);
184148debafeSMikulas Patocka 
184248debafeSMikulas Patocka 		max_pages = e->wc_list_contiguous;
184348debafeSMikulas Patocka 
1844609be106SChristoph Hellwig 		bio = bio_alloc_bioset(wc->dev->bdev, max_pages, REQ_OP_WRITE,
1845609be106SChristoph Hellwig 				       GFP_NOIO, &wc->bio_set);
184648debafeSMikulas Patocka 		wb = container_of(bio, struct writeback_struct, bio);
184748debafeSMikulas Patocka 		wb->wc = wc;
184809f2d656SHuaisheng Ye 		bio->bi_end_io = writecache_writeback_endio;
184909f2d656SHuaisheng Ye 		bio->bi_iter.bi_sector = read_original_sector(wc, e);
1850d715fa23SHeinz Mauelshagen 
1851d715fa23SHeinz Mauelshagen 		if (unlikely(max_pages > WB_LIST_INLINE))
1852d715fa23SHeinz Mauelshagen 			wb->wc_list = kmalloc_array(max_pages, sizeof(struct wc_entry *),
185348debafeSMikulas Patocka 						    GFP_NOIO | __GFP_NORETRY |
1854d715fa23SHeinz Mauelshagen 						    __GFP_NOMEMALLOC | __GFP_NOWARN);
1855d715fa23SHeinz Mauelshagen 
1856d715fa23SHeinz Mauelshagen 		if (likely(max_pages <= WB_LIST_INLINE) || unlikely(!wb->wc_list)) {
185748debafeSMikulas Patocka 			wb->wc_list = wb->wc_list_inline;
185848debafeSMikulas Patocka 			max_pages = WB_LIST_INLINE;
185948debafeSMikulas Patocka 		}
186048debafeSMikulas Patocka 
1861620cbe40SMikulas Patocka 		BUG_ON(!wc_add_block(wb, e));
186248debafeSMikulas Patocka 
186348debafeSMikulas Patocka 		wb->wc_list[0] = e;
186448debafeSMikulas Patocka 		wb->wc_list_n = 1;
186548debafeSMikulas Patocka 
186648debafeSMikulas Patocka 		while (wbl->size && wb->wc_list_n < max_pages) {
186748debafeSMikulas Patocka 			f = container_of(wbl->list.prev, struct wc_entry, lru);
186848debafeSMikulas Patocka 			if (read_original_sector(wc, f) !=
186948debafeSMikulas Patocka 			    read_original_sector(wc, e) + (wc->block_size >> SECTOR_SHIFT))
187048debafeSMikulas Patocka 				break;
1871620cbe40SMikulas Patocka 			if (!wc_add_block(wb, f))
187248debafeSMikulas Patocka 				break;
187348debafeSMikulas Patocka 			wbl->size--;
187448debafeSMikulas Patocka 			list_del(&f->lru);
187548debafeSMikulas Patocka 			wb->wc_list[wb->wc_list_n++] = f;
187648debafeSMikulas Patocka 			e = f;
187748debafeSMikulas Patocka 		}
1878609be106SChristoph Hellwig 		if (WC_MODE_FUA(wc))
1879609be106SChristoph Hellwig 			bio->bi_opf |= REQ_FUA;
188048debafeSMikulas Patocka 		if (writecache_has_error(wc)) {
188148debafeSMikulas Patocka 			bio->bi_status = BLK_STS_IOERR;
188209f2d656SHuaisheng Ye 			bio_endio(bio);
18834134455fSMikulas Patocka 		} else if (unlikely(!bio_sectors(bio))) {
18844134455fSMikulas Patocka 			bio->bi_status = BLK_STS_OK;
18854134455fSMikulas Patocka 			bio_endio(bio);
188648debafeSMikulas Patocka 		} else {
188709f2d656SHuaisheng Ye 			submit_bio(bio);
188848debafeSMikulas Patocka 		}
188948debafeSMikulas Patocka 
189048debafeSMikulas Patocka 		__writeback_throttle(wc, wbl);
189148debafeSMikulas Patocka 	}
189248debafeSMikulas Patocka }
189348debafeSMikulas Patocka 
__writecache_writeback_ssd(struct dm_writecache * wc,struct writeback_list * wbl)189448debafeSMikulas Patocka static void __writecache_writeback_ssd(struct dm_writecache *wc, struct writeback_list *wbl)
189548debafeSMikulas Patocka {
189648debafeSMikulas Patocka 	struct wc_entry *e, *f;
189748debafeSMikulas Patocka 	struct dm_io_region from, to;
189848debafeSMikulas Patocka 	struct copy_struct *c;
189948debafeSMikulas Patocka 
190048debafeSMikulas Patocka 	while (wbl->size) {
190186a3238cSHeinz Mauelshagen 		unsigned int n_sectors;
190248debafeSMikulas Patocka 
190348debafeSMikulas Patocka 		wbl->size--;
190448debafeSMikulas Patocka 		e = container_of(wbl->list.prev, struct wc_entry, lru);
190548debafeSMikulas Patocka 		list_del(&e->lru);
190648debafeSMikulas Patocka 
190748debafeSMikulas Patocka 		n_sectors = e->wc_list_contiguous << (wc->block_size_bits - SECTOR_SHIFT);
190848debafeSMikulas Patocka 
190948debafeSMikulas Patocka 		from.bdev = wc->ssd_dev->bdev;
191048debafeSMikulas Patocka 		from.sector = cache_sector(wc, e);
191148debafeSMikulas Patocka 		from.count = n_sectors;
191248debafeSMikulas Patocka 		to.bdev = wc->dev->bdev;
191348debafeSMikulas Patocka 		to.sector = read_original_sector(wc, e);
191448debafeSMikulas Patocka 		to.count = n_sectors;
191548debafeSMikulas Patocka 
191648debafeSMikulas Patocka 		c = mempool_alloc(&wc->copy_pool, GFP_NOIO);
191748debafeSMikulas Patocka 		c->wc = wc;
191848debafeSMikulas Patocka 		c->e = e;
191948debafeSMikulas Patocka 		c->n_entries = e->wc_list_contiguous;
192048debafeSMikulas Patocka 
192148debafeSMikulas Patocka 		while ((n_sectors -= wc->block_size >> SECTOR_SHIFT)) {
192248debafeSMikulas Patocka 			wbl->size--;
192348debafeSMikulas Patocka 			f = container_of(wbl->list.prev, struct wc_entry, lru);
192448debafeSMikulas Patocka 			BUG_ON(f != e + 1);
192548debafeSMikulas Patocka 			list_del(&f->lru);
192648debafeSMikulas Patocka 			e = f;
192748debafeSMikulas Patocka 		}
192848debafeSMikulas Patocka 
19294134455fSMikulas Patocka 		if (unlikely(to.sector + to.count > wc->data_device_sectors)) {
19304134455fSMikulas Patocka 			if (to.sector >= wc->data_device_sectors) {
19314134455fSMikulas Patocka 				writecache_copy_endio(0, 0, c);
19324134455fSMikulas Patocka 				continue;
19334134455fSMikulas Patocka 			}
19344134455fSMikulas Patocka 			from.count = to.count = wc->data_device_sectors - to.sector;
19354134455fSMikulas Patocka 		}
19364134455fSMikulas Patocka 
193748debafeSMikulas Patocka 		dm_kcopyd_copy(wc->dm_kcopyd, &from, 1, &to, 0, writecache_copy_endio, c);
193848debafeSMikulas Patocka 
193948debafeSMikulas Patocka 		__writeback_throttle(wc, wbl);
194048debafeSMikulas Patocka 	}
194148debafeSMikulas Patocka }
194248debafeSMikulas Patocka 
writecache_writeback(struct work_struct * work)194348debafeSMikulas Patocka static void writecache_writeback(struct work_struct *work)
194448debafeSMikulas Patocka {
194548debafeSMikulas Patocka 	struct dm_writecache *wc = container_of(work, struct dm_writecache, writeback_work);
194648debafeSMikulas Patocka 	struct blk_plug plug;
19473f649ab7SKees Cook 	struct wc_entry *f, *g, *e = NULL;
194848debafeSMikulas Patocka 	struct rb_node *node, *next_node;
194948debafeSMikulas Patocka 	struct list_head skipped;
195048debafeSMikulas Patocka 	struct writeback_list wbl;
195148debafeSMikulas Patocka 	unsigned long n_walked;
195248debafeSMikulas Patocka 
1953293128b1SMikulas Patocka 	if (!WC_MODE_PMEM(wc)) {
1954293128b1SMikulas Patocka 		/* Wait for any active kcopyd work on behalf of ssd writeback */
1955293128b1SMikulas Patocka 		dm_kcopyd_client_flush(wc->dm_kcopyd);
1956293128b1SMikulas Patocka 	}
1957293128b1SMikulas Patocka 
19585c0de3d7SMikulas Patocka 	if (likely(wc->pause != 0)) {
19595c0de3d7SMikulas Patocka 		while (1) {
19605c0de3d7SMikulas Patocka 			unsigned long idle;
19610ef0b471SHeinz Mauelshagen 
19625c0de3d7SMikulas Patocka 			if (unlikely(wc->cleaner) || unlikely(wc->writeback_all) ||
19635c0de3d7SMikulas Patocka 			    unlikely(dm_suspended(wc->ti)))
19645c0de3d7SMikulas Patocka 				break;
19655c0de3d7SMikulas Patocka 			idle = dm_iot_idle_time(&wc->iot);
19665c0de3d7SMikulas Patocka 			if (idle >= wc->pause)
19675c0de3d7SMikulas Patocka 				break;
19685c0de3d7SMikulas Patocka 			idle = wc->pause - idle;
19695c0de3d7SMikulas Patocka 			if (idle > HZ)
19705c0de3d7SMikulas Patocka 				idle = HZ;
19715c0de3d7SMikulas Patocka 			schedule_timeout_idle(idle);
197295b88f4dSMikulas Patocka 		}
197395b88f4dSMikulas Patocka 	}
197495b88f4dSMikulas Patocka 
197548debafeSMikulas Patocka 	wc_lock(wc);
197648debafeSMikulas Patocka restart:
197748debafeSMikulas Patocka 	if (writecache_has_error(wc)) {
197848debafeSMikulas Patocka 		wc_unlock(wc);
197948debafeSMikulas Patocka 		return;
198048debafeSMikulas Patocka 	}
198148debafeSMikulas Patocka 
198248debafeSMikulas Patocka 	if (unlikely(wc->writeback_all)) {
198348debafeSMikulas Patocka 		if (writecache_wait_for_writeback(wc))
198448debafeSMikulas Patocka 			goto restart;
198548debafeSMikulas Patocka 	}
198648debafeSMikulas Patocka 
19872d0f25cbSHeinz Mauelshagen 	if (wc->overwrote_committed)
198848debafeSMikulas Patocka 		writecache_wait_for_ios(wc, WRITE);
198948debafeSMikulas Patocka 
199048debafeSMikulas Patocka 	n_walked = 0;
199148debafeSMikulas Patocka 	INIT_LIST_HEAD(&skipped);
199248debafeSMikulas Patocka 	INIT_LIST_HEAD(&wbl.list);
199348debafeSMikulas Patocka 	wbl.size = 0;
199448debafeSMikulas Patocka 	while (!list_empty(&wc->lru) &&
199548debafeSMikulas Patocka 	       (wc->writeback_all ||
19963923d485SMikulas Patocka 		wc->freelist_size + wc->writeback_size <= wc->freelist_low_watermark ||
19973923d485SMikulas Patocka 		(jiffies - container_of(wc->lru.prev, struct wc_entry, lru)->age >=
19983923d485SMikulas Patocka 		 wc->max_age - wc->max_age / MAX_AGE_DIV))) {
199948debafeSMikulas Patocka 
200048debafeSMikulas Patocka 		n_walked++;
200148debafeSMikulas Patocka 		if (unlikely(n_walked > WRITEBACK_LATENCY) &&
2002af4f6cabSMikulas Patocka 		    likely(!wc->writeback_all)) {
2003af4f6cabSMikulas Patocka 			if (likely(!dm_suspended(wc->ti)))
200448debafeSMikulas Patocka 				queue_work(wc->writeback_wq, &wc->writeback_work);
200548debafeSMikulas Patocka 			break;
200648debafeSMikulas Patocka 		}
200748debafeSMikulas Patocka 
20085229b489SHuaisheng Ye 		if (unlikely(wc->writeback_all)) {
20095229b489SHuaisheng Ye 			if (unlikely(!e)) {
20105229b489SHuaisheng Ye 				writecache_flush(wc);
20115229b489SHuaisheng Ye 				e = container_of(rb_first(&wc->tree), struct wc_entry, rb_node);
20125229b489SHuaisheng Ye 			} else
20135229b489SHuaisheng Ye 				e = g;
20145229b489SHuaisheng Ye 		} else
201548debafeSMikulas Patocka 			e = container_of(wc->lru.prev, struct wc_entry, lru);
201648debafeSMikulas Patocka 		BUG_ON(e->write_in_progress);
20172d0f25cbSHeinz Mauelshagen 		if (unlikely(!writecache_entry_is_committed(wc, e)))
201848debafeSMikulas Patocka 			writecache_flush(wc);
20192d0f25cbSHeinz Mauelshagen 
202048debafeSMikulas Patocka 		node = rb_prev(&e->rb_node);
202148debafeSMikulas Patocka 		if (node) {
202248debafeSMikulas Patocka 			f = container_of(node, struct wc_entry, rb_node);
202348debafeSMikulas Patocka 			if (unlikely(read_original_sector(wc, f) ==
202448debafeSMikulas Patocka 				     read_original_sector(wc, e))) {
202548debafeSMikulas Patocka 				BUG_ON(!f->write_in_progress);
20268c77f1cbSBaokun Li 				list_move(&e->lru, &skipped);
202748debafeSMikulas Patocka 				cond_resched();
202848debafeSMikulas Patocka 				continue;
202948debafeSMikulas Patocka 			}
203048debafeSMikulas Patocka 		}
203148debafeSMikulas Patocka 		wc->writeback_size++;
20328c77f1cbSBaokun Li 		list_move(&e->lru, &wbl.list);
203348debafeSMikulas Patocka 		wbl.size++;
203448debafeSMikulas Patocka 		e->write_in_progress = true;
203548debafeSMikulas Patocka 		e->wc_list_contiguous = 1;
203648debafeSMikulas Patocka 
203748debafeSMikulas Patocka 		f = e;
203848debafeSMikulas Patocka 
203948debafeSMikulas Patocka 		while (1) {
204048debafeSMikulas Patocka 			next_node = rb_next(&f->rb_node);
204148debafeSMikulas Patocka 			if (unlikely(!next_node))
204248debafeSMikulas Patocka 				break;
204348debafeSMikulas Patocka 			g = container_of(next_node, struct wc_entry, rb_node);
204462421b38SHuaisheng Ye 			if (unlikely(read_original_sector(wc, g) ==
204562421b38SHuaisheng Ye 			    read_original_sector(wc, f))) {
204648debafeSMikulas Patocka 				f = g;
204748debafeSMikulas Patocka 				continue;
204848debafeSMikulas Patocka 			}
204948debafeSMikulas Patocka 			if (read_original_sector(wc, g) !=
205048debafeSMikulas Patocka 			    read_original_sector(wc, f) + (wc->block_size >> SECTOR_SHIFT))
205148debafeSMikulas Patocka 				break;
205248debafeSMikulas Patocka 			if (unlikely(g->write_in_progress))
205348debafeSMikulas Patocka 				break;
205448debafeSMikulas Patocka 			if (unlikely(!writecache_entry_is_committed(wc, g)))
205548debafeSMikulas Patocka 				break;
205648debafeSMikulas Patocka 
205748debafeSMikulas Patocka 			if (!WC_MODE_PMEM(wc)) {
205848debafeSMikulas Patocka 				if (g != f + 1)
205948debafeSMikulas Patocka 					break;
206048debafeSMikulas Patocka 			}
206148debafeSMikulas Patocka 
206248debafeSMikulas Patocka 			n_walked++;
206348debafeSMikulas Patocka 			//if (unlikely(n_walked > WRITEBACK_LATENCY) && likely(!wc->writeback_all))
206448debafeSMikulas Patocka 			//	break;
206548debafeSMikulas Patocka 
206648debafeSMikulas Patocka 			wc->writeback_size++;
20678c77f1cbSBaokun Li 			list_move(&g->lru, &wbl.list);
206848debafeSMikulas Patocka 			wbl.size++;
206948debafeSMikulas Patocka 			g->write_in_progress = true;
2070a8affc03SChristoph Hellwig 			g->wc_list_contiguous = BIO_MAX_VECS;
207148debafeSMikulas Patocka 			f = g;
207248debafeSMikulas Patocka 			e->wc_list_contiguous++;
2073a8affc03SChristoph Hellwig 			if (unlikely(e->wc_list_contiguous == BIO_MAX_VECS)) {
20745229b489SHuaisheng Ye 				if (unlikely(wc->writeback_all)) {
20755229b489SHuaisheng Ye 					next_node = rb_next(&f->rb_node);
20765229b489SHuaisheng Ye 					if (likely(next_node))
20775229b489SHuaisheng Ye 						g = container_of(next_node, struct wc_entry, rb_node);
20785229b489SHuaisheng Ye 				}
207948debafeSMikulas Patocka 				break;
208048debafeSMikulas Patocka 			}
20815229b489SHuaisheng Ye 		}
208248debafeSMikulas Patocka 		cond_resched();
208348debafeSMikulas Patocka 	}
208448debafeSMikulas Patocka 
208548debafeSMikulas Patocka 	if (!list_empty(&skipped)) {
208648debafeSMikulas Patocka 		list_splice_tail(&skipped, &wc->lru);
208748debafeSMikulas Patocka 		/*
208848debafeSMikulas Patocka 		 * If we didn't do any progress, we must wait until some
208948debafeSMikulas Patocka 		 * writeback finishes to avoid burning CPU in a loop
209048debafeSMikulas Patocka 		 */
209148debafeSMikulas Patocka 		if (unlikely(!wbl.size))
209248debafeSMikulas Patocka 			writecache_wait_for_writeback(wc);
209348debafeSMikulas Patocka 	}
209448debafeSMikulas Patocka 
209548debafeSMikulas Patocka 	wc_unlock(wc);
209648debafeSMikulas Patocka 
209748debafeSMikulas Patocka 	blk_start_plug(&plug);
209848debafeSMikulas Patocka 
209948debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
210048debafeSMikulas Patocka 		__writecache_writeback_pmem(wc, &wbl);
210148debafeSMikulas Patocka 	else
210248debafeSMikulas Patocka 		__writecache_writeback_ssd(wc, &wbl);
210348debafeSMikulas Patocka 
210448debafeSMikulas Patocka 	blk_finish_plug(&plug);
210548debafeSMikulas Patocka 
210648debafeSMikulas Patocka 	if (unlikely(wc->writeback_all)) {
210748debafeSMikulas Patocka 		wc_lock(wc);
210803b18887SHeinz Mauelshagen 		while (writecache_wait_for_writeback(wc))
210903b18887SHeinz Mauelshagen 			;
211048debafeSMikulas Patocka 		wc_unlock(wc);
211148debafeSMikulas Patocka 	}
211248debafeSMikulas Patocka }
211348debafeSMikulas Patocka 
calculate_memory_size(uint64_t device_size,unsigned int block_size,size_t * n_blocks_p,size_t * n_metadata_blocks_p)211486a3238cSHeinz Mauelshagen static int calculate_memory_size(uint64_t device_size, unsigned int block_size,
211548debafeSMikulas Patocka 				 size_t *n_blocks_p, size_t *n_metadata_blocks_p)
211648debafeSMikulas Patocka {
211748debafeSMikulas Patocka 	uint64_t n_blocks, offset;
211848debafeSMikulas Patocka 	struct wc_entry e;
211948debafeSMikulas Patocka 
212048debafeSMikulas Patocka 	n_blocks = device_size;
212148debafeSMikulas Patocka 	do_div(n_blocks, block_size + sizeof(struct wc_memory_entry));
212248debafeSMikulas Patocka 
212348debafeSMikulas Patocka 	while (1) {
212448debafeSMikulas Patocka 		if (!n_blocks)
212548debafeSMikulas Patocka 			return -ENOSPC;
212648debafeSMikulas Patocka 		/* Verify the following entries[n_blocks] won't overflow */
212748debafeSMikulas Patocka 		if (n_blocks >= ((size_t)-sizeof(struct wc_memory_superblock) /
212848debafeSMikulas Patocka 				 sizeof(struct wc_memory_entry)))
212948debafeSMikulas Patocka 			return -EFBIG;
213048debafeSMikulas Patocka 		offset = offsetof(struct wc_memory_superblock, entries[n_blocks]);
213148debafeSMikulas Patocka 		offset = (offset + block_size - 1) & ~(uint64_t)(block_size - 1);
213248debafeSMikulas Patocka 		if (offset + n_blocks * block_size <= device_size)
213348debafeSMikulas Patocka 			break;
213448debafeSMikulas Patocka 		n_blocks--;
213548debafeSMikulas Patocka 	}
213648debafeSMikulas Patocka 
213748debafeSMikulas Patocka 	/* check if the bit field overflows */
213848debafeSMikulas Patocka 	e.index = n_blocks;
213948debafeSMikulas Patocka 	if (e.index != n_blocks)
214048debafeSMikulas Patocka 		return -EFBIG;
214148debafeSMikulas Patocka 
214248debafeSMikulas Patocka 	if (n_blocks_p)
214348debafeSMikulas Patocka 		*n_blocks_p = n_blocks;
214448debafeSMikulas Patocka 	if (n_metadata_blocks_p)
214548debafeSMikulas Patocka 		*n_metadata_blocks_p = offset >> __ffs(block_size);
214648debafeSMikulas Patocka 	return 0;
214748debafeSMikulas Patocka }
214848debafeSMikulas Patocka 
init_memory(struct dm_writecache * wc)214948debafeSMikulas Patocka static int init_memory(struct dm_writecache *wc)
215048debafeSMikulas Patocka {
215148debafeSMikulas Patocka 	size_t b;
215248debafeSMikulas Patocka 	int r;
215348debafeSMikulas Patocka 
215448debafeSMikulas Patocka 	r = calculate_memory_size(wc->memory_map_size, wc->block_size, &wc->n_blocks, NULL);
215548debafeSMikulas Patocka 	if (r)
215648debafeSMikulas Patocka 		return r;
215748debafeSMikulas Patocka 
215848debafeSMikulas Patocka 	r = writecache_alloc_entries(wc);
215948debafeSMikulas Patocka 	if (r)
216048debafeSMikulas Patocka 		return r;
216148debafeSMikulas Patocka 
216248debafeSMikulas Patocka 	for (b = 0; b < ARRAY_SIZE(sb(wc)->padding); b++)
216348debafeSMikulas Patocka 		pmem_assign(sb(wc)->padding[b], cpu_to_le64(0));
216448debafeSMikulas Patocka 	pmem_assign(sb(wc)->version, cpu_to_le32(MEMORY_SUPERBLOCK_VERSION));
216548debafeSMikulas Patocka 	pmem_assign(sb(wc)->block_size, cpu_to_le32(wc->block_size));
216648debafeSMikulas Patocka 	pmem_assign(sb(wc)->n_blocks, cpu_to_le64(wc->n_blocks));
216748debafeSMikulas Patocka 	pmem_assign(sb(wc)->seq_count, cpu_to_le64(0));
216848debafeSMikulas Patocka 
21691edaa447SMikulas Patocka 	for (b = 0; b < wc->n_blocks; b++) {
217048debafeSMikulas Patocka 		write_original_sector_seq_count(wc, &wc->entries[b], -1, -1);
21711edaa447SMikulas Patocka 		cond_resched();
21721edaa447SMikulas Patocka 	}
217348debafeSMikulas Patocka 
217448debafeSMikulas Patocka 	writecache_flush_all_metadata(wc);
2175aa950920SMikulas Patocka 	writecache_commit_flushed(wc, false);
217648debafeSMikulas Patocka 	pmem_assign(sb(wc)->magic, cpu_to_le32(MEMORY_SUPERBLOCK_MAGIC));
21778d1058fbSHeinz Mauelshagen 	writecache_flush_region(wc, &sb(wc)->magic, sizeof(sb(wc)->magic));
2178aa950920SMikulas Patocka 	writecache_commit_flushed(wc, false);
217948debafeSMikulas Patocka 
218048debafeSMikulas Patocka 	return 0;
218148debafeSMikulas Patocka }
218248debafeSMikulas Patocka 
writecache_dtr(struct dm_target * ti)218348debafeSMikulas Patocka static void writecache_dtr(struct dm_target *ti)
218448debafeSMikulas Patocka {
218548debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
218648debafeSMikulas Patocka 
218748debafeSMikulas Patocka 	if (!wc)
218848debafeSMikulas Patocka 		return;
218948debafeSMikulas Patocka 
219048debafeSMikulas Patocka 	if (wc->endio_thread)
219148debafeSMikulas Patocka 		kthread_stop(wc->endio_thread);
219248debafeSMikulas Patocka 
219348debafeSMikulas Patocka 	if (wc->flush_thread)
219448debafeSMikulas Patocka 		kthread_stop(wc->flush_thread);
219548debafeSMikulas Patocka 
219648debafeSMikulas Patocka 	bioset_exit(&wc->bio_set);
219748debafeSMikulas Patocka 
219848debafeSMikulas Patocka 	mempool_exit(&wc->copy_pool);
219948debafeSMikulas Patocka 
220048debafeSMikulas Patocka 	if (wc->writeback_wq)
220148debafeSMikulas Patocka 		destroy_workqueue(wc->writeback_wq);
220248debafeSMikulas Patocka 
220348debafeSMikulas Patocka 	if (wc->dev)
220448debafeSMikulas Patocka 		dm_put_device(ti, wc->dev);
220548debafeSMikulas Patocka 
220648debafeSMikulas Patocka 	if (wc->ssd_dev)
220748debafeSMikulas Patocka 		dm_put_device(ti, wc->ssd_dev);
220848debafeSMikulas Patocka 
220948debafeSMikulas Patocka 	vfree(wc->entries);
221048debafeSMikulas Patocka 
221148debafeSMikulas Patocka 	if (wc->memory_map) {
221248debafeSMikulas Patocka 		if (WC_MODE_PMEM(wc))
221348debafeSMikulas Patocka 			persistent_memory_release(wc);
221448debafeSMikulas Patocka 		else
221548debafeSMikulas Patocka 			vfree(wc->memory_map);
221648debafeSMikulas Patocka 	}
221748debafeSMikulas Patocka 
221848debafeSMikulas Patocka 	if (wc->dm_kcopyd)
221948debafeSMikulas Patocka 		dm_kcopyd_client_destroy(wc->dm_kcopyd);
222048debafeSMikulas Patocka 
222148debafeSMikulas Patocka 	if (wc->dm_io)
222248debafeSMikulas Patocka 		dm_io_client_destroy(wc->dm_io);
222348debafeSMikulas Patocka 
222448debafeSMikulas Patocka 	vfree(wc->dirty_bitmap);
222548debafeSMikulas Patocka 
222648debafeSMikulas Patocka 	kfree(wc);
222748debafeSMikulas Patocka }
222848debafeSMikulas Patocka 
writecache_ctr(struct dm_target * ti,unsigned int argc,char ** argv)222986a3238cSHeinz Mauelshagen static int writecache_ctr(struct dm_target *ti, unsigned int argc, char **argv)
223048debafeSMikulas Patocka {
223148debafeSMikulas Patocka 	struct dm_writecache *wc;
223248debafeSMikulas Patocka 	struct dm_arg_set as;
223348debafeSMikulas Patocka 	const char *string;
223486a3238cSHeinz Mauelshagen 	unsigned int opt_params;
223548debafeSMikulas Patocka 	size_t offset, data_size;
223648debafeSMikulas Patocka 	int i, r;
223748debafeSMikulas Patocka 	char dummy;
223848debafeSMikulas Patocka 	int high_wm_percent = HIGH_WATERMARK;
223948debafeSMikulas Patocka 	int low_wm_percent = LOW_WATERMARK;
224048debafeSMikulas Patocka 	uint64_t x;
224148debafeSMikulas Patocka 	struct wc_memory_superblock s;
224248debafeSMikulas Patocka 
224348debafeSMikulas Patocka 	static struct dm_arg _args[] = {
22445c0de3d7SMikulas Patocka 		{0, 18, "Invalid number of feature args"},
224548debafeSMikulas Patocka 	};
224648debafeSMikulas Patocka 
224748debafeSMikulas Patocka 	as.argc = argc;
224848debafeSMikulas Patocka 	as.argv = argv;
224948debafeSMikulas Patocka 
225048debafeSMikulas Patocka 	wc = kzalloc(sizeof(struct dm_writecache), GFP_KERNEL);
225148debafeSMikulas Patocka 	if (!wc) {
225248debafeSMikulas Patocka 		ti->error = "Cannot allocate writecache structure";
225348debafeSMikulas Patocka 		r = -ENOMEM;
225448debafeSMikulas Patocka 		goto bad;
225548debafeSMikulas Patocka 	}
225648debafeSMikulas Patocka 	ti->private = wc;
225748debafeSMikulas Patocka 	wc->ti = ti;
225848debafeSMikulas Patocka 
225948debafeSMikulas Patocka 	mutex_init(&wc->lock);
22603923d485SMikulas Patocka 	wc->max_age = MAX_AGE_UNSPECIFIED;
226148debafeSMikulas Patocka 	writecache_poison_lists(wc);
226248debafeSMikulas Patocka 	init_waitqueue_head(&wc->freelist_wait);
226348debafeSMikulas Patocka 	timer_setup(&wc->autocommit_timer, writecache_autocommit_timer, 0);
22643923d485SMikulas Patocka 	timer_setup(&wc->max_age_timer, writecache_max_age_timer, 0);
226548debafeSMikulas Patocka 
226648debafeSMikulas Patocka 	for (i = 0; i < 2; i++) {
226748debafeSMikulas Patocka 		atomic_set(&wc->bio_in_progress[i], 0);
226848debafeSMikulas Patocka 		init_waitqueue_head(&wc->bio_in_progress_wait[i]);
226948debafeSMikulas Patocka 	}
227048debafeSMikulas Patocka 
227148debafeSMikulas Patocka 	wc->dm_io = dm_io_client_create();
227248debafeSMikulas Patocka 	if (IS_ERR(wc->dm_io)) {
227348debafeSMikulas Patocka 		r = PTR_ERR(wc->dm_io);
227448debafeSMikulas Patocka 		ti->error = "Unable to allocate dm-io client";
227548debafeSMikulas Patocka 		wc->dm_io = NULL;
227648debafeSMikulas Patocka 		goto bad;
227748debafeSMikulas Patocka 	}
227848debafeSMikulas Patocka 
2279f87e033bSHuaisheng Ye 	wc->writeback_wq = alloc_workqueue("writecache-writeback", WQ_MEM_RECLAIM, 1);
228048debafeSMikulas Patocka 	if (!wc->writeback_wq) {
228148debafeSMikulas Patocka 		r = -ENOMEM;
228248debafeSMikulas Patocka 		ti->error = "Could not allocate writeback workqueue";
228348debafeSMikulas Patocka 		goto bad;
228448debafeSMikulas Patocka 	}
228548debafeSMikulas Patocka 	INIT_WORK(&wc->writeback_work, writecache_writeback);
228648debafeSMikulas Patocka 	INIT_WORK(&wc->flush_work, writecache_flush_work);
228748debafeSMikulas Patocka 
228895b88f4dSMikulas Patocka 	dm_iot_init(&wc->iot);
228995b88f4dSMikulas Patocka 
229048debafeSMikulas Patocka 	raw_spin_lock_init(&wc->endio_list_lock);
229148debafeSMikulas Patocka 	INIT_LIST_HEAD(&wc->endio_list);
2292f635237aSCai Huoqing 	wc->endio_thread = kthread_run(writecache_endio_thread, wc, "writecache_endio");
229348debafeSMikulas Patocka 	if (IS_ERR(wc->endio_thread)) {
229448debafeSMikulas Patocka 		r = PTR_ERR(wc->endio_thread);
229548debafeSMikulas Patocka 		wc->endio_thread = NULL;
229648debafeSMikulas Patocka 		ti->error = "Couldn't spawn endio thread";
229748debafeSMikulas Patocka 		goto bad;
229848debafeSMikulas Patocka 	}
229948debafeSMikulas Patocka 
230048debafeSMikulas Patocka 	/*
230148debafeSMikulas Patocka 	 * Parse the mode (pmem or ssd)
230248debafeSMikulas Patocka 	 */
230348debafeSMikulas Patocka 	string = dm_shift_arg(&as);
230448debafeSMikulas Patocka 	if (!string)
230548debafeSMikulas Patocka 		goto bad_arguments;
230648debafeSMikulas Patocka 
230748debafeSMikulas Patocka 	if (!strcasecmp(string, "s")) {
230848debafeSMikulas Patocka 		wc->pmem_mode = false;
230948debafeSMikulas Patocka 	} else if (!strcasecmp(string, "p")) {
231048debafeSMikulas Patocka #ifdef DM_WRITECACHE_HAS_PMEM
231148debafeSMikulas Patocka 		wc->pmem_mode = true;
231248debafeSMikulas Patocka 		wc->writeback_fua = true;
231348debafeSMikulas Patocka #else
231448debafeSMikulas Patocka 		/*
231548debafeSMikulas Patocka 		 * If the architecture doesn't support persistent memory or
231648debafeSMikulas Patocka 		 * the kernel doesn't support any DAX drivers, this driver can
231748debafeSMikulas Patocka 		 * only be used in SSD-only mode.
231848debafeSMikulas Patocka 		 */
231948debafeSMikulas Patocka 		r = -EOPNOTSUPP;
232048debafeSMikulas Patocka 		ti->error = "Persistent memory or DAX not supported on this system";
232148debafeSMikulas Patocka 		goto bad;
232248debafeSMikulas Patocka #endif
232348debafeSMikulas Patocka 	} else {
232448debafeSMikulas Patocka 		goto bad_arguments;
232548debafeSMikulas Patocka 	}
232648debafeSMikulas Patocka 
232748debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc)) {
232848debafeSMikulas Patocka 		r = bioset_init(&wc->bio_set, BIO_POOL_SIZE,
232948debafeSMikulas Patocka 				offsetof(struct writeback_struct, bio),
233048debafeSMikulas Patocka 				BIOSET_NEED_BVECS);
233148debafeSMikulas Patocka 		if (r) {
233248debafeSMikulas Patocka 			ti->error = "Could not allocate bio set";
233348debafeSMikulas Patocka 			goto bad;
233448debafeSMikulas Patocka 		}
233548debafeSMikulas Patocka 	} else {
23365c0de3d7SMikulas Patocka 		wc->pause = PAUSE_WRITEBACK;
233748debafeSMikulas Patocka 		r = mempool_init_kmalloc_pool(&wc->copy_pool, 1, sizeof(struct copy_struct));
233848debafeSMikulas Patocka 		if (r) {
233948debafeSMikulas Patocka 			ti->error = "Could not allocate mempool";
234048debafeSMikulas Patocka 			goto bad;
234148debafeSMikulas Patocka 		}
234248debafeSMikulas Patocka 	}
234348debafeSMikulas Patocka 
234448debafeSMikulas Patocka 	/*
234548debafeSMikulas Patocka 	 * Parse the origin data device
234648debafeSMikulas Patocka 	 */
234748debafeSMikulas Patocka 	string = dm_shift_arg(&as);
234848debafeSMikulas Patocka 	if (!string)
234948debafeSMikulas Patocka 		goto bad_arguments;
235048debafeSMikulas Patocka 	r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->dev);
235148debafeSMikulas Patocka 	if (r) {
235248debafeSMikulas Patocka 		ti->error = "Origin data device lookup failed";
235348debafeSMikulas Patocka 		goto bad;
235448debafeSMikulas Patocka 	}
235548debafeSMikulas Patocka 
235648debafeSMikulas Patocka 	/*
235748debafeSMikulas Patocka 	 * Parse cache data device (be it pmem or ssd)
235848debafeSMikulas Patocka 	 */
235948debafeSMikulas Patocka 	string = dm_shift_arg(&as);
236048debafeSMikulas Patocka 	if (!string)
236148debafeSMikulas Patocka 		goto bad_arguments;
236248debafeSMikulas Patocka 
236348debafeSMikulas Patocka 	r = dm_get_device(ti, string, dm_table_get_mode(ti->table), &wc->ssd_dev);
236448debafeSMikulas Patocka 	if (r) {
236548debafeSMikulas Patocka 		ti->error = "Cache data device lookup failed";
236648debafeSMikulas Patocka 		goto bad;
236748debafeSMikulas Patocka 	}
23686dcbb52cSChristoph Hellwig 	wc->memory_map_size = bdev_nr_bytes(wc->ssd_dev->bdev);
236948debafeSMikulas Patocka 
237048debafeSMikulas Patocka 	/*
237148debafeSMikulas Patocka 	 * Parse the cache block size
237248debafeSMikulas Patocka 	 */
237348debafeSMikulas Patocka 	string = dm_shift_arg(&as);
237448debafeSMikulas Patocka 	if (!string)
237548debafeSMikulas Patocka 		goto bad_arguments;
237648debafeSMikulas Patocka 	if (sscanf(string, "%u%c", &wc->block_size, &dummy) != 1 ||
237748debafeSMikulas Patocka 	    wc->block_size < 512 || wc->block_size > PAGE_SIZE ||
237848debafeSMikulas Patocka 	    (wc->block_size & (wc->block_size - 1))) {
237948debafeSMikulas Patocka 		r = -EINVAL;
238048debafeSMikulas Patocka 		ti->error = "Invalid block size";
238148debafeSMikulas Patocka 		goto bad;
238248debafeSMikulas Patocka 	}
238331b22120SMikulas Patocka 	if (wc->block_size < bdev_logical_block_size(wc->dev->bdev) ||
238431b22120SMikulas Patocka 	    wc->block_size < bdev_logical_block_size(wc->ssd_dev->bdev)) {
238531b22120SMikulas Patocka 		r = -EINVAL;
238631b22120SMikulas Patocka 		ti->error = "Block size is smaller than device logical block size";
238731b22120SMikulas Patocka 		goto bad;
238831b22120SMikulas Patocka 	}
238948debafeSMikulas Patocka 	wc->block_size_bits = __ffs(wc->block_size);
239048debafeSMikulas Patocka 
239148debafeSMikulas Patocka 	wc->max_writeback_jobs = MAX_WRITEBACK_JOBS;
239248debafeSMikulas Patocka 	wc->autocommit_blocks = !WC_MODE_PMEM(wc) ? AUTOCOMMIT_BLOCKS_SSD : AUTOCOMMIT_BLOCKS_PMEM;
239348debafeSMikulas Patocka 	wc->autocommit_jiffies = msecs_to_jiffies(AUTOCOMMIT_MSEC);
239448debafeSMikulas Patocka 
239548debafeSMikulas Patocka 	/*
239648debafeSMikulas Patocka 	 * Parse optional arguments
239748debafeSMikulas Patocka 	 */
239848debafeSMikulas Patocka 	r = dm_read_arg_group(_args, &as, &opt_params, &ti->error);
239948debafeSMikulas Patocka 	if (r)
240048debafeSMikulas Patocka 		goto bad;
240148debafeSMikulas Patocka 
240248debafeSMikulas Patocka 	while (opt_params) {
240348debafeSMikulas Patocka 		string = dm_shift_arg(&as), opt_params--;
2404d284f824SMikulas Patocka 		if (!strcasecmp(string, "start_sector") && opt_params >= 1) {
2405d284f824SMikulas Patocka 			unsigned long long start_sector;
24060ef0b471SHeinz Mauelshagen 
2407d284f824SMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
2408d284f824SMikulas Patocka 			if (sscanf(string, "%llu%c", &start_sector, &dummy) != 1)
2409d284f824SMikulas Patocka 				goto invalid_optional;
2410d284f824SMikulas Patocka 			wc->start_sector = start_sector;
2411054bee16SMikulas Patocka 			wc->start_sector_set = true;
2412d284f824SMikulas Patocka 			if (wc->start_sector != start_sector ||
2413d284f824SMikulas Patocka 			    wc->start_sector >= wc->memory_map_size >> SECTOR_SHIFT)
2414d284f824SMikulas Patocka 				goto invalid_optional;
2415d284f824SMikulas Patocka 		} else if (!strcasecmp(string, "high_watermark") && opt_params >= 1) {
241648debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
241748debafeSMikulas Patocka 			if (sscanf(string, "%d%c", &high_wm_percent, &dummy) != 1)
241848debafeSMikulas Patocka 				goto invalid_optional;
241948debafeSMikulas Patocka 			if (high_wm_percent < 0 || high_wm_percent > 100)
242048debafeSMikulas Patocka 				goto invalid_optional;
2421054bee16SMikulas Patocka 			wc->high_wm_percent_value = high_wm_percent;
242248debafeSMikulas Patocka 			wc->high_wm_percent_set = true;
242348debafeSMikulas Patocka 		} else if (!strcasecmp(string, "low_watermark") && opt_params >= 1) {
242448debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
242548debafeSMikulas Patocka 			if (sscanf(string, "%d%c", &low_wm_percent, &dummy) != 1)
242648debafeSMikulas Patocka 				goto invalid_optional;
242748debafeSMikulas Patocka 			if (low_wm_percent < 0 || low_wm_percent > 100)
242848debafeSMikulas Patocka 				goto invalid_optional;
2429054bee16SMikulas Patocka 			wc->low_wm_percent_value = low_wm_percent;
243048debafeSMikulas Patocka 			wc->low_wm_percent_set = true;
243148debafeSMikulas Patocka 		} else if (!strcasecmp(string, "writeback_jobs") && opt_params >= 1) {
243248debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
243348debafeSMikulas Patocka 			if (sscanf(string, "%u%c", &wc->max_writeback_jobs, &dummy) != 1)
243448debafeSMikulas Patocka 				goto invalid_optional;
243548debafeSMikulas Patocka 			wc->max_writeback_jobs_set = true;
243648debafeSMikulas Patocka 		} else if (!strcasecmp(string, "autocommit_blocks") && opt_params >= 1) {
243748debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
243848debafeSMikulas Patocka 			if (sscanf(string, "%u%c", &wc->autocommit_blocks, &dummy) != 1)
243948debafeSMikulas Patocka 				goto invalid_optional;
244048debafeSMikulas Patocka 			wc->autocommit_blocks_set = true;
244148debafeSMikulas Patocka 		} else if (!strcasecmp(string, "autocommit_time") && opt_params >= 1) {
244286a3238cSHeinz Mauelshagen 			unsigned int autocommit_msecs;
24430ef0b471SHeinz Mauelshagen 
244448debafeSMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
244548debafeSMikulas Patocka 			if (sscanf(string, "%u%c", &autocommit_msecs, &dummy) != 1)
244648debafeSMikulas Patocka 				goto invalid_optional;
244748debafeSMikulas Patocka 			if (autocommit_msecs > 3600000)
244848debafeSMikulas Patocka 				goto invalid_optional;
244948debafeSMikulas Patocka 			wc->autocommit_jiffies = msecs_to_jiffies(autocommit_msecs);
2450054bee16SMikulas Patocka 			wc->autocommit_time_value = autocommit_msecs;
245148debafeSMikulas Patocka 			wc->autocommit_time_set = true;
24523923d485SMikulas Patocka 		} else if (!strcasecmp(string, "max_age") && opt_params >= 1) {
245386a3238cSHeinz Mauelshagen 			unsigned int max_age_msecs;
24540ef0b471SHeinz Mauelshagen 
24553923d485SMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
24563923d485SMikulas Patocka 			if (sscanf(string, "%u%c", &max_age_msecs, &dummy) != 1)
24573923d485SMikulas Patocka 				goto invalid_optional;
24583923d485SMikulas Patocka 			if (max_age_msecs > 86400000)
24593923d485SMikulas Patocka 				goto invalid_optional;
24603923d485SMikulas Patocka 			wc->max_age = msecs_to_jiffies(max_age_msecs);
2461054bee16SMikulas Patocka 			wc->max_age_set = true;
2462054bee16SMikulas Patocka 			wc->max_age_value = max_age_msecs;
246393de44ebSMikulas Patocka 		} else if (!strcasecmp(string, "cleaner")) {
2464054bee16SMikulas Patocka 			wc->cleaner_set = true;
246593de44ebSMikulas Patocka 			wc->cleaner = true;
246648debafeSMikulas Patocka 		} else if (!strcasecmp(string, "fua")) {
246748debafeSMikulas Patocka 			if (WC_MODE_PMEM(wc)) {
246848debafeSMikulas Patocka 				wc->writeback_fua = true;
246948debafeSMikulas Patocka 				wc->writeback_fua_set = true;
247003b18887SHeinz Mauelshagen 			} else
247103b18887SHeinz Mauelshagen 				goto invalid_optional;
247248debafeSMikulas Patocka 		} else if (!strcasecmp(string, "nofua")) {
247348debafeSMikulas Patocka 			if (WC_MODE_PMEM(wc)) {
247448debafeSMikulas Patocka 				wc->writeback_fua = false;
247548debafeSMikulas Patocka 				wc->writeback_fua_set = true;
247603b18887SHeinz Mauelshagen 			} else
247703b18887SHeinz Mauelshagen 				goto invalid_optional;
2478611c3e16SMikulas Patocka 		} else if (!strcasecmp(string, "metadata_only")) {
2479611c3e16SMikulas Patocka 			wc->metadata_only = true;
24805c0de3d7SMikulas Patocka 		} else if (!strcasecmp(string, "pause_writeback") && opt_params >= 1) {
248186a3238cSHeinz Mauelshagen 			unsigned int pause_msecs;
24820ef0b471SHeinz Mauelshagen 
24835c0de3d7SMikulas Patocka 			if (WC_MODE_PMEM(wc))
24845c0de3d7SMikulas Patocka 				goto invalid_optional;
24855c0de3d7SMikulas Patocka 			string = dm_shift_arg(&as), opt_params--;
24865c0de3d7SMikulas Patocka 			if (sscanf(string, "%u%c", &pause_msecs, &dummy) != 1)
24875c0de3d7SMikulas Patocka 				goto invalid_optional;
24885c0de3d7SMikulas Patocka 			if (pause_msecs > 60000)
24895c0de3d7SMikulas Patocka 				goto invalid_optional;
24905c0de3d7SMikulas Patocka 			wc->pause = msecs_to_jiffies(pause_msecs);
24915c0de3d7SMikulas Patocka 			wc->pause_set = true;
24925c0de3d7SMikulas Patocka 			wc->pause_value = pause_msecs;
249348debafeSMikulas Patocka 		} else {
249448debafeSMikulas Patocka invalid_optional:
249548debafeSMikulas Patocka 			r = -EINVAL;
249648debafeSMikulas Patocka 			ti->error = "Invalid optional argument";
249748debafeSMikulas Patocka 			goto bad;
249848debafeSMikulas Patocka 		}
249948debafeSMikulas Patocka 	}
250048debafeSMikulas Patocka 
250148debafeSMikulas Patocka 	if (high_wm_percent < low_wm_percent) {
250248debafeSMikulas Patocka 		r = -EINVAL;
250348debafeSMikulas Patocka 		ti->error = "High watermark must be greater than or equal to low watermark";
250448debafeSMikulas Patocka 		goto bad;
250548debafeSMikulas Patocka 	}
250648debafeSMikulas Patocka 
2507d284f824SMikulas Patocka 	if (WC_MODE_PMEM(wc)) {
2508a4662458SMichal Suchanek 		if (!dax_synchronous(wc->ssd_dev->dax_dev)) {
2509a4662458SMichal Suchanek 			r = -EOPNOTSUPP;
2510a4662458SMichal Suchanek 			ti->error = "Asynchronous persistent memory not supported as pmem cache";
2511a4662458SMichal Suchanek 			goto bad;
2512a4662458SMichal Suchanek 		}
2513a4662458SMichal Suchanek 
2514d284f824SMikulas Patocka 		r = persistent_memory_claim(wc);
2515d284f824SMikulas Patocka 		if (r) {
2516d284f824SMikulas Patocka 			ti->error = "Unable to map persistent memory for cache";
2517d284f824SMikulas Patocka 			goto bad;
2518d284f824SMikulas Patocka 		}
2519d284f824SMikulas Patocka 	} else {
252048debafeSMikulas Patocka 		size_t n_blocks, n_metadata_blocks;
252148debafeSMikulas Patocka 		uint64_t n_bitmap_bits;
252248debafeSMikulas Patocka 
2523d284f824SMikulas Patocka 		wc->memory_map_size -= (uint64_t)wc->start_sector << SECTOR_SHIFT;
2524d284f824SMikulas Patocka 
252548debafeSMikulas Patocka 		bio_list_init(&wc->flush_list);
2526f635237aSCai Huoqing 		wc->flush_thread = kthread_run(writecache_flush_thread, wc, "dm_writecache_flush");
252748debafeSMikulas Patocka 		if (IS_ERR(wc->flush_thread)) {
252848debafeSMikulas Patocka 			r = PTR_ERR(wc->flush_thread);
252948debafeSMikulas Patocka 			wc->flush_thread = NULL;
2530e8ea141aSShenghui Wang 			ti->error = "Couldn't spawn flush thread";
253148debafeSMikulas Patocka 			goto bad;
253248debafeSMikulas Patocka 		}
253348debafeSMikulas Patocka 
253448debafeSMikulas Patocka 		r = calculate_memory_size(wc->memory_map_size, wc->block_size,
253548debafeSMikulas Patocka 					  &n_blocks, &n_metadata_blocks);
253648debafeSMikulas Patocka 		if (r) {
253748debafeSMikulas Patocka 			ti->error = "Invalid device size";
253848debafeSMikulas Patocka 			goto bad;
253948debafeSMikulas Patocka 		}
254048debafeSMikulas Patocka 
254148debafeSMikulas Patocka 		n_bitmap_bits = (((uint64_t)n_metadata_blocks << wc->block_size_bits) +
254248debafeSMikulas Patocka 				 BITMAP_GRANULARITY - 1) / BITMAP_GRANULARITY;
254348debafeSMikulas Patocka 		/* this is limitation of test_bit functions */
254448debafeSMikulas Patocka 		if (n_bitmap_bits > 1U << 31) {
254548debafeSMikulas Patocka 			r = -EFBIG;
254648debafeSMikulas Patocka 			ti->error = "Invalid device size";
254748debafeSMikulas Patocka 			goto bad;
254848debafeSMikulas Patocka 		}
254948debafeSMikulas Patocka 
255048debafeSMikulas Patocka 		wc->memory_map = vmalloc(n_metadata_blocks << wc->block_size_bits);
255148debafeSMikulas Patocka 		if (!wc->memory_map) {
255248debafeSMikulas Patocka 			r = -ENOMEM;
255348debafeSMikulas Patocka 			ti->error = "Unable to allocate memory for metadata";
255448debafeSMikulas Patocka 			goto bad;
255548debafeSMikulas Patocka 		}
255648debafeSMikulas Patocka 
255748debafeSMikulas Patocka 		wc->dm_kcopyd = dm_kcopyd_client_create(&dm_kcopyd_throttle);
255848debafeSMikulas Patocka 		if (IS_ERR(wc->dm_kcopyd)) {
255948debafeSMikulas Patocka 			r = PTR_ERR(wc->dm_kcopyd);
256048debafeSMikulas Patocka 			ti->error = "Unable to allocate dm-kcopyd client";
256148debafeSMikulas Patocka 			wc->dm_kcopyd = NULL;
256248debafeSMikulas Patocka 			goto bad;
256348debafeSMikulas Patocka 		}
256448debafeSMikulas Patocka 
256548debafeSMikulas Patocka 		wc->metadata_sectors = n_metadata_blocks << (wc->block_size_bits - SECTOR_SHIFT);
256648debafeSMikulas Patocka 		wc->dirty_bitmap_size = (n_bitmap_bits + BITS_PER_LONG - 1) /
256748debafeSMikulas Patocka 			BITS_PER_LONG * sizeof(unsigned long);
256848debafeSMikulas Patocka 		wc->dirty_bitmap = vzalloc(wc->dirty_bitmap_size);
256948debafeSMikulas Patocka 		if (!wc->dirty_bitmap) {
257048debafeSMikulas Patocka 			r = -ENOMEM;
257148debafeSMikulas Patocka 			ti->error = "Unable to allocate dirty bitmap";
257248debafeSMikulas Patocka 			goto bad;
257348debafeSMikulas Patocka 		}
257448debafeSMikulas Patocka 
257531b22120SMikulas Patocka 		r = writecache_read_metadata(wc, wc->block_size >> SECTOR_SHIFT);
257648debafeSMikulas Patocka 		if (r) {
257731b22120SMikulas Patocka 			ti->error = "Unable to read first block of metadata";
257848debafeSMikulas Patocka 			goto bad;
257948debafeSMikulas Patocka 		}
258048debafeSMikulas Patocka 	}
258148debafeSMikulas Patocka 
2582ec6347bbSDan Williams 	r = copy_mc_to_kernel(&s, sb(wc), sizeof(struct wc_memory_superblock));
258348debafeSMikulas Patocka 	if (r) {
258448debafeSMikulas Patocka 		ti->error = "Hardware memory error when reading superblock";
258548debafeSMikulas Patocka 		goto bad;
258648debafeSMikulas Patocka 	}
258748debafeSMikulas Patocka 	if (!le32_to_cpu(s.magic) && !le32_to_cpu(s.version)) {
258848debafeSMikulas Patocka 		r = init_memory(wc);
258948debafeSMikulas Patocka 		if (r) {
259048debafeSMikulas Patocka 			ti->error = "Unable to initialize device";
259148debafeSMikulas Patocka 			goto bad;
259248debafeSMikulas Patocka 		}
2593ec6347bbSDan Williams 		r = copy_mc_to_kernel(&s, sb(wc),
2594ec6347bbSDan Williams 				      sizeof(struct wc_memory_superblock));
259548debafeSMikulas Patocka 		if (r) {
259648debafeSMikulas Patocka 			ti->error = "Hardware memory error when reading superblock";
259748debafeSMikulas Patocka 			goto bad;
259848debafeSMikulas Patocka 		}
259948debafeSMikulas Patocka 	}
260048debafeSMikulas Patocka 
260148debafeSMikulas Patocka 	if (le32_to_cpu(s.magic) != MEMORY_SUPERBLOCK_MAGIC) {
260248debafeSMikulas Patocka 		ti->error = "Invalid magic in the superblock";
260348debafeSMikulas Patocka 		r = -EINVAL;
260448debafeSMikulas Patocka 		goto bad;
260548debafeSMikulas Patocka 	}
260648debafeSMikulas Patocka 
260748debafeSMikulas Patocka 	if (le32_to_cpu(s.version) != MEMORY_SUPERBLOCK_VERSION) {
260848debafeSMikulas Patocka 		ti->error = "Invalid version in the superblock";
260948debafeSMikulas Patocka 		r = -EINVAL;
261048debafeSMikulas Patocka 		goto bad;
261148debafeSMikulas Patocka 	}
261248debafeSMikulas Patocka 
261348debafeSMikulas Patocka 	if (le32_to_cpu(s.block_size) != wc->block_size) {
261448debafeSMikulas Patocka 		ti->error = "Block size does not match superblock";
261548debafeSMikulas Patocka 		r = -EINVAL;
261648debafeSMikulas Patocka 		goto bad;
261748debafeSMikulas Patocka 	}
261848debafeSMikulas Patocka 
261948debafeSMikulas Patocka 	wc->n_blocks = le64_to_cpu(s.n_blocks);
262048debafeSMikulas Patocka 
262148debafeSMikulas Patocka 	offset = wc->n_blocks * sizeof(struct wc_memory_entry);
262248debafeSMikulas Patocka 	if (offset / sizeof(struct wc_memory_entry) != le64_to_cpu(sb(wc)->n_blocks)) {
262348debafeSMikulas Patocka overflow:
262448debafeSMikulas Patocka 		ti->error = "Overflow in size calculation";
262548debafeSMikulas Patocka 		r = -EINVAL;
262648debafeSMikulas Patocka 		goto bad;
262748debafeSMikulas Patocka 	}
262848debafeSMikulas Patocka 	offset += sizeof(struct wc_memory_superblock);
262948debafeSMikulas Patocka 	if (offset < sizeof(struct wc_memory_superblock))
263048debafeSMikulas Patocka 		goto overflow;
263148debafeSMikulas Patocka 	offset = (offset + wc->block_size - 1) & ~(size_t)(wc->block_size - 1);
263248debafeSMikulas Patocka 	data_size = wc->n_blocks * (size_t)wc->block_size;
263348debafeSMikulas Patocka 	if (!offset || (data_size / wc->block_size != wc->n_blocks) ||
263448debafeSMikulas Patocka 	    (offset + data_size < offset))
263548debafeSMikulas Patocka 		goto overflow;
263648debafeSMikulas Patocka 	if (offset + data_size > wc->memory_map_size) {
263748debafeSMikulas Patocka 		ti->error = "Memory area is too small";
263848debafeSMikulas Patocka 		r = -EINVAL;
263948debafeSMikulas Patocka 		goto bad;
264048debafeSMikulas Patocka 	}
264148debafeSMikulas Patocka 
264248debafeSMikulas Patocka 	wc->metadata_sectors = offset >> SECTOR_SHIFT;
264348debafeSMikulas Patocka 	wc->block_start = (char *)sb(wc) + offset;
264448debafeSMikulas Patocka 
264548debafeSMikulas Patocka 	x = (uint64_t)wc->n_blocks * (100 - high_wm_percent);
264648debafeSMikulas Patocka 	x += 50;
264748debafeSMikulas Patocka 	do_div(x, 100);
264848debafeSMikulas Patocka 	wc->freelist_high_watermark = x;
264948debafeSMikulas Patocka 	x = (uint64_t)wc->n_blocks * (100 - low_wm_percent);
265048debafeSMikulas Patocka 	x += 50;
265148debafeSMikulas Patocka 	do_div(x, 100);
265248debafeSMikulas Patocka 	wc->freelist_low_watermark = x;
265348debafeSMikulas Patocka 
265493de44ebSMikulas Patocka 	if (wc->cleaner)
265593de44ebSMikulas Patocka 		activate_cleaner(wc);
265693de44ebSMikulas Patocka 
265748debafeSMikulas Patocka 	r = writecache_alloc_entries(wc);
265848debafeSMikulas Patocka 	if (r) {
265948debafeSMikulas Patocka 		ti->error = "Cannot allocate memory";
266048debafeSMikulas Patocka 		goto bad;
266148debafeSMikulas Patocka 	}
266248debafeSMikulas Patocka 
2663ee55b92aSMikulas Patocka 	ti->num_flush_bios = WC_MODE_PMEM(wc) ? 1 : 2;
266448debafeSMikulas Patocka 	ti->flush_supported = true;
266548debafeSMikulas Patocka 	ti->num_discard_bios = 1;
266648debafeSMikulas Patocka 
266748debafeSMikulas Patocka 	if (WC_MODE_PMEM(wc))
266848debafeSMikulas Patocka 		persistent_memory_flush_cache(wc->memory_map, wc->memory_map_size);
266948debafeSMikulas Patocka 
267048debafeSMikulas Patocka 	return 0;
267148debafeSMikulas Patocka 
267248debafeSMikulas Patocka bad_arguments:
267348debafeSMikulas Patocka 	r = -EINVAL;
267448debafeSMikulas Patocka 	ti->error = "Bad arguments";
267548debafeSMikulas Patocka bad:
267648debafeSMikulas Patocka 	writecache_dtr(ti);
267748debafeSMikulas Patocka 	return r;
267848debafeSMikulas Patocka }
267948debafeSMikulas Patocka 
writecache_status(struct dm_target * ti,status_type_t type,unsigned int status_flags,char * result,unsigned int maxlen)268048debafeSMikulas Patocka static void writecache_status(struct dm_target *ti, status_type_t type,
268186a3238cSHeinz Mauelshagen 			      unsigned int status_flags, char *result, unsigned int maxlen)
268248debafeSMikulas Patocka {
268348debafeSMikulas Patocka 	struct dm_writecache *wc = ti->private;
268486a3238cSHeinz Mauelshagen 	unsigned int extra_args;
268586a3238cSHeinz Mauelshagen 	unsigned int sz = 0;
268648debafeSMikulas Patocka 
268748debafeSMikulas Patocka 	switch (type) {
268848debafeSMikulas Patocka 	case STATUSTYPE_INFO:
2689e3a35d03SMikulas Patocka 		DMEMIT("%ld %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu %llu",
2690e3a35d03SMikulas Patocka 		       writecache_has_error(wc),
269148debafeSMikulas Patocka 		       (unsigned long long)wc->n_blocks, (unsigned long long)wc->freelist_size,
2692e3a35d03SMikulas Patocka 		       (unsigned long long)wc->writeback_size,
2693e3a35d03SMikulas Patocka 		       wc->stats.reads,
2694e3a35d03SMikulas Patocka 		       wc->stats.read_hits,
2695e3a35d03SMikulas Patocka 		       wc->stats.writes,
2696e3a35d03SMikulas Patocka 		       wc->stats.write_hits_uncommitted,
2697e3a35d03SMikulas Patocka 		       wc->stats.write_hits_committed,
2698e3a35d03SMikulas Patocka 		       wc->stats.writes_around,
2699e3a35d03SMikulas Patocka 		       wc->stats.writes_allocate,
2700e3a35d03SMikulas Patocka 		       wc->stats.writes_blocked_on_freelist,
2701e3a35d03SMikulas Patocka 		       wc->stats.flushes,
2702e3a35d03SMikulas Patocka 		       wc->stats.discards);
270348debafeSMikulas Patocka 		break;
270448debafeSMikulas Patocka 	case STATUSTYPE_TABLE:
270548debafeSMikulas Patocka 		DMEMIT("%c %s %s %u ", WC_MODE_PMEM(wc) ? 'p' : 's',
270648debafeSMikulas Patocka 				wc->dev->name, wc->ssd_dev->name, wc->block_size);
270748debafeSMikulas Patocka 		extra_args = 0;
2708054bee16SMikulas Patocka 		if (wc->start_sector_set)
27099ff07e7dSMikulas Patocka 			extra_args += 2;
2710054bee16SMikulas Patocka 		if (wc->high_wm_percent_set)
271148debafeSMikulas Patocka 			extra_args += 2;
2712054bee16SMikulas Patocka 		if (wc->low_wm_percent_set)
271348debafeSMikulas Patocka 			extra_args += 2;
271448debafeSMikulas Patocka 		if (wc->max_writeback_jobs_set)
271548debafeSMikulas Patocka 			extra_args += 2;
271648debafeSMikulas Patocka 		if (wc->autocommit_blocks_set)
271748debafeSMikulas Patocka 			extra_args += 2;
271848debafeSMikulas Patocka 		if (wc->autocommit_time_set)
271948debafeSMikulas Patocka 			extra_args += 2;
2720054bee16SMikulas Patocka 		if (wc->max_age_set)
2721e5d41cbcSMikulas Patocka 			extra_args += 2;
2722054bee16SMikulas Patocka 		if (wc->cleaner_set)
272393de44ebSMikulas Patocka 			extra_args++;
272448debafeSMikulas Patocka 		if (wc->writeback_fua_set)
272548debafeSMikulas Patocka 			extra_args++;
2726611c3e16SMikulas Patocka 		if (wc->metadata_only)
2727611c3e16SMikulas Patocka 			extra_args++;
27285c0de3d7SMikulas Patocka 		if (wc->pause_set)
27295c0de3d7SMikulas Patocka 			extra_args += 2;
273048debafeSMikulas Patocka 
273148debafeSMikulas Patocka 		DMEMIT("%u", extra_args);
2732054bee16SMikulas Patocka 		if (wc->start_sector_set)
27339ff07e7dSMikulas Patocka 			DMEMIT(" start_sector %llu", (unsigned long long)wc->start_sector);
2734054bee16SMikulas Patocka 		if (wc->high_wm_percent_set)
2735054bee16SMikulas Patocka 			DMEMIT(" high_watermark %u", wc->high_wm_percent_value);
2736054bee16SMikulas Patocka 		if (wc->low_wm_percent_set)
2737054bee16SMikulas Patocka 			DMEMIT(" low_watermark %u", wc->low_wm_percent_value);
273848debafeSMikulas Patocka 		if (wc->max_writeback_jobs_set)
273948debafeSMikulas Patocka 			DMEMIT(" writeback_jobs %u", wc->max_writeback_jobs);
274048debafeSMikulas Patocka 		if (wc->autocommit_blocks_set)
274148debafeSMikulas Patocka 			DMEMIT(" autocommit_blocks %u", wc->autocommit_blocks);
274248debafeSMikulas Patocka 		if (wc->autocommit_time_set)
2743054bee16SMikulas Patocka 			DMEMIT(" autocommit_time %u", wc->autocommit_time_value);
2744054bee16SMikulas Patocka 		if (wc->max_age_set)
2745054bee16SMikulas Patocka 			DMEMIT(" max_age %u", wc->max_age_value);
2746054bee16SMikulas Patocka 		if (wc->cleaner_set)
274793de44ebSMikulas Patocka 			DMEMIT(" cleaner");
274848debafeSMikulas Patocka 		if (wc->writeback_fua_set)
274948debafeSMikulas Patocka 			DMEMIT(" %sfua", wc->writeback_fua ? "" : "no");
2750611c3e16SMikulas Patocka 		if (wc->metadata_only)
2751611c3e16SMikulas Patocka 			DMEMIT(" metadata_only");
27525c0de3d7SMikulas Patocka 		if (wc->pause_set)
27535c0de3d7SMikulas Patocka 			DMEMIT(" pause_writeback %u", wc->pause_value);
275448debafeSMikulas Patocka 		break;
27558ec45662STushar Sugandhi 	case STATUSTYPE_IMA:
27568ec45662STushar Sugandhi 		*result = '\0';
27578ec45662STushar Sugandhi 		break;
275848debafeSMikulas Patocka 	}
275948debafeSMikulas Patocka }
276048debafeSMikulas Patocka 
276148debafeSMikulas Patocka static struct target_type writecache_target = {
276248debafeSMikulas Patocka 	.name			= "writecache",
2763e3a35d03SMikulas Patocka 	.version		= {1, 6, 0},
276448debafeSMikulas Patocka 	.module			= THIS_MODULE,
276548debafeSMikulas Patocka 	.ctr			= writecache_ctr,
276648debafeSMikulas Patocka 	.dtr			= writecache_dtr,
276748debafeSMikulas Patocka 	.status			= writecache_status,
276848debafeSMikulas Patocka 	.postsuspend		= writecache_suspend,
276948debafeSMikulas Patocka 	.resume			= writecache_resume,
277048debafeSMikulas Patocka 	.message		= writecache_message,
277148debafeSMikulas Patocka 	.map			= writecache_map,
277248debafeSMikulas Patocka 	.end_io			= writecache_end_io,
277348debafeSMikulas Patocka 	.iterate_devices	= writecache_iterate_devices,
277448debafeSMikulas Patocka 	.io_hints		= writecache_io_hints,
277548debafeSMikulas Patocka };
27763664ff82SYangtao Li module_dm(writecache);
277748debafeSMikulas Patocka 
277848debafeSMikulas Patocka MODULE_DESCRIPTION(DM_NAME " writecache target");
2779*fa34e589SMike Snitzer MODULE_AUTHOR("Mikulas Patocka <dm-devel@lists.linux.dev>");
278048debafeSMikulas Patocka MODULE_LICENSE("GPL");
2781