Skip to contentSkip to conversation

improvement work on libswap library

PATCH Superseded Li Wang 2 participants 14 messages Jan 23, 2024 – Jan 24, 2024

v3 of 6 Superseded Latest is v6 →

lore
Share & tools
lore

Li Wang

author
#0 lore raw

v3 cover improvement work on libswap library

v2-->v3:
* replace strcmp() by strstr() becuase ext2/ext3/ext4 has same type name.
* add a dedicated function to create the contiguous file for swapon
* introduce set_nocow_attr to set 'NOCOW' attr for btrfs
* replace the kernel-version-check by variable fi_contiguous
* make use of SAFE_MACORs
* fix code incident to make check-libswap happy

Note: as change the return of is_swap_supported will affect more
tests in swapon/off, I didn't replace tst_brk by tst_res+return in this patchset, we can do it later in separate work.

Test env:

	Fedora38(x86_64, Btrfs), RHEL9(s390x, XFS), RHEL8(aarch64, XFS)

Li Wang (4):
libswap: add known swap supported fs check
swapon01: Improving test with memory limits and swap reporting
libswap: add function to prealloc contiguous file
libswap: Introduce file contiguity check

Petr Vorel (1):
swapon01: Test on all filesystems

 libs/libltpswap/libswap.c                   | 130 +++++++++++++++++++-
 testcases/kernel/syscalls/swapon/swapon01.c |  16 ++-
 2 files changed, 139 insertions(+), 7 deletions(-)
Signature · 5 lines
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#1 lore raw

PATCH v3 1/5 libswap: add known swap supported fs check

This introduce an enhancement to the library's is_swap_supported function to check for filesystem compatibility before attempting to create and enable a swap file. A list of supported filesystems is added (ext2, ext3, ext4, xfs, vfat, exfat, ntfs), and a check against this list is performed to ensure that the swap operations are only attempted on known compatible filesystems.

If the make_swapfile function fails, the error handling is now more descriptive: it distinguishes between failures due to the filesystem not supporting swap files and other types of failures. Similarly, when attempting to enable the swap file with swapon, the patch ensures that clearer error messages are provided in cases where the operation is not supported by the filesystem.

---
 libs/libltpswap/libswap.c | 25 +++++++++++++++++++++++--
 1 file changed, 23 insertions(+), 2 deletions(-)
libs/libltpswap/libswap.c +23 -2
index 13610709e..8c2ce6cd7 100644
@@ -12,6 +12,17 @@
 #include "libswap.h"
 #include "lapi/syscalls.h"
 
+static const char *const swap_supported_fs[] = {
+	"ext2",
+	"ext3",
+	"ext4",
+	"xfs",
+	"vfat",
+	"exfat",
+	"ntfs",
+	NULL
+};
+
 /*
  * Make a swap file
  */
@@ -26,6 +37,7 @@ int make_swapfile(const char *swapfile, int safe)
 
 	/* make the file swapfile */
 	const char *argv[2 + 1];
+
 	argv[0] = "mkswap";
 	argv[1] = swapfile;
 	argv[2] = NULL;
@@ -40,13 +52,22 @@ int make_swapfile(const char *swapfile, int safe)
  */
 void is_swap_supported(const char *filename)
 {
+	int i, sw_support = 0;
 	int fibmap = tst_fibmap(filename);
 	long fs_type = tst_fs_type(filename);
 	const char *fstype = tst_fs_type_name(fs_type);
 
+	for (i = 0; swap_supported_fs[i]; i++) {
+		if (strstr(fstype, swap_supported_fs[i])) {
+			sw_support = 1;
+			break;
+		}
+	}
+
 	int ret = make_swapfile(filename, 1);
+
 	if (ret != 0) {
-		if (fibmap == 1)
+		if (fibmap == 1 && sw_support == 0)
 			tst_brk(TCONF, "mkswap on %s not supported", fstype);
 		else
 			tst_brk(TFAIL, "mkswap on %s failed", fstype);
@@ -56,7 +77,7 @@ void is_swap_supported(const char *filename)
 	if (TST_RET == -1) {
 		if (errno == EPERM)
 			tst_brk(TCONF, "Permission denied for swapon()");
-		else if (fibmap == 1 && errno == EINVAL)
+		else if (fibmap == 1 && errno == EINVAL && sw_support == 0)
 			tst_brk(TCONF, "Swapfile on %s not implemented", fstype);
 		else
 			tst_brk(TFAIL | TTERRNO, "swapon() on %s failed", fstype);
Signature · 5 lines
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#2 lore raw

PATCH v3 2/5 swapon01: Test on all filesystems

In reply to Li Wang

From: Petr Vorel <[email protected]>

Test on all filesystems to increase coverage.
Skip filesystems which does not support swap (currently bcachefs, btrfs and tmpfs).

Tested on 5.10, 6.6 and 6.7.

---
 testcases/kernel/syscalls/swapon/swapon01.c | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)
testcases/kernel/syscalls/swapon/swapon01.c +6 -2
index e59fb20a1..e1fe50459 100644
@@ -8,6 +8,7 @@
  * [Description]
  *
  * Checks that swapon() succeds with swapfile.
+ * Testing on all filesystems which support swap file.
  */
 
 #include <unistd.h>
@@ -17,7 +18,8 @@
 #include "lapi/syscalls.h"
 #include "libswap.h"
 
-#define SWAP_FILE "swapfile01"
+#define MNTPOINT	"mntpoint"
+#define SWAP_FILE	MNTPOINT"/swapfile01"
 
 static void verify_swapon(void)
 {
@@ -36,8 +38,10 @@ static void setup(void)
 }
 
 static struct tst_test test = {
+	.mntpoint = MNTPOINT,
+	.mount_device = 1,
 	.needs_root = 1,
-	.needs_tmpdir = 1,
+	.all_filesystems = 1,
 	.test_all = verify_swapon,
 	.setup = setup
 };
Signature · 5 lines
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#3 lore raw

PATCH v3 3/5 swapon01: Improving test with memory limits and swap reporting

In reply to Li Wang

This is target to create a more robust and controlled environment to test the swapon system call. By introducing memory limits through cgroups and filling memory with a known pattern, the test can better assess swapon behavior when the system experiences memory pressure.

Additionally, the reporting of "SwapCached" memory before turning off the swap file provides a clearer understanding of the swap system's state in response to the test conditions.

---
 testcases/kernel/syscalls/swapon/swapon01.c | 8 ++++++++
 1 file changed, 8 insertions(+)
testcases/kernel/syscalls/swapon/swapon01.c +8 -0
index e1fe50459..a74a5171e 100644
@@ -20,11 +20,15 @@
 
 #define MNTPOINT	"mntpoint"
 #define SWAP_FILE	MNTPOINT"/swapfile01"
+#define TESTMEM		(1UL<<30)
 
 static void verify_swapon(void)
 {
 	TST_EXP_PASS(tst_syscall(__NR_swapon, SWAP_FILE, 0));
 
+	tst_pollute_memory(TESTMEM, 0x41);
+	tst_res(TINFO, "SwapCached: %ld Kb", SAFE_READ_MEMINFO("SwapCached:"));
+
 	if (TST_PASS && tst_syscall(__NR_swapoff, SWAP_FILE) != 0) {
 		tst_brk(TBROK | TERRNO,
 				"Failed to turn off swapfile, system reboot recommended");
@@ -35,6 +39,9 @@ static void setup(void)
 {
 	is_swap_supported(SWAP_FILE);
 	make_swapfile(SWAP_FILE, 0);
+
+	SAFE_CG_PRINTF(tst_cg, "cgroup.procs", "%d", getpid());
+	SAFE_CG_PRINTF(tst_cg, "memory.max", "%lu", TESTMEM);
 }
 
 static struct tst_test test = {
@@ -42,6 +49,7 @@ static struct tst_test test = {
 	.mount_device = 1,
 	.needs_root = 1,
 	.all_filesystems = 1,
+	.needs_cgroup_ctrls = (const char *const []){ "memory", NULL },
 	.test_all = verify_swapon,
 	.setup = setup
 };
Signature · 5 lines
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#4 lore raw

PATCH v3 4/5 libswap: add function to prealloc contiguous file

In reply to Li Wang

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.

---
 libs/libltpswap/libswap.c | 48 ++++++++++++++++++++++++++++++++++++++-
 1 file changed, 47 insertions(+), 1 deletion(-)
libs/libltpswap/libswap.c +47 -1
index 8c2ce6cd7..b253dbeec 100644
@@ -4,6 +4,7 @@
  * Author: Stanislav Kholmanskikh <[email protected]>
  */
 
+#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,7 +78,7 @@ 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");
 
 	/* make the file swapfile */
Signature · 5 lines
2.40.1


-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#5 lore raw

Re: [LTP] [PATCH V3 4/5] libswap: add function to prealloc contiguous file

Hi Li,

swapoff01 fails on TMPDIR on btrfs (regardless kernel version):

# ./swapoff01 
rm -f -f -r swapoff01 swapoff02  *.o *.pyc .cache.mk *.dwo .*.dwo
BUILD libltpswap.a
make[1]: Nothing to be done for 'all'.
CC testcases/kernel/syscalls/swapoff/swapoff01
CC testcases/kernel/syscalls/swapoff/swapoff02
tst_test.c:1709: TINFO: LTP version: 20230929-295-gc20ab499a
tst_test.c:1595: TINFO: Timeout per run is 0h 00m 30s
tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
libswap.c:33: TINFO: FS_NOCOW_FL attribute set on ./tstswap
swapoff01.c:24: TFAIL: Failed to turn on the swap file, skipping test iteration: EINVAL (22)

=> I guess we would need to replace tst_fill_file() with prealloc_contiguous_file() (which is not public), or use make_swapfile() directly. But here we create file first with 65536 blocks and make_swapfile() creates 10 block file (with prealloc_contiguous_file() or previously also with tst_fill_file()).

Kind regards,
Petr

kernel/syscalls/swapoff/swapoff01.c +1 -4
@@ -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", 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");
 }
 
 
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#6 lore raw

Re: [LTP] [PATCH V3 4/5] libswap: add function to prealloc contiguous file

Hi Li,

Quoted 3 lines

Hi Li,

swapoff01 fails on TMPDIR on btrfs (regardless kernel version):

FYI it works on Tumbleweed with 6.7 kernel. It's broken on some older SLES with 5.14 and all older kernels. I suppose with nocow (fixes I suggested previously) would work as expected (TPASS, or TCONF on kernel < 5.0).

Kind regards,
Petr

Quoted 34 lines
# ./swapoff01 
rm -f -f -r swapoff01 swapoff02  *.o *.pyc .cache.mk *.dwo .*.dwo
BUILD libltpswap.a
make[1]: Nothing to be done for 'all'.
CC testcases/kernel/syscalls/swapoff/swapoff01
CC testcases/kernel/syscalls/swapoff/swapoff02
tst_test.c:1709: TINFO: LTP version: 20230929-295-gc20ab499a
tst_test.c:1595: TINFO: Timeout per run is 0h 00m 30s
tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
libswap.c:33: TINFO: FS_NOCOW_FL attribute set on ./tstswap
swapoff01.c:24: TFAIL: Failed to turn on the swap file, skipping test iteration: EINVAL (22)

=> I guess we would need to replace tst_fill_file() with prealloc_contiguous_file() (which is not public), or use make_swapfile() directly. But here we create file first with 65536 blocks and make_swapfile() creates 10 block file (with prealloc_contiguous_file() or previously also with tst_fill_file()).

Kind regards,
Petr

kernel/syscalls/swapoff/swapoff01.c +1 -4
@@ -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", 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");
 }
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#7 lore raw

Re: [LTP] [PATCH V3 4/5] libswap: add function to prealloc contiguous file

Hi Petr,

Quoted 10 lines · Petr Vorel

Hi Li,

Quoted 3 lines

Hi Li,

swapoff01 fails on TMPDIR on btrfs (regardless kernel version):

FYI it works on Tumbleweed with 6.7 kernel. It's broken on some older SLES with 5.14 and all older kernels. I suppose with nocow (fixes I suggested previously) would work as expected (TPASS, or TCONF on kernel < 5.0).

You're right.

We have to guarantee the swapfile is a contiguous file whatever the FS type is.
So here making use of make_swapfile() is a hard requirement. And, I don't think the file first with 65536 blocks (in swapoff01) is not necessary.

Quoted 43 lines

Kind regards,
Petr

Quoted 11 lines
# ./swapoff01
rm -f -f -r swapoff01 swapoff02  *.o *.pyc .cache.mk *.dwo .*.dwo
BUILD libltpswap.a
make[1]: Nothing to be done for 'all'.
CC testcases/kernel/syscalls/swapoff/swapoff01
CC testcases/kernel/syscalls/swapoff/swapoff02
tst_test.c:1709: TINFO: LTP version: 20230929-295-gc20ab499a
tst_test.c:1595: TINFO: Timeout per run is 0h 00m 30s
tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
libswap.c:33: TINFO: FS_NOCOW_FL attribute set on ./tstswap
swapoff01.c:24: TFAIL: Failed to turn on the swap file, skipping test

iteration: EINVAL (22)

Quoted 3 lines

=> I guess we would need to replace tst_fill_file() with prealloc_contiguous_file() (which is not public), or use make_swapfile() directly. But here we create file first with 65536 blocks and

make_swapfile()

Quoted 1 line

creates 10 block file (with prealloc_contiguous_file() or previously

also with

Quoted 18 lines

tst_fill_file()).

Kind regards,
Petr

kernel/syscalls/swapoff/swapoff01.c +1 -4
@@ -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", 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");
 }
Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#8 lore raw

Re: [LTP] [PATCH V3 4/5] libswap: add function to prealloc contiguous file

Quoted 15 lines

Hi Petr,

Quoted 9 lines · Petr Vorel

Hi Li,

Quoted 3 lines

Hi Li,

swapoff01 fails on TMPDIR on btrfs (regardless kernel version):

FYI it works on Tumbleweed with 6.7 kernel. It's broken on some older SLES with 5.14 and all older kernels. I suppose with nocow (fixes I suggested previously) would work as expected (TPASS, or TCONF on kernel < 5.0).

Quoted 7 lines

You're right.

We have to guarantee the swapfile is a contiguous file whatever the FS type is.
So here making use of make_swapfile() is a hard requirement. And, I don't think the file first with 65536 blocks (in swapoff01) is not necessary.

Maybe not, but now we test on single swap size. Testing small swap and big swap was IMHO more testing coverage (various filesystems behave differently on different size), but given this would be more important for whole .all_filesystems = 1 testing I'm ok with the change.

Kind regards,
Petr

Quoted 40 lines
Quoted 40 lines

Kind regards,
Petr

Quoted 11 lines
# ./swapoff01
rm -f -f -r swapoff01 swapoff02  *.o *.pyc .cache.mk *.dwo .*.dwo
BUILD libltpswap.a
make[1]: Nothing to be done for 'all'.
CC testcases/kernel/syscalls/swapoff/swapoff01
CC testcases/kernel/syscalls/swapoff/swapoff02
tst_test.c:1709: TINFO: LTP version: 20230929-295-gc20ab499a
tst_test.c:1595: TINFO: Timeout per run is 0h 00m 30s
tst_ioctl.c:21: TINFO: FIBMAP ioctl is NOT supported: EINVAL (22)
libswap.c:33: TINFO: FS_NOCOW_FL attribute set on ./tstswap
swapoff01.c:24: TFAIL: Failed to turn on the swap file, skipping test

iteration: EINVAL (22)

Quoted 3 lines

=> I guess we would need to replace tst_fill_file() with prealloc_contiguous_file() (which is not public), or use make_swapfile() directly. But here we create file first with 65536 blocks and

make_swapfile()

Quoted 1 line

creates 10 block file (with prealloc_contiguous_file() or previously

also with

Quoted 18 lines

tst_fill_file()).

Kind regards,
Petr

kernel/syscalls/swapoff/swapoff01.c +1 -4
@@ -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", 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");
 }
Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#9 lore raw

Re: [LTP] [PATCH V3 4/5] libswap: add function to prealloc contiguous file

Quoted 26 lines · Petr Vorel
Quoted 11 lines

Hi Petr,

Quoted 7 lines · Petr Vorel

Hi Li,

Quoted 3 lines

Hi Li,

swapoff01 fails on TMPDIR on btrfs (regardless kernel version):

FYI it works on Tumbleweed with 6.7 kernel. It's broken on some older

SLES

Quoted 1 line
Quoted 1 line

with 5.14 and all older kernels. I suppose with nocow (fixes I

suggested

Quoted 6 lines
Quoted 1 line

previously) would work as expected (TPASS, or TCONF on kernel < 5.0).

You're right.

We have to guarantee the swapfile is a contiguous file whatever the FS

type

Quoted 4 lines

is.
So here making use of make_swapfile() is a hard requirement. And, I don't think the file first with 65536 blocks (in swapoff01) is not necessary.

Quoted 6 lines

Maybe not, but now we test on single swap size. Testing small swap and big swap
was IMHO more testing coverage (various filesystems behave differently on different size), but given this would be more important for whole .all_filesystems = 1 testing I'm ok with the change.

Ok, that could be achieved by customizing the swap file size later. It's not very hard. But now I don't want to increase the patchset number just for more coverage, that will be a burden for release testing work.

Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
#10 lore raw

Re: [LTP] [PATCH V3 4/5] libswap: add function to prealloc contiguous file

Quoted 35 lines · Petr Vorel
Quoted 27 lines
Quoted 12 lines

Hi Petr,

Quoted 7 lines · Petr Vorel

Hi Li,

Quoted 3 lines

Hi Li,

swapoff01 fails on TMPDIR on btrfs (regardless kernel version):

FYI it works on Tumbleweed with 6.7 kernel. It's broken on some older

SLES

Quoted 1 line
Quoted 1 line

with 5.14 and all older kernels. I suppose with nocow (fixes I

suggested

Quoted 6 lines
Quoted 1 line

previously) would work as expected (TPASS, or TCONF on kernel < 5.0).

You're right.

We have to guarantee the swapfile is a contiguous file whatever the FS

type

Quoted 4 lines

is.
So here making use of make_swapfile() is a hard requirement. And, I don't think the file first with 65536 blocks (in swapoff01) is not necessary.

Quoted 5 lines

Maybe not, but now we test on single swap size. Testing small swap and big swap
was IMHO more testing coverage (various filesystems behave differently on different size), but given this would be more important for whole .all_filesystems = 1 testing I'm ok with the change.

Quoted 3 lines

Ok, that could be achieved by customizing the swap file size later. It's not very hard. But now I don't want to increase the patchset number just for more coverage, that will be a burden for release testing work.

Hi Li,

+1, sure.

Kind regards,
Petr

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
#11 lore raw

Re: [LTP] [PATCH V3 4/5] libswap: add function to prealloc contiguous file

In reply to Li Wang

Hi Li,

Quoted 15 lines

Hi Petr,

Quoted 9 lines · Petr Vorel

Hi Li,

Quoted 3 lines

Hi Li,

swapoff01 fails on TMPDIR on btrfs (regardless kernel version):

FYI it works on Tumbleweed with 6.7 kernel. It's broken on some older SLES with 5.14 and all older kernels. I suppose with nocow (fixes I suggested previously) would work as expected (TPASS, or TCONF on kernel < 5.0).

Quoted 7 lines

You're right.

We have to guarantee the swapfile is a contiguous file whatever the FS type is.
So here making use of make_swapfile() is a hard requirement. And, I don't think the file first with 65536 blocks (in swapoff01) is not necessary.

Unfortunately this commit or the following (libswap: Introduce file contiguity check) breaks swapon01.c on older SLES (4.4 based kernel and older) on XFS:

tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported libswap.c:191: TFAIL: swapon() on xfs failed: EINVAL (22)

The failure is in is_swap_supported().

I'll try to give more info tomorrow.

Kind regards,
Petr

tst_test.c:1709: TINFO: LTP version: 20230929
tst_test.c:1595: TINFO: Timeout per run is 0h 00m 30s tst_supported_fs_types.c:97: TINFO: Kernel supports ext2 tst_supported_fs_types.c:62: TINFO: mkfs.ext2 does exist tst_supported_fs_types.c:97: TINFO: Kernel supports ext3 tst_supported_fs_types.c:62: TINFO: mkfs.ext3 does exist tst_supported_fs_types.c:97: TINFO: Kernel supports ext4 tst_supported_fs_types.c:62: TINFO: mkfs.ext4 does exist tst_supported_fs_types.c:97: TINFO: Kernel supports xfs tst_supported_fs_types.c:62: TINFO: mkfs.xfs does exist tst_supported_fs_types.c:97: TINFO: Kernel supports btrfs tst_supported_fs_types.c:62: TINFO: mkfs.btrfs does exist tst_supported_fs_types.c:105: TINFO: Skipping bcachefs because of FUSE blacklist tst_supported_fs_types.c:97: TINFO: Kernel supports vfat tst_supported_fs_types.c:62: TINFO: mkfs.vfat does exist tst_supported_fs_types.c:128: TINFO: Filesystem exfat is not supported tst_supported_fs_types.c:128: TINFO: Filesystem ntfs is not supported tst_supported_fs_types.c:97: TINFO: Kernel supports tmpfs tst_supported_fs_types.c:49: TINFO: mkfs is not needed for tmpfs tst_test.c:1669: TINFO: === Testing on ext2 ===
tst_test.c:1118: TINFO: Formatting /dev/loop0 with ext2 opts='' extra opts='' mke2fs 1.42.11 (09-Jul-2014)
tst_test.c:1132: TINFO: Mounting /dev/loop0 to /tmp/LTP_swa4rYYz4/mntpoint fstyp=ext2 flags=0 tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed swapon01.c:30: TINFO: SwapCached: 348 Kb
tst_test.c:1669: TINFO: === Testing on ext3 ===
tst_test.c:1118: TINFO: Formatting /dev/loop0 with ext3 opts='' extra opts='' mke2fs 1.42.11 (09-Jul-2014)
tst_test.c:1132: TINFO: Mounting /dev/loop0 to /tmp/LTP_swa4rYYz4/mntpoint fstyp=ext3 flags=0 tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed swapon01.c:30: TINFO: SwapCached: 136 Kb
tst_test.c:1669: TINFO: === Testing on ext4 ===
tst_test.c:1118: TINFO: Formatting /dev/loop0 with ext4 opts='' extra opts='' mke2fs 1.42.11 (09-Jul-2014)
tst_test.c:1132: TINFO: Mounting /dev/loop0 to /tmp/LTP_swa4rYYz4/mntpoint fstyp=ext4 flags=0 tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
swapon01.c:27: TPASS: tst_syscall(__NR_swapon, SWAP_FILE, 0) passed swapon01.c:30: TINFO: SwapCached: 116 Kb
tst_test.c:1669: TINFO: === Testing on xfs ===
tst_test.c:1118: TINFO: Formatting /dev/loop0 with xfs opts='' extra opts='' tst_test.c:1132: TINFO: Mounting /dev/loop0 to /tmp/LTP_swa4rYYz4/mntpoint fstyp=xfs flags=0 tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported
libswap.c:191: TFAIL: swapon() on xfs failed: EINVAL (22)

Summary:

passed   3
failed   1
broken   0
skipped  0

warnings 0

Signature · 1 line
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#12 lore raw

Re: [LTP] [PATCH V3 4/5] libswap: add function to prealloc contiguous file

Hi Petr,

Quoted 28 lines · Petr Vorel

Hi Li,

Quoted 11 lines

Hi Petr,

Quoted 7 lines · Petr Vorel

Hi Li,

Quoted 3 lines

Hi Li,

swapoff01 fails on TMPDIR on btrfs (regardless kernel version):

FYI it works on Tumbleweed with 6.7 kernel. It's broken on some older

SLES

Quoted 1 line
Quoted 1 line

with 5.14 and all older kernels. I suppose with nocow (fixes I

suggested

Quoted 6 lines
Quoted 1 line

previously) would work as expected (TPASS, or TCONF on kernel < 5.0).

You're right.

We have to guarantee the swapfile is a contiguous file whatever the FS

type

Quoted 4 lines

is.
So here making use of make_swapfile() is a hard requirement. And, I don't think the file first with 65536 blocks (in swapoff01) is not necessary.

Quoted 9 lines

Unfortunately this commit or the following (libswap: Introduce file contiguity
check) breaks swapon01.c on older SLES (4.4 based kernel and older) on XFS:

tst_ioctl.c:26: TINFO: FIBMAP ioctl is supported libswap.c:191: TFAIL: swapon() on xfs failed: EINVAL (22)

The failure is in is_swap_supported().

Good catch.

After testing on my side, reproduced that easily with old XFS. The reason is probably old XFS expects the swap file to be initialized in a certain way.

So a simple fix is just to fill full of the file after preallocating space:

libs/libltpswap/libswap.c +0 -0
@@ -140,6 +140,11 @@ int make_swapfile(const char *swapfile, int safe)
        if (prealloc_contiguous_file(swapfile, sysconf(_SC_PAGESIZE), 10)

!= 0)

                tst_brk(TBROK, "Failed to create swapfile");

+ 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];
Signature · 5 lines
Regards,
Li Wang

-- 
Mailing list info: https://lists.linux.it/listinfo/ltp
author
#13 lore raw

PATCH v3 5/5 libswap: Introduce file contiguity check

In reply to Li Wang

This patch introduces a new function file_is_contiguous to the libltpswap library to determine if a swap file is stored in a contiguous block of disk space, which is a typical requirement for swap files in Linux. The function performs a series of checks using the fiemap structure to assess the contiguity of the file and logs the result.

It is integrated into the is_swap_supported function to replace the previous tst_fibmap check, providing a more reliable method for verifying that a file suitable for swap is indeed contiguous.

---
 libs/libltpswap/libswap.c | 72 ++++++++++++++++++++++++++++++++++++---
 1 file changed, 67 insertions(+), 5 deletions(-)
libs/libltpswap/libswap.c +67 -5
index b253dbeec..8382ea23c 100644
@@ -6,6 +6,8 @@
 
 #include <linux/fs.h>
 #include <errno.h>
+#include <linux/fiemap.h>
+#include <stdlib.h>
 
 #define TST_NO_DEFAULT_MAIN
 
@@ -69,6 +71,63 @@ static int prealloc_contiguous_file(const char *path, size_t bs, size_t bcount)
 	return 0;
 }
 
+static int file_is_contiguous(const char *filename)
+{
+	int fd, contiguous = 0;
+	struct fiemap *fiemap;
+
+	if (tst_fibmap(filename) == 0) {
+		contiguous = 1;
+		goto out;
+	}
+
+	if (tst_fs_type(filename) == TST_TMPFS_MAGIC) {
+		contiguous = 0;
+		goto out;
+	}
+
+	fd = SAFE_OPEN(filename, O_RDONLY);
+
+	fiemap = (struct fiemap *)SAFE_MALLOC(sizeof(struct fiemap) + sizeof(struct fiemap_extent));
+	memset(fiemap, 0, sizeof(struct fiemap) + sizeof(struct fiemap_extent));
+
+	fiemap->fm_start = 0;
+	fiemap->fm_length = ~0;
+	fiemap->fm_flags = 0;
+	fiemap->fm_extent_count = 1;
+
+	SAFE_IOCTL(fd, FS_IOC_FIEMAP, fiemap);
+
+	/*
+	 * fiemap->fm_mapped_extents != 1:
+	 *   This checks if the file does not have exactly one extent. If there are more
+	 *   or zero extents, the file is not stored in a single contiguous block.
+	 *
+	 * fiemap->fm_extents[0].fe_logical != 0:
+	 *   This checks if the first extent does not start at the logical offset 0 of
+	 *   the file. If it doesn't, it indicates that the file's first block of data
+	 *   is not at the beginning of the file, which implies non-contiguity.
+	 *
+	 * (fiemap->fm_extents[0].fe_flags & FIEMAP_EXTENT_LAST) != FIEMAP_EXTENT_LAST:
+	 *   This checks if the first extent does not have the FIEMAP_EXTENT_LAST flag set.
+	 *   If the flag isn't set, it means that this extent is not the last one, suggesting
+	 *   that there are more extents and the file is not contiguous.
+	 */
+	if (fiemap->fm_mapped_extents != 1 ||
+		fiemap->fm_extents[0].fe_logical != 0 ||
+		(fiemap->fm_extents[0].fe_flags & FIEMAP_EXTENT_LAST) != FIEMAP_EXTENT_LAST) {
+
+		tst_res(TINFO, "File '%s' is not contiguous", filename);
+		contiguous = 0;
+	}
+
+	SAFE_CLOSE(fd);
+	free(fiemap);
+
+out:
+	return contiguous;
+}
+
 /*
  * Make a swap file
  */
@@ -99,10 +158,15 @@ int make_swapfile(const char *swapfile, int safe)
 void is_swap_supported(const char *filename)
 {
 	int i, sw_support = 0;
-	int fibmap = tst_fibmap(filename);
+	int ret = make_swapfile(filename, 1);
+	int fi_contiguous = file_is_contiguous(filename);
 	long fs_type = tst_fs_type(filename);
 	const char *fstype = tst_fs_type_name(fs_type);
 
+	if (fs_type == TST_BTRFS_MAGIC &&
+			tst_kvercmp(5, 0, 0) < 0)
+		tst_brk(TCONF, "Swapfile on Btrfs (kernel < 5.0) not implemented");
+
 	for (i = 0; swap_supported_fs[i]; i++) {
 		if (strstr(fstype, swap_supported_fs[i])) {
 			sw_support = 1;
@@ -110,10 +174,8 @@ void is_swap_supported(const char *filename)
 		}
 	}
 
-	int ret = make_swapfile(filename, 1);
-
 	if (ret != 0) {
-		if (fibmap == 1 && sw_support == 0)
+		if (fi_contiguous == 0 && sw_support == 0)
 			tst_brk(TCONF, "mkswap on %s not supported", fstype);
 		else
 			tst_brk(TFAIL, "mkswap on %s failed", fstype);
@@ -123,7 +185,7 @@ void is_swap_supported(const char *filename)
 	if (TST_RET == -1) {
 		if (errno == EPERM)
 			tst_brk(TCONF, "Permission denied for swapon()");
-		else if (fibmap == 1 && errno == EINVAL && sw_support == 0)
+		else if (errno == EINVAL && fi_contiguous == 0 && sw_support == 0)
 			tst_brk(TCONF, "Swapfile on %s not implemented", fstype);
 		else
 			tst_brk(TFAIL | TTERRNO, "swapon() on %s failed", fstype);
Signature · 5 lines
2.40.1


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