@@ -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
@@ -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);
@@ -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 = {
@@ -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);
@@ -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");
@@ -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;
}
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(-)