[LTP] [PATCH v5 6/8] libswap: customize swapfile size

Li Wang
2024-01-28T02:50:48Z

The key change is the modification of the make_swapfile function to
accept an additional parameter blocks, specifying the number of
blocks to allocate for the swap file. This change allows for more
granular control over the size of swap files created during tests.

Signed-off-by: Li Wang <liwang@redhat.com>
---
include/libswap.h | 2 +-
libs/libltpswap/libswap.c | 23 +++++++++++++------
testcases/kernel/syscalls/swapoff/swapoff01.c | 5 +---
testcases/kernel/syscalls/swapon/swapon01.c | 2 +-
testcases/kernel/syscalls/swapon/swapon02.c | 4 ++--
testcases/kernel/syscalls/swapon/swapon03.c | 4 ++--
6 files changed, 23 insertions(+), 17 deletions(-)

diff --git a/include/libswap.h b/include/libswap.h
index d4b5301a5..e67d65756 100644
--- a/include/libswap.h
+++ b/include/libswap.h
@@ -14,7 +14,7 @@
/*
* Make a swap file
*/
-int make_swapfile(const char *swapfile, int safe);
+int make_swapfile(const char *swapfile, int blocks, int safe);

/*
* Check swapon/swapoff support status of filesystems or files
diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
index 95d4f59b0..8aecad48d 100644
--- a/libs/libltpswap/libswap.c
+++ b/libs/libltpswap/libswap.c
@@ -4,6 +4,7 @@
* Author: Stanislav Kholmanskikh <stanislav.kholmanskikh@oracle.com>
*/

+#include <sys/statvfs.h>
#include <linux/fs.h>
#include <errno.h>
#include <linux/fiemap.h>
@@ -129,19 +130,27 @@ out:
/*
* Make a swap file
*/
-int make_swapfile(const char *swapfile, int safe)
+int make_swapfile(const char *swapfile, int blocks, int safe)
{
- if (!tst_fs_has_free(".", sysconf(_SC_PAGESIZE) * 10, TST_BYTES))
+ struct statvfs fs_info;
+ unsigned long blk_size;
+
+ if (statvfs(".", &fs_info) == -1)
+ return -1;
+
+ blk_size = fs_info.f_bsize;
+
+ if (!tst_fs_has_free(".", blk_size * blocks, TST_BYTES))
tst_brk(TBROK, "Insufficient disk space to create swap file");

/* create file */
- if (prealloc_contiguous_file(swapfile, sysconf(_SC_PAGESIZE), 10) != 0)
+ if (prealloc_contiguous_file(swapfile, blk_size, blocks) != 0)
tst_brk(TBROK, "Failed to create swapfile");

- /* Full the file to make old xfs happy*/
+ /* Fill the file if needed (specific to old xfs filesystems) */
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");
+ if (tst_fill_file(swapfile, 0, blk_size, blocks) != 0)
+ tst_brk(TBROK, "Failed to fill swapfile");
}

/* make the file swapfile */
@@ -162,7 +171,7 @@ int make_swapfile(const char *swapfile, int safe)
void is_swap_supported(const char *filename)
{
int i, sw_support = 0;
- int ret = make_swapfile(filename, 1);
+ int ret = make_swapfile(filename, 10, 1);
int fi_contiguous = file_is_contiguous(filename);
long fs_type = tst_fs_type(filename);
const char *fstype = tst_fs_type_name(fs_type);
diff --git a/testcases/kernel/syscalls/swapoff/swapoff01.c b/testcases/kernel/syscalls/swapoff/swapoff01.c
index b27eecdad..e3b445d05 100644
--- a/testcases/kernel/syscalls/swapoff/swapoff01.c
+++ b/testcases/kernel/syscalls/swapoff/swapoff01.c
@@ -44,11 +44,8 @@ static void setup(void)
tst_brk(TBROK,
"Insufficient disk space to create swap file");

- if (tst_fill_file("swapfile01", 0x00, 1024, 65536))
+ if (make_swapfile("swapfile01", 65536, 1))
tst_brk(TBROK, "Failed to create file for swap");
-
- if (system("mkswap swapfile01 > tmpfile 2>&1") != 0)
- tst_brk(TBROK, "Failed to make swapfile");
}

static struct tst_test test = {
diff --git a/testcases/kernel/syscalls/swapon/swapon01.c b/testcases/kernel/syscalls/swapon/swapon01.c
index a74a5171e..d406e4bd9 100644
--- a/testcases/kernel/syscalls/swapon/swapon01.c
+++ b/testcases/kernel/syscalls/swapon/swapon01.c
@@ -38,7 +38,7 @@ static void verify_swapon(void)
static void setup(void)
{
is_swap_supported(SWAP_FILE);
- make_swapfile(SWAP_FILE, 0);
+ make_swapfile(SWAP_FILE, 10, 0);

SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
diff --git a/testcases/kernel/syscalls/swapon/swapon02.c b/testcases/kernel/syscalls/swapon/swapon02.c
index fceea77be..f5b0d6d56 100644
--- a/testcases/kernel/syscalls/swapon/swapon02.c
+++ b/testcases/kernel/syscalls/swapon/swapon02.c
@@ -44,8 +44,8 @@ static void setup(void)
is_swap_supported("./tstswap");

SAFE_TOUCH("notswap", 0777, NULL);
- make_swapfile("swapfile01", 0);
- make_swapfile("alreadyused", 0);
+ make_swapfile("swapfile01", 10, 0);
+ make_swapfile("alreadyused", 10, 0);

if (tst_syscall(__NR_swapon, "alreadyused", 0))
tst_res(TWARN | TERRNO, "swapon(alreadyused) failed");
diff --git a/testcases/kernel/syscalls/swapon/swapon03.c b/testcases/kernel/syscalls/swapon/swapon03.c
index dc633ebc6..e13009111 100644
--- a/testcases/kernel/syscalls/swapon/swapon03.c
+++ b/testcases/kernel/syscalls/swapon/swapon03.c
@@ -155,7 +155,7 @@ static int setup_swap(void)
}

/* Create the swapfile */
- make_swapfile(filename, 0);
+ make_swapfile(filename, 10, 0);

/* turn on the swap file */
res = tst_syscall(__NR_swapon, filename, 0);
@@ -178,7 +178,7 @@ static int setup_swap(void)

/* Create all needed extra swapfiles for testing */
for (j = 0; j < testfiles; j++)
- make_swapfile(swap_testfiles[j].filename, 0);
+ make_swapfile(swap_testfiles[j].filename, 10, 0);

return 0;
}