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 V4 4/6] libswap: add function to prealloc contiguous file
Date: Wed, 24 Jan 2024 12:45:46 +0800	[thread overview]
Message-ID: <20240124044548.2652626-5-liwang@redhat.com> (raw)
In-Reply-To: <20240124044548.2652626-1-liwang@redhat.com>

The improve makes several key updates to the swap file handling
in the libswap.c file:

It incorporates support for the Btrfs filesystem, which is now
recognized as a valid filesystem for swap files.

A new function, set_nocow_attr, is added to apply the FS_NOCOW_FL
flag to files on Btrfs filesystems.

Introduces a new prealloc_contiguous_file function. This method
preallocates a contiguous block of space for the swap file during
its creation, rather than filling the file with data as was done
previously.

Modifications to the make_swapfile function are made to utilize
prealloc_contiguous_file for creating the swap file, ensuring
the file is created with contiguous space on the filesystem.

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

diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
index 8c2ce6cd7..e606e3f03 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
@@ -13,6 +14,7 @@
 #include "lapi/syscalls.h"
 
 static const char *const swap_supported_fs[] = {
+	"btrfs",
 	"ext2",
 	"ext3",
 	"ext4",
@@ -23,6 +25,50 @@ static const char *const swap_supported_fs[] = {
 	NULL
 };
 
+static void set_nocow_attr(const char *filename)
+{
+	int fd;
+	int attrs;
+
+	tst_res(TINFO, "FS_NOCOW_FL attribute set on %s", filename);
+
+	fd = SAFE_OPEN(filename, O_RDONLY);
+
+	SAFE_IOCTL(fd, FS_IOC_GETFLAGS, &attrs);
+
+	attrs |= FS_NOCOW_FL;
+
+	SAFE_IOCTL(fd, FS_IOC_SETFLAGS, &attrs);
+
+	SAFE_CLOSE(fd);
+}
+
+static int prealloc_contiguous_file(const char *path, size_t bs, size_t bcount)
+{
+	int fd;
+
+	fd = open(path, O_CREAT|O_WRONLY|O_TRUNC, 0600);
+	if (fd < 0)
+		return -1;
+
+	/* Btrfs file need set 'nocow' attribute */
+	if (tst_fs_type(path) == TST_BTRFS_MAGIC)
+		set_nocow_attr(path);
+
+	if (tst_prealloc_size_fd(fd, bs, bcount)) {
+		close(fd);
+		unlink(path);
+		return -1;
+	}
+
+	if (close(fd) < 0) {
+		unlink(path);
+		return -1;
+	}
+
+	return 0;
+}
+
 /*
  * Make a swap file
  */
@@ -32,9 +78,15 @@ int make_swapfile(const char *swapfile, int safe)
 		tst_brk(TBROK, "Insufficient disk space to create swap file");
 
 	/* create file */
-	if (tst_fill_file(swapfile, 0, sysconf(_SC_PAGESIZE), 10) != 0)
+	if (prealloc_contiguous_file(swapfile, sysconf(_SC_PAGESIZE), 10) != 0)
 		tst_brk(TBROK, "Failed to create swapfile");
 
+	/* Full the file to make old xfs happy*/
+	if (tst_fs_type(swapfile) == TST_XFS_MAGIC) {
+		if (tst_fill_file(swapfile, 0, sysconf(_SC_PAGESIZE), 10) != 0)
+			tst_brk(TBROK, "Failed to create swapfile");
+	}
+
 	/* make the file swapfile */
 	const char *argv[2 + 1];
 
-- 
2.40.1


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

  parent reply	other threads:[~2024-01-24  4:47 UTC|newest]

Thread overview: 16+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-01-24  4:45 [LTP] [PATCH V4 0/6] improvement work on libswap library Li Wang
2024-01-24  4:45 ` [LTP] [PATCH V4 1/6] libswap: add known swap supported fs check Li Wang
2024-01-24  4:45 ` [LTP] [PATCH V4 2/6] swapon01: Test on all filesystems Li Wang
2024-01-24  4:45 ` [LTP] [PATCH V4 3/6] swapon01: Improving test with memory limits and swap reporting Li Wang
2024-01-24  4:45 ` Li Wang [this message]
2024-01-24  4:45 ` [LTP] [PATCH V4 5/6] libswap: Introduce file contiguity check Li Wang
2024-01-24 18:17   ` Petr Vorel
2024-01-24  4:45 ` [LTP] [PATCH V4 6/6] swapoff01: make use of make_swapfile Li Wang
2024-01-24 19:22 ` [LTP] [PATCH V4 0/6] improvement work on libswap library Petr Vorel
2024-01-25 10:23   ` Li Wang
2024-01-26  8:26     ` Yang Xu (Fujitsu)
2024-01-26 10:05     ` Cyril Hrubis
2024-01-26 10:34       ` Li Wang
2024-01-26 10:39         ` Petr Vorel
2024-01-26 10:46           ` Cyril Hrubis
2024-01-26 11:29             ` 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=20240124044548.2652626-5-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