O_REGULAR flag support for open
Subsystems (4)
cover O_REGULAR flag support for open
Hi,
I came upon this "Ability to only open regular files" uapi feature suggestion from https://uapi-group.org/kernel-features/#ability-to-only-open-regular-files
and thought it would be something I could do as a first patch and get to know the kernel code a bit better.
I am not quite sure if the semantics that I baked into the code for this O_REGULAR flag's behavior when combined with other flags like O_CREAT look good and if there are other places that need the checks. I can fixup my patch according to suggestions for improvement. I did some happy path testing and the O_REGULAR flag seems to work as intended.
Thanks.
Regards,
Dorjoy
Dorjoy Chowdhury (2):
open: new O_REGULAR flag support
kselftest/openat2: test for O_REGULAR flag
fs/fcntl.c | 2 +- fs/namei.c | 6 +++ fs/open.c | 4 +- include/linux/fcntl.h | 2 +- include/uapi/asm-generic/errno-base.h | 1 + include/uapi/asm-generic/fcntl.h | 4 ++ .../testing/selftests/openat2/openat2_test.c | 37 ++++++++++++++++++- 7 files changed, 52 insertions(+), 4 deletions(-)
Signature · 1 line
2.52.0
PATCH 1/2 open: new O_REGULAR flag support
This flag indicates the path should be opened if it's a regular file. A relevant error code ENOTREGULAR(35) has been introduced. For example, if open is called on path /dev/null with O_REGULAR in the flag param, it will return -ENOTREGULAR.
When used in combination with O_CREAT, either the regular file is created, or if the path already exists, it is opened if it's a regular file. Otherwise, -ENOTREGULAR is returned.
-EINVAL is returned when O_REGULAR is combined with O_DIRECTORY (not part of O_TMPFILE) because it doesn't make sense to open a path that is both a directory and a regular file.
--- fs/fcntl.c | 2 +- fs/namei.c | 6 ++++++ fs/open.c | 4 +++- include/linux/fcntl.h | 2 +- include/uapi/asm-generic/errno-base.h | 1 + include/uapi/asm-generic/fcntl.h | 4 ++++ 6 files changed, 16 insertions(+), 3 deletions(-)
fs/fcntl.c +1 -1
@@ -1169,7 +1169,7 @@ static int __init fcntl_init(void)* Exceptions: O_NONBLOCK is a two bit define on parisc; O_NDELAY * is defined as O_NONBLOCK on some platforms and not on others. */ -BUILD_BUG_ON (20 -1 /* for O_RDONLY being 0 */ != +BUILD_BUG_ON (21 -1 /* for O_RDONLY being 0 */ !=HWEIGHT32 ( (VALID_OPEN_FLAGS & ~(O_NONBLOCK |O_NDELAY )) | __FMODE_EXEC));
fs/namei.c +6 -0
@@ -4617,6 +4617,10 @@ static int do_open(struct nameidata *nd,if (unlikely (error))return error; } + +if ((open_flag &O_REGULAR ) && !d_is_reg (nd->path.dentry)) +return -ENOTREGULAR ; +if ((nd->flags &LOOKUP_DIRECTORY ) && !d_can_lookup (nd->path.dentry))return -ENOTDIR ;
@@ -4766,6 +4770,8 @@ static int do_o_path(struct nameidata *nd, unsigned flags, struct file *file)struct path path;int error =path_lookupat (nd, flags, &path);if (!error) { +if ((file->f_flags &O_REGULAR ) && !d_is_reg (path.dentry)) +return -ENOTREGULAR ;audit_inode (nd->name, path.dentry,0 ); error =vfs_open (&path, file);path_put (&path);
fs/open.c +3 -1
@@ -1212,7 +1212,7 @@ struct file *kernel_file_open(const struct path *path, int flags,EXPORT_SYMBOL_GPL (kernel_file_open);#define WILL_CREATE (flags) (flags & (O_CREAT | __O_TMPFILE)) -#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC) +#define O_PATH_FLAGS (O_DIRECTORY | O_NOFOLLOW | O_PATH | O_CLOEXEC | O_REGULAR)inline struct open_how build_open_how (int flags,umode_t mode) {
@@ -1289,6 +1289,8 @@ inline int build_open_flags(const struct open_how *how, struct open_flags *op)return -EINVAL ;if (!(acc_mode &MAY_WRITE ))return -EINVAL ; + }else if ((flags &O_DIRECTORY ) && (flags &O_REGULAR )) { +return -EINVAL ; }if (flags &O_PATH ) {/* O_PATH only permits certain other flags to be set. */
include/linux/fcntl.h +1 -1
@@ -10,7 +10,7 @@ (O_RDONLY |O_WRONLY |O_RDWR |O_CREAT |O_EXCL |O_NOCTTY |O_TRUNC | \O_APPEND |O_NDELAY |O_NONBLOCK | __O_SYNC |O_DSYNC | \FASYNC |O_DIRECT |O_LARGEFILE |O_DIRECTORY |O_NOFOLLOW | \ -O_NOATIME |O_CLOEXEC |O_PATH | __O_TMPFILE) +O_NOATIME |O_CLOEXEC |O_PATH | __O_TMPFILE |O_REGULAR ) /* List of all valid flags for the how->resolve argument: */#define VALID_RESOLVE_FLAGS \
include/uapi/asm-generic/errno-base.h +1 -0
@@ -36,5 +36,6 @@#define EPIPE 32/* Broken pipe */ #define EDOM 33/* Math argument out of domain of func */ #define ERANGE 34/* Math result not representable */ +#define ENOTREGULAR 35/* Not a regular file */ #endif
include/uapi/asm-generic/fcntl.h +4 -0
@@ -88,6 +88,10 @@#define __O_TMPFILE 020000000#endif +#ifndef O_REGULAR +#define O_REGULAR 040000000 +#endif +/* a horrid kludge trying to make sure that this will fail on old kernels */ #define O_TMPFILE (__O_TMPFILE | O_DIRECTORY)
Signature · 1 line
2.52.0
Arnd Bergmann
maintainerRe: [PATCH 1/2] open: new O_REGULAR flag support
include/uapi/asm-generic/errno-base.h line 39
Quoted 10 lines · Dorjoy Chowdhury
include/uapi/asm-generic/errno-base.h +1
-0@@ -36,5 +36,6 @@#define EPIPE 32/* Broken pipe */ #define EDOM 33/* Math argument out of domain of func */ #define ERANGE 34/* Math result not representable */ +#define ENOTREGULAR 35/* Not a regular file */
This clashes with EDEADLK on most architectures, or with EAGAIN on alpha and ENOMSG on mips/parisc. You probably need to pick the next free value in uapi/asm-generic/errno.h and arch/*/include/uapi/asm/errno.h and keep this sorted after EHWPOISON if you can't find an existing error code.
include/uapi/asm-generic/fcntl.h line 93
Quoted 11 lines
include/uapi/asm-generic/fcntl.h +3
-0@@ -88,6 +88,10 @@#define __O_TMPFILE 020000000#endif +#ifndef O_REGULAR +#define O_REGULAR 040000000 +#endif
This in turn clashes with O_PATH on alpha, __O_TMPFILE on parisc, and __O_SYNC on sparc. We can probably fill the holes in asm/fcntl.h to define this.
Arnd
Re: [PATCH 1/2] open: new O_REGULAR flag support
Quoted 20 lines · Arnd Bergmann
include/uapi/asm-generic/errno-base.h line 39
Quoted 10 lines · Dorjoy Chowdhury
include/uapi/asm-generic/errno-base.h +1
-0@@ -36,5 +36,6 @@#define EPIPE 32/* Broken pipe */ #define EDOM 33/* Math argument out of domain of func */ #define ERANGE 34/* Math result not representable */ +#define ENOTREGULAR 35/* Not a regular file */ This clashes with EDEADLK on most architectures, or with EAGAIN on alpha and ENOMSG on mips/parisc. You probably need to pick the next free value in
uapi/asm-generic/errno.handarch/*/include/uapi/asm/errno.hand keep this sorted after EHWPOISON if you can't find an existing error code.
Thanks for pointing this out. I will fix up in v2 along with other comments (if any). I looked at the existing error codes in uapi/asm-generic/errno.h and didn't notice anything that I could reuse. So if I understand correctly, I will need this new error code in both uapi/asm-generic/errno.h (not in errno-base.h) and in arch/*/include/uapi/asm/errno.h (I see some parallel tools/arch/*/include/uapi/asm/errno.h files too) just after EHWPOISON, right?
Quoted 8 lines
Quoted 8 lines
include/uapi/asm-generic/fcntl.h +0
-0@@ -88,6 +88,10 @@#define __O_TMPFILE 020000000#endif
Quoted 8 lines
include/uapi/asm-generic/fcntl.h line 93
Quoted 3 lines
include/uapi/asm-generic/fcntl.h +3
-0+#ifndef O_REGULAR +#define O_REGULAR 040000000 +#endif This in turn clashes with
O_PATHon alpha,__O_TMPFILEon parisc, and__O_SYNCon sparc. We can probably fill the holes inasm/fcntl.hto define this.
And for this, I will need to just define O_REGULAR in alpha, parisc and sparc too, right?
Good catch on the sparc file, some are octal, some are hexadecimal, easy to miss. Thanks!
Regards,
Dorjoy
Jeff Layton
maintainerRe: [PATCH 1/2] open: new O_REGULAR flag support
Quoted 22 lines · Dorjoy Chowdhury
Quoted 20 lines · Arnd Bergmann
include/uapi/asm-generic/errno-base.h line 39
Quoted 10 lines · Dorjoy Chowdhury
include/uapi/asm-generic/errno-base.h +1
-0@@ -36,5 +36,6 @@#define EPIPE 32/* Broken pipe */ #define EDOM 33/* Math argument out of domain of func */ #define ERANGE 34/* Math result not representable */ +#define ENOTREGULAR 35/* Not a regular file */ This clashes with EDEADLK on most architectures, or with EAGAIN on alpha and ENOMSG on mips/parisc. You probably need to pick the next free value in
uapi/asm-generic/errno.handarch/*/include/uapi/asm/errno.hand keep this sorted after EHWPOISON if you can't find an existing error code.
Quoted 9 lines
Thanks for pointing this out. I will fix up in v2 along with other comments (if any). I looked at the existing error codes in
uapi/asm-generic/errno.hand didn't notice anything that I could reuse. So if I understand correctly, I will need this new error code in bothuapi/asm-generic/errno.h(not inerrno-base.h) and inarch/*/include/uapi/asm/errno.h(I see some paralleltools/arch/*/include/uapi/asm/errno.hfiles too) just after EHWPOISON, right?
nit: Can we call this this ENOTREG instead of ENOTREGULAR? That seems like it would read better alongside ENOTDIR and I hate extra typing.
Quoted 23 lines
Quoted 8 lines
Quoted 8 lines
include/uapi/asm-generic/fcntl.h +0
-0@@ -88,6 +88,10 @@#define __O_TMPFILE 020000000#endif Quoted 8 lines
include/uapi/asm-generic/fcntl.h line 93
Quoted 3 lines
include/uapi/asm-generic/fcntl.h +3
-0+#ifndef O_REGULAR +#define O_REGULAR 040000000 +#endif This in turn clashes with
O_PATHon alpha,__O_TMPFILEon parisc, and__O_SYNCon sparc. We can probably fill the holes inasm/fcntl.hto define this.And for this, I will need to just define
O_REGULARin alpha, parisc and sparc too, right?
Good catch on the sparc file, some are octal, some are hexadecimal, easy to miss. Thanks!
Signature · 1 line
Jeff Layton <[email protected]>
Re: [PATCH 1/2] open: new O_REGULAR flag support
Quoted 34 lines · Jeff Layton
Quoted 22 lines · Dorjoy Chowdhury
Quoted 20 lines · Arnd Bergmann
include/uapi/asm-generic/errno-base.h line 39
Quoted 10 lines · Dorjoy Chowdhury
include/uapi/asm-generic/errno-base.h +1
-0@@ -36,5 +36,6 @@#define EPIPE 32/* Broken pipe */ #define EDOM 33/* Math argument out of domain of func */ #define ERANGE 34/* Math result not representable */ +#define ENOTREGULAR 35/* Not a regular file */ This clashes with EDEADLK on most architectures, or with EAGAIN on alpha and ENOMSG on mips/parisc. You probably need to pick the next free value in
uapi/asm-generic/errno.handarch/*/include/uapi/asm/errno.hand keep this sorted after EHWPOISON if you can't find an existing error code.Quoted 9 lines
Thanks for pointing this out. I will fix up in v2 along with other comments (if any). I looked at the existing error codes in
uapi/asm-generic/errno.hand didn't notice anything that I could reuse. So if I understand correctly, I will need this new error code in bothuapi/asm-generic/errno.h(not inerrno-base.h) and inarch/*/include/uapi/asm/errno.h(I see some paralleltools/arch/*/include/uapi/asm/errno.hfiles too) just after EHWPOISON, right?
Quoted 3 lines
nit: Can we call this this ENOTREG instead of ENOTREGULAR? That seems like it would read better alongside ENOTDIR and I hate extra typing.
Good suggestion. I agree. Will fix up in v2. Thanks!
Regards,
Dorjoy
Arnd Bergmann
maintainerRe: [PATCH 1/2] open: new O_REGULAR flag support
In reply to Dorjoy ChowdhuryQuoted 9 lines · Dorjoy Chowdhury
Thanks for pointing this out. I will fix up in v2 along with other comments (if any). I looked at the existing error codes in
uapi/asm-generic/errno.hand didn't notice anything that I could reuse. So if I understand correctly, I will need this new error code in bothuapi/asm-generic/errno.h(not inerrno-base.h) and inarch/*/include/uapi/asm/errno.h(I see some paralleltools/arch/*/include/uapi/asm/errno.hfiles too) just after EHWPOISON, right?
Yes, sounds good to me.
Quoted 17 lines
Quoted 8 lines
Quoted 8 lines
include/uapi/asm-generic/fcntl.h +0
-0@@ -88,6 +88,10 @@#define __O_TMPFILE 020000000#endif Quoted 8 lines
include/uapi/asm-generic/fcntl.h line 93
Quoted 3 lines
include/uapi/asm-generic/fcntl.h +3
-0+#ifndef O_REGULAR +#define O_REGULAR 040000000 +#endif This in turn clashes with
O_PATHon alpha,__O_TMPFILEon parisc, and__O_SYNCon sparc. We can probably fill the holes inasm/fcntl.hto define this.
Quoted 2 lines
And for this, I will need to just define
O_REGULARin alpha, parisc and sparc too, right?
Yes, the only question is whether to use the first available bit, or the one after the previously last one.
Quoted 2 lines
Good catch on the sparc file, some are octal, some are hexadecimal, easy to miss. Thanks!
Right, I wonder if there are any downsides to redefining them all the same way.
Arnd
kernel test robot
build botRe: [PATCH 1/2] open: new O_REGULAR flag support
In reply to Dorjoy Chowdhurykernel test robot noticed the following build errors:
Hi Dorjoy,
kernel test robot noticed the following build errors:
[auto build test ERROR on brauner-vfs/vfs.all]
[also build test ERROR on shuah-kselftest/next shuah-kselftest/fixes linus/master arnd-asm-generic/master v6.19-rc6 next-20260123] [If your patch is applied to the wrong git tree, kindly drop us a note.
And when submitting patch, we suggest to use '--base' as documented in
https://git-scm.com/docs/git-format-patch#_base_tree_information]
url: https://github.com/intel-lab-lkp/linux/commits/Dorjoy-Chowdhury/open-new-O_REGULAR-flag-support/20260125-221826 base: https://git.kernel.org/pub/scm/linux/kernel/git/vfs/vfs.git vfs.all patch link: https://lore.kernel.org/r/20260125141518.59493-2-dorjoychy111%40gmail.com patch subject: [PATCH 1/2] open: new O_REGULAR flag support config: alpha-allnoconfig (https://download.01.org/0day-ci/archive/20260126/[email protected]/config) compiler: alpha-linux-gcc (GCC) 15.2.0 reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260126/[email protected]/reproduce)
If you fix the issue in a separate patch/commit (i.e. not just a new version of the same patch/commit), kindly add following tags
| Reported-by: kernel test robot <[email protected]>
| Closes: https://lore.kernel.org/oe-kbuild-all/[email protected]/
All errors (new ones prefixed by >>):
In file included from <command-line>:
fs/fcntl.c: In function 'fcntl_init':
>> include/linux/compiler_types.h:631:45: error: call to '__compiletime_assert_389' declared with attribute error: BUILD_BUG_ON failed: 21 - 1 != HWEIGHT32( (VALID_OPEN_FLAGS & ~(O_NONBLOCK | O_NDELAY)) | __FMODE_EXEC)
631 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^
include/linux/compiler_types.h:612:25: note: in definition of macro '__compiletime_assert'
612 | prefix ## suffix(); \
| ^~~~~~
include/linux/compiler_types.h:631:9: note: in expansion of macro '_compiletime_assert'
631 | _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)
| ^~~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:39:37: note: in expansion of macro 'compiletime_assert'
39 | #define BUILD_BUG_ON_MSG(cond, msg) compiletime_assert(!(cond), msg)
| ^~~~~~~~~~~~~~~~~~
include/linux/build_bug.h:50:9: note: in expansion of macro 'BUILD_BUG_ON_MSG'
50 | BUILD_BUG_ON_MSG(condition, "BUILD_BUG_ON failed: " #condition)
| ^~~~~~~~~~~~~~~~
fs/fcntl.c:1172:9: note: in expansion of macro 'BUILD_BUG_ON'
1172 | BUILD_BUG_ON(21 - 1 /* for O_RDONLY being 0 */ !=
| ^~~~~~~~~~~~vim +/__compiletime_assert_389 +631 include/linux/compiler_types.h
eb5c2d4b45e3d2 Will Deacon 2020-07-21 617 eb5c2d4b45e3d2 Will Deacon 2020-07-21 618 #define _compiletime_assert(condition, msg, prefix, suffix) \ eb5c2d4b45e3d2 Will Deacon 2020-07-21 619 __compiletime_assert(condition, msg, prefix, suffix) eb5c2d4b45e3d2 Will Deacon 2020-07-21 620 eb5c2d4b45e3d2 Will Deacon 2020-07-21 621 /** eb5c2d4b45e3d2 Will Deacon 2020-07-21 622 * compiletime_assert - break build and emit msg if condition is false eb5c2d4b45e3d2 Will Deacon 2020-07-21 623 * @condition: a compile-time constant condition to check eb5c2d4b45e3d2 Will Deacon 2020-07-21 624 * @msg: a message to emit if condition is false eb5c2d4b45e3d2 Will Deacon 2020-07-21 625 * eb5c2d4b45e3d2 Will Deacon 2020-07-21 626 * In tradition of POSIX assert, this macro will break the build if the eb5c2d4b45e3d2 Will Deacon 2020-07-21 627 * supplied condition is *false*, emitting the supplied error message if the eb5c2d4b45e3d2 Will Deacon 2020-07-21 628 * compiler has support to do so. eb5c2d4b45e3d2 Will Deacon 2020-07-21 629 */ eb5c2d4b45e3d2 Will Deacon 2020-07-21 630 #define compiletime_assert(condition, msg) \
eb5c2d4b45e3d2 Will Deacon 2020-07-21 @631 _compiletime_assert(condition, msg, __compiletime_assert_, __COUNTER__)eb5c2d4b45e3d2 Will Deacon 2020-07-21 632
Signature · 2 lines
0-DAY CI Kernel Test Service https://github.com/intel/lkp-tests/wiki
PATCH 2/2 kselftest/openat2: test for O_REGULAR flag
In reply to Dorjoy ChowdhuryJust a happy path test.
--- .../testing/selftests/openat2/openat2_test.c | 37 ++++++++++++++++++- 1 file changed, 36 insertions(+), 1 deletion(-)
tools/testing/selftests/openat2/openat2_test.c +36 -1
@@ -320,8 +320,42 @@ void test_openat2_flags(void) } } +#ifndef O_REGULAR +#define O_REGULAR 040000000 +#endif + +#ifndef ENOTREGULAR +#define ENOTREGULAR 35 +#endif + +void test_openat2_o_regular_flag (void ) +{ +if (!openat2_supported) { +ksft_test_result_skip ("Skipping %s as openat2 is not supported\n" , __func__); +return ; + } + +struct open_how how = { + .flags =O_REGULAR |O_RDONLY + }; + +int fd =sys_openat2 (AT_FDCWD ,"/dev/null" , &how); + +if (fd ==ENOENT ) { +ksft_test_result_skip ("Skipping %s as there is no /dev/null\n" , __func__); +return ; + } + +if (fd != -ENOTREGULAR ) { +ksft_test_result_fail ("openat2 should return ENOTREGULAR\n" ); +return ; + } + +ksft_test_result_pass ("%s succeeded\n" , __func__); +} +#define NUM_TESTS (NUM_OPENAT2_STRUCT_VARIATIONS * NUM_OPENAT2_STRUCT_TESTS + \ - NUM_OPENAT2_FLAG_TESTS) + NUM_OPENAT2_FLAG_TESTS + 1)int main (int argc,char **argv) {
@@ -330,6 +364,7 @@ int main(int argc, char **argv)test_openat2_struct ();test_openat2_flags (); +test_openat2_o_regular_flag ();if (ksft_get_fail_cnt () +ksft_get_error_cnt () >0 )ksft_exit_fail ();
Signature · 1 line
2.52.0