[RFC PATCH 5/6] fs: fat: move fstrim to file_operation

From: Enrico Weigelt, metux IT consult
Date: Tue Jun 22 2021 - 08:12:00 EST


Use the newly introduced file_operation callback for FITRIM ioctl.
This removes some common code, eg. permission check, buffer copying,
which is now done by generic vfs code.
---
fs/fat/file.c | 25 ++++---------------------
1 file changed, 4 insertions(+), 21 deletions(-)

diff --git a/fs/fat/file.c b/fs/fat/file.c
index 13855ba49cd9..5f57f06341d0 100644
--- a/fs/fat/file.c
+++ b/fs/fat/file.c
@@ -122,35 +122,20 @@ static int fat_ioctl_get_volume_id(struct inode *inode, u32 __user *user_attr)
return put_user(sbi->vol_id, user_attr);
}

-static int fat_ioctl_fitrim(struct inode *inode, unsigned long arg)
+static int fat_ioctl_fitrim(struct file *file, struct fstrim_range *range)
{
+ struct inode *inode = file_inode(file);
struct super_block *sb = inode->i_sb;
- struct fstrim_range __user *user_range;
- struct fstrim_range range;
struct request_queue *q = bdev_get_queue(sb->s_bdev);
int err;

- if (!capable(CAP_SYS_ADMIN))
- return -EPERM;
-
if (!blk_queue_discard(q))
return -EOPNOTSUPP;

- user_range = (struct fstrim_range __user *)arg;
- if (copy_from_user(&range, user_range, sizeof(range)))
- return -EFAULT;
-
- range.minlen = max_t(unsigned int, range.minlen,
+ range->minlen = max_t(unsigned int, range->minlen,
q->limits.discard_granularity);

- err = fat_trim_fs(inode, &range);
- if (err < 0)
- return err;
-
- if (copy_to_user(user_range, &range, sizeof(range)))
- return -EFAULT;
-
- return 0;
+ return fat_trim_fs(inode, range);
}

long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
@@ -165,8 +150,6 @@ long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
return fat_ioctl_set_attributes(filp, user_attr);
case FAT_IOCTL_GET_VOLUME_ID:
return fat_ioctl_get_volume_id(inode, user_attr);
- case FITRIM:
- return fat_ioctl_fitrim(inode, arg);
default:
return -ENOTTY; /* Inappropriate ioctl for device */
}
--
2.20.1