Linux Documentation
 help / color / mirror / Atom feed
From: 李珂11 <ke.li11@transsion.com>
To: "linux-f2fs-devel@lists.sourceforge.net"
	<linux-f2fs-devel@lists.sourceforge.net>
Cc: "jaegeuk@kernel.org" <jaegeuk@kernel.org>,
	"chao@kernel.org" <chao@kernel.org>,
	"linux-doc@vger.kernel.org" <linux-doc@vger.kernel.org>,
	李珂11 <ke.li11@transsion.com>
Subject: [PATCH 2/2] f2fs: allow f2fs_resize_fs to drop the last secondary device
Date: Fri, 28 Aug 2026 12:18:44 +0000	[thread overview]
Message-ID: <20260828121816.1463654-3-ke.li11@transsion.com> (raw)
In-Reply-To: <20260828121816.1463654-1-ke.li11@transsion.com>

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


      parent reply	other threads:[~2026-08-28 12:19 UTC|newest]

Thread overview: 3+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20260828121816.1463654-3-ke.li11@transsion.com \
    --to=ke.li11@transsion.com \
    --cc=chao@kernel.org \
    --cc=jaegeuk@kernel.org \
    --cc=linux-doc@vger.kernel.org \
    --cc=linux-f2fs-devel@lists.sourceforge.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox