blob: fd63485b27e9f1ca2e391e635abf45aa91fa6412 [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 SECTOR_TO_BLOCK_SHIFT 3
81
Joe Thornber8c971172012-07-27 15:07:58 +010082/*
Dennis Yang490ae012017-12-12 18:21:40 +080083 * For btree insert:
Joe Thornber8c971172012-07-27 15:07:58 +010084 * 3 for btree insert +
85 * 2 for btree lookup used within space map
Dennis Yang490ae012017-12-12 18:21:40 +080086 * For btree remove:
87 * 2 for shadow spine +
88 * 4 for rebalance 3 child node
Joe Thornber8c971172012-07-27 15:07:58 +010089 */
Dennis Yang490ae012017-12-12 18:21:40 +080090#define THIN_MAX_CONCURRENT_LOCKS 6
Joe Thornber8c971172012-07-27 15:07:58 +010091
Joe Thornber991d9fa2011-10-31 20:21:18 +000092/* This should be plenty */
93#define SPACE_MAP_ROOT_SIZE 128
94
95/*
96 * Little endian on-disk superblock and device details.
97 */
98struct thin_disk_superblock {
99 __le32 csum; /* Checksum of superblock except for this field. */
100 __le32 flags;
101 __le64 blocknr; /* This block number, dm_block_t. */
102
103 __u8 uuid[16];
104 __le64 magic;
105 __le32 version;
106 __le32 time;
107
108 __le64 trans_id;
109
110 /*
111 * Root held by userspace transactions.
112 */
113 __le64 held_root;
114
115 __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
116 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
117
118 /*
119 * 2-level btree mapping (dev_id, (dev block, time)) -> data block
120 */
121 __le64 data_mapping_root;
122
123 /*
124 * Device detail root mapping dev_id -> device_details
125 */
126 __le64 device_details_root;
127
128 __le32 data_block_size; /* In 512-byte sectors. */
129
130 __le32 metadata_block_size; /* In 512-byte sectors. */
131 __le64 metadata_nr_blocks;
132
133 __le32 compat_flags;
134 __le32 compat_ro_flags;
135 __le32 incompat_flags;
136} __packed;
137
138struct disk_device_details {
139 __le64 mapped_blocks;
140 __le64 transaction_id; /* When created. */
141 __le32 creation_time;
142 __le32 snapshotted_time;
143} __packed;
144
145struct dm_pool_metadata {
146 struct hlist_node hash;
147
148 struct block_device *bdev;
149 struct dm_block_manager *bm;
150 struct dm_space_map *metadata_sm;
151 struct dm_space_map *data_sm;
152 struct dm_transaction_manager *tm;
153 struct dm_transaction_manager *nb_tm;
154
155 /*
156 * Two-level btree.
157 * First level holds thin_dev_t.
158 * Second level holds mappings.
159 */
160 struct dm_btree_info info;
161
162 /*
163 * Non-blocking version of the above.
164 */
165 struct dm_btree_info nb_info;
166
167 /*
168 * Just the top level for deleting whole devices.
169 */
170 struct dm_btree_info tl_info;
171
172 /*
173 * Just the bottom level for creating new devices.
174 */
175 struct dm_btree_info bl_info;
176
177 /*
178 * Describes the device details btree.
179 */
180 struct dm_btree_info details_info;
181
182 struct rw_semaphore root_lock;
183 uint32_t time;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000184 dm_block_t root;
185 dm_block_t details_root;
186 struct list_head thin_devices;
187 uint64_t trans_id;
188 unsigned long flags;
189 sector_t data_block_size;
Joe Thornberda105ed2012-07-27 15:08:15 +0100190
191 /*
Joe Thornber3ab91822018-09-10 16:50:09 +0100192 * We reserve a section of the metadata for commit overhead.
193 * All reported space does *not* include this.
194 */
195 dm_block_t metadata_reserve;
196
197 /*
Joe Thornberda105ed2012-07-27 15:08:15 +0100198 * Set if a transaction has to be aborted but the attempt to roll back
199 * to the previous (good) transaction failed. The only pool metadata
200 * operation possible in this state is the closing of the device.
201 */
202 bool fail_io:1;
Joe Thornber5a320832014-03-27 14:13:23 +0000203
204 /*
205 * Reading the space map roots can fail, so we read it into these
206 * buffers before the superblock is locked and updated.
207 */
208 __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
209 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
Joe Thornber991d9fa2011-10-31 20:21:18 +0000210};
211
212struct dm_thin_device {
213 struct list_head list;
214 struct dm_pool_metadata *pmd;
215 dm_thin_id id;
216
217 int open_count;
Joe Thornberda105ed2012-07-27 15:08:15 +0100218 bool changed:1;
219 bool aborted_with_changes:1;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000220 uint64_t mapped_blocks;
221 uint64_t transaction_id;
222 uint32_t creation_time;
223 uint32_t snapshotted_time;
224};
225
226/*----------------------------------------------------------------
227 * superblock validator
228 *--------------------------------------------------------------*/
229
230#define SUPERBLOCK_CSUM_XOR 160774
231
232static void sb_prepare_for_write(struct dm_block_validator *v,
233 struct dm_block *b,
234 size_t block_size)
235{
236 struct thin_disk_superblock *disk_super = dm_block_data(b);
237
238 disk_super->blocknr = cpu_to_le64(dm_block_location(b));
239 disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
240 block_size - sizeof(__le32),
241 SUPERBLOCK_CSUM_XOR));
242}
243
244static int sb_check(struct dm_block_validator *v,
245 struct dm_block *b,
246 size_t block_size)
247{
248 struct thin_disk_superblock *disk_super = dm_block_data(b);
249 __le32 csum_le;
250
251 if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
252 DMERR("sb_check failed: blocknr %llu: "
253 "wanted %llu", le64_to_cpu(disk_super->blocknr),
254 (unsigned long long)dm_block_location(b));
255 return -ENOTBLK;
256 }
257
258 if (le64_to_cpu(disk_super->magic) != THIN_SUPERBLOCK_MAGIC) {
259 DMERR("sb_check failed: magic %llu: "
260 "wanted %llu", le64_to_cpu(disk_super->magic),
261 (unsigned long long)THIN_SUPERBLOCK_MAGIC);
262 return -EILSEQ;
263 }
264
265 csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
266 block_size - sizeof(__le32),
267 SUPERBLOCK_CSUM_XOR));
268 if (csum_le != disk_super->csum) {
269 DMERR("sb_check failed: csum %u: wanted %u",
270 le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
271 return -EILSEQ;
272 }
273
274 return 0;
275}
276
277static struct dm_block_validator sb_validator = {
278 .name = "superblock",
279 .prepare_for_write = sb_prepare_for_write,
280 .check = sb_check
281};
282
283/*----------------------------------------------------------------
284 * Methods for the btree value types
285 *--------------------------------------------------------------*/
286
287static uint64_t pack_block_time(dm_block_t b, uint32_t t)
288{
289 return (b << 24) | t;
290}
291
292static void unpack_block_time(uint64_t v, dm_block_t *b, uint32_t *t)
293{
294 *b = v >> 24;
295 *t = v & ((1 << 24) - 1);
296}
297
Mike Snitzer018cede2013-03-01 22:45:47 +0000298static void data_block_inc(void *context, const void *value_le)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000299{
300 struct dm_space_map *sm = context;
301 __le64 v_le;
302 uint64_t b;
303 uint32_t t;
304
305 memcpy(&v_le, value_le, sizeof(v_le));
306 unpack_block_time(le64_to_cpu(v_le), &b, &t);
307 dm_sm_inc_block(sm, b);
308}
309
Mike Snitzer018cede2013-03-01 22:45:47 +0000310static void data_block_dec(void *context, const void *value_le)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000311{
312 struct dm_space_map *sm = context;
313 __le64 v_le;
314 uint64_t b;
315 uint32_t t;
316
317 memcpy(&v_le, value_le, sizeof(v_le));
318 unpack_block_time(le64_to_cpu(v_le), &b, &t);
319 dm_sm_dec_block(sm, b);
320}
321
Mike Snitzer018cede2013-03-01 22:45:47 +0000322static int data_block_equal(void *context, const void *value1_le, const void *value2_le)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000323{
324 __le64 v1_le, v2_le;
325 uint64_t b1, b2;
326 uint32_t t;
327
328 memcpy(&v1_le, value1_le, sizeof(v1_le));
329 memcpy(&v2_le, value2_le, sizeof(v2_le));
330 unpack_block_time(le64_to_cpu(v1_le), &b1, &t);
331 unpack_block_time(le64_to_cpu(v2_le), &b2, &t);
332
333 return b1 == b2;
334}
335
Mike Snitzer018cede2013-03-01 22:45:47 +0000336static void subtree_inc(void *context, const void *value)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000337{
338 struct dm_btree_info *info = context;
339 __le64 root_le;
340 uint64_t root;
341
342 memcpy(&root_le, value, sizeof(root_le));
343 root = le64_to_cpu(root_le);
344 dm_tm_inc(info->tm, root);
345}
346
Mike Snitzer018cede2013-03-01 22:45:47 +0000347static void subtree_dec(void *context, const void *value)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000348{
349 struct dm_btree_info *info = context;
350 __le64 root_le;
351 uint64_t root;
352
353 memcpy(&root_le, value, sizeof(root_le));
354 root = le64_to_cpu(root_le);
355 if (dm_btree_del(info, root))
Mike Snitzer29f929b2016-01-21 14:04:04 -0500356 DMERR("btree delete failed");
Joe Thornber991d9fa2011-10-31 20:21:18 +0000357}
358
Mike Snitzer018cede2013-03-01 22:45:47 +0000359static int subtree_equal(void *context, const void *value1_le, const void *value2_le)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000360{
361 __le64 v1_le, v2_le;
362 memcpy(&v1_le, value1_le, sizeof(v1_le));
363 memcpy(&v2_le, value2_le, sizeof(v2_le));
364
365 return v1_le == v2_le;
366}
367
368/*----------------------------------------------------------------*/
369
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -0400370static inline void __pmd_write_lock(struct dm_pool_metadata *pmd)
371 __acquires(pmd->root_lock)
372{
373 down_write(&pmd->root_lock);
374}
375#define pmd_write_lock_in_core(pmd) __pmd_write_lock((pmd))
376
377static inline void pmd_write_lock(struct dm_pool_metadata *pmd)
378{
379 __pmd_write_lock(pmd);
380}
381
382static inline void pmd_write_unlock(struct dm_pool_metadata *pmd)
383 __releases(pmd->root_lock)
384{
385 up_write(&pmd->root_lock);
386}
387
388/*----------------------------------------------------------------*/
389
Joe Thornber25971192012-07-27 15:08:09 +0100390static int superblock_lock_zero(struct dm_pool_metadata *pmd,
391 struct dm_block **sblock)
392{
393 return dm_bm_write_lock_zero(pmd->bm, THIN_SUPERBLOCK_LOCATION,
394 &sb_validator, sblock);
395}
396
397static int superblock_lock(struct dm_pool_metadata *pmd,
398 struct dm_block **sblock)
399{
400 return dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
401 &sb_validator, sblock);
402}
403
Joe Thornber332627d2012-07-27 15:08:10 +0100404static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000405{
406 int r;
407 unsigned i;
408 struct dm_block *b;
409 __le64 *data_le, zero = cpu_to_le64(0);
410 unsigned block_size = dm_bm_block_size(bm) / sizeof(__le64);
411
412 /*
413 * We can't use a validator here - it may be all zeroes.
414 */
415 r = dm_bm_read_lock(bm, THIN_SUPERBLOCK_LOCATION, NULL, &b);
416 if (r)
417 return r;
418
419 data_le = dm_block_data(b);
420 *result = 1;
421 for (i = 0; i < block_size; i++) {
422 if (data_le[i] != zero) {
423 *result = 0;
424 break;
425 }
426 }
427
Mikulas Patocka4c7da062015-10-22 16:46:59 -0400428 dm_bm_unlock(b);
429
430 return 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000431}
432
Joe Thornber41675ae2012-07-27 15:08:08 +0100433static void __setup_btree_details(struct dm_pool_metadata *pmd)
434{
435 pmd->info.tm = pmd->tm;
436 pmd->info.levels = 2;
437 pmd->info.value_type.context = pmd->data_sm;
438 pmd->info.value_type.size = sizeof(__le64);
439 pmd->info.value_type.inc = data_block_inc;
440 pmd->info.value_type.dec = data_block_dec;
441 pmd->info.value_type.equal = data_block_equal;
442
443 memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info));
444 pmd->nb_info.tm = pmd->nb_tm;
445
446 pmd->tl_info.tm = pmd->tm;
447 pmd->tl_info.levels = 1;
Joe Thornbere3cbf942012-12-21 20:23:32 +0000448 pmd->tl_info.value_type.context = &pmd->bl_info;
Joe Thornber41675ae2012-07-27 15:08:08 +0100449 pmd->tl_info.value_type.size = sizeof(__le64);
450 pmd->tl_info.value_type.inc = subtree_inc;
451 pmd->tl_info.value_type.dec = subtree_dec;
452 pmd->tl_info.value_type.equal = subtree_equal;
453
454 pmd->bl_info.tm = pmd->tm;
455 pmd->bl_info.levels = 1;
456 pmd->bl_info.value_type.context = pmd->data_sm;
457 pmd->bl_info.value_type.size = sizeof(__le64);
458 pmd->bl_info.value_type.inc = data_block_inc;
459 pmd->bl_info.value_type.dec = data_block_dec;
460 pmd->bl_info.value_type.equal = data_block_equal;
461
462 pmd->details_info.tm = pmd->tm;
463 pmd->details_info.levels = 1;
464 pmd->details_info.value_type.context = NULL;
465 pmd->details_info.value_type.size = sizeof(struct disk_device_details);
466 pmd->details_info.value_type.inc = NULL;
467 pmd->details_info.value_type.dec = NULL;
468 pmd->details_info.value_type.equal = NULL;
469}
470
Joe Thornber5a320832014-03-27 14:13:23 +0000471static int save_sm_roots(struct dm_pool_metadata *pmd)
472{
473 int r;
474 size_t len;
475
476 r = dm_sm_root_size(pmd->metadata_sm, &len);
477 if (r < 0)
478 return r;
479
480 r = dm_sm_copy_root(pmd->metadata_sm, &pmd->metadata_space_map_root, len);
481 if (r < 0)
482 return r;
483
484 r = dm_sm_root_size(pmd->data_sm, &len);
485 if (r < 0)
486 return r;
487
488 return dm_sm_copy_root(pmd->data_sm, &pmd->data_space_map_root, len);
489}
490
491static void copy_sm_roots(struct dm_pool_metadata *pmd,
492 struct thin_disk_superblock *disk)
493{
494 memcpy(&disk->metadata_space_map_root,
495 &pmd->metadata_space_map_root,
496 sizeof(pmd->metadata_space_map_root));
497
498 memcpy(&disk->data_space_map_root,
499 &pmd->data_space_map_root,
500 sizeof(pmd->data_space_map_root));
501}
502
Joe Thornber9cb66532012-07-27 15:08:10 +0100503static int __write_initial_superblock(struct dm_pool_metadata *pmd)
504{
505 int r;
506 struct dm_block *sblock;
507 struct thin_disk_superblock *disk_super;
508 sector_t bdev_size = i_size_read(pmd->bdev->bd_inode) >> SECTOR_SHIFT;
509
510 if (bdev_size > THIN_METADATA_MAX_SECTORS)
511 bdev_size = THIN_METADATA_MAX_SECTORS;
512
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100513 r = dm_sm_commit(pmd->data_sm);
514 if (r < 0)
515 return r;
516
Joe Thornber91bcdb92017-05-15 09:43:05 -0400517 r = dm_tm_pre_commit(pmd->tm);
Joe Thornber5a320832014-03-27 14:13:23 +0000518 if (r < 0)
519 return r;
520
Joe Thornber91bcdb92017-05-15 09:43:05 -0400521 r = save_sm_roots(pmd);
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100522 if (r < 0)
523 return r;
524
Joe Thornber9cb66532012-07-27 15:08:10 +0100525 r = superblock_lock_zero(pmd, &sblock);
526 if (r)
527 return r;
528
529 disk_super = dm_block_data(sblock);
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100530 disk_super->flags = 0;
Joe Thornber583ceee2012-07-27 15:08:11 +0100531 memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
Joe Thornber9cb66532012-07-27 15:08:10 +0100532 disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
533 disk_super->version = cpu_to_le32(THIN_VERSION);
534 disk_super->time = 0;
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100535 disk_super->trans_id = 0;
536 disk_super->held_root = 0;
537
Joe Thornber5a320832014-03-27 14:13:23 +0000538 copy_sm_roots(pmd, disk_super);
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100539
540 disk_super->data_mapping_root = cpu_to_le64(pmd->root);
541 disk_super->device_details_root = cpu_to_le64(pmd->details_root);
Mike Snitzer7d489352014-02-12 23:58:15 -0500542 disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE);
Joe Thornber9cb66532012-07-27 15:08:10 +0100543 disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
544 disk_super->data_block_size = cpu_to_le32(pmd->data_block_size);
545
Joe Thornber270938b2012-07-27 15:08:11 +0100546 return dm_tm_commit(pmd->tm, sblock);
Joe Thornber9cb66532012-07-27 15:08:10 +0100547}
548
Joe Thornbera97e5e62012-07-27 15:08:12 +0100549static int __format_metadata(struct dm_pool_metadata *pmd)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000550{
551 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000552
Joe Thornbere4d22052012-07-27 15:08:12 +0100553 r = dm_tm_create_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
554 &pmd->tm, &pmd->metadata_sm);
555 if (r < 0) {
556 DMERR("tm_create_with_sm failed");
557 return r;
558 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000559
Joe Thornbera97e5e62012-07-27 15:08:12 +0100560 pmd->data_sm = dm_sm_disk_create(pmd->tm, 0);
Joe Thornbere4d22052012-07-27 15:08:12 +0100561 if (IS_ERR(pmd->data_sm)) {
562 DMERR("sm_disk_create failed");
563 r = PTR_ERR(pmd->data_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100564 goto bad_cleanup_tm;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000565 }
566
Joe Thornberd6332812012-07-27 15:08:12 +0100567 pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000568 if (!pmd->nb_tm) {
Joe Thornber0fa5b172012-07-27 15:08:14 +0100569 DMERR("could not create non-blocking clone tm");
Joe Thornber991d9fa2011-10-31 20:21:18 +0000570 r = -ENOMEM;
Joe Thornber0fa5b172012-07-27 15:08:14 +0100571 goto bad_cleanup_data_sm;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000572 }
573
Joe Thornber41675ae2012-07-27 15:08:08 +0100574 __setup_btree_details(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000575
Joe Thornber9cb66532012-07-27 15:08:10 +0100576 r = dm_btree_empty(&pmd->info, &pmd->root);
577 if (r < 0)
Joe Thornber0fa5b172012-07-27 15:08:14 +0100578 goto bad_cleanup_nb_tm;
Joe Thornber9cb66532012-07-27 15:08:10 +0100579
580 r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
581 if (r < 0) {
582 DMERR("couldn't create devices root");
Joe Thornber0fa5b172012-07-27 15:08:14 +0100583 goto bad_cleanup_nb_tm;
Joe Thornber9cb66532012-07-27 15:08:10 +0100584 }
585
586 r = __write_initial_superblock(pmd);
587 if (r)
Joe Thornber0fa5b172012-07-27 15:08:14 +0100588 goto bad_cleanup_nb_tm;
Joe Thornber9cb66532012-07-27 15:08:10 +0100589
Joe Thornber991d9fa2011-10-31 20:21:18 +0000590 return 0;
591
Joe Thornber0fa5b172012-07-27 15:08:14 +0100592bad_cleanup_nb_tm:
593 dm_tm_destroy(pmd->nb_tm);
594bad_cleanup_data_sm:
Joe Thornberd6332812012-07-27 15:08:12 +0100595 dm_sm_destroy(pmd->data_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100596bad_cleanup_tm:
Joe Thornberd6332812012-07-27 15:08:12 +0100597 dm_tm_destroy(pmd->tm);
598 dm_sm_destroy(pmd->metadata_sm);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000599
600 return r;
601}
602
Mike Snitzerd73ec522012-07-27 15:08:13 +0100603static int __check_incompat_features(struct thin_disk_superblock *disk_super,
604 struct dm_pool_metadata *pmd)
605{
606 uint32_t features;
607
608 features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP;
609 if (features) {
610 DMERR("could not access metadata due to unsupported optional features (%lx).",
611 (unsigned long)features);
612 return -EINVAL;
613 }
614
615 /*
616 * Check for read-only metadata to skip the following RDWR checks.
617 */
618 if (get_disk_ro(pmd->bdev->bd_disk))
619 return 0;
620
621 features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
622 if (features) {
623 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
624 (unsigned long)features);
625 return -EINVAL;
626 }
627
628 return 0;
629}
630
Joe Thornbere4d22052012-07-27 15:08:12 +0100631static int __open_metadata(struct dm_pool_metadata *pmd)
632{
633 int r;
634 struct dm_block *sblock;
635 struct thin_disk_superblock *disk_super;
636
637 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
638 &sb_validator, &sblock);
639 if (r < 0) {
640 DMERR("couldn't read superblock");
641 return r;
642 }
643
644 disk_super = dm_block_data(sblock);
Mike Snitzerd73ec522012-07-27 15:08:13 +0100645
Mike Snitzer9aec8622014-07-14 16:35:54 -0400646 /* Verify the data block size hasn't changed */
647 if (le32_to_cpu(disk_super->data_block_size) != pmd->data_block_size) {
648 DMERR("changing the data block size (from %u to %llu) is not supported",
649 le32_to_cpu(disk_super->data_block_size),
650 (unsigned long long)pmd->data_block_size);
651 r = -EINVAL;
652 goto bad_unlock_sblock;
653 }
654
Mike Snitzerd73ec522012-07-27 15:08:13 +0100655 r = __check_incompat_features(disk_super, pmd);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100656 if (r < 0)
657 goto bad_unlock_sblock;
Mike Snitzerd73ec522012-07-27 15:08:13 +0100658
Joe Thornbere4d22052012-07-27 15:08:12 +0100659 r = dm_tm_open_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
660 disk_super->metadata_space_map_root,
661 sizeof(disk_super->metadata_space_map_root),
662 &pmd->tm, &pmd->metadata_sm);
663 if (r < 0) {
664 DMERR("tm_open_with_sm failed");
Joe Thornber0fa5b172012-07-27 15:08:14 +0100665 goto bad_unlock_sblock;
Joe Thornbere4d22052012-07-27 15:08:12 +0100666 }
667
668 pmd->data_sm = dm_sm_disk_open(pmd->tm, disk_super->data_space_map_root,
669 sizeof(disk_super->data_space_map_root));
670 if (IS_ERR(pmd->data_sm)) {
671 DMERR("sm_disk_open failed");
Joe Thornbere4d22052012-07-27 15:08:12 +0100672 r = PTR_ERR(pmd->data_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100673 goto bad_cleanup_tm;
Joe Thornbere4d22052012-07-27 15:08:12 +0100674 }
675
Joe Thornbere4d22052012-07-27 15:08:12 +0100676 pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
677 if (!pmd->nb_tm) {
Joe Thornber0fa5b172012-07-27 15:08:14 +0100678 DMERR("could not create non-blocking clone tm");
Joe Thornbere4d22052012-07-27 15:08:12 +0100679 r = -ENOMEM;
Joe Thornber0fa5b172012-07-27 15:08:14 +0100680 goto bad_cleanup_data_sm;
Joe Thornbere4d22052012-07-27 15:08:12 +0100681 }
682
683 __setup_btree_details(pmd);
Mikulas Patocka4c7da062015-10-22 16:46:59 -0400684 dm_bm_unlock(sblock);
685
686 return 0;
Joe Thornbere4d22052012-07-27 15:08:12 +0100687
Joe Thornber0fa5b172012-07-27 15:08:14 +0100688bad_cleanup_data_sm:
Joe Thornbere4d22052012-07-27 15:08:12 +0100689 dm_sm_destroy(pmd->data_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100690bad_cleanup_tm:
Joe Thornbere4d22052012-07-27 15:08:12 +0100691 dm_tm_destroy(pmd->tm);
692 dm_sm_destroy(pmd->metadata_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100693bad_unlock_sblock:
694 dm_bm_unlock(sblock);
Joe Thornbere4d22052012-07-27 15:08:12 +0100695
696 return r;
697}
698
Joe Thornber66b1edc2012-07-27 15:08:14 +0100699static int __open_or_format_metadata(struct dm_pool_metadata *pmd, bool format_device)
Joe Thornbere4d22052012-07-27 15:08:12 +0100700{
Joe Thornber8801e062012-07-27 15:08:13 +0100701 int r, unformatted;
Joe Thornber237074c2012-07-27 15:08:13 +0100702
Joe Thornber8801e062012-07-27 15:08:13 +0100703 r = __superblock_all_zeroes(pmd->bm, &unformatted);
Joe Thornber237074c2012-07-27 15:08:13 +0100704 if (r)
705 return r;
706
Joe Thornber8801e062012-07-27 15:08:13 +0100707 if (unformatted)
Joe Thornber66b1edc2012-07-27 15:08:14 +0100708 return format_device ? __format_metadata(pmd) : -EPERM;
709
710 return __open_metadata(pmd);
Joe Thornbere4d22052012-07-27 15:08:12 +0100711}
712
Joe Thornber66b1edc2012-07-27 15:08:14 +0100713static int __create_persistent_data_objects(struct dm_pool_metadata *pmd, bool format_device)
Joe Thornber332627d2012-07-27 15:08:10 +0100714{
715 int r;
716
Mike Snitzer7d489352014-02-12 23:58:15 -0500717 pmd->bm = dm_block_manager_create(pmd->bdev, THIN_METADATA_BLOCK_SIZE << SECTOR_SHIFT,
Joe Thornber332627d2012-07-27 15:08:10 +0100718 THIN_MAX_CONCURRENT_LOCKS);
719 if (IS_ERR(pmd->bm)) {
720 DMERR("could not create block manager");
721 return PTR_ERR(pmd->bm);
722 }
723
Joe Thornber66b1edc2012-07-27 15:08:14 +0100724 r = __open_or_format_metadata(pmd, format_device);
Joe Thornber332627d2012-07-27 15:08:10 +0100725 if (r)
726 dm_block_manager_destroy(pmd->bm);
727
728 return r;
729}
730
Joe Thornberf9dd9352012-07-27 15:08:10 +0100731static void __destroy_persistent_data_objects(struct dm_pool_metadata *pmd)
732{
733 dm_sm_destroy(pmd->data_sm);
734 dm_sm_destroy(pmd->metadata_sm);
735 dm_tm_destroy(pmd->nb_tm);
736 dm_tm_destroy(pmd->tm);
737 dm_block_manager_destroy(pmd->bm);
738}
739
Joe Thornber991d9fa2011-10-31 20:21:18 +0000740static int __begin_transaction(struct dm_pool_metadata *pmd)
741{
742 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000743 struct thin_disk_superblock *disk_super;
744 struct dm_block *sblock;
745
746 /*
Joe Thornber991d9fa2011-10-31 20:21:18 +0000747 * We re-read the superblock every time. Shouldn't need to do this
748 * really.
749 */
750 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
751 &sb_validator, &sblock);
752 if (r)
753 return r;
754
755 disk_super = dm_block_data(sblock);
756 pmd->time = le32_to_cpu(disk_super->time);
757 pmd->root = le64_to_cpu(disk_super->data_mapping_root);
758 pmd->details_root = le64_to_cpu(disk_super->device_details_root);
759 pmd->trans_id = le64_to_cpu(disk_super->trans_id);
760 pmd->flags = le32_to_cpu(disk_super->flags);
761 pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
762
Joe Thornber991d9fa2011-10-31 20:21:18 +0000763 dm_bm_unlock(sblock);
Mike Snitzerd73ec522012-07-27 15:08:13 +0100764 return 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000765}
766
767static int __write_changed_details(struct dm_pool_metadata *pmd)
768{
769 int r;
770 struct dm_thin_device *td, *tmp;
771 struct disk_device_details details;
772 uint64_t key;
773
774 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
775 if (!td->changed)
776 continue;
777
778 key = td->id;
779
780 details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
781 details.transaction_id = cpu_to_le64(td->transaction_id);
782 details.creation_time = cpu_to_le32(td->creation_time);
783 details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
784 __dm_bless_for_disk(&details);
785
786 r = dm_btree_insert(&pmd->details_info, pmd->details_root,
787 &key, &details, &pmd->details_root);
788 if (r)
789 return r;
790
791 if (td->open_count)
792 td->changed = 0;
793 else {
794 list_del(&td->list);
795 kfree(td);
796 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000797 }
798
799 return 0;
800}
801
802static int __commit_transaction(struct dm_pool_metadata *pmd)
803{
Joe Thornber991d9fa2011-10-31 20:21:18 +0000804 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000805 struct thin_disk_superblock *disk_super;
806 struct dm_block *sblock;
807
808 /*
809 * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
810 */
811 BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
812
813 r = __write_changed_details(pmd);
814 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100815 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000816
Joe Thornber991d9fa2011-10-31 20:21:18 +0000817 r = dm_sm_commit(pmd->data_sm);
818 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100819 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000820
821 r = dm_tm_pre_commit(pmd->tm);
822 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100823 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000824
Joe Thornber5a320832014-03-27 14:13:23 +0000825 r = save_sm_roots(pmd);
826 if (r < 0)
827 return r;
828
Joe Thornber25971192012-07-27 15:08:09 +0100829 r = superblock_lock(pmd, &sblock);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000830 if (r)
Joe Thornberd973ac12012-07-27 15:07:58 +0100831 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000832
833 disk_super = dm_block_data(sblock);
834 disk_super->time = cpu_to_le32(pmd->time);
835 disk_super->data_mapping_root = cpu_to_le64(pmd->root);
836 disk_super->device_details_root = cpu_to_le64(pmd->details_root);
837 disk_super->trans_id = cpu_to_le64(pmd->trans_id);
838 disk_super->flags = cpu_to_le32(pmd->flags);
839
Joe Thornber5a320832014-03-27 14:13:23 +0000840 copy_sm_roots(pmd, disk_super);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000841
Joe Thornbereb04cf62012-07-27 15:08:08 +0100842 return dm_tm_commit(pmd->tm, sblock);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000843}
844
Joe Thornber3ab91822018-09-10 16:50:09 +0100845static void __set_metadata_reserve(struct dm_pool_metadata *pmd)
846{
847 int r;
848 dm_block_t total;
849 dm_block_t max_blocks = 4096; /* 16M */
850
851 r = dm_sm_get_nr_blocks(pmd->metadata_sm, &total);
852 if (r) {
853 DMERR("could not get size of metadata device");
854 pmd->metadata_reserve = max_blocks;
Mike Snitzer013ad042018-09-13 21:16:20 -0400855 } else
856 pmd->metadata_reserve = min(max_blocks, div_u64(total, 10));
Joe Thornber3ab91822018-09-10 16:50:09 +0100857}
858
Joe Thornber991d9fa2011-10-31 20:21:18 +0000859struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
Joe Thornber66b1edc2012-07-27 15:08:14 +0100860 sector_t data_block_size,
861 bool format_device)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000862{
863 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000864 struct dm_pool_metadata *pmd;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000865
866 pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
867 if (!pmd) {
868 DMERR("could not allocate metadata struct");
869 return ERR_PTR(-ENOMEM);
870 }
871
Joe Thornber6a0ebd32012-07-27 15:08:10 +0100872 init_rwsem(&pmd->root_lock);
873 pmd->time = 0;
874 INIT_LIST_HEAD(&pmd->thin_devices);
Joe Thornberda105ed2012-07-27 15:08:15 +0100875 pmd->fail_io = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000876 pmd->bdev = bdev;
Joe Thornber9cb66532012-07-27 15:08:10 +0100877 pmd->data_block_size = data_block_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000878
Joe Thornber66b1edc2012-07-27 15:08:14 +0100879 r = __create_persistent_data_objects(pmd, format_device);
Joe Thornber332627d2012-07-27 15:08:10 +0100880 if (r) {
881 kfree(pmd);
882 return ERR_PTR(r);
883 }
884
Joe Thornber270938b2012-07-27 15:08:11 +0100885 r = __begin_transaction(pmd);
886 if (r < 0) {
887 if (dm_pool_metadata_close(pmd) < 0)
888 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
889 return ERR_PTR(r);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000890 }
891
Joe Thornber3ab91822018-09-10 16:50:09 +0100892 __set_metadata_reserve(pmd);
893
Joe Thornber991d9fa2011-10-31 20:21:18 +0000894 return pmd;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000895}
896
897int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
898{
899 int r;
900 unsigned open_devices = 0;
901 struct dm_thin_device *td, *tmp;
902
903 down_read(&pmd->root_lock);
904 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
905 if (td->open_count)
906 open_devices++;
907 else {
908 list_del(&td->list);
909 kfree(td);
910 }
911 }
912 up_read(&pmd->root_lock);
913
914 if (open_devices) {
915 DMERR("attempt to close pmd when %u device(s) are still open",
916 open_devices);
917 return -EBUSY;
918 }
919
Mike Snitzer49f154c2015-04-23 15:06:27 -0400920 if (!dm_bm_is_read_only(pmd->bm) && !pmd->fail_io) {
Joe Thornber12ba58a2012-07-27 15:08:15 +0100921 r = __commit_transaction(pmd);
922 if (r < 0)
923 DMWARN("%s: __commit_transaction() failed, error = %d",
924 __func__, r);
925 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000926
Joe Thornberda105ed2012-07-27 15:08:15 +0100927 if (!pmd->fail_io)
928 __destroy_persistent_data_objects(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000929
Joe Thornberda105ed2012-07-27 15:08:15 +0100930 kfree(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000931 return 0;
932}
933
Mike Snitzer1f3db252012-03-07 19:09:41 +0000934/*
935 * __open_device: Returns @td corresponding to device with id @dev,
936 * creating it if @create is set and incrementing @td->open_count.
937 * On failure, @td is undefined.
938 */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000939static int __open_device(struct dm_pool_metadata *pmd,
940 dm_thin_id dev, int create,
941 struct dm_thin_device **td)
942{
943 int r, changed = 0;
944 struct dm_thin_device *td2;
945 uint64_t key = dev;
946 struct disk_device_details details_le;
947
948 /*
Mike Snitzer1f3db252012-03-07 19:09:41 +0000949 * If the device is already open, return it.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000950 */
951 list_for_each_entry(td2, &pmd->thin_devices, list)
952 if (td2->id == dev) {
Mike Snitzer1f3db252012-03-07 19:09:41 +0000953 /*
954 * May not create an already-open device.
955 */
956 if (create)
957 return -EEXIST;
958
Joe Thornber991d9fa2011-10-31 20:21:18 +0000959 td2->open_count++;
960 *td = td2;
961 return 0;
962 }
963
964 /*
965 * Check the device exists.
966 */
967 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
968 &key, &details_le);
969 if (r) {
970 if (r != -ENODATA || !create)
971 return r;
972
Mike Snitzer1f3db252012-03-07 19:09:41 +0000973 /*
974 * Create new device.
975 */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000976 changed = 1;
977 details_le.mapped_blocks = 0;
978 details_le.transaction_id = cpu_to_le64(pmd->trans_id);
979 details_le.creation_time = cpu_to_le32(pmd->time);
980 details_le.snapshotted_time = cpu_to_le32(pmd->time);
981 }
982
983 *td = kmalloc(sizeof(**td), GFP_NOIO);
984 if (!*td)
985 return -ENOMEM;
986
987 (*td)->pmd = pmd;
988 (*td)->id = dev;
989 (*td)->open_count = 1;
990 (*td)->changed = changed;
Joe Thornberda105ed2012-07-27 15:08:15 +0100991 (*td)->aborted_with_changes = false;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000992 (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
993 (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
994 (*td)->creation_time = le32_to_cpu(details_le.creation_time);
995 (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
996
997 list_add(&(*td)->list, &pmd->thin_devices);
998
999 return 0;
1000}
1001
1002static void __close_device(struct dm_thin_device *td)
1003{
1004 --td->open_count;
1005}
1006
1007static int __create_thin(struct dm_pool_metadata *pmd,
1008 dm_thin_id dev)
1009{
1010 int r;
1011 dm_block_t dev_root;
1012 uint64_t key = dev;
1013 struct disk_device_details details_le;
1014 struct dm_thin_device *td;
1015 __le64 value;
1016
1017 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
1018 &key, &details_le);
1019 if (!r)
1020 return -EEXIST;
1021
1022 /*
1023 * Create an empty btree for the mappings.
1024 */
1025 r = dm_btree_empty(&pmd->bl_info, &dev_root);
1026 if (r)
1027 return r;
1028
1029 /*
1030 * Insert it into the main mapping tree.
1031 */
1032 value = cpu_to_le64(dev_root);
1033 __dm_bless_for_disk(&value);
1034 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
1035 if (r) {
1036 dm_btree_del(&pmd->bl_info, dev_root);
1037 return r;
1038 }
1039
1040 r = __open_device(pmd, dev, 1, &td);
1041 if (r) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001042 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1043 dm_btree_del(&pmd->bl_info, dev_root);
1044 return r;
1045 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001046 __close_device(td);
1047
1048 return r;
1049}
1050
1051int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
1052{
Joe Thornberda105ed2012-07-27 15:08:15 +01001053 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001054
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001055 pmd_write_lock(pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001056 if (!pmd->fail_io)
1057 r = __create_thin(pmd, dev);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001058 pmd_write_unlock(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001059
1060 return r;
1061}
1062
1063static int __set_snapshot_details(struct dm_pool_metadata *pmd,
1064 struct dm_thin_device *snap,
1065 dm_thin_id origin, uint32_t time)
1066{
1067 int r;
1068 struct dm_thin_device *td;
1069
1070 r = __open_device(pmd, origin, 0, &td);
1071 if (r)
1072 return r;
1073
1074 td->changed = 1;
1075 td->snapshotted_time = time;
1076
1077 snap->mapped_blocks = td->mapped_blocks;
1078 snap->snapshotted_time = time;
1079 __close_device(td);
1080
1081 return 0;
1082}
1083
1084static int __create_snap(struct dm_pool_metadata *pmd,
1085 dm_thin_id dev, dm_thin_id origin)
1086{
1087 int r;
1088 dm_block_t origin_root;
1089 uint64_t key = origin, dev_key = dev;
1090 struct dm_thin_device *td;
1091 struct disk_device_details details_le;
1092 __le64 value;
1093
1094 /* check this device is unused */
1095 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
1096 &dev_key, &details_le);
1097 if (!r)
1098 return -EEXIST;
1099
1100 /* find the mapping tree for the origin */
1101 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
1102 if (r)
1103 return r;
1104 origin_root = le64_to_cpu(value);
1105
1106 /* clone the origin, an inc will do */
1107 dm_tm_inc(pmd->tm, origin_root);
1108
1109 /* insert into the main mapping tree */
1110 value = cpu_to_le64(origin_root);
1111 __dm_bless_for_disk(&value);
1112 key = dev;
1113 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
1114 if (r) {
1115 dm_tm_dec(pmd->tm, origin_root);
1116 return r;
1117 }
1118
1119 pmd->time++;
1120
1121 r = __open_device(pmd, dev, 1, &td);
1122 if (r)
1123 goto bad;
1124
1125 r = __set_snapshot_details(pmd, td, origin, pmd->time);
Mike Snitzer1f3db252012-03-07 19:09:41 +00001126 __close_device(td);
1127
Joe Thornber991d9fa2011-10-31 20:21:18 +00001128 if (r)
1129 goto bad;
1130
Joe Thornber991d9fa2011-10-31 20:21:18 +00001131 return 0;
1132
1133bad:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001134 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1135 dm_btree_remove(&pmd->details_info, pmd->details_root,
1136 &key, &pmd->details_root);
1137 return r;
1138}
1139
1140int dm_pool_create_snap(struct dm_pool_metadata *pmd,
1141 dm_thin_id dev,
1142 dm_thin_id origin)
1143{
Joe Thornberda105ed2012-07-27 15:08:15 +01001144 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001145
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001146 pmd_write_lock(pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001147 if (!pmd->fail_io)
1148 r = __create_snap(pmd, dev, origin);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001149 pmd_write_unlock(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001150
1151 return r;
1152}
1153
1154static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
1155{
1156 int r;
1157 uint64_t key = dev;
1158 struct dm_thin_device *td;
1159
1160 /* TODO: failure should mark the transaction invalid */
1161 r = __open_device(pmd, dev, 0, &td);
1162 if (r)
1163 return r;
1164
1165 if (td->open_count > 1) {
1166 __close_device(td);
1167 return -EBUSY;
1168 }
1169
1170 list_del(&td->list);
1171 kfree(td);
1172 r = dm_btree_remove(&pmd->details_info, pmd->details_root,
1173 &key, &pmd->details_root);
1174 if (r)
1175 return r;
1176
1177 r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1178 if (r)
1179 return r;
1180
Joe Thornber991d9fa2011-10-31 20:21:18 +00001181 return 0;
1182}
1183
1184int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
1185 dm_thin_id dev)
1186{
Joe Thornberda105ed2012-07-27 15:08:15 +01001187 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001188
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001189 pmd_write_lock(pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001190 if (!pmd->fail_io)
1191 r = __delete_device(pmd, dev);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001192 pmd_write_unlock(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001193
1194 return r;
1195}
1196
1197int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
1198 uint64_t current_id,
1199 uint64_t new_id)
1200{
Joe Thornberda105ed2012-07-27 15:08:15 +01001201 int r = -EINVAL;
1202
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001203 pmd_write_lock(pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001204
1205 if (pmd->fail_io)
1206 goto out;
1207
Joe Thornber991d9fa2011-10-31 20:21:18 +00001208 if (pmd->trans_id != current_id) {
Joe Thornber991d9fa2011-10-31 20:21:18 +00001209 DMERR("mismatched transaction id");
Joe Thornberda105ed2012-07-27 15:08:15 +01001210 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001211 }
1212
1213 pmd->trans_id = new_id;
Joe Thornberda105ed2012-07-27 15:08:15 +01001214 r = 0;
1215
1216out:
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001217 pmd_write_unlock(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001218
Joe Thornberda105ed2012-07-27 15:08:15 +01001219 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001220}
1221
1222int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
1223 uint64_t *result)
1224{
Joe Thornberda105ed2012-07-27 15:08:15 +01001225 int r = -EINVAL;
1226
Joe Thornber991d9fa2011-10-31 20:21:18 +00001227 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001228 if (!pmd->fail_io) {
1229 *result = pmd->trans_id;
1230 r = 0;
1231 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001232 up_read(&pmd->root_lock);
1233
Joe Thornberda105ed2012-07-27 15:08:15 +01001234 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001235}
1236
Joe Thornbercc8394d2012-06-03 00:30:01 +01001237static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
1238{
1239 int r, inc;
1240 struct thin_disk_superblock *disk_super;
1241 struct dm_block *copy, *sblock;
1242 dm_block_t held_root;
1243
1244 /*
Joe Thornber49e99fc2015-12-09 16:23:24 +00001245 * We commit to ensure the btree roots which we increment in a
1246 * moment are up to date.
1247 */
Mike Snitzera1ed4d92019-04-15 16:40:08 -04001248 r = __commit_transaction(pmd);
1249 if (r < 0) {
1250 DMWARN("%s: __commit_transaction() failed, error = %d",
1251 __func__, r);
1252 return r;
1253 }
Joe Thornber49e99fc2015-12-09 16:23:24 +00001254
1255 /*
Joe Thornbercc8394d2012-06-03 00:30:01 +01001256 * Copy the superblock.
1257 */
1258 dm_sm_inc_block(pmd->metadata_sm, THIN_SUPERBLOCK_LOCATION);
1259 r = dm_tm_shadow_block(pmd->tm, THIN_SUPERBLOCK_LOCATION,
1260 &sb_validator, &copy, &inc);
1261 if (r)
1262 return r;
1263
1264 BUG_ON(!inc);
1265
1266 held_root = dm_block_location(copy);
1267 disk_super = dm_block_data(copy);
1268
1269 if (le64_to_cpu(disk_super->held_root)) {
1270 DMWARN("Pool metadata snapshot already exists: release this before taking another.");
1271
1272 dm_tm_dec(pmd->tm, held_root);
1273 dm_tm_unlock(pmd->tm, copy);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001274 return -EBUSY;
1275 }
1276
1277 /*
1278 * Wipe the spacemap since we're not publishing this.
1279 */
1280 memset(&disk_super->data_space_map_root, 0,
1281 sizeof(disk_super->data_space_map_root));
1282 memset(&disk_super->metadata_space_map_root, 0,
1283 sizeof(disk_super->metadata_space_map_root));
1284
1285 /*
1286 * Increment the data structures that need to be preserved.
1287 */
1288 dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->data_mapping_root));
1289 dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->device_details_root));
1290 dm_tm_unlock(pmd->tm, copy);
1291
1292 /*
1293 * Write the held root into the superblock.
1294 */
Joe Thornber25971192012-07-27 15:08:09 +01001295 r = superblock_lock(pmd, &sblock);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001296 if (r) {
1297 dm_tm_dec(pmd->tm, held_root);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001298 return r;
1299 }
1300
1301 disk_super = dm_block_data(sblock);
1302 disk_super->held_root = cpu_to_le64(held_root);
1303 dm_bm_unlock(sblock);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001304 return 0;
1305}
1306
1307int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd)
1308{
Joe Thornberda105ed2012-07-27 15:08:15 +01001309 int r = -EINVAL;
Joe Thornbercc8394d2012-06-03 00:30:01 +01001310
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001311 pmd_write_lock(pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001312 if (!pmd->fail_io)
1313 r = __reserve_metadata_snap(pmd);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001314 pmd_write_unlock(pmd);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001315
1316 return r;
1317}
1318
1319static int __release_metadata_snap(struct dm_pool_metadata *pmd)
1320{
1321 int r;
1322 struct thin_disk_superblock *disk_super;
1323 struct dm_block *sblock, *copy;
1324 dm_block_t held_root;
1325
Joe Thornber25971192012-07-27 15:08:09 +01001326 r = superblock_lock(pmd, &sblock);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001327 if (r)
1328 return r;
1329
1330 disk_super = dm_block_data(sblock);
1331 held_root = le64_to_cpu(disk_super->held_root);
1332 disk_super->held_root = cpu_to_le64(0);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001333
1334 dm_bm_unlock(sblock);
1335
1336 if (!held_root) {
1337 DMWARN("No pool metadata snapshot found: nothing to release.");
1338 return -EINVAL;
1339 }
1340
1341 r = dm_tm_read_lock(pmd->tm, held_root, &sb_validator, &copy);
1342 if (r)
1343 return r;
1344
1345 disk_super = dm_block_data(copy);
Joe Thornber7f518ad2015-08-12 15:10:21 +01001346 dm_btree_del(&pmd->info, le64_to_cpu(disk_super->data_mapping_root));
1347 dm_btree_del(&pmd->details_info, le64_to_cpu(disk_super->device_details_root));
Joe Thornbercc8394d2012-06-03 00:30:01 +01001348 dm_sm_dec_block(pmd->metadata_sm, held_root);
1349
Mikulas Patocka4c7da062015-10-22 16:46:59 -04001350 dm_tm_unlock(pmd->tm, copy);
1351
1352 return 0;
Joe Thornbercc8394d2012-06-03 00:30:01 +01001353}
1354
1355int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd)
1356{
Joe Thornberda105ed2012-07-27 15:08:15 +01001357 int r = -EINVAL;
Joe Thornbercc8394d2012-06-03 00:30:01 +01001358
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001359 pmd_write_lock(pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001360 if (!pmd->fail_io)
1361 r = __release_metadata_snap(pmd);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001362 pmd_write_unlock(pmd);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001363
1364 return r;
1365}
1366
1367static int __get_metadata_snap(struct dm_pool_metadata *pmd,
1368 dm_block_t *result)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001369{
1370 int r;
1371 struct thin_disk_superblock *disk_super;
1372 struct dm_block *sblock;
1373
Joe Thornbercc8394d2012-06-03 00:30:01 +01001374 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
1375 &sb_validator, &sblock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001376 if (r)
1377 return r;
1378
1379 disk_super = dm_block_data(sblock);
1380 *result = le64_to_cpu(disk_super->held_root);
1381
Mikulas Patocka4c7da062015-10-22 16:46:59 -04001382 dm_bm_unlock(sblock);
1383
1384 return 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001385}
1386
Joe Thornbercc8394d2012-06-03 00:30:01 +01001387int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
1388 dm_block_t *result)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001389{
Joe Thornberda105ed2012-07-27 15:08:15 +01001390 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001391
1392 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001393 if (!pmd->fail_io)
1394 r = __get_metadata_snap(pmd, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001395 up_read(&pmd->root_lock);
1396
1397 return r;
1398}
1399
1400int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
1401 struct dm_thin_device **td)
1402{
Joe Thornberda105ed2012-07-27 15:08:15 +01001403 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001404
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001405 pmd_write_lock_in_core(pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001406 if (!pmd->fail_io)
1407 r = __open_device(pmd, dev, 0, td);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001408 pmd_write_unlock(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001409
1410 return r;
1411}
1412
1413int dm_pool_close_thin_device(struct dm_thin_device *td)
1414{
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001415 pmd_write_lock_in_core(td->pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001416 __close_device(td);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001417 pmd_write_unlock(td->pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001418
1419 return 0;
1420}
1421
1422dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
1423{
1424 return td->id;
1425}
1426
Joe Thornber19fa1a62013-12-17 12:09:40 -05001427/*
1428 * Check whether @time (of block creation) is older than @td's last snapshot.
1429 * If so then the associated block is shared with the last snapshot device.
1430 * Any block on a device created *after* the device last got snapshotted is
1431 * necessarily not shared.
1432 */
Mike Snitzer17b7d632012-07-27 15:07:57 +01001433static bool __snapshotted_since(struct dm_thin_device *td, uint32_t time)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001434{
1435 return td->snapshotted_time > time;
1436}
1437
Joe Thornber3d5f6732015-11-05 15:10:11 +00001438static void unpack_lookup_result(struct dm_thin_device *td, __le64 value,
1439 struct dm_thin_lookup_result *result)
1440{
1441 uint64_t block_time = 0;
1442 dm_block_t exception_block;
1443 uint32_t exception_time;
1444
1445 block_time = le64_to_cpu(value);
1446 unpack_block_time(block_time, &exception_block, &exception_time);
1447 result->block = exception_block;
1448 result->shared = __snapshotted_since(td, exception_time);
1449}
1450
Joe Thornber086fbbb2015-12-07 14:48:04 +00001451static int __find_block(struct dm_thin_device *td, dm_block_t block,
1452 int can_issue_io, struct dm_thin_lookup_result *result)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001453{
Joe Thornbere5cfc692014-10-06 15:24:55 +01001454 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001455 __le64 value;
1456 struct dm_pool_metadata *pmd = td->pmd;
1457 dm_block_t keys[2] = { td->id, block };
Joe Thornberda105ed2012-07-27 15:08:15 +01001458 struct dm_btree_info *info;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001459
Joe Thornbere5cfc692014-10-06 15:24:55 +01001460 if (can_issue_io) {
1461 info = &pmd->info;
1462 } else
1463 info = &pmd->nb_info;
Joe Thornberda105ed2012-07-27 15:08:15 +01001464
1465 r = dm_btree_lookup(info, pmd->root, keys, &value);
Joe Thornber3d5f6732015-11-05 15:10:11 +00001466 if (!r)
1467 unpack_lookup_result(td, value, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001468
Joe Thornber086fbbb2015-12-07 14:48:04 +00001469 return r;
1470}
1471
1472int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
1473 int can_issue_io, struct dm_thin_lookup_result *result)
1474{
1475 int r;
1476 struct dm_pool_metadata *pmd = td->pmd;
1477
1478 down_read(&pmd->root_lock);
1479 if (pmd->fail_io) {
1480 up_read(&pmd->root_lock);
1481 return -EINVAL;
1482 }
1483
1484 r = __find_block(td, block, can_issue_io, result);
1485
Joe Thornbere5cfc692014-10-06 15:24:55 +01001486 up_read(&pmd->root_lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001487 return r;
1488}
1489
Joe Thornber086fbbb2015-12-07 14:48:04 +00001490static int __find_next_mapped_block(struct dm_thin_device *td, dm_block_t block,
Joe Thornber3d5f6732015-11-05 15:10:11 +00001491 dm_block_t *vblock,
1492 struct dm_thin_lookup_result *result)
1493{
1494 int r;
1495 __le64 value;
1496 struct dm_pool_metadata *pmd = td->pmd;
1497 dm_block_t keys[2] = { td->id, block };
1498
Joe Thornber3d5f6732015-11-05 15:10:11 +00001499 r = dm_btree_lookup_next(&pmd->info, pmd->root, keys, vblock, &value);
1500 if (!r)
1501 unpack_lookup_result(td, value, result);
1502
Joe Thornber3d5f6732015-11-05 15:10:11 +00001503 return r;
1504}
1505
Joe Thornber086fbbb2015-12-07 14:48:04 +00001506static int __find_mapped_range(struct dm_thin_device *td,
1507 dm_block_t begin, dm_block_t end,
1508 dm_block_t *thin_begin, dm_block_t *thin_end,
1509 dm_block_t *pool_begin, bool *maybe_shared)
Joe Thornbera5d895a2015-04-16 12:47:21 +01001510{
1511 int r;
1512 dm_block_t pool_end;
1513 struct dm_thin_lookup_result lookup;
1514
1515 if (end < begin)
1516 return -ENODATA;
1517
Joe Thornber086fbbb2015-12-07 14:48:04 +00001518 r = __find_next_mapped_block(td, begin, &begin, &lookup);
Joe Thornber3d5f6732015-11-05 15:10:11 +00001519 if (r)
1520 return r;
Joe Thornbera5d895a2015-04-16 12:47:21 +01001521
Joe Thornber3d5f6732015-11-05 15:10:11 +00001522 if (begin >= end)
Joe Thornbera5d895a2015-04-16 12:47:21 +01001523 return -ENODATA;
1524
1525 *thin_begin = begin;
1526 *pool_begin = lookup.block;
1527 *maybe_shared = lookup.shared;
1528
1529 begin++;
1530 pool_end = *pool_begin + 1;
1531 while (begin != end) {
Joe Thornber086fbbb2015-12-07 14:48:04 +00001532 r = __find_block(td, begin, true, &lookup);
Joe Thornbera5d895a2015-04-16 12:47:21 +01001533 if (r) {
1534 if (r == -ENODATA)
1535 break;
1536 else
1537 return r;
1538 }
1539
1540 if ((lookup.block != pool_end) ||
1541 (lookup.shared != *maybe_shared))
1542 break;
1543
1544 pool_end++;
1545 begin++;
1546 }
1547
1548 *thin_end = begin;
1549 return 0;
1550}
1551
Joe Thornber086fbbb2015-12-07 14:48:04 +00001552int dm_thin_find_mapped_range(struct dm_thin_device *td,
1553 dm_block_t begin, dm_block_t end,
1554 dm_block_t *thin_begin, dm_block_t *thin_end,
1555 dm_block_t *pool_begin, bool *maybe_shared)
1556{
1557 int r = -EINVAL;
1558 struct dm_pool_metadata *pmd = td->pmd;
1559
1560 down_read(&pmd->root_lock);
1561 if (!pmd->fail_io) {
1562 r = __find_mapped_range(td, begin, end, thin_begin, thin_end,
1563 pool_begin, maybe_shared);
1564 }
1565 up_read(&pmd->root_lock);
1566
1567 return r;
1568}
1569
Joe Thornber991d9fa2011-10-31 20:21:18 +00001570static int __insert(struct dm_thin_device *td, dm_block_t block,
1571 dm_block_t data_block)
1572{
1573 int r, inserted;
1574 __le64 value;
1575 struct dm_pool_metadata *pmd = td->pmd;
1576 dm_block_t keys[2] = { td->id, block };
1577
Joe Thornber991d9fa2011-10-31 20:21:18 +00001578 value = cpu_to_le64(pack_block_time(data_block, pmd->time));
1579 __dm_bless_for_disk(&value);
1580
1581 r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
1582 &pmd->root, &inserted);
1583 if (r)
1584 return r;
1585
Joe Thornber40db5a52012-07-27 15:08:14 +01001586 td->changed = 1;
1587 if (inserted)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001588 td->mapped_blocks++;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001589
1590 return 0;
1591}
1592
1593int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
1594 dm_block_t data_block)
1595{
Joe Thornberda105ed2012-07-27 15:08:15 +01001596 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001597
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001598 pmd_write_lock(td->pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001599 if (!td->pmd->fail_io)
1600 r = __insert(td, block, data_block);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001601 pmd_write_unlock(td->pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001602
1603 return r;
1604}
1605
1606static int __remove(struct dm_thin_device *td, dm_block_t block)
1607{
1608 int r;
1609 struct dm_pool_metadata *pmd = td->pmd;
1610 dm_block_t keys[2] = { td->id, block };
1611
1612 r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
1613 if (r)
1614 return r;
1615
Joe Thornberaf63bcb2012-03-07 19:09:44 +00001616 td->mapped_blocks--;
1617 td->changed = 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001618
1619 return 0;
1620}
1621
Joe Thornber6550f072015-04-13 09:45:25 +01001622static int __remove_range(struct dm_thin_device *td, dm_block_t begin, dm_block_t end)
1623{
1624 int r;
Joe Thornber993ceab2015-12-02 12:24:39 +00001625 unsigned count, total_count = 0;
Joe Thornber6550f072015-04-13 09:45:25 +01001626 struct dm_pool_metadata *pmd = td->pmd;
1627 dm_block_t keys[1] = { td->id };
1628 __le64 value;
1629 dm_block_t mapping_root;
1630
1631 /*
1632 * Find the mapping tree
1633 */
1634 r = dm_btree_lookup(&pmd->tl_info, pmd->root, keys, &value);
1635 if (r)
1636 return r;
1637
1638 /*
1639 * Remove from the mapping tree, taking care to inc the
1640 * ref count so it doesn't get deleted.
1641 */
1642 mapping_root = le64_to_cpu(value);
1643 dm_tm_inc(pmd->tm, mapping_root);
1644 r = dm_btree_remove(&pmd->tl_info, pmd->root, keys, &pmd->root);
1645 if (r)
1646 return r;
1647
Joe Thornber993ceab2015-12-02 12:24:39 +00001648 /*
1649 * Remove leaves stops at the first unmapped entry, so we have to
1650 * loop round finding mapped ranges.
1651 */
1652 while (begin < end) {
1653 r = dm_btree_lookup_next(&pmd->bl_info, mapping_root, &begin, &begin, &value);
1654 if (r == -ENODATA)
1655 break;
Joe Thornber6550f072015-04-13 09:45:25 +01001656
Joe Thornber993ceab2015-12-02 12:24:39 +00001657 if (r)
1658 return r;
1659
1660 if (begin >= end)
1661 break;
1662
1663 r = dm_btree_remove_leaves(&pmd->bl_info, mapping_root, &begin, end, &mapping_root, &count);
1664 if (r)
1665 return r;
1666
1667 total_count += count;
1668 }
1669
1670 td->mapped_blocks -= total_count;
Joe Thornber6550f072015-04-13 09:45:25 +01001671 td->changed = 1;
1672
1673 /*
1674 * Reinsert the mapping tree.
1675 */
1676 value = cpu_to_le64(mapping_root);
1677 __dm_bless_for_disk(&value);
1678 return dm_btree_insert(&pmd->tl_info, pmd->root, keys, &value, &pmd->root);
1679}
1680
Joe Thornber991d9fa2011-10-31 20:21:18 +00001681int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
1682{
Joe Thornberda105ed2012-07-27 15:08:15 +01001683 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001684
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001685 pmd_write_lock(td->pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001686 if (!td->pmd->fail_io)
1687 r = __remove(td, block);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001688 pmd_write_unlock(td->pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001689
1690 return r;
1691}
1692
Joe Thornber6550f072015-04-13 09:45:25 +01001693int dm_thin_remove_range(struct dm_thin_device *td,
1694 dm_block_t begin, dm_block_t end)
1695{
1696 int r = -EINVAL;
1697
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001698 pmd_write_lock(td->pmd);
Joe Thornber6550f072015-04-13 09:45:25 +01001699 if (!td->pmd->fail_io)
1700 r = __remove_range(td, begin, end);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001701 pmd_write_unlock(td->pmd);
Joe Thornber6550f072015-04-13 09:45:25 +01001702
1703 return r;
1704}
1705
Joe Thornberd445bd92019-01-15 13:27:01 -05001706int dm_pool_block_is_shared(struct dm_pool_metadata *pmd, dm_block_t b, bool *result)
Joe Thornber19fa1a62013-12-17 12:09:40 -05001707{
1708 int r;
1709 uint32_t ref_count;
1710
1711 down_read(&pmd->root_lock);
1712 r = dm_sm_get_count(pmd->data_sm, b, &ref_count);
1713 if (!r)
Joe Thornberd445bd92019-01-15 13:27:01 -05001714 *result = (ref_count > 1);
Joe Thornber19fa1a62013-12-17 12:09:40 -05001715 up_read(&pmd->root_lock);
1716
1717 return r;
1718}
1719
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001720int dm_pool_inc_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e)
1721{
1722 int r = 0;
1723
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001724 pmd_write_lock(pmd);
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001725 for (; b != e; b++) {
1726 r = dm_sm_inc_block(pmd->data_sm, b);
1727 if (r)
1728 break;
1729 }
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001730 pmd_write_unlock(pmd);
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001731
1732 return r;
1733}
1734
1735int dm_pool_dec_data_range(struct dm_pool_metadata *pmd, dm_block_t b, dm_block_t e)
1736{
1737 int r = 0;
1738
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001739 pmd_write_lock(pmd);
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001740 for (; b != e; b++) {
1741 r = dm_sm_dec_block(pmd->data_sm, b);
1742 if (r)
1743 break;
1744 }
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001745 pmd_write_unlock(pmd);
Joe Thornber2a0fbff2016-07-01 14:00:02 +01001746
1747 return r;
1748}
1749
Joe Thornber40db5a52012-07-27 15:08:14 +01001750bool dm_thin_changed_this_transaction(struct dm_thin_device *td)
1751{
1752 int r;
1753
1754 down_read(&td->pmd->root_lock);
1755 r = td->changed;
1756 up_read(&td->pmd->root_lock);
1757
1758 return r;
1759}
1760
Mike Snitzer4d1662a2014-02-06 06:08:56 -05001761bool dm_pool_changed_this_transaction(struct dm_pool_metadata *pmd)
1762{
1763 bool r = false;
1764 struct dm_thin_device *td, *tmp;
1765
1766 down_read(&pmd->root_lock);
1767 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
1768 if (td->changed) {
1769 r = td->changed;
1770 break;
1771 }
1772 }
1773 up_read(&pmd->root_lock);
1774
1775 return r;
1776}
1777
Joe Thornberda105ed2012-07-27 15:08:15 +01001778bool dm_thin_aborted_changes(struct dm_thin_device *td)
1779{
1780 bool r;
1781
1782 down_read(&td->pmd->root_lock);
1783 r = td->aborted_with_changes;
1784 up_read(&td->pmd->root_lock);
1785
1786 return r;
1787}
1788
Joe Thornber991d9fa2011-10-31 20:21:18 +00001789int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
1790{
Joe Thornberda105ed2012-07-27 15:08:15 +01001791 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001792
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001793 pmd_write_lock(pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001794 if (!pmd->fail_io)
1795 r = dm_sm_new_block(pmd->data_sm, result);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001796 pmd_write_unlock(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001797
1798 return r;
1799}
1800
1801int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
1802{
Joe Thornberda105ed2012-07-27 15:08:15 +01001803 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001804
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001805 pmd_write_lock(pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001806 if (pmd->fail_io)
1807 goto out;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001808
1809 r = __commit_transaction(pmd);
1810 if (r <= 0)
1811 goto out;
1812
1813 /*
1814 * Open the next transaction.
1815 */
1816 r = __begin_transaction(pmd);
1817out:
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001818 pmd_write_unlock(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001819 return r;
1820}
1821
Joe Thornberda105ed2012-07-27 15:08:15 +01001822static void __set_abort_with_changes_flags(struct dm_pool_metadata *pmd)
1823{
1824 struct dm_thin_device *td;
1825
1826 list_for_each_entry(td, &pmd->thin_devices, list)
1827 td->aborted_with_changes = td->changed;
1828}
1829
1830int dm_pool_abort_metadata(struct dm_pool_metadata *pmd)
1831{
1832 int r = -EINVAL;
1833
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001834 pmd_write_lock(pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001835 if (pmd->fail_io)
1836 goto out;
1837
1838 __set_abort_with_changes_flags(pmd);
1839 __destroy_persistent_data_objects(pmd);
1840 r = __create_persistent_data_objects(pmd, false);
1841 if (r)
1842 pmd->fail_io = true;
1843
1844out:
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001845 pmd_write_unlock(pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001846
1847 return r;
1848}
1849
Joe Thornber991d9fa2011-10-31 20:21:18 +00001850int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
1851{
Joe Thornberda105ed2012-07-27 15:08:15 +01001852 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001853
1854 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001855 if (!pmd->fail_io)
1856 r = dm_sm_get_nr_free(pmd->data_sm, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001857 up_read(&pmd->root_lock);
1858
1859 return r;
1860}
1861
1862int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
1863 dm_block_t *result)
1864{
Joe Thornberda105ed2012-07-27 15:08:15 +01001865 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001866
1867 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001868 if (!pmd->fail_io)
1869 r = dm_sm_get_nr_free(pmd->metadata_sm, result);
Joe Thornber3ab91822018-09-10 16:50:09 +01001870
1871 if (!r) {
1872 if (*result < pmd->metadata_reserve)
1873 *result = 0;
1874 else
1875 *result -= pmd->metadata_reserve;
1876 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001877 up_read(&pmd->root_lock);
1878
1879 return r;
1880}
1881
1882int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
1883 dm_block_t *result)
1884{
Joe Thornberda105ed2012-07-27 15:08:15 +01001885 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001886
1887 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001888 if (!pmd->fail_io)
1889 r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001890 up_read(&pmd->root_lock);
1891
1892 return r;
1893}
1894
Joe Thornber991d9fa2011-10-31 20:21:18 +00001895int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
1896{
Joe Thornberda105ed2012-07-27 15:08:15 +01001897 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001898
1899 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001900 if (!pmd->fail_io)
1901 r = dm_sm_get_nr_blocks(pmd->data_sm, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001902 up_read(&pmd->root_lock);
1903
1904 return r;
1905}
1906
1907int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
1908{
Joe Thornberda105ed2012-07-27 15:08:15 +01001909 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001910 struct dm_pool_metadata *pmd = td->pmd;
1911
1912 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001913 if (!pmd->fail_io) {
1914 *result = td->mapped_blocks;
1915 r = 0;
1916 }
Joe Thornber991d9fa2011-10-31 20:21:18 +00001917 up_read(&pmd->root_lock);
1918
Joe Thornberda105ed2012-07-27 15:08:15 +01001919 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001920}
1921
1922static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
1923{
1924 int r;
1925 __le64 value_le;
1926 dm_block_t thin_root;
1927 struct dm_pool_metadata *pmd = td->pmd;
1928
1929 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
1930 if (r)
1931 return r;
1932
1933 thin_root = le64_to_cpu(value_le);
1934
1935 return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
1936}
1937
1938int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
1939 dm_block_t *result)
1940{
Joe Thornberda105ed2012-07-27 15:08:15 +01001941 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001942 struct dm_pool_metadata *pmd = td->pmd;
1943
1944 down_read(&pmd->root_lock);
Joe Thornberda105ed2012-07-27 15:08:15 +01001945 if (!pmd->fail_io)
1946 r = __highest_block(td, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001947 up_read(&pmd->root_lock);
1948
1949 return r;
1950}
1951
Joe Thornberb17446d2013-05-10 14:37:18 +01001952static int __resize_space_map(struct dm_space_map *sm, dm_block_t new_count)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001953{
1954 int r;
1955 dm_block_t old_count;
1956
Joe Thornberb17446d2013-05-10 14:37:18 +01001957 r = dm_sm_get_nr_blocks(sm, &old_count);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001958 if (r)
1959 return r;
1960
1961 if (new_count == old_count)
1962 return 0;
1963
1964 if (new_count < old_count) {
Joe Thornberb17446d2013-05-10 14:37:18 +01001965 DMERR("cannot reduce size of space map");
Joe Thornber991d9fa2011-10-31 20:21:18 +00001966 return -EINVAL;
1967 }
1968
Joe Thornberb17446d2013-05-10 14:37:18 +01001969 return dm_sm_extend(sm, new_count - old_count);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001970}
1971
1972int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1973{
Joe Thornberda105ed2012-07-27 15:08:15 +01001974 int r = -EINVAL;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001975
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001976 pmd_write_lock(pmd);
Joe Thornberda105ed2012-07-27 15:08:15 +01001977 if (!pmd->fail_io)
Joe Thornberb17446d2013-05-10 14:37:18 +01001978 r = __resize_space_map(pmd->data_sm, new_count);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001979 pmd_write_unlock(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001980
1981 return r;
1982}
Joe Thornber12ba58a2012-07-27 15:08:15 +01001983
Joe Thornber24347e92013-05-10 14:37:19 +01001984int dm_pool_resize_metadata_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1985{
1986 int r = -EINVAL;
1987
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001988 pmd_write_lock(pmd);
Joe Thornber3ab91822018-09-10 16:50:09 +01001989 if (!pmd->fail_io) {
Joe Thornber24347e92013-05-10 14:37:19 +01001990 r = __resize_space_map(pmd->metadata_sm, new_count);
Joe Thornber3ab91822018-09-10 16:50:09 +01001991 if (!r)
1992 __set_metadata_reserve(pmd);
1993 }
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04001994 pmd_write_unlock(pmd);
Joe Thornber24347e92013-05-10 14:37:19 +01001995
1996 return r;
1997}
1998
Joe Thornber12ba58a2012-07-27 15:08:15 +01001999void dm_pool_metadata_read_only(struct dm_pool_metadata *pmd)
2000{
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04002001 pmd_write_lock_in_core(pmd);
Joe Thornber12ba58a2012-07-27 15:08:15 +01002002 dm_bm_set_read_only(pmd->bm);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04002003 pmd_write_unlock(pmd);
Joe Thornber12ba58a2012-07-27 15:08:15 +01002004}
Joe Thornberac8c3f32013-05-10 14:37:21 +01002005
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002006void dm_pool_metadata_read_write(struct dm_pool_metadata *pmd)
2007{
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04002008 pmd_write_lock_in_core(pmd);
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002009 dm_bm_set_read_write(pmd->bm);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04002010 pmd_write_unlock(pmd);
Joe Thornber9b7aaa62013-12-04 16:58:19 -05002011}
2012
Joe Thornberac8c3f32013-05-10 14:37:21 +01002013int dm_pool_register_metadata_threshold(struct dm_pool_metadata *pmd,
2014 dm_block_t threshold,
2015 dm_sm_threshold_fn fn,
2016 void *context)
2017{
2018 int r;
2019
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04002020 pmd_write_lock_in_core(pmd);
Joe Thornberac8c3f32013-05-10 14:37:21 +01002021 r = dm_sm_register_threshold_callback(pmd->metadata_sm, threshold, fn, context);
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04002022 pmd_write_unlock(pmd);
Joe Thornberac8c3f32013-05-10 14:37:21 +01002023
2024 return r;
2025}
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002026
2027int dm_pool_metadata_set_needs_check(struct dm_pool_metadata *pmd)
2028{
2029 int r;
2030 struct dm_block *sblock;
2031 struct thin_disk_superblock *disk_super;
2032
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04002033 pmd_write_lock(pmd);
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002034 pmd->flags |= THIN_METADATA_NEEDS_CHECK_FLAG;
2035
2036 r = superblock_lock(pmd, &sblock);
2037 if (r) {
2038 DMERR("couldn't read superblock");
2039 goto out;
2040 }
2041
2042 disk_super = dm_block_data(sblock);
2043 disk_super->flags = cpu_to_le32(pmd->flags);
2044
2045 dm_bm_unlock(sblock);
2046out:
Mike Snitzer6a1b1dd2019-04-15 16:54:36 -04002047 pmd_write_unlock(pmd);
Mike Snitzer07f2b6e2014-02-14 11:58:41 -05002048 return r;
2049}
2050
2051bool dm_pool_metadata_needs_check(struct dm_pool_metadata *pmd)
2052{
2053 bool needs_check;
2054
2055 down_read(&pmd->root_lock);
2056 needs_check = pmd->flags & THIN_METADATA_NEEDS_CHECK_FLAG;
2057 up_read(&pmd->root_lock);
2058
2059 return needs_check;
2060}
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002061
2062void dm_pool_issue_prefetches(struct dm_pool_metadata *pmd)
2063{
Joe Thornber2eae9e42016-03-01 10:58:44 +00002064 down_read(&pmd->root_lock);
2065 if (!pmd->fail_io)
2066 dm_tm_issue_prefetches(pmd->tm);
2067 up_read(&pmd->root_lock);
Joe Thornber8a01a6a2014-10-06 15:28:30 +01002068}