diff mbox series

[v4,05/10] btrfs-progs: mkfs: align byte_count with sectorsize and zone size

Message ID 20240529071325.940910-6-naohiro.aota@wdc.com (mailing list archive)
State New, archived
Headers show
Series btrfs-progs: zoned: proper "mkfs.btrfs -b" support | expand

Commit Message

Naohiro Aota May 29, 2024, 7:13 a.m. UTC
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 btrfs 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>
---
 mkfs/main.c | 10 +++++++++-
 1 file changed, 9 insertions(+), 1 deletion(-)

Comments

Qu Wenruo May 29, 2024, 7:45 a.m. UTC | #1
在 2024/5/29 16:43, Naohiro Aota 写道:
> 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 btrfs 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>

To David, since I have the write permission to btrfs-progs and reviewed
the series, can I push it to devel branch now?

Thanks,
Qu
> ---
>   mkfs/main.c | 10 +++++++++-
>   1 file changed, 9 insertions(+), 1 deletion(-)
>
> diff --git a/mkfs/main.c b/mkfs/main.c
> index a437ecc40c7f..3446a5b1222f 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;
David Sterba May 30, 2024, 5:26 p.m. UTC | #2
On Wed, May 29, 2024 at 05:15:37PM +0930, Qu Wenruo wrote:
> 
> 
> 在 2024/5/29 16:43, Naohiro Aota 写道:
> > 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 btrfs 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>
> 
> To David, since I have the write permission to btrfs-progs and reviewed
> the series, can I push it to devel branch now?

OK, go on. I'm done for now with changes to fix the LE/BE and unaligned
access. For future it would be better if you create a pull request and
mark it that you want to merge it yourself, I could miss it in replies
to patches in the middle of a series.
diff mbox series

Patch

diff --git a/mkfs/main.c b/mkfs/main.c
index a437ecc40c7f..3446a5b1222f 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;