Skip to contentSkip to conversation

libf2fs: Accept Sparse files with non 4K Blocksize

PATCH In review Daniel Rosenberg via Linux-f2fs-devel 2 participants 3 messages Jan 26, 2024 – Jan 29, 2024 Library code

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 libf2fs: Accept Sparse files with non 4K Blocksize

Since we may not know the block size when initializing sparse files, we should assume that the sparse file's blocksize is correct.

---
 fsck/mount.c     | 20 +++++++++++++-------
 lib/libf2fs_io.c | 11 +++++++----
 2 files changed, 20 insertions(+), 11 deletions(-)
fsck/mount.c +13 -7
index 30c6228..7bbec3f 100644
@@ -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;
lib/libf2fs_io.c +7 -4
index d76da83..97c91ef 100644
@@ -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

Signature · 8 lines
2.43.0.429.g432eaa2c6b-goog



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#1 lore raw

PATCH 2/2 libf2fs: Fix possible memleak with Sparse Files

If sparse files is set along with multiple devices, we initialize sparse file data multiple times without freeing the previously allocated data. This skips the initialization for subsequent devices, as sparse file mode currently only deals with device 0 anyways.

---
 lib/libf2fs.c | 4 ++--
 1 file changed, 2 insertions(+), 2 deletions(-)
lib/libf2fs.c +2 -2
index 2451201..13f2b07 100644
@@ -974,7 +974,7 @@ int get_device_info(int i)
 
 	dev->fd = fd;
 
-	if (c.sparse_mode) {
+	if (c.sparse_mode && i == 0) {
 		if (f2fs_init_sparse_file()) {
 			free(stat_buf);
 			return -1;
@@ -1221,7 +1221,7 @@ int get_device_info(int i)
 	c.sectors_per_blk = F2FS_BLKSIZE / c.sector_size;
 	c.total_sectors += dev->total_sectors;
 
-	if (c.sparse_mode && f2fs_init_sparse_file())
+	if (c.sparse_mode && i==0 && f2fs_init_sparse_file())
 		return -1;
 	return 0;
 }
Signature · 8 lines
2.43.0.429.g432eaa2c6b-goog



_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel
#2 lore raw
In reply to Daniel Rosenberg via Linux-f2fs-devel
Quoted 14 lines · Daniel Rosenberg

Since we may not know the block size when initializing sparse files, we should assume that the sparse file's blocksize is correct.

---
 fsck/mount.c     | 20 +++++++++++++-------
 lib/libf2fs_io.c | 11 +++++++----
 2 files changed, 20 insertions(+), 11 deletions(-)
fsck/mount.c +0 -0
index 30c6228..7bbec3f 100644

fsck/mount.c int sanity_check_raw_super(struct f2fs_super_block *sb, enum SB_ADDR sb_addr) · line 1000

Quoted 6 lines
fsck/mount.c +3 -0
@@ -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.

fsck/mount.c +1 -1
@@ -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);
        }
 
 
Quoted 61 lines
fsck/mount.c +10 -7
+	}
 	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;
lib/libf2fs_io.c +7 -4
index d76da83..97c91ef 100644
@@ -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) {
Quoted 3 lines

base-commit: bf5100606d63f6928799846b7322aa6f3f158bcf

Signature · 1 line
2.43.0.429.g432eaa2c6b-goog

_______________________________________________
Linux-f2fs-devel mailing list
[email protected]
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel