diff mbox series

[v2] btrfs-progs: fix discard support check

Message ID 20211113224320.31415-1-wangyugui@e16-tech.com (mailing list archive)
State New, archived
Headers show
Series [v2] btrfs-progs: fix discard support check | expand

Commit Message

Wang Yugui Nov. 13, 2021, 10:43 p.m. UTC
[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")
Signed-off-by: Wang Yugui <wangyugui@e16-tech.com>
---
 common/device-utils.c | 5 +++--
 1 file changed, 3 insertions(+), 2 deletions(-)

Comments

David Sterba Nov. 15, 2021, 4:23 p.m. UTC | #1
On Sun, Nov 14, 2021 at 06:43:20AM +0800, Wang Yugui wrote:
> [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")
> Signed-off-by: Wang Yugui <wangyugui@e16-tech.com>

Added to devel, thanks.
diff mbox series

Patch

diff --git a/common/device-utils.c b/common/device-utils.c
index 74a25879..65353f28 100644
--- a/common/device-utils.c
+++ b/common/device-utils.c
@@ -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;
 		}
 	}