Skip to content

Commit 93b2b8e

Browse files
htejungregkh
authored andcommitted
sysfs, kernfs: introduce kernfs_create_dir[_ns]()
Introduce kernfs interface to manipulate a directory which takes and returns sysfs_dirents. create_dir() is renamed to kernfs_create_dir_ns() and its argumantes and return value are updated. create_dir() usages are replaced with kernfs_create_dir_ns() and sysfs_create_subdir() usages are replaced with kernfs_create_dir(). Dup warnings are handled explicitly by sysfs users of the kernfs interface. sysfs_enable_ns() is renamed to kernfs_enable_ns(). This patch doesn't introduce any behavior changes. v2: Dummy implementation for !CONFIG_SYSFS updated to return -ENOSYS. v3: kernfs_enable_ns() added. v4: Refreshed on top of "sysfs: drop kobj_ns_type handling, take raspberrypi#2" so that this patch removes sysfs_enable_ns(). Signed-off-by: Tejun Heo <tj@kernel.org> Signed-off-by: Greg Kroah-Hartman <gregkh@linuxfoundation.org>
1 parent 7c6e2d3 commit 93b2b8e

6 files changed

Lines changed: 54 additions & 37 deletions

File tree

‎fs/sysfs/dir.c‎

Lines changed: 30 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -666,9 +666,18 @@ struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd,
666666
}
667667
EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns);
668668

669-
static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
670-
const char *name, const void *ns,
671-
struct sysfs_dirent **p_sd)
669+
/**
670+
* kernfs_create_dir_ns - create a directory
671+
* @parent: parent in which to create a new directory
672+
* @name: name of the new directory
673+
* @priv: opaque data associated with the new directory
674+
* @ns: optional namespace tag of the directory
675+
*
676+
* Returns the created node on success, ERR_PTR() value on failure.
677+
*/
678+
struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
679+
const char *name, void *priv,
680+
const void *ns)
672681
{
673682
umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
674683
struct sysfs_addrm_cxt acxt;
@@ -678,28 +687,21 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
678687
/* allocate */
679688
sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
680689
if (!sd)
681-
return -ENOMEM;
690+
return ERR_PTR(-ENOMEM);
682691

683692
sd->s_ns = ns;
684-
sd->priv = kobj;
693+
sd->priv = priv;
685694

686695
/* link in */
687696
sysfs_addrm_start(&acxt);
688-
rc = sysfs_add_one(&acxt, sd, parent_sd);
697+
rc = __sysfs_add_one(&acxt, sd, parent);
689698
sysfs_addrm_finish(&acxt);
690699

691-
if (rc == 0)
692-
*p_sd = sd;
693-
else
694-
sysfs_put(sd);
700+
if (!rc)
701+
return sd;
695702

696-
return rc;
697-
}
698-
699-
int sysfs_create_subdir(struct kobject *kobj, const char *name,
700-
struct sysfs_dirent **p_sd)
701-
{
702-
return create_dir(kobj, kobj->sd, name, NULL, p_sd);
703+
sysfs_put(sd);
704+
return ERR_PTR(rc);
703705
}
704706

705707
/**
@@ -710,7 +712,6 @@ int sysfs_create_subdir(struct kobject *kobj, const char *name,
710712
int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
711713
{
712714
struct sysfs_dirent *parent_sd, *sd;
713-
int error = 0;
714715

715716
BUG_ON(!kobj);
716717

@@ -722,10 +723,15 @@ int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
722723
if (!parent_sd)
723724
return -ENOENT;
724725

725-
error = create_dir(kobj, parent_sd, kobject_name(kobj), ns, &sd);
726-
if (!error)
727-
kobj->sd = sd;
728-
return error;
726+
sd = kernfs_create_dir_ns(parent_sd, kobject_name(kobj), kobj, ns);
727+
if (IS_ERR(sd)) {
728+
if (PTR_ERR(sd) == -EEXIST)
729+
sysfs_warn_dup(parent_sd, kobject_name(kobj));
730+
return PTR_ERR(sd);
731+
}
732+
733+
kobj->sd = sd;
734+
return 0;
729735
}
730736

731737
static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry,
@@ -1005,14 +1011,14 @@ int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
10051011
}
10061012

10071013
/**
1008-
* sysfs_enable_ns - enable namespace under a directory
1014+
* kernfs_enable_ns - enable namespace under a directory
10091015
* @sd: directory of interest, should be empty
10101016
*
10111017
* This is to be called right after @sd is created to enable namespace
10121018
* under it. All children of @sd must have non-NULL namespace tags and
10131019
* only the ones which match the super_block's tag will be visible.
10141020
*/
1015-
void sysfs_enable_ns(struct sysfs_dirent *sd)
1021+
void kernfs_enable_ns(struct sysfs_dirent *sd)
10161022
{
10171023
WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR);
10181024
WARN_ON_ONCE(!RB_EMPTY_ROOT(&sd->s_dir.children));

‎fs/sysfs/group.c‎

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,12 @@ static int internal_create_group(struct kobject *kobj, int update,
101101
return -EINVAL;
102102
}
103103
if (grp->name) {
104-
error = sysfs_create_subdir(kobj, grp->name, &sd);
105-
if (error)
106-
return error;
104+
sd = kernfs_create_dir(kobj->sd, grp->name, kobj);
105+
if (IS_ERR(sd)) {
106+
if (PTR_ERR(sd) == -EEXIST)
107+
sysfs_warn_dup(kobj->sd, grp->name);
108+
return PTR_ERR(sd);
109+
}
107110
} else
108111
sd = kobj->sd;
109112
sysfs_get(sd);

‎fs/sysfs/sysfs.h‎

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -179,9 +179,6 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type);
179179

180180
void release_sysfs_dirent(struct sysfs_dirent *sd);
181181

182-
int sysfs_create_subdir(struct kobject *kobj, const char *name,
183-
struct sysfs_dirent **p_sd);
184-
185182
static inline struct sysfs_dirent *__sysfs_get(struct sysfs_dirent *sd)
186183
{
187184
if (sd) {

‎include/linux/kernfs.h‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@ struct sysfs_dirent;
1717

1818
#ifdef CONFIG_SYSFS
1919

20+
struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
21+
const char *name, void *priv,
22+
const void *ns);
2023
struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
2124
const char *name,
2225
struct sysfs_dirent *target);
@@ -25,10 +28,16 @@ int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name,
2528
const void *ns);
2629
int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
2730
const char *new_name, const void *new_ns);
31+
void kernfs_enable_ns(struct sysfs_dirent *sd);
2832
int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr);
2933

3034
#else /* CONFIG_SYSFS */
3135

36+
static inline struct sysfs_dirent *
37+
kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv,
38+
const void *ns)
39+
{ return ERR_PTR(-ENOSYS); }
40+
3241
static inline struct sysfs_dirent *
3342
kernfs_create_link(struct sysfs_dirent *parent, const char *name,
3443
struct sysfs_dirent *target)
@@ -45,12 +54,20 @@ static inline int kernfs_rename_ns(struct sysfs_dirent *sd,
4554
const char *new_name, const void *new_ns)
4655
{ return -ENOSYS; }
4756

57+
static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { }
58+
4859
static inline int kernfs_setattr(struct sysfs_dirent *sd,
4960
const struct iattr *iattr)
5061
{ return -ENOSYS; }
5162

5263
#endif /* CONFIG_SYSFS */
5364

65+
static inline struct sysfs_dirent *
66+
kernfs_create_dir(struct sysfs_dirent *parent, const char *name, void *priv)
67+
{
68+
return kernfs_create_dir_ns(parent, name, priv, NULL);
69+
}
70+
5471
static inline int kernfs_remove_by_name(struct sysfs_dirent *parent,
5572
const char *name)
5673
{

‎include/linux/sysfs.h‎

Lines changed: 0 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -219,8 +219,6 @@ int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target,
219219
void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
220220
const char *name);
221221

222-
void sysfs_enable_ns(struct sysfs_dirent *sd);
223-
224222
int __must_check sysfs_create_group(struct kobject *kobj,
225223
const struct attribute_group *grp);
226224
int __must_check sysfs_create_groups(struct kobject *kobj,
@@ -354,10 +352,6 @@ static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
354352
{
355353
}
356354

357-
static inline void sysfs_enable_ns(struct sysfs_dirent *sd)
358-
{
359-
}
360-
361355
static inline int sysfs_create_group(struct kobject *kobj,
362356
const struct attribute_group *grp)
363357
{

‎lib/kobject.c‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ static int create_dir(struct kobject *kobj)
9494
BUG_ON(ops->type >= KOBJ_NS_TYPES);
9595
BUG_ON(!kobj_ns_type_registered(ops->type));
9696

97-
sysfs_enable_ns(kobj->sd);
97+
kernfs_enable_ns(kobj->sd);
9898
}
9999

100100
return 0;

0 commit comments

Comments
 (0)