Skip to content

Add new FITRIM ioctl for fstrim to trim FAT file system #540

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions fs/fat/fat.h
Original file line number Diff line number Diff line change
Expand Up @@ -357,6 +357,7 @@ extern int fat_alloc_clusters(struct inode *inode, int *cluster,
int nr_cluster);
extern int fat_free_clusters(struct inode *inode, int cluster);
extern int fat_count_free_clusters(struct super_block *sb);
extern int fat_trim_fs(struct inode *inode, struct fstrim_range *range);

/* fat/file.c */
extern long fat_generic_ioctl(struct file *filp, unsigned int cmd,
Expand Down
61 changes: 61 additions & 0 deletions fs/fat/fatent.c
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,67 @@ static void fat_collect_bhs(struct buffer_head **bhs, int *nr_bhs,
}
}

int fat_trim_fs(struct inode *inode, struct fstrim_range *range)
{
struct super_block *sb = inode->i_sb;
struct msdos_sb_info *sbi = MSDOS_SB(sb);
struct fat_entry fatent;
int next;
u64 start_blk, len_blk, minlen_blk;
u64 ent_start, ent_end, ent;
u32 count = 0, trimmed = 0, minlen;

/*
* FAT data is organized as clusters, trim at the granulary of cluster.
*
* fstrim_range is in byte, convert vaules to cluster index.
* Treat sectors before data region as all used, not to trim them.
*/
start_blk = range->start >> sb->s_blocksize_bits;
len_blk = range->len >> sb->s_blocksize_bits;
minlen_blk = range->minlen >> sb->s_blocksize_bits;

ent_start = start_blk >> ffs(sbi->sec_per_clus);
ent_start = ent_start < FAT_START_ENT? FAT_START_ENT: ent_start;

ent_end = (ent_start + len_blk) >> ffs(sbi->sec_per_clus);
if (ent_end >= sbi->max_cluster)
return -EINVAL;

minlen = minlen_blk >> ffs(sbi->sec_per_clus);
if ( ent_start + minlen > ent_end)
return -EINVAL;

fatent_init(&fatent);
lock_fat(sbi);
ent = ent_start;
while (ent < ent_end) {
next = fat_ent_read(inode, &fatent, ent);

if (next == FAT_ENT_FREE) {
count++;
} else {
if (count != 0 && count >= minlen) {
sb_issue_discard(sb,
fat_clus_to_blknr(sbi, ent - count),
count * sbi->sec_per_clus,
GFP_NOFS, 0);

trimmed += count;
}
count = 0;
}

ent++;
}
fatent_brelse(&fatent);
unlock_fat(sbi);

range->len = (trimmed * sbi->sec_per_clus) << sb->s_blocksize_bits;

return 0;
}

int fat_alloc_clusters(struct inode *inode, int *cluster, int nr_cluster)
{
struct super_block *sb = inode->i_sb;
Expand Down
31 changes: 31 additions & 0 deletions fs/fat/file.c
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,35 @@ 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, u32 __user *arg)
{
struct super_block *sb = inode->i_sb;
struct fstrim_range range;
struct request_queue *q = bdev_get_queue(sb->s_bdev);
int ret = 0;

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

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

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

range.minlen = max_t(unsigned int, range.minlen,
q->limits.discard_granularity);

ret = fat_trim_fs(inode, &range);
if (ret < 0)
return ret;

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

return 0;
}

long fat_generic_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
{
struct inode *inode = file_inode(filp);
Expand All @@ -133,6 +162,8 @@ 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, user_attr);
default:
return -ENOTTY; /* Inappropriate ioctl for device */
}
Expand Down