Commit ca1bab38 authored by Eric W. Biederman's avatar Eric W. Biederman Committed by Greg Kroah-Hartman

sysfs: Factor out sysfs_rename from sysfs_rename_dir and sysfs_move_dir

These two functions do 90% of the same work and it doesn't significantly
obfuscate the function to allow both the parent dir and the name to change
at the same time.  So merge them together to simplify maintenance, and
increase testing.
Acked-by: default avatarTejun Heo <tj@kernel.org>
Acked-by: default avatarSerge Hallyn <serue@us.ibm.com>
Signed-off-by: default avatarEric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: default avatarGreg Kroah-Hartman <gregkh@suse.de>
parent 832b6af1
...@@ -760,30 +760,42 @@ void sysfs_remove_dir(struct kobject * kobj) ...@@ -760,30 +760,42 @@ void sysfs_remove_dir(struct kobject * kobj)
__sysfs_remove_dir(sd); __sysfs_remove_dir(sd);
} }
int sysfs_rename_dir(struct kobject * kobj, const char *new_name) int sysfs_rename(struct sysfs_dirent *sd,
struct sysfs_dirent *new_parent_sd, const char *new_name)
{ {
struct sysfs_dirent *sd = kobj->sd;
const char *dup_name = NULL; const char *dup_name = NULL;
int error; int error;
mutex_lock(&sysfs_mutex); mutex_lock(&sysfs_mutex);
error = 0; error = 0;
if (strcmp(sd->s_name, new_name) == 0) if ((sd->s_parent == new_parent_sd) &&
(strcmp(sd->s_name, new_name) == 0))
goto out; /* nothing to rename */ goto out; /* nothing to rename */
error = -EEXIST; error = -EEXIST;
if (sysfs_find_dirent(sd->s_parent, new_name)) if (sysfs_find_dirent(new_parent_sd, new_name))
goto out; goto out;
/* rename sysfs_dirent */ /* rename sysfs_dirent */
error = -ENOMEM; if (strcmp(sd->s_name, new_name) != 0) {
new_name = dup_name = kstrdup(new_name, GFP_KERNEL); error = -ENOMEM;
if (!new_name) new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
goto out; if (!new_name)
goto out;
dup_name = sd->s_name;
sd->s_name = new_name;
}
dup_name = sd->s_name; /* Remove from old parent's list and insert into new parent's list. */
sd->s_name = new_name; if (sd->s_parent != new_parent_sd) {
sysfs_unlink_sibling(sd);
sysfs_get(new_parent_sd);
sysfs_put(sd->s_parent);
sd->s_parent = new_parent_sd;
sysfs_link_sibling(sd);
}
error = 0; error = 0;
out: out:
...@@ -792,37 +804,21 @@ int sysfs_rename_dir(struct kobject * kobj, const char *new_name) ...@@ -792,37 +804,21 @@ int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
return error; return error;
} }
int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
{
return sysfs_rename(kobj->sd, kobj->sd->s_parent, new_name);
}
int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj) int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
{ {
struct sysfs_dirent *sd = kobj->sd; struct sysfs_dirent *sd = kobj->sd;
struct sysfs_dirent *new_parent_sd; struct sysfs_dirent *new_parent_sd;
int error;
BUG_ON(!sd->s_parent); BUG_ON(!sd->s_parent);
new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
mutex_lock(&sysfs_mutex);
new_parent_sd = (new_parent_kobj && new_parent_kobj->sd) ?
new_parent_kobj->sd : &sysfs_root; new_parent_kobj->sd : &sysfs_root;
error = 0; return sysfs_rename(sd, new_parent_sd, sd->s_name);
if (sd->s_parent == new_parent_sd)
goto out; /* nothing to move */
error = -EEXIST;
if (sysfs_find_dirent(new_parent_sd, sd->s_name))
goto out;
/* Remove from old parent's list and insert into new parent's list. */
sysfs_unlink_sibling(sd);
sysfs_get(new_parent_sd);
sysfs_put(sd->s_parent);
sd->s_parent = new_parent_sd;
sysfs_link_sibling(sd);
error = 0;
out:
mutex_unlock(&sysfs_mutex);
return error;
} }
/* Relationship between s_mode and the DT_xxx types */ /* Relationship between s_mode and the DT_xxx types */
......
...@@ -130,6 +130,9 @@ int sysfs_create_subdir(struct kobject *kobj, const char *name, ...@@ -130,6 +130,9 @@ int sysfs_create_subdir(struct kobject *kobj, const char *name,
struct sysfs_dirent **p_sd); struct sysfs_dirent **p_sd);
void sysfs_remove_subdir(struct sysfs_dirent *sd); void sysfs_remove_subdir(struct sysfs_dirent *sd);
int sysfs_rename(struct sysfs_dirent *sd,
struct sysfs_dirent *new_parent_sd, const char *new_name);
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) {
......
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