From: Daniel Palmer <daniel@thingy.jp>
To: w@1wt.eu, linux@weissschuh.net
Cc: david.laight.linux@gmail.com, linux-kernel@vger.kernel.org,
Daniel Palmer <daniel@thingy.jp>
Subject: [PATCH v4 3/3] selftests/nolibc: Add a very basic test for fallocate()
Date: Thu, 7 May 2026 18:03:53 +0900 [thread overview]
Message-ID: <20260507090353.356764-4-daniel@thingy.jp> (raw)
In-Reply-To: <20260507090353.356764-1-daniel@thingy.jp>
1: Create a tmp file, fallocate() to make it a bit bigger, check the
size is what was expected.
2: Try to fallocate() (1 << 20), this should work.
3: Try to fallocate() (1 << 52), this should cause ENOSPC or EFBIG.
2 and 3 are basically to make sure if the offset or size are split
into a pair of registers for the syscall that we are passing them
the correct way around.
This test requires /tmp to be a tmpfs and not ramfs or something
else so before running the tests check what /tmp is and if its
not tmpfs skip the test.
Signed-off-by: Daniel Palmer <daniel@thingy.jp>
---
tools/testing/selftests/nolibc/nolibc-test.c | 70 ++++++++++++++++++++
1 file changed, 70 insertions(+)
diff --git a/tools/testing/selftests/nolibc/nolibc-test.c b/tools/testing/selftests/nolibc/nolibc-test.c
index 1db6e8d55c16..2a3b855214bc 100644
--- a/tools/testing/selftests/nolibc/nolibc-test.c
+++ b/tools/testing/selftests/nolibc/nolibc-test.c
@@ -48,6 +48,9 @@
#include <endian.h>
#include <alloca.h>
+/* For TMPFS_MAGIC */
+#include <linux/magic.h>
+
#pragma GCC diagnostic ignored "-Wmissing-prototypes"
#include "nolibc-test-linkage.h"
@@ -903,6 +906,66 @@ int test_getpagesize(void)
return !c;
}
+int test_fallocate(void)
+{
+ struct stat st;
+ int fd, r;
+
+ /* Create a new tmp file */
+ fd = open("/tmp", O_TMPFILE | O_RDWR, 0644);
+ if (fd == -1)
+ return -1;
+
+ /* Expand it to 42 bytes */
+ r = fallocate(fd, 0, 0, 42);
+ if (r)
+ goto close_tmpfile;
+
+ /* Get the new stat */
+ r = fstat(fd, &st);
+ if (r)
+ goto close_tmpfile;
+
+ /* It should be 42 bytes long */
+ if (st.st_size != 42) {
+ r = -1;
+ goto close_tmpfile;
+ }
+
+ /* Now try to allocate 1MiB. This puts a single bit
+ * into one of the registers if the size is split into
+ * two registers. This shouldn't fail if the bit is in
+ * the correct register.
+ */
+ r = fallocate(fd, 0, 0, (1ll << 20));
+ if (r)
+ goto close_tmpfile;
+
+ /* Check a massive size that puts a single bit into
+ * the other register if splitting.
+ * This should return an error and errno = ENOSPC or
+ * EFBIG indicating the value was passed correctly but it
+ * was rejected.
+ */
+ r = fallocate(fd, 0, 0, (1ll << (20 + 32)));
+ if (r != -1) {
+ r = -1;
+ goto close_tmpfile;
+ }
+ if (errno != ENOSPC && errno != EFBIG) {
+ r = -1;
+ goto close_tmpfile;
+ }
+
+ /* Test passed */
+ r = 0;
+
+close_tmpfile:
+ close(fd);
+
+ return r;
+}
+
int test_file_stream(void)
{
FILE *f;
@@ -1455,6 +1518,8 @@ int run_syscall(int min, int max)
void *p1, *p2;
int has_gettid = 1;
int has_brk;
+ int tmp_is_tmpfs = 0;
+ struct statfs tmp_statfs_buf;
/* <proc> indicates whether or not /proc is mounted */
proc = stat("/proc", &stat_buf) == 0;
@@ -1470,6 +1535,10 @@ int run_syscall(int min, int max)
/* on musl setting brk()/sbrk() always fails */
has_brk = brk(0) == 0;
+ /* Check if /tmp is tmpfs */
+ if (statfs("/tmp", &tmp_statfs_buf) == 0 && tmp_statfs_buf.f_type == TMPFS_MAGIC)
+ tmp_is_tmpfs = 1;
+
for (test = min; test >= 0 && test <= max; test++) {
int llen = 0; /* line length */
@@ -1512,6 +1581,7 @@ int run_syscall(int min, int max)
CASE_TEST(dup3_0); tmp = dup3(0, 100, 0); EXPECT_SYSNE(1, tmp, -1); close(tmp); break;
CASE_TEST(dup3_m1); tmp = dup3(-1, 100, 0); EXPECT_SYSER(1, tmp, -1, EBADF); if (tmp != -1) close(tmp); break;
CASE_TEST(execve_root); EXPECT_SYSER(1, execve("/", (char*[]){ [0] = "/", [1] = NULL }, NULL), -1, EACCES); break;
+ CASE_TEST(fallocate); EXPECT_SYSZR(tmp_is_tmpfs, test_fallocate()); break;
CASE_TEST(fchdir_stdin); EXPECT_SYSER(1, fchdir(STDIN_FILENO), -1, ENOTDIR); break;
CASE_TEST(fchdir_badfd); EXPECT_SYSER(1, fchdir(-1), -1, EBADF); break;
CASE_TEST(file_stream); EXPECT_SYSZR(1, test_file_stream()); break;
--
2.53.0
next prev parent reply other threads:[~2026-05-07 9:04 UTC|newest]
Thread overview: 5+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-05-07 9:03 [PATCH v4 0/3] nolibc: Add fallocate() Daniel Palmer
2026-05-07 9:03 ` [PATCH v4 1/3] tools/nolibc: fcntl: " Daniel Palmer
2026-05-07 9:03 ` [PATCH v4 2/3] tools/nolibc: Add statfs() Daniel Palmer
2026-05-07 9:03 ` Daniel Palmer [this message]
2026-05-11 6:29 ` [PATCH v4 0/3] nolibc: Add fallocate() Willy Tarreau
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=20260507090353.356764-4-daniel@thingy.jp \
--to=daniel@thingy.jp \
--cc=david.laight.linux@gmail.com \
--cc=linux-kernel@vger.kernel.org \
--cc=linux@weissschuh.net \
--cc=w@1wt.eu \
/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