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