linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Anand Jain <anand.jain@oracle.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 2/3] btrfs-progs: cmd_start_replace() to use test_dev_for_mkfs()
Date: Wed,  7 Aug 2013 20:11:24 +0800	[thread overview]
Message-ID: <1375877485-12105-3-git-send-email-anand.jain@oracle.com> (raw)
In-Reply-To: <1375877485-12105-1-git-send-email-anand.jain@oracle.com>

test_dev_for_mkfs() is a common place where
we check if a device is fit for the btrfs use.
cmd_start_replace() should make use of test_dev_for_mkfs(),
and here the test_dev_for_mkfs() is further enhanced
to fit the cmd_start_replace() needs.

Thanks

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
 cmds-replace.c |   32 ++++----------------------------
 utils.c        |   12 +++++++++++-
 utils.h        |    2 ++
 3 files changed, 17 insertions(+), 29 deletions(-)

diff --git a/cmds-replace.c b/cmds-replace.c
index 25e8c7f..eecea4b 100644
--- a/cmds-replace.c
+++ b/cmds-replace.c
@@ -137,13 +137,12 @@ static int cmd_start_replace(int argc, char **argv)
 	char *dstdev;
 	int avoid_reading_from_srcdev = 0;
 	int force_using_targetdev = 0;
-	u64 total_devs = 1;
-	struct btrfs_fs_devices *fs_devices_mnt = NULL;
 	struct stat st;
 	u64 dstdev_block_count;
 	int do_not_background = 0;
 	int mixed = 0;
 	DIR *dirstream = NULL;
+	char estr[BTRFS_ERR_STR_LEN];
 
 	while ((c = getopt(argc, argv, "Brf")) != -1) {
 		switch (c) {
@@ -265,15 +264,9 @@ static int cmd_start_replace(int argc, char **argv)
 		start_args.start.srcdevid = 0;
 	}
 
-	ret = check_mounted(dstdev);
-	if (ret < 0) {
-		fprintf(stderr, "Error checking %s mount status\n", dstdev);
-		goto leave_with_error;
-	}
-	if (ret == 1) {
-		fprintf(stderr,
-			"Error, target device %s is in use and currently mounted!\n",
-			dstdev);
+	ret = test_dev_for_mkfs(dstdev, force_using_targetdev, estr);
+	if (ret) {
+		fprintf(stderr, "%s", estr);
 		goto leave_with_error;
 	}
 	fddstdev = open(dstdev, O_RDWR);
@@ -281,23 +274,6 @@ static int cmd_start_replace(int argc, char **argv)
 		fprintf(stderr, "Unable to open %s\n", dstdev);
 		goto leave_with_error;
 	}
-	ret = btrfs_scan_one_device(fddstdev, dstdev, &fs_devices_mnt,
-				    &total_devs, BTRFS_SUPER_INFO_OFFSET);
-	if (ret >= 0 && !force_using_targetdev) {
-		fprintf(stderr,
-			"Error, target device %s contains filesystem, use '-f' to force overwriting.\n",
-			dstdev);
-		goto leave_with_error;
-	}
-	ret = fstat(fddstdev, &st);
-	if (ret) {
-		fprintf(stderr, "Error: Unable to stat '%s'\n", dstdev);
-		goto leave_with_error;
-	}
-	if (!S_ISBLK(st.st_mode)) {
-		fprintf(stderr, "Error: '%s' is not a block device\n", dstdev);
-		goto leave_with_error;
-	}
 	strncpy((char *)start_args.start.tgtdev_name, dstdev,
 		BTRFS_DEVICE_PATH_NAME_MAX);
 	if (btrfs_prepare_device(fddstdev, dstdev, 1, &dstdev_block_count, 0,
diff --git a/utils.c b/utils.c
index 15b991f..e35529e 100644
--- a/utils.c
+++ b/utils.c
@@ -1806,7 +1806,8 @@ out:
 int test_dev_for_mkfs(char *file, int force_overwrite, char *estr)
 {
 	int ret, fd;
-	size_t sz = 100;
+	size_t sz = BTRFS_ERR_STR_LEN;
+	struct stat st;
 
 	ret = is_swap_device(file);
 	if (ret < 0) {
@@ -1841,6 +1842,15 @@ int test_dev_for_mkfs(char *file, int force_overwrite, char *estr)
 			strerror(errno));
 		return 1;
 	}
+	if (fstat(fd, &st)) {
+		snprintf(estr, sz, "unable to stat %s: %s\n", file,
+			strerror(errno));
+		return 1;
+	}
+	if (!S_ISBLK(st.st_mode)) {
+		fprintf(stderr, "'%s' is not a block device\n", file);
+		return 1;
+	}
 	close(fd);
 	return 0;
 }
diff --git a/utils.h b/utils.h
index 5b95f3b..1e5353b 100644
--- a/utils.h
+++ b/utils.h
@@ -28,6 +28,8 @@
 #define BTRFS_SCAN_PROC	1
 #define BTRFS_SCAN_DEV		2
 
+#define BTRFS_ERR_STR_LEN	100
+
 int make_btrfs(int fd, const char *device, const char *label,
 	       u64 blocks[6], u64 num_bytes, u32 nodesize,
 	       u32 leafsize, u32 sectorsize, u32 stripesize);
-- 
1.7.1


  parent reply	other threads:[~2013-08-07 12:05 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2013-07-25 17:35 [PATCH 0/6] Anand Jain
2013-07-25 17:35 ` [PATCH 1/6] btrfs-progs: close_all_devices() in btrfs-find-root.c does nothing Anand Jain
2013-07-25 17:35 ` [PATCH 2/6] btrfs-progs: let user know that devid can be used if path is missing Anand Jain
2013-07-25 17:35 ` [PATCH 3/6] btrfs-progs: cmd_start_replace() to use test_dev_for_mkfs() Anand Jain
2013-07-25 17:35 ` [PATCH 4/6] btrfs-progs: mkfs.c overwrites fd without appropriate close Anand Jain
2013-08-13 19:14   ` Josef Bacik
2013-08-13 19:19     ` Josef Bacik
2013-08-14  2:04     ` Anand Jain
2013-08-14  3:17       ` Anand Jain
2013-08-14 13:32         ` Josef Bacik
2013-08-14  4:37   ` [PATCH] btrfs-progs: Fix: mkfs.c overwrites fd without appropriate close patch Anand Jain
2013-07-25 17:35 ` [PATCH 5/6] btrfs-progs: avoid write to the disk before sure to create fs Anand Jain
2013-07-25 17:35 ` [PATCH 6/6] btrfs-progs: don't have to report ENOMEDIUM error during open Anand Jain
2013-08-07 12:11 ` [PATCH 0/3 resend] Anand Jain
2013-08-07 12:11   ` [PATCH 1/3] btrfs-progs: let user know that devid can be used if path is missing Anand Jain
2013-08-07 12:11   ` Anand Jain [this message]
2013-08-07 12:11   ` [PATCH 3/3] btrfs-progs: avoid write to the disk before sure to create fs Anand Jain
2013-08-20 19:19     ` Josef Bacik
2013-08-21  3:15       ` Anand Jain

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=1375877485-12105-3-git-send-email-anand.jain@oracle.com \
    --to=anand.jain@oracle.com \
    --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).