Skip to content

Commit 80b9bbe

Browse files
htejungregkh
authored andcommitted
kernfs: add kernfs_dir_ops
Add support for mkdir(2), rmdir(2) and rename(2) syscalls. This is implemented through optional kernfs_dir_ops callback table which can be specified on kernfs_create_root(). An implemented callback is invoked when the matching syscall is invoked. As kernfs keep dcache syncs with internal representation and revalidates dentries on each access, the implementation of these methods is extremely simple. Each just discovers the relevant kernfs_node(s) and invokes the requested callback which is allowed to do any kernfs operations and the end result doesn't necessarily have to match the expected semantics of the syscall. This will be used to convert cgroup to use kernfs instead of its own filesystem implementation. Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 19bbb92 commit 80b9bbe

3 files changed

Lines changed: 63 additions & 4 deletions

File tree

‎fs/kernfs/dir.c‎

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -583,12 +583,13 @@ EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
583583

584584
/**
585585
* kernfs_create_root - create a new kernfs hierarchy
586+
* @kdops: optional directory syscall operations for the hierarchy
586587
* @priv: opaque data associated with the new directory
587588
*
588589
* Returns the root of the new hierarchy on success, ERR_PTR() value on
589590
* failure.
590591
*/
591-
struct kernfs_root *kernfs_create_root(void *priv)
592+
struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
592593
{
593594
struct kernfs_root *root;
594595
struct kernfs_node *kn;
@@ -610,6 +611,7 @@ struct kernfs_root *kernfs_create_root(void *priv)
610611
kn->priv = priv;
611612
kn->dir.root = root;
612613

614+
root->dir_ops = kdops;
613615
root->kn = kn;
614616

615617
return root;
@@ -706,6 +708,42 @@ static struct dentry *kernfs_iop_lookup(struct inode *dir,
706708
return ret;
707709
}
708710

711+
static int kernfs_iop_mkdir(struct inode *dir, struct dentry *dentry,
712+
umode_t mode)
713+
{
714+
struct kernfs_node *parent = dir->i_private;
715+
struct kernfs_dir_ops *kdops = kernfs_root(parent)->dir_ops;
716+
717+
if (!kdops || !kdops->mkdir)
718+
return -EPERM;
719+
720+
return kdops->mkdir(parent, dentry->d_name.name, mode);
721+
}
722+
723+
static int kernfs_iop_rmdir(struct inode *dir, struct dentry *dentry)
724+
{
725+
struct kernfs_node *kn = dentry->d_fsdata;
726+
struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
727+
728+
if (!kdops || !kdops->rmdir)
729+
return -EPERM;
730+
731+
return kdops->rmdir(kn);
732+
}
733+
734+
static int kernfs_iop_rename(struct inode *old_dir, struct dentry *old_dentry,
735+
struct inode *new_dir, struct dentry *new_dentry)
736+
{
737+
struct kernfs_node *kn = old_dentry->d_fsdata;
738+
struct kernfs_node *new_parent = new_dir->i_private;
739+
struct kernfs_dir_ops *kdops = kernfs_root(kn)->dir_ops;
740+
741+
if (!kdops || !kdops->rename)
742+
return -EPERM;
743+
744+
return kdops->rename(kn, new_parent, new_dentry->d_name.name);
745+
}
746+
709747
const struct inode_operations kernfs_dir_iops = {
710748
.lookup = kernfs_iop_lookup,
711749
.permission = kernfs_iop_permission,
@@ -715,6 +753,10 @@ const struct inode_operations kernfs_dir_iops = {
715753
.removexattr = kernfs_iop_removexattr,
716754
.getxattr = kernfs_iop_getxattr,
717755
.listxattr = kernfs_iop_listxattr,
756+
757+
.mkdir = kernfs_iop_mkdir,
758+
.rmdir = kernfs_iop_rmdir,
759+
.rename = kernfs_iop_rename,
718760
};
719761

720762
static struct kernfs_node *kernfs_leftmost_descendant(struct kernfs_node *pos)

‎fs/sysfs/mount.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ int __init sysfs_init(void)
6262
{
6363
int err;
6464

65-
sysfs_root = kernfs_create_root(NULL);
65+
sysfs_root = kernfs_create_root(NULL, NULL);
6666
if (IS_ERR(sysfs_root))
6767
return PTR_ERR(sysfs_root);
6868

‎include/linux/kernfs.h‎

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,27 @@ struct kernfs_node {
111111
struct kernfs_iattrs *iattr;
112112
};
113113

114+
/*
115+
* kernfs_dir_ops may be specified on kernfs_create_root() to support
116+
* directory manipulation syscalls. These optional callbacks are invoked
117+
* on the matching syscalls and can perform any kernfs operations which
118+
* don't necessarily have to be the exact operation requested.
119+
*/
120+
struct kernfs_dir_ops {
121+
int (*mkdir)(struct kernfs_node *parent, const char *name,
122+
umode_t mode);
123+
int (*rmdir)(struct kernfs_node *kn);
124+
int (*rename)(struct kernfs_node *kn, struct kernfs_node *new_parent,
125+
const char *new_name);
126+
};
127+
114128
struct kernfs_root {
115129
/* published fields */
116130
struct kernfs_node *kn;
117131

118132
/* private fields, do not use outside kernfs proper */
119133
struct ida ino_ida;
134+
struct kernfs_dir_ops *dir_ops;
120135
};
121136

122137
struct kernfs_open_file {
@@ -206,7 +221,8 @@ struct kernfs_node *kernfs_find_and_get_ns(struct kernfs_node *parent,
206221
void kernfs_get(struct kernfs_node *kn);
207222
void kernfs_put(struct kernfs_node *kn);
208223

209-
struct kernfs_root *kernfs_create_root(void *priv);
224+
struct kernfs_root *kernfs_create_root(struct kernfs_dir_ops *kdops,
225+
void *priv);
210226
void kernfs_destroy_root(struct kernfs_root *root);
211227

212228
struct kernfs_node *kernfs_create_dir_ns(struct kernfs_node *parent,
@@ -255,7 +271,8 @@ kernfs_find_and_get_ns(struct kernfs_node *parent, const char *name,
255271
static inline void kernfs_get(struct kernfs_node *kn) { }
256272
static inline void kernfs_put(struct kernfs_node *kn) { }
257273

258-
static inline struct kernfs_root *kernfs_create_root(void *priv)
274+
static inline struct kernfs_root *
275+
kernfs_create_root(struct kernfs_dir_ops *kdops, void *priv)
259276
{ return ERR_PTR(-ENOSYS); }
260277

261278
static inline void kernfs_destroy_root(struct kernfs_root *root) { }

0 commit comments

Comments
 (0)