Skip to contentSkip to conversation

nolibc: Add fallocate()

PATCH In review Daniel Palmer 3 participants 12 messages May 7 – May 14

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

v4 cover nolibc: Add fallocate()

While poking around with my "static PIE for nommu" series I found I needed fallocate(). Implementing it turned out a bit more interesting than I thought it would be due to how the offset and size need to be passed on 32bit machines.

v4:
- Added statfs()...
- Reworked the test a bit to use statfs() to work out if /tmp is a tmpfs or not and skip the test if it isn't. This skips the test on sparc32 where CONFIG_TMPFS=n.
- Adding the needed bits to the sparc32 config to allow tmpfs to be enabled allows it to run and pass the test. I will check the changes and send a patch.
- Cleaned up the if() around deciding how to pass the parameters for fallocate() based on the comments from David and Thomas.
- Test passed on all of the supported targets.

v3:
- Reworked the test a bit, ran it on all the targets I could get to run (s390, loongarch and sh currently don't work for me). sparc32 doesn't seem to support fallocate() so made it skip the test.
- Rebase on nolibc/for-next with the large file support bits.

v2:
- Addressed Thomas' comments
- Trial and error'd a test for the arguments being passed correctly. Hopefully someone smarter than I am can tell if it actually works.

Daniel Palmer (3):
tools/nolibc: fcntl: Add fallocate() tools/nolibc: Add statfs()
selftests/nolibc: Add a very basic test for fallocate()

 tools/include/nolibc/Makefile                |  1 +
 tools/include/nolibc/arch-mips.h             | 11 +++
 tools/include/nolibc/fcntl.h                 | 31 +++++++++
 tools/include/nolibc/nolibc.h                |  1 +
 tools/include/nolibc/sys.h                   |  8 +++
 tools/include/nolibc/sys/statfs.h            | 50 ++++++++++++++
 tools/testing/selftests/nolibc/nolibc-test.c | 70 ++++++++++++++++++++
 7 files changed, 172 insertions(+)
 create mode 100644 tools/include/nolibc/sys/statfs.h

base-commit: bb2d82d41894cb30d836e9796ff67d2f9a71eccf

Signature · 1 line
2.53.0
#1 lore raw

PATCH v4 1/3 tools/nolibc: fcntl: Add fallocate()

Add fallocate().

Some special care is needed to put the offset and size into the syscall parameters for 32bit machines, x32, and mipsn32.

For x32 we can just check if the kernel long size is the same as off_t and use the same path as x86_64.

For mipsn32 we override the generic version and provide one that does the right thing.

---
 tools/include/nolibc/arch-mips.h | 11 +++++++++++
 tools/include/nolibc/fcntl.h     | 31 +++++++++++++++++++++++++++++++
 tools/include/nolibc/sys.h       |  8 ++++++++
 3 files changed, 50 insertions(+)
tools/include/nolibc/arch-mips.h +11 -0
index 1400653c76c1..e4e42f2bcaf4 100644
@@ -6,6 +6,7 @@
 
 #ifndef _NOLIBC_ARCH_MIPS_H
 #define _NOLIBC_ARCH_MIPS_H
+#include <linux/unistd.h>
 
 #include "compiler.h"
 #include "crt.h"
@@ -256,6 +257,16 @@
 	_arg4 ? -_num : _num;                                                 \
 })
 
+/* The generic version of this will split offset and size for _ABIN32,
+ * override it and do the right thing here.
+ */
+static __attribute__((unused))
+int _sys_fallocate(int fd, int mode, off_t offset, off_t size)
+{
+	return __nolibc_syscall4(__NR_fallocate, fd, mode, offset, size);
+}
+#define _sys_fallocate _sys_fallocate
+
 #endif /* _ABIO32 */
 
 #ifndef NOLIBC_NO_RUNTIME
tools/include/nolibc/fcntl.h +31 -0
index 014910a8e928..cea5fec66aec 100644
@@ -14,6 +14,9 @@
 #include "types.h"
 #include "sys.h"
 
+/* For fallocate() modes */
+#include <linux/falloc.h>
+
 /*
  * int openat(int dirfd, const char *path, int flags[, mode_t mode]);
  */
@@ -80,4 +83,32 @@ int creat(const char *path, mode_t mode)
 	return open(path, O_CREAT | O_WRONLY | O_TRUNC, mode);
 }
 
+/*
+ * int fallocate(int fd, int mode, off_t offset, off_t size);
+ */
+
+#if !defined(_sys_fallocate)
+static __attribute__((unused))
+int _sys_fallocate(int fd, int mode, off_t offset, off_t size)
+{
+	/*
+	 * For 32 bit machines __kernel_long_t will be 4, off_t will be 8
+	 * and we need to split offset and size, for 64 machines we can use
+	 * the values as-is.
+	 */
+	if (sizeof(__kernel_long_t) != 8)
+		return __nolibc_syscall6(__NR_fallocate, fd, mode,
+			__NOLIBC_LLARGPART(offset, 0), __NOLIBC_LLARGPART(offset, 1),
+			__NOLIBC_LLARGPART(size, 0), __NOLIBC_LLARGPART(size, 1));
+	else
+		return __nolibc_syscall4(__NR_fallocate, fd, mode, offset, size);
+}
+#endif
+
+static __attribute__((unused))
+int fallocate(int fd, int mode, off_t offset, off_t size)
+{
+	return __sysret(_sys_fallocate(fd, mode, offset, size));
+}
+
 #endif /* _NOLIBC_FCNTL_H */
tools/include/nolibc/sys.h +8 -0
index 33f9c970ae57..b7136a3a7d6a 100644
@@ -29,6 +29,14 @@
 #include "stdarg.h"
 #include "types.h"
 
+/*
+ * Helper for 32bit machines where a 64bit syscall arg needs to be split into
+ * two 32bit parts while making sure the order of the low/high parts are correct
+ * for the endian:
+ * __NOLIBC_LLARGPART(x, 0), __NOLIBC_LLARGPART(x, 1)
+ */
+#define __NOLIBC_LLARGPART(_arg, _part) \
+	(((union { long long ll; long l[2]; }) { .ll = _arg }).l[_part])
 
 /* Syscall return helper: takes the syscall value in argument and checks for an
  * error in it. This may only be used with signed returns (int or long), but
Signature · 1 line
2.53.0
#2 lore raw

PATCH v4 2/3 tools/nolibc: Add statfs()

In reply to Daniel Palmer

Add statfs().

---
 tools/include/nolibc/Makefile     |  1 +
 tools/include/nolibc/nolibc.h     |  1 +
 tools/include/nolibc/sys/statfs.h | 50 +++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+)
 create mode 100644 tools/include/nolibc/sys/statfs.h
tools/include/nolibc/Makefile +1 -0
index 00fd2e566d75..e6281f58e6e2 100644
@@ -60,6 +60,7 @@ all_files := \
 		sys/resource.h \
 		sys/select.h \
 		sys/stat.h \
+		sys/statfs.h \
 		sys/syscall.h \
 		sys/sysmacros.h \
 		sys/time.h \
tools/include/nolibc/nolibc.h +1 -0
index faa94f247281..425cf87befdc 100644
@@ -107,6 +107,7 @@
 #include "sys/resource.h"
 #include "sys/select.h"
 #include "sys/stat.h"
+#include "sys/statfs.h"
 #include "sys/syscall.h"
 #include "sys/sysmacros.h"
 #include "sys/time.h"
tools/include/nolibc/sys/statfs.h new file +50 -0

50 lines not on this page · raw message

Signature · 1 line
2.53.0
#3 lore raw

Re: [PATCH v4 2/3] tools/nolibc: Add statfs()

Quoted 58 lines · Daniel Palmer

Add statfs().

---
 tools/include/nolibc/Makefile     |  1 +
 tools/include/nolibc/nolibc.h     |  1 +
 tools/include/nolibc/sys/statfs.h | 50 +++++++++++++++++++++++++++++++
 3 files changed, 52 insertions(+)
 create mode 100644 tools/include/nolibc/sys/statfs.h
tools/include/nolibc/Makefile +1 -0
index 00fd2e566d75..e6281f58e6e2 100644
@@ -60,6 +60,7 @@ all_files := \
 		sys/resource.h \
 		sys/select.h \
 		sys/stat.h \
+		sys/statfs.h \
 		sys/syscall.h \
 		sys/sysmacros.h \
 		sys/time.h \
tools/include/nolibc/nolibc.h +1 -0
index faa94f247281..425cf87befdc 100644
@@ -107,6 +107,7 @@
 #include "sys/resource.h"
 #include "sys/select.h"
 #include "sys/stat.h"
+#include "sys/statfs.h"
 #include "sys/syscall.h"
 #include "sys/sysmacros.h"
 #include "sys/time.h"
tools/include/nolibc/sys/statfs.h new file +18 -0
new file mode 100644
index 000000000000..26976ac2e94a
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+/*
+ * statfs for NOLIBC
+ * Copyright (C) 2026 Daniel Palmer <[email protected]>
+ */
+
+/* make sure to include all global symbols */
+#include "../nolibc.h"
+
+#ifndef _NOLIBC_SYS_STATFS_H
+#define _NOLIBC_SYS_STATFS_H
+
+#include "../sys.h"
+
+/* Some preprocessor hackery to get struct statfs to
+ * always be the 64bit one.
+ */
+#define statfs __nolibc_kernel_statfs

tools/include/nolibc/sys/statfs.h line 28

Quoted 10 lines
tools/include/nolibc/sys/statfs.h new file +10 -0
+#define statfs64 __nolibc_kernel_statfs64
+#include <asm/statfs.h>
+#undef statfs
+#undef statfs64
+
+#ifdef __NR_statfs64
+#define statfs __nolibc_kernel_statfs64
+#else
+#define statfs __nolibc_kernel_statfs
+#endif

This #define can wreak havoc in user code.
While it would be nice to have statfs() in nolibc,
I don't see a proper way to work with the current UAPI header. Can we do without statfs() for now?

Quoted 14 lines
tools/include/nolibc/sys/statfs.h new file +14 -0
+
+/*
+ * statfs(const char *path, struct statfs *buf);
+ */
+
+static __attribute__((unused))
+int _sys_statfs(const char *path, struct statfs *buf)
+{
+#ifdef __NR_statfs64
+	return __nolibc_syscall3(__NR_statfs64, path, sizeof(*buf), buf);
+#else
+	return __nolibc_syscall2(__NR_statfs, path, buf);
+#endif
+}

(...)

#4 lore raw

Re: [PATCH v4 2/3] tools/nolibc: Add statfs()

Hi Thomas,

Quoted 11 lines · Thomas Weißschuh
Quoted 11 lines
tools/include/nolibc/sys/statfs.h new file +11 -0
+/* Some preprocessor hackery to get struct statfs to
+ * always be the 64bit one.
+ */
+#define statfs __nolibc_kernel_statfs
+#define statfs64 __nolibc_kernel_statfs64
+#include <asm/statfs.h>
+#undef statfs
+#undef statfs64
+
+#ifdef __NR_statfs64
+#define statfs __nolibc_kernel_statfs64
Quoted 8 lines

tools/include/nolibc/sys/statfs.h line 28

Quoted 3 lines
tools/include/nolibc/sys/statfs.h new file +3 -0
+#else
+#define statfs __nolibc_kernel_statfs
+#endif

This #define can wreak havoc in user code.
While it would be nice to have statfs() in nolibc,
I don't see a proper way to work with the current UAPI header. Can we do without statfs() for now?

I thought maybe we could look at /proc/mounts but it seems like if tmpfs is actually ramfs (CONFIG_SHMEM=n) it still shows tmpfs as the filesystem type.
So we'd be back to square one. Maybe look for tmpfs in /proc/mounts then try something that ramfs doesn't support like creating a tmp file and fallocate() on it to see if it's ramfs?

Thanks,

Daniel

#5 lore raw

Re: [PATCH v4 2/3] tools/nolibc: Add statfs()

Quoted 21 lines · Daniel Palmer
Quoted 11 lines · Thomas Weißschuh
Quoted 11 lines
tools/include/nolibc/sys/statfs.h new file +11 -0
+/* Some preprocessor hackery to get struct statfs to
+ * always be the 64bit one.
+ */
+#define statfs __nolibc_kernel_statfs
+#define statfs64 __nolibc_kernel_statfs64
+#include <asm/statfs.h>
+#undef statfs
+#undef statfs64
+
+#ifdef __NR_statfs64
+#define statfs __nolibc_kernel_statfs64
Quoted 8 lines

tools/include/nolibc/sys/statfs.h line 28

Quoted 3 lines
tools/include/nolibc/sys/statfs.h new file +3 -0
+#else
+#define statfs __nolibc_kernel_statfs
+#endif

This #define can wreak havoc in user code.
While it would be nice to have statfs() in nolibc,
I don't see a proper way to work with the current UAPI header. Can we do without statfs() for now?

Quoted 6 lines

I thought maybe we could look at /proc/mounts but it seems like if tmpfs is actually ramfs (CONFIG_SHMEM=n) it still shows tmpfs as the filesystem type.
So we'd be back to square one. Maybe look for tmpfs in /proc/mounts then try something that ramfs doesn't support like creating a tmp file and fallocate() on it to see if it's ramfs?

Why not check for EOPNOTSUPP from fallocate() and skip the test in that case? This would also allow the test to run on a regular filesystem on /tmp which does support fallocate().

Thomas

#6 lore raw

Re: [PATCH v4 2/3] tools/nolibc: Add statfs()

Hi Thomas,

Quoted 30 lines · Thomas Weißschuh
Quoted 21 lines · Daniel Palmer
Quoted 11 lines · Thomas Weißschuh
Quoted 11 lines
tools/include/nolibc/sys/statfs.h new file +11 -0
+/* Some preprocessor hackery to get struct statfs to
+ * always be the 64bit one.
+ */
+#define statfs __nolibc_kernel_statfs
+#define statfs64 __nolibc_kernel_statfs64
+#include <asm/statfs.h>
+#undef statfs
+#undef statfs64
+
+#ifdef __NR_statfs64
+#define statfs __nolibc_kernel_statfs64
Quoted 8 lines

tools/include/nolibc/sys/statfs.h line 28

Quoted 3 lines
tools/include/nolibc/sys/statfs.h new file +3 -0
+#else
+#define statfs __nolibc_kernel_statfs
+#endif

This #define can wreak havoc in user code.
While it would be nice to have statfs() in nolibc,
I don't see a proper way to work with the current UAPI header. Can we do without statfs() for now?

Quoted 6 lines

I thought maybe we could look at /proc/mounts but it seems like if tmpfs is actually ramfs (CONFIG_SHMEM=n) it still shows tmpfs as the filesystem type.
So we'd be back to square one. Maybe look for tmpfs in /proc/mounts then try something that ramfs doesn't support like creating a tmp file and fallocate() on it to see if it's ramfs?

Quoted 3 lines

Why not check for EOPNOTSUPP from fallocate() and skip the test in that case? This would also allow the test to run on a regular filesystem on /tmp which does support fallocate().

Yeah that makes sense. I will drop this patch and adjust the test so we check for fallocate().
I think I'll add a check for the size of the filesystem /tmp is on so we don't attempt to create a massive file. I was assuming because /tmp should be tmpfs attempting to allocate a file in RAM bigger than most CPUs can physically address would be ok. If /tmp could be a real disk I think it's safer to check. :)

Do you think there is a way to do statfs() properly in the future? I actually started using it in a mini (~64KB) userland I am building so I can get Linux to boot in 3.5MB of RAM.
I considered just copying the 64bit version of the statfs struct into nolibc so we don't need to use the UAPI header but it seemed like MIPS has a bunch of different versions of it so it wouldn't work on MIPS.

Cheers,

Daniel

#7 lore raw

Re: [PATCH v4 2/3] tools/nolibc: Add statfs()

Quoted 25 lines · Daniel Palmer

Hi Thomas,

Quoted 22 lines · Thomas Weißschuh
Quoted 20 lines · Daniel Palmer
Quoted 19 lines · Thomas Weißschuh
Quoted 4 lines
tools/include/nolibc/sys/statfs.h new file +4 -0
+/* Some preprocessor hackery to get struct statfs to
+ * always be the 64bit one.
+ */
+#define statfs __nolibc_kernel_statfs

tools/include/nolibc/sys/statfs.h line 28

Quoted 10 lines
tools/include/nolibc/sys/statfs.h new file +10 -0
+#define statfs64 __nolibc_kernel_statfs64
+#include <asm/statfs.h>
+#undef statfs
+#undef statfs64
+
+#ifdef __NR_statfs64
+#define statfs __nolibc_kernel_statfs64
+#else
+#define statfs __nolibc_kernel_statfs
+#endif

This #define can wreak havoc in user code.
While it would be nice to have statfs() in nolibc,
I don't see a proper way to work with the current UAPI header. Can we do without statfs() for now?

(...)

Quoted 3 lines

Do you think there is a way to do statfs() properly in the future? I actually started using it in a mini (~64KB) userland I am building so I can get Linux to boot in 3.5MB of RAM.

What exactly do you need out of statfs?

Quoted 3 lines

I considered just copying the 64bit version of the statfs struct into nolibc so we don't need to use the UAPI header but it seemed like MIPS has a bunch of different versions of it so it wouldn't work on MIPS.

I think we can do something like this:

#define statfs __nolibc_kernel_statfs
#define statfs64 __nolibc_kernel_statfs64
#include <asm/statfs.h>
#undef statfs
#undef statfs64

#define __nolibc_cloned_member(_orig_struct, _name) \
__nolibc_typeof_member(_orig_struct, _name) _name __nolibc_alignof_member(_orig_struct, _name)

struct statfs {
	__nolibc_cloned_member(struct __nolibc_kernel_statfs64, f_type);
	__nolibc_cloned_member(struct __nolibc_kernel_statfs64, f_bsize);
	...
};

And then in nolibc-test add a bunch of assertions which makes sure that the structures are actually compatible. (Fields have the same offsets and the structures are the same size). This way we can reuse the structure definitions from the UAPI definitions but avoid the macro pollution.

We do something similar for the time types, too.

Thomas

#8 lore raw

Re: [PATCH v4 2/3] tools/nolibc: Add statfs()

Hi Thomas,

Quoted 5 lines · Thomas Weißschuh
Quoted 3 lines

Do you think there is a way to do statfs() properly in the future? I actually started using it in a mini (~64KB) userland I am building so I can get Linux to boot in 3.5MB of RAM.

What exactly do you need out of statfs?

Total and used space on mounted filesystems. There doesn't seem to be any other way to get it.

Quoted 21 lines
Quoted 3 lines

I considered just copying the 64bit version of the statfs struct into nolibc so we don't need to use the UAPI header but it seemed like MIPS has a bunch of different versions of it so it wouldn't work on MIPS.

I think we can do something like this:

#define statfs __nolibc_kernel_statfs
#define statfs64 __nolibc_kernel_statfs64
#include <asm/statfs.h>
#undef statfs
#undef statfs64

#define __nolibc_cloned_member(_orig_struct, _name) \
__nolibc_typeof_member(_orig_struct, _name) _name __nolibc_alignof_member(_orig_struct, _name)

struct statfs {
        __nolibc_cloned_member(struct __nolibc_kernel_statfs64, f_type);
        __nolibc_cloned_member(struct __nolibc_kernel_statfs64, f_bsize);
        ...
};
Quoted 7 lines

And then in nolibc-test add a bunch of assertions which makes sure that the structures are actually compatible. (Fields have the same offsets and the structures are the same size). This way we can reuse the structure definitions from the UAPI definitions but avoid the macro pollution.

We do something similar for the time types, too.

Interesting. I'll have a go at this but separately from the fallocate() part.

Cheers,

Daniel

maintainer
#9 lore raw

Re: [PATCH v4 2/3] tools/nolibc: Add statfs()

In reply to Thomas Weißschuh
Quoted 2 lines · Thomas Weißschuh
Quoted 1 line · Daniel Palmer

Add statfs().

BTW Daniel, given the number of hacks the patch relies on, it's important to explain the challenges and choices in the commit message, as this patch is not adding just a wrapper around a syscall number.

Quoted 25 lines
Quoted 25 lines
tools/include/nolibc/sys/statfs.h new file +22 -0
@@ -0,0 +1,50 @@
+/* SPDX-License-Identifier: LGPL-2.1 OR MIT */
+/*
+ * statfs for NOLIBC
+ * Copyright (C) 2026 Daniel Palmer <[email protected]>
+ */
+
+/* make sure to include all global symbols */
+#include "../nolibc.h"
+
+#ifndef _NOLIBC_SYS_STATFS_H
+#define _NOLIBC_SYS_STATFS_H
+
+#include "../sys.h"
+
+/* Some preprocessor hackery to get struct statfs to
+ * always be the 64bit one.
+ */
+#define statfs __nolibc_kernel_statfs
+#define statfs64 __nolibc_kernel_statfs64
+#include <asm/statfs.h>
+#undef statfs
+#undef statfs64
Quoted 8 lines

tools/include/nolibc/sys/statfs.h line 28

Quoted 6 lines
tools/include/nolibc/sys/statfs.h new file +6 -0
+
+#ifdef __NR_statfs64
+#define statfs __nolibc_kernel_statfs64
+#else
+#define statfs __nolibc_kernel_statfs
+#endif

This #define can wreak havoc in user code.

Indeed.

Quoted 3 lines

While it would be nice to have statfs() in nolibc,
I don't see a proper way to work with the current UAPI header. Can we do without statfs() for now?

Well, in its origins, nolibc used to redefine all the structures it needed. It was not great of course and I'm very happy that this period is gone. But here we're facing a perfect case of discrepancy between an exposed kernel structure and the one supposed to be presented to the application.

Here it's not as if the structures were complex, maybe we should just define the application-visible statfs ourselves and be done with it. It's what any application will have to do to use this syscall as long as we don't provide it ourselves anyway, except that we have a better view of the differences between architectures and there's less risks of mistakes if centralized into nolibc.

Willy

#10 lore raw

PATCH v4 3/3 selftests/nolibc: Add a very basic test for fallocate()

In reply to Daniel Palmer

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.

---
 tools/testing/selftests/nolibc/nolibc-test.c | 70 ++++++++++++++++++++
 1 file changed, 70 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +70 -0
index 1db6e8d55c16..2a3b855214bc 100644
@@ -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;
Signature · 1 line
2.53.0
maintainer Acked-by on the v4 series
#11 lore raw
In reply to Daniel Palmer

Hi Daniel,

Quoted 5 lines · Daniel Palmer

While poking around with my "static PIE for nommu" series I found I needed fallocate(). Implementing it turned out a bit more interesting than I thought it would be due to how the offset and size need to be passed on 32bit machines.

Quoted 11 lines

v4:
- Added statfs()...
- Reworked the test a bit to use statfs() to work out if /tmp is a tmpfs or not and skip the test if it isn't. This skips the test on sparc32 where CONFIG_TMPFS=n.
- Adding the needed bits to the sparc32 config to allow tmpfs to be enabled allows it to run and pass the test. I will check the changes and send a patch.
- Cleaned up the if() around deciding how to pass the parameters for fallocate() based on the comments from David and Thomas.
- Test passed on all of the supported targets.

(...)

Thanks, the whole series looks good to me:

Let's wait for Thomas who usually spots finer issues than me ;-)

Thanks,
Willy