05ad79
diff -up util-linux-2.23.2/sys-utils/swapon.8.kzak util-linux-2.23.2/sys-utils/swapon.8
05ad79
--- util-linux-2.23.2/sys-utils/swapon.8.kzak	2013-06-13 09:46:10.544651682 +0200
05ad79
+++ util-linux-2.23.2/sys-utils/swapon.8	2014-09-24 10:57:45.855230767 +0200
05ad79
@@ -112,15 +112,25 @@ All devices marked as ``swap'' in
05ad79
 are made available, except for those with the ``noauto'' option.
05ad79
 Devices that are already being used as swap are silently skipped.
05ad79
 .TP
05ad79
-.B "\-d, \-\-discard"
05ad79
-Discard freed swap pages before they are reused, if the swap
05ad79
-device supports the discard or trim operation.  This may improve
05ad79
-performance on some Solid State Devices, but often it does not.
05ad79
+.B "\-d, \-\-discard\fR [=\fIpolicy\fR]"
05ad79
+Enable swap discards, if the swap backing device supports the discard or
05ad79
+trim operation. This may improve performance on some Solid State Devices,
05ad79
+but often it does not. The option allows one to select between two
05ad79
+available swap discard policies:
05ad79
+.BI \-\-discard=once
05ad79
+to perform a single-time discard operation for the whole swap area at swapon;
05ad79
+or
05ad79
+.BI \-\-discard=pages
05ad79
+to discard freed swap pages before they are reused, while swapping.
05ad79
+If no policy is selected, the default behavior is to enable both discard types.
05ad79
 The
05ad79
 .I /etc/fstab
05ad79
-mount option
05ad79
-.BI discard
05ad79
-may be also used to enable discard flag.
05ad79
+mount options
05ad79
+.BI discard,
05ad79
+.BI discard=once,
05ad79
+or
05ad79
+.BI discard=pages
05ad79
+may be also used to enable discard flags.
05ad79
 .TP
05ad79
 .B "\-e, \-\-ifexists"
05ad79
 Silently skip devices that do not exist.
05ad79
diff -up util-linux-2.23.2/sys-utils/swapon.c.kzak util-linux-2.23.2/sys-utils/swapon.c
05ad79
--- util-linux-2.23.2/sys-utils/swapon.c.kzak	2013-07-30 10:39:26.348739643 +0200
05ad79
+++ util-linux-2.23.2/sys-utils/swapon.c	2014-09-24 10:57:45.855230767 +0200
05ad79
@@ -34,9 +34,20 @@
05ad79
 #endif
05ad79
 
05ad79
 #ifndef SWAP_FLAG_DISCARD
05ad79
-# define SWAP_FLAG_DISCARD	0x10000 /* discard swap cluster after use */
05ad79
+# define SWAP_FLAG_DISCARD	0x10000 /* enable discard for swap */
05ad79
 #endif
05ad79
 
05ad79
+#ifndef SWAP_FLAG_DISCARD_ONCE
05ad79
+# define SWAP_FLAG_DISCARD_ONCE 0x20000 /* discard swap area at swapon-time */
05ad79
+#endif
05ad79
+
05ad79
+#ifndef SWAP_FLAG_DISCARD_PAGES
05ad79
+# define SWAP_FLAG_DISCARD_PAGES 0x40000 /* discard page-clusters after use */
05ad79
+#endif
05ad79
+
05ad79
+#define SWAP_FLAGS_DISCARD_VALID (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | \
05ad79
+				  SWAP_FLAG_DISCARD_PAGES)
05ad79
+
05ad79
 #ifndef SWAP_FLAG_PREFER
05ad79
 # define SWAP_FLAG_PREFER	0x8000	/* set if swap priority specified */
05ad79
 #endif
05ad79
@@ -70,7 +81,7 @@ enum {
05ad79
 
05ad79
 static int all;
05ad79
 static int priority = -1;	/* non-prioritized swap by default */
05ad79
-static int discard;
05ad79
+static int discard;		/* don't send swap discards by default */
05ad79
 
05ad79
 /* If true, don't complain if the device/file doesn't exist */
05ad79
 static int ifexists;
05ad79
@@ -567,8 +578,22 @@ static int do_swapon(const char *orig_sp
05ad79
 			   << SWAP_FLAG_PRIO_SHIFT);
05ad79
 	}
05ad79
 #endif
05ad79
-	if (fl_discard)
05ad79
-		flags |= SWAP_FLAG_DISCARD;
05ad79
+	/*
05ad79
+	 * Validate the discard flags passed and set them
05ad79
+	 * accordingly before calling sys_swapon.
05ad79
+	 */
05ad79
+	if (fl_discard && !(fl_discard & ~SWAP_FLAGS_DISCARD_VALID)) {
05ad79
+		/*
05ad79
+		 * If we get here with both discard policy flags set,
05ad79
+		 * we just need to tell the kernel to enable discards
05ad79
+		 * and it will do correctly, just as we expect.
05ad79
+		 */
05ad79
+		if ((fl_discard & SWAP_FLAG_DISCARD_ONCE) &&
05ad79
+		    (fl_discard & SWAP_FLAG_DISCARD_PAGES))
05ad79
+			flags |= SWAP_FLAG_DISCARD;
05ad79
+		else
05ad79
+			flags |= fl_discard;
05ad79
+	}
05ad79
 
05ad79
 	status = swapon(special, flags);
05ad79
 	if (status < 0)
05ad79
@@ -608,12 +633,22 @@ static int swapon_all(void)
05ad79
 	while (mnt_table_find_next_fs(tb, itr, match_swap, NULL, &fs) == 0) {
05ad79
 		/* defaults */
05ad79
 		int pri = priority, dsc = discard, nofail = ifexists;
05ad79
-		char *p, *src;
05ad79
+		char *p, *src, *dscarg;
05ad79
 
05ad79
 		if (mnt_fs_get_option(fs, "noauto", NULL, NULL) == 0)
05ad79
 			continue;
05ad79
-		if (mnt_fs_get_option(fs, "discard", NULL, NULL) == 0)
05ad79
-			dsc = 1;
05ad79
+		if (mnt_fs_get_option(fs, "discard", &dscarg, NULL) == 0) {
05ad79
+			dsc |= SWAP_FLAG_DISCARD;
05ad79
+			if (dscarg) {
05ad79
+				/* only single-time discards are wanted */
05ad79
+				if (strcmp(dscarg, "once") == 0)
05ad79
+					dsc |= SWAP_FLAG_DISCARD_ONCE;
05ad79
+
05ad79
+				/* do discard for every released swap page */
05ad79
+				if (strcmp(dscarg, "pages") == 0)
05ad79
+					dsc |= SWAP_FLAG_DISCARD_PAGES;
05ad79
+			}
05ad79
+		}
05ad79
 		if (mnt_fs_get_option(fs, "nofail", NULL, NULL) == 0)
05ad79
 			nofail = 1;
05ad79
 		if (mnt_fs_get_option(fs, "pri", &p, NULL) == 0 && p)
05ad79
@@ -643,17 +678,17 @@ static void __attribute__ ((__noreturn__
05ad79
 	fprintf(out, _(" %s [options] [<spec>]\n"), program_invocation_short_name);
05ad79
 
05ad79
 	fputs(USAGE_OPTIONS, out);
05ad79
-	fputs(_(" -a, --all              enable all swaps from /etc/fstab\n"
05ad79
-		" -d, --discard          discard freed pages before they are reused\n"
05ad79
-		" -e, --ifexists         silently skip devices that do not exist\n"
05ad79
-		" -f, --fixpgsz          reinitialize the swap space if necessary\n"
05ad79
-		" -p, --priority <prio>  specify the priority of the swap device\n"
05ad79
-		" -s, --summary          display summary about used swap devices\n"
05ad79
-		"     --show[=<columns>] display summary in definable table\n"
05ad79
-		"     --noheadings       don't print headings, use with --show\n"
05ad79
-		"     --raw              use the raw output format, use with --show\n"
05ad79
-		"     --bytes            display swap size in bytes in --show output\n"
05ad79
-		" -v, --verbose          verbose mode\n"), out);
05ad79
+	fputs(_(" -a, --all                enable all swaps from /etc/fstab\n"
05ad79
+		" -d, --discard[=<policy>] enable swap discards, if supported by device\n"
05ad79
+		" -e, --ifexists           silently skip devices that do not exist\n"
05ad79
+		" -f, --fixpgsz            reinitialize the swap space if necessary\n"
05ad79
+		" -p, --priority <prio>    specify the priority of the swap device\n"
05ad79
+		" -s, --summary            display summary about used swap devices\n"
05ad79
+		"     --show[=<columns>]   display summary in definable table\n"
05ad79
+		"     --noheadings         don't print headings, use with --show\n"
05ad79
+		"     --raw                use the raw output format, use with --show\n"
05ad79
+		"     --bytes              display swap size in bytes in --show output\n"
05ad79
+		" -v, --verbose            verbose mode\n"), out);
05ad79
 
05ad79
 	fputs(USAGE_SEPARATOR, out);
05ad79
 	fputs(USAGE_HELP, out);
05ad79
@@ -669,6 +704,11 @@ static void __attribute__ ((__noreturn__
05ad79
 		" <device>               name of device to be used\n"
05ad79
 		" <file>                 name of file to be used\n"), out);
05ad79
 
05ad79
+	fputs(_("\nAvailable discard policy types (for --discard):\n"
05ad79
+		" once	  : only single-time area discards are issued. (swapon)\n"
05ad79
+		" pages	  : discard freed pages before they are reused.\n"
05ad79
+		" * if no policy is selected both discard types are enabled. (default)\n"), out);
05ad79
+
05ad79
 	fputs(_("\nAvailable columns (for --show):\n"), out);
05ad79
 	for (i = 0; i < NCOLS; i++)
05ad79
 		fprintf(out, " %4s  %s\n", infos[i].name, _(infos[i].help));
05ad79
@@ -693,7 +733,7 @@ int main(int argc, char *argv[])
05ad79
 
05ad79
 	static const struct option long_opts[] = {
05ad79
 		{ "priority", 1, 0, 'p' },
05ad79
-		{ "discard",  0, 0, 'd' },
05ad79
+		{ "discard",  2, 0, 'd' },
05ad79
 		{ "ifexists", 0, 0, 'e' },
05ad79
 		{ "summary",  0, 0, 's' },
05ad79
 		{ "fixpgsz",  0, 0, 'f' },
05ad79
@@ -716,7 +756,7 @@ int main(int argc, char *argv[])
05ad79
 	mnt_init_debug(0);
05ad79
 	mntcache = mnt_new_cache();
05ad79
 
05ad79
-	while ((c = getopt_long(argc, argv, "ahdefp:svVL:U:",
05ad79
+	while ((c = getopt_long(argc, argv, "ahd::efp:svVL:U:",
05ad79
 				long_opts, NULL)) != -1) {
05ad79
 		switch (c) {
05ad79
 		case 'a':		/* all */
05ad79
@@ -736,7 +776,18 @@ int main(int argc, char *argv[])
05ad79
 			add_uuid(optarg);
05ad79
 			break;
05ad79
 		case 'd':
05ad79
-			discard = 1;
05ad79
+			discard |= SWAP_FLAG_DISCARD;
05ad79
+			if (optarg) {
05ad79
+				if (*optarg == '=')
05ad79
+					optarg++;
05ad79
+
05ad79
+				if (strcmp(optarg, "once") == 0)
05ad79
+					discard |= SWAP_FLAG_DISCARD_ONCE;
05ad79
+				else if (strcmp(optarg, "pages") == 0)
05ad79
+					discard |= SWAP_FLAG_DISCARD_PAGES;
05ad79
+				else
05ad79
+					errx(EXIT_FAILURE, _("unsupported discard policy: %s"), optarg);
05ad79
+			}
05ad79
 			break;
05ad79
 		case 'e':               /* ifexists */
05ad79
 		        ifexists = 1;