Add FITRIM ioctl support for exFAT filesystem
Subsystems (2)
v4 cover Add FITRIM ioctl support for exFAT filesystem
This is for adding FITRIM ioctl functionality to exFAT filesystem. Firstly, because the fstrim is long operation, introduce bitmap_lock to narrow the lock range to prevent read operation stall. After that, add generic ioctl function and FITRIM handler.
Changelog
=========v3->v4:
- Introduce bitmap_lock mutex to narrow the lock range for bitmap access and change to use bitmap_lock instead of s_lock in FITRIM handler to prevent read stall while ongoing fstrim.
- Minor code style fix
v2->v3:
- Remove unnecessary local variable
- Merge all changes to a single patch
v1->v2:
- Change variable declaration order as reverse tree style.
- Return -EOPNOTSUPP from sb_issue_discard() just as it is.
- Remove cond_resched() in while loop.
- Move ioctl related code into it's helper function.
Hyeongseok Kim (2):
exfat: introduce bitmap_lock for cluster bitmap access
exfat: add support ioctl and FITRIM function
fs/exfat/balloc.c | 80 +++++++++++++++++++++++++++++++++++++++++++++ fs/exfat/dir.c | 5 +++ fs/exfat/exfat_fs.h | 5 +++ fs/exfat/fatent.c | 37 ++++++++++++++++----- fs/exfat/file.c | 53 ++++++++++++++++++++++++++++++ fs/exfat/super.c | 1 + 6 files changed, 173 insertions(+), 8 deletions(-)
Signature · 1 line
2.27.0.83.g0313f36
PATCH v4 1/2 exfat: introduce bitmap_lock for cluster bitmap access
s_lock which is for protecting concurrent access of file operations is too huge for cluster bitmap protection, so introduce a new bitmap_lock to narrow the lock range if only need to access cluster bitmap.
--- fs/exfat/exfat_fs.h | 1 + fs/exfat/fatent.c | 37 +++++++++++++++++++++++++++++-------- fs/exfat/super.c | 1 + 3 files changed, 31 insertions(+), 8 deletions(-)
fs/exfat/exfat_fs.h +1 -0
@@ -238,6 +238,7 @@ struct exfat_sb_info {unsigned int used_clusters;/* number of used clusters */ struct mutex s_lock;/* superblock lock */ +struct mutex bitmap_lock;/* bitmap lock */ struct exfat_mount_options options;struct nls_table *nls_io;/* Charset used for input and display */ struct ratelimit_state ratelimit;
fs/exfat/fatent.c +29 -8
@@ -151,13 +151,14 @@ int exfat_chain_cont_cluster(struct super_block *sb, unsigned int chain,return 0 ; } -int exfat_free_cluster (struct inode *inode,struct exfat_chain *p_chain) +/* This function must be called with bitmap_lock held */ +static int __exfat_free_cluster (struct inode *inode,struct exfat_chain *p_chain) { -unsigned int num_clusters =0 ; -unsigned int clu;struct super_block *sb = inode->i_sb;struct exfat_sb_info *sbi =EXFAT_SB (sb);int cur_cmap_i, next_cmap_i; +unsigned int num_clusters =0 ; +unsigned int clu;/* invalid cluster number */ if (p_chain->dir == EXFAT_FREE_CLUSTER ||
@@ -230,6 +231,17 @@ int exfat_free_cluster(struct inode *inode, struct exfat_chain *p_chain)return 0 ; } +int exfat_free_cluster (struct inode *inode,struct exfat_chain *p_chain) +{ +int ret =0 ; + +mutex_lock (&EXFAT_SB (inode->i_sb)->bitmap_lock); + ret =__exfat_free_cluster (inode, p_chain); +mutex_unlock (&EXFAT_SB (inode->i_sb)->bitmap_lock); + +return ret; +} +int exfat_find_last_cluster (struct super_block *sb,struct exfat_chain *p_chain,unsigned int *ret_clu) {
@@ -328,6 +340,8 @@ int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc,if (num_alloc > total_cnt - sbi->used_clusters)return -ENOSPC ; +mutex_lock (&sbi->bitmap_lock); + hint_clu = p_chain->dir;/* find new cluster */ if (hint_clu ==EXFAT_EOF_CLUSTER ) {
@@ -338,8 +352,10 @@ int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc, } hint_clu =exfat_find_free_bitmap (sb, sbi->clu_srch_ptr); -if (hint_clu ==EXFAT_EOF_CLUSTER ) -return -ENOSPC ; +if (hint_clu ==EXFAT_EOF_CLUSTER ) { + ret = -ENOSPC ; + goto unlock; + } }/* check cluster validation */
@@ -349,8 +365,10 @@ int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc, hint_clu =EXFAT_FIRST_CLUSTER ;if (p_chain->flags ==ALLOC_NO_FAT_CHAIN ) {if (exfat_chain_cont_cluster (sb, p_chain->dir, - num_clusters)) -return -EIO ; + num_clusters)) { + ret = -EIO ; + goto unlock; + } p_chain->flags =ALLOC_FAT_CHAIN ; } }
@@ -400,6 +418,7 @@ int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc, sbi->used_clusters += num_clusters; p_chain->size += num_clusters; +mutex_unlock (&sbi->bitmap_lock);return 0 ; }
@@ -419,7 +438,9 @@ int exfat_alloc_cluster(struct inode *inode, unsigned int num_alloc, } free_cluster:if (num_clusters) -exfat_free_cluster (inode, p_chain); +__exfat_free_cluster (inode, p_chain); +unlock: +mutex_unlock (&sbi->bitmap_lock);return ret; }
fs/exfat/super.c +1 -0
@@ -752,6 +752,7 @@ static int exfat_init_fs_context(struct fs_context *fc)return -ENOMEM ;mutex_init (&sbi->s_lock); +mutex_init (&sbi->bitmap_lock);ratelimit_state_init (&sbi->ratelimit,DEFAULT_RATELIMIT_INTERVAL ,DEFAULT_RATELIMIT_BURST );
Signature · 1 line
2.27.0.83.g0313f36
RE: [PATCH v4 1/2] exfat: introduce bitmap_lock for cluster bitmap access
Quoted 5 lines
s_lockwhich is for protecting concurrent access of file operations is too huge for cluster bitmap protection, so introduce a newbitmap_lockto narrow the lock range if only need to access cluster bitmap.Signed-off-by Hyeongseok Kim <[email protected]>
Looks good.
Thanks for your work!
PATCH v4 2/2 exfat: add support ioctl and FITRIM function
In reply to Hyeongseok KimAdd FITRIM ioctl to enable discarding unused blocks while mounted. As current exFAT doesn't have generic ioctl handler, add empty ioctl function first, and add FITRIM handler.
--- fs/exfat/balloc.c | 80 +++++++++++++++++++++++++++++++++++++++++++++ fs/exfat/dir.c | 5 +++ fs/exfat/exfat_fs.h | 4 +++ fs/exfat/file.c | 53 ++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+)
fs/exfat/balloc.c +80 -0
@@ -273,3 +273,83 @@ int exfat_count_used_clusters(struct super_block *sb, unsigned int *ret_count) *ret_count = count;return 0 ; } + +int exfat_trim_fs (struct inode *inode,struct fstrim_range *range) +{ +unsigned int trim_begin, trim_end, count, next_free_clu; +u64 clu_start, clu_end, trim_minlen, trimmed_total =0 ; +struct super_block *sb = inode->i_sb; +struct exfat_sb_info *sbi =EXFAT_SB (sb); +int err =0 ; + + clu_start =max_t (u64, range->start >> sbi->cluster_size_bits, +EXFAT_FIRST_CLUSTER ); + clu_end = clu_start + (range->len >> sbi->cluster_size_bits) -1 ; + trim_minlen = range->minlen >> sbi->cluster_size_bits; + +if (clu_start >= sbi->num_clusters || range->len < sbi->cluster_size) +return -EINVAL ; + +if (clu_end >= sbi->num_clusters) + clu_end = sbi->num_clusters -1 ; + +mutex_lock (&sbi->bitmap_lock); + + trim_begin = trim_end =exfat_find_free_bitmap (sb, clu_start); +if (trim_begin ==EXFAT_EOF_CLUSTER ) + goto unlock; + + next_free_clu =exfat_find_free_bitmap (sb, trim_end +1 ); +if (next_free_clu ==EXFAT_EOF_CLUSTER ) + goto unlock; + +do { +if (next_free_clu == trim_end +1 ) { +/* extend trim range for continuous free cluster */ + trim_end++; + }else { +/* trim current range if it's larger than trim_minlen */ + count = trim_end - trim_begin +1 ; +if (count >= trim_minlen) { + err =sb_issue_discard (sb, +exfat_cluster_to_sector (sbi, trim_begin), + count * sbi->sect_per_clus,GFP_NOFS ,0 ); +if (err) + goto unlock; + + trimmed_total += count; + } + +/* set next start point of the free hole */ + trim_begin = trim_end = next_free_clu; + } + +if (next_free_clu >= clu_end) +break ; + +if (fatal_signal_pending (current)) { + err = -ERESTARTSYS ; + goto unlock; + } + + next_free_clu =exfat_find_free_bitmap (sb, next_free_clu +1 ); + }while (next_free_clu !=EXFAT_EOF_CLUSTER && + next_free_clu > trim_end); + +/* try to trim remainder */ + count = trim_end - trim_begin +1 ; +if (count >= trim_minlen) { + err =sb_issue_discard (sb,exfat_cluster_to_sector (sbi, trim_begin), + count * sbi->sect_per_clus,GFP_NOFS ,0 ); +if (err) + goto unlock; + + trimmed_total += count; + } + +unlock: +mutex_unlock (&sbi->bitmap_lock); + range->len = trimmed_total << sbi->cluster_size_bits; + +return err; +}
fs/exfat/dir.c +5 -0
@@ -4,6 +4,7 @@*/ #include <linux/slab.h> +#include <linux/compat.h> #include <linux/bio.h> #include <linux/buffer_head.h>
@@ -306,6 +307,10 @@ const struct file_operations exfat_dir_operations = { .llseek = generic_file_llseek, .read = generic_read_dir, .iterate = exfat_iterate, + .unlocked_ioctl = exfat_ioctl, +#ifdef CONFIG_COMPAT + .compat_ioctl = exfat_compat_ioctl, +#endif .fsync = exfat_file_fsync, };
fs/exfat/exfat_fs.h +4 -0
@@ -412,6 +412,7 @@ int exfat_set_bitmap(struct inode *inode, unsigned int clu);void exfat_clear_bitmap (struct inode *inode,unsigned int clu,bool sync);unsigned int exfat_find_free_bitmap (struct super_block *sb,unsigned int clu);int exfat_count_used_clusters (struct super_block *sb,unsigned int *ret_count); +int exfat_trim_fs (struct inode *inode,struct fstrim_range *range);/* file.c */ extern const struct file_operations exfat_file_operations;
@@ -421,6 +422,9 @@ int exfat_setattr(struct dentry *dentry, struct iattr *attr);int exfat_getattr (const struct path *path,struct kstat *stat,unsigned int request_mask,unsigned int query_flags);int exfat_file_fsync (struct file *file,loff_t start,loff_t end,int datasync); +long exfat_ioctl (struct file *filp,unsigned int cmd,unsigned long arg); +long exfat_compat_ioctl (struct file *filp,unsigned int cmd, +unsigned long arg);/* namei.c */ extern const struct dentry_operations exfat_dentry_ops;
fs/exfat/file.c +53 -0
71 lines not on this page · raw message
Signature · 1 line
2.27.0.83.g0313f36
RE: [PATCH v4 2/2] exfat: add support ioctl and FITRIM function
Quoted 4 lines
Add FITRIM ioctl to enable discarding unused blocks while mounted. As current exFAT doesn't have generic ioctl handler, add empty ioctl function first, and add FITRIM handler.
Quoted 9 lines
Reviewed-by Chaitanya Kulkarni <[email protected]>Signed-off-by Hyeongseok Kim <[email protected]>--- fs/exfat/balloc.c | 80 +++++++++++++++++++++++++++++++++++++++++++++ fs/exfat/dir.c | 5 +++ fs/exfat/exfat_fs.h | 4 +++ fs/exfat/file.c | 53 ++++++++++++++++++++++++++++++ 4 files changed, 142 insertions(+)
It looks better than v3.
Thanks for your work!
BTW, there is still a problem that the alloc/free cluster operation waits until the trimfs operation is finished.
Any ideas for improvement in the future are welcome. :)
Namjae Jeon
AppliedQuoted 23 lines
This is for adding FITRIM ioctl functionality to exFAT filesystem.
Firstly, because the fstrim is long operation, introducebitmap_lockto narrow the lock range to prevent read operation stall.
After that, add generic ioctl function and FITRIM handler.Changelog
=========v3->v4:
- Introducebitmap_lockmutex to narrow the lock range for bitmap access and change to usebitmap_lockinstead ofs_lockin FITRIM handler to prevent read stall while ongoing fstrim.
- Minor code style fix
v2->v3:
- Remove unnecessary local variable
- Merge all changes to a single patch
v1->v2:
- Change variable declaration order as reverse tree style.
- Return-EOPNOTSUPPfromsb_issue_discard()just as it is.
- Removecond_resched()in while loop.
- Move ioctl related code into it's helper function.
Quoted 3 lines
Hyeongseok Kim (2):
exfat: introducebitmap_lockfor cluster bitmap access
exfat: add support ioctl and FITRIM function
Applied. Thanks for your patches!
Quoted 11 lines
fs/exfat/balloc.c | 80 +++++++++++++++++++++++++++++++++++++++++++++ fs/exfat/dir.c | 5 +++ fs/exfat/exfat_fs.h | 5 +++ fs/exfat/fatent.c | 37 ++++++++++++++++----- fs/exfat/file.c | 53 ++++++++++++++++++++++++++++++ fs/exfat/super.c | 1 + 6 files changed, 173 insertions(+), 8 deletions(-)--
2.27.0.83.g0313f36