Skip to content

Commit aa81882

Browse files
shligitaxboe
authored andcommitted
kernfs: add exportfs operations
Now we have the facilities to implement exportfs operations. The idea is cgroup can export the fhandle info to userspace, then userspace uses fhandle to find the cgroup name. Another example is userspace can get fhandle for a cgroup and BPF uses the fhandle to filter info for the cgroup. Acked-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org> Signed-off-by: Shaohua Li <shli@fb.com> Signed-off-by: Jens Axboe <axboe@kernel.dk>
1 parent c53cd49 commit aa81882

3 files changed

Lines changed: 70 additions & 1 deletion

File tree

‎fs/kernfs/mount.c‎

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <linux/pagemap.h>
1717
#include <linux/namei.h>
1818
#include <linux/seq_file.h>
19+
#include <linux/exportfs.h>
1920

2021
#include "kernfs-internal.h"
2122

@@ -64,6 +65,59 @@ const struct super_operations kernfs_sops = {
6465
.show_path = kernfs_sop_show_path,
6566
};
6667

68+
static struct inode *kernfs_fh_get_inode(struct super_block *sb,
69+
u64 ino, u32 generation)
70+
{
71+
struct kernfs_super_info *info = kernfs_info(sb);
72+
struct inode *inode;
73+
struct kernfs_node *kn;
74+
75+
if (ino == 0)
76+
return ERR_PTR(-ESTALE);
77+
78+
kn = kernfs_find_and_get_node_by_ino(info->root, ino);
79+
if (!kn)
80+
return ERR_PTR(-ESTALE);
81+
inode = kernfs_get_inode(sb, kn);
82+
kernfs_put(kn);
83+
if (IS_ERR(inode))
84+
return ERR_CAST(inode);
85+
86+
if (generation && inode->i_generation != generation) {
87+
/* we didn't find the right inode.. */
88+
iput(inode);
89+
return ERR_PTR(-ESTALE);
90+
}
91+
return inode;
92+
}
93+
94+
static struct dentry *kernfs_fh_to_dentry(struct super_block *sb, struct fid *fid,
95+
int fh_len, int fh_type)
96+
{
97+
return generic_fh_to_dentry(sb, fid, fh_len, fh_type,
98+
kernfs_fh_get_inode);
99+
}
100+
101+
static struct dentry *kernfs_fh_to_parent(struct super_block *sb, struct fid *fid,
102+
int fh_len, int fh_type)
103+
{
104+
return generic_fh_to_parent(sb, fid, fh_len, fh_type,
105+
kernfs_fh_get_inode);
106+
}
107+
108+
static struct dentry *kernfs_get_parent_dentry(struct dentry *child)
109+
{
110+
struct kernfs_node *kn = kernfs_dentry_node(child);
111+
112+
return d_obtain_alias(kernfs_get_inode(child->d_sb, kn->parent));
113+
}
114+
115+
static const struct export_operations kernfs_export_ops = {
116+
.fh_to_dentry = kernfs_fh_to_dentry,
117+
.fh_to_parent = kernfs_fh_to_parent,
118+
.get_parent = kernfs_get_parent_dentry,
119+
};
120+
67121
/**
68122
* kernfs_root_from_sb - determine kernfs_root associated with a super_block
69123
* @sb: the super_block in question
@@ -159,6 +213,8 @@ static int kernfs_fill_super(struct super_block *sb, unsigned long magic)
159213
sb->s_magic = magic;
160214
sb->s_op = &kernfs_sops;
161215
sb->s_xattr = kernfs_xattr_handlers;
216+
if (info->root->flags & KERNFS_ROOT_SUPPORT_EXPORTOP)
217+
sb->s_export_op = &kernfs_export_ops;
162218
sb->s_time_gran = 1;
163219

164220
/* get root inode, initialize and unlock it */

‎include/linux/kernfs.h‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -69,6 +69,12 @@ enum kernfs_root_flag {
6969
* following flag enables that behavior.
7070
*/
7171
KERNFS_ROOT_EXTRA_OPEN_PERM_CHECK = 0x0002,
72+
73+
/*
74+
* The filesystem supports exportfs operation, so userspace can use
75+
* fhandle to access nodes of the fs.
76+
*/
77+
KERNFS_ROOT_SUPPORT_EXPORTOP = 0x0004,
7278
};
7379

7480
/* type-specific structures for kernfs_node union members */
@@ -98,6 +104,12 @@ struct kernfs_elem_attr {
98104
/* represent a kernfs node */
99105
union kernfs_node_id {
100106
struct {
107+
/*
108+
* blktrace will export this struct as a simplified 'struct
109+
* fid' (which is a big data struction), so userspace can use
110+
* it to find kernfs node. The layout must match the first two
111+
* fields of 'struct fid' exactly.
112+
*/
101113
u32 ino;
102114
u32 generation;
103115
};

‎kernel/cgroup/cgroup.c‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1737,7 +1737,8 @@ int cgroup_setup_root(struct cgroup_root *root, u16 ss_mask, int ref_flags)
17371737
&cgroup_kf_syscall_ops : &cgroup1_kf_syscall_ops;
17381738

17391739
root->kf_root = kernfs_create_root(kf_sops,
1740-
KERNFS_ROOT_CREATE_DEACTIVATED,
1740+
KERNFS_ROOT_CREATE_DEACTIVATED |
1741+
KERNFS_ROOT_SUPPORT_EXPORTOP,
17411742
root_cgrp);
17421743
if (IS_ERR(root->kf_root)) {
17431744
ret = PTR_ERR(root->kf_root);

0 commit comments

Comments
 (0)