Attached are a series of three patches affecting swaponoff.

Patch #1 fixes two small logic errors: (a) the bb_strtou function was called 
inside an instantiation of the MIN macro, which would cause the function to be 
called twice in the common case; and (b) the actual maximum allowable swap 
priority is SWAP_FLAG_PRIO_MASK >> SWAP_FLAG_PRIO_SHIFT, not simply 
SWAP_FLAG_PRIO_MASK as the code previously assumed. It just so happens that 
SWAP_FLAG_PRIO_SHIFT == 0, but one should not rely on that serendipity.

Patch #2 fixes the interaction of the -a and -p options to swapon. When -p is 
given on the command line, the specified priority should be applied to any swap 
areas given in /etc/fstab that lack a 'pri' option. This is the way swapon from 
util-linux does it, and it's the rational behavior, or else -p and -a should be 
mutually exclusive options.

Patch #3 adds a -d option to swapon, which sets SWAP_FLAG_DISCARD and 
potentially SWAP_FLAG_DISCARD_ONCE or SWAP_FLAG_DISCARD_PAGES if a policy is 
given. The patch also adds support for the 'discard' option in swap entries in 
/etc/fstab.
>From 1509caf8e5088ed92ae6c7ab54d65785df570351 Mon Sep 17 00:00:00 2001
From: Matt Whitlock <busy...@mattwhitlock.name>
Date: Sat, 22 Mar 2014 18:54:24 -0400
Subject: [PATCH 1/3] avoid calling bb_strtou twice in MIN macro expansion

Also, the maximum allowable value of swap priority is technically SWAP_FLAG_PRIO_MASK >> SWAP_FLAG_PRIO_SHIFT.
---
 util-linux/swaponoff.c | 13 ++++++++-----
 1 file changed, 8 insertions(+), 5 deletions(-)

diff --git a/util-linux/swaponoff.c b/util-linux/swaponoff.c
index 3f22334..d35e30e 100644
--- a/util-linux/swaponoff.c
+++ b/util-linux/swaponoff.c
@@ -100,12 +100,12 @@ static int do_em_all(void)
 				g_flags = 0; /* each swap space might have different flags */
 				p = hasmntopt(m, "pri");
 				if (p) {
-					/* Max allowed 32767 (==SWAP_FLAG_PRIO_MASK) */
-					unsigned int swap_prio = MIN(bb_strtou(p + 4 , NULL, 10), SWAP_FLAG_PRIO_MASK);
+					/* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK >> SWAP_FLAG_PRIO_SHIFT) */
+					unsigned prio = bb_strtou(p + 4, NULL, 10);
 					/* We want to allow "NNNN,foo", thus errno == EINVAL is allowed too */
 					if (errno != ERANGE) {
 						g_flags = SWAP_FLAG_PREFER |
-							(swap_prio << SWAP_FLAG_PRIO_SHIFT);
+							MIN(prio, SWAP_FLAG_PRIO_MASK >> SWAP_FLAG_PRIO_SHIFT) << SWAP_FLAG_PRIO_SHIFT;
 					}
 				}
 #endif
@@ -124,6 +124,9 @@ int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
 {
 	int ret;
+#if ENABLE_FEATURE_SWAPON_PRI
+	unsigned prio;
+#endif
 
 	INIT_G();
 
@@ -132,11 +135,11 @@ int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
 #else
 	if (applet_name[5] == 'n')
 		opt_complementary = "p+";
-	ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &g_flags);
+	ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &prio);
 
 	if (ret & 2) { // -p
 		g_flags = SWAP_FLAG_PREFER |
-			((g_flags & SWAP_FLAG_PRIO_MASK) << SWAP_FLAG_PRIO_SHIFT);
+			MIN(prio, SWAP_FLAG_PRIO_MASK >> SWAP_FLAG_PRIO_SHIFT) << SWAP_FLAG_PRIO_SHIFT;
 		ret &= 1;
 	}
 #endif
-- 
1.9.1

>From e7d0d02ca14aea0e12c2ca6b84e124db25172209 Mon Sep 17 00:00:00 2001
From: Matt Whitlock <busy...@mattwhitlock.name>
Date: Sat, 22 Mar 2014 19:10:08 -0400
Subject: [PATCH 2/3] fix interaction of -a and -p options in swapon

Swap entries in /etc/fstab inherit the priority specified on the command line unless they have 'pri' in their mount options.
---
 util-linux/swaponoff.c | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/util-linux/swaponoff.c b/util-linux/swaponoff.c
index d35e30e..df0c524 100644
--- a/util-linux/swaponoff.c
+++ b/util-linux/swaponoff.c
@@ -82,6 +82,9 @@ static int do_em_all(void)
 	struct mntent *m;
 	FILE *f;
 	int err;
+#ifdef G
+	int cl_flags = g_flags;
+#endif
 
 	f = setmntent("/etc/fstab", "r");
 	if (f == NULL)
@@ -97,14 +100,14 @@ static int do_em_all(void)
 			) {
 #if ENABLE_FEATURE_SWAPON_PRI
 				char *p;
-				g_flags = 0; /* each swap space might have different flags */
+				g_flags = cl_flags; /* each swap space might have different flags */
 				p = hasmntopt(m, "pri");
 				if (p) {
 					/* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK >> SWAP_FLAG_PRIO_SHIFT) */
 					unsigned prio = bb_strtou(p + 4, NULL, 10);
 					/* We want to allow "NNNN,foo", thus errno == EINVAL is allowed too */
 					if (errno != ERANGE) {
-						g_flags = SWAP_FLAG_PREFER |
+						g_flags = (g_flags & ~SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER |
 							MIN(prio, SWAP_FLAG_PRIO_MASK >> SWAP_FLAG_PRIO_SHIFT) << SWAP_FLAG_PRIO_SHIFT;
 					}
 				}
-- 
1.9.1

>From b2b2e65829785c959881d643bcff8843a6cddf7e Mon Sep 17 00:00:00 2001
From: Matt Whitlock <busy...@mattwhitlock.name>
Date: Sat, 22 Mar 2014 19:21:01 -0400
Subject: [PATCH 3/3] add discard option -d to swapon

---
 util-linux/Config.src  |  9 ++++++
 util-linux/swaponoff.c | 85 ++++++++++++++++++++++++++++++++++++++++++++------
 2 files changed, 85 insertions(+), 9 deletions(-)

diff --git a/util-linux/Config.src b/util-linux/Config.src
index 5a8b006..c1cd6da 100644
--- a/util-linux/Config.src
+++ b/util-linux/Config.src
@@ -599,6 +599,15 @@ config SWAPONOFF
 	  space. If you are not using any swap space, you can leave this
 	  option disabled.
 
+config FEATURE_SWAPON_DISCARD
+	bool "Support discard option -d"
+	default y
+	depends on SWAPONOFF
+	help
+	  Enable support for discarding swap area blocks at swapon and/or as
+	  the kernel frees them. This option enables both the -d option on
+	  'swapon' and the 'discard' option for swap entries in /etc/fstab.
+
 config FEATURE_SWAPON_PRI
 	bool "Support priority option -p"
 	default y
diff --git a/util-linux/swaponoff.c b/util-linux/swaponoff.c
index df0c524..0080ea7c 100644
--- a/util-linux/swaponoff.c
+++ b/util-linux/swaponoff.c
@@ -8,10 +8,14 @@
  */
 
 //usage:#define swapon_trivial_usage
-//usage:       "[-a]" IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
+//usage:       "[-a]" IF_FEATURE_SWAPON_DISCARD(" [-d[POL]]") IF_FEATURE_SWAPON_PRI(" [-p PRI]") " [DEVICE]"
 //usage:#define swapon_full_usage "\n\n"
 //usage:       "Start swapping on DEVICE\n"
 //usage:     "\n	-a	Start swapping on all swap devices"
+//usage:	IF_FEATURE_SWAPON_DISCARD(
+//usage:     "\n	-d[POL]	Discard blocks at swapon (POL=once),"
+//usage:     "\n		as freed (POL=pages), or both (POL omitted)"
+//usage:	)
 //usage:	IF_FEATURE_SWAPON_PRI(
 //usage:     "\n	-p PRI	Set swap device priority"
 //usage:	)
@@ -38,7 +42,22 @@
 # define MNTTYPE_SWAP "swap"
 #endif
 
-#if ENABLE_FEATURE_SWAPON_PRI
+#if ENABLE_FEATURE_SWAPON_DISCARD
+#ifndef SWAP_FLAG_DISCARD
+#define SWAP_FLAG_DISCARD 0x10000
+#endif
+#ifndef SWAP_FLAG_DISCARD_ONCE
+#define SWAP_FLAG_DISCARD_ONCE 0x20000
+#endif
+#ifndef SWAP_FLAG_DISCARD_PAGES
+#define SWAP_FLAG_DISCARD_PAGES 0x40000
+#endif
+#define SWAP_FLAG_DISCARD_MASK \
+	(SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE | SWAP_FLAG_DISCARD_PAGES)
+#endif
+
+
+#if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
 struct globals {
 	int flags;
 } FIX_ALIASING;
@@ -98,9 +117,23 @@ static int do_em_all(void)
 			if (applet_name[5] != 'n'
 			 || hasmntopt(m, MNTOPT_NOAUTO) == NULL
 			) {
-#if ENABLE_FEATURE_SWAPON_PRI
+#if ENABLE_FEATURE_SWAPON_DISCARD || ENABLE_FEATURE_SWAPON_PRI
 				char *p;
 				g_flags = cl_flags; /* each swap space might have different flags */
+#if ENABLE_FEATURE_SWAPON_DISCARD
+				p = hasmntopt(m, "discard");
+				if (p) {
+					if (p[7] == '=') {
+						if (strncmp(p + 8, "once", 4) == 0 && (p[12] == ',' || p[12] == '\0'))
+							g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE;
+						else if (strncmp(p + 8, "pages", 5) == 0 && (p[13] == ',' || p[13] == '\0'))
+							g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_PAGES;
+					}
+					else if (p[7] == ',' || p[7] == '\0')
+						g_flags = (g_flags & ~SWAP_FLAG_DISCARD_MASK) | SWAP_FLAG_DISCARD;
+				}
+#endif
+#if ENABLE_FEATURE_SWAPON_PRI
 				p = hasmntopt(m, "pri");
 				if (p) {
 					/* Max allowed 32767 (== SWAP_FLAG_PRIO_MASK >> SWAP_FLAG_PRIO_SHIFT) */
@@ -112,6 +145,7 @@ static int do_em_all(void)
 					}
 				}
 #endif
+#endif
 				err += swap_enable_disable(m->mnt_fsname);
 			}
 		}
@@ -127,24 +161,57 @@ int swap_on_off_main(int argc, char **argv) MAIN_EXTERNALLY_VISIBLE;
 int swap_on_off_main(int argc UNUSED_PARAM, char **argv)
 {
 	int ret;
+#if ENABLE_FEATURE_SWAPON_DISCARD
+	char *discard = NULL;
+#endif
 #if ENABLE_FEATURE_SWAPON_PRI
 	unsigned prio;
 #endif
 
 	INIT_G();
 
-#if !ENABLE_FEATURE_SWAPON_PRI
+#if !ENABLE_FEATURE_SWAPON_DISCARD && !ENABLE_FEATURE_SWAPON_PRI
 	ret = getopt32(argv, "a");
 #else
+#if ENABLE_FEATURE_SWAPON_PRI
 	if (applet_name[5] == 'n')
 		opt_complementary = "p+";
-	ret = getopt32(argv, (applet_name[5] == 'n') ? "ap:" : "a", &prio);
+#endif
+	ret = getopt32(argv, (applet_name[5] == 'n') ?
+#if ENABLE_FEATURE_SWAPON_DISCARD
+		"d::"
+#endif
+#if ENABLE_FEATURE_SWAPON_PRI
+		"p:"
+#endif
+		"a" : "a"
+#if ENABLE_FEATURE_SWAPON_DISCARD
+		, &discard
+#endif
+#if ENABLE_FEATURE_SWAPON_PRI
+		, &prio
+#endif
+		);
+#endif
 
-	if (ret & 2) { // -p
-		g_flags = SWAP_FLAG_PREFER |
-			MIN(prio, SWAP_FLAG_PRIO_MASK >> SWAP_FLAG_PRIO_SHIFT) << SWAP_FLAG_PRIO_SHIFT;
-		ret &= 1;
+#if ENABLE_FEATURE_SWAPON_DISCARD
+	if (ret & 1) { // -d
+		if (!discard)
+			g_flags |= SWAP_FLAG_DISCARD;
+		else if (strcmp(discard, "once") == 0)
+			g_flags |= SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE;
+		else if (strcmp(discard, "pages") == 0)
+			g_flags |= SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_PAGES;
+		else
+			bb_show_usage();
 	}
+	ret >>= 1;
+#endif
+#if ENABLE_FEATURE_SWAPON_PRI
+	if (ret & 1) // -p
+		g_flags |= SWAP_FLAG_PREFER |
+			MIN(prio, SWAP_FLAG_PRIO_MASK >> SWAP_FLAG_PRIO_SHIFT) << SWAP_FLAG_PRIO_SHIFT;
+	ret >>= 1;
 #endif
 
 	if (ret /* & 1: not needed */) // -a
-- 
1.9.1

_______________________________________________
busybox mailing list
busybox@busybox.net
http://lists.busybox.net/mailman/listinfo/busybox

Reply via email to