History

nsfs: add open_namespace()

and call it from open_related_ns().

Christian Brauner committed 2 years ago

Commit 460695a1 parent 85e4daa2 files · +33 −24Browse files

Unified · Split

2 files
  1. fs/internal.h +2 −0
  2. fs/nsfs.c +31 −24
fs/internal.h+2−0
@@ -17,6 +17,7 @@ struct fs_context; · 16 unmodified lines
1717 struct pipe_inode_info;
1818 struct iov_iter;
1919 struct mnt_idmap;
20+struct ns_common;
2021
2122 /*
2223 * block/bdev.c
@@ -321,3 +322,4 @@ struct stashed_operations { · 298 unmodified lines
321322 int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data,
322323 struct path *path);
323324 void stashed_dentry_prune(struct dentry *dentry);
325+int open_namespace(struct ns_common *ns);
fs/nsfs.c+31−24
@@ -82,40 +82,47 @@ int ns_get_path(struct path *path, struct task_struct *task, · 81 unmodified lines
8282 return ns_get_path_cb(path, ns_get_path_task, &args);
8383 }
8484
85−int open_related_ns(struct ns_common *ns,
86− struct ns_common *(*get_ns)(struct ns_common *ns))
85+/**
86+ * open_namespace - open a namespace
87+ * @ns: the namespace to open
88+ *
89+ * This will consume a reference to @ns indendent of success or failure.
90+ *
91+ * Return: A file descriptor on success or a negative error code on failure.
92+ */
93+int open_namespace(struct ns_common *ns)
8794 {
88− struct path path = {};
89− struct ns_common *relative;
95+ struct path path __free(path_put) = {};
9096 struct file *f;
9197 int err;
92− int fd;
9398
94− fd = get_unused_fd_flags(O_CLOEXEC);
99+ /* call first to consume reference */
100+ err = path_from_stashed(&ns->stashed, nsfs_mnt, ns, &path);
101+ if (err < 0)
102+ return err;
103+
104+ CLASS(get_unused_fd, fd)(O_CLOEXEC);
95105 if (fd < 0)
96106 return fd;
97107
108+ f = dentry_open(&path, O_RDONLY, current_cred());
109+ if (IS_ERR(f))
110+ return PTR_ERR(f);
111+
112+ fd_install(fd, f);
113+ return take_fd(fd);
114+}
115+
116+int open_related_ns(struct ns_common *ns,
117+ struct ns_common *(*get_ns)(struct ns_common *ns))
118+{
119+ struct ns_common *relative;
120+
98121 relative = get_ns(ns);
99− if (IS_ERR(relative)) {
100− put_unused_fd(fd);
122+ if (IS_ERR(relative))
101123 return PTR_ERR(relative);
102− }
103124
104− err = path_from_stashed(&relative->stashed, nsfs_mnt, relative, &path);
105− if (err < 0) {
106− put_unused_fd(fd);
107− return err;
108− }
109−
110− f = dentry_open(&path, O_RDONLY, current_cred());
111− path_put(&path);
112− if (IS_ERR(f)) {
113− put_unused_fd(fd);
114− fd = PTR_ERR(f);
115− } else
116− fd_install(fd, f);
117−
118− return fd;
125+ return open_namespace(relative);
119126 }
120127 EXPORT_SYMBOL_GPL(open_related_ns);
121128