Linux kernel mirror (for testing) git.kernel.org/pub/scm/linux/kernel/git/torvalds/linux.git
kernel os linux

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>

+20 -57
+18 -55
fs/nsfs.c
··· 27 27 static char *ns_dname(struct dentry *dentry, char *buffer, int buflen) 28 28 { 29 29 struct inode *inode = d_inode(dentry); 30 - const struct proc_ns_operations *ns_ops = dentry->d_fsdata; 30 + struct ns_common *ns = inode->i_private; 31 + const struct proc_ns_operations *ns_ops = ns->ops; 31 32 32 33 return dynamic_dname(buffer, buflen, "%s:[%lu]", 33 34 ns_ops->name, inode->i_ino); ··· 39 38 struct inode *inode = d_inode(dentry); 40 39 if (inode) { 41 40 struct ns_common *ns = inode->i_private; 42 - atomic_long_set(&ns->stashed, 0); 41 + WRITE_ONCE(ns->stashed, NULL); 43 42 } 44 43 } 45 44 ··· 57 56 ns->ops->put(ns); 58 57 } 59 58 60 - static int __ns_get_path(struct path *path, struct ns_common *ns) 61 - { 62 - struct vfsmount *mnt = nsfs_mnt; 63 - struct dentry *dentry; 64 - struct inode *inode; 65 - unsigned long d; 66 - 67 - rcu_read_lock(); 68 - d = atomic_long_read(&ns->stashed); 69 - if (!d) 70 - goto slow; 71 - dentry = (struct dentry *)d; 72 - if (!lockref_get_not_dead(&dentry->d_lockref)) 73 - goto slow; 74 - rcu_read_unlock(); 75 - ns->ops->put(ns); 76 - got_it: 77 - path->mnt = mntget(mnt); 78 - path->dentry = dentry; 79 - return 0; 80 - slow: 81 - rcu_read_unlock(); 82 - inode = new_inode_pseudo(mnt->mnt_sb); 83 - if (!inode) { 84 - ns->ops->put(ns); 85 - return -ENOMEM; 86 - } 87 - inode->i_ino = ns->inum; 88 - simple_inode_init_ts(inode); 89 - inode->i_flags |= S_IMMUTABLE; 90 - inode->i_mode = S_IFREG | S_IRUGO; 91 - inode->i_fop = &ns_file_operations; 92 - inode->i_private = ns; 93 - 94 - dentry = d_make_root(inode); /* not the normal use, but... */ 95 - if (!dentry) 96 - return -ENOMEM; 97 - dentry->d_fsdata = (void *)ns->ops; 98 - d = atomic_long_cmpxchg(&ns->stashed, 0, (unsigned long)dentry); 99 - if (d) { 100 - d_delete(dentry); /* make sure ->d_prune() does nothing */ 101 - dput(dentry); 102 - cpu_relax(); 103 - return -EAGAIN; 104 - } 105 - goto got_it; 106 - } 107 - 108 59 int ns_get_path_cb(struct path *path, ns_get_path_helper_t *ns_get_cb, 109 60 void *private_data) 110 61 { ··· 66 113 struct ns_common *ns = ns_get_cb(private_data); 67 114 if (!ns) 68 115 return -ENOENT; 69 - ret = __ns_get_path(path, ns); 116 + ret = path_from_stashed(&ns->stashed, ns->inum, nsfs_mnt, 117 + &ns_file_operations, ns, path); 118 + if (ret <= 0 && ret != -EAGAIN) 119 + ns->ops->put(ns); 70 120 } while (ret == -EAGAIN); 71 121 72 - return ret; 122 + if (ret < 0) 123 + return ret; 124 + 125 + return 0; 73 126 } 74 127 75 128 struct ns_get_path_task_args { ··· 122 163 return PTR_ERR(relative); 123 164 } 124 165 125 - err = __ns_get_path(&path, relative); 166 + err = path_from_stashed(&relative->stashed, relative->inum, nsfs_mnt, 167 + &ns_file_operations, relative, &path); 168 + if (err <= 0 && err != -EAGAIN) 169 + relative->ops->put(relative); 126 170 } while (err == -EAGAIN); 127 171 128 - if (err) { 172 + if (err < 0) { 129 173 put_unused_fd(fd); 130 174 return err; 131 175 } ··· 211 249 static int nsfs_show_path(struct seq_file *seq, struct dentry *dentry) 212 250 { 213 251 struct inode *inode = d_inode(dentry); 214 - const struct proc_ns_operations *ns_ops = dentry->d_fsdata; 252 + const struct ns_common *ns = inode->i_private; 253 + const struct proc_ns_operations *ns_ops = ns->ops; 215 254 216 255 seq_printf(seq, "%s:[%lu]", ns_ops->name, inode->i_ino); 217 256 return 0;
+1 -1
include/linux/ns_common.h
··· 7 7 struct proc_ns_operations; 8 8 9 9 struct ns_common { 10 - atomic_long_t stashed; 10 + struct dentry *stashed; 11 11 const struct proc_ns_operations *ops; 12 12 unsigned int inum; 13 13 refcount_t count;
+1 -1
include/linux/proc_ns.h
··· 66 66 67 67 static inline int ns_alloc_inum(struct ns_common *ns) 68 68 { 69 - atomic_long_set(&ns->stashed, 0); 69 + WRITE_ONCE(ns->stashed, NULL); 70 70 return proc_alloc_inum(&ns->inum); 71 71 } 72 72