[PATCH v2 2/2] f2fs: refresh pinned allocation boundary after resize
From: Wenjie Qi
Date: Sat Aug 29 2026 - 02:06:45 EST
pinned_area_max_secno is derived from MAIN_SECS(), the zoned device
boundary, and resizable_tail_secno. It is currently recalculated only
at mount and remount, so online resize leaves the pre-resize value
active after MAIN_SECS() changes.
A stale value can allow pinned allocations in sections outside the
new main-area boundary.
Move the calculation to segment.h and call it whenever
update_fs_metadata() changes MAIN_SECS(). This also restores the
previous boundary when a final checkpoint error rolls the resize
metadata back.
Mount option validation compares the tail count with the pre-resize
section count. Recalculating after a shrink below that tail would make
the unsigned subtraction wrap. Reject such a shrink with -EINVAL and
report the geometry before acquiring a mount write reference.
A concurrent remount can change the tail after that early check.
Recheck it after freezing the filesystem and taking the resize locks,
before changing block counts or filesystem geometry.
Fixes: c966d29e01bb ("f2fs: support resizable tail section and unify pinned allocation")
Signed-off-by: Wenjie Qi <qiwenjie@xxxxxxxxxx>
---
Changes since v1:
- Simplify the incompatible-tail condition.
- Log the main, shrink, and tail section counts on rejection.
- Validate before acquiring the mount write reference.
- Restore compact formatting in the shared boundary helper.
- Recheck after freeze to cover a concurrent remount option change.
fs/f2fs/gc.c | 27 ++++++++++++++++++++++++---
fs/f2fs/segment.h | 13 +++++++++++++
fs/f2fs/super.c | 15 ++-------------
3 files changed, 39 insertions(+), 16 deletions(-)
diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
index 0a00180c21dc..3c61bf675afa 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -2326,6 +2326,7 @@ static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs)
SM_I(sbi)->segment_count = (int)SM_I(sbi)->segment_count + segs;
MAIN_SEGS(sbi) = (int)MAIN_SEGS(sbi) + segs;
MAIN_SECS(sbi) += secs;
+ f2fs_adjust_pinned_area_boundary(sbi);
if (sbi->allocate_section_hint > MAIN_SECS(sbi))
sbi->allocate_section_hint = MAIN_SECS(sbi);
FREE_I(sbi)->free_sections = (int)FREE_I(sbi)->free_sections + secs;
@@ -2349,6 +2350,19 @@ static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs)
}
}
+static bool f2fs_resize_tail_invalid(struct f2fs_sb_info *sbi,
+ unsigned int secs)
+{
+ if (MAIN_SECS(sbi) >
+ secs + F2FS_OPTION(sbi).resizable_tail_secno)
+ return false;
+
+ f2fs_err(sbi, "Invalid resize: main %u, shrink %u, tail %u",
+ MAIN_SECS(sbi), secs,
+ F2FS_OPTION(sbi).resizable_tail_secno);
+ return true;
+}
+
int f2fs_resize_fs(struct file *filp, __u64 block_count)
{
struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
@@ -2392,13 +2406,15 @@ int f2fs_resize_fs(struct file *filp, __u64 block_count)
return -EINVAL;
}
+ shrunk_blocks = old_block_count - block_count;
+ secs = div_u64(shrunk_blocks, BLKS_PER_SEC(sbi));
+ if (f2fs_resize_tail_invalid(sbi, secs))
+ return -EINVAL;
+
err = mnt_want_write_file(filp);
if (err)
return err;
- shrunk_blocks = old_block_count - block_count;
- secs = div_u64(shrunk_blocks, BLKS_PER_SEC(sbi));
-
/* stop other GC */
if (!f2fs_down_write_trylock_trace(&sbi->gc_lock, &glc)) {
err = -EAGAIN;
@@ -2442,6 +2458,11 @@ int f2fs_resize_fs(struct file *filp, __u64 block_count)
f2fs_down_write_trace(&sbi->gc_lock, &glc);
f2fs_down_write_trace(&sbi->cp_global_sem, &clc);
+ if (f2fs_resize_tail_invalid(sbi, secs)) {
+ err = -EINVAL;
+ goto out_err;
+ }
+
spin_lock(&sbi->stat_lock);
if (shrunk_blocks + valid_user_blocks(sbi) +
sbi->current_reserved_blocks + sbi->unusable_block_count +
diff --git a/fs/f2fs/segment.h b/fs/f2fs/segment.h
index 5949aa5200ac..a31d3a6ca756 100644
--- a/fs/f2fs/segment.h
+++ b/fs/f2fs/segment.h
@@ -91,6 +91,19 @@ static inline void sanity_check_seg_type(struct f2fs_sb_info *sbi,
#define GET_ZONE_FROM_SEG(sbi, segno) \
GET_ZONE_FROM_SEC(sbi, GET_SEC_FROM_SEG(sbi, segno))
+static inline void f2fs_adjust_pinned_area_boundary(struct f2fs_sb_info *sbi)
+{
+ sbi->pinned_area_max_secno = MAIN_SECS(sbi);
+ if (f2fs_sb_has_blkzoned(sbi) &&
+ sbi->first_seq_zone_segno != NULL_SEGNO)
+ sbi->pinned_area_max_secno = min(sbi->pinned_area_max_secno,
+ GET_SEC_FROM_SEG(sbi, sbi->first_seq_zone_segno));
+ if (F2FS_OPTION(sbi).resizable_tail_secno)
+ sbi->pinned_area_max_secno = min(sbi->pinned_area_max_secno,
+ MAIN_SECS(sbi) -
+ F2FS_OPTION(sbi).resizable_tail_secno);
+}
+
#define GET_SUM_BLOCK(sbi, segno) \
(SM_I(sbi)->ssa_blkaddr + (segno / (sbi)->sums_per_block))
#define GET_SUM_BLKOFF(sbi, segno) (segno % (sbi)->sums_per_block)
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index 253a579e9d5b..c8f8ecf49635 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -554,17 +554,6 @@ static inline void adjust_unusable_cap_perc(struct f2fs_sb_info *sbi)
F2FS_OPTION(sbi).unusable_cap_perc);
}
-static inline void adjust_pinned_area_boundary(struct f2fs_sb_info *sbi)
-{
- sbi->pinned_area_max_secno = MAIN_SECS(sbi);
- if (f2fs_sb_has_blkzoned(sbi) && sbi->first_seq_zone_segno != NULL_SEGNO)
- sbi->pinned_area_max_secno = min(sbi->pinned_area_max_secno,
- GET_SEC_FROM_SEG(sbi, sbi->first_seq_zone_segno));
- if (F2FS_OPTION(sbi).resizable_tail_secno)
- sbi->pinned_area_max_secno = min(sbi->pinned_area_max_secno,
- MAIN_SECS(sbi) - F2FS_OPTION(sbi).resizable_tail_secno);
-}
-
static void init_once(void *foo)
{
struct f2fs_inode_info *fi = (struct f2fs_inode_info *) foo;
@@ -3078,7 +3067,7 @@ static int __f2fs_remount(struct fs_context *fc, struct super_block *sb)
sb->s_flags = (sb->s_flags & ~SB_POSIXACL) |
(test_opt(sbi, POSIX_ACL) ? SB_POSIXACL : 0);
- adjust_pinned_area_boundary(sbi);
+ f2fs_adjust_pinned_area_boundary(sbi);
limit_reserve_root(sbi);
fc->sb_flags = (flags & ~SB_LAZYTIME) | (sb->s_flags & SB_LAZYTIME);
@@ -5321,7 +5310,7 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
/* get segno of first zoned block device */
sbi->first_seq_zone_segno = get_first_seq_zone_segno(sbi);
- adjust_pinned_area_boundary(sbi);
+ f2fs_adjust_pinned_area_boundary(sbi);
sbi->reserved_pin_section = f2fs_sb_has_blkzoned(sbi) ?
ZONED_PIN_SEC_REQUIRED_COUNT :
--
2.43.0