3b1503dc1f13e4eeb309c4bf1c6437f4684be325
[firefly-linux-kernel-4.4.55.git] / drivers / md / dm-thin-metadata.c
1 /*
2  * Copyright (C) 2011-2012 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         bool read_only:1;
188
189         /*
190          * Set if a transaction has to be aborted but the attempt to roll back
191          * to the previous (good) transaction failed.  The only pool metadata
192          * operation possible in this state is the closing of the device.
193          */
194         bool fail_io:1;
195 };
196
197 struct dm_thin_device {
198         struct list_head list;
199         struct dm_pool_metadata *pmd;
200         dm_thin_id id;
201
202         int open_count;
203         bool changed:1;
204         bool aborted_with_changes:1;
205         uint64_t mapped_blocks;
206         uint64_t transaction_id;
207         uint32_t creation_time;
208         uint32_t snapshotted_time;
209 };
210
211 /*----------------------------------------------------------------
212  * superblock validator
213  *--------------------------------------------------------------*/
214
215 #define SUPERBLOCK_CSUM_XOR 160774
216
217 static void sb_prepare_for_write(struct dm_block_validator *v,
218                                  struct dm_block *b,
219                                  size_t block_size)
220 {
221         struct thin_disk_superblock *disk_super = dm_block_data(b);
222
223         disk_super->blocknr = cpu_to_le64(dm_block_location(b));
224         disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
225                                                       block_size - sizeof(__le32),
226                                                       SUPERBLOCK_CSUM_XOR));
227 }
228
229 static int sb_check(struct dm_block_validator *v,
230                     struct dm_block *b,
231                     size_t block_size)
232 {
233         struct thin_disk_superblock *disk_super = dm_block_data(b);
234         __le32 csum_le;
235
236         if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
237                 DMERR("sb_check failed: blocknr %llu: "
238                       "wanted %llu", le64_to_cpu(disk_super->blocknr),
239                       (unsigned long long)dm_block_location(b));
240                 return -ENOTBLK;
241         }
242
243         if (le64_to_cpu(disk_super->magic) != THIN_SUPERBLOCK_MAGIC) {
244                 DMERR("sb_check failed: magic %llu: "
245                       "wanted %llu", le64_to_cpu(disk_super->magic),
246                       (unsigned long long)THIN_SUPERBLOCK_MAGIC);
247                 return -EILSEQ;
248         }
249
250         csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
251                                              block_size - sizeof(__le32),
252                                              SUPERBLOCK_CSUM_XOR));
253         if (csum_le != disk_super->csum) {
254                 DMERR("sb_check failed: csum %u: wanted %u",
255                       le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
256                 return -EILSEQ;
257         }
258
259         return 0;
260 }
261
262 static struct dm_block_validator sb_validator = {
263         .name = "superblock",
264         .prepare_for_write = sb_prepare_for_write,
265         .check = sb_check
266 };
267
268 /*----------------------------------------------------------------
269  * Methods for the btree value types
270  *--------------------------------------------------------------*/
271
272 static uint64_t pack_block_time(dm_block_t b, uint32_t t)
273 {
274         return (b << 24) | t;
275 }
276
277 static void unpack_block_time(uint64_t v, dm_block_t *b, uint32_t *t)
278 {
279         *b = v >> 24;
280         *t = v & ((1 << 24) - 1);
281 }
282
283 static void data_block_inc(void *context, const void *value_le)
284 {
285         struct dm_space_map *sm = context;
286         __le64 v_le;
287         uint64_t b;
288         uint32_t t;
289
290         memcpy(&v_le, value_le, sizeof(v_le));
291         unpack_block_time(le64_to_cpu(v_le), &b, &t);
292         dm_sm_inc_block(sm, b);
293 }
294
295 static void data_block_dec(void *context, const void *value_le)
296 {
297         struct dm_space_map *sm = context;
298         __le64 v_le;
299         uint64_t b;
300         uint32_t t;
301
302         memcpy(&v_le, value_le, sizeof(v_le));
303         unpack_block_time(le64_to_cpu(v_le), &b, &t);
304         dm_sm_dec_block(sm, b);
305 }
306
307 static int data_block_equal(void *context, const void *value1_le, const void *value2_le)
308 {
309         __le64 v1_le, v2_le;
310         uint64_t b1, b2;
311         uint32_t t;
312
313         memcpy(&v1_le, value1_le, sizeof(v1_le));
314         memcpy(&v2_le, value2_le, sizeof(v2_le));
315         unpack_block_time(le64_to_cpu(v1_le), &b1, &t);
316         unpack_block_time(le64_to_cpu(v2_le), &b2, &t);
317
318         return b1 == b2;
319 }
320
321 static void subtree_inc(void *context, const void *value)
322 {
323         struct dm_btree_info *info = context;
324         __le64 root_le;
325         uint64_t root;
326
327         memcpy(&root_le, value, sizeof(root_le));
328         root = le64_to_cpu(root_le);
329         dm_tm_inc(info->tm, root);
330 }
331
332 static void subtree_dec(void *context, const void *value)
333 {
334         struct dm_btree_info *info = context;
335         __le64 root_le;
336         uint64_t root;
337
338         memcpy(&root_le, value, sizeof(root_le));
339         root = le64_to_cpu(root_le);
340         if (dm_btree_del(info, root))
341                 DMERR("btree delete failed\n");
342 }
343
344 static int subtree_equal(void *context, const void *value1_le, const void *value2_le)
345 {
346         __le64 v1_le, v2_le;
347         memcpy(&v1_le, value1_le, sizeof(v1_le));
348         memcpy(&v2_le, value2_le, sizeof(v2_le));
349
350         return v1_le == v2_le;
351 }
352
353 /*----------------------------------------------------------------*/
354
355 static int superblock_lock_zero(struct dm_pool_metadata *pmd,
356                                 struct dm_block **sblock)
357 {
358         return dm_bm_write_lock_zero(pmd->bm, THIN_SUPERBLOCK_LOCATION,
359                                      &sb_validator, sblock);
360 }
361
362 static int superblock_lock(struct dm_pool_metadata *pmd,
363                            struct dm_block **sblock)
364 {
365         return dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
366                                 &sb_validator, sblock);
367 }
368
369 static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result)
370 {
371         int r;
372         unsigned i;
373         struct dm_block *b;
374         __le64 *data_le, zero = cpu_to_le64(0);
375         unsigned block_size = dm_bm_block_size(bm) / sizeof(__le64);
376
377         /*
378          * We can't use a validator here - it may be all zeroes.
379          */
380         r = dm_bm_read_lock(bm, THIN_SUPERBLOCK_LOCATION, NULL, &b);
381         if (r)
382                 return r;
383
384         data_le = dm_block_data(b);
385         *result = 1;
386         for (i = 0; i < block_size; i++) {
387                 if (data_le[i] != zero) {
388                         *result = 0;
389                         break;
390                 }
391         }
392
393         return dm_bm_unlock(b);
394 }
395
396 static void __setup_btree_details(struct dm_pool_metadata *pmd)
397 {
398         pmd->info.tm = pmd->tm;
399         pmd->info.levels = 2;
400         pmd->info.value_type.context = pmd->data_sm;
401         pmd->info.value_type.size = sizeof(__le64);
402         pmd->info.value_type.inc = data_block_inc;
403         pmd->info.value_type.dec = data_block_dec;
404         pmd->info.value_type.equal = data_block_equal;
405
406         memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info));
407         pmd->nb_info.tm = pmd->nb_tm;
408
409         pmd->tl_info.tm = pmd->tm;
410         pmd->tl_info.levels = 1;
411         pmd->tl_info.value_type.context = &pmd->bl_info;
412         pmd->tl_info.value_type.size = sizeof(__le64);
413         pmd->tl_info.value_type.inc = subtree_inc;
414         pmd->tl_info.value_type.dec = subtree_dec;
415         pmd->tl_info.value_type.equal = subtree_equal;
416
417         pmd->bl_info.tm = pmd->tm;
418         pmd->bl_info.levels = 1;
419         pmd->bl_info.value_type.context = pmd->data_sm;
420         pmd->bl_info.value_type.size = sizeof(__le64);
421         pmd->bl_info.value_type.inc = data_block_inc;
422         pmd->bl_info.value_type.dec = data_block_dec;
423         pmd->bl_info.value_type.equal = data_block_equal;
424
425         pmd->details_info.tm = pmd->tm;
426         pmd->details_info.levels = 1;
427         pmd->details_info.value_type.context = NULL;
428         pmd->details_info.value_type.size = sizeof(struct disk_device_details);
429         pmd->details_info.value_type.inc = NULL;
430         pmd->details_info.value_type.dec = NULL;
431         pmd->details_info.value_type.equal = NULL;
432 }
433
434 static int __write_initial_superblock(struct dm_pool_metadata *pmd)
435 {
436         int r;
437         struct dm_block *sblock;
438         size_t metadata_len, data_len;
439         struct thin_disk_superblock *disk_super;
440         sector_t bdev_size = i_size_read(pmd->bdev->bd_inode) >> SECTOR_SHIFT;
441
442         if (bdev_size > THIN_METADATA_MAX_SECTORS)
443                 bdev_size = THIN_METADATA_MAX_SECTORS;
444
445         r = dm_sm_root_size(pmd->metadata_sm, &metadata_len);
446         if (r < 0)
447                 return r;
448
449         r = dm_sm_root_size(pmd->data_sm, &data_len);
450         if (r < 0)
451                 return r;
452
453         r = dm_sm_commit(pmd->data_sm);
454         if (r < 0)
455                 return r;
456
457         r = dm_tm_pre_commit(pmd->tm);
458         if (r < 0)
459                 return r;
460
461         r = superblock_lock_zero(pmd, &sblock);
462         if (r)
463                 return r;
464
465         disk_super = dm_block_data(sblock);
466         disk_super->flags = 0;
467         memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
468         disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
469         disk_super->version = cpu_to_le32(THIN_VERSION);
470         disk_super->time = 0;
471         disk_super->trans_id = 0;
472         disk_super->held_root = 0;
473
474         r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root,
475                             metadata_len);
476         if (r < 0)
477                 goto bad_locked;
478
479         r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root,
480                             data_len);
481         if (r < 0)
482                 goto bad_locked;
483
484         disk_super->data_mapping_root = cpu_to_le64(pmd->root);
485         disk_super->device_details_root = cpu_to_le64(pmd->details_root);
486         disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
487         disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
488         disk_super->data_block_size = cpu_to_le32(pmd->data_block_size);
489
490         return dm_tm_commit(pmd->tm, sblock);
491
492 bad_locked:
493         dm_bm_unlock(sblock);
494         return r;
495 }
496
497 static int __format_metadata(struct dm_pool_metadata *pmd)
498 {
499         int r;
500
501         r = dm_tm_create_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
502                                  &pmd->tm, &pmd->metadata_sm);
503         if (r < 0) {
504                 DMERR("tm_create_with_sm failed");
505                 return r;
506         }
507
508         pmd->data_sm = dm_sm_disk_create(pmd->tm, 0);
509         if (IS_ERR(pmd->data_sm)) {
510                 DMERR("sm_disk_create failed");
511                 r = PTR_ERR(pmd->data_sm);
512                 goto bad_cleanup_tm;
513         }
514
515         pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
516         if (!pmd->nb_tm) {
517                 DMERR("could not create non-blocking clone tm");
518                 r = -ENOMEM;
519                 goto bad_cleanup_data_sm;
520         }
521
522         __setup_btree_details(pmd);
523
524         r = dm_btree_empty(&pmd->info, &pmd->root);
525         if (r < 0)
526                 goto bad_cleanup_nb_tm;
527
528         r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
529         if (r < 0) {
530                 DMERR("couldn't create devices root");
531                 goto bad_cleanup_nb_tm;
532         }
533
534         r = __write_initial_superblock(pmd);
535         if (r)
536                 goto bad_cleanup_nb_tm;
537
538         return 0;
539
540 bad_cleanup_nb_tm:
541         dm_tm_destroy(pmd->nb_tm);
542 bad_cleanup_data_sm:
543         dm_sm_destroy(pmd->data_sm);
544 bad_cleanup_tm:
545         dm_tm_destroy(pmd->tm);
546         dm_sm_destroy(pmd->metadata_sm);
547
548         return r;
549 }
550
551 static int __check_incompat_features(struct thin_disk_superblock *disk_super,
552                                      struct dm_pool_metadata *pmd)
553 {
554         uint32_t features;
555
556         features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP;
557         if (features) {
558                 DMERR("could not access metadata due to unsupported optional features (%lx).",
559                       (unsigned long)features);
560                 return -EINVAL;
561         }
562
563         /*
564          * Check for read-only metadata to skip the following RDWR checks.
565          */
566         if (get_disk_ro(pmd->bdev->bd_disk))
567                 return 0;
568
569         features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
570         if (features) {
571                 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
572                       (unsigned long)features);
573                 return -EINVAL;
574         }
575
576         return 0;
577 }
578
579 static int __open_metadata(struct dm_pool_metadata *pmd)
580 {
581         int r;
582         struct dm_block *sblock;
583         struct thin_disk_superblock *disk_super;
584
585         r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
586                             &sb_validator, &sblock);
587         if (r < 0) {
588                 DMERR("couldn't read superblock");
589                 return r;
590         }
591
592         disk_super = dm_block_data(sblock);
593
594         /* Verify the data block size hasn't changed */
595         if (le32_to_cpu(disk_super->data_block_size) != pmd->data_block_size) {
596                 DMERR("changing the data block size (from %u to %llu) is not supported",
597                       le32_to_cpu(disk_super->data_block_size),
598                       (unsigned long long)pmd->data_block_size);
599                 r = -EINVAL;
600                 goto bad_unlock_sblock;
601         }
602
603         r = __check_incompat_features(disk_super, pmd);
604         if (r < 0)
605                 goto bad_unlock_sblock;
606
607         r = dm_tm_open_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
608                                disk_super->metadata_space_map_root,
609                                sizeof(disk_super->metadata_space_map_root),
610                                &pmd->tm, &pmd->metadata_sm);
611         if (r < 0) {
612                 DMERR("tm_open_with_sm failed");
613                 goto bad_unlock_sblock;
614         }
615
616         pmd->data_sm = dm_sm_disk_open(pmd->tm, disk_super->data_space_map_root,
617                                        sizeof(disk_super->data_space_map_root));
618         if (IS_ERR(pmd->data_sm)) {
619                 DMERR("sm_disk_open failed");
620                 r = PTR_ERR(pmd->data_sm);
621                 goto bad_cleanup_tm;
622         }
623
624         pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
625         if (!pmd->nb_tm) {
626                 DMERR("could not create non-blocking clone tm");
627                 r = -ENOMEM;
628                 goto bad_cleanup_data_sm;
629         }
630
631         __setup_btree_details(pmd);
632         return dm_bm_unlock(sblock);
633
634 bad_cleanup_data_sm:
635         dm_sm_destroy(pmd->data_sm);
636 bad_cleanup_tm:
637         dm_tm_destroy(pmd->tm);
638         dm_sm_destroy(pmd->metadata_sm);
639 bad_unlock_sblock:
640         dm_bm_unlock(sblock);
641
642         return r;
643 }
644
645 static int __open_or_format_metadata(struct dm_pool_metadata *pmd, bool format_device)
646 {
647         int r, unformatted;
648
649         r = __superblock_all_zeroes(pmd->bm, &unformatted);
650         if (r)
651                 return r;
652
653         if (unformatted)
654                 return format_device ? __format_metadata(pmd) : -EPERM;
655
656         return __open_metadata(pmd);
657 }
658
659 static int __create_persistent_data_objects(struct dm_pool_metadata *pmd, bool format_device)
660 {
661         int r;
662
663         pmd->bm = dm_block_manager_create(pmd->bdev, THIN_METADATA_BLOCK_SIZE,
664                                           THIN_METADATA_CACHE_SIZE,
665                                           THIN_MAX_CONCURRENT_LOCKS);
666         if (IS_ERR(pmd->bm)) {
667                 DMERR("could not create block manager");
668                 return PTR_ERR(pmd->bm);
669         }
670
671         r = __open_or_format_metadata(pmd, format_device);
672         if (r)
673                 dm_block_manager_destroy(pmd->bm);
674
675         return r;
676 }
677
678 static void __destroy_persistent_data_objects(struct dm_pool_metadata *pmd)
679 {
680         dm_sm_destroy(pmd->data_sm);
681         dm_sm_destroy(pmd->metadata_sm);
682         dm_tm_destroy(pmd->nb_tm);
683         dm_tm_destroy(pmd->tm);
684         dm_block_manager_destroy(pmd->bm);
685 }
686
687 static int __begin_transaction(struct dm_pool_metadata *pmd)
688 {
689         int r;
690         struct thin_disk_superblock *disk_super;
691         struct dm_block *sblock;
692
693         /*
694          * We re-read the superblock every time.  Shouldn't need to do this
695          * really.
696          */
697         r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
698                             &sb_validator, &sblock);
699         if (r)
700                 return r;
701
702         disk_super = dm_block_data(sblock);
703         pmd->time = le32_to_cpu(disk_super->time);
704         pmd->root = le64_to_cpu(disk_super->data_mapping_root);
705         pmd->details_root = le64_to_cpu(disk_super->device_details_root);
706         pmd->trans_id = le64_to_cpu(disk_super->trans_id);
707         pmd->flags = le32_to_cpu(disk_super->flags);
708         pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
709
710         dm_bm_unlock(sblock);
711         return 0;
712 }
713
714 static int __write_changed_details(struct dm_pool_metadata *pmd)
715 {
716         int r;
717         struct dm_thin_device *td, *tmp;
718         struct disk_device_details details;
719         uint64_t key;
720
721         list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
722                 if (!td->changed)
723                         continue;
724
725                 key = td->id;
726
727                 details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
728                 details.transaction_id = cpu_to_le64(td->transaction_id);
729                 details.creation_time = cpu_to_le32(td->creation_time);
730                 details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
731                 __dm_bless_for_disk(&details);
732
733                 r = dm_btree_insert(&pmd->details_info, pmd->details_root,
734                                     &key, &details, &pmd->details_root);
735                 if (r)
736                         return r;
737
738                 if (td->open_count)
739                         td->changed = 0;
740                 else {
741                         list_del(&td->list);
742                         kfree(td);
743                 }
744         }
745
746         return 0;
747 }
748
749 static int __commit_transaction(struct dm_pool_metadata *pmd)
750 {
751         int r;
752         size_t metadata_len, data_len;
753         struct thin_disk_superblock *disk_super;
754         struct dm_block *sblock;
755
756         /*
757          * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
758          */
759         BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
760
761         r = __write_changed_details(pmd);
762         if (r < 0)
763                 return r;
764
765         r = dm_sm_commit(pmd->data_sm);
766         if (r < 0)
767                 return r;
768
769         r = dm_tm_pre_commit(pmd->tm);
770         if (r < 0)
771                 return r;
772
773         r = dm_sm_root_size(pmd->metadata_sm, &metadata_len);
774         if (r < 0)
775                 return r;
776
777         r = dm_sm_root_size(pmd->data_sm, &data_len);
778         if (r < 0)
779                 return r;
780
781         r = superblock_lock(pmd, &sblock);
782         if (r)
783                 return r;
784
785         disk_super = dm_block_data(sblock);
786         disk_super->time = cpu_to_le32(pmd->time);
787         disk_super->data_mapping_root = cpu_to_le64(pmd->root);
788         disk_super->device_details_root = cpu_to_le64(pmd->details_root);
789         disk_super->trans_id = cpu_to_le64(pmd->trans_id);
790         disk_super->flags = cpu_to_le32(pmd->flags);
791
792         r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root,
793                             metadata_len);
794         if (r < 0)
795                 goto out_locked;
796
797         r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root,
798                             data_len);
799         if (r < 0)
800                 goto out_locked;
801
802         return dm_tm_commit(pmd->tm, sblock);
803
804 out_locked:
805         dm_bm_unlock(sblock);
806         return r;
807 }
808
809 struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
810                                                sector_t data_block_size,
811                                                bool format_device)
812 {
813         int r;
814         struct dm_pool_metadata *pmd;
815
816         pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
817         if (!pmd) {
818                 DMERR("could not allocate metadata struct");
819                 return ERR_PTR(-ENOMEM);
820         }
821
822         init_rwsem(&pmd->root_lock);
823         pmd->time = 0;
824         INIT_LIST_HEAD(&pmd->thin_devices);
825         pmd->read_only = false;
826         pmd->fail_io = false;
827         pmd->bdev = bdev;
828         pmd->data_block_size = data_block_size;
829
830         r = __create_persistent_data_objects(pmd, format_device);
831         if (r) {
832                 kfree(pmd);
833                 return ERR_PTR(r);
834         }
835
836         r = __begin_transaction(pmd);
837         if (r < 0) {
838                 if (dm_pool_metadata_close(pmd) < 0)
839                         DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
840                 return ERR_PTR(r);
841         }
842
843         return pmd;
844 }
845
846 int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
847 {
848         int r;
849         unsigned open_devices = 0;
850         struct dm_thin_device *td, *tmp;
851
852         down_read(&pmd->root_lock);
853         list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
854                 if (td->open_count)
855                         open_devices++;
856                 else {
857                         list_del(&td->list);
858                         kfree(td);
859                 }
860         }
861         up_read(&pmd->root_lock);
862
863         if (open_devices) {
864                 DMERR("attempt to close pmd when %u device(s) are still open",
865                        open_devices);
866                 return -EBUSY;
867         }
868
869         if (!pmd->read_only && !pmd->fail_io) {
870                 r = __commit_transaction(pmd);
871                 if (r < 0)
872                         DMWARN("%s: __commit_transaction() failed, error = %d",
873                                __func__, r);
874         }
875
876         if (!pmd->fail_io)
877                 __destroy_persistent_data_objects(pmd);
878
879         kfree(pmd);
880         return 0;
881 }
882
883 /*
884  * __open_device: Returns @td corresponding to device with id @dev,
885  * creating it if @create is set and incrementing @td->open_count.
886  * On failure, @td is undefined.
887  */
888 static int __open_device(struct dm_pool_metadata *pmd,
889                          dm_thin_id dev, int create,
890                          struct dm_thin_device **td)
891 {
892         int r, changed = 0;
893         struct dm_thin_device *td2;
894         uint64_t key = dev;
895         struct disk_device_details details_le;
896
897         /*
898          * If the device is already open, return it.
899          */
900         list_for_each_entry(td2, &pmd->thin_devices, list)
901                 if (td2->id == dev) {
902                         /*
903                          * May not create an already-open device.
904                          */
905                         if (create)
906                                 return -EEXIST;
907
908                         td2->open_count++;
909                         *td = td2;
910                         return 0;
911                 }
912
913         /*
914          * Check the device exists.
915          */
916         r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
917                             &key, &details_le);
918         if (r) {
919                 if (r != -ENODATA || !create)
920                         return r;
921
922                 /*
923                  * Create new device.
924                  */
925                 changed = 1;
926                 details_le.mapped_blocks = 0;
927                 details_le.transaction_id = cpu_to_le64(pmd->trans_id);
928                 details_le.creation_time = cpu_to_le32(pmd->time);
929                 details_le.snapshotted_time = cpu_to_le32(pmd->time);
930         }
931
932         *td = kmalloc(sizeof(**td), GFP_NOIO);
933         if (!*td)
934                 return -ENOMEM;
935
936         (*td)->pmd = pmd;
937         (*td)->id = dev;
938         (*td)->open_count = 1;
939         (*td)->changed = changed;
940         (*td)->aborted_with_changes = false;
941         (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
942         (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
943         (*td)->creation_time = le32_to_cpu(details_le.creation_time);
944         (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
945
946         list_add(&(*td)->list, &pmd->thin_devices);
947
948         return 0;
949 }
950
951 static void __close_device(struct dm_thin_device *td)
952 {
953         --td->open_count;
954 }
955
956 static int __create_thin(struct dm_pool_metadata *pmd,
957                          dm_thin_id dev)
958 {
959         int r;
960         dm_block_t dev_root;
961         uint64_t key = dev;
962         struct disk_device_details details_le;
963         struct dm_thin_device *td;
964         __le64 value;
965
966         r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
967                             &key, &details_le);
968         if (!r)
969                 return -EEXIST;
970
971         /*
972          * Create an empty btree for the mappings.
973          */
974         r = dm_btree_empty(&pmd->bl_info, &dev_root);
975         if (r)
976                 return r;
977
978         /*
979          * Insert it into the main mapping tree.
980          */
981         value = cpu_to_le64(dev_root);
982         __dm_bless_for_disk(&value);
983         r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
984         if (r) {
985                 dm_btree_del(&pmd->bl_info, dev_root);
986                 return r;
987         }
988
989         r = __open_device(pmd, dev, 1, &td);
990         if (r) {
991                 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
992                 dm_btree_del(&pmd->bl_info, dev_root);
993                 return r;
994         }
995         __close_device(td);
996
997         return r;
998 }
999
1000 int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
1001 {
1002         int r = -EINVAL;
1003
1004         down_write(&pmd->root_lock);
1005         if (!pmd->fail_io)
1006                 r = __create_thin(pmd, dev);
1007         up_write(&pmd->root_lock);
1008
1009         return r;
1010 }
1011
1012 static int __set_snapshot_details(struct dm_pool_metadata *pmd,
1013                                   struct dm_thin_device *snap,
1014                                   dm_thin_id origin, uint32_t time)
1015 {
1016         int r;
1017         struct dm_thin_device *td;
1018
1019         r = __open_device(pmd, origin, 0, &td);
1020         if (r)
1021                 return r;
1022
1023         td->changed = 1;
1024         td->snapshotted_time = time;
1025
1026         snap->mapped_blocks = td->mapped_blocks;
1027         snap->snapshotted_time = time;
1028         __close_device(td);
1029
1030         return 0;
1031 }
1032
1033 static int __create_snap(struct dm_pool_metadata *pmd,
1034                          dm_thin_id dev, dm_thin_id origin)
1035 {
1036         int r;
1037         dm_block_t origin_root;
1038         uint64_t key = origin, dev_key = dev;
1039         struct dm_thin_device *td;
1040         struct disk_device_details details_le;
1041         __le64 value;
1042
1043         /* check this device is unused */
1044         r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
1045                             &dev_key, &details_le);
1046         if (!r)
1047                 return -EEXIST;
1048
1049         /* find the mapping tree for the origin */
1050         r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
1051         if (r)
1052                 return r;
1053         origin_root = le64_to_cpu(value);
1054
1055         /* clone the origin, an inc will do */
1056         dm_tm_inc(pmd->tm, origin_root);
1057
1058         /* insert into the main mapping tree */
1059         value = cpu_to_le64(origin_root);
1060         __dm_bless_for_disk(&value);
1061         key = dev;
1062         r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
1063         if (r) {
1064                 dm_tm_dec(pmd->tm, origin_root);
1065                 return r;
1066         }
1067
1068         pmd->time++;
1069
1070         r = __open_device(pmd, dev, 1, &td);
1071         if (r)
1072                 goto bad;
1073
1074         r = __set_snapshot_details(pmd, td, origin, pmd->time);
1075         __close_device(td);
1076
1077         if (r)
1078                 goto bad;
1079
1080         return 0;
1081
1082 bad:
1083         dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1084         dm_btree_remove(&pmd->details_info, pmd->details_root,
1085                         &key, &pmd->details_root);
1086         return r;
1087 }
1088
1089 int dm_pool_create_snap(struct dm_pool_metadata *pmd,
1090                                  dm_thin_id dev,
1091                                  dm_thin_id origin)
1092 {
1093         int r = -EINVAL;
1094
1095         down_write(&pmd->root_lock);
1096         if (!pmd->fail_io)
1097                 r = __create_snap(pmd, dev, origin);
1098         up_write(&pmd->root_lock);
1099
1100         return r;
1101 }
1102
1103 static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
1104 {
1105         int r;
1106         uint64_t key = dev;
1107         struct dm_thin_device *td;
1108
1109         /* TODO: failure should mark the transaction invalid */
1110         r = __open_device(pmd, dev, 0, &td);
1111         if (r)
1112                 return r;
1113
1114         if (td->open_count > 1) {
1115                 __close_device(td);
1116                 return -EBUSY;
1117         }
1118
1119         list_del(&td->list);
1120         kfree(td);
1121         r = dm_btree_remove(&pmd->details_info, pmd->details_root,
1122                             &key, &pmd->details_root);
1123         if (r)
1124                 return r;
1125
1126         r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1127         if (r)
1128                 return r;
1129
1130         return 0;
1131 }
1132
1133 int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
1134                                dm_thin_id dev)
1135 {
1136         int r = -EINVAL;
1137
1138         down_write(&pmd->root_lock);
1139         if (!pmd->fail_io)
1140                 r = __delete_device(pmd, dev);
1141         up_write(&pmd->root_lock);
1142
1143         return r;
1144 }
1145
1146 int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
1147                                         uint64_t current_id,
1148                                         uint64_t new_id)
1149 {
1150         int r = -EINVAL;
1151
1152         down_write(&pmd->root_lock);
1153
1154         if (pmd->fail_io)
1155                 goto out;
1156
1157         if (pmd->trans_id != current_id) {
1158                 DMERR("mismatched transaction id");
1159                 goto out;
1160         }
1161
1162         pmd->trans_id = new_id;
1163         r = 0;
1164
1165 out:
1166         up_write(&pmd->root_lock);
1167
1168         return r;
1169 }
1170
1171 int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
1172                                         uint64_t *result)
1173 {
1174         int r = -EINVAL;
1175
1176         down_read(&pmd->root_lock);
1177         if (!pmd->fail_io) {
1178                 *result = pmd->trans_id;
1179                 r = 0;
1180         }
1181         up_read(&pmd->root_lock);
1182
1183         return r;
1184 }
1185
1186 static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
1187 {
1188         int r, inc;
1189         struct thin_disk_superblock *disk_super;
1190         struct dm_block *copy, *sblock;
1191         dm_block_t held_root;
1192
1193         /*
1194          * Copy the superblock.
1195          */
1196         dm_sm_inc_block(pmd->metadata_sm, THIN_SUPERBLOCK_LOCATION);
1197         r = dm_tm_shadow_block(pmd->tm, THIN_SUPERBLOCK_LOCATION,
1198                                &sb_validator, &copy, &inc);
1199         if (r)
1200                 return r;
1201
1202         BUG_ON(!inc);
1203
1204         held_root = dm_block_location(copy);
1205         disk_super = dm_block_data(copy);
1206
1207         if (le64_to_cpu(disk_super->held_root)) {
1208                 DMWARN("Pool metadata snapshot already exists: release this before taking another.");
1209
1210                 dm_tm_dec(pmd->tm, held_root);
1211                 dm_tm_unlock(pmd->tm, copy);
1212                 return -EBUSY;
1213         }
1214
1215         /*
1216          * Wipe the spacemap since we're not publishing this.
1217          */
1218         memset(&disk_super->data_space_map_root, 0,
1219                sizeof(disk_super->data_space_map_root));
1220         memset(&disk_super->metadata_space_map_root, 0,
1221                sizeof(disk_super->metadata_space_map_root));
1222
1223         /*
1224          * Increment the data structures that need to be preserved.
1225          */
1226         dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->data_mapping_root));
1227         dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->device_details_root));
1228         dm_tm_unlock(pmd->tm, copy);
1229
1230         /*
1231          * Write the held root into the superblock.
1232          */
1233         r = superblock_lock(pmd, &sblock);
1234         if (r) {
1235                 dm_tm_dec(pmd->tm, held_root);
1236                 return r;
1237         }
1238
1239         disk_super = dm_block_data(sblock);
1240         disk_super->held_root = cpu_to_le64(held_root);
1241         dm_bm_unlock(sblock);
1242         return 0;
1243 }
1244
1245 int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd)
1246 {
1247         int r = -EINVAL;
1248
1249         down_write(&pmd->root_lock);
1250         if (!pmd->fail_io)
1251                 r = __reserve_metadata_snap(pmd);
1252         up_write(&pmd->root_lock);
1253
1254         return r;
1255 }
1256
1257 static int __release_metadata_snap(struct dm_pool_metadata *pmd)
1258 {
1259         int r;
1260         struct thin_disk_superblock *disk_super;
1261         struct dm_block *sblock, *copy;
1262         dm_block_t held_root;
1263
1264         r = superblock_lock(pmd, &sblock);
1265         if (r)
1266                 return r;
1267
1268         disk_super = dm_block_data(sblock);
1269         held_root = le64_to_cpu(disk_super->held_root);
1270         disk_super->held_root = cpu_to_le64(0);
1271
1272         dm_bm_unlock(sblock);
1273
1274         if (!held_root) {
1275                 DMWARN("No pool metadata snapshot found: nothing to release.");
1276                 return -EINVAL;
1277         }
1278
1279         r = dm_tm_read_lock(pmd->tm, held_root, &sb_validator, &copy);
1280         if (r)
1281                 return r;
1282
1283         disk_super = dm_block_data(copy);
1284         dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->data_mapping_root));
1285         dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->device_details_root));
1286         dm_sm_dec_block(pmd->metadata_sm, held_root);
1287
1288         return dm_tm_unlock(pmd->tm, copy);
1289 }
1290
1291 int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd)
1292 {
1293         int r = -EINVAL;
1294
1295         down_write(&pmd->root_lock);
1296         if (!pmd->fail_io)
1297                 r = __release_metadata_snap(pmd);
1298         up_write(&pmd->root_lock);
1299
1300         return r;
1301 }
1302
1303 static int __get_metadata_snap(struct dm_pool_metadata *pmd,
1304                                dm_block_t *result)
1305 {
1306         int r;
1307         struct thin_disk_superblock *disk_super;
1308         struct dm_block *sblock;
1309
1310         r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
1311                             &sb_validator, &sblock);
1312         if (r)
1313                 return r;
1314
1315         disk_super = dm_block_data(sblock);
1316         *result = le64_to_cpu(disk_super->held_root);
1317
1318         return dm_bm_unlock(sblock);
1319 }
1320
1321 int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
1322                               dm_block_t *result)
1323 {
1324         int r = -EINVAL;
1325
1326         down_read(&pmd->root_lock);
1327         if (!pmd->fail_io)
1328                 r = __get_metadata_snap(pmd, result);
1329         up_read(&pmd->root_lock);
1330
1331         return r;
1332 }
1333
1334 int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
1335                              struct dm_thin_device **td)
1336 {
1337         int r = -EINVAL;
1338
1339         down_write(&pmd->root_lock);
1340         if (!pmd->fail_io)
1341                 r = __open_device(pmd, dev, 0, td);
1342         up_write(&pmd->root_lock);
1343
1344         return r;
1345 }
1346
1347 int dm_pool_close_thin_device(struct dm_thin_device *td)
1348 {
1349         down_write(&td->pmd->root_lock);
1350         __close_device(td);
1351         up_write(&td->pmd->root_lock);
1352
1353         return 0;
1354 }
1355
1356 dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
1357 {
1358         return td->id;
1359 }
1360
1361 /*
1362  * Check whether @time (of block creation) is older than @td's last snapshot.
1363  * If so then the associated block is shared with the last snapshot device.
1364  * Any block on a device created *after* the device last got snapshotted is
1365  * necessarily not shared.
1366  */
1367 static bool __snapshotted_since(struct dm_thin_device *td, uint32_t time)
1368 {
1369         return td->snapshotted_time > time;
1370 }
1371
1372 int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
1373                        int can_block, struct dm_thin_lookup_result *result)
1374 {
1375         int r = -EINVAL;
1376         uint64_t block_time = 0;
1377         __le64 value;
1378         struct dm_pool_metadata *pmd = td->pmd;
1379         dm_block_t keys[2] = { td->id, block };
1380         struct dm_btree_info *info;
1381
1382         if (can_block) {
1383                 down_read(&pmd->root_lock);
1384                 info = &pmd->info;
1385         } else if (down_read_trylock(&pmd->root_lock))
1386                 info = &pmd->nb_info;
1387         else
1388                 return -EWOULDBLOCK;
1389
1390         if (pmd->fail_io)
1391                 goto out;
1392
1393         r = dm_btree_lookup(info, pmd->root, keys, &value);
1394         if (!r)
1395                 block_time = le64_to_cpu(value);
1396
1397 out:
1398         up_read(&pmd->root_lock);
1399
1400         if (!r) {
1401                 dm_block_t exception_block;
1402                 uint32_t exception_time;
1403                 unpack_block_time(block_time, &exception_block,
1404                                   &exception_time);
1405                 result->block = exception_block;
1406                 result->shared = __snapshotted_since(td, exception_time);
1407         }
1408
1409         return r;
1410 }
1411
1412 static int __insert(struct dm_thin_device *td, dm_block_t block,
1413                     dm_block_t data_block)
1414 {
1415         int r, inserted;
1416         __le64 value;
1417         struct dm_pool_metadata *pmd = td->pmd;
1418         dm_block_t keys[2] = { td->id, block };
1419
1420         value = cpu_to_le64(pack_block_time(data_block, pmd->time));
1421         __dm_bless_for_disk(&value);
1422
1423         r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
1424                                    &pmd->root, &inserted);
1425         if (r)
1426                 return r;
1427
1428         td->changed = 1;
1429         if (inserted)
1430                 td->mapped_blocks++;
1431
1432         return 0;
1433 }
1434
1435 int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
1436                          dm_block_t data_block)
1437 {
1438         int r = -EINVAL;
1439
1440         down_write(&td->pmd->root_lock);
1441         if (!td->pmd->fail_io)
1442                 r = __insert(td, block, data_block);
1443         up_write(&td->pmd->root_lock);
1444
1445         return r;
1446 }
1447
1448 static int __remove(struct dm_thin_device *td, dm_block_t block)
1449 {
1450         int r;
1451         struct dm_pool_metadata *pmd = td->pmd;
1452         dm_block_t keys[2] = { td->id, block };
1453
1454         r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
1455         if (r)
1456                 return r;
1457
1458         td->mapped_blocks--;
1459         td->changed = 1;
1460
1461         return 0;
1462 }
1463
1464 int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
1465 {
1466         int r = -EINVAL;
1467
1468         down_write(&td->pmd->root_lock);
1469         if (!td->pmd->fail_io)
1470                 r = __remove(td, block);
1471         up_write(&td->pmd->root_lock);
1472
1473         return r;
1474 }
1475
1476 int dm_pool_block_is_used(struct dm_pool_metadata *pmd, dm_block_t b, bool *result)
1477 {
1478         int r;
1479         uint32_t ref_count;
1480
1481         down_read(&pmd->root_lock);
1482         r = dm_sm_get_count(pmd->data_sm, b, &ref_count);
1483         if (!r)
1484                 *result = (ref_count != 0);
1485         up_read(&pmd->root_lock);
1486
1487         return r;
1488 }
1489
1490 bool dm_thin_changed_this_transaction(struct dm_thin_device *td)
1491 {
1492         int r;
1493
1494         down_read(&td->pmd->root_lock);
1495         r = td->changed;
1496         up_read(&td->pmd->root_lock);
1497
1498         return r;
1499 }
1500
1501 bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd)
1502 {
1503         bool r = false;
1504         struct dm_thin_device *td, *tmp;
1505
1506         down_read(&pmd->root_lock);
1507         list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
1508                 if (td->changed) {
1509                         r = td->changed;
1510                         break;
1511                 }
1512         }
1513         up_read(&pmd->root_lock);
1514
1515         return r;
1516 }
1517
1518 bool dm_thin_aborted_changes(struct dm_thin_device *td)
1519 {
1520         bool r;
1521
1522         down_read(&td->pmd->root_lock);
1523         r = td->aborted_with_changes;
1524         up_read(&td->pmd->root_lock);
1525
1526         return r;
1527 }
1528
1529 int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
1530 {
1531         int r = -EINVAL;
1532
1533         down_write(&pmd->root_lock);
1534         if (!pmd->fail_io)
1535                 r = dm_sm_new_block(pmd->data_sm, result);
1536         up_write(&pmd->root_lock);
1537
1538         return r;
1539 }
1540
1541 int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
1542 {
1543         int r = -EINVAL;
1544
1545         down_write(&pmd->root_lock);
1546         if (pmd->fail_io)
1547                 goto out;
1548
1549         r = __commit_transaction(pmd);
1550         if (r <= 0)
1551                 goto out;
1552
1553         /*
1554          * Open the next transaction.
1555          */
1556         r = __begin_transaction(pmd);
1557 out:
1558         up_write(&pmd->root_lock);
1559         return r;
1560 }
1561
1562 static void __set_abort_with_changes_flags(struct dm_pool_metadata *pmd)
1563 {
1564         struct dm_thin_device *td;
1565
1566         list_for_each_entry(td, &pmd->thin_devices, list)
1567                 td->aborted_with_changes = td->changed;
1568 }
1569
1570 int dm_pool_abort_metadata(struct dm_pool_metadata *pmd)
1571 {
1572         int r = -EINVAL;
1573
1574         down_write(&pmd->root_lock);
1575         if (pmd->fail_io)
1576                 goto out;
1577
1578         __set_abort_with_changes_flags(pmd);
1579         __destroy_persistent_data_objects(pmd);
1580         r = __create_persistent_data_objects(pmd, false);
1581         if (r)
1582                 pmd->fail_io = true;
1583
1584 out:
1585         up_write(&pmd->root_lock);
1586
1587         return r;
1588 }
1589
1590 int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
1591 {
1592         int r = -EINVAL;
1593
1594         down_read(&pmd->root_lock);
1595         if (!pmd->fail_io)
1596                 r = dm_sm_get_nr_free(pmd->data_sm, result);
1597         up_read(&pmd->root_lock);
1598
1599         return r;
1600 }
1601
1602 int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
1603                                           dm_block_t *result)
1604 {
1605         int r = -EINVAL;
1606
1607         down_read(&pmd->root_lock);
1608         if (!pmd->fail_io)
1609                 r = dm_sm_get_nr_free(pmd->metadata_sm, result);
1610         up_read(&pmd->root_lock);
1611
1612         return r;
1613 }
1614
1615 int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
1616                                   dm_block_t *result)
1617 {
1618         int r = -EINVAL;
1619
1620         down_read(&pmd->root_lock);
1621         if (!pmd->fail_io)
1622                 r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
1623         up_read(&pmd->root_lock);
1624
1625         return r;
1626 }
1627
1628 int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result)
1629 {
1630         down_read(&pmd->root_lock);
1631         *result = pmd->data_block_size;
1632         up_read(&pmd->root_lock);
1633
1634         return 0;
1635 }
1636
1637 int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
1638 {
1639         int r = -EINVAL;
1640
1641         down_read(&pmd->root_lock);
1642         if (!pmd->fail_io)
1643                 r = dm_sm_get_nr_blocks(pmd->data_sm, result);
1644         up_read(&pmd->root_lock);
1645
1646         return r;
1647 }
1648
1649 int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
1650 {
1651         int r = -EINVAL;
1652         struct dm_pool_metadata *pmd = td->pmd;
1653
1654         down_read(&pmd->root_lock);
1655         if (!pmd->fail_io) {
1656                 *result = td->mapped_blocks;
1657                 r = 0;
1658         }
1659         up_read(&pmd->root_lock);
1660
1661         return r;
1662 }
1663
1664 static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
1665 {
1666         int r;
1667         __le64 value_le;
1668         dm_block_t thin_root;
1669         struct dm_pool_metadata *pmd = td->pmd;
1670
1671         r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
1672         if (r)
1673                 return r;
1674
1675         thin_root = le64_to_cpu(value_le);
1676
1677         return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
1678 }
1679
1680 int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
1681                                      dm_block_t *result)
1682 {
1683         int r = -EINVAL;
1684         struct dm_pool_metadata *pmd = td->pmd;
1685
1686         down_read(&pmd->root_lock);
1687         if (!pmd->fail_io)
1688                 r = __highest_block(td, result);
1689         up_read(&pmd->root_lock);
1690
1691         return r;
1692 }
1693
1694 static int __resize_space_map(struct dm_space_map *sm, dm_block_t new_count)
1695 {
1696         int r;
1697         dm_block_t old_count;
1698
1699         r = dm_sm_get_nr_blocks(sm, &old_count);
1700         if (r)
1701                 return r;
1702
1703         if (new_count == old_count)
1704                 return 0;
1705
1706         if (new_count < old_count) {
1707                 DMERR("cannot reduce size of space map");
1708                 return -EINVAL;
1709         }
1710
1711         return dm_sm_extend(sm, new_count - old_count);
1712 }
1713
1714 int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1715 {
1716         int r = -EINVAL;
1717
1718         down_write(&pmd->root_lock);
1719         if (!pmd->fail_io)
1720                 r = __resize_space_map(pmd->data_sm, new_count);
1721         up_write(&pmd->root_lock);
1722
1723         return r;
1724 }
1725
1726 int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1727 {
1728         int r = -EINVAL;
1729
1730         down_write(&pmd->root_lock);
1731         if (!pmd->fail_io)
1732                 r = __resize_space_map(pmd->metadata_sm, new_count);
1733         up_write(&pmd->root_lock);
1734
1735         return r;
1736 }
1737
1738 void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd)
1739 {
1740         down_write(&pmd->root_lock);
1741         pmd->read_only = true;
1742         dm_bm_set_read_only(pmd->bm);
1743         up_write(&pmd->root_lock);
1744 }
1745
1746 int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd,
1747                                         dm_block_t threshold,
1748                                         dm_sm_threshold_fn fn,
1749                                         void *context)
1750 {
1751         int r;
1752
1753         down_write(&pmd->root_lock);
1754         r = dm_sm_register_threshold_callback(pmd->metadata_sm, threshold, fn, context);
1755         up_write(&pmd->root_lock);
1756
1757         return r;
1758 }