[PATCH 1/2] open: new O_REGULAR flag support

Dorjoy Chowdhury posted 2 patches 2 weeks ago
There is a newer version of this series
[PATCH 1/2] open: new O_REGULAR flag support
Posted by Dorjoy Chowdhury 2 weeks ago
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.

Signed-off-by: Dorjoy Chowdhury <dorjoychy111@gmail.com>
---
 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(-)

diff --git a/fs/fcntl.c b/fs/fcntl.c
index f93dbca08435..62ab4ad2b6f5 100644
--- a/fs/fcntl.c
+++ b/fs/fcntl.c
@@ -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));
diff --git a/fs/namei.c b/fs/namei.c
index cf16b6822dd3..365f3cc77e1c 100644
--- a/fs/namei.c
+++ b/fs/namei.c
@@ -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);
diff --git a/fs/open.c b/fs/open.c
index f328622061c5..670cd6b4967a 100644
--- a/fs/open.c
+++ b/fs/open.c
@@ -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. */
diff --git a/include/linux/fcntl.h b/include/linux/fcntl.h
index a332e79b3207..4fd07b0e0a17 100644
--- a/include/linux/fcntl.h
+++ b/include/linux/fcntl.h
@@ -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 \
diff --git a/include/uapi/asm-generic/errno-base.h b/include/uapi/asm-generic/errno-base.h
index 9653140bff92..ea9a96d30737 100644
--- a/include/uapi/asm-generic/errno-base.h
+++ b/include/uapi/asm-generic/errno-base.h
@@ -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
diff --git a/include/uapi/asm-generic/fcntl.h b/include/uapi/asm-generic/fcntl.h
index 613475285643..11e5eadab868 100644
--- a/include/uapi/asm-generic/fcntl.h
+++ b/include/uapi/asm-generic/fcntl.h
@@ -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)
 
-- 
2.52.0
Re: [PATCH 1/2] open: new O_REGULAR flag support
Posted by kernel test robot 2 weeks ago
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/202601260042.TRDQjGeu-lkp@intel.com/config)
compiler: alpha-linux-gcc (GCC) 15.2.0
reproduce (this is a W=1 build): (https://download.01.org/0day-ci/archive/20260126/202601260042.TRDQjGeu-lkp@intel.com/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 <lkp@intel.com>
| Closes: https://lore.kernel.org/oe-kbuild-all/202601260042.TRDQjGeu-lkp@intel.com/

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  

-- 
0-DAY CI Kernel Test Service
https://github.com/intel/lkp-tests/wiki
Re: [PATCH 1/2] open: new O_REGULAR flag support
Posted by Arnd Bergmann 2 weeks ago
On Sun, Jan 25, 2026, at 15:14, Dorjoy Chowdhury wrote:

> diff --git a/include/uapi/asm-generic/errno-base.h 
> b/include/uapi/asm-generic/errno-base.h
> index 9653140bff92..ea9a96d30737 100644
> --- a/include/uapi/asm-generic/errno-base.h
> +++ b/include/uapi/asm-generic/errno-base.h
> @@ -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.

> diff --git a/include/uapi/asm-generic/fcntl.h b/include/uapi/asm-generic/fcntl.h
> index 613475285643..11e5eadab868 100644
> --- a/include/uapi/asm-generic/fcntl.h
> +++ b/include/uapi/asm-generic/fcntl.h
> @@ -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
Posted by Dorjoy Chowdhury 2 weeks ago
On Sun, Jan 25, 2026 at 8:40 PM Arnd Bergmann <arnd@arndb.de> wrote:
>
> On Sun, Jan 25, 2026, at 15:14, Dorjoy Chowdhury wrote:
>
> > diff --git a/include/uapi/asm-generic/errno-base.h
> > b/include/uapi/asm-generic/errno-base.h
> > index 9653140bff92..ea9a96d30737 100644
> > --- a/include/uapi/asm-generic/errno-base.h
> > +++ b/include/uapi/asm-generic/errno-base.h
> > @@ -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.
>

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?

> > diff --git a/include/uapi/asm-generic/fcntl.h b/include/uapi/asm-generic/fcntl.h
> > index 613475285643..11e5eadab868 100644
> > --- a/include/uapi/asm-generic/fcntl.h
> > +++ b/include/uapi/asm-generic/fcntl.h
> > @@ -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.
>

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
Re: [PATCH 1/2] open: new O_REGULAR flag support
Posted by Jeff Layton 2 weeks ago
On Sun, 2026-01-25 at 21:41 +0600, Dorjoy Chowdhury wrote:
> On Sun, Jan 25, 2026 at 8:40 PM Arnd Bergmann <arnd@arndb.de> wrote:
> > 
> > On Sun, Jan 25, 2026, at 15:14, Dorjoy Chowdhury wrote:
> > 
> > > diff --git a/include/uapi/asm-generic/errno-base.h
> > > b/include/uapi/asm-generic/errno-base.h
> > > index 9653140bff92..ea9a96d30737 100644
> > > --- a/include/uapi/asm-generic/errno-base.h
> > > +++ b/include/uapi/asm-generic/errno-base.h
> > > @@ -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.
> > 
> 
> 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?
> 

nit: Can we call this this ENOTREG instead of ENOTREGULAR? That seems
like it would read better alongside ENOTDIR and I hate extra typing.

> > > diff --git a/include/uapi/asm-generic/fcntl.h b/include/uapi/asm-generic/fcntl.h
> > > index 613475285643..11e5eadab868 100644
> > > --- a/include/uapi/asm-generic/fcntl.h
> > > +++ b/include/uapi/asm-generic/fcntl.h
> > > @@ -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.
> > 
> 
> 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!
> 
> 

-- 
Jeff Layton <jlayton@kernel.org>
Re: [PATCH 1/2] open: new O_REGULAR flag support
Posted by Dorjoy Chowdhury 2 weeks ago
On Sun, Jan 25, 2026 at 10:28 PM Jeff Layton <jlayton@kernel.org> wrote:
>
> On Sun, 2026-01-25 at 21:41 +0600, Dorjoy Chowdhury wrote:
> > On Sun, Jan 25, 2026 at 8:40 PM Arnd Bergmann <arnd@arndb.de> wrote:
> > >
> > > On Sun, Jan 25, 2026, at 15:14, Dorjoy Chowdhury wrote:
> > >
> > > > diff --git a/include/uapi/asm-generic/errno-base.h
> > > > b/include/uapi/asm-generic/errno-base.h
> > > > index 9653140bff92..ea9a96d30737 100644
> > > > --- a/include/uapi/asm-generic/errno-base.h
> > > > +++ b/include/uapi/asm-generic/errno-base.h
> > > > @@ -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.
> > >
> >
> > 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?
> >
>
> 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