Skip to contentSkip to conversation

f2fs: add F2FS_IOC_SEC_TRIM_FILE ioctl

PATCH Superseded Daeho Jeong 2 participants 2 messages Jun 18, 2020 – Jul 8, 2020 F2FS file system

v4 of 7 Superseded Latest is v7 →

lore
Share & tools
lore
#0 lore raw

PATCH v4 1/1 f2fs: add F2FS_IOC_SEC_TRIM_FILE ioctl

From: Daeho Jeong <[email protected]>

Added a new ioctl to send discard commands or/and zero out to selected data area of a regular file for security reason.

---
 fs/f2fs/f2fs.h |  15 +++++
 fs/f2fs/file.c | 179 +++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 194 insertions(+)
fs/f2fs/f2fs.h +15 -0
index c812fb8e2d9c..3763d9e03ca8 100644
@@ -434,6 +434,8 @@ static inline bool __has_cursum_space(struct f2fs_journal *journal,
 					_IOR(F2FS_IOCTL_MAGIC, 18, __u64)
 #define F2FS_IOC_RESERVE_COMPRESS_BLOCKS				\
 					_IOR(F2FS_IOCTL_MAGIC, 19, __u64)
+#define F2FS_IOC_SEC_TRIM_FILE		_IOW(F2FS_IOCTL_MAGIC, 20,	\
+						struct f2fs_sectrim_range)
 
 #define F2FS_IOC_GET_VOLUME_NAME	FS_IOC_GETFSLABEL
 #define F2FS_IOC_SET_VOLUME_NAME	FS_IOC_SETFSLABEL
@@ -453,6 +455,13 @@ static inline bool __has_cursum_space(struct f2fs_journal *journal,
 #define F2FS_GOING_DOWN_METAFLUSH	0x3	/* going down with meta flush */
 #define F2FS_GOING_DOWN_NEED_FSCK	0x4	/* going down to trigger fsck */
 
+/*
+ * Flags used by F2FS_IOC_SEC_TRIM_FILE
+ */
+#define F2FS_TRIM_FILE_DISCARD		0x1	/* send discard command */
+#define F2FS_TRIM_FILE_ZEROOUT		0x2	/* zero out */
+#define F2FS_TRIM_FILE_MASK		0x3
+
 #if defined(__KERNEL__) && defined(CONFIG_COMPAT)
 /*
  * ioctl commands in 32 bit emulation
@@ -488,6 +497,12 @@ struct f2fs_flush_device {
 	u32 segments;		/* # of segments to flush */
 };
 
+struct f2fs_sectrim_range {
+	u64 start;
+	u64 len;
+	u64 flags;
+};
+
 /* for inline stuff */
 #define DEF_INLINE_RESERVED_SIZE	1
 static inline int get_extra_isize(struct inode *inode);
fs/f2fs/file.c +179 -0

203 lines not on this page · raw message

Signature · 1 line
2.27.0.290.gba653c62da-goog
#1 lore raw

Hi Daeho,

Quoted 34 lines · Daeho Jeong
fs/f2fs/file.c +34 -0
+static int f2fs_sec_trim_file(struct file *filp, unsigned long arg)
+{
+	struct inode *inode = file_inode(filp);
+	struct f2fs_sb_info *sbi = F2FS_I_SB(inode);
+	struct address_space *mapping = inode->i_mapping;
+	struct block_device *prev_bdev = NULL;
+	struct f2fs_sectrim_range range;
+	pgoff_t index, pg_end;
+	block_t prev_block = 0, len = 0;
+	u64 end_addr;
+	bool to_end;
+	int ret = 0;
+
+	if (!(filp->f_mode & FMODE_WRITE))
+		return -EBADF;
+
+	if (copy_from_user(&range, (struct f2fs_sectrim_range __user *)arg,
+				sizeof(range)))
+		return -EFAULT;
+
+	if (range.flags == 0 || (range.flags & ~F2FS_TRIM_FILE_MASK) ||
+			!S_ISREG(inode->i_mode))
+		return -EINVAL;
+
+	if ((range.flags & F2FS_TRIM_FILE_DISCARD) &&
+			!f2fs_hw_support_discard(sbi))
+		return -EOPNOTSUPP;
+
+	file_start_write(filp);
+	inode_lock(inode);
+
+	if (f2fs_is_atomic_file(inode) || f2fs_compressed_file(inode)) {
+		ret = -EINVAL;
+		goto err;

fs/f2fs/file.c static int f2fs_reserve_compress_blocks(struct file *filp, unsigned long arg) · line 3818

Quoted 10 lines
fs/f2fs/file.c +10 -0
+	}
+
+	if (inode->i_size == 0)
+		goto err;
+
+	end_addr = range.start + range.len;
+	if (end_addr > inode->i_size) {
+		ret = -EINVAL;
+		goto err;
+	}

This is missing a check that range.start + range.len doesn't overflow.

- Eric