From 18e2663db3e18d4506b6fe583ad93fc83235fca9 Mon Sep 17 00:00:00 2001 From: Qu Wenruo Date: Fri, 4 Jul 2014 15:29:17 +0800 Subject: btrfs-progs: Add minimum device size check Btrfs has global block reservation, so even mkfs.btrfs can execute without problem, there is still a possibility that the filesystem can't be mounted. For example when mkfs.btrfs on a 8M file on x86_64 platform, kernel will refuse to mount due to ENOSPC, since system block group takes 4M and mixed block group takes 4M, and global block reservation will takes all the 4M from mixed block group, which makes btrfs unable to create uuid tree. This patch will add minimum device size check before actually mkfs. The minimum size calculation uses a simplified one: minimum_size_for_each_dev = 2 * (system block group + global block rsv) and global block rsv = leafsize << 10 Signed-off-by: Qu Wenruo Signed-off-by: David Sterba --- utils.c | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) (limited to 'utils.c') diff --git a/utils.c b/utils.c index 46c3a431..c139eb2e 100644 --- a/utils.c +++ b/utils.c @@ -2344,3 +2344,23 @@ int find_mount_root(const char *path, char **mount_root) free(longest_match); return ret; } + +int test_minimum_size(const char *file, u32 leafsize) +{ + int fd; + struct stat statbuf; + + fd = open(file, O_RDONLY); + if (fd < 0) + return -errno; + if (stat(file, &statbuf) < 0) { + close(fd); + return -errno; + } + if (btrfs_device_size(fd, &statbuf) < btrfs_min_dev_size(leafsize)) { + close(fd); + return 1; + } + close(fd); + return 0; +} -- cgit v1.2.3