2 * Copyright (C) 2011 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 1
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 struct dm_thin_device
{
190 struct list_head list
;
191 struct dm_pool_metadata
*pmd
;
196 uint64_t mapped_blocks
;
197 uint64_t transaction_id
;
198 uint32_t creation_time
;
199 uint32_t snapshotted_time
;
202 /*----------------------------------------------------------------
203 * superblock validator
204 *--------------------------------------------------------------*/
206 #define SUPERBLOCK_CSUM_XOR 160774
208 static void sb_prepare_for_write(struct dm_block_validator
*v
,
212 struct thin_disk_superblock
*disk_super
= dm_block_data(b
);
214 disk_super
->blocknr
= cpu_to_le64(dm_block_location(b
));
215 disk_super
->csum
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
216 block_size
- sizeof(__le32
),
217 SUPERBLOCK_CSUM_XOR
));
220 static int sb_check(struct dm_block_validator
*v
,
224 struct thin_disk_superblock
*disk_super
= dm_block_data(b
);
227 if (dm_block_location(b
) != le64_to_cpu(disk_super
->blocknr
)) {
228 DMERR("sb_check failed: blocknr %llu: "
229 "wanted %llu", le64_to_cpu(disk_super
->blocknr
),
230 (unsigned long long)dm_block_location(b
));
234 if (le64_to_cpu(disk_super
->magic
) != THIN_SUPERBLOCK_MAGIC
) {
235 DMERR("sb_check failed: magic %llu: "
236 "wanted %llu", le64_to_cpu(disk_super
->magic
),
237 (unsigned long long)THIN_SUPERBLOCK_MAGIC
);
241 csum_le
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
242 block_size
- sizeof(__le32
),
243 SUPERBLOCK_CSUM_XOR
));
244 if (csum_le
!= disk_super
->csum
) {
245 DMERR("sb_check failed: csum %u: wanted %u",
246 le32_to_cpu(csum_le
), le32_to_cpu(disk_super
->csum
));
253 static struct dm_block_validator sb_validator
= {
254 .name
= "superblock",
255 .prepare_for_write
= sb_prepare_for_write
,
259 /*----------------------------------------------------------------
260 * Methods for the btree value types
261 *--------------------------------------------------------------*/
263 static uint64_t pack_block_time(dm_block_t b
, uint32_t t
)
265 return (b
<< 24) | t
;
268 static void unpack_block_time(uint64_t v
, dm_block_t
*b
, uint32_t *t
)
271 *t
= v
& ((1 << 24) - 1);
274 static void data_block_inc(void *context
, void *value_le
)
276 struct dm_space_map
*sm
= context
;
281 memcpy(&v_le
, value_le
, sizeof(v_le
));
282 unpack_block_time(le64_to_cpu(v_le
), &b
, &t
);
283 dm_sm_inc_block(sm
, b
);
286 static void data_block_dec(void *context
, void *value_le
)
288 struct dm_space_map
*sm
= context
;
293 memcpy(&v_le
, value_le
, sizeof(v_le
));
294 unpack_block_time(le64_to_cpu(v_le
), &b
, &t
);
295 dm_sm_dec_block(sm
, b
);
298 static int data_block_equal(void *context
, void *value1_le
, void *value2_le
)
304 memcpy(&v1_le
, value1_le
, sizeof(v1_le
));
305 memcpy(&v2_le
, value2_le
, sizeof(v2_le
));
306 unpack_block_time(le64_to_cpu(v1_le
), &b1
, &t
);
307 unpack_block_time(le64_to_cpu(v2_le
), &b2
, &t
);
312 static void subtree_inc(void *context
, void *value
)
314 struct dm_btree_info
*info
= context
;
318 memcpy(&root_le
, value
, sizeof(root_le
));
319 root
= le64_to_cpu(root_le
);
320 dm_tm_inc(info
->tm
, root
);
323 static void subtree_dec(void *context
, void *value
)
325 struct dm_btree_info
*info
= context
;
329 memcpy(&root_le
, value
, sizeof(root_le
));
330 root
= le64_to_cpu(root_le
);
331 if (dm_btree_del(info
, root
))
332 DMERR("btree delete failed\n");
335 static int subtree_equal(void *context
, void *value1_le
, void *value2_le
)
338 memcpy(&v1_le
, value1_le
, sizeof(v1_le
));
339 memcpy(&v2_le
, value2_le
, sizeof(v2_le
));
341 return v1_le
== v2_le
;
344 /*----------------------------------------------------------------*/
346 static int superblock_lock_zero(struct dm_pool_metadata
*pmd
,
347 struct dm_block
**sblock
)
349 return dm_bm_write_lock_zero(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
350 &sb_validator
, sblock
);
353 static int superblock_lock(struct dm_pool_metadata
*pmd
,
354 struct dm_block
**sblock
)
356 return dm_bm_write_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
357 &sb_validator
, sblock
);
360 static int __superblock_all_zeroes(struct dm_block_manager
*bm
, int *result
)
365 __le64
*data_le
, zero
= cpu_to_le64(0);
366 unsigned block_size
= dm_bm_block_size(bm
) / sizeof(__le64
);
369 * We can't use a validator here - it may be all zeroes.
371 r
= dm_bm_read_lock(bm
, THIN_SUPERBLOCK_LOCATION
, NULL
, &b
);
375 data_le
= dm_block_data(b
);
377 for (i
= 0; i
< block_size
; i
++) {
378 if (data_le
[i
] != zero
) {
384 return dm_bm_unlock(b
);
387 static void __setup_btree_details(struct dm_pool_metadata
*pmd
)
389 pmd
->info
.tm
= pmd
->tm
;
390 pmd
->info
.levels
= 2;
391 pmd
->info
.value_type
.context
= pmd
->data_sm
;
392 pmd
->info
.value_type
.size
= sizeof(__le64
);
393 pmd
->info
.value_type
.inc
= data_block_inc
;
394 pmd
->info
.value_type
.dec
= data_block_dec
;
395 pmd
->info
.value_type
.equal
= data_block_equal
;
397 memcpy(&pmd
->nb_info
, &pmd
->info
, sizeof(pmd
->nb_info
));
398 pmd
->nb_info
.tm
= pmd
->nb_tm
;
400 pmd
->tl_info
.tm
= pmd
->tm
;
401 pmd
->tl_info
.levels
= 1;
402 pmd
->tl_info
.value_type
.context
= &pmd
->info
;
403 pmd
->tl_info
.value_type
.size
= sizeof(__le64
);
404 pmd
->tl_info
.value_type
.inc
= subtree_inc
;
405 pmd
->tl_info
.value_type
.dec
= subtree_dec
;
406 pmd
->tl_info
.value_type
.equal
= subtree_equal
;
408 pmd
->bl_info
.tm
= pmd
->tm
;
409 pmd
->bl_info
.levels
= 1;
410 pmd
->bl_info
.value_type
.context
= pmd
->data_sm
;
411 pmd
->bl_info
.value_type
.size
= sizeof(__le64
);
412 pmd
->bl_info
.value_type
.inc
= data_block_inc
;
413 pmd
->bl_info
.value_type
.dec
= data_block_dec
;
414 pmd
->bl_info
.value_type
.equal
= data_block_equal
;
416 pmd
->details_info
.tm
= pmd
->tm
;
417 pmd
->details_info
.levels
= 1;
418 pmd
->details_info
.value_type
.context
= NULL
;
419 pmd
->details_info
.value_type
.size
= sizeof(struct disk_device_details
);
420 pmd
->details_info
.value_type
.inc
= NULL
;
421 pmd
->details_info
.value_type
.dec
= NULL
;
422 pmd
->details_info
.value_type
.equal
= NULL
;
425 static int __write_initial_superblock(struct dm_pool_metadata
*pmd
)
428 struct dm_block
*sblock
;
429 size_t metadata_len
, data_len
;
430 struct thin_disk_superblock
*disk_super
;
431 sector_t bdev_size
= i_size_read(pmd
->bdev
->bd_inode
) >> SECTOR_SHIFT
;
433 if (bdev_size
> THIN_METADATA_MAX_SECTORS
)
434 bdev_size
= THIN_METADATA_MAX_SECTORS
;
436 r
= dm_sm_root_size(pmd
->metadata_sm
, &metadata_len
);
440 r
= dm_sm_root_size(pmd
->data_sm
, &data_len
);
444 r
= dm_sm_commit(pmd
->data_sm
);
448 r
= dm_tm_pre_commit(pmd
->tm
);
452 r
= superblock_lock_zero(pmd
, &sblock
);
456 disk_super
= dm_block_data(sblock
);
457 disk_super
->flags
= 0;
458 memset(disk_super
->uuid
, 0, sizeof(disk_super
->uuid
));
459 disk_super
->magic
= cpu_to_le64(THIN_SUPERBLOCK_MAGIC
);
460 disk_super
->version
= cpu_to_le32(THIN_VERSION
);
461 disk_super
->time
= 0;
462 disk_super
->trans_id
= 0;
463 disk_super
->held_root
= 0;
465 r
= dm_sm_copy_root(pmd
->metadata_sm
, &disk_super
->metadata_space_map_root
,
470 r
= dm_sm_copy_root(pmd
->data_sm
, &disk_super
->data_space_map_root
,
475 disk_super
->data_mapping_root
= cpu_to_le64(pmd
->root
);
476 disk_super
->device_details_root
= cpu_to_le64(pmd
->details_root
);
477 disk_super
->metadata_block_size
= cpu_to_le32(THIN_METADATA_BLOCK_SIZE
>> SECTOR_SHIFT
);
478 disk_super
->metadata_nr_blocks
= cpu_to_le64(bdev_size
>> SECTOR_TO_BLOCK_SHIFT
);
479 disk_super
->data_block_size
= cpu_to_le32(pmd
->data_block_size
);
481 return dm_tm_commit(pmd
->tm
, sblock
);
484 dm_bm_unlock(sblock
);
488 static int __format_metadata(struct dm_pool_metadata
*pmd
)
492 r
= dm_tm_create_with_sm(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
493 &pmd
->tm
, &pmd
->metadata_sm
);
495 DMERR("tm_create_with_sm failed");
499 pmd
->data_sm
= dm_sm_disk_create(pmd
->tm
, 0);
500 if (IS_ERR(pmd
->data_sm
)) {
501 DMERR("sm_disk_create failed");
502 r
= PTR_ERR(pmd
->data_sm
);
506 pmd
->nb_tm
= dm_tm_create_non_blocking_clone(pmd
->tm
);
508 DMERR("could not create clone tm");
513 __setup_btree_details(pmd
);
515 r
= dm_btree_empty(&pmd
->info
, &pmd
->root
);
519 r
= dm_btree_empty(&pmd
->details_info
, &pmd
->details_root
);
521 DMERR("couldn't create devices root");
525 r
= __write_initial_superblock(pmd
);
532 dm_sm_destroy(pmd
->data_sm
);
534 dm_tm_destroy(pmd
->tm
);
535 dm_sm_destroy(pmd
->metadata_sm
);
540 static int __open_metadata(struct dm_pool_metadata
*pmd
)
543 struct dm_block
*sblock
;
544 struct thin_disk_superblock
*disk_super
;
546 r
= dm_bm_read_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
547 &sb_validator
, &sblock
);
549 DMERR("couldn't read superblock");
553 disk_super
= dm_block_data(sblock
);
554 r
= dm_tm_open_with_sm(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
555 disk_super
->metadata_space_map_root
,
556 sizeof(disk_super
->metadata_space_map_root
),
557 &pmd
->tm
, &pmd
->metadata_sm
);
559 DMERR("tm_open_with_sm failed");
560 dm_bm_unlock(sblock
);
564 pmd
->data_sm
= dm_sm_disk_open(pmd
->tm
, disk_super
->data_space_map_root
,
565 sizeof(disk_super
->data_space_map_root
));
566 if (IS_ERR(pmd
->data_sm
)) {
567 DMERR("sm_disk_open failed");
568 dm_bm_unlock(sblock
);
569 r
= PTR_ERR(pmd
->data_sm
);
573 dm_bm_unlock(sblock
);
575 pmd
->nb_tm
= dm_tm_create_non_blocking_clone(pmd
->tm
);
577 DMERR("could not create clone tm");
582 __setup_btree_details(pmd
);
585 dm_sm_destroy(pmd
->data_sm
);
587 dm_tm_destroy(pmd
->tm
);
588 dm_sm_destroy(pmd
->metadata_sm
);
593 static int __open_or_format_metadata(struct dm_pool_metadata
*pmd
)
597 r
= __superblock_all_zeroes(pmd
->bm
, &unformatted
);
602 return __format_metadata(pmd
);
604 return __open_metadata(pmd
);
607 static int __create_persistent_data_objects(struct dm_pool_metadata
*pmd
)
611 pmd
->bm
= dm_block_manager_create(pmd
->bdev
, THIN_METADATA_BLOCK_SIZE
,
612 THIN_METADATA_CACHE_SIZE
,
613 THIN_MAX_CONCURRENT_LOCKS
);
614 if (IS_ERR(pmd
->bm
)) {
615 DMERR("could not create block manager");
616 return PTR_ERR(pmd
->bm
);
619 r
= __open_or_format_metadata(pmd
);
621 dm_block_manager_destroy(pmd
->bm
);
626 static void __destroy_persistent_data_objects(struct dm_pool_metadata
*pmd
)
628 dm_sm_destroy(pmd
->data_sm
);
629 dm_sm_destroy(pmd
->metadata_sm
);
630 dm_tm_destroy(pmd
->nb_tm
);
631 dm_tm_destroy(pmd
->tm
);
632 dm_block_manager_destroy(pmd
->bm
);
635 static int __begin_transaction(struct dm_pool_metadata
*pmd
)
639 struct thin_disk_superblock
*disk_super
;
640 struct dm_block
*sblock
;
643 * We re-read the superblock every time. Shouldn't need to do this
646 r
= dm_bm_read_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
647 &sb_validator
, &sblock
);
651 disk_super
= dm_block_data(sblock
);
652 pmd
->time
= le32_to_cpu(disk_super
->time
);
653 pmd
->root
= le64_to_cpu(disk_super
->data_mapping_root
);
654 pmd
->details_root
= le64_to_cpu(disk_super
->device_details_root
);
655 pmd
->trans_id
= le64_to_cpu(disk_super
->trans_id
);
656 pmd
->flags
= le32_to_cpu(disk_super
->flags
);
657 pmd
->data_block_size
= le32_to_cpu(disk_super
->data_block_size
);
659 features
= le32_to_cpu(disk_super
->incompat_flags
) & ~THIN_FEATURE_INCOMPAT_SUPP
;
661 DMERR("could not access metadata due to "
662 "unsupported optional features (%lx).",
663 (unsigned long)features
);
669 * Check for read-only metadata to skip the following RDWR checks.
671 if (get_disk_ro(pmd
->bdev
->bd_disk
))
674 features
= le32_to_cpu(disk_super
->compat_ro_flags
) & ~THIN_FEATURE_COMPAT_RO_SUPP
;
676 DMERR("could not access metadata RDWR due to "
677 "unsupported optional features (%lx).",
678 (unsigned long)features
);
683 dm_bm_unlock(sblock
);
687 static int __write_changed_details(struct dm_pool_metadata
*pmd
)
690 struct dm_thin_device
*td
, *tmp
;
691 struct disk_device_details details
;
694 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
700 details
.mapped_blocks
= cpu_to_le64(td
->mapped_blocks
);
701 details
.transaction_id
= cpu_to_le64(td
->transaction_id
);
702 details
.creation_time
= cpu_to_le32(td
->creation_time
);
703 details
.snapshotted_time
= cpu_to_le32(td
->snapshotted_time
);
704 __dm_bless_for_disk(&details
);
706 r
= dm_btree_insert(&pmd
->details_info
, pmd
->details_root
,
707 &key
, &details
, &pmd
->details_root
);
722 static int __commit_transaction(struct dm_pool_metadata
*pmd
)
725 * FIXME: Associated pool should be made read-only on failure.
728 size_t metadata_len
, data_len
;
729 struct thin_disk_superblock
*disk_super
;
730 struct dm_block
*sblock
;
733 * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
735 BUILD_BUG_ON(sizeof(struct thin_disk_superblock
) > 512);
737 r
= __write_changed_details(pmd
);
741 r
= dm_sm_commit(pmd
->data_sm
);
745 r
= dm_tm_pre_commit(pmd
->tm
);
749 r
= dm_sm_root_size(pmd
->metadata_sm
, &metadata_len
);
753 r
= dm_sm_root_size(pmd
->data_sm
, &data_len
);
757 r
= superblock_lock(pmd
, &sblock
);
761 disk_super
= dm_block_data(sblock
);
762 disk_super
->time
= cpu_to_le32(pmd
->time
);
763 disk_super
->data_mapping_root
= cpu_to_le64(pmd
->root
);
764 disk_super
->device_details_root
= cpu_to_le64(pmd
->details_root
);
765 disk_super
->trans_id
= cpu_to_le64(pmd
->trans_id
);
766 disk_super
->flags
= cpu_to_le32(pmd
->flags
);
768 r
= dm_sm_copy_root(pmd
->metadata_sm
, &disk_super
->metadata_space_map_root
,
773 r
= dm_sm_copy_root(pmd
->data_sm
, &disk_super
->data_space_map_root
,
778 return dm_tm_commit(pmd
->tm
, sblock
);
781 dm_bm_unlock(sblock
);
785 struct dm_pool_metadata
*dm_pool_metadata_open(struct block_device
*bdev
,
786 sector_t data_block_size
)
789 struct dm_pool_metadata
*pmd
;
791 pmd
= kmalloc(sizeof(*pmd
), GFP_KERNEL
);
793 DMERR("could not allocate metadata struct");
794 return ERR_PTR(-ENOMEM
);
797 init_rwsem(&pmd
->root_lock
);
799 INIT_LIST_HEAD(&pmd
->thin_devices
);
801 pmd
->data_block_size
= data_block_size
;
803 r
= __create_persistent_data_objects(pmd
);
809 r
= __begin_transaction(pmd
);
811 if (dm_pool_metadata_close(pmd
) < 0)
812 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
819 int dm_pool_metadata_close(struct dm_pool_metadata
*pmd
)
822 unsigned open_devices
= 0;
823 struct dm_thin_device
*td
, *tmp
;
825 down_read(&pmd
->root_lock
);
826 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
834 up_read(&pmd
->root_lock
);
837 DMERR("attempt to close pmd when %u device(s) are still open",
842 r
= __commit_transaction(pmd
);
844 DMWARN("%s: __commit_transaction() failed, error = %d",
847 __destroy_persistent_data_objects(pmd
);
854 * __open_device: Returns @td corresponding to device with id @dev,
855 * creating it if @create is set and incrementing @td->open_count.
856 * On failure, @td is undefined.
858 static int __open_device(struct dm_pool_metadata
*pmd
,
859 dm_thin_id dev
, int create
,
860 struct dm_thin_device
**td
)
863 struct dm_thin_device
*td2
;
865 struct disk_device_details details_le
;
868 * If the device is already open, return it.
870 list_for_each_entry(td2
, &pmd
->thin_devices
, list
)
871 if (td2
->id
== dev
) {
873 * May not create an already-open device.
884 * Check the device exists.
886 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
889 if (r
!= -ENODATA
|| !create
)
896 details_le
.mapped_blocks
= 0;
897 details_le
.transaction_id
= cpu_to_le64(pmd
->trans_id
);
898 details_le
.creation_time
= cpu_to_le32(pmd
->time
);
899 details_le
.snapshotted_time
= cpu_to_le32(pmd
->time
);
902 *td
= kmalloc(sizeof(**td
), GFP_NOIO
);
908 (*td
)->open_count
= 1;
909 (*td
)->changed
= changed
;
910 (*td
)->mapped_blocks
= le64_to_cpu(details_le
.mapped_blocks
);
911 (*td
)->transaction_id
= le64_to_cpu(details_le
.transaction_id
);
912 (*td
)->creation_time
= le32_to_cpu(details_le
.creation_time
);
913 (*td
)->snapshotted_time
= le32_to_cpu(details_le
.snapshotted_time
);
915 list_add(&(*td
)->list
, &pmd
->thin_devices
);
920 static void __close_device(struct dm_thin_device
*td
)
925 static int __create_thin(struct dm_pool_metadata
*pmd
,
931 struct disk_device_details details_le
;
932 struct dm_thin_device
*td
;
935 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
941 * Create an empty btree for the mappings.
943 r
= dm_btree_empty(&pmd
->bl_info
, &dev_root
);
948 * Insert it into the main mapping tree.
950 value
= cpu_to_le64(dev_root
);
951 __dm_bless_for_disk(&value
);
952 r
= dm_btree_insert(&pmd
->tl_info
, pmd
->root
, &key
, &value
, &pmd
->root
);
954 dm_btree_del(&pmd
->bl_info
, dev_root
);
958 r
= __open_device(pmd
, dev
, 1, &td
);
960 dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
961 dm_btree_del(&pmd
->bl_info
, dev_root
);
969 int dm_pool_create_thin(struct dm_pool_metadata
*pmd
, dm_thin_id dev
)
973 down_write(&pmd
->root_lock
);
974 r
= __create_thin(pmd
, dev
);
975 up_write(&pmd
->root_lock
);
980 static int __set_snapshot_details(struct dm_pool_metadata
*pmd
,
981 struct dm_thin_device
*snap
,
982 dm_thin_id origin
, uint32_t time
)
985 struct dm_thin_device
*td
;
987 r
= __open_device(pmd
, origin
, 0, &td
);
992 td
->snapshotted_time
= time
;
994 snap
->mapped_blocks
= td
->mapped_blocks
;
995 snap
->snapshotted_time
= time
;
1001 static int __create_snap(struct dm_pool_metadata
*pmd
,
1002 dm_thin_id dev
, dm_thin_id origin
)
1005 dm_block_t origin_root
;
1006 uint64_t key
= origin
, dev_key
= dev
;
1007 struct dm_thin_device
*td
;
1008 struct disk_device_details details_le
;
1011 /* check this device is unused */
1012 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
1013 &dev_key
, &details_le
);
1017 /* find the mapping tree for the origin */
1018 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, &key
, &value
);
1021 origin_root
= le64_to_cpu(value
);
1023 /* clone the origin, an inc will do */
1024 dm_tm_inc(pmd
->tm
, origin_root
);
1026 /* insert into the main mapping tree */
1027 value
= cpu_to_le64(origin_root
);
1028 __dm_bless_for_disk(&value
);
1030 r
= dm_btree_insert(&pmd
->tl_info
, pmd
->root
, &key
, &value
, &pmd
->root
);
1032 dm_tm_dec(pmd
->tm
, origin_root
);
1038 r
= __open_device(pmd
, dev
, 1, &td
);
1042 r
= __set_snapshot_details(pmd
, td
, origin
, pmd
->time
);
1051 dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
1052 dm_btree_remove(&pmd
->details_info
, pmd
->details_root
,
1053 &key
, &pmd
->details_root
);
1057 int dm_pool_create_snap(struct dm_pool_metadata
*pmd
,
1063 down_write(&pmd
->root_lock
);
1064 r
= __create_snap(pmd
, dev
, origin
);
1065 up_write(&pmd
->root_lock
);
1070 static int __delete_device(struct dm_pool_metadata
*pmd
, dm_thin_id dev
)
1074 struct dm_thin_device
*td
;
1076 /* TODO: failure should mark the transaction invalid */
1077 r
= __open_device(pmd
, dev
, 0, &td
);
1081 if (td
->open_count
> 1) {
1086 list_del(&td
->list
);
1088 r
= dm_btree_remove(&pmd
->details_info
, pmd
->details_root
,
1089 &key
, &pmd
->details_root
);
1093 r
= dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
1100 int dm_pool_delete_thin_device(struct dm_pool_metadata
*pmd
,
1105 down_write(&pmd
->root_lock
);
1106 r
= __delete_device(pmd
, dev
);
1107 up_write(&pmd
->root_lock
);
1112 int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata
*pmd
,
1113 uint64_t current_id
,
1116 down_write(&pmd
->root_lock
);
1117 if (pmd
->trans_id
!= current_id
) {
1118 up_write(&pmd
->root_lock
);
1119 DMERR("mismatched transaction id");
1123 pmd
->trans_id
= new_id
;
1124 up_write(&pmd
->root_lock
);
1129 int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata
*pmd
,
1132 down_read(&pmd
->root_lock
);
1133 *result
= pmd
->trans_id
;
1134 up_read(&pmd
->root_lock
);
1139 static int __reserve_metadata_snap(struct dm_pool_metadata
*pmd
)
1142 struct thin_disk_superblock
*disk_super
;
1143 struct dm_block
*copy
, *sblock
;
1144 dm_block_t held_root
;
1147 * Copy the superblock.
1149 dm_sm_inc_block(pmd
->metadata_sm
, THIN_SUPERBLOCK_LOCATION
);
1150 r
= dm_tm_shadow_block(pmd
->tm
, THIN_SUPERBLOCK_LOCATION
,
1151 &sb_validator
, ©
, &inc
);
1157 held_root
= dm_block_location(copy
);
1158 disk_super
= dm_block_data(copy
);
1160 if (le64_to_cpu(disk_super
->held_root
)) {
1161 DMWARN("Pool metadata snapshot already exists: release this before taking another.");
1163 dm_tm_dec(pmd
->tm
, held_root
);
1164 dm_tm_unlock(pmd
->tm
, copy
);
1169 * Wipe the spacemap since we're not publishing this.
1171 memset(&disk_super
->data_space_map_root
, 0,
1172 sizeof(disk_super
->data_space_map_root
));
1173 memset(&disk_super
->metadata_space_map_root
, 0,
1174 sizeof(disk_super
->metadata_space_map_root
));
1177 * Increment the data structures that need to be preserved.
1179 dm_tm_inc(pmd
->tm
, le64_to_cpu(disk_super
->data_mapping_root
));
1180 dm_tm_inc(pmd
->tm
, le64_to_cpu(disk_super
->device_details_root
));
1181 dm_tm_unlock(pmd
->tm
, copy
);
1184 * Write the held root into the superblock.
1186 r
= superblock_lock(pmd
, &sblock
);
1188 dm_tm_dec(pmd
->tm
, held_root
);
1192 disk_super
= dm_block_data(sblock
);
1193 disk_super
->held_root
= cpu_to_le64(held_root
);
1194 dm_bm_unlock(sblock
);
1198 int dm_pool_reserve_metadata_snap(struct dm_pool_metadata
*pmd
)
1202 down_write(&pmd
->root_lock
);
1203 r
= __reserve_metadata_snap(pmd
);
1204 up_write(&pmd
->root_lock
);
1209 static int __release_metadata_snap(struct dm_pool_metadata
*pmd
)
1212 struct thin_disk_superblock
*disk_super
;
1213 struct dm_block
*sblock
, *copy
;
1214 dm_block_t held_root
;
1216 r
= superblock_lock(pmd
, &sblock
);
1220 disk_super
= dm_block_data(sblock
);
1221 held_root
= le64_to_cpu(disk_super
->held_root
);
1222 disk_super
->held_root
= cpu_to_le64(0);
1224 dm_bm_unlock(sblock
);
1227 DMWARN("No pool metadata snapshot found: nothing to release.");
1231 r
= dm_tm_read_lock(pmd
->tm
, held_root
, &sb_validator
, ©
);
1235 disk_super
= dm_block_data(copy
);
1236 dm_sm_dec_block(pmd
->metadata_sm
, le64_to_cpu(disk_super
->data_mapping_root
));
1237 dm_sm_dec_block(pmd
->metadata_sm
, le64_to_cpu(disk_super
->device_details_root
));
1238 dm_sm_dec_block(pmd
->metadata_sm
, held_root
);
1240 return dm_tm_unlock(pmd
->tm
, copy
);
1243 int dm_pool_release_metadata_snap(struct dm_pool_metadata
*pmd
)
1247 down_write(&pmd
->root_lock
);
1248 r
= __release_metadata_snap(pmd
);
1249 up_write(&pmd
->root_lock
);
1254 static int __get_metadata_snap(struct dm_pool_metadata
*pmd
,
1258 struct thin_disk_superblock
*disk_super
;
1259 struct dm_block
*sblock
;
1261 r
= dm_bm_read_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
1262 &sb_validator
, &sblock
);
1266 disk_super
= dm_block_data(sblock
);
1267 *result
= le64_to_cpu(disk_super
->held_root
);
1269 return dm_bm_unlock(sblock
);
1272 int dm_pool_get_metadata_snap(struct dm_pool_metadata
*pmd
,
1277 down_read(&pmd
->root_lock
);
1278 r
= __get_metadata_snap(pmd
, result
);
1279 up_read(&pmd
->root_lock
);
1284 int dm_pool_open_thin_device(struct dm_pool_metadata
*pmd
, dm_thin_id dev
,
1285 struct dm_thin_device
**td
)
1289 down_write(&pmd
->root_lock
);
1290 r
= __open_device(pmd
, dev
, 0, td
);
1291 up_write(&pmd
->root_lock
);
1296 int dm_pool_close_thin_device(struct dm_thin_device
*td
)
1298 down_write(&td
->pmd
->root_lock
);
1300 up_write(&td
->pmd
->root_lock
);
1305 dm_thin_id
dm_thin_dev_id(struct dm_thin_device
*td
)
1310 static bool __snapshotted_since(struct dm_thin_device
*td
, uint32_t time
)
1312 return td
->snapshotted_time
> time
;
1315 int dm_thin_find_block(struct dm_thin_device
*td
, dm_block_t block
,
1316 int can_block
, struct dm_thin_lookup_result
*result
)
1319 uint64_t block_time
= 0;
1321 struct dm_pool_metadata
*pmd
= td
->pmd
;
1322 dm_block_t keys
[2] = { td
->id
, block
};
1325 down_read(&pmd
->root_lock
);
1326 r
= dm_btree_lookup(&pmd
->info
, pmd
->root
, keys
, &value
);
1328 block_time
= le64_to_cpu(value
);
1329 up_read(&pmd
->root_lock
);
1331 } else if (down_read_trylock(&pmd
->root_lock
)) {
1332 r
= dm_btree_lookup(&pmd
->nb_info
, pmd
->root
, keys
, &value
);
1334 block_time
= le64_to_cpu(value
);
1335 up_read(&pmd
->root_lock
);
1338 return -EWOULDBLOCK
;
1341 dm_block_t exception_block
;
1342 uint32_t exception_time
;
1343 unpack_block_time(block_time
, &exception_block
,
1345 result
->block
= exception_block
;
1346 result
->shared
= __snapshotted_since(td
, exception_time
);
1352 static int __insert(struct dm_thin_device
*td
, dm_block_t block
,
1353 dm_block_t data_block
)
1357 struct dm_pool_metadata
*pmd
= td
->pmd
;
1358 dm_block_t keys
[2] = { td
->id
, block
};
1360 value
= cpu_to_le64(pack_block_time(data_block
, pmd
->time
));
1361 __dm_bless_for_disk(&value
);
1363 r
= dm_btree_insert_notify(&pmd
->info
, pmd
->root
, keys
, &value
,
1364 &pmd
->root
, &inserted
);
1369 td
->mapped_blocks
++;
1376 int dm_thin_insert_block(struct dm_thin_device
*td
, dm_block_t block
,
1377 dm_block_t data_block
)
1381 down_write(&td
->pmd
->root_lock
);
1382 r
= __insert(td
, block
, data_block
);
1383 up_write(&td
->pmd
->root_lock
);
1388 static int __remove(struct dm_thin_device
*td
, dm_block_t block
)
1391 struct dm_pool_metadata
*pmd
= td
->pmd
;
1392 dm_block_t keys
[2] = { td
->id
, block
};
1394 r
= dm_btree_remove(&pmd
->info
, pmd
->root
, keys
, &pmd
->root
);
1398 td
->mapped_blocks
--;
1404 int dm_thin_remove_block(struct dm_thin_device
*td
, dm_block_t block
)
1408 down_write(&td
->pmd
->root_lock
);
1409 r
= __remove(td
, block
);
1410 up_write(&td
->pmd
->root_lock
);
1415 int dm_pool_alloc_data_block(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1419 down_write(&pmd
->root_lock
);
1420 r
= dm_sm_new_block(pmd
->data_sm
, result
);
1421 up_write(&pmd
->root_lock
);
1426 int dm_pool_commit_metadata(struct dm_pool_metadata
*pmd
)
1430 down_write(&pmd
->root_lock
);
1432 r
= __commit_transaction(pmd
);
1437 * Open the next transaction.
1439 r
= __begin_transaction(pmd
);
1441 up_write(&pmd
->root_lock
);
1445 int dm_pool_get_free_block_count(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1449 down_read(&pmd
->root_lock
);
1450 r
= dm_sm_get_nr_free(pmd
->data_sm
, result
);
1451 up_read(&pmd
->root_lock
);
1456 int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata
*pmd
,
1461 down_read(&pmd
->root_lock
);
1462 r
= dm_sm_get_nr_free(pmd
->metadata_sm
, result
);
1463 up_read(&pmd
->root_lock
);
1468 int dm_pool_get_metadata_dev_size(struct dm_pool_metadata
*pmd
,
1473 down_read(&pmd
->root_lock
);
1474 r
= dm_sm_get_nr_blocks(pmd
->metadata_sm
, result
);
1475 up_read(&pmd
->root_lock
);
1480 int dm_pool_get_data_block_size(struct dm_pool_metadata
*pmd
, sector_t
*result
)
1482 down_read(&pmd
->root_lock
);
1483 *result
= pmd
->data_block_size
;
1484 up_read(&pmd
->root_lock
);
1489 int dm_pool_get_data_dev_size(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1493 down_read(&pmd
->root_lock
);
1494 r
= dm_sm_get_nr_blocks(pmd
->data_sm
, result
);
1495 up_read(&pmd
->root_lock
);
1500 int dm_thin_get_mapped_count(struct dm_thin_device
*td
, dm_block_t
*result
)
1502 struct dm_pool_metadata
*pmd
= td
->pmd
;
1504 down_read(&pmd
->root_lock
);
1505 *result
= td
->mapped_blocks
;
1506 up_read(&pmd
->root_lock
);
1511 static int __highest_block(struct dm_thin_device
*td
, dm_block_t
*result
)
1515 dm_block_t thin_root
;
1516 struct dm_pool_metadata
*pmd
= td
->pmd
;
1518 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, &td
->id
, &value_le
);
1522 thin_root
= le64_to_cpu(value_le
);
1524 return dm_btree_find_highest_key(&pmd
->bl_info
, thin_root
, result
);
1527 int dm_thin_get_highest_mapped_block(struct dm_thin_device
*td
,
1531 struct dm_pool_metadata
*pmd
= td
->pmd
;
1533 down_read(&pmd
->root_lock
);
1534 r
= __highest_block(td
, result
);
1535 up_read(&pmd
->root_lock
);
1540 static int __resize_data_dev(struct dm_pool_metadata
*pmd
, dm_block_t new_count
)
1543 dm_block_t old_count
;
1545 r
= dm_sm_get_nr_blocks(pmd
->data_sm
, &old_count
);
1549 if (new_count
== old_count
)
1552 if (new_count
< old_count
) {
1553 DMERR("cannot reduce size of data device");
1557 return dm_sm_extend(pmd
->data_sm
, new_count
- old_count
);
1560 int dm_pool_resize_data_dev(struct dm_pool_metadata
*pmd
, dm_block_t new_count
)
1564 down_write(&pmd
->root_lock
);
1565 r
= __resize_data_dev(pmd
, new_count
);
1566 up_write(&pmd
->root_lock
);