Skip to content

Commit 7239ff4

Browse files
lorddoskiaskdave
authored andcommitted
btrfs: Introduce support for FSID change without metadata rewrite
This field is going to be used when the user wants to change the UUID of the filesystem without having to rewrite all metadata blocks. This field adds another level of indirection such that when the FSID is changed what really happens is the current UUID (the one with which the fs was created) is copied to the 'metadata_uuid' field in the superblock as well as a new incompat flag is set METADATA_UUID. When the kernel detects this flag is set it knows that the superblock in fact has 2 UUIDs: 1. Is the UUID which is user-visible, currently known as FSID. 2. Metadata UUID - this is the UUID which is stamped into all on-disk datastructures belonging to this file system. When the new incompat flag is present device scanning checks whether both fsid/metadata_uuid of the scanned device match any of the registered filesystems. When the flag is not set then both UUIDs are equal and only the FSID is retained on disk, metadata_uuid is set only in-memory during mount. Additionally a new metadata_uuid field is also added to the fs_info struct. It's initialised either with the FSID in case METADATA_UUID incompat flag is not set or with the metdata_uuid of the superblock otherwise. This commit introduces the new fields as well as the new incompat flag and switches all users of the fsid to the new logic. Signed-off-by: Nikolay Borisov <nborisov@suse.com> Reviewed-by: David Sterba <dsterba@suse.com> [ minor updates in comments ] Signed-off-by: David Sterba <dsterba@suse.com>
1 parent ce9f967 commit 7239ff4

8 files changed

Lines changed: 104 additions & 28 deletions

File tree

‎fs/btrfs/ctree.c‎

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -224,7 +224,7 @@ int btrfs_copy_root(struct btrfs_trans_handle *trans,
224224
else
225225
btrfs_set_header_owner(cow, new_root_objectid);
226226

227-
write_extent_buffer_fsid(cow, fs_info->fsid);
227+
write_extent_buffer_fsid(cow, fs_info->metadata_fsid);
228228

229229
WARN_ON(btrfs_header_generation(buf) > trans->transid);
230230
if (new_root_objectid == BTRFS_TREE_RELOC_OBJECTID)
@@ -1050,7 +1050,7 @@ static noinline int __btrfs_cow_block(struct btrfs_trans_handle *trans,
10501050
else
10511051
btrfs_set_header_owner(cow, root->root_key.objectid);
10521052

1053-
write_extent_buffer_fsid(cow, fs_info->fsid);
1053+
write_extent_buffer_fsid(cow, fs_info->metadata_fsid);
10541054

10551055
ret = update_ref_for_cow(trans, root, buf, cow, &last_ref);
10561056
if (ret) {

‎fs/btrfs/ctree.h‎

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -195,9 +195,10 @@ struct btrfs_root_backup {
195195
* it currently lacks any block count etc etc
196196
*/
197197
struct btrfs_super_block {
198-
u8 csum[BTRFS_CSUM_SIZE];
199198
/* the first 4 fields must match struct btrfs_header */
200-
u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
199+
u8 csum[BTRFS_CSUM_SIZE];
200+
/* FS specific UUID, visible to user */
201+
u8 fsid[BTRFS_FSID_SIZE];
201202
__le64 bytenr; /* this block number */
202203
__le64 flags;
203204

@@ -234,8 +235,11 @@ struct btrfs_super_block {
234235
__le64 cache_generation;
235236
__le64 uuid_tree_generation;
236237

238+
/* the UUID written into btree blocks */
239+
u8 metadata_uuid[BTRFS_FSID_SIZE];
240+
237241
/* future expansion */
238-
__le64 reserved[30];
242+
__le64 reserved[28];
239243
u8 sys_chunk_array[BTRFS_SYSTEM_CHUNK_ARRAY_SIZE];
240244
struct btrfs_root_backup super_roots[BTRFS_NUM_BACKUP_ROOTS];
241245
} __attribute__ ((__packed__));
@@ -265,7 +269,8 @@ struct btrfs_super_block {
265269
BTRFS_FEATURE_INCOMPAT_RAID56 | \
266270
BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF | \
267271
BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA | \
268-
BTRFS_FEATURE_INCOMPAT_NO_HOLES)
272+
BTRFS_FEATURE_INCOMPAT_NO_HOLES | \
273+
BTRFS_FEATURE_INCOMPAT_METADATA_UUID)
269274

270275
#define BTRFS_FEATURE_INCOMPAT_SAFE_SET \
271276
(BTRFS_FEATURE_INCOMPAT_EXTENDED_IREF)
@@ -768,7 +773,10 @@ bool btrfs_pinned_by_swapfile(struct btrfs_fs_info *fs_info, void *ptr);
768773
#define BTRFS_FS_BALANCE_RUNNING 18
769774

770775
struct btrfs_fs_info {
776+
/* User-visible fs UUID */
771777
u8 fsid[BTRFS_FSID_SIZE];
778+
/* UUID written to btree blocks */
779+
u8 metadata_fsid[BTRFS_FSID_SIZE];
772780
u8 chunk_tree_uuid[BTRFS_UUID_SIZE];
773781
unsigned long flags;
774782
struct btrfs_root *extent_root;

‎fs/btrfs/disk-io.c‎

Lines changed: 27 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -542,7 +542,7 @@ static int csum_dirty_buffer(struct btrfs_fs_info *fs_info, struct page *page)
542542
if (WARN_ON(!PageUptodate(page)))
543543
return -EUCLEAN;
544544

545-
ASSERT(memcmp_extent_buffer(eb, fs_info->fsid,
545+
ASSERT(memcmp_extent_buffer(eb, fs_info->metadata_fsid,
546546
btrfs_header_fsid(), BTRFS_FSID_SIZE) == 0);
547547

548548
return csum_tree_block(fs_info, eb, 0);
@@ -557,7 +557,20 @@ static int check_tree_block_fsid(struct btrfs_fs_info *fs_info,
557557

558558
read_extent_buffer(eb, fsid, btrfs_header_fsid(), BTRFS_FSID_SIZE);
559559
while (fs_devices) {
560-
if (!memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE)) {
560+
u8 *metadata_uuid;
561+
562+
/*
563+
* Checking the incompat flag is only valid for the current
564+
* fs. For seed devices it's forbidden to have their uuid
565+
* changed so reading ->fsid in this case is fine
566+
*/
567+
if (fs_devices == fs_info->fs_devices &&
568+
btrfs_fs_incompat(fs_info, METADATA_UUID))
569+
metadata_uuid = fs_devices->metadata_uuid;
570+
else
571+
metadata_uuid = fs_devices->fsid;
572+
573+
if (!memcmp(fsid, metadata_uuid, BTRFS_FSID_SIZE)) {
561574
ret = 0;
562575
break;
563576
}
@@ -2443,10 +2456,11 @@ static int validate_super(struct btrfs_fs_info *fs_info,
24432456
ret = -EINVAL;
24442457
}
24452458

2446-
if (memcmp(fs_info->fsid, sb->dev_item.fsid, BTRFS_FSID_SIZE) != 0) {
2459+
if (memcmp(fs_info->metadata_fsid, sb->dev_item.fsid,
2460+
BTRFS_FSID_SIZE) != 0) {
24472461
btrfs_err(fs_info,
2448-
"dev_item UUID does not match fsid: %pU != %pU",
2449-
fs_info->fsid, sb->dev_item.fsid);
2462+
"dev_item UUID does not match metadata fsid: %pU != %pU",
2463+
fs_info->metadata_fsid, sb->dev_item.fsid);
24502464
ret = -EINVAL;
24512465
}
24522466

@@ -2790,6 +2804,12 @@ int open_ctree(struct super_block *sb,
27902804
brelse(bh);
27912805

27922806
memcpy(fs_info->fsid, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
2807+
if (btrfs_fs_incompat(fs_info, METADATA_UUID)) {
2808+
memcpy(fs_info->metadata_fsid,
2809+
fs_info->super_copy->metadata_uuid, BTRFS_FSID_SIZE);
2810+
} else {
2811+
memcpy(fs_info->metadata_fsid, fs_info->fsid, BTRFS_FSID_SIZE);
2812+
}
27932813

27942814
ret = btrfs_validate_mount_super(fs_info);
27952815
if (ret) {
@@ -3728,7 +3748,8 @@ int write_all_supers(struct btrfs_fs_info *fs_info, int max_mirrors)
37283748
btrfs_set_stack_device_io_width(dev_item, dev->io_width);
37293749
btrfs_set_stack_device_sector_size(dev_item, dev->sector_size);
37303750
memcpy(dev_item->uuid, dev->uuid, BTRFS_UUID_SIZE);
3731-
memcpy(dev_item->fsid, dev->fs_devices->fsid, BTRFS_FSID_SIZE);
3751+
memcpy(dev_item->fsid, dev->fs_devices->metadata_uuid,
3752+
BTRFS_FSID_SIZE);
37323753

37333754
flags = btrfs_super_flags(sb);
37343755
btrfs_set_super_flags(sb, flags | BTRFS_HEADER_FLAG_WRITTEN);

‎fs/btrfs/extent-tree.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8312,7 +8312,7 @@ btrfs_init_new_buffer(struct btrfs_trans_handle *trans, struct btrfs_root *root,
83128312
btrfs_set_header_generation(buf, trans->transid);
83138313
btrfs_set_header_backref_rev(buf, BTRFS_MIXED_BACKREF_REV);
83148314
btrfs_set_header_owner(buf, owner);
8315-
write_extent_buffer_fsid(buf, fs_info->fsid);
8315+
write_extent_buffer_fsid(buf, fs_info->metadata_fsid);
83168316
write_extent_buffer_chunk_tree_uuid(buf, fs_info->chunk_tree_uuid);
83178317
if (root->root_key.objectid == BTRFS_TREE_LOG_OBJECTID) {
83188318
buf->log_index = root->log_transid % 2;

‎fs/btrfs/volumes.c‎

Lines changed: 59 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -245,13 +245,15 @@ struct list_head *btrfs_get_fs_uuids(void)
245245

246246
/*
247247
* alloc_fs_devices - allocate struct btrfs_fs_devices
248-
* @fsid: if not NULL, copy the uuid to fs_devices::fsid
248+
* @fsid: if not NULL, copy the UUID to fs_devices::fsid
249+
* @metadata_fsid: if not NULL, copy the UUID to fs_devices::metadata_fsid
249250
*
250251
* Return a pointer to a new struct btrfs_fs_devices on success, or ERR_PTR().
251252
* The returned struct is not linked onto any lists and can be destroyed with
252253
* kfree() right away.
253254
*/
254-
static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
255+
static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid,
256+
const u8 *metadata_fsid)
255257
{
256258
struct btrfs_fs_devices *fs_devs;
257259

@@ -268,6 +270,11 @@ static struct btrfs_fs_devices *alloc_fs_devices(const u8 *fsid)
268270
if (fsid)
269271
memcpy(fs_devs->fsid, fsid, BTRFS_FSID_SIZE);
270272

273+
if (metadata_fsid)
274+
memcpy(fs_devs->metadata_uuid, metadata_fsid, BTRFS_FSID_SIZE);
275+
else if (fsid)
276+
memcpy(fs_devs->metadata_uuid, fsid, BTRFS_FSID_SIZE);
277+
271278
return fs_devs;
272279
}
273280

@@ -375,13 +382,23 @@ static struct btrfs_device *find_device(struct btrfs_fs_devices *fs_devices,
375382
return NULL;
376383
}
377384

378-
static noinline struct btrfs_fs_devices *find_fsid(u8 *fsid)
385+
static noinline struct btrfs_fs_devices *find_fsid(
386+
const u8 *fsid, const u8 *metadata_fsid)
379387
{
380388
struct btrfs_fs_devices *fs_devices;
381389

390+
ASSERT(fsid);
391+
382392
list_for_each_entry(fs_devices, &fs_uuids, fs_list) {
383-
if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
384-
return fs_devices;
393+
if (metadata_fsid) {
394+
if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0
395+
&& memcmp(metadata_fsid, fs_devices->metadata_uuid,
396+
BTRFS_FSID_SIZE) == 0)
397+
return fs_devices;
398+
} else {
399+
if (memcmp(fsid, fs_devices->fsid, BTRFS_FSID_SIZE) == 0)
400+
return fs_devices;
401+
}
385402
}
386403
return NULL;
387404
}
@@ -716,6 +733,13 @@ static int btrfs_open_one_device(struct btrfs_fs_devices *fs_devices,
716733
device->generation = btrfs_super_generation(disk_super);
717734

718735
if (btrfs_super_flags(disk_super) & BTRFS_SUPER_FLAG_SEEDING) {
736+
if (btrfs_super_incompat_flags(disk_super) &
737+
BTRFS_FEATURE_INCOMPAT_METADATA_UUID) {
738+
pr_err(
739+
"BTRFS: Invalid seeding and uuid-changed device detected\n");
740+
goto error_brelse;
741+
}
742+
719743
clear_bit(BTRFS_DEV_STATE_WRITEABLE, &device->dev_state);
720744
fs_devices->seeding = 1;
721745
} else {
@@ -766,10 +790,21 @@ static noinline struct btrfs_device *device_list_add(const char *path,
766790
struct rcu_string *name;
767791
u64 found_transid = btrfs_super_generation(disk_super);
768792
u64 devid = btrfs_stack_device_id(&disk_super->dev_item);
793+
bool has_metadata_uuid = (btrfs_super_incompat_flags(disk_super) &
794+
BTRFS_FEATURE_INCOMPAT_METADATA_UUID);
795+
796+
if (has_metadata_uuid)
797+
fs_devices = find_fsid(disk_super->fsid, disk_super->metadata_uuid);
798+
else
799+
fs_devices = find_fsid(disk_super->fsid, NULL);
769800

770-
fs_devices = find_fsid(disk_super->fsid);
771801
if (!fs_devices) {
772-
fs_devices = alloc_fs_devices(disk_super->fsid);
802+
if (has_metadata_uuid)
803+
fs_devices = alloc_fs_devices(disk_super->fsid,
804+
disk_super->metadata_uuid);
805+
else
806+
fs_devices = alloc_fs_devices(disk_super->fsid, NULL);
807+
773808
if (IS_ERR(fs_devices))
774809
return ERR_CAST(fs_devices);
775810

@@ -920,7 +955,7 @@ static struct btrfs_fs_devices *clone_fs_devices(struct btrfs_fs_devices *orig)
920955
struct btrfs_device *device;
921956
struct btrfs_device *orig_dev;
922957

923-
fs_devices = alloc_fs_devices(orig->fsid);
958+
fs_devices = alloc_fs_devices(orig->fsid, NULL);
924959
if (IS_ERR(fs_devices))
925960
return fs_devices;
926961

@@ -1745,7 +1780,8 @@ static int btrfs_add_dev_item(struct btrfs_trans_handle *trans,
17451780
ptr = btrfs_device_uuid(dev_item);
17461781
write_extent_buffer(leaf, device->uuid, ptr, BTRFS_UUID_SIZE);
17471782
ptr = btrfs_device_fsid(dev_item);
1748-
write_extent_buffer(leaf, trans->fs_info->fsid, ptr, BTRFS_FSID_SIZE);
1783+
write_extent_buffer(leaf, trans->fs_info->metadata_fsid, ptr,
1784+
BTRFS_FSID_SIZE);
17491785
btrfs_mark_buffer_dirty(leaf);
17501786

17511787
ret = 0;
@@ -2176,7 +2212,13 @@ static struct btrfs_device *btrfs_find_device_by_path(
21762212
disk_super = (struct btrfs_super_block *)bh->b_data;
21772213
devid = btrfs_stack_device_id(&disk_super->dev_item);
21782214
dev_uuid = disk_super->dev_item.uuid;
2179-
device = btrfs_find_device(fs_info, devid, dev_uuid, disk_super->fsid);
2215+
if (btrfs_fs_incompat(fs_info, METADATA_UUID))
2216+
device = btrfs_find_device(fs_info, devid, dev_uuid,
2217+
disk_super->metadata_uuid);
2218+
else
2219+
device = btrfs_find_device(fs_info, devid,
2220+
dev_uuid, disk_super->fsid);
2221+
21802222
brelse(bh);
21812223
if (!device)
21822224
device = ERR_PTR(-ENOENT);
@@ -2246,7 +2288,7 @@ static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
22462288
if (!fs_devices->seeding)
22472289
return -EINVAL;
22482290

2249-
seed_devices = alloc_fs_devices(NULL);
2291+
seed_devices = alloc_fs_devices(NULL, NULL);
22502292
if (IS_ERR(seed_devices))
22512293
return PTR_ERR(seed_devices);
22522294

@@ -2283,6 +2325,8 @@ static int btrfs_prepare_sprout(struct btrfs_fs_info *fs_info)
22832325

22842326
generate_random_uuid(fs_devices->fsid);
22852327
memcpy(fs_info->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
2328+
memcpy(fs_devices->metadata_uuid, fs_devices->fsid, BTRFS_FSID_SIZE);
2329+
memcpy(fs_info->metadata_fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
22862330
memcpy(disk_super->fsid, fs_devices->fsid, BTRFS_FSID_SIZE);
22872331
mutex_unlock(&fs_devices->device_list_mutex);
22882332

@@ -6294,7 +6338,7 @@ struct btrfs_device *btrfs_find_device(struct btrfs_fs_info *fs_info, u64 devid,
62946338
cur_devices = fs_info->fs_devices;
62956339
while (cur_devices) {
62966340
if (!fsid ||
6297-
!memcmp(cur_devices->fsid, fsid, BTRFS_FSID_SIZE)) {
6341+
!memcmp(cur_devices->metadata_uuid, fsid, BTRFS_FSID_SIZE)) {
62986342
device = find_device(cur_devices, devid, uuid);
62996343
if (device)
63006344
return device;
@@ -6623,12 +6667,12 @@ static struct btrfs_fs_devices *open_seed_devices(struct btrfs_fs_info *fs_info,
66236667
fs_devices = fs_devices->seed;
66246668
}
66256669

6626-
fs_devices = find_fsid(fsid);
6670+
fs_devices = find_fsid(fsid, NULL);
66276671
if (!fs_devices) {
66286672
if (!btrfs_test_opt(fs_info, DEGRADED))
66296673
return ERR_PTR(-ENOENT);
66306674

6631-
fs_devices = alloc_fs_devices(fsid);
6675+
fs_devices = alloc_fs_devices(fsid, NULL);
66326676
if (IS_ERR(fs_devices))
66336677
return fs_devices;
66346678

@@ -6678,7 +6722,7 @@ static int read_one_dev(struct btrfs_fs_info *fs_info,
66786722
read_extent_buffer(leaf, fs_uuid, btrfs_device_fsid(dev_item),
66796723
BTRFS_FSID_SIZE);
66806724

6681-
if (memcmp(fs_uuid, fs_info->fsid, BTRFS_FSID_SIZE)) {
6725+
if (memcmp(fs_uuid, fs_info->metadata_fsid, BTRFS_FSID_SIZE)) {
66826726
fs_devices = open_seed_devices(fs_info, fs_uuid);
66836727
if (IS_ERR(fs_devices))
66846728
return PTR_ERR(fs_devices);

‎fs/btrfs/volumes.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,7 @@ BTRFS_DEVICE_GETSET_FUNCS(bytes_used);
210210

211211
struct btrfs_fs_devices {
212212
u8 fsid[BTRFS_FSID_SIZE]; /* FS specific uuid */
213+
u8 metadata_uuid[BTRFS_FSID_SIZE];
213214
struct list_head fs_list;
214215

215216
u64 num_devices;

‎include/uapi/linux/btrfs.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -269,6 +269,7 @@ struct btrfs_ioctl_fs_info_args {
269269
#define BTRFS_FEATURE_INCOMPAT_RAID56 (1ULL << 7)
270270
#define BTRFS_FEATURE_INCOMPAT_SKINNY_METADATA (1ULL << 8)
271271
#define BTRFS_FEATURE_INCOMPAT_NO_HOLES (1ULL << 9)
272+
#define BTRFS_FEATURE_INCOMPAT_METADATA_UUID (1ULL << 10)
272273

273274
struct btrfs_ioctl_feature_flags {
274275
__u64 compat_flags;

‎include/uapi/linux/btrfs_tree.h‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,7 @@ struct btrfs_free_space_header {
458458
#define BTRFS_SUPER_FLAG_METADUMP (1ULL << 33)
459459
#define BTRFS_SUPER_FLAG_METADUMP_V2 (1ULL << 34)
460460
#define BTRFS_SUPER_FLAG_CHANGING_FSID (1ULL << 35)
461+
#define BTRFS_SUPER_FLAG_CHANGING_FSID_V2 (1ULL << 36)
461462

462463

463464
/*

0 commit comments

Comments
 (0)