Commit 23dc2799 authored by Tejun Heo's avatar Tejun Heo Committed by Greg Kroah-Hartman

sysfs: make sysfs_add_one() automatically check for duplicate entry

Make sysfs_add_one() check for duplicate entry and return -EEXIST if
such entry exists.  This simplifies node addition code a bit.

This patch doesn't introduce any noticeable behavior change.
Signed-off-by: default avatarTejun Heo <htejun@gmail.com>
Acked-by: default avatarCornelia Huck <cornelia.huck@de.ibm.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 41fc1c27
...@@ -491,9 +491,16 @@ void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt, ...@@ -491,9 +491,16 @@ void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
* *
* LOCKING: * LOCKING:
* Determined by sysfs_addrm_start(). * Determined by sysfs_addrm_start().
*
* RETURNS:
* 0 on success, -EEXIST if entry with the given name already
* exists.
*/ */
void sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd) int sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
{ {
if (sysfs_find_dirent(acxt->parent_sd, sd->s_name))
return -EEXIST;
sd->s_parent = sysfs_get(acxt->parent_sd); sd->s_parent = sysfs_get(acxt->parent_sd);
if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode) if (sysfs_type(sd) == SYSFS_DIR && acxt->parent_inode)
...@@ -502,6 +509,8 @@ void sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd) ...@@ -502,6 +509,8 @@ void sysfs_add_one(struct sysfs_addrm_cxt *acxt, struct sysfs_dirent *sd)
acxt->cnt++; acxt->cnt++;
sysfs_link_sibling(sd); sysfs_link_sibling(sd);
return 0;
} }
/** /**
...@@ -691,6 +700,7 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, ...@@ -691,6 +700,7 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
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;
struct sysfs_dirent *sd; struct sysfs_dirent *sd;
int rc;
/* allocate */ /* allocate */
sd = sysfs_new_dirent(name, mode, SYSFS_DIR); sd = sysfs_new_dirent(name, mode, SYSFS_DIR);
...@@ -700,17 +710,15 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, ...@@ -700,17 +710,15 @@ static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd,
/* link in */ /* link in */
sysfs_addrm_start(&acxt, parent_sd); sysfs_addrm_start(&acxt, parent_sd);
rc = sysfs_add_one(&acxt, sd);
sysfs_addrm_finish(&acxt);
if (!sysfs_find_dirent(parent_sd, name)) if (rc == 0)
sysfs_add_one(&acxt, sd); *p_sd = sd;
else
if (!sysfs_addrm_finish(&acxt)) {
sysfs_put(sd); sysfs_put(sd);
return -EEXIST;
}
*p_sd = sd; return rc;
return 0;
} }
int sysfs_create_subdir(struct kobject *kobj, const char *name, int sysfs_create_subdir(struct kobject *kobj, const char *name,
......
...@@ -397,6 +397,7 @@ int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr, ...@@ -397,6 +397,7 @@ int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG; umode_t mode = (attr->mode & S_IALLUGO) | S_IFREG;
struct sysfs_addrm_cxt acxt; struct sysfs_addrm_cxt acxt;
struct sysfs_dirent *sd; struct sysfs_dirent *sd;
int rc;
sd = sysfs_new_dirent(attr->name, mode, type); sd = sysfs_new_dirent(attr->name, mode, type);
if (!sd) if (!sd)
...@@ -404,16 +405,13 @@ int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr, ...@@ -404,16 +405,13 @@ int sysfs_add_file(struct sysfs_dirent *dir_sd, const struct attribute *attr,
sd->s_elem.attr.attr = (void *)attr; sd->s_elem.attr.attr = (void *)attr;
sysfs_addrm_start(&acxt, dir_sd); sysfs_addrm_start(&acxt, dir_sd);
rc = sysfs_add_one(&acxt, sd);
sysfs_addrm_finish(&acxt);
if (!sysfs_find_dirent(dir_sd, attr->name)) if (rc)
sysfs_add_one(&acxt, sd);
if (!sysfs_addrm_finish(&acxt)) {
sysfs_put(sd); sysfs_put(sd);
return -EEXIST;
}
return 0; return rc;
} }
......
...@@ -91,14 +91,11 @@ int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char ...@@ -91,14 +91,11 @@ int sysfs_create_link(struct kobject * kobj, struct kobject * target, const char
target_sd = NULL; /* reference is now owned by the symlink */ target_sd = NULL; /* reference is now owned by the symlink */
sysfs_addrm_start(&acxt, parent_sd); sysfs_addrm_start(&acxt, parent_sd);
error = sysfs_add_one(&acxt, sd);
sysfs_addrm_finish(&acxt);
if (!sysfs_find_dirent(parent_sd, name)) if (error)
sysfs_add_one(&acxt, sd);
if (!sysfs_addrm_finish(&acxt)) {
error = -EEXIST;
goto out_put; goto out_put;
}
return 0; return 0;
......
...@@ -62,7 +62,7 @@ extern struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd); ...@@ -62,7 +62,7 @@ extern struct sysfs_dirent *sysfs_get_active_two(struct sysfs_dirent *sd);
extern void sysfs_put_active_two(struct sysfs_dirent *sd); extern void sysfs_put_active_two(struct sysfs_dirent *sd);
extern void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt, extern void sysfs_addrm_start(struct sysfs_addrm_cxt *acxt,
struct sysfs_dirent *parent_sd); struct sysfs_dirent *parent_sd);
extern void sysfs_add_one(struct sysfs_addrm_cxt *acxt, extern int sysfs_add_one(struct sysfs_addrm_cxt *acxt,
struct sysfs_dirent *sd); struct sysfs_dirent *sd);
extern void sysfs_remove_one(struct sysfs_addrm_cxt *acxt, extern void sysfs_remove_one(struct sysfs_addrm_cxt *acxt,
struct sysfs_dirent *sd); struct sysfs_dirent *sd);
......
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