Commit 460695a2 authored by Christian Brauner's avatar Christian Brauner
parent 85e4daae
...@@ -17,6 +17,7 @@ struct fs_context; ...@@ -17,6 +17,7 @@ struct fs_context;
struct pipe_inode_info; struct pipe_inode_info;
struct iov_iter; struct iov_iter;
struct mnt_idmap; struct mnt_idmap;
struct ns_common;
/* /*
* block/bdev.c * block/bdev.c
...@@ -321,3 +322,4 @@ struct stashed_operations { ...@@ -321,3 +322,4 @@ struct stashed_operations {
int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data, int path_from_stashed(struct dentry **stashed, struct vfsmount *mnt, void *data,
struct path *path); struct path *path);
void stashed_dentry_prune(struct dentry *dentry); void stashed_dentry_prune(struct dentry *dentry);
int open_namespace(struct ns_common *ns);
...@@ -82,40 +82,47 @@ int ns_get_path(struct path *path, struct task_struct *task, ...@@ -82,40 +82,47 @@ int ns_get_path(struct path *path, struct task_struct *task,
return ns_get_path_cb(path, ns_get_path_task, &args); return ns_get_path_cb(path, ns_get_path_task, &args);
} }
int open_related_ns(struct ns_common *ns, /**
struct ns_common *(*get_ns)(struct ns_common *ns)) * open_namespace - open a namespace
* @ns: the namespace to open
*
* This will consume a reference to @ns indendent of success or failure.
*
* Return: A file descriptor on success or a negative error code on failure.
*/
int open_namespace(struct ns_common *ns)
{ {
struct path path = {}; struct path path __free(path_put) = {};
struct ns_common *relative;
struct file *f; struct file *f;
int err; int err;
int fd;
fd = get_unused_fd_flags(O_CLOEXEC); /* call first to consume reference */
err = path_from_stashed(&ns->stashed, nsfs_mnt, ns, &path);
if (err < 0)
return err;
CLASS(get_unused_fd, fd)(O_CLOEXEC);
if (fd < 0) if (fd < 0)
return fd; return fd;
f = dentry_open(&path, O_RDONLY, current_cred());
if (IS_ERR(f))
return PTR_ERR(f);
fd_install(fd, f);
return take_fd(fd);
}
int open_related_ns(struct ns_common *ns,
struct ns_common *(*get_ns)(struct ns_common *ns))
{
struct ns_common *relative;
relative = get_ns(ns); relative = get_ns(ns);
if (IS_ERR(relative)) { if (IS_ERR(relative))
put_unused_fd(fd);
return PTR_ERR(relative); return PTR_ERR(relative);
}
err = path_from_stashed(&relative->stashed, nsfs_mnt, relative, &path); return open_namespace(relative);
if (err < 0) {
put_unused_fd(fd);
return err;
}
f = dentry_open(&path, O_RDONLY, current_cred());
path_put(&path);
if (IS_ERR(f)) {
put_unused_fd(fd);
fd = PTR_ERR(f);
} else
fd_install(fd, f);
return fd;
} }
EXPORT_SYMBOL_GPL(open_related_ns); EXPORT_SYMBOL_GPL(open_related_ns);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment