Skip to content

Commit

Permalink
nsfs: convert to path_from_stashed() helper
Browse files Browse the repository at this point in the history
  • Loading branch information
brauner committed Feb 24, 2024
1 parent b985c0c commit ef86bcd
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 57 deletions.
73 changes: 18 additions & 55 deletions fs/nsfs.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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);
}
}

Expand All @@ -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)
{
Expand All @@ -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 {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion include/linux/ns_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion include/linux/proc_ns.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}

Expand Down

0 comments on commit ef86bcd

Please sign in to comment.