Skip to contentSkip to conversation

ext4: check if device support discard in FITRIM ioctl

PATCH Applied Lukas Czerner 2 participants 5 messages Feb 15, 2011 – Feb 23, 2011 ext4 file system

lore
Share & tools
lore
#0 lore raw

PATCH 1/2 ext4: check if device support discard in FITRIM ioctl

Right now we, are relying on the fact that when we attempt to actually do the discard, blkdev_issue_discar() returns -EOPNOTSUPP and the user is informed that the device does not support discard.

However, in the case where the we do not hit any suitable free extent to trim in FITRIM code, it will finish without any error. This is very confusing, because it seems that FITRIM was successful even though the device does not actually supports discard.

Solution: Check for the discard support before attempt to search for free extents.

---
 fs/ext4/ioctl.c |    4 ++++
 1 files changed, 4 insertions(+), 0 deletions(-)
fs/ext4/ioctl.c +4 -0
index eb3bc2f..25ba7c7 100644
@@ -334,12 +334,16 @@ mext_out:
 	case FITRIM:
 	{
 		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 *)arg,
 		    sizeof(range)))
 			return -EFAULT;
Signature · 1 line
1.7.4
#1 lore raw

PATCH 2/2 ext4: Adjust mineln with discard_granularity in FITRIM code

Discard granularity tells us the minimum size of extent to be discarded. Use that information to adjust minlen properly in FITRIM code. Smaller extents will be ignored anyway, so we can optimize by not even trying to discard them.

---
 fs/ext4/ioctl.c |    2 ++
 1 files changed, 2 insertions(+), 0 deletions(-)
fs/ext4/ioctl.c +2 -0
index 25ba7c7..c052c9f 100644
@@ -348,6 +348,8 @@ mext_out:
 		    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;
Signature · 1 line
1.7.4
maintainer Applied
#2 lore raw

Re: [PATCH 2/2] ext4: Adjust mineln with discard_granularity in FITRIM code

Quoted 6 lines · Lukas Czerner

Discard granularity tells us the minimum size of extent to be discarded. Use that information to adjust minlen properly in FITRIM code. Smaller extents will be ignored anyway, so we can optimize by not even trying to discard them.

Applied to the ext4 patch queue, with a rewritten commit description.

"Always write with a deep sympathy for the reader"

- Ted

ext4: Adjust minlen with discard_granularity in the FITRIM ioctl

From: Lukas Czerner <[email protected]>

Discard granularity tells us the minimum size of extent that can be discard by the device. If the user supplies a minimum extent that should be discarded (range.minlen) which is smaller than the discard granularity, increase minlen to the discard granularity, since there's no point submitting trim requests that the device will reject anyway.

#3 lore raw

Re: [PATCH 2/2] ext4: Adjust mineln with discard_granularity in FITRIM code

Quoted 18 lines · Ted Ts'o
Quoted 6 lines · Lukas Czerner

Discard granularity tells us the minimum size of extent to be discarded. Use that information to adjust minlen properly in FITRIM code. Smaller extents will be ignored anyway, so we can optimize by not even trying to discard them.

Applied to the ext4 patch queue, with a rewritten commit description.

"Always write with a deep sympathy for the reader"

- Ted

ext4: Adjust minlen with discard_granularity in the FITRIM ioctl

From: Lukas Czerner <[email protected]>

Quoted 9 lines

Discard granularity tells us the minimum size of extent that can be discard by the device. If the user supplies a minimum extent that should be discarded (range.minlen) which is smaller than the discard granularity, increase minlen to the discard granularity, since there's no point submitting trim requests that the device will reject anyway.

Hi Ted,

thanks for merging and for the description. My English still needs a lot of practice :)

Thanks!
-Lukas

maintainer
#4 lore raw
In reply to Lukas Czerner
Quoted 9 lines · Lukas Czerner

Right now we, are relying on the fact that when we attempt to actually do the discard, blkdev_issue_discar() returns -EOPNOTSUPP and the user is informed that the device does not support discard.

However, in the case where the we do not hit any suitable free extent to trim in FITRIM code, it will finish without any error. This is very confusing, because it seems that FITRIM was successful even though the device does not actually supports discard.

Quoted 4 lines

Solution: Check for the discard support before attempt to search for free extents.

Added to the ext4 patch queue, but I rewrote the commit description to make it more understable.

- Ted

ext4: check if device support discard in FITRIM ioctl

From: Lukas Czerner <[email protected]>

For a device that does not support discard, the FITRIM ioctl returns -EOPNOTSUPP when blkdev_issue_discard() returns this error code, which is how the user is informed that the device does not support discard.

If there are no suitable free extents to be trimmed, then FITRIM will return success even though the device does not support discard, which could confuse the user. So check explicitly if the device supports discard and return an error code at the beginning of the FITRIM ioctl processing.