From: Li Wang <liwang@redhat.com>
To: ltp@lists.linux.it
Subject: [LTP] [PATCH v5 5/8] libswap: Introduce file contiguity check
Date: Sun, 28 Jan 2024 10:48:35 +0800 [thread overview]
Message-ID: <20240128024838.2699248-6-liwang@redhat.com> (raw)
In-Reply-To: <20240128024838.2699248-1-liwang@redhat.com>
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.
Signed-off-by: Li Wang <liwang@redhat.com>
Reviewed-by: Petr Vorel <pvorel@suse.cz>
Tested-by: Petr Vorel <pvorel@suse.cz>
---
libs/libltpswap/libswap.c | 70 ++++++++++++++++++++++++++++++++++++---
1 file changed, 65 insertions(+), 5 deletions(-)
diff --git a/libs/libltpswap/libswap.c b/libs/libltpswap/libswap.c
index e606e3f03..95d4f59b0 100644
--- a/libs/libltpswap/libswap.c
+++ b/libs/libltpswap/libswap.c
@@ -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,61 @@ 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)
+ 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
*/
@@ -105,10 +162,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;
@@ -116,10 +178,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);
@@ -129,7 +189,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);
--
2.40.1
--
Mailing list info: https://lists.linux.it/listinfo/ltp
next prev parent reply other threads:[~2024-01-28 2:49 UTC|newest]
Thread overview: 14+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-01-28 2:48 [LTP] [PATCH v5 0/8] improvement work on libswap library Li Wang
2024-01-28 2:48 ` [LTP] [PATCH v5 1/8] libswap: add known swap supported fs check Li Wang
2024-01-28 2:48 ` [LTP] [PATCH v5 2/8] swapon01: Test on all filesystems Li Wang
2024-01-28 2:48 ` [LTP] [PATCH v5 3/8] swapon01: Improving test with memory limits and swap reporting Li Wang
2024-01-28 2:48 ` [LTP] [PATCH v5 4/8] libswap: add function to prealloc contiguous file Li Wang
2024-01-28 2:48 ` Li Wang [this message]
2024-01-28 2:48 ` [LTP] [PATCH v5 6/8] libswap: customize swapfile size Li Wang
2024-01-28 2:48 ` [LTP] [PATCH v5 7/8] swapon/off: enable all_filesystem in swap test Li Wang
2024-01-29 8:03 ` Petr Vorel
2024-01-29 8:26 ` Li Wang
2024-01-28 2:48 ` [LTP] [PATCH v5 8/8] libswap: Refactor is_swap_supported function to return status Li Wang
2024-01-29 8:03 ` Petr Vorel
2024-01-29 11:36 ` Li Wang
2024-01-29 12:22 ` 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=20240128024838.2699248-6-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