mirror of
https://github.com/kdave/btrfs-progs
synced 2025-03-11 05:07:51 +00:00
btrfs-progs: btrfstune: rework change_uuid
Change the change_uuid(): 1) Remove new_chunk_tree_uuid parameter As chunk_tree_uuid is only internal used, no need to manual specify it. Use random generated UUID instead. 2) Don't use heap allocated memory for fs_info->new_fsid/chunk_tree_id. It's easy to forgot free or double free heap memory. Use stack memory instead. (In fact, I forgot to free them in previous patchset) 3) Print destination fsid. As now it's possible to change fsid to random uuid, it's better to print it out. Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com> Signed-off-by: David Sterba <dsterba@suse.cz>
This commit is contained in:
parent
af3b243358
commit
e737a9d56b
63
btrfstune.c
63
btrfstune.c
@ -293,36 +293,30 @@ static int change_fsid_done(struct btrfs_fs_info *fs_info)
|
|||||||
return write_all_supers(fs_info->tree_root);
|
return write_all_supers(fs_info->tree_root);
|
||||||
}
|
}
|
||||||
|
|
||||||
static int change_uuid(struct btrfs_fs_info *fs_info, const char *new_fsid,
|
/*
|
||||||
const char *new_chunk_uuid)
|
* Change fsid of a given fs.
|
||||||
|
*
|
||||||
|
* If new_fsid_str is not given, use a random generated UUID.
|
||||||
|
*/
|
||||||
|
static int change_uuid(struct btrfs_fs_info *fs_info, const char *new_fsid_str)
|
||||||
{
|
{
|
||||||
|
uuid_t new_fsid;
|
||||||
|
uuid_t new_chunk_id;
|
||||||
|
char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
|
||||||
int ret = 0;
|
int ret = 0;
|
||||||
|
|
||||||
/* caller should do extra check on passed uuid */
|
/* caller should do extra check on passed uuid */
|
||||||
if (new_fsid) {
|
if (new_fsid_str)
|
||||||
/* allocated mem will be freed at close_ctree() */
|
uuid_parse(new_fsid_str, new_fsid);
|
||||||
fs_info->new_fsid = malloc(BTRFS_FSID_SIZE);
|
else
|
||||||
if (!fs_info->new_fsid) {
|
uuid_generate(new_fsid);
|
||||||
ret = -ENOMEM;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
ret = uuid_parse(new_fsid, fs_info->new_fsid);
|
|
||||||
if (ret < 0)
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_chunk_uuid) {
|
uuid_generate(new_chunk_id);
|
||||||
/* allocated mem will be freed at close_ctree() */
|
fs_info->new_fsid = new_fsid;
|
||||||
fs_info->new_chunk_tree_uuid = malloc(BTRFS_UUID_SIZE);
|
fs_info->new_chunk_tree_uuid = new_chunk_id;
|
||||||
if (!fs_info->new_chunk_tree_uuid) {
|
|
||||||
ret = -ENOMEM;
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
ret = uuid_parse(new_chunk_uuid, fs_info->new_chunk_tree_uuid);
|
|
||||||
if (ret < 0)
|
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
uuid_unparse_upper(new_fsid, uuid_buf);
|
||||||
|
printf("Changing fsid to %s\n", uuid_buf);
|
||||||
/* Now we can begin fsid change */
|
/* Now we can begin fsid change */
|
||||||
ret = change_fsid_prepare(fs_info);
|
ret = change_fsid_prepare(fs_info);
|
||||||
if (ret < 0)
|
if (ret < 0)
|
||||||
@ -342,19 +336,20 @@ static int change_uuid(struct btrfs_fs_info *fs_info, const char *new_fsid,
|
|||||||
goto out;
|
goto out;
|
||||||
}
|
}
|
||||||
|
|
||||||
/* Last, change fsid in super, only fsid change needs this */
|
/* Last, change fsid in super */
|
||||||
if (new_fsid) {
|
memcpy(fs_info->fs_devices->fsid, fs_info->new_fsid,
|
||||||
memcpy(fs_info->fs_devices->fsid, fs_info->new_fsid,
|
BTRFS_FSID_SIZE);
|
||||||
BTRFS_FSID_SIZE);
|
memcpy(fs_info->super_copy->fsid, fs_info->new_fsid,
|
||||||
memcpy(fs_info->super_copy->fsid, fs_info->new_fsid,
|
BTRFS_FSID_SIZE);
|
||||||
BTRFS_FSID_SIZE);
|
ret = write_all_supers(fs_info->tree_root);
|
||||||
ret = write_all_supers(fs_info->tree_root);
|
if (ret < 0)
|
||||||
if (ret < 0)
|
goto out;
|
||||||
goto out;
|
|
||||||
}
|
|
||||||
|
|
||||||
/* Now fsid change is done */
|
/* Now fsid change is done */
|
||||||
ret = change_fsid_done(fs_info);
|
ret = change_fsid_done(fs_info);
|
||||||
|
fs_info->new_fsid = NULL;
|
||||||
|
fs_info->new_chunk_tree_uuid = NULL;
|
||||||
|
printf("Fsid changed to %s\n", uuid_buf);
|
||||||
out:
|
out:
|
||||||
return ret;
|
return ret;
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user