@@ -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) {
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(+)