diff options
Diffstat (limited to 'fs/sysfs/dir.c')
-rw-r--r-- | fs/sysfs/dir.c | 231 |
1 files changed, 126 insertions, 105 deletions
diff --git a/fs/sysfs/dir.c b/fs/sysfs/dir.c index 987211296106..aee966c44aac 100644 --- a/fs/sysfs/dir.c +++ b/fs/sysfs/dir.c | |||
@@ -568,10 +568,10 @@ static void sysfs_drop_dentry(struct sysfs_dirent *sd) | |||
568 | spin_unlock(&dcache_lock); | 568 | spin_unlock(&dcache_lock); |
569 | spin_unlock(&sysfs_assoc_lock); | 569 | spin_unlock(&sysfs_assoc_lock); |
570 | 570 | ||
571 | dput(dentry); | 571 | /* dentries for shadowed inodes are pinned, unpin */ |
572 | /* XXX: unpin if directory, this will go away soon */ | 572 | if (dentry && sysfs_is_shadowed_inode(dentry->d_inode)) |
573 | if (sysfs_type(sd) == SYSFS_DIR) | ||
574 | dput(dentry); | 573 | dput(dentry); |
574 | dput(dentry); | ||
575 | 575 | ||
576 | /* adjust nlink and update timestamp */ | 576 | /* adjust nlink and update timestamp */ |
577 | inode = ilookup(sysfs_sb, sd->s_ino); | 577 | inode = ilookup(sysfs_sb, sd->s_ino); |
@@ -686,69 +686,29 @@ struct sysfs_dirent *sysfs_get_dirent(struct sysfs_dirent *parent_sd, | |||
686 | static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, | 686 | static int create_dir(struct kobject *kobj, struct sysfs_dirent *parent_sd, |
687 | const char *name, struct sysfs_dirent **p_sd) | 687 | const char *name, struct sysfs_dirent **p_sd) |
688 | { | 688 | { |
689 | struct dentry *parent = parent_sd->s_dentry; | ||
690 | struct sysfs_addrm_cxt acxt; | ||
691 | int error; | ||
692 | umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; | 689 | umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO; |
693 | struct dentry *dentry; | 690 | struct sysfs_addrm_cxt acxt; |
694 | struct inode *inode; | ||
695 | struct sysfs_dirent *sd; | 691 | struct sysfs_dirent *sd; |
696 | 692 | ||
697 | sysfs_addrm_start(&acxt, parent_sd); | ||
698 | |||
699 | /* allocate */ | 693 | /* allocate */ |
700 | dentry = lookup_one_len(name, parent, strlen(name)); | ||
701 | if (IS_ERR(dentry)) { | ||
702 | error = PTR_ERR(dentry); | ||
703 | goto out_finish; | ||
704 | } | ||
705 | |||
706 | error = -EEXIST; | ||
707 | if (dentry->d_inode) | ||
708 | goto out_dput; | ||
709 | |||
710 | error = -ENOMEM; | ||
711 | sd = sysfs_new_dirent(name, mode, SYSFS_DIR); | 694 | sd = sysfs_new_dirent(name, mode, SYSFS_DIR); |
712 | if (!sd) | 695 | if (!sd) |
713 | goto out_drop; | 696 | return -ENOMEM; |
714 | sd->s_elem.dir.kobj = kobj; | 697 | sd->s_elem.dir.kobj = kobj; |
715 | 698 | ||
716 | inode = sysfs_get_inode(sd); | ||
717 | if (!inode) | ||
718 | goto out_sput; | ||
719 | |||
720 | if (inode->i_state & I_NEW) { | ||
721 | inode->i_op = &sysfs_dir_inode_operations; | ||
722 | inode->i_fop = &sysfs_dir_operations; | ||
723 | /* directory inodes start off with i_nlink == 2 (for ".") */ | ||
724 | inc_nlink(inode); | ||
725 | } | ||
726 | |||
727 | /* link in */ | 699 | /* link in */ |
728 | error = -EEXIST; | 700 | sysfs_addrm_start(&acxt, parent_sd); |
729 | if (sysfs_find_dirent(parent_sd, name)) | 701 | if (!sysfs_find_dirent(parent_sd, name)) { |
730 | goto out_iput; | 702 | sysfs_add_one(&acxt, sd); |
731 | 703 | sysfs_link_sibling(sd); | |
732 | sysfs_add_one(&acxt, sd); | 704 | } |
733 | sysfs_link_sibling(sd); | 705 | if (sysfs_addrm_finish(&acxt)) { |
734 | sysfs_instantiate(dentry, inode); | 706 | *p_sd = sd; |
735 | sysfs_attach_dentry(sd, dentry); | 707 | return 0; |
736 | 708 | } | |
737 | *p_sd = sd; | ||
738 | error = 0; | ||
739 | goto out_finish; /* pin directory dentry in core */ | ||
740 | 709 | ||
741 | out_iput: | ||
742 | iput(inode); | ||
743 | out_sput: | ||
744 | sysfs_put(sd); | 710 | sysfs_put(sd); |
745 | out_drop: | 711 | return -EEXIST; |
746 | d_drop(dentry); | ||
747 | out_dput: | ||
748 | dput(dentry); | ||
749 | out_finish: | ||
750 | sysfs_addrm_finish(&acxt); | ||
751 | return error; | ||
752 | } | 712 | } |
753 | 713 | ||
754 | int sysfs_create_subdir(struct kobject *kobj, const char *name, | 714 | int sysfs_create_subdir(struct kobject *kobj, const char *name, |
@@ -785,6 +745,17 @@ int sysfs_create_dir(struct kobject *kobj, | |||
785 | return error; | 745 | return error; |
786 | } | 746 | } |
787 | 747 | ||
748 | static int sysfs_count_nlink(struct sysfs_dirent *sd) | ||
749 | { | ||
750 | struct sysfs_dirent *child; | ||
751 | int nr = 0; | ||
752 | |||
753 | for (child = sd->s_children; child; child = child->s_sibling) | ||
754 | if (sysfs_type(child) == SYSFS_DIR) | ||
755 | nr++; | ||
756 | return nr + 2; | ||
757 | } | ||
758 | |||
788 | static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, | 759 | static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, |
789 | struct nameidata *nd) | 760 | struct nameidata *nd) |
790 | { | 761 | { |
@@ -795,7 +766,7 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, | |||
795 | int found = 0; | 766 | int found = 0; |
796 | 767 | ||
797 | for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) { | 768 | for (sd = parent_sd->s_children; sd; sd = sd->s_sibling) { |
798 | if ((sysfs_type(sd) & SYSFS_NOT_PINNED) && | 769 | if (sysfs_type(sd) && |
799 | !strcmp(sd->s_name, dentry->d_name.name)) { | 770 | !strcmp(sd->s_name, dentry->d_name.name)) { |
800 | found = 1; | 771 | found = 1; |
801 | break; | 772 | break; |
@@ -816,6 +787,11 @@ static struct dentry * sysfs_lookup(struct inode *dir, struct dentry *dentry, | |||
816 | if (inode->i_state & I_NEW) { | 787 | if (inode->i_state & I_NEW) { |
817 | /* initialize inode according to type */ | 788 | /* initialize inode according to type */ |
818 | switch (sysfs_type(sd)) { | 789 | switch (sysfs_type(sd)) { |
790 | case SYSFS_DIR: | ||
791 | inode->i_op = &sysfs_dir_inode_operations; | ||
792 | inode->i_fop = &sysfs_dir_operations; | ||
793 | inode->i_nlink = sysfs_count_nlink(sd); | ||
794 | break; | ||
819 | case SYSFS_KOBJ_ATTR: | 795 | case SYSFS_KOBJ_ATTR: |
820 | inode->i_size = PAGE_SIZE; | 796 | inode->i_size = PAGE_SIZE; |
821 | inode->i_fop = &sysfs_file_operations; | 797 | inode->i_fop = &sysfs_file_operations; |
@@ -876,7 +852,7 @@ static void __sysfs_remove_dir(struct sysfs_dirent *dir_sd) | |||
876 | while (*pos) { | 852 | while (*pos) { |
877 | struct sysfs_dirent *sd = *pos; | 853 | struct sysfs_dirent *sd = *pos; |
878 | 854 | ||
879 | if (sysfs_type(sd) && (sysfs_type(sd) & SYSFS_NOT_PINNED)) { | 855 | if (sysfs_type(sd) && sysfs_type(sd) != SYSFS_DIR) { |
880 | *pos = sd->s_sibling; | 856 | *pos = sd->s_sibling; |
881 | sd->s_sibling = NULL; | 857 | sd->s_sibling = NULL; |
882 | sysfs_remove_one(&acxt, sd); | 858 | sysfs_remove_one(&acxt, sd); |
@@ -912,14 +888,25 @@ int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd, | |||
912 | const char *new_name) | 888 | const char *new_name) |
913 | { | 889 | { |
914 | struct sysfs_dirent *sd = kobj->sd; | 890 | struct sysfs_dirent *sd = kobj->sd; |
915 | struct dentry *new_parent = new_parent_sd->s_dentry; | 891 | struct dentry *new_parent = NULL; |
916 | struct dentry *new_dentry; | 892 | struct dentry *old_dentry = NULL, *new_dentry = NULL; |
917 | char *dup_name; | 893 | const char *dup_name = NULL; |
918 | int error; | 894 | int error; |
919 | 895 | ||
920 | if (!new_parent_sd) | 896 | /* get dentries */ |
921 | return -EFAULT; | 897 | old_dentry = sysfs_get_dentry(sd); |
898 | if (IS_ERR(old_dentry)) { | ||
899 | error = PTR_ERR(old_dentry); | ||
900 | goto out_dput; | ||
901 | } | ||
902 | |||
903 | new_parent = sysfs_get_dentry(new_parent_sd); | ||
904 | if (IS_ERR(new_parent)) { | ||
905 | error = PTR_ERR(new_parent); | ||
906 | goto out_dput; | ||
907 | } | ||
922 | 908 | ||
909 | /* lock new_parent and get dentry for new name */ | ||
923 | mutex_lock(&new_parent->d_inode->i_mutex); | 910 | mutex_lock(&new_parent->d_inode->i_mutex); |
924 | 911 | ||
925 | new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name)); | 912 | new_dentry = lookup_one_len(new_name, new_parent, strlen(new_name)); |
@@ -933,14 +920,14 @@ int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd, | |||
933 | * shadows of the same directory | 920 | * shadows of the same directory |
934 | */ | 921 | */ |
935 | error = -EINVAL; | 922 | error = -EINVAL; |
936 | if (sd->s_parent->s_dentry->d_inode != new_parent->d_inode || | 923 | if (old_dentry->d_parent->d_inode != new_parent->d_inode || |
937 | new_dentry->d_parent->d_inode != new_parent->d_inode || | 924 | new_dentry->d_parent->d_inode != new_parent->d_inode || |
938 | new_dentry == sd->s_dentry) | 925 | old_dentry == new_dentry) |
939 | goto out_dput; | 926 | goto out_unlock; |
940 | 927 | ||
941 | error = -EEXIST; | 928 | error = -EEXIST; |
942 | if (new_dentry->d_inode) | 929 | if (new_dentry->d_inode) |
943 | goto out_dput; | 930 | goto out_unlock; |
944 | 931 | ||
945 | /* rename kobject and sysfs_dirent */ | 932 | /* rename kobject and sysfs_dirent */ |
946 | error = -ENOMEM; | 933 | error = -ENOMEM; |
@@ -950,9 +937,9 @@ int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd, | |||
950 | 937 | ||
951 | error = kobject_set_name(kobj, "%s", new_name); | 938 | error = kobject_set_name(kobj, "%s", new_name); |
952 | if (error) | 939 | if (error) |
953 | goto out_free; | 940 | goto out_drop; |
954 | 941 | ||
955 | kfree(sd->s_name); | 942 | dup_name = sd->s_name; |
956 | sd->s_name = new_name; | 943 | sd->s_name = new_name; |
957 | 944 | ||
958 | /* move under the new parent */ | 945 | /* move under the new parent */ |
@@ -972,45 +959,58 @@ int sysfs_rename_dir(struct kobject *kobj, struct sysfs_dirent *new_parent_sd, | |||
972 | error = 0; | 959 | error = 0; |
973 | goto out_unlock; | 960 | goto out_unlock; |
974 | 961 | ||
975 | out_free: | ||
976 | kfree(dup_name); | ||
977 | out_drop: | 962 | out_drop: |
978 | d_drop(new_dentry); | 963 | d_drop(new_dentry); |
979 | out_dput: | ||
980 | dput(new_dentry); | ||
981 | out_unlock: | 964 | out_unlock: |
982 | mutex_unlock(&new_parent->d_inode->i_mutex); | 965 | mutex_unlock(&new_parent->d_inode->i_mutex); |
966 | out_dput: | ||
967 | kfree(dup_name); | ||
968 | dput(new_parent); | ||
969 | dput(old_dentry); | ||
970 | dput(new_dentry); | ||
983 | return error; | 971 | return error; |
984 | } | 972 | } |
985 | 973 | ||
986 | int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent) | 974 | int sysfs_move_dir(struct kobject *kobj, struct kobject *new_parent_kobj) |
987 | { | 975 | { |
988 | struct dentry *old_parent_dentry, *new_parent_dentry, *new_dentry; | 976 | struct sysfs_dirent *sd = kobj->sd; |
989 | struct sysfs_dirent *new_parent_sd, *sd; | 977 | struct sysfs_dirent *new_parent_sd; |
978 | struct dentry *old_parent, *new_parent = NULL; | ||
979 | struct dentry *old_dentry = NULL, *new_dentry = NULL; | ||
990 | int error; | 980 | int error; |
991 | 981 | ||
992 | old_parent_dentry = kobj->parent ? | 982 | BUG_ON(!sd->s_parent); |
993 | kobj->parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root; | 983 | new_parent_sd = new_parent_kobj->sd ? new_parent_kobj->sd : &sysfs_root; |
994 | new_parent_dentry = new_parent ? | 984 | |
995 | new_parent->sd->s_dentry : sysfs_mount->mnt_sb->s_root; | 985 | /* get dentries */ |
986 | old_dentry = sysfs_get_dentry(sd); | ||
987 | if (IS_ERR(old_dentry)) { | ||
988 | error = PTR_ERR(old_dentry); | ||
989 | goto out_dput; | ||
990 | } | ||
991 | old_parent = sd->s_parent->s_dentry; | ||
992 | |||
993 | new_parent = sysfs_get_dentry(new_parent_sd); | ||
994 | if (IS_ERR(new_parent)) { | ||
995 | error = PTR_ERR(new_parent); | ||
996 | goto out_dput; | ||
997 | } | ||
996 | 998 | ||
997 | if (old_parent_dentry->d_inode == new_parent_dentry->d_inode) | 999 | if (old_parent->d_inode == new_parent->d_inode) { |
998 | return 0; /* nothing to move */ | 1000 | error = 0; |
1001 | goto out_dput; /* nothing to move */ | ||
1002 | } | ||
999 | again: | 1003 | again: |
1000 | mutex_lock(&old_parent_dentry->d_inode->i_mutex); | 1004 | mutex_lock(&old_parent->d_inode->i_mutex); |
1001 | if (!mutex_trylock(&new_parent_dentry->d_inode->i_mutex)) { | 1005 | if (!mutex_trylock(&new_parent->d_inode->i_mutex)) { |
1002 | mutex_unlock(&old_parent_dentry->d_inode->i_mutex); | 1006 | mutex_unlock(&old_parent->d_inode->i_mutex); |
1003 | goto again; | 1007 | goto again; |
1004 | } | 1008 | } |
1005 | 1009 | ||
1006 | new_parent_sd = new_parent_dentry->d_fsdata; | 1010 | new_dentry = lookup_one_len(kobj->name, new_parent, strlen(kobj->name)); |
1007 | sd = kobj->sd; | ||
1008 | |||
1009 | new_dentry = lookup_one_len(kobj->name, new_parent_dentry, | ||
1010 | strlen(kobj->name)); | ||
1011 | if (IS_ERR(new_dentry)) { | 1011 | if (IS_ERR(new_dentry)) { |
1012 | error = PTR_ERR(new_dentry); | 1012 | error = PTR_ERR(new_dentry); |
1013 | goto out; | 1013 | goto out_unlock; |
1014 | } else | 1014 | } else |
1015 | error = 0; | 1015 | error = 0; |
1016 | d_add(new_dentry, NULL); | 1016 | d_add(new_dentry, NULL); |
@@ -1027,10 +1027,14 @@ again: | |||
1027 | sysfs_link_sibling(sd); | 1027 | sysfs_link_sibling(sd); |
1028 | 1028 | ||
1029 | mutex_unlock(&sysfs_mutex); | 1029 | mutex_unlock(&sysfs_mutex); |
1030 | out: | ||
1031 | mutex_unlock(&new_parent_dentry->d_inode->i_mutex); | ||
1032 | mutex_unlock(&old_parent_dentry->d_inode->i_mutex); | ||
1033 | 1030 | ||
1031 | out_unlock: | ||
1032 | mutex_unlock(&new_parent->d_inode->i_mutex); | ||
1033 | mutex_unlock(&old_parent->d_inode->i_mutex); | ||
1034 | out_dput: | ||
1035 | dput(new_parent); | ||
1036 | dput(old_dentry); | ||
1037 | dput(new_dentry); | ||
1034 | return error; | 1038 | return error; |
1035 | } | 1039 | } |
1036 | 1040 | ||
@@ -1191,12 +1195,20 @@ static loff_t sysfs_dir_lseek(struct file * file, loff_t offset, int origin) | |||
1191 | int sysfs_make_shadowed_dir(struct kobject *kobj, | 1195 | int sysfs_make_shadowed_dir(struct kobject *kobj, |
1192 | void * (*follow_link)(struct dentry *, struct nameidata *)) | 1196 | void * (*follow_link)(struct dentry *, struct nameidata *)) |
1193 | { | 1197 | { |
1198 | struct dentry *dentry; | ||
1194 | struct inode *inode; | 1199 | struct inode *inode; |
1195 | struct inode_operations *i_op; | 1200 | struct inode_operations *i_op; |
1196 | 1201 | ||
1197 | inode = kobj->sd->s_dentry->d_inode; | 1202 | /* get dentry for @kobj->sd, dentry of a shadowed dir is pinned */ |
1198 | if (inode->i_op != &sysfs_dir_inode_operations) | 1203 | dentry = sysfs_get_dentry(kobj->sd); |
1204 | if (IS_ERR(dentry)) | ||
1205 | return PTR_ERR(dentry); | ||
1206 | |||
1207 | inode = dentry->d_inode; | ||
1208 | if (inode->i_op != &sysfs_dir_inode_operations) { | ||
1209 | dput(dentry); | ||
1199 | return -EINVAL; | 1210 | return -EINVAL; |
1211 | } | ||
1200 | 1212 | ||
1201 | i_op = kmalloc(sizeof(*i_op), GFP_KERNEL); | 1213 | i_op = kmalloc(sizeof(*i_op), GFP_KERNEL); |
1202 | if (!i_op) | 1214 | if (!i_op) |
@@ -1223,17 +1235,23 @@ int sysfs_make_shadowed_dir(struct kobject *kobj, | |||
1223 | 1235 | ||
1224 | struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj) | 1236 | struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj) |
1225 | { | 1237 | { |
1226 | struct dentry *dir = kobj->sd->s_dentry; | 1238 | struct sysfs_dirent *parent_sd = kobj->sd->s_parent; |
1227 | struct inode *inode = dir->d_inode; | 1239 | struct dentry *dir, *parent, *shadow; |
1228 | struct dentry *parent = dir->d_parent; | 1240 | struct inode *inode; |
1229 | struct sysfs_dirent *parent_sd = parent->d_fsdata; | ||
1230 | struct dentry *shadow; | ||
1231 | struct sysfs_dirent *sd; | 1241 | struct sysfs_dirent *sd; |
1232 | struct sysfs_addrm_cxt acxt; | 1242 | struct sysfs_addrm_cxt acxt; |
1233 | 1243 | ||
1244 | dir = sysfs_get_dentry(kobj->sd); | ||
1245 | if (IS_ERR(dir)) { | ||
1246 | sd = (void *)dir; | ||
1247 | goto out; | ||
1248 | } | ||
1249 | parent = dir->d_parent; | ||
1250 | |||
1251 | inode = dir->d_inode; | ||
1234 | sd = ERR_PTR(-EINVAL); | 1252 | sd = ERR_PTR(-EINVAL); |
1235 | if (!sysfs_is_shadowed_inode(inode)) | 1253 | if (!sysfs_is_shadowed_inode(inode)) |
1236 | goto out; | 1254 | goto out_dput; |
1237 | 1255 | ||
1238 | shadow = d_alloc(parent, &dir->d_name); | 1256 | shadow = d_alloc(parent, &dir->d_name); |
1239 | if (!shadow) | 1257 | if (!shadow) |
@@ -1258,12 +1276,15 @@ struct sysfs_dirent *sysfs_create_shadow_dir(struct kobject *kobj) | |||
1258 | 1276 | ||
1259 | dget(shadow); /* Extra count - pin the dentry in core */ | 1277 | dget(shadow); /* Extra count - pin the dentry in core */ |
1260 | 1278 | ||
1261 | out: | 1279 | goto out_dput; |
1262 | return sd; | 1280 | |
1263 | nomem: | 1281 | nomem: |
1264 | dput(shadow); | 1282 | dput(shadow); |
1265 | sd = ERR_PTR(-ENOMEM); | 1283 | sd = ERR_PTR(-ENOMEM); |
1266 | goto out; | 1284 | out_dput: |
1285 | dput(dir); | ||
1286 | out: | ||
1287 | return sd; | ||
1267 | } | 1288 | } |
1268 | 1289 | ||
1269 | /** | 1290 | /** |