Skip to contentSkip to conversation

nolibc: Add fallocate()

PATCH Superseded Daniel Palmer 2 participants 5 messages Apr 15

Subsystems (2)

v1 of 4 Superseded Latest is v4 →

lore
Share & tools
lore
#0 lore raw

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.

I have ran the tests on m68k, arm, arm64, riscv[32|64], x86_64, i386, x32. I probably missed something, maybe there is a better way to do this. Maybe it can't actually pass an offset or size

Quoted 1 line

4GB on x32?

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

 tools/include/nolibc/fcntl.h                 | 30 ++++++++++++++++++
 tools/testing/selftests/nolibc/nolibc-test.c | 33 ++++++++++++++++++++
 2 files changed, 63 insertions(+)
Signature · 1 line
2.51.0
#1 lore raw

PATCH 1/2 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 and for x32.

---
 tools/include/nolibc/fcntl.h | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
tools/include/nolibc/fcntl.h +30 -0
index ed2f5553c65a..0ebfdb7bc792 100644
@@ -66,4 +66,34 @@ 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);
+ */
+
+#define __NOLIBC_LLARGPART(_arg, _part) \
+	(((union { long long ll; long l[2]; }) { .ll = _arg }).l[_part])
+
+static __attribute__((unused))
+int sys_fallocate(int fd, int mode, off_t offset, off_t size)
+{
+#if defined(__x86_64__) && defined(__ILP32__)
+	const bool offsetsz_two_args = false;
+#else
+	const bool offsetsz_two_args = sizeof(long) != sizeof(off_t);
+#endif
+
+	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);
+}
+
+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 */
Signature · 1 line
2.51.0
#3 lore raw

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

In reply to Daniel Palmer

Create a tmp file, fallocate() to make it a bit bigger, check the size is what was expected.

---
 tools/testing/selftests/nolibc/nolibc-test.c | 33 ++++++++++++++++++++
 1 file changed, 33 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +33 -0
index d3c4facb54c0..066f436561e7 100644
@@ -896,6 +896,38 @@ 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;
+	}
+
+close_tmpfile:
+	close(fd);
+
+	return r;
+}
+
 int test_file_stream(void)
 {
 	FILE *f;
@@ -1442,6 +1474,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(1, 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.51.0
#2 lore raw

Re: [PATCH 1/2] tools/nolibc: fcntl: Add fallocate()

In reply to Daniel Palmer

Hi Daniel!

thanks for the patches.

Quoted 14 lines · Daniel Palmer

Add fallocate(), some special care is needed to put the offset and size into the syscall parameters for 32bit machines and for x32.

---
 tools/include/nolibc/fcntl.h | 30 ++++++++++++++++++++++++++++++
 1 file changed, 30 insertions(+)
tools/include/nolibc/fcntl.h +0 -0
index ed2f5553c65a..0ebfdb7bc792 100644

tools/include/nolibc/fcntl.h int open(const char *path, int flags, ...) · line 74

Quoted 9 lines
tools/include/nolibc/fcntl.h +6 -0
@@ -66,4 +66,34 @@ 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);
+ */
+
+#define __NOLIBC_LLARGPART(_arg, _part) \
+	(((union { long long ll; long l[2]; }) { .ll = _arg }).l[_part])

This should go into sys.h, as it is fairly generic.

tools/include/nolibc/fcntl.h int open(const char *path, int flags, ...) · line 77

Quoted 2 lines
tools/include/nolibc/fcntl.h +2 -0
+static __attribute__((unused))
+int sys_fallocate(int fd, int mode, off_t offset, off_t size)

This should be _sys_fallocate() to avoid clashing with user symbols.

tools/include/nolibc/fcntl.h int open(const char *path, int flags, ...) · line 83

Quoted 6 lines
tools/include/nolibc/fcntl.h +6 -0
+{
+#if defined(__x86_64__) && defined(__ILP32__)
+	const bool offsetsz_two_args = false;
+#else
+	const bool offsetsz_two_args = sizeof(long) != sizeof(off_t);
+#endif

We try to avoid architecture-specific logic in the generic files. x32 would be covered with sizeof(__kernel_long_t). But MIPS N32 is also detected incorrectly here. In my opinion the best solution would be to have an override mechansim like for _sys_mmap and override it for x32 and N32 in arch-x86.h and arch-mips.h.

Maybe also look at the recent proposal for ftruncate() which is somewhat related:

https://git.kernel.org/pub/scm/linux/kernel/git/thomas.weissschuh/linux.git/commit/?h=b4/nolibc-ftruncate
https://lore.kernel.org/lkml/[email protected]/

Quoted 19 lines
tools/include/nolibc/fcntl.h +15 -0
+
+	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);
+}
+
+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 */
Signature · 1 line
2.51.0
#4 lore raw

Re: [PATCH 2/2] selftests/nolibc: Add a very basic test for fallocate()

In reply to Daniel Palmer
Quoted 31 lines · Daniel Palmer

Create a tmp file, fallocate() to make it a bit bigger, check the size is what was expected.

---
 tools/testing/selftests/nolibc/nolibc-test.c | 33 ++++++++++++++++++++
 1 file changed, 33 insertions(+)
tools/testing/selftests/nolibc/nolibc-test.c +15 -0
index d3c4facb54c0..066f436561e7 100644
@@ -896,6 +896,38 @@ 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;
+

tools/testing/selftests/nolibc/nolibc-test.c int test_getpagesize(void) · line 923

Quoted 10 lines
tools/testing/selftests/nolibc/nolibc-test.c +10 -0
+	/* 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;
+	}

This should also check that full 64-bit values are passed through.

Quoted 6 lines
tools/testing/selftests/nolibc/nolibc-test.c +6 -0
+
+close_tmpfile:
+	close(fd);
+
+	return r;
+}

(...)