]> git.ipfire.org Git - thirdparty/systemd.git/commitdiff
mountpoint-util: use mount_option_supported() to detect if 'discard' is support for...
authorLennart Poettering <lennart@poettering.net>
Tue, 7 Mar 2023 10:19:56 +0000 (11:19 +0100)
committerLennart Poettering <lennart@poettering.net>
Thu, 9 Mar 2023 20:56:42 +0000 (21:56 +0100)
src/basic/mountpoint-util.c
src/test/test-mountpoint-util.c

index 6ba07416aebb88a7c4b556e080d2a04831ce3292..4e0994c881c9de82b7d51828c798f3debfd9f46a 100644 (file)
@@ -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",
index 5feda556414d355aa81bfa93088727f6be44d754..8fdfa4d96050c05b58adb2620c0f150da3abb2e4 100644 (file)
@@ -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. */