vfs: mkdirat_fd() syscall
Subsystems (7)
cover vfs: mkdirat_fd() syscall
This series implements the mkdirat_fd() syscall that was suggested over at the UAPI group kernel feature page [1] with some tests.
Obviously, if we want this we should also implement mknodeat_fd() and symlinkat_fd(), but their implementation can be done quite similar I believe.
I have added an unigned int flags like [2] suggests and an example flag that we may want to remove (it right now mainly serves an internal purpose). But it marks where I would want to place the definitions.
This has been compiled and tested on x86 only. [2] is a bit confusing here and there, so I hope I have added the proper syscall definitions everywhere where they needs to be added.
[1]: https://github.com/uapi-group/kernel-features?tab=readme-ov-file#race-free-creation-and-opening-of-non-file-inodes
[2]: https://www.kernel.org/doc/html/latest/process/adding-syscalls.html
Jori Koolstra (2):
vfs: syscalls: add mkdirat_fd()
selftest: add tests for mkdirat_fd()
arch/x86/entry/syscalls/syscall_64.tbl | 1 + fs/internal.h | 1 + fs/namei.c | 26 +++- include/linux/fcntl.h | 2 + include/linux/syscalls.h | 2 + include/uapi/asm-generic/fcntl.h | 3 + include/uapi/asm-generic/unistd.h | 5 +- scripts/syscall.tbl | 1 + tools/include/uapi/asm-generic/unistd.h | 5 +- tools/testing/selftests/filesystems/Makefile | 4 +- .../selftests/filesystems/mkdirat_fd_test.c | 139 ++++++++++++++++++ 11 files changed, 183 insertions(+), 6 deletions(-) create mode 100644 tools/testing/selftests/filesystems/mkdirat_fd_test.c
Signature · 1 line
2.53.0
RFC PATCH 1/2 vfs: syscalls: add mkdirat_fd()
Currently there is no way to race-freely create and open a directory. For regular files we have open(O_CREAT) for creating a new file inode, and returning a pinning fd to it. The lack of such functionality for directories means that when populating a directory tree there's always a race involved: the inodes first need to be created, and then opened to adjust their permissions/ownership/labels/timestamps/acls/xattrs/..., but in the time window between the creation and the opening they might be replaced by something else.
Addressing this race without proper APIs is possible (by immediately fstat()ing what was opened, to verify that it has the right inode type), but difficult to get right. Hence, mkdirat_fd() that creates a directory and returns an O_DIRECTORY fd is useful.
This feature idea (and description) is taken from the UAPI group:
https://github.com/uapi-group/kernel-features?tab=readme-ov-file#race-free-creation-and-opening-of-non-file-inodes
--- arch/x86/entry/syscalls/syscall_64.tbl | 1 + fs/internal.h | 1 + fs/namei.c | 26 ++++++++++++++++++++++++-- include/linux/fcntl.h | 2 ++ include/linux/syscalls.h | 2 ++ include/uapi/asm-generic/fcntl.h | 3 +++ include/uapi/asm-generic/unistd.h | 5 ++++- scripts/syscall.tbl | 1 + 8 files changed, 38 insertions(+), 3 deletions(-)
arch/x86/entry/syscalls/syscall_64.tbl +1 -0
@@ -396,6 +396,7 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mkdirat_fd sys_mkdirat_fd # # Due to a historical design error, certain syscalls are numbered differently
fs/internal.h +1 -0
@@ -58,6 +58,7 @@ int filename_unlinkat(int dfd, struct filename *name);int may_linkat (struct mnt_idmap *idmap,const struct path *link);int filename_renameat2 (int olddfd,struct filename *oldname,int newdfd,struct filename *newname,unsigned int flags); +int filename_mkdirat_fd (int dfd,struct filename *name,umode_t mode,unsigned int flags);int filename_mkdirat (int dfd,struct filename *name,umode_t mode);int filename_mknodat (int dfd,struct filename *name,umode_t mode,unsigned int dev);int filename_symlinkat (struct filename *from,int newdfd,struct filename *to);
fs/namei.c +24 -2
@@ -5256,6 +5256,11 @@ struct dentry *vfs_mkdir(struct mnt_idmap *idmap, struct inode *dir,EXPORT_SYMBOL (vfs_mkdir);int filename_mkdirat (int dfd,struct filename *name,umode_t mode) +{ +return filename_mkdirat_fd (dfd, name, mode,0 ); +} + +int filename_mkdirat_fd (int dfd,struct filename *name,umode_t mode,unsigned int flags) {struct dentry *dentry;struct path path;
@@ -5263,7 +5268,7 @@ int filename_mkdirat(int dfd, struct filename *name, umode_t mode)unsigned int lookup_flags =LOOKUP_DIRECTORY ;struct delegated_inode delegated_inode = { }; -retry: +start: dentry =filename_create (dfd, name, &path, lookup_flags);if (IS_ERR (dentry))return PTR_ERR (dentry);
@@ -5276,7 +5281,6 @@ int filename_mkdirat(int dfd, struct filename *name, umode_t mode)if (IS_ERR (dentry)) error =PTR_ERR (dentry); } -end_creating_path (&path, dentry);if (is_delegated (&delegated_inode)) { error =break_deleg_wait (&delegated_inode);if (!error)
@@ -5286,7 +5290,25 @@ int filename_mkdirat(int dfd, struct filename *name, umode_t mode) lookup_flags |=LOOKUP_REVAL ; goto retry; } + +if (!error && (flags &MKDIRAT_FD_NEED_FD )) { +struct path new_path = { .mnt = path.mnt, .dentry = dentry }; + error =FD_ADD (0 ,dentry_open (&new_path,O_DIRECTORY ,current_cred ())); + } +end_creating_path (&path, dentry);return error; +retry: +end_creating_path (&path, dentry); + goto start; +} + +SYSCALL_DEFINE4(mkdirat_fd, int, dfd, const char __user *, pathname, umode_t, mode, + unsigned int, flags) +{ +CLASS (filename, name)(pathname); +if (flags & ~VALID_MKDIRAT_FD_FLAGS ) +return -EINVAL ; +return filename_mkdirat_fd (dfd, name, mode, flags |MKDIRAT_FD_NEED_FD ); } SYSCALL_DEFINE3(mkdirat, int, dfd, const char __user *, pathname, umode_t, mode)
include/linux/fcntl.h +2 -0
@@ -25,6 +25,8 @@#define force_o_largefile () (!IS_ENABLED(CONFIG_ARCH_32BIT_OFF_T))#endif +#define VALID_MKDIRAT_FD_FLAGS (MKDIRAT_FD_NEED_FD) +#if BITS_PER_LONG ==32 #define IS_GETLK32 (cmd) ((cmd) == F_GETLK)#define IS_SETLK32 (cmd) ((cmd) == F_SETLK)
include/linux/syscalls.h +2 -0
@@ -999,6 +999,8 @@ asmlinkage long sys_lsm_get_self_attr(unsigned int attr, struct lsm_ctx __user *asmlinkage long sys_lsm_set_self_attr (unsigned int attr,struct lsm_ctx __user *ctx,u32 size,u32 flags);asmlinkage long sys_lsm_list_modules (u64 __user *ids,u32 __user *size,u32 flags); +asmlinkage long sys_mkdirat_fd (int dfd,const char __user *pathname,umode_t mode, +unsigned int flags)/* * Architecture-specific system calls
include/uapi/asm-generic/fcntl.h +3 -0
@@ -95,6 +95,9 @@#define O_NDELAY O_NONBLOCK#endif +/* Flags for mkdirat_fd */ +#define MKDIRAT_FD_NEED_FD 0x01 +#define F_DUPFD 0/* dup */ #define F_GETFD 1/* get close_on_exec */ #define F_SETFD 2/* set/clear close_on_exec */
include/uapi/asm-generic/unistd.h +4 -1
@@ -863,8 +863,11 @@ __SYSCALL(__NR_listns, sys_listns)#define __NR_rseq_slice_yield 471 __SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield) +#define __NR_mkdirat_fd 472 +__SYSCALL(__NR_mkdirat_fd, sys_mkdirat_fd) +#undef __NR_syscalls -#define __NR_syscalls 472 +#define __NR_syscalls 473/* * 32 bit systems traditionally used different
scripts/syscall.tbl +1 -0
@@ -412,3 +412,4 @@ 469 common file_setattr sys_file_setattr 470 common listns sys_listns 471 common rseq_slice_yield sys_rseq_slice_yield +472 common mkdirat_fd sys_mkdirat_fd
Signature · 1 line
2.53.0
Arnd Bergmann
maintainerRe: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
Quoted 14 lines · Jori Koolstra
Currently there is no way to race-freely create and open a directory. For regular files we have
open(O_CREAT)for creating a new file inode, and returning a pinning fd to it. The lack of such functionality for directories means that when populating a directory tree there's always a race involved: the inodes first need to be created, and then opened to adjust their permissions/ownership/labels/timestamps/acls/xattrs/..., but in the time window between the creation and the opening they might be replaced by something else.Addressing this race without proper APIs is possible (by immediately fstat()ing what was opened, to verify that it has the right inode type), but difficult to get right. Hence,
mkdirat_fd()that creates a directory and returns anO_DIRECTORYfd is useful.
Quoted 4 lines
This feature idea (and description) is taken from the UAPI group:
https://github.com/uapi-group/kernel-features?tab=readme-ov-file#race-free-creation-and-opening-of-non-file-inodesSigned-off-by Jori Koolstra <[email protected]>
I checked that the calling conventions are fine, i.e. this will work as expected across all architectures. I assume you are also aware that the non-RFC patch will need to add the syscall number to all .tbl files.
The hardest problem here does seem to be the naming of the new syscall, and I'm sorry to not be able to offer any solution either, just two observations:
- mkdirat/mkdirat_fd sounds similar to the existing quotactl/quotactl_fd pair, but quotactl_fd() takes a file descriptor argument rather than returning it, which makes this addition quite confusing.
- the nicest interface IMO would have been a variation of openat(dfd, filename, O_CREAT | O_DIRECTORY, mode) but that is a minefield of incompatible implementations[1], so we can't do that without changing the behavior for existing callers that currently run into an error.
Arnd
Re: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
Quoted 34 lines · On Tue, 31 Mar 2026 21:13:34 +0200 "Arnd Bergmann" <[email protected]> wrote:
Quoted 14 lines · Jori Koolstra
Currently there is no way to race-freely create and open a directory. For regular files we have
open(O_CREAT)for creating a new file inode, and returning a pinning fd to it. The lack of such functionality for directories means that when populating a directory tree there's always a race involved: the inodes first need to be created, and then opened to adjust their permissions/ownership/labels/timestamps/acls/xattrs/..., but in the time window between the creation and the opening they might be replaced by something else.Addressing this race without proper APIs is possible (by immediately fstat()ing what was opened, to verify that it has the right inode type), but difficult to get right. Hence,
mkdirat_fd()that creates a directory and returns anO_DIRECTORYfd is useful.Quoted 4 lines
This feature idea (and description) is taken from the UAPI group:
https://github.com/uapi-group/kernel-features?tab=readme-ov-file#race-free-creation-and-opening-of-non-file-inodesSigned-off-by Jori Koolstra <[email protected]>I checked that the calling conventions are fine, i.e. this will work as expected across all architectures. I assume you are also aware that the non-RFC patch will need to add the syscall number to all .tbl files.
The hardest problem here does seem to be the naming of the new syscall, and I'm sorry to not be able to offer any solution either, just two observations:
- mkdirat/mkdirat_fd sounds similar to the existing quotactl/quotactl_fd pair, but
quotactl_fd()takes a file descriptor argument rather than returning it, which makes this addition quite confusing.
Quoted 5 lines
- the nicest interface IMO would have been a variation of
openat(dfd, filename, O_CREAT | O_DIRECTORY, mode)but that is a minefield of incompatible implementations[1], so we can't do that without changing the behavior for existing callers that currently run into an error.
Just require O_TMPFILE to be set as well :-) You know you'll never regret it one Apr-1 is over.
Can something be done with the flags to openat2(). That might save allocating an extra system call.
David
Quoted 5 lines
Arnd
Re: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
In reply to Jori KoolstraHi,
Le 31/03/2026 à 19:19, Jori Koolstra a écrit :
Quoted 34 lines
Currently there is no way to race-freely create and open a directory. For regular files we have
open(O_CREAT)for creating a new file inode, and returning a pinning fd to it. The lack of such functionality for directories means that when populating a directory tree there's always a race involved: the inodes first need to be created, and then opened to adjust their permissions/ownership/labels/timestamps/acls/xattrs/..., but in the time window between the creation and the opening they might be replaced by something else.Addressing this race without proper APIs is possible (by immediately fstat()ing what was opened, to verify that it has the right inode type), but difficult to get right. Hence,
mkdirat_fd()that creates a directory and returns anO_DIRECTORYfd is useful.This feature idea (and description) is taken from the UAPI group:
https://github.com/uapi-group/kernel-features?tab=readme-ov-file#race-free-creation-and-opening-of-non-file-inodesSigned-off-by Jori Koolstra <[email protected]>--- arch/x86/entry/syscalls/syscall_64.tbl | 1 + fs/internal.h | 1 + fs/namei.c | 26 ++++++++++++++++++++++++-- include/linux/fcntl.h | 2 ++ include/linux/syscalls.h | 2 ++ include/uapi/asm-generic/fcntl.h | 3 +++ include/uapi/asm-generic/unistd.h | 5 ++++- scripts/syscall.tbl | 1 + 8 files changed, 38 insertions(+), 3 deletions(-)include/linux/fcntl.h +0
-0
include/linux/fcntl.h line 29
Quoted 5 lines
include/linux/fcntl.h +2
-0@@ -25,6 +25,8 @@#define force_o_largefile () (!IS_ENABLED(CONFIG_ARCH_32BIT_OFF_T))#endif +#define VALID_MKDIRAT_FD_FLAGS (MKDIRAT_FD_NEED_FD) +
I don't see support for O_CLOEXEC-ish flag, is the file descriptor in close-on-exec mode by default ? If yes, it should be mentioned.
include/uapi/asm-generic/fcntl.h line 100
Quoted 11 lines
include/uapi/asm-generic/fcntl.h +3
-0@@ -95,6 +95,9 @@#define O_NDELAY O_NONBLOCK#endif +/* Flags for mkdirat_fd */ +#define MKDIRAT_FD_NEED_FD 0x01 +
Regards.
H. Peter Anvin
reviewerRe: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
Quoted 45 lines · Yann Droneaud
Hi,
Le 31/03/2026 à 19:19, Jori Koolstra a écrit :
Quoted 34 lines
Currently there is no way to race-freely create and open a directory. For regular files we have
open(O_CREAT)for creating a new file inode, and returning a pinning fd to it. The lack of such functionality for directories means that when populating a directory tree there's always a race involved: the inodes first need to be created, and then opened to adjust their permissions/ownership/labels/timestamps/acls/xattrs/..., but in the time window between the creation and the opening they might be replaced by something else.Addressing this race without proper APIs is possible (by immediately fstat()ing what was opened, to verify that it has the right inode type), but difficult to get right. Hence,
mkdirat_fd()that creates a directory and returns anO_DIRECTORYfd is useful.This feature idea (and description) is taken from the UAPI group:
https://github.com/uapi-group/kernel-features?tab=readme-ov-file#race-free-creation-and-opening-of-non-file-inodesSigned-off-by Jori Koolstra <[email protected]>--- arch/x86/entry/syscalls/syscall_64.tbl | 1 + fs/internal.h | 1 + fs/namei.c | 26 ++++++++++++++++++++++++-- include/linux/fcntl.h | 2 ++ include/linux/syscalls.h | 2 ++ include/uapi/asm-generic/fcntl.h | 3 +++ include/uapi/asm-generic/unistd.h | 5 ++++- scripts/syscall.tbl | 1 + 8 files changed, 38 insertions(+), 3 deletions(-)include/linux/fcntl.h +0
-0include/linux/fcntl.h line 28
Quoted 4 lines
include/linux/fcntl.h +1
-0@@ -25,6 +25,8 @@#define force_o_largefile () (!IS_ENABLED(CONFIG_ARCH_32BIT_OFF_T))#endif +#define VALID_MKDIRAT_FD_FLAGS (MKDIRAT_FD_NEED_FD) +I don't see support for O_CLOEXEC-ish flag, is the file descriptor in close-on-exec mode by default ? If yes, it should be mentioned.
Quoted 15 lines
include/uapi/asm-generic/fcntl.h line 99
Quoted 10 lines
include/uapi/asm-generic/fcntl.h +2
-0@@ -95,6 +95,9 @@#define O_NDELAY O_NONBLOCK#endif +/* Flags for mkdirat_fd */ +#define MKDIRAT_FD_NEED_FD 0x01 +Regards.
And even if it is, POSIX already has O_CLOFORK and we should expect that that will be needed, too.
Re: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
In reply to Jori Koolstrafs/namei.c int filename_mkdirat(int dfd, struct filename *name, umode_t mode) · line 5299
Quoted 11 lines · Jori Koolstra
fs/namei.c +6
-0@@ -5286,7 +5290,25 @@ int filename_mkdirat(int dfd, struct filename *name, umode_t mode) lookup_flags |=LOOKUP_REVAL ; goto retry; } + +if (!error && (flags &MKDIRAT_FD_NEED_FD )) { +struct path new_path = { .mnt = path.mnt, .dentry = dentry }; + error =FD_ADD (0 ,dentry_open (&new_path,O_DIRECTORY ,current_cred ())); + } +end_creating_path (&path, dentry);return error;
You can't do it like this. Should it turn out no fd can be allocated, the entire thing is going to error out while keeping the newly created directory behind. You need to allocate the fd first, then do the hard work, and only then fd_install and or free the fd. The FD_ADD machinery can probably still be used provided proper wrapping of the real new mkdir.
It should be perfectly feasible to de facto wrap existing mkdir functionality by this syscall.
On top of that similarly to what other people mentioned the new syscall will definitely want to support O_CLOEXEC and probably other flags down the line.
Trying to handle this in open() is a no-go. openat2 is rather problematic.
I tend to agree mkdirat_fd is not a good name for the syscall either, but I don't have a suggestion I'm happy with. I think least bad name would follow the existing stuff and be mkdirat2 or similar.
The routine would have to start with validating the passed O_ flags, for now only allowing O_CLOEXEC and EINVAL-ing otherwise.
Re: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
Hi!
Quoted 3 lines
I tend to agree
mkdirat_fdis not a good name for the syscall either, but I don't have a suggestion I'm happy with. I think least bad name would follow the existing stuff and be mkdirat2 or similar.
Why not mkdirat_open() as it does combine these two syscalls into one?
Signature · 2 lines
Cyril Hrubis [email protected]
Re: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
In reply to Mateusz GuzikQuoted 18 lines
Op 01-04-2026 06:19 CEST schreef Mateusz Guzik <[email protected]>:
fs/namei.c int filename_mkdirat(int dfd, struct filename *name, umode_t mode) · line 5299
Quoted 11 lines · Jori Koolstra
fs/namei.c +6
-0@@ -5286,7 +5290,25 @@ int filename_mkdirat(int dfd, struct filename *name, umode_t mode) lookup_flags |=LOOKUP_REVAL ; goto retry; } + +if (!error && (flags &MKDIRAT_FD_NEED_FD )) { +struct path new_path = { .mnt = path.mnt, .dentry = dentry }; + error =FD_ADD (0 ,dentry_open (&new_path,O_DIRECTORY ,current_cred ())); + } +end_creating_path (&path, dentry);return error;
Quoted 6 lines
You can't do it like this. Should it turn out no fd can be allocated, the entire thing is going to error out while keeping the newly created directory behind. You need to allocate the fd first, then do the hard work, and only then
fd_installand or free the fd. TheFD_ADDmachinery can probably still be used provided proper wrapping of the real new mkdir.
But isn't this exactly what happens in open(O_CREAT) too? Eventually we call
error = dir_inode->i_op->create(idmap, dir_inode, dentry, mode, open_flag & O_EXCL);
and only then do we assign and install the fd. AFAIK there is no cleanup happening there either if the FD_ADD step fails. You will just have a regular file and no descriptor. But I would have to test this to be sure.
Quoted 5 lines
On top of that similarly to what other people mentioned the new syscall will definitely want to support
O_CLOEXECand probably other flags down the line.
I agree, and perhaps O_PATH too. Maybe just all open flags relevant to directories?
Quoted 2 lines
Trying to handle this in
open()is a no-go. openat2 is rather problematic.
I don't think that is necessarily true. It turned out O_CREAT | O_DIRECTORY was bugged for a very long time. Christian Brauner fixed it eventually, and that combination now returns EINVAL. But I think there is nothing really stopping us from implementing that combination in the expected way, apart from whatever reasons there were for not allowing this in the first place, which I don't know about (maybe mixing semantics?)
Quoted 7 lines
I tend to agree
mkdirat_fdis not a good name for the syscall either, but I don't have a suggestion I'm happy with. I think least bad name would follow the existing stuff and be mkdirat2 or similar.The routine would have to start with validating the passed O_ flags, for now only allowing
O_CLOEXECand EINVAL-ing otherwise.
Thanks,
Jori
Re: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
Quoted 26 lines · Jori Koolstra
Quoted 19 lines
Op 01-04-2026 06:19 CEST schreef Mateusz Guzik <[email protected]>:
fs/namei.c int filename_mkdirat(int dfd, struct filename *name, umode_t mode) · line 5299
Quoted 11 lines · Jori Koolstra
fs/namei.c +6
-0@@ -5286,7 +5290,25 @@ int filename_mkdirat(int dfd, struct filename *name, umode_t mode) lookup_flags |=LOOKUP_REVAL ; goto retry; } + +if (!error && (flags &MKDIRAT_FD_NEED_FD )) { +struct path new_path = { .mnt = path.mnt, .dentry = dentry }; + error =FD_ADD (0 ,dentry_open (&new_path,O_DIRECTORY ,current_cred ())); + } +end_creating_path (&path, dentry);return error;Quoted 6 lines
You can't do it like this. Should it turn out no fd can be allocated, the entire thing is going to error out while keeping the newly created directory behind. You need to allocate the fd first, then do the hard work, and only then
fd_installand or free the fd. TheFD_ADDmachinery can probably still be used provided proper wrapping of the real new mkdir.
Quoted 9 lines
But isn't this exactly what happens in
open(O_CREAT)too? Eventually we callerror = dir_inode->i_op->create(idmap, dir_inode, dentry, mode, open_flag & O_EXCL);and only then do we assign and install the fd. AFAIK there is no cleanup happening there either if the
FD_ADDstep fails. You will just have a regular file and no descriptor. But I would have to test this to be sure.
FD_ADD(how->flags, do_file_open(dfd, name, &op)) means fd itself will be allocated upfront and only then file creation will happen and which is what I'm saying is how it should be done. With your patch the directory is created first and the possibly failing fd allocation happens later.
Quoted 9 lines
Quoted 5 lines
On top of that similarly to what other people mentioned the new syscall will definitely want to support
O_CLOEXECand probably other flags down the line.I agree, and perhaps
O_PATHtoo. Maybe just all open flags relevant to directories?
I don't know about O_PATH as is, but certainly the syscall needs to be able to grab more flags in the future.
Quoted 10 lines
Quoted 2 lines
Trying to handle this in
open()is a no-go. openat2 is rather problematic.I don't think that is necessarily true. It turned out
O_CREAT|O_DIRECTORYwas bugged for a very long time. Christian Brauner fixed it eventually, and that combination now returns EINVAL. But I think there is nothing really stopping us from implementing that combination in the expected way, apart from whatever reasons there were for not allowing this in the first place, which I don't know about (maybe mixing semantics?)
I am not saying it's impossible. I am saying mkdir was always a separate codepath and in order to change that you would need to add a branchfest to open. I don't see any reason to go that route.
Re: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
Quoted 41 lines
Op 07-04-2026 11:00 CEST schreef Mateusz Guzik <[email protected]>:
Quoted 26 lines · Jori Koolstra
Quoted 19 lines
Op 01-04-2026 06:19 CEST schreef Mateusz Guzik <[email protected]>:
fs/namei.c int filename_mkdirat(int dfd, struct filename *name, umode_t mode) · line 5299
Quoted 11 lines · Jori Koolstra
fs/namei.c +6
-0@@ -5286,7 +5290,25 @@ int filename_mkdirat(int dfd, struct filename *name, umode_t mode) lookup_flags |=LOOKUP_REVAL ; goto retry; } + +if (!error && (flags &MKDIRAT_FD_NEED_FD )) { +struct path new_path = { .mnt = path.mnt, .dentry = dentry }; + error =FD_ADD (0 ,dentry_open (&new_path,O_DIRECTORY ,current_cred ())); + } +end_creating_path (&path, dentry);return error;Quoted 6 lines
You can't do it like this. Should it turn out no fd can be allocated, the entire thing is going to error out while keeping the newly created directory behind. You need to allocate the fd first, then do the hard work, and only then
fd_installand or free the fd. TheFD_ADDmachinery can probably still be used provided proper wrapping of the real new mkdir.Quoted 9 lines
But isn't this exactly what happens in
open(O_CREAT)too? Eventually we callerror = dir_inode->i_op->create(idmap, dir_inode, dentry, mode, open_flag & O_EXCL);and only then do we assign and install the fd. AFAIK there is no cleanup happening there either if the
FD_ADDstep fails. You will just have a regular file and no descriptor. But I would have to test this to be sure.
Quoted 5 lines
FD_ADD(how->flags, do_file_open(dfd, name, &op))means fd itself will be allocated upfront and only then file creation will happen and which is what I'm saying is how it should be done. With your patch the directory is created first and the possibly failing fd allocation happens later.
Err, you're right. I understand what you mean now. That does need to be fixed. I misremembered how FD_ADD works. I'll get back to this in the weekend.
Quoted 11 lines
Quoted 10 lines
Quoted 2 lines
Trying to handle this in
open()is a no-go. openat2 is rather problematic.I don't think that is necessarily true. It turned out
O_CREAT|O_DIRECTORYwas bugged for a very long time. Christian Brauner fixed it eventually, and that combination now returns EINVAL. But I think there is nothing really stopping us from implementing that combination in the expected way, apart from whatever reasons there were for not allowing this in the first place, which I don't know about (maybe mixing semantics?)
Quoted 3 lines
I am not saying it's impossible. I am saying mkdir was always a separate codepath and in order to change that you would need to add a branchfest to open. I don't see any reason to go that route.
That's a fair point. But there's also upsides like Aleksa has mentioned. I'm not very opinionated on the matter, especially since I don't know why those paths were ever separated.
Thanks,
Jori.
Re: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
Quoted 1 line · On Wed, 8 Apr 2026 23:16:31 +0200 (CEST) Jori Koolstra <[email protected]>…
Quoted 1 line
Op 07-04-2026 11:00 CEST schreef Mateusz Guzik <[email protected]>:
...
Quoted 3 lines
Quoted 3 lines
I am not saying it's impossible. I am saying mkdir was always a separate codepath and in order to change that you would need to add a branchfest to open. I don't see any reason to go that route.
The open code is complex enough that an extra branch won't matter.
Quoted 3 lines
That's a fair point. But there's also upsides like Aleksa has mentioned. I'm not very opinionated on the matter, especially since I don't know why those paths were ever separated.
I doubt they were ever joined.mkdir() is more likely to have been separated from mknod() when the code to add the "." and ".." entries was moved into the kernel filesystem code. I'm not sure when that would have happened, mvdir() was done in userspace with the link() and unlink() system calls until (at least) the mid 1980s. It was probably the complexity of locking in SMP kernels that make both "." and ".." be 'canned' names rather than just references to another directory. (Yes, it used to be easy to make ".." refer to the 'wrong' place and get find to loop.)
Of course, this all predates Linux.
David
Quoted 4 lines
Thanks,
Jori.
Re: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
In reply to Mateusz GuzikQuoted 24 lines · Mateusz Guzik
fs/namei.c int filename_mkdirat(int dfd, struct filename *name, umode_t mode) · line 5299
Quoted 11 lines · Jori Koolstra
fs/namei.c +6
-0@@ -5286,7 +5290,25 @@ int filename_mkdirat(int dfd, struct filename *name, umode_t mode) lookup_flags |=LOOKUP_REVAL ; goto retry; } + +if (!error && (flags &MKDIRAT_FD_NEED_FD )) { +struct path new_path = { .mnt = path.mnt, .dentry = dentry }; + error =FD_ADD (0 ,dentry_open (&new_path,O_DIRECTORY ,current_cred ())); + } +end_creating_path (&path, dentry);return error;You can't do it like this. Should it turn out no fd can be allocated, the entire thing is going to error out while keeping the newly created directory behind. You need to allocate the fd first, then do the hard work, and only then
fd_installand or free the fd. TheFD_ADDmachinery can probably still be used provided proper wrapping of the real new mkdir.It should be perfectly feasible to de facto wrap existing mkdir functionality by this syscall.
Quoted 6 lines
On top of that similarly to what other people mentioned the new syscall will definitely want to support
O_CLOEXECand probably other flags down the line.Trying to handle this in
open()is a no-go. openat2 is rather problematic.
I'm interested in what makes you say that. It would be very nice to be able to do mkdir + RESOLVE_IN_ROOT and get an fd back all in one syscall. :D
To be fair, build_open_how() will need some more magic to keep openat() working, and that won't be particularly pretty. If we went with O_CREAT|O_DIRECTORY we would need to be quite careful to make sure O_TMPFILE continues to work for both openat() and openat2()...
Quoted 6 lines
I tend to agree
mkdirat_fdis not a good name for the syscall either, but I don't have a suggestion I'm happy with. I think least bad name would follow the existing stuff and be mkdirat2 or similar.The routine would have to start with validating the passed O_ flags, for now only allowing
O_CLOEXECand EINVAL-ing otherwise.
Please do not use O_* flags! O_CLOEXEC takes up 3 flag bits on different architectures which makes adding new flags a nightmare.
I think this should take AT_* flags and (like most newer syscalls) O_CLOEXEC should be automatically set. Userspace can unset it with fnctl(F_SETFD) in the relatively rare case where they don't want O_CLOEXEC. Alternatively, we could just bite the bullet and make AT_NO_CLOEXEC a thing...
But yes, new syscalls *absolutely* need to take some kind of flag argument. I'd hoped we finally learned our lesson on that one...
Signature · 2 lines
Aleksa Sarai https://www.cyphar.com/
Re: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
Quoted 8 lines · Aleksa Sarai
Quoted 2 lines · Mateusz Guzik
Trying to handle this in
open()is a no-go. openat2 is rather problematic.I'm interested in what makes you say that. It would be very nice to be able to do mkdir +
RESOLVE_IN_ROOTand get an fd back all in one syscall. :D
Not handling this in either of open or openat2 does not preclude mkdir
+ RESOLVE_IN_ROOT + getting a fd in one go from existing.
Creating a directory was always a different syscall than creating a file. I don't see any benefit to squeezing it into open. I do see a downside because of an extra branchfest to differentiate the cases.
Quoted 6 lines
Quoted 2 lines
The routine would have to start with validating the passed O_ flags, for now only allowing
O_CLOEXECand EINVAL-ing otherwise.Please do not use O_* flags!
O_CLOEXECtakes up 3 flag bits on different architectures which makes adding new flags a nightmare.
With my proposal there are no new flags added so I don't think that's relevant.
Quoted 6 lines
I think this should take AT_* flags and (like most newer syscalls)
O_CLOEXECshould be automatically set. Userspace can unset it withfnctl(F_SETFD)in the relatively rare case where they don't wantO_CLOEXEC. Alternatively, we could just bite the bullet and makeAT_NO_CLOEXECa thing...
I would say that's a pretty weird discrepancy vs what normally happens with other syscalls, but perhaps it would be fine.
Re: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
Quoted 6 lines · Mateusz Guzik
Quoted 5 lines · Aleksa Sarai
Quoted 2 lines · Mateusz Guzik
Trying to handle this in
open()is a no-go. openat2 is rather problematic.
Quoted 6 lines
Quoted 3 lines
I'm interested in what makes you say that. It would be very nice to be able to do mkdir +
RESOLVE_IN_ROOTand get an fd back all in one syscall. :DNot handling this in either of open or openat2 does not preclude mkdir
+RESOLVE_IN_ROOT+ getting a fd in one go from existing.
Well, that would also require passing RESOLVE_* flags to mkdirat2(2) which kind of begs the question why not just integrate it into openat2(2) -- otherwise there will always be more features available to O_CREAT than mkdirat2(2) which seems unfortunate.
Quoted 3 lines
Creating a directory was always a different syscall than creating a file. I don't see any benefit to squeezing it into open. I do see a downside because of an extra branchfest to differentiate the cases.
Ah, so it's just an issue of taste, not a technical problem (as the mail I replied to made it sound)?
Quoted 8 lines
Quoted 6 lines
Quoted 2 lines
The routine would have to start with validating the passed O_ flags, for now only allowing
O_CLOEXECand EINVAL-ing otherwise.Please do not use O_* flags!
O_CLOEXECtakes up 3 flag bits on different architectures which makes adding new flags a nightmare.With my proposal there are no new flags added so I don't think that's relevant.
I'm confused, was "the new routine would have to start with validating the passed O_ flags" talking about a hypothetical API you oppose? It read like a suggestion on my first pass-through, hence the reply.
If you're saying that your proposal doesn't add any new O_* (or MKDIRAT_*) flags that really isn't the issue -- any syscall that takes a flag argument will grow new flags eventually and using the literal value of O_CLOEXEC for some other syscall's flags just leads to burning three flag bits needlessly.
This is arguably the most painful thing about open_tree(2)'s flags -- most other syscalls define their own flag that is equivalent to O_CLOEXEC but not literally equal to it (this is even recommended in Documentation/process/adding-syscalls.rst!).
Quoted 9 lines
Quoted 6 lines
I think this should take AT_* flags and (like most newer syscalls)
O_CLOEXECshould be automatically set. Userspace can unset it withfnctl(F_SETFD)in the relatively rare case where they don't wantO_CLOEXEC. Alternatively, we could just bite the bullet and makeAT_NO_CLOEXECa thing...I would say that's a pretty weird discrepancy vs what normally happens with other syscalls, but perhaps it would be fine.
Quite a few of the newer uAPIs do this -- all of the pidfd APIs do it, as well as newer ioctls that return fds (like the NS_GET_* ioctls for nsfs).
Clearing O_CLOEXEC safely is trivial but safely setting it is not really possible in multi-threaded programs (see "man 2 openat"), so it makes more sense for newer APIs to just default to O_CLOEXEC and userspace can unset it (and that is what newer APIs already do).
We should probably update Documentation/process/adding-syscalls.rst to mention this...
Signature · 2 lines
Aleksa Sarai https://www.cyphar.com/
Re: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
In reply to Aleksa SaraiQuoted 10 lines
Op 02-04-2026 04:52 CEST schreef Aleksa Sarai <[email protected]>:
Please do not use O_* flags!
O_CLOEXECtakes up 3 flag bits on different architectures which makes adding new flags a nightmare.I think this should take AT_* flags and (like most newer syscalls)
O_CLOEXECshould be automatically set. Userspace can unset it withfnctl(F_SETFD)in the relatively rare case where they don't wantO_CLOEXEC.
And then do something like statx_lookup_flags() does to build the lookup flags from those AT flags? But there is also no AT_ROOT_CONTAINED (or whatever you would want to call the RESOLVE_IN_ROOT AT-equivalent) right now.
Quoted 2 lines
Alternatively, we could just bite the bullet and make
AT_NO_CLOEXECa thing...
What's the bullet to bite there?
Quoted 7 lines
But yes, new syscalls *absolutely* need to take some kind of flag argument. I'd hoped we finally learned our lesson on that one...
Signature · 2 lines
Aleksa Sarai https://www.cyphar.com/
Thanks,
Jori.
Re: [RFC PATCH 1/2] vfs: syscalls: add mkdirat_fd()
Quoted 6 lines · Jori Koolstra
Quoted 6 lines
Op 02-04-2026 04:52 CEST schreef Aleksa Sarai <[email protected]>:
Please do not use O_* flags!
O_CLOEXECtakes up 3 flag bits on different architectures which makes adding new flags a nightmare.
Quoted 7 lines
Quoted 4 lines
I think this should take AT_* flags and (like most newer syscalls)
O_CLOEXECshould be automatically set. Userspace can unset it withfnctl(F_SETFD)in the relatively rare case where they don't wantO_CLOEXEC.And then do something like
statx_lookup_flags()does to build the lookup flags from those AT flags?
Yeah, that is the usual pattern for *at(2) syscalls.
Quoted 2 lines
But there is also no
AT_ROOT_CONTAINED(or whatever you would want to call theRESOLVE_IN_ROOTAT-equivalent) right now.
This point about AT_* flags was a separate point to my hopes that we could get this into openat2(2). We don't have AT_* equivalents to RESOLVE_* flags because it would burn too many bits (at least 5, likely more when we add RESOLVE_NO_DOTDOT and other such extensions) and openat2(2) is actually sufficient for almost all operations in practice.
Quoted 4 lines
Quoted 2 lines
Alternatively, we could just bite the bullet and make
AT_NO_CLOEXECa thing...What's the bullet to bite there?
It's not a big deal but it just burns another generic AT_* flag bit for something that userspace can do themselves with fnctl(2).
Maybe having it will encourage future syscall authors to default to O_CLOEXEC, but we could end up with the slightly silly AT_SYMLINK_FOLLOW / AT_SYMLINK_NOFOLLOW situation too. Documenting it in seemingly-rarely-read Documentation/process/adding-syscalls.rst might end up being equally (in)effective without burning a flag bit... :/
Signature · 2 lines
Aleksa Sarai https://www.cyphar.com/
RFC PATCH 2/2 selftest: add tests for mkdirat_fd()
In reply to Jori KoolstraAdd some tests for the new mkdirat_fd() syscall to test compliance and to showcase its behaviour.
--- tools/include/uapi/asm-generic/unistd.h | 5 +- tools/testing/selftests/filesystems/Makefile | 4 +- .../selftests/filesystems/mkdirat_fd_test.c | 139 ++++++++++++++++++ 3 files changed, 145 insertions(+), 3 deletions(-) create mode 100644 tools/testing/selftests/filesystems/mkdirat_fd_test.c
tools/include/uapi/asm-generic/unistd.h +4 -1
@@ -863,8 +863,11 @@ __SYSCALL(__NR_listns, sys_listns)#define __NR_rseq_slice_yield 471 __SYSCALL(__NR_rseq_slice_yield, sys_rseq_slice_yield) +#define __NR_mkdirat_fd 472 +__SYSCALL(__NR_mkdirat_fd, sys_mkdirat_fd) +#undef __NR_syscalls -#define __NR_syscalls 472 +#define __NR_syscalls 473/* * 32 bit systems traditionally used different
tools/testing/selftests/filesystems/Makefile +2 -2
@@ -1,7 +1,7 @@ # SPDX-License-Identifier: GPL-2.0 -CFLAGS += $(KHDR_INCLUDES) -TEST_GEN_PROGS := devpts_pts file_stressor anon_inode_test kernfs_test fclog +CFLAGS += $(KHDR_INCLUDES) $(TOOLS_INCLUDES) +TEST_GEN_PROGS := devpts_pts file_stressor anon_inode_test kernfs_test fclog mkdirat_fd_test TEST_GEN_PROGS_EXTENDED := dnotify_test include ../lib.mk
tools/testing/selftests/filesystems/mkdirat_fd_test.c new file +139 -0
139 lines not on this page · raw message
Signature · 1 line
2.53.0