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
85 * 3 for btree insert +
86 * 2 for btree lookup used within space map
88 * 2 for shadow spine +
89 * 4 for rebalance 3 child node
91 #define THIN_MAX_CONCURRENT_LOCKS 6
93 /* This should be plenty */
94 #define SPACE_MAP_ROOT_SIZE 128
97 * Little endian on-disk superblock and device details.
99 struct thin_disk_superblock
{
100 __le32 csum
; /* Checksum of superblock except for this field. */
102 __le64 blocknr
; /* This block number, dm_block_t. */
112 * Root held by userspace transactions.
116 __u8 data_space_map_root
[SPACE_MAP_ROOT_SIZE
];
117 __u8 metadata_space_map_root
[SPACE_MAP_ROOT_SIZE
];
120 * 2-level btree mapping (dev_id, (dev block, time)) -> data block
122 __le64 data_mapping_root
;
125 * Device detail root mapping dev_id -> device_details
127 __le64 device_details_root
;
129 __le32 data_block_size
; /* In 512-byte sectors. */
131 __le32 metadata_block_size
; /* In 512-byte sectors. */
132 __le64 metadata_nr_blocks
;
135 __le32 compat_ro_flags
;
136 __le32 incompat_flags
;
139 struct disk_device_details
{
140 __le64 mapped_blocks
;
141 __le64 transaction_id
; /* When created. */
142 __le32 creation_time
;
143 __le32 snapshotted_time
;
146 struct dm_pool_metadata
{
147 struct hlist_node hash
;
149 struct block_device
*bdev
;
150 struct dm_block_manager
*bm
;
151 struct dm_space_map
*metadata_sm
;
152 struct dm_space_map
*data_sm
;
153 struct dm_transaction_manager
*tm
;
154 struct dm_transaction_manager
*nb_tm
;
158 * First level holds thin_dev_t.
159 * Second level holds mappings.
161 struct dm_btree_info info
;
164 * Non-blocking version of the above.
166 struct dm_btree_info nb_info
;
169 * Just the top level for deleting whole devices.
171 struct dm_btree_info tl_info
;
174 * Just the bottom level for creating new devices.
176 struct dm_btree_info bl_info
;
179 * Describes the device details btree.
181 struct dm_btree_info details_info
;
183 struct rw_semaphore root_lock
;
186 dm_block_t details_root
;
187 struct list_head thin_devices
;
190 sector_t data_block_size
;
193 * We reserve a section of the metadata for commit overhead.
194 * All reported space does *not* include this.
196 dm_block_t metadata_reserve
;
199 * Set if a transaction has to be aborted but the attempt to roll back
200 * to the previous (good) transaction failed. The only pool metadata
201 * operation possible in this state is the closing of the device.
206 * Reading the space map roots can fail, so we read it into these
207 * buffers before the superblock is locked and updated.
209 __u8 data_space_map_root
[SPACE_MAP_ROOT_SIZE
];
210 __u8 metadata_space_map_root
[SPACE_MAP_ROOT_SIZE
];
213 struct dm_thin_device
{
214 struct list_head list
;
215 struct dm_pool_metadata
*pmd
;
220 bool aborted_with_changes
:1;
221 uint64_t mapped_blocks
;
222 uint64_t transaction_id
;
223 uint32_t creation_time
;
224 uint32_t snapshotted_time
;
227 /*----------------------------------------------------------------
228 * superblock validator
229 *--------------------------------------------------------------*/
231 #define SUPERBLOCK_CSUM_XOR 160774
233 static void sb_prepare_for_write(struct dm_block_validator
*v
,
237 struct thin_disk_superblock
*disk_super
= dm_block_data(b
);
239 disk_super
->blocknr
= cpu_to_le64(dm_block_location(b
));
240 disk_super
->csum
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
241 block_size
- sizeof(__le32
),
242 SUPERBLOCK_CSUM_XOR
));
245 static int sb_check(struct dm_block_validator
*v
,
249 struct thin_disk_superblock
*disk_super
= dm_block_data(b
);
252 if (dm_block_location(b
) != le64_to_cpu(disk_super
->blocknr
)) {
253 DMERR("sb_check failed: blocknr %llu: "
254 "wanted %llu", le64_to_cpu(disk_super
->blocknr
),
255 (unsigned long long)dm_block_location(b
));
259 if (le64_to_cpu(disk_super
->magic
) != THIN_SUPERBLOCK_MAGIC
) {
260 DMERR("sb_check failed: magic %llu: "
261 "wanted %llu", le64_to_cpu(disk_super
->magic
),
262 (unsigned long long)THIN_SUPERBLOCK_MAGIC
);
266 csum_le
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
267 block_size
- sizeof(__le32
),
268 SUPERBLOCK_CSUM_XOR
));
269 if (csum_le
!= disk_super
->csum
) {
270 DMERR("sb_check failed: csum %u: wanted %u",
271 le32_to_cpu(csum_le
), le32_to_cpu(disk_super
->csum
));
278 static struct dm_block_validator sb_validator
= {
279 .name
= "superblock",
280 .prepare_for_write
= sb_prepare_for_write
,
284 /*----------------------------------------------------------------
285 * Methods for the btree value types
286 *--------------------------------------------------------------*/
288 static uint64_t pack_block_time(dm_block_t b
, uint32_t t
)
290 return (b
<< 24) | t
;
293 static void unpack_block_time(uint64_t v
, dm_block_t
*b
, uint32_t *t
)
296 *t
= v
& ((1 << 24) - 1);
299 static void data_block_inc(void *context
, const void *value_le
)
301 struct dm_space_map
*sm
= context
;
306 memcpy(&v_le
, value_le
, sizeof(v_le
));
307 unpack_block_time(le64_to_cpu(v_le
), &b
, &t
);
308 dm_sm_inc_block(sm
, b
);
311 static void data_block_dec(void *context
, const void *value_le
)
313 struct dm_space_map
*sm
= context
;
318 memcpy(&v_le
, value_le
, sizeof(v_le
));
319 unpack_block_time(le64_to_cpu(v_le
), &b
, &t
);
320 dm_sm_dec_block(sm
, b
);
323 static int data_block_equal(void *context
, const void *value1_le
, const void *value2_le
)
329 memcpy(&v1_le
, value1_le
, sizeof(v1_le
));
330 memcpy(&v2_le
, value2_le
, sizeof(v2_le
));
331 unpack_block_time(le64_to_cpu(v1_le
), &b1
, &t
);
332 unpack_block_time(le64_to_cpu(v2_le
), &b2
, &t
);
337 static void subtree_inc(void *context
, const void *value
)
339 struct dm_btree_info
*info
= context
;
343 memcpy(&root_le
, value
, sizeof(root_le
));
344 root
= le64_to_cpu(root_le
);
345 dm_tm_inc(info
->tm
, root
);
348 static void subtree_dec(void *context
, const void *value
)
350 struct dm_btree_info
*info
= context
;
354 memcpy(&root_le
, value
, sizeof(root_le
));
355 root
= le64_to_cpu(root_le
);
356 if (dm_btree_del(info
, root
))
357 DMERR("btree delete failed\n");
360 static int subtree_equal(void *context
, const void *value1_le
, const void *value2_le
)
363 memcpy(&v1_le
, value1_le
, sizeof(v1_le
));
364 memcpy(&v2_le
, value2_le
, sizeof(v2_le
));
366 return v1_le
== v2_le
;
369 /*----------------------------------------------------------------*/
371 static int superblock_lock_zero(struct dm_pool_metadata
*pmd
,
372 struct dm_block
**sblock
)
374 return dm_bm_write_lock_zero(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
375 &sb_validator
, sblock
);
378 static int superblock_lock(struct dm_pool_metadata
*pmd
,
379 struct dm_block
**sblock
)
381 return dm_bm_write_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
382 &sb_validator
, sblock
);
385 static int __superblock_all_zeroes(struct dm_block_manager
*bm
, int *result
)
390 __le64
*data_le
, zero
= cpu_to_le64(0);
391 unsigned block_size
= dm_bm_block_size(bm
) / sizeof(__le64
);
394 * We can't use a validator here - it may be all zeroes.
396 r
= dm_bm_read_lock(bm
, THIN_SUPERBLOCK_LOCATION
, NULL
, &b
);
400 data_le
= dm_block_data(b
);
402 for (i
= 0; i
< block_size
; i
++) {
403 if (data_le
[i
] != zero
) {
414 static void __setup_btree_details(struct dm_pool_metadata
*pmd
)
416 pmd
->info
.tm
= pmd
->tm
;
417 pmd
->info
.levels
= 2;
418 pmd
->info
.value_type
.context
= pmd
->data_sm
;
419 pmd
->info
.value_type
.size
= sizeof(__le64
);
420 pmd
->info
.value_type
.inc
= data_block_inc
;
421 pmd
->info
.value_type
.dec
= data_block_dec
;
422 pmd
->info
.value_type
.equal
= data_block_equal
;
424 memcpy(&pmd
->nb_info
, &pmd
->info
, sizeof(pmd
->nb_info
));
425 pmd
->nb_info
.tm
= pmd
->nb_tm
;
427 pmd
->tl_info
.tm
= pmd
->tm
;
428 pmd
->tl_info
.levels
= 1;
429 pmd
->tl_info
.value_type
.context
= &pmd
->bl_info
;
430 pmd
->tl_info
.value_type
.size
= sizeof(__le64
);
431 pmd
->tl_info
.value_type
.inc
= subtree_inc
;
432 pmd
->tl_info
.value_type
.dec
= subtree_dec
;
433 pmd
->tl_info
.value_type
.equal
= subtree_equal
;
435 pmd
->bl_info
.tm
= pmd
->tm
;
436 pmd
->bl_info
.levels
= 1;
437 pmd
->bl_info
.value_type
.context
= pmd
->data_sm
;
438 pmd
->bl_info
.value_type
.size
= sizeof(__le64
);
439 pmd
->bl_info
.value_type
.inc
= data_block_inc
;
440 pmd
->bl_info
.value_type
.dec
= data_block_dec
;
441 pmd
->bl_info
.value_type
.equal
= data_block_equal
;
443 pmd
->details_info
.tm
= pmd
->tm
;
444 pmd
->details_info
.levels
= 1;
445 pmd
->details_info
.value_type
.context
= NULL
;
446 pmd
->details_info
.value_type
.size
= sizeof(struct disk_device_details
);
447 pmd
->details_info
.value_type
.inc
= NULL
;
448 pmd
->details_info
.value_type
.dec
= NULL
;
449 pmd
->details_info
.value_type
.equal
= NULL
;
452 static int save_sm_roots(struct dm_pool_metadata
*pmd
)
457 r
= dm_sm_root_size(pmd
->metadata_sm
, &len
);
461 r
= dm_sm_copy_root(pmd
->metadata_sm
, &pmd
->metadata_space_map_root
, len
);
465 r
= dm_sm_root_size(pmd
->data_sm
, &len
);
469 return dm_sm_copy_root(pmd
->data_sm
, &pmd
->data_space_map_root
, len
);
472 static void copy_sm_roots(struct dm_pool_metadata
*pmd
,
473 struct thin_disk_superblock
*disk
)
475 memcpy(&disk
->metadata_space_map_root
,
476 &pmd
->metadata_space_map_root
,
477 sizeof(pmd
->metadata_space_map_root
));
479 memcpy(&disk
->data_space_map_root
,
480 &pmd
->data_space_map_root
,
481 sizeof(pmd
->data_space_map_root
));
484 static int __write_initial_superblock(struct dm_pool_metadata
*pmd
)
487 struct dm_block
*sblock
;
488 struct thin_disk_superblock
*disk_super
;
489 sector_t bdev_size
= i_size_read(pmd
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
491 if (bdev_size
> THIN_METADATA_MAX_SECTORS
)
492 bdev_size
= THIN_METADATA_MAX_SECTORS
;
494 r
= dm_sm_commit(pmd
->data_sm
);
498 r
= dm_tm_pre_commit(pmd
->tm
);
502 r
= save_sm_roots(pmd
);
506 r
= superblock_lock_zero(pmd
, &sblock
);
510 disk_super
= dm_block_data(sblock
);
511 disk_super
->flags
= 0;
512 memset(disk_super
->uuid
, 0, sizeof(disk_super
->uuid
));
513 disk_super
->magic
= cpu_to_le64(THIN_SUPERBLOCK_MAGIC
);
514 disk_super
->version
= cpu_to_le32(THIN_VERSION
);
515 disk_super
->time
= 0;
516 disk_super
->trans_id
= 0;
517 disk_super
->held_root
= 0;
519 copy_sm_roots(pmd
, disk_super
);
521 disk_super
->data_mapping_root
= cpu_to_le64(pmd
->root
);
522 disk_super
->device_details_root
= cpu_to_le64(pmd
->details_root
);
523 disk_super
->metadata_block_size
= cpu_to_le32(THIN_METADATA_BLOCK_SIZE
);
524 disk_super
->metadata_nr_blocks
= cpu_to_le64(bdev_size
>> SECTOR_TO_BLOCK_SHIFT
);
525 disk_super
->data_block_size
= cpu_to_le32(pmd
->data_block_size
);
527 return dm_tm_commit(pmd
->tm
, sblock
);
530 static int __format_metadata(struct dm_pool_metadata
*pmd
)
534 r
= dm_tm_create_with_sm(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
535 &pmd
->tm
, &pmd
->metadata_sm
);
537 DMERR("tm_create_with_sm failed");
541 pmd
->data_sm
= dm_sm_disk_create(pmd
->tm
, 0);
542 if (IS_ERR(pmd
->data_sm
)) {
543 DMERR("sm_disk_create failed");
544 r
= PTR_ERR(pmd
->data_sm
);
548 pmd
->nb_tm
= dm_tm_create_non_blocking_clone(pmd
->tm
);
550 DMERR("could not create non-blocking clone tm");
552 goto bad_cleanup_data_sm
;
555 __setup_btree_details(pmd
);
557 r
= dm_btree_empty(&pmd
->info
, &pmd
->root
);
559 goto bad_cleanup_nb_tm
;
561 r
= dm_btree_empty(&pmd
->details_info
, &pmd
->details_root
);
563 DMERR("couldn't create devices root");
564 goto bad_cleanup_nb_tm
;
567 r
= __write_initial_superblock(pmd
);
569 goto bad_cleanup_nb_tm
;
574 dm_tm_destroy(pmd
->nb_tm
);
576 dm_sm_destroy(pmd
->data_sm
);
578 dm_tm_destroy(pmd
->tm
);
579 dm_sm_destroy(pmd
->metadata_sm
);
584 static int __check_incompat_features(struct thin_disk_superblock
*disk_super
,
585 struct dm_pool_metadata
*pmd
)
589 features
= le32_to_cpu(disk_super
->incompat_flags
) & ~THIN_FEATURE_INCOMPAT_SUPP
;
591 DMERR("could not access metadata due to unsupported optional features (%lx).",
592 (unsigned long)features
);
597 * Check for read-only metadata to skip the following RDWR checks.
599 if (get_disk_ro(pmd
->bdev
->bd_disk
))
602 features
= le32_to_cpu(disk_super
->compat_ro_flags
) & ~THIN_FEATURE_COMPAT_RO_SUPP
;
604 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
605 (unsigned long)features
);
612 static int __open_metadata(struct dm_pool_metadata
*pmd
)
615 struct dm_block
*sblock
;
616 struct thin_disk_superblock
*disk_super
;
618 r
= dm_bm_read_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
619 &sb_validator
, &sblock
);
621 DMERR("couldn't read superblock");
625 disk_super
= dm_block_data(sblock
);
627 /* Verify the data block size hasn't changed */
628 if (le32_to_cpu(disk_super
->data_block_size
) != pmd
->data_block_size
) {
629 DMERR("changing the data block size (from %u to %llu) is not supported",
630 le32_to_cpu(disk_super
->data_block_size
),
631 (unsigned long long)pmd
->data_block_size
);
633 goto bad_unlock_sblock
;
636 r
= __check_incompat_features(disk_super
, pmd
);
638 goto bad_unlock_sblock
;
640 r
= dm_tm_open_with_sm(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
641 disk_super
->metadata_space_map_root
,
642 sizeof(disk_super
->metadata_space_map_root
),
643 &pmd
->tm
, &pmd
->metadata_sm
);
645 DMERR("tm_open_with_sm failed");
646 goto bad_unlock_sblock
;
649 pmd
->data_sm
= dm_sm_disk_open(pmd
->tm
, disk_super
->data_space_map_root
,
650 sizeof(disk_super
->data_space_map_root
));
651 if (IS_ERR(pmd
->data_sm
)) {
652 DMERR("sm_disk_open failed");
653 r
= PTR_ERR(pmd
->data_sm
);
657 pmd
->nb_tm
= dm_tm_create_non_blocking_clone(pmd
->tm
);
659 DMERR("could not create non-blocking clone tm");
661 goto bad_cleanup_data_sm
;
664 __setup_btree_details(pmd
);
665 dm_bm_unlock(sblock
);
670 dm_sm_destroy(pmd
->data_sm
);
672 dm_tm_destroy(pmd
->tm
);
673 dm_sm_destroy(pmd
->metadata_sm
);
675 dm_bm_unlock(sblock
);
680 static int __open_or_format_metadata(struct dm_pool_metadata
*pmd
, bool format_device
)
684 r
= __superblock_all_zeroes(pmd
->bm
, &unformatted
);
689 return format_device
? __format_metadata(pmd
) : -EPERM
;
691 return __open_metadata(pmd
);
694 static int __create_persistent_data_objects(struct dm_pool_metadata
*pmd
, bool format_device
)
698 pmd
->bm
= dm_block_manager_create(pmd
->bdev
, THIN_METADATA_BLOCK_SIZE
<< SECTOR_SHIFT
,
699 THIN_METADATA_CACHE_SIZE
,
700 THIN_MAX_CONCURRENT_LOCKS
);
701 if (IS_ERR(pmd
->bm
)) {
702 DMERR("could not create block manager");
703 return PTR_ERR(pmd
->bm
);
706 r
= __open_or_format_metadata(pmd
, format_device
);
708 dm_block_manager_destroy(pmd
->bm
);
713 static void __destroy_persistent_data_objects(struct dm_pool_metadata
*pmd
)
715 dm_sm_destroy(pmd
->data_sm
);
716 dm_sm_destroy(pmd
->metadata_sm
);
717 dm_tm_destroy(pmd
->nb_tm
);
718 dm_tm_destroy(pmd
->tm
);
719 dm_block_manager_destroy(pmd
->bm
);
722 static int __begin_transaction(struct dm_pool_metadata
*pmd
)
725 struct thin_disk_superblock
*disk_super
;
726 struct dm_block
*sblock
;
729 * We re-read the superblock every time. Shouldn't need to do this
732 r
= dm_bm_read_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
733 &sb_validator
, &sblock
);
737 disk_super
= dm_block_data(sblock
);
738 pmd
->time
= le32_to_cpu(disk_super
->time
);
739 pmd
->root
= le64_to_cpu(disk_super
->data_mapping_root
);
740 pmd
->details_root
= le64_to_cpu(disk_super
->device_details_root
);
741 pmd
->trans_id
= le64_to_cpu(disk_super
->trans_id
);
742 pmd
->flags
= le32_to_cpu(disk_super
->flags
);
743 pmd
->data_block_size
= le32_to_cpu(disk_super
->data_block_size
);
745 dm_bm_unlock(sblock
);
749 static int __write_changed_details(struct dm_pool_metadata
*pmd
)
752 struct dm_thin_device
*td
, *tmp
;
753 struct disk_device_details details
;
756 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
762 details
.mapped_blocks
= cpu_to_le64(td
->mapped_blocks
);
763 details
.transaction_id
= cpu_to_le64(td
->transaction_id
);
764 details
.creation_time
= cpu_to_le32(td
->creation_time
);
765 details
.snapshotted_time
= cpu_to_le32(td
->snapshotted_time
);
766 __dm_bless_for_disk(&details
);
768 r
= dm_btree_insert(&pmd
->details_info
, pmd
->details_root
,
769 &key
, &details
, &pmd
->details_root
);
784 static int __commit_transaction(struct dm_pool_metadata
*pmd
)
787 size_t metadata_len
, data_len
;
788 struct thin_disk_superblock
*disk_super
;
789 struct dm_block
*sblock
;
792 * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
794 BUILD_BUG_ON(sizeof(struct thin_disk_superblock
) > 512);
796 r
= __write_changed_details(pmd
);
800 r
= dm_sm_commit(pmd
->data_sm
);
804 r
= dm_tm_pre_commit(pmd
->tm
);
808 r
= dm_sm_root_size(pmd
->metadata_sm
, &metadata_len
);
812 r
= dm_sm_root_size(pmd
->data_sm
, &data_len
);
816 r
= save_sm_roots(pmd
);
820 r
= superblock_lock(pmd
, &sblock
);
824 disk_super
= dm_block_data(sblock
);
825 disk_super
->time
= cpu_to_le32(pmd
->time
);
826 disk_super
->data_mapping_root
= cpu_to_le64(pmd
->root
);
827 disk_super
->device_details_root
= cpu_to_le64(pmd
->details_root
);
828 disk_super
->trans_id
= cpu_to_le64(pmd
->trans_id
);
829 disk_super
->flags
= cpu_to_le32(pmd
->flags
);
831 copy_sm_roots(pmd
, disk_super
);
833 return dm_tm_commit(pmd
->tm
, sblock
);
836 static void __set_metadata_reserve(struct dm_pool_metadata
*pmd
)
840 dm_block_t max_blocks
= 4096; /* 16M */
842 r
= dm_sm_get_nr_blocks(pmd
->metadata_sm
, &total
);
844 DMERR("could not get size of metadata device");
845 pmd
->metadata_reserve
= max_blocks
;
847 pmd
->metadata_reserve
= min(max_blocks
, div_u64(total
, 10));
850 struct dm_pool_metadata
*dm_pool_metadata_open(struct block_device
*bdev
,
851 sector_t data_block_size
,
855 struct dm_pool_metadata
*pmd
;
857 pmd
= kmalloc(sizeof(*pmd
), GFP_KERNEL
);
859 DMERR("could not allocate metadata struct");
860 return ERR_PTR(-ENOMEM
);
863 init_rwsem(&pmd
->root_lock
);
865 INIT_LIST_HEAD(&pmd
->thin_devices
);
866 pmd
->fail_io
= false;
868 pmd
->data_block_size
= data_block_size
;
870 r
= __create_persistent_data_objects(pmd
, format_device
);
876 r
= __begin_transaction(pmd
);
878 if (dm_pool_metadata_close(pmd
) < 0)
879 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
883 __set_metadata_reserve(pmd
);
888 int dm_pool_metadata_close(struct dm_pool_metadata
*pmd
)
891 unsigned open_devices
= 0;
892 struct dm_thin_device
*td
, *tmp
;
894 down_read(&pmd
->root_lock
);
895 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
903 up_read(&pmd
->root_lock
);
906 DMERR("attempt to close pmd when %u device(s) are still open",
911 if (!dm_bm_is_read_only(pmd
->bm
) && !pmd
->fail_io
) {
912 r
= __commit_transaction(pmd
);
914 DMWARN("%s: __commit_transaction() failed, error = %d",
919 __destroy_persistent_data_objects(pmd
);
926 * __open_device: Returns @td corresponding to device with id @dev,
927 * creating it if @create is set and incrementing @td->open_count.
928 * On failure, @td is undefined.
930 static int __open_device(struct dm_pool_metadata
*pmd
,
931 dm_thin_id dev
, int create
,
932 struct dm_thin_device
**td
)
935 struct dm_thin_device
*td2
;
937 struct disk_device_details details_le
;
940 * If the device is already open, return it.
942 list_for_each_entry(td2
, &pmd
->thin_devices
, list
)
943 if (td2
->id
== dev
) {
945 * May not create an already-open device.
956 * Check the device exists.
958 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
961 if (r
!= -ENODATA
|| !create
)
968 details_le
.mapped_blocks
= 0;
969 details_le
.transaction_id
= cpu_to_le64(pmd
->trans_id
);
970 details_le
.creation_time
= cpu_to_le32(pmd
->time
);
971 details_le
.snapshotted_time
= cpu_to_le32(pmd
->time
);
974 *td
= kmalloc(sizeof(**td
), GFP_NOIO
);
980 (*td
)->open_count
= 1;
981 (*td
)->changed
= changed
;
982 (*td
)->aborted_with_changes
= false;
983 (*td
)->mapped_blocks
= le64_to_cpu(details_le
.mapped_blocks
);
984 (*td
)->transaction_id
= le64_to_cpu(details_le
.transaction_id
);
985 (*td
)->creation_time
= le32_to_cpu(details_le
.creation_time
);
986 (*td
)->snapshotted_time
= le32_to_cpu(details_le
.snapshotted_time
);
988 list_add(&(*td
)->list
, &pmd
->thin_devices
);
993 static void __close_device(struct dm_thin_device
*td
)
998 static int __create_thin(struct dm_pool_metadata
*pmd
,
1002 dm_block_t dev_root
;
1004 struct disk_device_details details_le
;
1005 struct dm_thin_device
*td
;
1008 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
1014 * Create an empty btree for the mappings.
1016 r
= dm_btree_empty(&pmd
->bl_info
, &dev_root
);
1021 * Insert it into the main mapping tree.
1023 value
= cpu_to_le64(dev_root
);
1024 __dm_bless_for_disk(&value
);
1025 r
= dm_btree_insert(&pmd
->tl_info
, pmd
->root
, &key
, &value
, &pmd
->root
);
1027 dm_btree_del(&pmd
->bl_info
, dev_root
);
1031 r
= __open_device(pmd
, dev
, 1, &td
);
1033 dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
1034 dm_btree_del(&pmd
->bl_info
, dev_root
);
1042 int dm_pool_create_thin(struct dm_pool_metadata
*pmd
, dm_thin_id dev
)
1046 down_write(&pmd
->root_lock
);
1048 r
= __create_thin(pmd
, dev
);
1049 up_write(&pmd
->root_lock
);
1054 static int __set_snapshot_details(struct dm_pool_metadata
*pmd
,
1055 struct dm_thin_device
*snap
,
1056 dm_thin_id origin
, uint32_t time
)
1059 struct dm_thin_device
*td
;
1061 r
= __open_device(pmd
, origin
, 0, &td
);
1066 td
->snapshotted_time
= time
;
1068 snap
->mapped_blocks
= td
->mapped_blocks
;
1069 snap
->snapshotted_time
= time
;
1075 static int __create_snap(struct dm_pool_metadata
*pmd
,
1076 dm_thin_id dev
, dm_thin_id origin
)
1079 dm_block_t origin_root
;
1080 uint64_t key
= origin
, dev_key
= dev
;
1081 struct dm_thin_device
*td
;
1082 struct disk_device_details details_le
;
1085 /* check this device is unused */
1086 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
1087 &dev_key
, &details_le
);
1091 /* find the mapping tree for the origin */
1092 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, &key
, &value
);
1095 origin_root
= le64_to_cpu(value
);
1097 /* clone the origin, an inc will do */
1098 dm_tm_inc(pmd
->tm
, origin_root
);
1100 /* insert into the main mapping tree */
1101 value
= cpu_to_le64(origin_root
);
1102 __dm_bless_for_disk(&value
);
1104 r
= dm_btree_insert(&pmd
->tl_info
, pmd
->root
, &key
, &value
, &pmd
->root
);
1106 dm_tm_dec(pmd
->tm
, origin_root
);
1112 r
= __open_device(pmd
, dev
, 1, &td
);
1116 r
= __set_snapshot_details(pmd
, td
, origin
, pmd
->time
);
1125 dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
1126 dm_btree_remove(&pmd
->details_info
, pmd
->details_root
,
1127 &key
, &pmd
->details_root
);
1131 int dm_pool_create_snap(struct dm_pool_metadata
*pmd
,
1137 down_write(&pmd
->root_lock
);
1139 r
= __create_snap(pmd
, dev
, origin
);
1140 up_write(&pmd
->root_lock
);
1145 static int __delete_device(struct dm_pool_metadata
*pmd
, dm_thin_id dev
)
1149 struct dm_thin_device
*td
;
1151 /* TODO: failure should mark the transaction invalid */
1152 r
= __open_device(pmd
, dev
, 0, &td
);
1156 if (td
->open_count
> 1) {
1161 list_del(&td
->list
);
1163 r
= dm_btree_remove(&pmd
->details_info
, pmd
->details_root
,
1164 &key
, &pmd
->details_root
);
1168 r
= dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
1175 int dm_pool_delete_thin_device(struct dm_pool_metadata
*pmd
,
1180 down_write(&pmd
->root_lock
);
1182 r
= __delete_device(pmd
, dev
);
1183 up_write(&pmd
->root_lock
);
1188 int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata
*pmd
,
1189 uint64_t current_id
,
1194 down_write(&pmd
->root_lock
);
1199 if (pmd
->trans_id
!= current_id
) {
1200 DMERR("mismatched transaction id");
1204 pmd
->trans_id
= new_id
;
1208 up_write(&pmd
->root_lock
);
1213 int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata
*pmd
,
1218 down_read(&pmd
->root_lock
);
1219 if (!pmd
->fail_io
) {
1220 *result
= pmd
->trans_id
;
1223 up_read(&pmd
->root_lock
);
1228 static int __reserve_metadata_snap(struct dm_pool_metadata
*pmd
)
1231 struct thin_disk_superblock
*disk_super
;
1232 struct dm_block
*copy
, *sblock
;
1233 dm_block_t held_root
;
1236 * We commit to ensure the btree roots which we increment in a
1237 * moment are up to date.
1239 __commit_transaction(pmd
);
1242 * Copy the superblock.
1244 dm_sm_inc_block(pmd
->metadata_sm
, THIN_SUPERBLOCK_LOCATION
);
1245 r
= dm_tm_shadow_block(pmd
->tm
, THIN_SUPERBLOCK_LOCATION
,
1246 &sb_validator
, ©
, &inc
);
1252 held_root
= dm_block_location(copy
);
1253 disk_super
= dm_block_data(copy
);
1255 if (le64_to_cpu(disk_super
->held_root
)) {
1256 DMWARN("Pool metadata snapshot already exists: release this before taking another.");
1258 dm_tm_dec(pmd
->tm
, held_root
);
1259 dm_tm_unlock(pmd
->tm
, copy
);
1264 * Wipe the spacemap since we're not publishing this.
1266 memset(&disk_super
->data_space_map_root
, 0,
1267 sizeof(disk_super
->data_space_map_root
));
1268 memset(&disk_super
->metadata_space_map_root
, 0,
1269 sizeof(disk_super
->metadata_space_map_root
));
1272 * Increment the data structures that need to be preserved.
1274 dm_tm_inc(pmd
->tm
, le64_to_cpu(disk_super
->data_mapping_root
));
1275 dm_tm_inc(pmd
->tm
, le64_to_cpu(disk_super
->device_details_root
));
1276 dm_tm_unlock(pmd
->tm
, copy
);
1279 * Write the held root into the superblock.
1281 r
= superblock_lock(pmd
, &sblock
);
1283 dm_tm_dec(pmd
->tm
, held_root
);
1287 disk_super
= dm_block_data(sblock
);
1288 disk_super
->held_root
= cpu_to_le64(held_root
);
1289 dm_bm_unlock(sblock
);
1293 int dm_pool_reserve_metadata_snap(struct dm_pool_metadata
*pmd
)
1297 down_write(&pmd
->root_lock
);
1299 r
= __reserve_metadata_snap(pmd
);
1300 up_write(&pmd
->root_lock
);
1305 static int __release_metadata_snap(struct dm_pool_metadata
*pmd
)
1308 struct thin_disk_superblock
*disk_super
;
1309 struct dm_block
*sblock
, *copy
;
1310 dm_block_t held_root
;
1312 r
= superblock_lock(pmd
, &sblock
);
1316 disk_super
= dm_block_data(sblock
);
1317 held_root
= le64_to_cpu(disk_super
->held_root
);
1318 disk_super
->held_root
= cpu_to_le64(0);
1320 dm_bm_unlock(sblock
);
1323 DMWARN("No pool metadata snapshot found: nothing to release.");
1327 r
= dm_tm_read_lock(pmd
->tm
, held_root
, &sb_validator
, ©
);
1331 disk_super
= dm_block_data(copy
);
1332 dm_btree_del(&pmd
->info
, le64_to_cpu(disk_super
->data_mapping_root
));
1333 dm_btree_del(&pmd
->details_info
, le64_to_cpu(disk_super
->device_details_root
));
1334 dm_sm_dec_block(pmd
->metadata_sm
, held_root
);
1336 dm_tm_unlock(pmd
->tm
, copy
);
1341 int dm_pool_release_metadata_snap(struct dm_pool_metadata
*pmd
)
1345 down_write(&pmd
->root_lock
);
1347 r
= __release_metadata_snap(pmd
);
1348 up_write(&pmd
->root_lock
);
1353 static int __get_metadata_snap(struct dm_pool_metadata
*pmd
,
1357 struct thin_disk_superblock
*disk_super
;
1358 struct dm_block
*sblock
;
1360 r
= dm_bm_read_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
1361 &sb_validator
, &sblock
);
1365 disk_super
= dm_block_data(sblock
);
1366 *result
= le64_to_cpu(disk_super
->held_root
);
1368 dm_bm_unlock(sblock
);
1373 int dm_pool_get_metadata_snap(struct dm_pool_metadata
*pmd
,
1378 down_read(&pmd
->root_lock
);
1380 r
= __get_metadata_snap(pmd
, result
);
1381 up_read(&pmd
->root_lock
);
1386 int dm_pool_open_thin_device(struct dm_pool_metadata
*pmd
, dm_thin_id dev
,
1387 struct dm_thin_device
**td
)
1391 down_write(&pmd
->root_lock
);
1393 r
= __open_device(pmd
, dev
, 0, td
);
1394 up_write(&pmd
->root_lock
);
1399 int dm_pool_close_thin_device(struct dm_thin_device
*td
)
1401 down_write(&td
->pmd
->root_lock
);
1403 up_write(&td
->pmd
->root_lock
);
1408 dm_thin_id
dm_thin_dev_id(struct dm_thin_device
*td
)
1414 * Check whether @time (of block creation) is older than @td's last snapshot.
1415 * If so then the associated block is shared with the last snapshot device.
1416 * Any block on a device created *after* the device last got snapshotted is
1417 * necessarily not shared.
1419 static bool __snapshotted_since(struct dm_thin_device
*td
, uint32_t time
)
1421 return td
->snapshotted_time
> time
;
1424 int dm_thin_find_block(struct dm_thin_device
*td
, dm_block_t block
,
1425 int can_issue_io
, struct dm_thin_lookup_result
*result
)
1429 struct dm_pool_metadata
*pmd
= td
->pmd
;
1430 dm_block_t keys
[2] = { td
->id
, block
};
1431 struct dm_btree_info
*info
;
1433 down_read(&pmd
->root_lock
);
1435 up_read(&pmd
->root_lock
);
1442 info
= &pmd
->nb_info
;
1444 r
= dm_btree_lookup(info
, pmd
->root
, keys
, &value
);
1446 uint64_t block_time
= 0;
1447 dm_block_t exception_block
;
1448 uint32_t exception_time
;
1450 block_time
= le64_to_cpu(value
);
1451 unpack_block_time(block_time
, &exception_block
,
1453 result
->block
= exception_block
;
1454 result
->shared
= __snapshotted_since(td
, exception_time
);
1457 up_read(&pmd
->root_lock
);
1461 /* FIXME: write a more efficient one in btree */
1462 int dm_thin_find_mapped_range(struct dm_thin_device
*td
,
1463 dm_block_t begin
, dm_block_t end
,
1464 dm_block_t
*thin_begin
, dm_block_t
*thin_end
,
1465 dm_block_t
*pool_begin
, bool *maybe_shared
)
1468 dm_block_t pool_end
;
1469 struct dm_thin_lookup_result lookup
;
1475 * Find first mapped block.
1477 while (begin
< end
) {
1478 r
= dm_thin_find_block(td
, begin
, true, &lookup
);
1491 *thin_begin
= begin
;
1492 *pool_begin
= lookup
.block
;
1493 *maybe_shared
= lookup
.shared
;
1496 pool_end
= *pool_begin
+ 1;
1497 while (begin
!= end
) {
1498 r
= dm_thin_find_block(td
, begin
, true, &lookup
);
1506 if ((lookup
.block
!= pool_end
) ||
1507 (lookup
.shared
!= *maybe_shared
))
1518 static int __insert(struct dm_thin_device
*td
, dm_block_t block
,
1519 dm_block_t data_block
)
1523 struct dm_pool_metadata
*pmd
= td
->pmd
;
1524 dm_block_t keys
[2] = { td
->id
, block
};
1526 value
= cpu_to_le64(pack_block_time(data_block
, pmd
->time
));
1527 __dm_bless_for_disk(&value
);
1529 r
= dm_btree_insert_notify(&pmd
->info
, pmd
->root
, keys
, &value
,
1530 &pmd
->root
, &inserted
);
1536 td
->mapped_blocks
++;
1541 int dm_thin_insert_block(struct dm_thin_device
*td
, dm_block_t block
,
1542 dm_block_t data_block
)
1546 down_write(&td
->pmd
->root_lock
);
1547 if (!td
->pmd
->fail_io
)
1548 r
= __insert(td
, block
, data_block
);
1549 up_write(&td
->pmd
->root_lock
);
1554 static int __remove(struct dm_thin_device
*td
, dm_block_t block
)
1557 struct dm_pool_metadata
*pmd
= td
->pmd
;
1558 dm_block_t keys
[2] = { td
->id
, block
};
1560 r
= dm_btree_remove(&pmd
->info
, pmd
->root
, keys
, &pmd
->root
);
1564 td
->mapped_blocks
--;
1570 static int __remove_range(struct dm_thin_device
*td
, dm_block_t begin
, dm_block_t end
)
1573 unsigned count
, total_count
= 0;
1574 struct dm_pool_metadata
*pmd
= td
->pmd
;
1575 dm_block_t keys
[1] = { td
->id
};
1577 dm_block_t mapping_root
;
1580 * Find the mapping tree
1582 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, keys
, &value
);
1587 * Remove from the mapping tree, taking care to inc the
1588 * ref count so it doesn't get deleted.
1590 mapping_root
= le64_to_cpu(value
);
1591 dm_tm_inc(pmd
->tm
, mapping_root
);
1592 r
= dm_btree_remove(&pmd
->tl_info
, pmd
->root
, keys
, &pmd
->root
);
1597 * Remove leaves stops at the first unmapped entry, so we have to
1598 * loop round finding mapped ranges.
1600 while (begin
< end
) {
1601 r
= dm_btree_lookup_next(&pmd
->bl_info
, mapping_root
, &begin
, &begin
, &value
);
1611 r
= dm_btree_remove_leaves(&pmd
->bl_info
, mapping_root
, &begin
, end
, &mapping_root
, &count
);
1615 total_count
+= count
;
1618 td
->mapped_blocks
-= total_count
;
1622 * Reinsert the mapping tree.
1624 value
= cpu_to_le64(mapping_root
);
1625 __dm_bless_for_disk(&value
);
1626 return dm_btree_insert(&pmd
->tl_info
, pmd
->root
, keys
, &value
, &pmd
->root
);
1629 int dm_thin_remove_block(struct dm_thin_device
*td
, dm_block_t block
)
1633 down_write(&td
->pmd
->root_lock
);
1634 if (!td
->pmd
->fail_io
)
1635 r
= __remove(td
, block
);
1636 up_write(&td
->pmd
->root_lock
);
1641 int dm_thin_remove_range(struct dm_thin_device
*td
,
1642 dm_block_t begin
, dm_block_t end
)
1646 down_write(&td
->pmd
->root_lock
);
1647 if (!td
->pmd
->fail_io
)
1648 r
= __remove_range(td
, begin
, end
);
1649 up_write(&td
->pmd
->root_lock
);
1654 int dm_pool_block_is_used(struct dm_pool_metadata
*pmd
, dm_block_t b
, bool *result
)
1659 down_read(&pmd
->root_lock
);
1660 r
= dm_sm_get_count(pmd
->data_sm
, b
, &ref_count
);
1662 *result
= (ref_count
!= 0);
1663 up_read(&pmd
->root_lock
);
1668 bool dm_thin_changed_this_transaction(struct dm_thin_device
*td
)
1672 down_read(&td
->pmd
->root_lock
);
1674 up_read(&td
->pmd
->root_lock
);
1679 bool dm_pool_changed_this_transaction(struct dm_pool_metadata
*pmd
)
1682 struct dm_thin_device
*td
, *tmp
;
1684 down_read(&pmd
->root_lock
);
1685 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
1691 up_read(&pmd
->root_lock
);
1696 bool dm_thin_aborted_changes(struct dm_thin_device
*td
)
1700 down_read(&td
->pmd
->root_lock
);
1701 r
= td
->aborted_with_changes
;
1702 up_read(&td
->pmd
->root_lock
);
1707 int dm_pool_alloc_data_block(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1711 down_write(&pmd
->root_lock
);
1713 r
= dm_sm_new_block(pmd
->data_sm
, result
);
1714 up_write(&pmd
->root_lock
);
1719 int dm_pool_commit_metadata(struct dm_pool_metadata
*pmd
)
1723 down_write(&pmd
->root_lock
);
1727 r
= __commit_transaction(pmd
);
1732 * Open the next transaction.
1734 r
= __begin_transaction(pmd
);
1736 up_write(&pmd
->root_lock
);
1740 static void __set_abort_with_changes_flags(struct dm_pool_metadata
*pmd
)
1742 struct dm_thin_device
*td
;
1744 list_for_each_entry(td
, &pmd
->thin_devices
, list
)
1745 td
->aborted_with_changes
= td
->changed
;
1748 int dm_pool_abort_metadata(struct dm_pool_metadata
*pmd
)
1752 down_write(&pmd
->root_lock
);
1756 __set_abort_with_changes_flags(pmd
);
1757 __destroy_persistent_data_objects(pmd
);
1758 r
= __create_persistent_data_objects(pmd
, false);
1760 pmd
->fail_io
= true;
1763 up_write(&pmd
->root_lock
);
1768 int dm_pool_get_free_block_count(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1772 down_read(&pmd
->root_lock
);
1774 r
= dm_sm_get_nr_free(pmd
->data_sm
, result
);
1775 up_read(&pmd
->root_lock
);
1780 int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata
*pmd
,
1785 down_read(&pmd
->root_lock
);
1787 r
= dm_sm_get_nr_free(pmd
->metadata_sm
, result
);
1790 if (*result
< pmd
->metadata_reserve
)
1793 *result
-= pmd
->metadata_reserve
;
1795 up_read(&pmd
->root_lock
);
1800 int dm_pool_get_metadata_dev_size(struct dm_pool_metadata
*pmd
,
1805 down_read(&pmd
->root_lock
);
1807 r
= dm_sm_get_nr_blocks(pmd
->metadata_sm
, result
);
1808 up_read(&pmd
->root_lock
);
1813 int dm_pool_get_data_dev_size(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1817 down_read(&pmd
->root_lock
);
1819 r
= dm_sm_get_nr_blocks(pmd
->data_sm
, result
);
1820 up_read(&pmd
->root_lock
);
1825 int dm_thin_get_mapped_count(struct dm_thin_device
*td
, dm_block_t
*result
)
1828 struct dm_pool_metadata
*pmd
= td
->pmd
;
1830 down_read(&pmd
->root_lock
);
1831 if (!pmd
->fail_io
) {
1832 *result
= td
->mapped_blocks
;
1835 up_read(&pmd
->root_lock
);
1840 static int __highest_block(struct dm_thin_device
*td
, dm_block_t
*result
)
1844 dm_block_t thin_root
;
1845 struct dm_pool_metadata
*pmd
= td
->pmd
;
1847 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, &td
->id
, &value_le
);
1851 thin_root
= le64_to_cpu(value_le
);
1853 return dm_btree_find_highest_key(&pmd
->bl_info
, thin_root
, result
);
1856 int dm_thin_get_highest_mapped_block(struct dm_thin_device
*td
,
1860 struct dm_pool_metadata
*pmd
= td
->pmd
;
1862 down_read(&pmd
->root_lock
);
1864 r
= __highest_block(td
, result
);
1865 up_read(&pmd
->root_lock
);
1870 static int __resize_space_map(struct dm_space_map
*sm
, dm_block_t new_count
)
1873 dm_block_t old_count
;
1875 r
= dm_sm_get_nr_blocks(sm
, &old_count
);
1879 if (new_count
== old_count
)
1882 if (new_count
< old_count
) {
1883 DMERR("cannot reduce size of space map");
1887 return dm_sm_extend(sm
, new_count
- old_count
);
1890 int dm_pool_resize_data_dev(struct dm_pool_metadata
*pmd
, dm_block_t new_count
)
1894 down_write(&pmd
->root_lock
);
1896 r
= __resize_space_map(pmd
->data_sm
, new_count
);
1897 up_write(&pmd
->root_lock
);
1902 int dm_pool_resize_metadata_dev(struct dm_pool_metadata
*pmd
, dm_block_t new_count
)
1906 down_write(&pmd
->root_lock
);
1907 if (!pmd
->fail_io
) {
1908 r
= __resize_space_map(pmd
->metadata_sm
, new_count
);
1910 __set_metadata_reserve(pmd
);
1912 up_write(&pmd
->root_lock
);
1917 void dm_pool_metadata_read_only(struct dm_pool_metadata
*pmd
)
1919 down_write(&pmd
->root_lock
);
1920 dm_bm_set_read_only(pmd
->bm
);
1921 up_write(&pmd
->root_lock
);
1924 void dm_pool_metadata_read_write(struct dm_pool_metadata
*pmd
)
1926 down_write(&pmd
->root_lock
);
1927 dm_bm_set_read_write(pmd
->bm
);
1928 up_write(&pmd
->root_lock
);
1931 int dm_pool_register_metadata_threshold(struct dm_pool_metadata
*pmd
,
1932 dm_block_t threshold
,
1933 dm_sm_threshold_fn fn
,
1938 down_write(&pmd
->root_lock
);
1939 r
= dm_sm_register_threshold_callback(pmd
->metadata_sm
, threshold
, fn
, context
);
1940 up_write(&pmd
->root_lock
);
1945 int dm_pool_metadata_set_needs_check(struct dm_pool_metadata
*pmd
)
1948 struct dm_block
*sblock
;
1949 struct thin_disk_superblock
*disk_super
;
1951 down_write(&pmd
->root_lock
);
1952 pmd
->flags
|= THIN_METADATA_NEEDS_CHECK_FLAG
;
1954 r
= superblock_lock(pmd
, &sblock
);
1956 DMERR("couldn't read superblock");
1960 disk_super
= dm_block_data(sblock
);
1961 disk_super
->flags
= cpu_to_le32(pmd
->flags
);
1963 dm_bm_unlock(sblock
);
1965 up_write(&pmd
->root_lock
);
1969 bool dm_pool_metadata_needs_check(struct dm_pool_metadata
*pmd
)
1973 down_read(&pmd
->root_lock
);
1974 needs_check
= pmd
->flags
& THIN_METADATA_NEEDS_CHECK_FLAG
;
1975 up_read(&pmd
->root_lock
);
1980 void dm_pool_issue_prefetches(struct dm_pool_metadata
*pmd
)
1982 down_read(&pmd
->root_lock
);
1984 dm_tm_issue_prefetches(pmd
->tm
);
1985 up_read(&pmd
->root_lock
);