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
;
183 dm_block_t details_root
;
184 struct list_head thin_devices
;
187 sector_t data_block_size
;
190 struct dm_thin_device
{
191 struct list_head list
;
192 struct dm_pool_metadata
*pmd
;
197 uint64_t mapped_blocks
;
198 uint64_t transaction_id
;
199 uint32_t creation_time
;
200 uint32_t snapshotted_time
;
203 /*----------------------------------------------------------------
204 * superblock validator
205 *--------------------------------------------------------------*/
207 #define SUPERBLOCK_CSUM_XOR 160774
209 static void sb_prepare_for_write(struct dm_block_validator
*v
,
213 struct thin_disk_superblock
*disk_super
= dm_block_data(b
);
215 disk_super
->blocknr
= cpu_to_le64(dm_block_location(b
));
216 disk_super
->csum
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
217 block_size
- sizeof(__le32
),
218 SUPERBLOCK_CSUM_XOR
));
221 static int sb_check(struct dm_block_validator
*v
,
225 struct thin_disk_superblock
*disk_super
= dm_block_data(b
);
228 if (dm_block_location(b
) != le64_to_cpu(disk_super
->blocknr
)) {
229 DMERR("sb_check failed: blocknr %llu: "
230 "wanted %llu", le64_to_cpu(disk_super
->blocknr
),
231 (unsigned long long)dm_block_location(b
));
235 if (le64_to_cpu(disk_super
->magic
) != THIN_SUPERBLOCK_MAGIC
) {
236 DMERR("sb_check failed: magic %llu: "
237 "wanted %llu", le64_to_cpu(disk_super
->magic
),
238 (unsigned long long)THIN_SUPERBLOCK_MAGIC
);
242 csum_le
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
243 block_size
- sizeof(__le32
),
244 SUPERBLOCK_CSUM_XOR
));
245 if (csum_le
!= disk_super
->csum
) {
246 DMERR("sb_check failed: csum %u: wanted %u",
247 le32_to_cpu(csum_le
), le32_to_cpu(disk_super
->csum
));
254 static struct dm_block_validator sb_validator
= {
255 .name
= "superblock",
256 .prepare_for_write
= sb_prepare_for_write
,
260 /*----------------------------------------------------------------
261 * Methods for the btree value types
262 *--------------------------------------------------------------*/
264 static uint64_t pack_block_time(dm_block_t b
, uint32_t t
)
266 return (b
<< 24) | t
;
269 static void unpack_block_time(uint64_t v
, dm_block_t
*b
, uint32_t *t
)
272 *t
= v
& ((1 << 24) - 1);
275 static void data_block_inc(void *context
, void *value_le
)
277 struct dm_space_map
*sm
= context
;
282 memcpy(&v_le
, value_le
, sizeof(v_le
));
283 unpack_block_time(le64_to_cpu(v_le
), &b
, &t
);
284 dm_sm_inc_block(sm
, b
);
287 static void data_block_dec(void *context
, void *value_le
)
289 struct dm_space_map
*sm
= context
;
294 memcpy(&v_le
, value_le
, sizeof(v_le
));
295 unpack_block_time(le64_to_cpu(v_le
), &b
, &t
);
296 dm_sm_dec_block(sm
, b
);
299 static int data_block_equal(void *context
, void *value1_le
, void *value2_le
)
305 memcpy(&v1_le
, value1_le
, sizeof(v1_le
));
306 memcpy(&v2_le
, value2_le
, sizeof(v2_le
));
307 unpack_block_time(le64_to_cpu(v1_le
), &b1
, &t
);
308 unpack_block_time(le64_to_cpu(v2_le
), &b2
, &t
);
313 static void subtree_inc(void *context
, void *value
)
315 struct dm_btree_info
*info
= context
;
319 memcpy(&root_le
, value
, sizeof(root_le
));
320 root
= le64_to_cpu(root_le
);
321 dm_tm_inc(info
->tm
, root
);
324 static void subtree_dec(void *context
, void *value
)
326 struct dm_btree_info
*info
= context
;
330 memcpy(&root_le
, value
, sizeof(root_le
));
331 root
= le64_to_cpu(root_le
);
332 if (dm_btree_del(info
, root
))
333 DMERR("btree delete failed\n");
336 static int subtree_equal(void *context
, void *value1_le
, void *value2_le
)
339 memcpy(&v1_le
, value1_le
, sizeof(v1_le
));
340 memcpy(&v2_le
, value2_le
, sizeof(v2_le
));
342 return v1_le
== v2_le
;
345 /*----------------------------------------------------------------*/
347 static int superblock_all_zeroes(struct dm_block_manager
*bm
, int *result
)
352 __le64
*data_le
, zero
= cpu_to_le64(0);
353 unsigned block_size
= dm_bm_block_size(bm
) / sizeof(__le64
);
356 * We can't use a validator here - it may be all zeroes.
358 r
= dm_bm_read_lock(bm
, THIN_SUPERBLOCK_LOCATION
, NULL
, &b
);
362 data_le
= dm_block_data(b
);
364 for (i
= 0; i
< block_size
; i
++) {
365 if (data_le
[i
] != zero
) {
371 return dm_bm_unlock(b
);
374 static void __setup_btree_details(struct dm_pool_metadata
*pmd
)
376 pmd
->info
.tm
= pmd
->tm
;
377 pmd
->info
.levels
= 2;
378 pmd
->info
.value_type
.context
= pmd
->data_sm
;
379 pmd
->info
.value_type
.size
= sizeof(__le64
);
380 pmd
->info
.value_type
.inc
= data_block_inc
;
381 pmd
->info
.value_type
.dec
= data_block_dec
;
382 pmd
->info
.value_type
.equal
= data_block_equal
;
384 memcpy(&pmd
->nb_info
, &pmd
->info
, sizeof(pmd
->nb_info
));
385 pmd
->nb_info
.tm
= pmd
->nb_tm
;
387 pmd
->tl_info
.tm
= pmd
->tm
;
388 pmd
->tl_info
.levels
= 1;
389 pmd
->tl_info
.value_type
.context
= &pmd
->info
;
390 pmd
->tl_info
.value_type
.size
= sizeof(__le64
);
391 pmd
->tl_info
.value_type
.inc
= subtree_inc
;
392 pmd
->tl_info
.value_type
.dec
= subtree_dec
;
393 pmd
->tl_info
.value_type
.equal
= subtree_equal
;
395 pmd
->bl_info
.tm
= pmd
->tm
;
396 pmd
->bl_info
.levels
= 1;
397 pmd
->bl_info
.value_type
.context
= pmd
->data_sm
;
398 pmd
->bl_info
.value_type
.size
= sizeof(__le64
);
399 pmd
->bl_info
.value_type
.inc
= data_block_inc
;
400 pmd
->bl_info
.value_type
.dec
= data_block_dec
;
401 pmd
->bl_info
.value_type
.equal
= data_block_equal
;
403 pmd
->details_info
.tm
= pmd
->tm
;
404 pmd
->details_info
.levels
= 1;
405 pmd
->details_info
.value_type
.context
= NULL
;
406 pmd
->details_info
.value_type
.size
= sizeof(struct disk_device_details
);
407 pmd
->details_info
.value_type
.inc
= NULL
;
408 pmd
->details_info
.value_type
.dec
= NULL
;
409 pmd
->details_info
.value_type
.equal
= NULL
;
412 static int init_pmd(struct dm_pool_metadata
*pmd
,
413 struct dm_block_manager
*bm
,
414 dm_block_t nr_blocks
, int create
)
417 struct dm_space_map
*sm
, *data_sm
;
418 struct dm_transaction_manager
*tm
;
419 struct dm_block
*sblock
;
422 r
= dm_tm_create_with_sm(bm
, THIN_SUPERBLOCK_LOCATION
,
423 &sb_validator
, &tm
, &sm
, &sblock
);
425 DMERR("tm_create_with_sm failed");
429 data_sm
= dm_sm_disk_create(tm
, nr_blocks
);
430 if (IS_ERR(data_sm
)) {
431 DMERR("sm_disk_create failed");
432 dm_tm_unlock(tm
, sblock
);
433 r
= PTR_ERR(data_sm
);
437 struct thin_disk_superblock
*disk_super
= NULL
;
438 size_t space_map_root_offset
=
439 offsetof(struct thin_disk_superblock
, metadata_space_map_root
);
441 r
= dm_tm_open_with_sm(bm
, THIN_SUPERBLOCK_LOCATION
,
442 &sb_validator
, space_map_root_offset
,
443 SPACE_MAP_ROOT_SIZE
, &tm
, &sm
, &sblock
);
445 DMERR("tm_open_with_sm failed");
449 disk_super
= dm_block_data(sblock
);
450 data_sm
= dm_sm_disk_open(tm
, disk_super
->data_space_map_root
,
451 sizeof(disk_super
->data_space_map_root
));
452 if (IS_ERR(data_sm
)) {
453 DMERR("sm_disk_open failed");
454 r
= PTR_ERR(data_sm
);
460 r
= dm_tm_unlock(tm
, sblock
);
462 DMERR("couldn't unlock superblock");
467 pmd
->metadata_sm
= sm
;
468 pmd
->data_sm
= data_sm
;
470 pmd
->nb_tm
= dm_tm_create_non_blocking_clone(tm
);
472 DMERR("could not create clone tm");
477 __setup_btree_details(pmd
);
480 init_rwsem(&pmd
->root_lock
);
482 pmd
->need_commit
= 0;
483 pmd
->details_root
= 0;
486 INIT_LIST_HEAD(&pmd
->thin_devices
);
491 dm_sm_destroy(data_sm
);
499 static int __begin_transaction(struct dm_pool_metadata
*pmd
)
503 struct thin_disk_superblock
*disk_super
;
504 struct dm_block
*sblock
;
507 * __maybe_commit_transaction() resets these
509 WARN_ON(pmd
->need_commit
);
512 * We re-read the superblock every time. Shouldn't need to do this
515 r
= dm_bm_read_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
516 &sb_validator
, &sblock
);
520 disk_super
= dm_block_data(sblock
);
521 pmd
->time
= le32_to_cpu(disk_super
->time
);
522 pmd
->root
= le64_to_cpu(disk_super
->data_mapping_root
);
523 pmd
->details_root
= le64_to_cpu(disk_super
->device_details_root
);
524 pmd
->trans_id
= le64_to_cpu(disk_super
->trans_id
);
525 pmd
->flags
= le32_to_cpu(disk_super
->flags
);
526 pmd
->data_block_size
= le32_to_cpu(disk_super
->data_block_size
);
528 features
= le32_to_cpu(disk_super
->incompat_flags
) & ~THIN_FEATURE_INCOMPAT_SUPP
;
530 DMERR("could not access metadata due to "
531 "unsupported optional features (%lx).",
532 (unsigned long)features
);
538 * Check for read-only metadata to skip the following RDWR checks.
540 if (get_disk_ro(pmd
->bdev
->bd_disk
))
543 features
= le32_to_cpu(disk_super
->compat_ro_flags
) & ~THIN_FEATURE_COMPAT_RO_SUPP
;
545 DMERR("could not access metadata RDWR due to "
546 "unsupported optional features (%lx).",
547 (unsigned long)features
);
552 dm_bm_unlock(sblock
);
556 static int __write_changed_details(struct dm_pool_metadata
*pmd
)
559 struct dm_thin_device
*td
, *tmp
;
560 struct disk_device_details details
;
563 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
569 details
.mapped_blocks
= cpu_to_le64(td
->mapped_blocks
);
570 details
.transaction_id
= cpu_to_le64(td
->transaction_id
);
571 details
.creation_time
= cpu_to_le32(td
->creation_time
);
572 details
.snapshotted_time
= cpu_to_le32(td
->snapshotted_time
);
573 __dm_bless_for_disk(&details
);
575 r
= dm_btree_insert(&pmd
->details_info
, pmd
->details_root
,
576 &key
, &details
, &pmd
->details_root
);
587 pmd
->need_commit
= 1;
593 static int __commit_transaction(struct dm_pool_metadata
*pmd
)
596 * FIXME: Associated pool should be made read-only on failure.
599 size_t metadata_len
, data_len
;
600 struct thin_disk_superblock
*disk_super
;
601 struct dm_block
*sblock
;
604 * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
606 BUILD_BUG_ON(sizeof(struct thin_disk_superblock
) > 512);
608 r
= __write_changed_details(pmd
);
612 if (!pmd
->need_commit
)
615 r
= dm_sm_commit(pmd
->data_sm
);
619 r
= dm_tm_pre_commit(pmd
->tm
);
623 r
= dm_sm_root_size(pmd
->metadata_sm
, &metadata_len
);
627 r
= dm_sm_root_size(pmd
->data_sm
, &data_len
);
631 r
= dm_bm_write_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
632 &sb_validator
, &sblock
);
636 disk_super
= dm_block_data(sblock
);
637 disk_super
->time
= cpu_to_le32(pmd
->time
);
638 disk_super
->data_mapping_root
= cpu_to_le64(pmd
->root
);
639 disk_super
->device_details_root
= cpu_to_le64(pmd
->details_root
);
640 disk_super
->trans_id
= cpu_to_le64(pmd
->trans_id
);
641 disk_super
->flags
= cpu_to_le32(pmd
->flags
);
643 r
= dm_sm_copy_root(pmd
->metadata_sm
, &disk_super
->metadata_space_map_root
,
648 r
= dm_sm_copy_root(pmd
->data_sm
, &disk_super
->data_space_map_root
,
653 r
= dm_tm_commit(pmd
->tm
, sblock
);
655 pmd
->need_commit
= 0;
660 dm_bm_unlock(sblock
);
664 struct dm_pool_metadata
*dm_pool_metadata_open(struct block_device
*bdev
,
665 sector_t data_block_size
)
668 struct thin_disk_superblock
*disk_super
;
669 struct dm_pool_metadata
*pmd
;
670 sector_t bdev_size
= i_size_read(bdev
->bd_inode
) >> SECTOR_SHIFT
;
671 struct dm_block_manager
*bm
;
673 struct dm_block
*sblock
;
675 pmd
= kmalloc(sizeof(*pmd
), GFP_KERNEL
);
677 DMERR("could not allocate metadata struct");
678 return ERR_PTR(-ENOMEM
);
681 bm
= dm_block_manager_create(bdev
, THIN_METADATA_BLOCK_SIZE
,
682 THIN_METADATA_CACHE_SIZE
,
683 THIN_MAX_CONCURRENT_LOCKS
);
685 DMERR("could not create block manager");
687 return ERR_PTR(-ENOMEM
);
690 r
= superblock_all_zeroes(bm
, &create
);
692 dm_block_manager_destroy(bm
);
698 r
= init_pmd(pmd
, bm
, 0, create
);
700 dm_block_manager_destroy(bm
);
707 r
= __begin_transaction(pmd
);
716 r
= dm_bm_write_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
717 &sb_validator
, &sblock
);
721 if (bdev_size
> THIN_METADATA_MAX_SECTORS
)
722 bdev_size
= THIN_METADATA_MAX_SECTORS
;
724 disk_super
= dm_block_data(sblock
);
725 disk_super
->magic
= cpu_to_le64(THIN_SUPERBLOCK_MAGIC
);
726 disk_super
->version
= cpu_to_le32(THIN_VERSION
);
727 disk_super
->time
= 0;
728 disk_super
->metadata_block_size
= cpu_to_le32(THIN_METADATA_BLOCK_SIZE
>> SECTOR_SHIFT
);
729 disk_super
->metadata_nr_blocks
= cpu_to_le64(bdev_size
>> SECTOR_TO_BLOCK_SHIFT
);
730 disk_super
->data_block_size
= cpu_to_le32(data_block_size
);
732 r
= dm_bm_unlock(sblock
);
736 r
= dm_btree_empty(&pmd
->info
, &pmd
->root
);
740 r
= dm_btree_empty(&pmd
->details_info
, &pmd
->details_root
);
742 DMERR("couldn't create devices root");
747 pmd
->need_commit
= 1;
748 r
= dm_pool_commit_metadata(pmd
);
750 DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
758 if (dm_pool_metadata_close(pmd
) < 0)
759 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
763 int dm_pool_metadata_close(struct dm_pool_metadata
*pmd
)
766 unsigned open_devices
= 0;
767 struct dm_thin_device
*td
, *tmp
;
769 down_read(&pmd
->root_lock
);
770 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
778 up_read(&pmd
->root_lock
);
781 DMERR("attempt to close pmd when %u device(s) are still open",
786 r
= __commit_transaction(pmd
);
788 DMWARN("%s: __commit_transaction() failed, error = %d",
791 dm_tm_destroy(pmd
->tm
);
792 dm_tm_destroy(pmd
->nb_tm
);
793 dm_block_manager_destroy(pmd
->bm
);
794 dm_sm_destroy(pmd
->metadata_sm
);
795 dm_sm_destroy(pmd
->data_sm
);
802 * __open_device: Returns @td corresponding to device with id @dev,
803 * creating it if @create is set and incrementing @td->open_count.
804 * On failure, @td is undefined.
806 static int __open_device(struct dm_pool_metadata
*pmd
,
807 dm_thin_id dev
, int create
,
808 struct dm_thin_device
**td
)
811 struct dm_thin_device
*td2
;
813 struct disk_device_details details_le
;
816 * If the device is already open, return it.
818 list_for_each_entry(td2
, &pmd
->thin_devices
, list
)
819 if (td2
->id
== dev
) {
821 * May not create an already-open device.
832 * Check the device exists.
834 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
837 if (r
!= -ENODATA
|| !create
)
844 details_le
.mapped_blocks
= 0;
845 details_le
.transaction_id
= cpu_to_le64(pmd
->trans_id
);
846 details_le
.creation_time
= cpu_to_le32(pmd
->time
);
847 details_le
.snapshotted_time
= cpu_to_le32(pmd
->time
);
850 *td
= kmalloc(sizeof(**td
), GFP_NOIO
);
856 (*td
)->open_count
= 1;
857 (*td
)->changed
= changed
;
858 (*td
)->mapped_blocks
= le64_to_cpu(details_le
.mapped_blocks
);
859 (*td
)->transaction_id
= le64_to_cpu(details_le
.transaction_id
);
860 (*td
)->creation_time
= le32_to_cpu(details_le
.creation_time
);
861 (*td
)->snapshotted_time
= le32_to_cpu(details_le
.snapshotted_time
);
863 list_add(&(*td
)->list
, &pmd
->thin_devices
);
868 static void __close_device(struct dm_thin_device
*td
)
873 static int __create_thin(struct dm_pool_metadata
*pmd
,
879 struct disk_device_details details_le
;
880 struct dm_thin_device
*td
;
883 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
889 * Create an empty btree for the mappings.
891 r
= dm_btree_empty(&pmd
->bl_info
, &dev_root
);
896 * Insert it into the main mapping tree.
898 value
= cpu_to_le64(dev_root
);
899 __dm_bless_for_disk(&value
);
900 r
= dm_btree_insert(&pmd
->tl_info
, pmd
->root
, &key
, &value
, &pmd
->root
);
902 dm_btree_del(&pmd
->bl_info
, dev_root
);
906 r
= __open_device(pmd
, dev
, 1, &td
);
908 dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
909 dm_btree_del(&pmd
->bl_info
, dev_root
);
917 int dm_pool_create_thin(struct dm_pool_metadata
*pmd
, dm_thin_id dev
)
921 down_write(&pmd
->root_lock
);
922 r
= __create_thin(pmd
, dev
);
923 up_write(&pmd
->root_lock
);
928 static int __set_snapshot_details(struct dm_pool_metadata
*pmd
,
929 struct dm_thin_device
*snap
,
930 dm_thin_id origin
, uint32_t time
)
933 struct dm_thin_device
*td
;
935 r
= __open_device(pmd
, origin
, 0, &td
);
940 td
->snapshotted_time
= time
;
942 snap
->mapped_blocks
= td
->mapped_blocks
;
943 snap
->snapshotted_time
= time
;
949 static int __create_snap(struct dm_pool_metadata
*pmd
,
950 dm_thin_id dev
, dm_thin_id origin
)
953 dm_block_t origin_root
;
954 uint64_t key
= origin
, dev_key
= dev
;
955 struct dm_thin_device
*td
;
956 struct disk_device_details details_le
;
959 /* check this device is unused */
960 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
961 &dev_key
, &details_le
);
965 /* find the mapping tree for the origin */
966 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, &key
, &value
);
969 origin_root
= le64_to_cpu(value
);
971 /* clone the origin, an inc will do */
972 dm_tm_inc(pmd
->tm
, origin_root
);
974 /* insert into the main mapping tree */
975 value
= cpu_to_le64(origin_root
);
976 __dm_bless_for_disk(&value
);
978 r
= dm_btree_insert(&pmd
->tl_info
, pmd
->root
, &key
, &value
, &pmd
->root
);
980 dm_tm_dec(pmd
->tm
, origin_root
);
986 r
= __open_device(pmd
, dev
, 1, &td
);
990 r
= __set_snapshot_details(pmd
, td
, origin
, pmd
->time
);
999 dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
1000 dm_btree_remove(&pmd
->details_info
, pmd
->details_root
,
1001 &key
, &pmd
->details_root
);
1005 int dm_pool_create_snap(struct dm_pool_metadata
*pmd
,
1011 down_write(&pmd
->root_lock
);
1012 r
= __create_snap(pmd
, dev
, origin
);
1013 up_write(&pmd
->root_lock
);
1018 static int __delete_device(struct dm_pool_metadata
*pmd
, dm_thin_id dev
)
1022 struct dm_thin_device
*td
;
1024 /* TODO: failure should mark the transaction invalid */
1025 r
= __open_device(pmd
, dev
, 0, &td
);
1029 if (td
->open_count
> 1) {
1034 list_del(&td
->list
);
1036 r
= dm_btree_remove(&pmd
->details_info
, pmd
->details_root
,
1037 &key
, &pmd
->details_root
);
1041 r
= dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
1045 pmd
->need_commit
= 1;
1050 int dm_pool_delete_thin_device(struct dm_pool_metadata
*pmd
,
1055 down_write(&pmd
->root_lock
);
1056 r
= __delete_device(pmd
, dev
);
1057 up_write(&pmd
->root_lock
);
1062 int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata
*pmd
,
1063 uint64_t current_id
,
1066 down_write(&pmd
->root_lock
);
1067 if (pmd
->trans_id
!= current_id
) {
1068 up_write(&pmd
->root_lock
);
1069 DMERR("mismatched transaction id");
1073 pmd
->trans_id
= new_id
;
1074 pmd
->need_commit
= 1;
1075 up_write(&pmd
->root_lock
);
1080 int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata
*pmd
,
1083 down_read(&pmd
->root_lock
);
1084 *result
= pmd
->trans_id
;
1085 up_read(&pmd
->root_lock
);
1090 static int __reserve_metadata_snap(struct dm_pool_metadata
*pmd
)
1093 struct thin_disk_superblock
*disk_super
;
1094 struct dm_block
*copy
, *sblock
;
1095 dm_block_t held_root
;
1098 * Copy the superblock.
1100 dm_sm_inc_block(pmd
->metadata_sm
, THIN_SUPERBLOCK_LOCATION
);
1101 r
= dm_tm_shadow_block(pmd
->tm
, THIN_SUPERBLOCK_LOCATION
,
1102 &sb_validator
, ©
, &inc
);
1108 held_root
= dm_block_location(copy
);
1109 disk_super
= dm_block_data(copy
);
1111 if (le64_to_cpu(disk_super
->held_root
)) {
1112 DMWARN("Pool metadata snapshot already exists: release this before taking another.");
1114 dm_tm_dec(pmd
->tm
, held_root
);
1115 dm_tm_unlock(pmd
->tm
, copy
);
1116 pmd
->need_commit
= 1;
1122 * Wipe the spacemap since we're not publishing this.
1124 memset(&disk_super
->data_space_map_root
, 0,
1125 sizeof(disk_super
->data_space_map_root
));
1126 memset(&disk_super
->metadata_space_map_root
, 0,
1127 sizeof(disk_super
->metadata_space_map_root
));
1130 * Increment the data structures that need to be preserved.
1132 dm_tm_inc(pmd
->tm
, le64_to_cpu(disk_super
->data_mapping_root
));
1133 dm_tm_inc(pmd
->tm
, le64_to_cpu(disk_super
->device_details_root
));
1134 dm_tm_unlock(pmd
->tm
, copy
);
1137 * Write the held root into the superblock.
1139 r
= dm_bm_write_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
1140 &sb_validator
, &sblock
);
1142 dm_tm_dec(pmd
->tm
, held_root
);
1143 pmd
->need_commit
= 1;
1147 disk_super
= dm_block_data(sblock
);
1148 disk_super
->held_root
= cpu_to_le64(held_root
);
1149 dm_bm_unlock(sblock
);
1151 pmd
->need_commit
= 1;
1156 int dm_pool_reserve_metadata_snap(struct dm_pool_metadata
*pmd
)
1160 down_write(&pmd
->root_lock
);
1161 r
= __reserve_metadata_snap(pmd
);
1162 up_write(&pmd
->root_lock
);
1167 static int __release_metadata_snap(struct dm_pool_metadata
*pmd
)
1170 struct thin_disk_superblock
*disk_super
;
1171 struct dm_block
*sblock
, *copy
;
1172 dm_block_t held_root
;
1174 r
= dm_bm_write_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
1175 &sb_validator
, &sblock
);
1179 disk_super
= dm_block_data(sblock
);
1180 held_root
= le64_to_cpu(disk_super
->held_root
);
1181 disk_super
->held_root
= cpu_to_le64(0);
1182 pmd
->need_commit
= 1;
1184 dm_bm_unlock(sblock
);
1187 DMWARN("No pool metadata snapshot found: nothing to release.");
1191 r
= dm_tm_read_lock(pmd
->tm
, held_root
, &sb_validator
, ©
);
1195 disk_super
= dm_block_data(copy
);
1196 dm_sm_dec_block(pmd
->metadata_sm
, le64_to_cpu(disk_super
->data_mapping_root
));
1197 dm_sm_dec_block(pmd
->metadata_sm
, le64_to_cpu(disk_super
->device_details_root
));
1198 dm_sm_dec_block(pmd
->metadata_sm
, held_root
);
1200 return dm_tm_unlock(pmd
->tm
, copy
);
1203 int dm_pool_release_metadata_snap(struct dm_pool_metadata
*pmd
)
1207 down_write(&pmd
->root_lock
);
1208 r
= __release_metadata_snap(pmd
);
1209 up_write(&pmd
->root_lock
);
1214 static int __get_metadata_snap(struct dm_pool_metadata
*pmd
,
1218 struct thin_disk_superblock
*disk_super
;
1219 struct dm_block
*sblock
;
1221 r
= dm_bm_read_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
1222 &sb_validator
, &sblock
);
1226 disk_super
= dm_block_data(sblock
);
1227 *result
= le64_to_cpu(disk_super
->held_root
);
1229 return dm_bm_unlock(sblock
);
1232 int dm_pool_get_metadata_snap(struct dm_pool_metadata
*pmd
,
1237 down_read(&pmd
->root_lock
);
1238 r
= __get_metadata_snap(pmd
, result
);
1239 up_read(&pmd
->root_lock
);
1244 int dm_pool_open_thin_device(struct dm_pool_metadata
*pmd
, dm_thin_id dev
,
1245 struct dm_thin_device
**td
)
1249 down_write(&pmd
->root_lock
);
1250 r
= __open_device(pmd
, dev
, 0, td
);
1251 up_write(&pmd
->root_lock
);
1256 int dm_pool_close_thin_device(struct dm_thin_device
*td
)
1258 down_write(&td
->pmd
->root_lock
);
1260 up_write(&td
->pmd
->root_lock
);
1265 dm_thin_id
dm_thin_dev_id(struct dm_thin_device
*td
)
1270 static bool __snapshotted_since(struct dm_thin_device
*td
, uint32_t time
)
1272 return td
->snapshotted_time
> time
;
1275 int dm_thin_find_block(struct dm_thin_device
*td
, dm_block_t block
,
1276 int can_block
, struct dm_thin_lookup_result
*result
)
1279 uint64_t block_time
= 0;
1281 struct dm_pool_metadata
*pmd
= td
->pmd
;
1282 dm_block_t keys
[2] = { td
->id
, block
};
1285 down_read(&pmd
->root_lock
);
1286 r
= dm_btree_lookup(&pmd
->info
, pmd
->root
, keys
, &value
);
1288 block_time
= le64_to_cpu(value
);
1289 up_read(&pmd
->root_lock
);
1291 } else if (down_read_trylock(&pmd
->root_lock
)) {
1292 r
= dm_btree_lookup(&pmd
->nb_info
, pmd
->root
, keys
, &value
);
1294 block_time
= le64_to_cpu(value
);
1295 up_read(&pmd
->root_lock
);
1298 return -EWOULDBLOCK
;
1301 dm_block_t exception_block
;
1302 uint32_t exception_time
;
1303 unpack_block_time(block_time
, &exception_block
,
1305 result
->block
= exception_block
;
1306 result
->shared
= __snapshotted_since(td
, exception_time
);
1312 static int __insert(struct dm_thin_device
*td
, dm_block_t block
,
1313 dm_block_t data_block
)
1317 struct dm_pool_metadata
*pmd
= td
->pmd
;
1318 dm_block_t keys
[2] = { td
->id
, block
};
1320 pmd
->need_commit
= 1;
1321 value
= cpu_to_le64(pack_block_time(data_block
, pmd
->time
));
1322 __dm_bless_for_disk(&value
);
1324 r
= dm_btree_insert_notify(&pmd
->info
, pmd
->root
, keys
, &value
,
1325 &pmd
->root
, &inserted
);
1330 td
->mapped_blocks
++;
1337 int dm_thin_insert_block(struct dm_thin_device
*td
, dm_block_t block
,
1338 dm_block_t data_block
)
1342 down_write(&td
->pmd
->root_lock
);
1343 r
= __insert(td
, block
, data_block
);
1344 up_write(&td
->pmd
->root_lock
);
1349 static int __remove(struct dm_thin_device
*td
, dm_block_t block
)
1352 struct dm_pool_metadata
*pmd
= td
->pmd
;
1353 dm_block_t keys
[2] = { td
->id
, block
};
1355 r
= dm_btree_remove(&pmd
->info
, pmd
->root
, keys
, &pmd
->root
);
1359 td
->mapped_blocks
--;
1361 pmd
->need_commit
= 1;
1366 int dm_thin_remove_block(struct dm_thin_device
*td
, dm_block_t block
)
1370 down_write(&td
->pmd
->root_lock
);
1371 r
= __remove(td
, block
);
1372 up_write(&td
->pmd
->root_lock
);
1377 int dm_pool_alloc_data_block(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1381 down_write(&pmd
->root_lock
);
1383 r
= dm_sm_new_block(pmd
->data_sm
, result
);
1384 pmd
->need_commit
= 1;
1386 up_write(&pmd
->root_lock
);
1391 int dm_pool_commit_metadata(struct dm_pool_metadata
*pmd
)
1395 down_write(&pmd
->root_lock
);
1397 r
= __commit_transaction(pmd
);
1402 * Open the next transaction.
1404 r
= __begin_transaction(pmd
);
1406 up_write(&pmd
->root_lock
);
1410 int dm_pool_get_free_block_count(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1414 down_read(&pmd
->root_lock
);
1415 r
= dm_sm_get_nr_free(pmd
->data_sm
, result
);
1416 up_read(&pmd
->root_lock
);
1421 int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata
*pmd
,
1426 down_read(&pmd
->root_lock
);
1427 r
= dm_sm_get_nr_free(pmd
->metadata_sm
, result
);
1428 up_read(&pmd
->root_lock
);
1433 int dm_pool_get_metadata_dev_size(struct dm_pool_metadata
*pmd
,
1438 down_read(&pmd
->root_lock
);
1439 r
= dm_sm_get_nr_blocks(pmd
->metadata_sm
, result
);
1440 up_read(&pmd
->root_lock
);
1445 int dm_pool_get_data_block_size(struct dm_pool_metadata
*pmd
, sector_t
*result
)
1447 down_read(&pmd
->root_lock
);
1448 *result
= pmd
->data_block_size
;
1449 up_read(&pmd
->root_lock
);
1454 int dm_pool_get_data_dev_size(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1458 down_read(&pmd
->root_lock
);
1459 r
= dm_sm_get_nr_blocks(pmd
->data_sm
, result
);
1460 up_read(&pmd
->root_lock
);
1465 int dm_thin_get_mapped_count(struct dm_thin_device
*td
, dm_block_t
*result
)
1467 struct dm_pool_metadata
*pmd
= td
->pmd
;
1469 down_read(&pmd
->root_lock
);
1470 *result
= td
->mapped_blocks
;
1471 up_read(&pmd
->root_lock
);
1476 static int __highest_block(struct dm_thin_device
*td
, dm_block_t
*result
)
1480 dm_block_t thin_root
;
1481 struct dm_pool_metadata
*pmd
= td
->pmd
;
1483 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, &td
->id
, &value_le
);
1487 thin_root
= le64_to_cpu(value_le
);
1489 return dm_btree_find_highest_key(&pmd
->bl_info
, thin_root
, result
);
1492 int dm_thin_get_highest_mapped_block(struct dm_thin_device
*td
,
1496 struct dm_pool_metadata
*pmd
= td
->pmd
;
1498 down_read(&pmd
->root_lock
);
1499 r
= __highest_block(td
, result
);
1500 up_read(&pmd
->root_lock
);
1505 static int __resize_data_dev(struct dm_pool_metadata
*pmd
, dm_block_t new_count
)
1508 dm_block_t old_count
;
1510 r
= dm_sm_get_nr_blocks(pmd
->data_sm
, &old_count
);
1514 if (new_count
== old_count
)
1517 if (new_count
< old_count
) {
1518 DMERR("cannot reduce size of data device");
1522 r
= dm_sm_extend(pmd
->data_sm
, new_count
- old_count
);
1524 pmd
->need_commit
= 1;
1529 int dm_pool_resize_data_dev(struct dm_pool_metadata
*pmd
, dm_block_t new_count
)
1533 down_write(&pmd
->root_lock
);
1534 r
= __resize_data_dev(pmd
, new_count
);
1535 up_write(&pmd
->root_lock
);