diff mbox series

[fstools,3/4] fstools: block: add discard support to swapon

Message ID bd385ffd022bbf3dcdf4ff68c9cc0ba8d3982ca4.1753203829.git.m.storchak@gmail.com
State New
Headers show
Series Support discard in "block" and "swapon" | expand

Commit Message

Maxim Storchak July 23, 2025, 1:19 p.m. UTC
From: Maxim Storchak <m.storchak@gmail.com>

Add a new option, -d, with an optional argument "once" or "pages".
-d with no arguments uses the default kernel discarding policy which enables
both the initial discard (once) and discards the released swap pages.
By default discard is disabled.
Inspired by busybox's swaponoff.c

Signed-off-by: Maxim Storchak <m.storchak@gmail.com>
---
 block.c | 36 ++++++++++++++++++++++++++++--------
 1 file changed, 28 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/block.c b/block.c
index c5e54fc..0d9129b 100644
--- a/block.c
+++ b/block.c
@@ -1842,11 +1842,12 @@  static int main_info(int argc, char **argv)
 
 static int swapon_usage(void)
 {
-	fprintf(stderr, "Usage: swapon [-s] [-a] [[-p pri] DEVICE]\n\n"
-		"\tStart swapping on [DEVICE]\n"
-		" -a\tStart swapping on all swap devices\n"
-		" -p pri\tSet priority of swap device\n"
-		" -s\tShow summary\n");
+	fprintf(stderr, "Usage: swapon [-s] [-a] [ [[-p pri] [-d [once|pages]] DEVICE] ]\n\n"
+		"\t\t\tStart swapping on [DEVICE]\n"
+		" -a\t\t\tStart swapping on all swap devices\n"
+		" -p pri\t\t\tSet priority of swap device\n"
+		" -d [once|pages]\tEnable discard, using 'once', 'pages' or both (default) discard policy\n"
+		" -s\t\t\tShow summary\n");
 	return -1;
 }
 
@@ -1862,12 +1863,11 @@  static int main_swapon(int argc, char **argv)
 	struct stat st;
 	int err;
 
-	while ((ch = getopt(argc, argv, "ap:s")) != -1) {
+	while ((ch = getopt(argc, argv, "ap:sd::")) != -1) {
 		switch(ch) {
 		case 's':
 			fp = fopen("/proc/swaps", "r");
 			lineptr = NULL;
-
 			if (!fp) {
 				ULOG_ERR("failed to open /proc/swaps\n");
 				return -1;
@@ -1890,7 +1890,27 @@  static int main_swapon(int argc, char **argv)
 		case 'p':
 			pri = atoi(optarg);
 			if (pri >= 0)
-				flags = ((pri << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
+				flags |= ((pri << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
+			break;
+		case 'd':
+			lineptr = NULL;
+			flags |= SWAP_FLAG_DISCARD;
+			if (optarg)
+				lineptr = optarg;
+			else if (optind == (argc - 1))
+				break;
+			else if (optind < argc && argv[optind][0] != '-') {
+				lineptr = argv[optind];
+				optind++;
+			}
+			if (lineptr) {
+				if (!strcmp(lineptr, "once"))
+					flags |= SWAP_FLAG_DISCARD_ONCE;
+				else if (!strcmp(lineptr, "pages"))
+					flags |= SWAP_FLAG_DISCARD_PAGES;
+				else
+					return swapon_usage();
+			}
 			break;
 		default:
 			return swapon_usage();