Linux Btrfs filesystem development
 help / color / mirror / Atom feed
* [PATCH v2] btrfs-progs: check discard_max_bytes in discard_supported()
@ 2025-08-05 17:14 Anand Jain
  2025-08-05 18:04 ` Boris Burkov
  2025-08-06  5:06 ` Qu Wenruo
  0 siblings, 2 replies; 9+ messages in thread
From: Anand Jain @ 2025-08-05 17:14 UTC (permalink / raw)
  To: linux-btrfs

Some devices may advertise discard support but have discard_max_bytes=0,
effectively disabling it. Add a check to read discard_max_bytes and
treat zero as no discard support.

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
...

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

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

Helps avoid false positives in discard capability detection.

Signed-off-by: Anand Jain <anand.jain@oracle.com>
---
v1: https://lore.kernel.org/linux-btrfs/2f9687740a9f9d60bdea8d24f215c6c0e2a9657b.1753713395.git.anand.jain@oracle.com/

v2: Checks for discard_max_bytes().

 common/device-utils.c | 11 +++++++++++
 1 file changed, 11 insertions(+)

diff --git a/common/device-utils.c b/common/device-utils.c
index 783d79555446..d110292fe718 100644
--- a/common/device-utils.c
+++ b/common/device-utils.c
@@ -76,6 +76,17 @@ static int discard_supported(const char *device)
 		}
 	}
 
+	ret = device_get_queue_param(device, "discard_max_bytes", buf, sizeof(buf));
+	if (ret == 0) {
+		pr_verbose(3, "cannot read discard_max_bytes for %s\n", device);
+		return 0;
+	} else {
+		if (atoi(buf) == 0) {
+			pr_verbose(3, "%s: discard_max_bytes %s", device, buf);
+			return 0;
+		}
+	}
+
 	return 1;
 }
 
-- 
2.50.1


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

end of thread, other threads:[~2025-08-06 13:23 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2025-08-05 17:14 [PATCH v2] btrfs-progs: check discard_max_bytes in discard_supported() Anand Jain
2025-08-05 18:04 ` Boris Burkov
2025-08-06  3:29   ` Anand Jain
2025-08-06  5:06 ` Qu Wenruo
2025-08-06  6:01   ` Anand Jain
2025-08-06  6:15     ` Qu Wenruo
2025-08-06  6:29       ` Anand Jain
2025-08-06  6:35         ` Qu Wenruo
2025-08-06 13:23           ` Anand Jain

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox