summaryrefslogtreecommitdiffstats
path: root/fs/nsfs.c
diff options
context:
space:
mode:
authorChristian Brauner <brauner@kernel.org>2024-06-27 16:11:41 +0200
committerChristian Brauner <brauner@kernel.org>2024-06-28 10:37:29 +0200
commit460695a294e63de4e4f30ad1679ffddeec932bb8 (patch)
tree3b0e1b8c5feb8657a131084c93b9fc155f0b8137 /fs/nsfs.c
parentnsproxy: add helper to go from arbitrary namespace to ns_common (diff)
downloadlinux-460695a294e63de4e4f30ad1679ffddeec932bb8.tar.xz
linux-460695a294e63de4e4f30ad1679ffddeec932bb8.zip
nsfs: add open_namespace()
and call it from open_related_ns(). Link: https://lore.kernel.org/r/20240627-work-pidfs-v1-3-7e9ab6cc3bb1@kernel.org Reviewed-by: Jeff Layton <jlayton@kernel.org> Reviewed-by: Josef Bacik <josef@toxicpanda.com> Reviewed-by: Alexander Mikhalitsyn <aleksandr.mikhalitsyn@canonical.com> Signed-off-by: Christian Brauner <brauner@kernel.org>
Diffstat (limited to 'fs/nsfs.c')
-rw-r--r--fs/nsfs.c55
1 files changed, 31 insertions, 24 deletions
diff --git a/fs/nsfs.c b/fs/nsfs.c
index 07e22a15ef02..4c4dbdce7601 100644
--- a/fs/nsfs.c
+++ b/fs/nsfs.c
@@ -82,40 +82,47 @@ int ns_get_path(struct path *path, struct task_struct *task,
return ns_get_path_cb(path, ns_get_path_task, &args);
}
-int open_related_ns(struct ns_common *ns,
- struct ns_common *(*get_ns)(struct ns_common *ns))
+/**
+ * open_namespace - open a namespace
+ * @ns: the namespace to open
+ *
+ * This will consume a reference to @ns indendent of success or failure.
+ *
+ * Return: A file descriptor on success or a negative error code on failure.
+ */
+int open_namespace(struct ns_common *ns)
{
- struct path path = {};
- struct ns_common *relative;
+ struct path path __free(path_put) = {};
struct file *f;
int err;
- int fd;
- fd = get_unused_fd_flags(O_CLOEXEC);
+ /* call first to consume reference */
+ err = path_from_stashed(&ns->stashed, nsfs_mnt, ns, &path);
+ if (err < 0)
+ return err;
+
+ CLASS(get_unused_fd, fd)(O_CLOEXEC);
if (fd < 0)
return fd;
+ f = dentry_open(&path, O_RDONLY, current_cred());
+ if (IS_ERR(f))
+ return PTR_ERR(f);
+
+ fd_install(fd, f);
+ return take_fd(fd);
+}
+
+int open_related_ns(struct ns_common *ns,
+ struct ns_common *(*get_ns)(struct ns_common *ns))
+{
+ struct ns_common *relative;
+
relative = get_ns(ns);
- if (IS_ERR(relative)) {
- put_unused_fd(fd);
+ if (IS_ERR(relative))
return PTR_ERR(relative);
- }
- err = path_from_stashed(&relative->stashed, nsfs_mnt, relative, &path);
- if (err < 0) {
- put_unused_fd(fd);
- return err;
- }
-
- f = dentry_open(&path, O_RDONLY, current_cred());
- path_put(&path);
- if (IS_ERR(f)) {
- put_unused_fd(fd);
- fd = PTR_ERR(f);
- } else
- fd_install(fd, f);
-
- return fd;
+ return open_namespace(relative);
}
EXPORT_SYMBOL_GPL(open_related_ns);