xref: /openbmc/linux/drivers/md/dm-thin-metadata.c (revision 8111964f1b8524c4bb56b02cd9c7a37725ea21fd)
1 /*
2  * Copyright (C) 2011-2012 Red Hat, Inc.
3  *
4  * This file is released under the GPL.
5  */
6 
7 #include "dm-thin-metadata.h"
8 #include "persistent-data/dm-btree.h"
9 #include "persistent-data/dm-space-map.h"
10 #include "persistent-data/dm-space-map-disk.h"
11 #include "persistent-data/dm-transaction-manager.h"
12 
13 #include <linux/list.h>
14 #include <linux/device-mapper.h>
15 #include <linux/workqueue.h>
16 
17 /*--------------------------------------------------------------------------
18  * As far as the metadata goes, there is:
19  *
20  * - A superblock in block zero, taking up fewer than 512 bytes for
21  *   atomic writes.
22  *
23  * - A space map managing the metadata blocks.
24  *
25  * - A space map managing the data blocks.
26  *
27  * - A btree mapping our internal thin dev ids onto struct disk_device_details.
28  *
29  * - A hierarchical btree, with 2 levels which effectively maps (thin
30  *   dev id, virtual block) -> block_time.  Block time is a 64-bit
31  *   field holding the time in the low 24 bits, and block in the top 40
32  *   bits.
33  *
34  * BTrees consist solely of btree_nodes, that fill a block.  Some are
35  * internal nodes, as such their values are a __le64 pointing to other
36  * nodes.  Leaf nodes can store data of any reasonable size (ie. much
37  * smaller than the block size).  The nodes consist of the header,
38  * followed by an array of keys, followed by an array of values.  We have
39  * to binary search on the keys so they're all held together to help the
40  * cpu cache.
41  *
42  * Space maps have 2 btrees:
43  *
44  * - One maps a uint64_t onto a struct index_entry.  Which points to a
45  *   bitmap block, and has some details about how many free entries there
46  *   are etc.
47  *
48  * - The bitmap blocks have a header (for the checksum).  Then the rest
49  *   of the block is pairs of bits.  With the meaning being:
50  *
51  *   0 - ref count is 0
52  *   1 - ref count is 1
53  *   2 - ref count is 2
54  *   3 - ref count is higher than 2
55  *
56  * - If the count is higher than 2 then the ref count is entered in a
57  *   second btree that directly maps the block_address to a uint32_t ref
58  *   count.
59  *
60  * The space map metadata variant doesn't have a bitmaps btree.  Instead
61  * it has one single blocks worth of index_entries.  This avoids
62  * recursive issues with the bitmap btree needing to allocate space in
63  * order to insert.  With a small data block size such as 64k the
64  * metadata support data devices that are hundreds of terrabytes.
65  *
66  * The space maps allocate space linearly from front to back.  Space that
67  * is freed in a transaction is never recycled within that transaction.
68  * To try and avoid fragmenting _free_ space the allocator always goes
69  * back and fills in gaps.
70  *
71  * All metadata io is in THIN_METADATA_BLOCK_SIZE sized/aligned chunks
72  * from the block manager.
73  *--------------------------------------------------------------------------*/
74 
75 #define DM_MSG_PREFIX   "thin metadata"
76 
77 #define THIN_SUPERBLOCK_MAGIC 27022010
78 #define THIN_SUPERBLOCK_LOCATION 0
79 #define THIN_VERSION 2
80 #define SECTOR_TO_BLOCK_SHIFT 3
81 
82 /*
83  * For btree insert:
84  *  3 for btree insert +
85  *  2 for btree lookup used within space map
86  * For btree remove:
87  *  2 for shadow spine +
88  *  4 for rebalance 3 child node
89  */
90 #define THIN_MAX_CONCURRENT_LOCKS 6
91 
92 /* This should be plenty */
93 #define SPACE_MAP_ROOT_SIZE 128
94 
95 /*
96  * Little endian on-disk superblock and device details.
97  */
98 struct thin_disk_superblock {
99 	__le32 csum;	/* Checksum of superblock except for this field. */
100 	__le32 flags;
101 	__le64 blocknr;	/* This block number, dm_block_t. */
102 
103 	__u8 uuid[16];
104 	__le64 magic;
105 	__le32 version;
106 	__le32 time;
107 
108 	__le64 trans_id;
109 
110 	/*
111 	 * Root held by userspace transactions.
112 	 */
113 	__le64 held_root;
114 
115 	__u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
116 	__u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
117 
118 	/*
119 	 * 2-level btree mapping (dev_id, (dev block, time)) -> data block
120 	 */
121 	__le64 data_mapping_root;
122 
123 	/*
124 	 * Device detail root mapping dev_id -> device_details
125 	 */
126 	__le64 device_details_root;
127 
128 	__le32 data_block_size;		/* In 512-byte sectors. */
129 
130 	__le32 metadata_block_size;	/* In 512-byte sectors. */
131 	__le64 metadata_nr_blocks;
132 
133 	__le32 compat_flags;
134 	__le32 compat_ro_flags;
135 	__le32 incompat_flags;
136 } __packed;
137 
138 struct disk_device_details {
139 	__le64 mapped_blocks;
140 	__le64 transaction_id;		/* When created. */
141 	__le32 creation_time;
142 	__le32 snapshotted_time;
143 } __packed;
144 
145 struct dm_pool_metadata {
146 	struct hlist_node hash;
147 
148 	struct block_device *bdev;
149 	struct dm_block_manager *bm;
150 	struct dm_space_map *metadata_sm;
151 	struct dm_space_map *data_sm;
152 	struct dm_transaction_manager *tm;
153 	struct dm_transaction_manager *nb_tm;
154 
155 	/*
156 	 * Two-level btree.
157 	 * First level holds thin_dev_t.
158 	 * Second level holds mappings.
159 	 */
160 	struct dm_btree_info info;
161 
162 	/*
163 	 * Non-blocking version of the above.
164 	 */
165 	struct dm_btree_info nb_info;
166 
167 	/*
168 	 * Just the top level for deleting whole devices.
169 	 */
170 	struct dm_btree_info tl_info;
171 
172 	/*
173 	 * Just the bottom level for creating new devices.
174 	 */
175 	struct dm_btree_info bl_info;
176 
177 	/*
178 	 * Describes the device details btree.
179 	 */
180 	struct dm_btree_info details_info;
181 
182 	struct rw_semaphore root_lock;
183 	uint32_t time;
184 	dm_block_t root;
185 	dm_block_t details_root;
186 	struct list_head thin_devices;
187 	uint64_t trans_id;
188 	unsigned long flags;
189 	sector_t data_block_size;
190 
191 	/*
192 	 * Pre-commit callback.
193 	 *
194 	 * This allows the thin provisioning target to run a callback before
195 	 * the metadata are committed.
196 	 */
197 	dm_pool_pre_commit_fn pre_commit_fn;
198 	void *pre_commit_context;
199 
200 	/*
201 	 * We reserve a section of the metadata for commit overhead.
202 	 * All reported space does *not* include this.
203 	 */
204 	dm_block_t metadata_reserve;
205 
206 	/*
207 	 * Set if a transaction has to be aborted but the attempt to roll back
208 	 * to the previous (good) transaction failed.  The only pool metadata
209 	 * operation possible in this state is the closing of the device.
210 	 */
211 	bool fail_io:1;
212 
213 	/*
214 	 * Set once a thin-pool has been accessed through one of the interfaces
215 	 * that imply the pool is in-service (e.g. thin devices created/deleted,
216 	 * thin-pool message, metadata snapshots, etc).
217 	 */
218 	bool in_service:1;
219 
220 	/*
221 	 * Reading the space map roots can fail, so we read it into these
222 	 * buffers before the superblock is locked and updated.
223 	 */
224 	__u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
225 	__u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
226 };
227 
228 struct dm_thin_device {
229 	struct list_head list;
230 	struct dm_pool_metadata *pmd;
231 	dm_thin_id id;
232 
233 	int open_count;
234 	bool changed:1;
235 	bool aborted_with_changes:1;
236 	uint64_t mapped_blocks;
237 	uint64_t transaction_id;
238 	uint32_t creation_time;
239 	uint32_t snapshotted_time;
240 };
241 
242 /*----------------------------------------------------------------
243  * superblock validator
244  *--------------------------------------------------------------*/
245 
246 #define SUPERBLOCK_CSUM_XOR 160774
247 
248 static void sb_prepare_for_write(struct dm_block_validator *v,
249 				 struct dm_block *b,
250 				 size_t block_size)
251 {
252 	struct thin_disk_superblock *disk_super = dm_block_data(b);
253 
254 	disk_super->blocknr = cpu_to_le64(dm_block_location(b));
255 	disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
256 						      block_size - sizeof(__le32),
257 						      SUPERBLOCK_CSUM_XOR));
258 }
259 
260 static int sb_check(struct dm_block_validator *v,
261 		    struct dm_block *b,
262 		    size_t block_size)
263 {
264 	struct thin_disk_superblock *disk_super = dm_block_data(b);
265 	__le32 csum_le;
266 
267 	if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
268 		DMERR("sb_check failed: blocknr %llu: "
269 		      "wanted %llu", le64_to_cpu(disk_super->blocknr),
270 		      (unsigned long long)dm_block_location(b));
271 		return -ENOTBLK;
272 	}
273 
274 	if (le64_to_cpu(disk_super->magic) != THIN_SUPERBLOCK_MAGIC) {
275 		DMERR("sb_check failed: magic %llu: "
276 		      "wanted %llu", le64_to_cpu(disk_super->magic),
277 		      (unsigned long long)THIN_SUPERBLOCK_MAGIC);
278 		return -EILSEQ;
279 	}
280 
281 	csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
282 					     block_size - sizeof(__le32),
283 					     SUPERBLOCK_CSUM_XOR));
284 	if (csum_le != disk_super->csum) {
285 		DMERR("sb_check failed: csum %u: wanted %u",
286 		      le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
287 		return -EILSEQ;
288 	}
289 
290 	return 0;
291 }
292 
293 static struct dm_block_validator sb_validator = {
294 	.name = "superblock",
295 	.prepare_for_write = sb_prepare_for_write,
296 	.check = sb_check
297 };
298 
299 /*----------------------------------------------------------------
300  * Methods for the btree value types
301  *--------------------------------------------------------------*/
302 
303 static uint64_t pack_block_time(dm_block_t b, uint32_t t)
304 {
305 	return (b << 24) | t;
306 }
307 
308 static void unpack_block_time(uint64_t v, dm_block_t *b, uint32_t *t)
309 {
310 	*b = v >> 24;
311 	*t = v & ((1 << 24) - 1);
312 }
313 
314 /*
315  * It's more efficient to call dm_sm_{inc,dec}_blocks as few times as
316  * possible.  'with_runs' reads contiguous runs of blocks, and calls the
317  * given sm function.
318  */
319 typedef int (*run_fn)(struct dm_space_map *, dm_block_t, dm_block_t);
320 
321 static void with_runs(struct dm_space_map *sm, const __le64 *value_le, unsigned count, run_fn fn)
322 {
323 	uint64_t b, begin, end;
324 	uint32_t t;
325 	bool in_run = false;
326 	unsigned i;
327 
328 	for (i = 0; i < count; i++, value_le++) {
329 		/* We know value_le is 8 byte aligned */
330 		unpack_block_time(le64_to_cpu(*value_le), &b, &t);
331 
332 		if (in_run) {
333 			if (b == end) {
334 				end++;
335 			} else {
336 				fn(sm, begin, end);
337 				begin = b;
338 				end = b + 1;
339 			}
340 		} else {
341 			in_run = true;
342 			begin = b;
343 			end = b + 1;
344 		}
345 	}
346 
347 	if (in_run)
348 		fn(sm, begin, end);
349 }
350 
351 static void data_block_inc(void *context, const void *value_le, unsigned count)
352 {
353 	with_runs((struct dm_space_map *) context,
354 		  (const __le64 *) value_le, count, dm_sm_inc_blocks);
355 }
356 
357 static void data_block_dec(void *context, const void *value_le, unsigned count)
358 {
359 	with_runs((struct dm_space_map *) context,
360 		  (const __le64 *) value_le, count, dm_sm_dec_blocks);
361 }
362 
363 static int data_block_equal(void *context, const void *value1_le, const void *value2_le)
364 {
365 	__le64 v1_le, v2_le;
366 	uint64_t b1, b2;
367 	uint32_t t;
368 
369 	memcpy(&v1_le, value1_le, sizeof(v1_le));
370 	memcpy(&v2_le, value2_le, sizeof(v2_le));
371 	unpack_block_time(le64_to_cpu(v1_le), &b1, &t);
372 	unpack_block_time(le64_to_cpu(v2_le), &b2, &t);
373 
374 	return b1 == b2;
375 }
376 
377 static void subtree_inc(void *context, const void *value, unsigned count)
378 {
379 	struct dm_btree_info *info = context;
380 	const __le64 *root_le = value;
381 	unsigned i;
382 
383 	for (i = 0; i < count; i++, root_le++)
384 		dm_tm_inc(info->tm, le64_to_cpu(*root_le));
385 }
386 
387 static void subtree_dec(void *context, const void *value, unsigned count)
388 {
389 	struct dm_btree_info *info = context;
390 	const __le64 *root_le = value;
391 	unsigned i;
392 
393 	for (i = 0; i < count; i++, root_le++)
394 		if (dm_btree_del(info, le64_to_cpu(*root_le)))
395 			DMERR("btree delete failed");
396 }
397 
398 static int subtree_equal(void *context, const void *value1_le, const void *value2_le)
399 {
400 	__le64 v1_le, v2_le;
401 	memcpy(&v1_le, value1_le, sizeof(v1_le));
402 	memcpy(&v2_le, value2_le, sizeof(v2_le));
403 
404 	return v1_le == v2_le;
405 }
406 
407 /*----------------------------------------------------------------*/
408 
409 /*
410  * Variant that is used for in-core only changes or code that
411  * shouldn't put the pool in service on its own (e.g. commit).
412  */
413 static inline void pmd_write_lock_in_core(struct dm_pool_metadata *pmd)
414 	__acquires(pmd->root_lock)
415 {
416 	down_write(&pmd->root_lock);
417 }
418 
419 static inline void pmd_write_lock(struct dm_pool_metadata *pmd)
420 {
421 	pmd_write_lock_in_core(pmd);
422 	if (unlikely(!pmd->in_service))
423 		pmd->in_service = true;
424 }
425 
426 static inline void pmd_write_unlock(struct dm_pool_metadata *pmd)
427 	__releases(pmd->root_lock)
428 {
429 	up_write(&pmd->root_lock);
430 }
431 
432 /*----------------------------------------------------------------*/
433 
434 static int superblock_lock_zero(struct dm_pool_metadata *pmd,
435 				struct dm_block **sblock)
436 {
437 	return dm_bm_write_lock_zero(pmd->bm, THIN_SUPERBLOCK_LOCATION,
438 				     &sb_validator, sblock);
439 }
440 
441 static int superblock_lock(struct dm_pool_metadata *pmd,
442 			   struct dm_block **sblock)
443 {
444 	return dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
445 				&sb_validator, sblock);
446 }
447 
448 static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result)
449 {
450 	int r;
451 	unsigned i;
452 	struct dm_block *b;
453 	__le64 *data_le, zero = cpu_to_le64(0);
454 	unsigned block_size = dm_bm_block_size(bm) / sizeof(__le64);
455 
456 	/*
457 	 * We can't use a validator here - it may be all zeroes.
458 	 */
459 	r = dm_bm_read_lock(bm, THIN_SUPERBLOCK_LOCATION, NULL, &b);
460 	if (r)
461 		return r;
462 
463 	data_le = dm_block_data(b);
464 	*result = 1;
465 	for (i = 0; i < block_size; i++) {
466 		if (data_le[i] != zero) {
467 			*result = 0;
468 			break;
469 		}
470 	}
471 
472 	dm_bm_unlock(b);
473 
474 	return 0;
475 }
476 
477 static void __setup_btree_details(struct dm_pool_metadata *pmd)
478 {
479 	pmd->info.tm = pmd->tm;
480 	pmd->info.levels = 2;
481 	pmd->info.value_type.context = pmd->data_sm;
482 	pmd->info.value_type.size = sizeof(__le64);
483 	pmd->info.value_type.inc = data_block_inc;
484 	pmd->info.value_type.dec = data_block_dec;
485 	pmd->info.value_type.equal = data_block_equal;
486 
487 	memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info));
488 	pmd->nb_info.tm = pmd->nb_tm;
489 
490 	pmd->tl_info.tm = pmd->tm;
491 	pmd->tl_info.levels = 1;
492 	pmd->tl_info.value_type.context = &pmd->bl_info;
493 	pmd->tl_info.value_type.size = sizeof(__le64);
494 	pmd->tl_info.value_type.inc = subtree_inc;
495 	pmd->tl_info.value_type.dec = subtree_dec;
496 	pmd->tl_info.value_type.equal = subtree_equal;
497 
498 	pmd->bl_info.tm = pmd->tm;
499 	pmd->bl_info.levels = 1;
500 	pmd->bl_info.value_type.context = pmd->data_sm;
501 	pmd->bl_info.value_type.size = sizeof(__le64);
502 	pmd->bl_info.value_type.inc = data_block_inc;
503 	pmd->bl_info.value_type.dec = data_block_dec;
504 	pmd->bl_info.value_type.equal = data_block_equal;
505 
506 	pmd->details_info.tm = pmd->tm;
507 	pmd->details_info.levels = 1;
508 	pmd->details_info.value_type.context = NULL;
509 	pmd->details_info.value_type.size = sizeof(struct disk_device_details);
510 	pmd->details_info.value_type.inc = NULL;
511 	pmd->details_info.value_type.dec = NULL;
512 	pmd->details_info.value_type.equal = NULL;
513 }
514 
515 static int save_sm_roots(struct dm_pool_metadata *pmd)
516 {
517 	int r;
518 	size_t len;
519 
520 	r = dm_sm_root_size(pmd->metadata_sm, &len);
521 	if (r < 0)
522 		return r;
523 
524 	r = dm_sm_copy_root(pmd->metadata_sm, &pmd->metadata_space_map_root, len);
525 	if (r < 0)
526 		return r;
527 
528 	r = dm_sm_root_size(pmd->data_sm, &len);
529 	if (r < 0)
530 		return r;
531 
532 	return dm_sm_copy_root(pmd->data_sm, &pmd->data_space_map_root, len);
533 }
534 
535 static void copy_sm_roots(struct dm_pool_metadata *pmd,
536 			  struct thin_disk_superblock *disk)
537 {
538 	memcpy(&disk->metadata_space_map_root,
539 	       &pmd->metadata_space_map_root,
540 	       sizeof(pmd->metadata_space_map_root));
541 
542 	memcpy(&disk->data_space_map_root,
543 	       &pmd->data_space_map_root,
544 	       sizeof(pmd->data_space_map_root));
545 }
546 
547 static int __write_initial_superblock(struct dm_pool_metadata *pmd)
548 {
549 	int r;
550 	struct dm_block *sblock;
551 	struct thin_disk_superblock *disk_super;
552 	sector_t bdev_size = bdev_nr_sectors(pmd->bdev);
553 
554 	if (bdev_size > THIN_METADATA_MAX_SECTORS)
555 		bdev_size = THIN_METADATA_MAX_SECTORS;
556 
557 	r = dm_sm_commit(pmd->data_sm);
558 	if (r < 0)
559 		return r;
560 
561 	r = dm_tm_pre_commit(pmd->tm);
562 	if (r < 0)
563 		return r;
564 
565 	r = save_sm_roots(pmd);
566 	if (r < 0)
567 		return r;
568 
569 	r = superblock_lock_zero(pmd, &sblock);
570 	if (r)
571 		return r;
572 
573 	disk_super = dm_block_data(sblock);
574 	disk_super->flags = 0;
575 	memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
576 	disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
577 	disk_super->version = cpu_to_le32(THIN_VERSION);
578 	disk_super->time = 0;
579 	disk_super->trans_id = 0;
580 	disk_super->held_root = 0;
581 
582 	copy_sm_roots(pmd, disk_super);
583 
584 	disk_super->data_mapping_root = cpu_to_le64(pmd->root);
585 	disk_super->device_details_root = cpu_to_le64(pmd->details_root);
586 	disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE);
587 	disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
588 	disk_super->data_block_size = cpu_to_le32(pmd->data_block_size);
589 
590 	return dm_tm_commit(pmd->tm, sblock);
591 }
592 
593 static int __format_metadata(struct dm_pool_metadata *pmd)
594 {
595 	int r;
596 
597 	r = dm_tm_create_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
598 				 &pmd->tm, &pmd->metadata_sm);
599 	if (r < 0) {
600 		DMERR("tm_create_with_sm failed");
601 		return r;
602 	}
603 
604 	pmd->data_sm = dm_sm_disk_create(pmd->tm, 0);
605 	if (IS_ERR(pmd->data_sm)) {
606 		DMERR("sm_disk_create failed");
607 		r = PTR_ERR(pmd->data_sm);
608 		goto bad_cleanup_tm;
609 	}
610 
611 	pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
612 	if (!pmd->nb_tm) {
613 		DMERR("could not create non-blocking clone tm");
614 		r = -ENOMEM;
615 		goto bad_cleanup_data_sm;
616 	}
617 
618 	__setup_btree_details(pmd);
619 
620 	r = dm_btree_empty(&pmd->info, &pmd->root);
621 	if (r < 0)
622 		goto bad_cleanup_nb_tm;
623 
624 	r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
625 	if (r < 0) {
626 		DMERR("couldn't create devices root");
627 		goto bad_cleanup_nb_tm;
628 	}
629 
630 	r = __write_initial_superblock(pmd);
631 	if (r)
632 		goto bad_cleanup_nb_tm;
633 
634 	return 0;
635 
636 bad_cleanup_nb_tm:
637 	dm_tm_destroy(pmd->nb_tm);
638 bad_cleanup_data_sm:
639 	dm_sm_destroy(pmd->data_sm);
640 bad_cleanup_tm:
641 	dm_tm_destroy(pmd->tm);
642 	dm_sm_destroy(pmd->metadata_sm);
643 
644 	return r;
645 }
646 
647 static int __check_incompat_features(struct thin_disk_superblock *disk_super,
648 				     struct dm_pool_metadata *pmd)
649 {
650 	uint32_t features;
651 
652 	features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP;
653 	if (features) {
654 		DMERR("could not access metadata due to unsupported optional features (%lx).",
655 		      (unsigned long)features);
656 		return -EINVAL;
657 	}
658 
659 	/*
660 	 * Check for read-only metadata to skip the following RDWR checks.
661 	 */
662 	if (bdev_read_only(pmd->bdev))
663 		return 0;
664 
665 	features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
666 	if (features) {
667 		DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
668 		      (unsigned long)features);
669 		return -EINVAL;
670 	}
671 
672 	return 0;
673 }
674 
675 static int __open_metadata(struct dm_pool_metadata *pmd)
676 {
677 	int r;
678 	struct dm_block *sblock;
679 	struct thin_disk_superblock *disk_super;
680 
681 	r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
682 			    &sb_validator, &sblock);
683 	if (r < 0) {
684 		DMERR("couldn't read superblock");
685 		return r;
686 	}
687 
688 	disk_super = dm_block_data(sblock);
689 
690 	/* Verify the data block size hasn't changed */
691 	if (le32_to_cpu(disk_super->data_block_size) != pmd->data_block_size) {
692 		DMERR("changing the data block size (from %u to %llu) is not supported",
693 		      le32_to_cpu(disk_super->data_block_size),
694 		      (unsigned long long)pmd->data_block_size);
695 		r = -EINVAL;
696 		goto bad_unlock_sblock;
697 	}
698 
699 	r = __check_incompat_features(disk_super, pmd);
700 	if (r < 0)
701 		goto bad_unlock_sblock;
702 
703 	r = dm_tm_open_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
704 			       disk_super->metadata_space_map_root,
705 			       sizeof(disk_super->metadata_space_map_root),
706 			       &pmd->tm, &pmd->metadata_sm);
707 	if (r < 0) {
708 		DMERR("tm_open_with_sm failed");
709 		goto bad_unlock_sblock;
710 	}
711 
712 	pmd->data_sm = dm_sm_disk_open(pmd->tm, disk_super->data_space_map_root,
713 				       sizeof(disk_super->data_space_map_root));
714 	if (IS_ERR(pmd->data_sm)) {
715 		DMERR("sm_disk_open failed");
716 		r = PTR_ERR(pmd->data_sm);
717 		goto bad_cleanup_tm;
718 	}
719 
720 	pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
721 	if (!pmd->nb_tm) {
722 		DMERR("could not create non-blocking clone tm");
723 		r = -ENOMEM;
724 		goto bad_cleanup_data_sm;
725 	}
726 
727 	__setup_btree_details(pmd);
728 	dm_bm_unlock(sblock);
729 
730 	return 0;
731 
732 bad_cleanup_data_sm:
733 	dm_sm_destroy(pmd->data_sm);
734 bad_cleanup_tm:
735 	dm_tm_destroy(pmd->tm);
736 	dm_sm_destroy(pmd->metadata_sm);
737 bad_unlock_sblock:
738 	dm_bm_unlock(sblock);
739 
740 	return r;
741 }
742 
743 static int __open_or_format_metadata(struct dm_pool_metadata *pmd, bool format_device)
744 {
745 	int r, unformatted;
746 
747 	r = __superblock_all_zeroes(pmd->bm, &unformatted);
748 	if (r)
749 		return r;
750 
751 	if (unformatted)
752 		return format_device ? __format_metadata(pmd) : -EPERM;
753 
754 	return __open_metadata(pmd);
755 }
756 
757 static int __create_persistent_data_objects(struct dm_pool_metadata *pmd, bool format_device)
758 {
759 	int r;
760 
761 	pmd->bm = dm_block_manager_create(pmd->bdev, THIN_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
762 					  THIN_MAX_CONCURRENT_LOCKS);
763 	if (IS_ERR(pmd->bm)) {
764 		DMERR("could not create block manager");
765 		r = PTR_ERR(pmd->bm);
766 		pmd->bm = NULL;
767 		return r;
768 	}
769 
770 	r = __open_or_format_metadata(pmd, format_device);
771 	if (r) {
772 		dm_block_manager_destroy(pmd->bm);
773 		pmd->bm = NULL;
774 	}
775 
776 	return r;
777 }
778 
779 static void __destroy_persistent_data_objects(struct dm_pool_metadata *pmd,
780 					      bool destroy_bm)
781 {
782 	dm_sm_destroy(pmd->data_sm);
783 	dm_sm_destroy(pmd->metadata_sm);
784 	dm_tm_destroy(pmd->nb_tm);
785 	dm_tm_destroy(pmd->tm);
786 	if (destroy_bm)
787 		dm_block_manager_destroy(pmd->bm);
788 }
789 
790 static int __begin_transaction(struct dm_pool_metadata *pmd)
791 {
792 	int r;
793 	struct thin_disk_superblock *disk_super;
794 	struct dm_block *sblock;
795 
796 	/*
797 	 * We re-read the superblock every time.  Shouldn't need to do this
798 	 * really.
799 	 */
800 	r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
801 			    &sb_validator, &sblock);
802 	if (r)
803 		return r;
804 
805 	disk_super = dm_block_data(sblock);
806 	pmd->time = le32_to_cpu(disk_super->time);
807 	pmd->root = le64_to_cpu(disk_super->data_mapping_root);
808 	pmd->details_root = le64_to_cpu(disk_super->device_details_root);
809 	pmd->trans_id = le64_to_cpu(disk_super->trans_id);
810 	pmd->flags = le32_to_cpu(disk_super->flags);
811 	pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
812 
813 	dm_bm_unlock(sblock);
814 	return 0;
815 }
816 
817 static int __write_changed_details(struct dm_pool_metadata *pmd)
818 {
819 	int r;
820 	struct dm_thin_device *td, *tmp;
821 	struct disk_device_details details;
822 	uint64_t key;
823 
824 	list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
825 		if (!td->changed)
826 			continue;
827 
828 		key = td->id;
829 
830 		details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
831 		details.transaction_id = cpu_to_le64(td->transaction_id);
832 		details.creation_time = cpu_to_le32(td->creation_time);
833 		details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
834 		__dm_bless_for_disk(&details);
835 
836 		r = dm_btree_insert(&pmd->details_info, pmd->details_root,
837 				    &key, &details, &pmd->details_root);
838 		if (r)
839 			return r;
840 
841 		if (td->open_count)
842 			td->changed = false;
843 		else {
844 			list_del(&td->list);
845 			kfree(td);
846 		}
847 	}
848 
849 	return 0;
850 }
851 
852 static int __commit_transaction(struct dm_pool_metadata *pmd)
853 {
854 	int r;
855 	struct thin_disk_superblock *disk_super;
856 	struct dm_block *sblock;
857 
858 	/*
859 	 * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
860 	 */
861 	BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
862 	BUG_ON(!rwsem_is_locked(&pmd->root_lock));
863 
864 	if (unlikely(!pmd->in_service))
865 		return 0;
866 
867 	if (pmd->pre_commit_fn) {
868 		r = pmd->pre_commit_fn(pmd->pre_commit_context);
869 		if (r < 0) {
870 			DMERR("pre-commit callback failed");
871 			return r;
872 		}
873 	}
874 
875 	r = __write_changed_details(pmd);
876 	if (r < 0)
877 		return r;
878 
879 	r = dm_sm_commit(pmd->data_sm);
880 	if (r < 0)
881 		return r;
882 
883 	r = dm_tm_pre_commit(pmd->tm);
884 	if (r < 0)
885 		return r;
886 
887 	r = save_sm_roots(pmd);
888 	if (r < 0)
889 		return r;
890 
891 	r = superblock_lock(pmd, &sblock);
892 	if (r)
893 		return r;
894 
895 	disk_super = dm_block_data(sblock);
896 	disk_super->time = cpu_to_le32(pmd->time);
897 	disk_super->data_mapping_root = cpu_to_le64(pmd->root);
898 	disk_super->device_details_root = cpu_to_le64(pmd->details_root);
899 	disk_super->trans_id = cpu_to_le64(pmd->trans_id);
900 	disk_super->flags = cpu_to_le32(pmd->flags);
901 
902 	copy_sm_roots(pmd, disk_super);
903 
904 	return dm_tm_commit(pmd->tm, sblock);
905 }
906 
907 static void __set_metadata_reserve(struct dm_pool_metadata *pmd)
908 {
909 	int r;
910 	dm_block_t total;
911 	dm_block_t max_blocks = 4096; /* 16M */
912 
913 	r = dm_sm_get_nr_blocks(pmd->metadata_sm, &total);
914 	if (r) {
915 		DMERR("could not get size of metadata device");
916 		pmd->metadata_reserve = max_blocks;
917 	} else
918 		pmd->metadata_reserve = min(max_blocks, div_u64(total, 10));
919 }
920 
921 struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
922 					       sector_t data_block_size,
923 					       bool format_device)
924 {
925 	int r;
926 	struct dm_pool_metadata *pmd;
927 
928 	pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
929 	if (!pmd) {
930 		DMERR("could not allocate metadata struct");
931 		return ERR_PTR(-ENOMEM);
932 	}
933 
934 	init_rwsem(&pmd->root_lock);
935 	pmd->time = 0;
936 	INIT_LIST_HEAD(&pmd->thin_devices);
937 	pmd->fail_io = false;
938 	pmd->in_service = false;
939 	pmd->bdev = bdev;
940 	pmd->data_block_size = data_block_size;
941 	pmd->pre_commit_fn = NULL;
942 	pmd->pre_commit_context = NULL;
943 
944 	r = __create_persistent_data_objects(pmd, format_device);
945 	if (r) {
946 		kfree(pmd);
947 		return ERR_PTR(r);
948 	}
949 
950 	r = __begin_transaction(pmd);
951 	if (r < 0) {
952 		if (dm_pool_metadata_close(pmd) < 0)
953 			DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
954 		return ERR_PTR(r);
955 	}
956 
957 	__set_metadata_reserve(pmd);
958 
959 	return pmd;
960 }
961 
962 int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
963 {
964 	int r;
965 	unsigned open_devices = 0;
966 	struct dm_thin_device *td, *tmp;
967 
968 	down_read(&pmd->root_lock);
969 	list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
970 		if (td->open_count)
971 			open_devices++;
972 		else {
973 			list_del(&td->list);
974 			kfree(td);
975 		}
976 	}
977 	up_read(&pmd->root_lock);
978 
979 	if (open_devices) {
980 		DMERR("attempt to close pmd when %u device(s) are still open",
981 		       open_devices);
982 		return -EBUSY;
983 	}
984 
985 	pmd_write_lock_in_core(pmd);
986 	if (!pmd->fail_io && !dm_bm_is_read_only(pmd->bm)) {
987 		r = __commit_transaction(pmd);
988 		if (r < 0)
989 			DMWARN("%s: __commit_transaction() failed, error = %d",
990 			       __func__, r);
991 	}
992 	pmd_write_unlock(pmd);
993 	if (!pmd->fail_io)
994 		__destroy_persistent_data_objects(pmd, true);
995 
996 	kfree(pmd);
997 	return 0;
998 }
999 
1000 /*
1001  * __open_device: Returns @td corresponding to device with id @dev,
1002  * creating it if @create is set and incrementing @td->open_count.
1003  * On failure, @td is undefined.
1004  */
1005 static int __open_device(struct dm_pool_metadata *pmd,
1006 			 dm_thin_id dev, int create,
1007 			 struct dm_thin_device **td)
1008 {
1009 	int r, changed = 0;
1010 	struct dm_thin_device *td2;
1011 	uint64_t key = dev;
1012 	struct disk_device_details details_le;
1013 
1014 	/*
1015 	 * If the device is already open, return it.
1016 	 */
1017 	list_for_each_entry(td2, &pmd->thin_devices, list)
1018 		if (td2->id == dev) {
1019 			/*
1020 			 * May not create an already-open device.
1021 			 */
1022 			if (create)
1023 				return -EEXIST;
1024 
1025 			td2->open_count++;
1026 			*td = td2;
1027 			return 0;
1028 		}
1029 
1030 	/*
1031 	 * Check the device exists.
1032 	 */
1033 	r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
1034 			    &key, &details_le);
1035 	if (r) {
1036 		if (r != -ENODATA || !create)
1037 			return r;
1038 
1039 		/*
1040 		 * Create new device.
1041 		 */
1042 		changed = 1;
1043 		details_le.mapped_blocks = 0;
1044 		details_le.transaction_id = cpu_to_le64(pmd->trans_id);
1045 		details_le.creation_time = cpu_to_le32(pmd->time);
1046 		details_le.snapshotted_time = cpu_to_le32(pmd->time);
1047 	}
1048 
1049 	*td = kmalloc(sizeof(**td), GFP_NOIO);
1050 	if (!*td)
1051 		return -ENOMEM;
1052 
1053 	(*td)->pmd = pmd;
1054 	(*td)->id = dev;
1055 	(*td)->open_count = 1;
1056 	(*td)->changed = changed;
1057 	(*td)->aborted_with_changes = false;
1058 	(*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
1059 	(*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
1060 	(*td)->creation_time = le32_to_cpu(details_le.creation_time);
1061 	(*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
1062 
1063 	list_add(&(*td)->list, &pmd->thin_devices);
1064 
1065 	return 0;
1066 }
1067 
1068 static void __close_device(struct dm_thin_device *td)
1069 {
1070 	--td->open_count;
1071 }
1072 
1073 static int __create_thin(struct dm_pool_metadata *pmd,
1074 			 dm_thin_id dev)
1075 {
1076 	int r;
1077 	dm_block_t dev_root;
1078 	uint64_t key = dev;
1079 	struct dm_thin_device *td;
1080 	__le64 value;
1081 
1082 	r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
1083 			    &key, NULL);
1084 	if (!r)
1085 		return -EEXIST;
1086 
1087 	/*
1088 	 * Create an empty btree for the mappings.
1089 	 */
1090 	r = dm_btree_empty(&pmd->bl_info, &dev_root);
1091 	if (r)
1092 		return r;
1093 
1094 	/*
1095 	 * Insert it into the main mapping tree.
1096 	 */
1097 	value = cpu_to_le64(dev_root);
1098 	__dm_bless_for_disk(&value);
1099 	r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
1100 	if (r) {
1101 		dm_btree_del(&pmd->bl_info, dev_root);
1102 		return r;
1103 	}
1104 
1105 	r = __open_device(pmd, dev, 1, &td);
1106 	if (r) {
1107 		dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1108 		dm_btree_del(&pmd->bl_info, dev_root);
1109 		return r;
1110 	}
1111 	__close_device(td);
1112 
1113 	return r;
1114 }
1115 
1116 int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
1117 {
1118 	int r = -EINVAL;
1119 
1120 	pmd_write_lock(pmd);
1121 	if (!pmd->fail_io)
1122 		r = __create_thin(pmd, dev);
1123 	pmd_write_unlock(pmd);
1124 
1125 	return r;
1126 }
1127 
1128 static int __set_snapshot_details(struct dm_pool_metadata *pmd,
1129 				  struct dm_thin_device *snap,
1130 				  dm_thin_id origin, uint32_t time)
1131 {
1132 	int r;
1133 	struct dm_thin_device *td;
1134 
1135 	r = __open_device(pmd, origin, 0, &td);
1136 	if (r)
1137 		return r;
1138 
1139 	td->changed = true;
1140 	td->snapshotted_time = time;
1141 
1142 	snap->mapped_blocks = td->mapped_blocks;
1143 	snap->snapshotted_time = time;
1144 	__close_device(td);
1145 
1146 	return 0;
1147 }
1148 
1149 static int __create_snap(struct dm_pool_metadata *pmd,
1150 			 dm_thin_id dev, dm_thin_id origin)
1151 {
1152 	int r;
1153 	dm_block_t origin_root;
1154 	uint64_t key = origin, dev_key = dev;
1155 	struct dm_thin_device *td;
1156 	__le64 value;
1157 
1158 	/* check this device is unused */
1159 	r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
1160 			    &dev_key, NULL);
1161 	if (!r)
1162 		return -EEXIST;
1163 
1164 	/* find the mapping tree for the origin */
1165 	r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
1166 	if (r)
1167 		return r;
1168 	origin_root = le64_to_cpu(value);
1169 
1170 	/* clone the origin, an inc will do */
1171 	dm_tm_inc(pmd->tm, origin_root);
1172 
1173 	/* insert into the main mapping tree */
1174 	value = cpu_to_le64(origin_root);
1175 	__dm_bless_for_disk(&value);
1176 	key = dev;
1177 	r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
1178 	if (r) {
1179 		dm_tm_dec(pmd->tm, origin_root);
1180 		return r;
1181 	}
1182 
1183 	pmd->time++;
1184 
1185 	r = __open_device(pmd, dev, 1, &td);
1186 	if (r)
1187 		goto bad;
1188 
1189 	r = __set_snapshot_details(pmd, td, origin, pmd->time);
1190 	__close_device(td);
1191 
1192 	if (r)
1193 		goto bad;
1194 
1195 	return 0;
1196 
1197 bad:
1198 	dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1199 	dm_btree_remove(&pmd->details_info, pmd->details_root,
1200 			&key, &pmd->details_root);
1201 	return r;
1202 }
1203 
1204 int dm_pool_create_snap(struct dm_pool_metadata *pmd,
1205 				 dm_thin_id dev,
1206 				 dm_thin_id origin)
1207 {
1208 	int r = -EINVAL;
1209 
1210 	pmd_write_lock(pmd);
1211 	if (!pmd->fail_io)
1212 		r = __create_snap(pmd, dev, origin);
1213 	pmd_write_unlock(pmd);
1214 
1215 	return r;
1216 }
1217 
1218 static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
1219 {
1220 	int r;
1221 	uint64_t key = dev;
1222 	struct dm_thin_device *td;
1223 
1224 	/* TODO: failure should mark the transaction invalid */
1225 	r = __open_device(pmd, dev, 0, &td);
1226 	if (r)
1227 		return r;
1228 
1229 	if (td->open_count > 1) {
1230 		__close_device(td);
1231 		return -EBUSY;
1232 	}
1233 
1234 	list_del(&td->list);
1235 	kfree(td);
1236 	r = dm_btree_remove(&pmd->details_info, pmd->details_root,
1237 			    &key, &pmd->details_root);
1238 	if (r)
1239 		return r;
1240 
1241 	r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1242 	if (r)
1243 		return r;
1244 
1245 	return 0;
1246 }
1247 
1248 int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
1249 			       dm_thin_id dev)
1250 {
1251 	int r = -EINVAL;
1252 
1253 	pmd_write_lock(pmd);
1254 	if (!pmd->fail_io)
1255 		r = __delete_device(pmd, dev);
1256 	pmd_write_unlock(pmd);
1257 
1258 	return r;
1259 }
1260 
1261 int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
1262 					uint64_t current_id,
1263 					uint64_t new_id)
1264 {
1265 	int r = -EINVAL;
1266 
1267 	pmd_write_lock(pmd);
1268 
1269 	if (pmd->fail_io)
1270 		goto out;
1271 
1272 	if (pmd->trans_id != current_id) {
1273 		DMERR("mismatched transaction id");
1274 		goto out;
1275 	}
1276 
1277 	pmd->trans_id = new_id;
1278 	r = 0;
1279 
1280 out:
1281 	pmd_write_unlock(pmd);
1282 
1283 	return r;
1284 }
1285 
1286 int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
1287 					uint64_t *result)
1288 {
1289 	int r = -EINVAL;
1290 
1291 	down_read(&pmd->root_lock);
1292 	if (!pmd->fail_io) {
1293 		*result = pmd->trans_id;
1294 		r = 0;
1295 	}
1296 	up_read(&pmd->root_lock);
1297 
1298 	return r;
1299 }
1300 
1301 static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
1302 {
1303 	int r, inc;
1304 	struct thin_disk_superblock *disk_super;
1305 	struct dm_block *copy, *sblock;
1306 	dm_block_t held_root;
1307 
1308 	/*
1309 	 * We commit to ensure the btree roots which we increment in a
1310 	 * moment are up to date.
1311 	 */
1312 	r = __commit_transaction(pmd);
1313 	if (r < 0) {
1314 		DMWARN("%s: __commit_transaction() failed, error = %d",
1315 		       __func__, r);
1316 		return r;
1317 	}
1318 
1319 	/*
1320 	 * Copy the superblock.
1321 	 */
1322 	dm_sm_inc_block(pmd->metadata_sm, THIN_SUPERBLOCK_LOCATION);
1323 	r = dm_tm_shadow_block(pmd->tm, THIN_SUPERBLOCK_LOCATION,
1324 			       &sb_validator, &copy, &inc);
1325 	if (r)
1326 		return r;
1327 
1328 	BUG_ON(!inc);
1329 
1330 	held_root = dm_block_location(copy);
1331 	disk_super = dm_block_data(copy);
1332 
1333 	if (le64_to_cpu(disk_super->held_root)) {
1334 		DMWARN("Pool metadata snapshot already exists: release this before taking another.");
1335 
1336 		dm_tm_dec(pmd->tm, held_root);
1337 		dm_tm_unlock(pmd->tm, copy);
1338 		return -EBUSY;
1339 	}
1340 
1341 	/*
1342 	 * Wipe the spacemap since we're not publishing this.
1343 	 */
1344 	memset(&disk_super->data_space_map_root, 0,
1345 	       sizeof(disk_super->data_space_map_root));
1346 	memset(&disk_super->metadata_space_map_root, 0,
1347 	       sizeof(disk_super->metadata_space_map_root));
1348 
1349 	/*
1350 	 * Increment the data structures that need to be preserved.
1351 	 */
1352 	dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->data_mapping_root));
1353 	dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->device_details_root));
1354 	dm_tm_unlock(pmd->tm, copy);
1355 
1356 	/*
1357 	 * Write the held root into the superblock.
1358 	 */
1359 	r = superblock_lock(pmd, &sblock);
1360 	if (r) {
1361 		dm_tm_dec(pmd->tm, held_root);
1362 		return r;
1363 	}
1364 
1365 	disk_super = dm_block_data(sblock);
1366 	disk_super->held_root = cpu_to_le64(held_root);
1367 	dm_bm_unlock(sblock);
1368 	return 0;
1369 }
1370 
1371 int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd)
1372 {
1373 	int r = -EINVAL;
1374 
1375 	pmd_write_lock(pmd);
1376 	if (!pmd->fail_io)
1377 		r = __reserve_metadata_snap(pmd);
1378 	pmd_write_unlock(pmd);
1379 
1380 	return r;
1381 }
1382 
1383 static int __release_metadata_snap(struct dm_pool_metadata *pmd)
1384 {
1385 	int r;
1386 	struct thin_disk_superblock *disk_super;
1387 	struct dm_block *sblock, *copy;
1388 	dm_block_t held_root;
1389 
1390 	r = superblock_lock(pmd, &sblock);
1391 	if (r)
1392 		return r;
1393 
1394 	disk_super = dm_block_data(sblock);
1395 	held_root = le64_to_cpu(disk_super->held_root);
1396 	disk_super->held_root = cpu_to_le64(0);
1397 
1398 	dm_bm_unlock(sblock);
1399 
1400 	if (!held_root) {
1401 		DMWARN("No pool metadata snapshot found: nothing to release.");
1402 		return -EINVAL;
1403 	}
1404 
1405 	r = dm_tm_read_lock(pmd->tm, held_root, &sb_validator, &copy);
1406 	if (r)
1407 		return r;
1408 
1409 	disk_super = dm_block_data(copy);
1410 	dm_btree_del(&pmd->info, le64_to_cpu(disk_super->data_mapping_root));
1411 	dm_btree_del(&pmd->details_info, le64_to_cpu(disk_super->device_details_root));
1412 	dm_sm_dec_block(pmd->metadata_sm, held_root);
1413 
1414 	dm_tm_unlock(pmd->tm, copy);
1415 
1416 	return 0;
1417 }
1418 
1419 int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd)
1420 {
1421 	int r = -EINVAL;
1422 
1423 	pmd_write_lock(pmd);
1424 	if (!pmd->fail_io)
1425 		r = __release_metadata_snap(pmd);
1426 	pmd_write_unlock(pmd);
1427 
1428 	return r;
1429 }
1430 
1431 static int __get_metadata_snap(struct dm_pool_metadata *pmd,
1432 			       dm_block_t *result)
1433 {
1434 	int r;
1435 	struct thin_disk_superblock *disk_super;
1436 	struct dm_block *sblock;
1437 
1438 	r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
1439 			    &sb_validator, &sblock);
1440 	if (r)
1441 		return r;
1442 
1443 	disk_super = dm_block_data(sblock);
1444 	*result = le64_to_cpu(disk_super->held_root);
1445 
1446 	dm_bm_unlock(sblock);
1447 
1448 	return 0;
1449 }
1450 
1451 int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
1452 			      dm_block_t *result)
1453 {
1454 	int r = -EINVAL;
1455 
1456 	down_read(&pmd->root_lock);
1457 	if (!pmd->fail_io)
1458 		r = __get_metadata_snap(pmd, result);
1459 	up_read(&pmd->root_lock);
1460 
1461 	return r;
1462 }
1463 
1464 int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
1465 			     struct dm_thin_device **td)
1466 {
1467 	int r = -EINVAL;
1468 
1469 	pmd_write_lock_in_core(pmd);
1470 	if (!pmd->fail_io)
1471 		r = __open_device(pmd, dev, 0, td);
1472 	pmd_write_unlock(pmd);
1473 
1474 	return r;
1475 }
1476 
1477 int dm_pool_close_thin_device(struct dm_thin_device *td)
1478 {
1479 	pmd_write_lock_in_core(td->pmd);
1480 	__close_device(td);
1481 	pmd_write_unlock(td->pmd);
1482 
1483 	return 0;
1484 }
1485 
1486 dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
1487 {
1488 	return td->id;
1489 }
1490 
1491 /*
1492  * Check whether @time (of block creation) is older than @td's last snapshot.
1493  * If so then the associated block is shared with the last snapshot device.
1494  * Any block on a device created *after* the device last got snapshotted is
1495  * necessarily not shared.
1496  */
1497 static bool __snapshotted_since(struct dm_thin_device *td, uint32_t time)
1498 {
1499 	return td->snapshotted_time > time;
1500 }
1501 
1502 static void unpack_lookup_result(struct dm_thin_device *td, __le64 value,
1503 				 struct dm_thin_lookup_result *result)
1504 {
1505 	uint64_t block_time = 0;
1506 	dm_block_t exception_block;
1507 	uint32_t exception_time;
1508 
1509 	block_time = le64_to_cpu(value);
1510 	unpack_block_time(block_time, &exception_block, &exception_time);
1511 	result->block = exception_block;
1512 	result->shared = __snapshotted_since(td, exception_time);
1513 }
1514 
1515 static int __find_block(struct dm_thin_device *td, dm_block_t block,
1516 			int can_issue_io, struct dm_thin_lookup_result *result)
1517 {
1518 	int r;
1519 	__le64 value;
1520 	struct dm_pool_metadata *pmd = td->pmd;
1521 	dm_block_t keys[2] = { td->id, block };
1522 	struct dm_btree_info *info;
1523 
1524 	if (can_issue_io) {
1525 		info = &pmd->info;
1526 	} else
1527 		info = &pmd->nb_info;
1528 
1529 	r = dm_btree_lookup(info, pmd->root, keys, &value);
1530 	if (!r)
1531 		unpack_lookup_result(td, value, result);
1532 
1533 	return r;
1534 }
1535 
1536 int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
1537 		       int can_issue_io, struct dm_thin_lookup_result *result)
1538 {
1539 	int r;
1540 	struct dm_pool_metadata *pmd = td->pmd;
1541 
1542 	down_read(&pmd->root_lock);
1543 	if (pmd->fail_io) {
1544 		up_read(&pmd->root_lock);
1545 		return -EINVAL;
1546 	}
1547 
1548 	r = __find_block(td, block, can_issue_io, result);
1549 
1550 	up_read(&pmd->root_lock);
1551 	return r;
1552 }
1553 
1554 static int __find_next_mapped_block(struct dm_thin_device *td, dm_block_t block,
1555 					  dm_block_t *vblock,
1556 					  struct dm_thin_lookup_result *result)
1557 {
1558 	int r;
1559 	__le64 value;
1560 	struct dm_pool_metadata *pmd = td->pmd;
1561 	dm_block_t keys[2] = { td->id, block };
1562 
1563 	r = dm_btree_lookup_next(&pmd->info, pmd->root, keys, vblock, &value);
1564 	if (!r)
1565 		unpack_lookup_result(td, value, result);
1566 
1567 	return r;
1568 }
1569 
1570 static int __find_mapped_range(struct dm_thin_device *td,
1571 			       dm_block_t begin, dm_block_t end,
1572 			       dm_block_t *thin_begin, dm_block_t *thin_end,
1573 			       dm_block_t *pool_begin, bool *maybe_shared)
1574 {
1575 	int r;
1576 	dm_block_t pool_end;
1577 	struct dm_thin_lookup_result lookup;
1578 
1579 	if (end < begin)
1580 		return -ENODATA;
1581 
1582 	r = __find_next_mapped_block(td, begin, &begin, &lookup);
1583 	if (r)
1584 		return r;
1585 
1586 	if (begin >= end)
1587 		return -ENODATA;
1588 
1589 	*thin_begin = begin;
1590 	*pool_begin = lookup.block;
1591 	*maybe_shared = lookup.shared;
1592 
1593 	begin++;
1594 	pool_end = *pool_begin + 1;
1595 	while (begin != end) {
1596 		r = __find_block(td, begin, true, &lookup);
1597 		if (r) {
1598 			if (r == -ENODATA)
1599 				break;
1600 			else
1601 				return r;
1602 		}
1603 
1604 		if ((lookup.block != pool_end) ||
1605 		    (lookup.shared != *maybe_shared))
1606 			break;
1607 
1608 		pool_end++;
1609 		begin++;
1610 	}
1611 
1612 	*thin_end = begin;
1613 	return 0;
1614 }
1615 
1616 int dm_thin_find_mapped_range(struct dm_thin_device *td,
1617 			      dm_block_t begin, dm_block_t end,
1618 			      dm_block_t *thin_begin, dm_block_t *thin_end,
1619 			      dm_block_t *pool_begin, bool *maybe_shared)
1620 {
1621 	int r = -EINVAL;
1622 	struct dm_pool_metadata *pmd = td->pmd;
1623 
1624 	down_read(&pmd->root_lock);
1625 	if (!pmd->fail_io) {
1626 		r = __find_mapped_range(td, begin, end, thin_begin, thin_end,
1627 					pool_begin, maybe_shared);
1628 	}
1629 	up_read(&pmd->root_lock);
1630 
1631 	return r;
1632 }
1633 
1634 static int __insert(struct dm_thin_device *td, dm_block_t block,
1635 		    dm_block_t data_block)
1636 {
1637 	int r, inserted;
1638 	__le64 value;
1639 	struct dm_pool_metadata *pmd = td->pmd;
1640 	dm_block_t keys[2] = { td->id, block };
1641 
1642 	value = cpu_to_le64(pack_block_time(data_block, pmd->time));
1643 	__dm_bless_for_disk(&value);
1644 
1645 	r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
1646 				   &pmd->root, &inserted);
1647 	if (r)
1648 		return r;
1649 
1650 	td->changed = true;
1651 	if (inserted)
1652 		td->mapped_blocks++;
1653 
1654 	return 0;
1655 }
1656 
1657 int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
1658 			 dm_block_t data_block)
1659 {
1660 	int r = -EINVAL;
1661 
1662 	pmd_write_lock(td->pmd);
1663 	if (!td->pmd->fail_io)
1664 		r = __insert(td, block, data_block);
1665 	pmd_write_unlock(td->pmd);
1666 
1667 	return r;
1668 }
1669 
1670 static int __remove_range(struct dm_thin_device *td, dm_block_t begin, dm_block_t end)
1671 {
1672 	int r;
1673 	unsigned count, total_count = 0;
1674 	struct dm_pool_metadata *pmd = td->pmd;
1675 	dm_block_t keys[1] = { td->id };
1676 	__le64 value;
1677 	dm_block_t mapping_root;
1678 
1679 	/*
1680 	 * Find the mapping tree
1681 	 */
1682 	r = dm_btree_lookup(&pmd->tl_info, pmd->root, keys, &value);
1683 	if (r)
1684 		return r;
1685 
1686 	/*
1687 	 * Remove from the mapping tree, taking care to inc the
1688 	 * ref count so it doesn't get deleted.
1689 	 */
1690 	mapping_root = le64_to_cpu(value);
1691 	dm_tm_inc(pmd->tm, mapping_root);
1692 	r = dm_btree_remove(&pmd->tl_info, pmd->root, keys, &pmd->root);
1693 	if (r)
1694 		return r;
1695 
1696 	/*
1697 	 * Remove leaves stops at the first unmapped entry, so we have to
1698 	 * loop round finding mapped ranges.
1699 	 */
1700 	while (begin < end) {
1701 		r = dm_btree_lookup_next(&pmd->bl_info, mapping_root, &begin, &begin, &value);
1702 		if (r == -ENODATA)
1703 			break;
1704 
1705 		if (r)
1706 			return r;
1707 
1708 		if (begin >= end)
1709 			break;
1710 
1711 		r = dm_btree_remove_leaves(&pmd->bl_info, mapping_root, &begin, end, &mapping_root, &count);
1712 		if (r)
1713 			return r;
1714 
1715 		total_count += count;
1716 	}
1717 
1718 	td->mapped_blocks -= total_count;
1719 	td->changed = true;
1720 
1721 	/*
1722 	 * Reinsert the mapping tree.
1723 	 */
1724 	value = cpu_to_le64(mapping_root);
1725 	__dm_bless_for_disk(&value);
1726 	return dm_btree_insert(&pmd->tl_info, pmd->root, keys, &value, &pmd->root);
1727 }
1728 
1729 int dm_thin_remove_range(struct dm_thin_device *td,
1730 			 dm_block_t begin, dm_block_t end)
1731 {
1732 	int r = -EINVAL;
1733 
1734 	pmd_write_lock(td->pmd);
1735 	if (!td->pmd->fail_io)
1736 		r = __remove_range(td, begin, end);
1737 	pmd_write_unlock(td->pmd);
1738 
1739 	return r;
1740 }
1741 
1742 int dm_pool_block_is_shared(struct dm_pool_metadata *pmd, dm_block_t b, bool *result)
1743 {
1744 	int r;
1745 	uint32_t ref_count;
1746 
1747 	down_read(&pmd->root_lock);
1748 	r = dm_sm_get_count(pmd->data_sm, b, &ref_count);
1749 	if (!r)
1750 		*result = (ref_count > 1);
1751 	up_read(&pmd->root_lock);
1752 
1753 	return r;
1754 }
1755 
1756 int dm_pool_inc_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e)
1757 {
1758 	int r = 0;
1759 
1760 	pmd_write_lock(pmd);
1761 	r = dm_sm_inc_blocks(pmd->data_sm, b, e);
1762 	pmd_write_unlock(pmd);
1763 
1764 	return r;
1765 }
1766 
1767 int dm_pool_dec_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e)
1768 {
1769 	int r = 0;
1770 
1771 	pmd_write_lock(pmd);
1772 	r = dm_sm_dec_blocks(pmd->data_sm, b, e);
1773 	pmd_write_unlock(pmd);
1774 
1775 	return r;
1776 }
1777 
1778 bool dm_thin_changed_this_transaction(struct dm_thin_device *td)
1779 {
1780 	int r;
1781 
1782 	down_read(&td->pmd->root_lock);
1783 	r = td->changed;
1784 	up_read(&td->pmd->root_lock);
1785 
1786 	return r;
1787 }
1788 
1789 bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd)
1790 {
1791 	bool r = false;
1792 	struct dm_thin_device *td, *tmp;
1793 
1794 	down_read(&pmd->root_lock);
1795 	list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
1796 		if (td->changed) {
1797 			r = td->changed;
1798 			break;
1799 		}
1800 	}
1801 	up_read(&pmd->root_lock);
1802 
1803 	return r;
1804 }
1805 
1806 bool dm_thin_aborted_changes(struct dm_thin_device *td)
1807 {
1808 	bool r;
1809 
1810 	down_read(&td->pmd->root_lock);
1811 	r = td->aborted_with_changes;
1812 	up_read(&td->pmd->root_lock);
1813 
1814 	return r;
1815 }
1816 
1817 int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
1818 {
1819 	int r = -EINVAL;
1820 
1821 	pmd_write_lock(pmd);
1822 	if (!pmd->fail_io)
1823 		r = dm_sm_new_block(pmd->data_sm, result);
1824 	pmd_write_unlock(pmd);
1825 
1826 	return r;
1827 }
1828 
1829 int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
1830 {
1831 	int r = -EINVAL;
1832 
1833 	/*
1834 	 * Care is taken to not have commit be what
1835 	 * triggers putting the thin-pool in-service.
1836 	 */
1837 	pmd_write_lock_in_core(pmd);
1838 	if (pmd->fail_io)
1839 		goto out;
1840 
1841 	r = __commit_transaction(pmd);
1842 	if (r < 0)
1843 		goto out;
1844 
1845 	/*
1846 	 * Open the next transaction.
1847 	 */
1848 	r = __begin_transaction(pmd);
1849 out:
1850 	pmd_write_unlock(pmd);
1851 	return r;
1852 }
1853 
1854 static void __set_abort_with_changes_flags(struct dm_pool_metadata *pmd)
1855 {
1856 	struct dm_thin_device *td;
1857 
1858 	list_for_each_entry(td, &pmd->thin_devices, list)
1859 		td->aborted_with_changes = td->changed;
1860 }
1861 
1862 int dm_pool_abort_metadata(struct dm_pool_metadata *pmd)
1863 {
1864 	int r = -EINVAL;
1865 	struct dm_block_manager *old_bm = NULL, *new_bm = NULL;
1866 
1867 	/* fail_io is double-checked with pmd->root_lock held below */
1868 	if (unlikely(pmd->fail_io))
1869 		return r;
1870 
1871 	/*
1872 	 * Replacement block manager (new_bm) is created and old_bm destroyed outside of
1873 	 * pmd root_lock to avoid ABBA deadlock that would result (due to life-cycle of
1874 	 * shrinker associated with the block manager's bufio client vs pmd root_lock).
1875 	 * - must take shrinker_rwsem without holding pmd->root_lock
1876 	 */
1877 	new_bm = dm_block_manager_create(pmd->bdev, THIN_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
1878 					 THIN_MAX_CONCURRENT_LOCKS);
1879 
1880 	pmd_write_lock(pmd);
1881 	if (pmd->fail_io) {
1882 		pmd_write_unlock(pmd);
1883 		goto out;
1884 	}
1885 
1886 	__set_abort_with_changes_flags(pmd);
1887 	__destroy_persistent_data_objects(pmd, false);
1888 	old_bm = pmd->bm;
1889 	if (IS_ERR(new_bm)) {
1890 		DMERR("could not create block manager during abort");
1891 		pmd->bm = NULL;
1892 		r = PTR_ERR(new_bm);
1893 		goto out_unlock;
1894 	}
1895 
1896 	pmd->bm = new_bm;
1897 	r = __open_or_format_metadata(pmd, false);
1898 	if (r) {
1899 		pmd->bm = NULL;
1900 		goto out_unlock;
1901 	}
1902 	new_bm = NULL;
1903 out_unlock:
1904 	if (r)
1905 		pmd->fail_io = true;
1906 	pmd_write_unlock(pmd);
1907 	dm_block_manager_destroy(old_bm);
1908 out:
1909 	if (new_bm && !IS_ERR(new_bm))
1910 		dm_block_manager_destroy(new_bm);
1911 
1912 	return r;
1913 }
1914 
1915 int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
1916 {
1917 	int r = -EINVAL;
1918 
1919 	down_read(&pmd->root_lock);
1920 	if (!pmd->fail_io)
1921 		r = dm_sm_get_nr_free(pmd->data_sm, result);
1922 	up_read(&pmd->root_lock);
1923 
1924 	return r;
1925 }
1926 
1927 int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
1928 					  dm_block_t *result)
1929 {
1930 	int r = -EINVAL;
1931 
1932 	down_read(&pmd->root_lock);
1933 	if (!pmd->fail_io)
1934 		r = dm_sm_get_nr_free(pmd->metadata_sm, result);
1935 
1936 	if (!r) {
1937 		if (*result < pmd->metadata_reserve)
1938 			*result = 0;
1939 		else
1940 			*result -= pmd->metadata_reserve;
1941 	}
1942 	up_read(&pmd->root_lock);
1943 
1944 	return r;
1945 }
1946 
1947 int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
1948 				  dm_block_t *result)
1949 {
1950 	int r = -EINVAL;
1951 
1952 	down_read(&pmd->root_lock);
1953 	if (!pmd->fail_io)
1954 		r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
1955 	up_read(&pmd->root_lock);
1956 
1957 	return r;
1958 }
1959 
1960 int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
1961 {
1962 	int r = -EINVAL;
1963 
1964 	down_read(&pmd->root_lock);
1965 	if (!pmd->fail_io)
1966 		r = dm_sm_get_nr_blocks(pmd->data_sm, result);
1967 	up_read(&pmd->root_lock);
1968 
1969 	return r;
1970 }
1971 
1972 int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
1973 {
1974 	int r = -EINVAL;
1975 	struct dm_pool_metadata *pmd = td->pmd;
1976 
1977 	down_read(&pmd->root_lock);
1978 	if (!pmd->fail_io) {
1979 		*result = td->mapped_blocks;
1980 		r = 0;
1981 	}
1982 	up_read(&pmd->root_lock);
1983 
1984 	return r;
1985 }
1986 
1987 static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
1988 {
1989 	int r;
1990 	__le64 value_le;
1991 	dm_block_t thin_root;
1992 	struct dm_pool_metadata *pmd = td->pmd;
1993 
1994 	r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
1995 	if (r)
1996 		return r;
1997 
1998 	thin_root = le64_to_cpu(value_le);
1999 
2000 	return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
2001 }
2002 
2003 int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
2004 				     dm_block_t *result)
2005 {
2006 	int r = -EINVAL;
2007 	struct dm_pool_metadata *pmd = td->pmd;
2008 
2009 	down_read(&pmd->root_lock);
2010 	if (!pmd->fail_io)
2011 		r = __highest_block(td, result);
2012 	up_read(&pmd->root_lock);
2013 
2014 	return r;
2015 }
2016 
2017 static int __resize_space_map(struct dm_space_map *sm, dm_block_t new_count)
2018 {
2019 	int r;
2020 	dm_block_t old_count;
2021 
2022 	r = dm_sm_get_nr_blocks(sm, &old_count);
2023 	if (r)
2024 		return r;
2025 
2026 	if (new_count == old_count)
2027 		return 0;
2028 
2029 	if (new_count < old_count) {
2030 		DMERR("cannot reduce size of space map");
2031 		return -EINVAL;
2032 	}
2033 
2034 	return dm_sm_extend(sm, new_count - old_count);
2035 }
2036 
2037 int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
2038 {
2039 	int r = -EINVAL;
2040 
2041 	pmd_write_lock(pmd);
2042 	if (!pmd->fail_io)
2043 		r = __resize_space_map(pmd->data_sm, new_count);
2044 	pmd_write_unlock(pmd);
2045 
2046 	return r;
2047 }
2048 
2049 int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
2050 {
2051 	int r = -EINVAL;
2052 
2053 	pmd_write_lock(pmd);
2054 	if (!pmd->fail_io) {
2055 		r = __resize_space_map(pmd->metadata_sm, new_count);
2056 		if (!r)
2057 			__set_metadata_reserve(pmd);
2058 	}
2059 	pmd_write_unlock(pmd);
2060 
2061 	return r;
2062 }
2063 
2064 void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd)
2065 {
2066 	pmd_write_lock_in_core(pmd);
2067 	dm_bm_set_read_only(pmd->bm);
2068 	pmd_write_unlock(pmd);
2069 }
2070 
2071 void dm_pool_metadata_read_write(struct dm_pool_metadata *pmd)
2072 {
2073 	pmd_write_lock_in_core(pmd);
2074 	dm_bm_set_read_write(pmd->bm);
2075 	pmd_write_unlock(pmd);
2076 }
2077 
2078 int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd,
2079 					dm_block_t threshold,
2080 					dm_sm_threshold_fn fn,
2081 					void *context)
2082 {
2083 	int r = -EINVAL;
2084 
2085 	pmd_write_lock_in_core(pmd);
2086 	if (!pmd->fail_io) {
2087 		r = dm_sm_register_threshold_callback(pmd->metadata_sm,
2088 						      threshold, fn, context);
2089 	}
2090 	pmd_write_unlock(pmd);
2091 
2092 	return r;
2093 }
2094 
2095 void dm_pool_register_pre_commit_callback(struct dm_pool_metadata *pmd,
2096 					  dm_pool_pre_commit_fn fn,
2097 					  void *context)
2098 {
2099 	pmd_write_lock_in_core(pmd);
2100 	pmd->pre_commit_fn = fn;
2101 	pmd->pre_commit_context = context;
2102 	pmd_write_unlock(pmd);
2103 }
2104 
2105 int dm_pool_metadata_set_needs_check(struct dm_pool_metadata *pmd)
2106 {
2107 	int r = -EINVAL;
2108 	struct dm_block *sblock;
2109 	struct thin_disk_superblock *disk_super;
2110 
2111 	pmd_write_lock(pmd);
2112 	if (pmd->fail_io)
2113 		goto out;
2114 
2115 	pmd->flags |= THIN_METADATA_NEEDS_CHECK_FLAG;
2116 
2117 	r = superblock_lock(pmd, &sblock);
2118 	if (r) {
2119 		DMERR("couldn't lock superblock");
2120 		goto out;
2121 	}
2122 
2123 	disk_super = dm_block_data(sblock);
2124 	disk_super->flags = cpu_to_le32(pmd->flags);
2125 
2126 	dm_bm_unlock(sblock);
2127 out:
2128 	pmd_write_unlock(pmd);
2129 	return r;
2130 }
2131 
2132 bool dm_pool_metadata_needs_check(struct dm_pool_metadata *pmd)
2133 {
2134 	bool needs_check;
2135 
2136 	down_read(&pmd->root_lock);
2137 	needs_check = pmd->flags & THIN_METADATA_NEEDS_CHECK_FLAG;
2138 	up_read(&pmd->root_lock);
2139 
2140 	return needs_check;
2141 }
2142 
2143 void dm_pool_issue_prefetches(struct dm_pool_metadata *pmd)
2144 {
2145 	down_read(&pmd->root_lock);
2146 	if (!pmd->fail_io)
2147 		dm_tm_issue_prefetches(pmd->tm);
2148 	up_read(&pmd->root_lock);
2149 }
2150