All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] f2fs: keep pinned fallocate failures section-aligned
@ 2026-07-23 13:36 ` Wenjie Qi
  0 siblings, 0 replies; 6+ messages in thread
From: Wenjie Qi @ 2026-07-23 13:36 UTC (permalink / raw)
  To: jaegeuk, chao; +Cc: linux-f2fs-devel, linux-kernel, qiwenjie, qwjhust, stable

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.

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@kernel.org
Signed-off-by: Wenjie Qi <qiwenjie@xiaomi.com>
---
 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;
 }
 
-- 
2.43.0


^ permalink raw reply related	[flat|nested] 6+ messages in thread

* [f2fs-dev] [PATCH] f2fs: keep pinned fallocate failures section-aligned
@ 2026-07-23 13:36 ` Wenjie Qi
  0 siblings, 0 replies; 6+ messages in thread
From: Wenjie Qi @ 2026-07-23 13:36 UTC (permalink / raw)
  To: jaegeuk, chao; +Cc: qwjhust, stable, qiwenjie, linux-kernel, linux-f2fs-devel

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.

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@kernel.org
Signed-off-by: Wenjie Qi <qiwenjie@xiaomi.com>
---
 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;
 }
 
-- 
2.43.0



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply related	[flat|nested] 6+ messages in thread

* Re: [f2fs-dev] [PATCH] f2fs: keep pinned fallocate failures section-aligned
  2026-07-23 13:36 ` [f2fs-dev] " Wenjie Qi
@ 2026-08-03  7:24   ` Chao Yu
  -1 siblings, 0 replies; 6+ messages in thread
From: Chao Yu via Linux-f2fs-devel @ 2026-08-03  7:24 UTC (permalink / raw)
  To: Wenjie Qi, jaegeuk; +Cc: stable, qiwenjie, linux-kernel, linux-f2fs-devel

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@kernel.org
> Signed-off-by: Wenjie Qi <qiwenjie@xiaomi.com>
> ---
>   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;
>   }
>   



_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] f2fs: keep pinned fallocate failures section-aligned
@ 2026-08-03  7:24   ` Chao Yu
  0 siblings, 0 replies; 6+ messages in thread
From: Chao Yu @ 2026-08-03  7:24 UTC (permalink / raw)
  To: Wenjie Qi, jaegeuk; +Cc: chao, linux-f2fs-devel, linux-kernel, qiwenjie, stable

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@kernel.org
> Signed-off-by: Wenjie Qi <qiwenjie@xiaomi.com>
> ---
>   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;
>   }
>   


^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [f2fs-dev] [PATCH] f2fs: keep pinned fallocate failures section-aligned
  2026-08-03  7:24   ` Chao Yu
@ 2026-08-03  8:35     ` Wenjie Qi
  -1 siblings, 0 replies; 6+ messages in thread
From: Wenjie Qi @ 2026-08-03  8:35 UTC (permalink / raw)
  To: Chao Yu; +Cc: jaegeuk, qiwenjie, linux-kernel, stable, linux-f2fs-devel

  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@kernel.org> 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@kernel.org
> > Signed-off-by: Wenjie Qi <qiwenjie@xiaomi.com>
> > ---
> >   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;
> >   }
> >
>


_______________________________________________
Linux-f2fs-devel mailing list
Linux-f2fs-devel@lists.sourceforge.net
https://lists.sourceforge.net/lists/listinfo/linux-f2fs-devel

^ permalink raw reply	[flat|nested] 6+ messages in thread

* Re: [PATCH] f2fs: keep pinned fallocate failures section-aligned
@ 2026-08-03  8:35     ` Wenjie Qi
  0 siblings, 0 replies; 6+ messages in thread
From: Wenjie Qi @ 2026-08-03  8:35 UTC (permalink / raw)
  To: Chao Yu; +Cc: jaegeuk, linux-f2fs-devel, linux-kernel, qiwenjie, stable

  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@kernel.org> 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@kernel.org
> > Signed-off-by: Wenjie Qi <qiwenjie@xiaomi.com>
> > ---
> >   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;
> >   }
> >
>

^ permalink raw reply	[flat|nested] 6+ messages in thread

end of thread, other threads:[~2026-08-03  8:36 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-07-23 13:36 [PATCH] f2fs: keep pinned fallocate failures section-aligned Wenjie Qi
2026-07-23 13:36 ` [f2fs-dev] " Wenjie Qi
2026-08-03  7:24 ` Chao Yu via Linux-f2fs-devel
2026-08-03  7:24   ` Chao Yu
2026-08-03  8:35   ` [f2fs-dev] " Wenjie Qi
2026-08-03  8:35     ` Wenjie Qi

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.