Commit ca1bab38195d66bdf42320a99cc7359434a271d3

Authored by Eric W. Biederman
Committed by Greg Kroah-Hartman
1 parent 832b6af198

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: Tejun Heo <tj@kernel.org>
Acked-by: Serge Hallyn <serue@us.ibm.com>
Signed-off-by: Eric W. Biederman <ebiederm@aristanetworks.com>
Signed-off-by: Greg Kroah-Hartman <gregkh@suse.de>

Showing 2 changed files with 32 additions and 33 deletions Side-by-side Diff

... ... @@ -760,31 +760,43 @@
760 760 __sysfs_remove_dir(sd);
761 761 }
762 762  
763   -int sysfs_rename_dir(struct kobject * kobj, const char *new_name)
  763 +int sysfs_rename(struct sysfs_dirent *sd,
  764 + struct sysfs_dirent *new_parent_sd, const char *new_name)
764 765 {
765   - struct sysfs_dirent *sd = kobj->sd;
766 766 const char *dup_name = NULL;
767 767 int error;
768 768  
769 769 mutex_lock(&sysfs_mutex);
770 770  
771 771 error = 0;
772   - if (strcmp(sd->s_name, new_name) == 0)
  772 + if ((sd->s_parent == new_parent_sd) &&
  773 + (strcmp(sd->s_name, new_name) == 0))
773 774 goto out; /* nothing to rename */
774 775  
775 776 error = -EEXIST;
776   - if (sysfs_find_dirent(sd->s_parent, new_name))
  777 + if (sysfs_find_dirent(new_parent_sd, new_name))
777 778 goto out;
778 779  
779 780 /* rename sysfs_dirent */
780   - error = -ENOMEM;
781   - new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
782   - if (!new_name)
783   - goto out;
  781 + if (strcmp(sd->s_name, new_name) != 0) {
  782 + error = -ENOMEM;
  783 + new_name = dup_name = kstrdup(new_name, GFP_KERNEL);
  784 + if (!new_name)
  785 + goto out;
784 786  
785   - dup_name = sd->s_name;
786   - sd->s_name = new_name;
  787 + dup_name = sd->s_name;
  788 + sd->s_name = new_name;
  789 + }
787 790  
  791 + /* Remove from old parent's list and insert into new parent's list. */
  792 + if (sd->s_parent != new_parent_sd) {
  793 + sysfs_unlink_sibling(sd);
  794 + sysfs_get(new_parent_sd);
  795 + sysfs_put(sd->s_parent);
  796 + sd->s_parent = new_parent_sd;
  797 + sysfs_link_sibling(sd);
  798 + }
  799 +
788 800 error = 0;
789 801 out:
790 802 mutex_unlock(&sysfs_mutex);
791 803  
792 804  
793 805  
... ... @@ -792,37 +804,21 @@
792 804 return error;
793 805 }
794 806  
  807 +int sysfs_rename_dir(struct kobject *kobj, const char *new_name)
  808 +{
  809 + return sysfs_rename(kobj->sd, kobj->sd->s_parent, new_name);
  810 +}
  811 +
795 812 int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj)
796 813 {
797 814 struct sysfs_dirent *sd = kobj->sd;
798 815 struct sysfs_dirent *new_parent_sd;
799   - int error;
800 816  
801 817 BUG_ON(!sd->s_parent);
802   -
803   - mutex_lock(&sysfs_mutex);
804   - new_parent_sd = (new_parent_kobj && new_parent_kobj->sd) ?
  818 + new_parent_sd = new_parent_kobj && new_parent_kobj->sd ?
805 819 new_parent_kobj->sd : &sysfs_root;
806 820  
807   - error = 0;
808   - if (sd->s_parent == new_parent_sd)
809   - goto out; /* nothing to move */
810   -
811   - error = -EEXIST;
812   - if (sysfs_find_dirent(new_parent_sd, sd->s_name))
813   - goto out;
814   -
815   - /* Remove from old parent's list and insert into new parent's list. */
816   - sysfs_unlink_sibling(sd);
817   - sysfs_get(new_parent_sd);
818   - sysfs_put(sd->s_parent);
819   - sd->s_parent = new_parent_sd;
820   - sysfs_link_sibling(sd);
821   -
822   - error = 0;
823   -out:
824   - mutex_unlock(&sysfs_mutex);
825   - return error;
  821 + return sysfs_rename(sd, new_parent_sd, sd->s_name);
826 822 }
827 823  
828 824 /* Relationship between s_mode and the DT_xxx types */
... ... @@ -130,6 +130,9 @@
130 130 struct sysfs_dirent **p_sd);
131 131 void sysfs_remove_subdir(struct sysfs_dirent *sd);
132 132  
  133 +int sysfs_rename(struct sysfs_dirent *sd,
  134 + struct sysfs_dirent *new_parent_sd, const char *new_name);
  135 +
133 136 static inline struct sysfs_dirent *__sysfs_get(struct sysfs_dirent *sd)
134 137 {
135 138 if (sd) {