blob: c8f36227fd7b8b788ee7ba44c24734f7586e8ca9 [file] [log] [blame]
Joe Thornber991d9fa2011-10-31 20:21:18 +00001/*
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
Joe Thornber8c971172012-07-27 15:07:58 +010083/*
84 * 3 for btree insert +
85 * 2 for btree lookup used within space map
86 */
87#define THIN_MAX_CONCURRENT_LOCKS 5
88
Joe Thornber991d9fa2011-10-31 20:21:18 +000089/* 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;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000181 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
346static int superblock_all_zeroes(struct dm_block_manager *bm, int *result)
347{
348 int r;
349 unsigned i;
350 struct dm_block *b;
351 __le64 *data_le, zero = cpu_to_le64(0);
352 unsigned block_size = dm_bm_block_size(bm) / sizeof(__le64);
353
354 /*
355 * We can't use a validator here - it may be all zeroes.
356 */
357 r = dm_bm_read_lock(bm, THIN_SUPERBLOCK_LOCATION, NULL, &b);
358 if (r)
359 return r;
360
361 data_le = dm_block_data(b);
362 *result = 1;
363 for (i = 0; i < block_size; i++) {
364 if (data_le[i] != zero) {
365 *result = 0;
366 break;
367 }
368 }
369
370 return dm_bm_unlock(b);
371}
372
Joe Thornber41675ae2012-07-27 15:08:08 +0100373static void __setup_btree_details(struct dm_pool_metadata *pmd)
374{
375 pmd->info.tm = pmd->tm;
376 pmd->info.levels = 2;
377 pmd->info.value_type.context = pmd->data_sm;
378 pmd->info.value_type.size = sizeof(__le64);
379 pmd->info.value_type.inc = data_block_inc;
380 pmd->info.value_type.dec = data_block_dec;
381 pmd->info.value_type.equal = data_block_equal;
382
383 memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info));
384 pmd->nb_info.tm = pmd->nb_tm;
385
386 pmd->tl_info.tm = pmd->tm;
387 pmd->tl_info.levels = 1;
388 pmd->tl_info.value_type.context = &pmd->info;
389 pmd->tl_info.value_type.size = sizeof(__le64);
390 pmd->tl_info.value_type.inc = subtree_inc;
391 pmd->tl_info.value_type.dec = subtree_dec;
392 pmd->tl_info.value_type.equal = subtree_equal;
393
394 pmd->bl_info.tm = pmd->tm;
395 pmd->bl_info.levels = 1;
396 pmd->bl_info.value_type.context = pmd->data_sm;
397 pmd->bl_info.value_type.size = sizeof(__le64);
398 pmd->bl_info.value_type.inc = data_block_inc;
399 pmd->bl_info.value_type.dec = data_block_dec;
400 pmd->bl_info.value_type.equal = data_block_equal;
401
402 pmd->details_info.tm = pmd->tm;
403 pmd->details_info.levels = 1;
404 pmd->details_info.value_type.context = NULL;
405 pmd->details_info.value_type.size = sizeof(struct disk_device_details);
406 pmd->details_info.value_type.inc = NULL;
407 pmd->details_info.value_type.dec = NULL;
408 pmd->details_info.value_type.equal = NULL;
409}
410
Joe Thornber991d9fa2011-10-31 20:21:18 +0000411static int init_pmd(struct dm_pool_metadata *pmd,
412 struct dm_block_manager *bm,
413 dm_block_t nr_blocks, int create)
414{
415 int r;
416 struct dm_space_map *sm, *data_sm;
417 struct dm_transaction_manager *tm;
418 struct dm_block *sblock;
419
420 if (create) {
421 r = dm_tm_create_with_sm(bm, THIN_SUPERBLOCK_LOCATION,
422 &sb_validator, &tm, &sm, &sblock);
423 if (r < 0) {
424 DMERR("tm_create_with_sm failed");
425 return r;
426 }
427
428 data_sm = dm_sm_disk_create(tm, nr_blocks);
429 if (IS_ERR(data_sm)) {
430 DMERR("sm_disk_create failed");
Joe Thornber4469a5f2012-03-07 19:09:43 +0000431 dm_tm_unlock(tm, sblock);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000432 r = PTR_ERR(data_sm);
433 goto bad;
434 }
435 } else {
436 struct thin_disk_superblock *disk_super = NULL;
437 size_t space_map_root_offset =
438 offsetof(struct thin_disk_superblock, metadata_space_map_root);
439
440 r = dm_tm_open_with_sm(bm, THIN_SUPERBLOCK_LOCATION,
441 &sb_validator, space_map_root_offset,
442 SPACE_MAP_ROOT_SIZE, &tm, &sm, &sblock);
443 if (r < 0) {
444 DMERR("tm_open_with_sm failed");
445 return r;
446 }
447
448 disk_super = dm_block_data(sblock);
449 data_sm = dm_sm_disk_open(tm, disk_super->data_space_map_root,
450 sizeof(disk_super->data_space_map_root));
451 if (IS_ERR(data_sm)) {
452 DMERR("sm_disk_open failed");
453 r = PTR_ERR(data_sm);
454 goto bad;
455 }
456 }
457
458
459 r = dm_tm_unlock(tm, sblock);
460 if (r < 0) {
461 DMERR("couldn't unlock superblock");
462 goto bad_data_sm;
463 }
464
465 pmd->bm = bm;
466 pmd->metadata_sm = sm;
467 pmd->data_sm = data_sm;
468 pmd->tm = tm;
469 pmd->nb_tm = dm_tm_create_non_blocking_clone(tm);
470 if (!pmd->nb_tm) {
471 DMERR("could not create clone tm");
472 r = -ENOMEM;
473 goto bad_data_sm;
474 }
475
Joe Thornber41675ae2012-07-27 15:08:08 +0100476 __setup_btree_details(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000477 pmd->root = 0;
478
479 init_rwsem(&pmd->root_lock);
480 pmd->time = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000481 pmd->details_root = 0;
482 pmd->trans_id = 0;
483 pmd->flags = 0;
484 INIT_LIST_HEAD(&pmd->thin_devices);
485
486 return 0;
487
488bad_data_sm:
489 dm_sm_destroy(data_sm);
490bad:
491 dm_tm_destroy(tm);
492 dm_sm_destroy(sm);
493
494 return r;
495}
496
497static int __begin_transaction(struct dm_pool_metadata *pmd)
498{
499 int r;
500 u32 features;
501 struct thin_disk_superblock *disk_super;
502 struct dm_block *sblock;
503
504 /*
Joe Thornber991d9fa2011-10-31 20:21:18 +0000505 * We re-read the superblock every time. Shouldn't need to do this
506 * really.
507 */
508 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
509 &sb_validator, &sblock);
510 if (r)
511 return r;
512
513 disk_super = dm_block_data(sblock);
514 pmd->time = le32_to_cpu(disk_super->time);
515 pmd->root = le64_to_cpu(disk_super->data_mapping_root);
516 pmd->details_root = le64_to_cpu(disk_super->device_details_root);
517 pmd->trans_id = le64_to_cpu(disk_super->trans_id);
518 pmd->flags = le32_to_cpu(disk_super->flags);
519 pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
520
521 features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP;
522 if (features) {
523 DMERR("could not access metadata due to "
524 "unsupported optional features (%lx).",
525 (unsigned long)features);
526 r = -EINVAL;
527 goto out;
528 }
529
530 /*
531 * Check for read-only metadata to skip the following RDWR checks.
532 */
533 if (get_disk_ro(pmd->bdev->bd_disk))
534 goto out;
535
536 features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
537 if (features) {
538 DMERR("could not access metadata RDWR due to "
539 "unsupported optional features (%lx).",
540 (unsigned long)features);
541 r = -EINVAL;
542 }
543
544out:
545 dm_bm_unlock(sblock);
546 return r;
547}
548
549static int __write_changed_details(struct dm_pool_metadata *pmd)
550{
551 int r;
552 struct dm_thin_device *td, *tmp;
553 struct disk_device_details details;
554 uint64_t key;
555
556 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
557 if (!td->changed)
558 continue;
559
560 key = td->id;
561
562 details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
563 details.transaction_id = cpu_to_le64(td->transaction_id);
564 details.creation_time = cpu_to_le32(td->creation_time);
565 details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
566 __dm_bless_for_disk(&details);
567
568 r = dm_btree_insert(&pmd->details_info, pmd->details_root,
569 &key, &details, &pmd->details_root);
570 if (r)
571 return r;
572
573 if (td->open_count)
574 td->changed = 0;
575 else {
576 list_del(&td->list);
577 kfree(td);
578 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000579 }
580
581 return 0;
582}
583
584static int __commit_transaction(struct dm_pool_metadata *pmd)
585{
586 /*
587 * FIXME: Associated pool should be made read-only on failure.
588 */
589 int r;
590 size_t metadata_len, data_len;
591 struct thin_disk_superblock *disk_super;
592 struct dm_block *sblock;
593
594 /*
595 * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
596 */
597 BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
598
599 r = __write_changed_details(pmd);
600 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100601 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000602
Joe Thornber991d9fa2011-10-31 20:21:18 +0000603 r = dm_sm_commit(pmd->data_sm);
604 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100605 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000606
607 r = dm_tm_pre_commit(pmd->tm);
608 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100609 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000610
611 r = dm_sm_root_size(pmd->metadata_sm, &metadata_len);
612 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100613 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000614
Joe Thornberfef838c2012-03-28 18:41:25 +0100615 r = dm_sm_root_size(pmd->data_sm, &data_len);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000616 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100617 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000618
619 r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
620 &sb_validator, &sblock);
621 if (r)
Joe Thornberd973ac12012-07-27 15:07:58 +0100622 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000623
624 disk_super = dm_block_data(sblock);
625 disk_super->time = cpu_to_le32(pmd->time);
626 disk_super->data_mapping_root = cpu_to_le64(pmd->root);
627 disk_super->device_details_root = cpu_to_le64(pmd->details_root);
628 disk_super->trans_id = cpu_to_le64(pmd->trans_id);
629 disk_super->flags = cpu_to_le32(pmd->flags);
630
631 r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root,
632 metadata_len);
633 if (r < 0)
634 goto out_locked;
635
636 r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root,
637 data_len);
638 if (r < 0)
639 goto out_locked;
640
Joe Thornbereb04cf62012-07-27 15:08:08 +0100641 return dm_tm_commit(pmd->tm, sblock);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000642
643out_locked:
644 dm_bm_unlock(sblock);
645 return r;
646}
647
648struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
649 sector_t data_block_size)
650{
651 int r;
652 struct thin_disk_superblock *disk_super;
653 struct dm_pool_metadata *pmd;
654 sector_t bdev_size = i_size_read(bdev->bd_inode) >> SECTOR_SHIFT;
655 struct dm_block_manager *bm;
656 int create;
657 struct dm_block *sblock;
658
659 pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
660 if (!pmd) {
661 DMERR("could not allocate metadata struct");
662 return ERR_PTR(-ENOMEM);
663 }
664
Joe Thornber991d9fa2011-10-31 20:21:18 +0000665 bm = dm_block_manager_create(bdev, THIN_METADATA_BLOCK_SIZE,
Joe Thornber8c971172012-07-27 15:07:58 +0100666 THIN_METADATA_CACHE_SIZE,
667 THIN_MAX_CONCURRENT_LOCKS);
Joe Thornber51a0f652012-07-27 15:08:08 +0100668 if (IS_ERR(bm)) {
669 r = PTR_ERR(bm);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000670 DMERR("could not create block manager");
671 kfree(pmd);
Joe Thornber51a0f652012-07-27 15:08:08 +0100672 return ERR_PTR(r);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000673 }
674
675 r = superblock_all_zeroes(bm, &create);
676 if (r) {
677 dm_block_manager_destroy(bm);
678 kfree(pmd);
679 return ERR_PTR(r);
680 }
681
Joe Thornber991d9fa2011-10-31 20:21:18 +0000682 r = init_pmd(pmd, bm, 0, create);
683 if (r) {
684 dm_block_manager_destroy(bm);
685 kfree(pmd);
686 return ERR_PTR(r);
687 }
688 pmd->bdev = bdev;
689
690 if (!create) {
691 r = __begin_transaction(pmd);
692 if (r < 0)
693 goto bad;
694 return pmd;
695 }
696
697 /*
698 * Create.
699 */
700 r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
701 &sb_validator, &sblock);
702 if (r)
703 goto bad;
704
Mike Snitzerc4a69ec2012-03-28 18:41:28 +0100705 if (bdev_size > THIN_METADATA_MAX_SECTORS)
706 bdev_size = THIN_METADATA_MAX_SECTORS;
707
Joe Thornber991d9fa2011-10-31 20:21:18 +0000708 disk_super = dm_block_data(sblock);
709 disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
710 disk_super->version = cpu_to_le32(THIN_VERSION);
711 disk_super->time = 0;
712 disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
713 disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
714 disk_super->data_block_size = cpu_to_le32(data_block_size);
715
716 r = dm_bm_unlock(sblock);
717 if (r < 0)
718 goto bad;
719
720 r = dm_btree_empty(&pmd->info, &pmd->root);
721 if (r < 0)
722 goto bad;
723
724 r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
725 if (r < 0) {
726 DMERR("couldn't create devices root");
727 goto bad;
728 }
729
730 pmd->flags = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000731 r = dm_pool_commit_metadata(pmd);
732 if (r < 0) {
733 DMERR("%s: dm_pool_commit_metadata() failed, error = %d",
734 __func__, r);
735 goto bad;
736 }
737
738 return pmd;
739
740bad:
741 if (dm_pool_metadata_close(pmd) < 0)
742 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
743 return ERR_PTR(r);
744}
745
746int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
747{
748 int r;
749 unsigned open_devices = 0;
750 struct dm_thin_device *td, *tmp;
751
752 down_read(&pmd->root_lock);
753 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
754 if (td->open_count)
755 open_devices++;
756 else {
757 list_del(&td->list);
758 kfree(td);
759 }
760 }
761 up_read(&pmd->root_lock);
762
763 if (open_devices) {
764 DMERR("attempt to close pmd when %u device(s) are still open",
765 open_devices);
766 return -EBUSY;
767 }
768
769 r = __commit_transaction(pmd);
770 if (r < 0)
771 DMWARN("%s: __commit_transaction() failed, error = %d",
772 __func__, r);
773
774 dm_tm_destroy(pmd->tm);
775 dm_tm_destroy(pmd->nb_tm);
776 dm_block_manager_destroy(pmd->bm);
777 dm_sm_destroy(pmd->metadata_sm);
778 dm_sm_destroy(pmd->data_sm);
779 kfree(pmd);
780
781 return 0;
782}
783
Mike Snitzer1f3db252012-03-07 19:09:41 +0000784/*
785 * __open_device: Returns @td corresponding to device with id @dev,
786 * creating it if @create is set and incrementing @td->open_count.
787 * On failure, @td is undefined.
788 */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000789static int __open_device(struct dm_pool_metadata *pmd,
790 dm_thin_id dev, int create,
791 struct dm_thin_device **td)
792{
793 int r, changed = 0;
794 struct dm_thin_device *td2;
795 uint64_t key = dev;
796 struct disk_device_details details_le;
797
798 /*
Mike Snitzer1f3db252012-03-07 19:09:41 +0000799 * If the device is already open, return it.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000800 */
801 list_for_each_entry(td2, &pmd->thin_devices, list)
802 if (td2->id == dev) {
Mike Snitzer1f3db252012-03-07 19:09:41 +0000803 /*
804 * May not create an already-open device.
805 */
806 if (create)
807 return -EEXIST;
808
Joe Thornber991d9fa2011-10-31 20:21:18 +0000809 td2->open_count++;
810 *td = td2;
811 return 0;
812 }
813
814 /*
815 * Check the device exists.
816 */
817 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
818 &key, &details_le);
819 if (r) {
820 if (r != -ENODATA || !create)
821 return r;
822
Mike Snitzer1f3db252012-03-07 19:09:41 +0000823 /*
824 * Create new device.
825 */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000826 changed = 1;
827 details_le.mapped_blocks = 0;
828 details_le.transaction_id = cpu_to_le64(pmd->trans_id);
829 details_le.creation_time = cpu_to_le32(pmd->time);
830 details_le.snapshotted_time = cpu_to_le32(pmd->time);
831 }
832
833 *td = kmalloc(sizeof(**td), GFP_NOIO);
834 if (!*td)
835 return -ENOMEM;
836
837 (*td)->pmd = pmd;
838 (*td)->id = dev;
839 (*td)->open_count = 1;
840 (*td)->changed = changed;
841 (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
842 (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
843 (*td)->creation_time = le32_to_cpu(details_le.creation_time);
844 (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
845
846 list_add(&(*td)->list, &pmd->thin_devices);
847
848 return 0;
849}
850
851static void __close_device(struct dm_thin_device *td)
852{
853 --td->open_count;
854}
855
856static int __create_thin(struct dm_pool_metadata *pmd,
857 dm_thin_id dev)
858{
859 int r;
860 dm_block_t dev_root;
861 uint64_t key = dev;
862 struct disk_device_details details_le;
863 struct dm_thin_device *td;
864 __le64 value;
865
866 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
867 &key, &details_le);
868 if (!r)
869 return -EEXIST;
870
871 /*
872 * Create an empty btree for the mappings.
873 */
874 r = dm_btree_empty(&pmd->bl_info, &dev_root);
875 if (r)
876 return r;
877
878 /*
879 * Insert it into the main mapping tree.
880 */
881 value = cpu_to_le64(dev_root);
882 __dm_bless_for_disk(&value);
883 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
884 if (r) {
885 dm_btree_del(&pmd->bl_info, dev_root);
886 return r;
887 }
888
889 r = __open_device(pmd, dev, 1, &td);
890 if (r) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000891 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
892 dm_btree_del(&pmd->bl_info, dev_root);
893 return r;
894 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000895 __close_device(td);
896
897 return r;
898}
899
900int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
901{
902 int r;
903
904 down_write(&pmd->root_lock);
905 r = __create_thin(pmd, dev);
906 up_write(&pmd->root_lock);
907
908 return r;
909}
910
911static int __set_snapshot_details(struct dm_pool_metadata *pmd,
912 struct dm_thin_device *snap,
913 dm_thin_id origin, uint32_t time)
914{
915 int r;
916 struct dm_thin_device *td;
917
918 r = __open_device(pmd, origin, 0, &td);
919 if (r)
920 return r;
921
922 td->changed = 1;
923 td->snapshotted_time = time;
924
925 snap->mapped_blocks = td->mapped_blocks;
926 snap->snapshotted_time = time;
927 __close_device(td);
928
929 return 0;
930}
931
932static int __create_snap(struct dm_pool_metadata *pmd,
933 dm_thin_id dev, dm_thin_id origin)
934{
935 int r;
936 dm_block_t origin_root;
937 uint64_t key = origin, dev_key = dev;
938 struct dm_thin_device *td;
939 struct disk_device_details details_le;
940 __le64 value;
941
942 /* check this device is unused */
943 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
944 &dev_key, &details_le);
945 if (!r)
946 return -EEXIST;
947
948 /* find the mapping tree for the origin */
949 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
950 if (r)
951 return r;
952 origin_root = le64_to_cpu(value);
953
954 /* clone the origin, an inc will do */
955 dm_tm_inc(pmd->tm, origin_root);
956
957 /* insert into the main mapping tree */
958 value = cpu_to_le64(origin_root);
959 __dm_bless_for_disk(&value);
960 key = dev;
961 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
962 if (r) {
963 dm_tm_dec(pmd->tm, origin_root);
964 return r;
965 }
966
967 pmd->time++;
968
969 r = __open_device(pmd, dev, 1, &td);
970 if (r)
971 goto bad;
972
973 r = __set_snapshot_details(pmd, td, origin, pmd->time);
Mike Snitzer1f3db252012-03-07 19:09:41 +0000974 __close_device(td);
975
Joe Thornber991d9fa2011-10-31 20:21:18 +0000976 if (r)
977 goto bad;
978
Joe Thornber991d9fa2011-10-31 20:21:18 +0000979 return 0;
980
981bad:
Joe Thornber991d9fa2011-10-31 20:21:18 +0000982 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
983 dm_btree_remove(&pmd->details_info, pmd->details_root,
984 &key, &pmd->details_root);
985 return r;
986}
987
988int dm_pool_create_snap(struct dm_pool_metadata *pmd,
989 dm_thin_id dev,
990 dm_thin_id origin)
991{
992 int r;
993
994 down_write(&pmd->root_lock);
995 r = __create_snap(pmd, dev, origin);
996 up_write(&pmd->root_lock);
997
998 return r;
999}
1000
1001static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
1002{
1003 int r;
1004 uint64_t key = dev;
1005 struct dm_thin_device *td;
1006
1007 /* TODO: failure should mark the transaction invalid */
1008 r = __open_device(pmd, dev, 0, &td);
1009 if (r)
1010 return r;
1011
1012 if (td->open_count > 1) {
1013 __close_device(td);
1014 return -EBUSY;
1015 }
1016
1017 list_del(&td->list);
1018 kfree(td);
1019 r = dm_btree_remove(&pmd->details_info, pmd->details_root,
1020 &key, &pmd->details_root);
1021 if (r)
1022 return r;
1023
1024 r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1025 if (r)
1026 return r;
1027
Joe Thornber991d9fa2011-10-31 20:21:18 +00001028 return 0;
1029}
1030
1031int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
1032 dm_thin_id dev)
1033{
1034 int r;
1035
1036 down_write(&pmd->root_lock);
1037 r = __delete_device(pmd, dev);
1038 up_write(&pmd->root_lock);
1039
1040 return r;
1041}
1042
1043int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
1044 uint64_t current_id,
1045 uint64_t new_id)
1046{
1047 down_write(&pmd->root_lock);
1048 if (pmd->trans_id != current_id) {
1049 up_write(&pmd->root_lock);
1050 DMERR("mismatched transaction id");
1051 return -EINVAL;
1052 }
1053
1054 pmd->trans_id = new_id;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001055 up_write(&pmd->root_lock);
1056
1057 return 0;
1058}
1059
1060int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
1061 uint64_t *result)
1062{
1063 down_read(&pmd->root_lock);
1064 *result = pmd->trans_id;
1065 up_read(&pmd->root_lock);
1066
1067 return 0;
1068}
1069
Joe Thornbercc8394d2012-06-03 00:30:01 +01001070static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
1071{
1072 int r, inc;
1073 struct thin_disk_superblock *disk_super;
1074 struct dm_block *copy, *sblock;
1075 dm_block_t held_root;
1076
1077 /*
1078 * Copy the superblock.
1079 */
1080 dm_sm_inc_block(pmd->metadata_sm, THIN_SUPERBLOCK_LOCATION);
1081 r = dm_tm_shadow_block(pmd->tm, THIN_SUPERBLOCK_LOCATION,
1082 &sb_validator, &copy, &inc);
1083 if (r)
1084 return r;
1085
1086 BUG_ON(!inc);
1087
1088 held_root = dm_block_location(copy);
1089 disk_super = dm_block_data(copy);
1090
1091 if (le64_to_cpu(disk_super->held_root)) {
1092 DMWARN("Pool metadata snapshot already exists: release this before taking another.");
1093
1094 dm_tm_dec(pmd->tm, held_root);
1095 dm_tm_unlock(pmd->tm, copy);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001096 return -EBUSY;
1097 }
1098
1099 /*
1100 * Wipe the spacemap since we're not publishing this.
1101 */
1102 memset(&disk_super->data_space_map_root, 0,
1103 sizeof(disk_super->data_space_map_root));
1104 memset(&disk_super->metadata_space_map_root, 0,
1105 sizeof(disk_super->metadata_space_map_root));
1106
1107 /*
1108 * Increment the data structures that need to be preserved.
1109 */
1110 dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->data_mapping_root));
1111 dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->device_details_root));
1112 dm_tm_unlock(pmd->tm, copy);
1113
1114 /*
1115 * Write the held root into the superblock.
1116 */
1117 r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
1118 &sb_validator, &sblock);
1119 if (r) {
1120 dm_tm_dec(pmd->tm, held_root);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001121 return r;
1122 }
1123
1124 disk_super = dm_block_data(sblock);
1125 disk_super->held_root = cpu_to_le64(held_root);
1126 dm_bm_unlock(sblock);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001127 return 0;
1128}
1129
1130int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd)
1131{
1132 int r;
1133
1134 down_write(&pmd->root_lock);
1135 r = __reserve_metadata_snap(pmd);
1136 up_write(&pmd->root_lock);
1137
1138 return r;
1139}
1140
1141static int __release_metadata_snap(struct dm_pool_metadata *pmd)
1142{
1143 int r;
1144 struct thin_disk_superblock *disk_super;
1145 struct dm_block *sblock, *copy;
1146 dm_block_t held_root;
1147
1148 r = dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
1149 &sb_validator, &sblock);
1150 if (r)
1151 return r;
1152
1153 disk_super = dm_block_data(sblock);
1154 held_root = le64_to_cpu(disk_super->held_root);
1155 disk_super->held_root = cpu_to_le64(0);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001156
1157 dm_bm_unlock(sblock);
1158
1159 if (!held_root) {
1160 DMWARN("No pool metadata snapshot found: nothing to release.");
1161 return -EINVAL;
1162 }
1163
1164 r = dm_tm_read_lock(pmd->tm, held_root, &sb_validator, &copy);
1165 if (r)
1166 return r;
1167
1168 disk_super = dm_block_data(copy);
1169 dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->data_mapping_root));
1170 dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->device_details_root));
1171 dm_sm_dec_block(pmd->metadata_sm, held_root);
1172
1173 return dm_tm_unlock(pmd->tm, copy);
1174}
1175
1176int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd)
1177{
1178 int r;
1179
1180 down_write(&pmd->root_lock);
1181 r = __release_metadata_snap(pmd);
1182 up_write(&pmd->root_lock);
1183
1184 return r;
1185}
1186
1187static int __get_metadata_snap(struct dm_pool_metadata *pmd,
1188 dm_block_t *result)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001189{
1190 int r;
1191 struct thin_disk_superblock *disk_super;
1192 struct dm_block *sblock;
1193
Joe Thornbercc8394d2012-06-03 00:30:01 +01001194 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
1195 &sb_validator, &sblock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001196 if (r)
1197 return r;
1198
1199 disk_super = dm_block_data(sblock);
1200 *result = le64_to_cpu(disk_super->held_root);
1201
1202 return dm_bm_unlock(sblock);
1203}
1204
Joe Thornbercc8394d2012-06-03 00:30:01 +01001205int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
1206 dm_block_t *result)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001207{
1208 int r;
1209
1210 down_read(&pmd->root_lock);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001211 r = __get_metadata_snap(pmd, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001212 up_read(&pmd->root_lock);
1213
1214 return r;
1215}
1216
1217int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
1218 struct dm_thin_device **td)
1219{
1220 int r;
1221
1222 down_write(&pmd->root_lock);
1223 r = __open_device(pmd, dev, 0, td);
1224 up_write(&pmd->root_lock);
1225
1226 return r;
1227}
1228
1229int dm_pool_close_thin_device(struct dm_thin_device *td)
1230{
1231 down_write(&td->pmd->root_lock);
1232 __close_device(td);
1233 up_write(&td->pmd->root_lock);
1234
1235 return 0;
1236}
1237
1238dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
1239{
1240 return td->id;
1241}
1242
Mike Snitzer17b7d632012-07-27 15:07:57 +01001243static bool __snapshotted_since(struct dm_thin_device *td, uint32_t time)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001244{
1245 return td->snapshotted_time > time;
1246}
1247
1248int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
1249 int can_block, struct dm_thin_lookup_result *result)
1250{
1251 int r;
1252 uint64_t block_time = 0;
1253 __le64 value;
1254 struct dm_pool_metadata *pmd = td->pmd;
1255 dm_block_t keys[2] = { td->id, block };
1256
1257 if (can_block) {
1258 down_read(&pmd->root_lock);
1259 r = dm_btree_lookup(&pmd->info, pmd->root, keys, &value);
1260 if (!r)
1261 block_time = le64_to_cpu(value);
1262 up_read(&pmd->root_lock);
1263
1264 } else if (down_read_trylock(&pmd->root_lock)) {
1265 r = dm_btree_lookup(&pmd->nb_info, pmd->root, keys, &value);
1266 if (!r)
1267 block_time = le64_to_cpu(value);
1268 up_read(&pmd->root_lock);
1269
1270 } else
1271 return -EWOULDBLOCK;
1272
1273 if (!r) {
1274 dm_block_t exception_block;
1275 uint32_t exception_time;
1276 unpack_block_time(block_time, &exception_block,
1277 &exception_time);
1278 result->block = exception_block;
1279 result->shared = __snapshotted_since(td, exception_time);
1280 }
1281
1282 return r;
1283}
1284
1285static int __insert(struct dm_thin_device *td, dm_block_t block,
1286 dm_block_t data_block)
1287{
1288 int r, inserted;
1289 __le64 value;
1290 struct dm_pool_metadata *pmd = td->pmd;
1291 dm_block_t keys[2] = { td->id, block };
1292
Joe Thornber991d9fa2011-10-31 20:21:18 +00001293 value = cpu_to_le64(pack_block_time(data_block, pmd->time));
1294 __dm_bless_for_disk(&value);
1295
1296 r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
1297 &pmd->root, &inserted);
1298 if (r)
1299 return r;
1300
1301 if (inserted) {
1302 td->mapped_blocks++;
1303 td->changed = 1;
1304 }
1305
1306 return 0;
1307}
1308
1309int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
1310 dm_block_t data_block)
1311{
1312 int r;
1313
1314 down_write(&td->pmd->root_lock);
1315 r = __insert(td, block, data_block);
1316 up_write(&td->pmd->root_lock);
1317
1318 return r;
1319}
1320
1321static int __remove(struct dm_thin_device *td, dm_block_t block)
1322{
1323 int r;
1324 struct dm_pool_metadata *pmd = td->pmd;
1325 dm_block_t keys[2] = { td->id, block };
1326
1327 r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
1328 if (r)
1329 return r;
1330
Joe Thornberaf63bcb2012-03-07 19:09:44 +00001331 td->mapped_blocks--;
1332 td->changed = 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001333
1334 return 0;
1335}
1336
1337int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
1338{
1339 int r;
1340
1341 down_write(&td->pmd->root_lock);
1342 r = __remove(td, block);
1343 up_write(&td->pmd->root_lock);
1344
1345 return r;
1346}
1347
1348int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
1349{
1350 int r;
1351
1352 down_write(&pmd->root_lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001353 r = dm_sm_new_block(pmd->data_sm, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001354 up_write(&pmd->root_lock);
1355
1356 return r;
1357}
1358
1359int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
1360{
1361 int r;
1362
1363 down_write(&pmd->root_lock);
1364
1365 r = __commit_transaction(pmd);
1366 if (r <= 0)
1367 goto out;
1368
1369 /*
1370 * Open the next transaction.
1371 */
1372 r = __begin_transaction(pmd);
1373out:
1374 up_write(&pmd->root_lock);
1375 return r;
1376}
1377
1378int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
1379{
1380 int r;
1381
1382 down_read(&pmd->root_lock);
1383 r = dm_sm_get_nr_free(pmd->data_sm, result);
1384 up_read(&pmd->root_lock);
1385
1386 return r;
1387}
1388
1389int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
1390 dm_block_t *result)
1391{
1392 int r;
1393
1394 down_read(&pmd->root_lock);
1395 r = dm_sm_get_nr_free(pmd->metadata_sm, result);
1396 up_read(&pmd->root_lock);
1397
1398 return r;
1399}
1400
1401int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
1402 dm_block_t *result)
1403{
1404 int r;
1405
1406 down_read(&pmd->root_lock);
1407 r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
1408 up_read(&pmd->root_lock);
1409
1410 return r;
1411}
1412
1413int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result)
1414{
1415 down_read(&pmd->root_lock);
1416 *result = pmd->data_block_size;
1417 up_read(&pmd->root_lock);
1418
1419 return 0;
1420}
1421
1422int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
1423{
1424 int r;
1425
1426 down_read(&pmd->root_lock);
1427 r = dm_sm_get_nr_blocks(pmd->data_sm, result);
1428 up_read(&pmd->root_lock);
1429
1430 return r;
1431}
1432
1433int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
1434{
1435 struct dm_pool_metadata *pmd = td->pmd;
1436
1437 down_read(&pmd->root_lock);
1438 *result = td->mapped_blocks;
1439 up_read(&pmd->root_lock);
1440
1441 return 0;
1442}
1443
1444static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
1445{
1446 int r;
1447 __le64 value_le;
1448 dm_block_t thin_root;
1449 struct dm_pool_metadata *pmd = td->pmd;
1450
1451 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
1452 if (r)
1453 return r;
1454
1455 thin_root = le64_to_cpu(value_le);
1456
1457 return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
1458}
1459
1460int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
1461 dm_block_t *result)
1462{
1463 int r;
1464 struct dm_pool_metadata *pmd = td->pmd;
1465
1466 down_read(&pmd->root_lock);
1467 r = __highest_block(td, result);
1468 up_read(&pmd->root_lock);
1469
1470 return r;
1471}
1472
1473static int __resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1474{
1475 int r;
1476 dm_block_t old_count;
1477
1478 r = dm_sm_get_nr_blocks(pmd->data_sm, &old_count);
1479 if (r)
1480 return r;
1481
1482 if (new_count == old_count)
1483 return 0;
1484
1485 if (new_count < old_count) {
1486 DMERR("cannot reduce size of data device");
1487 return -EINVAL;
1488 }
1489
Joe Thornbereb04cf62012-07-27 15:08:08 +01001490 return dm_sm_extend(pmd->data_sm, new_count - old_count);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001491}
1492
1493int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1494{
1495 int r;
1496
1497 down_write(&pmd->root_lock);
1498 r = __resize_data_dev(pmd, new_count);
1499 up_write(&pmd->root_lock);
1500
1501 return r;
1502}