93a10f
From 9272023b42febae7db1ec828016a980146095a83 Mon Sep 17 00:00:00 2001
93a10f
From: Karel Zak <kzak@redhat.com>
93a10f
Date: Thu, 2 Feb 2023 13:21:33 +0100
93a10f
Subject: fstrim: backport entries de-duplication
93a10f
93a10f
Upstream: http://github.com/util-linux/util-linux/commit/9dbc073e4c58a56f68da8209df19789131446f5e
93a10f
Upstream: http://github.com/util-linux/util-linux/commit/67f974d41d62c8b521fe81e1aac92562366f6a0a
93a10f
Upstream: http://github.com/util-linux/util-linux/commit/20af6cee463cd6329b4f06db3282a09be942bd7a
93a10f
Addresses: https://bugzilla.redhat.com/show_bug.cgi?id=2121699
93a10f
Signed-off-by: Karel Zak <kzak@redhat.com>
93a10f
---
93a10f
 sys-utils/fstrim.c | 88 ++++++++++++++++++++++++++++++++++++++--------
93a10f
 1 file changed, 74 insertions(+), 14 deletions(-)
93a10f
93a10f
diff --git a/sys-utils/fstrim.c b/sys-utils/fstrim.c
93a10f
index 70870ef69..6aa523d72 100644
93a10f
--- a/sys-utils/fstrim.c
93a10f
+++ b/sys-utils/fstrim.c
93a10f
@@ -35,6 +35,7 @@
93a10f
 
93a10f
 #include <sys/ioctl.h>
93a10f
 #include <sys/stat.h>
93a10f
+#include <sys/vfs.h>
93a10f
 #include <linux/fs.h>
93a10f
 
93a10f
 #include "nls.h"
93a10f
@@ -43,6 +44,7 @@
93a10f
 #include "closestream.h"
93a10f
 #include "pathnames.h"
93a10f
 #include "sysfs.h"
93a10f
+#include "statfs_magic.h"
93a10f
 
93a10f
 #include <libmount.h>
93a10f
 
93a10f
@@ -84,6 +86,7 @@ static int fstrim_filesystem(const char *path, struct fstrim_range *rangetpl,
93a10f
 		goto done;
93a10f
 	}
93a10f
 	errno = 0;
93a10f
+
93a10f
 	if (ioctl(fd, FITRIM, &range)) {
93a10f
 		rc = errno == EOPNOTSUPP || errno == ENOTTY ? 1 : -errno;
93a10f
 
93a10f
@@ -114,7 +117,7 @@ static int has_discard(const char *devname, struct sysfs_cxt *wholedisk)
93a10f
 	struct sysfs_cxt cxt, *parent = NULL;
93a10f
 	uint64_t dg = 0;
93a10f
 	dev_t disk = 0, dev;
93a10f
-	int rc;
93a10f
+	int rc, rdonly = 0;
93a10f
 
93a10f
 	dev = sysfs_devname_to_devno(devname, NULL);
93a10f
 	if (!dev)
93a10f
@@ -139,11 +142,46 @@ static int has_discard(const char *devname, struct sysfs_cxt *wholedisk)
93a10f
 	rc = sysfs_init(&cxt, dev, parent);
93a10f
 	if (!rc)
93a10f
 		rc = sysfs_read_u64(&cxt, "queue/discard_granularity", &dg;;
93a10f
+	if (!rc)
93a10f
+		sysfs_scanf(&cxt, "ro", "%d", &rdonly);
93a10f
 
93a10f
 	sysfs_deinit(&cxt);
93a10f
-	return rc == 0 && dg > 0;
93a10f
+	return rc == 0 && dg > 0 && rdonly == 0;
93a10f
 }
93a10f
 
93a10f
+static int is_unwanted_fs(struct libmnt_fs *fs, const char *tgt)
93a10f
+{
93a10f
+	struct statfs vfs;
93a10f
+	int fd, rc;
93a10f
+
93a10f
+	if (mnt_fs_is_pseudofs(fs))
93a10f
+		return 1;
93a10f
+	if (mnt_fs_is_netfs(fs))
93a10f
+		return 1;
93a10f
+	if (mnt_fs_is_swaparea(fs))
93a10f
+		return 1;
93a10f
+	if (mnt_fs_match_fstype(fs, "autofs"))
93a10f
+		return 1;
93a10f
+	if (mnt_fs_match_options(fs, "ro"))
93a10f
+		return 1;
93a10f
+
93a10f
+	fd = open(tgt, O_PATH);
93a10f
+	if (fd < 0)
93a10f
+		return 1;
93a10f
+	rc = fstatfs(fd, &vfs) != 0 || vfs.f_type == STATFS_AUTOFS_MAGIC;
93a10f
+	close(fd);
93a10f
+	if (rc)
93a10f
+		return 1;
93a10f
+
93a10f
+	/* FITRIM on read-only filesystem can fail, and it can fail */
93a10f
+	if (access(tgt, W_OK) != 0) {
93a10f
+		if (errno == EROFS)
93a10f
+			return 1;
93a10f
+		if (errno == EACCES)
93a10f
+			return 1;
93a10f
+	}
93a10f
+	return 0;
93a10f
+}
93a10f
 
93a10f
 static int uniq_fs_target_cmp(
93a10f
 		struct libmnt_table *tb __attribute__((__unused__)),
93a10f
@@ -182,30 +220,38 @@ static int fstrim_all(struct fstrim_range *rangetpl, int verbose)
93a10f
 
93a10f
 	mnt_init_debug(0);
93a10f
 
93a10f
-	itr = mnt_new_iter(MNT_ITER_BACKWARD);
93a10f
-	if (!itr)
93a10f
-		err(MNT_EX_FAIL, _("failed to initialize libmount iterator"));
93a10f
-
93a10f
 	tab = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO);
93a10f
 	if (!tab)
93a10f
 		err(MNT_EX_FAIL, _("failed to parse %s"), _PATH_PROC_MOUNTINFO);
93a10f
 
93a10f
+	if (mnt_table_is_empty(tab)) {
93a10f
+		mnt_unref_table(tab);
93a10f
+		return MNT_EX_SUCCESS;
93a10f
+	}
93a10f
+
93a10f
 	/* de-duplicate by mountpoints */
93a10f
 	mnt_table_uniq_fs(tab, 0, uniq_fs_target_cmp);
93a10f
 
93a10f
-	/* de-duplicate by source */
93a10f
-	mnt_table_uniq_fs(tab, MNT_UNIQ_FORWARD, uniq_fs_source_cmp);
93a10f
+	itr = mnt_new_iter(MNT_ITER_BACKWARD);
93a10f
+	if (!itr)
93a10f
+		err(MNT_EX_FAIL, _("failed to initialize libmount iterator"));
93a10f
 
93a10f
+	/* Remove useless entries and canonicalize the table */
93a10f
 	while (mnt_table_next_fs(tab, itr, &fs) == 0) {
93a10f
 		const char *src = mnt_fs_get_srcpath(fs),
93a10f
 			   *tgt = mnt_fs_get_target(fs);
93a10f
 		char *path;
93a10f
 		int rc = 1;
93a10f
 
93a10f
-		if (!src || !tgt || *src != '/' ||
93a10f
-		    mnt_fs_is_pseudofs(fs) ||
93a10f
-		    mnt_fs_is_netfs(fs))
93a10f
+		if (!tgt || is_unwanted_fs(fs, tgt)) {
93a10f
+			mnt_table_remove_fs(tab, fs);
93a10f
 			continue;
93a10f
+		}
93a10f
+
93a10f
+		if (!src || *src != '/') {
93a10f
+			mnt_table_remove_fs(tab, fs);
93a10f
+			continue;
93a10f
+		}
93a10f
 
93a10f
 		/* Is it really accessible mountpoint? Not all mountpoints are
93a10f
 		 * accessible (maybe over mounted by another filesystem) */
93a10f
@@ -213,11 +259,25 @@ static int fstrim_all(struct fstrim_range *rangetpl, int verbose)
93a10f
 		if (path && strcmp(path, tgt) == 0)
93a10f
 			rc = 0;
93a10f
 		free(path);
93a10f
-		if (rc)
93a10f
+		if (rc) {
93a10f
+			mnt_table_remove_fs(tab, fs);
93a10f
 			continue;	/* overlaying mount */
93a10f
+		}
93a10f
 
93a10f
-		if (!has_discard(src, &wholedisk))
93a10f
+		if (!has_discard(src, &wholedisk)) {
93a10f
+			mnt_table_remove_fs(tab, fs);
93a10f
 			continue;
93a10f
+		}
93a10f
+	}
93a10f
+
93a10f
+	/* de-duplicate by source */
93a10f
+	mnt_table_uniq_fs(tab, MNT_UNIQ_FORWARD, uniq_fs_source_cmp);
93a10f
+
93a10f
+	mnt_reset_iter(itr, MNT_ITER_BACKWARD);
93a10f
+
93a10f
+	/* Do FITRIM */
93a10f
+	while (mnt_table_next_fs(tab, itr, &fs) == 0) {
93a10f
+		const char *tgt = mnt_fs_get_target(fs);
93a10f
 		cnt++;
93a10f
 
93a10f
 		/*
93a10f
@@ -231,10 +291,10 @@ static int fstrim_all(struct fstrim_range *rangetpl, int verbose)
93a10f
 		if (fstrim_filesystem(tgt, rangetpl, verbose) < 0)
93a10f
 		       cnt_err++;
93a10f
 	}
93a10f
+	mnt_free_iter(itr);
93a10f
 
93a10f
 	sysfs_deinit(&wholedisk);
93a10f
 	mnt_unref_table(tab);
93a10f
-	mnt_free_iter(itr);
93a10f
 
93a10f
 	if (cnt && cnt == cnt_err)
93a10f
 		return MNT_EX_FAIL;		/* all failed */
93a10f
-- 
93a10f
2.39.1
93a10f