Skip to content

Commit ba7443b

Browse files
htejungregkh
authored andcommitted
sysfs, kernfs: implement kernfs_create/destroy_root()
There currently is single kernfs hierarchy in the whole system which is used for sysfs. kernfs needs to support multiple hierarchies to allow other users. This patch introduces struct kernfs_root which serves as the root of each kernfs hierarchy and implements kernfs_create/destroy_root(). * Each kernfs_root is associated with a root sd (sysfs_dentry). The root is freed when the root sd is released and kernfs_destory_root() simply invokes kernfs_remove() on the root sd. sysfs_remove_one() is updated to handle release of the root sd. Note that ps_iattr update in sysfs_remove_one() is trivially updated for readability. * Root sd's are now dynamically allocated using sysfs_new_dirent(). Update sysfs_alloc_ino() so that it gives out ino from 1 so that the root sd still gets ino 1. * While kernfs currently only points to the root sd, it'll soon grow fields which are specific to each hierarchy. As determining a given sd's root will be necessary, sd->s_dir.root is added. This backlink fits better as a separate field in sd; however, sd->s_dir is inside union with space to spare, so use it to save space and provide kernfs_root() accessor to determine the root sd. * As hierarchies may be destroyed now, each mount needs to hold onto the hierarchy it's attached to. Update sysfs_fill_super() and sysfs_kill_sb() so that they get and put the kernfs_root respectively. * sysfs_root is replaced with kernfs_root which is dynamically created by invoking kernfs_create_root() from sysfs_init(). This patch doesn't introduce any visible behavior changes. v2: kernfs_create_root() forgot to set @sd->priv. Fixed. Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 061447a commit ba7443b

File tree

4 files changed

+113
-20
lines changed

4 files changed

+113
-20
lines changed

fs/kernfs/dir.c

Lines changed: 62 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ static int sysfs_alloc_ino(unsigned int *pino)
211211

212212
retry:
213213
spin_lock(&sysfs_ino_lock);
214-
rc = ida_get_new_above(&sysfs_ino_ida, 2, &ino);
214+
rc = ida_get_new_above(&sysfs_ino_ida, 1, &ino);
215215
spin_unlock(&sysfs_ino_lock);
216216

217217
if (rc == -EAGAIN) {
@@ -253,9 +253,11 @@ EXPORT_SYMBOL_GPL(kernfs_get);
253253
void kernfs_put(struct sysfs_dirent *sd)
254254
{
255255
struct sysfs_dirent *parent_sd;
256+
struct kernfs_root *root;
256257

257258
if (!sd || !atomic_dec_and_test(&sd->s_count))
258259
return;
260+
root = kernfs_root(sd);
259261
repeat:
260262
/* Moving/renaming is always done while holding reference.
261263
* sd->s_parent won't change beneath us.
@@ -278,8 +280,13 @@ void kernfs_put(struct sysfs_dirent *sd)
278280
kmem_cache_free(sysfs_dir_cachep, sd);
279281

280282
sd = parent_sd;
281-
if (sd && atomic_dec_and_test(&sd->s_count))
282-
goto repeat;
283+
if (sd) {
284+
if (atomic_dec_and_test(&sd->s_count))
285+
goto repeat;
286+
} else {
287+
/* just released the root sd, free @root too */
288+
kfree(root);
289+
}
283290
}
284291
EXPORT_SYMBOL_GPL(kernfs_put);
285292

@@ -493,13 +500,15 @@ static void sysfs_remove_one(struct sysfs_addrm_cxt *acxt,
493500
if (sd->s_flags & SYSFS_FLAG_REMOVED)
494501
return;
495502

496-
sysfs_unlink_sibling(sd);
503+
if (sd->s_parent) {
504+
sysfs_unlink_sibling(sd);
497505

498-
/* Update timestamps on the parent */
499-
ps_iattr = sd->s_parent->s_iattr;
500-
if (ps_iattr) {
501-
struct iattr *ps_iattrs = &ps_iattr->ia_iattr;
502-
ps_iattrs->ia_ctime = ps_iattrs->ia_mtime = CURRENT_TIME;
506+
/* Update timestamps on the parent */
507+
ps_iattr = sd->s_parent->s_iattr;
508+
if (ps_iattr) {
509+
ps_iattr->ia_iattr.ia_ctime = CURRENT_TIME;
510+
ps_iattr->ia_iattr.ia_mtime = CURRENT_TIME;
511+
}
503512
}
504513

505514
sd->s_flags |= SYSFS_FLAG_REMOVED;
@@ -603,6 +612,49 @@ struct sysfs_dirent *kernfs_find_and_get_ns(struct sysfs_dirent *parent,
603612
}
604613
EXPORT_SYMBOL_GPL(kernfs_find_and_get_ns);
605614

615+
/**
616+
* kernfs_create_root - create a new kernfs hierarchy
617+
* @priv: opaque data associated with the new directory
618+
*
619+
* Returns the root of the new hierarchy on success, ERR_PTR() value on
620+
* failure.
621+
*/
622+
struct kernfs_root *kernfs_create_root(void *priv)
623+
{
624+
struct kernfs_root *root;
625+
struct sysfs_dirent *sd;
626+
627+
root = kzalloc(sizeof(*root), GFP_KERNEL);
628+
if (!root)
629+
return ERR_PTR(-ENOMEM);
630+
631+
sd = sysfs_new_dirent("", S_IFDIR | S_IRUGO | S_IXUGO, SYSFS_DIR);
632+
if (!sd) {
633+
kfree(root);
634+
return ERR_PTR(-ENOMEM);
635+
}
636+
637+
sd->s_flags &= ~SYSFS_FLAG_REMOVED;
638+
sd->priv = priv;
639+
sd->s_dir.root = root;
640+
641+
root->sd = sd;
642+
643+
return root;
644+
}
645+
646+
/**
647+
* kernfs_destroy_root - destroy a kernfs hierarchy
648+
* @root: root of the hierarchy to destroy
649+
*
650+
* Destroy the hierarchy anchored at @root by removing all existing
651+
* directories and destroying @root.
652+
*/
653+
void kernfs_destroy_root(struct kernfs_root *root)
654+
{
655+
kernfs_remove(root->sd); /* will also free @root */
656+
}
657+
606658
/**
607659
* kernfs_create_dir_ns - create a directory
608660
* @parent: parent in which to create a new directory
@@ -626,6 +678,7 @@ struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
626678
if (!sd)
627679
return ERR_PTR(-ENOMEM);
628680

681+
sd->s_dir.root = parent->s_dir.root;
629682
sd->s_ns = ns;
630683
sd->priv = priv;
631684

fs/kernfs/kernfs-internal.h

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,12 @@ struct sysfs_elem_dir {
2525
unsigned long subdirs;
2626
/* children rbtree starts here and goes through sd->s_rb */
2727
struct rb_root children;
28+
29+
/*
30+
* The kernfs hierarchy this directory belongs to. This fits
31+
* better directly in sysfs_dirent but is here to save space.
32+
*/
33+
struct kernfs_root *root;
2834
};
2935

3036
struct sysfs_elem_symlink {
@@ -104,6 +110,20 @@ static inline unsigned int sysfs_type(struct sysfs_dirent *sd)
104110
return sd->s_flags & SYSFS_TYPE_MASK;
105111
}
106112

113+
/**
114+
* kernfs_root - find out the kernfs_root a sysfs_dirent belongs to
115+
* @sd: sysfs_dirent of interest
116+
*
117+
* Return the kernfs_root @sd belongs to.
118+
*/
119+
static inline struct kernfs_root *kernfs_root(struct sysfs_dirent *sd)
120+
{
121+
/* if parent exists, it's always a dir; otherwise, @sd is a dir */
122+
if (sd->s_parent)
123+
sd = sd->s_parent;
124+
return sd->s_dir.root;
125+
}
126+
107127
/*
108128
* Context structure to be used while adding/removing nodes.
109129
*/

fs/sysfs/mount.c

Lines changed: 18 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -32,15 +32,8 @@ static const struct super_operations sysfs_ops = {
3232
.evict_inode = sysfs_evict_inode,
3333
};
3434

35-
static struct sysfs_dirent sysfs_root = {
36-
.s_name = "",
37-
.s_count = ATOMIC_INIT(1),
38-
.s_flags = SYSFS_DIR,
39-
.s_mode = S_IFDIR | S_IRUGO | S_IXUGO,
40-
.s_ino = 1,
41-
};
42-
43-
struct sysfs_dirent *sysfs_root_sd = &sysfs_root;
35+
static struct kernfs_root *sysfs_root;
36+
struct sysfs_dirent *sysfs_root_sd;
4437

4538
static int sysfs_fill_super(struct super_block *sb)
4639
{
@@ -68,6 +61,7 @@ static int sysfs_fill_super(struct super_block *sb)
6861
pr_debug("%s: could not get root dentry!\n", __func__);
6962
return -ENOMEM;
7063
}
64+
kernfs_get(sysfs_root_sd);
7165
root->d_fsdata = sysfs_root_sd;
7266
sb->s_root = root;
7367
sb->s_d_op = &sysfs_dentry_ops;
@@ -138,11 +132,15 @@ static struct dentry *sysfs_mount(struct file_system_type *fs_type,
138132
static void sysfs_kill_sb(struct super_block *sb)
139133
{
140134
struct sysfs_super_info *info = sysfs_info(sb);
141-
/* Remove the superblock from fs_supers/s_instances
135+
struct sysfs_dirent *root_sd = sb->s_root->d_fsdata;
136+
137+
/*
138+
* Remove the superblock from fs_supers/s_instances
142139
* so we can't find it, before freeing sysfs_super_info.
143140
*/
144141
kill_anon_super(sb);
145142
free_sysfs_super_info(info);
143+
kernfs_put(root_sd);
146144
}
147145

148146
static struct file_system_type sysfs_fs_type = {
@@ -166,12 +164,21 @@ int __init sysfs_init(void)
166164
if (err)
167165
goto out_err;
168166

167+
sysfs_root = kernfs_create_root(NULL);
168+
if (IS_ERR(sysfs_root)) {
169+
err = PTR_ERR(sysfs_root);
170+
goto out_err;
171+
}
172+
sysfs_root_sd = sysfs_root->sd;
173+
169174
err = register_filesystem(&sysfs_fs_type);
170175
if (err)
171-
goto out_err;
176+
goto out_destroy_root;
172177

173178
return 0;
174179

180+
out_destroy_root:
181+
kernfs_destroy_root(sysfs_root);
175182
out_err:
176183
kmem_cache_destroy(sysfs_dir_cachep);
177184
sysfs_dir_cachep = NULL;

include/linux/kernfs.h

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ struct vm_area_struct;
2020

2121
struct sysfs_dirent;
2222

23+
struct kernfs_root {
24+
/* published fields */
25+
struct sysfs_dirent *sd;
26+
};
27+
2328
struct sysfs_open_file {
2429
/* published fields */
2530
struct sysfs_dirent *sd;
@@ -76,6 +81,9 @@ struct sysfs_dirent *kernfs_find_and_get_ns(struct sysfs_dirent *parent,
7681
void kernfs_get(struct sysfs_dirent *sd);
7782
void kernfs_put(struct sysfs_dirent *sd);
7883

84+
struct kernfs_root *kernfs_create_root(void *priv);
85+
void kernfs_destroy_root(struct kernfs_root *root);
86+
7987
struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
8088
const char *name, void *priv,
8189
const void *ns);
@@ -107,6 +115,11 @@ kernfs_find_and_get_ns(struct sysfs_dirent *parent, const char *name,
107115
static inline void kernfs_get(struct sysfs_dirent *sd) { }
108116
static inline void kernfs_put(struct sysfs_dirent *sd) { }
109117

118+
static inline struct kernfs_root *kernfs_create_root(void *priv)
119+
{ return ERR_PTR(-ENOSYS); }
120+
121+
static inline void kernfs_destroy_root(struct kernfs_root *root) { }
122+
110123
static inline struct sysfs_dirent *
111124
kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv,
112125
const void *ns)

0 commit comments

Comments
 (0)