Adds an filesystem independent ioctl to allow implementation of file system batched discard support. It takes an array of three uint64_t as an argument, the meaning of each item is as follows: array[0] - (start) first Byte to trim array[1] - (len) number of Bytes to trim from start array[2] - (minlen) minimum extent length to trim, free extents shorter than this number of Bytes will be ignored. This number will be rounded up to the block size. It is also possible to specify NULL as an argument. In this case the arguments will set itself as follows: args[0] = 0; args[1] = ULLONG_MAX; args[2] = 0; So it will trim the whole file system at one run. After the FITRIM is done, the number of actually discarded Bytes is stored in args[1] (len) to give the user better insight on how much storage space was really released for wear-leveling. Signed-off-by: Lukas Czerner <lczerner@xxxxxxxxxx> Reviewed-by: Dmitry Monakhov <dmonakhov@xxxxxxxxxx> --- fs/ioctl.c | 39 +++++++++++++++++++++++++++++++++++++++ include/linux/fs.h | 2 ++ 2 files changed, 41 insertions(+), 0 deletions(-) diff --git a/fs/ioctl.c b/fs/ioctl.c index f855ea4..3dd96b6 100644 --- a/fs/ioctl.c +++ b/fs/ioctl.c @@ -530,6 +530,41 @@ static int ioctl_fsthaw(struct file *filp) return thaw_super(sb); } +static int ioctl_fstrim(struct file *filp, void __user *argp) +{ + struct super_block *sb = filp->f_path.dentry->d_inode->i_sb; + uint64_t args[3]; + int ret = 0; + + if (!capable(CAP_SYS_ADMIN)) + return -EPERM; + + /* If filesystem doesn't support trim feature, return. */ + if (sb->s_op->trim_fs == NULL) + return -EOPNOTSUPP; + + /* If a blockdevice-backed filesystem isn't specified, return EINVAL. */ + if (sb->s_bdev == NULL) + return -EINVAL; + + if (argp == NULL) { + args[0] = 0; + args[1] = ULLONG_MAX; + args[2] = 0; + } else if (copy_from_user(args, argp, sizeof(args))) + return -EFAULT; + + ret = sb->s_op->trim_fs(sb, args); + if (ret < 0) + return ret; + + if ((argp != NULL) && + (copy_to_user(argp, args, sizeof(args)))) + return -EFAULT; + + return 0; +} + /* * When you add any new common ioctls to the switches above and below * please update compat_sys_ioctl() too. @@ -580,6 +615,10 @@ int do_vfs_ioctl(struct file *filp, unsigned int fd, unsigned int cmd, error = ioctl_fsthaw(filp); break; + case FITRIM: + error = ioctl_fstrim(filp, argp); + break; + case FS_IOC_FIEMAP: return ioctl_fiemap(filp, arg); diff --git a/include/linux/fs.h b/include/linux/fs.h index 76041b6..e1b8a4a 100644 --- a/include/linux/fs.h +++ b/include/linux/fs.h @@ -316,6 +316,7 @@ struct inodes_stat_t { #define FIGETBSZ _IO(0x00,2) /* get the block size used for bmap */ #define FIFREEZE _IOWR('X', 119, int) /* Freeze */ #define FITHAW _IOWR('X', 120, int) /* Thaw */ +#define FITRIM _IOWR('X', 121, uint64_t) /* Trim */ #define FS_IOC_GETFLAGS _IOR('f', 1, long) #define FS_IOC_SETFLAGS _IOW('f', 2, long) @@ -1577,6 +1578,7 @@ struct super_operations { ssize_t (*quota_write)(struct super_block *, int, const char *, size_t, loff_t); #endif int (*bdev_try_to_free_page)(struct super_block*, struct page*, gfp_t); + int (*trim_fs) (struct super_block *, uint64_t *); }; /* -- 1.7.2.3 -- To unsubscribe from this list: send the line "unsubscribe linux-ext4" in the body of a message to majordomo@xxxxxxxxxxxxxxx More majordomo info at http://vger.kernel.org/majordomo-info.html