hc
2024-05-10 61598093bbdd283a7edc367d900f223070ead8d2
kernel/fs/nsfs.c
....@@ -1,7 +1,9 @@
11 // SPDX-License-Identifier: GPL-2.0
22 #include <linux/mount.h>
3
+#include <linux/pseudo_fs.h>
34 #include <linux/file.h>
45 #include <linux/fs.h>
6
+#include <linux/proc_fs.h>
57 #include <linux/proc_ns.h>
68 #include <linux/magic.h>
79 #include <linux/ktime.h>
....@@ -9,6 +11,8 @@
911 #include <linux/user_namespace.h>
1012 #include <linux/nsfs.h>
1113 #include <linux/uaccess.h>
14
+
15
+#include "internal.h"
1216
1317 static struct vfsmount *nsfs_mnt;
1418
....@@ -51,7 +55,7 @@
5155 ns->ops->put(ns);
5256 }
5357
54
-static void *__ns_get_path(struct path *path, struct ns_common *ns)
58
+static int __ns_get_path(struct path *path, struct ns_common *ns)
5559 {
5660 struct vfsmount *mnt = nsfs_mnt;
5761 struct dentry *dentry;
....@@ -70,13 +74,13 @@
7074 got_it:
7175 path->mnt = mntget(mnt);
7276 path->dentry = dentry;
73
- return NULL;
77
+ return 0;
7478 slow:
7579 rcu_read_unlock();
7680 inode = new_inode_pseudo(mnt->mnt_sb);
7781 if (!inode) {
7882 ns->ops->put(ns);
79
- return ERR_PTR(-ENOMEM);
83
+ return -ENOMEM;
8084 }
8185 inode->i_ino = ns->inum;
8286 inode->i_mtime = inode->i_atime = inode->i_ctime = current_time(inode);
....@@ -88,7 +92,7 @@
8892 dentry = d_alloc_anon(mnt->mnt_sb);
8993 if (!dentry) {
9094 iput(inode);
91
- return ERR_PTR(-ENOMEM);
95
+ return -ENOMEM;
9296 }
9397 d_instantiate(dentry, inode);
9498 dentry->d_fsdata = (void *)ns->ops;
....@@ -97,25 +101,23 @@
97101 d_delete(dentry); /* make sure ->d_prune() does nothing */
98102 dput(dentry);
99103 cpu_relax();
100
- return ERR_PTR(-EAGAIN);
104
+ return -EAGAIN;
101105 }
102106 goto got_it;
103107 }
104108
105
-void *ns_get_path_cb(struct path *path, ns_get_path_helper_t *ns_get_cb,
109
+int ns_get_path_cb(struct path *path, ns_get_path_helper_t *ns_get_cb,
106110 void *private_data)
107111 {
108
- struct ns_common *ns;
109
- void *ret;
112
+ int ret;
110113
111
-again:
112
- ns = ns_get_cb(private_data);
113
- if (!ns)
114
- return ERR_PTR(-ENOENT);
114
+ do {
115
+ struct ns_common *ns = ns_get_cb(private_data);
116
+ if (!ns)
117
+ return -ENOENT;
118
+ ret = __ns_get_path(path, ns);
119
+ } while (ret == -EAGAIN);
115120
116
- ret = __ns_get_path(path, ns);
117
- if (IS_ERR(ret) && PTR_ERR(ret) == -EAGAIN)
118
- goto again;
119121 return ret;
120122 }
121123
....@@ -131,7 +133,7 @@
131133 return args->ns_ops->get(args->task);
132134 }
133135
134
-void *ns_get_path(struct path *path, struct task_struct *task,
136
+int ns_get_path(struct path *path, struct task_struct *task,
135137 const struct proc_ns_operations *ns_ops)
136138 {
137139 struct ns_get_path_task_args args = {
....@@ -147,14 +149,14 @@
147149 {
148150 struct path path = {};
149151 struct file *f;
150
- void *err;
152
+ int err;
151153 int fd;
152154
153155 fd = get_unused_fd_flags(O_CLOEXEC);
154156 if (fd < 0)
155157 return fd;
156158
157
- while (1) {
159
+ do {
158160 struct ns_common *relative;
159161
160162 relative = get_ns(ns);
....@@ -164,13 +166,11 @@
164166 }
165167
166168 err = __ns_get_path(&path, relative);
167
- if (IS_ERR(err) && PTR_ERR(err) == -EAGAIN)
168
- continue;
169
- break;
170
- }
171
- if (IS_ERR(err)) {
169
+ } while (err == -EAGAIN);
170
+
171
+ if (err) {
172172 put_unused_fd(fd);
173
- return PTR_ERR(err);
173
+ return err;
174174 }
175175
176176 f = dentry_open(&path, O_RDONLY, current_cred());
....@@ -229,6 +229,11 @@
229229 return res;
230230 }
231231
232
+bool proc_ns_file(const struct file *file)
233
+{
234
+ return file->f_op == &ns_file_operations;
235
+}
236
+
232237 struct file *proc_ns_fget(int fd)
233238 {
234239 struct file *file;
....@@ -247,6 +252,20 @@
247252 return ERR_PTR(-EINVAL);
248253 }
249254
255
+/**
256
+ * ns_match() - Returns true if current namespace matches dev/ino provided.
257
+ * @ns_common: current ns
258
+ * @dev: dev_t from nsfs that will be matched against current nsfs
259
+ * @ino: ino_t from nsfs that will be matched against current nsfs
260
+ *
261
+ * Return: true if dev and ino matches the current nsfs.
262
+ */
263
+bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino)
264
+{
265
+ return (ns->inum == ino) && (nsfs_mnt->mnt_sb->s_dev == dev);
266
+}
267
+
268
+
250269 static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry)
251270 {
252271 struct inode *inode = d_inode(dentry);
....@@ -261,15 +280,20 @@
261280 .evict_inode = nsfs_evict,
262281 .show_path = nsfs_show_path,
263282 };
264
-static struct dentry *nsfs_mount(struct file_system_type *fs_type,
265
- int flags, const char *dev_name, void *data)
283
+
284
+static int nsfs_init_fs_context(struct fs_context *fc)
266285 {
267
- return mount_pseudo(fs_type, "nsfs:", &nsfs_ops,
268
- &ns_dentry_operations, NSFS_MAGIC);
286
+ struct pseudo_fs_context *ctx = init_pseudo(fc, NSFS_MAGIC);
287
+ if (!ctx)
288
+ return -ENOMEM;
289
+ ctx->ops = &nsfs_ops;
290
+ ctx->dops = &ns_dentry_operations;
291
+ return 0;
269292 }
293
+
270294 static struct file_system_type nsfs = {
271295 .name = "nsfs",
272
- .mount = nsfs_mount,
296
+ .init_fs_context = nsfs_init_fs_context,
273297 .kill_sb = kill_anon_super,
274298 };
275299