Linux Documentation
 help / color / mirror / Atom feed
* [PATCH 0/2] f2fs: multi-device expandable storage support
@ 2026-08-28 12:18 李珂11
  2026-08-28 12:18 ` [PATCH 1/2] f2fs: add alloc_primary mount option 李珂11
  2026-08-28 12:18 ` [PATCH 2/2] f2fs: allow f2fs_resize_fs to drop the last secondary device 李珂11
  0 siblings, 2 replies; 3+ messages in thread
From: 李珂11 @ 2026-08-28 12:18 UTC (permalink / raw)
  To: linux-f2fs-devel@lists.sourceforge.net
  Cc: jaegeuk@kernel.org, chao@kernel.org, linux-doc@vger.kernel.org,
	李珂11

From: "ke.li11" <ke.li11@transsion.com>

This series improves multi-device F2FS for expandable storage setups
where a secondary block device holds warm/cold data and may be removed
after userspace performs a final shrink.

Patch 1 adds an alloc_primary mount option.  By default F2FS steers
warm/cold allocations to the secondary device via ALLOC_NEXT; this
option keeps them on the primary device when required by userspace
volume management.

Patch 2 extends f2fs_resize_fs() to allow a section-aligned shrink
that fully removes the last secondary device.  The on-disk superblock
path for that device is cleared before commit so the next mount does
not try to open a block device that userspace is deleting.  The
in-memory device is detached only after both the superblock update and
checkpoint succeed; the path is restored if either step fails.

Testing:
- Mounted /data as a multi-device F2FS filesystem with a secondary expand
  device (device=exp_user_device)
- Exercised dynamic resize while the expand volume is attached
- Shrunk the filesystem until the entire exp_user_device is removed;
  this triggers the drop_last_dev path in f2fs_resize_fs()
- Verified remount succeeds after userspace tears down the expand
  block device
- Mounted with and without alloc_primary and verified allocation behavior

Signed-off-by: ke.li11 <ke.li11@transsion.com>

ke.li11 (2):
  f2fs: add alloc_primary mount option
  f2fs: allow f2fs_resize_fs to drop the last secondary device

 Documentation/filesystems/f2fs.rst |   3 +
 fs/f2fs/f2fs.h                     |   6 ++
 fs/f2fs/gc.c                       | 127 +++++++++++++++++++++++++++--
 fs/f2fs/super.c                    |  12 +++
 4 files changed, 143 insertions(+), 5 deletions(-)

-- 
2.34.1


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

* [PATCH 1/2] f2fs: add alloc_primary mount option
  2026-08-28 12:18 [PATCH 0/2] f2fs: multi-device expandable storage support 李珂11
@ 2026-08-28 12:18 ` 李珂11
  2026-08-28 12:18 ` [PATCH 2/2] f2fs: allow f2fs_resize_fs to drop the last secondary device 李珂11
  1 sibling, 0 replies; 3+ messages in thread
From: 李珂11 @ 2026-08-28 12:18 UTC (permalink / raw)
  To: linux-f2fs-devel@lists.sourceforge.net
  Cc: jaegeuk@kernel.org, chao@kernel.org, linux-doc@vger.kernel.org,
	李珂11

From: "ke.li11" <ke.li11@transsion.com>

In multi-device setups, F2FS steers warm/cold data to the secondary
device by setting ALLOC_NEXT past the primary device end.  Add an
alloc_primary mount option to keep such allocations on the primary
device when userspace manages expandable storage on a separate volume.

Document the new option in Documentation/filesystems/f2fs.rst.

Signed-off-by: ke.li11 <ke.li11@transsion.com>
---
 Documentation/filesystems/f2fs.rst |  3 +++
 fs/f2fs/f2fs.h                     |  6 ++++++
 fs/f2fs/gc.c                       |  3 ++-
 fs/f2fs/super.c                    | 12 ++++++++++++
 4 files changed, 23 insertions(+), 1 deletion(-)

diff --git a/Documentation/filesystems/f2fs.rst b/Documentation/filesystems/f2fs.rst
index b45d7a687625..f065639d931d 100644
--- a/Documentation/filesystems/f2fs.rst
+++ b/Documentation/filesystems/f2fs.rst
@@ -395,6 +395,9 @@ errors=%s		 Specify f2fs behavior on critical errors. This supports modes:
 			     ====================== =============== =============== ========
 nat_bits		 Enable nat_bits feature to enhance full/empty nat blocks access,
 			 by default it's disabled.
+alloc_primary		 Keep warm/cold/pin data allocation on the primary device
+			 in a multi-device configuration.  Without this option, F2FS
+			 steers ALLOC_NEXT segments to the secondary device.
 lookup_mode=%s		 Control the directory lookup behavior for casefolded
 			 directories. This option has no effect on directories
 			 that do not have the casefold feature enabled.
diff --git a/fs/f2fs/f2fs.h b/fs/f2fs/f2fs.h
index 8376bbe58ee3..2db4d6a2837a 100644
--- a/fs/f2fs/f2fs.h
+++ b/fs/f2fs/f2fs.h
@@ -266,6 +266,12 @@ struct f2fs_mount_info {
 	unsigned char extensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN];	/* extensions */
 	unsigned char noextensions[COMPRESS_EXT_NUM][F2FS_EXTENSION_LEN]; /* extensions */
 	unsigned int lookup_mode;
+	/*
+	 * alloc_primary: keep warm/cold/pin allocation on the primary
+	 * device.  Skips the multi-device ALLOC_NEXT hint that would
+	 * otherwise steer them onto the secondary (expand) device.
+	 */
+	bool alloc_primary;
 };
 
 #define F2FS_FEATURE_ENCRYPT			0x00000001
diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
index ffaa7ba76a1b..c5c243e6d3b2 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -2147,7 +2147,8 @@ void f2fs_build_gc_manager(struct f2fs_sb_info *sbi)
 	sbi->gc_pin_file_threshold = DEF_GC_FAILED_PINNED_FILES;
 
 	/* give warm/cold data area from slower device */
-	if (f2fs_is_multi_device(sbi) && !__is_large_section(sbi))
+	if (!F2FS_OPTION(sbi).alloc_primary &&
+	    f2fs_is_multi_device(sbi) && !__is_large_section(sbi))
 		SIT_I(sbi)->last_victim[ALLOC_NEXT] =
 				GET_SEGNO(sbi, FDEV(0).end_blk) + 1;
 
diff --git a/fs/f2fs/super.c b/fs/f2fs/super.c
index be22c31015ef..3df06c525a2c 100644
--- a/fs/f2fs/super.c
+++ b/fs/f2fs/super.c
@@ -235,6 +235,7 @@ enum {
 	Opt_jqfmt,
 	Opt_checkpoint,
 	Opt_lookup_mode,
+	Opt_alloc_primary,
 	Opt_err,
 };
 
@@ -366,6 +367,7 @@ static const struct fs_parameter_spec f2fs_param_specs[] = {
 	fsparam_flag("age_extent_cache", Opt_age_extent_cache),
 	fsparam_enum("errors", Opt_errors, f2fs_param_errors),
 	fsparam_enum("lookup_mode", Opt_lookup_mode, f2fs_param_lookup_mode),
+	fsparam_flag("alloc_primary", Opt_alloc_primary),
 	{}
 };
 
@@ -404,6 +406,7 @@ static match_table_t f2fs_checkpoint_tokens = {
 #define F2FS_SPEC_errors			(1 << 23)
 #define F2FS_SPEC_lookup_mode			(1 << 24)
 #define F2FS_SPEC_reserve_node			(1 << 25)
+#define F2FS_SPEC_alloc_primary			(1 << 26)
 
 struct f2fs_fs_context {
 	struct f2fs_mount_info info;
@@ -1235,6 +1238,10 @@ static int f2fs_parse_param(struct fs_context *fc, struct fs_parameter *param)
 		F2FS_CTX_INFO(ctx).lookup_mode = result.uint_32;
 		ctx->spec_mask |= F2FS_SPEC_lookup_mode;
 		break;
+	case Opt_alloc_primary:
+		F2FS_CTX_INFO(ctx).alloc_primary = true;
+		ctx->spec_mask |= F2FS_SPEC_alloc_primary;
+		break;
 	}
 	return 0;
 }
@@ -1763,6 +1770,8 @@ static void f2fs_apply_options(struct fs_context *fc, struct super_block *sb)
 		F2FS_OPTION(sbi).errors = F2FS_CTX_INFO(ctx).errors;
 	if (ctx->spec_mask & F2FS_SPEC_lookup_mode)
 		F2FS_OPTION(sbi).lookup_mode = F2FS_CTX_INFO(ctx).lookup_mode;
+	if (ctx->spec_mask & F2FS_SPEC_alloc_primary)
+		F2FS_OPTION(sbi).alloc_primary = F2FS_CTX_INFO(ctx).alloc_primary;
 
 	f2fs_apply_compression(fc, sb);
 	f2fs_apply_test_dummy_encryption(fc, sb);
@@ -2543,6 +2552,9 @@ static int f2fs_show_options(struct seq_file *seq, struct dentry *root)
 	else if (F2FS_OPTION(sbi).lookup_mode == LOOKUP_AUTO)
 		seq_show_option(seq, "lookup_mode", "auto");
 
+	if (F2FS_OPTION(sbi).alloc_primary)
+		seq_puts(seq, ",alloc_primary");
+
 	return 0;
 }
 
-- 
2.34.1


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

* [PATCH 2/2] f2fs: allow f2fs_resize_fs to drop the last secondary device
  2026-08-28 12:18 [PATCH 0/2] f2fs: multi-device expandable storage support 李珂11
  2026-08-28 12:18 ` [PATCH 1/2] f2fs: add alloc_primary mount option 李珂11
@ 2026-08-28 12:18 ` 李珂11
  1 sibling, 0 replies; 3+ messages in thread
From: 李珂11 @ 2026-08-28 12:18 UTC (permalink / raw)
  To: linux-f2fs-devel@lists.sourceforge.net
  Cc: jaegeuk@kernel.org, chao@kernel.org, linux-doc@vger.kernel.org,
	李珂11

From: "ke.li11" <ke.li11@transsion.com>

When shrinking a multi-device filesystem, allow a section-aligned
resize that fully removes the last secondary device.  Clear the
device path in the on-disk superblock before commit so the next
mount does not open a block device that userspace is removing, and
detach the in-memory device only after superblock and checkpoint
are both persisted.  Restore the path if commit or checkpoint fails.

Signed-off-by: ke.li11 <ke.li11@transsion.com>
---
 fs/f2fs/gc.c | 124 +++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 120 insertions(+), 4 deletions(-)

diff --git a/fs/f2fs/gc.c b/fs/f2fs/gc.c
index c5c243e6d3b2..737bac48c8f5 100644
--- a/fs/f2fs/gc.c
+++ b/fs/f2fs/gc.c
@@ -2316,6 +2316,84 @@ static void update_fs_metadata(struct f2fs_sb_info *sbi, int secs)
 	}
 }
 
+/*
+ * After a shrink that zeroes the last secondary device's total_segments,
+ * clear its on-disk path so the next mount does not try to open a mapper
+ * that userspace is about to tear down.
+ *
+ * Must be called with total_segments already updated; restores path on
+ * the raw superblock if @restore is true (commit failed).
+ */
+static void f2fs_update_empty_secondary_sb_path(struct f2fs_sb_info *sbi,
+					     char path_backup[MAX_PATH_LEN],
+					     bool restore)
+{
+	struct f2fs_super_block *raw_sb = F2FS_RAW_SUPER(sbi);
+	int last_dev;
+
+	if (!f2fs_is_multi_device(sbi))
+		return;
+
+	last_dev = sbi->s_ndevs - 1;
+	if (last_dev < 1)
+		return;
+
+	f2fs_down_write(&sbi->sb_lock);
+	if (restore) {
+		if (path_backup[0]) {
+			memcpy(raw_sb->devs[last_dev].path, path_backup,
+			       MAX_PATH_LEN);
+			f2fs_info(sbi,
+				  "resize_fs: restored FDEV[%d] path after failed drop",
+				  last_dev);
+		}
+	} else if (le32_to_cpu(raw_sb->devs[last_dev].total_segments) == 0 &&
+		   raw_sb->devs[last_dev].path[0]) {
+		memcpy(path_backup, raw_sb->devs[last_dev].path, MAX_PATH_LEN);
+		memset(raw_sb->devs[last_dev].path, 0, MAX_PATH_LEN);
+		f2fs_info(sbi,
+			  "resize_fs: cleared FDEV[%d] path (total_segments=0)",
+			  last_dev);
+	}
+	f2fs_up_write(&sbi->sb_lock);
+}
+
+/* Detach last empty secondary from in-memory FDEV[] after successful SB+CP. */
+static void f2fs_detach_empty_secondary(struct f2fs_sb_info *sbi)
+{
+	int last_dev;
+
+	if (!f2fs_is_multi_device(sbi))
+		return;
+
+	last_dev = sbi->s_ndevs - 1;
+	if (last_dev < 1)
+		return;
+
+	if (FDEV(last_dev).total_segments != 0)
+		return;
+
+	f2fs_info(sbi,
+		  "resize_fs: dropping FDEV[%d], ndevs %d->%d",
+		  last_dev, sbi->s_ndevs, sbi->s_ndevs - 1);
+
+	if (FDEV(last_dev).bdev_file) {
+		fs_bdev_file_release(FDEV(last_dev).bdev_file, sbi->sb);
+		FDEV(last_dev).bdev_file = NULL;
+		FDEV(last_dev).bdev = NULL;
+	}
+#ifdef CONFIG_BLK_DEV_ZONED
+	kvfree(FDEV(last_dev).blkz_seq);
+	FDEV(last_dev).blkz_seq = NULL;
+#endif
+	memset(FDEV(last_dev).path, 0, MAX_PATH_LEN + 1);
+	FDEV(last_dev).total_segments = 0;
+	FDEV(last_dev).start_blk = 0;
+	FDEV(last_dev).end_blk = 0;
+	sbi->s_ndevs--;
+}
+
+
 int f2fs_resize_fs(struct file *filp, __u64 block_count)
 {
 	struct f2fs_sb_info *sbi = F2FS_I_SB(file_inode(filp));
@@ -2327,6 +2405,8 @@ int f2fs_resize_fs(struct file *filp, __u64 block_count)
 	unsigned int secs;
 	int err = 0;
 	__u32 rem;
+	bool drop_last_dev = false;
+	char drop_path_backup[MAX_PATH_LEN] = {};
 
 	old_block_count = le64_to_cpu(F2FS_RAW_SUPER(sbi)->block_count);
 	if (block_count > old_block_count)
@@ -2336,9 +2416,33 @@ int f2fs_resize_fs(struct file *filp, __u64 block_count)
 		int last_dev = sbi->s_ndevs - 1;
 		__u64 last_segs = FDEV(last_dev).total_segments;
 
-		if (block_count + SEGS_TO_BLKS(sbi, last_segs) <=
-								old_block_count)
-			return -EINVAL;
+		__u64 last_blks = SEGS_TO_BLKS(sbi, last_segs);
+		__u64 primary_end = old_block_count - last_blks;
+
+		/*
+		 * Stock F2FS rejects any shrink with block_count + last_blks <=
+		 * old_block_count, i.e. the new size would no longer cover the
+		 * last secondary device.  As an exception, accept a section-
+		 * aligned @block_count at or below primary_end when it removes
+		 * the entire last device (drop_last_dev), so userspace can
+		 * delete the expand volume after the final shrink.  Still reject
+		 * if @block_count falls below the section boundary at
+		 * primary_end and would shrink into the primary device.
+		 */
+		if (block_count <= primary_end) {
+			__u64 primary_aligned = rounddown(primary_end,
+							  BLKS_PER_SEC(sbi));
+
+			if (block_count < primary_aligned) {
+				f2fs_err(sbi,
+					 "resize_fs: reject new=%llu < primary_aligned=%llu (primary_end=%llu)",
+					 (unsigned long long)block_count,
+					 (unsigned long long)primary_aligned,
+					 (unsigned long long)primary_end);
+				return -EINVAL;
+			}
+			drop_last_dev = true;
+		}
 	}
 
 	/* new fs size should align to section size */
@@ -2426,9 +2530,14 @@ int f2fs_resize_fs(struct file *filp, __u64 block_count)
 		goto recover_out;
 
 	update_sb_metadata(sbi, -secs);
+	if (drop_last_dev)
+		f2fs_update_empty_secondary_sb_path(sbi, drop_path_backup, false);
 
 	err = f2fs_commit_super(sbi, false);
 	if (err) {
+		if (drop_last_dev)
+			f2fs_update_empty_secondary_sb_path(sbi, drop_path_backup,
+							 true);
 		update_sb_metadata(sbi, secs);
 		goto recover_out;
 	}
@@ -2441,14 +2550,21 @@ int f2fs_resize_fs(struct file *filp, __u64 block_count)
 	err = f2fs_write_checkpoint(sbi, &cpc);
 	if (err) {
 		update_fs_metadata(sbi, secs);
+		if (drop_last_dev)
+			f2fs_update_empty_secondary_sb_path(sbi, drop_path_backup,
+							 true);
 		update_sb_metadata(sbi, secs);
 		f2fs_commit_super(sbi, false);
+	} else if (drop_last_dev) {
+		f2fs_detach_empty_secondary(sbi);
 	}
 recover_out:
 	clear_sbi_flag(sbi, SBI_IS_RESIZEFS);
 	if (err) {
 		set_sbi_flag(sbi, SBI_NEED_FSCK);
-		f2fs_err(sbi, "resize_fs failed, should run fsck to repair!");
+		f2fs_err(sbi,
+			"resize_fs failed err=%d, should run fsck to repair!",
+			err);
 
 		spin_lock(&sbi->stat_lock);
 		sbi->user_block_count += shrunk_blocks;
-- 
2.34.1


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

end of thread, other threads:[~2026-08-28 12:19 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2026-08-28 12:18 [PATCH 0/2] f2fs: multi-device expandable storage support 李珂11
2026-08-28 12:18 ` [PATCH 1/2] f2fs: add alloc_primary mount option 李珂11
2026-08-28 12:18 ` [PATCH 2/2] f2fs: allow f2fs_resize_fs to drop the last secondary device 李珂11

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox