diff mbox series

[f2fs-dev,1/2] libf2fs: Accept Sparse files with non 4K Blocksize

Message ID 20240126221845.265859-1-drosen@google.com (mailing list archive)
State New
Headers show
Series [f2fs-dev,1/2] libf2fs: Accept Sparse files with non 4K Blocksize | expand

Commit Message

Daniel Rosenberg Jan. 26, 2024, 10:18 p.m. UTC
Since we may not know the block size when initializing sparse files, we
should assume that the sparse file's blocksize is correct.

Signed-off-by: Daniel Rosenberg <drosen@google.com>
---
 fsck/mount.c     | 20 +++++++++++++-------
 lib/libf2fs_io.c | 11 +++++++----
 2 files changed, 20 insertions(+), 11 deletions(-)


base-commit: bf5100606d63f6928799846b7322aa6f3f158bcf

Comments

Jaegeuk Kim Jan. 29, 2024, 8:14 p.m. UTC | #1
On 01/26, Daniel Rosenberg wrote:
> Since we may not know the block size when initializing sparse files, we
> should assume that the sparse file's blocksize is correct.
> 
> Signed-off-by: Daniel Rosenberg <drosen@google.com>
> ---
>  fsck/mount.c     | 20 +++++++++++++-------
>  lib/libf2fs_io.c | 11 +++++++----
>  2 files changed, 20 insertions(+), 11 deletions(-)
> 
> diff --git a/fsck/mount.c b/fsck/mount.c
> index 30c6228..7bbec3f 100644
> --- a/fsck/mount.c
> +++ b/fsck/mount.c
> @@ -995,6 +995,10 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
>  		return -1;
>  
>  	blocksize = 1 << get_sb(log_blocksize);
> +	if (c.sparse_mode && F2FS_BLKSIZE != blocksize) {
> +		MSG(0, "Invalid blocksize (%u), does not equal sparse file blocksize (%u)",
> +			F2FS_BLKSIZE);

Applied with the below fix.
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -997,7 +997,7 @@ int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
        blocksize = 1 << get_sb(log_blocksize);
        if (c.sparse_mode && F2FS_BLKSIZE != blocksize) {
                MSG(0, "Invalid blocksize (%u), does not equal sparse file blocksize (%u)",
-                       F2FS_BLKSIZE);
+                       F2FS_BLKSIZE, blocksize);
        }


> +	}
>  	if (blocksize < F2FS_MIN_BLKSIZE || blocksize > F2FS_MAX_BLKSIZE) {
>  		MSG(0, "Invalid blocksize (%u), must be between 4KB and 16KB\n",
>  			blocksize);
> @@ -3965,20 +3969,22 @@ int f2fs_do_mount(struct f2fs_sb_info *sbi)
>  	sbi->active_logs = NR_CURSEG_TYPE;
>  	ret = validate_super_block(sbi, SB0_ADDR);
>  	if (ret) {
> -		/* Assuming 4K Block Size */
> -		c.blksize_bits = 12;
> -		c.blksize = 1 << c.blksize_bits;
> -		MSG(0, "Looking for secondary superblock assuming 4K Block Size\n");
> +		if (!c.sparse_mode) {
> +			/* Assuming 4K Block Size */
> +			c.blksize_bits = 12;
> +			c.blksize = 1 << c.blksize_bits;
> +			MSG(0, "Looking for secondary superblock assuming 4K Block Size\n");
> +		}
>  		ret = validate_super_block(sbi, SB1_ADDR);
> -		if (ret) {
> +		if (ret && !c.sparse_mode) {
>  			/* Trying 16K Block Size */
>  			c.blksize_bits = 14;
>  			c.blksize = 1 << c.blksize_bits;
>  			MSG(0, "Looking for secondary superblock assuming 16K Block Size\n");
>  			ret = validate_super_block(sbi, SB1_ADDR);
> -			if (ret)
> -				return -1;
>  		}
> +		if (ret)
> +			return -1;
>  	}
>  	sb = F2FS_RAW_SUPER(sbi);
>  	c.cache_config.num_cache_entry = num_cache_entry;
> diff --git a/lib/libf2fs_io.c b/lib/libf2fs_io.c
> index d76da83..97c91ef 100644
> --- a/lib/libf2fs_io.c
> +++ b/lib/libf2fs_io.c
> @@ -662,14 +662,17 @@ int f2fs_init_sparse_file(void)
>  		if (!f2fs_sparse_file)
>  			return -1;
>  
> +		c.blksize = sparse_file_block_size(f2fs_sparse_file);
> +		c.blksize_bits = log_base_2(c.blksize);
> +		if (c.blksize_bits == -1) {
> +			MSG(0, "\tError: Sparse file blocksize not a power of 2.\n");
> +			return -1;
> +		}
> +
>  		c.device_size = sparse_file_len(f2fs_sparse_file, 0, 0);
>  		c.device_size &= (~((uint64_t)(F2FS_BLKSIZE - 1)));
>  	}
>  
> -	if (sparse_file_block_size(f2fs_sparse_file) != F2FS_BLKSIZE) {
> -		MSG(0, "\tError: Corrupted sparse file\n");
> -		return -1;
> -	}
>  	blocks_count = c.device_size / F2FS_BLKSIZE;
>  	blocks = calloc(blocks_count, sizeof(char *));
>  	if (!blocks) {
> 
> base-commit: bf5100606d63f6928799846b7322aa6f3f158bcf
> -- 
> 2.43.0.429.g432eaa2c6b-goog
diff mbox series

Patch

diff --git a/fsck/mount.c b/fsck/mount.c
index 30c6228..7bbec3f 100644
--- a/fsck/mount.c
+++ b/fsck/mount.c
@@ -995,6 +995,10 @@  int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr)
 		return -1;
 
 	blocksize = 1 << get_sb(log_blocksize);
+	if (c.sparse_mode && F2FS_BLKSIZE != blocksize) {
+		MSG(0, "Invalid blocksize (%u), does not equal sparse file blocksize (%u)",
+			F2FS_BLKSIZE);
+	}
 	if (blocksize < F2FS_MIN_BLKSIZE || blocksize > F2FS_MAX_BLKSIZE) {
 		MSG(0, "Invalid blocksize (%u), must be between 4KB and 16KB\n",
 			blocksize);
@@ -3965,20 +3969,22 @@  int f2fs_do_mount(struct f2fs_sb_info *sbi)
 	sbi->active_logs = NR_CURSEG_TYPE;
 	ret = validate_super_block(sbi, SB0_ADDR);
 	if (ret) {
-		/* Assuming 4K Block Size */
-		c.blksize_bits = 12;
-		c.blksize = 1 << c.blksize_bits;
-		MSG(0, "Looking for secondary superblock assuming 4K Block Size\n");
+		if (!c.sparse_mode) {
+			/* Assuming 4K Block Size */
+			c.blksize_bits = 12;
+			c.blksize = 1 << c.blksize_bits;
+			MSG(0, "Looking for secondary superblock assuming 4K Block Size\n");
+		}
 		ret = validate_super_block(sbi, SB1_ADDR);
-		if (ret) {
+		if (ret && !c.sparse_mode) {
 			/* Trying 16K Block Size */
 			c.blksize_bits = 14;
 			c.blksize = 1 << c.blksize_bits;
 			MSG(0, "Looking for secondary superblock assuming 16K Block Size\n");
 			ret = validate_super_block(sbi, SB1_ADDR);
-			if (ret)
-				return -1;
 		}
+		if (ret)
+			return -1;
 	}
 	sb = F2FS_RAW_SUPER(sbi);
 	c.cache_config.num_cache_entry = num_cache_entry;
diff --git a/lib/libf2fs_io.c b/lib/libf2fs_io.c
index d76da83..97c91ef 100644
--- a/lib/libf2fs_io.c
+++ b/lib/libf2fs_io.c
@@ -662,14 +662,17 @@  int f2fs_init_sparse_file(void)
 		if (!f2fs_sparse_file)
 			return -1;
 
+		c.blksize = sparse_file_block_size(f2fs_sparse_file);
+		c.blksize_bits = log_base_2(c.blksize);
+		if (c.blksize_bits == -1) {
+			MSG(0, "\tError: Sparse file blocksize not a power of 2.\n");
+			return -1;
+		}
+
 		c.device_size = sparse_file_len(f2fs_sparse_file, 0, 0);
 		c.device_size &= (~((uint64_t)(F2FS_BLKSIZE - 1)));
 	}
 
-	if (sparse_file_block_size(f2fs_sparse_file) != F2FS_BLKSIZE) {
-		MSG(0, "\tError: Corrupted sparse file\n");
-		return -1;
-	}
 	blocks_count = c.device_size / F2FS_BLKSIZE;
 	blocks = calloc(blocks_count, sizeof(char *));
 	if (!blocks) {