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 <naohiro.aota@wdc.com>
Reviewed-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: Qu Wenruo <wqu@suse.com>
Signed-off-by: David Sterba <dsterba@suse.com>
This commit is contained in:
Naohiro Aota 2024-05-29 16:13:20 +09:00 committed by David Sterba
parent 48c5ef6582
commit 970c1a543c

View File

@ -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;