linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH] btrfs-progs: device add should check existing FS before adding
@ 2013-08-30 11:09 Anand Jain
  2013-08-30 23:33 ` David Sterba
  0 siblings, 1 reply; 3+ messages in thread
From: Anand Jain @ 2013-08-30 11:09 UTC (permalink / raw)
  To: linux-btrfs

as of now, when 'btrfs device add' adds a device it doesn't
check if the given device contains an existing FS. This
patch will change that to check the same. which when true
add will fail, and ask user to use -f option to overwrite.

further, since now we have test_dev_for_mkfs() function
to check if a disk can be used, so this patch will also
use this function to test the given device before adding.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 cmds-device.c |   44 ++++++++++++++++++--------------------------
 1 files changed, 18 insertions(+), 26 deletions(-)

diff --git a/cmds-device.c b/cmds-device.c
index 282590c..8446502 100644
--- a/cmds-device.c
+++ b/cmds-device.c
@@ -53,12 +53,25 @@ static const char * const cmd_add_dev_usage[] = {
 static int cmd_add_dev(int argc, char **argv)
 {
 	char	*mntpnt;
-	int	i, fdmnt, ret=0, e;
+	int	i = 1, fdmnt, ret = 0, e;
 	DIR	*dirstream = NULL;
+	int	c, force = 0;
+	char	estr[100];
 
 	if (check_argc_min(argc, 3))
 		usage(cmd_add_dev_usage);
 
+	while ((c = getopt(argc, argv, "f")) != -1) {
+		switch (c) {
+		case 'f':
+			force = 1;
+			i++;
+			break;
+		default:
+			usage(cmd_add_dev_usage);
+		}
+	}
+
 	mntpnt = argv[argc - 1];
 
 	fdmnt = open_file_or_dir(mntpnt, &dirstream);
@@ -67,23 +80,15 @@ static int cmd_add_dev(int argc, char **argv)
 		return 12;
 	}
 
-	for (i = 1; i < argc - 1; i++ ){
+	for (; i < argc - 1; i++) {
 		struct btrfs_ioctl_vol_args ioctl_args;
 		int	devfd, res;
 		u64 dev_block_count = 0;
-		struct stat st;
 		int mixed = 0;
 
-		res = check_mounted(argv[i]);
-		if (res < 0) {
-			fprintf(stderr, "error checking %s mount status\n",
-				argv[i]);
-			ret++;
-			continue;
-		}
-		if (res == 1) {
-			fprintf(stderr, "%s is mounted\n", argv[i]);
-			ret++;
+		res = test_dev_for_mkfs(argv[i], force, estr);
+		if (res) {
+			fprintf(stderr, "%s", estr);
 			continue;
 		}
 
@@ -93,19 +98,6 @@ static int cmd_add_dev(int argc, char **argv)
 			ret++;
 			continue;
 		}
-		res = fstat(devfd, &st);
-		if (res) {
-			fprintf(stderr, "ERROR: Unable to stat '%s'\n", argv[i]);
-			close(devfd);
-			ret++;
-			continue;
-		}
-		if (!S_ISBLK(st.st_mode)) {
-			fprintf(stderr, "ERROR: '%s' is not a block device\n", argv[i]);
-			close(devfd);
-			ret++;
-			continue;
-		}
 
 		res = btrfs_prepare_device(devfd, argv[i], 1, &dev_block_count,
 					   0, &mixed, 0);
-- 
1.7.1


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

end of thread, other threads:[~2013-08-31  6:55 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-08-30 11:09 [PATCH] btrfs-progs: device add should check existing FS before adding Anand Jain
2013-08-30 23:33 ` David Sterba
2013-08-31  7:01   ` Anand Jain

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