linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH 0/5] btrfs-progs: Introduce --extra-dev option
@ 2015-09-04 13:24 Zhao Lei
  2015-09-04 13:24 ` [PATCH 1/5] " Zhao Lei
                   ` (4 more replies)
  0 siblings, 5 replies; 6+ messages in thread
From: Zhao Lei @ 2015-09-04 13:24 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

Some command in current tools can't support filesystem with multiple
loop device, for example:
 # truncate -s 200M ./1.img
 # truncate -s 200M ./2.img
 # mkfs.btrfs -f ./1.img ./2.img
 #
 # btrfs rescue chunk-recover -v ./1.img
 All Devices:
        Device: id = 1, name = ./1.img
 ...
 # btrfs rescue super-recover -v ./1.img
 All Devices:
        Device: id = 1, name = ./1.img
 ...
 # btrfs rescue zero-log ./1.img
 warning devid 2 not found already
 Clearing log on ./1.img, previous log_root 0, level 0
 ...

As a workaround, we can bind all of above image files to loop device,
but to make things simple, this patch introduce --extra-dev option,
to specify non-block-device manually, as:
 # btrfs rescue chunk-recover -v --extra-device=./2.img ./1.img
 All Devices:
        Device: id = 2, name = ./2.img
        Device: id = 1, name = ./1.img
 ...
 # btrfs rescue super-recover --extra-dev=./2.img -v ./1.img
 All Devices:
        Device: id = 2, name = ./2.img
        Device: id = 1, name = ./1.img
 ...
 # btrfs rescue zero-log --extra-dev=./2.img ./1.img
 Clearing log on ./1.img, previous log_root 0, level 0

We can add multiple extra-dev by:
 --extra-dev="1.img,2.img"
 or
 --extra-dev=1.img --extra-dev=2.img

Zhao Lei (5):
  btrfs-progs: Introduce --extra-dev option
  btrfs-progs: Support extra-dev for btrfs rescue
  btrfs-progs: Avoid using uninitialized optind in cmd_rescue_zero_log
  btrfs-progs: add help command to btrfs rescue zero-log
  btrfs-progs: update manual for --extra-dev and -h for btrfs rescue

 Documentation/btrfs-rescue.asciidoc | 13 ++++++-
 cmds-rescue.c                       | 39 +++++++++++++++++----
 utils.c                             | 70 +++++++++++++++++++++++++++++++++++++
 utils.h                             |  5 +++
 4 files changed, 119 insertions(+), 8 deletions(-)

-- 
1.8.5.1


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

* [PATCH 1/5] btrfs-progs: Introduce --extra-dev option
  2015-09-04 13:24 [PATCH 0/5] btrfs-progs: Introduce --extra-dev option Zhao Lei
@ 2015-09-04 13:24 ` Zhao Lei
  2015-09-04 13:24 ` [PATCH 2/5] btrfs-progs: Support extra-dev for btrfs rescue Zhao Lei
                   ` (3 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Zhao Lei @ 2015-09-04 13:24 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

Some command in current tools can't support filesystem with multiple
loop device, for example:
 # truncate -s 200M ./1.img
 # truncate -s 200M ./2.img
 # mkfs.btrfs -f ./1.img ./2.img
 #
 # btrfs rescue chunk-recover -v ./1.img
 All Devices:
        Device: id = 1, name = ./1.img
 ...
 # btrfs rescue super-recover -v ./1.img
 All Devices:
        Device: id = 1, name = ./1.img
 ...
 # btrfs rescue zero-log ./1.img
 warning devid 2 not found already
 Clearing log on ./1.img, previous log_root 0, level 0
 ...

As a workaround, we can bind all of above image files to loop device,
but to make things simple, this patch introduce --extra-dev option,
to specify non-block-device manually, as:
 # btrfs rescue chunk-recover -v --extra-device=./2.img ./1.img
 All Devices:
        Device: id = 2, name = ./2.img
        Device: id = 1, name = ./1.img
 ...
 # btrfs rescue super-recover --extra-dev=./2.img -v ./1.img
 All Devices:
        Device: id = 2, name = ./2.img
        Device: id = 1, name = ./1.img
 ...
 # btrfs rescue zero-log --extra-dev=./2.img ./1.img
 Clearing log on ./1.img, previous log_root 0, level 0

We can add multiple extra-dev by:
 --extra-dev="1.img,2.img"
 or
 --extra-dev=1.img --extra-dev=2.img

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 utils.c | 70 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
 utils.h |  5 +++++
 2 files changed, 75 insertions(+)

diff --git a/utils.c b/utils.c
index 52791b5..ed1fb13 100644
--- a/utils.c
+++ b/utils.c
@@ -56,6 +56,8 @@ static int btrfs_scan_done = 0;
 
 static char argv0_buf[ARGV0_BUF_SIZE] = "btrfs";
 
+char btrfs_extra_devs[1024][PATH_MAX];
+
 const char *get_argv0_buf(void)
 {
 	return argv0_buf;
@@ -2560,6 +2562,7 @@ int btrfs_scan_lblkid()
 	blkid_dev dev = NULL;
 	blkid_cache cache = NULL;
 	char path[PATH_MAX];
+	int i;
 
 	if (btrfs_scan_done)
 		return 0;
@@ -2596,6 +2599,29 @@ int btrfs_scan_lblkid()
 	blkid_dev_iterate_end(iter);
 	blkid_put_cache(cache);
 
+	for (i = 0; i < ARRAY_SIZE(btrfs_extra_devs); i++) {
+		if (btrfs_extra_devs[i][0] == 0)
+			break;
+
+		fd = open(btrfs_extra_devs[i], O_RDONLY);
+		if (fd < 0) {
+			printf("ERROR: could not open %s\n",
+			       btrfs_extra_devs[i]);
+			continue;
+		}
+		ret = btrfs_scan_one_device(fd, btrfs_extra_devs[i],
+					    &tmp_devices, &num_devices,
+					    BTRFS_SUPER_INFO_OFFSET, 0);
+		if (ret) {
+			printf("ERROR: could not scan %s\n",
+			       btrfs_extra_devs[i]);
+			close(fd);
+			continue;
+		}
+
+		close(fd);
+	}
+
 	btrfs_scan_done = 1;
 
 	return 0;
@@ -3069,3 +3095,47 @@ unsigned int get_unit_mode_from_arg(int *argc, char *argv[], int df_mode)
 
 	return unit_mode;
 }
+
+int set_extra_dev_from_arg(int *argc, char *argv[])
+{
+	int arg_i;
+	int arg_end;
+	int extra_dev_cnt = 0;
+
+	static const char opt_head[] = "--extra-dev=";
+
+	for (arg_i = 1; arg_i < *argc; arg_i++) {
+		if (strncmp(argv[arg_i], opt_head, strlen(opt_head)))
+			continue;
+
+		char *val_begin = argv[arg_i] + strlen(opt_head);
+		char *val_end;
+
+		while (1) {
+			val_end = strchrnul(val_begin, ',');
+			if (val_end != val_begin) {
+				memcpy(btrfs_extra_devs[extra_dev_cnt],
+				       val_begin, val_end - val_begin);
+				btrfs_extra_devs[extra_dev_cnt]
+						[val_end - val_begin] = 0;
+				extra_dev_cnt++;
+			}
+			if (*val_end == 0)
+				break;
+			val_begin = val_end + 1;
+		}
+		argv[arg_i] = NULL;
+	}
+	btrfs_extra_devs[extra_dev_cnt][0] = 0;
+
+	for (arg_i = 0, arg_end = 0; arg_i < *argc; arg_i++) {
+		if (!argv[arg_i])
+			continue;
+		argv[arg_end] = argv[arg_i];
+		arg_end++;
+	}
+
+	*argc = arg_end;
+
+	return 0;
+}
diff --git a/utils.h b/utils.h
index dce0a47..4070a1f 100644
--- a/utils.h
+++ b/utils.h
@@ -269,4 +269,9 @@ const char *get_argv0_buf(void);
 
 unsigned int get_unit_mode_from_arg(int *argc, char *argv[], int df_mode);
 
+#define HELPINFO_EXTRA_DEV \
+	"--extra-dev        set extra device to search"
+
+int set_extra_dev_from_arg(int *argc, char *argv[]);
+
 #endif
-- 
1.8.5.1


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

* [PATCH 2/5] btrfs-progs: Support extra-dev for btrfs rescue
  2015-09-04 13:24 [PATCH 0/5] btrfs-progs: Introduce --extra-dev option Zhao Lei
  2015-09-04 13:24 ` [PATCH 1/5] " Zhao Lei
@ 2015-09-04 13:24 ` Zhao Lei
  2015-09-04 13:24 ` [PATCH 3/5] btrfs-progs: Avoid using uninitialized optind in cmd_rescue_zero_log Zhao Lei
                   ` (2 subsequent siblings)
  4 siblings, 0 replies; 6+ messages in thread
From: Zhao Lei @ 2015-09-04 13:24 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

Make following command support --extra-dev:
 btrfs rescue chunk-recover
 btrfs rescue super-recover
 btrfs rescue zero-log

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 cmds-rescue.c | 24 ++++++++++++++++++------
 1 file changed, 18 insertions(+), 6 deletions(-)

diff --git a/cmds-rescue.c b/cmds-rescue.c
index dd7c01e..163d0bb 100644
--- a/cmds-rescue.c
+++ b/cmds-rescue.c
@@ -37,9 +37,10 @@ const char * const cmd_rescue_chunk_recover_usage[] = {
 	"btrfs rescue chunk-recover [options] <device>",
 	"Recover the chunk tree by scanning the devices one by one.",
 	"",
-	"-y	Assume an answer of `yes' to all questions",
-	"-v	Verbose mode",
-	"-h	Help",
+	HELPINFO_EXTRA_DEV,
+	"-y                 Assume an answer of `yes' to all questions",
+	"-v                 Verbose mode",
+	"-h                 Help",
 	NULL
 };
 
@@ -47,8 +48,9 @@ const char * const cmd_rescue_super_recover_usage[] = {
 	"btrfs rescue super-recover [options] <device>",
 	"Recover bad superblocks from good copies",
 	"",
-	"-y	Assume an answer of `yes' to all questions",
-	"-v	Verbose mode",
+	HELPINFO_EXTRA_DEV,
+	"-y                 Assume an answer of `yes' to all questions",
+	"-v                 Verbose mode",
 	NULL
 };
 
@@ -59,6 +61,9 @@ int cmd_rescue_chunk_recover(int argc, char *argv[])
 	int yes = 0;
 	int verbose = 0;
 
+	if (set_extra_dev_from_arg(&argc, argv))
+		usage(cmd_rescue_chunk_recover_usage);
+
 	while (1) {
 		int c = getopt(argc, argv, "yvh");
 		if (c < 0)
@@ -119,6 +124,9 @@ int cmd_rescue_super_recover(int argc, char **argv)
 	int yes = 0;
 	char *dname;
 
+	if (set_extra_dev_from_arg(&argc, argv))
+		usage(cmd_rescue_super_recover_usage);
+
 	while (1) {
 		int c = getopt(argc, argv, "vy");
 		if (c < 0)
@@ -153,9 +161,10 @@ int cmd_rescue_super_recover(int argc, char **argv)
 }
 
 const char * const cmd_rescue_zero_log_usage[] = {
-	"btrfs rescue zero-log <device>",
+	"btrfs rescue zero-log [options] <device>",
 	"Clear the tree log. Usable if it's corrupted and prevents mount.",
 	"",
+	HELPINFO_EXTRA_DEV,
 	NULL
 };
 
@@ -167,6 +176,9 @@ int cmd_rescue_zero_log(int argc, char **argv)
 	char *devname;
 	int ret;
 
+	if (set_extra_dev_from_arg(&argc, argv))
+		usage(cmd_rescue_zero_log_usage);
+
 	if (check_argc_exact(argc, 2))
 		usage(cmd_rescue_zero_log_usage);
 
-- 
1.8.5.1


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

* [PATCH 3/5] btrfs-progs: Avoid using uninitialized optind in cmd_rescue_zero_log
  2015-09-04 13:24 [PATCH 0/5] btrfs-progs: Introduce --extra-dev option Zhao Lei
  2015-09-04 13:24 ` [PATCH 1/5] " Zhao Lei
  2015-09-04 13:24 ` [PATCH 2/5] btrfs-progs: Support extra-dev for btrfs rescue Zhao Lei
@ 2015-09-04 13:24 ` Zhao Lei
  2015-09-04 13:24 ` [PATCH 4/5] btrfs-progs: add help command to btrfs rescue zero-log Zhao Lei
  2015-09-04 13:24 ` [PATCH 5/5] btrfs-progs: update manual for --extra-dev and -h for btrfs rescue Zhao Lei
  4 siblings, 0 replies; 6+ messages in thread
From: Zhao Lei @ 2015-09-04 13:24 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

Better to avoid using optind in cmd_rescue_zero_log(), because no
getopt() before.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 cmds-rescue.c | 2 +-
 1 file changed, 1 insertion(+), 1 deletion(-)

diff --git a/cmds-rescue.c b/cmds-rescue.c
index 163d0bb..a709e1b 100644
--- a/cmds-rescue.c
+++ b/cmds-rescue.c
@@ -182,7 +182,7 @@ int cmd_rescue_zero_log(int argc, char **argv)
 	if (check_argc_exact(argc, 2))
 		usage(cmd_rescue_zero_log_usage);
 
-	devname = argv[optind];
+	devname = argv[1];
 	ret = check_mounted(devname);
 	if (ret < 0) {
 		fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
-- 
1.8.5.1


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

* [PATCH 4/5] btrfs-progs: add help command to btrfs rescue zero-log
  2015-09-04 13:24 [PATCH 0/5] btrfs-progs: Introduce --extra-dev option Zhao Lei
                   ` (2 preceding siblings ...)
  2015-09-04 13:24 ` [PATCH 3/5] btrfs-progs: Avoid using uninitialized optind in cmd_rescue_zero_log Zhao Lei
@ 2015-09-04 13:24 ` Zhao Lei
  2015-09-04 13:24 ` [PATCH 5/5] btrfs-progs: update manual for --extra-dev and -h for btrfs rescue Zhao Lei
  4 siblings, 0 replies; 6+ messages in thread
From: Zhao Lei @ 2015-09-04 13:24 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

To make it unified with other two rescue command, which support
--help option.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 cmds-rescue.c | 17 +++++++++++++++--
 1 file changed, 15 insertions(+), 2 deletions(-)

diff --git a/cmds-rescue.c b/cmds-rescue.c
index a709e1b..3ed63f6 100644
--- a/cmds-rescue.c
+++ b/cmds-rescue.c
@@ -165,6 +165,7 @@ const char * const cmd_rescue_zero_log_usage[] = {
 	"Clear the tree log. Usable if it's corrupted and prevents mount.",
 	"",
 	HELPINFO_EXTRA_DEV,
+	"-h                 Help",
 	NULL
 };
 
@@ -179,10 +180,22 @@ int cmd_rescue_zero_log(int argc, char **argv)
 	if (set_extra_dev_from_arg(&argc, argv))
 		usage(cmd_rescue_zero_log_usage);
 
-	if (check_argc_exact(argc, 2))
+	while (1) {
+		int c = getopt(argc, argv, "h");
+
+		if (c < 0)
+			break;
+		switch (c) {
+		case 'h':
+		default:
+			usage(cmd_rescue_zero_log_usage);
+		}
+	}
+
+	if (check_argc_exact(argc - optind, 1))
 		usage(cmd_rescue_zero_log_usage);
 
-	devname = argv[1];
+	devname = argv[optind];
 	ret = check_mounted(devname);
 	if (ret < 0) {
 		fprintf(stderr, "Could not check mount status: %s\n", strerror(-ret));
-- 
1.8.5.1


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

* [PATCH 5/5] btrfs-progs: update manual for --extra-dev and -h for btrfs rescue
  2015-09-04 13:24 [PATCH 0/5] btrfs-progs: Introduce --extra-dev option Zhao Lei
                   ` (3 preceding siblings ...)
  2015-09-04 13:24 ` [PATCH 4/5] btrfs-progs: add help command to btrfs rescue zero-log Zhao Lei
@ 2015-09-04 13:24 ` Zhao Lei
  4 siblings, 0 replies; 6+ messages in thread
From: Zhao Lei @ 2015-09-04 13:24 UTC (permalink / raw)
  To: linux-btrfs; +Cc: Zhao Lei

--extra-dev and -h option was added to btrfs rescue command,
this patch update relative manual.

Signed-off-by: Zhao Lei <zhaolei@cn.fujitsu.com>
---
 Documentation/btrfs-rescue.asciidoc | 13 ++++++++++++-
 1 file changed, 12 insertions(+), 1 deletion(-)

diff --git a/Documentation/btrfs-rescue.asciidoc b/Documentation/btrfs-rescue.asciidoc
index 42aca64..11f133c 100644
--- a/Documentation/btrfs-rescue.asciidoc
+++ b/Documentation/btrfs-rescue.asciidoc
@@ -20,6 +20,8 @@ Recover the chunk tree by scanning the devices
 +
 `Options`
 +
+--extra-dev::::
+set extra device to search.
 -y::::
 assume an answer of 'yes' to all questions.
 -v::::
@@ -35,13 +37,22 @@ Recover bad superblocks from good copies.
 +
 `Options`
 +
+--extra-dev::::
+set extra device to search.
 -y::::
 assume an answer of 'yes' to all questions.
 -v::::
 verbose mode.
 
-*zero-log* <device>::
+*zero-log* [options] <device>::
 clear the filesystem log tree
++
+`Options`
++
+--extra-dev::::
+set extra device to search.
+-h::::
+help.
 
 This command will clear the filesystem log tree. This may fix a specific
 set of problem when the filesystem mount fails due to the log replay. See below
-- 
1.8.5.1


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

end of thread, other threads:[~2015-09-04 13:26 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-09-04 13:24 [PATCH 0/5] btrfs-progs: Introduce --extra-dev option Zhao Lei
2015-09-04 13:24 ` [PATCH 1/5] " Zhao Lei
2015-09-04 13:24 ` [PATCH 2/5] btrfs-progs: Support extra-dev for btrfs rescue Zhao Lei
2015-09-04 13:24 ` [PATCH 3/5] btrfs-progs: Avoid using uninitialized optind in cmd_rescue_zero_log Zhao Lei
2015-09-04 13:24 ` [PATCH 4/5] btrfs-progs: add help command to btrfs rescue zero-log Zhao Lei
2015-09-04 13:24 ` [PATCH 5/5] btrfs-progs: update manual for --extra-dev and -h for btrfs rescue Zhao Lei

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).