All of lore.kernel.org
 help / color / mirror / Atom feed
From: Guangyu Sun <guangyu.sun@oracle.com>
To: linux-btrfs@vger.kernel.org
Subject: [PATCH 2/2] btrfs-progs: don't zero dev if dev size is smaller than -r rootdir size
Date: Tue, 12 Aug 2014 17:13:55 -0700	[thread overview]
Message-ID: <1407888835-15739-2-git-send-email-guangyu.sun@oracle.com> (raw)
In-Reply-To: <1407888835-15739-1-git-send-email-guangyu.sun@oracle.com>

Even if the size of rootdir given by -r option is larger than the
specified partition, zero_output_file() is still called. It will take
time to fill up the partition with zero if the partition is big, and
end up with an EIO.

The size should be checked before zeroing the partition.

Signed-off-by: Guangyu Sun <guangyu.sun@oracle.com>
---
 mkfs.c |   19 +++++++++++++++++++
 1 file changed, 19 insertions(+)

diff --git a/mkfs.c b/mkfs.c
index 71aea40..ea61180 100644
--- a/mkfs.c
+++ b/mkfs.c
@@ -1475,12 +1475,31 @@ int main(int ac, char **av)
 			exit(1);
 		}
 	} else {
+		struct stat st;
+		u64 size;
+
 		fd = open_target(file);
 		if (fd < 0) {
 			fprintf(stderr, "unable to open the %s\n", file);
 			exit(1);
 		}
 
+		if (fstat(fd, &st) < 0) {
+			fprintf(stderr, "unable to stat %s\n", file);
+			exit(1);
+		}
+
+		size = btrfs_device_size(fd, &st);
+		if (size == 0) {
+			fprintf(stderr, "unable to find %s size\n", file);
+			exit(1);
+		}
+
+		if (size < block_count) {
+			fprintf(stderr, "%s is smaller than requested size\n", file);
+			exit(1);
+		}
+
 		first_file = file;
 		ret = zero_output_file(fd, block_count, sectorsize);
 		if (ret) {
-- 
1.7.9.5


      reply	other threads:[~2014-08-13  0:14 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-08-13  0:13 [PATCH 1/2] btrfs-progs: force mixed groups when -r rootdir is small Guangyu Sun
2014-08-13  0:13 ` Guangyu Sun [this message]

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=1407888835-15739-2-git-send-email-guangyu.sun@oracle.com \
    --to=guangyu.sun@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 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.