linux-kernel.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
* [PATCH v2 0/2] Add FITRIM ioctl support for exFAT filesystem
@ 2021-02-16 22:33 Hyeongseok Kim
  2021-02-16 22:33 ` [PATCH v2 1/2] exfat: add initial ioctl function Hyeongseok Kim
  2021-02-16 22:33 ` [PATCH v2 2/2] exfat: add support FITRIM ioctl Hyeongseok Kim
  0 siblings, 2 replies; 11+ messages in thread
From: Hyeongseok Kim @ 2021-02-16 22:33 UTC (permalink / raw)
  To: namjae.jeon, sj1557.seo; +Cc: linux-fsdevel, linux-kernel, Hyeongseok Kim

This is for adding FITRIM ioctl functionality for exFAT filesystem.
For this, add generic ioctl handler and FITRIM operation.

Changelog
=========

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: add initial ioctl function
  exfat: add support FITRIM ioctl

 fs/exfat/balloc.c   | 81 +++++++++++++++++++++++++++++++++++++++++++++
 fs/exfat/dir.c      |  5 +++
 fs/exfat/exfat_fs.h |  4 +++
 fs/exfat/file.c     | 54 ++++++++++++++++++++++++++++++
 4 files changed, 144 insertions(+)

-- 
2.27.0.83.g0313f36


^ permalink raw reply	[flat|nested] 11+ messages in thread

* [PATCH v2 1/2] exfat: add initial ioctl function
  2021-02-16 22:33 [PATCH v2 0/2] Add FITRIM ioctl support for exFAT filesystem Hyeongseok Kim
@ 2021-02-16 22:33 ` Hyeongseok Kim
  2021-02-16 23:51   ` Chaitanya Kulkarni
  2021-02-16 22:33 ` [PATCH v2 2/2] exfat: add support FITRIM ioctl Hyeongseok Kim
  1 sibling, 1 reply; 11+ messages in thread
From: Hyeongseok Kim @ 2021-02-16 22:33 UTC (permalink / raw)
  To: namjae.jeon, sj1557.seo; +Cc: linux-fsdevel, linux-kernel, Hyeongseok Kim

Initialize empty ioctl function

Signed-off-by: Hyeongseok Kim <hyeongseok@gmail.com>
---
 fs/exfat/dir.c      |  5 +++++
 fs/exfat/exfat_fs.h |  3 +++
 fs/exfat/file.c     | 21 +++++++++++++++++++++
 3 files changed, 29 insertions(+)

diff --git a/fs/exfat/dir.c b/fs/exfat/dir.c
index 916797077aad..e1d5536de948 100644
--- a/fs/exfat/dir.c
+++ b/fs/exfat/dir.c
@@ -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,
 };
 
diff --git a/fs/exfat/exfat_fs.h b/fs/exfat/exfat_fs.h
index 764bc645241e..a183021ae31d 100644
--- a/fs/exfat/exfat_fs.h
+++ b/fs/exfat/exfat_fs.h
@@ -420,6 +420,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;
diff --git a/fs/exfat/file.c b/fs/exfat/file.c
index a92478eabfa4..679828e7be07 100644
--- a/fs/exfat/file.c
+++ b/fs/exfat/file.c
@@ -4,6 +4,7 @@
  */
 
 #include <linux/slab.h>
+#include <linux/compat.h>
 #include <linux/cred.h>
 #include <linux/buffer_head.h>
 #include <linux/blkdev.h>
@@ -348,6 +349,22 @@ int exfat_setattr(struct dentry *dentry, struct iattr *attr)
 	return error;
 }
 
+long exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
+{
+	switch (cmd) {
+	default:
+		return -ENOTTY;
+	}
+}
+
+#ifdef CONFIG_COMPAT
+long exfat_compat_ioctl(struct file *filp, unsigned int cmd,
+				unsigned long arg)
+{
+	return exfat_ioctl(filp, cmd, (unsigned long)compat_ptr(arg));
+}
+#endif
+
 int exfat_file_fsync(struct file *filp, loff_t start, loff_t end, int datasync)
 {
 	struct inode *inode = filp->f_mapping->host;
@@ -368,6 +385,10 @@ const struct file_operations exfat_file_operations = {
 	.llseek		= generic_file_llseek,
 	.read_iter	= generic_file_read_iter,
 	.write_iter	= generic_file_write_iter,
+	.unlocked_ioctl = exfat_ioctl,
+#ifdef CONFIG_COMPAT
+	.compat_ioctl = exfat_compat_ioctl,
+#endif
 	.mmap		= generic_file_mmap,
 	.fsync		= exfat_file_fsync,
 	.splice_read	= generic_file_splice_read,
-- 
2.27.0.83.g0313f36


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* [PATCH v2 2/2] exfat: add support FITRIM ioctl
  2021-02-16 22:33 [PATCH v2 0/2] Add FITRIM ioctl support for exFAT filesystem Hyeongseok Kim
  2021-02-16 22:33 ` [PATCH v2 1/2] exfat: add initial ioctl function Hyeongseok Kim
@ 2021-02-16 22:33 ` Hyeongseok Kim
  2021-02-16 23:56   ` Chaitanya Kulkarni
  1 sibling, 1 reply; 11+ messages in thread
From: Hyeongseok Kim @ 2021-02-16 22:33 UTC (permalink / raw)
  To: namjae.jeon, sj1557.seo; +Cc: linux-fsdevel, linux-kernel, Hyeongseok Kim

add FITRIM ioctl to support trimming mounted filesystem

Signed-off-by: Hyeongseok Kim <hyeongseok@gmail.com>
---
 fs/exfat/balloc.c   | 81 +++++++++++++++++++++++++++++++++++++++++++++
 fs/exfat/exfat_fs.h |  1 +
 fs/exfat/file.c     | 33 ++++++++++++++++++
 3 files changed, 115 insertions(+)

diff --git a/fs/exfat/balloc.c b/fs/exfat/balloc.c
index 761c79c3a4ba..d47beef66892 100644
--- a/fs/exfat/balloc.c
+++ b/fs/exfat/balloc.c
@@ -273,3 +273,84 @@ 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(&EXFAT_SB(inode->i_sb)->s_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(&EXFAT_SB(inode->i_sb)->s_lock);
+	range->len = trimmed_total << sbi->cluster_size_bits;
+
+	return err;
+}
diff --git a/fs/exfat/exfat_fs.h b/fs/exfat/exfat_fs.h
index a183021ae31d..e050aea0b639 100644
--- a/fs/exfat/exfat_fs.h
+++ b/fs/exfat/exfat_fs.h
@@ -411,6 +411,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;
diff --git a/fs/exfat/file.c b/fs/exfat/file.c
index 679828e7be07..0cbad1577841 100644
--- a/fs/exfat/file.c
+++ b/fs/exfat/file.c
@@ -349,9 +349,42 @@ int exfat_setattr(struct dentry *dentry, struct iattr *attr)
 	return error;
 }
 
+static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
+{
+	struct super_block *sb = inode->i_sb;
+	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;
+
+	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 = exfat_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 exfat_ioctl(struct file *filp, unsigned int cmd, unsigned long arg)
 {
+	struct inode *inode = file_inode(filp);
+
 	switch (cmd) {
+	case FITRIM:
+		return exfat_ioctl_fitrim(inode, arg);
 	default:
 		return -ENOTTY;
 	}
-- 
2.27.0.83.g0313f36


^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] exfat: add initial ioctl function
  2021-02-16 22:33 ` [PATCH v2 1/2] exfat: add initial ioctl function Hyeongseok Kim
@ 2021-02-16 23:51   ` Chaitanya Kulkarni
  2021-02-17  0:13     ` Hyeongseok Kim
  0 siblings, 1 reply; 11+ messages in thread
From: Chaitanya Kulkarni @ 2021-02-16 23:51 UTC (permalink / raw)
  To: Hyeongseok Kim, namjae.jeon, sj1557.seo; +Cc: linux-fsdevel, linux-kernel

On 2/16/21 14:36, Hyeongseok Kim wrote:
> Initialize empty ioctl function
>
> Signed-off-by: Hyeongseok Kim <hyeongseok@gmail.com>
This patch doesn't do much, but this commit log could be better.

Also from my experience there is not point in introducing an empty
function.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 2/2] exfat: add support FITRIM ioctl
  2021-02-16 22:33 ` [PATCH v2 2/2] exfat: add support FITRIM ioctl Hyeongseok Kim
@ 2021-02-16 23:56   ` Chaitanya Kulkarni
  2021-02-17  0:14     ` Hyeongseok Kim
  0 siblings, 1 reply; 11+ messages in thread
From: Chaitanya Kulkarni @ 2021-02-16 23:56 UTC (permalink / raw)
  To: Hyeongseok Kim, namjae.jeon, sj1557.seo; +Cc: linux-fsdevel, linux-kernel

On 2/16/21 14:36, Hyeongseok Kim wrote:
> +static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
> +{
> +	struct super_block *sb = inode->i_sb;
Do you really need sb variable ? it is only used once if I'm not wrong.
> +	struct request_queue *q = bdev_get_queue(sb->s_bdev);
> +	struct fstrim_range range;
> +	int ret = 0;


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] exfat: add initial ioctl function
  2021-02-16 23:51   ` Chaitanya Kulkarni
@ 2021-02-17  0:13     ` Hyeongseok Kim
  2021-02-17  0:17       ` Chaitanya Kulkarni
  0 siblings, 1 reply; 11+ messages in thread
From: Hyeongseok Kim @ 2021-02-17  0:13 UTC (permalink / raw)
  To: Chaitanya Kulkarni, namjae.jeon, sj1557.seo; +Cc: linux-fsdevel, linux-kernel

On 2/17/21 8:51 AM, Chaitanya Kulkarni wrote:
> On 2/16/21 14:36, Hyeongseok Kim wrote:
>> Initialize empty ioctl function
>>
>> Signed-off-by: Hyeongseok Kim <hyeongseok@gmail.com>
> This patch doesn't do much, but this commit log could be better.
Sorry, I don't understand exactly.
You're saying that these 2 patch should be merged to a single patch?
Would it be better?
>
> Also from my experience there is not point in introducing an empty
> function.
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 2/2] exfat: add support FITRIM ioctl
  2021-02-16 23:56   ` Chaitanya Kulkarni
@ 2021-02-17  0:14     ` Hyeongseok Kim
  0 siblings, 0 replies; 11+ messages in thread
From: Hyeongseok Kim @ 2021-02-17  0:14 UTC (permalink / raw)
  To: Chaitanya Kulkarni, namjae.jeon, sj1557.seo; +Cc: linux-fsdevel, linux-kernel

On 2/17/21 8:56 AM, Chaitanya Kulkarni wrote:
> On 2/16/21 14:36, Hyeongseok Kim wrote:
>> +static int exfat_ioctl_fitrim(struct inode *inode, unsigned long arg)
>> +{
>> +	struct super_block *sb = inode->i_sb;
> Do you really need sb variable ? it is only used once if I'm not wrong.
I got it. You're right.
>> +	struct request_queue *q = bdev_get_queue(sb->s_bdev);
>> +	struct fstrim_range range;
>> +	int ret = 0;
>


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] exfat: add initial ioctl function
  2021-02-17  0:13     ` Hyeongseok Kim
@ 2021-02-17  0:17       ` Chaitanya Kulkarni
  2021-02-17  0:33         ` Hyeongseok Kim
  0 siblings, 1 reply; 11+ messages in thread
From: Chaitanya Kulkarni @ 2021-02-17  0:17 UTC (permalink / raw)
  To: Hyeongseok Kim, namjae.jeon, sj1557.seo; +Cc: linux-fsdevel, linux-kernel

On 2/16/21 16:13, Hyeongseok Kim wrote:
> Sorry, I don't understand exactly.
> You're saying that these 2 patch should be merged to a single patch?
> Would it be better?
I think so unless there is a specific reason for this to keep it isolated.

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] exfat: add initial ioctl function
  2021-02-17  0:17       ` Chaitanya Kulkarni
@ 2021-02-17  0:33         ` Hyeongseok Kim
  2021-02-17  5:39           ` Namjae Jeon
  0 siblings, 1 reply; 11+ messages in thread
From: Hyeongseok Kim @ 2021-02-17  0:33 UTC (permalink / raw)
  To: Chaitanya Kulkarni, namjae.jeon, sj1557.seo; +Cc: linux-fsdevel, linux-kernel

On 2/17/21 9:17 AM, Chaitanya Kulkarni wrote:
> On 2/16/21 16:13, Hyeongseok Kim wrote:
>> Sorry, I don't understand exactly.
>> You're saying that these 2 patch should be merged to a single patch?
>> Would it be better?
> I think so unless there is a specific reason for this to keep it isolated.
>
The reason was just that I think it seems better to seperate ioctl 
initializing and adding specific ioctl functionality.
Anyway, I got it.

Namjae,
Do you have any other opinion about this?
If you agree, I'll merge these as one.


^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] exfat: add initial ioctl function
  2021-02-17  0:33         ` Hyeongseok Kim
@ 2021-02-17  5:39           ` Namjae Jeon
  2021-02-17  6:05             ` Hyeongseok Kim
  0 siblings, 1 reply; 11+ messages in thread
From: Namjae Jeon @ 2021-02-17  5:39 UTC (permalink / raw)
  To: Hyeongseok Kim
  Cc: Chaitanya Kulkarni, namjae.jeon, sj1557.seo, linux-fsdevel, linux-kernel

2021-02-17 9:33 GMT+09:00, Hyeongseok Kim <hyeongseok@gmail.com>:
> On 2/17/21 9:17 AM, Chaitanya Kulkarni wrote:
>> On 2/16/21 16:13, Hyeongseok Kim wrote:
>>> Sorry, I don't understand exactly.
>>> You're saying that these 2 patch should be merged to a single patch?
>>> Would it be better?
>> I think so unless there is a specific reason for this to keep it
>> isolated.
>>
> The reason was just that I think it seems better to seperate ioctl
> initializing and adding specific ioctl functionality.
> Anyway, I got it.
>
> Namjae,
Hi Hyeongseok,
> Do you have any other opinion about this?
I also think this patch should be combined with the 2/2 patch.
> If you agree, I'll merge these as one.
Yep, Agreed. Please do that:)
Thanks!
>
>

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: [PATCH v2 1/2] exfat: add initial ioctl function
  2021-02-17  5:39           ` Namjae Jeon
@ 2021-02-17  6:05             ` Hyeongseok Kim
  0 siblings, 0 replies; 11+ messages in thread
From: Hyeongseok Kim @ 2021-02-17  6:05 UTC (permalink / raw)
  To: Namjae Jeon
  Cc: Chaitanya Kulkarni, namjae.jeon, sj1557.seo, linux-fsdevel, linux-kernel

On 2/17/21 2:39 PM, Namjae Jeon wrote:
> Hi Hyeongseok,
>> Do you have any other opinion about this?
> I also think this patch should be combined with the 2/2 patch.
>> If you agree, I'll merge these as one.
> Yep, Agreed. Please do that:)
> Thanks!
Thank you for the opinion.
I sent out v3.



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2021-02-17  6:07 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-02-16 22:33 [PATCH v2 0/2] Add FITRIM ioctl support for exFAT filesystem Hyeongseok Kim
2021-02-16 22:33 ` [PATCH v2 1/2] exfat: add initial ioctl function Hyeongseok Kim
2021-02-16 23:51   ` Chaitanya Kulkarni
2021-02-17  0:13     ` Hyeongseok Kim
2021-02-17  0:17       ` Chaitanya Kulkarni
2021-02-17  0:33         ` Hyeongseok Kim
2021-02-17  5:39           ` Namjae Jeon
2021-02-17  6:05             ` Hyeongseok Kim
2021-02-16 22:33 ` [PATCH v2 2/2] exfat: add support FITRIM ioctl Hyeongseok Kim
2021-02-16 23:56   ` Chaitanya Kulkarni
2021-02-17  0:14     ` Hyeongseok Kim

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).