]> git.kernelconcepts.de Git - karo-tx-linux.git/blob - drivers/md/dm-thin-metadata.c
dm thin metadata: add format option to dm_pool_metadata_open
[karo-tx-linux.git] / drivers / md / dm-thin-metadata.c
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
83 /*
84  *  3 for btree insert +
85  *  2 for btree lookup used within space map
86  */
87 #define THIN_MAX_CONCURRENT_LOCKS 5
88
89 /* This should be plenty */
90 #define SPACE_MAP_ROOT_SIZE 128
91
92 /*
93  * Little endian on-disk superblock and device details.
94  */
95 struct 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
135 struct disk_device_details {
136         __le64 mapped_blocks;
137         __le64 transaction_id;          /* When created. */
138         __le32 creation_time;
139         __le32 snapshotted_time;
140 } __packed;
141
142 struct 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;
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
189 struct 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
208 static 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
220 static 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
253 static 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
263 static uint64_t pack_block_time(dm_block_t b, uint32_t t)
264 {
265         return (b << 24) | t;
266 }
267
268 static 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
274 static 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
286 static 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
298 static 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
312 static 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
323 static 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
335 static 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
346 static 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
353 static 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
360 static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result)
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
387 static 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
425 static int __write_initial_superblock(struct dm_pool_metadata *pmd)
426 {
427         int r;
428         struct dm_block *sblock;
429         size_t metadata_len, data_len;
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
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
452         r = superblock_lock_zero(pmd, &sblock);
453         if (r)
454                 return r;
455
456         disk_super = dm_block_data(sblock);
457         disk_super->flags = 0;
458         memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
459         disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
460         disk_super->version = cpu_to_le32(THIN_VERSION);
461         disk_super->time = 0;
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);
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
481         return dm_tm_commit(pmd->tm, sblock);
482
483 bad_locked:
484         dm_bm_unlock(sblock);
485         return r;
486 }
487
488 static int __format_metadata(struct dm_pool_metadata *pmd)
489 {
490         int r;
491
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         }
498
499         pmd->data_sm = dm_sm_disk_create(pmd->tm, 0);
500         if (IS_ERR(pmd->data_sm)) {
501                 DMERR("sm_disk_create failed");
502                 r = PTR_ERR(pmd->data_sm);
503                 goto bad_cleanup_tm;
504         }
505
506         pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
507         if (!pmd->nb_tm) {
508                 DMERR("could not create non-blocking clone tm");
509                 r = -ENOMEM;
510                 goto bad_cleanup_data_sm;
511         }
512
513         __setup_btree_details(pmd);
514
515         r = dm_btree_empty(&pmd->info, &pmd->root);
516         if (r < 0)
517                 goto bad_cleanup_nb_tm;
518
519         r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
520         if (r < 0) {
521                 DMERR("couldn't create devices root");
522                 goto bad_cleanup_nb_tm;
523         }
524
525         r = __write_initial_superblock(pmd);
526         if (r)
527                 goto bad_cleanup_nb_tm;
528
529         return 0;
530
531 bad_cleanup_nb_tm:
532         dm_tm_destroy(pmd->nb_tm);
533 bad_cleanup_data_sm:
534         dm_sm_destroy(pmd->data_sm);
535 bad_cleanup_tm:
536         dm_tm_destroy(pmd->tm);
537         dm_sm_destroy(pmd->metadata_sm);
538
539         return r;
540 }
541
542 static int __check_incompat_features(struct thin_disk_superblock *disk_super,
543                                      struct dm_pool_metadata *pmd)
544 {
545         uint32_t features;
546
547         features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP;
548         if (features) {
549                 DMERR("could not access metadata due to unsupported optional features (%lx).",
550                       (unsigned long)features);
551                 return -EINVAL;
552         }
553
554         /*
555          * Check for read-only metadata to skip the following RDWR checks.
556          */
557         if (get_disk_ro(pmd->bdev->bd_disk))
558                 return 0;
559
560         features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
561         if (features) {
562                 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
563                       (unsigned long)features);
564                 return -EINVAL;
565         }
566
567         return 0;
568 }
569
570 static int __open_metadata(struct dm_pool_metadata *pmd)
571 {
572         int r;
573         struct dm_block *sblock;
574         struct thin_disk_superblock *disk_super;
575
576         r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
577                             &sb_validator, &sblock);
578         if (r < 0) {
579                 DMERR("couldn't read superblock");
580                 return r;
581         }
582
583         disk_super = dm_block_data(sblock);
584
585         r = __check_incompat_features(disk_super, pmd);
586         if (r < 0)
587                 goto bad_unlock_sblock;
588
589         r = dm_tm_open_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
590                                disk_super->metadata_space_map_root,
591                                sizeof(disk_super->metadata_space_map_root),
592                                &pmd->tm, &pmd->metadata_sm);
593         if (r < 0) {
594                 DMERR("tm_open_with_sm failed");
595                 goto bad_unlock_sblock;
596         }
597
598         pmd->data_sm = dm_sm_disk_open(pmd->tm, disk_super->data_space_map_root,
599                                        sizeof(disk_super->data_space_map_root));
600         if (IS_ERR(pmd->data_sm)) {
601                 DMERR("sm_disk_open failed");
602                 r = PTR_ERR(pmd->data_sm);
603                 goto bad_cleanup_tm;
604         }
605
606         pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
607         if (!pmd->nb_tm) {
608                 DMERR("could not create non-blocking clone tm");
609                 r = -ENOMEM;
610                 goto bad_cleanup_data_sm;
611         }
612
613         __setup_btree_details(pmd);
614         return dm_bm_unlock(sblock);
615
616 bad_cleanup_data_sm:
617         dm_sm_destroy(pmd->data_sm);
618 bad_cleanup_tm:
619         dm_tm_destroy(pmd->tm);
620         dm_sm_destroy(pmd->metadata_sm);
621 bad_unlock_sblock:
622         dm_bm_unlock(sblock);
623
624         return r;
625 }
626
627 static int __open_or_format_metadata(struct dm_pool_metadata *pmd, bool format_device)
628 {
629         int r, unformatted;
630
631         r = __superblock_all_zeroes(pmd->bm, &unformatted);
632         if (r)
633                 return r;
634
635         if (unformatted)
636                 return format_device ? __format_metadata(pmd) : -EPERM;
637
638         return __open_metadata(pmd);
639 }
640
641 static int __create_persistent_data_objects(struct dm_pool_metadata *pmd, bool format_device)
642 {
643         int r;
644
645         pmd->bm = dm_block_manager_create(pmd->bdev, THIN_METADATA_BLOCK_SIZE,
646                                           THIN_METADATA_CACHE_SIZE,
647                                           THIN_MAX_CONCURRENT_LOCKS);
648         if (IS_ERR(pmd->bm)) {
649                 DMERR("could not create block manager");
650                 return PTR_ERR(pmd->bm);
651         }
652
653         r = __open_or_format_metadata(pmd, format_device);
654         if (r)
655                 dm_block_manager_destroy(pmd->bm);
656
657         return r;
658 }
659
660 static void __destroy_persistent_data_objects(struct dm_pool_metadata *pmd)
661 {
662         dm_sm_destroy(pmd->data_sm);
663         dm_sm_destroy(pmd->metadata_sm);
664         dm_tm_destroy(pmd->nb_tm);
665         dm_tm_destroy(pmd->tm);
666         dm_block_manager_destroy(pmd->bm);
667 }
668
669 static int __begin_transaction(struct dm_pool_metadata *pmd)
670 {
671         int r;
672         struct thin_disk_superblock *disk_super;
673         struct dm_block *sblock;
674
675         /*
676          * We re-read the superblock every time.  Shouldn't need to do this
677          * really.
678          */
679         r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
680                             &sb_validator, &sblock);
681         if (r)
682                 return r;
683
684         disk_super = dm_block_data(sblock);
685         pmd->time = le32_to_cpu(disk_super->time);
686         pmd->root = le64_to_cpu(disk_super->data_mapping_root);
687         pmd->details_root = le64_to_cpu(disk_super->device_details_root);
688         pmd->trans_id = le64_to_cpu(disk_super->trans_id);
689         pmd->flags = le32_to_cpu(disk_super->flags);
690         pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
691
692         dm_bm_unlock(sblock);
693         return 0;
694 }
695
696 static int __write_changed_details(struct dm_pool_metadata *pmd)
697 {
698         int r;
699         struct dm_thin_device *td, *tmp;
700         struct disk_device_details details;
701         uint64_t key;
702
703         list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
704                 if (!td->changed)
705                         continue;
706
707                 key = td->id;
708
709                 details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
710                 details.transaction_id = cpu_to_le64(td->transaction_id);
711                 details.creation_time = cpu_to_le32(td->creation_time);
712                 details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
713                 __dm_bless_for_disk(&details);
714
715                 r = dm_btree_insert(&pmd->details_info, pmd->details_root,
716                                     &key, &details, &pmd->details_root);
717                 if (r)
718                         return r;
719
720                 if (td->open_count)
721                         td->changed = 0;
722                 else {
723                         list_del(&td->list);
724                         kfree(td);
725                 }
726         }
727
728         return 0;
729 }
730
731 static int __commit_transaction(struct dm_pool_metadata *pmd)
732 {
733         /*
734          * FIXME: Associated pool should be made read-only on failure.
735          */
736         int r;
737         size_t metadata_len, data_len;
738         struct thin_disk_superblock *disk_super;
739         struct dm_block *sblock;
740
741         /*
742          * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
743          */
744         BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
745
746         r = __write_changed_details(pmd);
747         if (r < 0)
748                 return r;
749
750         r = dm_sm_commit(pmd->data_sm);
751         if (r < 0)
752                 return r;
753
754         r = dm_tm_pre_commit(pmd->tm);
755         if (r < 0)
756                 return r;
757
758         r = dm_sm_root_size(pmd->metadata_sm, &metadata_len);
759         if (r < 0)
760                 return r;
761
762         r = dm_sm_root_size(pmd->data_sm, &data_len);
763         if (r < 0)
764                 return r;
765
766         r = superblock_lock(pmd, &sblock);
767         if (r)
768                 return r;
769
770         disk_super = dm_block_data(sblock);
771         disk_super->time = cpu_to_le32(pmd->time);
772         disk_super->data_mapping_root = cpu_to_le64(pmd->root);
773         disk_super->device_details_root = cpu_to_le64(pmd->details_root);
774         disk_super->trans_id = cpu_to_le64(pmd->trans_id);
775         disk_super->flags = cpu_to_le32(pmd->flags);
776
777         r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root,
778                             metadata_len);
779         if (r < 0)
780                 goto out_locked;
781
782         r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root,
783                             data_len);
784         if (r < 0)
785                 goto out_locked;
786
787         return dm_tm_commit(pmd->tm, sblock);
788
789 out_locked:
790         dm_bm_unlock(sblock);
791         return r;
792 }
793
794 struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
795                                                sector_t data_block_size,
796                                                bool format_device)
797 {
798         int r;
799         struct dm_pool_metadata *pmd;
800
801         pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
802         if (!pmd) {
803                 DMERR("could not allocate metadata struct");
804                 return ERR_PTR(-ENOMEM);
805         }
806
807         init_rwsem(&pmd->root_lock);
808         pmd->time = 0;
809         INIT_LIST_HEAD(&pmd->thin_devices);
810         pmd->bdev = bdev;
811         pmd->data_block_size = data_block_size;
812
813         r = __create_persistent_data_objects(pmd, format_device);
814         if (r) {
815                 kfree(pmd);
816                 return ERR_PTR(r);
817         }
818
819         r = __begin_transaction(pmd);
820         if (r < 0) {
821                 if (dm_pool_metadata_close(pmd) < 0)
822                         DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
823                 return ERR_PTR(r);
824         }
825
826         return pmd;
827 }
828
829 int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
830 {
831         int r;
832         unsigned open_devices = 0;
833         struct dm_thin_device *td, *tmp;
834
835         down_read(&pmd->root_lock);
836         list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
837                 if (td->open_count)
838                         open_devices++;
839                 else {
840                         list_del(&td->list);
841                         kfree(td);
842                 }
843         }
844         up_read(&pmd->root_lock);
845
846         if (open_devices) {
847                 DMERR("attempt to close pmd when %u device(s) are still open",
848                        open_devices);
849                 return -EBUSY;
850         }
851
852         r = __commit_transaction(pmd);
853         if (r < 0)
854                 DMWARN("%s: __commit_transaction() failed, error = %d",
855                        __func__, r);
856
857         __destroy_persistent_data_objects(pmd);
858         kfree(pmd);
859
860         return 0;
861 }
862
863 /*
864  * __open_device: Returns @td corresponding to device with id @dev,
865  * creating it if @create is set and incrementing @td->open_count.
866  * On failure, @td is undefined.
867  */
868 static int __open_device(struct dm_pool_metadata *pmd,
869                          dm_thin_id dev, int create,
870                          struct dm_thin_device **td)
871 {
872         int r, changed = 0;
873         struct dm_thin_device *td2;
874         uint64_t key = dev;
875         struct disk_device_details details_le;
876
877         /*
878          * If the device is already open, return it.
879          */
880         list_for_each_entry(td2, &pmd->thin_devices, list)
881                 if (td2->id == dev) {
882                         /*
883                          * May not create an already-open device.
884                          */
885                         if (create)
886                                 return -EEXIST;
887
888                         td2->open_count++;
889                         *td = td2;
890                         return 0;
891                 }
892
893         /*
894          * Check the device exists.
895          */
896         r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
897                             &key, &details_le);
898         if (r) {
899                 if (r != -ENODATA || !create)
900                         return r;
901
902                 /*
903                  * Create new device.
904                  */
905                 changed = 1;
906                 details_le.mapped_blocks = 0;
907                 details_le.transaction_id = cpu_to_le64(pmd->trans_id);
908                 details_le.creation_time = cpu_to_le32(pmd->time);
909                 details_le.snapshotted_time = cpu_to_le32(pmd->time);
910         }
911
912         *td = kmalloc(sizeof(**td), GFP_NOIO);
913         if (!*td)
914                 return -ENOMEM;
915
916         (*td)->pmd = pmd;
917         (*td)->id = dev;
918         (*td)->open_count = 1;
919         (*td)->changed = changed;
920         (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
921         (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
922         (*td)->creation_time = le32_to_cpu(details_le.creation_time);
923         (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
924
925         list_add(&(*td)->list, &pmd->thin_devices);
926
927         return 0;
928 }
929
930 static void __close_device(struct dm_thin_device *td)
931 {
932         --td->open_count;
933 }
934
935 static int __create_thin(struct dm_pool_metadata *pmd,
936                          dm_thin_id dev)
937 {
938         int r;
939         dm_block_t dev_root;
940         uint64_t key = dev;
941         struct disk_device_details details_le;
942         struct dm_thin_device *td;
943         __le64 value;
944
945         r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
946                             &key, &details_le);
947         if (!r)
948                 return -EEXIST;
949
950         /*
951          * Create an empty btree for the mappings.
952          */
953         r = dm_btree_empty(&pmd->bl_info, &dev_root);
954         if (r)
955                 return r;
956
957         /*
958          * Insert it into the main mapping tree.
959          */
960         value = cpu_to_le64(dev_root);
961         __dm_bless_for_disk(&value);
962         r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
963         if (r) {
964                 dm_btree_del(&pmd->bl_info, dev_root);
965                 return r;
966         }
967
968         r = __open_device(pmd, dev, 1, &td);
969         if (r) {
970                 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
971                 dm_btree_del(&pmd->bl_info, dev_root);
972                 return r;
973         }
974         __close_device(td);
975
976         return r;
977 }
978
979 int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
980 {
981         int r;
982
983         down_write(&pmd->root_lock);
984         r = __create_thin(pmd, dev);
985         up_write(&pmd->root_lock);
986
987         return r;
988 }
989
990 static int __set_snapshot_details(struct dm_pool_metadata *pmd,
991                                   struct dm_thin_device *snap,
992                                   dm_thin_id origin, uint32_t time)
993 {
994         int r;
995         struct dm_thin_device *td;
996
997         r = __open_device(pmd, origin, 0, &td);
998         if (r)
999                 return r;
1000
1001         td->changed = 1;
1002         td->snapshotted_time = time;
1003
1004         snap->mapped_blocks = td->mapped_blocks;
1005         snap->snapshotted_time = time;
1006         __close_device(td);
1007
1008         return 0;
1009 }
1010
1011 static int __create_snap(struct dm_pool_metadata *pmd,
1012                          dm_thin_id dev, dm_thin_id origin)
1013 {
1014         int r;
1015         dm_block_t origin_root;
1016         uint64_t key = origin, dev_key = dev;
1017         struct dm_thin_device *td;
1018         struct disk_device_details details_le;
1019         __le64 value;
1020
1021         /* check this device is unused */
1022         r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
1023                             &dev_key, &details_le);
1024         if (!r)
1025                 return -EEXIST;
1026
1027         /* find the mapping tree for the origin */
1028         r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
1029         if (r)
1030                 return r;
1031         origin_root = le64_to_cpu(value);
1032
1033         /* clone the origin, an inc will do */
1034         dm_tm_inc(pmd->tm, origin_root);
1035
1036         /* insert into the main mapping tree */
1037         value = cpu_to_le64(origin_root);
1038         __dm_bless_for_disk(&value);
1039         key = dev;
1040         r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
1041         if (r) {
1042                 dm_tm_dec(pmd->tm, origin_root);
1043                 return r;
1044         }
1045
1046         pmd->time++;
1047
1048         r = __open_device(pmd, dev, 1, &td);
1049         if (r)
1050                 goto bad;
1051
1052         r = __set_snapshot_details(pmd, td, origin, pmd->time);
1053         __close_device(td);
1054
1055         if (r)
1056                 goto bad;
1057
1058         return 0;
1059
1060 bad:
1061         dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1062         dm_btree_remove(&pmd->details_info, pmd->details_root,
1063                         &key, &pmd->details_root);
1064         return r;
1065 }
1066
1067 int dm_pool_create_snap(struct dm_pool_metadata *pmd,
1068                                  dm_thin_id dev,
1069                                  dm_thin_id origin)
1070 {
1071         int r;
1072
1073         down_write(&pmd->root_lock);
1074         r = __create_snap(pmd, dev, origin);
1075         up_write(&pmd->root_lock);
1076
1077         return r;
1078 }
1079
1080 static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
1081 {
1082         int r;
1083         uint64_t key = dev;
1084         struct dm_thin_device *td;
1085
1086         /* TODO: failure should mark the transaction invalid */
1087         r = __open_device(pmd, dev, 0, &td);
1088         if (r)
1089                 return r;
1090
1091         if (td->open_count > 1) {
1092                 __close_device(td);
1093                 return -EBUSY;
1094         }
1095
1096         list_del(&td->list);
1097         kfree(td);
1098         r = dm_btree_remove(&pmd->details_info, pmd->details_root,
1099                             &key, &pmd->details_root);
1100         if (r)
1101                 return r;
1102
1103         r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1104         if (r)
1105                 return r;
1106
1107         return 0;
1108 }
1109
1110 int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
1111                                dm_thin_id dev)
1112 {
1113         int r;
1114
1115         down_write(&pmd->root_lock);
1116         r = __delete_device(pmd, dev);
1117         up_write(&pmd->root_lock);
1118
1119         return r;
1120 }
1121
1122 int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
1123                                         uint64_t current_id,
1124                                         uint64_t new_id)
1125 {
1126         down_write(&pmd->root_lock);
1127         if (pmd->trans_id != current_id) {
1128                 up_write(&pmd->root_lock);
1129                 DMERR("mismatched transaction id");
1130                 return -EINVAL;
1131         }
1132
1133         pmd->trans_id = new_id;
1134         up_write(&pmd->root_lock);
1135
1136         return 0;
1137 }
1138
1139 int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
1140                                         uint64_t *result)
1141 {
1142         down_read(&pmd->root_lock);
1143         *result = pmd->trans_id;
1144         up_read(&pmd->root_lock);
1145
1146         return 0;
1147 }
1148
1149 static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
1150 {
1151         int r, inc;
1152         struct thin_disk_superblock *disk_super;
1153         struct dm_block *copy, *sblock;
1154         dm_block_t held_root;
1155
1156         /*
1157          * Copy the superblock.
1158          */
1159         dm_sm_inc_block(pmd->metadata_sm, THIN_SUPERBLOCK_LOCATION);
1160         r = dm_tm_shadow_block(pmd->tm, THIN_SUPERBLOCK_LOCATION,
1161                                &sb_validator, &copy, &inc);
1162         if (r)
1163                 return r;
1164
1165         BUG_ON(!inc);
1166
1167         held_root = dm_block_location(copy);
1168         disk_super = dm_block_data(copy);
1169
1170         if (le64_to_cpu(disk_super->held_root)) {
1171                 DMWARN("Pool metadata snapshot already exists: release this before taking another.");
1172
1173                 dm_tm_dec(pmd->tm, held_root);
1174                 dm_tm_unlock(pmd->tm, copy);
1175                 return -EBUSY;
1176         }
1177
1178         /*
1179          * Wipe the spacemap since we're not publishing this.
1180          */
1181         memset(&disk_super->data_space_map_root, 0,
1182                sizeof(disk_super->data_space_map_root));
1183         memset(&disk_super->metadata_space_map_root, 0,
1184                sizeof(disk_super->metadata_space_map_root));
1185
1186         /*
1187          * Increment the data structures that need to be preserved.
1188          */
1189         dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->data_mapping_root));
1190         dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->device_details_root));
1191         dm_tm_unlock(pmd->tm, copy);
1192
1193         /*
1194          * Write the held root into the superblock.
1195          */
1196         r = superblock_lock(pmd, &sblock);
1197         if (r) {
1198                 dm_tm_dec(pmd->tm, held_root);
1199                 return r;
1200         }
1201
1202         disk_super = dm_block_data(sblock);
1203         disk_super->held_root = cpu_to_le64(held_root);
1204         dm_bm_unlock(sblock);
1205         return 0;
1206 }
1207
1208 int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd)
1209 {
1210         int r;
1211
1212         down_write(&pmd->root_lock);
1213         r = __reserve_metadata_snap(pmd);
1214         up_write(&pmd->root_lock);
1215
1216         return r;
1217 }
1218
1219 static int __release_metadata_snap(struct dm_pool_metadata *pmd)
1220 {
1221         int r;
1222         struct thin_disk_superblock *disk_super;
1223         struct dm_block *sblock, *copy;
1224         dm_block_t held_root;
1225
1226         r = superblock_lock(pmd, &sblock);
1227         if (r)
1228                 return r;
1229
1230         disk_super = dm_block_data(sblock);
1231         held_root = le64_to_cpu(disk_super->held_root);
1232         disk_super->held_root = cpu_to_le64(0);
1233
1234         dm_bm_unlock(sblock);
1235
1236         if (!held_root) {
1237                 DMWARN("No pool metadata snapshot found: nothing to release.");
1238                 return -EINVAL;
1239         }
1240
1241         r = dm_tm_read_lock(pmd->tm, held_root, &sb_validator, &copy);
1242         if (r)
1243                 return r;
1244
1245         disk_super = dm_block_data(copy);
1246         dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->data_mapping_root));
1247         dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->device_details_root));
1248         dm_sm_dec_block(pmd->metadata_sm, held_root);
1249
1250         return dm_tm_unlock(pmd->tm, copy);
1251 }
1252
1253 int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd)
1254 {
1255         int r;
1256
1257         down_write(&pmd->root_lock);
1258         r = __release_metadata_snap(pmd);
1259         up_write(&pmd->root_lock);
1260
1261         return r;
1262 }
1263
1264 static int __get_metadata_snap(struct dm_pool_metadata *pmd,
1265                                dm_block_t *result)
1266 {
1267         int r;
1268         struct thin_disk_superblock *disk_super;
1269         struct dm_block *sblock;
1270
1271         r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
1272                             &sb_validator, &sblock);
1273         if (r)
1274                 return r;
1275
1276         disk_super = dm_block_data(sblock);
1277         *result = le64_to_cpu(disk_super->held_root);
1278
1279         return dm_bm_unlock(sblock);
1280 }
1281
1282 int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
1283                               dm_block_t *result)
1284 {
1285         int r;
1286
1287         down_read(&pmd->root_lock);
1288         r = __get_metadata_snap(pmd, result);
1289         up_read(&pmd->root_lock);
1290
1291         return r;
1292 }
1293
1294 int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
1295                              struct dm_thin_device **td)
1296 {
1297         int r;
1298
1299         down_write(&pmd->root_lock);
1300         r = __open_device(pmd, dev, 0, td);
1301         up_write(&pmd->root_lock);
1302
1303         return r;
1304 }
1305
1306 int dm_pool_close_thin_device(struct dm_thin_device *td)
1307 {
1308         down_write(&td->pmd->root_lock);
1309         __close_device(td);
1310         up_write(&td->pmd->root_lock);
1311
1312         return 0;
1313 }
1314
1315 dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
1316 {
1317         return td->id;
1318 }
1319
1320 static bool __snapshotted_since(struct dm_thin_device *td, uint32_t time)
1321 {
1322         return td->snapshotted_time > time;
1323 }
1324
1325 int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
1326                        int can_block, struct dm_thin_lookup_result *result)
1327 {
1328         int r;
1329         uint64_t block_time = 0;
1330         __le64 value;
1331         struct dm_pool_metadata *pmd = td->pmd;
1332         dm_block_t keys[2] = { td->id, block };
1333
1334         if (can_block) {
1335                 down_read(&pmd->root_lock);
1336                 r = dm_btree_lookup(&pmd->info, pmd->root, keys, &value);
1337                 if (!r)
1338                         block_time = le64_to_cpu(value);
1339                 up_read(&pmd->root_lock);
1340
1341         } else if (down_read_trylock(&pmd->root_lock)) {
1342                 r = dm_btree_lookup(&pmd->nb_info, pmd->root, keys, &value);
1343                 if (!r)
1344                         block_time = le64_to_cpu(value);
1345                 up_read(&pmd->root_lock);
1346
1347         } else
1348                 return -EWOULDBLOCK;
1349
1350         if (!r) {
1351                 dm_block_t exception_block;
1352                 uint32_t exception_time;
1353                 unpack_block_time(block_time, &exception_block,
1354                                   &exception_time);
1355                 result->block = exception_block;
1356                 result->shared = __snapshotted_since(td, exception_time);
1357         }
1358
1359         return r;
1360 }
1361
1362 static int __insert(struct dm_thin_device *td, dm_block_t block,
1363                     dm_block_t data_block)
1364 {
1365         int r, inserted;
1366         __le64 value;
1367         struct dm_pool_metadata *pmd = td->pmd;
1368         dm_block_t keys[2] = { td->id, block };
1369
1370         value = cpu_to_le64(pack_block_time(data_block, pmd->time));
1371         __dm_bless_for_disk(&value);
1372
1373         r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
1374                                    &pmd->root, &inserted);
1375         if (r)
1376                 return r;
1377
1378         if (inserted) {
1379                 td->mapped_blocks++;
1380                 td->changed = 1;
1381         }
1382
1383         return 0;
1384 }
1385
1386 int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
1387                          dm_block_t data_block)
1388 {
1389         int r;
1390
1391         down_write(&td->pmd->root_lock);
1392         r = __insert(td, block, data_block);
1393         up_write(&td->pmd->root_lock);
1394
1395         return r;
1396 }
1397
1398 static int __remove(struct dm_thin_device *td, dm_block_t block)
1399 {
1400         int r;
1401         struct dm_pool_metadata *pmd = td->pmd;
1402         dm_block_t keys[2] = { td->id, block };
1403
1404         r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
1405         if (r)
1406                 return r;
1407
1408         td->mapped_blocks--;
1409         td->changed = 1;
1410
1411         return 0;
1412 }
1413
1414 int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
1415 {
1416         int r;
1417
1418         down_write(&td->pmd->root_lock);
1419         r = __remove(td, block);
1420         up_write(&td->pmd->root_lock);
1421
1422         return r;
1423 }
1424
1425 int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
1426 {
1427         int r;
1428
1429         down_write(&pmd->root_lock);
1430         r = dm_sm_new_block(pmd->data_sm, result);
1431         up_write(&pmd->root_lock);
1432
1433         return r;
1434 }
1435
1436 int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
1437 {
1438         int r;
1439
1440         down_write(&pmd->root_lock);
1441
1442         r = __commit_transaction(pmd);
1443         if (r <= 0)
1444                 goto out;
1445
1446         /*
1447          * Open the next transaction.
1448          */
1449         r = __begin_transaction(pmd);
1450 out:
1451         up_write(&pmd->root_lock);
1452         return r;
1453 }
1454
1455 int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
1456 {
1457         int r;
1458
1459         down_read(&pmd->root_lock);
1460         r = dm_sm_get_nr_free(pmd->data_sm, result);
1461         up_read(&pmd->root_lock);
1462
1463         return r;
1464 }
1465
1466 int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
1467                                           dm_block_t *result)
1468 {
1469         int r;
1470
1471         down_read(&pmd->root_lock);
1472         r = dm_sm_get_nr_free(pmd->metadata_sm, result);
1473         up_read(&pmd->root_lock);
1474
1475         return r;
1476 }
1477
1478 int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
1479                                   dm_block_t *result)
1480 {
1481         int r;
1482
1483         down_read(&pmd->root_lock);
1484         r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
1485         up_read(&pmd->root_lock);
1486
1487         return r;
1488 }
1489
1490 int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result)
1491 {
1492         down_read(&pmd->root_lock);
1493         *result = pmd->data_block_size;
1494         up_read(&pmd->root_lock);
1495
1496         return 0;
1497 }
1498
1499 int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
1500 {
1501         int r;
1502
1503         down_read(&pmd->root_lock);
1504         r = dm_sm_get_nr_blocks(pmd->data_sm, result);
1505         up_read(&pmd->root_lock);
1506
1507         return r;
1508 }
1509
1510 int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
1511 {
1512         struct dm_pool_metadata *pmd = td->pmd;
1513
1514         down_read(&pmd->root_lock);
1515         *result = td->mapped_blocks;
1516         up_read(&pmd->root_lock);
1517
1518         return 0;
1519 }
1520
1521 static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
1522 {
1523         int r;
1524         __le64 value_le;
1525         dm_block_t thin_root;
1526         struct dm_pool_metadata *pmd = td->pmd;
1527
1528         r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
1529         if (r)
1530                 return r;
1531
1532         thin_root = le64_to_cpu(value_le);
1533
1534         return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
1535 }
1536
1537 int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
1538                                      dm_block_t *result)
1539 {
1540         int r;
1541         struct dm_pool_metadata *pmd = td->pmd;
1542
1543         down_read(&pmd->root_lock);
1544         r = __highest_block(td, result);
1545         up_read(&pmd->root_lock);
1546
1547         return r;
1548 }
1549
1550 static int __resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1551 {
1552         int r;
1553         dm_block_t old_count;
1554
1555         r = dm_sm_get_nr_blocks(pmd->data_sm, &old_count);
1556         if (r)
1557                 return r;
1558
1559         if (new_count == old_count)
1560                 return 0;
1561
1562         if (new_count < old_count) {
1563                 DMERR("cannot reduce size of data device");
1564                 return -EINVAL;
1565         }
1566
1567         return dm_sm_extend(pmd->data_sm, new_count - old_count);
1568 }
1569
1570 int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1571 {
1572         int r;
1573
1574         down_write(&pmd->root_lock);
1575         r = __resize_data_dev(pmd, new_count);
1576         up_write(&pmd->root_lock);
1577
1578         return r;
1579 }