All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH 1/4] btrfs-progs: Minor change of change_uuid().
@ 2015-05-15  6:28 Qu Wenruo
  2015-05-15  6:28 ` [PATCH 2/4] btrfs-progs: Add ability to restore unfinished fsid change Qu Wenruo
                   ` (3 more replies)
  0 siblings, 4 replies; 5+ messages in thread
From: Qu Wenruo @ 2015-05-15  6:28 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba

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


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 2/4] btrfs-progs: Add ability to restore unfinished fsid change.
  2015-05-15  6:28 [PATCH 1/4] btrfs-progs: Minor change of change_uuid() Qu Wenruo
@ 2015-05-15  6:28 ` Qu Wenruo
  2015-05-15  6:28 ` [PATCH 3/4] btrfs-progs: Add '-U' and '-u' option for btrfstune Qu Wenruo
                   ` (2 subsequent siblings)
  3 siblings, 0 replies; 5+ messages in thread
From: Qu Wenruo @ 2015-05-15  6:28 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba

Now change_uuid() can auto detected unfinished fsid change and restore
it.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 btrfstune.c | 60 +++++++++++++++++++++++++++++++++++++++++++++++++++++-------
 1 file changed, 53 insertions(+), 7 deletions(-)

diff --git a/btrfstune.c b/btrfstune.c
index 36d7a00..1d5af33 100644
--- a/btrfstune.c
+++ b/btrfstune.c
@@ -267,7 +267,9 @@ out:
 
 static int change_fsid_prepare(struct btrfs_fs_info *fs_info)
 {
+	struct btrfs_root *tree_root = fs_info->tree_root;
 	u64 flags = btrfs_super_flags(fs_info->super_copy);
+	int ret = 0;
 
 	if (!fs_info->new_fsid && !fs_info->new_chunk_tree_uuid)
 		return 0;
@@ -276,7 +278,16 @@ static int change_fsid_prepare(struct btrfs_fs_info *fs_info)
 		flags |= BTRFS_SUPER_FLAG_CHANGING_FSID;
 	btrfs_set_super_flags(fs_info->super_copy, flags);
 
-	return write_all_supers(fs_info->tree_root);
+	memcpy(fs_info->super_copy->fsid, fs_info->new_fsid, BTRFS_FSID_SIZE);
+	ret = write_all_supers(tree_root);
+	if (ret < 0)
+		return ret;
+
+	/* also restore new chunk_tree_id into tree_root for restore */
+	write_extent_buffer(tree_root->node, fs_info->new_chunk_tree_uuid,
+			    btrfs_header_chunk_tree_uuid(tree_root->node),
+			    BTRFS_UUID_SIZE);
+	return write_tree_block(NULL, tree_root, tree_root->node);
 }
 
 static int change_fsid_done(struct btrfs_fs_info *fs_info)
@@ -294,9 +305,31 @@ static int change_fsid_done(struct btrfs_fs_info *fs_info)
 }
 
 /*
+ * Return 0 for no unfinished fsid change.
+ * Return >0 for unfinished fsid change, and restore unfinished fsid/
+ * chunk_tree_id into fsid_ret/chunk_id_ret.
+ */
+static int check_unfinished_fsid_change(struct btrfs_fs_info *fs_info,
+					uuid_t fsid_ret, uuid_t chunk_id_ret)
+{
+	struct btrfs_root *tree_root = fs_info->tree_root;
+	u64 flags = btrfs_super_flags(fs_info->super_copy);
+
+	if (flags & BTRFS_SUPER_FLAG_CHANGING_FSID) {
+		memcpy(fsid_ret, fs_info->super_copy->fsid, BTRFS_FSID_SIZE);
+		read_extent_buffer(tree_root->node, chunk_id_ret,
+				btrfs_header_chunk_tree_uuid(tree_root->node),
+				BTRFS_UUID_SIZE);
+		return 1;
+	}
+	return 0;
+}
+
+/*
  * Change fsid of a given fs.
  *
  * If new_fsid_str is not given, use a random generated UUID.
+ * Caller should check new_fsid_str is valid
  */
 static int change_uuid(struct btrfs_fs_info *fs_info, const char *new_fsid_str)
 {
@@ -305,13 +338,26 @@ static int change_uuid(struct btrfs_fs_info *fs_info, const char *new_fsid_str)
 	char uuid_buf[BTRFS_UUID_UNPARSED_SIZE];
 	int ret = 0;
 
-	/* caller should do extra check on passed uuid */
-	if (new_fsid_str)
-		uuid_parse(new_fsid_str, new_fsid);
-	else
-		uuid_generate(new_fsid);
+	if (check_unfinished_fsid_change(fs_info, new_fsid, new_chunk_id)) {
+		if (new_fsid_str) {
+			uuid_t tmp;
 
-	uuid_generate(new_chunk_id);
+			uuid_parse(new_fsid_str, tmp);
+			if (memcmp(tmp, new_fsid, BTRFS_FSID_SIZE)) {
+				fprintf(stderr,
+					"ERROR: New fsid %s is not the same with unfinished fsid change\n",
+					new_fsid_str);
+				return -EINVAL;
+			}
+		}
+	} else {
+		if (new_fsid_str)
+			uuid_parse(new_fsid_str, new_fsid);
+		else
+			uuid_generate(new_fsid);
+
+		uuid_generate(new_chunk_id);
+	}
 	fs_info->new_fsid = new_fsid;
 	fs_info->new_chunk_tree_uuid = new_chunk_id;
 
-- 
2.4.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 3/4] btrfs-progs: Add '-U' and '-u' option for btrfstune.
  2015-05-15  6:28 [PATCH 1/4] btrfs-progs: Minor change of change_uuid() Qu Wenruo
  2015-05-15  6:28 ` [PATCH 2/4] btrfs-progs: Add ability to restore unfinished fsid change Qu Wenruo
@ 2015-05-15  6:28 ` 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
  3 siblings, 0 replies; 5+ messages in thread
From: Qu Wenruo @ 2015-05-15  6:28 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba

Add two options, '-U' and '-u' for btrfstune.

For '-u', it is used to change fsid to a random new UUID.
For '-U', it is used to change fsid to a specified UUID.

Both will also change the internal use only chunk_tree_uuid to a random
new UUID.

Although there is a GNU getopt extension "::" to get optional
option-argument, but is forbidden by POSIX.1-2008, so use split options
here.

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 btrfstune.c | 67 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++-----
 1 file changed, 62 insertions(+), 5 deletions(-)

diff --git a/btrfstune.c b/btrfstune.c
index 1d5af33..e324f83 100644
--- a/btrfstune.c
+++ b/btrfstune.c
@@ -406,23 +406,27 @@ static void print_usage(void)
 	fprintf(stderr, "\t-S value\tpositive value will enable seeding, zero to disable, negative is not allowed\n");
 	fprintf(stderr, "\t-r \t\tenable extended inode refs\n");
 	fprintf(stderr, "\t-x \t\tenable skinny metadata extent refs\n");
-	fprintf(stderr, "\t-f \t\tforce to set or clear flags, make sure that you are aware of the dangers\n");
+	fprintf(stderr, "\t-f \t\tforce to do dangerous operation, make sure that you are aware of the dangers\n");
+	fprintf(stderr, "\t-U [uuid]\t\tchange fsid, if uuid is not given, use a random one\n");
 }
 
 int main(int argc, char *argv[])
 {
 	struct btrfs_root *root;
+	enum btrfs_open_ctree_flags ctree_flags = OPEN_CTREE_WRITES;
 	int success = 0;
 	int total = 0;
 	int extrefs_flag = 0;
 	int seeding_flag = 0;
 	u64 seeding_value = 0;
 	int skinny_flag = 0;
+	int random_fsid = 0;
+	char *new_fsid_str = NULL;
 	int ret;
 
 	optind = 1;
 	while(1) {
-		int c = getopt(argc, argv, "S:rxf");
+		int c = getopt(argc, argv, "S:rxfuU:");
 		if (c < 0)
 			break;
 		switch(c) {
@@ -439,6 +443,14 @@ int main(int argc, char *argv[])
 		case 'f':
 			force = 1;
 			break;
+		case 'U':
+			ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH;
+			new_fsid_str = optarg;
+			break;
+		case 'u':
+			ctree_flags |= OPEN_CTREE_IGNORE_FSID_MISMATCH;
+			random_fsid = 1;
+			break;
 		default:
 			print_usage();
 			return 1;
@@ -453,13 +465,37 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
-	if (!(seeding_flag + extrefs_flag + skinny_flag)) {
+	if (random_fsid && new_fsid_str) {
+		fprintf(stderr,
+			"ERROR: Random fsid can't be used with specified fsid\n");
+		return 1;
+	}
+	if (!(seeding_flag + extrefs_flag + skinny_flag) &&
+	    !(random_fsid || new_fsid_str)) {
 		fprintf(stderr,
 			"ERROR: At least one option should be assigned.\n");
 		print_usage();
 		return 1;
 	}
 
+	if (new_fsid_str) {
+		uuid_t tmp;
+
+		ret = uuid_parse(new_fsid_str, tmp);
+		if (ret < 0) {
+			fprintf(stderr,
+				"ERROR: Could not parse UUID: %s\n",
+				new_fsid_str);
+			return 1;
+		}
+		if (!test_uuid_unique(new_fsid_str)) {
+			fprintf(stderr,
+				"ERROR: Fsid %s is not unique\n",
+				new_fsid_str);
+			return 1;
+		}
+	}
+
 	ret = check_mounted(device);
 	if (ret < 0) {
 		fprintf(stderr, "Could not check mount status: %s\n",
@@ -470,7 +506,7 @@ int main(int argc, char *argv[])
 		return 1;
 	}
 
-	root = open_ctree(device, 0, OPEN_CTREE_WRITES);
+	root = open_ctree(device, 0, ctree_flags);
 
 	if (!root) {
 		fprintf(stderr, "Open ctree failed\n");
@@ -483,7 +519,8 @@ int main(int argc, char *argv[])
 			ret = ask_user("We are going to clear the seeding flag, are you sure?");
 			if (!ret) {
 				fprintf(stderr, "Clear seeding flag canceled\n");
-				return 1;
+				ret = 1;
+				goto out;
 			}
 		}
 
@@ -505,6 +542,25 @@ int main(int argc, char *argv[])
 		total++;
 	}
 
+	if (random_fsid || new_fsid_str) {
+		if (!force) {
+			fprintf(stderr,
+				"Warning: It's highly recommended to run 'btrfs check' before this operation\n");
+			fprintf(stderr,
+				"Also canceling running UUID change progress may cause corruption\n");
+			ret = ask_user("We are going to change UUID, are your sure?");
+			if (!ret) {
+				fprintf(stderr, "UUID change canceled\n");
+				ret = 1;
+				goto out;
+			}
+		}
+		ret = change_uuid(root->fs_info, new_fsid_str);
+		if (!ret)
+			success++;
+		total++;
+	}
+
 	if (success == total) {
 		ret = 0;
 	} else {
@@ -512,6 +568,7 @@ int main(int argc, char *argv[])
 		ret = 1;
 		fprintf(stderr, "btrfstune failed\n");
 	}
+out:
 	close_ctree(root);
 
 	return ret;
-- 
2.4.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* [PATCH 4/4] btrfs-progs: Update the Document for offline fsid change.
  2015-05-15  6:28 [PATCH 1/4] btrfs-progs: Minor change of change_uuid() Qu Wenruo
  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 ` Qu Wenruo
  2015-05-26 15:24 ` [PATCH 1/4] btrfs-progs: Minor change of change_uuid() David Sterba
  3 siblings, 0 replies; 5+ messages in thread
From: Qu Wenruo @ 2015-05-15  6:28 UTC (permalink / raw)
  To: linux-btrfs; +Cc: dsterba

Signed-off-by: Qu Wenruo <quwenruo@cn.fujitsu.com>
---
 Documentation/btrfstune.asciidoc | 18 ++++++++++++++++--
 1 file changed, 16 insertions(+), 2 deletions(-)

diff --git a/Documentation/btrfstune.asciidoc b/Documentation/btrfstune.asciidoc
index 9620221..6927758 100644
--- a/Documentation/btrfstune.asciidoc
+++ b/Documentation/btrfstune.asciidoc
@@ -25,8 +25,22 @@ Enable extended inode refs.
 -x::
 Enable skinny metadata extent refs.
 -f::
-Allow dangerous changes, e.g. clear the seeding flag. Make sure that you are
-aware of the dangers.
+Allow dangerous changes, e.g. clear the seeding flag or change fsid. Make sure
+that you are aware of the dangers.
+-u::
+Change fsid to a random generated UUID.
+If there is a previous unfinished fsid change, it will continue the unfinished
+fsid change.
+-U <UUID>::
+Change fsid to <UUID>.
+The <UUID> should be a 36 bytes string in `printf`(3) format
+"%08x-%04x-%04x-%04x-%012x".
+If there is a previous unfinished fsid change, it will only continue if the
+<UUID> matches the unfinished one.
+
+WARNING: Canceling a running UUID progress will cause the filesystem unmountable.
+To fix it, please rerun 'btrfstune -u' to restore the UUID and wait it complete.
+
 
 When mounting the new device, btrfs will check whether the seeding flag is set
 when try to open seeding device.  If the user clears the seeding flag of the
-- 
2.4.0


^ permalink raw reply related	[flat|nested] 5+ messages in thread

* Re: [PATCH 1/4] btrfs-progs: Minor change of change_uuid().
  2015-05-15  6:28 [PATCH 1/4] btrfs-progs: Minor change of change_uuid() Qu Wenruo
                   ` (2 preceding siblings ...)
  2015-05-15  6:28 ` [PATCH 4/4] btrfs-progs: Update the Document for offline fsid change Qu Wenruo
@ 2015-05-26 15:24 ` David Sterba
  3 siblings, 0 replies; 5+ messages in thread
From: David Sterba @ 2015-05-26 15:24 UTC (permalink / raw)
  To: Qu Wenruo; +Cc: linux-btrfs, dsterba

On Fri, May 15, 2015 at 02:28:23PM +0800, Qu Wenruo wrote:
> 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>

All of the patchset applied, thanks!

^ permalink raw reply	[flat|nested] 5+ messages in thread

end of thread, other threads:[~2015-05-26 15:24 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2015-05-15  6:28 [PATCH 1/4] btrfs-progs: Minor change of change_uuid() Qu Wenruo
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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.