From 970c1a543c229f9e2c41693143d15457de0c23aa Mon Sep 17 00:00:00 2001 From: Naohiro Aota Date: Wed, 29 May 2024 16:13:20 +0900 Subject: [PATCH] btrfs-progs: mkfs: align byte_count with sectorsize and zone size While "byte_count" is eventually rounded down to sectorsize at make_btrfs() or btrfs_add_to_fs_id(), it would be better round it down first and do the size checks not to confuse the things. Also, on a zoned device, creating a filesystem whose size is not aligned to the zone boundary can be confusing. Round it down further to the zone boundary. The size calculation with a source directory is also tweaked to be aligned. device_get_partition_size_fd_stat() must be aligned down not to exceed the device size. And, btrfs_mkfs_size_dir() should have return sectorsize aligned size. So, add an UASSERT for it. Signed-off-by: Naohiro Aota Reviewed-by: Qu Wenruo Signed-off-by: Qu Wenruo Signed-off-by: David Sterba --- mkfs/main.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/mkfs/main.c b/mkfs/main.c index c7fbe5af..0b9d1d78 100644 --- a/mkfs/main.c +++ b/mkfs/main.c @@ -1591,6 +1591,12 @@ int BOX_MAIN(mkfs)(int argc, char **argv) min_dev_size = btrfs_min_dev_size(nodesize, mixed, opt_zoned ? zone_size(file) : 0, metadata_profile, data_profile); + if (byte_count) { + byte_count = round_down(byte_count, sectorsize); + if (opt_zoned) + byte_count = round_down(byte_count, zone_size(file)); + } + /* * Enlarge the destination file or create a new one, using the size * calculated from source dir. @@ -1624,9 +1630,11 @@ int BOX_MAIN(mkfs)(int argc, char **argv) * Or we will always use source_dir_size calculated for mkfs. */ if (!byte_count) - byte_count = device_get_partition_size_fd_stat(fd, &statbuf); + byte_count = round_down(device_get_partition_size_fd_stat(fd, &statbuf), + sectorsize); source_dir_size = btrfs_mkfs_size_dir(source_dir, sectorsize, min_dev_size, metadata_profile, data_profile); + UASSERT(IS_ALIGNED(source_dir_size, sectorsize)); if (byte_count < source_dir_size) { if (S_ISREG(statbuf.st_mode)) { byte_count = source_dir_size;