diff options
author | Christian Brauner <brauner@kernel.org> | 2024-02-18 14:51:23 +0100 |
---|---|---|
committer | Christian Brauner <brauner@kernel.org> | 2024-03-01 12:23:44 +0100 |
commit | 1fa08aece42512be072351f482096d5796edf7ca (patch) | |
tree | 6ebfa283e0423520423e886bdf0e06c8252d6ec4 | |
parent | libfs: add path_from_stashed() (diff) | |
download | linux-1fa08aece42512be072351f482096d5796edf7ca.tar.xz linux-1fa08aece42512be072351f482096d5796edf7ca.zip |
nsfs: convert to path_from_stashed() helper
Use the newly added path_from_stashed() helper for nsfs.
Link: https://lore.kernel.org/r/20240218-neufahrzeuge-brauhaus-fb0eb6459771@brauner
Signed-off-by: Christian Brauner <brauner@kernel.org>
-rw-r--r-- | fs/nsfs.c | 73 | ||||
-rw-r--r-- | include/linux/ns_common.h | 2 | ||||
-rw-r--r-- | include/linux/proc_ns.h | 2 |
3 files changed, 20 insertions, 57 deletions
diff --git a/fs/nsfs.c b/fs/nsfs.c index 34e1e3e36733..39dc2604bec0 100644 --- a/fs/nsfs.c +++ b/fs/nsfs.c @@ -27,7 +27,8 @@ static const struct file_operations ns_file_operations = { static char *ns_dname(struct dentry *dentry, char *buffer, int buflen) { struct inode *inode = d_inode(dentry); - const struct proc_ns_operations *ns_ops = dentry->d_fsdata; + struct ns_common *ns = inode->i_private; + const struct proc_ns_operations *ns_ops = ns->ops; return dynamic_dname(buffer, buflen, "%s:[%lu]", ns_ops->name, inode->i_ino); @@ -38,7 +39,7 @@ static void ns_prune_dentry(struct dentry *dentry) struct inode *inode = d_inode(dentry); if (inode) { struct ns_common *ns = inode->i_private; - atomic_long_set(&ns->stashed, 0); + WRITE_ONCE(ns->stashed, NULL); } } @@ -56,54 +57,6 @@ static void nsfs_evict(struct inode *inode) ns->ops->put(ns); } -static int __ns_get_path(struct path *path, struct ns_common *ns) -{ - struct vfsmount *mnt = nsfs_mnt; - struct dentry *dentry; - struct inode *inode; - unsigned long d; - - rcu_read_lock(); - d = atomic_long_read(&ns->stashed); - if (!d) - goto slow; - dentry = (struct dentry *)d; - if (!lockref_get_not_dead(&dentry->d_lockref)) - goto slow; - rcu_read_unlock(); - ns->ops->put(ns); -got_it: - path->mnt = mntget(mnt); - path->dentry = dentry; - return 0; -slow: - rcu_read_unlock(); - inode = new_inode_pseudo(mnt->mnt_sb); - if (!inode) { - ns->ops->put(ns); - return -ENOMEM; - } - inode->i_ino = ns->inum; - simple_inode_init_ts(inode); - inode->i_flags |= S_IMMUTABLE; - inode->i_mode = S_IFREG | S_IRUGO; - inode->i_fop = &ns_file_operations; - inode->i_private = ns; - - dentry = d_make_root(inode); /* not the normal use, but... */ - if (!dentry) - return -ENOMEM; - dentry->d_fsdata = (void *)ns->ops; - d = atomic_long_cmpxchg(&ns->stashed, 0, (unsigned long)dentry); - if (d) { - d_delete(dentry); /* make sure ->d_prune() does nothing */ - dput(dentry); - cpu_relax(); - return -EAGAIN; - } - goto got_it; -} - int ns_get_path_cb(struct path *path, ns_get_path_helper_t *ns_get_cb, void *private_data) { @@ -113,10 +66,16 @@ int ns_get_path_cb(struct path *path, ns_get_path_helper_t *ns_get_cb, struct ns_common *ns = ns_get_cb(private_data); if (!ns) return -ENOENT; - ret = __ns_get_path(path, ns); + ret = path_from_stashed(&ns->stashed, ns->inum, nsfs_mnt, + &ns_file_operations, ns, path); + if (ret <= 0 && ret != -EAGAIN) + ns->ops->put(ns); } while (ret == -EAGAIN); - return ret; + if (ret < 0) + return ret; + + return 0; } struct ns_get_path_task_args { @@ -163,10 +122,13 @@ int open_related_ns(struct ns_common *ns, return PTR_ERR(relative); } - err = __ns_get_path(&path, relative); + err = path_from_stashed(&relative->stashed, relative->inum, nsfs_mnt, + &ns_file_operations, relative, &path); + if (err <= 0 && err != -EAGAIN) + relative->ops->put(relative); } while (err == -EAGAIN); - if (err) { + if (err < 0) { put_unused_fd(fd); return err; } @@ -249,7 +211,8 @@ bool ns_match(const struct ns_common *ns, dev_t dev, ino_t ino) static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry) { struct inode *inode = d_inode(dentry); - const struct proc_ns_operations *ns_ops = dentry->d_fsdata; + const struct ns_common *ns = inode->i_private; + const struct proc_ns_operations *ns_ops = ns->ops; seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino); return 0; diff --git a/include/linux/ns_common.h b/include/linux/ns_common.h index 0f1d024bd958..7d22ea50b098 100644 --- a/include/linux/ns_common.h +++ b/include/linux/ns_common.h @@ -7,7 +7,7 @@ struct proc_ns_operations; struct ns_common { - atomic_long_t stashed; + struct dentry *stashed; const struct proc_ns_operations *ops; unsigned int inum; refcount_t count; diff --git a/include/linux/proc_ns.h b/include/linux/proc_ns.h index 49539bc416ce..5ea470eb4d76 100644 --- a/include/linux/proc_ns.h +++ b/include/linux/proc_ns.h @@ -66,7 +66,7 @@ static inline void proc_free_inum(unsigned int inum) {} static inline int ns_alloc_inum(struct ns_common *ns) { - atomic_long_set(&ns->stashed, 0); + WRITE_ONCE(ns->stashed, NULL); return proc_alloc_inum(&ns->inum); } |