blob: 6ba47cfb1443748ccf092819a6a5ef160bb856fd [file] [log] [blame]
Joe Thornber991d9fa2011-10-31 20:21:18 +00001/*
Joe Thornberda105ed2012-07-27 15:08:15 +01002 * Copyright (C) 2011-2012 Red Hat, Inc.
Joe Thornber991d9fa2011-10-31 20:21:18 +00003 *
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
Mike Snitzer07f2b6e2014-02-14 11:58:41 -050079#define THIN_VERSION 2
Joe Thornber991d9fa2011-10-31 20:21:18 +000080#define THIN_METADATA_CACHE_SIZE 64
81#define SECTOR_TO_BLOCK_SHIFT 3
82
Joe Thornber8c971172012-07-27 15:07:58 +010083/*
84 * 3 for btree insert +
85 * 2 for btree lookup used within space map
86 */
87#define THIN_MAX_CONCURRENT_LOCKS 5
88
Joe Thornber991d9fa2011-10-31 20:21:18 +000089/* This should be plenty */
90#define SPACE_MAP_ROOT_SIZE 128
91
92/*
93 * Little endian on-disk superblock and device details.
94 */
95struct thin_disk_superblock {
96 __le32 csum; /* Checksum of superblock except for this field. */
97 __le32 flags;
98 __le64 blocknr; /* This block number, dm_block_t. */
99
100 __u8 uuid[16];
101 __le64 magic;
102 __le32 version;
103 __le32 time;
104
105 __le64 trans_id;
106
107 /*
108 * Root held by userspace transactions.
109 */
110 __le64 held_root;
111
112 __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
113 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
114
115 /*
116 * 2-level btree mapping (dev_id, (dev block, time)) -> data block
117 */
118 __le64 data_mapping_root;
119
120 /*
121 * Device detail root mapping dev_id -> device_details
122 */
123 __le64 device_details_root;
124
125 __le32 data_block_size; /* In 512-byte sectors. */
126
127 __le32 metadata_block_size; /* In 512-byte sectors. */
128 __le64 metadata_nr_blocks;
129
130 __le32 compat_flags;
131 __le32 compat_ro_flags;
132 __le32 incompat_flags;
133} __packed;
134
135struct disk_device_details {
136 __le64 mapped_blocks;
137 __le64 transaction_id; /* When created. */
138 __le32 creation_time;
139 __le32 snapshotted_time;
140} __packed;
141
142struct dm_pool_metadata {
143 struct hlist_node hash;
144
145 struct block_device *bdev;
146 struct dm_block_manager *bm;
147 struct dm_space_map *metadata_sm;
148 struct dm_space_map *data_sm;
149 struct dm_transaction_manager *tm;
150 struct dm_transaction_manager *nb_tm;
151
152 /*
153 * Two-level btree.
154 * First level holds thin_dev_t.
155 * Second level holds mappings.
156 */
157 struct dm_btree_info info;
158
159 /*
160 * Non-blocking version of the above.
161 */
162 struct dm_btree_info nb_info;
163
164 /*
165 * Just the top level for deleting whole devices.
166 */
167 struct dm_btree_info tl_info;
168
169 /*
170 * Just the bottom level for creating new devices.
171 */
172 struct dm_btree_info bl_info;
173
174 /*
175 * Describes the device details btree.
176 */
177 struct dm_btree_info details_info;
178
179 struct rw_semaphore root_lock;
180 uint32_t time;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000181 dm_block_t root;
182 dm_block_t details_root;
183 struct list_head thin_devices;
184 uint64_t trans_id;
185 unsigned long flags;
186 sector_t data_block_size;
Joe Thornberda105ed2012-07-27 15:08:15 +0100187
188 /*
189 * Set if a transaction has to be aborted but the attempt to roll back
190 * to the previous (good) transaction failed. The only pool metadata
191 * operation possible in this state is the closing of the device.
192 */
193 bool fail_io:1;
Joe Thornber5a320832014-03-27 14:13:23 +0000194
195 /*
196 * Reading the space map roots can fail, so we read it into these
197 * buffers before the superblock is locked and updated.
198 */
199 __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
200 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
Joe Thornber991d9fa2011-10-31 20:21:18 +0000201};
202
203struct dm_thin_device {
204 struct list_head list;
205 struct dm_pool_metadata *pmd;
206 dm_thin_id id;
207
208 int open_count;
Joe Thornberda105ed2012-07-27 15:08:15 +0100209 bool changed:1;
210 bool aborted_with_changes:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000211 uint64_t mapped_blocks;
212 uint64_t transaction_id;
213 uint32_t creation_time;
214 uint32_t snapshotted_time;
215};
216
217/*----------------------------------------------------------------
218 * superblock validator
219 *--------------------------------------------------------------*/
220
221#define SUPERBLOCK_CSUM_XOR 160774
222
223static void sb_prepare_for_write(struct dm_block_validator *v,
224 struct dm_block *b,
225 size_t block_size)
226{
227 struct thin_disk_superblock *disk_super = dm_block_data(b);
228
229 disk_super->blocknr = cpu_to_le64(dm_block_location(b));
230 disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
231 block_size - sizeof(__le32),
232 SUPERBLOCK_CSUM_XOR));
233}
234
235static int sb_check(struct dm_block_validator *v,
236 struct dm_block *b,
237 size_t block_size)
238{
239 struct thin_disk_superblock *disk_super = dm_block_data(b);
240 __le32 csum_le;
241
242 if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
243 DMERR("sb_check failed: blocknr %llu: "
244 "wanted %llu", le64_to_cpu(disk_super->blocknr),
245 (unsigned long long)dm_block_location(b));
246 return -ENOTBLK;
247 }
248
249 if (le64_to_cpu(disk_super->magic) != THIN_SUPERBLOCK_MAGIC) {
250 DMERR("sb_check failed: magic %llu: "
251 "wanted %llu", le64_to_cpu(disk_super->magic),
252 (unsigned long long)THIN_SUPERBLOCK_MAGIC);
253 return -EILSEQ;
254 }
255
256 csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
257 block_size - sizeof(__le32),
258 SUPERBLOCK_CSUM_XOR));
259 if (csum_le != disk_super->csum) {
260 DMERR("sb_check failed: csum %u: wanted %u",
261 le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
262 return -EILSEQ;
263 }
264
265 return 0;
266}
267
268static struct dm_block_validator sb_validator = {
269 .name = "superblock",
270 .prepare_for_write = sb_prepare_for_write,
271 .check = sb_check
272};
273
274/*----------------------------------------------------------------
275 * Methods for the btree value types
276 *--------------------------------------------------------------*/
277
278static uint64_t pack_block_time(dm_block_t b, uint32_t t)
279{
280 return (b << 24) | t;
281}
282
283static void unpack_block_time(uint64_t v, dm_block_t *b, uint32_t *t)
284{
285 *b = v >> 24;
286 *t = v & ((1 << 24) - 1);
287}
288
Mike Snitzer018cede2013-03-01 22:45:47 +0000289static void data_block_inc(void *context, const void *value_le)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000290{
291 struct dm_space_map *sm = context;
292 __le64 v_le;
293 uint64_t b;
294 uint32_t t;
295
296 memcpy(&v_le, value_le, sizeof(v_le));
297 unpack_block_time(le64_to_cpu(v_le), &b, &t);
298 dm_sm_inc_block(sm, b);
299}
300
Mike Snitzer018cede2013-03-01 22:45:47 +0000301static void data_block_dec(void *context, const void *value_le)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000302{
303 struct dm_space_map *sm = context;
304 __le64 v_le;
305 uint64_t b;
306 uint32_t t;
307
308 memcpy(&v_le, value_le, sizeof(v_le));
309 unpack_block_time(le64_to_cpu(v_le), &b, &t);
310 dm_sm_dec_block(sm, b);
311}
312
Mike Snitzer018cede2013-03-01 22:45:47 +0000313static int data_block_equal(void *context, const void *value1_le, const void *value2_le)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000314{
315 __le64 v1_le, v2_le;
316 uint64_t b1, b2;
317 uint32_t t;
318
319 memcpy(&v1_le, value1_le, sizeof(v1_le));
320 memcpy(&v2_le, value2_le, sizeof(v2_le));
321 unpack_block_time(le64_to_cpu(v1_le), &b1, &t);
322 unpack_block_time(le64_to_cpu(v2_le), &b2, &t);
323
324 return b1 == b2;
325}
326
Mike Snitzer018cede2013-03-01 22:45:47 +0000327static void subtree_inc(void *context, const void *value)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000328{
329 struct dm_btree_info *info = context;
330 __le64 root_le;
331 uint64_t root;
332
333 memcpy(&root_le, value, sizeof(root_le));
334 root = le64_to_cpu(root_le);
335 dm_tm_inc(info->tm, root);
336}
337
Mike Snitzer018cede2013-03-01 22:45:47 +0000338static void subtree_dec(void *context, const void *value)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000339{
340 struct dm_btree_info *info = context;
341 __le64 root_le;
342 uint64_t root;
343
344 memcpy(&root_le, value, sizeof(root_le));
345 root = le64_to_cpu(root_le);
346 if (dm_btree_del(info, root))
347 DMERR("btree delete failed\n");
348}
349
Mike Snitzer018cede2013-03-01 22:45:47 +0000350static int subtree_equal(void *context, const void *value1_le, const void *value2_le)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000351{
352 __le64 v1_le, v2_le;
353 memcpy(&v1_le, value1_le, sizeof(v1_le));
354 memcpy(&v2_le, value2_le, sizeof(v2_le));
355
356 return v1_le == v2_le;
357}
358
359/*----------------------------------------------------------------*/
360
Joe Thornber25971192012-07-27 15:08:09 +0100361static int superblock_lock_zero(struct dm_pool_metadata *pmd,
362 struct dm_block **sblock)
363{
364 return dm_bm_write_lock_zero(pmd->bm, THIN_SUPERBLOCK_LOCATION,
365 &sb_validator, sblock);
366}
367
368static int superblock_lock(struct dm_pool_metadata *pmd,
369 struct dm_block **sblock)
370{
371 return dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
372 &sb_validator, sblock);
373}
374
Joe Thornber332627d2012-07-27 15:08:10 +0100375static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000376{
377 int r;
378 unsigned i;
379 struct dm_block *b;
380 __le64 *data_le, zero = cpu_to_le64(0);
381 unsigned block_size = dm_bm_block_size(bm) / sizeof(__le64);
382
383 /*
384 * We can't use a validator here - it may be all zeroes.
385 */
386 r = dm_bm_read_lock(bm, THIN_SUPERBLOCK_LOCATION, NULL, &b);
387 if (r)
388 return r;
389
390 data_le = dm_block_data(b);
391 *result = 1;
392 for (i = 0; i < block_size; i++) {
393 if (data_le[i] != zero) {
394 *result = 0;
395 break;
396 }
397 }
398
399 return dm_bm_unlock(b);
400}
401
Joe Thornber41675ae2012-07-27 15:08:08 +0100402static void __setup_btree_details(struct dm_pool_metadata *pmd)
403{
404 pmd->info.tm = pmd->tm;
405 pmd->info.levels = 2;
406 pmd->info.value_type.context = pmd->data_sm;
407 pmd->info.value_type.size = sizeof(__le64);
408 pmd->info.value_type.inc = data_block_inc;
409 pmd->info.value_type.dec = data_block_dec;
410 pmd->info.value_type.equal = data_block_equal;
411
412 memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info));
413 pmd->nb_info.tm = pmd->nb_tm;
414
415 pmd->tl_info.tm = pmd->tm;
416 pmd->tl_info.levels = 1;
Joe Thornbere3cbf942012-12-21 20:23:32 +0000417 pmd->tl_info.value_type.context = &pmd->bl_info;
Joe Thornber41675ae2012-07-27 15:08:08 +0100418 pmd->tl_info.value_type.size = sizeof(__le64);
419 pmd->tl_info.value_type.inc = subtree_inc;
420 pmd->tl_info.value_type.dec = subtree_dec;
421 pmd->tl_info.value_type.equal = subtree_equal;
422
423 pmd->bl_info.tm = pmd->tm;
424 pmd->bl_info.levels = 1;
425 pmd->bl_info.value_type.context = pmd->data_sm;
426 pmd->bl_info.value_type.size = sizeof(__le64);
427 pmd->bl_info.value_type.inc = data_block_inc;
428 pmd->bl_info.value_type.dec = data_block_dec;
429 pmd->bl_info.value_type.equal = data_block_equal;
430
431 pmd->details_info.tm = pmd->tm;
432 pmd->details_info.levels = 1;
433 pmd->details_info.value_type.context = NULL;
434 pmd->details_info.value_type.size = sizeof(struct disk_device_details);
435 pmd->details_info.value_type.inc = NULL;
436 pmd->details_info.value_type.dec = NULL;
437 pmd->details_info.value_type.equal = NULL;
438}
439
Joe Thornber5a320832014-03-27 14:13:23 +0000440static int save_sm_roots(struct dm_pool_metadata *pmd)
441{
442 int r;
443 size_t len;
444
445 r = dm_sm_root_size(pmd->metadata_sm, &len);
446 if (r < 0)
447 return r;
448
449 r = dm_sm_copy_root(pmd->metadata_sm, &pmd->metadata_space_map_root, len);
450 if (r < 0)
451 return r;
452
453 r = dm_sm_root_size(pmd->data_sm, &len);
454 if (r < 0)
455 return r;
456
457 return dm_sm_copy_root(pmd->data_sm, &pmd->data_space_map_root, len);
458}
459
460static void copy_sm_roots(struct dm_pool_metadata *pmd,
461 struct thin_disk_superblock *disk)
462{
463 memcpy(&disk->metadata_space_map_root,
464 &pmd->metadata_space_map_root,
465 sizeof(pmd->metadata_space_map_root));
466
467 memcpy(&disk->data_space_map_root,
468 &pmd->data_space_map_root,
469 sizeof(pmd->data_space_map_root));
470}
471
Joe Thornber9cb66532012-07-27 15:08:10 +0100472static int __write_initial_superblock(struct dm_pool_metadata *pmd)
473{
474 int r;
475 struct dm_block *sblock;
476 struct thin_disk_superblock *disk_super;
477 sector_t bdev_size = i_size_read(pmd->bdev->bd_inode) >> SECTOR_SHIFT;
478
479 if (bdev_size > THIN_METADATA_MAX_SECTORS)
480 bdev_size = THIN_METADATA_MAX_SECTORS;
481
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100482 r = dm_sm_commit(pmd->data_sm);
483 if (r < 0)
484 return r;
485
Joe Thornber5a320832014-03-27 14:13:23 +0000486 r = save_sm_roots(pmd);
487 if (r < 0)
488 return r;
489
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100490 r = dm_tm_pre_commit(pmd->tm);
491 if (r < 0)
492 return r;
493
Joe Thornber9cb66532012-07-27 15:08:10 +0100494 r = superblock_lock_zero(pmd, &sblock);
495 if (r)
496 return r;
497
498 disk_super = dm_block_data(sblock);
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100499 disk_super->flags = 0;
Joe Thornber583ceee2012-07-27 15:08:11 +0100500 memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
Joe Thornber9cb66532012-07-27 15:08:10 +0100501 disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
502 disk_super->version = cpu_to_le32(THIN_VERSION);
503 disk_super->time = 0;
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100504 disk_super->trans_id = 0;
505 disk_super->held_root = 0;
506
Joe Thornber5a320832014-03-27 14:13:23 +0000507 copy_sm_roots(pmd, disk_super);
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100508
509 disk_super->data_mapping_root = cpu_to_le64(pmd->root);
510 disk_super->device_details_root = cpu_to_le64(pmd->details_root);
Mike Snitzer7d489352014-02-12 23:58:15 -0500511 disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE);
Joe Thornber9cb66532012-07-27 15:08:10 +0100512 disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
513 disk_super->data_block_size = cpu_to_le32(pmd->data_block_size);
514
Joe Thornber270938b2012-07-27 15:08:11 +0100515 return dm_tm_commit(pmd->tm, sblock);
Joe Thornber9cb66532012-07-27 15:08:10 +0100516}
517
Joe Thornbera97e5e62012-07-27 15:08:12 +0100518static int __format_metadata(struct dm_pool_metadata *pmd)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000519{
520 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000521
Joe Thornbere4d22052012-07-27 15:08:12 +0100522 r = dm_tm_create_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
523 &pmd->tm, &pmd->metadata_sm);
524 if (r < 0) {
525 DMERR("tm_create_with_sm failed");
526 return r;
527 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000528
Joe Thornbera97e5e62012-07-27 15:08:12 +0100529 pmd->data_sm = dm_sm_disk_create(pmd->tm, 0);
Joe Thornbere4d22052012-07-27 15:08:12 +0100530 if (IS_ERR(pmd->data_sm)) {
531 DMERR("sm_disk_create failed");
532 r = PTR_ERR(pmd->data_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100533 goto bad_cleanup_tm;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000534 }
535
Joe Thornberd6332812012-07-27 15:08:12 +0100536 pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000537 if (!pmd->nb_tm) {
Joe Thornber0fa5b172012-07-27 15:08:14 +0100538 DMERR("could not create non-blocking clone tm");
Joe Thornber991d9fa2011-10-31 20:21:18 +0000539 r = -ENOMEM;
Joe Thornber0fa5b172012-07-27 15:08:14 +0100540 goto bad_cleanup_data_sm;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000541 }
542
Joe Thornber41675ae2012-07-27 15:08:08 +0100543 __setup_btree_details(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000544
Joe Thornber9cb66532012-07-27 15:08:10 +0100545 r = dm_btree_empty(&pmd->info, &pmd->root);
546 if (r < 0)
Joe Thornber0fa5b172012-07-27 15:08:14 +0100547 goto bad_cleanup_nb_tm;
Joe Thornber9cb66532012-07-27 15:08:10 +0100548
549 r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
550 if (r < 0) {
551 DMERR("couldn't create devices root");
Joe Thornber0fa5b172012-07-27 15:08:14 +0100552 goto bad_cleanup_nb_tm;
Joe Thornber9cb66532012-07-27 15:08:10 +0100553 }
554
555 r = __write_initial_superblock(pmd);
556 if (r)
Joe Thornber0fa5b172012-07-27 15:08:14 +0100557 goto bad_cleanup_nb_tm;
Joe Thornber9cb66532012-07-27 15:08:10 +0100558
Joe Thornber991d9fa2011-10-31 20:21:18 +0000559 return 0;
560
Joe Thornber0fa5b172012-07-27 15:08:14 +0100561bad_cleanup_nb_tm:
562 dm_tm_destroy(pmd->nb_tm);
563bad_cleanup_data_sm:
Joe Thornberd6332812012-07-27 15:08:12 +0100564 dm_sm_destroy(pmd->data_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100565bad_cleanup_tm:
Joe Thornberd6332812012-07-27 15:08:12 +0100566 dm_tm_destroy(pmd->tm);
567 dm_sm_destroy(pmd->metadata_sm);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000568
569 return r;
570}
571
Mike Snitzerd73ec522012-07-27 15:08:13 +0100572static int __check_incompat_features(struct thin_disk_superblock *disk_super,
573 struct dm_pool_metadata *pmd)
574{
575 uint32_t features;
576
577 features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP;
578 if (features) {
579 DMERR("could not access metadata due to unsupported optional features (%lx).",
580 (unsigned long)features);
581 return -EINVAL;
582 }
583
584 /*
585 * Check for read-only metadata to skip the following RDWR checks.
586 */
587 if (get_disk_ro(pmd->bdev->bd_disk))
588 return 0;
589
590 features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
591 if (features) {
592 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
593 (unsigned long)features);
594 return -EINVAL;
595 }
596
597 return 0;
598}
599
Joe Thornbere4d22052012-07-27 15:08:12 +0100600static int __open_metadata(struct dm_pool_metadata *pmd)
601{
602 int r;
603 struct dm_block *sblock;
604 struct thin_disk_superblock *disk_super;
605
606 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
607 &sb_validator, &sblock);
608 if (r < 0) {
609 DMERR("couldn't read superblock");
610 return r;
611 }
612
613 disk_super = dm_block_data(sblock);
Mike Snitzerd73ec522012-07-27 15:08:13 +0100614
Mike Snitzer9aec8622014-07-14 16:35:54 -0400615 /* Verify the data block size hasn't changed */
616 if (le32_to_cpu(disk_super->data_block_size) != pmd->data_block_size) {
617 DMERR("changing the data block size (from %u to %llu) is not supported",
618 le32_to_cpu(disk_super->data_block_size),
619 (unsigned long long)pmd->data_block_size);
620 r = -EINVAL;
621 goto bad_unlock_sblock;
622 }
623
Mike Snitzerd73ec522012-07-27 15:08:13 +0100624 r = __check_incompat_features(disk_super, pmd);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100625 if (r < 0)
626 goto bad_unlock_sblock;
Mike Snitzerd73ec522012-07-27 15:08:13 +0100627
Joe Thornbere4d22052012-07-27 15:08:12 +0100628 r = dm_tm_open_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
629 disk_super->metadata_space_map_root,
630 sizeof(disk_super->metadata_space_map_root),
631 &pmd->tm, &pmd->metadata_sm);
632 if (r < 0) {
633 DMERR("tm_open_with_sm failed");
Joe Thornber0fa5b172012-07-27 15:08:14 +0100634 goto bad_unlock_sblock;
Joe Thornbere4d22052012-07-27 15:08:12 +0100635 }
636
637 pmd->data_sm = dm_sm_disk_open(pmd->tm, disk_super->data_space_map_root,
638 sizeof(disk_super->data_space_map_root));
639 if (IS_ERR(pmd->data_sm)) {
640 DMERR("sm_disk_open failed");
Joe Thornbere4d22052012-07-27 15:08:12 +0100641 r = PTR_ERR(pmd->data_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100642 goto bad_cleanup_tm;
Joe Thornbere4d22052012-07-27 15:08:12 +0100643 }
644
Joe Thornbere4d22052012-07-27 15:08:12 +0100645 pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
646 if (!pmd->nb_tm) {
Joe Thornber0fa5b172012-07-27 15:08:14 +0100647 DMERR("could not create non-blocking clone tm");
Joe Thornbere4d22052012-07-27 15:08:12 +0100648 r = -ENOMEM;
Joe Thornber0fa5b172012-07-27 15:08:14 +0100649 goto bad_cleanup_data_sm;
Joe Thornbere4d22052012-07-27 15:08:12 +0100650 }
651
652 __setup_btree_details(pmd);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100653 return dm_bm_unlock(sblock);
Joe Thornbere4d22052012-07-27 15:08:12 +0100654
Joe Thornber0fa5b172012-07-27 15:08:14 +0100655bad_cleanup_data_sm:
Joe Thornbere4d22052012-07-27 15:08:12 +0100656 dm_sm_destroy(pmd->data_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100657bad_cleanup_tm:
Joe Thornbere4d22052012-07-27 15:08:12 +0100658 dm_tm_destroy(pmd->tm);
659 dm_sm_destroy(pmd->metadata_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100660bad_unlock_sblock:
661 dm_bm_unlock(sblock);
Joe Thornbere4d22052012-07-27 15:08:12 +0100662
663 return r;
664}
665
Joe Thornber66b1edc2012-07-27 15:08:14 +0100666static int __open_or_format_metadata(struct dm_pool_metadata *pmd, bool format_device)
Joe Thornbere4d22052012-07-27 15:08:12 +0100667{
Joe Thornber8801e062012-07-27 15:08:13 +0100668 int r, unformatted;
Joe Thornber237074c2012-07-27 15:08:13 +0100669
Joe Thornber8801e062012-07-27 15:08:13 +0100670 r = __superblock_all_zeroes(pmd->bm, &unformatted);
Joe Thornber237074c2012-07-27 15:08:13 +0100671 if (r)
672 return r;
673
Joe Thornber8801e062012-07-27 15:08:13 +0100674 if (unformatted)
Joe Thornber66b1edc2012-07-27 15:08:14 +0100675 return format_device ? __format_metadata(pmd) : -EPERM;
676
677 return __open_metadata(pmd);
Joe Thornbere4d22052012-07-27 15:08:12 +0100678}
679
Joe Thornber66b1edc2012-07-27 15:08:14 +0100680static int __create_persistent_data_objects(struct dm_pool_metadata *pmd, bool format_device)
Joe Thornber332627d2012-07-27 15:08:10 +0100681{
682 int r;
683
Mike Snitzer7d489352014-02-12 23:58:15 -0500684 pmd->bm = dm_block_manager_create(pmd->bdev, THIN_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
Joe Thornber332627d2012-07-27 15:08:10 +0100685 THIN_METADATA_CACHE_SIZE,
686 THIN_MAX_CONCURRENT_LOCKS);
687 if (IS_ERR(pmd->bm)) {
688 DMERR("could not create block manager");
689 return PTR_ERR(pmd->bm);
690 }
691
Joe Thornber66b1edc2012-07-27 15:08:14 +0100692 r = __open_or_format_metadata(pmd, format_device);
Joe Thornber332627d2012-07-27 15:08:10 +0100693 if (r)
694 dm_block_manager_destroy(pmd->bm);
695
696 return r;
697}
698
Joe Thornberf9dd9352012-07-27 15:08:10 +0100699static void __destroy_persistent_data_objects(struct dm_pool_metadata *pmd)
700{
701 dm_sm_destroy(pmd->data_sm);
702 dm_sm_destroy(pmd->metadata_sm);
703 dm_tm_destroy(pmd->nb_tm);
704 dm_tm_destroy(pmd->tm);
705 dm_block_manager_destroy(pmd->bm);
706}
707
Joe Thornber991d9fa2011-10-31 20:21:18 +0000708static int __begin_transaction(struct dm_pool_metadata *pmd)
709{
710 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000711 struct thin_disk_superblock *disk_super;
712 struct dm_block *sblock;
713
714 /*
Joe Thornber991d9fa2011-10-31 20:21:18 +0000715 * We re-read the superblock every time. Shouldn't need to do this
716 * really.
717 */
718 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
719 &sb_validator, &sblock);
720 if (r)
721 return r;
722
723 disk_super = dm_block_data(sblock);
724 pmd->time = le32_to_cpu(disk_super->time);
725 pmd->root = le64_to_cpu(disk_super->data_mapping_root);
726 pmd->details_root = le64_to_cpu(disk_super->device_details_root);
727 pmd->trans_id = le64_to_cpu(disk_super->trans_id);
728 pmd->flags = le32_to_cpu(disk_super->flags);
729 pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
730
Joe Thornber991d9fa2011-10-31 20:21:18 +0000731 dm_bm_unlock(sblock);
Mike Snitzerd73ec522012-07-27 15:08:13 +0100732 return 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000733}
734
735static int __write_changed_details(struct dm_pool_metadata *pmd)
736{
737 int r;
738 struct dm_thin_device *td, *tmp;
739 struct disk_device_details details;
740 uint64_t key;
741
742 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
743 if (!td->changed)
744 continue;
745
746 key = td->id;
747
748 details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
749 details.transaction_id = cpu_to_le64(td->transaction_id);
750 details.creation_time = cpu_to_le32(td->creation_time);
751 details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
752 __dm_bless_for_disk(&details);
753
754 r = dm_btree_insert(&pmd->details_info, pmd->details_root,
755 &key, &details, &pmd->details_root);
756 if (r)
757 return r;
758
759 if (td->open_count)
760 td->changed = 0;
761 else {
762 list_del(&td->list);
763 kfree(td);
764 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000765 }
766
767 return 0;
768}
769
770static int __commit_transaction(struct dm_pool_metadata *pmd)
771{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000772 int r;
773 size_t metadata_len, data_len;
774 struct thin_disk_superblock *disk_super;
775 struct dm_block *sblock;
776
777 /*
778 * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
779 */
780 BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
781
782 r = __write_changed_details(pmd);
783 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100784 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000785
Joe Thornber991d9fa2011-10-31 20:21:18 +0000786 r = dm_sm_commit(pmd->data_sm);
787 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100788 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000789
790 r = dm_tm_pre_commit(pmd->tm);
791 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100792 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000793
794 r = dm_sm_root_size(pmd->metadata_sm, &metadata_len);
795 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100796 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000797
Joe Thornberfef838c2012-03-28 18:41:25 +0100798 r = dm_sm_root_size(pmd->data_sm, &data_len);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000799 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100800 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000801
Joe Thornber5a320832014-03-27 14:13:23 +0000802 r = save_sm_roots(pmd);
803 if (r < 0)
804 return r;
805
Joe Thornber25971192012-07-27 15:08:09 +0100806 r = superblock_lock(pmd, &sblock);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000807 if (r)
Joe Thornberd973ac12012-07-27 15:07:58 +0100808 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000809
810 disk_super = dm_block_data(sblock);
811 disk_super->time = cpu_to_le32(pmd->time);
812 disk_super->data_mapping_root = cpu_to_le64(pmd->root);
813 disk_super->device_details_root = cpu_to_le64(pmd->details_root);
814 disk_super->trans_id = cpu_to_le64(pmd->trans_id);
815 disk_super->flags = cpu_to_le32(pmd->flags);
816
Joe Thornber5a320832014-03-27 14:13:23 +0000817 copy_sm_roots(pmd, disk_super);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000818
Joe Thornbereb04cf62012-07-27 15:08:08 +0100819 return dm_tm_commit(pmd->tm, sblock);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000820}
821
822struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
Joe Thornber66b1edc2012-07-27 15:08:14 +0100823 sector_t data_block_size,
824 bool format_device)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000825{
826 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000827 struct dm_pool_metadata *pmd;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000828
829 pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
830 if (!pmd) {
831 DMERR("could not allocate metadata struct");
832 return ERR_PTR(-ENOMEM);
833 }
834
Joe Thornber6a0ebd32012-07-27 15:08:10 +0100835 init_rwsem(&pmd->root_lock);
836 pmd->time = 0;
837 INIT_LIST_HEAD(&pmd->thin_devices);
Joe Thornberda105ed2012-07-27 15:08:15 +0100838 pmd->fail_io = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000839 pmd->bdev = bdev;
Joe Thornber9cb66532012-07-27 15:08:10 +0100840 pmd->data_block_size = data_block_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000841
Joe Thornber66b1edc2012-07-27 15:08:14 +0100842 r = __create_persistent_data_objects(pmd, format_device);
Joe Thornber332627d2012-07-27 15:08:10 +0100843 if (r) {
844 kfree(pmd);
845 return ERR_PTR(r);
846 }
847
Joe Thornber270938b2012-07-27 15:08:11 +0100848 r = __begin_transaction(pmd);
849 if (r < 0) {
850 if (dm_pool_metadata_close(pmd) < 0)
851 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
852 return ERR_PTR(r);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000853 }
854
855 return pmd;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000856}
857
858int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
859{
860 int r;
861 unsigned open_devices = 0;
862 struct dm_thin_device *td, *tmp;
863
864 down_read(&pmd->root_lock);
865 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
866 if (td->open_count)
867 open_devices++;
868 else {
869 list_del(&td->list);
870 kfree(td);
871 }
872 }
873 up_read(&pmd->root_lock);
874
875 if (open_devices) {
876 DMERR("attempt to close pmd when %u device(s) are still open",
877 open_devices);
878 return -EBUSY;
879 }
880
Mike Snitzer49f154c2015-04-23 15:06:27 -0400881 if (!dm_bm_is_read_only(pmd->bm) && !pmd->fail_io) {
Joe Thornber12ba58a2012-07-27 15:08:15 +0100882 r = __commit_transaction(pmd);
883 if (r < 0)
884 DMWARN("%s: __commit_transaction() failed, error = %d",
885 __func__, r);
886 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000887
Joe Thornberda105ed2012-07-27 15:08:15 +0100888 if (!pmd->fail_io)
889 __destroy_persistent_data_objects(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000890
Joe Thornberda105ed2012-07-27 15:08:15 +0100891 kfree(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000892 return 0;
893}
894
Mike Snitzer1f3db252012-03-07 19:09:41 +0000895/*
896 * __open_device: Returns @td corresponding to device with id @dev,
897 * creating it if @create is set and incrementing @td->open_count.
898 * On failure, @td is undefined.
899 */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000900static int __open_device(struct dm_pool_metadata *pmd,
901 dm_thin_id dev, int create,
902 struct dm_thin_device **td)
903{
904 int r, changed = 0;
905 struct dm_thin_device *td2;
906 uint64_t key = dev;
907 struct disk_device_details details_le;
908
909 /*
Mike Snitzer1f3db252012-03-07 19:09:41 +0000910 * If the device is already open, return it.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000911 */
912 list_for_each_entry(td2, &pmd->thin_devices, list)
913 if (td2->id == dev) {
Mike Snitzer1f3db252012-03-07 19:09:41 +0000914 /*
915 * May not create an already-open device.
916 */
917 if (create)
918 return -EEXIST;
919
Joe Thornber991d9fa2011-10-31 20:21:18 +0000920 td2->open_count++;
921 *td = td2;
922 return 0;
923 }
924
925 /*
926 * Check the device exists.
927 */
928 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
929 &key, &details_le);
930 if (r) {
931 if (r != -ENODATA || !create)
932 return r;
933
Mike Snitzer1f3db252012-03-07 19:09:41 +0000934 /*
935 * Create new device.
936 */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000937 changed = 1;
938 details_le.mapped_blocks = 0;
939 details_le.transaction_id = cpu_to_le64(pmd->trans_id);
940 details_le.creation_time = cpu_to_le32(pmd->time);
941 details_le.snapshotted_time = cpu_to_le32(pmd->time);
942 }
943
944 *td = kmalloc(sizeof(**td), GFP_NOIO);
945 if (!*td)
946 return -ENOMEM;
947
948 (*td)->pmd = pmd;
949 (*td)->id = dev;
950 (*td)->open_count = 1;
951 (*td)->changed = changed;
Joe Thornberda105ed2012-07-27 15:08:15 +0100952 (*td)->aborted_with_changes = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000953 (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
954 (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
955 (*td)->creation_time = le32_to_cpu(details_le.creation_time);
956 (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
957
958 list_add(&(*td)->list, &pmd->thin_devices);
959
960 return 0;
961}
962
963static void __close_device(struct dm_thin_device *td)
964{
965 --td->open_count;
966}
967
968static int __create_thin(struct dm_pool_metadata *pmd,
969 dm_thin_id dev)
970{
971 int r;
972 dm_block_t dev_root;
973 uint64_t key = dev;
974 struct disk_device_details details_le;
975 struct dm_thin_device *td;
976 __le64 value;
977
978 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
979 &key, &details_le);
980 if (!r)
981 return -EEXIST;
982
983 /*
984 * Create an empty btree for the mappings.
985 */
986 r = dm_btree_empty(&pmd->bl_info, &dev_root);
987 if (r)
988 return r;
989
990 /*
991 * Insert it into the main mapping tree.
992 */
993 value = cpu_to_le64(dev_root);
994 __dm_bless_for_disk(&value);
995 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
996 if (r) {
997 dm_btree_del(&pmd->bl_info, dev_root);
998 return r;
999 }
1000
1001 r = __open_device(pmd, dev, 1, &td);
1002 if (r) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001003 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1004 dm_btree_del(&pmd->bl_info, dev_root);
1005 return r;
1006 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001007 __close_device(td);
1008
1009 return r;
1010}
1011
1012int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
1013{
Joe Thornberda105ed2012-07-27 15:08:15 +01001014 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001015
1016 down_write(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001017 if (!pmd->fail_io)
1018 r = __create_thin(pmd, dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001019 up_write(&pmd->root_lock);
1020
1021 return r;
1022}
1023
1024static int __set_snapshot_details(struct dm_pool_metadata *pmd,
1025 struct dm_thin_device *snap,
1026 dm_thin_id origin, uint32_t time)
1027{
1028 int r;
1029 struct dm_thin_device *td;
1030
1031 r = __open_device(pmd, origin, 0, &td);
1032 if (r)
1033 return r;
1034
1035 td->changed = 1;
1036 td->snapshotted_time = time;
1037
1038 snap->mapped_blocks = td->mapped_blocks;
1039 snap->snapshotted_time = time;
1040 __close_device(td);
1041
1042 return 0;
1043}
1044
1045static int __create_snap(struct dm_pool_metadata *pmd,
1046 dm_thin_id dev, dm_thin_id origin)
1047{
1048 int r;
1049 dm_block_t origin_root;
1050 uint64_t key = origin, dev_key = dev;
1051 struct dm_thin_device *td;
1052 struct disk_device_details details_le;
1053 __le64 value;
1054
1055 /* check this device is unused */
1056 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
1057 &dev_key, &details_le);
1058 if (!r)
1059 return -EEXIST;
1060
1061 /* find the mapping tree for the origin */
1062 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
1063 if (r)
1064 return r;
1065 origin_root = le64_to_cpu(value);
1066
1067 /* clone the origin, an inc will do */
1068 dm_tm_inc(pmd->tm, origin_root);
1069
1070 /* insert into the main mapping tree */
1071 value = cpu_to_le64(origin_root);
1072 __dm_bless_for_disk(&value);
1073 key = dev;
1074 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
1075 if (r) {
1076 dm_tm_dec(pmd->tm, origin_root);
1077 return r;
1078 }
1079
1080 pmd->time++;
1081
1082 r = __open_device(pmd, dev, 1, &td);
1083 if (r)
1084 goto bad;
1085
1086 r = __set_snapshot_details(pmd, td, origin, pmd->time);
Mike Snitzer1f3db252012-03-07 19:09:41 +00001087 __close_device(td);
1088
Joe Thornber991d9fa2011-10-31 20:21:18 +00001089 if (r)
1090 goto bad;
1091
Joe Thornber991d9fa2011-10-31 20:21:18 +00001092 return 0;
1093
1094bad:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001095 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1096 dm_btree_remove(&pmd->details_info, pmd->details_root,
1097 &key, &pmd->details_root);
1098 return r;
1099}
1100
1101int dm_pool_create_snap(struct dm_pool_metadata *pmd,
1102 dm_thin_id dev,
1103 dm_thin_id origin)
1104{
Joe Thornberda105ed2012-07-27 15:08:15 +01001105 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001106
1107 down_write(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001108 if (!pmd->fail_io)
1109 r = __create_snap(pmd, dev, origin);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001110 up_write(&pmd->root_lock);
1111
1112 return r;
1113}
1114
1115static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
1116{
1117 int r;
1118 uint64_t key = dev;
1119 struct dm_thin_device *td;
1120
1121 /* TODO: failure should mark the transaction invalid */
1122 r = __open_device(pmd, dev, 0, &td);
1123 if (r)
1124 return r;
1125
1126 if (td->open_count > 1) {
1127 __close_device(td);
1128 return -EBUSY;
1129 }
1130
1131 list_del(&td->list);
1132 kfree(td);
1133 r = dm_btree_remove(&pmd->details_info, pmd->details_root,
1134 &key, &pmd->details_root);
1135 if (r)
1136 return r;
1137
1138 r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1139 if (r)
1140 return r;
1141
Joe Thornber991d9fa2011-10-31 20:21:18 +00001142 return 0;
1143}
1144
1145int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
1146 dm_thin_id dev)
1147{
Joe Thornberda105ed2012-07-27 15:08:15 +01001148 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001149
1150 down_write(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001151 if (!pmd->fail_io)
1152 r = __delete_device(pmd, dev);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001153 up_write(&pmd->root_lock);
1154
1155 return r;
1156}
1157
1158int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
1159 uint64_t current_id,
1160 uint64_t new_id)
1161{
Joe Thornberda105ed2012-07-27 15:08:15 +01001162 int r = -EINVAL;
1163
Joe Thornber991d9fa2011-10-31 20:21:18 +00001164 down_write(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001165
1166 if (pmd->fail_io)
1167 goto out;
1168
Joe Thornber991d9fa2011-10-31 20:21:18 +00001169 if (pmd->trans_id != current_id) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001170 DMERR("mismatched transaction id");
Joe Thornberda105ed2012-07-27 15:08:15 +01001171 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001172 }
1173
1174 pmd->trans_id = new_id;
Joe Thornberda105ed2012-07-27 15:08:15 +01001175 r = 0;
1176
1177out:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001178 up_write(&pmd->root_lock);
1179
Joe Thornberda105ed2012-07-27 15:08:15 +01001180 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001181}
1182
1183int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
1184 uint64_t *result)
1185{
Joe Thornberda105ed2012-07-27 15:08:15 +01001186 int r = -EINVAL;
1187
Joe Thornber991d9fa2011-10-31 20:21:18 +00001188 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001189 if (!pmd->fail_io) {
1190 *result = pmd->trans_id;
1191 r = 0;
1192 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001193 up_read(&pmd->root_lock);
1194
Joe Thornberda105ed2012-07-27 15:08:15 +01001195 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001196}
1197
Joe Thornbercc8394d2012-06-03 00:30:01 +01001198static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
1199{
1200 int r, inc;
1201 struct thin_disk_superblock *disk_super;
1202 struct dm_block *copy, *sblock;
1203 dm_block_t held_root;
1204
1205 /*
1206 * Copy the superblock.
1207 */
1208 dm_sm_inc_block(pmd->metadata_sm, THIN_SUPERBLOCK_LOCATION);
1209 r = dm_tm_shadow_block(pmd->tm, THIN_SUPERBLOCK_LOCATION,
1210 &sb_validator, &copy, &inc);
1211 if (r)
1212 return r;
1213
1214 BUG_ON(!inc);
1215
1216 held_root = dm_block_location(copy);
1217 disk_super = dm_block_data(copy);
1218
1219 if (le64_to_cpu(disk_super->held_root)) {
1220 DMWARN("Pool metadata snapshot already exists: release this before taking another.");
1221
1222 dm_tm_dec(pmd->tm, held_root);
1223 dm_tm_unlock(pmd->tm, copy);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001224 return -EBUSY;
1225 }
1226
1227 /*
1228 * Wipe the spacemap since we're not publishing this.
1229 */
1230 memset(&disk_super->data_space_map_root, 0,
1231 sizeof(disk_super->data_space_map_root));
1232 memset(&disk_super->metadata_space_map_root, 0,
1233 sizeof(disk_super->metadata_space_map_root));
1234
1235 /*
1236 * Increment the data structures that need to be preserved.
1237 */
1238 dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->data_mapping_root));
1239 dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->device_details_root));
1240 dm_tm_unlock(pmd->tm, copy);
1241
1242 /*
1243 * Write the held root into the superblock.
1244 */
Joe Thornber25971192012-07-27 15:08:09 +01001245 r = superblock_lock(pmd, &sblock);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001246 if (r) {
1247 dm_tm_dec(pmd->tm, held_root);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001248 return r;
1249 }
1250
1251 disk_super = dm_block_data(sblock);
1252 disk_super->held_root = cpu_to_le64(held_root);
1253 dm_bm_unlock(sblock);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001254 return 0;
1255}
1256
1257int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd)
1258{
Joe Thornberda105ed2012-07-27 15:08:15 +01001259 int r = -EINVAL;
Joe Thornbercc8394d2012-06-03 00:30:01 +01001260
1261 down_write(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001262 if (!pmd->fail_io)
1263 r = __reserve_metadata_snap(pmd);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001264 up_write(&pmd->root_lock);
1265
1266 return r;
1267}
1268
1269static int __release_metadata_snap(struct dm_pool_metadata *pmd)
1270{
1271 int r;
1272 struct thin_disk_superblock *disk_super;
1273 struct dm_block *sblock, *copy;
1274 dm_block_t held_root;
1275
Joe Thornber25971192012-07-27 15:08:09 +01001276 r = superblock_lock(pmd, &sblock);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001277 if (r)
1278 return r;
1279
1280 disk_super = dm_block_data(sblock);
1281 held_root = le64_to_cpu(disk_super->held_root);
1282 disk_super->held_root = cpu_to_le64(0);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001283
1284 dm_bm_unlock(sblock);
1285
1286 if (!held_root) {
1287 DMWARN("No pool metadata snapshot found: nothing to release.");
1288 return -EINVAL;
1289 }
1290
1291 r = dm_tm_read_lock(pmd->tm, held_root, &sb_validator, &copy);
1292 if (r)
1293 return r;
1294
1295 disk_super = dm_block_data(copy);
Joe Thornber7f518ad2015-08-12 15:10:21 +01001296 dm_btree_del(&pmd->info, le64_to_cpu(disk_super->data_mapping_root));
1297 dm_btree_del(&pmd->details_info, le64_to_cpu(disk_super->device_details_root));
Joe Thornbercc8394d2012-06-03 00:30:01 +01001298 dm_sm_dec_block(pmd->metadata_sm, held_root);
1299
1300 return dm_tm_unlock(pmd->tm, copy);
1301}
1302
1303int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd)
1304{
Joe Thornberda105ed2012-07-27 15:08:15 +01001305 int r = -EINVAL;
Joe Thornbercc8394d2012-06-03 00:30:01 +01001306
1307 down_write(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001308 if (!pmd->fail_io)
1309 r = __release_metadata_snap(pmd);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001310 up_write(&pmd->root_lock);
1311
1312 return r;
1313}
1314
1315static int __get_metadata_snap(struct dm_pool_metadata *pmd,
1316 dm_block_t *result)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001317{
1318 int r;
1319 struct thin_disk_superblock *disk_super;
1320 struct dm_block *sblock;
1321
Joe Thornbercc8394d2012-06-03 00:30:01 +01001322 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
1323 &sb_validator, &sblock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001324 if (r)
1325 return r;
1326
1327 disk_super = dm_block_data(sblock);
1328 *result = le64_to_cpu(disk_super->held_root);
1329
1330 return dm_bm_unlock(sblock);
1331}
1332
Joe Thornbercc8394d2012-06-03 00:30:01 +01001333int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
1334 dm_block_t *result)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001335{
Joe Thornberda105ed2012-07-27 15:08:15 +01001336 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001337
1338 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001339 if (!pmd->fail_io)
1340 r = __get_metadata_snap(pmd, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001341 up_read(&pmd->root_lock);
1342
1343 return r;
1344}
1345
1346int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
1347 struct dm_thin_device **td)
1348{
Joe Thornberda105ed2012-07-27 15:08:15 +01001349 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001350
1351 down_write(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001352 if (!pmd->fail_io)
1353 r = __open_device(pmd, dev, 0, td);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001354 up_write(&pmd->root_lock);
1355
1356 return r;
1357}
1358
1359int dm_pool_close_thin_device(struct dm_thin_device *td)
1360{
1361 down_write(&td->pmd->root_lock);
1362 __close_device(td);
1363 up_write(&td->pmd->root_lock);
1364
1365 return 0;
1366}
1367
1368dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
1369{
1370 return td->id;
1371}
1372
Joe Thornber19fa1a62013-12-17 12:09:40 -05001373/*
1374 * Check whether @time (of block creation) is older than @td's last snapshot.
1375 * If so then the associated block is shared with the last snapshot device.
1376 * Any block on a device created *after* the device last got snapshotted is
1377 * necessarily not shared.
1378 */
Mike Snitzer17b7d632012-07-27 15:07:57 +01001379static bool __snapshotted_since(struct dm_thin_device *td, uint32_t time)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001380{
1381 return td->snapshotted_time > time;
1382}
1383
1384int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
Joe Thornbere5cfc692014-10-06 15:24:55 +01001385 int can_issue_io, struct dm_thin_lookup_result *result)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001386{
Joe Thornbere5cfc692014-10-06 15:24:55 +01001387 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001388 __le64 value;
1389 struct dm_pool_metadata *pmd = td->pmd;
1390 dm_block_t keys[2] = { td->id, block };
Joe Thornberda105ed2012-07-27 15:08:15 +01001391 struct dm_btree_info *info;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001392
Joe Thornbere5cfc692014-10-06 15:24:55 +01001393 down_read(&pmd->root_lock);
Joe Thornberb1f11af2015-06-11 17:11:48 +01001394 if (pmd->fail_io) {
1395 up_read(&pmd->root_lock);
1396 return -EINVAL;
1397 }
Joe Thornbere5cfc692014-10-06 15:24:55 +01001398
1399 if (can_issue_io) {
1400 info = &pmd->info;
1401 } else
1402 info = &pmd->nb_info;
Joe Thornberda105ed2012-07-27 15:08:15 +01001403
1404 r = dm_btree_lookup(info, pmd->root, keys, &value);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001405 if (!r) {
Joe Thornbere5cfc692014-10-06 15:24:55 +01001406 uint64_t block_time = 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001407 dm_block_t exception_block;
1408 uint32_t exception_time;
Joe Thornbere5cfc692014-10-06 15:24:55 +01001409
1410 block_time = le64_to_cpu(value);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001411 unpack_block_time(block_time, &exception_block,
1412 &exception_time);
1413 result->block = exception_block;
1414 result->shared = __snapshotted_since(td, exception_time);
1415 }
1416
Joe Thornbere5cfc692014-10-06 15:24:55 +01001417 up_read(&pmd->root_lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001418 return r;
1419}
1420
Joe Thornbera5d895a2015-04-16 12:47:21 +01001421/* FIXME: write a more efficient one in btree */
1422int dm_thin_find_mapped_range(struct dm_thin_device *td,
1423 dm_block_t begin, dm_block_t end,
1424 dm_block_t *thin_begin, dm_block_t *thin_end,
1425 dm_block_t *pool_begin, bool *maybe_shared)
1426{
1427 int r;
1428 dm_block_t pool_end;
1429 struct dm_thin_lookup_result lookup;
1430
1431 if (end < begin)
1432 return -ENODATA;
1433
1434 /*
1435 * Find first mapped block.
1436 */
1437 while (begin < end) {
1438 r = dm_thin_find_block(td, begin, true, &lookup);
1439 if (r) {
1440 if (r != -ENODATA)
1441 return r;
1442 } else
1443 break;
1444
1445 begin++;
1446 }
1447
1448 if (begin == end)
1449 return -ENODATA;
1450
1451 *thin_begin = begin;
1452 *pool_begin = lookup.block;
1453 *maybe_shared = lookup.shared;
1454
1455 begin++;
1456 pool_end = *pool_begin + 1;
1457 while (begin != end) {
1458 r = dm_thin_find_block(td, begin, true, &lookup);
1459 if (r) {
1460 if (r == -ENODATA)
1461 break;
1462 else
1463 return r;
1464 }
1465
1466 if ((lookup.block != pool_end) ||
1467 (lookup.shared != *maybe_shared))
1468 break;
1469
1470 pool_end++;
1471 begin++;
1472 }
1473
1474 *thin_end = begin;
1475 return 0;
1476}
1477
Joe Thornber991d9fa2011-10-31 20:21:18 +00001478static int __insert(struct dm_thin_device *td, dm_block_t block,
1479 dm_block_t data_block)
1480{
1481 int r, inserted;
1482 __le64 value;
1483 struct dm_pool_metadata *pmd = td->pmd;
1484 dm_block_t keys[2] = { td->id, block };
1485
Joe Thornber991d9fa2011-10-31 20:21:18 +00001486 value = cpu_to_le64(pack_block_time(data_block, pmd->time));
1487 __dm_bless_for_disk(&value);
1488
1489 r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
1490 &pmd->root, &inserted);
1491 if (r)
1492 return r;
1493
Joe Thornber40db5a52012-07-27 15:08:14 +01001494 td->changed = 1;
1495 if (inserted)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001496 td->mapped_blocks++;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001497
1498 return 0;
1499}
1500
1501int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
1502 dm_block_t data_block)
1503{
Joe Thornberda105ed2012-07-27 15:08:15 +01001504 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001505
1506 down_write(&td->pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001507 if (!td->pmd->fail_io)
1508 r = __insert(td, block, data_block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001509 up_write(&td->pmd->root_lock);
1510
1511 return r;
1512}
1513
1514static int __remove(struct dm_thin_device *td, dm_block_t block)
1515{
1516 int r;
1517 struct dm_pool_metadata *pmd = td->pmd;
1518 dm_block_t keys[2] = { td->id, block };
1519
1520 r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
1521 if (r)
1522 return r;
1523
Joe Thornberaf63bcb2012-03-07 19:09:44 +00001524 td->mapped_blocks--;
1525 td->changed = 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001526
1527 return 0;
1528}
1529
Joe Thornber6550f072015-04-13 09:45:25 +01001530static int __remove_range(struct dm_thin_device *td, dm_block_t begin, dm_block_t end)
1531{
1532 int r;
1533 unsigned count;
1534 struct dm_pool_metadata *pmd = td->pmd;
1535 dm_block_t keys[1] = { td->id };
1536 __le64 value;
1537 dm_block_t mapping_root;
1538
1539 /*
1540 * Find the mapping tree
1541 */
1542 r = dm_btree_lookup(&pmd->tl_info, pmd->root, keys, &value);
1543 if (r)
1544 return r;
1545
1546 /*
1547 * Remove from the mapping tree, taking care to inc the
1548 * ref count so it doesn't get deleted.
1549 */
1550 mapping_root = le64_to_cpu(value);
1551 dm_tm_inc(pmd->tm, mapping_root);
1552 r = dm_btree_remove(&pmd->tl_info, pmd->root, keys, &pmd->root);
1553 if (r)
1554 return r;
1555
1556 r = dm_btree_remove_leaves(&pmd->bl_info, mapping_root, &begin, end, &mapping_root, &count);
1557 if (r)
1558 return r;
1559
1560 td->mapped_blocks -= count;
1561 td->changed = 1;
1562
1563 /*
1564 * Reinsert the mapping tree.
1565 */
1566 value = cpu_to_le64(mapping_root);
1567 __dm_bless_for_disk(&value);
1568 return dm_btree_insert(&pmd->tl_info, pmd->root, keys, &value, &pmd->root);
1569}
1570
Joe Thornber991d9fa2011-10-31 20:21:18 +00001571int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
1572{
Joe Thornberda105ed2012-07-27 15:08:15 +01001573 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001574
1575 down_write(&td->pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001576 if (!td->pmd->fail_io)
1577 r = __remove(td, block);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001578 up_write(&td->pmd->root_lock);
1579
1580 return r;
1581}
1582
Joe Thornber6550f072015-04-13 09:45:25 +01001583int dm_thin_remove_range(struct dm_thin_device *td,
1584 dm_block_t begin, dm_block_t end)
1585{
1586 int r = -EINVAL;
1587
1588 down_write(&td->pmd->root_lock);
1589 if (!td->pmd->fail_io)
1590 r = __remove_range(td, begin, end);
1591 up_write(&td->pmd->root_lock);
1592
1593 return r;
1594}
1595
Joe Thornber19fa1a62013-12-17 12:09:40 -05001596int dm_pool_block_is_used(struct dm_pool_metadata *pmd, dm_block_t b, bool *result)
1597{
1598 int r;
1599 uint32_t ref_count;
1600
1601 down_read(&pmd->root_lock);
1602 r = dm_sm_get_count(pmd->data_sm, b, &ref_count);
1603 if (!r)
1604 *result = (ref_count != 0);
1605 up_read(&pmd->root_lock);
1606
1607 return r;
1608}
1609
Joe Thornber40db5a52012-07-27 15:08:14 +01001610bool dm_thin_changed_this_transaction(struct dm_thin_device *td)
1611{
1612 int r;
1613
1614 down_read(&td->pmd->root_lock);
1615 r = td->changed;
1616 up_read(&td->pmd->root_lock);
1617
1618 return r;
1619}
1620
Mike Snitzer4d1662a2014-02-06 06:08:56 -05001621bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd)
1622{
1623 bool r = false;
1624 struct dm_thin_device *td, *tmp;
1625
1626 down_read(&pmd->root_lock);
1627 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
1628 if (td->changed) {
1629 r = td->changed;
1630 break;
1631 }
1632 }
1633 up_read(&pmd->root_lock);
1634
1635 return r;
1636}
1637
Joe Thornberda105ed2012-07-27 15:08:15 +01001638bool dm_thin_aborted_changes(struct dm_thin_device *td)
1639{
1640 bool r;
1641
1642 down_read(&td->pmd->root_lock);
1643 r = td->aborted_with_changes;
1644 up_read(&td->pmd->root_lock);
1645
1646 return r;
1647}
1648
Joe Thornber991d9fa2011-10-31 20:21:18 +00001649int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
1650{
Joe Thornberda105ed2012-07-27 15:08:15 +01001651 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001652
1653 down_write(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001654 if (!pmd->fail_io)
1655 r = dm_sm_new_block(pmd->data_sm, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001656 up_write(&pmd->root_lock);
1657
1658 return r;
1659}
1660
1661int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
1662{
Joe Thornberda105ed2012-07-27 15:08:15 +01001663 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001664
1665 down_write(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001666 if (pmd->fail_io)
1667 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001668
1669 r = __commit_transaction(pmd);
1670 if (r <= 0)
1671 goto out;
1672
1673 /*
1674 * Open the next transaction.
1675 */
1676 r = __begin_transaction(pmd);
1677out:
1678 up_write(&pmd->root_lock);
1679 return r;
1680}
1681
Joe Thornberda105ed2012-07-27 15:08:15 +01001682static void __set_abort_with_changes_flags(struct dm_pool_metadata *pmd)
1683{
1684 struct dm_thin_device *td;
1685
1686 list_for_each_entry(td, &pmd->thin_devices, list)
1687 td->aborted_with_changes = td->changed;
1688}
1689
1690int dm_pool_abort_metadata(struct dm_pool_metadata *pmd)
1691{
1692 int r = -EINVAL;
1693
1694 down_write(&pmd->root_lock);
1695 if (pmd->fail_io)
1696 goto out;
1697
1698 __set_abort_with_changes_flags(pmd);
1699 __destroy_persistent_data_objects(pmd);
1700 r = __create_persistent_data_objects(pmd, false);
1701 if (r)
1702 pmd->fail_io = true;
1703
1704out:
1705 up_write(&pmd->root_lock);
1706
1707 return r;
1708}
1709
Joe Thornber991d9fa2011-10-31 20:21:18 +00001710int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
1711{
Joe Thornberda105ed2012-07-27 15:08:15 +01001712 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001713
1714 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001715 if (!pmd->fail_io)
1716 r = dm_sm_get_nr_free(pmd->data_sm, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001717 up_read(&pmd->root_lock);
1718
1719 return r;
1720}
1721
1722int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
1723 dm_block_t *result)
1724{
Joe Thornberda105ed2012-07-27 15:08:15 +01001725 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001726
1727 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001728 if (!pmd->fail_io)
1729 r = dm_sm_get_nr_free(pmd->metadata_sm, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001730 up_read(&pmd->root_lock);
1731
1732 return r;
1733}
1734
1735int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
1736 dm_block_t *result)
1737{
Joe Thornberda105ed2012-07-27 15:08:15 +01001738 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001739
1740 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001741 if (!pmd->fail_io)
1742 r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001743 up_read(&pmd->root_lock);
1744
1745 return r;
1746}
1747
Joe Thornber991d9fa2011-10-31 20:21:18 +00001748int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
1749{
Joe Thornberda105ed2012-07-27 15:08:15 +01001750 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001751
1752 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001753 if (!pmd->fail_io)
1754 r = dm_sm_get_nr_blocks(pmd->data_sm, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001755 up_read(&pmd->root_lock);
1756
1757 return r;
1758}
1759
1760int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
1761{
Joe Thornberda105ed2012-07-27 15:08:15 +01001762 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001763 struct dm_pool_metadata *pmd = td->pmd;
1764
1765 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001766 if (!pmd->fail_io) {
1767 *result = td->mapped_blocks;
1768 r = 0;
1769 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001770 up_read(&pmd->root_lock);
1771
Joe Thornberda105ed2012-07-27 15:08:15 +01001772 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001773}
1774
1775static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
1776{
1777 int r;
1778 __le64 value_le;
1779 dm_block_t thin_root;
1780 struct dm_pool_metadata *pmd = td->pmd;
1781
1782 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
1783 if (r)
1784 return r;
1785
1786 thin_root = le64_to_cpu(value_le);
1787
1788 return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
1789}
1790
1791int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
1792 dm_block_t *result)
1793{
Joe Thornberda105ed2012-07-27 15:08:15 +01001794 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001795 struct dm_pool_metadata *pmd = td->pmd;
1796
1797 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001798 if (!pmd->fail_io)
1799 r = __highest_block(td, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001800 up_read(&pmd->root_lock);
1801
1802 return r;
1803}
1804
Joe Thornberb17446d2013-05-10 14:37:18 +01001805static int __resize_space_map(struct dm_space_map *sm, dm_block_t new_count)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001806{
1807 int r;
1808 dm_block_t old_count;
1809
Joe Thornberb17446d2013-05-10 14:37:18 +01001810 r = dm_sm_get_nr_blocks(sm, &old_count);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001811 if (r)
1812 return r;
1813
1814 if (new_count == old_count)
1815 return 0;
1816
1817 if (new_count < old_count) {
Joe Thornberb17446d2013-05-10 14:37:18 +01001818 DMERR("cannot reduce size of space map");
Joe Thornber991d9fa2011-10-31 20:21:18 +00001819 return -EINVAL;
1820 }
1821
Joe Thornberb17446d2013-05-10 14:37:18 +01001822 return dm_sm_extend(sm, new_count - old_count);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001823}
1824
1825int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1826{
Joe Thornberda105ed2012-07-27 15:08:15 +01001827 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001828
1829 down_write(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001830 if (!pmd->fail_io)
Joe Thornberb17446d2013-05-10 14:37:18 +01001831 r = __resize_space_map(pmd->data_sm, new_count);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001832 up_write(&pmd->root_lock);
1833
1834 return r;
1835}
Joe Thornber12ba58a2012-07-27 15:08:15 +01001836
Joe Thornber24347e92013-05-10 14:37:19 +01001837int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1838{
1839 int r = -EINVAL;
1840
1841 down_write(&pmd->root_lock);
1842 if (!pmd->fail_io)
1843 r = __resize_space_map(pmd->metadata_sm, new_count);
1844 up_write(&pmd->root_lock);
1845
1846 return r;
1847}
1848
Joe Thornber12ba58a2012-07-27 15:08:15 +01001849void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd)
1850{
1851 down_write(&pmd->root_lock);
Joe Thornber12ba58a2012-07-27 15:08:15 +01001852 dm_bm_set_read_only(pmd->bm);
1853 up_write(&pmd->root_lock);
1854}
Joe Thornberac8c3f32013-05-10 14:37:21 +01001855
Joe Thornber9b7aaa62013-12-04 16:58:19 -05001856void dm_pool_metadata_read_write(struct dm_pool_metadata *pmd)
1857{
1858 down_write(&pmd->root_lock);
Joe Thornber9b7aaa62013-12-04 16:58:19 -05001859 dm_bm_set_read_write(pmd->bm);
1860 up_write(&pmd->root_lock);
1861}
1862
Joe Thornberac8c3f32013-05-10 14:37:21 +01001863int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd,
1864 dm_block_t threshold,
1865 dm_sm_threshold_fn fn,
1866 void *context)
1867{
1868 int r;
1869
1870 down_write(&pmd->root_lock);
1871 r = dm_sm_register_threshold_callback(pmd->metadata_sm, threshold, fn, context);
1872 up_write(&pmd->root_lock);
1873
1874 return r;
1875}
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05001876
1877int dm_pool_metadata_set_needs_check(struct dm_pool_metadata *pmd)
1878{
1879 int r;
1880 struct dm_block *sblock;
1881 struct thin_disk_superblock *disk_super;
1882
1883 down_write(&pmd->root_lock);
1884 pmd->flags |= THIN_METADATA_NEEDS_CHECK_FLAG;
1885
1886 r = superblock_lock(pmd, &sblock);
1887 if (r) {
1888 DMERR("couldn't read superblock");
1889 goto out;
1890 }
1891
1892 disk_super = dm_block_data(sblock);
1893 disk_super->flags = cpu_to_le32(pmd->flags);
1894
1895 dm_bm_unlock(sblock);
1896out:
1897 up_write(&pmd->root_lock);
1898 return r;
1899}
1900
1901bool dm_pool_metadata_needs_check(struct dm_pool_metadata *pmd)
1902{
1903 bool needs_check;
1904
1905 down_read(&pmd->root_lock);
1906 needs_check = pmd->flags & THIN_METADATA_NEEDS_CHECK_FLAG;
1907 up_read(&pmd->root_lock);
1908
1909 return needs_check;
1910}
Joe Thornber8a01a6a2014-10-06 15:28:30 +01001911
1912void dm_pool_issue_prefetches(struct dm_pool_metadata *pmd)
1913{
1914 dm_tm_issue_prefetches(pmd->tm);
1915}