blob: 1b6d2857b3f1d321bca36a1ae613a40dd126c5b6 [file] [log] [blame]
Joe Thornber991d9fa2011-10-31 20:21:18 +00001/*
2 * Copyright (C) 2011 Red Hat, Inc.
3 *
4 * This file is released under the GPL.
5 */
6
7#include "dm-thin-metadata.h"
8#include "persistent-data/dm-btree.h"
9#include "persistent-data/dm-space-map.h"
10#include "persistent-data/dm-space-map-disk.h"
11#include "persistent-data/dm-transaction-manager.h"
12
13#include <linux/list.h>
14#include <linux/device-mapper.h>
15#include <linux/workqueue.h>
16
17/*--------------------------------------------------------------------------
18 * As far as the metadata goes, there is:
19 *
20 * - A superblock in block zero, taking up fewer than 512 bytes for
21 * atomic writes.
22 *
23 * - A space map managing the metadata blocks.
24 *
25 * - A space map managing the data blocks.
26 *
27 * - A btree mapping our internal thin dev ids onto struct disk_device_details.
28 *
29 * - A hierarchical btree, with 2 levels which effectively maps (thin
30 * dev id, virtual block) -> block_time. Block time is a 64-bit
31 * field holding the time in the low 24 bits, and block in the top 48
32 * bits.
33 *
34 * BTrees consist solely of btree_nodes, that fill a block. Some are
35 * internal nodes, as such their values are a __le64 pointing to other
36 * nodes. Leaf nodes can store data of any reasonable size (ie. much
37 * smaller than the block size). The nodes consist of the header,
38 * followed by an array of keys, followed by an array of values. We have
39 * to binary search on the keys so they're all held together to help the
40 * cpu cache.
41 *
42 * Space maps have 2 btrees:
43 *
44 * - One maps a uint64_t onto a struct index_entry. Which points to a
45 * bitmap block, and has some details about how many free entries there
46 * are etc.
47 *
48 * - The bitmap blocks have a header (for the checksum). Then the rest
49 * of the block is pairs of bits. With the meaning being:
50 *
51 * 0 - ref count is 0
52 * 1 - ref count is 1
53 * 2 - ref count is 2
54 * 3 - ref count is higher than 2
55 *
56 * - If the count is higher than 2 then the ref count is entered in a
57 * second btree that directly maps the block_address to a uint32_t ref
58 * count.
59 *
60 * The space map metadata variant doesn't have a bitmaps btree. Instead
61 * it has one single blocks worth of index_entries. This avoids
62 * recursive issues with the bitmap btree needing to allocate space in
63 * order to insert. With a small data block size such as 64k the
64 * metadata support data devices that are hundreds of terrabytes.
65 *
66 * The space maps allocate space linearly from front to back. Space that
67 * is freed in a transaction is never recycled within that transaction.
68 * To try and avoid fragmenting _free_ space the allocator always goes
69 * back and fills in gaps.
70 *
71 * All metadata io is in THIN_METADATA_BLOCK_SIZE sized/aligned chunks
72 * from the block manager.
73 *--------------------------------------------------------------------------*/
74
75#define DM_MSG_PREFIX "thin metadata"
76
77#define THIN_SUPERBLOCK_MAGIC 27022010
78#define THIN_SUPERBLOCK_LOCATION 0
79#define THIN_VERSION 1
80#define THIN_METADATA_CACHE_SIZE 64
81#define SECTOR_TO_BLOCK_SHIFT 3
82
Joe Thornber8c971172012-07-27 15:07:58 +010083/*
84 * 3 for btree insert +
85 * 2 for btree lookup used within space map
86 */
87#define THIN_MAX_CONCURRENT_LOCKS 5
88
Joe Thornber991d9fa2011-10-31 20:21:18 +000089/* This should be plenty */
90#define SPACE_MAP_ROOT_SIZE 128
91
92/*
93 * Little endian on-disk superblock and device details.
94 */
95struct thin_disk_superblock {
96 __le32 csum; /* Checksum of superblock except for this field. */
97 __le32 flags;
98 __le64 blocknr; /* This block number, dm_block_t. */
99
100 __u8 uuid[16];
101 __le64 magic;
102 __le32 version;
103 __le32 time;
104
105 __le64 trans_id;
106
107 /*
108 * Root held by userspace transactions.
109 */
110 __le64 held_root;
111
112 __u8 data_space_map_root[SPACE_MAP_ROOT_SIZE];
113 __u8 metadata_space_map_root[SPACE_MAP_ROOT_SIZE];
114
115 /*
116 * 2-level btree mapping (dev_id, (dev block, time)) -> data block
117 */
118 __le64 data_mapping_root;
119
120 /*
121 * Device detail root mapping dev_id -> device_details
122 */
123 __le64 device_details_root;
124
125 __le32 data_block_size; /* In 512-byte sectors. */
126
127 __le32 metadata_block_size; /* In 512-byte sectors. */
128 __le64 metadata_nr_blocks;
129
130 __le32 compat_flags;
131 __le32 compat_ro_flags;
132 __le32 incompat_flags;
133} __packed;
134
135struct disk_device_details {
136 __le64 mapped_blocks;
137 __le64 transaction_id; /* When created. */
138 __le32 creation_time;
139 __le32 snapshotted_time;
140} __packed;
141
142struct dm_pool_metadata {
143 struct hlist_node hash;
144
145 struct block_device *bdev;
146 struct dm_block_manager *bm;
147 struct dm_space_map *metadata_sm;
148 struct dm_space_map *data_sm;
149 struct dm_transaction_manager *tm;
150 struct dm_transaction_manager *nb_tm;
151
152 /*
153 * Two-level btree.
154 * First level holds thin_dev_t.
155 * Second level holds mappings.
156 */
157 struct dm_btree_info info;
158
159 /*
160 * Non-blocking version of the above.
161 */
162 struct dm_btree_info nb_info;
163
164 /*
165 * Just the top level for deleting whole devices.
166 */
167 struct dm_btree_info tl_info;
168
169 /*
170 * Just the bottom level for creating new devices.
171 */
172 struct dm_btree_info bl_info;
173
174 /*
175 * Describes the device details btree.
176 */
177 struct dm_btree_info details_info;
178
179 struct rw_semaphore root_lock;
180 uint32_t time;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000181 dm_block_t root;
182 dm_block_t details_root;
183 struct list_head thin_devices;
184 uint64_t trans_id;
185 unsigned long flags;
186 sector_t data_block_size;
187};
188
189struct dm_thin_device {
190 struct list_head list;
191 struct dm_pool_metadata *pmd;
192 dm_thin_id id;
193
194 int open_count;
195 int changed;
196 uint64_t mapped_blocks;
197 uint64_t transaction_id;
198 uint32_t creation_time;
199 uint32_t snapshotted_time;
200};
201
202/*----------------------------------------------------------------
203 * superblock validator
204 *--------------------------------------------------------------*/
205
206#define SUPERBLOCK_CSUM_XOR 160774
207
208static void sb_prepare_for_write(struct dm_block_validator *v,
209 struct dm_block *b,
210 size_t block_size)
211{
212 struct thin_disk_superblock *disk_super = dm_block_data(b);
213
214 disk_super->blocknr = cpu_to_le64(dm_block_location(b));
215 disk_super->csum = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
216 block_size - sizeof(__le32),
217 SUPERBLOCK_CSUM_XOR));
218}
219
220static int sb_check(struct dm_block_validator *v,
221 struct dm_block *b,
222 size_t block_size)
223{
224 struct thin_disk_superblock *disk_super = dm_block_data(b);
225 __le32 csum_le;
226
227 if (dm_block_location(b) != le64_to_cpu(disk_super->blocknr)) {
228 DMERR("sb_check failed: blocknr %llu: "
229 "wanted %llu", le64_to_cpu(disk_super->blocknr),
230 (unsigned long long)dm_block_location(b));
231 return -ENOTBLK;
232 }
233
234 if (le64_to_cpu(disk_super->magic) != THIN_SUPERBLOCK_MAGIC) {
235 DMERR("sb_check failed: magic %llu: "
236 "wanted %llu", le64_to_cpu(disk_super->magic),
237 (unsigned long long)THIN_SUPERBLOCK_MAGIC);
238 return -EILSEQ;
239 }
240
241 csum_le = cpu_to_le32(dm_bm_checksum(&disk_super->flags,
242 block_size - sizeof(__le32),
243 SUPERBLOCK_CSUM_XOR));
244 if (csum_le != disk_super->csum) {
245 DMERR("sb_check failed: csum %u: wanted %u",
246 le32_to_cpu(csum_le), le32_to_cpu(disk_super->csum));
247 return -EILSEQ;
248 }
249
250 return 0;
251}
252
253static struct dm_block_validator sb_validator = {
254 .name = "superblock",
255 .prepare_for_write = sb_prepare_for_write,
256 .check = sb_check
257};
258
259/*----------------------------------------------------------------
260 * Methods for the btree value types
261 *--------------------------------------------------------------*/
262
263static uint64_t pack_block_time(dm_block_t b, uint32_t t)
264{
265 return (b << 24) | t;
266}
267
268static void unpack_block_time(uint64_t v, dm_block_t *b, uint32_t *t)
269{
270 *b = v >> 24;
271 *t = v & ((1 << 24) - 1);
272}
273
274static void data_block_inc(void *context, void *value_le)
275{
276 struct dm_space_map *sm = context;
277 __le64 v_le;
278 uint64_t b;
279 uint32_t t;
280
281 memcpy(&v_le, value_le, sizeof(v_le));
282 unpack_block_time(le64_to_cpu(v_le), &b, &t);
283 dm_sm_inc_block(sm, b);
284}
285
286static void data_block_dec(void *context, void *value_le)
287{
288 struct dm_space_map *sm = context;
289 __le64 v_le;
290 uint64_t b;
291 uint32_t t;
292
293 memcpy(&v_le, value_le, sizeof(v_le));
294 unpack_block_time(le64_to_cpu(v_le), &b, &t);
295 dm_sm_dec_block(sm, b);
296}
297
298static int data_block_equal(void *context, void *value1_le, void *value2_le)
299{
300 __le64 v1_le, v2_le;
301 uint64_t b1, b2;
302 uint32_t t;
303
304 memcpy(&v1_le, value1_le, sizeof(v1_le));
305 memcpy(&v2_le, value2_le, sizeof(v2_le));
306 unpack_block_time(le64_to_cpu(v1_le), &b1, &t);
307 unpack_block_time(le64_to_cpu(v2_le), &b2, &t);
308
309 return b1 == b2;
310}
311
312static void subtree_inc(void *context, void *value)
313{
314 struct dm_btree_info *info = context;
315 __le64 root_le;
316 uint64_t root;
317
318 memcpy(&root_le, value, sizeof(root_le));
319 root = le64_to_cpu(root_le);
320 dm_tm_inc(info->tm, root);
321}
322
323static void subtree_dec(void *context, void *value)
324{
325 struct dm_btree_info *info = context;
326 __le64 root_le;
327 uint64_t root;
328
329 memcpy(&root_le, value, sizeof(root_le));
330 root = le64_to_cpu(root_le);
331 if (dm_btree_del(info, root))
332 DMERR("btree delete failed\n");
333}
334
335static int subtree_equal(void *context, void *value1_le, void *value2_le)
336{
337 __le64 v1_le, v2_le;
338 memcpy(&v1_le, value1_le, sizeof(v1_le));
339 memcpy(&v2_le, value2_le, sizeof(v2_le));
340
341 return v1_le == v2_le;
342}
343
344/*----------------------------------------------------------------*/
345
Joe Thornber25971192012-07-27 15:08:09 +0100346static int superblock_lock_zero(struct dm_pool_metadata *pmd,
347 struct dm_block **sblock)
348{
349 return dm_bm_write_lock_zero(pmd->bm, THIN_SUPERBLOCK_LOCATION,
350 &sb_validator, sblock);
351}
352
353static int superblock_lock(struct dm_pool_metadata *pmd,
354 struct dm_block **sblock)
355{
356 return dm_bm_write_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
357 &sb_validator, sblock);
358}
359
Joe Thornber332627d2012-07-27 15:08:10 +0100360static int __superblock_all_zeroes(struct dm_block_manager *bm, int *result)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000361{
362 int r;
363 unsigned i;
364 struct dm_block *b;
365 __le64 *data_le, zero = cpu_to_le64(0);
366 unsigned block_size = dm_bm_block_size(bm) / sizeof(__le64);
367
368 /*
369 * We can't use a validator here - it may be all zeroes.
370 */
371 r = dm_bm_read_lock(bm, THIN_SUPERBLOCK_LOCATION, NULL, &b);
372 if (r)
373 return r;
374
375 data_le = dm_block_data(b);
376 *result = 1;
377 for (i = 0; i < block_size; i++) {
378 if (data_le[i] != zero) {
379 *result = 0;
380 break;
381 }
382 }
383
384 return dm_bm_unlock(b);
385}
386
Joe Thornber41675ae2012-07-27 15:08:08 +0100387static void __setup_btree_details(struct dm_pool_metadata *pmd)
388{
389 pmd->info.tm = pmd->tm;
390 pmd->info.levels = 2;
391 pmd->info.value_type.context = pmd->data_sm;
392 pmd->info.value_type.size = sizeof(__le64);
393 pmd->info.value_type.inc = data_block_inc;
394 pmd->info.value_type.dec = data_block_dec;
395 pmd->info.value_type.equal = data_block_equal;
396
397 memcpy(&pmd->nb_info, &pmd->info, sizeof(pmd->nb_info));
398 pmd->nb_info.tm = pmd->nb_tm;
399
400 pmd->tl_info.tm = pmd->tm;
401 pmd->tl_info.levels = 1;
402 pmd->tl_info.value_type.context = &pmd->info;
403 pmd->tl_info.value_type.size = sizeof(__le64);
404 pmd->tl_info.value_type.inc = subtree_inc;
405 pmd->tl_info.value_type.dec = subtree_dec;
406 pmd->tl_info.value_type.equal = subtree_equal;
407
408 pmd->bl_info.tm = pmd->tm;
409 pmd->bl_info.levels = 1;
410 pmd->bl_info.value_type.context = pmd->data_sm;
411 pmd->bl_info.value_type.size = sizeof(__le64);
412 pmd->bl_info.value_type.inc = data_block_inc;
413 pmd->bl_info.value_type.dec = data_block_dec;
414 pmd->bl_info.value_type.equal = data_block_equal;
415
416 pmd->details_info.tm = pmd->tm;
417 pmd->details_info.levels = 1;
418 pmd->details_info.value_type.context = NULL;
419 pmd->details_info.value_type.size = sizeof(struct disk_device_details);
420 pmd->details_info.value_type.inc = NULL;
421 pmd->details_info.value_type.dec = NULL;
422 pmd->details_info.value_type.equal = NULL;
423}
424
Joe Thornber9cb66532012-07-27 15:08:10 +0100425static int __write_initial_superblock(struct dm_pool_metadata *pmd)
426{
427 int r;
428 struct dm_block *sblock;
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100429 size_t metadata_len, data_len;
Joe Thornber9cb66532012-07-27 15:08:10 +0100430 struct thin_disk_superblock *disk_super;
431 sector_t bdev_size = i_size_read(pmd->bdev->bd_inode) >> SECTOR_SHIFT;
432
433 if (bdev_size > THIN_METADATA_MAX_SECTORS)
434 bdev_size = THIN_METADATA_MAX_SECTORS;
435
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100436 r = dm_sm_root_size(pmd->metadata_sm, &metadata_len);
437 if (r < 0)
438 return r;
439
440 r = dm_sm_root_size(pmd->data_sm, &data_len);
441 if (r < 0)
442 return r;
443
444 r = dm_sm_commit(pmd->data_sm);
445 if (r < 0)
446 return r;
447
448 r = dm_tm_pre_commit(pmd->tm);
449 if (r < 0)
450 return r;
451
Joe Thornber9cb66532012-07-27 15:08:10 +0100452 r = superblock_lock_zero(pmd, &sblock);
453 if (r)
454 return r;
455
456 disk_super = dm_block_data(sblock);
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100457 disk_super->flags = 0;
Joe Thornber583ceee2012-07-27 15:08:11 +0100458 memset(disk_super->uuid, 0, sizeof(disk_super->uuid));
Joe Thornber9cb66532012-07-27 15:08:10 +0100459 disk_super->magic = cpu_to_le64(THIN_SUPERBLOCK_MAGIC);
460 disk_super->version = cpu_to_le32(THIN_VERSION);
461 disk_super->time = 0;
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100462 disk_super->trans_id = 0;
463 disk_super->held_root = 0;
464
465 r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root,
466 metadata_len);
467 if (r < 0)
468 goto bad_locked;
469
470 r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root,
471 data_len);
472 if (r < 0)
473 goto bad_locked;
474
475 disk_super->data_mapping_root = cpu_to_le64(pmd->root);
476 disk_super->device_details_root = cpu_to_le64(pmd->details_root);
Joe Thornber9cb66532012-07-27 15:08:10 +0100477 disk_super->metadata_block_size = cpu_to_le32(THIN_METADATA_BLOCK_SIZE >> SECTOR_SHIFT);
478 disk_super->metadata_nr_blocks = cpu_to_le64(bdev_size >> SECTOR_TO_BLOCK_SHIFT);
479 disk_super->data_block_size = cpu_to_le32(pmd->data_block_size);
480
Joe Thornber270938b2012-07-27 15:08:11 +0100481 return dm_tm_commit(pmd->tm, sblock);
Joe Thornber9cb66532012-07-27 15:08:10 +0100482
Joe Thornber10d2a9f2012-07-27 15:08:11 +0100483bad_locked:
484 dm_bm_unlock(sblock);
Joe Thornber9cb66532012-07-27 15:08:10 +0100485 return r;
486}
487
Joe Thornbera97e5e62012-07-27 15:08:12 +0100488static int __format_metadata(struct dm_pool_metadata *pmd)
Joe Thornber991d9fa2011-10-31 20:21:18 +0000489{
490 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000491
Joe Thornbere4d22052012-07-27 15:08:12 +0100492 r = dm_tm_create_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
493 &pmd->tm, &pmd->metadata_sm);
494 if (r < 0) {
495 DMERR("tm_create_with_sm failed");
496 return r;
497 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000498
Joe Thornbera97e5e62012-07-27 15:08:12 +0100499 pmd->data_sm = dm_sm_disk_create(pmd->tm, 0);
Joe Thornbere4d22052012-07-27 15:08:12 +0100500 if (IS_ERR(pmd->data_sm)) {
501 DMERR("sm_disk_create failed");
502 r = PTR_ERR(pmd->data_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100503 goto bad_cleanup_tm;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000504 }
505
Joe Thornberd6332812012-07-27 15:08:12 +0100506 pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000507 if (!pmd->nb_tm) {
Joe Thornber0fa5b172012-07-27 15:08:14 +0100508 DMERR("could not create non-blocking clone tm");
Joe Thornber991d9fa2011-10-31 20:21:18 +0000509 r = -ENOMEM;
Joe Thornber0fa5b172012-07-27 15:08:14 +0100510 goto bad_cleanup_data_sm;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000511 }
512
Joe Thornber41675ae2012-07-27 15:08:08 +0100513 __setup_btree_details(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000514
Joe Thornber9cb66532012-07-27 15:08:10 +0100515 r = dm_btree_empty(&pmd->info, &pmd->root);
516 if (r < 0)
Joe Thornber0fa5b172012-07-27 15:08:14 +0100517 goto bad_cleanup_nb_tm;
Joe Thornber9cb66532012-07-27 15:08:10 +0100518
519 r = dm_btree_empty(&pmd->details_info, &pmd->details_root);
520 if (r < 0) {
521 DMERR("couldn't create devices root");
Joe Thornber0fa5b172012-07-27 15:08:14 +0100522 goto bad_cleanup_nb_tm;
Joe Thornber9cb66532012-07-27 15:08:10 +0100523 }
524
525 r = __write_initial_superblock(pmd);
526 if (r)
Joe Thornber0fa5b172012-07-27 15:08:14 +0100527 goto bad_cleanup_nb_tm;
Joe Thornber9cb66532012-07-27 15:08:10 +0100528
Joe Thornber991d9fa2011-10-31 20:21:18 +0000529 return 0;
530
Joe Thornber0fa5b172012-07-27 15:08:14 +0100531bad_cleanup_nb_tm:
532 dm_tm_destroy(pmd->nb_tm);
533bad_cleanup_data_sm:
Joe Thornberd6332812012-07-27 15:08:12 +0100534 dm_sm_destroy(pmd->data_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100535bad_cleanup_tm:
Joe Thornberd6332812012-07-27 15:08:12 +0100536 dm_tm_destroy(pmd->tm);
537 dm_sm_destroy(pmd->metadata_sm);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000538
539 return r;
540}
541
Mike Snitzerd73ec522012-07-27 15:08:13 +0100542static int __check_incompat_features(struct thin_disk_superblock *disk_super,
543 struct dm_pool_metadata *pmd)
544{
545 uint32_t features;
546
547 features = le32_to_cpu(disk_super->incompat_flags) & ~THIN_FEATURE_INCOMPAT_SUPP;
548 if (features) {
549 DMERR("could not access metadata due to unsupported optional features (%lx).",
550 (unsigned long)features);
551 return -EINVAL;
552 }
553
554 /*
555 * Check for read-only metadata to skip the following RDWR checks.
556 */
557 if (get_disk_ro(pmd->bdev->bd_disk))
558 return 0;
559
560 features = le32_to_cpu(disk_super->compat_ro_flags) & ~THIN_FEATURE_COMPAT_RO_SUPP;
561 if (features) {
562 DMERR("could not access metadata RDWR due to unsupported optional features (%lx).",
563 (unsigned long)features);
564 return -EINVAL;
565 }
566
567 return 0;
568}
569
Joe Thornbere4d22052012-07-27 15:08:12 +0100570static int __open_metadata(struct dm_pool_metadata *pmd)
571{
572 int r;
573 struct dm_block *sblock;
574 struct thin_disk_superblock *disk_super;
575
576 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
577 &sb_validator, &sblock);
578 if (r < 0) {
579 DMERR("couldn't read superblock");
580 return r;
581 }
582
583 disk_super = dm_block_data(sblock);
Mike Snitzerd73ec522012-07-27 15:08:13 +0100584
585 r = __check_incompat_features(disk_super, pmd);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100586 if (r < 0)
587 goto bad_unlock_sblock;
Mike Snitzerd73ec522012-07-27 15:08:13 +0100588
Joe Thornbere4d22052012-07-27 15:08:12 +0100589 r = dm_tm_open_with_sm(pmd->bm, THIN_SUPERBLOCK_LOCATION,
590 disk_super->metadata_space_map_root,
591 sizeof(disk_super->metadata_space_map_root),
592 &pmd->tm, &pmd->metadata_sm);
593 if (r < 0) {
594 DMERR("tm_open_with_sm failed");
Joe Thornber0fa5b172012-07-27 15:08:14 +0100595 goto bad_unlock_sblock;
Joe Thornbere4d22052012-07-27 15:08:12 +0100596 }
597
598 pmd->data_sm = dm_sm_disk_open(pmd->tm, disk_super->data_space_map_root,
599 sizeof(disk_super->data_space_map_root));
600 if (IS_ERR(pmd->data_sm)) {
601 DMERR("sm_disk_open failed");
Joe Thornbere4d22052012-07-27 15:08:12 +0100602 r = PTR_ERR(pmd->data_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100603 goto bad_cleanup_tm;
Joe Thornbere4d22052012-07-27 15:08:12 +0100604 }
605
Joe Thornbere4d22052012-07-27 15:08:12 +0100606 pmd->nb_tm = dm_tm_create_non_blocking_clone(pmd->tm);
607 if (!pmd->nb_tm) {
Joe Thornber0fa5b172012-07-27 15:08:14 +0100608 DMERR("could not create non-blocking clone tm");
Joe Thornbere4d22052012-07-27 15:08:12 +0100609 r = -ENOMEM;
Joe Thornber0fa5b172012-07-27 15:08:14 +0100610 goto bad_cleanup_data_sm;
Joe Thornbere4d22052012-07-27 15:08:12 +0100611 }
612
613 __setup_btree_details(pmd);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100614 return dm_bm_unlock(sblock);
Joe Thornbere4d22052012-07-27 15:08:12 +0100615
Joe Thornber0fa5b172012-07-27 15:08:14 +0100616bad_cleanup_data_sm:
Joe Thornbere4d22052012-07-27 15:08:12 +0100617 dm_sm_destroy(pmd->data_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100618bad_cleanup_tm:
Joe Thornbere4d22052012-07-27 15:08:12 +0100619 dm_tm_destroy(pmd->tm);
620 dm_sm_destroy(pmd->metadata_sm);
Joe Thornber0fa5b172012-07-27 15:08:14 +0100621bad_unlock_sblock:
622 dm_bm_unlock(sblock);
Joe Thornbere4d22052012-07-27 15:08:12 +0100623
624 return r;
625}
626
Joe Thornber8801e062012-07-27 15:08:13 +0100627static int __open_or_format_metadata(struct dm_pool_metadata *pmd)
Joe Thornbere4d22052012-07-27 15:08:12 +0100628{
Joe Thornber8801e062012-07-27 15:08:13 +0100629 int r, unformatted;
Joe Thornber237074c2012-07-27 15:08:13 +0100630
Joe Thornber8801e062012-07-27 15:08:13 +0100631 r = __superblock_all_zeroes(pmd->bm, &unformatted);
Joe Thornber237074c2012-07-27 15:08:13 +0100632 if (r)
633 return r;
634
Joe Thornber8801e062012-07-27 15:08:13 +0100635 if (unformatted)
Joe Thornbera97e5e62012-07-27 15:08:12 +0100636 return __format_metadata(pmd);
Joe Thornbere4d22052012-07-27 15:08:12 +0100637 else
638 return __open_metadata(pmd);
639}
640
Joe Thornber8801e062012-07-27 15:08:13 +0100641static int __create_persistent_data_objects(struct dm_pool_metadata *pmd)
Joe Thornber332627d2012-07-27 15:08:10 +0100642{
643 int r;
644
645 pmd->bm = dm_block_manager_create(pmd->bdev, THIN_METADATA_BLOCK_SIZE,
646 THIN_METADATA_CACHE_SIZE,
647 THIN_MAX_CONCURRENT_LOCKS);
648 if (IS_ERR(pmd->bm)) {
649 DMERR("could not create block manager");
650 return PTR_ERR(pmd->bm);
651 }
652
Joe Thornber8801e062012-07-27 15:08:13 +0100653 r = __open_or_format_metadata(pmd);
Joe Thornber332627d2012-07-27 15:08:10 +0100654 if (r)
655 dm_block_manager_destroy(pmd->bm);
656
657 return r;
658}
659
Joe Thornberf9dd9352012-07-27 15:08:10 +0100660static void __destroy_persistent_data_objects(struct dm_pool_metadata *pmd)
661{
662 dm_sm_destroy(pmd->data_sm);
663 dm_sm_destroy(pmd->metadata_sm);
664 dm_tm_destroy(pmd->nb_tm);
665 dm_tm_destroy(pmd->tm);
666 dm_block_manager_destroy(pmd->bm);
667}
668
Joe Thornber991d9fa2011-10-31 20:21:18 +0000669static int __begin_transaction(struct dm_pool_metadata *pmd)
670{
671 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000672 struct thin_disk_superblock *disk_super;
673 struct dm_block *sblock;
674
675 /*
Joe Thornber991d9fa2011-10-31 20:21:18 +0000676 * We re-read the superblock every time. Shouldn't need to do this
677 * really.
678 */
679 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
680 &sb_validator, &sblock);
681 if (r)
682 return r;
683
684 disk_super = dm_block_data(sblock);
685 pmd->time = le32_to_cpu(disk_super->time);
686 pmd->root = le64_to_cpu(disk_super->data_mapping_root);
687 pmd->details_root = le64_to_cpu(disk_super->device_details_root);
688 pmd->trans_id = le64_to_cpu(disk_super->trans_id);
689 pmd->flags = le32_to_cpu(disk_super->flags);
690 pmd->data_block_size = le32_to_cpu(disk_super->data_block_size);
691
Joe Thornber991d9fa2011-10-31 20:21:18 +0000692 dm_bm_unlock(sblock);
Mike Snitzerd73ec522012-07-27 15:08:13 +0100693 return 0;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000694}
695
696static int __write_changed_details(struct dm_pool_metadata *pmd)
697{
698 int r;
699 struct dm_thin_device *td, *tmp;
700 struct disk_device_details details;
701 uint64_t key;
702
703 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
704 if (!td->changed)
705 continue;
706
707 key = td->id;
708
709 details.mapped_blocks = cpu_to_le64(td->mapped_blocks);
710 details.transaction_id = cpu_to_le64(td->transaction_id);
711 details.creation_time = cpu_to_le32(td->creation_time);
712 details.snapshotted_time = cpu_to_le32(td->snapshotted_time);
713 __dm_bless_for_disk(&details);
714
715 r = dm_btree_insert(&pmd->details_info, pmd->details_root,
716 &key, &details, &pmd->details_root);
717 if (r)
718 return r;
719
720 if (td->open_count)
721 td->changed = 0;
722 else {
723 list_del(&td->list);
724 kfree(td);
725 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000726 }
727
728 return 0;
729}
730
731static int __commit_transaction(struct dm_pool_metadata *pmd)
732{
733 /*
734 * FIXME: Associated pool should be made read-only on failure.
735 */
736 int r;
737 size_t metadata_len, data_len;
738 struct thin_disk_superblock *disk_super;
739 struct dm_block *sblock;
740
741 /*
742 * We need to know if the thin_disk_superblock exceeds a 512-byte sector.
743 */
744 BUILD_BUG_ON(sizeof(struct thin_disk_superblock) > 512);
745
746 r = __write_changed_details(pmd);
747 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100748 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000749
Joe Thornber991d9fa2011-10-31 20:21:18 +0000750 r = dm_sm_commit(pmd->data_sm);
751 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100752 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000753
754 r = dm_tm_pre_commit(pmd->tm);
755 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100756 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000757
758 r = dm_sm_root_size(pmd->metadata_sm, &metadata_len);
759 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100760 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000761
Joe Thornberfef838c2012-03-28 18:41:25 +0100762 r = dm_sm_root_size(pmd->data_sm, &data_len);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000763 if (r < 0)
Joe Thornberd973ac12012-07-27 15:07:58 +0100764 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000765
Joe Thornber25971192012-07-27 15:08:09 +0100766 r = superblock_lock(pmd, &sblock);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000767 if (r)
Joe Thornberd973ac12012-07-27 15:07:58 +0100768 return r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000769
770 disk_super = dm_block_data(sblock);
771 disk_super->time = cpu_to_le32(pmd->time);
772 disk_super->data_mapping_root = cpu_to_le64(pmd->root);
773 disk_super->device_details_root = cpu_to_le64(pmd->details_root);
774 disk_super->trans_id = cpu_to_le64(pmd->trans_id);
775 disk_super->flags = cpu_to_le32(pmd->flags);
776
777 r = dm_sm_copy_root(pmd->metadata_sm, &disk_super->metadata_space_map_root,
778 metadata_len);
779 if (r < 0)
780 goto out_locked;
781
782 r = dm_sm_copy_root(pmd->data_sm, &disk_super->data_space_map_root,
783 data_len);
784 if (r < 0)
785 goto out_locked;
786
Joe Thornbereb04cf62012-07-27 15:08:08 +0100787 return dm_tm_commit(pmd->tm, sblock);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000788
789out_locked:
790 dm_bm_unlock(sblock);
791 return r;
792}
793
794struct dm_pool_metadata *dm_pool_metadata_open(struct block_device *bdev,
795 sector_t data_block_size)
796{
797 int r;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000798 struct dm_pool_metadata *pmd;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000799
800 pmd = kmalloc(sizeof(*pmd), GFP_KERNEL);
801 if (!pmd) {
802 DMERR("could not allocate metadata struct");
803 return ERR_PTR(-ENOMEM);
804 }
805
Joe Thornber6a0ebd32012-07-27 15:08:10 +0100806 init_rwsem(&pmd->root_lock);
807 pmd->time = 0;
808 INIT_LIST_HEAD(&pmd->thin_devices);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000809 pmd->bdev = bdev;
Joe Thornber9cb66532012-07-27 15:08:10 +0100810 pmd->data_block_size = data_block_size;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000811
Joe Thornber8801e062012-07-27 15:08:13 +0100812 r = __create_persistent_data_objects(pmd);
Joe Thornber332627d2012-07-27 15:08:10 +0100813 if (r) {
814 kfree(pmd);
815 return ERR_PTR(r);
816 }
817
Joe Thornber270938b2012-07-27 15:08:11 +0100818 r = __begin_transaction(pmd);
819 if (r < 0) {
820 if (dm_pool_metadata_close(pmd) < 0)
821 DMWARN("%s: dm_pool_metadata_close() failed.", __func__);
822 return ERR_PTR(r);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000823 }
824
825 return pmd;
Joe Thornber991d9fa2011-10-31 20:21:18 +0000826}
827
828int dm_pool_metadata_close(struct dm_pool_metadata *pmd)
829{
830 int r;
831 unsigned open_devices = 0;
832 struct dm_thin_device *td, *tmp;
833
834 down_read(&pmd->root_lock);
835 list_for_each_entry_safe(td, tmp, &pmd->thin_devices, list) {
836 if (td->open_count)
837 open_devices++;
838 else {
839 list_del(&td->list);
840 kfree(td);
841 }
842 }
843 up_read(&pmd->root_lock);
844
845 if (open_devices) {
846 DMERR("attempt to close pmd when %u device(s) are still open",
847 open_devices);
848 return -EBUSY;
849 }
850
851 r = __commit_transaction(pmd);
852 if (r < 0)
853 DMWARN("%s: __commit_transaction() failed, error = %d",
854 __func__, r);
855
Joe Thornberf9dd9352012-07-27 15:08:10 +0100856 __destroy_persistent_data_objects(pmd);
Joe Thornber991d9fa2011-10-31 20:21:18 +0000857 kfree(pmd);
858
859 return 0;
860}
861
Mike Snitzer1f3db252012-03-07 19:09:41 +0000862/*
863 * __open_device: Returns @td corresponding to device with id @dev,
864 * creating it if @create is set and incrementing @td->open_count.
865 * On failure, @td is undefined.
866 */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000867static int __open_device(struct dm_pool_metadata *pmd,
868 dm_thin_id dev, int create,
869 struct dm_thin_device **td)
870{
871 int r, changed = 0;
872 struct dm_thin_device *td2;
873 uint64_t key = dev;
874 struct disk_device_details details_le;
875
876 /*
Mike Snitzer1f3db252012-03-07 19:09:41 +0000877 * If the device is already open, return it.
Joe Thornber991d9fa2011-10-31 20:21:18 +0000878 */
879 list_for_each_entry(td2, &pmd->thin_devices, list)
880 if (td2->id == dev) {
Mike Snitzer1f3db252012-03-07 19:09:41 +0000881 /*
882 * May not create an already-open device.
883 */
884 if (create)
885 return -EEXIST;
886
Joe Thornber991d9fa2011-10-31 20:21:18 +0000887 td2->open_count++;
888 *td = td2;
889 return 0;
890 }
891
892 /*
893 * Check the device exists.
894 */
895 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
896 &key, &details_le);
897 if (r) {
898 if (r != -ENODATA || !create)
899 return r;
900
Mike Snitzer1f3db252012-03-07 19:09:41 +0000901 /*
902 * Create new device.
903 */
Joe Thornber991d9fa2011-10-31 20:21:18 +0000904 changed = 1;
905 details_le.mapped_blocks = 0;
906 details_le.transaction_id = cpu_to_le64(pmd->trans_id);
907 details_le.creation_time = cpu_to_le32(pmd->time);
908 details_le.snapshotted_time = cpu_to_le32(pmd->time);
909 }
910
911 *td = kmalloc(sizeof(**td), GFP_NOIO);
912 if (!*td)
913 return -ENOMEM;
914
915 (*td)->pmd = pmd;
916 (*td)->id = dev;
917 (*td)->open_count = 1;
918 (*td)->changed = changed;
919 (*td)->mapped_blocks = le64_to_cpu(details_le.mapped_blocks);
920 (*td)->transaction_id = le64_to_cpu(details_le.transaction_id);
921 (*td)->creation_time = le32_to_cpu(details_le.creation_time);
922 (*td)->snapshotted_time = le32_to_cpu(details_le.snapshotted_time);
923
924 list_add(&(*td)->list, &pmd->thin_devices);
925
926 return 0;
927}
928
929static void __close_device(struct dm_thin_device *td)
930{
931 --td->open_count;
932}
933
934static int __create_thin(struct dm_pool_metadata *pmd,
935 dm_thin_id dev)
936{
937 int r;
938 dm_block_t dev_root;
939 uint64_t key = dev;
940 struct disk_device_details details_le;
941 struct dm_thin_device *td;
942 __le64 value;
943
944 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
945 &key, &details_le);
946 if (!r)
947 return -EEXIST;
948
949 /*
950 * Create an empty btree for the mappings.
951 */
952 r = dm_btree_empty(&pmd->bl_info, &dev_root);
953 if (r)
954 return r;
955
956 /*
957 * Insert it into the main mapping tree.
958 */
959 value = cpu_to_le64(dev_root);
960 __dm_bless_for_disk(&value);
961 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
962 if (r) {
963 dm_btree_del(&pmd->bl_info, dev_root);
964 return r;
965 }
966
967 r = __open_device(pmd, dev, 1, &td);
968 if (r) {
Joe Thornber991d9fa2011-10-31 20:21:18 +0000969 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
970 dm_btree_del(&pmd->bl_info, dev_root);
971 return r;
972 }
Joe Thornber991d9fa2011-10-31 20:21:18 +0000973 __close_device(td);
974
975 return r;
976}
977
978int dm_pool_create_thin(struct dm_pool_metadata *pmd, dm_thin_id dev)
979{
980 int r;
981
982 down_write(&pmd->root_lock);
983 r = __create_thin(pmd, dev);
984 up_write(&pmd->root_lock);
985
986 return r;
987}
988
989static int __set_snapshot_details(struct dm_pool_metadata *pmd,
990 struct dm_thin_device *snap,
991 dm_thin_id origin, uint32_t time)
992{
993 int r;
994 struct dm_thin_device *td;
995
996 r = __open_device(pmd, origin, 0, &td);
997 if (r)
998 return r;
999
1000 td->changed = 1;
1001 td->snapshotted_time = time;
1002
1003 snap->mapped_blocks = td->mapped_blocks;
1004 snap->snapshotted_time = time;
1005 __close_device(td);
1006
1007 return 0;
1008}
1009
1010static int __create_snap(struct dm_pool_metadata *pmd,
1011 dm_thin_id dev, dm_thin_id origin)
1012{
1013 int r;
1014 dm_block_t origin_root;
1015 uint64_t key = origin, dev_key = dev;
1016 struct dm_thin_device *td;
1017 struct disk_device_details details_le;
1018 __le64 value;
1019
1020 /* check this device is unused */
1021 r = dm_btree_lookup(&pmd->details_info, pmd->details_root,
1022 &dev_key, &details_le);
1023 if (!r)
1024 return -EEXIST;
1025
1026 /* find the mapping tree for the origin */
1027 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &key, &value);
1028 if (r)
1029 return r;
1030 origin_root = le64_to_cpu(value);
1031
1032 /* clone the origin, an inc will do */
1033 dm_tm_inc(pmd->tm, origin_root);
1034
1035 /* insert into the main mapping tree */
1036 value = cpu_to_le64(origin_root);
1037 __dm_bless_for_disk(&value);
1038 key = dev;
1039 r = dm_btree_insert(&pmd->tl_info, pmd->root, &key, &value, &pmd->root);
1040 if (r) {
1041 dm_tm_dec(pmd->tm, origin_root);
1042 return r;
1043 }
1044
1045 pmd->time++;
1046
1047 r = __open_device(pmd, dev, 1, &td);
1048 if (r)
1049 goto bad;
1050
1051 r = __set_snapshot_details(pmd, td, origin, pmd->time);
Mike Snitzer1f3db252012-03-07 19:09:41 +00001052 __close_device(td);
1053
Joe Thornber991d9fa2011-10-31 20:21:18 +00001054 if (r)
1055 goto bad;
1056
Joe Thornber991d9fa2011-10-31 20:21:18 +00001057 return 0;
1058
1059bad:
Joe Thornber991d9fa2011-10-31 20:21:18 +00001060 dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1061 dm_btree_remove(&pmd->details_info, pmd->details_root,
1062 &key, &pmd->details_root);
1063 return r;
1064}
1065
1066int dm_pool_create_snap(struct dm_pool_metadata *pmd,
1067 dm_thin_id dev,
1068 dm_thin_id origin)
1069{
1070 int r;
1071
1072 down_write(&pmd->root_lock);
1073 r = __create_snap(pmd, dev, origin);
1074 up_write(&pmd->root_lock);
1075
1076 return r;
1077}
1078
1079static int __delete_device(struct dm_pool_metadata *pmd, dm_thin_id dev)
1080{
1081 int r;
1082 uint64_t key = dev;
1083 struct dm_thin_device *td;
1084
1085 /* TODO: failure should mark the transaction invalid */
1086 r = __open_device(pmd, dev, 0, &td);
1087 if (r)
1088 return r;
1089
1090 if (td->open_count > 1) {
1091 __close_device(td);
1092 return -EBUSY;
1093 }
1094
1095 list_del(&td->list);
1096 kfree(td);
1097 r = dm_btree_remove(&pmd->details_info, pmd->details_root,
1098 &key, &pmd->details_root);
1099 if (r)
1100 return r;
1101
1102 r = dm_btree_remove(&pmd->tl_info, pmd->root, &key, &pmd->root);
1103 if (r)
1104 return r;
1105
Joe Thornber991d9fa2011-10-31 20:21:18 +00001106 return 0;
1107}
1108
1109int dm_pool_delete_thin_device(struct dm_pool_metadata *pmd,
1110 dm_thin_id dev)
1111{
1112 int r;
1113
1114 down_write(&pmd->root_lock);
1115 r = __delete_device(pmd, dev);
1116 up_write(&pmd->root_lock);
1117
1118 return r;
1119}
1120
1121int dm_pool_set_metadata_transaction_id(struct dm_pool_metadata *pmd,
1122 uint64_t current_id,
1123 uint64_t new_id)
1124{
1125 down_write(&pmd->root_lock);
1126 if (pmd->trans_id != current_id) {
1127 up_write(&pmd->root_lock);
1128 DMERR("mismatched transaction id");
1129 return -EINVAL;
1130 }
1131
1132 pmd->trans_id = new_id;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001133 up_write(&pmd->root_lock);
1134
1135 return 0;
1136}
1137
1138int dm_pool_get_metadata_transaction_id(struct dm_pool_metadata *pmd,
1139 uint64_t *result)
1140{
1141 down_read(&pmd->root_lock);
1142 *result = pmd->trans_id;
1143 up_read(&pmd->root_lock);
1144
1145 return 0;
1146}
1147
Joe Thornbercc8394d2012-06-03 00:30:01 +01001148static int __reserve_metadata_snap(struct dm_pool_metadata *pmd)
1149{
1150 int r, inc;
1151 struct thin_disk_superblock *disk_super;
1152 struct dm_block *copy, *sblock;
1153 dm_block_t held_root;
1154
1155 /*
1156 * Copy the superblock.
1157 */
1158 dm_sm_inc_block(pmd->metadata_sm, THIN_SUPERBLOCK_LOCATION);
1159 r = dm_tm_shadow_block(pmd->tm, THIN_SUPERBLOCK_LOCATION,
1160 &sb_validator, &copy, &inc);
1161 if (r)
1162 return r;
1163
1164 BUG_ON(!inc);
1165
1166 held_root = dm_block_location(copy);
1167 disk_super = dm_block_data(copy);
1168
1169 if (le64_to_cpu(disk_super->held_root)) {
1170 DMWARN("Pool metadata snapshot already exists: release this before taking another.");
1171
1172 dm_tm_dec(pmd->tm, held_root);
1173 dm_tm_unlock(pmd->tm, copy);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001174 return -EBUSY;
1175 }
1176
1177 /*
1178 * Wipe the spacemap since we're not publishing this.
1179 */
1180 memset(&disk_super->data_space_map_root, 0,
1181 sizeof(disk_super->data_space_map_root));
1182 memset(&disk_super->metadata_space_map_root, 0,
1183 sizeof(disk_super->metadata_space_map_root));
1184
1185 /*
1186 * Increment the data structures that need to be preserved.
1187 */
1188 dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->data_mapping_root));
1189 dm_tm_inc(pmd->tm, le64_to_cpu(disk_super->device_details_root));
1190 dm_tm_unlock(pmd->tm, copy);
1191
1192 /*
1193 * Write the held root into the superblock.
1194 */
Joe Thornber25971192012-07-27 15:08:09 +01001195 r = superblock_lock(pmd, &sblock);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001196 if (r) {
1197 dm_tm_dec(pmd->tm, held_root);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001198 return r;
1199 }
1200
1201 disk_super = dm_block_data(sblock);
1202 disk_super->held_root = cpu_to_le64(held_root);
1203 dm_bm_unlock(sblock);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001204 return 0;
1205}
1206
1207int dm_pool_reserve_metadata_snap(struct dm_pool_metadata *pmd)
1208{
1209 int r;
1210
1211 down_write(&pmd->root_lock);
1212 r = __reserve_metadata_snap(pmd);
1213 up_write(&pmd->root_lock);
1214
1215 return r;
1216}
1217
1218static int __release_metadata_snap(struct dm_pool_metadata *pmd)
1219{
1220 int r;
1221 struct thin_disk_superblock *disk_super;
1222 struct dm_block *sblock, *copy;
1223 dm_block_t held_root;
1224
Joe Thornber25971192012-07-27 15:08:09 +01001225 r = superblock_lock(pmd, &sblock);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001226 if (r)
1227 return r;
1228
1229 disk_super = dm_block_data(sblock);
1230 held_root = le64_to_cpu(disk_super->held_root);
1231 disk_super->held_root = cpu_to_le64(0);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001232
1233 dm_bm_unlock(sblock);
1234
1235 if (!held_root) {
1236 DMWARN("No pool metadata snapshot found: nothing to release.");
1237 return -EINVAL;
1238 }
1239
1240 r = dm_tm_read_lock(pmd->tm, held_root, &sb_validator, &copy);
1241 if (r)
1242 return r;
1243
1244 disk_super = dm_block_data(copy);
1245 dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->data_mapping_root));
1246 dm_sm_dec_block(pmd->metadata_sm, le64_to_cpu(disk_super->device_details_root));
1247 dm_sm_dec_block(pmd->metadata_sm, held_root);
1248
1249 return dm_tm_unlock(pmd->tm, copy);
1250}
1251
1252int dm_pool_release_metadata_snap(struct dm_pool_metadata *pmd)
1253{
1254 int r;
1255
1256 down_write(&pmd->root_lock);
1257 r = __release_metadata_snap(pmd);
1258 up_write(&pmd->root_lock);
1259
1260 return r;
1261}
1262
1263static int __get_metadata_snap(struct dm_pool_metadata *pmd,
1264 dm_block_t *result)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001265{
1266 int r;
1267 struct thin_disk_superblock *disk_super;
1268 struct dm_block *sblock;
1269
Joe Thornbercc8394d2012-06-03 00:30:01 +01001270 r = dm_bm_read_lock(pmd->bm, THIN_SUPERBLOCK_LOCATION,
1271 &sb_validator, &sblock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001272 if (r)
1273 return r;
1274
1275 disk_super = dm_block_data(sblock);
1276 *result = le64_to_cpu(disk_super->held_root);
1277
1278 return dm_bm_unlock(sblock);
1279}
1280
Joe Thornbercc8394d2012-06-03 00:30:01 +01001281int dm_pool_get_metadata_snap(struct dm_pool_metadata *pmd,
1282 dm_block_t *result)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001283{
1284 int r;
1285
1286 down_read(&pmd->root_lock);
Joe Thornbercc8394d2012-06-03 00:30:01 +01001287 r = __get_metadata_snap(pmd, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001288 up_read(&pmd->root_lock);
1289
1290 return r;
1291}
1292
1293int dm_pool_open_thin_device(struct dm_pool_metadata *pmd, dm_thin_id dev,
1294 struct dm_thin_device **td)
1295{
1296 int r;
1297
1298 down_write(&pmd->root_lock);
1299 r = __open_device(pmd, dev, 0, td);
1300 up_write(&pmd->root_lock);
1301
1302 return r;
1303}
1304
1305int dm_pool_close_thin_device(struct dm_thin_device *td)
1306{
1307 down_write(&td->pmd->root_lock);
1308 __close_device(td);
1309 up_write(&td->pmd->root_lock);
1310
1311 return 0;
1312}
1313
1314dm_thin_id dm_thin_dev_id(struct dm_thin_device *td)
1315{
1316 return td->id;
1317}
1318
Mike Snitzer17b7d632012-07-27 15:07:57 +01001319static bool __snapshotted_since(struct dm_thin_device *td, uint32_t time)
Joe Thornber991d9fa2011-10-31 20:21:18 +00001320{
1321 return td->snapshotted_time > time;
1322}
1323
1324int dm_thin_find_block(struct dm_thin_device *td, dm_block_t block,
1325 int can_block, struct dm_thin_lookup_result *result)
1326{
1327 int r;
1328 uint64_t block_time = 0;
1329 __le64 value;
1330 struct dm_pool_metadata *pmd = td->pmd;
1331 dm_block_t keys[2] = { td->id, block };
1332
1333 if (can_block) {
1334 down_read(&pmd->root_lock);
1335 r = dm_btree_lookup(&pmd->info, pmd->root, keys, &value);
1336 if (!r)
1337 block_time = le64_to_cpu(value);
1338 up_read(&pmd->root_lock);
1339
1340 } else if (down_read_trylock(&pmd->root_lock)) {
1341 r = dm_btree_lookup(&pmd->nb_info, pmd->root, keys, &value);
1342 if (!r)
1343 block_time = le64_to_cpu(value);
1344 up_read(&pmd->root_lock);
1345
1346 } else
1347 return -EWOULDBLOCK;
1348
1349 if (!r) {
1350 dm_block_t exception_block;
1351 uint32_t exception_time;
1352 unpack_block_time(block_time, &exception_block,
1353 &exception_time);
1354 result->block = exception_block;
1355 result->shared = __snapshotted_since(td, exception_time);
1356 }
1357
1358 return r;
1359}
1360
1361static int __insert(struct dm_thin_device *td, dm_block_t block,
1362 dm_block_t data_block)
1363{
1364 int r, inserted;
1365 __le64 value;
1366 struct dm_pool_metadata *pmd = td->pmd;
1367 dm_block_t keys[2] = { td->id, block };
1368
Joe Thornber991d9fa2011-10-31 20:21:18 +00001369 value = cpu_to_le64(pack_block_time(data_block, pmd->time));
1370 __dm_bless_for_disk(&value);
1371
1372 r = dm_btree_insert_notify(&pmd->info, pmd->root, keys, &value,
1373 &pmd->root, &inserted);
1374 if (r)
1375 return r;
1376
1377 if (inserted) {
1378 td->mapped_blocks++;
1379 td->changed = 1;
1380 }
1381
1382 return 0;
1383}
1384
1385int dm_thin_insert_block(struct dm_thin_device *td, dm_block_t block,
1386 dm_block_t data_block)
1387{
1388 int r;
1389
1390 down_write(&td->pmd->root_lock);
1391 r = __insert(td, block, data_block);
1392 up_write(&td->pmd->root_lock);
1393
1394 return r;
1395}
1396
1397static int __remove(struct dm_thin_device *td, dm_block_t block)
1398{
1399 int r;
1400 struct dm_pool_metadata *pmd = td->pmd;
1401 dm_block_t keys[2] = { td->id, block };
1402
1403 r = dm_btree_remove(&pmd->info, pmd->root, keys, &pmd->root);
1404 if (r)
1405 return r;
1406
Joe Thornberaf63bcb2012-03-07 19:09:44 +00001407 td->mapped_blocks--;
1408 td->changed = 1;
Joe Thornber991d9fa2011-10-31 20:21:18 +00001409
1410 return 0;
1411}
1412
1413int dm_thin_remove_block(struct dm_thin_device *td, dm_block_t block)
1414{
1415 int r;
1416
1417 down_write(&td->pmd->root_lock);
1418 r = __remove(td, block);
1419 up_write(&td->pmd->root_lock);
1420
1421 return r;
1422}
1423
1424int dm_pool_alloc_data_block(struct dm_pool_metadata *pmd, dm_block_t *result)
1425{
1426 int r;
1427
1428 down_write(&pmd->root_lock);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001429 r = dm_sm_new_block(pmd->data_sm, result);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001430 up_write(&pmd->root_lock);
1431
1432 return r;
1433}
1434
1435int dm_pool_commit_metadata(struct dm_pool_metadata *pmd)
1436{
1437 int r;
1438
1439 down_write(&pmd->root_lock);
1440
1441 r = __commit_transaction(pmd);
1442 if (r <= 0)
1443 goto out;
1444
1445 /*
1446 * Open the next transaction.
1447 */
1448 r = __begin_transaction(pmd);
1449out:
1450 up_write(&pmd->root_lock);
1451 return r;
1452}
1453
1454int dm_pool_get_free_block_count(struct dm_pool_metadata *pmd, dm_block_t *result)
1455{
1456 int r;
1457
1458 down_read(&pmd->root_lock);
1459 r = dm_sm_get_nr_free(pmd->data_sm, result);
1460 up_read(&pmd->root_lock);
1461
1462 return r;
1463}
1464
1465int dm_pool_get_free_metadata_block_count(struct dm_pool_metadata *pmd,
1466 dm_block_t *result)
1467{
1468 int r;
1469
1470 down_read(&pmd->root_lock);
1471 r = dm_sm_get_nr_free(pmd->metadata_sm, result);
1472 up_read(&pmd->root_lock);
1473
1474 return r;
1475}
1476
1477int dm_pool_get_metadata_dev_size(struct dm_pool_metadata *pmd,
1478 dm_block_t *result)
1479{
1480 int r;
1481
1482 down_read(&pmd->root_lock);
1483 r = dm_sm_get_nr_blocks(pmd->metadata_sm, result);
1484 up_read(&pmd->root_lock);
1485
1486 return r;
1487}
1488
1489int dm_pool_get_data_block_size(struct dm_pool_metadata *pmd, sector_t *result)
1490{
1491 down_read(&pmd->root_lock);
1492 *result = pmd->data_block_size;
1493 up_read(&pmd->root_lock);
1494
1495 return 0;
1496}
1497
1498int dm_pool_get_data_dev_size(struct dm_pool_metadata *pmd, dm_block_t *result)
1499{
1500 int r;
1501
1502 down_read(&pmd->root_lock);
1503 r = dm_sm_get_nr_blocks(pmd->data_sm, result);
1504 up_read(&pmd->root_lock);
1505
1506 return r;
1507}
1508
1509int dm_thin_get_mapped_count(struct dm_thin_device *td, dm_block_t *result)
1510{
1511 struct dm_pool_metadata *pmd = td->pmd;
1512
1513 down_read(&pmd->root_lock);
1514 *result = td->mapped_blocks;
1515 up_read(&pmd->root_lock);
1516
1517 return 0;
1518}
1519
1520static int __highest_block(struct dm_thin_device *td, dm_block_t *result)
1521{
1522 int r;
1523 __le64 value_le;
1524 dm_block_t thin_root;
1525 struct dm_pool_metadata *pmd = td->pmd;
1526
1527 r = dm_btree_lookup(&pmd->tl_info, pmd->root, &td->id, &value_le);
1528 if (r)
1529 return r;
1530
1531 thin_root = le64_to_cpu(value_le);
1532
1533 return dm_btree_find_highest_key(&pmd->bl_info, thin_root, result);
1534}
1535
1536int dm_thin_get_highest_mapped_block(struct dm_thin_device *td,
1537 dm_block_t *result)
1538{
1539 int r;
1540 struct dm_pool_metadata *pmd = td->pmd;
1541
1542 down_read(&pmd->root_lock);
1543 r = __highest_block(td, result);
1544 up_read(&pmd->root_lock);
1545
1546 return r;
1547}
1548
1549static int __resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1550{
1551 int r;
1552 dm_block_t old_count;
1553
1554 r = dm_sm_get_nr_blocks(pmd->data_sm, &old_count);
1555 if (r)
1556 return r;
1557
1558 if (new_count == old_count)
1559 return 0;
1560
1561 if (new_count < old_count) {
1562 DMERR("cannot reduce size of data device");
1563 return -EINVAL;
1564 }
1565
Joe Thornbereb04cf62012-07-27 15:08:08 +01001566 return dm_sm_extend(pmd->data_sm, new_count - old_count);
Joe Thornber991d9fa2011-10-31 20:21:18 +00001567}
1568
1569int dm_pool_resize_data_dev(struct dm_pool_metadata *pmd, dm_block_t new_count)
1570{
1571 int r;
1572
1573 down_write(&pmd->root_lock);
1574 r = __resize_data_dev(pmd, new_count);
1575 up_write(&pmd->root_lock);
1576
1577 return r;
1578}