Skip to contentSkip to conversation

f2fs: support dynamic include/exclude for device aliasing

PATCH Superseded Daeho Jeong 2 participants 5 messages Jun 5 – Jun 6

Subsystems (2)

v2 of 8 Superseded Latest is v8 →

lore
Share & tools
lore
#0 lore raw

PATCH v2 1/1 f2fs: support dynamic include/exclude for device aliasing

From: Daeho Jeong <[email protected]>

This patch adds a dynamic management feature to the existing device aliasing functionality. It allows users to dynamically exclude or include specific devices from the filesystem's free pool at runtime through new ioctls.

To support this, three new ioctls are introduced:
- F2FS_IOC_EXCLUDE_DEV_ALIAS: This reclaims the space occupied by a device aliasing file. It first performs a capacity check, resets GC victim information for the target range, marks the segments as in-use to prevent new allocations, and then triggers GC to migrate existing valid data out of the range. Finally, it reserves these blocks in the SIT to effectively exclude the device from the usable capacity.

- F2FS_IOC_INCLUDE_DEV_ALIAS: This releases the reserved space of a previously excluded device aliasing file. It truncates the blocks associated with the file, which makes them available for general filesystem allocation again.

- F2FS_IOC_GET_DEV_ALIAS_STATUS: This retrieves the current aliasing status of a device aliasing file, returning whether the file is included (active alias) or excluded (inactive alias, with blocks fully allocated on the device).

---
v2: prevent operations during checkpoint=disabled.

---
 Documentation/filesystems/f2fs.rst |  35 ++++
 fs/f2fs/f2fs.h                     |   9 +-
 fs/f2fs/file.c                     | 270 ++++++++++++++++++++++++++++-
 fs/f2fs/gc.c                       |  30 ++--
 fs/f2fs/namei.c                    |  11 ++
 fs/f2fs/segment.c                  | 178 +++++++++++++------
 fs/f2fs/segment.h                  |  11 ++
 fs/f2fs/super.c                    |  34 ++++
 include/uapi/linux/f2fs.h          |   7 +
 9 files changed, 517 insertions(+), 68 deletions(-)
Documentation/filesystems/f2fs.rst +35 -0
index 7e4031631286..d154c8ac0cd7 100644
@@ -1036,6 +1036,41 @@ So, the key idea is, user can do any file operations on /dev/vdc, and
 reclaim the space after the use, while the space is counted as /data.
 That doesn't require modifying partition size and filesystem format.
 
+Dynamic Device Aliasing Management
+----------------------------------
+
+In addition to static device aliasing by deleting the aliasing file, F2FS
+supports dynamic management of device aliasing. This mechanism allows the system
+to dynamically transition partition ownership between F2FS userdata and external
+entities (e.g., zRAM, raw partition) based on system requirements without
+deleting the master aliasing file or requiring unmount/remount.
+
+The master aliasing file is created during the initial format of the file system
+and remains as a persistent control entity (ioctl gateway) in the root directory.
+
+- Partition Exclusion (In-service to Aliased)
+  When a specific partition needs to be dedicated to external services (e.g., zRAM),
+  a user can exclude the device alias range via ioctl. The kernel resets GC victim
+  information for the target range, marks segments as in-use to prevent new
+  allocations, and triggers forced GC to migrate existing valid data out of the
+  range. Finally, it reserves these blocks in the SIT to effectively exclude the
+  device from the usable capacity.
+
+- Partition Inclusion (Aliased to In-service)
+  When external usage concludes, the space is reclaimed not by deleting the file,
+  but through the inclusion ioctl. The kernel truncates blocks associated with
+  the file, releasing them back to general filesystem allocation.
+
+.. code-block::
+
+   # f2fs_io dev_alias include /mnt/f2fs/vdc.file
+   # df -h
+   /dev/vdb                            64G  753M   64G   2% /mnt/f2fs
+
+   # f2fs_io dev_alias exclude /mnt/f2fs/vdc.file
+   # df -h
+   /dev/vdb                            64G   33G   32G  52% /mnt/f2fs
+
 Per-file Read-Only Large Folio Support
 --------------------------------------
 
fs/f2fs/f2fs.h +8 -1
index 29f81a496b72..5e0c5701c088 100644
@@ -1398,6 +1398,8 @@ struct f2fs_dev_info {
 	unsigned int total_segments;
 	block_t start_blk;
 	block_t end_blk;
+	bool has_alias;
+	bool is_excluding;
 #ifdef CONFIG_BLK_DEV_ZONED
 	unsigned int nr_blkz;		/* Total number of zones */
 	unsigned long *blkz_seq;	/* Bitmap indicating sequential zones */
@@ -3970,7 +3972,10 @@ int f2fs_create_flush_cmd_control(struct f2fs_sb_info *sbi);
 int f2fs_flush_device_cache(struct f2fs_sb_info *sbi);
 void f2fs_destroy_flush_cmd_control(struct f2fs_sb_info *sbi, bool free);
 void f2fs_invalidate_blocks(struct f2fs_sb_info *sbi, block_t addr,
-						unsigned int len);
+				unsigned int len);
+void f2fs_reserve_device_alias(struct f2fs_sb_info *sbi, block_t addr,
+				unsigned int len);
+
 bool f2fs_is_checkpointed_data(struct f2fs_sb_info *sbi, block_t blkaddr);
 int f2fs_start_discard_thread(struct f2fs_sb_info *sbi);
 void f2fs_drop_discard_cmd(struct f2fs_sb_info *sbi);
@@ -4189,6 +4194,8 @@ void f2fs_build_gc_manager(struct f2fs_sb_info *sbi);
 int f2fs_gc_range(struct f2fs_sb_info *sbi,
 		unsigned int start_seg, unsigned int end_seg,
 		bool dry_run, unsigned int dry_run_sections);
+void f2fs_reset_gc_victim_resource(struct f2fs_sb_info *sbi,
+		unsigned int start, unsigned int end);
 int f2fs_resize_fs(struct file *filp, __u64 block_count);
 int __init f2fs_create_garbage_collection_cache(void);
 void f2fs_destroy_garbage_collection_cache(void);
fs/f2fs/file.c +266 -4

333 lines not on this page · raw message

fs/f2fs/gc.c +19 -11
index 60378614bc54..755df9b6bbaa 100644
@@ -2143,29 +2143,37 @@ int f2fs_gc_range(struct f2fs_sb_info *sbi,
 	return 0;
 }
 
+void f2fs_reset_gc_victim_resource(struct f2fs_sb_info *sbi,
+			unsigned int start, unsigned int end)
+{
+	int i;
+
+	mutex_lock(&DIRTY_I(sbi)->seglist_lock);
+	for (i = 0; i < MAX_GC_POLICY; i++)
+		if (SIT_I(sbi)->last_victim[i] >= start &&
+			SIT_I(sbi)->last_victim[i] <= end)
+			SIT_I(sbi)->last_victim[i] = 0;
+
+	for (i = BG_GC; i <= FG_GC; i++)
+		if (sbi->next_victim_seg[i] >= start &&
+			sbi->next_victim_seg[i] <= end)
+			sbi->next_victim_seg[i] = NULL_SEGNO;
+	mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
+}
+
 static int free_segment_range(struct f2fs_sb_info *sbi,
 				unsigned int secs, bool dry_run)
 {
 	unsigned int next_inuse, start, end;
 	struct cp_control cpc = { CP_RESIZE, 0, 0, 0 };
-	int gc_mode, gc_type;
 	int err = 0;
 	int type;
 
-	/* Force block allocation for GC */
 	MAIN_SECS(sbi) -= secs;
 	start = MAIN_SECS(sbi) * SEGS_PER_SEC(sbi);
 	end = MAIN_SEGS(sbi) - 1;
 
-	mutex_lock(&DIRTY_I(sbi)->seglist_lock);
-	for (gc_mode = 0; gc_mode < MAX_GC_POLICY; gc_mode++)
-		if (SIT_I(sbi)->last_victim[gc_mode] >= start)
-			SIT_I(sbi)->last_victim[gc_mode] = 0;
-
-	for (gc_type = BG_GC; gc_type <= FG_GC; gc_type++)
-		if (sbi->next_victim_seg[gc_type] >= start)
-			sbi->next_victim_seg[gc_type] = NULL_SEGNO;
-	mutex_unlock(&DIRTY_I(sbi)->seglist_lock);
+	f2fs_reset_gc_victim_resource(sbi, start, end);
 
 	/* Move out cursegs from the target range */
 	for (type = CURSEG_HOT_DATA; type < NR_CURSEG_PERSIST_TYPE; type++) {
fs/f2fs/namei.c +11 -0
index e360f08a9586..b7974242ead1 100644
@@ -553,6 +553,9 @@ static int f2fs_unlink(struct inode *dir, struct dentry *dentry)
 
 	trace_f2fs_unlink_enter(dir, dentry);
 
+	if (IS_DEVICE_ALIASING(inode))
+		return -EPERM;
+
 	if (unlikely(f2fs_cp_error(sbi))) {
 		err = -EIO;
 		goto out;
@@ -931,6 +934,9 @@ static int f2fs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
 	bool old_is_dir = S_ISDIR(old_inode->i_mode);
 	int err;
 
+	if (IS_DEVICE_ALIASING(old_inode))
+		return -EPERM;
+
 	if (unlikely(f2fs_cp_error(sbi)))
 		return -EIO;
 	if (!f2fs_is_checkpoint_ready(sbi))
@@ -1000,6 +1006,8 @@ static int f2fs_rename(struct mnt_idmap *idmap, struct inode *old_dir,
 	}
 
 	if (new_inode) {
+		if (IS_DEVICE_ALIASING(new_inode))
+			return -EPERM;
 
 		err = -ENOTEMPTY;
 		if (old_is_dir && !f2fs_empty_dir(new_inode))
@@ -1127,6 +1135,9 @@ static int f2fs_cross_rename(struct inode *old_dir, struct dentry *old_dentry,
 	int old_nlink = 0, new_nlink = 0;
 	int err;
 
+	if (IS_DEVICE_ALIASING(old_inode) || IS_DEVICE_ALIASING(new_inode))
+		return -EPERM;
+
 	if (unlikely(f2fs_cp_error(sbi)))
 		return -EIO;
 	if (!f2fs_is_checkpoint_ready(sbi))
fs/f2fs/segment.c +126 -52

224 lines not on this page · raw message

fs/f2fs/segment.h +11 -0
index 068845660b0f..914523f5d3ea 100644
@@ -980,6 +980,17 @@ static inline bool sec_usage_check(struct f2fs_sb_info *sbi, unsigned int secno)
 {
 	if (is_cursec(sbi, secno) || (sbi->cur_victim_sec == secno))
 		return true;
+	if (f2fs_sb_has_device_alias(sbi) && f2fs_is_multi_device(sbi)) {
+		int i;
+		block_t start_blk = START_BLOCK(sbi, GET_SEG_FROM_SEC(sbi, secno));
+
+		for (i = 0; i < sbi->s_ndevs; i++) {
+			if (FDEV(i).is_excluding &&
+				start_blk >= FDEV(i).start_blk &&
+				start_blk <= FDEV(i).end_blk)
+				return true;
+		}
+	}
 	return false;
 }
 
fs/f2fs/super.c +34 -0
index 9d421a07d2d5..ee599d202fc9 100644
@@ -4916,6 +4916,38 @@ static void f2fs_tuning_parameters(struct f2fs_sb_info *sbi)
 	sbi->readdir_ra = true;
 }
 
+static void f2fs_restore_device_alias(struct f2fs_sb_info *sbi)
+{
+	struct inode *root = d_inode(sbi->sb->s_root);
+	struct f2fs_dir_entry *de;
+	struct folio *folio;
+	int i;
+
+	if (!f2fs_sb_has_device_alias(sbi))
+		return;
+
+	for (i = 1; i < sbi->s_ndevs; i++) {
+		char *name = strrchr(FDEV(i).path, '/');
+		struct qstr qstr;
+
+		name = name ? name + 1 : FDEV(i).path;
+		qstr.name = name;
+		qstr.len = strlen(name);
+
+		de = f2fs_find_entry(root, &qstr, &folio);
+		if (de) {
+			struct inode *inode = f2fs_iget(sbi->sb, le32_to_cpu(de->ino));
+
+			if (!IS_ERR(inode)) {
+				if (IS_DEVICE_ALIASING(inode))
+					FDEV(i).has_alias = true;
+				iput(inode);
+			}
+			f2fs_folio_put(folio, 0);
+		}
+	}
+}
+
 static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 {
 	struct f2fs_fs_context *ctx = fc->fs_private;
@@ -5341,6 +5373,8 @@ static int f2fs_fill_super(struct super_block *sb, struct fs_context *fc)
 	f2fs_update_time(sbi, REQ_TIME);
 	clear_sbi_flag(sbi, SBI_CP_DISABLED_QUICK);
 
+	f2fs_restore_device_alias(sbi);
+
 	sbi->umount_lock_holder = NULL;
 	return 0;
 
include/uapi/linux/f2fs.h +7 -0
index 795e26258355..6ca6ae06918e 100644
@@ -45,6 +45,9 @@
 #define F2FS_IOC_START_ATOMIC_REPLACE	_IO(F2FS_IOCTL_MAGIC, 25)
 #define F2FS_IOC_GET_DEV_ALIAS_FILE	_IOR(F2FS_IOCTL_MAGIC, 26, __u32)
 #define F2FS_IOC_IO_PRIO		_IOW(F2FS_IOCTL_MAGIC, 27, __u32)
+#define F2FS_IOC_EXCLUDE_DEV_ALIAS	_IO(F2FS_IOCTL_MAGIC, 28)
+#define F2FS_IOC_INCLUDE_DEV_ALIAS	_IO(F2FS_IOCTL_MAGIC, 29)
+#define F2FS_IOC_GET_DEV_ALIAS_STATUS	_IOR(F2FS_IOCTL_MAGIC, 30, __u32)
 
 /*
  * should be same as XFS_IOC_GOINGDOWN.
@@ -70,6 +73,10 @@ enum {
 	F2FS_IOPRIO_MAX,
 };
 
+/* for F2FS_IOC_GET_DEV_ALIAS_STATUS */
+#define F2FS_DEV_ALIAS_STATUS_INCLUDED	0
+#define F2FS_DEV_ALIAS_STATUS_EXCLUDED	1
+
 struct f2fs_gc_range {
 	__u32 sync;
 	__u64 start;
Signature · 1 line
2.54.0.1032.g2f8565e1d1-goog
#1 lore raw
Quoted 6 lines · Daeho Jeong

- F2FS_IOC_EXCLUDE_DEV_ALIAS: This reclaims the space occupied by a device aliasing file. It first performs a capacity check, resets GC victim information for the target range, marks the segments as in-use to prevent new allocations, and then triggers GC to migrate existing valid data out of the range. Finally, it reserves these blocks in the SIT to effectively exclude the device from the usable capacity.

Does this do anything to zeroize the device so that random data isn't leaked in it, or is that the responsibility of the userspace code that reallocates the device to something?

fs/f2fs/file.c static const struct { · line 2235

Quoted 7 lines
fs/f2fs/file.c +2 -1
@@ -2214,7 +2231,8 @@ static const struct {
 		FS_INLINE_DATA_FL |	\
 		FS_NOCOW_FL |		\
 		FS_VERITY_FL |		\
-		FS_CASEFOLD_FL)
+		FS_CASEFOLD_FL |	\
+		F2FS_DEVICE_ALIAS_FL)

This is extending FS_IOC_GETFLAGS, which you probably don't really want to be doing, right?

- Eric

#2 lore raw
Quoted 9 lines · Eric Biggers
Quoted 6 lines · Daeho Jeong

- F2FS_IOC_EXCLUDE_DEV_ALIAS: This reclaims the space occupied by a device aliasing file. It first performs a capacity check, resets GC victim information for the target range, marks the segments as in-use to prevent new allocations, and then triggers GC to migrate existing valid data out of the range. Finally, it reserves these blocks in the SIT to effectively exclude the device from the usable capacity.

Quoted 3 lines

Does this do anything to zeroize the device so that random data isn't leaked in it, or is that the responsibility of the userspace code that reallocates the device to something?

By default, this ioctl does not zeroize or discard the underlying physical device upon exclusion.

Currently, ensuring that stale random data isn't leaked is the responsibility of the userspace claiming the block device. We deliberately decoupled bulk zeroing/discard from the kernel ioctl because:
- Synchronously zeroing out or issuing massive blkdiscard requests across multi-gigabyte partitions inside the ioctl would introduce prohibitive I/O latency spikes and unnecessary flash wear (P/E cycles).
- Access Control: The F2FS_IOC_EXCLUDE_DEV_ALIAS ioctl strictly requires CAP_SYS_ADMIN. Unprivileged users cannot trigger this transition.
- Raw Device Protection: Accessing the excluded raw block device directly requires root/block-group privileges and matching SELinux permissions.
- fscrypt Encryption: In typical production environments (such as Android), file contents are encrypted via fscrypt. Stale disk remnants remain unreadable ciphertext, preventing meaningful data leakage.

However, if you believe a kernel-enforced sanitization mechanism is necessary to prevent it, we would be glad to support an optional secure zeroing/discard flag in the exclude ioctl.

Quoted 11 lines

fs/f2fs/file.c static const struct { · line 2235

Quoted 8 lines
fs/f2fs/file.c +2 -1
@@ -2214,7 +2231,8 @@ static const struct {
              FS_INLINE_DATA_FL |     \
              FS_NOCOW_FL |           \
              FS_VERITY_FL |          \
-             FS_CASEFOLD_FL)
+             FS_CASEFOLD_FL |        \
+             F2FS_DEVICE_ALIAS_FL)

This is extending FS_IOC_GETFLAGS, which you probably don't really want to be doing, right?

It seems my test code was not properly cleaned up before submission. I'm sorry about the confusion.

Quoted 2 lines

- Eric

#3 lore raw
Quoted 3 lines · Daeho Jeong

- Access Control: The F2FS_IOC_EXCLUDE_DEV_ALIAS ioctl strictly requires CAP_SYS_ADMIN. Unprivileged users cannot trigger this transition.

No, it doesn't.

How much of this been reviewed by a human?

- Eric

#4 lore raw
Quoted 11 lines · Eric Biggers
Quoted 3 lines · Daeho Jeong

- Access Control: The F2FS_IOC_EXCLUDE_DEV_ALIAS ioctl strictly requires CAP_SYS_ADMIN. Unprivileged users cannot trigger this transition.

No, it doesn't.

How much of this been reviewed by a human?

- Eric

Let me check that part again.