Re: [PATCH] f2fs: keep pinned fallocate failures section-aligned

From: Wenjie Qi

Date: Mon Aug 03 2026 - 04:36:21 EST


Yes. I used a block-allocation fault injection reproducer.

My reproducer uses F2FS FAULT_BLOCK injection in the f2fs_map_blocks()
path. Guest steps were:

1. mount /dev/vdb
2. create a pinned file
3. set inject_type=128 (FAULT_BLOCK)
4. set inject_rate=257 and run:
/media/repro_f2fs_pinfile_fault falloc /mnt/f2fs/pinfile 2097152

On the old code this returns -ENOSPC, but the file is left as:
SIZE=1048576 BLOCKS=2048

So the failure happened in the middle of a 2MiB section, and the pinned
file was left section-unaligned.

On Mon, Aug 3, 2026 at 3:24 PM Chao Yu <chao@xxxxxxxxxx> wrote:
>
> On 7/23/26 21:36, Wenjie Qi wrote:
> > Pinned fallocate allocates one section at a time. If f2fs_map_blocks()
> > fails after allocating part of the current section, the error path still
> > adds the partial length to expanded and clears FADVISE_TRUNC_BIT. A failed
> > fallocate can then leave a pinned file with a non-section-aligned size.
>
> Do you have a reproducer? maybe inject memory allocation failure?
>
> Thanks,
>
> >
> > Count only complete sections on failure and keep FADVISE_TRUNC_BIT set
> > until the failed section preallocation has been removed.
> >
> > Fixes: f5a53edcf01e ("f2fs: support aligned pinned file")
> > Cc: stable@xxxxxxxxxx
> > Signed-off-by: Wenjie Qi <qiwenjie@xxxxxxxxxx>
> > ---
> > fs/f2fs/file.c | 17 ++++++++++++++---
> > 1 file changed, 14 insertions(+), 3 deletions(-)
> >
> > diff --git a/fs/f2fs/file.c b/fs/f2fs/file.c
> > index c54897a25981..83af73f3831e 100644
> > --- a/fs/f2fs/file.c
> > +++ b/fs/f2fs/file.c
> > @@ -1966,11 +1966,12 @@ static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
> >
> > map.m_seg_type = CURSEG_COLD_DATA_PINNED;
> > err = f2fs_map_blocks(inode, &map, F2FS_GET_BLOCK_PRE_DIO);
> > - file_dont_truncate(inode);
> > + if (!err)
> > + file_dont_truncate(inode);
> >
> > f2fs_up_write(&sbi->pin_sem);
> >
> > - expanded += map.m_len;
> > + expanded += err ? rounddown(map.m_len, sec_blks) : map.m_len;
> > sec_len -= map.m_len;
> > map.m_lblk += map.m_len;
> > if (!err && sec_len)
> > @@ -1986,7 +1987,7 @@ static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
> > pgoff_t last_off;
> >
> > if (!expanded)
> > - return err;
> > + goto out;
> >
> > last_off = pg_start + expanded - 1;
> >
> > @@ -2004,6 +2005,16 @@ static int f2fs_expand_inode_data(struct inode *inode, loff_t offset,
> > f2fs_i_size_write(inode, new_size);
> > }
> >
> > +out:
> > + if (err && f2fs_is_pinned_file(inode) && file_should_truncate(inode)) {
> > + f2fs_down_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
> > + filemap_invalidate_lock(inode->i_mapping);
> > + if (!f2fs_truncate(inode))
> > + file_dont_truncate(inode);
> > + filemap_invalidate_unlock(inode->i_mapping);
> > + f2fs_up_write(&F2FS_I(inode)->i_gc_rwsem[WRITE]);
> > + }
> > +
> > return err;
> > }
> >
>