linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Qu Wenruo <quwenruo@cn.fujitsu.com>
To: <linux-btrfs@vger.kernel.org>
Cc: <dsterba@suse.cz>
Subject: [PATCH 1/4] btrfs-progs: Minor change of change_uuid().
Date: Fri, 15 May 2015 14:28:23 +0800	[thread overview]
Message-ID: <1431671306-22262-1-git-send-email-quwenruo@cn.fujitsu.com> (raw)

Change the change_uuid():
1) Remove new_chunk_tree_uuid para
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 dst 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>
---
 btrfstune.c | 63 ++++++++++++++++++++++++++++---------------------------------
 1 file changed, 29 insertions(+), 34 deletions(-)

diff --git a/btrfstune.c b/btrfstune.c
index 7b8090a..36d7a00 100644
--- a/btrfstune.c
+++ b/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);
 }
 
-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;
 }
-- 
2.4.0


             reply	other threads:[~2015-05-15  6:30 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-05-15  6:28 Qu Wenruo [this message]
2015-05-15  6:28 ` [PATCH 2/4] btrfs-progs: Add ability to restore unfinished fsid change Qu Wenruo
2015-05-15  6:28 ` [PATCH 3/4] btrfs-progs: Add '-U' and '-u' option for btrfstune Qu Wenruo
2015-05-15  6:28 ` [PATCH 4/4] btrfs-progs: Update the Document for offline fsid change Qu Wenruo
2015-05-26 15:24 ` [PATCH 1/4] btrfs-progs: Minor change of change_uuid() David Sterba

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1431671306-22262-1-git-send-email-quwenruo@cn.fujitsu.com \
    --to=quwenruo@cn.fujitsu.com \
    --cc=dsterba@suse.cz \
    --cc=linux-btrfs@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).