All of lore.kernel.org
 help / color / mirror / Atom feed
* [PATCH] btrfs-progs: use zero-range ioctl to verify discard support
@ 2025-07-28 14:36 Anand Jain
  2025-07-30 17:09 ` Boris Burkov
  2025-07-31  2:56 ` Martin K. Petersen
  0 siblings, 2 replies; 6+ messages in thread
From: Anand Jain @ 2025-07-28 14:36 UTC (permalink / raw)
  To: linux-btrfs

The discard_supported() function currently checks
/sys/block/sda/queue/discard_granularity to determine if discard
is supported, assuming a non-zero value means support is available.

However, this isn't always reliable. For example, on a virtual device,
discard_granularity may be non-zero (e.g., 512), but an actual
ioctl(BLKDISCARD) call still fails with EOPNOTSUPP.

Example:

$ cat /sys/block/sda/queue/discard_granularity
512

$ ./mkfs.btrfs -vvv -f /dev/sda
...
Performing full device TRIM /dev/sda (3.00GiB) ...
discard_range ret -1 errno Operation not supported
...

One workaround is to also check discard_max_bytes for a non-zero value.

$ cat /sys/block/sda/queue/discard_max_bytes
0

But a better and more direct way is to test discard support by issuing
a BLKDISCARD ioctl with a zero-length range. If this call fails with
EOPNOTSUPP, discard isn't supported.

This patch changes discard_supported() to use that method.

Signed-off-by: Anand Jain anand.jain@oracle.com
---
 common/device-utils.c | 26 ++++++++++----------------
 1 file changed, 10 insertions(+), 16 deletions(-)

diff --git a/common/device-utils.c b/common/device-utils.c
index 783d79555446..d95d406d9676 100644
--- a/common/device-utils.c
+++ b/common/device-utils.c
@@ -53,30 +53,24 @@
  */
 static int discard_range(int fd, u64 start, u64 len)
 {
+	int ret;
 	u64 range[2] = { start, len };
 
-	if (ioctl(fd, BLKDISCARD, &range) < 0)
-		return errno;
+	ret = ioctl(fd, BLKDISCARD, &range);
+	if (ret < 0)
+		return -errno;
 	return 0;
 }
 
-static int discard_supported(const char *device)
+static bool discard_supported(int fd)
 {
 	int ret;
-	char buf[128] = {};
 
-	ret = device_get_queue_param(device, "discard_granularity", buf, sizeof(buf));
-	if (ret == 0) {
-		pr_verbose(3, "cannot read discard_granularity for %s\n", device);
-		return 0;
-	} else {
-		if (atoi(buf) == 0) {
-			pr_verbose(3, "%s: discard_granularity %s", device, buf);
-			return 0;
-		}
-	}
+	ret = discard_range(fd, 0, 0);
+	if (ret == -EOPNOTSUPP)
+		return false;
 
-	return 1;
+	return true;
 }
 
 /*
@@ -278,7 +272,7 @@ int btrfs_prepare_device(int fd, const char *file, u64 *byte_count_ret,
 		 * is not necessary for the mkfs functionality but just an
 		 * optimization.
 		 */
-		if (discard_supported(file)) {
+		if (discard_supported(fd)) {
 			if (opflags & PREP_DEVICE_VERBOSE)
 				printf("Performing full device TRIM %s (%s) ...\n",
 				       file, pretty_size(byte_count));
-- 
2.50.0


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

end of thread, other threads:[~2025-08-01  2:02 UTC | newest]

Thread overview: 6+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-07-28 14:36 [PATCH] btrfs-progs: use zero-range ioctl to verify discard support Anand Jain
2025-07-30 17:09 ` Boris Burkov
2025-07-31  2:56 ` Martin K. Petersen
2025-07-31 14:25   ` Christoph Hellwig
2025-07-31 15:42     ` Martin K. Petersen
2025-08-01  2:02   ` Anand Jain

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.