cdown / rpms / util-linux

Forked from rpms/util-linux 2 years ago
Clone
531551
diff -up util-linux-2.23.2/libmount/src/libmount.h.in.kzak util-linux-2.23.2/libmount/src/libmount.h.in
531551
--- util-linux-2.23.2/libmount/src/libmount.h.in.kzak	2015-06-23 11:54:00.510210784 +0200
531551
+++ util-linux-2.23.2/libmount/src/libmount.h.in	2015-06-23 11:54:58.592785482 +0200
531551
@@ -420,6 +420,15 @@ extern int mnt_table_get_root_fs(struct
531551
 extern int mnt_table_set_iter(struct libmnt_table *tb, struct libmnt_iter *itr,
531551
 			      struct libmnt_fs *fs);
531551
 
531551
+enum {
531551
+	MNT_UNIQ_FORWARD  = (1 << 1),	/* default is backward */
531551
+	MNT_UNIQ_KEEPTREE = (1 << 2)
531551
+};
531551
+extern int mnt_table_uniq_fs(struct libmnt_table *tb, int flags,
531551
+				int (*cmp)(struct libmnt_table *,
531551
+					   struct libmnt_fs *,
531551
+					   struct libmnt_fs *));
531551
+
531551
 extern struct libmnt_fs *mnt_table_find_mountpoint(struct libmnt_table *tb,
531551
 				const char *path, int direction);
531551
 extern struct libmnt_fs *mnt_table_find_target(struct libmnt_table *tb,
531551
diff -up util-linux-2.23.2/libmount/src/libmount.sym.kzak util-linux-2.23.2/libmount/src/libmount.sym
531551
--- util-linux-2.23.2/libmount/src/libmount.sym.kzak	2015-06-23 11:56:47.259989779 +0200
531551
+++ util-linux-2.23.2/libmount/src/libmount.sym	2015-06-23 11:56:17.681206366 +0200
531551
@@ -256,3 +256,9 @@ global:
531551
 	mnt_context_find_umount_fs;
531551
 	mnt_table_find_mountpoint;
531551
 } MOUNT_2.22;
531551
+
531551
+/* backport from v2.25 to RHEL7 */
531551
+MOUNT_2.25 {
531551
+	mnt_table_uniq_fs;
531551
+} MOUNT_2.23;
531551
+
531551
diff -up util-linux-2.23.2/libmount/src/tab.c.kzak util-linux-2.23.2/libmount/src/tab.c
531551
--- util-linux-2.23.2/libmount/src/tab.c.kzak	2015-06-23 11:52:04.750058424 +0200
531551
+++ util-linux-2.23.2/libmount/src/tab.c	2015-06-23 11:53:26.109462680 +0200
531551
@@ -398,6 +398,93 @@ int mnt_table_find_next_fs(struct libmnt
531551
 	return 1;
531551
 }
531551
 
531551
+static int mnt_table_move_parent(struct libmnt_table *tb, int oldid, int newid)
531551
+{
531551
+	struct libmnt_iter itr;
531551
+	struct libmnt_fs *fs;
531551
+
531551
+	if (!tb)
531551
+		return -EINVAL;
531551
+	if (list_empty(&tb->ents))
531551
+		return 0;
531551
+
531551
+
531551
+	mnt_reset_iter(&itr, MNT_ITER_FORWARD);
531551
+
531551
+	while (mnt_table_next_fs(tb, &itr, &fs) == 0) {
531551
+		if (fs->parent == oldid)
531551
+			fs->parent = newid;
531551
+	}
531551
+	return 0;
531551
+}
531551
+
531551
+/**
531551
+ * mnt_table_uniq_fs:
531551
+ * @tb: table
531551
+ * @flags: MNT_UNIQ_*
531551
+ * @cmp: function to compare filesystems
531551
+ *
531551
+ * This function de-duplicate the @tb, but does not change order of the
531551
+ * filesystems. The @cmp function has to return 0 if the filesystems are
531551
+ * equal, otherwise non-zero.
531551
+ *
531551
+ * The default is to keep in the table later mounted filesystems (function uses
531551
+ * backward mode iterator).
531551
+ *
531551
+ * @MNT_UNIQ_FORWARD:  remove later mounted filesystems
531551
+ * @MNT_UNIQ_KEEPTREE: keep parent->id relation ship stil valid
531551
+ *
531551
+ * Returns: negative number in case of error, or 0 o success.
531551
+ */
531551
+int mnt_table_uniq_fs(struct libmnt_table *tb, int flags,
531551
+				int (*cmp)(struct libmnt_table *,
531551
+					   struct libmnt_fs *,
531551
+					   struct libmnt_fs *))
531551
+{
531551
+	struct libmnt_iter itr;
531551
+	struct libmnt_fs *fs;
531551
+	int direction = MNT_ITER_BACKWARD;
531551
+
531551
+	if (!tb || !cmp)
531551
+		return -EINVAL;
531551
+	if (list_empty(&tb->ents))
531551
+		return 0;
531551
+
531551
+	if (flags & MNT_UNIQ_FORWARD)
531551
+		direction = MNT_ITER_FORWARD;
531551
+
531551
+
531551
+	mnt_reset_iter(&itr, direction);
531551
+
531551
+	if ((flags & MNT_UNIQ_KEEPTREE) && !is_mountinfo(tb))
531551
+		flags &= ~MNT_UNIQ_KEEPTREE;
531551
+
531551
+	while (mnt_table_next_fs(tb, &itr, &fs) == 0) {
531551
+		int want = 1;
531551
+		struct libmnt_iter xtr;
531551
+		struct libmnt_fs *x;
531551
+
531551
+		mnt_reset_iter(&xtr, direction);
531551
+		while (want && mnt_table_next_fs(tb, &xtr, &x) == 0) {
531551
+			if (fs == x)
531551
+				break;
531551
+			want = cmp(tb, x, fs) != 0;
531551
+		}
531551
+
531551
+		if (!want) {
531551
+			if (flags & MNT_UNIQ_KEEPTREE)
531551
+				mnt_table_move_parent(tb, mnt_fs_get_id(fs),
531551
+							  mnt_fs_get_parent_id(fs));
531551
+
531551
+
531551
+
531551
+			mnt_table_remove_fs(tb, fs);
531551
+		}
531551
+	}
531551
+
531551
+	return 0;
531551
+}
531551
+
531551
 /**
531551
  * mnt_table_set_iter:
531551
  * @tb: tab pointer
531551
diff -up util-linux-2.23.2/sys-utils/fstrim.c.kzak util-linux-2.23.2/sys-utils/fstrim.c
531551
--- util-linux-2.23.2/sys-utils/fstrim.c.kzak	2013-06-13 09:46:10.535651605 +0200
531551
+++ util-linux-2.23.2/sys-utils/fstrim.c	2015-06-23 11:57:59.435461283 +0200
531551
@@ -41,6 +41,11 @@
531551
 #include "strutils.h"
531551
 #include "c.h"
531551
 #include "closestream.h"
531551
+#include "pathnames.h"
531551
+#include "sysfs.h"
531551
+#include "exitcodes.h"
531551
+
531551
+#include <libmount.h>
531551
 
531551
 #ifndef FITRIM
531551
 struct fstrim_range {
531551
@@ -51,16 +56,216 @@ struct fstrim_range {
531551
 #define FITRIM		_IOWR('X', 121, struct fstrim_range)
531551
 #endif
531551
 
531551
+/* returns: 0 = success, 1 = unsupported, < 0 = error */
531551
+static int fstrim_filesystem(const char *path, struct fstrim_range *rangetpl,
531551
+			    int verbose)
531551
+{
531551
+	int fd;
531551
+	struct stat sb;
531551
+	struct fstrim_range range;
531551
+
531551
+	/* kernel modifies the range */
531551
+	memcpy(&range, rangetpl, sizeof(range));
531551
+
531551
+	fd = open(path, O_RDONLY);
531551
+	if (fd < 0) {
531551
+		warn(_("cannot open %s"), path);
531551
+		return -1;
531551
+	}
531551
+	if (fstat(fd, &sb) == -1) {
531551
+		warn(_("stat of %s failed"), path);
531551
+		return -1;
531551
+	}
531551
+	if (!S_ISDIR(sb.st_mode)) {
531551
+		warnx(_("%s: not a directory"), path);
531551
+		return -1;
531551
+	}
531551
+	errno = 0;
531551
+	if (ioctl(fd, FITRIM, &range)) {
531551
+		int rc = errno == EOPNOTSUPP || errno == ENOTTY ? 1 : -1;
531551
+
531551
+		if (rc != 1)
531551
+			warn(_("%s: FITRIM ioctl failed"), path);
531551
+		close(fd);
531551
+		return rc;
531551
+	}
531551
+
531551
+	if (verbose) {
531551
+		char *str = size_to_human_string(
531551
+				SIZE_SUFFIX_3LETTER | SIZE_SUFFIX_SPACE,
531551
+				(uint64_t) range.len);
531551
+		/* TRANSLATORS: The standard value here is a very large number. */
531551
+		printf(_("%s: %s (%" PRIu64 " bytes) trimmed\n"),
531551
+				path, str, (uint64_t) range.len);
531551
+		free(str);
531551
+	}
531551
+	close(fd);
531551
+	return 0;
531551
+}
531551
+
531551
+static int has_discard(const char *devname, struct sysfs_cxt *wholedisk)
531551
+{
531551
+	struct sysfs_cxt cxt, *parent = NULL;
531551
+	uint64_t dg = 0;
531551
+	dev_t disk = 0, dev;
531551
+	int rc;
531551
+
531551
+	dev = sysfs_devname_to_devno(devname, NULL);
531551
+	if (!dev)
531551
+		return 1;
531551
+	/*
531551
+	 * This is tricky to read the info from sys/, because the queue
531551
+	 * atrributes are provided for whole devices (disk) only. We're trying
531551
+	 * to reuse the whole-disk sysfs context to optimize this stuff (as
531551
+	 * system usually have just one disk only).
531551
+	 */
531551
+	if (sysfs_devno_to_wholedisk(dev, NULL, 0, &disk) || !disk)
531551
+		return 1;
531551
+	if (dev != disk) {
531551
+		if (wholedisk->devno != disk) {
531551
+			sysfs_deinit(wholedisk);
531551
+			if (sysfs_init(wholedisk, disk, NULL))
531551
+				return 1;
531551
+		}
531551
+		parent = wholedisk;
531551
+	}
531551
+
531551
+	rc = sysfs_init(&cxt, dev, parent);
531551
+	if (!rc)
531551
+		rc = sysfs_read_u64(&cxt, "queue/discard_granularity", &dg;;
531551
+
531551
+	sysfs_deinit(&cxt);
531551
+	return rc == 0 && dg > 0;
531551
+}
531551
+
531551
+
531551
+static int uniq_fs_target_cmp(
531551
+		struct libmnt_table *tb __attribute__((__unused__)),
531551
+		struct libmnt_fs *a,
531551
+		struct libmnt_fs *b)
531551
+{
531551
+	return !mnt_fs_streq_target(a, mnt_fs_get_target(b));
531551
+}
531551
+
531551
+static int uniq_fs_source_cmp(
531551
+		struct libmnt_table *tb __attribute__((__unused__)),
531551
+		struct libmnt_fs *a,
531551
+		struct libmnt_fs *b)
531551
+{
531551
+	int eq;
531551
+
531551
+	if (mnt_fs_is_pseudofs(a) || mnt_fs_is_netfs(a) ||
531551
+	    mnt_fs_is_pseudofs(b) || mnt_fs_is_netfs(b))
531551
+		return 1;
531551
+
531551
+	eq = mnt_fs_streq_srcpath(a, mnt_fs_get_srcpath(b));
531551
+	if (eq) {
531551
+		const char *aroot = mnt_fs_get_root(a),
531551
+			   *broot = mnt_fs_get_root(b);
531551
+		if (!aroot || !broot)
531551
+			eq = 0;
531551
+		else if (strcmp(aroot, broot) != 0)
531551
+			eq = 0;
531551
+	}
531551
+
531551
+	return !eq;
531551
+}
531551
+
531551
+/*
531551
+ * fstrim --all follows "mount -a" return codes:
531551
+ *
531551
+ * 0  = all success
531551
+ * 32 = all failed
531551
+ * 64 = some failed, some success
531551
+ */
531551
+static int fstrim_all(struct fstrim_range *rangetpl, int verbose)
531551
+{
531551
+	struct libmnt_fs *fs;
531551
+	struct libmnt_iter *itr;
531551
+	struct libmnt_table *tab;
531551
+	struct sysfs_cxt wholedisk = UL_SYSFSCXT_EMPTY;
531551
+	int cnt = 0, cnt_err = 0;
531551
+
531551
+	mnt_init_debug(0);
531551
+
531551
+	itr = mnt_new_iter(MNT_ITER_BACKWARD);
531551
+	if (!itr)
531551
+		err(MOUNT_EX_FAIL, _("failed to initialize libmount iterator"));
531551
+
531551
+	tab = mnt_new_table_from_file(_PATH_PROC_MOUNTINFO);
531551
+	if (!tab)
531551
+		err(MOUNT_EX_FAIL, _("failed to parse %s"), _PATH_PROC_MOUNTINFO);
531551
+
531551
+	/* de-duplicate by mountpoints */
531551
+	mnt_table_uniq_fs(tab, 0, uniq_fs_target_cmp);
531551
+
531551
+	/* de-duplicate by source and root */
531551
+	mnt_table_uniq_fs(tab, 0, uniq_fs_source_cmp);
531551
+
531551
+	while (mnt_table_next_fs(tab, itr, &fs) == 0) {
531551
+		const char *src = mnt_fs_get_srcpath(fs),
531551
+			   *tgt = mnt_fs_get_target(fs);
531551
+		char *path;
531551
+		int rc = 1;
531551
+
531551
+		if (!src || !tgt || *src != '/' ||
531551
+		    mnt_fs_is_pseudofs(fs) ||
531551
+		    mnt_fs_is_netfs(fs))
531551
+			continue;
531551
+
531551
+		/* Is it really accessible mountpoint? Not all mountpoints are
531551
+		 * accessible (maybe over mounted by another fylesystem) */
531551
+		path = mnt_get_mountpoint(tgt);
531551
+		if (path && strcmp(path, tgt) == 0)
531551
+			rc = 0;
531551
+		free(path);
531551
+		if (rc)
531551
+			continue;	/* overlaying mount */
531551
+
531551
+		if (!has_discard(src, &wholedisk))
531551
+			continue;
531551
+		cnt++;
531551
+
531551
+		/*
531551
+		 * We're able to detect that the device supports discard, but
531551
+		 * things also depend on filesystem or device mapping, for
531551
+		 * example vfat or LUKS (by default) does not support FSTRIM.
531551
+		 *
531551
+		 * This is reason why we ignore EOPNOTSUPP and ENOTTY errors
531551
+		 * from discard ioctl.
531551
+		 */
531551
+		if (fstrim_filesystem(tgt, rangetpl, verbose) < 0)
531551
+		       cnt_err++;
531551
+	}
531551
+
531551
+	sysfs_deinit(&wholedisk);
531551
+	mnt_free_table(tab);
531551
+	mnt_free_iter(itr);
531551
+
531551
+	if (cnt && cnt == cnt_err)
531551
+		return MOUNT_EX_FAIL;		/* all failed */
531551
+	if (cnt && cnt_err)
531551
+		return MOUNT_EX_SOMEOK;		/* some ok */
531551
+
531551
+	return EXIT_SUCCESS;
531551
+}
531551
+
531551
 static void __attribute__((__noreturn__)) usage(FILE *out)
531551
 {
531551
 	fputs(USAGE_HEADER, out);
531551
 	fprintf(out,
531551
 	      _(" %s [options] <mount point>\n"), program_invocation_short_name);
531551
+
531551
+	fputs(USAGE_SEPARATOR, out);
531551
+	fputs(_("Discard unused blocks on a mounted filesystem.\n"), out);
531551
+
531551
 	fputs(USAGE_OPTIONS, out);
531551
-	fputs(_(" -o, --offset <num>  offset in bytes to discard from\n"
531551
-		" -l, --length <num>  length of bytes to discard from the offset\n"
531551
-		" -m, --minimum <num> minimum extent length to discard\n"
531551
-		" -v, --verbose       print number of discarded bytes\n"), out);
531551
+	fputs(_(" -a, --all           trim all mounted filesystems that are supported\n"), out);
531551
+	fputs(_(" -o, --offset <num>  the offset in bytes to start discarding from\n"), out);
531551
+	fputs(_(" -l, --length <num>  the number of bytes to discard\n"), out);
531551
+	fputs(_(" -m, --minimum <num> the minimum extent length to discard\n"), out);
531551
+	fputs(_(" -v, --verbose       print number of discarded bytes\n"), out);
531551
+
531551
 	fputs(USAGE_SEPARATOR, out);
531551
 	fputs(USAGE_HELP, out);
531551
 	fputs(USAGE_VERSION, out);
531551
@@ -70,12 +275,12 @@ static void __attribute__((__noreturn__)
531551
 
531551
 int main(int argc, char **argv)
531551
 {
531551
-	char *path;
531551
-	int c, fd, verbose = 0;
531551
+	char *path = NULL;
531551
+	int c, rc, verbose = 0, all = 0;
531551
 	struct fstrim_range range;
531551
-	struct stat sb;
531551
 
531551
 	static const struct option longopts[] = {
531551
+	    { "all",       0, 0, 'a' },
531551
 	    { "help",      0, 0, 'h' },
531551
 	    { "version",   0, 0, 'V' },
531551
 	    { "offset",    1, 0, 'o' },
531551
@@ -93,8 +298,11 @@ int main(int argc, char **argv)
531551
 	memset(&range, 0, sizeof(range));
531551
 	range.len = ULLONG_MAX;
531551
 
531551
-	while ((c = getopt_long(argc, argv, "hVo:l:m:v", longopts, NULL)) != -1) {
531551
+	while ((c = getopt_long(argc, argv, "ahVo:l:m:v", longopts, NULL)) != -1) {
531551
 		switch(c) {
531551
+		case 'a':
531551
+			all = 1;
531551
+			break;
531551
 		case 'h':
531551
 			usage(stdout);
531551
 			break;
531551
@@ -122,38 +330,26 @@ int main(int argc, char **argv)
531551
 		}
531551
 	}
531551
 
531551
-	if (optind == argc)
531551
-		errx(EXIT_FAILURE, _("no mountpoint specified"));
531551
-
531551
-	path = argv[optind++];
531551
+	if (!all) {
531551
+		if (optind == argc)
531551
+			errx(EXIT_FAILURE, _("no mountpoint specified"));
531551
+		path = argv[optind++];
531551
+	}
531551
 
531551
 	if (optind != argc) {
531551
 		warnx(_("unexpected number of arguments"));
531551
 		usage(stderr);
531551
 	}
531551
 
531551
-	if (stat(path, &sb) == -1)
531551
-		err(EXIT_FAILURE, _("stat failed %s"), path);
531551
-	if (!S_ISDIR(sb.st_mode))
531551
-		errx(EXIT_FAILURE, _("%s: not a directory"), path);
531551
-
531551
-	fd = open(path, O_RDONLY);
531551
-	if (fd < 0)
531551
-		err(EXIT_FAILURE, _("cannot open %s"), path);
531551
-
531551
-	if (ioctl(fd, FITRIM, &range))
531551
-		err(EXIT_FAILURE, _("%s: FITRIM ioctl failed"), path);
531551
-
531551
-	if (verbose) {
531551
-		char *str = size_to_human_string(SIZE_SUFFIX_3LETTER |
531551
-						 SIZE_SUFFIX_SPACE,
531551
-						 (uint64_t) range.len);
531551
-		/* TRANSLATORS: The standard value here is a very large number. */
531551
-		printf(_("%s: %s (%" PRIu64 " bytes) trimmed\n"),
531551
-						path, str,
531551
-						(uint64_t) range.len);
531551
-		free(str);
531551
+	if (all)
531551
+		rc = fstrim_all(&range, verbose);
531551
+	else {
531551
+		rc = fstrim_filesystem(path, &range, verbose);
531551
+		if (rc == 1) {
531551
+			warnx(_("%s: the discard operation is not supported"), path);
531551
+			rc = EXIT_FAILURE;
531551
+		}
531551
 	}
531551
-	close(fd);
531551
-	return EXIT_SUCCESS;
531551
+
531551
+	return rc;
531551
 }
531551
diff -up util-linux-2.23.2/sys-utils/fstrim.service.in.kzak util-linux-2.23.2/sys-utils/fstrim.service
531551
--- util-linux-2.23.2/sys-utils/fstrim.service.in.kzak	2015-06-23 12:02:18.505564273 +0200
531551
+++ util-linux-2.23.2/sys-utils/fstrim.service.in	2015-06-23 12:02:05.049662802 +0200
531551
@@ -0,0 +1,6 @@
531551
+[Unit]
531551
+Description=Discard unused blocks
531551
+
531551
+[Service]
531551
+Type=oneshot
531551
+ExecStart=@sbindir@/fstrim -a
531551
diff -up util-linux-2.23.2/sys-utils/fstrim.timer.kzak util-linux-2.23.2/sys-utils/fstrim.timer
531551
--- util-linux-2.23.2/sys-utils/fstrim.timer.kzak	2015-06-23 12:02:18.505564273 +0200
531551
+++ util-linux-2.23.2/sys-utils/fstrim.timer	2015-06-23 12:02:05.049662802 +0200
531551
@@ -0,0 +1,11 @@
531551
+[Unit]
531551
+Description=Discard unused blocks once a week
531551
+Documentation=man:fstrim
531551
+
531551
+[Timer]
531551
+OnCalendar=weekly
531551
+AccuracySec=1h
531551
+Persistent=true
531551
+
531551
+[Install]
531551
+WantedBy=multi-user.target
531551
diff -up util-linux-2.23.2/sys-utils/Makemodule.am.kzak util-linux-2.23.2/sys-utils/Makemodule.am
531551
--- util-linux-2.23.2/sys-utils/Makemodule.am.kzak	2015-06-23 11:59:05.803975307 +0200
531551
+++ util-linux-2.23.2/sys-utils/Makemodule.am	2015-06-23 12:01:18.682002323 +0200
531551
@@ -68,7 +68,18 @@ fsfreeze_SOURCES = sys-utils/fsfreeze.c
531551
 sbin_PROGRAMS += fstrim
531551
 dist_man_MANS += sys-utils/fstrim.8
531551
 fstrim_SOURCES = sys-utils/fstrim.c
531551
-fstrim_LDADD = $(LDADD) libcommon.la
531551
+fstrim_LDADD = $(LDADD) libcommon.la libmount.la
531551
+fstrim_CFLAGS = $(AM_CFLAGS) -I$(ul_libmount_incdir)
531551
+
531551
+if HAVE_SYSTEMD
531551
+systemdsystemunit_DATA += \
531551
+		sys-utils/fstrim.service \
531551
+		sys-utils/fstrim.timer
531551
+endif
531551
+
531551
+PATHFILES += sys-utils/fstrim.service
531551
+EXTRA_DIST += sys-utils/fstrim.timer
531551
+
531551
 
531551
 sbin_PROGRAMS += blkdiscard
531551
 dist_man_MANS += sys-utils/blkdiscard.8