linux-btrfs.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Wang Shaoyan <stufever@gmail.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH] btrfs: when mkfs.btrfs, add control to the multiple device which have mounted
Date: Thu, 5 Aug 2010 18:04:51 +0800	[thread overview]
Message-ID: <AANLkTi=uYk5A+5QTwLA8bS2KDpe12yAyxTNORfqzCV=X@mail.gmail.com> (raw)

If some devices were formatted as a fs, and mounted to some directory,
mkfs.btrfs do no check to devices except the first one. Then even if
the device is mounted, we can also mkfs. I think when a device is in
use, we can not mkfs.

But there are some other problems, for examle when we do offline fsck
to an device, then we mkfs it, we also can, it should be prevent.

diff --git a/mkfs.c b/mkfs.c
index 2e99b95..ceb65cb 100644
--- a/mkfs.c
+++ b/mkfs.c
@@ -431,6 +431,10 @@ int main(int ac, char **av)
 		fprintf(stderr, "%s is mounted\n", file);
 		exit(1);
 	}
+	if (ret == 2) {
+		fprintf(stderr, "%s is apparently in use by the system\n", file);
+		exit(1);
+	}
 	ac--;
 	fd = open(file, O_RDWR);
 	if (fd < 0) {
@@ -489,6 +493,10 @@ int main(int ac, char **av)
 			fprintf(stderr, "%s is mounted\n", file);
 			exit(1);
 		}
+		if (ret == 2) {
+			fprintf(stderr, "%s is apparently in use by the system\n", file);
+			exit(1);
+		}
 		fd = open(file, O_RDWR);
 		if (fd < 0) {
 			fprintf(stderr, "unable to open %s\n", file);
diff --git a/utils.c b/utils.c
index 2f4c6e1..e400745 100644
--- a/utils.c
+++ b/utils.c
@@ -587,7 +587,7 @@ error:
 }

 /*
- * returns 1 if the device was mounted, < 0 on error or 0 if everything
+ * returns 2 if the device is busy, returns 1 if the device was
mounted, < 0 on error or 0 if everything
  * is safe to continue.  TODO, this should also scan multi-device filesystems
  */
 int check_mounted(char *file)
@@ -599,6 +599,7 @@ int check_mounted(char *file)
 	ino_t file_ino = 0;
 	FILE *f;
 	int ret = 0;
+	int fd;

 	if ((f = setmntent ("/proc/mounts", "r")) == NULL)
 		return -errno;
@@ -629,12 +630,22 @@ int check_mounted(char *file)
 		}
 	}

+	endmntent (f);
 	if (mnt) {
 		/* found an entry in mnt table */
 		ret = 1;
+		goto out;
 	}

-	endmntent (f);
+	fd = open(file, O_RDONLY | O_EXCL);
+	if (fd < 0) {
+		if (errno == EBUSY) {
+			ret = 2;
+		}
+	} else
+		close(fd);
+
+out:
 	return ret;
 }


-- 
Wang Shaoyan

                 reply	other threads:[~2010-08-05 10:04 UTC|newest]

Thread overview: [no followups] expand[flat|nested]  mbox.gz  Atom feed

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='AANLkTi=uYk5A+5QTwLA8bS2KDpe12yAyxTNORfqzCV=X@mail.gmail.com' \
    --to=stufever@gmail.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).