f2fs: introduce reserve_shrink mount option for filesystem shrinkage
Subsystems (2)
PATCH 1/1 f2fs: introduce reserve_shrink mount option for filesystem shrinkage
From: Daeho Jeong <[email protected]>
When preparing for subsequent online filesystem shrinkage (e.g., during partition resizing or FOTA updates), sufficient free space must be preserved so that valid data blocks can be evacuated and the filesystem can safely shrink.
Existing reserve_root cannot prevent space exhaustion by privileged root processes (such as OTA updaters, package managers, and system daemons running with CAP_SYS_RESOURCE), which can allocate blocks from the root reserve and lead to resize failures due to lack of space. Moreover, runtime configurable reserved_blocks represents permanent GC and metadata headroom that must persist after resize, which would cause double-counting if inflated for shrinkage.
To resolve this, introduce a dedicated reserve_shrink=<blocks> mount option:
1. Symmetrically mirrors reserve_root=<blocks> in block units to pre-reserve space specifically for subsequent filesystem shrinkage.
2. In get_available_block_count(), unconditionally deducts reserve_shrink_blocks for all callers, strictly rejecting all allocations (including root / CAP_SYS_RESOURCE) once available blocks are exhausted.
3. In f2fs_statfs(), deducts reserve_shrink_blocks from f_bfree (and f_bavail) so that filesystem statistics accurately reflect usable space.
4. In f2fs_resize_fs(), automatically resets reserve_shrink_blocks to 0 and clears F2FS_MOUNT_RESERVE_SHRINK upon successful shrink completion, releasing any remaining reservation.
5. In sysfs: reserved_blocks, subtracts reserve_shrink_blocks when validating the upper limit of configurable reserved blocks.
--- Documentation/filesystems/f2fs.rst | 7 +++++ fs/f2fs/f2fs.h | 9 ++++++ fs/f2fs/gc.c | 5 ++++ fs/f2fs/super.c | 47 ++++++++++++++++++++++++++++++ fs/f2fs/sysfs.c | 13 +++++++-- 5 files changed, 79 insertions(+), 2 deletions(-)
Documentation/filesystems/f2fs.rst +7 -0
@@ -191,6 +191,13 @@ reserve_node=%d Support configuring reserved nodes which are used for gid, the default limit is 12.5% of all nodes. resuid=%d The user ID which may use the reserved blocks and nodes. resgid=%d The group ID which may use the reserved blocks and nodes. +reserve_shrink=%d Support pre-reserving space for subsequent filesystem + shrinkage (e.g. during partition resizing or FOTA), + unit: blocks. Unlike reserve_root, allocations from + this reserved space strictly reject all callers + (including root / CAP_SYS_RESOURCE). Once the + filesystem is successfully shrunk via resize, this + value is automatically reset to 0. fault_injection=%d Enable fault injection in all supported types with specified injection rate. fault_type=%d Support configuring fault injection type, should be
fs/f2fs/f2fs.h +9 -0
@@ -145,6 +145,7 @@ enum f2fs_mount_opt {*/ F2FS_MOUNT_LAZYTIME ,F2FS_MOUNT_RESERVE_NODE , + F2FS_MOUNT_RESERVE_SHRINK, };#define F2FS_OPTION (sbi) ((sbi)->mount_opt)
@@ -226,6 +227,7 @@ struct f2fs_mount_info {unsigned long long opt;block_t root_reserved_blocks;/* root reserved blocks */ block_t root_reserved_nodes;/* root reserved nodes */ +block_t reserve_shrink_blocks;/* reserve blocks for shrink */ kuid_t s_resuid;/* reserved blocks for uid */ kgid_t s_resgid;/* reserved blocks for gid */ int active_logs;/* # of active logs */
@@ -2658,6 +2660,13 @@ static inline unsigned int get_available_block_count(struct f2fs_sb_info *sbi,if (test_opt (sbi,RESERVE_ROOT ) && !__allow_reserved_root (sbi, inode, cap)) avail_user_block_count -=F2FS_OPTION (sbi).root_reserved_blocks; +if (test_opt (sbi,RESERVE_SHRINK )) { +if (avail_user_block_count >F2FS_OPTION (sbi).reserve_shrink_blocks) + avail_user_block_count -=F2FS_OPTION (sbi).reserve_shrink_blocks; +else + avail_user_block_count =0 ; + } +if (unlikely (is_sbi_flag_set (sbi,SBI_CP_DISABLED ))) {if (avail_user_block_count > sbi->unusable_block_count) avail_user_block_count -= sbi->unusable_block_count;
fs/f2fs/gc.c +5 -0
@@ -2492,6 +2492,11 @@ int f2fs_resize_fs(struct file *filp, __u64 block_count)spin_lock (&sbi->stat_lock); sbi->user_block_count += shrunk_blocks;spin_unlock (&sbi->stat_lock); + }else if (test_opt (sbi,RESERVE_SHRINK )) { +spin_lock (&sbi->stat_lock); +F2FS_OPTION (sbi).reserve_shrink_blocks =0 ; +clear_opt (sbi,RESERVE_SHRINK ); +spin_unlock (&sbi->stat_lock); } out_err:f2fs_up_write_trace (&sbi->cp_global_sem, &clc);
fs/f2fs/super.c +47 -0
107 lines not on this page · raw message
fs/f2fs/sysfs.c +11 -2
@@ -591,9 +591,18 @@ static ssize_t __sbi_store(struct f2fs_attr *a, }#endif if (a->struct_type ==RESERVED_BLOCKS ) { +unsigned long limit; +spin_lock (&sbi->stat_lock); -if (t > (unsigned long )(sbi->user_block_count - -F2FS_OPTION (sbi).root_reserved_blocks)) { + limit = sbi->user_block_count - +F2FS_OPTION (sbi).root_reserved_blocks; +if (test_opt (sbi,RESERVE_SHRINK )) { +if (limit >F2FS_OPTION (sbi).reserve_shrink_blocks) + limit -=F2FS_OPTION (sbi).reserve_shrink_blocks; +else + limit =0 ; + } +if (t > limit) {spin_unlock (&sbi->stat_lock);return -EINVAL ; }
Signature · 1 line
2.55.0.1007.g17ff1f9808-goog
kernel test robot
build botkernel test robot noticed the following build warnings:
Hi Daeho,
kernel test robot noticed the following build warnings:
[auto build test WARNING on jaegeuk-f2fs/dev-test]
[also build test WARNING on jaegeuk-f2fs/dev linus/master v7.3-rc2 next-20260909] [If your patch is applied to the wrong git tree, kindly drop us a note. And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Daeho-Jeong/f2fs-introduce-reserve_shrink-mount-option-for-filesystem-shrinkage/20260910-111056 base: https://git.kernel.org/pub/scm/linux/kernel/git/jaegeuk/f2fs.git dev-test patch link: https://lore.kernel.org/r/20260910181057.640203-1-daeho43%40gmail.com patch subject: [PATCH] f2fs: introduce reserve_shrink mount option for filesystem shrinkage config: powerpc-randconfig-001-20260911 (https://download.01.org/0day-ci/archive/20260911/[email protected]/config) compiler: clang version 21.1.8 (https://github.com/llvm/llvm-project 2078da43e25a4623cab2d0d60decddf709aaea28) reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260911/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All warnings (new ones prefixed by >>):
In file included from fs/f2fs/debug.c:18:
>> fs/f2fs/f2fs.h:2663:6: warning: shift count >= width of type [-Wshift-count-overflow]
2663 | if (test_opt(sbi, RESERVE_SHRINK)) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs/f2fs/f2fs.h:157:26: note: expanded from macro 'test_opt'
157 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~~~~~~~~~~~~~~~~~~~~~~ include/vdso/bits.h:7:26: note: expanded from macro 'BIT'
7 | #define BIT(nr) (UL(1) << (nr))
| ^ ~~~~
1 warning generated.
--
In file included from fs/f2fs/gc.c:19:
>> fs/f2fs/f2fs.h:2663:6: warning: shift count >= width of type [-Wshift-count-overflow]
2663 | if (test_opt(sbi, RESERVE_SHRINK)) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs/f2fs/f2fs.h:157:26: note: expanded from macro 'test_opt'
157 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~~~~~~~~~~~~~~~~~~~~~~
include/vdso/bits.h:7:26: note: expanded from macro 'BIT'
7 | #define BIT(nr) (UL(1) << (nr))
| ^ ~~~~
>> fs/f2fs/gc.c:2495:13: warning: shift count >= width of type [-Wshift-count-overflow]
2495 | } else if (test_opt(sbi, RESERVE_SHRINK)) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs/f2fs/f2fs.h:157:26: note: expanded from macro 'test_opt'
157 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~~~~~~~~~~~~~~~~~~~~~~
include/vdso/bits.h:7:26: note: expanded from macro 'BIT'
7 | #define BIT(nr) (UL(1) << (nr))
| ^ ~~~~
fs/f2fs/gc.c:2498:3: warning: shift count >= width of type [-Wshift-count-overflow]
2498 | clear_opt(sbi, RESERVE_SHRINK);
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs/f2fs/f2fs.h:153:28: note: expanded from macro 'clear_opt'
153 | (F2FS_OPTION(sbi).opt &= ~BIT(F2FS_MOUNT_##option))
| ^~~~~~~~~~~~~~~~~~~~~~~~ include/vdso/bits.h:7:26: note: expanded from macro 'BIT'
7 | #define BIT(nr) (UL(1) << (nr))
| ^ ~~~~
3 warnings generated.
--
In file included from fs/f2fs/super.c:34:
>> fs/f2fs/f2fs.h:2663:6: warning: shift count >= width of type [-Wshift-count-overflow]
2663 | if (test_opt(sbi, RESERVE_SHRINK)) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs/f2fs/f2fs.h:157:26: note: expanded from macro 'test_opt'
157 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~~~~~~~~~~~~~~~~~~~~~~
include/vdso/bits.h:7:26: note: expanded from macro 'BIT'
7 | #define BIT(nr) (UL(1) << (nr))
| ^ ~~~~
>> fs/f2fs/super.c:548:7: warning: shift count >= width of type [-Wshift-count-overflow]
548 | if (!test_opt(sbi, RESERVE_SHRINK))
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs/f2fs/f2fs.h:157:26: note: expanded from macro 'test_opt'
157 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~~~~~~~~~~~~~~~~~~~~~~
include/vdso/bits.h:7:26: note: expanded from macro 'BIT'
7 | #define BIT(nr) (UL(1) << (nr))
| ^ ~~~~
fs/f2fs/super.c:2326:6: warning: shift count >= width of type [-Wshift-count-overflow]
2326 | if (test_opt(sbi, RESERVE_SHRINK)) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs/f2fs/f2fs.h:157:26: note: expanded from macro 'test_opt'
157 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~~~~~~~~~~~~~~~~~~~~~~
include/vdso/bits.h:7:26: note: expanded from macro 'BIT'
7 | #define BIT(nr) (UL(1) << (nr))
| ^ ~~~~
fs/f2fs/super.c:2557:6: warning: shift count >= width of type [-Wshift-count-overflow]
2557 | if (test_opt(sbi, RESERVE_SHRINK))
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs/f2fs/f2fs.h:157:26: note: expanded from macro 'test_opt'
157 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~~~~~~~~~~~~~~~~~~~~~~ include/vdso/bits.h:7:26: note: expanded from macro 'BIT'
7 | #define BIT(nr) (UL(1) << (nr))
| ^ ~~~~
4 warnings generated.
--
In file included from fs/f2fs/sysfs.c:17:
>> fs/f2fs/f2fs.h:2663:6: warning: shift count >= width of type [-Wshift-count-overflow]
2663 | if (test_opt(sbi, RESERVE_SHRINK)) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs/f2fs/f2fs.h:157:26: note: expanded from macro 'test_opt'
157 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~~~~~~~~~~~~~~~~~~~~~~
include/vdso/bits.h:7:26: note: expanded from macro 'BIT'
7 | #define BIT(nr) (UL(1) << (nr))
| ^ ~~~~
>> fs/f2fs/sysfs.c:599:7: warning: shift count >= width of type [-Wshift-count-overflow]
599 | if (test_opt(sbi, RESERVE_SHRINK)) {
| ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
fs/f2fs/f2fs.h:157:26: note: expanded from macro 'test_opt'
157 | (F2FS_OPTION(sbi).opt & BIT(F2FS_MOUNT_##option))
| ^~~~~~~~~~~~~~~~~~~~~~~~ include/vdso/bits.h:7:26: note: expanded from macro 'BIT'
7 | #define BIT(nr) (UL(1) << (nr))
| ^ ~~~~
2 warnings generated.
Kconfig warnings: (for reference only)
WARNING: unmet direct dependencies detected for HOTPLUG_CPU
Depends on [n]: SMP [=y] && (PPC_PSERIES [=n] || PPC_PMAC [=n] || PPC_POWERNV [=n] || FSL_SOC_BOOKE [=n]) Selected by [y]:
- PM_SLEEP_SMP [=y] && SMP [=y] && (ARCH_SUSPEND_POSSIBLE [=n] || ARCH_HIBERNATION_POSSIBLE [=y]) && PM_SLEEP [=y]
vim +2663 fs/f2fs/f2fs.h
2650
2651 static inline unsigned int get_available_block_count(struct f2fs_sb_info *sbi,
2652 struct inode *inode, bool cap)
2653 {
2654 block_t avail_user_block_count;
2655
2656 avail_user_block_count = sbi->user_block_count -
2657 sbi->current_reserved_blocks -
2658 sbi->alias_reserved_blocks;
2659
2660 if (test_opt(sbi, RESERVE_ROOT) && !__allow_reserved_root(sbi, inode, cap))
2661 avail_user_block_count -= F2FS_OPTION(sbi).root_reserved_blocks;
2662
> 2663 if (test_opt(sbi, RESERVE_SHRINK)) { 2664 if (avail_user_block_count > F2FS_OPTION(sbi).reserve_shrink_blocks)
2665 avail_user_block_count -= F2FS_OPTION(sbi).reserve_shrink_blocks;
2666 else
2667 avail_user_block_count = 0;
2668 }
2669
2670 if (unlikely(is_sbi_flag_set(sbi, SBI_CP_DISABLED))) {
2671 if (avail_user_block_count > sbi->unusable_block_count)
2672 avail_user_block_count -= sbi->unusable_block_count;
2673 else
2674 avail_user_block_count = 0;
2675 }
2676
2677 return avail_user_block_count;
2678 }
2679
--
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki