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