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>
master
Qu Wenruo 2015-05-15 14:28:23 +08:00 committed by David Sterba
parent af3b243358
commit e737a9d56b
1 changed files with 29 additions and 34 deletions

View File

@ -293,36 +293,30 @@ static int change_fsid_done(struct btrfs_fs_info *fs_info)
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;
/* caller should do extra check on passed uuid */
if (new_fsid) {
/* allocated mem will be freed at close_ctree() */
fs_info->new_fsid = malloc(BTRFS_FSID_SIZE);
if (!fs_info->new_fsid) {
ret = -ENOMEM;
goto out;
}
ret = uuid_parse(new_fsid, fs_info->new_fsid);
if (ret < 0)
goto out;
}
if (new_fsid_str)
uuid_parse(new_fsid_str, new_fsid);
else
uuid_generate(new_fsid);
if (new_chunk_uuid) {
/* allocated mem will be freed at close_ctree() */
fs_info->new_chunk_tree_uuid = malloc(BTRFS_UUID_SIZE);
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_generate(new_chunk_id);
fs_info->new_fsid = new_fsid;
fs_info->new_chunk_tree_uuid = new_chunk_id;
uuid_unparse_upper(new_fsid, uuid_buf);
printf("Changing fsid to %s\n", uuid_buf);
/* Now we can begin fsid change */
ret = change_fsid_prepare(fs_info);
if (ret < 0)
@ -342,19 +336,20 @@ static int change_uuid(struct btrfs_fs_info *fs_info, const char *new_fsid,
goto out;
}
/* Last, change fsid in super, only fsid change needs this */
if (new_fsid) {
memcpy(fs_info->fs_devices->fsid, fs_info->new_fsid,
BTRFS_FSID_SIZE);
memcpy(fs_info->super_copy->fsid, fs_info->new_fsid,
BTRFS_FSID_SIZE);
ret = write_all_supers(fs_info->tree_root);
if (ret < 0)
goto out;
}
/* Last, change fsid in super */
memcpy(fs_info->fs_devices->fsid, fs_info->new_fsid,
BTRFS_FSID_SIZE);
memcpy(fs_info->super_copy->fsid, fs_info->new_fsid,
BTRFS_FSID_SIZE);
ret = write_all_supers(fs_info->tree_root);
if (ret < 0)
goto out;
/* Now fsid change is done */
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:
return ret;
}