]> git.ipfire.org Git - thirdparty/linux.git/commitdiff
nsfs: convert to path_from_stashed() helper
authorChristian Brauner <brauner@kernel.org>
Sun, 18 Feb 2024 13:51:23 +0000 (14:51 +0100)
committerChristian Brauner <brauner@kernel.org>
Fri, 1 Mar 2024 11:23:44 +0000 (12:23 +0100)
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>
fs/nsfs.c
include/linux/ns_common.h
include/linux/proc_ns.h

index 34e1e3e36733da8ed12de3582123638095f30fab..39dc2604bec01e80b112854a5fb338c5deef2103 100644 (file)
--- 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;
index 0f1d024bd9582618a54b601988969694b4dafb47..7d22ea50b09841ed2f764273564e53588e9b7a4e 100644 (file)
@@ -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;
index 49539bc416cecb7b45bfd4f11df817cd11f2e791..5ea470eb4d768ad92f2785187aa9298c39025ea2 100644 (file)
@@ -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);
 }