From: Daniel Palmer <daniel@thingy.jp>
To: w@1wt.eu, linux@weissschuh.net
Cc: linux-kernel@vger.kernel.org, Daniel Palmer <daniel@thingy.jp>
Subject: [PATCH v2 1/2] tools/nolibc: fcntl: Add fallocate()
Date: Fri, 17 Apr 2026 20:26:21 +0900 [thread overview]
Message-ID: <20260417112622.1182216-2-daniel@thingy.jp> (raw)
In-Reply-To: <20260417112622.1182216-1-daniel@thingy.jp>
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.
Signed-off-by: Daniel Palmer <daniel@thingy.jp>
---
tools/include/nolibc/arch-mips.h | 11 +++++++++++
tools/include/nolibc/fcntl.h | 32 ++++++++++++++++++++++++++++++++
tools/include/nolibc/sys.h | 8 ++++++++
3 files changed, 51 insertions(+)
diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-mips.h
index bb9d580ea1b1..2c5cda632911 100644
--- a/tools/include/nolibc/arch-mips.h
+++ b/tools/include/nolibc/arch-mips.h
@@ -6,6 +6,7 @@
#ifndef _NOLIBC_ARCH_MIPS_H
#define _NOLIBC_ARCH_MIPS_H
+#include <linux/unistd.h>
#include "compiler.h"
#include "crt.h"
@@ -252,6 +253,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
diff --git a/tools/include/nolibc/fcntl.h b/tools/include/nolibc/fcntl.h
index ed2f5553c65a..e65be2f84cbd 100644
--- a/tools/include/nolibc/fcntl.h
+++ b/tools/include/nolibc/fcntl.h
@@ -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]);
*/
@@ -66,4 +69,33 @@ int open(const char *path, int flags, ...)
return __sysret(_sys_open(path, flags, 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 it for 64 machines we can use the values as-is
+ */
+ const bool offsetsz_two_args = sizeof(__kernel_long_t) != sizeof(off_t);
+
+ if (offsetsz_two_args)
+ 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 */
diff --git a/tools/include/nolibc/sys.h b/tools/include/nolibc/sys.h
index 6335fd51f07f..4f4491af9426 100644
--- a/tools/include/nolibc/sys.h
+++ b/tools/include/nolibc/sys.h
@@ -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
--
2.51.0
next prev parent reply other threads:[~2026-04-17 11:26 UTC|newest]
Thread overview: 4+ messages / expand[flat|nested] mbox.gz Atom feed top
2026-04-17 11:26 [PATCH v2 0/2] nolibc: " Daniel Palmer
2026-04-17 11:26 ` Daniel Palmer [this message]
2026-04-17 11:26 ` [PATCH v2 2/2] selftests/nolibc: Add a very basic test for fallocate() Daniel Palmer
2026-04-27 20:01 ` [PATCH v2 0/2] nolibc: Add fallocate() Thomas Weißschuh
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=20260417112622.1182216-2-daniel@thingy.jp \
--to=daniel@thingy.jp \
--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
Powered by JetHome