From: Lennart Poettering Date: Tue, 7 Mar 2023 10:19:56 +0000 (+0100) Subject: mountpoint-util: use mount_option_supported() to detect if 'discard' is support for... X-Git-Tag: v254-rc1~1073^2~3 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4e6d305ae2c9d6d24815c507ed9f143079a185e3;p=thirdparty%2Fsystemd.git mountpoint-util: use mount_option_supported() to detect if 'discard' is support for an fs --- diff --git a/src/basic/mountpoint-util.c b/src/basic/mountpoint-util.c index 6ba07416aeb..4e0994c881c 100644 --- a/src/basic/mountpoint-util.c +++ b/src/basic/mountpoint-util.c @@ -461,6 +461,15 @@ bool fstype_is_ro(const char *fstype) { } bool fstype_can_discard(const char *fstype) { + int r; + + assert(fstype); + + /* On new kernels we can just ask the kernel */ + r = mount_option_supported(fstype, "discard", NULL); + if (r >= 0) + return r; + return STR_IN_SET(fstype, "btrfs", "f2fs", diff --git a/src/test/test-mountpoint-util.c b/src/test/test-mountpoint-util.c index 5feda556414..8fdfa4d9605 100644 --- a/src/test/test-mountpoint-util.c +++ b/src/test/test-mountpoint-util.c @@ -349,6 +349,12 @@ TEST(mount_option_supported) { assert_se(r >= 0 || (r < 0 && ERRNO_IS_PRIVILEGE(r))); } +TEST(fstype_can_discard) { + assert_se(fstype_can_discard("ext4")); + assert_se(!fstype_can_discard("squashfs")); + assert_se(!fstype_can_discard("iso9660")); +} + static int intro(void) { /* let's move into our own mount namespace with all propagation from the host turned off, so * that /proc/self/mountinfo is static and constant for the whole time our test runs. */