Skip to content

Commit

Permalink
fs: ext4: move fstrim to file_operation
Browse files Browse the repository at this point in the history
  • Loading branch information
metux authored and intel-lab-lkp committed Jun 22, 2021
1 parent bee5a73 commit 2f26b68
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 36 deletions.
1 change: 1 addition & 0 deletions fs/ext4/ext4.h
Expand Up @@ -1272,6 +1272,7 @@ struct ext4_inode_info {
#define ext4_find_next_bit find_next_bit_le

extern void ext4_set_bits(void *bm, int cur, int len);
extern long ext4_fitrim(struct file *filp, struct fstrim_range *range);

/*
* Maximal mount counts between two filesystem checks
Expand Down
1 change: 1 addition & 0 deletions fs/ext4/file.c
Expand Up @@ -927,6 +927,7 @@ const struct file_operations ext4_file_operations = {
.splice_read = generic_file_splice_read,
.splice_write = iter_file_splice_write,
.fallocate = ext4_fallocate,
.fitrim = ext4_fitrim,
};

const struct inode_operations ext4_file_inode_operations = {
Expand Down
60 changes: 24 additions & 36 deletions fs/ext4/ioctl.c
Expand Up @@ -800,6 +800,30 @@ static int ext4_ioctl_get_es_cache(struct file *filp, unsigned long arg)
return error;
}

long ext4_fitrim(struct file *filp, struct fstrim_range *range)
{
struct request_queue *q = bdev_get_queue(sb->s_bdev);
int ret = 0;

if (!blk_queue_discard(q))
return -EOPNOTSUPP;

/*
* We haven't replayed the journal, so we cannot use our
* block-bitmap-guided storage zapping commands.
*/
if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb))
return -EROFS;

range->minlen = max((unsigned int)range->minlen,
q->limits.discard_granularity);
ret = ext4_trim_fs(sb, range);
if (ret < 0)
return ret;

return 0;
}

static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct inode *inode = file_inode(filp);
Expand Down Expand Up @@ -1044,41 +1068,6 @@ static long __ext4_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
return err;
}

case FITRIM:
{
struct request_queue *q = bdev_get_queue(sb->s_bdev);
struct fstrim_range range;
int ret = 0;

if (!capable(CAP_SYS_ADMIN))
return -EPERM;

if (!blk_queue_discard(q))
return -EOPNOTSUPP;

/*
* We haven't replayed the journal, so we cannot use our
* block-bitmap-guided storage zapping commands.
*/
if (test_opt(sb, NOLOAD) && ext4_has_feature_journal(sb))
return -EROFS;

if (copy_from_user(&range, (struct fstrim_range __user *)arg,
sizeof(range)))
return -EFAULT;

range.minlen = max((unsigned int)range.minlen,
q->limits.discard_granularity);
ret = ext4_trim_fs(sb, &range);
if (ret < 0)
return ret;

if (copy_to_user((struct fstrim_range __user *)arg, &range,
sizeof(range)))
return -EFAULT;

return 0;
}
case EXT4_IOC_PRECACHE_EXTENTS:
return ext4_ext_precache(inode);

Expand Down Expand Up @@ -1272,7 +1261,6 @@ long ext4_compat_ioctl(struct file *file, unsigned int cmd, unsigned long arg)
}
case EXT4_IOC_MOVE_EXT:
case EXT4_IOC_RESIZE_FS:
case FITRIM:
case EXT4_IOC_PRECACHE_EXTENTS:
case FS_IOC_SET_ENCRYPTION_POLICY:
case FS_IOC_GET_ENCRYPTION_PWSALT:
Expand Down

0 comments on commit 2f26b68

Please sign in to comment.