Skip to contentSkip to conversation

f2fs: fix valid block count leak on data block allocation failure

PATCH Superseded Chen Changcheng 3 participants 8 messages Jul 22 – Aug 5

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 1/1 f2fs: fix valid block count leak on data block allocation failure

In __allocate_data_block(), when allocating a new data block (dn->data_blkaddr == NULL_ADDR), inc_valid_block_count() is called first to increment total_valid_block_count and i_blocks. If the subsequent f2fs_allocate_data_block() fails, the function returns the error directly without rolling back the already-incremented block counts, causing a permanent leak.

Fix this by calling dec_valid_block_count() to undo the increment before returning the error. The condition old_blkaddr == NULL_ADDR precisely identifies the case where inc_valid_block_count() was called.

---
 fs/f2fs/data.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/f2fs/data.c +4 -1
index a765fda71536..819631bb61ae 100644
@@ -1532,8 +1532,11 @@ static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
 	old_blkaddr = dn->data_blkaddr;
 	err = f2fs_allocate_data_block(sbi, NULL, old_blkaddr,
 				&dn->data_blkaddr, &sum, seg_type, NULL);
-	if (err)
+	if (err) {
+		if (old_blkaddr == NULL_ADDR)
+			dec_valid_block_count(sbi, dn->inode, count);
 		return err;
+	}
 
 	if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
 		f2fs_invalidate_internal_cache(sbi, old_blkaddr, 1);
Signature · 1 line
2.25.1
maintainer Reviewed-by on v1 1/1
#1 lore raw
Quoted 7 lines · Chen Changcheng

In __allocate_data_block(), when allocating a new data block (dn->data_blkaddr == NULL_ADDR), inc_valid_block_count() is called first to increment total_valid_block_count and i_blocks. If the subsequent f2fs_allocate_data_block() fails, the function returns the error directly without rolling back the already-incremented block counts, causing a permanent leak.

Quoted 5 lines

Fix this by calling dec_valid_block_count() to undo the increment before returning the error. The condition old_blkaddr == NULL_ADDR precisely identifies the case where inc_valid_block_count() was called.

Needs fixes and Cc stable line?

Quoted 1 line

Thanks,

Quoted 21 lines
---
  fs/f2fs/data.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
fs/f2fs/data.c +4 -1
index a765fda71536..819631bb61ae 100644
@@ -1532,8 +1532,11 @@ static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
  	old_blkaddr = dn->data_blkaddr;
  	err = f2fs_allocate_data_block(sbi, NULL, old_blkaddr,
  				&dn->data_blkaddr, &sum, seg_type, NULL);
-	if (err)
+	if (err) {
+		if (old_blkaddr == NULL_ADDR)
+			dec_valid_block_count(sbi, dn->inode, count);
  		return err;
+	}
 
  	if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
  		f2fs_invalidate_internal_cache(sbi, old_blkaddr, 1);
#3 lore raw

PATCH v2 1/1 f2fs: fix valid block count leak on data block allocation failure

In __allocate_data_block(), when allocating a new data block (dn->data_blkaddr == NULL_ADDR), inc_valid_block_count() is called first to increment total_valid_block_count and i_blocks. If the subsequent f2fs_allocate_data_block() fails, the function returns the error directly without rolling back the already-incremented block counts, causing a permanent leak.

Fix this by calling dec_valid_block_count() to undo the increment before returning the error. The condition old_blkaddr == NULL_ADDR precisely identifies the case where inc_valid_block_count() was called.

Fixes 54607494875e ("f2fs: compress: fix to avoid inconsistence bewteen i_blocks and dnode")
---
 fs/f2fs/data.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/f2fs/data.c +4 -1
index a765fda71536..819631bb61ae 100644
@@ -1532,8 +1532,11 @@ static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
 	old_blkaddr = dn->data_blkaddr;
 	err = f2fs_allocate_data_block(sbi, NULL, old_blkaddr,
 				&dn->data_blkaddr, &sum, seg_type, NULL);
-	if (err)
+	if (err) {
+		if (old_blkaddr == NULL_ADDR)
+			dec_valid_block_count(sbi, dn->inode, count);
 		return err;
+	}
 
 	if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
 		f2fs_invalidate_internal_cache(sbi, old_blkaddr, 1);
Signature · 1 line
2.25.1
maintainer
#4 lore raw
Quoted 7 lines · Chen Changcheng

In __allocate_data_block(), when allocating a new data block (dn->data_blkaddr == NULL_ADDR), inc_valid_block_count() is called first to increment total_valid_block_count and i_blocks. If the subsequent f2fs_allocate_data_block() fails, the function returns the error directly without rolling back the already-incremented block counts, causing a permanent leak.

Quoted 6 lines

Fix this by calling dec_valid_block_count() to undo the increment before returning the error. The condition old_blkaddr == NULL_ADDR precisely identifies the case where inc_valid_block_count() was called.

Fixes 54607494875e ("f2fs: compress: fix to avoid inconsistence bewteen i_blocks and dnode")

Should be?

Fixes 7d009e048d7c ("f2fs: fix to handle segment allocation failure correctly")

Thanks,

Quoted 24 lines
---
  fs/f2fs/data.c | 5 ++++-
  1 file changed, 4 insertions(+), 1 deletion(-)
fs/f2fs/data.c +4 -1
index a765fda71536..819631bb61ae 100644
@@ -1532,8 +1532,11 @@ static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
  	old_blkaddr = dn->data_blkaddr;
  	err = f2fs_allocate_data_block(sbi, NULL, old_blkaddr,
  				&dn->data_blkaddr, &sum, seg_type, NULL);
-	if (err)
+	if (err) {
+		if (old_blkaddr == NULL_ADDR)
+			dec_valid_block_count(sbi, dn->inode, count);
  		return err;
+	}
 
  	if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
  		f2fs_invalidate_internal_cache(sbi, old_blkaddr, 1);
#5 lore raw

PATCH v3 1/1 f2fs: fix valid block count leak on data block allocation failure

In __allocate_data_block(), when allocating a new data block (dn->data_blkaddr == NULL_ADDR), inc_valid_block_count() is called first to increment total_valid_block_count and i_blocks. If the subsequent f2fs_allocate_data_block() fails, the function returns the error directly without rolling back the already-incremented block counts, causing a permanent leak.

Fix this by calling dec_valid_block_count() to undo the increment before returning the error. The condition old_blkaddr == NULL_ADDR precisely identifies the case where inc_valid_block_count() was called.

Fixes 7d009e048d7c ("f2fs: fix to handle segment allocation failure correctly")
---
 fs/f2fs/data.c | 5 ++++-
 1 file changed, 4 insertions(+), 1 deletion(-)
fs/f2fs/data.c +4 -1
index a765fda71536..819631bb61ae 100644
@@ -1532,8 +1532,11 @@ static int __allocate_data_block(struct dnode_of_data *dn, int seg_type)
 	old_blkaddr = dn->data_blkaddr;
 	err = f2fs_allocate_data_block(sbi, NULL, old_blkaddr,
 				&dn->data_blkaddr, &sum, seg_type, NULL);
-	if (err)
+	if (err) {
+		if (old_blkaddr == NULL_ADDR)
+			dec_valid_block_count(sbi, dn->inode, count);
 		return err;
+	}
 
 	if (GET_SEGNO(sbi, old_blkaddr) != NULL_SEGNO)
 		f2fs_invalidate_internal_cache(sbi, old_blkaddr, 1);
Signature · 1 line
2.25.1
maintainer Reviewed-by on v3 1/1
#6 lore raw
Quoted 12 lines · Chen Changcheng

In __allocate_data_block(), when allocating a new data block (dn->data_blkaddr == NULL_ADDR), inc_valid_block_count() is called first to increment total_valid_block_count and i_blocks. If the subsequent f2fs_allocate_data_block() fails, the function returns the error directly without rolling back the already-incremented block counts, causing a permanent leak.

Fix this by calling dec_valid_block_count() to undo the increment before returning the error. The condition old_blkaddr == NULL_ADDR precisely identifies the case where inc_valid_block_count() was called.

Quoted 4 lines
Fixes 7d009e048d7c ("f2fs: fix to handle segment allocation failure correctly")

Thanks,

#7 lore raw
In reply to Chen Changcheng
This patch was applied to jaegeuk/f2fs.git (dev) by Jaegeuk Kim <[email protected]>:

Hello:

This patch was applied to jaegeuk/f2fs.git (dev)
by Jaegeuk Kim <[email protected]>:

Quoted 8 lines · On Tue, 4 Aug 2026 08:54:02 +0800 you wrote:

In __allocate_data_block(), when allocating a new data block (dn->data_blkaddr == NULL_ADDR), inc_valid_block_count() is called first to increment total_valid_block_count and i_blocks. If the subsequent f2fs_allocate_data_block() fails, the function returns the error directly without rolling back the already-incremented block counts, causing a permanent leak.

[...]

Here is the summary with links:
- [f2fs-dev,v3] f2fs: fix valid block count leak on data block allocation failure

    https://git.kernel.org/jaegeuk/f2fs/c/0f9af07ecc1a

You are awesome, thank you!

Signature · 2 lines
Deet-doot-dot, I am a bot.
https://korg.docs.kernel.org/patchwork/pwbot.html