13 #include <linux/list.h>
75 #define DM_MSG_PREFIX "thin metadata"
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
87 #define THIN_MAX_CONCURRENT_LOCKS 5
90 #define SPACE_MAP_ROOT_SIZE 128
215 #define SUPERBLOCK_CSUM_XOR 160774
225 block_size -
sizeof(
__le32),
237 DMERR(
"sb_check failed: blocknr %llu: "
244 DMERR(
"sb_check failed: magic %llu: "
251 block_size -
sizeof(
__le32),
253 if (csum_le != disk_super->
csum) {
254 DMERR(
"sb_check failed: csum %u: wanted %u",
263 .name =
"superblock",
264 .prepare_for_write = sb_prepare_for_write,
274 return (b << 24) |
t;
280 *t = v & ((1 << 24) - 1);
283 static void data_block_inc(
void *
context,
void *value_le)
290 memcpy(&v_le, value_le,
sizeof(v_le));
292 dm_sm_inc_block(sm, b);
295 static void data_block_dec(
void *context,
void *value_le)
302 memcpy(&v_le, value_le,
sizeof(v_le));
304 dm_sm_dec_block(sm, b);
307 static int data_block_equal(
void *context,
void *value1_le,
void *value2_le)
313 memcpy(&v1_le, value1_le,
sizeof(v1_le));
314 memcpy(&v2_le, value2_le,
sizeof(v2_le));
321 static void subtree_inc(
void *context,
void *
value)
327 memcpy(&root_le, value,
sizeof(root_le));
332 static void subtree_dec(
void *context,
void *value)
338 memcpy(&root_le, value,
sizeof(root_le));
341 DMERR(
"btree delete failed\n");
344 static int subtree_equal(
void *context,
void *value1_le,
void *value2_le)
347 memcpy(&v1_le, value1_le,
sizeof(v1_le));
348 memcpy(&v2_le, value2_le,
sizeof(v2_le));
350 return v1_le == v2_le;
356 struct dm_block **sblock)
359 &sb_validator, sblock);
363 struct dm_block **sblock)
366 &sb_validator, sblock);
387 if (data_le[i] != zero) {
399 pmd->
info.levels = 2;
402 pmd->
info.value_type.inc = data_block_inc;
403 pmd->
info.value_type.dec = data_block_dec;
404 pmd->
info.value_type.equal = data_block_equal;
413 pmd->
tl_info.value_type.inc = subtree_inc;
414 pmd->
tl_info.value_type.dec = subtree_dec;
415 pmd->
tl_info.value_type.equal = subtree_equal;
421 pmd->
bl_info.value_type.inc = data_block_inc;
422 pmd->
bl_info.value_type.dec = data_block_dec;
423 pmd->
bl_info.value_type.equal = data_block_equal;
437 struct dm_block *sblock;
445 r = dm_sm_root_size(pmd->
metadata_sm, &metadata_len);
449 r = dm_sm_root_size(pmd->
data_sm, &data_len);
453 r = dm_sm_commit(pmd->
data_sm);
461 r = superblock_lock_zero(pmd, &sblock);
466 disk_super->
flags = 0;
470 disk_super->
time = 0;
504 DMERR(
"tm_create_with_sm failed");
510 DMERR(
"sm_disk_create failed");
517 DMERR(
"could not create non-blocking clone tm");
519 goto bad_cleanup_data_sm;
522 __setup_btree_details(pmd);
526 goto bad_cleanup_nb_tm;
530 DMERR(
"couldn't create devices root");
531 goto bad_cleanup_nb_tm;
534 r = __write_initial_superblock(pmd);
536 goto bad_cleanup_nb_tm;
558 DMERR(
"could not access metadata due to unsupported optional features (%lx).",
559 (
unsigned long)features);
566 if (get_disk_ro(pmd->
bdev->bd_disk))
571 DMERR(
"could not access metadata RDWR due to unsupported optional features (%lx).",
572 (
unsigned long)features);
582 struct dm_block *sblock;
586 &sb_validator, &sblock);
588 DMERR(
"couldn't read superblock");
594 r = __check_incompat_features(disk_super, pmd);
596 goto bad_unlock_sblock;
603 DMERR(
"tm_open_with_sm failed");
604 goto bad_unlock_sblock;
610 DMERR(
"sm_disk_open failed");
617 DMERR(
"could not create non-blocking clone tm");
619 goto bad_cleanup_data_sm;
622 __setup_btree_details(pmd);
636 static int __open_or_format_metadata(
struct dm_pool_metadata *pmd,
bool format_device)
640 r = __superblock_all_zeroes(pmd->
bm, &unformatted);
645 return format_device ? __format_metadata(pmd) : -
EPERM;
647 return __open_metadata(pmd);
650 static int __create_persistent_data_objects(
struct dm_pool_metadata *pmd,
bool format_device)
657 if (IS_ERR(pmd->
bm)) {
658 DMERR(
"could not create block manager");
659 return PTR_ERR(pmd->
bm);
662 r = __open_or_format_metadata(pmd, format_device);
682 struct dm_block *sblock;
689 &sb_validator, &sblock);
745 struct dm_block *sblock;
752 r = __write_changed_details(pmd);
756 r = dm_sm_commit(pmd->
data_sm);
764 r = dm_sm_root_size(pmd->
metadata_sm, &metadata_len);
768 r = dm_sm_root_size(pmd->
data_sm, &data_len);
772 r = superblock_lock(pmd, &sblock);
809 DMERR(
"could not allocate metadata struct");
821 r = __create_persistent_data_objects(pmd, format_device);
827 r = __begin_transaction(pmd);
830 DMWARN(
"%s: dm_pool_metadata_close() failed.", __func__);
840 unsigned open_devices = 0;
855 DMERR(
"attempt to close pmd when %u device(s) are still open",
861 r = __commit_transaction(pmd);
863 DMWARN(
"%s: __commit_transaction() failed, error = %d",
868 __destroy_persistent_data_objects(pmd);
892 if (td2->
id == dev) {
917 details_le.mapped_blocks = 0;
929 (*td)->open_count = 1;
931 (*td)->aborted_with_changes =
false;
932 (*td)->mapped_blocks =
le64_to_cpu(details_le.mapped_blocks);
933 (*td)->transaction_id =
le64_to_cpu(details_le.transaction_id);
934 (*td)->creation_time =
le32_to_cpu(details_le.creation_time);
935 (*td)->snapshotted_time =
le32_to_cpu(details_le.snapshotted_time);
980 r = __open_device(pmd, dev, 1, &td);
997 r = __create_thin(pmd, dev);
1010 r = __open_device(pmd, origin, 0, &td);
1036 &dev_key, &details_le);
1061 r = __open_device(pmd, dev, 1, &td);
1065 r = __set_snapshot_details(pmd, td, origin, pmd->
time);
1088 r = __create_snap(pmd, dev, origin);
1101 r = __open_device(pmd, dev, 0, &td);
1131 r = __delete_device(pmd, dev);
1149 DMERR(
"mismatched transaction id");
1181 struct dm_block *copy, *sblock;
1189 &sb_validator, ©, &inc);
1199 DMWARN(
"Pool metadata snapshot already exists: release this before taking another.");
1224 r = superblock_lock(pmd, &sblock);
1242 r = __reserve_metadata_snap(pmd);
1252 struct dm_block *sblock, *copy;
1255 r = superblock_lock(pmd, &sblock);
1266 DMWARN(
"No pool metadata snapshot found: nothing to release.");
1288 r = __release_metadata_snap(pmd);
1299 struct dm_block *sblock;
1302 &sb_validator, &sblock);
1319 r = __get_metadata_snap(pmd, result);
1332 r = __open_device(pmd, dev, 0, td);
1388 unpack_block_time(block_time, &exception_block,
1390 result->
block = exception_block;
1391 result->
shared = __snapshotted_since(td, exception_time);
1409 &pmd->
root, &inserted);
1426 if (!td->
pmd->fail_io)
1427 r = __insert(td, block, data_block);
1454 if (!td->
pmd->fail_io)
1455 r = __remove(td, block);
1489 r = dm_sm_new_block(pmd->
data_sm, result);
1503 r = __commit_transaction(pmd);
1510 r = __begin_transaction(pmd);
1532 __set_abort_with_changes_flags(pmd);
1533 __destroy_persistent_data_objects(pmd);
1534 r = __create_persistent_data_objects(pmd,
false);
1536 pmd->fail_io =
true;
1550 r = dm_sm_get_nr_free(pmd->
data_sm, result);
1576 r = dm_sm_get_nr_blocks(pmd->
metadata_sm, result);
1597 r = dm_sm_get_nr_blocks(pmd->
data_sm, result);
1642 r = __highest_block(td, result);
1653 r = dm_sm_get_nr_blocks(pmd->
data_sm, &old_count);
1657 if (new_count == old_count)
1660 if (new_count < old_count) {
1661 DMERR(
"cannot reduce size of data device");
1665 return dm_sm_extend(pmd->
data_sm, new_count - old_count);
1674 r = __resize_data_dev(pmd, new_count);