public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
From: Willy Tarreau <w@1wt.eu>
To: David Laight <david.laight.linux@gmail.com>
Cc: Daniel Palmer <daniel@thingy.jp>,
	linux@weissschuh.net, linux-kernel@vger.kernel.org
Subject: Re: [PATCH v3 1/2] tools/nolibc: fcntl: Add fallocate()
Date: Sat, 2 May 2026 05:00:06 +0200	[thread overview]
Message-ID: <afVotmWPrgPrQ9RZ@1wt.eu> (raw)
In-Reply-To: <20260501091831.7abc39f1@pumpkin>

On Fri, May 01, 2026 at 09:18:31AM +0100, David Laight wrote:
> On Fri,  1 May 2026 01:41:24 +0900
> Daniel Palmer <daniel@thingy.jp> wrote:
> 
> > 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     | 33 ++++++++++++++++++++++++++++++++
> >  tools/include/nolibc/sys.h       |  8 ++++++++
> >  3 files changed, 52 insertions(+)
> > 
> > diff --git a/tools/include/nolibc/arch-mips.h b/tools/include/nolibc/arch-mips.h
> > index 1400653c76c1..e4e42f2bcaf4 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"
> > @@ -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
> > diff --git a/tools/include/nolibc/fcntl.h b/tools/include/nolibc/fcntl.h
> > index 014910a8e928..dbc99188a49e 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]);
> >   */
> > @@ -80,4 +83,34 @@ 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.
> > +	 */
> > +	const bool offsetsz_two_args = sizeof(__kernel_long_t) != sizeof(off_t);
> 
> I don't think you care about the size of off_t.
> Were it to be 4 the code would be badly wrong.
> 
> > +
> > +	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);
> > +}
> 
> The above might be more readable as:
> 	if (sizeof(__kernel_long_t) == 8)
> 		/* 64 bit, values fit in single arguments */
> 		return __nolibc_syscall4(__NR_fallocate, fd, mode, offset, size);
> 
> 	/* 32 bit, values need splitting, order depends on endianness */
> 	/* This test for endianness doesn't rely on any pre-processor defines */
> 	if (({union {int x; char c;} u; u.x = 1; u.c;}))
> 		/* Little endian */
> 		return __nolibc_syscall6(__NR_fallocate, fd, mode,
> 			offset, offset >> 32, size, size >> 32);
> 	/* Big endian */
> 	return __nolibc_syscall6(__NR_fallocate, fd, mode,
> 		offset >> 32, offset, size >> 32, size);

Honestly David, I find Daniel's version way more readable :-) Precisely
because the repeated variations are abstracted with this more readable
macro. If it was used only once I could possibly agree. Even the
endianness test is hard to read, better rely on __BYTE_ORDER__ for
this.

Cheers,
Willy

  reply	other threads:[~2026-05-02  3:00 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-04-30 16:41 [PATCH v3 0/2] nolibc: Add fallocate() Daniel Palmer
2026-04-30 16:41 ` [PATCH v3 1/2] tools/nolibc: fcntl: " Daniel Palmer
2026-05-01  8:18   ` David Laight
2026-05-02  3:00     ` Willy Tarreau [this message]
2026-05-02 21:26       ` David Laight
2026-05-03 16:28         ` Thomas Weißschuh
2026-05-03 22:38           ` David Laight
2026-04-30 16:41 ` [PATCH v3 2/2] selftests/nolibc: Add a very basic test for fallocate() Daniel Palmer
2026-05-02  3:04   ` Willy Tarreau
2026-05-02  4:00     ` Daniel Palmer
2026-05-02  4:40       ` Willy Tarreau
2026-05-03 16:20   ` Thomas Weißschuh
2026-05-02  3:05 ` [PATCH v3 0/2] nolibc: Add fallocate() Willy Tarreau
2026-05-03 16:21 ` Thomas Weißschuh
2026-05-04  1:46   ` Daniel Palmer
2026-05-04 15:33     ` Thomas Weißschuh
2026-05-05  2:20       ` Daniel Palmer

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=afVotmWPrgPrQ9RZ@1wt.eu \
    --to=w@1wt.eu \
    --cc=daniel@thingy.jp \
    --cc=david.laight.linux@gmail.com \
    --cc=linux-kernel@vger.kernel.org \
    --cc=linux@weissschuh.net \
    /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