Skip to contentSkip to conversation

btrfs-progs: fix discard support check

PATCH In review Wang Yugui 2 participants 2 messages Nov 13, 2021 – Nov 15, 2021 Btrfs file system

lore
Share & tools
lore

Wang Yugui

author
#0 lore raw

PATCH v2 1/1 btrfs-progs: fix discard support check

[BUG]
mkfs.btrfs(v5.15) output a message even if the disk is a HDD without TRIM/DISCARD support.
Performing full device TRIM /dev/sdc2 (326.03GiB) ...

[CAUSE]
mkfs.btrfs check TRIM/DISCARD support through the content of queue/discard_granularity, but compare it against a wrong value.

When HDD without TRIM/DISCARD support, the content of queue/discard_granularity is '0' '\n' '\0', rather than '0' '\0'.

[FIX]
- compare the value based on atoi() to provide more robustness
- delete unnecessary '\n' in pr_verbose()

Fixes c50c448518bb ("btrfs-progs: do sysfs detection of device discard capability")
---
 common/device-utils.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)
common/device-utils.c +3 -2
index 74a25879..65353f28 100644
@@ -64,8 +64,9 @@ static int discard_supported(const char *device)
 		pr_verbose(3, "cannot read discard_granularity for %s\n", device);
 		return 0;
 	} else {
-		if (buf[0] == '0' && buf[1] == 0) {
-			pr_verbose(3, "%s: discard_granularity %s\n", device, buf);
+		/* string(buf) end with '\n\0' */
+		if (atoi(buf) == 0) {
+			pr_verbose(3, "%s: discard_granularity %s", device, buf);
 			return 0;
 		}
 	}
Signature · 1 line
2.32.0
#1 lore raw
Quoted 12 lines · Wang Yugui

[BUG]
mkfs.btrfs(v5.15) output a message even if the disk is a HDD without TRIM/DISCARD support.
Performing full device TRIM /dev/sdc2 (326.03GiB) ...

[CAUSE]
mkfs.btrfs check TRIM/DISCARD support through the content of queue/discard_granularity, but compare it against a wrong value.

When HDD without TRIM/DISCARD support, the content of queue/discard_granularity is '0' '\n' '\0', rather than '0' '\0'.

Quoted 6 lines

[FIX]
- compare the value based on atoi() to provide more robustness
- delete unnecessary '\n' in pr_verbose()

Fixes c50c448518bb ("btrfs-progs: do sysfs detection of device discard capability")

Added to devel, thanks.