Commit 93b2b8e4 authored by Tejun Heo's avatar Tejun Heo Committed by Greg Kroah-Hartman

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 #2"
    so that this patch removes sysfs_enable_ns().
Signed-off-by: default avatarTejun Heo <tj@kernel.org>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@linuxfoundation.org>
parent 7c6e2d36
...@@ -666,9 +666,18 @@ struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd, ...@@ -666,9 +666,18 @@ struct sysfs_dirent *sysfs_get_dirent_ns(struct sysfs_dirent *parent_sd,
} }
EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns); EXPORT_SYMBOL_GPL(sysfs_get_dirent_ns);
static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, /**
const char *name, const void *ns, * kernfs_create_dir_ns - create a directory
struct sysfs_dirent **p_sd) * @parent: parent in which to create a new directory
* @name: name of the new directory
* @priv: opaque data associated with the new directory
* @ns: optional namespace tag of the directory
*
* Returns the created node on success, ERR_PTR() value on failure.
*/
struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
const char *name, void *priv,
const void *ns)
{ {
umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO; umode_t mode = S_IFDIR | S_IRWXU | S_IRUGO | S_IXUGO;
struct sysfs_addrm_cxt acxt; struct sysfs_addrm_cxt acxt;
...@@ -678,28 +687,21 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, ...@@ -678,28 +687,21 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
/* allocate */ /* allocate */
sd = sysfs_new_dirent(name, mode, SYSFS_DIR); sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
if (!sd) if (!sd)
return -ENOMEM; return ERR_PTR(-ENOMEM);
sd->s_ns = ns; sd->s_ns = ns;
sd->priv = kobj; sd->priv = priv;
/* link in */ /* link in */
sysfs_addrm_start(&acxt); sysfs_addrm_start(&acxt);
rc = sysfs_add_one(&acxt, sd, parent_sd); rc = __sysfs_add_one(&acxt, sd, parent);
sysfs_addrm_finish(&acxt); sysfs_addrm_finish(&acxt);
if (rc == 0) if (!rc)
*p_sd = sd; return sd;
else
sysfs_put(sd);
return rc;
}
int sysfs_create_subdir(struct kobject *kobj, const char *name, sysfs_put(sd);
struct sysfs_dirent **p_sd) return ERR_PTR(rc);
{
return create_dir(kobj, kobj->sd, name, NULL, p_sd);
} }
/** /**
...@@ -710,7 +712,6 @@ int sysfs_create_subdir(struct kobject *kobj, const char *name, ...@@ -710,7 +712,6 @@ int sysfs_create_subdir(struct kobject *kobj, const char *name,
int sysfs_create_dir_ns(struct kobject *kobj, const void *ns) int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
{ {
struct sysfs_dirent *parent_sd, *sd; struct sysfs_dirent *parent_sd, *sd;
int error = 0;
BUG_ON(!kobj); BUG_ON(!kobj);
...@@ -722,10 +723,15 @@ int sysfs_create_dir_ns(struct kobject *kobj, const void *ns) ...@@ -722,10 +723,15 @@ int sysfs_create_dir_ns(struct kobject *kobj, const void *ns)
if (!parent_sd) if (!parent_sd)
return -ENOENT; return -ENOENT;
error = create_dir(kobj, parent_sd, kobject_name(kobj), ns, &sd); sd = kernfs_create_dir_ns(parent_sd, kobject_name(kobj), kobj, ns);
if (!error) if (IS_ERR(sd)) {
if (PTR_ERR(sd) == -EEXIST)
sysfs_warn_dup(parent_sd, kobject_name(kobj));
return PTR_ERR(sd);
}
kobj->sd = sd; kobj->sd = sd;
return error; return 0;
} }
static struct dentry *sysfs_lookup(struct inode *dir, struct dentry *dentry, 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, ...@@ -1005,14 +1011,14 @@ int sysfs_move_dir_ns(struct kobject *kobj, struct kobject *new_parent_kobj,
} }
/** /**
* sysfs_enable_ns - enable namespace under a directory * kernfs_enable_ns - enable namespace under a directory
* @sd: directory of interest, should be empty * @sd: directory of interest, should be empty
* *
* This is to be called right after @sd is created to enable namespace * This is to be called right after @sd is created to enable namespace
* under it. All children of @sd must have non-NULL namespace tags and * under it. All children of @sd must have non-NULL namespace tags and
* only the ones which match the super_block's tag will be visible. * only the ones which match the super_block's tag will be visible.
*/ */
void sysfs_enable_ns(struct sysfs_dirent *sd) void kernfs_enable_ns(struct sysfs_dirent *sd)
{ {
WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR); WARN_ON_ONCE(sysfs_type(sd) != SYSFS_DIR);
WARN_ON_ONCE(!RB_EMPTY_ROOT(&sd->s_dir.children)); WARN_ON_ONCE(!RB_EMPTY_ROOT(&sd->s_dir.children));
......
...@@ -101,9 +101,12 @@ static int internal_create_group(struct kobject *kobj, int update, ...@@ -101,9 +101,12 @@ static int internal_create_group(struct kobject *kobj, int update,
return -EINVAL; return -EINVAL;
} }
if (grp->name) { if (grp->name) {
error = sysfs_create_subdir(kobj, grp->name, &sd); sd = kernfs_create_dir(kobj->sd, grp->name, kobj);
if (error) if (IS_ERR(sd)) {
return error; if (PTR_ERR(sd) == -EEXIST)
sysfs_warn_dup(kobj->sd, grp->name);
return PTR_ERR(sd);
}
} else } else
sd = kobj->sd; sd = kobj->sd;
sysfs_get(sd); sysfs_get(sd);
......
...@@ -179,9 +179,6 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type); ...@@ -179,9 +179,6 @@ struct sysfs_dirent *sysfs_new_dirent(const char *name, umode_t mode, int type);
void release_sysfs_dirent(struct sysfs_dirent *sd); void release_sysfs_dirent(struct sysfs_dirent *sd);
int sysfs_create_subdir(struct kobject *kobj, const char *name,
struct sysfs_dirent **p_sd);
static inline struct sysfs_dirent *__sysfs_get(struct sysfs_dirent *sd) static inline struct sysfs_dirent *__sysfs_get(struct sysfs_dirent *sd)
{ {
if (sd) { if (sd) {
......
...@@ -17,6 +17,9 @@ struct sysfs_dirent; ...@@ -17,6 +17,9 @@ struct sysfs_dirent;
#ifdef CONFIG_SYSFS #ifdef CONFIG_SYSFS
struct sysfs_dirent *kernfs_create_dir_ns(struct sysfs_dirent *parent,
const char *name, void *priv,
const void *ns);
struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent, struct sysfs_dirent *kernfs_create_link(struct sysfs_dirent *parent,
const char *name, const char *name,
struct sysfs_dirent *target); struct sysfs_dirent *target);
...@@ -25,10 +28,16 @@ int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name, ...@@ -25,10 +28,16 @@ int kernfs_remove_by_name_ns(struct sysfs_dirent *parent, const char *name,
const void *ns); const void *ns);
int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent, int kernfs_rename_ns(struct sysfs_dirent *sd, struct sysfs_dirent *new_parent,
const char *new_name, const void *new_ns); const char *new_name, const void *new_ns);
void kernfs_enable_ns(struct sysfs_dirent *sd);
int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr); int kernfs_setattr(struct sysfs_dirent *sd, const struct iattr *iattr);
#else /* CONFIG_SYSFS */ #else /* CONFIG_SYSFS */
static inline struct sysfs_dirent *
kernfs_create_dir_ns(struct sysfs_dirent *parent, const char *name, void *priv,
const void *ns)
{ return ERR_PTR(-ENOSYS); }
static inline struct sysfs_dirent * static inline struct sysfs_dirent *
kernfs_create_link(struct sysfs_dirent *parent, const char *name, kernfs_create_link(struct sysfs_dirent *parent, const char *name,
struct sysfs_dirent *target) struct sysfs_dirent *target)
...@@ -45,12 +54,20 @@ static inline int kernfs_rename_ns(struct sysfs_dirent *sd, ...@@ -45,12 +54,20 @@ static inline int kernfs_rename_ns(struct sysfs_dirent *sd,
const char *new_name, const void *new_ns) const char *new_name, const void *new_ns)
{ return -ENOSYS; } { return -ENOSYS; }
static inline void kernfs_enable_ns(struct sysfs_dirent *sd) { }
static inline int kernfs_setattr(struct sysfs_dirent *sd, static inline int kernfs_setattr(struct sysfs_dirent *sd,
const struct iattr *iattr) const struct iattr *iattr)
{ return -ENOSYS; } { return -ENOSYS; }
#endif /* CONFIG_SYSFS */ #endif /* CONFIG_SYSFS */
static inline struct sysfs_dirent *
kernfs_create_dir(struct sysfs_dirent *parent, const char *name, void *priv)
{
return kernfs_create_dir_ns(parent, name, priv, NULL);
}
static inline int kernfs_remove_by_name(struct sysfs_dirent *parent, static inline int kernfs_remove_by_name(struct sysfs_dirent *parent,
const char *name) const char *name)
{ {
......
...@@ -219,8 +219,6 @@ int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target, ...@@ -219,8 +219,6 @@ int sysfs_rename_link_ns(struct kobject *kobj, struct kobject *target,
void sysfs_delete_link(struct kobject *dir, struct kobject *targ, void sysfs_delete_link(struct kobject *dir, struct kobject *targ,
const char *name); const char *name);
void sysfs_enable_ns(struct sysfs_dirent *sd);
int __must_check sysfs_create_group(struct kobject *kobj, int __must_check sysfs_create_group(struct kobject *kobj,
const struct attribute_group *grp); const struct attribute_group *grp);
int __must_check sysfs_create_groups(struct kobject *kobj, 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, ...@@ -354,10 +352,6 @@ static inline void sysfs_delete_link(struct kobject *k, struct kobject *t,
{ {
} }
static inline void sysfs_enable_ns(struct sysfs_dirent *sd)
{
}
static inline int sysfs_create_group(struct kobject *kobj, static inline int sysfs_create_group(struct kobject *kobj,
const struct attribute_group *grp) const struct attribute_group *grp)
{ {
......
...@@ -94,7 +94,7 @@ static int create_dir(struct kobject *kobj) ...@@ -94,7 +94,7 @@ static int create_dir(struct kobject *kobj)
BUG_ON(ops->type >= KOBJ_NS_TYPES); BUG_ON(ops->type >= KOBJ_NS_TYPES);
BUG_ON(!kobj_ns_type_registered(ops->type)); BUG_ON(!kobj_ns_type_registered(ops->type));
sysfs_enable_ns(kobj->sd); kernfs_enable_ns(kobj->sd);
} }
return 0; return 0;
......
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