From: Christian Brauner <brauner@kernel.org>
To: linux-fsdevel@vger.kernel.org
Cc: Josef Bacik <josef@toxicpanda.com>,
Seth Forshee <sforshee@kernel.org>,
Stephane Graber <stgraber@stgraber.org>,
Jeff Layton <jlayton@kernel.org>,
Aleksa Sarai <cyphar@cyphar.com>,
Alexander Mikhalitsyn <alexander@mihalicyn.com>,
Christian Brauner <brauner@kernel.org>
Subject: [PATCH RFC 3/4] nsfs: add open_namespace()
Date: Thu, 27 Jun 2024 16:11:41 +0200 [thread overview]
Message-ID: <20240627-work-pidfs-v1-3-7e9ab6cc3bb1@kernel.org> (raw)
In-Reply-To: <20240627-work-pidfs-v1-0-7e9ab6cc3bb1@kernel.org>
and call it from open_related_ns().
Signed-off-by: Christian Brauner <brauner@kernel.org>
---
fs/internal.h | 1 +
fs/nsfs.c | 55 +++++++++++++++++++++++++++++++------------------------
2 files changed, 32 insertions(+), 24 deletions(-)
diff --git a/fs/internal.h b/fs/internal.h
index ab2225136f60..1ece6a3d34cb 100644
--- a/fs/internal.h
+++ b/fs/internal.h
@@ -321,3 +321,4 @@ struct stashed_operations {
int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data,
struct path *path);
void stashed_dentry_prune(struct dentry *dentry);
+int open_namespace(struct ns_common *ns);
diff --git a/fs/nsfs.c b/fs/nsfs.c
index af352dadffe1..c35bc3ca466f 100644
--- a/fs/nsfs.c
+++ b/fs/nsfs.c
@@ -83,40 +83,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);
--
2.43.0
next prev parent reply other threads:[~2024-06-27 14:12 UTC|newest]
Thread overview: 10+ messages / expand[flat|nested] mbox.gz Atom feed top
2024-06-27 14:11 [PATCH RFC 0/4] pidfs: allow retrieval of namespace descriptors Christian Brauner
2024-06-27 14:11 ` [PATCH RFC 1/4] file: add take_fd() cleanup helper Christian Brauner
2024-06-27 17:24 ` Josef Bacik
2024-06-28 8:37 ` Christian Brauner
2024-06-27 14:11 ` [PATCH RFC 2/4] nsproxy: add a cleanup helper for nsproxy Christian Brauner
2024-06-27 14:11 ` Christian Brauner [this message]
2024-06-27 14:11 ` [PATCH RFC 4/4] pidfs: allow retrieval of namespace file descriptors Christian Brauner
2024-06-27 16:51 ` [PATCH RFC 0/4] pidfs: allow retrieval of namespace descriptors Jeff Layton
2024-06-27 17:26 ` Josef Bacik
2024-06-27 20:05 ` Alexander Mikhalitsyn
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20240627-work-pidfs-v1-3-7e9ab6cc3bb1@kernel.org \
--to=brauner@kernel.org \
--cc=alexander@mihalicyn.com \
--cc=cyphar@cyphar.com \
--cc=jlayton@kernel.org \
--cc=josef@toxicpanda.com \
--cc=linux-fsdevel@vger.kernel.org \
--cc=sforshee@kernel.org \
--cc=stgraber@stgraber.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).