Re: [f2fs-dev] [PATCH] f2fs: fix valid block count leak on data block allocation failure

Chao Yu via Linux-f2fs-devel <[email protected]> Mon, 3 Aug 2026 15:11:23 +0800
Newsgroups net.sourceforge.lists.linux-f2fs-devel,org.kernel.vger.linux-kernel
Message-ID <[email protected]>
On 7/22/26 08:54, Chen Changcheng 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.
> 
> 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?

> Signed-off-by: Chen Changcheng <[email protected]>

Reviewed-by: Chao Yu <[email protected]>

Thanks,

> ---
>   fs/f2fs/data.c | 5 ++++-
>   1 file changed, 4 insertions(+), 1 deletion(-)
> 
> diff --git a/fs/f2fs/data.c b/fs/f2fs/data.c
> index a765fda71536..819631bb61ae 100644
> --- a/fs/f2fs/data.c
> +++ b/fs/f2fs/data.c
> @@ -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);



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