2 * Copyright (C) 2011-2012 Red Hat, Inc.
4 * This file is released under the GPL.
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"
13 #include <linux/list.h>
14 #include <linux/device-mapper.h>
15 #include <linux/workqueue.h>
17 /*--------------------------------------------------------------------------
18 * As far as the metadata goes, there is:
20 * - A superblock in block zero, taking up fewer than 512 bytes for
23 * - A space map managing the metadata blocks.
25 * - A space map managing the data blocks.
27 * - A btree mapping our internal thin dev ids onto struct disk_device_details.
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 48
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
42 * Space maps have 2 btrees:
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
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:
54 * 3 - ref count is higher than 2
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
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.
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.
71 * All metadata io is in THIN_METADATA_BLOCK_SIZE sized/aligned chunks
72 * from the block manager.
73 *--------------------------------------------------------------------------*/
75 #define DM_MSG_PREFIX "thin metadata"
77 #define THIN_SUPERBLOCK_MAGIC 27022010
78 #define THIN_SUPERBLOCK_LOCATION 0
79 #define THIN_VERSION 2
80 #define SECTOR_TO_BLOCK_SHIFT 3
84 * 3 for btree insert +
85 * 2 for btree lookup used within space map
87 * 2 for shadow spine +
88 * 4 for rebalance 3 child node
90 #define THIN_MAX_CONCURRENT_LOCKS 6
92 /* This should be plenty */
93 #define SPACE_MAP_ROOT_SIZE 128
96 * Little endian on-disk superblock and device details.
98 struct thin_disk_superblock {
99 __le32 csum; /* Checksum of superblock except for this field. */
101 __le64 blocknr; /* This block number, dm_block_t. */
111 * Root held by userspace transactions.
115 __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
116 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
119 * 2-level btree mapping (dev_id, (dev block, time)) -> data block
121 __le64 data_mapping_root;
124 * Device detail root mapping dev_id -> device_details
126 __le64 device_details_root;
128 __le32 data_block_size; /* In 512-byte sectors. */
130 __le32 metadata_block_size; /* In 512-byte sectors. */
131 __le64 metadata_nr_blocks;
134 __le32 compat_ro_flags;
135 __le32 incompat_flags;
138 struct disk_device_details {
139 __le64 mapped_blocks;
140 __le64 transaction_id; /* When created. */
141 __le32 creation_time;
142 __le32 snapshotted_time;
145 struct dm_pool_metadata {
146 struct hlist_node hash;
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;
157 * First level holds thin_dev_t.
158 * Second level holds mappings.
160 struct dm_btree_info info;
163 * Non-blocking version of the above.
165 struct dm_btree_info nb_info;
168 * Just the top level for deleting whole devices.
170 struct dm_btree_info tl_info;
173 * Just the bottom level for creating new devices.
175 struct dm_btree_info bl_info;
178 * Describes the device details btree.
180 struct dm_btree_info details_info;
182 struct rw_semaphore root_lock;
185 dm_block_t details_root;
186 struct list_head thin_devices;
189 sector_t data_block_size;
192 * Pre-commit callback.
194 * This allows the thin provisioning target to run a callback before
195 * the metadata are committed.
197 dm_pool_pre_commit_fn pre_commit_fn;
198 void *pre_commit_context;
201 * We reserve a section of the metadata for commit overhead.
202 * All reported space does *not* include this.
204 dm_block_t metadata_reserve;
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.
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).
221 * Reading the space map roots can fail, so we read it into these
222 * buffers before the superblock is locked and updated.
224 __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
225 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
228 struct dm_thin_device {
229 struct list_head list;
230 struct dm_pool_metadata *pmd;
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;
242 /*----------------------------------------------------------------
243 * superblock validator
244 *--------------------------------------------------------------*/
246 #define SUPERBLOCK_CSUM_XOR 160774
248 static void sb_prepare_for_write(struct dm_block_validator *v,
252 struct thin_disk_superblock *disk_super = dm_block_data(b);
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));
260 static int sb_check(struct dm_block_validator *v,
264 struct thin_disk_superblock *disk_super = dm_block_data(b);
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));
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);
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));
293 static struct dm_block_validator sb_validator = {
294 .name = "superblock",
295 .prepare_for_write = sb_prepare_for_write,
299 /*----------------------------------------------------------------
300 * Methods for the btree value types
301 *--------------------------------------------------------------*/
303 static uint64_t pack_block_time(dm_block_t b, uint32_t t)
305 return (b << 24) | t;
308 static void unpack_block_time(uint64_t v, dm_block_t *b, uint32_t *t)
311 *t = v & ((1 << 24) - 1);
314 static void data_block_inc(void *context, const void *value_le)
316 struct dm_space_map *sm = context;
321 memcpy(&v_le, value_le, sizeof(v_le));
322 unpack_block_time(le64_to_cpu(v_le), &b, &t);
323 dm_sm_inc_block(sm, b);
326 static void data_block_dec(void *context, const void *value_le)
328 struct dm_space_map *sm = context;
333 memcpy(&v_le, value_le, sizeof(v_le));
334 unpack_block_time(le64_to_cpu(v_le), &b, &t);
335 dm_sm_dec_block(sm, b);
338 static int data_block_equal(void *context, const void *value1_le, const void *value2_le)
344 memcpy(&v1_le, value1_le, sizeof(v1_le));
345 memcpy(&v2_le, value2_le, sizeof(v2_le));
346 unpack_block_time(le64_to_cpu(v1_le), &b1, &t);
347 unpack_block_time(le64_to_cpu(v2_le), &b2, &t);
352 static void subtree_inc(void *context, const void *value)
354 struct dm_btree_info *info = context;
358 memcpy(&root_le, value, sizeof(root_le));
359 root = le64_to_cpu(root_le);
360 dm_tm_inc(info->tm, root);
363 static void subtree_dec(void *context, const void *value)
365 struct dm_btree_info *info = context;
369 memcpy(&root_le, value, sizeof(root_le));
370 root = le64_to_cpu(root_le);
371 if (dm_btree_del(info, root))
372 DMERR("btree delete failed");
375 static int subtree_equal(void *context, const void *value1_le, const void *value2_le)
378 memcpy(&v1_le, value1_le, sizeof(v1_le));
379 memcpy(&v2_le, value2_le, sizeof(v2_le));
381 return v1_le == v2_le;
384 /*----------------------------------------------------------------*/
387 * Variant that is used for in-core only changes or code that
388 * shouldn't put the pool in service on its own (e.g. commit).
390 static inline void __pmd_write_lock(struct dm_pool_metadata *pmd)
391 __acquires(pmd->root_lock)
393 down_write(&pmd->root_lock);
395 #define pmd_write_lock_in_core(pmd) __pmd_write_lock((pmd))
397 static inline void pmd_write_lock(struct dm_pool_metadata *pmd)
399 __pmd_write_lock(pmd);
400 if (unlikely(!pmd->in_service))
401 pmd->in_service = true;
404 static inline void pmd_write_unlock(struct dm_pool_metadata *pmd)
405 __releases(pmd->root_lock)
407 up_write(&pmd->root_lock);
410 /*----------------------------------------------------------------*/
412 static int superblock_lock_zero(struct dm_pool_metadata *pmd,
413 struct dm_block **sblock)
415 return dm_bm_write_lock_zero(pmd->bm, THIN_SUPERBLOCK_LOCATION,
416 &sb_validator, sblock);
419 static int superblock_lock(struct dm_pool_metadata *pmd,
420 struct dm_block **sblock)
422 return dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
423 &sb_validator, sblock);
426 static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result)
431 __le64 *data_le, zero = cpu_to_le64(0);
432 unsigned block_size = dm_bm_block_size(bm) / sizeof(__le64);
435 * We can't use a validator here - it may be all zeroes.
437 r = dm_bm_read_lock(bm, THIN_SUPERBLOCK_LOCATION, NULL, &b);
441 data_le = dm_block_data(b);
443 for (i = 0; i < block_size; i++) {
444 if (data_le[i] != zero) {
455 static void __setup_btree_details(struct dm_pool_metadata *pmd)
457 pmd->info.tm = pmd->tm;
458 pmd->info.levels = 2;
459 pmd->info.value_type.context = pmd->data_sm;
460 pmd->info.value_type.size = sizeof(__le64);
461 pmd->info.value_type.inc = data_block_inc;
462 pmd->info.value_type.dec = data_block_dec;
463 pmd->info.value_type.equal = data_block_equal;
465 memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info));
466 pmd->nb_info.tm = pmd->nb_tm;
468 pmd->tl_info.tm = pmd->tm;
469 pmd->tl_info.levels = 1;
470 pmd->tl_info.value_type.context = &pmd->bl_info;
471 pmd->tl_info.value_type.size = sizeof(__le64);
472 pmd->tl_info.value_type.inc = subtree_inc;
473 pmd->tl_info.value_type.dec = subtree_dec;
474 pmd->tl_info.value_type.equal = subtree_equal;
476 pmd->bl_info.tm = pmd->tm;
477 pmd->bl_info.levels = 1;
478 pmd->bl_info.value_type.context = pmd->data_sm;
479 pmd->bl_info.value_type.size = sizeof(__le64);
480 pmd->bl_info.value_type.inc = data_block_inc;
481 pmd->bl_info.value_type.dec = data_block_dec;
482 pmd->bl_info.value_type.equal = data_block_equal;
484 pmd->details_info.tm = pmd->tm;
485 pmd->details_info.levels = 1;
486 pmd->details_info.value_type.context = NULL;
487 pmd->details_info.value_type.size = sizeof(struct disk_device_details);
488 pmd->details_info.value_type.inc = NULL;
489 pmd->details_info.value_type.dec = NULL;
490 pmd->details_info.value_type.equal = NULL;
493 static int save_sm_roots(struct dm_pool_metadata *pmd)
498 r = dm_sm_root_size(pmd->metadata_sm, &len);
502 r = dm_sm_copy_root(pmd->metadata_sm, &pmd->metadata_space_map_root, len);
506 r = dm_sm_root_size(pmd->data_sm, &len);
510 return dm_sm_copy_root(pmd->data_sm, &pmd->data_space_map_root, len);
513 static void copy_sm_roots(struct dm_pool_metadata *pmd,
514 struct thin_disk_superblock *disk)
516 memcpy(&disk->metadata_space_map_root,
517 &pmd->metadata_space_map_root,
518 sizeof(pmd->metadata_space_map_root));
520 memcpy(&disk->data_space_map_root,
521 &pmd->data_space_map_root,
522 sizeof(pmd->data_space_map_root));
525 static int __write_initial_superblock(struct dm_pool_metadata *pmd)
528 struct dm_block *sblock;
529 struct thin_disk_superblock *disk_super;
530 sector_t bdev_size = i_size_read(pmd->bdev->bd_inode) >> SECTOR_SHIFT;
532 if (bdev_size > THIN_METADATA_MAX_SECTORS)
533 bdev_size = THIN_METADATA_MAX_SECTORS;
535 r = dm_sm_commit(pmd->data_sm);
539 r = dm_tm_pre_commit(pmd->tm);
543 r = save_sm_roots(pmd);
547 r = superblock_lock_zero(pmd, &sblock);
551 disk_super = dm_block_data(sblock);
552 disk_super->flags = 0;
553 memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
554 disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
555 disk_super->version = cpu_to_le32(THIN_VERSION);
556 disk_super->time = 0;
557 disk_super->trans_id = 0;
558 disk_super->held_root = 0;
560 copy_sm_roots(pmd, disk_super);
562 disk_super->data_mapping_root = cpu_to_le64(pmd->root);
563 disk_super->device_details_root = cpu_to_le64(pmd->details_root);
564 disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE);
565 disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
566 disk_super->data_block_size = cpu_to_le32(pmd->data_block_size);
568 return dm_tm_commit(pmd->tm, sblock);
571 static int __format_metadata(struct dm_pool_metadata *pmd)
575 r = dm_tm_create_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
576 &pmd->tm, &pmd->metadata_sm);
578 DMERR("tm_create_with_sm failed");
582 pmd->data_sm = dm_sm_disk_create(pmd->tm, 0);
583 if (IS_ERR(pmd->data_sm)) {
584 DMERR("sm_disk_create failed");
585 r = PTR_ERR(pmd->data_sm);
589 pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
591 DMERR("could not create non-blocking clone tm");
593 goto bad_cleanup_data_sm;
596 __setup_btree_details(pmd);
598 r = dm_btree_empty(&pmd->info, &pmd->root);
600 goto bad_cleanup_nb_tm;
602 r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
604 DMERR("couldn't create devices root");
605 goto bad_cleanup_nb_tm;
608 r = __write_initial_superblock(pmd);
610 goto bad_cleanup_nb_tm;
615 dm_tm_destroy(pmd->nb_tm);
617 dm_sm_destroy(pmd->data_sm);
619 dm_tm_destroy(pmd->tm);
620 dm_sm_destroy(pmd->metadata_sm);
625 static int __check_incompat_features(struct thin_disk_superblock *disk_super,
626 struct dm_pool_metadata *pmd)
630 features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP;
632 DMERR("could not access metadata due to unsupported optional features (%lx).",
633 (unsigned long)features);
638 * Check for read-only metadata to skip the following RDWR checks.
640 if (get_disk_ro(pmd->bdev->bd_disk))
643 features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
645 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
646 (unsigned long)features);
653 static int __open_metadata(struct dm_pool_metadata *pmd)
656 struct dm_block *sblock;
657 struct thin_disk_superblock *disk_super;
659 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
660 &sb_validator, &sblock);
662 DMERR("couldn't read superblock");
666 disk_super = dm_block_data(sblock);
668 /* Verify the data block size hasn't changed */
669 if (le32_to_cpu(disk_super->data_block_size) != pmd->data_block_size) {
670 DMERR("changing the data block size (from %u to %llu) is not supported",
671 le32_to_cpu(disk_super->data_block_size),
672 (unsigned long long)pmd->data_block_size);
674 goto bad_unlock_sblock;
677 r = __check_incompat_features(disk_super, pmd);
679 goto bad_unlock_sblock;
681 r = dm_tm_open_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
682 disk_super->metadata_space_map_root,
683 sizeof(disk_super->metadata_space_map_root),
684 &pmd->tm, &pmd->metadata_sm);
686 DMERR("tm_open_with_sm failed");
687 goto bad_unlock_sblock;
690 pmd->data_sm = dm_sm_disk_open(pmd->tm, disk_super->data_space_map_root,
691 sizeof(disk_super->data_space_map_root));
692 if (IS_ERR(pmd->data_sm)) {
693 DMERR("sm_disk_open failed");
694 r = PTR_ERR(pmd->data_sm);
698 pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
700 DMERR("could not create non-blocking clone tm");
702 goto bad_cleanup_data_sm;
705 __setup_btree_details(pmd);
706 dm_bm_unlock(sblock);
711 dm_sm_destroy(pmd->data_sm);
713 dm_tm_destroy(pmd->tm);
714 dm_sm_destroy(pmd->metadata_sm);
716 dm_bm_unlock(sblock);
721 static int __open_or_format_metadata(struct dm_pool_metadata *pmd, bool format_device)
725 r = __superblock_all_zeroes(pmd->bm, &unformatted);
730 return format_device ? __format_metadata(pmd) : -EPERM;
732 return __open_metadata(pmd);
735 static int __create_persistent_data_objects(struct dm_pool_metadata *pmd, bool format_device)
739 pmd->bm = dm_block_manager_create(pmd->bdev, THIN_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
740 THIN_MAX_CONCURRENT_LOCKS);
741 if (IS_ERR(pmd->bm)) {
742 DMERR("could not create block manager");
743 return PTR_ERR(pmd->bm);
746 r = __open_or_format_metadata(pmd, format_device);
748 dm_block_manager_destroy(pmd->bm);
753 static void __destroy_persistent_data_objects(struct dm_pool_metadata *pmd)
755 dm_sm_destroy(pmd->data_sm);
756 dm_sm_destroy(pmd->metadata_sm);
757 dm_tm_destroy(pmd->nb_tm);
758 dm_tm_destroy(pmd->tm);
759 dm_block_manager_destroy(pmd->bm);
762 static int __begin_transaction(struct dm_pool_metadata *pmd)
765 struct thin_disk_superblock *disk_super;
766 struct dm_block *sblock;
769 * We re-read the superblock every time. Shouldn't need to do this
772 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
773 &sb_validator, &sblock);
777 disk_super = dm_block_data(sblock);
778 pmd->time = le32_to_cpu(disk_super->time);
779 pmd->root = le64_to_cpu(disk_super->data_mapping_root);
780 pmd->details_root = le64_to_cpu(disk_super->device_details_root);
781 pmd->trans_id = le64_to_cpu(disk_super->trans_id);
782 pmd->flags = le32_to_cpu(disk_super->flags);
783 pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
785 dm_bm_unlock(sblock);
789 static int __write_changed_details(struct dm_pool_metadata *pmd)
792 struct dm_thin_device *td, *tmp;
793 struct disk_device_details details;
796 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
802 details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
803 details.transaction_id = cpu_to_le64(td->transaction_id);
804 details.creation_time = cpu_to_le32(td->creation_time);
805 details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
806 __dm_bless_for_disk(&details);
808 r = dm_btree_insert(&pmd->details_info, pmd->details_root,
809 &key, &details, &pmd->details_root);
824 static int __commit_transaction(struct dm_pool_metadata *pmd)
827 struct thin_disk_superblock *disk_super;
828 struct dm_block *sblock;
831 * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
833 BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
835 if (unlikely(!pmd->in_service))
838 if (pmd->pre_commit_fn) {
839 r = pmd->pre_commit_fn(pmd->pre_commit_context);
841 DMERR("pre-commit callback failed");
846 r = __write_changed_details(pmd);
850 r = dm_sm_commit(pmd->data_sm);
854 r = dm_tm_pre_commit(pmd->tm);
858 r = save_sm_roots(pmd);
862 r = superblock_lock(pmd, &sblock);
866 disk_super = dm_block_data(sblock);
867 disk_super->time = cpu_to_le32(pmd->time);
868 disk_super->data_mapping_root = cpu_to_le64(pmd->root);
869 disk_super->device_details_root = cpu_to_le64(pmd->details_root);
870 disk_super->trans_id = cpu_to_le64(pmd->trans_id);
871 disk_super->flags = cpu_to_le32(pmd->flags);
873 copy_sm_roots(pmd, disk_super);
875 return dm_tm_commit(pmd->tm, sblock);
878 static void __set_metadata_reserve(struct dm_pool_metadata *pmd)
882 dm_block_t max_blocks = 4096; /* 16M */
884 r = dm_sm_get_nr_blocks(pmd->metadata_sm, &total);
886 DMERR("could not get size of metadata device");
887 pmd->metadata_reserve = max_blocks;
889 pmd->metadata_reserve = min(max_blocks, div_u64(total, 10));
892 struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
893 sector_t data_block_size,
897 struct dm_pool_metadata *pmd;
899 pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
901 DMERR("could not allocate metadata struct");
902 return ERR_PTR(-ENOMEM);
905 init_rwsem(&pmd->root_lock);
907 INIT_LIST_HEAD(&pmd->thin_devices);
908 pmd->fail_io = false;
909 pmd->in_service = false;
911 pmd->data_block_size = data_block_size;
912 pmd->pre_commit_fn = NULL;
913 pmd->pre_commit_context = NULL;
915 r = __create_persistent_data_objects(pmd, format_device);
921 r = __begin_transaction(pmd);
923 if (dm_pool_metadata_close(pmd) < 0)
924 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
928 __set_metadata_reserve(pmd);
933 int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
936 unsigned open_devices = 0;
937 struct dm_thin_device *td, *tmp;
939 down_read(&pmd->root_lock);
940 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
948 up_read(&pmd->root_lock);
951 DMERR("attempt to close pmd when %u device(s) are still open",
956 if (!dm_bm_is_read_only(pmd->bm) && !pmd->fail_io) {
957 r = __commit_transaction(pmd);
959 DMWARN("%s: __commit_transaction() failed, error = %d",
963 __destroy_persistent_data_objects(pmd);
970 * __open_device: Returns @td corresponding to device with id @dev,
971 * creating it if @create is set and incrementing @td->open_count.
972 * On failure, @td is undefined.
974 static int __open_device(struct dm_pool_metadata *pmd,
975 dm_thin_id dev, int create,
976 struct dm_thin_device **td)
979 struct dm_thin_device *td2;
981 struct disk_device_details details_le;
984 * If the device is already open, return it.
986 list_for_each_entry(td2, &pmd->thin_devices, list)
987 if (td2->id == dev) {
989 * May not create an already-open device.
1000 * Check the device exists.
1002 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
1005 if (r != -ENODATA || !create)
1009 * Create new device.
1012 details_le.mapped_blocks = 0;
1013 details_le.transaction_id = cpu_to_le64(pmd->trans_id);
1014 details_le.creation_time = cpu_to_le32(pmd->time);
1015 details_le.snapshotted_time = cpu_to_le32(pmd->time);
1018 *td = kmalloc(sizeof(**td), GFP_NOIO);
1024 (*td)->open_count = 1;
1025 (*td)->changed = changed;
1026 (*td)->aborted_with_changes = false;
1027 (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
1028 (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
1029 (*td)->creation_time = le32_to_cpu(details_le.creation_time);
1030 (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
1032 list_add(&(*td)->list, &pmd->thin_devices);
1037 static void __close_device(struct dm_thin_device *td)
1042 static int __create_thin(struct dm_pool_metadata *pmd,
1046 dm_block_t dev_root;
1048 struct disk_device_details details_le;
1049 struct dm_thin_device *td;
1052 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
1058 * Create an empty btree for the mappings.
1060 r = dm_btree_empty(&pmd->bl_info, &dev_root);
1065 * Insert it into the main mapping tree.
1067 value = cpu_to_le64(dev_root);
1068 __dm_bless_for_disk(&value);
1069 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
1071 dm_btree_del(&pmd->bl_info, dev_root);
1075 r = __open_device(pmd, dev, 1, &td);
1077 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1078 dm_btree_del(&pmd->bl_info, dev_root);
1086 int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
1090 pmd_write_lock(pmd);
1092 r = __create_thin(pmd, dev);
1093 pmd_write_unlock(pmd);
1098 static int __set_snapshot_details(struct dm_pool_metadata *pmd,
1099 struct dm_thin_device *snap,
1100 dm_thin_id origin, uint32_t time)
1103 struct dm_thin_device *td;
1105 r = __open_device(pmd, origin, 0, &td);
1110 td->snapshotted_time = time;
1112 snap->mapped_blocks = td->mapped_blocks;
1113 snap->snapshotted_time = time;
1119 static int __create_snap(struct dm_pool_metadata *pmd,
1120 dm_thin_id dev, dm_thin_id origin)
1123 dm_block_t origin_root;
1124 uint64_t key = origin, dev_key = dev;
1125 struct dm_thin_device *td;
1126 struct disk_device_details details_le;
1129 /* check this device is unused */
1130 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
1131 &dev_key, &details_le);
1135 /* find the mapping tree for the origin */
1136 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
1139 origin_root = le64_to_cpu(value);
1141 /* clone the origin, an inc will do */
1142 dm_tm_inc(pmd->tm, origin_root);
1144 /* insert into the main mapping tree */
1145 value = cpu_to_le64(origin_root);
1146 __dm_bless_for_disk(&value);
1148 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
1150 dm_tm_dec(pmd->tm, origin_root);
1156 r = __open_device(pmd, dev, 1, &td);
1160 r = __set_snapshot_details(pmd, td, origin, pmd->time);
1169 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1170 dm_btree_remove(&pmd->details_info, pmd->details_root,
1171 &key, &pmd->details_root);
1175 int dm_pool_create_snap(struct dm_pool_metadata *pmd,
1181 pmd_write_lock(pmd);
1183 r = __create_snap(pmd, dev, origin);
1184 pmd_write_unlock(pmd);
1189 static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
1193 struct dm_thin_device *td;
1195 /* TODO: failure should mark the transaction invalid */
1196 r = __open_device(pmd, dev, 0, &td);
1200 if (td->open_count > 1) {
1205 list_del(&td->list);
1207 r = dm_btree_remove(&pmd->details_info, pmd->details_root,
1208 &key, &pmd->details_root);
1212 r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1219 int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
1224 pmd_write_lock(pmd);
1226 r = __delete_device(pmd, dev);
1227 pmd_write_unlock(pmd);
1232 int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
1233 uint64_t current_id,
1238 pmd_write_lock(pmd);
1243 if (pmd->trans_id != current_id) {
1244 DMERR("mismatched transaction id");
1248 pmd->trans_id = new_id;
1252 pmd_write_unlock(pmd);
1257 int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
1262 down_read(&pmd->root_lock);
1263 if (!pmd->fail_io) {
1264 *result = pmd->trans_id;
1267 up_read(&pmd->root_lock);
1272 static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
1275 struct thin_disk_superblock *disk_super;
1276 struct dm_block *copy, *sblock;
1277 dm_block_t held_root;
1280 * We commit to ensure the btree roots which we increment in a
1281 * moment are up to date.
1283 r = __commit_transaction(pmd);
1285 DMWARN("%s: __commit_transaction() failed, error = %d",
1291 * Copy the superblock.
1293 dm_sm_inc_block(pmd->metadata_sm, THIN_SUPERBLOCK_LOCATION);
1294 r = dm_tm_shadow_block(pmd->tm, THIN_SUPERBLOCK_LOCATION,
1295 &sb_validator, ©, &inc);
1301 held_root = dm_block_location(copy);
1302 disk_super = dm_block_data(copy);
1304 if (le64_to_cpu(disk_super->held_root)) {
1305 DMWARN("Pool metadata snapshot already exists: release this before taking another.");
1307 dm_tm_dec(pmd->tm, held_root);
1308 dm_tm_unlock(pmd->tm, copy);
1313 * Wipe the spacemap since we're not publishing this.
1315 memset(&disk_super->data_space_map_root, 0,
1316 sizeof(disk_super->data_space_map_root));
1317 memset(&disk_super->metadata_space_map_root, 0,
1318 sizeof(disk_super->metadata_space_map_root));
1321 * Increment the data structures that need to be preserved.
1323 dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->data_mapping_root));
1324 dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->device_details_root));
1325 dm_tm_unlock(pmd->tm, copy);
1328 * Write the held root into the superblock.
1330 r = superblock_lock(pmd, &sblock);
1332 dm_tm_dec(pmd->tm, held_root);
1336 disk_super = dm_block_data(sblock);
1337 disk_super->held_root = cpu_to_le64(held_root);
1338 dm_bm_unlock(sblock);
1342 int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd)
1346 pmd_write_lock(pmd);
1348 r = __reserve_metadata_snap(pmd);
1349 pmd_write_unlock(pmd);
1354 static int __release_metadata_snap(struct dm_pool_metadata *pmd)
1357 struct thin_disk_superblock *disk_super;
1358 struct dm_block *sblock, *copy;
1359 dm_block_t held_root;
1361 r = superblock_lock(pmd, &sblock);
1365 disk_super = dm_block_data(sblock);
1366 held_root = le64_to_cpu(disk_super->held_root);
1367 disk_super->held_root = cpu_to_le64(0);
1369 dm_bm_unlock(sblock);
1372 DMWARN("No pool metadata snapshot found: nothing to release.");
1376 r = dm_tm_read_lock(pmd->tm, held_root, &sb_validator, ©);
1380 disk_super = dm_block_data(copy);
1381 dm_btree_del(&pmd->info, le64_to_cpu(disk_super->data_mapping_root));
1382 dm_btree_del(&pmd->details_info, le64_to_cpu(disk_super->device_details_root));
1383 dm_sm_dec_block(pmd->metadata_sm, held_root);
1385 dm_tm_unlock(pmd->tm, copy);
1390 int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd)
1394 pmd_write_lock(pmd);
1396 r = __release_metadata_snap(pmd);
1397 pmd_write_unlock(pmd);
1402 static int __get_metadata_snap(struct dm_pool_metadata *pmd,
1406 struct thin_disk_superblock *disk_super;
1407 struct dm_block *sblock;
1409 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
1410 &sb_validator, &sblock);
1414 disk_super = dm_block_data(sblock);
1415 *result = le64_to_cpu(disk_super->held_root);
1417 dm_bm_unlock(sblock);
1422 int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
1427 down_read(&pmd->root_lock);
1429 r = __get_metadata_snap(pmd, result);
1430 up_read(&pmd->root_lock);
1435 int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
1436 struct dm_thin_device **td)
1440 pmd_write_lock_in_core(pmd);
1442 r = __open_device(pmd, dev, 0, td);
1443 pmd_write_unlock(pmd);
1448 int dm_pool_close_thin_device(struct dm_thin_device *td)
1450 pmd_write_lock_in_core(td->pmd);
1452 pmd_write_unlock(td->pmd);
1457 dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
1463 * Check whether @time (of block creation) is older than @td's last snapshot.
1464 * If so then the associated block is shared with the last snapshot device.
1465 * Any block on a device created *after* the device last got snapshotted is
1466 * necessarily not shared.
1468 static bool __snapshotted_since(struct dm_thin_device *td, uint32_t time)
1470 return td->snapshotted_time > time;
1473 static void unpack_lookup_result(struct dm_thin_device *td, __le64 value,
1474 struct dm_thin_lookup_result *result)
1476 uint64_t block_time = 0;
1477 dm_block_t exception_block;
1478 uint32_t exception_time;
1480 block_time = le64_to_cpu(value);
1481 unpack_block_time(block_time, &exception_block, &exception_time);
1482 result->block = exception_block;
1483 result->shared = __snapshotted_since(td, exception_time);
1486 static int __find_block(struct dm_thin_device *td, dm_block_t block,
1487 int can_issue_io, struct dm_thin_lookup_result *result)
1491 struct dm_pool_metadata *pmd = td->pmd;
1492 dm_block_t keys[2] = { td->id, block };
1493 struct dm_btree_info *info;
1498 info = &pmd->nb_info;
1500 r = dm_btree_lookup(info, pmd->root, keys, &value);
1502 unpack_lookup_result(td, value, result);
1507 int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
1508 int can_issue_io, struct dm_thin_lookup_result *result)
1511 struct dm_pool_metadata *pmd = td->pmd;
1513 down_read(&pmd->root_lock);
1515 up_read(&pmd->root_lock);
1519 r = __find_block(td, block, can_issue_io, result);
1521 up_read(&pmd->root_lock);
1525 static int __find_next_mapped_block(struct dm_thin_device *td, dm_block_t block,
1527 struct dm_thin_lookup_result *result)
1531 struct dm_pool_metadata *pmd = td->pmd;
1532 dm_block_t keys[2] = { td->id, block };
1534 r = dm_btree_lookup_next(&pmd->info, pmd->root, keys, vblock, &value);
1536 unpack_lookup_result(td, value, result);
1541 static int __find_mapped_range(struct dm_thin_device *td,
1542 dm_block_t begin, dm_block_t end,
1543 dm_block_t *thin_begin, dm_block_t *thin_end,
1544 dm_block_t *pool_begin, bool *maybe_shared)
1547 dm_block_t pool_end;
1548 struct dm_thin_lookup_result lookup;
1553 r = __find_next_mapped_block(td, begin, &begin, &lookup);
1560 *thin_begin = begin;
1561 *pool_begin = lookup.block;
1562 *maybe_shared = lookup.shared;
1565 pool_end = *pool_begin + 1;
1566 while (begin != end) {
1567 r = __find_block(td, begin, true, &lookup);
1575 if ((lookup.block != pool_end) ||
1576 (lookup.shared != *maybe_shared))
1587 int dm_thin_find_mapped_range(struct dm_thin_device *td,
1588 dm_block_t begin, dm_block_t end,
1589 dm_block_t *thin_begin, dm_block_t *thin_end,
1590 dm_block_t *pool_begin, bool *maybe_shared)
1593 struct dm_pool_metadata *pmd = td->pmd;
1595 down_read(&pmd->root_lock);
1596 if (!pmd->fail_io) {
1597 r = __find_mapped_range(td, begin, end, thin_begin, thin_end,
1598 pool_begin, maybe_shared);
1600 up_read(&pmd->root_lock);
1605 static int __insert(struct dm_thin_device *td, dm_block_t block,
1606 dm_block_t data_block)
1610 struct dm_pool_metadata *pmd = td->pmd;
1611 dm_block_t keys[2] = { td->id, block };
1613 value = cpu_to_le64(pack_block_time(data_block, pmd->time));
1614 __dm_bless_for_disk(&value);
1616 r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
1617 &pmd->root, &inserted);
1623 td->mapped_blocks++;
1628 int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
1629 dm_block_t data_block)
1633 pmd_write_lock(td->pmd);
1634 if (!td->pmd->fail_io)
1635 r = __insert(td, block, data_block);
1636 pmd_write_unlock(td->pmd);
1641 static int __remove(struct dm_thin_device *td, dm_block_t block)
1644 struct dm_pool_metadata *pmd = td->pmd;
1645 dm_block_t keys[2] = { td->id, block };
1647 r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
1651 td->mapped_blocks--;
1657 static int __remove_range(struct dm_thin_device *td, dm_block_t begin, dm_block_t end)
1660 unsigned count, total_count = 0;
1661 struct dm_pool_metadata *pmd = td->pmd;
1662 dm_block_t keys[1] = { td->id };
1664 dm_block_t mapping_root;
1667 * Find the mapping tree
1669 r = dm_btree_lookup(&pmd->tl_info, pmd->root, keys, &value);
1674 * Remove from the mapping tree, taking care to inc the
1675 * ref count so it doesn't get deleted.
1677 mapping_root = le64_to_cpu(value);
1678 dm_tm_inc(pmd->tm, mapping_root);
1679 r = dm_btree_remove(&pmd->tl_info, pmd->root, keys, &pmd->root);
1684 * Remove leaves stops at the first unmapped entry, so we have to
1685 * loop round finding mapped ranges.
1687 while (begin < end) {
1688 r = dm_btree_lookup_next(&pmd->bl_info, mapping_root, &begin, &begin, &value);
1698 r = dm_btree_remove_leaves(&pmd->bl_info, mapping_root, &begin, end, &mapping_root, &count);
1702 total_count += count;
1705 td->mapped_blocks -= total_count;
1709 * Reinsert the mapping tree.
1711 value = cpu_to_le64(mapping_root);
1712 __dm_bless_for_disk(&value);
1713 return dm_btree_insert(&pmd->tl_info, pmd->root, keys, &value, &pmd->root);
1716 int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
1720 pmd_write_lock(td->pmd);
1721 if (!td->pmd->fail_io)
1722 r = __remove(td, block);
1723 pmd_write_unlock(td->pmd);
1728 int dm_thin_remove_range(struct dm_thin_device *td,
1729 dm_block_t begin, dm_block_t end)
1733 pmd_write_lock(td->pmd);
1734 if (!td->pmd->fail_io)
1735 r = __remove_range(td, begin, end);
1736 pmd_write_unlock(td->pmd);
1741 int dm_pool_block_is_shared(struct dm_pool_metadata *pmd, dm_block_t b, bool *result)
1746 down_read(&pmd->root_lock);
1747 r = dm_sm_get_count(pmd->data_sm, b, &ref_count);
1749 *result = (ref_count > 1);
1750 up_read(&pmd->root_lock);
1755 int dm_pool_inc_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e)
1759 pmd_write_lock(pmd);
1760 for (; b != e; b++) {
1761 r = dm_sm_inc_block(pmd->data_sm, b);
1765 pmd_write_unlock(pmd);
1770 int dm_pool_dec_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e)
1774 pmd_write_lock(pmd);
1775 for (; b != e; b++) {
1776 r = dm_sm_dec_block(pmd->data_sm, b);
1780 pmd_write_unlock(pmd);
1785 bool dm_thin_changed_this_transaction(struct dm_thin_device *td)
1789 down_read(&td->pmd->root_lock);
1791 up_read(&td->pmd->root_lock);
1796 bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd)
1799 struct dm_thin_device *td, *tmp;
1801 down_read(&pmd->root_lock);
1802 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
1808 up_read(&pmd->root_lock);
1813 bool dm_thin_aborted_changes(struct dm_thin_device *td)
1817 down_read(&td->pmd->root_lock);
1818 r = td->aborted_with_changes;
1819 up_read(&td->pmd->root_lock);
1824 int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
1828 pmd_write_lock(pmd);
1830 r = dm_sm_new_block(pmd->data_sm, result);
1831 pmd_write_unlock(pmd);
1836 int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
1841 * Care is taken to not have commit be what
1842 * triggers putting the thin-pool in-service.
1844 __pmd_write_lock(pmd);
1848 r = __commit_transaction(pmd);
1853 * Open the next transaction.
1855 r = __begin_transaction(pmd);
1857 pmd_write_unlock(pmd);
1861 static void __set_abort_with_changes_flags(struct dm_pool_metadata *pmd)
1863 struct dm_thin_device *td;
1865 list_for_each_entry(td, &pmd->thin_devices, list)
1866 td->aborted_with_changes = td->changed;
1869 int dm_pool_abort_metadata(struct dm_pool_metadata *pmd)
1873 pmd_write_lock(pmd);
1877 __set_abort_with_changes_flags(pmd);
1878 __destroy_persistent_data_objects(pmd);
1879 r = __create_persistent_data_objects(pmd, false);
1881 pmd->fail_io = true;
1884 pmd_write_unlock(pmd);
1889 int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
1893 down_read(&pmd->root_lock);
1895 r = dm_sm_get_nr_free(pmd->data_sm, result);
1896 up_read(&pmd->root_lock);
1901 int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
1906 down_read(&pmd->root_lock);
1908 r = dm_sm_get_nr_free(pmd->metadata_sm, result);
1911 if (*result < pmd->metadata_reserve)
1914 *result -= pmd->metadata_reserve;
1916 up_read(&pmd->root_lock);
1921 int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
1926 down_read(&pmd->root_lock);
1928 r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
1929 up_read(&pmd->root_lock);
1934 int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
1938 down_read(&pmd->root_lock);
1940 r = dm_sm_get_nr_blocks(pmd->data_sm, result);
1941 up_read(&pmd->root_lock);
1946 int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
1949 struct dm_pool_metadata *pmd = td->pmd;
1951 down_read(&pmd->root_lock);
1952 if (!pmd->fail_io) {
1953 *result = td->mapped_blocks;
1956 up_read(&pmd->root_lock);
1961 static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
1965 dm_block_t thin_root;
1966 struct dm_pool_metadata *pmd = td->pmd;
1968 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
1972 thin_root = le64_to_cpu(value_le);
1974 return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
1977 int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
1981 struct dm_pool_metadata *pmd = td->pmd;
1983 down_read(&pmd->root_lock);
1985 r = __highest_block(td, result);
1986 up_read(&pmd->root_lock);
1991 static int __resize_space_map(struct dm_space_map *sm, dm_block_t new_count)
1994 dm_block_t old_count;
1996 r = dm_sm_get_nr_blocks(sm, &old_count);
2000 if (new_count == old_count)
2003 if (new_count < old_count) {
2004 DMERR("cannot reduce size of space map");
2008 return dm_sm_extend(sm, new_count - old_count);
2011 int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
2015 pmd_write_lock(pmd);
2017 r = __resize_space_map(pmd->data_sm, new_count);
2018 pmd_write_unlock(pmd);
2023 int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
2027 pmd_write_lock(pmd);
2028 if (!pmd->fail_io) {
2029 r = __resize_space_map(pmd->metadata_sm, new_count);
2031 __set_metadata_reserve(pmd);
2033 pmd_write_unlock(pmd);
2038 void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd)
2040 pmd_write_lock_in_core(pmd);
2041 dm_bm_set_read_only(pmd->bm);
2042 pmd_write_unlock(pmd);
2045 void dm_pool_metadata_read_write(struct dm_pool_metadata *pmd)
2047 pmd_write_lock_in_core(pmd);
2048 dm_bm_set_read_write(pmd->bm);
2049 pmd_write_unlock(pmd);
2052 int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd,
2053 dm_block_t threshold,
2054 dm_sm_threshold_fn fn,
2059 pmd_write_lock_in_core(pmd);
2060 r = dm_sm_register_threshold_callback(pmd->metadata_sm, threshold, fn, context);
2061 pmd_write_unlock(pmd);
2066 void dm_pool_register_pre_commit_callback(struct dm_pool_metadata *pmd,
2067 dm_pool_pre_commit_fn fn,
2070 pmd_write_lock_in_core(pmd);
2071 pmd->pre_commit_fn = fn;
2072 pmd->pre_commit_context = context;
2073 pmd_write_unlock(pmd);
2076 int dm_pool_metadata_set_needs_check(struct dm_pool_metadata *pmd)
2079 struct dm_block *sblock;
2080 struct thin_disk_superblock *disk_super;
2082 pmd_write_lock(pmd);
2086 pmd->flags |= THIN_METADATA_NEEDS_CHECK_FLAG;
2088 r = superblock_lock(pmd, &sblock);
2090 DMERR("couldn't lock superblock");
2094 disk_super = dm_block_data(sblock);
2095 disk_super->flags = cpu_to_le32(pmd->flags);
2097 dm_bm_unlock(sblock);
2099 pmd_write_unlock(pmd);
2103 bool dm_pool_metadata_needs_check(struct dm_pool_metadata *pmd)
2107 down_read(&pmd->root_lock);
2108 needs_check = pmd->flags & THIN_METADATA_NEEDS_CHECK_FLAG;
2109 up_read(&pmd->root_lock);
2114 void dm_pool_issue_prefetches(struct dm_pool_metadata *pmd)
2116 down_read(&pmd->root_lock);
2118 dm_tm_issue_prefetches(pmd->tm);
2119 up_read(&pmd->root_lock);