Add linux-next specific files for 20110801
[linux-2.6/next.git] / drivers / md / dm-thin-metadata.c
blob063d4bc0b868597de2be41c212602bc800b320aa
1 /*
2 * Copyright (C) 2011 Red Hat, Inc. All rights reserved.
4 * This file is released under the GPL.
5 */
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
21 * atomic writes.
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
32 * bits.
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
40 * cpu cache.
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
46 * are etc.
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:
51 * 0 - ref count is 0
52 * 1 - ref count is 1
53 * 2 - ref count is 2
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
58 * count.
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. */
92 __le32 flags;
93 __le64 blocknr; /* This block number, dm_block_t. */
95 __u8 uuid[16];
96 __le64 magic;
97 __le32 version;
98 __le32 time;
100 __le64 trans_id;
103 * Root held by userspace transactions.
105 __le64 held_root;
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;
125 __le32 compat_flags;
126 __le32 compat_ro_flags;
127 __le32 incompat_flags;
128 } __packed;
130 struct disk_device_details {
131 __le64 mapped_blocks;
132 __le64 transaction_id; /* When created. */
133 __le32 creation_time;
134 __le32 snapshotted_time;
135 } __packed;
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;
148 * Two-level btree.
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;
175 uint32_t time;
176 int need_commit;
177 struct dm_block *sblock;
178 dm_block_t root;
179 dm_block_t details_root;
180 struct list_head thin_devices;
181 uint64_t trans_id;
182 unsigned long flags;
183 sector_t data_block_size;
186 struct dm_thin_device {
187 struct list_head list;
188 struct dm_pool_metadata *pmd;
189 dm_thin_id id;
191 int open_count;
192 int changed;
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,
204 struct dm_block *b,
205 size_t block_size)
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,
214 struct dm_block *b,
215 size_t block_size)
217 struct thin_disk_superblock *disk_super = dm_block_data(b);
218 __le32 csum_le;
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));
224 return -ENOTBLK;
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);
231 return -EILSEQ;
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));
238 return -EILSEQ;
241 return 0;
244 static struct dm_block_validator sb_validator = {
245 .name = "superblock",
246 .prepare_for_write = sb_prepare_for_write,
247 .check = sb_check
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)
261 *b = v >> 24;
262 *t = v & ((1 << 24) - 1);
265 static void data_block_inc(void *context, void *value_le)
267 struct dm_space_map *sm = context;
268 __le64 v_le;
269 uint64_t b;
270 uint32_t t;
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;
280 __le64 v_le;
281 uint64_t b;
282 uint32_t t;
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)
291 __le64 v1_le, v2_le;
292 uint64_t b1, b2;
293 uint32_t t;
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);
300 return b1 == b2;
303 static void subtree_inc(void *context, void *value)
305 struct dm_btree_info *info = context;
306 __le64 root_le;
307 uint64_t root;
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;
317 __le64 root_le;
318 uint64_t root;
320 memcpy(&root_le, value, sizeof(root_le));
321 root = le64_to_cpu(root_le);
322 if (dm_btree_del(info, root))
323 DMERR("btree delete failed\n");
326 static int subtree_equal(void *context, void *value1_le, void *value2_le)
328 __le64 v1_le, v2_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)
339 int r;
340 unsigned i;
341 struct dm_block *b;
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);
349 if (r)
350 return r;
352 data_le = dm_block_data(b);
353 *result = 1;
354 for (i = 0; i < block_size; i++) {
355 if (data_le[i] != zero) {
356 *result = 0;
357 break;
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)
367 int r;
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;
373 if (create) {
374 r = dm_tm_create_with_sm(bm, THIN_SUPERBLOCK_LOCATION,
375 &sb_validator, &tm, &sm, &sblock);
376 if (r < 0) {
377 DMERR("tm_create_with_sm failed");
378 return ERR_PTR(r);
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);
385 goto bad;
388 r = dm_tm_pre_commit(tm);
389 if (r < 0) {
390 DMERR("couldn't pre commit");
391 goto bad_data_sm;
394 r = dm_tm_commit(tm, sblock);
395 if (r < 0) {
396 DMERR("couldn't commit");
397 goto bad_data_sm;
399 } else {
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);
407 if (r < 0) {
408 DMERR("tm_open_with_sm failed");
409 return ERR_PTR(r);
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);
418 goto bad;
421 dm_tm_unlock(tm, sblock);
424 pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
425 if (!pmd) {
426 DMERR("could not allocate metadata struct");
427 r = -ENOMEM;
428 goto bad_data_sm;
431 pmd->bm = bm;
432 pmd->metadata_sm = sm;
433 pmd->data_sm = data_sm;
434 pmd->tm = tm;
435 pmd->nb_tm = dm_tm_create_non_blocking_clone(tm);
436 if (!pmd->nb_tm) {
437 DMERR("could not create clone tm");
438 r = -ENOMEM;
439 goto bad_pmd;
442 pmd->sblock = NULL;
444 pmd->info.tm = 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;
479 pmd->root = 0;
481 init_rwsem(&pmd->root_lock);
482 pmd->time = 0;
483 pmd->need_commit = 0;
484 pmd->details_root = 0;
485 INIT_LIST_HEAD(&pmd->thin_devices);
487 return pmd;
489 bad_pmd:
490 kfree(pmd);
491 bad_data_sm:
492 dm_sm_destroy(data_sm);
493 bad:
494 dm_tm_destroy(tm);
495 dm_sm_destroy(sm);
497 return ERR_PTR(r);
500 static int begin_transaction(struct dm_pool_metadata *pmd)
502 int r;
503 u32 features;
504 struct thin_disk_superblock *disk_super;
507 * dm_pool_commit_metadata() resets pmd->sblock
509 WARN_ON(pmd->sblock);
510 pmd->need_commit = 0;
513 * superblock is unlocked via dm_tm_commit()
515 r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
516 &sb_validator, &pmd->sblock);
517 if (r)
518 return r;
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;
529 if (features) {
530 DMERR("could not access metadata due to "
531 "unsupported optional features (%lx).",
532 (unsigned long)features);
533 return -EINVAL;
537 * Check for read-only metadata to skip the following RDWR checks.
539 if (get_disk_ro(pmd->bdev->bd_disk))
540 return 0;
542 features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
543 if (features) {
544 DMERR("could not access metadata RDWR due to "
545 "unsupported optional features (%lx).",
546 (unsigned long)features);
547 return -EINVAL;
550 return 0;
553 struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
554 sector_t data_block_size)
556 int r;
557 struct thin_disk_superblock *disk_super;
558 struct dm_pool_metadata *pmd;
559 sector_t bdev_size = i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
560 struct dm_block_manager *bm;
561 int create;
563 bm = dm_block_manager_create(bdev, THIN_METADATA_BLOCK_SIZE,
564 THIN_METADATA_CACHE_SIZE);
565 if (!bm) {
566 DMERR("could not create block manager");
567 return ERR_PTR(-ENOMEM);
570 r = superblock_all_zeroes(bm, &create);
571 if (r) {
572 dm_block_manager_destroy(bm);
573 return ERR_PTR(r);
576 pmd = alloc_pmd(bm, 0, create);
577 if (IS_ERR(pmd)) {
578 dm_block_manager_destroy(bm);
579 return pmd;
581 pmd->bdev = bdev;
583 if (!create) {
584 r = begin_transaction(pmd);
585 if (r < 0)
586 goto bad;
587 return pmd;
591 * Create.
593 if (!pmd->sblock) {
594 r = begin_transaction(pmd);
595 if (r < 0)
596 goto bad;
599 disk_super = dm_block_data(pmd->sblock);
600 disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
601 disk_super->version = cpu_to_le32(THIN_VERSION);
602 disk_super->time = 0;
603 disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
604 disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
605 disk_super->data_block_size = cpu_to_le32(data_block_size);
607 r = dm_btree_empty(&pmd->info, &pmd->root);
608 if (r < 0)
609 goto bad;
611 r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
612 if (r < 0) {
613 DMERR("couldn't create devices root");
614 goto bad;
617 pmd->flags = 0;
618 pmd->need_commit = 1;
619 r = dm_pool_commit_metadata(pmd);
620 if (r < 0) {
621 DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
622 __func__, r);
623 goto bad;
626 return pmd;
628 bad:
629 if (dm_pool_metadata_close(pmd) < 0)
630 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
631 return ERR_PTR(r);
634 int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
636 int r;
637 unsigned open_devices = 0;
638 struct dm_thin_device *td, *tmp;
640 down_read(&pmd->root_lock);
641 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
642 if (td->open_count)
643 open_devices++;
644 else {
645 list_del(&td->list);
646 kfree(td);
649 up_read(&pmd->root_lock);
651 if (open_devices) {
652 DMERR("attempt to close pmd when %u device(s) are still open",
653 open_devices);
654 return -EBUSY;
657 if (pmd->sblock) {
658 r = dm_pool_commit_metadata(pmd);
659 if (r)
660 DMWARN("%s: dm_pool_commit_metadata() failed, error = %d",
661 __func__, r);
664 dm_tm_destroy(pmd->tm);
665 dm_tm_destroy(pmd->nb_tm);
666 dm_block_manager_destroy(pmd->bm);
667 dm_sm_destroy(pmd->metadata_sm);
668 dm_sm_destroy(pmd->data_sm);
669 kfree(pmd);
671 return 0;
674 int dm_pool_rebind_metadata_device(struct dm_pool_metadata *pmd,
675 struct block_device *bdev)
677 return dm_bm_rebind_block_device(pmd->bm, bdev);
680 static int __open_device(struct dm_pool_metadata *pmd,
681 dm_thin_id dev, int create,
682 struct dm_thin_device **td)
684 int r, changed = 0;
685 struct dm_thin_device *td2;
686 uint64_t key = dev;
687 struct disk_device_details details_le;
690 * If the device is already open, just increment its open_count.
692 list_for_each_entry(td2, &pmd->thin_devices, list)
693 if (td2->id == dev) {
694 td2->open_count++;
695 *td = td2;
696 return 0;
700 * Check the device exists.
702 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
703 &key, &details_le);
704 if (r) {
705 if (r != -ENODATA || !create)
706 return r;
709 * New device.
711 changed = 1;
712 details_le.mapped_blocks = 0;
713 details_le.transaction_id = cpu_to_le64(pmd->trans_id);
714 details_le.creation_time = cpu_to_le32(pmd->time);
715 details_le.snapshotted_time = cpu_to_le32(pmd->time);
718 *td = kmalloc(sizeof(**td), GFP_NOIO);
719 if (!*td)
720 return -ENOMEM;
722 (*td)->pmd = pmd;
723 (*td)->id = dev;
724 (*td)->open_count = 1;
725 (*td)->changed = changed;
726 (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
727 (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
728 (*td)->creation_time = le32_to_cpu(details_le.creation_time);
729 (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
731 list_add(&(*td)->list, &pmd->thin_devices);
733 return 0;
736 static void __close_device(struct dm_thin_device *td)
738 --td->open_count;
741 static int __create_thin(struct dm_pool_metadata *pmd,
742 dm_thin_id dev)
744 int r;
745 dm_block_t dev_root;
746 uint64_t key = dev;
747 struct disk_device_details details_le;
748 struct dm_thin_device *td;
749 __le64 value;
751 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
752 &key, &details_le);
753 if (!r)
754 return -EEXIST;
757 * Create an empty btree for the mappings.
759 r = dm_btree_empty(&pmd->bl_info, &dev_root);
760 if (r)
761 return r;
764 * Insert it into the main mapping tree.
766 value = cpu_to_le64(dev_root);
767 __dm_bless_for_disk(&value);
768 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
769 if (r) {
770 dm_btree_del(&pmd->bl_info, dev_root);
771 return r;
774 r = __open_device(pmd, dev, 1, &td);
775 if (r) {
776 __close_device(td);
777 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
778 dm_btree_del(&pmd->bl_info, dev_root);
779 return r;
781 td->changed = 1;
782 __close_device(td);
784 return r;
787 int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
789 int r;
791 down_write(&pmd->root_lock);
792 r = __create_thin(pmd, dev);
793 up_write(&pmd->root_lock);
795 return r;
798 static int __set_snapshot_details(struct dm_pool_metadata *pmd,
799 struct dm_thin_device *snap,
800 dm_thin_id origin, uint32_t time)
802 int r;
803 struct dm_thin_device *td;
805 r = __open_device(pmd, origin, 0, &td);
806 if (r)
807 return r;
809 td->changed = 1;
810 td->snapshotted_time = time;
812 snap->mapped_blocks = td->mapped_blocks;
813 snap->snapshotted_time = time;
814 __close_device(td);
816 return 0;
819 static int __create_snap(struct dm_pool_metadata *pmd,
820 dm_thin_id dev, dm_thin_id origin)
822 int r;
823 dm_block_t origin_root, snap_root;
824 uint64_t key = origin, dev_key = dev;
825 struct dm_thin_device *td;
826 struct disk_device_details details_le;
827 __le64 value;
829 /* check this device is unused */
830 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
831 &dev_key, &details_le);
832 if (!r)
833 return -EEXIST;
835 /* find the mapping tree for the origin */
836 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
837 if (r)
838 return r;
839 origin_root = le64_to_cpu(value);
841 /* clone the origin */
842 r = dm_btree_clone(&pmd->bl_info, origin_root, &snap_root);
843 if (r)
844 return r;
846 /* insert into the main mapping tree */
847 value = cpu_to_le64(snap_root);
848 __dm_bless_for_disk(&value);
849 key = dev;
850 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
851 if (r) {
852 dm_btree_del(&pmd->bl_info, snap_root);
853 return r;
856 pmd->time++;
858 r = __open_device(pmd, dev, 1, &td);
859 if (r)
860 goto bad;
862 r = __set_snapshot_details(pmd, td, origin, pmd->time);
863 if (r)
864 goto bad;
866 __close_device(td);
867 return 0;
869 bad:
870 __close_device(td);
871 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
872 dm_btree_remove(&pmd->details_info, pmd->details_root,
873 &key, &pmd->details_root);
874 return r;
877 int dm_pool_create_snap(struct dm_pool_metadata *pmd,
878 dm_thin_id dev,
879 dm_thin_id origin)
881 int r;
883 down_write(&pmd->root_lock);
884 r = __create_snap(pmd, dev, origin);
885 up_write(&pmd->root_lock);
887 return r;
890 static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
892 int r;
893 uint64_t key = dev;
894 struct dm_thin_device *td;
896 /* TODO: failure should mark the transaction invalid */
897 r = __open_device(pmd, dev, 0, &td);
898 if (r)
899 return r;
901 if (td->open_count > 1) {
902 __close_device(td);
903 return -EBUSY;
906 list_del(&td->list);
907 kfree(td);
908 r = dm_btree_remove(&pmd->details_info, pmd->details_root,
909 &key, &pmd->details_root);
910 if (r)
911 return r;
913 r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
914 if (r)
915 return r;
917 pmd->need_commit = 1;
919 return 0;
922 int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
923 dm_thin_id dev)
925 int r;
927 down_write(&pmd->root_lock);
928 r = __delete_device(pmd, dev);
929 up_write(&pmd->root_lock);
931 return r;
934 static int __trim_thin_dev(struct dm_thin_device *td, sector_t new_size)
936 struct dm_pool_metadata *pmd = td->pmd;
937 /* FIXME: convert new size to blocks */
938 uint64_t key[2] = { td->id, new_size - 1 };
940 td->changed = 1;
943 * We need to truncate all the extraneous mappings.
945 * FIXME: We have to be careful to do this atomically.
946 * Perhaps clone the bottom layer first so we can revert?
948 return dm_btree_del_gt(&pmd->info, pmd->root, key, &pmd->root);
951 int dm_pool_trim_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
952 sector_t new_size)
954 int r;
955 struct dm_thin_device *td;
957 down_write(&pmd->root_lock);
958 r = __open_device(pmd, dev, 1, &td);
959 if (r)
960 DMERR("couldn't open virtual device");
961 else {
962 r = __trim_thin_dev(td, new_size);
963 __close_device(td);
966 /* FIXME: update mapped_blocks */
968 up_write(&pmd->root_lock);
970 return r;
973 int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
974 uint64_t current_id,
975 uint64_t new_id)
977 down_write(&pmd->root_lock);
978 if (pmd->trans_id != current_id) {
979 up_write(&pmd->root_lock);
980 DMERR("mismatched transaction id");
981 return -EINVAL;
984 pmd->trans_id = new_id;
985 pmd->need_commit = 1;
986 up_write(&pmd->root_lock);
988 return 0;
991 int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
992 uint64_t *result)
994 down_read(&pmd->root_lock);
995 *result = pmd->trans_id;
996 up_read(&pmd->root_lock);
998 return 0;
1001 int dm_pool_get_held_metadata_root(struct dm_pool_metadata *pmd,
1002 dm_block_t *result)
1004 struct thin_disk_superblock *disk_super;
1006 down_read(&pmd->root_lock);
1007 disk_super = dm_block_data(pmd->sblock);
1008 *result = le64_to_cpu(disk_super->held_root);
1009 up_read(&pmd->root_lock);
1011 return 0;
1014 int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
1015 struct dm_thin_device **td)
1017 int r;
1019 down_write(&pmd->root_lock);
1020 r = __open_device(pmd, dev, 0, td);
1021 up_write(&pmd->root_lock);
1023 return r;
1026 int dm_pool_close_thin_device(struct dm_thin_device *td)
1028 down_write(&td->pmd->root_lock);
1029 __close_device(td);
1030 up_write(&td->pmd->root_lock);
1032 return 0;
1035 dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
1037 return td->id;
1040 static int __snapshotted_since(struct dm_thin_device *td, uint32_t time)
1042 return td->snapshotted_time > time;
1045 int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
1046 int can_block, struct dm_thin_lookup_result *result)
1048 int r;
1049 uint64_t block_time = 0;
1050 __le64 value;
1051 struct dm_pool_metadata *pmd = td->pmd;
1052 dm_block_t keys[2] = { td->id, block };
1054 if (can_block) {
1055 down_read(&pmd->root_lock);
1056 r = dm_btree_lookup(&pmd->info, pmd->root, keys, &value);
1057 if (!r)
1058 block_time = le64_to_cpu(value);
1059 up_read(&pmd->root_lock);
1061 } else if (down_read_trylock(&pmd->root_lock)) {
1062 r = dm_btree_lookup(&pmd->nb_info, pmd->root, keys, &value);
1063 if (!r)
1064 block_time = le64_to_cpu(value);
1065 up_read(&pmd->root_lock);
1067 } else
1068 return -EWOULDBLOCK;
1070 if (!r) {
1071 dm_block_t exception_block;
1072 uint32_t exception_time;
1073 unpack_block_time(block_time, &exception_block,
1074 &exception_time);
1075 result->block = exception_block;
1076 result->shared = __snapshotted_since(td, exception_time);
1079 return r;
1082 static int __insert(struct dm_thin_device *td, dm_block_t block,
1083 dm_block_t data_block)
1085 int r, inserted;
1086 __le64 value;
1087 struct dm_pool_metadata *pmd = td->pmd;
1088 dm_block_t keys[2] = { td->id, block };
1090 pmd->need_commit = 1;
1091 value = cpu_to_le64(pack_block_time(data_block, pmd->time));
1092 __dm_bless_for_disk(&value);
1094 r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
1095 &pmd->root, &inserted);
1096 if (r)
1097 return r;
1099 if (inserted) {
1100 td->mapped_blocks++;
1101 td->changed = 1;
1104 return 0;
1107 int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
1108 dm_block_t data_block)
1110 int r;
1112 down_write(&td->pmd->root_lock);
1113 r = __insert(td, block, data_block);
1114 up_write(&td->pmd->root_lock);
1116 return r;
1119 static int __remove(struct dm_thin_device *td, dm_block_t block)
1121 int r;
1122 struct dm_pool_metadata *pmd = td->pmd;
1123 dm_block_t keys[2] = { td->id, block };
1125 r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
1126 if (r)
1127 return r;
1129 pmd->need_commit = 1;
1131 return 0;
1134 int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
1136 int r;
1138 down_write(&td->pmd->root_lock);
1139 r = __remove(td, block);
1140 up_write(&td->pmd->root_lock);
1142 return r;
1145 int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
1147 int r;
1149 down_write(&pmd->root_lock);
1151 r = dm_sm_new_block(pmd->data_sm, result);
1152 pmd->need_commit = 1;
1154 up_write(&pmd->root_lock);
1156 return r;
1159 static int __write_changed_details(struct dm_pool_metadata *pmd)
1161 int r;
1162 struct dm_thin_device *td, *tmp;
1163 struct disk_device_details details;
1164 uint64_t key;
1166 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
1167 if (!td->changed)
1168 continue;
1170 key = td->id;
1172 details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
1173 details.transaction_id = cpu_to_le64(td->transaction_id);
1174 details.creation_time = cpu_to_le32(td->creation_time);
1175 details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
1176 __dm_bless_for_disk(&details);
1178 r = dm_btree_insert(&pmd->details_info, pmd->details_root,
1179 &key, &details, &pmd->details_root);
1180 if (r)
1181 return r;
1183 if (td->open_count)
1184 td->changed = 0;
1185 else {
1186 list_del(&td->list);
1187 kfree(td);
1190 pmd->need_commit = 1;
1193 return 0;
1196 int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
1199 * FIXME: associated pool should be made read-only on
1200 * dm_pool_commit_metadata failure.
1202 int r;
1203 size_t len;
1204 struct thin_disk_superblock *disk_super;
1207 * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
1209 BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
1211 down_write(&pmd->root_lock);
1212 r = __write_changed_details(pmd);
1213 if (r < 0)
1214 goto out;
1216 if (!pmd->need_commit)
1217 goto out;
1219 r = dm_tm_pre_commit(pmd->tm);
1220 if (r < 0)
1221 goto out;
1223 r = dm_sm_root_size(pmd->metadata_sm, &len);
1224 if (r < 0)
1225 goto out;
1227 disk_super = dm_block_data(pmd->sblock);
1228 disk_super->time = cpu_to_le32(pmd->time);
1229 disk_super->data_mapping_root = cpu_to_le64(pmd->root);
1230 disk_super->device_details_root = cpu_to_le64(pmd->details_root);
1231 disk_super->trans_id = cpu_to_le64(pmd->trans_id);
1232 disk_super->flags = cpu_to_le32(pmd->flags);
1234 r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root, len);
1235 if (r < 0)
1236 goto out;
1238 r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root, len);
1239 if (r < 0)
1240 goto out;
1242 r = dm_tm_commit(pmd->tm, pmd->sblock);
1243 if (r < 0)
1244 goto out;
1247 * Open the next transaction.
1249 pmd->sblock = NULL;
1251 r = begin_transaction(pmd);
1252 out:
1253 up_write(&pmd->root_lock);
1254 return r;
1257 int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
1259 int r;
1261 down_read(&pmd->root_lock);
1262 r = dm_sm_get_nr_free(pmd->data_sm, result);
1263 up_read(&pmd->root_lock);
1265 return r;
1268 int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
1269 dm_block_t *result)
1271 int r;
1273 down_read(&pmd->root_lock);
1274 r = dm_sm_get_nr_free(pmd->metadata_sm, result);
1275 up_read(&pmd->root_lock);
1277 return r;
1280 int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result)
1282 down_read(&pmd->root_lock);
1283 *result = pmd->data_block_size;
1284 up_read(&pmd->root_lock);
1286 return 0;
1289 int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
1291 int r;
1293 down_read(&pmd->root_lock);
1294 r = dm_sm_get_nr_blocks(pmd->data_sm, result);
1295 up_read(&pmd->root_lock);
1297 return r;
1300 int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
1302 struct dm_pool_metadata *pmd = td->pmd;
1304 down_read(&pmd->root_lock);
1305 *result = td->mapped_blocks;
1306 up_read(&pmd->root_lock);
1308 return 0;
1311 static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
1313 int r;
1314 __le64 value_le;
1315 dm_block_t thin_root;
1316 struct dm_pool_metadata *pmd = td->pmd;
1318 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
1319 if (r)
1320 return r;
1322 thin_root = le64_to_cpu(value_le);
1324 return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
1327 int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
1328 dm_block_t *result)
1330 int r;
1331 struct dm_pool_metadata *pmd = td->pmd;
1333 down_read(&pmd->root_lock);
1334 r = __highest_block(td, result);
1335 up_read(&pmd->root_lock);
1337 return r;
1340 static int __resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1342 int r;
1343 dm_block_t old_count;
1345 r = dm_sm_get_nr_blocks(pmd->data_sm, &old_count);
1346 if (r)
1347 return r;
1349 if (new_count == old_count)
1350 return 0;
1352 if (new_count < old_count) {
1353 DMERR("cannot reduce size of data device");
1354 return -EINVAL;
1357 r = dm_sm_extend(pmd->data_sm, new_count - old_count);
1358 if (!r)
1359 pmd->need_commit = 1;
1361 return r;
1364 int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1366 int r;
1368 down_write(&pmd->root_lock);
1369 r = __resize_data_dev(pmd, new_count);
1370 up_write(&pmd->root_lock);
1372 return r;