]> git.proxmox.com Git - mirror_ubuntu-artful-kernel.git/blame - drivers/md/dm-thin-metadata.c
dm thin metadata: split __open or format metadata
[mirror_ubuntu-artful-kernel.git] / drivers / md / dm-thin-metadata.c
CommitLineData
991d9fa0
JT
1/*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This file is released under the GPL.
5 */
6
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"
12
13#include <linux/list.h>
14#include <linux/device-mapper.h>
15#include <linux/workqueue.h>
16
17/*--------------------------------------------------------------------------
18 * As far as the metadata goes, there is:
19 *
20 * - A superblock in block zero, taking up fewer than 512 bytes for
21 * atomic writes.
22 *
23 * - A space map managing the metadata blocks.
24 *
25 * - A space map managing the data blocks.
26 *
27 * - A btree mapping our internal thin dev ids onto struct disk_device_details.
28 *
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.
33 *
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.
41 *
42 * Space maps have 2 btrees:
43 *
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.
47 *
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:
50 *
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
55 *
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.
59 *
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.
65 *
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.
70 *
71 * All metadata io is in THIN_METADATA_BLOCK_SIZE sized/aligned chunks
72 * from the block manager.
73 *--------------------------------------------------------------------------*/
74
75#define DM_MSG_PREFIX "thin metadata"
76
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
82
8c971178
JT
83/*
84 * 3 for btree insert +
85 * 2 for btree lookup used within space map
86 */
87#define THIN_MAX_CONCURRENT_LOCKS 5
88
991d9fa0
JT
89/* This should be plenty */
90#define SPACE_MAP_ROOT_SIZE 128
91
92/*
93 * Little endian on-disk superblock and device details.
94 */
95struct thin_disk_superblock {
96 __le32 csum; /* Checksum of superblock except for this field. */
97 __le32 flags;
98 __le64 blocknr; /* This block number, dm_block_t. */
99
100 __u8 uuid[16];
101 __le64 magic;
102 __le32 version;
103 __le32 time;
104
105 __le64 trans_id;
106
107 /*
108 * Root held by userspace transactions.
109 */
110 __le64 held_root;
111
112 __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
113 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
114
115 /*
116 * 2-level btree mapping (dev_id, (dev block, time)) -> data block
117 */
118 __le64 data_mapping_root;
119
120 /*
121 * Device detail root mapping dev_id -> device_details
122 */
123 __le64 device_details_root;
124
125 __le32 data_block_size; /* In 512-byte sectors. */
126
127 __le32 metadata_block_size; /* In 512-byte sectors. */
128 __le64 metadata_nr_blocks;
129
130 __le32 compat_flags;
131 __le32 compat_ro_flags;
132 __le32 incompat_flags;
133} __packed;
134
135struct disk_device_details {
136 __le64 mapped_blocks;
137 __le64 transaction_id; /* When created. */
138 __le32 creation_time;
139 __le32 snapshotted_time;
140} __packed;
141
142struct dm_pool_metadata {
143 struct hlist_node hash;
144
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;
151
152 /*
153 * Two-level btree.
154 * First level holds thin_dev_t.
155 * Second level holds mappings.
156 */
157 struct dm_btree_info info;
158
159 /*
160 * Non-blocking version of the above.
161 */
162 struct dm_btree_info nb_info;
163
164 /*
165 * Just the top level for deleting whole devices.
166 */
167 struct dm_btree_info tl_info;
168
169 /*
170 * Just the bottom level for creating new devices.
171 */
172 struct dm_btree_info bl_info;
173
174 /*
175 * Describes the device details btree.
176 */
177 struct dm_btree_info details_info;
178
179 struct rw_semaphore root_lock;
180 uint32_t time;
991d9fa0
JT
181 dm_block_t root;
182 dm_block_t details_root;
183 struct list_head thin_devices;
184 uint64_t trans_id;
185 unsigned long flags;
186 sector_t data_block_size;
187};
188
189struct dm_thin_device {
190 struct list_head list;
191 struct dm_pool_metadata *pmd;
192 dm_thin_id id;
193
194 int open_count;
195 int changed;
196 uint64_t mapped_blocks;
197 uint64_t transaction_id;
198 uint32_t creation_time;
199 uint32_t snapshotted_time;
200};
201
202/*----------------------------------------------------------------
203 * superblock validator
204 *--------------------------------------------------------------*/
205
206#define SUPERBLOCK_CSUM_XOR 160774
207
208static void sb_prepare_for_write(struct dm_block_validator *v,
209 struct dm_block *b,
210 size_t block_size)
211{
212 struct thin_disk_superblock *disk_super = dm_block_data(b);
213
214 disk_super->blocknr = cpu_to_le64(dm_block_location(b));
215 disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
216 block_size - sizeof(__le32),
217 SUPERBLOCK_CSUM_XOR));
218}
219
220static int sb_check(struct dm_block_validator *v,
221 struct dm_block *b,
222 size_t block_size)
223{
224 struct thin_disk_superblock *disk_super = dm_block_data(b);
225 __le32 csum_le;
226
227 if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
228 DMERR("sb_check failed: blocknr %llu: "
229 "wanted %llu", le64_to_cpu(disk_super->blocknr),
230 (unsigned long long)dm_block_location(b));
231 return -ENOTBLK;
232 }
233
234 if (le64_to_cpu(disk_super->magic) != THIN_SUPERBLOCK_MAGIC) {
235 DMERR("sb_check failed: magic %llu: "
236 "wanted %llu", le64_to_cpu(disk_super->magic),
237 (unsigned long long)THIN_SUPERBLOCK_MAGIC);
238 return -EILSEQ;
239 }
240
241 csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
242 block_size - sizeof(__le32),
243 SUPERBLOCK_CSUM_XOR));
244 if (csum_le != disk_super->csum) {
245 DMERR("sb_check failed: csum %u: wanted %u",
246 le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
247 return -EILSEQ;
248 }
249
250 return 0;
251}
252
253static struct dm_block_validator sb_validator = {
254 .name = "superblock",
255 .prepare_for_write = sb_prepare_for_write,
256 .check = sb_check
257};
258
259/*----------------------------------------------------------------
260 * Methods for the btree value types
261 *--------------------------------------------------------------*/
262
263static uint64_t pack_block_time(dm_block_t b, uint32_t t)
264{
265 return (b << 24) | t;
266}
267
268static void unpack_block_time(uint64_t v, dm_block_t *b, uint32_t *t)
269{
270 *b = v >> 24;
271 *t = v & ((1 << 24) - 1);
272}
273
274static void data_block_inc(void *context, void *value_le)
275{
276 struct dm_space_map *sm = context;
277 __le64 v_le;
278 uint64_t b;
279 uint32_t t;
280
281 memcpy(&v_le, value_le, sizeof(v_le));
282 unpack_block_time(le64_to_cpu(v_le), &b, &t);
283 dm_sm_inc_block(sm, b);
284}
285
286static void data_block_dec(void *context, void *value_le)
287{
288 struct dm_space_map *sm = context;
289 __le64 v_le;
290 uint64_t b;
291 uint32_t t;
292
293 memcpy(&v_le, value_le, sizeof(v_le));
294 unpack_block_time(le64_to_cpu(v_le), &b, &t);
295 dm_sm_dec_block(sm, b);
296}
297
298static int data_block_equal(void *context, void *value1_le, void *value2_le)
299{
300 __le64 v1_le, v2_le;
301 uint64_t b1, b2;
302 uint32_t t;
303
304 memcpy(&v1_le, value1_le, sizeof(v1_le));
305 memcpy(&v2_le, value2_le, sizeof(v2_le));
306 unpack_block_time(le64_to_cpu(v1_le), &b1, &t);
307 unpack_block_time(le64_to_cpu(v2_le), &b2, &t);
308
309 return b1 == b2;
310}
311
312static void subtree_inc(void *context, void *value)
313{
314 struct dm_btree_info *info = context;
315 __le64 root_le;
316 uint64_t root;
317
318 memcpy(&root_le, value, sizeof(root_le));
319 root = le64_to_cpu(root_le);
320 dm_tm_inc(info->tm, root);
321}
322
323static void subtree_dec(void *context, void *value)
324{
325 struct dm_btree_info *info = context;
326 __le64 root_le;
327 uint64_t root;
328
329 memcpy(&root_le, value, sizeof(root_le));
330 root = le64_to_cpu(root_le);
331 if (dm_btree_del(info, root))
332 DMERR("btree delete failed\n");
333}
334
335static int subtree_equal(void *context, void *value1_le, void *value2_le)
336{
337 __le64 v1_le, v2_le;
338 memcpy(&v1_le, value1_le, sizeof(v1_le));
339 memcpy(&v2_le, value2_le, sizeof(v2_le));
340
341 return v1_le == v2_le;
342}
343
344/*----------------------------------------------------------------*/
345
25971192
JT
346static int superblock_lock_zero(struct dm_pool_metadata *pmd,
347 struct dm_block **sblock)
348{
349 return dm_bm_write_lock_zero(pmd->bm, THIN_SUPERBLOCK_LOCATION,
350 &sb_validator, sblock);
351}
352
353static int superblock_lock(struct dm_pool_metadata *pmd,
354 struct dm_block **sblock)
355{
356 return dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
357 &sb_validator, sblock);
358}
359
332627db 360static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result)
991d9fa0
JT
361{
362 int r;
363 unsigned i;
364 struct dm_block *b;
365 __le64 *data_le, zero = cpu_to_le64(0);
366 unsigned block_size = dm_bm_block_size(bm) / sizeof(__le64);
367
368 /*
369 * We can't use a validator here - it may be all zeroes.
370 */
371 r = dm_bm_read_lock(bm, THIN_SUPERBLOCK_LOCATION, NULL, &b);
372 if (r)
373 return r;
374
375 data_le = dm_block_data(b);
376 *result = 1;
377 for (i = 0; i < block_size; i++) {
378 if (data_le[i] != zero) {
379 *result = 0;
380 break;
381 }
382 }
383
384 return dm_bm_unlock(b);
385}
386
41675aea
JT
387static void __setup_btree_details(struct dm_pool_metadata *pmd)
388{
389 pmd->info.tm = pmd->tm;
390 pmd->info.levels = 2;
391 pmd->info.value_type.context = pmd->data_sm;
392 pmd->info.value_type.size = sizeof(__le64);
393 pmd->info.value_type.inc = data_block_inc;
394 pmd->info.value_type.dec = data_block_dec;
395 pmd->info.value_type.equal = data_block_equal;
396
397 memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info));
398 pmd->nb_info.tm = pmd->nb_tm;
399
400 pmd->tl_info.tm = pmd->tm;
401 pmd->tl_info.levels = 1;
402 pmd->tl_info.value_type.context = &pmd->info;
403 pmd->tl_info.value_type.size = sizeof(__le64);
404 pmd->tl_info.value_type.inc = subtree_inc;
405 pmd->tl_info.value_type.dec = subtree_dec;
406 pmd->tl_info.value_type.equal = subtree_equal;
407
408 pmd->bl_info.tm = pmd->tm;
409 pmd->bl_info.levels = 1;
410 pmd->bl_info.value_type.context = pmd->data_sm;
411 pmd->bl_info.value_type.size = sizeof(__le64);
412 pmd->bl_info.value_type.inc = data_block_inc;
413 pmd->bl_info.value_type.dec = data_block_dec;
414 pmd->bl_info.value_type.equal = data_block_equal;
415
416 pmd->details_info.tm = pmd->tm;
417 pmd->details_info.levels = 1;
418 pmd->details_info.value_type.context = NULL;
419 pmd->details_info.value_type.size = sizeof(struct disk_device_details);
420 pmd->details_info.value_type.inc = NULL;
421 pmd->details_info.value_type.dec = NULL;
422 pmd->details_info.value_type.equal = NULL;
423}
424
9cb6653f
JT
425static int __write_initial_superblock(struct dm_pool_metadata *pmd)
426{
427 int r;
428 struct dm_block *sblock;
10d2a9ff 429 size_t metadata_len, data_len;
9cb6653f
JT
430 struct thin_disk_superblock *disk_super;
431 sector_t bdev_size = i_size_read(pmd->bdev->bd_inode) >> SECTOR_SHIFT;
432
433 if (bdev_size > THIN_METADATA_MAX_SECTORS)
434 bdev_size = THIN_METADATA_MAX_SECTORS;
435
10d2a9ff
JT
436 r = dm_sm_root_size(pmd->metadata_sm, &metadata_len);
437 if (r < 0)
438 return r;
439
440 r = dm_sm_root_size(pmd->data_sm, &data_len);
441 if (r < 0)
442 return r;
443
444 r = dm_sm_commit(pmd->data_sm);
445 if (r < 0)
446 return r;
447
448 r = dm_tm_pre_commit(pmd->tm);
449 if (r < 0)
450 return r;
451
9cb6653f
JT
452 r = superblock_lock_zero(pmd, &sblock);
453 if (r)
454 return r;
455
456 disk_super = dm_block_data(sblock);
10d2a9ff 457 disk_super->flags = 0;
583ceee2 458 memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
9cb6653f
JT
459 disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
460 disk_super->version = cpu_to_le32(THIN_VERSION);
461 disk_super->time = 0;
10d2a9ff
JT
462 disk_super->trans_id = 0;
463 disk_super->held_root = 0;
464
465 r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root,
466 metadata_len);
467 if (r < 0)
468 goto bad_locked;
469
470 r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root,
471 data_len);
472 if (r < 0)
473 goto bad_locked;
474
475 disk_super->data_mapping_root = cpu_to_le64(pmd->root);
476 disk_super->device_details_root = cpu_to_le64(pmd->details_root);
9cb6653f
JT
477 disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
478 disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
479 disk_super->data_block_size = cpu_to_le32(pmd->data_block_size);
480
270938ba 481 return dm_tm_commit(pmd->tm, sblock);
9cb6653f 482
10d2a9ff
JT
483bad_locked:
484 dm_bm_unlock(sblock);
9cb6653f
JT
485 return r;
486}
487
e4d2205c 488static int __format_metadata(struct dm_pool_metadata *pmd, dm_block_t nr_blocks)
991d9fa0
JT
489{
490 int r;
384ef0e6 491
e4d2205c
JT
492 r = dm_tm_create_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
493 &pmd->tm, &pmd->metadata_sm);
494 if (r < 0) {
495 DMERR("tm_create_with_sm failed");
496 return r;
497 }
991d9fa0 498
e4d2205c
JT
499 pmd->data_sm = dm_sm_disk_create(pmd->tm, nr_blocks);
500 if (IS_ERR(pmd->data_sm)) {
501 DMERR("sm_disk_create failed");
502 r = PTR_ERR(pmd->data_sm);
503 goto bad;
991d9fa0
JT
504 }
505
d6332814 506 pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
991d9fa0
JT
507 if (!pmd->nb_tm) {
508 DMERR("could not create clone tm");
509 r = -ENOMEM;
510 goto bad_data_sm;
511 }
512
41675aea 513 __setup_btree_details(pmd);
991d9fa0 514
6a0ebd31 515 pmd->root = 0;
991d9fa0
JT
516 pmd->details_root = 0;
517 pmd->trans_id = 0;
518 pmd->flags = 0;
991d9fa0 519
9cb6653f
JT
520 r = dm_btree_empty(&pmd->info, &pmd->root);
521 if (r < 0)
522 goto bad_data_sm;
523
524 r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
525 if (r < 0) {
526 DMERR("couldn't create devices root");
527 goto bad_data_sm;
528 }
529
530 r = __write_initial_superblock(pmd);
531 if (r)
532 goto bad_data_sm;
533
991d9fa0
JT
534 return 0;
535
536bad_data_sm:
d6332814 537 dm_sm_destroy(pmd->data_sm);
991d9fa0 538bad:
d6332814
JT
539 dm_tm_destroy(pmd->tm);
540 dm_sm_destroy(pmd->metadata_sm);
991d9fa0
JT
541
542 return r;
543}
544
e4d2205c
JT
545static int __open_metadata(struct dm_pool_metadata *pmd)
546{
547 int r;
548 struct dm_block *sblock;
549 struct thin_disk_superblock *disk_super;
550
551 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
552 &sb_validator, &sblock);
553 if (r < 0) {
554 DMERR("couldn't read superblock");
555 return r;
556 }
557
558 disk_super = dm_block_data(sblock);
559 r = dm_tm_open_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
560 disk_super->metadata_space_map_root,
561 sizeof(disk_super->metadata_space_map_root),
562 &pmd->tm, &pmd->metadata_sm);
563 if (r < 0) {
564 DMERR("tm_open_with_sm failed");
565 dm_bm_unlock(sblock);
566 return r;
567 }
568
569 pmd->data_sm = dm_sm_disk_open(pmd->tm, disk_super->data_space_map_root,
570 sizeof(disk_super->data_space_map_root));
571 if (IS_ERR(pmd->data_sm)) {
572 DMERR("sm_disk_open failed");
573 dm_bm_unlock(sblock);
574 r = PTR_ERR(pmd->data_sm);
575 goto bad;
576 }
577
578 dm_bm_unlock(sblock);
579
580 pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
581 if (!pmd->nb_tm) {
582 DMERR("could not create clone tm");
583 r = -ENOMEM;
584 goto bad_data_sm;
585 }
586
587 __setup_btree_details(pmd);
588
589bad_data_sm:
590 dm_sm_destroy(pmd->data_sm);
591bad:
592 dm_tm_destroy(pmd->tm);
593 dm_sm_destroy(pmd->metadata_sm);
594
595 return r;
596}
597
598static int __open_or_format_metadata(struct dm_pool_metadata *pmd,
599 dm_block_t nr_blocks, int create)
600{
601 if (create)
602 return __format_metadata(pmd, nr_blocks);
603 else
604 return __open_metadata(pmd);
605}
606
332627db
JT
607static int __create_persistent_data_objects(struct dm_pool_metadata *pmd,
608 dm_block_t nr_blocks, int *create)
609{
610 int r;
611
612 pmd->bm = dm_block_manager_create(pmd->bdev, THIN_METADATA_BLOCK_SIZE,
613 THIN_METADATA_CACHE_SIZE,
614 THIN_MAX_CONCURRENT_LOCKS);
615 if (IS_ERR(pmd->bm)) {
616 DMERR("could not create block manager");
617 return PTR_ERR(pmd->bm);
618 }
619
620 r = __superblock_all_zeroes(pmd->bm, create);
621 if (r) {
622 dm_block_manager_destroy(pmd->bm);
623 return r;
624 }
625
d6332814 626 r = __open_or_format_metadata(pmd, nr_blocks, *create);
332627db
JT
627 if (r)
628 dm_block_manager_destroy(pmd->bm);
629
630 return r;
631}
632
f9dd9352
JT
633static void __destroy_persistent_data_objects(struct dm_pool_metadata *pmd)
634{
635 dm_sm_destroy(pmd->data_sm);
636 dm_sm_destroy(pmd->metadata_sm);
637 dm_tm_destroy(pmd->nb_tm);
638 dm_tm_destroy(pmd->tm);
639 dm_block_manager_destroy(pmd->bm);
640}
641
991d9fa0
JT
642static int __begin_transaction(struct dm_pool_metadata *pmd)
643{
644 int r;
645 u32 features;
646 struct thin_disk_superblock *disk_super;
647 struct dm_block *sblock;
648
991d9fa0
JT
649 /*
650 * We re-read the superblock every time. Shouldn't need to do this
651 * really.
652 */
653 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
654 &sb_validator, &sblock);
655 if (r)
656 return r;
657
658 disk_super = dm_block_data(sblock);
659 pmd->time = le32_to_cpu(disk_super->time);
660 pmd->root = le64_to_cpu(disk_super->data_mapping_root);
661 pmd->details_root = le64_to_cpu(disk_super->device_details_root);
662 pmd->trans_id = le64_to_cpu(disk_super->trans_id);
663 pmd->flags = le32_to_cpu(disk_super->flags);
664 pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
665
666 features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP;
667 if (features) {
668 DMERR("could not access metadata due to "
669 "unsupported optional features (%lx).",
670 (unsigned long)features);
671 r = -EINVAL;
672 goto out;
673 }
674
675 /*
676 * Check for read-only metadata to skip the following RDWR checks.
677 */
678 if (get_disk_ro(pmd->bdev->bd_disk))
679 goto out;
680
681 features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
682 if (features) {
683 DMERR("could not access metadata RDWR due to "
684 "unsupported optional features (%lx).",
685 (unsigned long)features);
686 r = -EINVAL;
687 }
688
689out:
690 dm_bm_unlock(sblock);
691 return r;
692}
693
694static int __write_changed_details(struct dm_pool_metadata *pmd)
695{
696 int r;
697 struct dm_thin_device *td, *tmp;
698 struct disk_device_details details;
699 uint64_t key;
700
701 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
702 if (!td->changed)
703 continue;
704
705 key = td->id;
706
707 details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
708 details.transaction_id = cpu_to_le64(td->transaction_id);
709 details.creation_time = cpu_to_le32(td->creation_time);
710 details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
711 __dm_bless_for_disk(&details);
712
713 r = dm_btree_insert(&pmd->details_info, pmd->details_root,
714 &key, &details, &pmd->details_root);
715 if (r)
716 return r;
717
718 if (td->open_count)
719 td->changed = 0;
720 else {
721 list_del(&td->list);
722 kfree(td);
723 }
991d9fa0
JT
724 }
725
726 return 0;
727}
728
729static int __commit_transaction(struct dm_pool_metadata *pmd)
730{
731 /*
732 * FIXME: Associated pool should be made read-only on failure.
733 */
734 int r;
735 size_t metadata_len, data_len;
736 struct thin_disk_superblock *disk_super;
737 struct dm_block *sblock;
738
739 /*
740 * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
741 */
742 BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
743
744 r = __write_changed_details(pmd);
745 if (r < 0)
d973ac19 746 return r;
991d9fa0 747
991d9fa0
JT
748 r = dm_sm_commit(pmd->data_sm);
749 if (r < 0)
d973ac19 750 return r;
991d9fa0
JT
751
752 r = dm_tm_pre_commit(pmd->tm);
753 if (r < 0)
d973ac19 754 return r;
991d9fa0
JT
755
756 r = dm_sm_root_size(pmd->metadata_sm, &metadata_len);
757 if (r < 0)
d973ac19 758 return r;
991d9fa0 759
fef838cc 760 r = dm_sm_root_size(pmd->data_sm, &data_len);
991d9fa0 761 if (r < 0)
d973ac19 762 return r;
991d9fa0 763
25971192 764 r = superblock_lock(pmd, &sblock);
991d9fa0 765 if (r)
d973ac19 766 return r;
991d9fa0
JT
767
768 disk_super = dm_block_data(sblock);
769 disk_super->time = cpu_to_le32(pmd->time);
770 disk_super->data_mapping_root = cpu_to_le64(pmd->root);
771 disk_super->device_details_root = cpu_to_le64(pmd->details_root);
772 disk_super->trans_id = cpu_to_le64(pmd->trans_id);
773 disk_super->flags = cpu_to_le32(pmd->flags);
774
775 r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root,
776 metadata_len);
777 if (r < 0)
778 goto out_locked;
779
780 r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root,
781 data_len);
782 if (r < 0)
783 goto out_locked;
784
eb04cf63 785 return dm_tm_commit(pmd->tm, sblock);
991d9fa0
JT
786
787out_locked:
788 dm_bm_unlock(sblock);
789 return r;
790}
791
792struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
793 sector_t data_block_size)
794{
795 int r;
991d9fa0 796 struct dm_pool_metadata *pmd;
991d9fa0 797 int create;
991d9fa0
JT
798
799 pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
800 if (!pmd) {
801 DMERR("could not allocate metadata struct");
802 return ERR_PTR(-ENOMEM);
803 }
804
6a0ebd31
JT
805 init_rwsem(&pmd->root_lock);
806 pmd->time = 0;
807 INIT_LIST_HEAD(&pmd->thin_devices);
332627db 808 pmd->bdev = bdev;
9cb6653f 809 pmd->data_block_size = data_block_size;
991d9fa0 810
332627db 811 r = __create_persistent_data_objects(pmd, 0, &create);
991d9fa0 812 if (r) {
991d9fa0
JT
813 kfree(pmd);
814 return ERR_PTR(r);
815 }
991d9fa0 816
270938ba
JT
817 r = __begin_transaction(pmd);
818 if (r < 0) {
819 if (dm_pool_metadata_close(pmd) < 0)
820 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
821 return ERR_PTR(r);
991d9fa0
JT
822 }
823
824 return pmd;
991d9fa0
JT
825}
826
827int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
828{
829 int r;
830 unsigned open_devices = 0;
831 struct dm_thin_device *td, *tmp;
832
833 down_read(&pmd->root_lock);
834 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
835 if (td->open_count)
836 open_devices++;
837 else {
838 list_del(&td->list);
839 kfree(td);
840 }
841 }
842 up_read(&pmd->root_lock);
843
844 if (open_devices) {
845 DMERR("attempt to close pmd when %u device(s) are still open",
846 open_devices);
847 return -EBUSY;
848 }
849
850 r = __commit_transaction(pmd);
851 if (r < 0)
852 DMWARN("%s: __commit_transaction() failed, error = %d",
853 __func__, r);
854
f9dd9352 855 __destroy_persistent_data_objects(pmd);
991d9fa0
JT
856 kfree(pmd);
857
858 return 0;
859}
860
1f3db25d
MS
861/*
862 * __open_device: Returns @td corresponding to device with id @dev,
863 * creating it if @create is set and incrementing @td->open_count.
864 * On failure, @td is undefined.
865 */
991d9fa0
JT
866static int __open_device(struct dm_pool_metadata *pmd,
867 dm_thin_id dev, int create,
868 struct dm_thin_device **td)
869{
870 int r, changed = 0;
871 struct dm_thin_device *td2;
872 uint64_t key = dev;
873 struct disk_device_details details_le;
874
875 /*
1f3db25d 876 * If the device is already open, return it.
991d9fa0
JT
877 */
878 list_for_each_entry(td2, &pmd->thin_devices, list)
879 if (td2->id == dev) {
1f3db25d
MS
880 /*
881 * May not create an already-open device.
882 */
883 if (create)
884 return -EEXIST;
885
991d9fa0
JT
886 td2->open_count++;
887 *td = td2;
888 return 0;
889 }
890
891 /*
892 * Check the device exists.
893 */
894 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
895 &key, &details_le);
896 if (r) {
897 if (r != -ENODATA || !create)
898 return r;
899
1f3db25d
MS
900 /*
901 * Create new device.
902 */
991d9fa0
JT
903 changed = 1;
904 details_le.mapped_blocks = 0;
905 details_le.transaction_id = cpu_to_le64(pmd->trans_id);
906 details_le.creation_time = cpu_to_le32(pmd->time);
907 details_le.snapshotted_time = cpu_to_le32(pmd->time);
908 }
909
910 *td = kmalloc(sizeof(**td), GFP_NOIO);
911 if (!*td)
912 return -ENOMEM;
913
914 (*td)->pmd = pmd;
915 (*td)->id = dev;
916 (*td)->open_count = 1;
917 (*td)->changed = changed;
918 (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
919 (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
920 (*td)->creation_time = le32_to_cpu(details_le.creation_time);
921 (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
922
923 list_add(&(*td)->list, &pmd->thin_devices);
924
925 return 0;
926}
927
928static void __close_device(struct dm_thin_device *td)
929{
930 --td->open_count;
931}
932
933static int __create_thin(struct dm_pool_metadata *pmd,
934 dm_thin_id dev)
935{
936 int r;
937 dm_block_t dev_root;
938 uint64_t key = dev;
939 struct disk_device_details details_le;
940 struct dm_thin_device *td;
941 __le64 value;
942
943 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
944 &key, &details_le);
945 if (!r)
946 return -EEXIST;
947
948 /*
949 * Create an empty btree for the mappings.
950 */
951 r = dm_btree_empty(&pmd->bl_info, &dev_root);
952 if (r)
953 return r;
954
955 /*
956 * Insert it into the main mapping tree.
957 */
958 value = cpu_to_le64(dev_root);
959 __dm_bless_for_disk(&value);
960 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
961 if (r) {
962 dm_btree_del(&pmd->bl_info, dev_root);
963 return r;
964 }
965
966 r = __open_device(pmd, dev, 1, &td);
967 if (r) {
991d9fa0
JT
968 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
969 dm_btree_del(&pmd->bl_info, dev_root);
970 return r;
971 }
991d9fa0
JT
972 __close_device(td);
973
974 return r;
975}
976
977int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
978{
979 int r;
980
981 down_write(&pmd->root_lock);
982 r = __create_thin(pmd, dev);
983 up_write(&pmd->root_lock);
984
985 return r;
986}
987
988static int __set_snapshot_details(struct dm_pool_metadata *pmd,
989 struct dm_thin_device *snap,
990 dm_thin_id origin, uint32_t time)
991{
992 int r;
993 struct dm_thin_device *td;
994
995 r = __open_device(pmd, origin, 0, &td);
996 if (r)
997 return r;
998
999 td->changed = 1;
1000 td->snapshotted_time = time;
1001
1002 snap->mapped_blocks = td->mapped_blocks;
1003 snap->snapshotted_time = time;
1004 __close_device(td);
1005
1006 return 0;
1007}
1008
1009static int __create_snap(struct dm_pool_metadata *pmd,
1010 dm_thin_id dev, dm_thin_id origin)
1011{
1012 int r;
1013 dm_block_t origin_root;
1014 uint64_t key = origin, dev_key = dev;
1015 struct dm_thin_device *td;
1016 struct disk_device_details details_le;
1017 __le64 value;
1018
1019 /* check this device is unused */
1020 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
1021 &dev_key, &details_le);
1022 if (!r)
1023 return -EEXIST;
1024
1025 /* find the mapping tree for the origin */
1026 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
1027 if (r)
1028 return r;
1029 origin_root = le64_to_cpu(value);
1030
1031 /* clone the origin, an inc will do */
1032 dm_tm_inc(pmd->tm, origin_root);
1033
1034 /* insert into the main mapping tree */
1035 value = cpu_to_le64(origin_root);
1036 __dm_bless_for_disk(&value);
1037 key = dev;
1038 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
1039 if (r) {
1040 dm_tm_dec(pmd->tm, origin_root);
1041 return r;
1042 }
1043
1044 pmd->time++;
1045
1046 r = __open_device(pmd, dev, 1, &td);
1047 if (r)
1048 goto bad;
1049
1050 r = __set_snapshot_details(pmd, td, origin, pmd->time);
1f3db25d
MS
1051 __close_device(td);
1052
991d9fa0
JT
1053 if (r)
1054 goto bad;
1055
991d9fa0
JT
1056 return 0;
1057
1058bad:
991d9fa0
JT
1059 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1060 dm_btree_remove(&pmd->details_info, pmd->details_root,
1061 &key, &pmd->details_root);
1062 return r;
1063}
1064
1065int dm_pool_create_snap(struct dm_pool_metadata *pmd,
1066 dm_thin_id dev,
1067 dm_thin_id origin)
1068{
1069 int r;
1070
1071 down_write(&pmd->root_lock);
1072 r = __create_snap(pmd, dev, origin);
1073 up_write(&pmd->root_lock);
1074
1075 return r;
1076}
1077
1078static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
1079{
1080 int r;
1081 uint64_t key = dev;
1082 struct dm_thin_device *td;
1083
1084 /* TODO: failure should mark the transaction invalid */
1085 r = __open_device(pmd, dev, 0, &td);
1086 if (r)
1087 return r;
1088
1089 if (td->open_count > 1) {
1090 __close_device(td);
1091 return -EBUSY;
1092 }
1093
1094 list_del(&td->list);
1095 kfree(td);
1096 r = dm_btree_remove(&pmd->details_info, pmd->details_root,
1097 &key, &pmd->details_root);
1098 if (r)
1099 return r;
1100
1101 r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1102 if (r)
1103 return r;
1104
991d9fa0
JT
1105 return 0;
1106}
1107
1108int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
1109 dm_thin_id dev)
1110{
1111 int r;
1112
1113 down_write(&pmd->root_lock);
1114 r = __delete_device(pmd, dev);
1115 up_write(&pmd->root_lock);
1116
1117 return r;
1118}
1119
1120int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
1121 uint64_t current_id,
1122 uint64_t new_id)
1123{
1124 down_write(&pmd->root_lock);
1125 if (pmd->trans_id != current_id) {
1126 up_write(&pmd->root_lock);
1127 DMERR("mismatched transaction id");
1128 return -EINVAL;
1129 }
1130
1131 pmd->trans_id = new_id;
991d9fa0
JT
1132 up_write(&pmd->root_lock);
1133
1134 return 0;
1135}
1136
1137int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
1138 uint64_t *result)
1139{
1140 down_read(&pmd->root_lock);
1141 *result = pmd->trans_id;
1142 up_read(&pmd->root_lock);
1143
1144 return 0;
1145}
1146
cc8394d8
JT
1147static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
1148{
1149 int r, inc;
1150 struct thin_disk_superblock *disk_super;
1151 struct dm_block *copy, *sblock;
1152 dm_block_t held_root;
1153
1154 /*
1155 * Copy the superblock.
1156 */
1157 dm_sm_inc_block(pmd->metadata_sm, THIN_SUPERBLOCK_LOCATION);
1158 r = dm_tm_shadow_block(pmd->tm, THIN_SUPERBLOCK_LOCATION,
1159 &sb_validator, &copy, &inc);
1160 if (r)
1161 return r;
1162
1163 BUG_ON(!inc);
1164
1165 held_root = dm_block_location(copy);
1166 disk_super = dm_block_data(copy);
1167
1168 if (le64_to_cpu(disk_super->held_root)) {
1169 DMWARN("Pool metadata snapshot already exists: release this before taking another.");
1170
1171 dm_tm_dec(pmd->tm, held_root);
1172 dm_tm_unlock(pmd->tm, copy);
cc8394d8
JT
1173 return -EBUSY;
1174 }
1175
1176 /*
1177 * Wipe the spacemap since we're not publishing this.
1178 */
1179 memset(&disk_super->data_space_map_root, 0,
1180 sizeof(disk_super->data_space_map_root));
1181 memset(&disk_super->metadata_space_map_root, 0,
1182 sizeof(disk_super->metadata_space_map_root));
1183
1184 /*
1185 * Increment the data structures that need to be preserved.
1186 */
1187 dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->data_mapping_root));
1188 dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->device_details_root));
1189 dm_tm_unlock(pmd->tm, copy);
1190
1191 /*
1192 * Write the held root into the superblock.
1193 */
25971192 1194 r = superblock_lock(pmd, &sblock);
cc8394d8
JT
1195 if (r) {
1196 dm_tm_dec(pmd->tm, held_root);
cc8394d8
JT
1197 return r;
1198 }
1199
1200 disk_super = dm_block_data(sblock);
1201 disk_super->held_root = cpu_to_le64(held_root);
1202 dm_bm_unlock(sblock);
cc8394d8
JT
1203 return 0;
1204}
1205
1206int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd)
1207{
1208 int r;
1209
1210 down_write(&pmd->root_lock);
1211 r = __reserve_metadata_snap(pmd);
1212 up_write(&pmd->root_lock);
1213
1214 return r;
1215}
1216
1217static int __release_metadata_snap(struct dm_pool_metadata *pmd)
991d9fa0
JT
1218{
1219 int r;
1220 struct thin_disk_superblock *disk_super;
cc8394d8
JT
1221 struct dm_block *sblock, *copy;
1222 dm_block_t held_root;
991d9fa0 1223
25971192 1224 r = superblock_lock(pmd, &sblock);
991d9fa0
JT
1225 if (r)
1226 return r;
1227
cc8394d8
JT
1228 disk_super = dm_block_data(sblock);
1229 held_root = le64_to_cpu(disk_super->held_root);
1230 disk_super->held_root = cpu_to_le64(0);
cc8394d8
JT
1231
1232 dm_bm_unlock(sblock);
1233
1234 if (!held_root) {
1235 DMWARN("No pool metadata snapshot found: nothing to release.");
1236 return -EINVAL;
1237 }
1238
1239 r = dm_tm_read_lock(pmd->tm, held_root, &sb_validator, &copy);
1240 if (r)
1241 return r;
1242
1243 disk_super = dm_block_data(copy);
1244 dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->data_mapping_root));
1245 dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->device_details_root));
1246 dm_sm_dec_block(pmd->metadata_sm, held_root);
1247
1248 return dm_tm_unlock(pmd->tm, copy);
1249}
1250
1251int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd)
1252{
1253 int r;
1254
1255 down_write(&pmd->root_lock);
1256 r = __release_metadata_snap(pmd);
1257 up_write(&pmd->root_lock);
1258
1259 return r;
1260}
1261
1262static int __get_metadata_snap(struct dm_pool_metadata *pmd,
1263 dm_block_t *result)
1264{
1265 int r;
1266 struct thin_disk_superblock *disk_super;
1267 struct dm_block *sblock;
1268
1269 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
1270 &sb_validator, &sblock);
1271 if (r)
1272 return r;
1273
991d9fa0
JT
1274 disk_super = dm_block_data(sblock);
1275 *result = le64_to_cpu(disk_super->held_root);
1276
1277 return dm_bm_unlock(sblock);
1278}
1279
cc8394d8
JT
1280int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
1281 dm_block_t *result)
991d9fa0
JT
1282{
1283 int r;
1284
1285 down_read(&pmd->root_lock);
cc8394d8 1286 r = __get_metadata_snap(pmd, result);
991d9fa0
JT
1287 up_read(&pmd->root_lock);
1288
1289 return r;
1290}
1291
1292int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
1293 struct dm_thin_device **td)
1294{
1295 int r;
1296
1297 down_write(&pmd->root_lock);
1298 r = __open_device(pmd, dev, 0, td);
1299 up_write(&pmd->root_lock);
1300
1301 return r;
1302}
1303
1304int dm_pool_close_thin_device(struct dm_thin_device *td)
1305{
1306 down_write(&td->pmd->root_lock);
1307 __close_device(td);
1308 up_write(&td->pmd->root_lock);
1309
1310 return 0;
1311}
1312
1313dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
1314{
1315 return td->id;
1316}
1317
17b7d63f 1318static bool __snapshotted_since(struct dm_thin_device *td, uint32_t time)
991d9fa0
JT
1319{
1320 return td->snapshotted_time > time;
1321}
1322
1323int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
1324 int can_block, struct dm_thin_lookup_result *result)
1325{
1326 int r;
1327 uint64_t block_time = 0;
1328 __le64 value;
1329 struct dm_pool_metadata *pmd = td->pmd;
1330 dm_block_t keys[2] = { td->id, block };
1331
1332 if (can_block) {
1333 down_read(&pmd->root_lock);
1334 r = dm_btree_lookup(&pmd->info, pmd->root, keys, &value);
1335 if (!r)
1336 block_time = le64_to_cpu(value);
1337 up_read(&pmd->root_lock);
1338
1339 } else if (down_read_trylock(&pmd->root_lock)) {
1340 r = dm_btree_lookup(&pmd->nb_info, pmd->root, keys, &value);
1341 if (!r)
1342 block_time = le64_to_cpu(value);
1343 up_read(&pmd->root_lock);
1344
1345 } else
1346 return -EWOULDBLOCK;
1347
1348 if (!r) {
1349 dm_block_t exception_block;
1350 uint32_t exception_time;
1351 unpack_block_time(block_time, &exception_block,
1352 &exception_time);
1353 result->block = exception_block;
1354 result->shared = __snapshotted_since(td, exception_time);
1355 }
1356
1357 return r;
1358}
1359
1360static int __insert(struct dm_thin_device *td, dm_block_t block,
1361 dm_block_t data_block)
1362{
1363 int r, inserted;
1364 __le64 value;
1365 struct dm_pool_metadata *pmd = td->pmd;
1366 dm_block_t keys[2] = { td->id, block };
1367
991d9fa0
JT
1368 value = cpu_to_le64(pack_block_time(data_block, pmd->time));
1369 __dm_bless_for_disk(&value);
1370
1371 r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
1372 &pmd->root, &inserted);
1373 if (r)
1374 return r;
1375
1376 if (inserted) {
1377 td->mapped_blocks++;
1378 td->changed = 1;
1379 }
1380
1381 return 0;
1382}
1383
1384int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
1385 dm_block_t data_block)
1386{
1387 int r;
1388
1389 down_write(&td->pmd->root_lock);
1390 r = __insert(td, block, data_block);
1391 up_write(&td->pmd->root_lock);
1392
1393 return r;
1394}
1395
1396static int __remove(struct dm_thin_device *td, dm_block_t block)
1397{
1398 int r;
1399 struct dm_pool_metadata *pmd = td->pmd;
1400 dm_block_t keys[2] = { td->id, block };
1401
1402 r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
1403 if (r)
1404 return r;
1405
af63bcb8
JT
1406 td->mapped_blocks--;
1407 td->changed = 1;
991d9fa0
JT
1408
1409 return 0;
1410}
1411
1412int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
1413{
1414 int r;
1415
1416 down_write(&td->pmd->root_lock);
1417 r = __remove(td, block);
1418 up_write(&td->pmd->root_lock);
1419
1420 return r;
1421}
1422
1423int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
1424{
1425 int r;
1426
1427 down_write(&pmd->root_lock);
991d9fa0 1428 r = dm_sm_new_block(pmd->data_sm, result);
991d9fa0
JT
1429 up_write(&pmd->root_lock);
1430
1431 return r;
1432}
1433
1434int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
1435{
1436 int r;
1437
1438 down_write(&pmd->root_lock);
1439
1440 r = __commit_transaction(pmd);
1441 if (r <= 0)
1442 goto out;
1443
1444 /*
1445 * Open the next transaction.
1446 */
1447 r = __begin_transaction(pmd);
1448out:
1449 up_write(&pmd->root_lock);
1450 return r;
1451}
1452
1453int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
1454{
1455 int r;
1456
1457 down_read(&pmd->root_lock);
1458 r = dm_sm_get_nr_free(pmd->data_sm, result);
1459 up_read(&pmd->root_lock);
1460
1461 return r;
1462}
1463
1464int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
1465 dm_block_t *result)
1466{
1467 int r;
1468
1469 down_read(&pmd->root_lock);
1470 r = dm_sm_get_nr_free(pmd->metadata_sm, result);
1471 up_read(&pmd->root_lock);
1472
1473 return r;
1474}
1475
1476int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
1477 dm_block_t *result)
1478{
1479 int r;
1480
1481 down_read(&pmd->root_lock);
1482 r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
1483 up_read(&pmd->root_lock);
1484
1485 return r;
1486}
1487
1488int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result)
1489{
1490 down_read(&pmd->root_lock);
1491 *result = pmd->data_block_size;
1492 up_read(&pmd->root_lock);
1493
1494 return 0;
1495}
1496
1497int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
1498{
1499 int r;
1500
1501 down_read(&pmd->root_lock);
1502 r = dm_sm_get_nr_blocks(pmd->data_sm, result);
1503 up_read(&pmd->root_lock);
1504
1505 return r;
1506}
1507
1508int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
1509{
1510 struct dm_pool_metadata *pmd = td->pmd;
1511
1512 down_read(&pmd->root_lock);
1513 *result = td->mapped_blocks;
1514 up_read(&pmd->root_lock);
1515
1516 return 0;
1517}
1518
1519static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
1520{
1521 int r;
1522 __le64 value_le;
1523 dm_block_t thin_root;
1524 struct dm_pool_metadata *pmd = td->pmd;
1525
1526 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
1527 if (r)
1528 return r;
1529
1530 thin_root = le64_to_cpu(value_le);
1531
1532 return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
1533}
1534
1535int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
1536 dm_block_t *result)
1537{
1538 int r;
1539 struct dm_pool_metadata *pmd = td->pmd;
1540
1541 down_read(&pmd->root_lock);
1542 r = __highest_block(td, result);
1543 up_read(&pmd->root_lock);
1544
1545 return r;
1546}
1547
1548static int __resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1549{
1550 int r;
1551 dm_block_t old_count;
1552
1553 r = dm_sm_get_nr_blocks(pmd->data_sm, &old_count);
1554 if (r)
1555 return r;
1556
1557 if (new_count == old_count)
1558 return 0;
1559
1560 if (new_count < old_count) {
1561 DMERR("cannot reduce size of data device");
1562 return -EINVAL;
1563 }
1564
eb04cf63 1565 return dm_sm_extend(pmd->data_sm, new_count - old_count);
991d9fa0
JT
1566}
1567
1568int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1569{
1570 int r;
1571
1572 down_write(&pmd->root_lock);
1573 r = __resize_data_dev(pmd, new_count);
1574 up_write(&pmd->root_lock);
1575
1576 return r;
1577}