xref: /linux/drivers/md/dm-era-target.c (revision 2524933307fd0036d5c32357c693c021ab09a0b0)
109c434b8SThomas Gleixner // SPDX-License-Identifier: GPL-2.0-only
2eec40579SJoe Thornber #include "dm.h"
3eec40579SJoe Thornber #include "persistent-data/dm-transaction-manager.h"
4eec40579SJoe Thornber #include "persistent-data/dm-bitset.h"
5eec40579SJoe Thornber #include "persistent-data/dm-space-map.h"
6eec40579SJoe Thornber 
7eec40579SJoe Thornber #include <linux/dm-io.h>
8eec40579SJoe Thornber #include <linux/dm-kcopyd.h>
9eec40579SJoe Thornber #include <linux/init.h>
10eec40579SJoe Thornber #include <linux/mempool.h>
11eec40579SJoe Thornber #include <linux/module.h>
12eec40579SJoe Thornber #include <linux/slab.h>
13eec40579SJoe Thornber #include <linux/vmalloc.h>
14eec40579SJoe Thornber 
15eec40579SJoe Thornber #define DM_MSG_PREFIX "era"
16eec40579SJoe Thornber 
17eec40579SJoe Thornber #define SUPERBLOCK_LOCATION 0
18eec40579SJoe Thornber #define SUPERBLOCK_MAGIC 2126579579
19eec40579SJoe Thornber #define SUPERBLOCK_CSUM_XOR 146538381
20eec40579SJoe Thornber #define MIN_ERA_VERSION 1
21eec40579SJoe Thornber #define MAX_ERA_VERSION 1
22eec40579SJoe Thornber #define INVALID_WRITESET_ROOT SUPERBLOCK_LOCATION
23eec40579SJoe Thornber #define MIN_BLOCK_SIZE 8
24eec40579SJoe Thornber 
25eec40579SJoe Thornber /*----------------------------------------------------------------
26eec40579SJoe Thornber  * Writeset
27eec40579SJoe Thornber  *--------------------------------------------------------------*/
28eec40579SJoe Thornber struct writeset_metadata {
29eec40579SJoe Thornber 	uint32_t nr_bits;
30eec40579SJoe Thornber 	dm_block_t root;
31eec40579SJoe Thornber };
32eec40579SJoe Thornber 
33eec40579SJoe Thornber struct writeset {
34eec40579SJoe Thornber 	struct writeset_metadata md;
35eec40579SJoe Thornber 
36eec40579SJoe Thornber 	/*
37eec40579SJoe Thornber 	 * An in core copy of the bits to save constantly doing look ups on
38eec40579SJoe Thornber 	 * disk.
39eec40579SJoe Thornber 	 */
40eec40579SJoe Thornber 	unsigned long *bits;
41eec40579SJoe Thornber };
42eec40579SJoe Thornber 
43eec40579SJoe Thornber /*
44eec40579SJoe Thornber  * This does not free off the on disk bitset as this will normally be done
45eec40579SJoe Thornber  * after digesting into the era array.
46eec40579SJoe Thornber  */
47eec40579SJoe Thornber static void writeset_free(struct writeset *ws)
48eec40579SJoe Thornber {
49eec40579SJoe Thornber 	vfree(ws->bits);
50eec40579SJoe Thornber }
51eec40579SJoe Thornber 
52eec40579SJoe Thornber static int setup_on_disk_bitset(struct dm_disk_bitset *info,
53eec40579SJoe Thornber 				unsigned nr_bits, dm_block_t *root)
54eec40579SJoe Thornber {
55eec40579SJoe Thornber 	int r;
56eec40579SJoe Thornber 
57eec40579SJoe Thornber 	r = dm_bitset_empty(info, root);
58eec40579SJoe Thornber 	if (r)
59eec40579SJoe Thornber 		return r;
60eec40579SJoe Thornber 
61eec40579SJoe Thornber 	return dm_bitset_resize(info, *root, 0, nr_bits, false, root);
62eec40579SJoe Thornber }
63eec40579SJoe Thornber 
64eec40579SJoe Thornber static size_t bitset_size(unsigned nr_bits)
65eec40579SJoe Thornber {
66eec40579SJoe Thornber 	return sizeof(unsigned long) * dm_div_up(nr_bits, BITS_PER_LONG);
67eec40579SJoe Thornber }
68eec40579SJoe Thornber 
69eec40579SJoe Thornber /*
70eec40579SJoe Thornber  * Allocates memory for the in core bitset.
71eec40579SJoe Thornber  */
72eec40579SJoe Thornber static int writeset_alloc(struct writeset *ws, dm_block_t nr_blocks)
73eec40579SJoe Thornber {
74eec40579SJoe Thornber 	ws->bits = vzalloc(bitset_size(nr_blocks));
75eec40579SJoe Thornber 	if (!ws->bits) {
76eec40579SJoe Thornber 		DMERR("%s: couldn't allocate in memory bitset", __func__);
77eec40579SJoe Thornber 		return -ENOMEM;
78eec40579SJoe Thornber 	}
79eec40579SJoe Thornber 
80eec40579SJoe Thornber 	return 0;
81eec40579SJoe Thornber }
82eec40579SJoe Thornber 
83eec40579SJoe Thornber /*
84eec40579SJoe Thornber  * Wipes the in-core bitset, and creates a new on disk bitset.
85eec40579SJoe Thornber  */
86de89afc1SNikos Tsironis static int writeset_init(struct dm_disk_bitset *info, struct writeset *ws,
87de89afc1SNikos Tsironis 			 dm_block_t nr_blocks)
88eec40579SJoe Thornber {
89eec40579SJoe Thornber 	int r;
90eec40579SJoe Thornber 
91de89afc1SNikos Tsironis 	memset(ws->bits, 0, bitset_size(nr_blocks));
92eec40579SJoe Thornber 
93de89afc1SNikos Tsironis 	ws->md.nr_bits = nr_blocks;
94eec40579SJoe Thornber 	r = setup_on_disk_bitset(info, ws->md.nr_bits, &ws->md.root);
95eec40579SJoe Thornber 	if (r) {
96eec40579SJoe Thornber 		DMERR("%s: setup_on_disk_bitset failed", __func__);
97eec40579SJoe Thornber 		return r;
98eec40579SJoe Thornber 	}
99eec40579SJoe Thornber 
100eec40579SJoe Thornber 	return 0;
101eec40579SJoe Thornber }
102eec40579SJoe Thornber 
103eec40579SJoe Thornber static bool writeset_marked(struct writeset *ws, dm_block_t block)
104eec40579SJoe Thornber {
105eec40579SJoe Thornber 	return test_bit(block, ws->bits);
106eec40579SJoe Thornber }
107eec40579SJoe Thornber 
108eec40579SJoe Thornber static int writeset_marked_on_disk(struct dm_disk_bitset *info,
109eec40579SJoe Thornber 				   struct writeset_metadata *m, dm_block_t block,
110eec40579SJoe Thornber 				   bool *result)
111eec40579SJoe Thornber {
112eec40579SJoe Thornber 	dm_block_t old = m->root;
113eec40579SJoe Thornber 
114eec40579SJoe Thornber 	/*
115eec40579SJoe Thornber 	 * The bitset was flushed when it was archived, so we know there'll
116eec40579SJoe Thornber 	 * be no change to the root.
117eec40579SJoe Thornber 	 */
118eec40579SJoe Thornber 	int r = dm_bitset_test_bit(info, m->root, block, &m->root, result);
119eec40579SJoe Thornber 	if (r) {
120eec40579SJoe Thornber 		DMERR("%s: dm_bitset_test_bit failed", __func__);
121eec40579SJoe Thornber 		return r;
122eec40579SJoe Thornber 	}
123eec40579SJoe Thornber 
124eec40579SJoe Thornber 	BUG_ON(m->root != old);
125eec40579SJoe Thornber 
126eec40579SJoe Thornber 	return r;
127eec40579SJoe Thornber }
128eec40579SJoe Thornber 
129eec40579SJoe Thornber /*
130eec40579SJoe Thornber  * Returns < 0 on error, 0 if the bit wasn't previously set, 1 if it was.
131eec40579SJoe Thornber  */
132eec40579SJoe Thornber static int writeset_test_and_set(struct dm_disk_bitset *info,
133eec40579SJoe Thornber 				 struct writeset *ws, uint32_t block)
134eec40579SJoe Thornber {
135eec40579SJoe Thornber 	int r;
136eec40579SJoe Thornber 
1372099b145SNikos Tsironis 	if (!test_bit(block, ws->bits)) {
138eec40579SJoe Thornber 		r = dm_bitset_set_bit(info, ws->md.root, block, &ws->md.root);
139eec40579SJoe Thornber 		if (r) {
140eec40579SJoe Thornber 			/* FIXME: fail mode */
141eec40579SJoe Thornber 			return r;
142eec40579SJoe Thornber 		}
143eec40579SJoe Thornber 
144eec40579SJoe Thornber 		return 0;
145eec40579SJoe Thornber 	}
146eec40579SJoe Thornber 
147eec40579SJoe Thornber 	return 1;
148eec40579SJoe Thornber }
149eec40579SJoe Thornber 
150eec40579SJoe Thornber /*----------------------------------------------------------------
151eec40579SJoe Thornber  * On disk metadata layout
152eec40579SJoe Thornber  *--------------------------------------------------------------*/
153eec40579SJoe Thornber #define SPACE_MAP_ROOT_SIZE 128
154eec40579SJoe Thornber #define UUID_LEN 16
155eec40579SJoe Thornber 
156eec40579SJoe Thornber struct writeset_disk {
157eec40579SJoe Thornber 	__le32 nr_bits;
158eec40579SJoe Thornber 	__le64 root;
159eec40579SJoe Thornber } __packed;
160eec40579SJoe Thornber 
161eec40579SJoe Thornber struct superblock_disk {
162eec40579SJoe Thornber 	__le32 csum;
163eec40579SJoe Thornber 	__le32 flags;
164eec40579SJoe Thornber 	__le64 blocknr;
165eec40579SJoe Thornber 
166eec40579SJoe Thornber 	__u8 uuid[UUID_LEN];
167eec40579SJoe Thornber 	__le64 magic;
168eec40579SJoe Thornber 	__le32 version;
169eec40579SJoe Thornber 
170eec40579SJoe Thornber 	__u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
171eec40579SJoe Thornber 
172eec40579SJoe Thornber 	__le32 data_block_size;
173eec40579SJoe Thornber 	__le32 metadata_block_size;
174eec40579SJoe Thornber 	__le32 nr_blocks;
175eec40579SJoe Thornber 
176eec40579SJoe Thornber 	__le32 current_era;
177eec40579SJoe Thornber 	struct writeset_disk current_writeset;
178eec40579SJoe Thornber 
179eec40579SJoe Thornber 	/*
180eec40579SJoe Thornber 	 * Only these two fields are valid within the metadata snapshot.
181eec40579SJoe Thornber 	 */
182eec40579SJoe Thornber 	__le64 writeset_tree_root;
183eec40579SJoe Thornber 	__le64 era_array_root;
184eec40579SJoe Thornber 
185eec40579SJoe Thornber 	__le64 metadata_snap;
186eec40579SJoe Thornber } __packed;
187eec40579SJoe Thornber 
188eec40579SJoe Thornber /*----------------------------------------------------------------
189eec40579SJoe Thornber  * Superblock validation
190eec40579SJoe Thornber  *--------------------------------------------------------------*/
191eec40579SJoe Thornber static void sb_prepare_for_write(struct dm_block_validator *v,
192eec40579SJoe Thornber 				 struct dm_block *b,
193eec40579SJoe Thornber 				 size_t sb_block_size)
194eec40579SJoe Thornber {
195eec40579SJoe Thornber 	struct superblock_disk *disk = dm_block_data(b);
196eec40579SJoe Thornber 
197eec40579SJoe Thornber 	disk->blocknr = cpu_to_le64(dm_block_location(b));
198eec40579SJoe Thornber 	disk->csum = cpu_to_le32(dm_bm_checksum(&disk->flags,
199eec40579SJoe Thornber 						sb_block_size - sizeof(__le32),
200eec40579SJoe Thornber 						SUPERBLOCK_CSUM_XOR));
201eec40579SJoe Thornber }
202eec40579SJoe Thornber 
203eec40579SJoe Thornber static int check_metadata_version(struct superblock_disk *disk)
204eec40579SJoe Thornber {
205eec40579SJoe Thornber 	uint32_t metadata_version = le32_to_cpu(disk->version);
206eec40579SJoe Thornber 	if (metadata_version < MIN_ERA_VERSION || metadata_version > MAX_ERA_VERSION) {
207eec40579SJoe Thornber 		DMERR("Era metadata version %u found, but only versions between %u and %u supported.",
208eec40579SJoe Thornber 		      metadata_version, MIN_ERA_VERSION, MAX_ERA_VERSION);
209eec40579SJoe Thornber 		return -EINVAL;
210eec40579SJoe Thornber 	}
211eec40579SJoe Thornber 
212eec40579SJoe Thornber 	return 0;
213eec40579SJoe Thornber }
214eec40579SJoe Thornber 
215eec40579SJoe Thornber static int sb_check(struct dm_block_validator *v,
216eec40579SJoe Thornber 		    struct dm_block *b,
217eec40579SJoe Thornber 		    size_t sb_block_size)
218eec40579SJoe Thornber {
219eec40579SJoe Thornber 	struct superblock_disk *disk = dm_block_data(b);
220eec40579SJoe Thornber 	__le32 csum_le;
221eec40579SJoe Thornber 
222eec40579SJoe Thornber 	if (dm_block_location(b) != le64_to_cpu(disk->blocknr)) {
223eec40579SJoe Thornber 		DMERR("sb_check failed: blocknr %llu: wanted %llu",
224eec40579SJoe Thornber 		      le64_to_cpu(disk->blocknr),
225eec40579SJoe Thornber 		      (unsigned long long)dm_block_location(b));
226eec40579SJoe Thornber 		return -ENOTBLK;
227eec40579SJoe Thornber 	}
228eec40579SJoe Thornber 
229eec40579SJoe Thornber 	if (le64_to_cpu(disk->magic) != SUPERBLOCK_MAGIC) {
230eec40579SJoe Thornber 		DMERR("sb_check failed: magic %llu: wanted %llu",
231eec40579SJoe Thornber 		      le64_to_cpu(disk->magic),
232eec40579SJoe Thornber 		      (unsigned long long) SUPERBLOCK_MAGIC);
233eec40579SJoe Thornber 		return -EILSEQ;
234eec40579SJoe Thornber 	}
235eec40579SJoe Thornber 
236eec40579SJoe Thornber 	csum_le = cpu_to_le32(dm_bm_checksum(&disk->flags,
237eec40579SJoe Thornber 					     sb_block_size - sizeof(__le32),
238eec40579SJoe Thornber 					     SUPERBLOCK_CSUM_XOR));
239eec40579SJoe Thornber 	if (csum_le != disk->csum) {
240eec40579SJoe Thornber 		DMERR("sb_check failed: csum %u: wanted %u",
241eec40579SJoe Thornber 		      le32_to_cpu(csum_le), le32_to_cpu(disk->csum));
242eec40579SJoe Thornber 		return -EILSEQ;
243eec40579SJoe Thornber 	}
244eec40579SJoe Thornber 
245eec40579SJoe Thornber 	return check_metadata_version(disk);
246eec40579SJoe Thornber }
247eec40579SJoe Thornber 
248eec40579SJoe Thornber static struct dm_block_validator sb_validator = {
249eec40579SJoe Thornber 	.name = "superblock",
250eec40579SJoe Thornber 	.prepare_for_write = sb_prepare_for_write,
251eec40579SJoe Thornber 	.check = sb_check
252eec40579SJoe Thornber };
253eec40579SJoe Thornber 
254eec40579SJoe Thornber /*----------------------------------------------------------------
255eec40579SJoe Thornber  * Low level metadata handling
256eec40579SJoe Thornber  *--------------------------------------------------------------*/
257eec40579SJoe Thornber #define DM_ERA_METADATA_BLOCK_SIZE 4096
258eec40579SJoe Thornber #define ERA_MAX_CONCURRENT_LOCKS 5
259eec40579SJoe Thornber 
260eec40579SJoe Thornber struct era_metadata {
261eec40579SJoe Thornber 	struct block_device *bdev;
262eec40579SJoe Thornber 	struct dm_block_manager *bm;
263eec40579SJoe Thornber 	struct dm_space_map *sm;
264eec40579SJoe Thornber 	struct dm_transaction_manager *tm;
265eec40579SJoe Thornber 
266eec40579SJoe Thornber 	dm_block_t block_size;
267eec40579SJoe Thornber 	uint32_t nr_blocks;
268eec40579SJoe Thornber 
269eec40579SJoe Thornber 	uint32_t current_era;
270eec40579SJoe Thornber 
271eec40579SJoe Thornber 	/*
272eec40579SJoe Thornber 	 * We preallocate 2 writesets.  When an era rolls over we
273eec40579SJoe Thornber 	 * switch between them. This means the allocation is done at
274eec40579SJoe Thornber 	 * preresume time, rather than on the io path.
275eec40579SJoe Thornber 	 */
276eec40579SJoe Thornber 	struct writeset writesets[2];
277eec40579SJoe Thornber 	struct writeset *current_writeset;
278eec40579SJoe Thornber 
279eec40579SJoe Thornber 	dm_block_t writeset_tree_root;
280eec40579SJoe Thornber 	dm_block_t era_array_root;
281eec40579SJoe Thornber 
282eec40579SJoe Thornber 	struct dm_disk_bitset bitset_info;
283eec40579SJoe Thornber 	struct dm_btree_info writeset_tree_info;
284eec40579SJoe Thornber 	struct dm_array_info era_array_info;
285eec40579SJoe Thornber 
286eec40579SJoe Thornber 	dm_block_t metadata_snap;
287eec40579SJoe Thornber 
288eec40579SJoe Thornber 	/*
289eec40579SJoe Thornber 	 * A flag that is set whenever a writeset has been archived.
290eec40579SJoe Thornber 	 */
291eec40579SJoe Thornber 	bool archived_writesets;
2925a32083dSJoe Thornber 
2935a32083dSJoe Thornber 	/*
2945a32083dSJoe Thornber 	 * Reading the space map root can fail, so we read it into this
2955a32083dSJoe Thornber 	 * buffer before the superblock is locked and updated.
2965a32083dSJoe Thornber 	 */
2975a32083dSJoe Thornber 	__u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
298eec40579SJoe Thornber };
299eec40579SJoe Thornber 
300eec40579SJoe Thornber static int superblock_read_lock(struct era_metadata *md,
301eec40579SJoe Thornber 				struct dm_block **sblock)
302eec40579SJoe Thornber {
303eec40579SJoe Thornber 	return dm_bm_read_lock(md->bm, SUPERBLOCK_LOCATION,
304eec40579SJoe Thornber 			       &sb_validator, sblock);
305eec40579SJoe Thornber }
306eec40579SJoe Thornber 
307eec40579SJoe Thornber static int superblock_lock_zero(struct era_metadata *md,
308eec40579SJoe Thornber 				struct dm_block **sblock)
309eec40579SJoe Thornber {
310eec40579SJoe Thornber 	return dm_bm_write_lock_zero(md->bm, SUPERBLOCK_LOCATION,
311eec40579SJoe Thornber 				     &sb_validator, sblock);
312eec40579SJoe Thornber }
313eec40579SJoe Thornber 
314eec40579SJoe Thornber static int superblock_lock(struct era_metadata *md,
315eec40579SJoe Thornber 			   struct dm_block **sblock)
316eec40579SJoe Thornber {
317eec40579SJoe Thornber 	return dm_bm_write_lock(md->bm, SUPERBLOCK_LOCATION,
318eec40579SJoe Thornber 				&sb_validator, sblock);
319eec40579SJoe Thornber }
320eec40579SJoe Thornber 
321eec40579SJoe Thornber /* FIXME: duplication with cache and thin */
322eec40579SJoe Thornber static int superblock_all_zeroes(struct dm_block_manager *bm, bool *result)
323eec40579SJoe Thornber {
324eec40579SJoe Thornber 	int r;
325eec40579SJoe Thornber 	unsigned i;
326eec40579SJoe Thornber 	struct dm_block *b;
327eec40579SJoe Thornber 	__le64 *data_le, zero = cpu_to_le64(0);
328eec40579SJoe Thornber 	unsigned sb_block_size = dm_bm_block_size(bm) / sizeof(__le64);
329eec40579SJoe Thornber 
330eec40579SJoe Thornber 	/*
331eec40579SJoe Thornber 	 * We can't use a validator here - it may be all zeroes.
332eec40579SJoe Thornber 	 */
333eec40579SJoe Thornber 	r = dm_bm_read_lock(bm, SUPERBLOCK_LOCATION, NULL, &b);
334eec40579SJoe Thornber 	if (r)
335eec40579SJoe Thornber 		return r;
336eec40579SJoe Thornber 
337eec40579SJoe Thornber 	data_le = dm_block_data(b);
338eec40579SJoe Thornber 	*result = true;
339eec40579SJoe Thornber 	for (i = 0; i < sb_block_size; i++) {
340eec40579SJoe Thornber 		if (data_le[i] != zero) {
341eec40579SJoe Thornber 			*result = false;
342eec40579SJoe Thornber 			break;
343eec40579SJoe Thornber 		}
344eec40579SJoe Thornber 	}
345eec40579SJoe Thornber 
3464c7da06fSMikulas Patocka 	dm_bm_unlock(b);
3474c7da06fSMikulas Patocka 
3484c7da06fSMikulas Patocka 	return 0;
349eec40579SJoe Thornber }
350eec40579SJoe Thornber 
351eec40579SJoe Thornber /*----------------------------------------------------------------*/
352eec40579SJoe Thornber 
353eec40579SJoe Thornber static void ws_pack(const struct writeset_metadata *core, struct writeset_disk *disk)
354eec40579SJoe Thornber {
355eec40579SJoe Thornber 	disk->nr_bits = cpu_to_le32(core->nr_bits);
356eec40579SJoe Thornber 	disk->root = cpu_to_le64(core->root);
357eec40579SJoe Thornber }
358eec40579SJoe Thornber 
359eec40579SJoe Thornber static void ws_unpack(const struct writeset_disk *disk, struct writeset_metadata *core)
360eec40579SJoe Thornber {
361eec40579SJoe Thornber 	core->nr_bits = le32_to_cpu(disk->nr_bits);
362eec40579SJoe Thornber 	core->root = le64_to_cpu(disk->root);
363eec40579SJoe Thornber }
364eec40579SJoe Thornber 
365eec40579SJoe Thornber static void ws_inc(void *context, const void *value)
366eec40579SJoe Thornber {
367eec40579SJoe Thornber 	struct era_metadata *md = context;
368eec40579SJoe Thornber 	struct writeset_disk ws_d;
369eec40579SJoe Thornber 	dm_block_t b;
370eec40579SJoe Thornber 
371eec40579SJoe Thornber 	memcpy(&ws_d, value, sizeof(ws_d));
372eec40579SJoe Thornber 	b = le64_to_cpu(ws_d.root);
373eec40579SJoe Thornber 
374eec40579SJoe Thornber 	dm_tm_inc(md->tm, b);
375eec40579SJoe Thornber }
376eec40579SJoe Thornber 
377eec40579SJoe Thornber static void ws_dec(void *context, const void *value)
378eec40579SJoe Thornber {
379eec40579SJoe Thornber 	struct era_metadata *md = context;
380eec40579SJoe Thornber 	struct writeset_disk ws_d;
381eec40579SJoe Thornber 	dm_block_t b;
382eec40579SJoe Thornber 
383eec40579SJoe Thornber 	memcpy(&ws_d, value, sizeof(ws_d));
384eec40579SJoe Thornber 	b = le64_to_cpu(ws_d.root);
385eec40579SJoe Thornber 
386eec40579SJoe Thornber 	dm_bitset_del(&md->bitset_info, b);
387eec40579SJoe Thornber }
388eec40579SJoe Thornber 
389eec40579SJoe Thornber static int ws_eq(void *context, const void *value1, const void *value2)
390eec40579SJoe Thornber {
391eec40579SJoe Thornber 	return !memcmp(value1, value2, sizeof(struct writeset_metadata));
392eec40579SJoe Thornber }
393eec40579SJoe Thornber 
394eec40579SJoe Thornber /*----------------------------------------------------------------*/
395eec40579SJoe Thornber 
396eec40579SJoe Thornber static void setup_writeset_tree_info(struct era_metadata *md)
397eec40579SJoe Thornber {
398eec40579SJoe Thornber 	struct dm_btree_value_type *vt = &md->writeset_tree_info.value_type;
399eec40579SJoe Thornber 	md->writeset_tree_info.tm = md->tm;
400eec40579SJoe Thornber 	md->writeset_tree_info.levels = 1;
401eec40579SJoe Thornber 	vt->context = md;
402eec40579SJoe Thornber 	vt->size = sizeof(struct writeset_disk);
403eec40579SJoe Thornber 	vt->inc = ws_inc;
404eec40579SJoe Thornber 	vt->dec = ws_dec;
405eec40579SJoe Thornber 	vt->equal = ws_eq;
406eec40579SJoe Thornber }
407eec40579SJoe Thornber 
408eec40579SJoe Thornber static void setup_era_array_info(struct era_metadata *md)
409eec40579SJoe Thornber 
410eec40579SJoe Thornber {
411eec40579SJoe Thornber 	struct dm_btree_value_type vt;
412eec40579SJoe Thornber 	vt.context = NULL;
413eec40579SJoe Thornber 	vt.size = sizeof(__le32);
414eec40579SJoe Thornber 	vt.inc = NULL;
415eec40579SJoe Thornber 	vt.dec = NULL;
416eec40579SJoe Thornber 	vt.equal = NULL;
417eec40579SJoe Thornber 
418eec40579SJoe Thornber 	dm_array_info_init(&md->era_array_info, md->tm, &vt);
419eec40579SJoe Thornber }
420eec40579SJoe Thornber 
421eec40579SJoe Thornber static void setup_infos(struct era_metadata *md)
422eec40579SJoe Thornber {
423eec40579SJoe Thornber 	dm_disk_bitset_init(md->tm, &md->bitset_info);
424eec40579SJoe Thornber 	setup_writeset_tree_info(md);
425eec40579SJoe Thornber 	setup_era_array_info(md);
426eec40579SJoe Thornber }
427eec40579SJoe Thornber 
428eec40579SJoe Thornber /*----------------------------------------------------------------*/
429eec40579SJoe Thornber 
430eec40579SJoe Thornber static int create_fresh_metadata(struct era_metadata *md)
431eec40579SJoe Thornber {
432eec40579SJoe Thornber 	int r;
433eec40579SJoe Thornber 
434eec40579SJoe Thornber 	r = dm_tm_create_with_sm(md->bm, SUPERBLOCK_LOCATION,
435eec40579SJoe Thornber 				 &md->tm, &md->sm);
436eec40579SJoe Thornber 	if (r < 0) {
437eec40579SJoe Thornber 		DMERR("dm_tm_create_with_sm failed");
438eec40579SJoe Thornber 		return r;
439eec40579SJoe Thornber 	}
440eec40579SJoe Thornber 
441eec40579SJoe Thornber 	setup_infos(md);
442eec40579SJoe Thornber 
443eec40579SJoe Thornber 	r = dm_btree_empty(&md->writeset_tree_info, &md->writeset_tree_root);
444eec40579SJoe Thornber 	if (r) {
445eec40579SJoe Thornber 		DMERR("couldn't create new writeset tree");
446eec40579SJoe Thornber 		goto bad;
447eec40579SJoe Thornber 	}
448eec40579SJoe Thornber 
449eec40579SJoe Thornber 	r = dm_array_empty(&md->era_array_info, &md->era_array_root);
450eec40579SJoe Thornber 	if (r) {
451eec40579SJoe Thornber 		DMERR("couldn't create era array");
452eec40579SJoe Thornber 		goto bad;
453eec40579SJoe Thornber 	}
454eec40579SJoe Thornber 
455eec40579SJoe Thornber 	return 0;
456eec40579SJoe Thornber 
457eec40579SJoe Thornber bad:
458eec40579SJoe Thornber 	dm_sm_destroy(md->sm);
459eec40579SJoe Thornber 	dm_tm_destroy(md->tm);
460eec40579SJoe Thornber 
461eec40579SJoe Thornber 	return r;
462eec40579SJoe Thornber }
463eec40579SJoe Thornber 
4645a32083dSJoe Thornber static int save_sm_root(struct era_metadata *md)
4655a32083dSJoe Thornber {
4665a32083dSJoe Thornber 	int r;
4675a32083dSJoe Thornber 	size_t metadata_len;
4685a32083dSJoe Thornber 
4695a32083dSJoe Thornber 	r = dm_sm_root_size(md->sm, &metadata_len);
4705a32083dSJoe Thornber 	if (r < 0)
4715a32083dSJoe Thornber 		return r;
4725a32083dSJoe Thornber 
4735a32083dSJoe Thornber 	return dm_sm_copy_root(md->sm, &md->metadata_space_map_root,
4745a32083dSJoe Thornber 			       metadata_len);
4755a32083dSJoe Thornber }
4765a32083dSJoe Thornber 
4775a32083dSJoe Thornber static void copy_sm_root(struct era_metadata *md, struct superblock_disk *disk)
4785a32083dSJoe Thornber {
4795a32083dSJoe Thornber 	memcpy(&disk->metadata_space_map_root,
4805a32083dSJoe Thornber 	       &md->metadata_space_map_root,
4815a32083dSJoe Thornber 	       sizeof(md->metadata_space_map_root));
4825a32083dSJoe Thornber }
4835a32083dSJoe Thornber 
484eec40579SJoe Thornber /*
485eec40579SJoe Thornber  * Writes a superblock, including the static fields that don't get updated
486eec40579SJoe Thornber  * with every commit (possible optimisation here).  'md' should be fully
487eec40579SJoe Thornber  * constructed when this is called.
488eec40579SJoe Thornber  */
4895a32083dSJoe Thornber static void prepare_superblock(struct era_metadata *md, struct superblock_disk *disk)
490eec40579SJoe Thornber {
491eec40579SJoe Thornber 	disk->magic = cpu_to_le64(SUPERBLOCK_MAGIC);
492eec40579SJoe Thornber 	disk->flags = cpu_to_le32(0ul);
493eec40579SJoe Thornber 
494eec40579SJoe Thornber 	/* FIXME: can't keep blanking the uuid (uuid is currently unused though) */
495eec40579SJoe Thornber 	memset(disk->uuid, 0, sizeof(disk->uuid));
496eec40579SJoe Thornber 	disk->version = cpu_to_le32(MAX_ERA_VERSION);
497eec40579SJoe Thornber 
4985a32083dSJoe Thornber 	copy_sm_root(md, disk);
499eec40579SJoe Thornber 
500eec40579SJoe Thornber 	disk->data_block_size = cpu_to_le32(md->block_size);
501eec40579SJoe Thornber 	disk->metadata_block_size = cpu_to_le32(DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
502eec40579SJoe Thornber 	disk->nr_blocks = cpu_to_le32(md->nr_blocks);
503eec40579SJoe Thornber 	disk->current_era = cpu_to_le32(md->current_era);
504eec40579SJoe Thornber 
505eec40579SJoe Thornber 	ws_pack(&md->current_writeset->md, &disk->current_writeset);
506eec40579SJoe Thornber 	disk->writeset_tree_root = cpu_to_le64(md->writeset_tree_root);
507eec40579SJoe Thornber 	disk->era_array_root = cpu_to_le64(md->era_array_root);
508eec40579SJoe Thornber 	disk->metadata_snap = cpu_to_le64(md->metadata_snap);
509eec40579SJoe Thornber }
510eec40579SJoe Thornber 
511eec40579SJoe Thornber static int write_superblock(struct era_metadata *md)
512eec40579SJoe Thornber {
513eec40579SJoe Thornber 	int r;
514eec40579SJoe Thornber 	struct dm_block *sblock;
515eec40579SJoe Thornber 	struct superblock_disk *disk;
516eec40579SJoe Thornber 
5175a32083dSJoe Thornber 	r = save_sm_root(md);
5185a32083dSJoe Thornber 	if (r) {
5195a32083dSJoe Thornber 		DMERR("%s: save_sm_root failed", __func__);
5205a32083dSJoe Thornber 		return r;
5215a32083dSJoe Thornber 	}
5225a32083dSJoe Thornber 
523eec40579SJoe Thornber 	r = superblock_lock_zero(md, &sblock);
524eec40579SJoe Thornber 	if (r)
525eec40579SJoe Thornber 		return r;
526eec40579SJoe Thornber 
527eec40579SJoe Thornber 	disk = dm_block_data(sblock);
5285a32083dSJoe Thornber 	prepare_superblock(md, disk);
529eec40579SJoe Thornber 
530eec40579SJoe Thornber 	return dm_tm_commit(md->tm, sblock);
531eec40579SJoe Thornber }
532eec40579SJoe Thornber 
533eec40579SJoe Thornber /*
534eec40579SJoe Thornber  * Assumes block_size and the infos are set.
535eec40579SJoe Thornber  */
536eec40579SJoe Thornber static int format_metadata(struct era_metadata *md)
537eec40579SJoe Thornber {
538eec40579SJoe Thornber 	int r;
539eec40579SJoe Thornber 
540eec40579SJoe Thornber 	r = create_fresh_metadata(md);
541eec40579SJoe Thornber 	if (r)
542eec40579SJoe Thornber 		return r;
543eec40579SJoe Thornber 
544eec40579SJoe Thornber 	r = write_superblock(md);
545eec40579SJoe Thornber 	if (r) {
546eec40579SJoe Thornber 		dm_sm_destroy(md->sm);
547eec40579SJoe Thornber 		dm_tm_destroy(md->tm);
548eec40579SJoe Thornber 		return r;
549eec40579SJoe Thornber 	}
550eec40579SJoe Thornber 
551eec40579SJoe Thornber 	return 0;
552eec40579SJoe Thornber }
553eec40579SJoe Thornber 
554eec40579SJoe Thornber static int open_metadata(struct era_metadata *md)
555eec40579SJoe Thornber {
556eec40579SJoe Thornber 	int r;
557eec40579SJoe Thornber 	struct dm_block *sblock;
558eec40579SJoe Thornber 	struct superblock_disk *disk;
559eec40579SJoe Thornber 
560eec40579SJoe Thornber 	r = superblock_read_lock(md, &sblock);
561eec40579SJoe Thornber 	if (r) {
562eec40579SJoe Thornber 		DMERR("couldn't read_lock superblock");
563eec40579SJoe Thornber 		return r;
564eec40579SJoe Thornber 	}
565eec40579SJoe Thornber 
566eec40579SJoe Thornber 	disk = dm_block_data(sblock);
567eec40579SJoe Thornber 	r = dm_tm_open_with_sm(md->bm, SUPERBLOCK_LOCATION,
568eec40579SJoe Thornber 			       disk->metadata_space_map_root,
569eec40579SJoe Thornber 			       sizeof(disk->metadata_space_map_root),
570eec40579SJoe Thornber 			       &md->tm, &md->sm);
571eec40579SJoe Thornber 	if (r) {
572eec40579SJoe Thornber 		DMERR("dm_tm_open_with_sm failed");
573eec40579SJoe Thornber 		goto bad;
574eec40579SJoe Thornber 	}
575eec40579SJoe Thornber 
576eec40579SJoe Thornber 	setup_infos(md);
577eec40579SJoe Thornber 
578eec40579SJoe Thornber 	md->block_size = le32_to_cpu(disk->data_block_size);
579eec40579SJoe Thornber 	md->nr_blocks = le32_to_cpu(disk->nr_blocks);
580eec40579SJoe Thornber 	md->current_era = le32_to_cpu(disk->current_era);
581eec40579SJoe Thornber 
582de89afc1SNikos Tsironis 	ws_unpack(&disk->current_writeset, &md->current_writeset->md);
583eec40579SJoe Thornber 	md->writeset_tree_root = le64_to_cpu(disk->writeset_tree_root);
584eec40579SJoe Thornber 	md->era_array_root = le64_to_cpu(disk->era_array_root);
585eec40579SJoe Thornber 	md->metadata_snap = le64_to_cpu(disk->metadata_snap);
586eec40579SJoe Thornber 	md->archived_writesets = true;
587eec40579SJoe Thornber 
5884c7da06fSMikulas Patocka 	dm_bm_unlock(sblock);
5894c7da06fSMikulas Patocka 
5904c7da06fSMikulas Patocka 	return 0;
591eec40579SJoe Thornber 
592eec40579SJoe Thornber bad:
593eec40579SJoe Thornber 	dm_bm_unlock(sblock);
594eec40579SJoe Thornber 	return r;
595eec40579SJoe Thornber }
596eec40579SJoe Thornber 
597eec40579SJoe Thornber static int open_or_format_metadata(struct era_metadata *md,
598eec40579SJoe Thornber 				   bool may_format)
599eec40579SJoe Thornber {
600eec40579SJoe Thornber 	int r;
601eec40579SJoe Thornber 	bool unformatted = false;
602eec40579SJoe Thornber 
603eec40579SJoe Thornber 	r = superblock_all_zeroes(md->bm, &unformatted);
604eec40579SJoe Thornber 	if (r)
605eec40579SJoe Thornber 		return r;
606eec40579SJoe Thornber 
607eec40579SJoe Thornber 	if (unformatted)
608eec40579SJoe Thornber 		return may_format ? format_metadata(md) : -EPERM;
609eec40579SJoe Thornber 
610eec40579SJoe Thornber 	return open_metadata(md);
611eec40579SJoe Thornber }
612eec40579SJoe Thornber 
613eec40579SJoe Thornber static int create_persistent_data_objects(struct era_metadata *md,
614eec40579SJoe Thornber 					  bool may_format)
615eec40579SJoe Thornber {
616eec40579SJoe Thornber 	int r;
617eec40579SJoe Thornber 
618eec40579SJoe Thornber 	md->bm = dm_block_manager_create(md->bdev, DM_ERA_METADATA_BLOCK_SIZE,
619eec40579SJoe Thornber 					 ERA_MAX_CONCURRENT_LOCKS);
620eec40579SJoe Thornber 	if (IS_ERR(md->bm)) {
621eec40579SJoe Thornber 		DMERR("could not create block manager");
622eec40579SJoe Thornber 		return PTR_ERR(md->bm);
623eec40579SJoe Thornber 	}
624eec40579SJoe Thornber 
625eec40579SJoe Thornber 	r = open_or_format_metadata(md, may_format);
626eec40579SJoe Thornber 	if (r)
627eec40579SJoe Thornber 		dm_block_manager_destroy(md->bm);
628eec40579SJoe Thornber 
629eec40579SJoe Thornber 	return r;
630eec40579SJoe Thornber }
631eec40579SJoe Thornber 
632eec40579SJoe Thornber static void destroy_persistent_data_objects(struct era_metadata *md)
633eec40579SJoe Thornber {
634eec40579SJoe Thornber 	dm_sm_destroy(md->sm);
635eec40579SJoe Thornber 	dm_tm_destroy(md->tm);
636eec40579SJoe Thornber 	dm_block_manager_destroy(md->bm);
637eec40579SJoe Thornber }
638eec40579SJoe Thornber 
639eec40579SJoe Thornber /*
640eec40579SJoe Thornber  * This waits until all era_map threads have picked up the new filter.
641eec40579SJoe Thornber  */
642eec40579SJoe Thornber static void swap_writeset(struct era_metadata *md, struct writeset *new_writeset)
643eec40579SJoe Thornber {
644eec40579SJoe Thornber 	rcu_assign_pointer(md->current_writeset, new_writeset);
645eec40579SJoe Thornber 	synchronize_rcu();
646eec40579SJoe Thornber }
647eec40579SJoe Thornber 
648eec40579SJoe Thornber /*----------------------------------------------------------------
649eec40579SJoe Thornber  * Writesets get 'digested' into the main era array.
650eec40579SJoe Thornber  *
651eec40579SJoe Thornber  * We're using a coroutine here so the worker thread can do the digestion,
652eec40579SJoe Thornber  * thus avoiding synchronisation of the metadata.  Digesting a whole
653eec40579SJoe Thornber  * writeset in one go would cause too much latency.
654eec40579SJoe Thornber  *--------------------------------------------------------------*/
655eec40579SJoe Thornber struct digest {
656eec40579SJoe Thornber 	uint32_t era;
657eec40579SJoe Thornber 	unsigned nr_bits, current_bit;
658eec40579SJoe Thornber 	struct writeset_metadata writeset;
659eec40579SJoe Thornber 	__le32 value;
660eec40579SJoe Thornber 	struct dm_disk_bitset info;
661eec40579SJoe Thornber 
662eec40579SJoe Thornber 	int (*step)(struct era_metadata *, struct digest *);
663eec40579SJoe Thornber };
664eec40579SJoe Thornber 
665eec40579SJoe Thornber static int metadata_digest_lookup_writeset(struct era_metadata *md,
666eec40579SJoe Thornber 					   struct digest *d);
667eec40579SJoe Thornber 
668eec40579SJoe Thornber static int metadata_digest_remove_writeset(struct era_metadata *md,
669eec40579SJoe Thornber 					   struct digest *d)
670eec40579SJoe Thornber {
671eec40579SJoe Thornber 	int r;
672eec40579SJoe Thornber 	uint64_t key = d->era;
673eec40579SJoe Thornber 
674eec40579SJoe Thornber 	r = dm_btree_remove(&md->writeset_tree_info, md->writeset_tree_root,
675eec40579SJoe Thornber 			    &key, &md->writeset_tree_root);
676eec40579SJoe Thornber 	if (r) {
677eec40579SJoe Thornber 		DMERR("%s: dm_btree_remove failed", __func__);
678eec40579SJoe Thornber 		return r;
679eec40579SJoe Thornber 	}
680eec40579SJoe Thornber 
681eec40579SJoe Thornber 	d->step = metadata_digest_lookup_writeset;
682eec40579SJoe Thornber 	return 0;
683eec40579SJoe Thornber }
684eec40579SJoe Thornber 
685eec40579SJoe Thornber #define INSERTS_PER_STEP 100
686eec40579SJoe Thornber 
687eec40579SJoe Thornber static int metadata_digest_transcribe_writeset(struct era_metadata *md,
688eec40579SJoe Thornber 					       struct digest *d)
689eec40579SJoe Thornber {
690eec40579SJoe Thornber 	int r;
691eec40579SJoe Thornber 	bool marked;
692eec40579SJoe Thornber 	unsigned b, e = min(d->current_bit + INSERTS_PER_STEP, d->nr_bits);
693eec40579SJoe Thornber 
694eec40579SJoe Thornber 	for (b = d->current_bit; b < e; b++) {
695eec40579SJoe Thornber 		r = writeset_marked_on_disk(&d->info, &d->writeset, b, &marked);
696eec40579SJoe Thornber 		if (r) {
697eec40579SJoe Thornber 			DMERR("%s: writeset_marked_on_disk failed", __func__);
698eec40579SJoe Thornber 			return r;
699eec40579SJoe Thornber 		}
700eec40579SJoe Thornber 
701eec40579SJoe Thornber 		if (!marked)
702eec40579SJoe Thornber 			continue;
703eec40579SJoe Thornber 
704eec40579SJoe Thornber 		__dm_bless_for_disk(&d->value);
705eec40579SJoe Thornber 		r = dm_array_set_value(&md->era_array_info, md->era_array_root,
706eec40579SJoe Thornber 				       b, &d->value, &md->era_array_root);
707eec40579SJoe Thornber 		if (r) {
708eec40579SJoe Thornber 			DMERR("%s: dm_array_set_value failed", __func__);
709eec40579SJoe Thornber 			return r;
710eec40579SJoe Thornber 		}
711eec40579SJoe Thornber 	}
712eec40579SJoe Thornber 
713eec40579SJoe Thornber 	if (b == d->nr_bits)
714eec40579SJoe Thornber 		d->step = metadata_digest_remove_writeset;
715eec40579SJoe Thornber 	else
716eec40579SJoe Thornber 		d->current_bit = b;
717eec40579SJoe Thornber 
718eec40579SJoe Thornber 	return 0;
719eec40579SJoe Thornber }
720eec40579SJoe Thornber 
721eec40579SJoe Thornber static int metadata_digest_lookup_writeset(struct era_metadata *md,
722eec40579SJoe Thornber 					   struct digest *d)
723eec40579SJoe Thornber {
724eec40579SJoe Thornber 	int r;
725eec40579SJoe Thornber 	uint64_t key;
726eec40579SJoe Thornber 	struct writeset_disk disk;
727eec40579SJoe Thornber 
728eec40579SJoe Thornber 	r = dm_btree_find_lowest_key(&md->writeset_tree_info,
729eec40579SJoe Thornber 				     md->writeset_tree_root, &key);
730eec40579SJoe Thornber 	if (r < 0)
731eec40579SJoe Thornber 		return r;
732eec40579SJoe Thornber 
733eec40579SJoe Thornber 	d->era = key;
734eec40579SJoe Thornber 
735eec40579SJoe Thornber 	r = dm_btree_lookup(&md->writeset_tree_info,
736eec40579SJoe Thornber 			    md->writeset_tree_root, &key, &disk);
737eec40579SJoe Thornber 	if (r) {
738eec40579SJoe Thornber 		if (r == -ENODATA) {
739eec40579SJoe Thornber 			d->step = NULL;
740eec40579SJoe Thornber 			return 0;
741eec40579SJoe Thornber 		}
742eec40579SJoe Thornber 
743eec40579SJoe Thornber 		DMERR("%s: dm_btree_lookup failed", __func__);
744eec40579SJoe Thornber 		return r;
745eec40579SJoe Thornber 	}
746eec40579SJoe Thornber 
747eec40579SJoe Thornber 	ws_unpack(&disk, &d->writeset);
748eec40579SJoe Thornber 	d->value = cpu_to_le32(key);
749eec40579SJoe Thornber 
750*25249333SNikos Tsironis 	/*
751*25249333SNikos Tsironis 	 * We initialise another bitset info to avoid any caching side effects
752*25249333SNikos Tsironis 	 * with the previous one.
753*25249333SNikos Tsironis 	 */
754*25249333SNikos Tsironis 	dm_disk_bitset_init(md->tm, &d->info);
755*25249333SNikos Tsironis 
756eec40579SJoe Thornber 	d->nr_bits = min(d->writeset.nr_bits, md->nr_blocks);
757eec40579SJoe Thornber 	d->current_bit = 0;
758eec40579SJoe Thornber 	d->step = metadata_digest_transcribe_writeset;
759eec40579SJoe Thornber 
760eec40579SJoe Thornber 	return 0;
761eec40579SJoe Thornber }
762eec40579SJoe Thornber 
763eec40579SJoe Thornber static int metadata_digest_start(struct era_metadata *md, struct digest *d)
764eec40579SJoe Thornber {
765eec40579SJoe Thornber 	if (d->step)
766eec40579SJoe Thornber 		return 0;
767eec40579SJoe Thornber 
768eec40579SJoe Thornber 	memset(d, 0, sizeof(*d));
769eec40579SJoe Thornber 	d->step = metadata_digest_lookup_writeset;
770eec40579SJoe Thornber 
771eec40579SJoe Thornber 	return 0;
772eec40579SJoe Thornber }
773eec40579SJoe Thornber 
774eec40579SJoe Thornber /*----------------------------------------------------------------
775eec40579SJoe Thornber  * High level metadata interface.  Target methods should use these, and not
776eec40579SJoe Thornber  * the lower level ones.
777eec40579SJoe Thornber  *--------------------------------------------------------------*/
778eec40579SJoe Thornber static struct era_metadata *metadata_open(struct block_device *bdev,
779eec40579SJoe Thornber 					  sector_t block_size,
780eec40579SJoe Thornber 					  bool may_format)
781eec40579SJoe Thornber {
782eec40579SJoe Thornber 	int r;
783eec40579SJoe Thornber 	struct era_metadata *md = kzalloc(sizeof(*md), GFP_KERNEL);
784eec40579SJoe Thornber 
785eec40579SJoe Thornber 	if (!md)
786eec40579SJoe Thornber 		return NULL;
787eec40579SJoe Thornber 
788eec40579SJoe Thornber 	md->bdev = bdev;
789eec40579SJoe Thornber 	md->block_size = block_size;
790eec40579SJoe Thornber 
791eec40579SJoe Thornber 	md->writesets[0].md.root = INVALID_WRITESET_ROOT;
792eec40579SJoe Thornber 	md->writesets[1].md.root = INVALID_WRITESET_ROOT;
793eec40579SJoe Thornber 	md->current_writeset = &md->writesets[0];
794eec40579SJoe Thornber 
795eec40579SJoe Thornber 	r = create_persistent_data_objects(md, may_format);
796eec40579SJoe Thornber 	if (r) {
797eec40579SJoe Thornber 		kfree(md);
798eec40579SJoe Thornber 		return ERR_PTR(r);
799eec40579SJoe Thornber 	}
800eec40579SJoe Thornber 
801eec40579SJoe Thornber 	return md;
802eec40579SJoe Thornber }
803eec40579SJoe Thornber 
804eec40579SJoe Thornber static void metadata_close(struct era_metadata *md)
805eec40579SJoe Thornber {
806eec40579SJoe Thornber 	destroy_persistent_data_objects(md);
807eec40579SJoe Thornber 	kfree(md);
808eec40579SJoe Thornber }
809eec40579SJoe Thornber 
810eec40579SJoe Thornber static bool valid_nr_blocks(dm_block_t n)
811eec40579SJoe Thornber {
812eec40579SJoe Thornber 	/*
813eec40579SJoe Thornber 	 * dm_bitset restricts us to 2^32.  test_bit & co. restrict us
814eec40579SJoe Thornber 	 * further to 2^31 - 1
815eec40579SJoe Thornber 	 */
816eec40579SJoe Thornber 	return n < (1ull << 31);
817eec40579SJoe Thornber }
818eec40579SJoe Thornber 
819eec40579SJoe Thornber static int metadata_resize(struct era_metadata *md, void *arg)
820eec40579SJoe Thornber {
821eec40579SJoe Thornber 	int r;
822eec40579SJoe Thornber 	dm_block_t *new_size = arg;
823eec40579SJoe Thornber 	__le32 value;
824eec40579SJoe Thornber 
825eec40579SJoe Thornber 	if (!valid_nr_blocks(*new_size)) {
826eec40579SJoe Thornber 		DMERR("Invalid number of origin blocks %llu",
827eec40579SJoe Thornber 		      (unsigned long long) *new_size);
828eec40579SJoe Thornber 		return -EINVAL;
829eec40579SJoe Thornber 	}
830eec40579SJoe Thornber 
831eec40579SJoe Thornber 	writeset_free(&md->writesets[0]);
832eec40579SJoe Thornber 	writeset_free(&md->writesets[1]);
833eec40579SJoe Thornber 
834eec40579SJoe Thornber 	r = writeset_alloc(&md->writesets[0], *new_size);
835eec40579SJoe Thornber 	if (r) {
836eec40579SJoe Thornber 		DMERR("%s: writeset_alloc failed for writeset 0", __func__);
837eec40579SJoe Thornber 		return r;
838eec40579SJoe Thornber 	}
839eec40579SJoe Thornber 
840eec40579SJoe Thornber 	r = writeset_alloc(&md->writesets[1], *new_size);
841eec40579SJoe Thornber 	if (r) {
842eec40579SJoe Thornber 		DMERR("%s: writeset_alloc failed for writeset 1", __func__);
843eec40579SJoe Thornber 		return r;
844eec40579SJoe Thornber 	}
845eec40579SJoe Thornber 
846eec40579SJoe Thornber 	value = cpu_to_le32(0u);
847eec40579SJoe Thornber 	__dm_bless_for_disk(&value);
848eec40579SJoe Thornber 	r = dm_array_resize(&md->era_array_info, md->era_array_root,
849eec40579SJoe Thornber 			    md->nr_blocks, *new_size,
850eec40579SJoe Thornber 			    &value, &md->era_array_root);
851eec40579SJoe Thornber 	if (r) {
852eec40579SJoe Thornber 		DMERR("%s: dm_array_resize failed", __func__);
853eec40579SJoe Thornber 		return r;
854eec40579SJoe Thornber 	}
855eec40579SJoe Thornber 
856eec40579SJoe Thornber 	md->nr_blocks = *new_size;
857eec40579SJoe Thornber 	return 0;
858eec40579SJoe Thornber }
859eec40579SJoe Thornber 
860eec40579SJoe Thornber static int metadata_era_archive(struct era_metadata *md)
861eec40579SJoe Thornber {
862eec40579SJoe Thornber 	int r;
863eec40579SJoe Thornber 	uint64_t keys[1];
864eec40579SJoe Thornber 	struct writeset_disk value;
865eec40579SJoe Thornber 
866eec40579SJoe Thornber 	r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
867eec40579SJoe Thornber 			    &md->current_writeset->md.root);
868eec40579SJoe Thornber 	if (r) {
869eec40579SJoe Thornber 		DMERR("%s: dm_bitset_flush failed", __func__);
870eec40579SJoe Thornber 		return r;
871eec40579SJoe Thornber 	}
872eec40579SJoe Thornber 
873eec40579SJoe Thornber 	ws_pack(&md->current_writeset->md, &value);
874eec40579SJoe Thornber 
875eec40579SJoe Thornber 	keys[0] = md->current_era;
876eec40579SJoe Thornber 	__dm_bless_for_disk(&value);
877eec40579SJoe Thornber 	r = dm_btree_insert(&md->writeset_tree_info, md->writeset_tree_root,
878eec40579SJoe Thornber 			    keys, &value, &md->writeset_tree_root);
879eec40579SJoe Thornber 	if (r) {
880eec40579SJoe Thornber 		DMERR("%s: couldn't insert writeset into btree", __func__);
881eec40579SJoe Thornber 		/* FIXME: fail mode */
882eec40579SJoe Thornber 		return r;
883eec40579SJoe Thornber 	}
884eec40579SJoe Thornber 
885de89afc1SNikos Tsironis 	md->current_writeset->md.root = INVALID_WRITESET_ROOT;
886eec40579SJoe Thornber 	md->archived_writesets = true;
887eec40579SJoe Thornber 
888eec40579SJoe Thornber 	return 0;
889eec40579SJoe Thornber }
890eec40579SJoe Thornber 
891eec40579SJoe Thornber static struct writeset *next_writeset(struct era_metadata *md)
892eec40579SJoe Thornber {
893eec40579SJoe Thornber 	return (md->current_writeset == &md->writesets[0]) ?
894eec40579SJoe Thornber 		&md->writesets[1] : &md->writesets[0];
895eec40579SJoe Thornber }
896eec40579SJoe Thornber 
897eec40579SJoe Thornber static int metadata_new_era(struct era_metadata *md)
898eec40579SJoe Thornber {
899eec40579SJoe Thornber 	int r;
900eec40579SJoe Thornber 	struct writeset *new_writeset = next_writeset(md);
901eec40579SJoe Thornber 
902de89afc1SNikos Tsironis 	r = writeset_init(&md->bitset_info, new_writeset, md->nr_blocks);
903eec40579SJoe Thornber 	if (r) {
904eec40579SJoe Thornber 		DMERR("%s: writeset_init failed", __func__);
905eec40579SJoe Thornber 		return r;
906eec40579SJoe Thornber 	}
907eec40579SJoe Thornber 
908eec40579SJoe Thornber 	swap_writeset(md, new_writeset);
909eec40579SJoe Thornber 	md->current_era++;
910eec40579SJoe Thornber 
911eec40579SJoe Thornber 	return 0;
912eec40579SJoe Thornber }
913eec40579SJoe Thornber 
914eec40579SJoe Thornber static int metadata_era_rollover(struct era_metadata *md)
915eec40579SJoe Thornber {
916eec40579SJoe Thornber 	int r;
917eec40579SJoe Thornber 
918eec40579SJoe Thornber 	if (md->current_writeset->md.root != INVALID_WRITESET_ROOT) {
919eec40579SJoe Thornber 		r = metadata_era_archive(md);
920eec40579SJoe Thornber 		if (r) {
921eec40579SJoe Thornber 			DMERR("%s: metadata_archive_era failed", __func__);
922eec40579SJoe Thornber 			/* FIXME: fail mode? */
923eec40579SJoe Thornber 			return r;
924eec40579SJoe Thornber 		}
925eec40579SJoe Thornber 	}
926eec40579SJoe Thornber 
927eec40579SJoe Thornber 	r = metadata_new_era(md);
928eec40579SJoe Thornber 	if (r) {
929eec40579SJoe Thornber 		DMERR("%s: new era failed", __func__);
930eec40579SJoe Thornber 		/* FIXME: fail mode */
931eec40579SJoe Thornber 		return r;
932eec40579SJoe Thornber 	}
933eec40579SJoe Thornber 
934eec40579SJoe Thornber 	return 0;
935eec40579SJoe Thornber }
936eec40579SJoe Thornber 
937eec40579SJoe Thornber static bool metadata_current_marked(struct era_metadata *md, dm_block_t block)
938eec40579SJoe Thornber {
939eec40579SJoe Thornber 	bool r;
940eec40579SJoe Thornber 	struct writeset *ws;
941eec40579SJoe Thornber 
942eec40579SJoe Thornber 	rcu_read_lock();
943eec40579SJoe Thornber 	ws = rcu_dereference(md->current_writeset);
944eec40579SJoe Thornber 	r = writeset_marked(ws, block);
945eec40579SJoe Thornber 	rcu_read_unlock();
946eec40579SJoe Thornber 
947eec40579SJoe Thornber 	return r;
948eec40579SJoe Thornber }
949eec40579SJoe Thornber 
950eec40579SJoe Thornber static int metadata_commit(struct era_metadata *md)
951eec40579SJoe Thornber {
952eec40579SJoe Thornber 	int r;
953eec40579SJoe Thornber 	struct dm_block *sblock;
954eec40579SJoe Thornber 
955de89afc1SNikos Tsironis 	if (md->current_writeset->md.root != INVALID_WRITESET_ROOT) {
956eec40579SJoe Thornber 		r = dm_bitset_flush(&md->bitset_info, md->current_writeset->md.root,
957eec40579SJoe Thornber 				    &md->current_writeset->md.root);
958eec40579SJoe Thornber 		if (r) {
959eec40579SJoe Thornber 			DMERR("%s: bitset flush failed", __func__);
960eec40579SJoe Thornber 			return r;
961eec40579SJoe Thornber 		}
962eec40579SJoe Thornber 	}
963eec40579SJoe Thornber 
964eec40579SJoe Thornber 	r = dm_tm_pre_commit(md->tm);
965eec40579SJoe Thornber 	if (r) {
966eec40579SJoe Thornber 		DMERR("%s: pre commit failed", __func__);
967eec40579SJoe Thornber 		return r;
968eec40579SJoe Thornber 	}
969eec40579SJoe Thornber 
970117aceb0SSomasundaram Krishnasamy 	r = save_sm_root(md);
971117aceb0SSomasundaram Krishnasamy 	if (r) {
972117aceb0SSomasundaram Krishnasamy 		DMERR("%s: save_sm_root failed", __func__);
973117aceb0SSomasundaram Krishnasamy 		return r;
974117aceb0SSomasundaram Krishnasamy 	}
975117aceb0SSomasundaram Krishnasamy 
976eec40579SJoe Thornber 	r = superblock_lock(md, &sblock);
977eec40579SJoe Thornber 	if (r) {
978eec40579SJoe Thornber 		DMERR("%s: superblock lock failed", __func__);
979eec40579SJoe Thornber 		return r;
980eec40579SJoe Thornber 	}
981eec40579SJoe Thornber 
9825a32083dSJoe Thornber 	prepare_superblock(md, dm_block_data(sblock));
983eec40579SJoe Thornber 
984eec40579SJoe Thornber 	return dm_tm_commit(md->tm, sblock);
985eec40579SJoe Thornber }
986eec40579SJoe Thornber 
987eec40579SJoe Thornber static int metadata_checkpoint(struct era_metadata *md)
988eec40579SJoe Thornber {
989eec40579SJoe Thornber 	/*
990eec40579SJoe Thornber 	 * For now we just rollover, but later I want to put a check in to
991eec40579SJoe Thornber 	 * avoid this if the filter is still pretty fresh.
992eec40579SJoe Thornber 	 */
993eec40579SJoe Thornber 	return metadata_era_rollover(md);
994eec40579SJoe Thornber }
995eec40579SJoe Thornber 
996eec40579SJoe Thornber /*
997eec40579SJoe Thornber  * Metadata snapshots allow userland to access era data.
998eec40579SJoe Thornber  */
999eec40579SJoe Thornber static int metadata_take_snap(struct era_metadata *md)
1000eec40579SJoe Thornber {
1001eec40579SJoe Thornber 	int r, inc;
1002eec40579SJoe Thornber 	struct dm_block *clone;
1003eec40579SJoe Thornber 
1004eec40579SJoe Thornber 	if (md->metadata_snap != SUPERBLOCK_LOCATION) {
1005eec40579SJoe Thornber 		DMERR("%s: metadata snapshot already exists", __func__);
1006eec40579SJoe Thornber 		return -EINVAL;
1007eec40579SJoe Thornber 	}
1008eec40579SJoe Thornber 
1009eec40579SJoe Thornber 	r = metadata_era_rollover(md);
1010eec40579SJoe Thornber 	if (r) {
1011eec40579SJoe Thornber 		DMERR("%s: era rollover failed", __func__);
1012eec40579SJoe Thornber 		return r;
1013eec40579SJoe Thornber 	}
1014eec40579SJoe Thornber 
1015eec40579SJoe Thornber 	r = metadata_commit(md);
1016eec40579SJoe Thornber 	if (r) {
1017eec40579SJoe Thornber 		DMERR("%s: pre commit failed", __func__);
1018eec40579SJoe Thornber 		return r;
1019eec40579SJoe Thornber 	}
1020eec40579SJoe Thornber 
1021eec40579SJoe Thornber 	r = dm_sm_inc_block(md->sm, SUPERBLOCK_LOCATION);
1022eec40579SJoe Thornber 	if (r) {
1023eec40579SJoe Thornber 		DMERR("%s: couldn't increment superblock", __func__);
1024eec40579SJoe Thornber 		return r;
1025eec40579SJoe Thornber 	}
1026eec40579SJoe Thornber 
1027eec40579SJoe Thornber 	r = dm_tm_shadow_block(md->tm, SUPERBLOCK_LOCATION,
1028eec40579SJoe Thornber 			       &sb_validator, &clone, &inc);
1029eec40579SJoe Thornber 	if (r) {
1030eec40579SJoe Thornber 		DMERR("%s: couldn't shadow superblock", __func__);
1031eec40579SJoe Thornber 		dm_sm_dec_block(md->sm, SUPERBLOCK_LOCATION);
1032eec40579SJoe Thornber 		return r;
1033eec40579SJoe Thornber 	}
1034eec40579SJoe Thornber 	BUG_ON(!inc);
1035eec40579SJoe Thornber 
1036eec40579SJoe Thornber 	r = dm_sm_inc_block(md->sm, md->writeset_tree_root);
1037eec40579SJoe Thornber 	if (r) {
1038eec40579SJoe Thornber 		DMERR("%s: couldn't inc writeset tree root", __func__);
1039eec40579SJoe Thornber 		dm_tm_unlock(md->tm, clone);
1040eec40579SJoe Thornber 		return r;
1041eec40579SJoe Thornber 	}
1042eec40579SJoe Thornber 
1043eec40579SJoe Thornber 	r = dm_sm_inc_block(md->sm, md->era_array_root);
1044eec40579SJoe Thornber 	if (r) {
1045eec40579SJoe Thornber 		DMERR("%s: couldn't inc era tree root", __func__);
1046eec40579SJoe Thornber 		dm_sm_dec_block(md->sm, md->writeset_tree_root);
1047eec40579SJoe Thornber 		dm_tm_unlock(md->tm, clone);
1048eec40579SJoe Thornber 		return r;
1049eec40579SJoe Thornber 	}
1050eec40579SJoe Thornber 
1051eec40579SJoe Thornber 	md->metadata_snap = dm_block_location(clone);
1052eec40579SJoe Thornber 
10534c7da06fSMikulas Patocka 	dm_tm_unlock(md->tm, clone);
1054eec40579SJoe Thornber 
1055eec40579SJoe Thornber 	return 0;
1056eec40579SJoe Thornber }
1057eec40579SJoe Thornber 
1058eec40579SJoe Thornber static int metadata_drop_snap(struct era_metadata *md)
1059eec40579SJoe Thornber {
1060eec40579SJoe Thornber 	int r;
1061eec40579SJoe Thornber 	dm_block_t location;
1062eec40579SJoe Thornber 	struct dm_block *clone;
1063eec40579SJoe Thornber 	struct superblock_disk *disk;
1064eec40579SJoe Thornber 
1065eec40579SJoe Thornber 	if (md->metadata_snap == SUPERBLOCK_LOCATION) {
1066eec40579SJoe Thornber 		DMERR("%s: no snap to drop", __func__);
1067eec40579SJoe Thornber 		return -EINVAL;
1068eec40579SJoe Thornber 	}
1069eec40579SJoe Thornber 
1070eec40579SJoe Thornber 	r = dm_tm_read_lock(md->tm, md->metadata_snap, &sb_validator, &clone);
1071eec40579SJoe Thornber 	if (r) {
1072eec40579SJoe Thornber 		DMERR("%s: couldn't read lock superblock clone", __func__);
1073eec40579SJoe Thornber 		return r;
1074eec40579SJoe Thornber 	}
1075eec40579SJoe Thornber 
1076eec40579SJoe Thornber 	/*
1077eec40579SJoe Thornber 	 * Whatever happens now we'll commit with no record of the metadata
1078eec40579SJoe Thornber 	 * snap.
1079eec40579SJoe Thornber 	 */
1080eec40579SJoe Thornber 	md->metadata_snap = SUPERBLOCK_LOCATION;
1081eec40579SJoe Thornber 
1082eec40579SJoe Thornber 	disk = dm_block_data(clone);
1083eec40579SJoe Thornber 	r = dm_btree_del(&md->writeset_tree_info,
1084eec40579SJoe Thornber 			 le64_to_cpu(disk->writeset_tree_root));
1085eec40579SJoe Thornber 	if (r) {
1086eec40579SJoe Thornber 		DMERR("%s: error deleting writeset tree clone", __func__);
1087eec40579SJoe Thornber 		dm_tm_unlock(md->tm, clone);
1088eec40579SJoe Thornber 		return r;
1089eec40579SJoe Thornber 	}
1090eec40579SJoe Thornber 
1091eec40579SJoe Thornber 	r = dm_array_del(&md->era_array_info, le64_to_cpu(disk->era_array_root));
1092eec40579SJoe Thornber 	if (r) {
1093eec40579SJoe Thornber 		DMERR("%s: error deleting era array clone", __func__);
1094eec40579SJoe Thornber 		dm_tm_unlock(md->tm, clone);
1095eec40579SJoe Thornber 		return r;
1096eec40579SJoe Thornber 	}
1097eec40579SJoe Thornber 
1098eec40579SJoe Thornber 	location = dm_block_location(clone);
1099eec40579SJoe Thornber 	dm_tm_unlock(md->tm, clone);
1100eec40579SJoe Thornber 
1101eec40579SJoe Thornber 	return dm_sm_dec_block(md->sm, location);
1102eec40579SJoe Thornber }
1103eec40579SJoe Thornber 
1104eec40579SJoe Thornber struct metadata_stats {
1105eec40579SJoe Thornber 	dm_block_t used;
1106eec40579SJoe Thornber 	dm_block_t total;
1107eec40579SJoe Thornber 	dm_block_t snap;
1108eec40579SJoe Thornber 	uint32_t era;
1109eec40579SJoe Thornber };
1110eec40579SJoe Thornber 
1111eec40579SJoe Thornber static int metadata_get_stats(struct era_metadata *md, void *ptr)
1112eec40579SJoe Thornber {
1113eec40579SJoe Thornber 	int r;
1114eec40579SJoe Thornber 	struct metadata_stats *s = ptr;
1115eec40579SJoe Thornber 	dm_block_t nr_free, nr_total;
1116eec40579SJoe Thornber 
1117eec40579SJoe Thornber 	r = dm_sm_get_nr_free(md->sm, &nr_free);
1118eec40579SJoe Thornber 	if (r) {
1119eec40579SJoe Thornber 		DMERR("dm_sm_get_nr_free returned %d", r);
1120eec40579SJoe Thornber 		return r;
1121eec40579SJoe Thornber 	}
1122eec40579SJoe Thornber 
1123eec40579SJoe Thornber 	r = dm_sm_get_nr_blocks(md->sm, &nr_total);
1124eec40579SJoe Thornber 	if (r) {
1125eec40579SJoe Thornber 		DMERR("dm_pool_get_metadata_dev_size returned %d", r);
1126eec40579SJoe Thornber 		return r;
1127eec40579SJoe Thornber 	}
1128eec40579SJoe Thornber 
1129eec40579SJoe Thornber 	s->used = nr_total - nr_free;
1130eec40579SJoe Thornber 	s->total = nr_total;
1131eec40579SJoe Thornber 	s->snap = md->metadata_snap;
1132eec40579SJoe Thornber 	s->era = md->current_era;
1133eec40579SJoe Thornber 
1134eec40579SJoe Thornber 	return 0;
1135eec40579SJoe Thornber }
1136eec40579SJoe Thornber 
1137eec40579SJoe Thornber /*----------------------------------------------------------------*/
1138eec40579SJoe Thornber 
1139eec40579SJoe Thornber struct era {
1140eec40579SJoe Thornber 	struct dm_target *ti;
1141eec40579SJoe Thornber 
1142eec40579SJoe Thornber 	struct dm_dev *metadata_dev;
1143eec40579SJoe Thornber 	struct dm_dev *origin_dev;
1144eec40579SJoe Thornber 
1145eec40579SJoe Thornber 	dm_block_t nr_blocks;
1146eec40579SJoe Thornber 	uint32_t sectors_per_block;
1147eec40579SJoe Thornber 	int sectors_per_block_shift;
1148eec40579SJoe Thornber 	struct era_metadata *md;
1149eec40579SJoe Thornber 
1150eec40579SJoe Thornber 	struct workqueue_struct *wq;
1151eec40579SJoe Thornber 	struct work_struct worker;
1152eec40579SJoe Thornber 
1153eec40579SJoe Thornber 	spinlock_t deferred_lock;
1154eec40579SJoe Thornber 	struct bio_list deferred_bios;
1155eec40579SJoe Thornber 
1156eec40579SJoe Thornber 	spinlock_t rpc_lock;
1157eec40579SJoe Thornber 	struct list_head rpc_calls;
1158eec40579SJoe Thornber 
1159eec40579SJoe Thornber 	struct digest digest;
1160eec40579SJoe Thornber 	atomic_t suspended;
1161eec40579SJoe Thornber };
1162eec40579SJoe Thornber 
1163eec40579SJoe Thornber struct rpc {
1164eec40579SJoe Thornber 	struct list_head list;
1165eec40579SJoe Thornber 
1166eec40579SJoe Thornber 	int (*fn0)(struct era_metadata *);
1167eec40579SJoe Thornber 	int (*fn1)(struct era_metadata *, void *);
1168eec40579SJoe Thornber 	void *arg;
1169eec40579SJoe Thornber 	int result;
1170eec40579SJoe Thornber 
1171eec40579SJoe Thornber 	struct completion complete;
1172eec40579SJoe Thornber };
1173eec40579SJoe Thornber 
1174eec40579SJoe Thornber /*----------------------------------------------------------------
1175eec40579SJoe Thornber  * Remapping.
1176eec40579SJoe Thornber  *---------------------------------------------------------------*/
1177eec40579SJoe Thornber static bool block_size_is_power_of_two(struct era *era)
1178eec40579SJoe Thornber {
1179eec40579SJoe Thornber 	return era->sectors_per_block_shift >= 0;
1180eec40579SJoe Thornber }
1181eec40579SJoe Thornber 
1182eec40579SJoe Thornber static dm_block_t get_block(struct era *era, struct bio *bio)
1183eec40579SJoe Thornber {
1184eec40579SJoe Thornber 	sector_t block_nr = bio->bi_iter.bi_sector;
1185eec40579SJoe Thornber 
1186eec40579SJoe Thornber 	if (!block_size_is_power_of_two(era))
1187eec40579SJoe Thornber 		(void) sector_div(block_nr, era->sectors_per_block);
1188eec40579SJoe Thornber 	else
1189eec40579SJoe Thornber 		block_nr >>= era->sectors_per_block_shift;
1190eec40579SJoe Thornber 
1191eec40579SJoe Thornber 	return block_nr;
1192eec40579SJoe Thornber }
1193eec40579SJoe Thornber 
1194eec40579SJoe Thornber static void remap_to_origin(struct era *era, struct bio *bio)
1195eec40579SJoe Thornber {
119674d46992SChristoph Hellwig 	bio_set_dev(bio, era->origin_dev->bdev);
1197eec40579SJoe Thornber }
1198eec40579SJoe Thornber 
1199eec40579SJoe Thornber /*----------------------------------------------------------------
1200eec40579SJoe Thornber  * Worker thread
1201eec40579SJoe Thornber  *--------------------------------------------------------------*/
1202eec40579SJoe Thornber static void wake_worker(struct era *era)
1203eec40579SJoe Thornber {
1204eec40579SJoe Thornber 	if (!atomic_read(&era->suspended))
1205eec40579SJoe Thornber 		queue_work(era->wq, &era->worker);
1206eec40579SJoe Thornber }
1207eec40579SJoe Thornber 
1208eec40579SJoe Thornber static void process_old_eras(struct era *era)
1209eec40579SJoe Thornber {
1210eec40579SJoe Thornber 	int r;
1211eec40579SJoe Thornber 
1212eec40579SJoe Thornber 	if (!era->digest.step)
1213eec40579SJoe Thornber 		return;
1214eec40579SJoe Thornber 
1215eec40579SJoe Thornber 	r = era->digest.step(era->md, &era->digest);
1216eec40579SJoe Thornber 	if (r < 0) {
1217eec40579SJoe Thornber 		DMERR("%s: digest step failed, stopping digestion", __func__);
1218eec40579SJoe Thornber 		era->digest.step = NULL;
1219eec40579SJoe Thornber 
1220eec40579SJoe Thornber 	} else if (era->digest.step)
1221eec40579SJoe Thornber 		wake_worker(era);
1222eec40579SJoe Thornber }
1223eec40579SJoe Thornber 
1224eec40579SJoe Thornber static void process_deferred_bios(struct era *era)
1225eec40579SJoe Thornber {
1226eec40579SJoe Thornber 	int r;
1227eec40579SJoe Thornber 	struct bio_list deferred_bios, marked_bios;
1228eec40579SJoe Thornber 	struct bio *bio;
12292099b145SNikos Tsironis 	struct blk_plug plug;
1230eec40579SJoe Thornber 	bool commit_needed = false;
1231eec40579SJoe Thornber 	bool failed = false;
12322099b145SNikos Tsironis 	struct writeset *ws = era->md->current_writeset;
1233eec40579SJoe Thornber 
1234eec40579SJoe Thornber 	bio_list_init(&deferred_bios);
1235eec40579SJoe Thornber 	bio_list_init(&marked_bios);
1236eec40579SJoe Thornber 
1237eec40579SJoe Thornber 	spin_lock(&era->deferred_lock);
1238eec40579SJoe Thornber 	bio_list_merge(&deferred_bios, &era->deferred_bios);
1239eec40579SJoe Thornber 	bio_list_init(&era->deferred_bios);
1240eec40579SJoe Thornber 	spin_unlock(&era->deferred_lock);
1241eec40579SJoe Thornber 
12422099b145SNikos Tsironis 	if (bio_list_empty(&deferred_bios))
12432099b145SNikos Tsironis 		return;
12442099b145SNikos Tsironis 
1245eec40579SJoe Thornber 	while ((bio = bio_list_pop(&deferred_bios))) {
12462099b145SNikos Tsironis 		r = writeset_test_and_set(&era->md->bitset_info, ws,
1247eec40579SJoe Thornber 					  get_block(era, bio));
1248eec40579SJoe Thornber 		if (r < 0) {
1249eec40579SJoe Thornber 			/*
1250eec40579SJoe Thornber 			 * This is bad news, we need to rollback.
1251eec40579SJoe Thornber 			 * FIXME: finish.
1252eec40579SJoe Thornber 			 */
1253eec40579SJoe Thornber 			failed = true;
1254eec40579SJoe Thornber 		} else if (r == 0)
1255eec40579SJoe Thornber 			commit_needed = true;
1256eec40579SJoe Thornber 
1257eec40579SJoe Thornber 		bio_list_add(&marked_bios, bio);
1258eec40579SJoe Thornber 	}
1259eec40579SJoe Thornber 
1260eec40579SJoe Thornber 	if (commit_needed) {
1261eec40579SJoe Thornber 		r = metadata_commit(era->md);
1262eec40579SJoe Thornber 		if (r)
1263eec40579SJoe Thornber 			failed = true;
1264eec40579SJoe Thornber 	}
1265eec40579SJoe Thornber 
1266eec40579SJoe Thornber 	if (failed)
1267eec40579SJoe Thornber 		while ((bio = bio_list_pop(&marked_bios)))
1268eec40579SJoe Thornber 			bio_io_error(bio);
12692099b145SNikos Tsironis 	else {
12702099b145SNikos Tsironis 		blk_start_plug(&plug);
12712099b145SNikos Tsironis 		while ((bio = bio_list_pop(&marked_bios))) {
12722099b145SNikos Tsironis 			/*
12732099b145SNikos Tsironis 			 * Only update the in-core writeset if the on-disk one
12742099b145SNikos Tsironis 			 * was updated too.
12752099b145SNikos Tsironis 			 */
12762099b145SNikos Tsironis 			if (commit_needed)
12772099b145SNikos Tsironis 				set_bit(get_block(era, bio), ws->bits);
1278ed00aabdSChristoph Hellwig 			submit_bio_noacct(bio);
1279eec40579SJoe Thornber 		}
12802099b145SNikos Tsironis 		blk_finish_plug(&plug);
12812099b145SNikos Tsironis 	}
12822099b145SNikos Tsironis }
1283eec40579SJoe Thornber 
1284eec40579SJoe Thornber static void process_rpc_calls(struct era *era)
1285eec40579SJoe Thornber {
1286eec40579SJoe Thornber 	int r;
1287eec40579SJoe Thornber 	bool need_commit = false;
1288eec40579SJoe Thornber 	struct list_head calls;
1289eec40579SJoe Thornber 	struct rpc *rpc, *tmp;
1290eec40579SJoe Thornber 
1291eec40579SJoe Thornber 	INIT_LIST_HEAD(&calls);
1292eec40579SJoe Thornber 	spin_lock(&era->rpc_lock);
1293eec40579SJoe Thornber 	list_splice_init(&era->rpc_calls, &calls);
1294eec40579SJoe Thornber 	spin_unlock(&era->rpc_lock);
1295eec40579SJoe Thornber 
1296eec40579SJoe Thornber 	list_for_each_entry_safe(rpc, tmp, &calls, list) {
1297eec40579SJoe Thornber 		rpc->result = rpc->fn0 ? rpc->fn0(era->md) : rpc->fn1(era->md, rpc->arg);
1298eec40579SJoe Thornber 		need_commit = true;
1299eec40579SJoe Thornber 	}
1300eec40579SJoe Thornber 
1301eec40579SJoe Thornber 	if (need_commit) {
1302eec40579SJoe Thornber 		r = metadata_commit(era->md);
1303eec40579SJoe Thornber 		if (r)
1304eec40579SJoe Thornber 			list_for_each_entry_safe(rpc, tmp, &calls, list)
1305eec40579SJoe Thornber 				rpc->result = r;
1306eec40579SJoe Thornber 	}
1307eec40579SJoe Thornber 
1308eec40579SJoe Thornber 	list_for_each_entry_safe(rpc, tmp, &calls, list)
1309eec40579SJoe Thornber 		complete(&rpc->complete);
1310eec40579SJoe Thornber }
1311eec40579SJoe Thornber 
1312eec40579SJoe Thornber static void kick_off_digest(struct era *era)
1313eec40579SJoe Thornber {
1314eec40579SJoe Thornber 	if (era->md->archived_writesets) {
1315eec40579SJoe Thornber 		era->md->archived_writesets = false;
1316eec40579SJoe Thornber 		metadata_digest_start(era->md, &era->digest);
1317eec40579SJoe Thornber 	}
1318eec40579SJoe Thornber }
1319eec40579SJoe Thornber 
1320eec40579SJoe Thornber static void do_work(struct work_struct *ws)
1321eec40579SJoe Thornber {
1322eec40579SJoe Thornber 	struct era *era = container_of(ws, struct era, worker);
1323eec40579SJoe Thornber 
1324eec40579SJoe Thornber 	kick_off_digest(era);
1325eec40579SJoe Thornber 	process_old_eras(era);
1326eec40579SJoe Thornber 	process_deferred_bios(era);
1327eec40579SJoe Thornber 	process_rpc_calls(era);
1328eec40579SJoe Thornber }
1329eec40579SJoe Thornber 
1330eec40579SJoe Thornber static void defer_bio(struct era *era, struct bio *bio)
1331eec40579SJoe Thornber {
1332eec40579SJoe Thornber 	spin_lock(&era->deferred_lock);
1333eec40579SJoe Thornber 	bio_list_add(&era->deferred_bios, bio);
1334eec40579SJoe Thornber 	spin_unlock(&era->deferred_lock);
1335eec40579SJoe Thornber 
1336eec40579SJoe Thornber 	wake_worker(era);
1337eec40579SJoe Thornber }
1338eec40579SJoe Thornber 
1339eec40579SJoe Thornber /*
1340eec40579SJoe Thornber  * Make an rpc call to the worker to change the metadata.
1341eec40579SJoe Thornber  */
1342eec40579SJoe Thornber static int perform_rpc(struct era *era, struct rpc *rpc)
1343eec40579SJoe Thornber {
1344eec40579SJoe Thornber 	rpc->result = 0;
1345eec40579SJoe Thornber 	init_completion(&rpc->complete);
1346eec40579SJoe Thornber 
1347eec40579SJoe Thornber 	spin_lock(&era->rpc_lock);
1348eec40579SJoe Thornber 	list_add(&rpc->list, &era->rpc_calls);
1349eec40579SJoe Thornber 	spin_unlock(&era->rpc_lock);
1350eec40579SJoe Thornber 
1351eec40579SJoe Thornber 	wake_worker(era);
1352eec40579SJoe Thornber 	wait_for_completion(&rpc->complete);
1353eec40579SJoe Thornber 
1354eec40579SJoe Thornber 	return rpc->result;
1355eec40579SJoe Thornber }
1356eec40579SJoe Thornber 
1357eec40579SJoe Thornber static int in_worker0(struct era *era, int (*fn)(struct era_metadata *))
1358eec40579SJoe Thornber {
1359eec40579SJoe Thornber 	struct rpc rpc;
1360eec40579SJoe Thornber 	rpc.fn0 = fn;
1361eec40579SJoe Thornber 	rpc.fn1 = NULL;
1362eec40579SJoe Thornber 
1363eec40579SJoe Thornber 	return perform_rpc(era, &rpc);
1364eec40579SJoe Thornber }
1365eec40579SJoe Thornber 
1366eec40579SJoe Thornber static int in_worker1(struct era *era,
1367eec40579SJoe Thornber 		      int (*fn)(struct era_metadata *, void *), void *arg)
1368eec40579SJoe Thornber {
1369eec40579SJoe Thornber 	struct rpc rpc;
1370eec40579SJoe Thornber 	rpc.fn0 = NULL;
1371eec40579SJoe Thornber 	rpc.fn1 = fn;
1372eec40579SJoe Thornber 	rpc.arg = arg;
1373eec40579SJoe Thornber 
1374eec40579SJoe Thornber 	return perform_rpc(era, &rpc);
1375eec40579SJoe Thornber }
1376eec40579SJoe Thornber 
1377eec40579SJoe Thornber static void start_worker(struct era *era)
1378eec40579SJoe Thornber {
1379eec40579SJoe Thornber 	atomic_set(&era->suspended, 0);
1380eec40579SJoe Thornber }
1381eec40579SJoe Thornber 
1382eec40579SJoe Thornber static void stop_worker(struct era *era)
1383eec40579SJoe Thornber {
1384eec40579SJoe Thornber 	atomic_set(&era->suspended, 1);
1385eec40579SJoe Thornber 	flush_workqueue(era->wq);
1386eec40579SJoe Thornber }
1387eec40579SJoe Thornber 
1388eec40579SJoe Thornber /*----------------------------------------------------------------
1389eec40579SJoe Thornber  * Target methods
1390eec40579SJoe Thornber  *--------------------------------------------------------------*/
1391eec40579SJoe Thornber static void era_destroy(struct era *era)
1392eec40579SJoe Thornber {
1393989f26f5SJoe Thornber 	if (era->md)
1394eec40579SJoe Thornber 		metadata_close(era->md);
1395eec40579SJoe Thornber 
1396eec40579SJoe Thornber 	if (era->wq)
1397eec40579SJoe Thornber 		destroy_workqueue(era->wq);
1398eec40579SJoe Thornber 
1399eec40579SJoe Thornber 	if (era->origin_dev)
1400eec40579SJoe Thornber 		dm_put_device(era->ti, era->origin_dev);
1401eec40579SJoe Thornber 
1402eec40579SJoe Thornber 	if (era->metadata_dev)
1403eec40579SJoe Thornber 		dm_put_device(era->ti, era->metadata_dev);
1404eec40579SJoe Thornber 
1405eec40579SJoe Thornber 	kfree(era);
1406eec40579SJoe Thornber }
1407eec40579SJoe Thornber 
1408eec40579SJoe Thornber static dm_block_t calc_nr_blocks(struct era *era)
1409eec40579SJoe Thornber {
1410eec40579SJoe Thornber 	return dm_sector_div_up(era->ti->len, era->sectors_per_block);
1411eec40579SJoe Thornber }
1412eec40579SJoe Thornber 
1413eec40579SJoe Thornber static bool valid_block_size(dm_block_t block_size)
1414eec40579SJoe Thornber {
1415eec40579SJoe Thornber 	bool greater_than_zero = block_size > 0;
1416eec40579SJoe Thornber 	bool multiple_of_min_block_size = (block_size & (MIN_BLOCK_SIZE - 1)) == 0;
1417eec40579SJoe Thornber 
1418eec40579SJoe Thornber 	return greater_than_zero && multiple_of_min_block_size;
1419eec40579SJoe Thornber }
1420eec40579SJoe Thornber 
1421eec40579SJoe Thornber /*
1422eec40579SJoe Thornber  * <metadata dev> <data dev> <data block size (sectors)>
1423eec40579SJoe Thornber  */
1424eec40579SJoe Thornber static int era_ctr(struct dm_target *ti, unsigned argc, char **argv)
1425eec40579SJoe Thornber {
1426eec40579SJoe Thornber 	int r;
1427eec40579SJoe Thornber 	char dummy;
1428eec40579SJoe Thornber 	struct era *era;
1429eec40579SJoe Thornber 	struct era_metadata *md;
1430eec40579SJoe Thornber 
1431eec40579SJoe Thornber 	if (argc != 3) {
1432eec40579SJoe Thornber 		ti->error = "Invalid argument count";
1433eec40579SJoe Thornber 		return -EINVAL;
1434eec40579SJoe Thornber 	}
1435eec40579SJoe Thornber 
1436eec40579SJoe Thornber 	era = kzalloc(sizeof(*era), GFP_KERNEL);
1437eec40579SJoe Thornber 	if (!era) {
1438eec40579SJoe Thornber 		ti->error = "Error allocating era structure";
1439eec40579SJoe Thornber 		return -ENOMEM;
1440eec40579SJoe Thornber 	}
1441eec40579SJoe Thornber 
1442eec40579SJoe Thornber 	era->ti = ti;
1443eec40579SJoe Thornber 
1444eec40579SJoe Thornber 	r = dm_get_device(ti, argv[0], FMODE_READ | FMODE_WRITE, &era->metadata_dev);
1445eec40579SJoe Thornber 	if (r) {
1446eec40579SJoe Thornber 		ti->error = "Error opening metadata device";
1447eec40579SJoe Thornber 		era_destroy(era);
1448eec40579SJoe Thornber 		return -EINVAL;
1449eec40579SJoe Thornber 	}
1450eec40579SJoe Thornber 
1451eec40579SJoe Thornber 	r = dm_get_device(ti, argv[1], FMODE_READ | FMODE_WRITE, &era->origin_dev);
1452eec40579SJoe Thornber 	if (r) {
1453eec40579SJoe Thornber 		ti->error = "Error opening data device";
1454eec40579SJoe Thornber 		era_destroy(era);
1455eec40579SJoe Thornber 		return -EINVAL;
1456eec40579SJoe Thornber 	}
1457eec40579SJoe Thornber 
1458eec40579SJoe Thornber 	r = sscanf(argv[2], "%u%c", &era->sectors_per_block, &dummy);
1459eec40579SJoe Thornber 	if (r != 1) {
1460eec40579SJoe Thornber 		ti->error = "Error parsing block size";
1461eec40579SJoe Thornber 		era_destroy(era);
1462eec40579SJoe Thornber 		return -EINVAL;
1463eec40579SJoe Thornber 	}
1464eec40579SJoe Thornber 
1465eec40579SJoe Thornber 	r = dm_set_target_max_io_len(ti, era->sectors_per_block);
1466eec40579SJoe Thornber 	if (r) {
1467eec40579SJoe Thornber 		ti->error = "could not set max io len";
1468eec40579SJoe Thornber 		era_destroy(era);
1469eec40579SJoe Thornber 		return -EINVAL;
1470eec40579SJoe Thornber 	}
1471eec40579SJoe Thornber 
1472eec40579SJoe Thornber 	if (!valid_block_size(era->sectors_per_block)) {
1473eec40579SJoe Thornber 		ti->error = "Invalid block size";
1474eec40579SJoe Thornber 		era_destroy(era);
1475eec40579SJoe Thornber 		return -EINVAL;
1476eec40579SJoe Thornber 	}
1477eec40579SJoe Thornber 	if (era->sectors_per_block & (era->sectors_per_block - 1))
1478eec40579SJoe Thornber 		era->sectors_per_block_shift = -1;
1479eec40579SJoe Thornber 	else
1480eec40579SJoe Thornber 		era->sectors_per_block_shift = __ffs(era->sectors_per_block);
1481eec40579SJoe Thornber 
1482eec40579SJoe Thornber 	md = metadata_open(era->metadata_dev->bdev, era->sectors_per_block, true);
1483eec40579SJoe Thornber 	if (IS_ERR(md)) {
1484eec40579SJoe Thornber 		ti->error = "Error reading metadata";
1485eec40579SJoe Thornber 		era_destroy(era);
1486eec40579SJoe Thornber 		return PTR_ERR(md);
1487eec40579SJoe Thornber 	}
1488eec40579SJoe Thornber 	era->md = md;
1489eec40579SJoe Thornber 
1490eec40579SJoe Thornber 	era->nr_blocks = calc_nr_blocks(era);
1491eec40579SJoe Thornber 
1492eec40579SJoe Thornber 	r = metadata_resize(era->md, &era->nr_blocks);
1493eec40579SJoe Thornber 	if (r) {
1494eec40579SJoe Thornber 		ti->error = "couldn't resize metadata";
1495eec40579SJoe Thornber 		era_destroy(era);
1496eec40579SJoe Thornber 		return -ENOMEM;
1497eec40579SJoe Thornber 	}
1498eec40579SJoe Thornber 
1499eec40579SJoe Thornber 	era->wq = alloc_ordered_workqueue("dm-" DM_MSG_PREFIX, WQ_MEM_RECLAIM);
1500eec40579SJoe Thornber 	if (!era->wq) {
1501eec40579SJoe Thornber 		ti->error = "could not create workqueue for metadata object";
1502eec40579SJoe Thornber 		era_destroy(era);
1503eec40579SJoe Thornber 		return -ENOMEM;
1504eec40579SJoe Thornber 	}
1505eec40579SJoe Thornber 	INIT_WORK(&era->worker, do_work);
1506eec40579SJoe Thornber 
1507eec40579SJoe Thornber 	spin_lock_init(&era->deferred_lock);
1508eec40579SJoe Thornber 	bio_list_init(&era->deferred_bios);
1509eec40579SJoe Thornber 
1510eec40579SJoe Thornber 	spin_lock_init(&era->rpc_lock);
1511eec40579SJoe Thornber 	INIT_LIST_HEAD(&era->rpc_calls);
1512eec40579SJoe Thornber 
1513eec40579SJoe Thornber 	ti->private = era;
1514eec40579SJoe Thornber 	ti->num_flush_bios = 1;
1515eec40579SJoe Thornber 	ti->flush_supported = true;
1516eec40579SJoe Thornber 
1517eec40579SJoe Thornber 	ti->num_discard_bios = 1;
1518eec40579SJoe Thornber 
1519eec40579SJoe Thornber 	return 0;
1520eec40579SJoe Thornber }
1521eec40579SJoe Thornber 
1522eec40579SJoe Thornber static void era_dtr(struct dm_target *ti)
1523eec40579SJoe Thornber {
1524eec40579SJoe Thornber 	era_destroy(ti->private);
1525eec40579SJoe Thornber }
1526eec40579SJoe Thornber 
1527eec40579SJoe Thornber static int era_map(struct dm_target *ti, struct bio *bio)
1528eec40579SJoe Thornber {
1529eec40579SJoe Thornber 	struct era *era = ti->private;
1530eec40579SJoe Thornber 	dm_block_t block = get_block(era, bio);
1531eec40579SJoe Thornber 
1532eec40579SJoe Thornber 	/*
1533eec40579SJoe Thornber 	 * All bios get remapped to the origin device.  We do this now, but
1534eec40579SJoe Thornber 	 * it may not get issued until later.  Depending on whether the
1535eec40579SJoe Thornber 	 * block is marked in this era.
1536eec40579SJoe Thornber 	 */
1537eec40579SJoe Thornber 	remap_to_origin(era, bio);
1538eec40579SJoe Thornber 
1539eec40579SJoe Thornber 	/*
154028a8f0d3SMike Christie 	 * REQ_PREFLUSH bios carry no data, so we're not interested in them.
1541eec40579SJoe Thornber 	 */
15421eff9d32SJens Axboe 	if (!(bio->bi_opf & REQ_PREFLUSH) &&
1543eec40579SJoe Thornber 	    (bio_data_dir(bio) == WRITE) &&
1544eec40579SJoe Thornber 	    !metadata_current_marked(era->md, block)) {
1545eec40579SJoe Thornber 		defer_bio(era, bio);
1546eec40579SJoe Thornber 		return DM_MAPIO_SUBMITTED;
1547eec40579SJoe Thornber 	}
1548eec40579SJoe Thornber 
1549eec40579SJoe Thornber 	return DM_MAPIO_REMAPPED;
1550eec40579SJoe Thornber }
1551eec40579SJoe Thornber 
1552eec40579SJoe Thornber static void era_postsuspend(struct dm_target *ti)
1553eec40579SJoe Thornber {
1554eec40579SJoe Thornber 	int r;
1555eec40579SJoe Thornber 	struct era *era = ti->private;
1556eec40579SJoe Thornber 
1557eec40579SJoe Thornber 	r = in_worker0(era, metadata_era_archive);
1558eec40579SJoe Thornber 	if (r) {
1559eec40579SJoe Thornber 		DMERR("%s: couldn't archive current era", __func__);
1560eec40579SJoe Thornber 		/* FIXME: fail mode */
1561eec40579SJoe Thornber 	}
1562eec40579SJoe Thornber 
1563eec40579SJoe Thornber 	stop_worker(era);
1564eec40579SJoe Thornber }
1565eec40579SJoe Thornber 
1566eec40579SJoe Thornber static int era_preresume(struct dm_target *ti)
1567eec40579SJoe Thornber {
1568eec40579SJoe Thornber 	int r;
1569eec40579SJoe Thornber 	struct era *era = ti->private;
1570eec40579SJoe Thornber 	dm_block_t new_size = calc_nr_blocks(era);
1571eec40579SJoe Thornber 
1572eec40579SJoe Thornber 	if (era->nr_blocks != new_size) {
1573eec40579SJoe Thornber 		r = in_worker1(era, metadata_resize, &new_size);
1574eec40579SJoe Thornber 		if (r)
1575eec40579SJoe Thornber 			return r;
1576eec40579SJoe Thornber 
1577eec40579SJoe Thornber 		era->nr_blocks = new_size;
1578eec40579SJoe Thornber 	}
1579eec40579SJoe Thornber 
1580eec40579SJoe Thornber 	start_worker(era);
1581eec40579SJoe Thornber 
1582de89afc1SNikos Tsironis 	r = in_worker0(era, metadata_era_rollover);
1583eec40579SJoe Thornber 	if (r) {
1584eec40579SJoe Thornber 		DMERR("%s: metadata_era_rollover failed", __func__);
1585eec40579SJoe Thornber 		return r;
1586eec40579SJoe Thornber 	}
1587eec40579SJoe Thornber 
1588eec40579SJoe Thornber 	return 0;
1589eec40579SJoe Thornber }
1590eec40579SJoe Thornber 
1591eec40579SJoe Thornber /*
1592eec40579SJoe Thornber  * Status format:
1593eec40579SJoe Thornber  *
1594eec40579SJoe Thornber  * <metadata block size> <#used metadata blocks>/<#total metadata blocks>
1595eec40579SJoe Thornber  * <current era> <held metadata root | '-'>
1596eec40579SJoe Thornber  */
1597eec40579SJoe Thornber static void era_status(struct dm_target *ti, status_type_t type,
1598eec40579SJoe Thornber 		       unsigned status_flags, char *result, unsigned maxlen)
1599eec40579SJoe Thornber {
1600eec40579SJoe Thornber 	int r;
1601eec40579SJoe Thornber 	struct era *era = ti->private;
1602eec40579SJoe Thornber 	ssize_t sz = 0;
1603eec40579SJoe Thornber 	struct metadata_stats stats;
1604eec40579SJoe Thornber 	char buf[BDEVNAME_SIZE];
1605eec40579SJoe Thornber 
1606eec40579SJoe Thornber 	switch (type) {
1607eec40579SJoe Thornber 	case STATUSTYPE_INFO:
1608eec40579SJoe Thornber 		r = in_worker1(era, metadata_get_stats, &stats);
1609eec40579SJoe Thornber 		if (r)
1610eec40579SJoe Thornber 			goto err;
1611eec40579SJoe Thornber 
1612eec40579SJoe Thornber 		DMEMIT("%u %llu/%llu %u",
1613eec40579SJoe Thornber 		       (unsigned) (DM_ERA_METADATA_BLOCK_SIZE >> SECTOR_SHIFT),
1614eec40579SJoe Thornber 		       (unsigned long long) stats.used,
1615eec40579SJoe Thornber 		       (unsigned long long) stats.total,
1616eec40579SJoe Thornber 		       (unsigned) stats.era);
1617eec40579SJoe Thornber 
1618eec40579SJoe Thornber 		if (stats.snap != SUPERBLOCK_LOCATION)
1619eec40579SJoe Thornber 			DMEMIT(" %llu", stats.snap);
1620eec40579SJoe Thornber 		else
1621eec40579SJoe Thornber 			DMEMIT(" -");
1622eec40579SJoe Thornber 		break;
1623eec40579SJoe Thornber 
1624eec40579SJoe Thornber 	case STATUSTYPE_TABLE:
1625eec40579SJoe Thornber 		format_dev_t(buf, era->metadata_dev->bdev->bd_dev);
1626eec40579SJoe Thornber 		DMEMIT("%s ", buf);
1627eec40579SJoe Thornber 		format_dev_t(buf, era->origin_dev->bdev->bd_dev);
1628eec40579SJoe Thornber 		DMEMIT("%s %u", buf, era->sectors_per_block);
1629eec40579SJoe Thornber 		break;
1630eec40579SJoe Thornber 	}
1631eec40579SJoe Thornber 
1632eec40579SJoe Thornber 	return;
1633eec40579SJoe Thornber 
1634eec40579SJoe Thornber err:
1635eec40579SJoe Thornber 	DMEMIT("Error");
1636eec40579SJoe Thornber }
1637eec40579SJoe Thornber 
16381eb5fa84SMike Snitzer static int era_message(struct dm_target *ti, unsigned argc, char **argv,
16391eb5fa84SMike Snitzer 		       char *result, unsigned maxlen)
1640eec40579SJoe Thornber {
1641eec40579SJoe Thornber 	struct era *era = ti->private;
1642eec40579SJoe Thornber 
1643eec40579SJoe Thornber 	if (argc != 1) {
1644eec40579SJoe Thornber 		DMERR("incorrect number of message arguments");
1645eec40579SJoe Thornber 		return -EINVAL;
1646eec40579SJoe Thornber 	}
1647eec40579SJoe Thornber 
1648eec40579SJoe Thornber 	if (!strcasecmp(argv[0], "checkpoint"))
1649eec40579SJoe Thornber 		return in_worker0(era, metadata_checkpoint);
1650eec40579SJoe Thornber 
1651eec40579SJoe Thornber 	if (!strcasecmp(argv[0], "take_metadata_snap"))
1652eec40579SJoe Thornber 		return in_worker0(era, metadata_take_snap);
1653eec40579SJoe Thornber 
1654eec40579SJoe Thornber 	if (!strcasecmp(argv[0], "drop_metadata_snap"))
1655eec40579SJoe Thornber 		return in_worker0(era, metadata_drop_snap);
1656eec40579SJoe Thornber 
1657eec40579SJoe Thornber 	DMERR("unsupported message '%s'", argv[0]);
1658eec40579SJoe Thornber 	return -EINVAL;
1659eec40579SJoe Thornber }
1660eec40579SJoe Thornber 
1661eec40579SJoe Thornber static sector_t get_dev_size(struct dm_dev *dev)
1662eec40579SJoe Thornber {
1663eec40579SJoe Thornber 	return i_size_read(dev->bdev->bd_inode) >> SECTOR_SHIFT;
1664eec40579SJoe Thornber }
1665eec40579SJoe Thornber 
1666eec40579SJoe Thornber static int era_iterate_devices(struct dm_target *ti,
1667eec40579SJoe Thornber 			       iterate_devices_callout_fn fn, void *data)
1668eec40579SJoe Thornber {
1669eec40579SJoe Thornber 	struct era *era = ti->private;
1670eec40579SJoe Thornber 	return fn(ti, era->origin_dev, 0, get_dev_size(era->origin_dev), data);
1671eec40579SJoe Thornber }
1672eec40579SJoe Thornber 
1673eec40579SJoe Thornber static void era_io_hints(struct dm_target *ti, struct queue_limits *limits)
1674eec40579SJoe Thornber {
1675eec40579SJoe Thornber 	struct era *era = ti->private;
1676eec40579SJoe Thornber 	uint64_t io_opt_sectors = limits->io_opt >> SECTOR_SHIFT;
1677eec40579SJoe Thornber 
1678eec40579SJoe Thornber 	/*
1679eec40579SJoe Thornber 	 * If the system-determined stacked limits are compatible with the
1680eec40579SJoe Thornber 	 * era device's blocksize (io_opt is a factor) do not override them.
1681eec40579SJoe Thornber 	 */
1682eec40579SJoe Thornber 	if (io_opt_sectors < era->sectors_per_block ||
1683eec40579SJoe Thornber 	    do_div(io_opt_sectors, era->sectors_per_block)) {
1684eec40579SJoe Thornber 		blk_limits_io_min(limits, 0);
1685eec40579SJoe Thornber 		blk_limits_io_opt(limits, era->sectors_per_block << SECTOR_SHIFT);
1686eec40579SJoe Thornber 	}
1687eec40579SJoe Thornber }
1688eec40579SJoe Thornber 
1689eec40579SJoe Thornber /*----------------------------------------------------------------*/
1690eec40579SJoe Thornber 
1691eec40579SJoe Thornber static struct target_type era_target = {
1692eec40579SJoe Thornber 	.name = "era",
1693eec40579SJoe Thornber 	.version = {1, 0, 0},
1694eec40579SJoe Thornber 	.module = THIS_MODULE,
1695eec40579SJoe Thornber 	.ctr = era_ctr,
1696eec40579SJoe Thornber 	.dtr = era_dtr,
1697eec40579SJoe Thornber 	.map = era_map,
1698eec40579SJoe Thornber 	.postsuspend = era_postsuspend,
1699eec40579SJoe Thornber 	.preresume = era_preresume,
1700eec40579SJoe Thornber 	.status = era_status,
1701eec40579SJoe Thornber 	.message = era_message,
1702eec40579SJoe Thornber 	.iterate_devices = era_iterate_devices,
1703eec40579SJoe Thornber 	.io_hints = era_io_hints
1704eec40579SJoe Thornber };
1705eec40579SJoe Thornber 
1706eec40579SJoe Thornber static int __init dm_era_init(void)
1707eec40579SJoe Thornber {
1708eec40579SJoe Thornber 	int r;
1709eec40579SJoe Thornber 
1710eec40579SJoe Thornber 	r = dm_register_target(&era_target);
1711eec40579SJoe Thornber 	if (r) {
1712eec40579SJoe Thornber 		DMERR("era target registration failed: %d", r);
1713eec40579SJoe Thornber 		return r;
1714eec40579SJoe Thornber 	}
1715eec40579SJoe Thornber 
1716eec40579SJoe Thornber 	return 0;
1717eec40579SJoe Thornber }
1718eec40579SJoe Thornber 
1719eec40579SJoe Thornber static void __exit dm_era_exit(void)
1720eec40579SJoe Thornber {
1721eec40579SJoe Thornber 	dm_unregister_target(&era_target);
1722eec40579SJoe Thornber }
1723eec40579SJoe Thornber 
1724eec40579SJoe Thornber module_init(dm_era_init);
1725eec40579SJoe Thornber module_exit(dm_era_exit);
1726eec40579SJoe Thornber 
1727eec40579SJoe Thornber MODULE_DESCRIPTION(DM_NAME " era target");
1728eec40579SJoe Thornber MODULE_AUTHOR("Joe Thornber <ejt@redhat.com>");
1729eec40579SJoe Thornber MODULE_LICENSE("GPL");
1730