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
83 /* This should be plenty */
84 #define SPACE_MAP_ROOT_SIZE 128
87 * Little endian on-disk superblock and device details.
89 struct thin_disk_superblock
{
90 __le32 csum
; /* Checksum of superblock except for this field. */
92 __le64 blocknr
; /* This block number, dm_block_t. */
102 * Root held by userspace transactions.
106 __u8 data_space_map_root
[SPACE_MAP_ROOT_SIZE
];
107 __u8 metadata_space_map_root
[SPACE_MAP_ROOT_SIZE
];
110 * 2-level btree mapping (dev_id, (dev block, time)) -> data block
112 __le64 data_mapping_root
;
115 * Device detail root mapping dev_id -> device_details
117 __le64 device_details_root
;
119 __le32 data_block_size
; /* In 512-byte sectors. */
121 __le32 metadata_block_size
; /* In 512-byte sectors. */
122 __le64 metadata_nr_blocks
;
125 __le32 compat_ro_flags
;
126 __le32 incompat_flags
;
129 struct disk_device_details
{
130 __le64 mapped_blocks
;
131 __le64 transaction_id
; /* When created. */
132 __le32 creation_time
;
133 __le32 snapshotted_time
;
136 struct dm_pool_metadata
{
137 struct hlist_node hash
;
139 struct block_device
*bdev
;
140 struct dm_block_manager
*bm
;
141 struct dm_space_map
*metadata_sm
;
142 struct dm_space_map
*data_sm
;
143 struct dm_transaction_manager
*tm
;
144 struct dm_transaction_manager
*nb_tm
;
148 * First level holds thin_dev_t.
149 * Second level holds mappings.
151 struct dm_btree_info info
;
154 * Non-blocking version of the above.
156 struct dm_btree_info nb_info
;
159 * Just the top level for deleting whole devices.
161 struct dm_btree_info tl_info
;
164 * Just the bottom level for creating new devices.
166 struct dm_btree_info bl_info
;
169 * Describes the device details btree.
171 struct dm_btree_info details_info
;
173 struct rw_semaphore root_lock
;
177 dm_block_t details_root
;
178 struct list_head thin_devices
;
181 sector_t data_block_size
;
184 struct dm_thin_device
{
185 struct list_head list
;
186 struct dm_pool_metadata
*pmd
;
191 uint64_t mapped_blocks
;
192 uint64_t transaction_id
;
193 uint32_t creation_time
;
194 uint32_t snapshotted_time
;
197 /*----------------------------------------------------------------
198 * superblock validator
199 *--------------------------------------------------------------*/
201 #define SUPERBLOCK_CSUM_XOR 160774
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_bm_checksum(&disk_super
->flags
,
211 block_size
- sizeof(__le32
),
212 SUPERBLOCK_CSUM_XOR
));
215 static int sb_check(struct dm_block_validator
*v
,
219 struct thin_disk_superblock
*disk_super
= dm_block_data(b
);
222 if (dm_block_location(b
) != le64_to_cpu(disk_super
->blocknr
)) {
223 DMERR("sb_check failed: blocknr %llu: "
224 "wanted %llu", le64_to_cpu(disk_super
->blocknr
),
225 (unsigned long long)dm_block_location(b
));
229 if (le64_to_cpu(disk_super
->magic
) != THIN_SUPERBLOCK_MAGIC
) {
230 DMERR("sb_check failed: magic %llu: "
231 "wanted %llu", le64_to_cpu(disk_super
->magic
),
232 (unsigned long long)THIN_SUPERBLOCK_MAGIC
);
236 csum_le
= cpu_to_le32(dm_bm_checksum(&disk_super
->flags
,
237 block_size
- sizeof(__le32
),
238 SUPERBLOCK_CSUM_XOR
));
239 if (csum_le
!= disk_super
->csum
) {
240 DMERR("sb_check failed: csum %u: wanted %u",
241 le32_to_cpu(csum_le
), le32_to_cpu(disk_super
->csum
));
248 static struct dm_block_validator sb_validator
= {
249 .name
= "superblock",
250 .prepare_for_write
= sb_prepare_for_write
,
254 /*----------------------------------------------------------------
255 * Methods for the btree value types
256 *--------------------------------------------------------------*/
258 static uint64_t pack_block_time(dm_block_t b
, uint32_t t
)
260 return (b
<< 24) | t
;
263 static void unpack_block_time(uint64_t v
, dm_block_t
*b
, uint32_t *t
)
266 *t
= v
& ((1 << 24) - 1);
269 static void data_block_inc(void *context
, void *value_le
)
271 struct dm_space_map
*sm
= context
;
276 memcpy(&v_le
, value_le
, sizeof(v_le
));
277 unpack_block_time(le64_to_cpu(v_le
), &b
, &t
);
278 dm_sm_inc_block(sm
, b
);
281 static void data_block_dec(void *context
, void *value_le
)
283 struct dm_space_map
*sm
= context
;
288 memcpy(&v_le
, value_le
, sizeof(v_le
));
289 unpack_block_time(le64_to_cpu(v_le
), &b
, &t
);
290 dm_sm_dec_block(sm
, b
);
293 static int data_block_equal(void *context
, void *value1_le
, void *value2_le
)
299 memcpy(&v1_le
, value1_le
, sizeof(v1_le
));
300 memcpy(&v2_le
, value2_le
, sizeof(v2_le
));
301 unpack_block_time(le64_to_cpu(v1_le
), &b1
, &t
);
302 unpack_block_time(le64_to_cpu(v2_le
), &b2
, &t
);
307 static void subtree_inc(void *context
, void *value
)
309 struct dm_btree_info
*info
= context
;
313 memcpy(&root_le
, value
, sizeof(root_le
));
314 root
= le64_to_cpu(root_le
);
315 dm_tm_inc(info
->tm
, root
);
318 static void subtree_dec(void *context
, void *value
)
320 struct dm_btree_info
*info
= context
;
324 memcpy(&root_le
, value
, sizeof(root_le
));
325 root
= le64_to_cpu(root_le
);
326 if (dm_btree_del(info
, root
))
327 DMERR("btree delete failed\n");
330 static int subtree_equal(void *context
, void *value1_le
, void *value2_le
)
333 memcpy(&v1_le
, value1_le
, sizeof(v1_le
));
334 memcpy(&v2_le
, value2_le
, sizeof(v2_le
));
336 return v1_le
== v2_le
;
339 /*----------------------------------------------------------------*/
341 static int superblock_all_zeroes(struct dm_block_manager
*bm
, int *result
)
346 __le64
*data_le
, zero
= cpu_to_le64(0);
347 unsigned block_size
= dm_bm_block_size(bm
) / sizeof(__le64
);
350 * We can't use a validator here - it may be all zeroes.
352 r
= dm_bm_read_lock(bm
, THIN_SUPERBLOCK_LOCATION
, NULL
, &b
);
356 data_le
= dm_block_data(b
);
358 for (i
= 0; i
< block_size
; i
++) {
359 if (data_le
[i
] != zero
) {
365 return dm_bm_unlock(b
);
368 static int init_pmd(struct dm_pool_metadata
*pmd
,
369 struct dm_block_manager
*bm
,
370 dm_block_t nr_blocks
, int create
)
373 struct dm_space_map
*sm
, *data_sm
;
374 struct dm_transaction_manager
*tm
;
375 struct dm_block
*sblock
;
378 r
= dm_tm_create_with_sm(bm
, THIN_SUPERBLOCK_LOCATION
,
379 &sb_validator
, &tm
, &sm
, &sblock
);
381 DMERR("tm_create_with_sm failed");
385 data_sm
= dm_sm_disk_create(tm
, nr_blocks
);
386 if (IS_ERR(data_sm
)) {
387 DMERR("sm_disk_create failed");
388 r
= PTR_ERR(data_sm
);
392 struct thin_disk_superblock
*disk_super
= NULL
;
393 size_t space_map_root_offset
=
394 offsetof(struct thin_disk_superblock
, metadata_space_map_root
);
396 r
= dm_tm_open_with_sm(bm
, THIN_SUPERBLOCK_LOCATION
,
397 &sb_validator
, space_map_root_offset
,
398 SPACE_MAP_ROOT_SIZE
, &tm
, &sm
, &sblock
);
400 DMERR("tm_open_with_sm failed");
404 disk_super
= dm_block_data(sblock
);
405 data_sm
= dm_sm_disk_open(tm
, disk_super
->data_space_map_root
,
406 sizeof(disk_super
->data_space_map_root
));
407 if (IS_ERR(data_sm
)) {
408 DMERR("sm_disk_open failed");
409 r
= PTR_ERR(data_sm
);
415 r
= dm_tm_unlock(tm
, sblock
);
417 DMERR("couldn't unlock superblock");
422 pmd
->metadata_sm
= sm
;
423 pmd
->data_sm
= data_sm
;
425 pmd
->nb_tm
= dm_tm_create_non_blocking_clone(tm
);
427 DMERR("could not create clone tm");
433 pmd
->info
.levels
= 2;
434 pmd
->info
.value_type
.context
= pmd
->data_sm
;
435 pmd
->info
.value_type
.size
= sizeof(__le64
);
436 pmd
->info
.value_type
.inc
= data_block_inc
;
437 pmd
->info
.value_type
.dec
= data_block_dec
;
438 pmd
->info
.value_type
.equal
= data_block_equal
;
440 memcpy(&pmd
->nb_info
, &pmd
->info
, sizeof(pmd
->nb_info
));
441 pmd
->nb_info
.tm
= pmd
->nb_tm
;
443 pmd
->tl_info
.tm
= tm
;
444 pmd
->tl_info
.levels
= 1;
445 pmd
->tl_info
.value_type
.context
= &pmd
->info
;
446 pmd
->tl_info
.value_type
.size
= sizeof(__le64
);
447 pmd
->tl_info
.value_type
.inc
= subtree_inc
;
448 pmd
->tl_info
.value_type
.dec
= subtree_dec
;
449 pmd
->tl_info
.value_type
.equal
= subtree_equal
;
451 pmd
->bl_info
.tm
= tm
;
452 pmd
->bl_info
.levels
= 1;
453 pmd
->bl_info
.value_type
.context
= pmd
->data_sm
;
454 pmd
->bl_info
.value_type
.size
= sizeof(__le64
);
455 pmd
->bl_info
.value_type
.inc
= data_block_inc
;
456 pmd
->bl_info
.value_type
.dec
= data_block_dec
;
457 pmd
->bl_info
.value_type
.equal
= data_block_equal
;
459 pmd
->details_info
.tm
= tm
;
460 pmd
->details_info
.levels
= 1;
461 pmd
->details_info
.value_type
.context
= NULL
;
462 pmd
->details_info
.value_type
.size
= sizeof(struct disk_device_details
);
463 pmd
->details_info
.value_type
.inc
= NULL
;
464 pmd
->details_info
.value_type
.dec
= NULL
;
465 pmd
->details_info
.value_type
.equal
= NULL
;
469 init_rwsem(&pmd
->root_lock
);
471 pmd
->need_commit
= 0;
472 pmd
->details_root
= 0;
475 INIT_LIST_HEAD(&pmd
->thin_devices
);
480 dm_sm_destroy(data_sm
);
488 static int __begin_transaction(struct dm_pool_metadata
*pmd
)
492 struct thin_disk_superblock
*disk_super
;
493 struct dm_block
*sblock
;
496 * __maybe_commit_transaction() resets these
498 WARN_ON(pmd
->need_commit
);
501 * We re-read the superblock every time. Shouldn't need to do this
504 r
= dm_bm_read_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
505 &sb_validator
, &sblock
);
509 disk_super
= dm_block_data(sblock
);
510 pmd
->time
= le32_to_cpu(disk_super
->time
);
511 pmd
->root
= le64_to_cpu(disk_super
->data_mapping_root
);
512 pmd
->details_root
= le64_to_cpu(disk_super
->device_details_root
);
513 pmd
->trans_id
= le64_to_cpu(disk_super
->trans_id
);
514 pmd
->flags
= le32_to_cpu(disk_super
->flags
);
515 pmd
->data_block_size
= le32_to_cpu(disk_super
->data_block_size
);
517 features
= le32_to_cpu(disk_super
->incompat_flags
) & ~THIN_FEATURE_INCOMPAT_SUPP
;
519 DMERR("could not access metadata due to "
520 "unsupported optional features (%lx).",
521 (unsigned long)features
);
527 * Check for read-only metadata to skip the following RDWR checks.
529 if (get_disk_ro(pmd
->bdev
->bd_disk
))
532 features
= le32_to_cpu(disk_super
->compat_ro_flags
) & ~THIN_FEATURE_COMPAT_RO_SUPP
;
534 DMERR("could not access metadata RDWR due to "
535 "unsupported optional features (%lx).",
536 (unsigned long)features
);
541 dm_bm_unlock(sblock
);
545 static int __write_changed_details(struct dm_pool_metadata
*pmd
)
548 struct dm_thin_device
*td
, *tmp
;
549 struct disk_device_details details
;
552 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
558 details
.mapped_blocks
= cpu_to_le64(td
->mapped_blocks
);
559 details
.transaction_id
= cpu_to_le64(td
->transaction_id
);
560 details
.creation_time
= cpu_to_le32(td
->creation_time
);
561 details
.snapshotted_time
= cpu_to_le32(td
->snapshotted_time
);
562 __dm_bless_for_disk(&details
);
564 r
= dm_btree_insert(&pmd
->details_info
, pmd
->details_root
,
565 &key
, &details
, &pmd
->details_root
);
576 pmd
->need_commit
= 1;
582 static int __commit_transaction(struct dm_pool_metadata
*pmd
)
585 * FIXME: Associated pool should be made read-only on failure.
588 size_t metadata_len
, data_len
;
589 struct thin_disk_superblock
*disk_super
;
590 struct dm_block
*sblock
;
593 * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
595 BUILD_BUG_ON(sizeof(struct thin_disk_superblock
) > 512);
597 r
= __write_changed_details(pmd
);
601 if (!pmd
->need_commit
)
604 r
= dm_sm_commit(pmd
->data_sm
);
608 r
= dm_tm_pre_commit(pmd
->tm
);
612 r
= dm_sm_root_size(pmd
->metadata_sm
, &metadata_len
);
616 r
= dm_sm_root_size(pmd
->metadata_sm
, &data_len
);
620 r
= dm_bm_write_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
621 &sb_validator
, &sblock
);
625 disk_super
= dm_block_data(sblock
);
626 disk_super
->time
= cpu_to_le32(pmd
->time
);
627 disk_super
->data_mapping_root
= cpu_to_le64(pmd
->root
);
628 disk_super
->device_details_root
= cpu_to_le64(pmd
->details_root
);
629 disk_super
->trans_id
= cpu_to_le64(pmd
->trans_id
);
630 disk_super
->flags
= cpu_to_le32(pmd
->flags
);
632 r
= dm_sm_copy_root(pmd
->metadata_sm
, &disk_super
->metadata_space_map_root
,
637 r
= dm_sm_copy_root(pmd
->data_sm
, &disk_super
->data_space_map_root
,
642 r
= dm_tm_commit(pmd
->tm
, sblock
);
644 pmd
->need_commit
= 0;
650 dm_bm_unlock(sblock
);
654 struct dm_pool_metadata
*dm_pool_metadata_open(struct block_device
*bdev
,
655 sector_t data_block_size
)
658 struct thin_disk_superblock
*disk_super
;
659 struct dm_pool_metadata
*pmd
;
660 sector_t bdev_size
= i_size_read(bdev
->bd_inode
) >> SECTOR_SHIFT
;
661 struct dm_block_manager
*bm
;
663 struct dm_block
*sblock
;
665 pmd
= kmalloc(sizeof(*pmd
), GFP_KERNEL
);
667 DMERR("could not allocate metadata struct");
668 return ERR_PTR(-ENOMEM
);
673 * 3 for btree insert +
674 * 2 for btree lookup used within space map
676 bm
= dm_block_manager_create(bdev
, THIN_METADATA_BLOCK_SIZE
,
677 THIN_METADATA_CACHE_SIZE
, 5);
679 DMERR("could not create block manager");
681 return ERR_PTR(-ENOMEM
);
684 r
= superblock_all_zeroes(bm
, &create
);
686 dm_block_manager_destroy(bm
);
692 r
= init_pmd(pmd
, bm
, 0, create
);
694 dm_block_manager_destroy(bm
);
701 r
= __begin_transaction(pmd
);
710 r
= dm_bm_write_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
711 &sb_validator
, &sblock
);
715 disk_super
= dm_block_data(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_bm_unlock(sblock
);
727 r
= dm_btree_empty(&pmd
->info
, &pmd
->root
);
731 r
= dm_btree_empty(&pmd
->details_info
, &pmd
->details_root
);
733 DMERR("couldn't create devices root");
738 pmd
->need_commit
= 1;
739 r
= dm_pool_commit_metadata(pmd
);
741 DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
749 if (dm_pool_metadata_close(pmd
) < 0)
750 DMWARN("%s: dm_pool_metadata_close() failed.", __func__
);
754 int dm_pool_metadata_close(struct dm_pool_metadata
*pmd
)
757 unsigned open_devices
= 0;
758 struct dm_thin_device
*td
, *tmp
;
760 down_read(&pmd
->root_lock
);
761 list_for_each_entry_safe(td
, tmp
, &pmd
->thin_devices
, list
) {
769 up_read(&pmd
->root_lock
);
772 DMERR("attempt to close pmd when %u device(s) are still open",
777 r
= __commit_transaction(pmd
);
779 DMWARN("%s: __commit_transaction() failed, error = %d",
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 static int __open_device(struct dm_pool_metadata
*pmd
,
793 dm_thin_id dev
, int create
,
794 struct dm_thin_device
**td
)
797 struct dm_thin_device
*td2
;
799 struct disk_device_details details_le
;
802 * Check the device isn't already open.
804 list_for_each_entry(td2
, &pmd
->thin_devices
, list
)
805 if (td2
->id
== dev
) {
812 * Check the device exists.
814 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
817 if (r
!= -ENODATA
|| !create
)
821 details_le
.mapped_blocks
= 0;
822 details_le
.transaction_id
= cpu_to_le64(pmd
->trans_id
);
823 details_le
.creation_time
= cpu_to_le32(pmd
->time
);
824 details_le
.snapshotted_time
= cpu_to_le32(pmd
->time
);
827 *td
= kmalloc(sizeof(**td
), GFP_NOIO
);
833 (*td
)->open_count
= 1;
834 (*td
)->changed
= changed
;
835 (*td
)->mapped_blocks
= le64_to_cpu(details_le
.mapped_blocks
);
836 (*td
)->transaction_id
= le64_to_cpu(details_le
.transaction_id
);
837 (*td
)->creation_time
= le32_to_cpu(details_le
.creation_time
);
838 (*td
)->snapshotted_time
= le32_to_cpu(details_le
.snapshotted_time
);
840 list_add(&(*td
)->list
, &pmd
->thin_devices
);
845 static void __close_device(struct dm_thin_device
*td
)
850 static int __create_thin(struct dm_pool_metadata
*pmd
,
856 struct disk_device_details details_le
;
857 struct dm_thin_device
*td
;
860 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
866 * Create an empty btree for the mappings.
868 r
= dm_btree_empty(&pmd
->bl_info
, &dev_root
);
873 * Insert it into the main mapping tree.
875 value
= cpu_to_le64(dev_root
);
876 __dm_bless_for_disk(&value
);
877 r
= dm_btree_insert(&pmd
->tl_info
, pmd
->root
, &key
, &value
, &pmd
->root
);
879 dm_btree_del(&pmd
->bl_info
, dev_root
);
883 r
= __open_device(pmd
, dev
, 1, &td
);
886 dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
887 dm_btree_del(&pmd
->bl_info
, dev_root
);
896 int dm_pool_create_thin(struct dm_pool_metadata
*pmd
, dm_thin_id dev
)
900 down_write(&pmd
->root_lock
);
901 r
= __create_thin(pmd
, dev
);
902 up_write(&pmd
->root_lock
);
907 static int __set_snapshot_details(struct dm_pool_metadata
*pmd
,
908 struct dm_thin_device
*snap
,
909 dm_thin_id origin
, uint32_t time
)
912 struct dm_thin_device
*td
;
914 r
= __open_device(pmd
, origin
, 0, &td
);
919 td
->snapshotted_time
= time
;
921 snap
->mapped_blocks
= td
->mapped_blocks
;
922 snap
->snapshotted_time
= time
;
928 static int __create_snap(struct dm_pool_metadata
*pmd
,
929 dm_thin_id dev
, dm_thin_id origin
)
932 dm_block_t origin_root
;
933 uint64_t key
= origin
, dev_key
= dev
;
934 struct dm_thin_device
*td
;
935 struct disk_device_details details_le
;
938 /* check this device is unused */
939 r
= dm_btree_lookup(&pmd
->details_info
, pmd
->details_root
,
940 &dev_key
, &details_le
);
944 /* find the mapping tree for the origin */
945 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, &key
, &value
);
948 origin_root
= le64_to_cpu(value
);
950 /* clone the origin, an inc will do */
951 dm_tm_inc(pmd
->tm
, origin_root
);
953 /* insert into the main mapping tree */
954 value
= cpu_to_le64(origin_root
);
955 __dm_bless_for_disk(&value
);
957 r
= dm_btree_insert(&pmd
->tl_info
, pmd
->root
, &key
, &value
, &pmd
->root
);
959 dm_tm_dec(pmd
->tm
, origin_root
);
965 r
= __open_device(pmd
, dev
, 1, &td
);
969 r
= __set_snapshot_details(pmd
, td
, origin
, pmd
->time
);
978 dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
979 dm_btree_remove(&pmd
->details_info
, pmd
->details_root
,
980 &key
, &pmd
->details_root
);
984 int dm_pool_create_snap(struct dm_pool_metadata
*pmd
,
990 down_write(&pmd
->root_lock
);
991 r
= __create_snap(pmd
, dev
, origin
);
992 up_write(&pmd
->root_lock
);
997 static int __delete_device(struct dm_pool_metadata
*pmd
, dm_thin_id dev
)
1001 struct dm_thin_device
*td
;
1003 /* TODO: failure should mark the transaction invalid */
1004 r
= __open_device(pmd
, dev
, 0, &td
);
1008 if (td
->open_count
> 1) {
1013 list_del(&td
->list
);
1015 r
= dm_btree_remove(&pmd
->details_info
, pmd
->details_root
,
1016 &key
, &pmd
->details_root
);
1020 r
= dm_btree_remove(&pmd
->tl_info
, pmd
->root
, &key
, &pmd
->root
);
1024 pmd
->need_commit
= 1;
1029 int dm_pool_delete_thin_device(struct dm_pool_metadata
*pmd
,
1034 down_write(&pmd
->root_lock
);
1035 r
= __delete_device(pmd
, dev
);
1036 up_write(&pmd
->root_lock
);
1041 int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata
*pmd
,
1042 uint64_t current_id
,
1045 down_write(&pmd
->root_lock
);
1046 if (pmd
->trans_id
!= current_id
) {
1047 up_write(&pmd
->root_lock
);
1048 DMERR("mismatched transaction id");
1052 pmd
->trans_id
= new_id
;
1053 pmd
->need_commit
= 1;
1054 up_write(&pmd
->root_lock
);
1059 int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata
*pmd
,
1062 down_read(&pmd
->root_lock
);
1063 *result
= pmd
->trans_id
;
1064 up_read(&pmd
->root_lock
);
1069 static int __get_held_metadata_root(struct dm_pool_metadata
*pmd
,
1073 struct thin_disk_superblock
*disk_super
;
1074 struct dm_block
*sblock
;
1076 r
= dm_bm_write_lock(pmd
->bm
, THIN_SUPERBLOCK_LOCATION
,
1077 &sb_validator
, &sblock
);
1081 disk_super
= dm_block_data(sblock
);
1082 *result
= le64_to_cpu(disk_super
->held_root
);
1084 return dm_bm_unlock(sblock
);
1087 int dm_pool_get_held_metadata_root(struct dm_pool_metadata
*pmd
,
1092 down_read(&pmd
->root_lock
);
1093 r
= __get_held_metadata_root(pmd
, result
);
1094 up_read(&pmd
->root_lock
);
1099 int dm_pool_open_thin_device(struct dm_pool_metadata
*pmd
, dm_thin_id dev
,
1100 struct dm_thin_device
**td
)
1104 down_write(&pmd
->root_lock
);
1105 r
= __open_device(pmd
, dev
, 0, td
);
1106 up_write(&pmd
->root_lock
);
1111 int dm_pool_close_thin_device(struct dm_thin_device
*td
)
1113 down_write(&td
->pmd
->root_lock
);
1115 up_write(&td
->pmd
->root_lock
);
1120 dm_thin_id
dm_thin_dev_id(struct dm_thin_device
*td
)
1125 static int __snapshotted_since(struct dm_thin_device
*td
, uint32_t time
)
1127 return td
->snapshotted_time
> time
;
1130 int dm_thin_find_block(struct dm_thin_device
*td
, dm_block_t block
,
1131 int can_block
, struct dm_thin_lookup_result
*result
)
1134 uint64_t block_time
= 0;
1136 struct dm_pool_metadata
*pmd
= td
->pmd
;
1137 dm_block_t keys
[2] = { td
->id
, block
};
1140 down_read(&pmd
->root_lock
);
1141 r
= dm_btree_lookup(&pmd
->info
, pmd
->root
, keys
, &value
);
1143 block_time
= le64_to_cpu(value
);
1144 up_read(&pmd
->root_lock
);
1146 } else if (down_read_trylock(&pmd
->root_lock
)) {
1147 r
= dm_btree_lookup(&pmd
->nb_info
, pmd
->root
, keys
, &value
);
1149 block_time
= le64_to_cpu(value
);
1150 up_read(&pmd
->root_lock
);
1153 return -EWOULDBLOCK
;
1156 dm_block_t exception_block
;
1157 uint32_t exception_time
;
1158 unpack_block_time(block_time
, &exception_block
,
1160 result
->block
= exception_block
;
1161 result
->shared
= __snapshotted_since(td
, exception_time
);
1167 static int __insert(struct dm_thin_device
*td
, dm_block_t block
,
1168 dm_block_t data_block
)
1172 struct dm_pool_metadata
*pmd
= td
->pmd
;
1173 dm_block_t keys
[2] = { td
->id
, block
};
1175 pmd
->need_commit
= 1;
1176 value
= cpu_to_le64(pack_block_time(data_block
, pmd
->time
));
1177 __dm_bless_for_disk(&value
);
1179 r
= dm_btree_insert_notify(&pmd
->info
, pmd
->root
, keys
, &value
,
1180 &pmd
->root
, &inserted
);
1185 td
->mapped_blocks
++;
1192 int dm_thin_insert_block(struct dm_thin_device
*td
, dm_block_t block
,
1193 dm_block_t data_block
)
1197 down_write(&td
->pmd
->root_lock
);
1198 r
= __insert(td
, block
, data_block
);
1199 up_write(&td
->pmd
->root_lock
);
1204 static int __remove(struct dm_thin_device
*td
, dm_block_t block
)
1207 struct dm_pool_metadata
*pmd
= td
->pmd
;
1208 dm_block_t keys
[2] = { td
->id
, block
};
1210 r
= dm_btree_remove(&pmd
->info
, pmd
->root
, keys
, &pmd
->root
);
1214 pmd
->need_commit
= 1;
1219 int dm_thin_remove_block(struct dm_thin_device
*td
, dm_block_t block
)
1223 down_write(&td
->pmd
->root_lock
);
1224 r
= __remove(td
, block
);
1225 up_write(&td
->pmd
->root_lock
);
1230 int dm_pool_alloc_data_block(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1234 down_write(&pmd
->root_lock
);
1236 r
= dm_sm_new_block(pmd
->data_sm
, result
);
1237 pmd
->need_commit
= 1;
1239 up_write(&pmd
->root_lock
);
1244 int dm_pool_commit_metadata(struct dm_pool_metadata
*pmd
)
1248 down_write(&pmd
->root_lock
);
1250 r
= __commit_transaction(pmd
);
1255 * Open the next transaction.
1257 r
= __begin_transaction(pmd
);
1259 up_write(&pmd
->root_lock
);
1263 int dm_pool_get_free_block_count(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1267 down_read(&pmd
->root_lock
);
1268 r
= dm_sm_get_nr_free(pmd
->data_sm
, result
);
1269 up_read(&pmd
->root_lock
);
1274 int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata
*pmd
,
1279 down_read(&pmd
->root_lock
);
1280 r
= dm_sm_get_nr_free(pmd
->metadata_sm
, result
);
1281 up_read(&pmd
->root_lock
);
1286 int dm_pool_get_metadata_dev_size(struct dm_pool_metadata
*pmd
,
1291 down_read(&pmd
->root_lock
);
1292 r
= dm_sm_get_nr_blocks(pmd
->metadata_sm
, result
);
1293 up_read(&pmd
->root_lock
);
1298 int dm_pool_get_data_block_size(struct dm_pool_metadata
*pmd
, sector_t
*result
)
1300 down_read(&pmd
->root_lock
);
1301 *result
= pmd
->data_block_size
;
1302 up_read(&pmd
->root_lock
);
1307 int dm_pool_get_data_dev_size(struct dm_pool_metadata
*pmd
, dm_block_t
*result
)
1311 down_read(&pmd
->root_lock
);
1312 r
= dm_sm_get_nr_blocks(pmd
->data_sm
, result
);
1313 up_read(&pmd
->root_lock
);
1318 int dm_thin_get_mapped_count(struct dm_thin_device
*td
, dm_block_t
*result
)
1320 struct dm_pool_metadata
*pmd
= td
->pmd
;
1322 down_read(&pmd
->root_lock
);
1323 *result
= td
->mapped_blocks
;
1324 up_read(&pmd
->root_lock
);
1329 static int __highest_block(struct dm_thin_device
*td
, dm_block_t
*result
)
1333 dm_block_t thin_root
;
1334 struct dm_pool_metadata
*pmd
= td
->pmd
;
1336 r
= dm_btree_lookup(&pmd
->tl_info
, pmd
->root
, &td
->id
, &value_le
);
1340 thin_root
= le64_to_cpu(value_le
);
1342 return dm_btree_find_highest_key(&pmd
->bl_info
, thin_root
, result
);
1345 int dm_thin_get_highest_mapped_block(struct dm_thin_device
*td
,
1349 struct dm_pool_metadata
*pmd
= td
->pmd
;
1351 down_read(&pmd
->root_lock
);
1352 r
= __highest_block(td
, result
);
1353 up_read(&pmd
->root_lock
);
1358 static int __resize_data_dev(struct dm_pool_metadata
*pmd
, dm_block_t new_count
)
1361 dm_block_t old_count
;
1363 r
= dm_sm_get_nr_blocks(pmd
->data_sm
, &old_count
);
1367 if (new_count
== old_count
)
1370 if (new_count
< old_count
) {
1371 DMERR("cannot reduce size of data device");
1375 r
= dm_sm_extend(pmd
->data_sm
, new_count
- old_count
);
1377 pmd
->need_commit
= 1;
1382 int dm_pool_resize_data_dev(struct dm_pool_metadata
*pmd
, dm_block_t new_count
)
1386 down_write(&pmd
->root_lock
);
1387 r
= __resize_data_dev(pmd
, new_count
);
1388 up_write(&pmd
->root_lock
);