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 THIN_METADATA_CACHE_SIZE 64
81 #define SECTOR_TO_BLOCK_SHIFT 3
84 * 3 for btree insert +
85 * 2 for btree lookup used within space map
87 #define THIN_MAX_CONCURRENT_LOCKS 5
89 /* This should be plenty */
90 #define SPACE_MAP_ROOT_SIZE 128
93 * Little endian on-disk superblock and device details.
95 struct thin_disk_superblock
{
96 __le32 csum
; /* Checksum of superblock except for this field. */
98 __le64 blocknr
; /* This block number, dm_block_t. */
108 * Root held by userspace transactions.
112 __u8 data_space_map_root
[SPACE_MAP_ROOT_SIZE
];
113 __u8 metadata_space_map_root
[SPACE_MAP_ROOT_SIZE
];
116 * 2-level btree mapping (dev_id, (dev block, time)) -> data block
118 __le64 data_mapping_root
;
121 * Device detail root mapping dev_id -> device_details
123 __le64 device_details_root
;
125 __le32 data_block_size
; /* In 512-byte sectors. */
127 __le32 metadata_block_size
; /* In 512-byte sectors. */
128 __le64 metadata_nr_blocks
;
131 __le32 compat_ro_flags
;
132 __le32 incompat_flags
;
135 struct disk_device_details
{
136 __le64 mapped_blocks
;
137 __le64 transaction_id
; /* When created. */
138 __le32 creation_time
;
139 __le32 snapshotted_time
;
142 struct dm_pool_metadata
{
143 struct hlist_node hash
;
145 struct block_device
*bdev
;
146 struct dm_block_manager
*bm
;
147 struct dm_space_map
*metadata_sm
;
148 struct dm_space_map
*data_sm
;
149 struct dm_transaction_manager
*tm
;
150 struct dm_transaction_manager
*nb_tm
;
154 * First level holds thin_dev_t.
155 * Second level holds mappings.
157 struct dm_btree_info info
;
160 * Non-blocking version of the above.
162 struct dm_btree_info nb_info
;
165 * Just the top level for deleting whole devices.
167 struct dm_btree_info tl_info
;
170 * Just the bottom level for creating new devices.
172 struct dm_btree_info bl_info
;
175 * Describes the device details btree.
177 struct dm_btree_info details_info
;
179 struct rw_semaphore root_lock
;
182 dm_block_t details_root
;
183 struct list_head thin_devices
;
186 sector_t data_block_size
;
189 * Set if a transaction has to be aborted but the attempt to roll back
190 * to the previous (good) transaction failed. The only pool metadata
191 * operation possible in this state is the closing of the device.
196 * Reading the space map roots can fail, so we read it into these
197 * buffers before the superblock is locked and updated.
199 __u8 data_space_map_root
[SPACE_MAP_ROOT_SIZE
];
200 __u8 metadata_space_map_root
[SPACE_MAP_ROOT_SIZE
];
203 struct dm_thin_device
{
204 struct list_head list
;
205 struct dm_pool_metadata
*pmd
;
210 bool aborted_with_changes
:1;
211 uint64_t mapped_blocks
;
212 uint64_t transaction_id
;
213 uint32_t creation_time
;
214 uint32_t snapshotted_time
;
217 /*----------------------------------------------------------------
218 * superblock validator
219 *--------------------------------------------------------------*/
221 #define SUPERBLOCK_CSUM_XOR 160774
223 static void sb_prepare_for_write(struct dm_block_validator
*v
,
227 struct thin_disk_superblock
*disk_super
= dm_block_data(b
);
229 disk_super
->blocknr
= cpu_to_le64(dm_block_location(b
));
230 disk_super
->csum
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
231 block_size
- sizeof(__le32
),
232 SUPERBLOCK_CSUM_XOR
));
235 static int sb_check(struct dm_block_validator
*v
,
239 struct thin_disk_superblock
*disk_super
= dm_block_data(b
);
242 if (dm_block_location(b
) != le64_to_cpu(disk_super
->blocknr
)) {
243 DMERR("sb_check failed: blocknr %llu: "
244 "wanted %llu", le64_to_cpu(disk_super
->blocknr
),
245 (unsigned long long)dm_block_location(b
));
249 if (le64_to_cpu(disk_super
->magic
) != THIN_SUPERBLOCK_MAGIC
) {
250 DMERR("sb_check failed: magic %llu: "
251 "wanted %llu", le64_to_cpu(disk_super
->magic
),
252 (unsigned long long)THIN_SUPERBLOCK_MAGIC
);
256 csum_le
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
257 block_size
- sizeof(__le32
),
258 SUPERBLOCK_CSUM_XOR
));
259 if (csum_le
!= disk_super
->csum
) {
260 DMERR("sb_check failed: csum %u: wanted %u",
261 le32_to_cpu(csum_le
), le32_to_cpu(disk_super
->csum
));
268 static struct dm_block_validator sb_validator
= {
269 .name
= "superblock",
270 .prepare_for_write
= sb_prepare_for_write
,
274 /*----------------------------------------------------------------
275 * Methods for the btree value types
276 *--------------------------------------------------------------*/
278 static uint64_t pack_block_time(dm_block_t b
, uint32_t t
)
280 return (b
<< 24) | t
;
283 static void unpack_block_time(uint64_t v
, dm_block_t
*b
, uint32_t *t
)
286 *t
= v
& ((1 << 24) - 1);
289 static void data_block_inc(void *context
, const void *value_le
)
291 struct dm_space_map
*sm
= context
;
296 memcpy(&v_le
, value_le
, sizeof(v_le
));
297 unpack_block_time(le64_to_cpu(v_le
), &b
, &t
);
298 dm_sm_inc_block(sm
, b
);
301 static void data_block_dec(void *context
, const void *value_le
)
303 struct dm_space_map
*sm
= context
;
308 memcpy(&v_le
, value_le
, sizeof(v_le
));
309 unpack_block_time(le64_to_cpu(v_le
), &b
, &t
);
310 dm_sm_dec_block(sm
, b
);
313 static int data_block_equal(void *context
, const void *value1_le
, const void *value2_le
)
319 memcpy(&v1_le
, value1_le
, sizeof(v1_le
));
320 memcpy(&v2_le
, value2_le
, sizeof(v2_le
));
321 unpack_block_time(le64_to_cpu(v1_le
), &b1
, &t
);
322 unpack_block_time(le64_to_cpu(v2_le
), &b2
, &t
);
327 static void subtree_inc(void *context
, const void *value
)
329 struct dm_btree_info
*info
= context
;
333 memcpy(&root_le
, value
, sizeof(root_le
));
334 root
= le64_to_cpu(root_le
);
335 dm_tm_inc(info
->tm
, root
);
338 static void subtree_dec(void *context
, const void *value
)
340 struct dm_btree_info
*info
= context
;
344 memcpy(&root_le
, value
, sizeof(root_le
));
345 root
= le64_to_cpu(root_le
);
346 if (dm_btree_del(info
, root
))
347 DMERR("btree delete failed");
350 static int subtree_equal(void *context
, const void *value1_le
, const void *value2_le
)
353 memcpy(&v1_le
, value1_le
, sizeof(v1_le
));
354 memcpy(&v2_le
, value2_le
, sizeof(v2_le
));
356 return v1_le
== v2_le
;
359 /*----------------------------------------------------------------*/
361 static int superblock_lock_zero(struct dm_pool_metadata
*pmd
,
362 struct dm_block
**sblock
)
364 return dm_bm_write_lock_zero(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
365 &sb_validator
, sblock
);
368 static int superblock_lock(struct dm_pool_metadata
*pmd
,
369 struct dm_block
**sblock
)
371 return dm_bm_write_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
372 &sb_validator
, sblock
);
375 static int __superblock_all_zeroes(struct dm_block_manager
*bm
, int *result
)
380 __le64
*data_le
, zero
= cpu_to_le64(0);
381 unsigned block_size
= dm_bm_block_size(bm
) / sizeof(__le64
);
384 * We can't use a validator here - it may be all zeroes.
386 r
= dm_bm_read_lock(bm
, THIN_SUPERBLOCK_LOCATION
, NULL
, &b
);
390 data_le
= dm_block_data(b
);
392 for (i
= 0; i
< block_size
; i
++) {
393 if (data_le
[i
] != zero
) {
404 static void __setup_btree_details(struct dm_pool_metadata
*pmd
)
406 pmd
->info
.tm
= pmd
->tm
;
407 pmd
->info
.levels
= 2;
408 pmd
->info
.value_type
.context
= pmd
->data_sm
;
409 pmd
->info
.value_type
.size
= sizeof(__le64
);
410 pmd
->info
.value_type
.inc
= data_block_inc
;
411 pmd
->info
.value_type
.dec
= data_block_dec
;
412 pmd
->info
.value_type
.equal
= data_block_equal
;
414 memcpy(&pmd
->nb_info
, &pmd
->info
, sizeof(pmd
->nb_info
));
415 pmd
->nb_info
.tm
= pmd
->nb_tm
;
417 pmd
->tl_info
.tm
= pmd
->tm
;
418 pmd
->tl_info
.levels
= 1;
419 pmd
->tl_info
.value_type
.context
= &pmd
->bl_info
;
420 pmd
->tl_info
.value_type
.size
= sizeof(__le64
);
421 pmd
->tl_info
.value_type
.inc
= subtree_inc
;
422 pmd
->tl_info
.value_type
.dec
= subtree_dec
;
423 pmd
->tl_info
.value_type
.equal
= subtree_equal
;
425 pmd
->bl_info
.tm
= pmd
->tm
;
426 pmd
->bl_info
.levels
= 1;
427 pmd
->bl_info
.value_type
.context
= pmd
->data_sm
;
428 pmd
->bl_info
.value_type
.size
= sizeof(__le64
);
429 pmd
->bl_info
.value_type
.inc
= data_block_inc
;
430 pmd
->bl_info
.value_type
.dec
= data_block_dec
;
431 pmd
->bl_info
.value_type
.equal
= data_block_equal
;
433 pmd
->details_info
.tm
= pmd
->tm
;
434 pmd
->details_info
.levels
= 1;
435 pmd
->details_info
.value_type
.context
= NULL
;
436 pmd
->details_info
.value_type
.size
= sizeof(struct disk_device_details
);
437 pmd
->details_info
.value_type
.inc
= NULL
;
438 pmd
->details_info
.value_type
.dec
= NULL
;
439 pmd
->details_info
.value_type
.equal
= NULL
;
442 static int save_sm_roots(struct dm_pool_metadata
*pmd
)
447 r
= dm_sm_root_size(pmd
->metadata_sm
, &len
);
451 r
= dm_sm_copy_root(pmd
->metadata_sm
, &pmd
->metadata_space_map_root
, len
);
455 r
= dm_sm_root_size(pmd
->data_sm
, &len
);
459 return dm_sm_copy_root(pmd
->data_sm
, &pmd
->data_space_map_root
, len
);
462 static void copy_sm_roots(struct dm_pool_metadata
*pmd
,
463 struct thin_disk_superblock
*disk
)
465 memcpy(&disk
->metadata_space_map_root
,
466 &pmd
->metadata_space_map_root
,
467 sizeof(pmd
->metadata_space_map_root
));
469 memcpy(&disk
->data_space_map_root
,
470 &pmd
->data_space_map_root
,
471 sizeof(pmd
->data_space_map_root
));
474 static int __write_initial_superblock(struct dm_pool_metadata
*pmd
)
477 struct dm_block
*sblock
;
478 struct thin_disk_superblock
*disk_super
;
479 sector_t bdev_size
= i_size_read(pmd
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
481 if (bdev_size
> THIN_METADATA_MAX_SECTORS
)
482 bdev_size
= THIN_METADATA_MAX_SECTORS
;
484 r
= dm_sm_commit(pmd
->data_sm
);
488 r
= save_sm_roots(pmd
);
492 r
= dm_tm_pre_commit(pmd
->tm
);
496 r
= superblock_lock_zero(pmd
, &sblock
);
500 disk_super
= dm_block_data(sblock
);
501 disk_super
->flags
= 0;
502 memset(disk_super
->uuid
, 0, sizeof(disk_super
->uuid
));
503 disk_super
->magic
= cpu_to_le64(THIN_SUPERBLOCK_MAGIC
);
504 disk_super
->version
= cpu_to_le32(THIN_VERSION
);
505 disk_super
->time
= 0;
506 disk_super
->trans_id
= 0;
507 disk_super
->held_root
= 0;
509 copy_sm_roots(pmd
, disk_super
);
511 disk_super
->data_mapping_root
= cpu_to_le64(pmd
->root
);
512 disk_super
->device_details_root
= cpu_to_le64(pmd
->details_root
);
513 disk_super
->metadata_block_size
= cpu_to_le32(THIN_METADATA_BLOCK_SIZE
);
514 disk_super
->metadata_nr_blocks
= cpu_to_le64(bdev_size
>> SECTOR_TO_BLOCK_SHIFT
);
515 disk_super
->data_block_size
= cpu_to_le32(pmd
->data_block_size
);
517 return dm_tm_commit(pmd
->tm
, sblock
);
520 static int __format_metadata(struct dm_pool_metadata
*pmd
)
524 r
= dm_tm_create_with_sm(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
525 &pmd
->tm
, &pmd
->metadata_sm
);
527 DMERR("tm_create_with_sm failed");
531 pmd
->data_sm
= dm_sm_disk_create(pmd
->tm
, 0);
532 if (IS_ERR(pmd
->data_sm
)) {
533 DMERR("sm_disk_create failed");
534 r
= PTR_ERR(pmd
->data_sm
);
538 pmd
->nb_tm
= dm_tm_create_non_blocking_clone(pmd
->tm
);
540 DMERR("could not create non-blocking clone tm");
542 goto bad_cleanup_data_sm
;
545 __setup_btree_details(pmd
);
547 r
= dm_btree_empty(&pmd
->info
, &pmd
->root
);
549 goto bad_cleanup_nb_tm
;
551 r
= dm_btree_empty(&pmd
->details_info
, &pmd
->details_root
);
553 DMERR("couldn't create devices root");
554 goto bad_cleanup_nb_tm
;
557 r
= __write_initial_superblock(pmd
);
559 goto bad_cleanup_nb_tm
;
564 dm_tm_destroy(pmd
->nb_tm
);
566 dm_sm_destroy(pmd
->data_sm
);
568 dm_tm_destroy(pmd
->tm
);
569 dm_sm_destroy(pmd
->metadata_sm
);
574 static int __check_incompat_features(struct thin_disk_superblock
*disk_super
,
575 struct dm_pool_metadata
*pmd
)
579 features
= le32_to_cpu(disk_super
->incompat_flags
) & ~THIN_FEATURE_INCOMPAT_SUPP
;
581 DMERR("could not access metadata due to unsupported optional features (%lx).",
582 (unsigned long)features
);
587 * Check for read-only metadata to skip the following RDWR checks.
589 if (get_disk_ro(pmd
->bdev
->bd_disk
))
592 features
= le32_to_cpu(disk_super
->compat_ro_flags
) & ~THIN_FEATURE_COMPAT_RO_SUPP
;
594 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
595 (unsigned long)features
);
602 static int __open_metadata(struct dm_pool_metadata
*pmd
)
605 struct dm_block
*sblock
;
606 struct thin_disk_superblock
*disk_super
;
608 r
= dm_bm_read_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
609 &sb_validator
, &sblock
);
611 DMERR("couldn't read superblock");
615 disk_super
= dm_block_data(sblock
);
617 /* Verify the data block size hasn't changed */
618 if (le32_to_cpu(disk_super
->data_block_size
) != pmd
->data_block_size
) {
619 DMERR("changing the data block size (from %u to %llu) is not supported",
620 le32_to_cpu(disk_super
->data_block_size
),
621 (unsigned long long)pmd
->data_block_size
);
623 goto bad_unlock_sblock
;
626 r
= __check_incompat_features(disk_super
, pmd
);
628 goto bad_unlock_sblock
;
630 r
= dm_tm_open_with_sm(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
631 disk_super
->metadata_space_map_root
,
632 sizeof(disk_super
->metadata_space_map_root
),
633 &pmd
->tm
, &pmd
->metadata_sm
);
635 DMERR("tm_open_with_sm failed");
636 goto bad_unlock_sblock
;
639 pmd
->data_sm
= dm_sm_disk_open(pmd
->tm
, disk_super
->data_space_map_root
,
640 sizeof(disk_super
->data_space_map_root
));
641 if (IS_ERR(pmd
->data_sm
)) {
642 DMERR("sm_disk_open failed");
643 r
= PTR_ERR(pmd
->data_sm
);
647 pmd
->nb_tm
= dm_tm_create_non_blocking_clone(pmd
->tm
);
649 DMERR("could not create non-blocking clone tm");
651 goto bad_cleanup_data_sm
;
654 __setup_btree_details(pmd
);
655 dm_bm_unlock(sblock
);
660 dm_sm_destroy(pmd
->data_sm
);
662 dm_tm_destroy(pmd
->tm
);
663 dm_sm_destroy(pmd
->metadata_sm
);
665 dm_bm_unlock(sblock
);
670 static int __open_or_format_metadata(struct dm_pool_metadata
*pmd
, bool format_device
)
674 r
= __superblock_all_zeroes(pmd
->bm
, &unformatted
);
679 return format_device
? __format_metadata(pmd
) : -EPERM
;
681 return __open_metadata(pmd
);
684 static int __create_persistent_data_objects(struct dm_pool_metadata
*pmd
, bool format_device
)
688 pmd
->bm
= dm_block_manager_create(pmd
->bdev
, THIN_METADATA_BLOCK_SIZE
<< SECTOR_SHIFT
,
689 THIN_METADATA_CACHE_SIZE
,
690 THIN_MAX_CONCURRENT_LOCKS
);
691 if (IS_ERR(pmd
->bm
)) {
692 DMERR("could not create block manager");
693 return PTR_ERR(pmd
->bm
);
696 r
= __open_or_format_metadata(pmd
, format_device
);
698 dm_block_manager_destroy(pmd
->bm
);
703 static void __destroy_persistent_data_objects(struct dm_pool_metadata
*pmd
)
705 dm_sm_destroy(pmd
->data_sm
);
706 dm_sm_destroy(pmd
->metadata_sm
);
707 dm_tm_destroy(pmd
->nb_tm
);
708 dm_tm_destroy(pmd
->tm
);
709 dm_block_manager_destroy(pmd
->bm
);
712 static int __begin_transaction(struct dm_pool_metadata
*pmd
)
715 struct thin_disk_superblock
*disk_super
;
716 struct dm_block
*sblock
;
719 * We re-read the superblock every time. Shouldn't need to do this
722 r
= dm_bm_read_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
723 &sb_validator
, &sblock
);
727 disk_super
= dm_block_data(sblock
);
728 pmd
->time
= le32_to_cpu(disk_super
->time
);
729 pmd
->root
= le64_to_cpu(disk_super
->data_mapping_root
);
730 pmd
->details_root
= le64_to_cpu(disk_super
->device_details_root
);
731 pmd
->trans_id
= le64_to_cpu(disk_super
->trans_id
);
732 pmd
->flags
= le32_to_cpu(disk_super
->flags
);
733 pmd
->data_block_size
= le32_to_cpu(disk_super
->data_block_size
);
735 dm_bm_unlock(sblock
);
739 static int __write_changed_details(struct dm_pool_metadata
*pmd
)
742 struct dm_thin_device
*td
, *tmp
;
743 struct disk_device_details details
;
746 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
752 details
.mapped_blocks
= cpu_to_le64(td
->mapped_blocks
);
753 details
.transaction_id
= cpu_to_le64(td
->transaction_id
);
754 details
.creation_time
= cpu_to_le32(td
->creation_time
);
755 details
.snapshotted_time
= cpu_to_le32(td
->snapshotted_time
);
756 __dm_bless_for_disk(&details
);
758 r
= dm_btree_insert(&pmd
->details_info
, pmd
->details_root
,
759 &key
, &details
, &pmd
->details_root
);
774 static int __commit_transaction(struct dm_pool_metadata
*pmd
)
777 size_t metadata_len
, data_len
;
778 struct thin_disk_superblock
*disk_super
;
779 struct dm_block
*sblock
;
782 * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
784 BUILD_BUG_ON(sizeof(struct thin_disk_superblock
) > 512);
786 r
= __write_changed_details(pmd
);
790 r
= dm_sm_commit(pmd
->data_sm
);
794 r
= dm_tm_pre_commit(pmd
->tm
);
798 r
= dm_sm_root_size(pmd
->metadata_sm
, &metadata_len
);
802 r
= dm_sm_root_size(pmd
->data_sm
, &data_len
);
806 r
= save_sm_roots(pmd
);
810 r
= superblock_lock(pmd
, &sblock
);
814 disk_super
= dm_block_data(sblock
);
815 disk_super
->time
= cpu_to_le32(pmd
->time
);
816 disk_super
->data_mapping_root
= cpu_to_le64(pmd
->root
);
817 disk_super
->device_details_root
= cpu_to_le64(pmd
->details_root
);
818 disk_super
->trans_id
= cpu_to_le64(pmd
->trans_id
);
819 disk_super
->flags
= cpu_to_le32(pmd
->flags
);
821 copy_sm_roots(pmd
, disk_super
);
823 return dm_tm_commit(pmd
->tm
, sblock
);
826 struct dm_pool_metadata
*dm_pool_metadata_open(struct block_device
*bdev
,
827 sector_t data_block_size
,
831 struct dm_pool_metadata
*pmd
;
833 pmd
= kmalloc(sizeof(*pmd
), GFP_KERNEL
);
835 DMERR("could not allocate metadata struct");
836 return ERR_PTR(-ENOMEM
);
839 init_rwsem(&pmd
->root_lock
);
841 INIT_LIST_HEAD(&pmd
->thin_devices
);
842 pmd
->fail_io
= false;
844 pmd
->data_block_size
= data_block_size
;
846 r
= __create_persistent_data_objects(pmd
, format_device
);
852 r
= __begin_transaction(pmd
);
854 if (dm_pool_metadata_close(pmd
) < 0)
855 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
862 int dm_pool_metadata_close(struct dm_pool_metadata
*pmd
)
865 unsigned open_devices
= 0;
866 struct dm_thin_device
*td
, *tmp
;
868 down_read(&pmd
->root_lock
);
869 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
877 up_read(&pmd
->root_lock
);
880 DMERR("attempt to close pmd when %u device(s) are still open",
885 if (!dm_bm_is_read_only(pmd
->bm
) && !pmd
->fail_io
) {
886 r
= __commit_transaction(pmd
);
888 DMWARN("%s: __commit_transaction() failed, error = %d",
893 __destroy_persistent_data_objects(pmd
);
900 * __open_device: Returns @td corresponding to device with id @dev,
901 * creating it if @create is set and incrementing @td->open_count.
902 * On failure, @td is undefined.
904 static int __open_device(struct dm_pool_metadata
*pmd
,
905 dm_thin_id dev
, int create
,
906 struct dm_thin_device
**td
)
909 struct dm_thin_device
*td2
;
911 struct disk_device_details details_le
;
914 * If the device is already open, return it.
916 list_for_each_entry(td2
, &pmd
->thin_devices
, list
)
917 if (td2
->id
== dev
) {
919 * May not create an already-open device.
930 * Check the device exists.
932 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
935 if (r
!= -ENODATA
|| !create
)
942 details_le
.mapped_blocks
= 0;
943 details_le
.transaction_id
= cpu_to_le64(pmd
->trans_id
);
944 details_le
.creation_time
= cpu_to_le32(pmd
->time
);
945 details_le
.snapshotted_time
= cpu_to_le32(pmd
->time
);
948 *td
= kmalloc(sizeof(**td
), GFP_NOIO
);
954 (*td
)->open_count
= 1;
955 (*td
)->changed
= changed
;
956 (*td
)->aborted_with_changes
= false;
957 (*td
)->mapped_blocks
= le64_to_cpu(details_le
.mapped_blocks
);
958 (*td
)->transaction_id
= le64_to_cpu(details_le
.transaction_id
);
959 (*td
)->creation_time
= le32_to_cpu(details_le
.creation_time
);
960 (*td
)->snapshotted_time
= le32_to_cpu(details_le
.snapshotted_time
);
962 list_add(&(*td
)->list
, &pmd
->thin_devices
);
967 static void __close_device(struct dm_thin_device
*td
)
972 static int __create_thin(struct dm_pool_metadata
*pmd
,
978 struct disk_device_details details_le
;
979 struct dm_thin_device
*td
;
982 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
988 * Create an empty btree for the mappings.
990 r
= dm_btree_empty(&pmd
->bl_info
, &dev_root
);
995 * Insert it into the main mapping tree.
997 value
= cpu_to_le64(dev_root
);
998 __dm_bless_for_disk(&value
);
999 r
= dm_btree_insert(&pmd
->tl_info
, pmd
->root
, &key
, &value
, &pmd
->root
);
1001 dm_btree_del(&pmd
->bl_info
, dev_root
);
1005 r
= __open_device(pmd
, dev
, 1, &td
);
1007 dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
1008 dm_btree_del(&pmd
->bl_info
, dev_root
);
1016 int dm_pool_create_thin(struct dm_pool_metadata
*pmd
, dm_thin_id dev
)
1020 down_write(&pmd
->root_lock
);
1022 r
= __create_thin(pmd
, dev
);
1023 up_write(&pmd
->root_lock
);
1028 static int __set_snapshot_details(struct dm_pool_metadata
*pmd
,
1029 struct dm_thin_device
*snap
,
1030 dm_thin_id origin
, uint32_t time
)
1033 struct dm_thin_device
*td
;
1035 r
= __open_device(pmd
, origin
, 0, &td
);
1040 td
->snapshotted_time
= time
;
1042 snap
->mapped_blocks
= td
->mapped_blocks
;
1043 snap
->snapshotted_time
= time
;
1049 static int __create_snap(struct dm_pool_metadata
*pmd
,
1050 dm_thin_id dev
, dm_thin_id origin
)
1053 dm_block_t origin_root
;
1054 uint64_t key
= origin
, dev_key
= dev
;
1055 struct dm_thin_device
*td
;
1056 struct disk_device_details details_le
;
1059 /* check this device is unused */
1060 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
1061 &dev_key
, &details_le
);
1065 /* find the mapping tree for the origin */
1066 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, &key
, &value
);
1069 origin_root
= le64_to_cpu(value
);
1071 /* clone the origin, an inc will do */
1072 dm_tm_inc(pmd
->tm
, origin_root
);
1074 /* insert into the main mapping tree */
1075 value
= cpu_to_le64(origin_root
);
1076 __dm_bless_for_disk(&value
);
1078 r
= dm_btree_insert(&pmd
->tl_info
, pmd
->root
, &key
, &value
, &pmd
->root
);
1080 dm_tm_dec(pmd
->tm
, origin_root
);
1086 r
= __open_device(pmd
, dev
, 1, &td
);
1090 r
= __set_snapshot_details(pmd
, td
, origin
, pmd
->time
);
1099 dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
1100 dm_btree_remove(&pmd
->details_info
, pmd
->details_root
,
1101 &key
, &pmd
->details_root
);
1105 int dm_pool_create_snap(struct dm_pool_metadata
*pmd
,
1111 down_write(&pmd
->root_lock
);
1113 r
= __create_snap(pmd
, dev
, origin
);
1114 up_write(&pmd
->root_lock
);
1119 static int __delete_device(struct dm_pool_metadata
*pmd
, dm_thin_id dev
)
1123 struct dm_thin_device
*td
;
1125 /* TODO: failure should mark the transaction invalid */
1126 r
= __open_device(pmd
, dev
, 0, &td
);
1130 if (td
->open_count
> 1) {
1135 list_del(&td
->list
);
1137 r
= dm_btree_remove(&pmd
->details_info
, pmd
->details_root
,
1138 &key
, &pmd
->details_root
);
1142 r
= dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
1149 int dm_pool_delete_thin_device(struct dm_pool_metadata
*pmd
,
1154 down_write(&pmd
->root_lock
);
1156 r
= __delete_device(pmd
, dev
);
1157 up_write(&pmd
->root_lock
);
1162 int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata
*pmd
,
1163 uint64_t current_id
,
1168 down_write(&pmd
->root_lock
);
1173 if (pmd
->trans_id
!= current_id
) {
1174 DMERR("mismatched transaction id");
1178 pmd
->trans_id
= new_id
;
1182 up_write(&pmd
->root_lock
);
1187 int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata
*pmd
,
1192 down_read(&pmd
->root_lock
);
1193 if (!pmd
->fail_io
) {
1194 *result
= pmd
->trans_id
;
1197 up_read(&pmd
->root_lock
);
1202 static int __reserve_metadata_snap(struct dm_pool_metadata
*pmd
)
1205 struct thin_disk_superblock
*disk_super
;
1206 struct dm_block
*copy
, *sblock
;
1207 dm_block_t held_root
;
1210 * We commit to ensure the btree roots which we increment in a
1211 * moment are up to date.
1213 __commit_transaction(pmd
);
1216 * Copy the superblock.
1218 dm_sm_inc_block(pmd
->metadata_sm
, THIN_SUPERBLOCK_LOCATION
);
1219 r
= dm_tm_shadow_block(pmd
->tm
, THIN_SUPERBLOCK_LOCATION
,
1220 &sb_validator
, ©
, &inc
);
1226 held_root
= dm_block_location(copy
);
1227 disk_super
= dm_block_data(copy
);
1229 if (le64_to_cpu(disk_super
->held_root
)) {
1230 DMWARN("Pool metadata snapshot already exists: release this before taking another.");
1232 dm_tm_dec(pmd
->tm
, held_root
);
1233 dm_tm_unlock(pmd
->tm
, copy
);
1238 * Wipe the spacemap since we're not publishing this.
1240 memset(&disk_super
->data_space_map_root
, 0,
1241 sizeof(disk_super
->data_space_map_root
));
1242 memset(&disk_super
->metadata_space_map_root
, 0,
1243 sizeof(disk_super
->metadata_space_map_root
));
1246 * Increment the data structures that need to be preserved.
1248 dm_tm_inc(pmd
->tm
, le64_to_cpu(disk_super
->data_mapping_root
));
1249 dm_tm_inc(pmd
->tm
, le64_to_cpu(disk_super
->device_details_root
));
1250 dm_tm_unlock(pmd
->tm
, copy
);
1253 * Write the held root into the superblock.
1255 r
= superblock_lock(pmd
, &sblock
);
1257 dm_tm_dec(pmd
->tm
, held_root
);
1261 disk_super
= dm_block_data(sblock
);
1262 disk_super
->held_root
= cpu_to_le64(held_root
);
1263 dm_bm_unlock(sblock
);
1267 int dm_pool_reserve_metadata_snap(struct dm_pool_metadata
*pmd
)
1271 down_write(&pmd
->root_lock
);
1273 r
= __reserve_metadata_snap(pmd
);
1274 up_write(&pmd
->root_lock
);
1279 static int __release_metadata_snap(struct dm_pool_metadata
*pmd
)
1282 struct thin_disk_superblock
*disk_super
;
1283 struct dm_block
*sblock
, *copy
;
1284 dm_block_t held_root
;
1286 r
= superblock_lock(pmd
, &sblock
);
1290 disk_super
= dm_block_data(sblock
);
1291 held_root
= le64_to_cpu(disk_super
->held_root
);
1292 disk_super
->held_root
= cpu_to_le64(0);
1294 dm_bm_unlock(sblock
);
1297 DMWARN("No pool metadata snapshot found: nothing to release.");
1301 r
= dm_tm_read_lock(pmd
->tm
, held_root
, &sb_validator
, ©
);
1305 disk_super
= dm_block_data(copy
);
1306 dm_btree_del(&pmd
->info
, le64_to_cpu(disk_super
->data_mapping_root
));
1307 dm_btree_del(&pmd
->details_info
, le64_to_cpu(disk_super
->device_details_root
));
1308 dm_sm_dec_block(pmd
->metadata_sm
, held_root
);
1310 dm_tm_unlock(pmd
->tm
, copy
);
1315 int dm_pool_release_metadata_snap(struct dm_pool_metadata
*pmd
)
1319 down_write(&pmd
->root_lock
);
1321 r
= __release_metadata_snap(pmd
);
1322 up_write(&pmd
->root_lock
);
1327 static int __get_metadata_snap(struct dm_pool_metadata
*pmd
,
1331 struct thin_disk_superblock
*disk_super
;
1332 struct dm_block
*sblock
;
1334 r
= dm_bm_read_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
1335 &sb_validator
, &sblock
);
1339 disk_super
= dm_block_data(sblock
);
1340 *result
= le64_to_cpu(disk_super
->held_root
);
1342 dm_bm_unlock(sblock
);
1347 int dm_pool_get_metadata_snap(struct dm_pool_metadata
*pmd
,
1352 down_read(&pmd
->root_lock
);
1354 r
= __get_metadata_snap(pmd
, result
);
1355 up_read(&pmd
->root_lock
);
1360 int dm_pool_open_thin_device(struct dm_pool_metadata
*pmd
, dm_thin_id dev
,
1361 struct dm_thin_device
**td
)
1365 down_write(&pmd
->root_lock
);
1367 r
= __open_device(pmd
, dev
, 0, td
);
1368 up_write(&pmd
->root_lock
);
1373 int dm_pool_close_thin_device(struct dm_thin_device
*td
)
1375 down_write(&td
->pmd
->root_lock
);
1377 up_write(&td
->pmd
->root_lock
);
1382 dm_thin_id
dm_thin_dev_id(struct dm_thin_device
*td
)
1388 * Check whether @time (of block creation) is older than @td's last snapshot.
1389 * If so then the associated block is shared with the last snapshot device.
1390 * Any block on a device created *after* the device last got snapshotted is
1391 * necessarily not shared.
1393 static bool __snapshotted_since(struct dm_thin_device
*td
, uint32_t time
)
1395 return td
->snapshotted_time
> time
;
1398 static void unpack_lookup_result(struct dm_thin_device
*td
, __le64 value
,
1399 struct dm_thin_lookup_result
*result
)
1401 uint64_t block_time
= 0;
1402 dm_block_t exception_block
;
1403 uint32_t exception_time
;
1405 block_time
= le64_to_cpu(value
);
1406 unpack_block_time(block_time
, &exception_block
, &exception_time
);
1407 result
->block
= exception_block
;
1408 result
->shared
= __snapshotted_since(td
, exception_time
);
1411 static int __find_block(struct dm_thin_device
*td
, dm_block_t block
,
1412 int can_issue_io
, struct dm_thin_lookup_result
*result
)
1416 struct dm_pool_metadata
*pmd
= td
->pmd
;
1417 dm_block_t keys
[2] = { td
->id
, block
};
1418 struct dm_btree_info
*info
;
1423 info
= &pmd
->nb_info
;
1425 r
= dm_btree_lookup(info
, pmd
->root
, keys
, &value
);
1427 unpack_lookup_result(td
, value
, result
);
1432 int dm_thin_find_block(struct dm_thin_device
*td
, dm_block_t block
,
1433 int can_issue_io
, struct dm_thin_lookup_result
*result
)
1436 struct dm_pool_metadata
*pmd
= td
->pmd
;
1438 down_read(&pmd
->root_lock
);
1440 up_read(&pmd
->root_lock
);
1444 r
= __find_block(td
, block
, can_issue_io
, result
);
1446 up_read(&pmd
->root_lock
);
1450 static int __find_next_mapped_block(struct dm_thin_device
*td
, dm_block_t block
,
1452 struct dm_thin_lookup_result
*result
)
1456 struct dm_pool_metadata
*pmd
= td
->pmd
;
1457 dm_block_t keys
[2] = { td
->id
, block
};
1459 r
= dm_btree_lookup_next(&pmd
->info
, pmd
->root
, keys
, vblock
, &value
);
1461 unpack_lookup_result(td
, value
, result
);
1466 static int __find_mapped_range(struct dm_thin_device
*td
,
1467 dm_block_t begin
, dm_block_t end
,
1468 dm_block_t
*thin_begin
, dm_block_t
*thin_end
,
1469 dm_block_t
*pool_begin
, bool *maybe_shared
)
1472 dm_block_t pool_end
;
1473 struct dm_thin_lookup_result lookup
;
1478 r
= __find_next_mapped_block(td
, begin
, &begin
, &lookup
);
1485 *thin_begin
= begin
;
1486 *pool_begin
= lookup
.block
;
1487 *maybe_shared
= lookup
.shared
;
1490 pool_end
= *pool_begin
+ 1;
1491 while (begin
!= end
) {
1492 r
= __find_block(td
, begin
, true, &lookup
);
1500 if ((lookup
.block
!= pool_end
) ||
1501 (lookup
.shared
!= *maybe_shared
))
1512 int dm_thin_find_mapped_range(struct dm_thin_device
*td
,
1513 dm_block_t begin
, dm_block_t end
,
1514 dm_block_t
*thin_begin
, dm_block_t
*thin_end
,
1515 dm_block_t
*pool_begin
, bool *maybe_shared
)
1518 struct dm_pool_metadata
*pmd
= td
->pmd
;
1520 down_read(&pmd
->root_lock
);
1521 if (!pmd
->fail_io
) {
1522 r
= __find_mapped_range(td
, begin
, end
, thin_begin
, thin_end
,
1523 pool_begin
, maybe_shared
);
1525 up_read(&pmd
->root_lock
);
1530 static int __insert(struct dm_thin_device
*td
, dm_block_t block
,
1531 dm_block_t data_block
)
1535 struct dm_pool_metadata
*pmd
= td
->pmd
;
1536 dm_block_t keys
[2] = { td
->id
, block
};
1538 value
= cpu_to_le64(pack_block_time(data_block
, pmd
->time
));
1539 __dm_bless_for_disk(&value
);
1541 r
= dm_btree_insert_notify(&pmd
->info
, pmd
->root
, keys
, &value
,
1542 &pmd
->root
, &inserted
);
1548 td
->mapped_blocks
++;
1553 int dm_thin_insert_block(struct dm_thin_device
*td
, dm_block_t block
,
1554 dm_block_t data_block
)
1558 down_write(&td
->pmd
->root_lock
);
1559 if (!td
->pmd
->fail_io
)
1560 r
= __insert(td
, block
, data_block
);
1561 up_write(&td
->pmd
->root_lock
);
1566 static int __remove(struct dm_thin_device
*td
, dm_block_t block
)
1569 struct dm_pool_metadata
*pmd
= td
->pmd
;
1570 dm_block_t keys
[2] = { td
->id
, block
};
1572 r
= dm_btree_remove(&pmd
->info
, pmd
->root
, keys
, &pmd
->root
);
1576 td
->mapped_blocks
--;
1582 static int __remove_range(struct dm_thin_device
*td
, dm_block_t begin
, dm_block_t end
)
1585 unsigned count
, total_count
= 0;
1586 struct dm_pool_metadata
*pmd
= td
->pmd
;
1587 dm_block_t keys
[1] = { td
->id
};
1589 dm_block_t mapping_root
;
1592 * Find the mapping tree
1594 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, keys
, &value
);
1599 * Remove from the mapping tree, taking care to inc the
1600 * ref count so it doesn't get deleted.
1602 mapping_root
= le64_to_cpu(value
);
1603 dm_tm_inc(pmd
->tm
, mapping_root
);
1604 r
= dm_btree_remove(&pmd
->tl_info
, pmd
->root
, keys
, &pmd
->root
);
1609 * Remove leaves stops at the first unmapped entry, so we have to
1610 * loop round finding mapped ranges.
1612 while (begin
< end
) {
1613 r
= dm_btree_lookup_next(&pmd
->bl_info
, mapping_root
, &begin
, &begin
, &value
);
1623 r
= dm_btree_remove_leaves(&pmd
->bl_info
, mapping_root
, &begin
, end
, &mapping_root
, &count
);
1627 total_count
+= count
;
1630 td
->mapped_blocks
-= total_count
;
1634 * Reinsert the mapping tree.
1636 value
= cpu_to_le64(mapping_root
);
1637 __dm_bless_for_disk(&value
);
1638 return dm_btree_insert(&pmd
->tl_info
, pmd
->root
, keys
, &value
, &pmd
->root
);
1641 int dm_thin_remove_block(struct dm_thin_device
*td
, dm_block_t block
)
1645 down_write(&td
->pmd
->root_lock
);
1646 if (!td
->pmd
->fail_io
)
1647 r
= __remove(td
, block
);
1648 up_write(&td
->pmd
->root_lock
);
1653 int dm_thin_remove_range(struct dm_thin_device
*td
,
1654 dm_block_t begin
, dm_block_t end
)
1658 down_write(&td
->pmd
->root_lock
);
1659 if (!td
->pmd
->fail_io
)
1660 r
= __remove_range(td
, begin
, end
);
1661 up_write(&td
->pmd
->root_lock
);
1666 int dm_pool_block_is_used(struct dm_pool_metadata
*pmd
, dm_block_t b
, bool *result
)
1671 down_read(&pmd
->root_lock
);
1672 r
= dm_sm_get_count(pmd
->data_sm
, b
, &ref_count
);
1674 *result
= (ref_count
!= 0);
1675 up_read(&pmd
->root_lock
);
1680 bool dm_thin_changed_this_transaction(struct dm_thin_device
*td
)
1684 down_read(&td
->pmd
->root_lock
);
1686 up_read(&td
->pmd
->root_lock
);
1691 bool dm_pool_changed_this_transaction(struct dm_pool_metadata
*pmd
)
1694 struct dm_thin_device
*td
, *tmp
;
1696 down_read(&pmd
->root_lock
);
1697 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
1703 up_read(&pmd
->root_lock
);
1708 bool dm_thin_aborted_changes(struct dm_thin_device
*td
)
1712 down_read(&td
->pmd
->root_lock
);
1713 r
= td
->aborted_with_changes
;
1714 up_read(&td
->pmd
->root_lock
);
1719 int dm_pool_alloc_data_block(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1723 down_write(&pmd
->root_lock
);
1725 r
= dm_sm_new_block(pmd
->data_sm
, result
);
1726 up_write(&pmd
->root_lock
);
1731 int dm_pool_commit_metadata(struct dm_pool_metadata
*pmd
)
1735 down_write(&pmd
->root_lock
);
1739 r
= __commit_transaction(pmd
);
1744 * Open the next transaction.
1746 r
= __begin_transaction(pmd
);
1748 up_write(&pmd
->root_lock
);
1752 static void __set_abort_with_changes_flags(struct dm_pool_metadata
*pmd
)
1754 struct dm_thin_device
*td
;
1756 list_for_each_entry(td
, &pmd
->thin_devices
, list
)
1757 td
->aborted_with_changes
= td
->changed
;
1760 int dm_pool_abort_metadata(struct dm_pool_metadata
*pmd
)
1764 down_write(&pmd
->root_lock
);
1768 __set_abort_with_changes_flags(pmd
);
1769 __destroy_persistent_data_objects(pmd
);
1770 r
= __create_persistent_data_objects(pmd
, false);
1772 pmd
->fail_io
= true;
1775 up_write(&pmd
->root_lock
);
1780 int dm_pool_get_free_block_count(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1784 down_read(&pmd
->root_lock
);
1786 r
= dm_sm_get_nr_free(pmd
->data_sm
, result
);
1787 up_read(&pmd
->root_lock
);
1792 int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata
*pmd
,
1797 down_read(&pmd
->root_lock
);
1799 r
= dm_sm_get_nr_free(pmd
->metadata_sm
, result
);
1800 up_read(&pmd
->root_lock
);
1805 int dm_pool_get_metadata_dev_size(struct dm_pool_metadata
*pmd
,
1810 down_read(&pmd
->root_lock
);
1812 r
= dm_sm_get_nr_blocks(pmd
->metadata_sm
, result
);
1813 up_read(&pmd
->root_lock
);
1818 int dm_pool_get_data_dev_size(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1822 down_read(&pmd
->root_lock
);
1824 r
= dm_sm_get_nr_blocks(pmd
->data_sm
, result
);
1825 up_read(&pmd
->root_lock
);
1830 int dm_thin_get_mapped_count(struct dm_thin_device
*td
, dm_block_t
*result
)
1833 struct dm_pool_metadata
*pmd
= td
->pmd
;
1835 down_read(&pmd
->root_lock
);
1836 if (!pmd
->fail_io
) {
1837 *result
= td
->mapped_blocks
;
1840 up_read(&pmd
->root_lock
);
1845 static int __highest_block(struct dm_thin_device
*td
, dm_block_t
*result
)
1849 dm_block_t thin_root
;
1850 struct dm_pool_metadata
*pmd
= td
->pmd
;
1852 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, &td
->id
, &value_le
);
1856 thin_root
= le64_to_cpu(value_le
);
1858 return dm_btree_find_highest_key(&pmd
->bl_info
, thin_root
, result
);
1861 int dm_thin_get_highest_mapped_block(struct dm_thin_device
*td
,
1865 struct dm_pool_metadata
*pmd
= td
->pmd
;
1867 down_read(&pmd
->root_lock
);
1869 r
= __highest_block(td
, result
);
1870 up_read(&pmd
->root_lock
);
1875 static int __resize_space_map(struct dm_space_map
*sm
, dm_block_t new_count
)
1878 dm_block_t old_count
;
1880 r
= dm_sm_get_nr_blocks(sm
, &old_count
);
1884 if (new_count
== old_count
)
1887 if (new_count
< old_count
) {
1888 DMERR("cannot reduce size of space map");
1892 return dm_sm_extend(sm
, new_count
- old_count
);
1895 int dm_pool_resize_data_dev(struct dm_pool_metadata
*pmd
, dm_block_t new_count
)
1899 down_write(&pmd
->root_lock
);
1901 r
= __resize_space_map(pmd
->data_sm
, new_count
);
1902 up_write(&pmd
->root_lock
);
1907 int dm_pool_resize_metadata_dev(struct dm_pool_metadata
*pmd
, dm_block_t new_count
)
1911 down_write(&pmd
->root_lock
);
1913 r
= __resize_space_map(pmd
->metadata_sm
, new_count
);
1914 up_write(&pmd
->root_lock
);
1919 void dm_pool_metadata_read_only(struct dm_pool_metadata
*pmd
)
1921 down_write(&pmd
->root_lock
);
1922 dm_bm_set_read_only(pmd
->bm
);
1923 up_write(&pmd
->root_lock
);
1926 void dm_pool_metadata_read_write(struct dm_pool_metadata
*pmd
)
1928 down_write(&pmd
->root_lock
);
1929 dm_bm_set_read_write(pmd
->bm
);
1930 up_write(&pmd
->root_lock
);
1933 int dm_pool_register_metadata_threshold(struct dm_pool_metadata
*pmd
,
1934 dm_block_t threshold
,
1935 dm_sm_threshold_fn fn
,
1940 down_write(&pmd
->root_lock
);
1941 r
= dm_sm_register_threshold_callback(pmd
->metadata_sm
, threshold
, fn
, context
);
1942 up_write(&pmd
->root_lock
);
1947 int dm_pool_metadata_set_needs_check(struct dm_pool_metadata
*pmd
)
1950 struct dm_block
*sblock
;
1951 struct thin_disk_superblock
*disk_super
;
1953 down_write(&pmd
->root_lock
);
1954 pmd
->flags
|= THIN_METADATA_NEEDS_CHECK_FLAG
;
1956 r
= superblock_lock(pmd
, &sblock
);
1958 DMERR("couldn't read superblock");
1962 disk_super
= dm_block_data(sblock
);
1963 disk_super
->flags
= cpu_to_le32(pmd
->flags
);
1965 dm_bm_unlock(sblock
);
1967 up_write(&pmd
->root_lock
);
1971 bool dm_pool_metadata_needs_check(struct dm_pool_metadata
*pmd
)
1975 down_read(&pmd
->root_lock
);
1976 needs_check
= pmd
->flags
& THIN_METADATA_NEEDS_CHECK_FLAG
;
1977 up_read(&pmd
->root_lock
);
1982 void dm_pool_issue_prefetches(struct dm_pool_metadata
*pmd
)
1984 down_read(&pmd
->root_lock
);
1986 dm_tm_issue_prefetches(pmd
->tm
);
1987 up_read(&pmd
->root_lock
);