2 * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
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_BLOCK_SIZE 4096
81 #define THIN_METADATA_CACHE_SIZE 64
82 #define SECTOR_TO_BLOCK_SHIFT 3
84 /* This should be plenty */
85 #define SPACE_MAP_ROOT_SIZE 128
88 * Little endian on-disk superblock and device details.
90 struct thin_disk_superblock
{
91 __le32 csum
; /* Checksum of superblock except for this field. */
93 __le64 blocknr
; /* This block number, dm_block_t. */
103 * Root held by userspace transactions.
107 __u8 data_space_map_root
[SPACE_MAP_ROOT_SIZE
];
108 __u8 metadata_space_map_root
[SPACE_MAP_ROOT_SIZE
];
111 * 2-level btree mapping (dev_id, (dev block, time)) -> data block
113 __le64 data_mapping_root
;
116 * Device detail root mapping dev_id -> device_details
118 __le64 device_details_root
;
120 __le32 data_block_size
; /* In 512-byte sectors. */
122 __le32 metadata_block_size
; /* In 512-byte sectors. */
123 __le64 metadata_nr_blocks
;
126 __le32 compat_ro_flags
;
127 __le32 incompat_flags
;
130 struct disk_device_details
{
131 __le64 mapped_blocks
;
132 __le64 transaction_id
; /* When created. */
133 __le32 creation_time
;
134 __le32 snapshotted_time
;
137 struct dm_pool_metadata
{
138 struct hlist_node hash
;
140 struct block_device
*bdev
;
141 struct dm_block_manager
*bm
;
142 struct dm_space_map
*metadata_sm
;
143 struct dm_space_map
*data_sm
;
144 struct dm_transaction_manager
*tm
;
145 struct dm_transaction_manager
*nb_tm
;
149 * First level holds thin_dev_t.
150 * Second level holds mappings.
152 struct dm_btree_info info
;
155 * Non-blocking version of the above.
157 struct dm_btree_info nb_info
;
160 * Just the top level for deleting whole devices.
162 struct dm_btree_info tl_info
;
165 * Just the bottom level for creating new devices.
167 struct dm_btree_info bl_info
;
170 * Describes the device details btree.
172 struct dm_btree_info details_info
;
174 struct rw_semaphore root_lock
;
177 struct dm_block
*sblock
;
179 dm_block_t details_root
;
180 struct list_head thin_devices
;
183 sector_t data_block_size
;
186 struct dm_thin_device
{
187 struct list_head list
;
188 struct dm_pool_metadata
*pmd
;
193 uint64_t mapped_blocks
;
194 uint64_t transaction_id
;
195 uint32_t creation_time
;
196 uint32_t snapshotted_time
;
199 /*----------------------------------------------------------------
200 * superblock validator
201 *--------------------------------------------------------------*/
203 static void sb_prepare_for_write(struct dm_block_validator
*v
,
207 struct thin_disk_superblock
*disk_super
= dm_block_data(b
);
209 disk_super
->blocknr
= cpu_to_le64(dm_block_location(b
));
210 disk_super
->csum
= cpu_to_le32(dm_block_csum_data(&disk_super
->flags
, sizeof(*disk_super
) - sizeof(__le32
)));
213 static int sb_check(struct dm_block_validator
*v
,
217 struct thin_disk_superblock
*disk_super
= dm_block_data(b
);
220 if (dm_block_location(b
) != le64_to_cpu(disk_super
->blocknr
)) {
221 DMERR("sb_check failed: blocknr %llu: "
222 "wanted %llu", le64_to_cpu(disk_super
->blocknr
),
223 (unsigned long long)dm_block_location(b
));
227 if (le64_to_cpu(disk_super
->magic
) != THIN_SUPERBLOCK_MAGIC
) {
228 DMERR("sb_check failed: magic %llu: "
229 "wanted %llu", le64_to_cpu(disk_super
->magic
),
230 (unsigned long long)THIN_SUPERBLOCK_MAGIC
);
234 csum_le
= cpu_to_le32(dm_block_csum_data(&disk_super
->flags
, sizeof(*disk_super
) - sizeof(__le32
)));
235 if (csum_le
!= disk_super
->csum
) {
236 DMERR("sb_check failed: csum %u: wanted %u",
237 le32_to_cpu(csum_le
), le32_to_cpu(disk_super
->csum
));
244 static struct dm_block_validator sb_validator
= {
245 .name
= "superblock",
246 .prepare_for_write
= sb_prepare_for_write
,
250 /*----------------------------------------------------------------
251 * Methods for the btree value types
252 *--------------------------------------------------------------*/
254 static uint64_t pack_block_time(dm_block_t b
, uint32_t t
)
256 return (b
<< 24) | t
;
259 static void unpack_block_time(uint64_t v
, dm_block_t
*b
, uint32_t *t
)
262 *t
= v
& ((1 << 24) - 1);
265 static void data_block_inc(void *context
, void *value_le
)
267 struct dm_space_map
*sm
= context
;
272 memcpy(&v_le
, value_le
, sizeof(v_le
));
273 unpack_block_time(le64_to_cpu(v_le
), &b
, &t
);
274 dm_sm_inc_block(sm
, b
);
277 static void data_block_dec(void *context
, void *value_le
)
279 struct dm_space_map
*sm
= context
;
284 memcpy(&v_le
, value_le
, sizeof(v_le
));
285 unpack_block_time(le64_to_cpu(v_le
), &b
, &t
);
286 dm_sm_dec_block(sm
, b
);
289 static int data_block_equal(void *context
, void *value1_le
, void *value2_le
)
295 memcpy(&v1_le
, value1_le
, sizeof(v1_le
));
296 memcpy(&v2_le
, value2_le
, sizeof(v2_le
));
297 unpack_block_time(le64_to_cpu(v1_le
), &b1
, &t
);
298 unpack_block_time(le64_to_cpu(v2_le
), &b2
, &t
);
303 static void subtree_inc(void *context
, void *value
)
305 struct dm_btree_info
*info
= context
;
309 memcpy(&root_le
, value
, sizeof(root_le
));
310 root
= le64_to_cpu(root_le
);
311 dm_tm_inc(info
->tm
, root
);
314 static void subtree_dec(void *context
, void *value
)
316 struct dm_btree_info
*info
= context
;
320 memcpy(&root_le
, value
, sizeof(root_le
));
321 root
= le64_to_cpu(root_le
);
322 if (dm_btree_destroy(info
, root
))
323 DMERR("btree delete failed\n");
326 static int subtree_equal(void *context
, void *value1_le
, void *value2_le
)
329 memcpy(&v1_le
, value1_le
, sizeof(v1_le
));
330 memcpy(&v2_le
, value2_le
, sizeof(v2_le
));
332 return v1_le
== v2_le
;
335 /*----------------------------------------------------------------*/
337 static int superblock_all_zeroes(struct dm_block_manager
*bm
, int *result
)
342 __le64
*data_le
, zero
= cpu_to_le64(0);
343 unsigned block_size
= dm_bm_block_size(bm
) / sizeof(__le64
);
346 * We can't use a validator here - it may be all zeroes.
348 r
= dm_bm_read_lock(bm
, THIN_SUPERBLOCK_LOCATION
, NULL
, &b
);
352 data_le
= dm_block_data(b
);
354 for (i
= 0; i
< block_size
; i
++) {
355 if (data_le
[i
] != zero
) {
361 return dm_bm_unlock(b
);
364 static struct dm_pool_metadata
*alloc_pmd(struct dm_block_manager
*bm
,
365 dm_block_t nr_blocks
, int create
)
368 struct dm_space_map
*sm
, *data_sm
;
369 struct dm_transaction_manager
*tm
;
370 struct dm_pool_metadata
*pmd
= NULL
;
371 struct dm_block
*sblock
;
374 r
= dm_tm_create_with_sm(bm
, THIN_SUPERBLOCK_LOCATION
,
375 &sb_validator
, &tm
, &sm
, &sblock
);
377 DMERR("tm_create_with_sm failed");
381 data_sm
= dm_sm_disk_create(tm
, nr_blocks
);
382 if (IS_ERR(data_sm
)) {
383 DMERR("sm_disk_create failed");
384 r
= PTR_ERR(data_sm
);
388 r
= dm_tm_pre_commit(tm
);
390 DMERR("couldn't pre commit");
394 r
= dm_tm_commit(tm
, sblock
);
396 DMERR("couldn't commit");
400 struct thin_disk_superblock
*disk_super
= NULL
;
401 size_t space_map_root_offset
=
402 offsetof(struct thin_disk_superblock
, metadata_space_map_root
);
404 r
= dm_tm_open_with_sm(bm
, THIN_SUPERBLOCK_LOCATION
,
405 &sb_validator
, space_map_root_offset
,
406 SPACE_MAP_ROOT_SIZE
, &tm
, &sm
, &sblock
);
408 DMERR("tm_open_with_sm failed");
412 disk_super
= dm_block_data(sblock
);
413 data_sm
= dm_sm_disk_open(tm
, disk_super
->data_space_map_root
,
414 sizeof(disk_super
->data_space_map_root
));
415 if (IS_ERR(data_sm
)) {
416 DMERR("sm_disk_open failed");
417 r
= PTR_ERR(data_sm
);
421 dm_tm_unlock(tm
, sblock
);
424 pmd
= kmalloc(sizeof(*pmd
), GFP_KERNEL
);
426 DMERR("could not allocate metadata struct");
432 pmd
->metadata_sm
= sm
;
433 pmd
->data_sm
= data_sm
;
435 pmd
->nb_tm
= dm_tm_create_non_blocking_clone(tm
);
437 DMERR("could not create clone tm");
445 pmd
->info
.levels
= 2;
446 pmd
->info
.value_type
.context
= pmd
->data_sm
;
447 pmd
->info
.value_type
.size
= sizeof(__le64
);
448 pmd
->info
.value_type
.inc
= data_block_inc
;
449 pmd
->info
.value_type
.dec
= data_block_dec
;
450 pmd
->info
.value_type
.equal
= data_block_equal
;
452 memcpy(&pmd
->nb_info
, &pmd
->info
, sizeof(pmd
->nb_info
));
453 pmd
->nb_info
.tm
= pmd
->nb_tm
;
455 pmd
->tl_info
.tm
= tm
;
456 pmd
->tl_info
.levels
= 1;
457 pmd
->tl_info
.value_type
.context
= &pmd
->info
;
458 pmd
->tl_info
.value_type
.size
= sizeof(__le64
);
459 pmd
->tl_info
.value_type
.inc
= subtree_inc
;
460 pmd
->tl_info
.value_type
.dec
= subtree_dec
;
461 pmd
->tl_info
.value_type
.equal
= subtree_equal
;
463 pmd
->bl_info
.tm
= tm
;
464 pmd
->bl_info
.levels
= 1;
465 pmd
->bl_info
.value_type
.context
= pmd
->data_sm
;
466 pmd
->bl_info
.value_type
.size
= sizeof(__le64
);
467 pmd
->bl_info
.value_type
.inc
= data_block_inc
;
468 pmd
->bl_info
.value_type
.dec
= data_block_dec
;
469 pmd
->bl_info
.value_type
.equal
= data_block_equal
;
471 pmd
->details_info
.tm
= tm
;
472 pmd
->details_info
.levels
= 1;
473 pmd
->details_info
.value_type
.context
= NULL
;
474 pmd
->details_info
.value_type
.size
= sizeof(struct disk_device_details
);
475 pmd
->details_info
.value_type
.inc
= NULL
;
476 pmd
->details_info
.value_type
.dec
= NULL
;
477 pmd
->details_info
.value_type
.equal
= NULL
;
481 init_rwsem(&pmd
->root_lock
);
483 pmd
->need_commit
= 0;
484 pmd
->details_root
= 0;
485 INIT_LIST_HEAD(&pmd
->thin_devices
);
492 dm_sm_destroy(data_sm
);
500 static int __begin_transaction(struct dm_pool_metadata
*pmd
)
504 struct thin_disk_superblock
*disk_super
;
507 * __maybe_commit_transaction() resets these
509 WARN_ON(pmd
->sblock
);
510 WARN_ON(pmd
->need_commit
);
513 * superblock is unlocked via dm_tm_commit()
515 r
= dm_bm_write_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
516 &sb_validator
, &pmd
->sblock
);
520 disk_super
= dm_block_data(pmd
->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
);
537 * Check for read-only metadata to skip the following RDWR checks.
539 if (get_disk_ro(pmd
->bdev
->bd_disk
))
542 features
= le32_to_cpu(disk_super
->compat_ro_flags
) & ~THIN_FEATURE_COMPAT_RO_SUPP
;
544 DMERR("could not access metadata RDWR due to "
545 "unsupported optional features (%lx).",
546 (unsigned long)features
);
553 static int __write_changed_details(struct dm_pool_metadata
*pmd
)
556 struct dm_thin_device
*td
, *tmp
;
557 struct disk_device_details details
;
560 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
566 details
.mapped_blocks
= cpu_to_le64(td
->mapped_blocks
);
567 details
.transaction_id
= cpu_to_le64(td
->transaction_id
);
568 details
.creation_time
= cpu_to_le32(td
->creation_time
);
569 details
.snapshotted_time
= cpu_to_le32(td
->snapshotted_time
);
570 __dm_bless_for_disk(&details
);
572 r
= dm_btree_insert(&pmd
->details_info
, pmd
->details_root
,
573 &key
, &details
, &pmd
->details_root
);
584 pmd
->need_commit
= 1;
591 * If there is data waiting to be committed, commit it.
592 * Returns 1 if commit took place, 0 if not, or < 0 on error.
594 static int __maybe_commit_transaction(struct dm_pool_metadata
*pmd
)
597 * FIXME: Associated pool should be made read-only on failure.
601 struct thin_disk_superblock
*disk_super
;
604 * thin_disk_superblock is assumed not to exceed 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_tm_pre_commit(pmd
->tm
);
619 r
= dm_sm_root_size(pmd
->metadata_sm
, &len
);
623 disk_super
= dm_block_data(pmd
->sblock
);
624 disk_super
->time
= cpu_to_le32(pmd
->time
);
625 disk_super
->data_mapping_root
= cpu_to_le64(pmd
->root
);
626 disk_super
->device_details_root
= cpu_to_le64(pmd
->details_root
);
627 disk_super
->trans_id
= cpu_to_le64(pmd
->trans_id
);
628 disk_super
->flags
= cpu_to_le32(pmd
->flags
);
630 r
= dm_sm_copy_root(pmd
->metadata_sm
, &disk_super
->metadata_space_map_root
, len
);
634 r
= dm_sm_copy_root(pmd
->data_sm
, &disk_super
->data_space_map_root
, len
);
638 r
= dm_tm_commit(pmd
->tm
, pmd
->sblock
);
642 pmd
->need_commit
= 0;
649 int dm_pool_commit_metadata(struct dm_pool_metadata
*pmd
)
653 down_write(&pmd
->root_lock
);
655 r
= __maybe_commit_transaction(pmd
);
660 * Open the next transaction.
662 r
= __begin_transaction(pmd
);
665 up_write(&pmd
->root_lock
);
669 struct dm_pool_metadata
*dm_pool_metadata_open(struct block_device
*bdev
,
670 sector_t data_block_size
)
673 struct thin_disk_superblock
*disk_super
;
674 struct dm_pool_metadata
*pmd
;
675 sector_t bdev_size
= i_size_read(bdev
->bd_inode
) >> SECTOR_SHIFT
;
676 struct dm_block_manager
*bm
;
679 bm
= dm_block_manager_create(bdev
, THIN_METADATA_BLOCK_SIZE
,
680 THIN_METADATA_CACHE_SIZE
, 6);
682 DMERR("could not create block manager");
683 return ERR_PTR(-ENOMEM
);
686 r
= superblock_all_zeroes(bm
, &create
);
688 dm_block_manager_destroy(bm
);
692 pmd
= alloc_pmd(bm
, 0, create
);
694 dm_block_manager_destroy(bm
);
700 r
= __begin_transaction(pmd
);
710 r
= __begin_transaction(pmd
);
715 disk_super
= dm_block_data(pmd
->sblock
);
716 disk_super
->magic
= cpu_to_le64(THIN_SUPERBLOCK_MAGIC
);
717 disk_super
->version
= cpu_to_le32(THIN_VERSION
);
718 disk_super
->time
= 0;
719 disk_super
->metadata_block_size
= cpu_to_le32(THIN_METADATA_BLOCK_SIZE
>> SECTOR_SHIFT
);
720 disk_super
->metadata_nr_blocks
= cpu_to_le64(bdev_size
>> SECTOR_TO_BLOCK_SHIFT
);
721 disk_super
->data_block_size
= cpu_to_le32(data_block_size
);
723 r
= dm_btree_create(&pmd
->info
, &pmd
->root
);
727 r
= dm_btree_create(&pmd
->details_info
, &pmd
->details_root
);
729 DMERR("couldn't create devices root");
734 pmd
->need_commit
= 1;
735 r
= dm_pool_commit_metadata(pmd
);
737 DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
745 if (dm_pool_metadata_close(pmd
) < 0)
746 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
750 int dm_pool_metadata_close(struct dm_pool_metadata
*pmd
)
753 unsigned open_devices
= 0;
754 struct dm_thin_device
*td
, *tmp
;
756 down_read(&pmd
->root_lock
);
757 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
765 up_read(&pmd
->root_lock
);
768 DMERR("attempt to close pmd when %u device(s) are still open",
774 r
= __maybe_commit_transaction(pmd
);
776 DMWARN("%s: __maybe_commit_transaction() failed, error = %d",
779 dm_tm_unlock(pmd
->tm
, pmd
->sblock
);
782 dm_tm_destroy(pmd
->tm
);
783 dm_tm_destroy(pmd
->nb_tm
);
784 dm_block_manager_destroy(pmd
->bm
);
785 dm_sm_destroy(pmd
->metadata_sm
);
786 dm_sm_destroy(pmd
->data_sm
);
792 int dm_pool_rebind_metadata_device(struct dm_pool_metadata
*pmd
,
793 struct block_device
*bdev
)
795 return dm_bm_rebind_block_device(pmd
->bm
, bdev
);
798 static int __open_device(struct dm_pool_metadata
*pmd
,
799 dm_thin_id dev
, int create
,
800 struct dm_thin_device
**td
)
803 struct dm_thin_device
*td2
;
805 struct disk_device_details details_le
;
808 * If the device is already open, just increment its open_count.
810 list_for_each_entry(td2
, &pmd
->thin_devices
, list
)
811 if (td2
->id
== dev
) {
818 * Check the device exists.
820 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
823 if (r
!= -ENODATA
|| !create
)
830 details_le
.mapped_blocks
= 0;
831 details_le
.transaction_id
= cpu_to_le64(pmd
->trans_id
);
832 details_le
.creation_time
= cpu_to_le32(pmd
->time
);
833 details_le
.snapshotted_time
= cpu_to_le32(pmd
->time
);
836 *td
= kmalloc(sizeof(**td
), GFP_NOIO
);
842 (*td
)->open_count
= 1;
843 (*td
)->changed
= changed
;
844 (*td
)->mapped_blocks
= le64_to_cpu(details_le
.mapped_blocks
);
845 (*td
)->transaction_id
= le64_to_cpu(details_le
.transaction_id
);
846 (*td
)->creation_time
= le32_to_cpu(details_le
.creation_time
);
847 (*td
)->snapshotted_time
= le32_to_cpu(details_le
.snapshotted_time
);
849 list_add(&(*td
)->list
, &pmd
->thin_devices
);
854 static void __close_device(struct dm_thin_device
*td
)
859 static int __create_thin(struct dm_pool_metadata
*pmd
,
865 struct disk_device_details details_le
;
866 struct dm_thin_device
*td
;
869 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
875 * Create an empty btree for the mappings.
877 r
= dm_btree_create(&pmd
->bl_info
, &dev_root
);
882 * Insert it into the main mapping tree.
884 value
= cpu_to_le64(dev_root
);
885 __dm_bless_for_disk(&value
);
886 r
= dm_btree_insert(&pmd
->tl_info
, pmd
->root
, &key
, &value
, &pmd
->root
);
888 dm_btree_destroy(&pmd
->bl_info
, dev_root
);
892 r
= __open_device(pmd
, dev
, 1, &td
);
895 dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
896 dm_btree_destroy(&pmd
->bl_info
, dev_root
);
905 int dm_pool_create_thin(struct dm_pool_metadata
*pmd
, dm_thin_id dev
)
909 down_write(&pmd
->root_lock
);
910 r
= __create_thin(pmd
, dev
);
911 up_write(&pmd
->root_lock
);
916 static int __set_snapshot_details(struct dm_pool_metadata
*pmd
,
917 struct dm_thin_device
*snap
,
918 dm_thin_id origin
, uint32_t time
)
921 struct dm_thin_device
*td
;
923 r
= __open_device(pmd
, origin
, 0, &td
);
928 td
->snapshotted_time
= time
;
930 snap
->mapped_blocks
= td
->mapped_blocks
;
931 snap
->snapshotted_time
= time
;
937 static int __create_snap(struct dm_pool_metadata
*pmd
,
938 dm_thin_id dev
, dm_thin_id origin
)
941 dm_block_t origin_root
, snap_root
;
942 uint64_t key
= origin
, dev_key
= dev
;
943 struct dm_thin_device
*td
;
944 struct disk_device_details details_le
;
947 /* check this device is unused */
948 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
949 &dev_key
, &details_le
);
953 /* find the mapping tree for the origin */
954 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, &key
, &value
);
957 origin_root
= le64_to_cpu(value
);
959 /* clone the origin */
960 r
= dm_btree_clone(&pmd
->bl_info
, origin_root
, &snap_root
);
964 /* insert into the main mapping tree */
965 value
= cpu_to_le64(snap_root
);
966 __dm_bless_for_disk(&value
);
968 r
= dm_btree_insert(&pmd
->tl_info
, pmd
->root
, &key
, &value
, &pmd
->root
);
970 dm_btree_destroy(&pmd
->bl_info
, snap_root
);
976 r
= __open_device(pmd
, dev
, 1, &td
);
980 r
= __set_snapshot_details(pmd
, td
, origin
, pmd
->time
);
989 dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
990 dm_btree_remove(&pmd
->details_info
, pmd
->details_root
,
991 &key
, &pmd
->details_root
);
995 int dm_pool_create_snap(struct dm_pool_metadata
*pmd
,
1001 down_write(&pmd
->root_lock
);
1002 r
= __create_snap(pmd
, dev
, origin
);
1003 up_write(&pmd
->root_lock
);
1008 static int __delete_device(struct dm_pool_metadata
*pmd
, dm_thin_id dev
)
1012 struct dm_thin_device
*td
;
1014 /* TODO: failure should mark the transaction invalid */
1015 r
= __open_device(pmd
, dev
, 0, &td
);
1019 if (td
->open_count
> 1) {
1024 list_del(&td
->list
);
1026 r
= dm_btree_remove(&pmd
->details_info
, pmd
->details_root
,
1027 &key
, &pmd
->details_root
);
1031 r
= dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
1035 pmd
->need_commit
= 1;
1040 int dm_pool_delete_thin_device(struct dm_pool_metadata
*pmd
,
1045 down_write(&pmd
->root_lock
);
1046 r
= __delete_device(pmd
, dev
);
1047 up_write(&pmd
->root_lock
);
1052 static int __trim_thin_dev(struct dm_thin_device
*td
, sector_t new_size
)
1054 struct dm_pool_metadata
*pmd
= td
->pmd
;
1055 /* FIXME: convert new size to blocks */
1056 uint64_t key
[2] = { td
->id
, new_size
- 1 };
1061 * We need to truncate all the extraneous mappings.
1063 * FIXME: We have to be careful to do this atomically.
1064 * Perhaps clone the bottom layer first so we can revert?
1066 return dm_btree_delete_gt(&pmd
->info
, pmd
->root
, key
, &pmd
->root
);
1069 // FIXME Incomplete implementation. Finish or remove it before final submission.
1070 int dm_pool_trim_thin_device(struct dm_pool_metadata
*pmd
, dm_thin_id dev
,
1074 struct dm_thin_device
*td
;
1076 down_write(&pmd
->root_lock
);
1077 r
= __open_device(pmd
, dev
, 1, &td
);
1079 DMERR("couldn't open virtual device");
1081 r
= __trim_thin_dev(td
, new_size
);
1085 /* FIXME: update mapped_blocks */
1087 up_write(&pmd
->root_lock
);
1092 int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata
*pmd
,
1093 uint64_t current_id
,
1096 down_write(&pmd
->root_lock
);
1097 if (pmd
->trans_id
!= current_id
) {
1098 up_write(&pmd
->root_lock
);
1099 DMERR("mismatched transaction id");
1103 pmd
->trans_id
= new_id
;
1104 pmd
->need_commit
= 1;
1105 up_write(&pmd
->root_lock
);
1110 int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata
*pmd
,
1113 down_read(&pmd
->root_lock
);
1114 *result
= pmd
->trans_id
;
1115 up_read(&pmd
->root_lock
);
1120 int dm_pool_get_held_metadata_root(struct dm_pool_metadata
*pmd
,
1123 struct thin_disk_superblock
*disk_super
;
1125 down_read(&pmd
->root_lock
);
1126 disk_super
= dm_block_data(pmd
->sblock
);
1127 *result
= le64_to_cpu(disk_super
->held_root
);
1128 up_read(&pmd
->root_lock
);
1133 int dm_pool_open_thin_device(struct dm_pool_metadata
*pmd
, dm_thin_id dev
,
1134 struct dm_thin_device
**td
)
1138 down_write(&pmd
->root_lock
);
1139 r
= __open_device(pmd
, dev
, 0, td
);
1140 up_write(&pmd
->root_lock
);
1145 int dm_pool_close_thin_device(struct dm_thin_device
*td
)
1147 down_write(&td
->pmd
->root_lock
);
1149 up_write(&td
->pmd
->root_lock
);
1154 dm_thin_id
dm_thin_dev_id(struct dm_thin_device
*td
)
1159 static int __snapshotted_since(struct dm_thin_device
*td
, uint32_t time
)
1161 return td
->snapshotted_time
> time
;
1164 int dm_thin_find_block(struct dm_thin_device
*td
, dm_block_t block
,
1165 int can_block
, struct dm_thin_lookup_result
*result
)
1168 uint64_t block_time
= 0;
1170 struct dm_pool_metadata
*pmd
= td
->pmd
;
1171 dm_block_t keys
[2] = { td
->id
, block
};
1174 down_read(&pmd
->root_lock
);
1175 r
= dm_btree_lookup(&pmd
->info
, pmd
->root
, keys
, &value
);
1177 block_time
= le64_to_cpu(value
);
1178 up_read(&pmd
->root_lock
);
1180 } else if (down_read_trylock(&pmd
->root_lock
)) {
1181 r
= dm_btree_lookup(&pmd
->nb_info
, pmd
->root
, keys
, &value
);
1183 block_time
= le64_to_cpu(value
);
1184 up_read(&pmd
->root_lock
);
1187 return -EWOULDBLOCK
;
1190 dm_block_t exception_block
;
1191 uint32_t exception_time
;
1192 unpack_block_time(block_time
, &exception_block
,
1194 result
->block
= exception_block
;
1195 result
->shared
= __snapshotted_since(td
, exception_time
);
1201 static int __insert(struct dm_thin_device
*td
, dm_block_t block
,
1202 dm_block_t data_block
)
1206 struct dm_pool_metadata
*pmd
= td
->pmd
;
1207 dm_block_t keys
[2] = { td
->id
, block
};
1209 pmd
->need_commit
= 1;
1210 value
= cpu_to_le64(pack_block_time(data_block
, pmd
->time
));
1211 __dm_bless_for_disk(&value
);
1213 r
= dm_btree_insert_notify(&pmd
->info
, pmd
->root
, keys
, &value
,
1214 &pmd
->root
, &inserted
);
1219 td
->mapped_blocks
++;
1226 int dm_thin_insert_block(struct dm_thin_device
*td
, dm_block_t block
,
1227 dm_block_t data_block
)
1231 down_write(&td
->pmd
->root_lock
);
1232 r
= __insert(td
, block
, data_block
);
1233 up_write(&td
->pmd
->root_lock
);
1238 static int __remove(struct dm_thin_device
*td
, dm_block_t block
)
1241 struct dm_pool_metadata
*pmd
= td
->pmd
;
1242 dm_block_t keys
[2] = { td
->id
, block
};
1244 r
= dm_btree_remove(&pmd
->info
, pmd
->root
, keys
, &pmd
->root
);
1248 pmd
->need_commit
= 1;
1253 int dm_thin_remove_block(struct dm_thin_device
*td
, dm_block_t block
)
1257 down_write(&td
->pmd
->root_lock
);
1258 r
= __remove(td
, block
);
1259 up_write(&td
->pmd
->root_lock
);
1264 int dm_pool_alloc_data_block(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1268 down_write(&pmd
->root_lock
);
1270 r
= dm_sm_new_block(pmd
->data_sm
, result
);
1271 pmd
->need_commit
= 1;
1273 up_write(&pmd
->root_lock
);
1278 int dm_pool_get_free_block_count(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1282 down_read(&pmd
->root_lock
);
1283 r
= dm_sm_get_nr_free(pmd
->data_sm
, result
);
1284 up_read(&pmd
->root_lock
);
1289 int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata
*pmd
,
1294 down_read(&pmd
->root_lock
);
1295 r
= dm_sm_get_nr_free(pmd
->metadata_sm
, result
);
1296 up_read(&pmd
->root_lock
);
1301 int dm_pool_get_metadata_dev_size(struct dm_pool_metadata
*pmd
,
1306 down_read(&pmd
->root_lock
);
1307 r
= dm_sm_get_nr_blocks(pmd
->metadata_sm
, result
);
1308 up_read(&pmd
->root_lock
);
1313 int dm_pool_get_data_block_size(struct dm_pool_metadata
*pmd
, sector_t
*result
)
1315 down_read(&pmd
->root_lock
);
1316 *result
= pmd
->data_block_size
;
1317 up_read(&pmd
->root_lock
);
1322 int dm_pool_get_data_dev_size(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1326 down_read(&pmd
->root_lock
);
1327 r
= dm_sm_get_nr_blocks(pmd
->data_sm
, result
);
1328 up_read(&pmd
->root_lock
);
1333 int dm_thin_get_mapped_count(struct dm_thin_device
*td
, dm_block_t
*result
)
1335 struct dm_pool_metadata
*pmd
= td
->pmd
;
1337 down_read(&pmd
->root_lock
);
1338 *result
= td
->mapped_blocks
;
1339 up_read(&pmd
->root_lock
);
1344 static int __highest_block(struct dm_thin_device
*td
, dm_block_t
*result
)
1348 dm_block_t thin_root
;
1349 struct dm_pool_metadata
*pmd
= td
->pmd
;
1351 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, &td
->id
, &value_le
);
1355 thin_root
= le64_to_cpu(value_le
);
1357 return dm_btree_find_highest_key(&pmd
->bl_info
, thin_root
, result
);
1360 int dm_thin_get_highest_mapped_block(struct dm_thin_device
*td
,
1364 struct dm_pool_metadata
*pmd
= td
->pmd
;
1366 down_read(&pmd
->root_lock
);
1367 r
= __highest_block(td
, result
);
1368 up_read(&pmd
->root_lock
);
1373 static int __resize_data_dev(struct dm_pool_metadata
*pmd
, dm_block_t new_count
)
1376 dm_block_t old_count
;
1378 r
= dm_sm_get_nr_blocks(pmd
->data_sm
, &old_count
);
1382 if (new_count
== old_count
)
1385 if (new_count
< old_count
) {
1386 DMERR("cannot reduce size of data device");
1390 r
= dm_sm_extend(pmd
->data_sm
, new_count
- old_count
);
1392 pmd
->need_commit
= 1;
1397 int dm_pool_resize_data_dev(struct dm_pool_metadata
*pmd
, dm_block_t new_count
)
1401 down_write(&pmd
->root_lock
);
1402 r
= __resize_data_dev(pmd
, new_count
);
1403 up_write(&pmd
->root_lock
);