btrfs-progs: mkfs: Use the whole file or block device to mkfs for rootdir
authorQu Wenruo <wqu@suse.com>
Wed, 29 Nov 2017 08:42:05 +0000 (16:42 +0800)
committerDavid Sterba <dsterba@suse.com>
Mon, 8 Jan 2018 17:15:20 +0000 (18:15 +0100)
For --rootdir, even for large existing file or block device, it will
always shrink the resulting filesystem.

The problem is, mkfs.btrfs will try to calculate the dir size, and use
it as @block_count to mkfs, which makes the filesystem shrunk.

Fix it by trying to get the original block device or file size as
@block_count, so mkfs.btrfs can use the full file/block device for
--rootdir option.

Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
mkfs/main.c
utils.c
utils.h

index 94d9ef0..d1b687d 100644 (file)
@@ -970,13 +970,31 @@ int main(int argc, char **argv)
         * This must be done before minimal device size checks.
         */
        if (source_dir_set) {
-               fd = open(file, O_CREAT | O_RDWR, S_IRUSR | S_IWUSR | S_IRGRP |
-                         S_IWGRP | S_IROTH);
+               int oflags = O_RDWR;
+               struct stat statbuf;
+
+               if (is_path_exist(file) == 0)
+                       oflags |= O_CREAT;
+
+               fd = open(file, oflags, S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP |
+                                        S_IROTH);
                if (fd < 0) {
                        error("unable to open %s: %s", file, strerror(errno));
                        goto error;
                }
+               ret = fstat(fd, &statbuf);
+               if (ret < 0) {
+                       error("unable to stat %s: %s", file, strerror(errno));
+                       ret = -errno;
+                       goto error;
+               }
 
+               /*
+                * Block_count not specified, use file/device size first.
+                * Or we will always use source_dir_size calculated for mkfs.
+                */
+               if (!block_count)
+                       block_count = btrfs_device_size(fd, &statbuf);
                source_dir_size = btrfs_mkfs_size_dir(source_dir, sectorsize,
                                min_dev_size, metadata_profile, data_profile);
                if (block_count < source_dir_size)
diff --git a/utils.c b/utils.c
index 22c1375..80071e2 100644 (file)
--- a/utils.c
+++ b/utils.c
@@ -447,7 +447,7 @@ int is_mount_point(const char *path)
        return ret;
 }
 
-static int is_reg_file(const char *path)
+int is_reg_file(const char *path)
 {
        struct stat statbuf;
 
diff --git a/utils.h b/utils.h
index 860c25d..bd7484f 100644 (file)
--- a/utils.h
+++ b/utils.h
@@ -123,6 +123,7 @@ char *__strncpy_null(char *dest, const char *src, size_t n);
 int is_block_device(const char *file);
 int is_mount_point(const char *file);
 int is_path_exist(const char *file);
+int is_reg_file(const char *path);
 int check_arg_type(const char *input);
 int open_path_or_dev_mnt(const char *path, DIR **dirstream, int verbose);
 int btrfs_open(const char *path, DIR **dirstream, int verbose, int dir_only);