diff mbox

[2/2] btrfs-progs: don't zero dev if dev size is smaller than -r rootdir size

Message ID 1407888835-15739-2-git-send-email-guangyu.sun@oracle.com (mailing list archive)
State Not Applicable
Headers show

Commit Message

Guangyu Sun Aug. 13, 2014, 12:13 a.m. UTC
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 mbox

Patch

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