d017ad
From 07ed253a49cbe80c15d43ed3800206f99d15b43e Mon Sep 17 00:00:00 2001
d017ad
From: Karel Zak <kzak@redhat.com>
d017ad
Date: Mon, 4 Oct 2021 11:14:01 +0200
d017ad
Subject: fstrim: don't trigger autofs
d017ad
d017ad
- ignore read-only entries
d017ad
- ignore autofs entries (for example from /proc/self/mountinfo)
d017ad
- ignore autofs mountpoints where automounter has not been triggered yet
d017ad
d017ad
Fixes: https://github.com/karelzak/util-linux/issues/1463
d017ad
Signed-off-by: Karel Zak <kzak@redhat.com>
d017ad
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=2165981
d017ad
---
d017ad
 sys-utils/fstrim.8.adoc |  2 +-
d017ad
 sys-utils/fstrim.c      | 28 +++++++++++++++++++++++++++-
d017ad
 2 files changed, 28 insertions(+), 2 deletions(-)
d017ad
d017ad
diff --git a/sys-utils/fstrim.8.adoc b/sys-utils/fstrim.8.adoc
d017ad
index 66671c293..d2f3b05be 100644
d017ad
--- a/sys-utils/fstrim.8.adoc
d017ad
+++ b/sys-utils/fstrim.8.adoc
d017ad
@@ -29,7 +29,7 @@ Running *fstrim* frequently, or even using *mount -o discard*, might negatively
d017ad
 The _offset_, _length_, and _minimum-size_ arguments may be followed by the multiplicative suffixes KiB (=1024), MiB (=1024*1024), and so on for GiB, TiB, PiB, EiB, ZiB and YiB (the "iB" is optional, e.g., "K" has the same meaning as "KiB") or the suffixes KB (=1000), MB (=1000*1000), and so on for GB, TB, PB, EB, ZB and YB.
d017ad
 
d017ad
 *-A, --fstab*::
d017ad
-Trim all mounted filesystems mentioned in _/etc/fstab_ on devices that support the discard operation. The root filesystem is determined from kernel command line if missing in the file. The other supplied options, like *--offset*, *--length* and *--minimum*, are applied to all these devices. Errors from filesystems that do not support the discard operation, read-only devices and read-only filesystems are silently ignored.
d017ad
+Trim all mounted filesystems mentioned in _/etc/fstab_ on devices that support the discard operation. The root filesystem is determined from kernel command line if missing in the file. The other supplied options, like *--offset*, *--length* and *--minimum*, are applied to all these devices. Errors from filesystems that do not support the discard operation, read-only devices, autofs and read-only filesystems are silently ignored.
d017ad
 
d017ad
 *-a, --all*::
d017ad
 Trim all mounted filesystems on devices that support the discard operation. The other supplied options, like *--offset*, *--length* and *--minimum*, are applied to all these devices. Errors from filesystems that do not support the discard operation, read-only devices and read-only filesystems are silently ignored.
d017ad
diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c
d017ad
index d2aec4f71..ea787f42c 100644
d017ad
--- a/sys-utils/fstrim.c
d017ad
+++ b/sys-utils/fstrim.c
d017ad
@@ -35,6 +35,7 @@
d017ad
 
d017ad
 #include <sys/ioctl.h>
d017ad
 #include <sys/stat.h>
d017ad
+#include <sys/vfs.h>
d017ad
 #include <linux/fs.h>
d017ad
 
d017ad
 #include "nls.h"
d017ad
@@ -45,6 +46,7 @@
d017ad
 #include "pathnames.h"
d017ad
 #include "sysfs.h"
d017ad
 #include "optutils.h"
d017ad
+#include "statfs_magic.h"
d017ad
 
d017ad
 #include <libmount.h>
d017ad
 
d017ad
@@ -207,6 +209,30 @@ fail:
d017ad
 	return 1;
d017ad
 }
d017ad
 
d017ad
+static int is_unwanted_fs(struct libmnt_fs *fs, const char *tgt)
d017ad
+{
d017ad
+	struct statfs vfs;
d017ad
+	int fd, rc;
d017ad
+
d017ad
+	if (mnt_fs_is_pseudofs(fs))
d017ad
+		return 1;
d017ad
+	if (mnt_fs_is_netfs(fs))
d017ad
+		return 1;
d017ad
+	if (mnt_fs_is_swaparea(fs))
d017ad
+		return 1;
d017ad
+	if (mnt_fs_match_fstype(fs, "autofs"))
d017ad
+		return 1;
d017ad
+	if (mnt_fs_match_options(fs, "ro"))
d017ad
+		return 1;
d017ad
+
d017ad
+	fd = open(tgt, O_PATH);
d017ad
+	if (!fd)
d017ad
+		return 1;
d017ad
+	rc = fstatfs(fd, &vfs) != 0 || vfs.f_type == STATFS_AUTOFS_MAGIC;
d017ad
+	close(fd);
d017ad
+
d017ad
+	return rc;
d017ad
+}
d017ad
 
d017ad
 static int uniq_fs_target_cmp(
d017ad
 		struct libmnt_table *tb __attribute__((__unused__)),
d017ad
@@ -292,7 +318,7 @@ static int fstrim_all_from_file(struct fstrim_control *ctl, const char *filename
d017ad
 		const char *src = mnt_fs_get_srcpath(fs),
d017ad
 			   *tgt = mnt_fs_get_target(fs);
d017ad
 
d017ad
-		if (!tgt || mnt_fs_is_pseudofs(fs) || mnt_fs_is_netfs(fs)) {
d017ad
+		if (!tgt || is_unwanted_fs(fs, tgt)) {
d017ad
 			mnt_table_remove_fs(tab, fs);
d017ad
 			continue;
d017ad
 		}
d017ad
-- 
d017ad
2.39.1
d017ad