public inbox for ltp@lists.linux.it
 help / color / mirror / Atom feed
From: Li Wang <liwang@redhat.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH 4/4] libswap: add Btrfs noCOW attribute setting for swap files
Date: Mon, 22 Jan 2024 15:29:48 +0800	[thread overview]
Message-ID: <20240122072948.2568801-4-liwang@redhat.com> (raw)
In-Reply-To: <20240122072948.2568801-1-liwang@redhat.com>

The patch aims to ensure swap files on Btrfs filesystems are created
with the appropriate FS_NOCOW_FL attribute, which is necessary to
disable CoW (Copy-on-Write) for swap files, perthe btrfs(5) manual page.
This change is gated behind a kernel version check to ensure compatibility
with the system's capabilities.

Signed-off-by: Li Wang <liwang@redhat.com>
---
 libs/libltpswap/libswap.c | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
index 623f2fb3c..0b1d9a227 100644
--- a/libs/libltpswap/libswap.c
+++ b/libs/libltpswap/libswap.c
@@ -4,6 +4,7 @@
  * Author: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
  */
 
+#include <linux/fs.h>
 #include <errno.h>
 
 #define TST_NO_DEFAULT_MAIN
@@ -23,11 +24,37 @@ static const char *const swap_supported_fs[] = {
 	NULL
 };
 
+static void set_nocow_attr(const char *filename)
+{
+	int fd;
+	int attrs;
+
+	fd = SAFE_OPEN(filename, O_RDONLY);
+
+	if (ioctl(fd, FS_IOC_GETFLAGS, &attrs) == -1) {
+		tst_res(TFAIL | TERRNO, "Error getting attributes");
+		close(fd);
+		return;
+	}
+
+	attrs |= FS_NOCOW_FL;
+
+	if (ioctl(fd, FS_IOC_SETFLAGS, &attrs) == -1)
+		tst_res(TFAIL | TERRNO, "Error setting FS_NOCOW_FL attribute");
+	else
+		tst_res(TINFO, "FS_NOCOW_FL attribute set on %s\n", filename);
+
+	close(fd);
+}
+
 /*
  * Make a swap file
  */
 int make_swapfile(const char *swapfile, int safe)
 {
+	long fs_type = tst_fs_type(swapfile);
+	const char *fstype = tst_fs_type_name(fs_type);
+
 	if (!tst_fs_has_free(".", sysconf(_SC_PAGESIZE) * 10, TST_BYTES))
 		tst_brk(TBROK, "Insufficient disk space to create swap file");
 
@@ -35,6 +62,14 @@ int make_swapfile(const char *swapfile, int safe)
 	if (tst_fill_file(swapfile, 0, sysconf(_SC_PAGESIZE), 10) != 0)
 		tst_brk(TBROK, "Failed to create swapfile");
 
+	/* Btrfs file need set 'nocow' attribute */
+	if (strcmp(fstype, "btrfs") == 0) {
+		if (tst_kvercmp(5, 0, 0) > 0)
+			set_nocow_attr(swapfile);
+		else
+			tst_brk(TCONF, "Swapfile on %s not implemented", fstype);
+	}
+
 	/* make the file swapfile */
 	const char *argv[2 + 1];
 	argv[0] = "mkswap";
-- 
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp

  parent reply	other threads:[~2024-01-22  7:31 UTC|newest]

Thread overview: 26+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-22  7:29 [LTP] [PATCH 1/4] libswap: add known swap supported fs check Li Wang
2024-01-22  7:29 ` [LTP] [PATCH 2/4] swapon01: Test on all filesystems Li Wang
2024-01-22  7:29 ` [LTP] [PATCH 3/4] swapon01: Improving test with memory limits and swap reporting Li Wang
2024-01-22  9:08   ` Petr Vorel
2024-01-22  7:29 ` Li Wang [this message]
2024-01-22  8:40   ` [LTP] [PATCH 4/4] libswap: add Btrfs noCOW attribute setting for swap files Petr Vorel
2024-01-22  8:47     ` Petr Vorel
2024-01-22  9:20       ` Li Wang
2024-01-22  9:04     ` Li Wang
2024-01-22  9:03 ` [LTP] [PATCH 1/4] libswap: add known swap supported fs check Petr Vorel
2024-01-22  9:12   ` Li Wang
2024-01-22 10:57     ` Petr Vorel
2024-01-22  9:41   ` Petr Vorel
2024-01-22 10:22     ` Li Wang
2024-01-22  9:13 ` Petr Vorel
2024-01-22  9:32   ` Li Wang
2024-01-22 11:03     ` Petr Vorel
2024-01-22 14:23       ` Li Wang
2024-01-22 20:23         ` Petr Vorel
2024-01-23  5:55           ` Li Wang
2024-01-23  7:30             ` Li Wang
2024-01-23  7:48               ` Petr Vorel
2024-01-23 12:04                 ` Li Wang
2024-01-23 12:31                   ` Petr Vorel
2024-01-23 13:10                     ` Li Wang
2024-01-23 15:45                       ` Petr Vorel

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20240122072948.2568801-4-liwang@redhat.com \
    --to=liwang@redhat.com \
    --cc=ltp@lists.linux.it \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox