diff mbox series

[fstools,2/4] fstools: block: support discard in block

Message ID 1761795122e3e3d4b00dee52b1570a3e8500cf48.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>

Intorduce a new fstab.@swap[] attribute, "discard" that can take values
- on, 1, yes -- to enable the default discard mode
- once, pages -- to enable the corresponding discard policy
- anythinge else -- to disable discard
Discard is enabled by default.

config swap
	option enabled '1'
	option label 'swap'
	option discard '1'

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

Patch

diff --git a/block.c b/block.c
index 4553ee2..c5e54fc 100644
--- a/block.c
+++ b/block.c
@@ -153,6 +153,7 @@  enum {
 	SWAP_LABEL,
 	SWAP_DEVICE,
 	SWAP_PRIO,
+	SWAP_DISCARD,
 	__SWAP_MAX
 };
 
@@ -162,6 +163,7 @@  static const struct blobmsg_policy swap_policy[__SWAP_MAX] = {
 	[SWAP_LABEL] = { .name = "label", .type = BLOBMSG_TYPE_STRING },
 	[SWAP_DEVICE] = { .name = "device", .type = BLOBMSG_TYPE_STRING },
 	[SWAP_PRIO] = { .name = "priority", .type = BLOBMSG_TYPE_INT32 },
+	[SWAP_DISCARD] = { .name = "discard", .type = BLOBMSG_TYPE_STRING },
 };
 
 static const struct uci_blob_param_list swap_attr_list = {
@@ -317,6 +319,7 @@  static int swap_add(struct uci_section *s)
 {
 	struct blob_attr *tb[__SWAP_MAX] = { 0 };
 	struct mount *m;
+	char *discard;
 
         blob_buf_init(&b, 0);
 	uci_to_blob(&b, s, &swap_attr_list);
@@ -331,11 +334,23 @@  static int swap_add(struct uci_section *s)
 	m->uuid = blobmsg_get_strdup(tb[SWAP_UUID]);
 	m->label = blobmsg_get_strdup(tb[SWAP_LABEL]);
 	m->device = blobmsg_get_basename(tb[SWAP_DEVICE]);
+	discard = blobmsg_get_strdup(tb[SWAP_DISCARD]);
+
 	if (tb[SWAP_PRIO])
 		m->prio = blobmsg_get_u32(tb[SWAP_PRIO]);
 	if (m->prio)
 		m->prio = ((m->prio << SWAP_FLAG_PRIO_SHIFT) & SWAP_FLAG_PRIO_MASK) | SWAP_FLAG_PREFER;
 
+	if (discard) {
+		if (!strcmp(discard, "yes") || !strcmp(discard, "1") || !strcmp(discard, "on"))
+			 m->prio |= SWAP_FLAG_DISCARD;
+		else if (!strcmp(discard, "pages"))
+			 m->prio |= (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_PAGES);
+		else if (!strcmp(discard, "once"))
+			 m->prio |= (SWAP_FLAG_DISCARD | SWAP_FLAG_DISCARD_ONCE);
+	} else
+		 m->prio |= SWAP_FLAG_DISCARD;
+
 	if ((!tb[SWAP_ENABLE]) || blobmsg_get_u32(tb[SWAP_ENABLE])) {
 		/* store complete swap path */
 		if (tb[SWAP_DEVICE])