Skip to content

Commit 901766d

Browse files
committed
fs: add vfs_open_tree() helper
Split out vfs_open_tree() from open_tree() so we can use it in later patches. Link: https://lore.kernel.org/r/20250128-work-mnt_idmap-update-v2-v1-1-c25feb0d2eb3@kernel.org Reviewed-by: "Seth Forshee (DigitalOcean)" <sforshee@kernel.org> Signed-off-by: Christian Brauner <brauner@kernel.org>
1 parent 8f6116b commit 901766d

1 file changed

Lines changed: 26 additions & 23 deletions

File tree

fs/namespace.c

Lines changed: 26 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3002,24 +3002,22 @@ static struct file *open_detached_copy(struct path *path, bool recursive)
30023002
return file;
30033003
}
30043004

3005-
SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags)
3005+
static struct file *vfs_open_tree(int dfd, const char __user *filename, unsigned int flags)
30063006
{
3007-
struct file *file;
3008-
struct path path;
3007+
int ret;
3008+
struct path path __free(path_put) = {};
30093009
int lookup_flags = LOOKUP_AUTOMOUNT | LOOKUP_FOLLOW;
30103010
bool detached = flags & OPEN_TREE_CLONE;
3011-
int error;
3012-
int fd;
30133011

30143012
BUILD_BUG_ON(OPEN_TREE_CLOEXEC != O_CLOEXEC);
30153013

30163014
if (flags & ~(AT_EMPTY_PATH | AT_NO_AUTOMOUNT | AT_RECURSIVE |
30173015
AT_SYMLINK_NOFOLLOW | OPEN_TREE_CLONE |
30183016
OPEN_TREE_CLOEXEC))
3019-
return -EINVAL;
3017+
return ERR_PTR(-EINVAL);
30203018

30213019
if ((flags & (AT_RECURSIVE | OPEN_TREE_CLONE)) == AT_RECURSIVE)
3022-
return -EINVAL;
3020+
return ERR_PTR(-EINVAL);
30233021

30243022
if (flags & AT_NO_AUTOMOUNT)
30253023
lookup_flags &= ~LOOKUP_AUTOMOUNT;
@@ -3029,27 +3027,32 @@ SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, fl
30293027
lookup_flags |= LOOKUP_EMPTY;
30303028

30313029
if (detached && !may_mount())
3032-
return -EPERM;
3030+
return ERR_PTR(-EPERM);
3031+
3032+
ret = user_path_at(dfd, filename, lookup_flags, &path);
3033+
if (unlikely(ret))
3034+
return ERR_PTR(ret);
3035+
3036+
if (detached)
3037+
return open_detached_copy(&path, flags & AT_RECURSIVE);
3038+
3039+
return dentry_open(&path, O_PATH, current_cred());
3040+
}
3041+
3042+
SYSCALL_DEFINE3(open_tree, int, dfd, const char __user *, filename, unsigned, flags)
3043+
{
3044+
int fd;
3045+
struct file *file __free(fput) = NULL;
3046+
3047+
file = vfs_open_tree(dfd, filename, flags);
3048+
if (IS_ERR(file))
3049+
return PTR_ERR(file);
30333050

30343051
fd = get_unused_fd_flags(flags & O_CLOEXEC);
30353052
if (fd < 0)
30363053
return fd;
30373054

3038-
error = user_path_at(dfd, filename, lookup_flags, &path);
3039-
if (unlikely(error)) {
3040-
file = ERR_PTR(error);
3041-
} else {
3042-
if (detached)
3043-
file = open_detached_copy(&path, flags & AT_RECURSIVE);
3044-
else
3045-
file = dentry_open(&path, O_PATH, current_cred());
3046-
path_put(&path);
3047-
}
3048-
if (IS_ERR(file)) {
3049-
put_unused_fd(fd);
3050-
return PTR_ERR(file);
3051-
}
3052-
fd_install(fd, file);
3055+
fd_install(fd, no_free_ptr(file));
30533056
return fd;
30543057
}
30553058

0 commit comments

Comments
 (0)