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