1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27#undef DEBUG
28
29#include <linux/fs.h>
30#include <linux/mount.h>
31#include <linux/module.h>
32#include <linux/slab.h>
33#include <linux/err.h>
34
35#include <linux/configfs.h>
36#include "configfs_internal.h"
37
38DECLARE_RWSEM(configfs_rename_sem);
39
40
41
42
43
44
45
46
47
48
49
50
51DEFINE_SPINLOCK(configfs_dirent_lock);
52
53static void configfs_d_iput(struct dentry * dentry,
54 struct inode * inode)
55{
56 struct configfs_dirent *sd = dentry->d_fsdata;
57
58 if (sd) {
59
60 spin_lock(&configfs_dirent_lock);
61
62
63
64
65
66
67
68
69 if (atomic_read(&sd->s_count) <= 2)
70 sd->s_dentry = NULL;
71
72 spin_unlock(&configfs_dirent_lock);
73 configfs_put(sd);
74 }
75 iput(inode);
76}
77
78const struct dentry_operations configfs_dentry_ops = {
79 .d_iput = configfs_d_iput,
80 .d_delete = always_delete_dentry,
81};
82
83#ifdef CONFIG_LOCKDEP
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104static void configfs_init_dirent_depth(struct configfs_dirent *sd)
105{
106 sd->s_depth = -1;
107}
108
109static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
110 struct configfs_dirent *sd)
111{
112 int parent_depth = parent_sd->s_depth;
113
114 if (parent_depth >= 0)
115 sd->s_depth = parent_depth + 1;
116}
117
118static void
119configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
120{
121
122
123
124
125
126
127
128
129
130
131 if (sd->s_depth == -1)
132
133
134
135
136 sd->s_depth = 0;
137}
138
139static void
140configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
141{
142
143 sd->s_depth = -1;
144}
145
146#else
147
148static void configfs_init_dirent_depth(struct configfs_dirent *sd)
149{
150}
151
152static void configfs_set_dir_dirent_depth(struct configfs_dirent *parent_sd,
153 struct configfs_dirent *sd)
154{
155}
156
157static void
158configfs_adjust_dir_dirent_depth_before_populate(struct configfs_dirent *sd)
159{
160}
161
162static void
163configfs_adjust_dir_dirent_depth_after_populate(struct configfs_dirent *sd)
164{
165}
166
167#endif
168
169
170
171
172static struct configfs_dirent *configfs_new_dirent(struct configfs_dirent *parent_sd,
173 void *element, int type)
174{
175 struct configfs_dirent * sd;
176
177 sd = kmem_cache_zalloc(configfs_dir_cachep, GFP_KERNEL);
178 if (!sd)
179 return ERR_PTR(-ENOMEM);
180
181 atomic_set(&sd->s_count, 1);
182 INIT_LIST_HEAD(&sd->s_links);
183 INIT_LIST_HEAD(&sd->s_children);
184 sd->s_element = element;
185 sd->s_type = type;
186 configfs_init_dirent_depth(sd);
187 spin_lock(&configfs_dirent_lock);
188 if (parent_sd->s_type & CONFIGFS_USET_DROPPING) {
189 spin_unlock(&configfs_dirent_lock);
190 kmem_cache_free(configfs_dir_cachep, sd);
191 return ERR_PTR(-ENOENT);
192 }
193 list_add(&sd->s_sibling, &parent_sd->s_children);
194 spin_unlock(&configfs_dirent_lock);
195
196 return sd;
197}
198
199
200
201
202
203
204
205
206static int configfs_dirent_exists(struct configfs_dirent *parent_sd,
207 const unsigned char *new)
208{
209 struct configfs_dirent * sd;
210
211 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
212 if (sd->s_element) {
213 const unsigned char *existing = configfs_get_name(sd);
214 if (strcmp(existing, new))
215 continue;
216 else
217 return -EEXIST;
218 }
219 }
220
221 return 0;
222}
223
224
225int configfs_make_dirent(struct configfs_dirent * parent_sd,
226 struct dentry * dentry, void * element,
227 umode_t mode, int type)
228{
229 struct configfs_dirent * sd;
230
231 sd = configfs_new_dirent(parent_sd, element, type);
232 if (IS_ERR(sd))
233 return PTR_ERR(sd);
234
235 sd->s_mode = mode;
236 sd->s_dentry = dentry;
237 if (dentry)
238 dentry->d_fsdata = configfs_get(sd);
239
240 return 0;
241}
242
243static int init_dir(struct inode * inode)
244{
245 inode->i_op = &configfs_dir_inode_operations;
246 inode->i_fop = &configfs_dir_operations;
247
248
249 inc_nlink(inode);
250 return 0;
251}
252
253static int configfs_init_file(struct inode * inode)
254{
255 inode->i_size = PAGE_SIZE;
256 inode->i_fop = &configfs_file_operations;
257 return 0;
258}
259
260static int init_symlink(struct inode * inode)
261{
262 inode->i_op = &configfs_symlink_inode_operations;
263 return 0;
264}
265
266static int create_dir(struct config_item *k, struct dentry *d)
267{
268 int error;
269 umode_t mode = S_IFDIR| S_IRWXU | S_IRUGO | S_IXUGO;
270 struct dentry *p = d->d_parent;
271
272 BUG_ON(!k);
273
274 error = configfs_dirent_exists(p->d_fsdata, d->d_name.name);
275 if (!error)
276 error = configfs_make_dirent(p->d_fsdata, d, k, mode,
277 CONFIGFS_DIR | CONFIGFS_USET_CREATING);
278 if (!error) {
279 configfs_set_dir_dirent_depth(p->d_fsdata, d->d_fsdata);
280 error = configfs_create(d, mode, init_dir);
281 if (!error) {
282 inc_nlink(p->d_inode);
283 } else {
284 struct configfs_dirent *sd = d->d_fsdata;
285 if (sd) {
286 spin_lock(&configfs_dirent_lock);
287 list_del_init(&sd->s_sibling);
288 spin_unlock(&configfs_dirent_lock);
289 configfs_put(sd);
290 }
291 }
292 }
293 return error;
294}
295
296
297
298
299
300
301
302
303
304
305
306static int configfs_create_dir(struct config_item * item, struct dentry *dentry)
307{
308 int error = create_dir(item, dentry);
309 if (!error)
310 item->ci_dentry = dentry;
311 return error;
312}
313
314
315
316
317
318
319
320
321static void configfs_dir_set_ready(struct configfs_dirent *sd)
322{
323 struct configfs_dirent *child_sd;
324
325 sd->s_type &= ~CONFIGFS_USET_CREATING;
326 list_for_each_entry(child_sd, &sd->s_children, s_sibling)
327 if (child_sd->s_type & CONFIGFS_USET_CREATING)
328 configfs_dir_set_ready(child_sd);
329}
330
331
332
333
334
335
336
337
338
339
340
341int configfs_dirent_is_ready(struct configfs_dirent *sd)
342{
343 int ret;
344
345 spin_lock(&configfs_dirent_lock);
346 ret = !(sd->s_type & CONFIGFS_USET_CREATING);
347 spin_unlock(&configfs_dirent_lock);
348
349 return ret;
350}
351
352int configfs_create_link(struct configfs_symlink *sl,
353 struct dentry *parent,
354 struct dentry *dentry)
355{
356 int err = 0;
357 umode_t mode = S_IFLNK | S_IRWXUGO;
358
359 err = configfs_make_dirent(parent->d_fsdata, dentry, sl, mode,
360 CONFIGFS_ITEM_LINK);
361 if (!err) {
362 err = configfs_create(dentry, mode, init_symlink);
363 if (err) {
364 struct configfs_dirent *sd = dentry->d_fsdata;
365 if (sd) {
366 spin_lock(&configfs_dirent_lock);
367 list_del_init(&sd->s_sibling);
368 spin_unlock(&configfs_dirent_lock);
369 configfs_put(sd);
370 }
371 }
372 }
373 return err;
374}
375
376static void remove_dir(struct dentry * d)
377{
378 struct dentry * parent = dget(d->d_parent);
379 struct configfs_dirent * sd;
380
381 sd = d->d_fsdata;
382 spin_lock(&configfs_dirent_lock);
383 list_del_init(&sd->s_sibling);
384 spin_unlock(&configfs_dirent_lock);
385 configfs_put(sd);
386 if (d->d_inode)
387 simple_rmdir(parent->d_inode,d);
388
389 pr_debug(" o %s removing done (%d)\n",d->d_name.name, d_count(d));
390
391 dput(parent);
392}
393
394
395
396
397
398
399
400
401
402
403
404
405static void configfs_remove_dir(struct config_item * item)
406{
407 struct dentry * dentry = dget(item->ci_dentry);
408
409 if (!dentry)
410 return;
411
412 remove_dir(dentry);
413
414
415
416 dput(dentry);
417}
418
419
420
421
422
423static int configfs_attach_attr(struct configfs_dirent * sd, struct dentry * dentry)
424{
425 struct configfs_attribute * attr = sd->s_element;
426 int error;
427
428 spin_lock(&configfs_dirent_lock);
429 dentry->d_fsdata = configfs_get(sd);
430 sd->s_dentry = dentry;
431 spin_unlock(&configfs_dirent_lock);
432
433 error = configfs_create(dentry, (attr->ca_mode & S_IALLUGO) | S_IFREG,
434 configfs_init_file);
435 if (error) {
436 configfs_put(sd);
437 return error;
438 }
439
440 d_rehash(dentry);
441
442 return 0;
443}
444
445static struct dentry * configfs_lookup(struct inode *dir,
446 struct dentry *dentry,
447 unsigned int flags)
448{
449 struct configfs_dirent * parent_sd = dentry->d_parent->d_fsdata;
450 struct configfs_dirent * sd;
451 int found = 0;
452 int err;
453
454
455
456
457
458
459
460
461
462 err = -ENOENT;
463 if (!configfs_dirent_is_ready(parent_sd))
464 goto out;
465
466 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
467 if (sd->s_type & CONFIGFS_NOT_PINNED) {
468 const unsigned char * name = configfs_get_name(sd);
469
470 if (strcmp(name, dentry->d_name.name))
471 continue;
472
473 found = 1;
474 err = configfs_attach_attr(sd, dentry);
475 break;
476 }
477 }
478
479 if (!found) {
480
481
482
483
484 if (dentry->d_name.len > NAME_MAX)
485 return ERR_PTR(-ENAMETOOLONG);
486 d_add(dentry, NULL);
487 return NULL;
488 }
489
490out:
491 return ERR_PTR(err);
492}
493
494
495
496
497
498
499
500
501
502static int configfs_detach_prep(struct dentry *dentry, struct mutex **wait_mutex)
503{
504 struct configfs_dirent *parent_sd = dentry->d_fsdata;
505 struct configfs_dirent *sd;
506 int ret;
507
508
509 parent_sd->s_type |= CONFIGFS_USET_DROPPING;
510
511 ret = -EBUSY;
512 if (!list_empty(&parent_sd->s_links))
513 goto out;
514
515 ret = 0;
516 list_for_each_entry(sd, &parent_sd->s_children, s_sibling) {
517 if (!sd->s_element ||
518 (sd->s_type & CONFIGFS_NOT_PINNED))
519 continue;
520 if (sd->s_type & CONFIGFS_USET_DEFAULT) {
521
522 if (sd->s_type & CONFIGFS_USET_IN_MKDIR) {
523 if (wait_mutex)
524 *wait_mutex = &sd->s_dentry->d_inode->i_mutex;
525 return -EAGAIN;
526 }
527
528
529
530
531
532 ret = configfs_detach_prep(sd->s_dentry, wait_mutex);
533 if (!ret)
534 continue;
535 } else
536 ret = -ENOTEMPTY;
537
538 break;
539 }
540
541out:
542 return ret;
543}
544
545
546
547
548
549static void configfs_detach_rollback(struct dentry *dentry)
550{
551 struct configfs_dirent *parent_sd = dentry->d_fsdata;
552 struct configfs_dirent *sd;
553
554 parent_sd->s_type &= ~CONFIGFS_USET_DROPPING;
555
556 list_for_each_entry(sd, &parent_sd->s_children, s_sibling)
557 if (sd->s_type & CONFIGFS_USET_DEFAULT)
558 configfs_detach_rollback(sd->s_dentry);
559}
560
561static void detach_attrs(struct config_item * item)
562{
563 struct dentry * dentry = dget(item->ci_dentry);
564 struct configfs_dirent * parent_sd;
565 struct configfs_dirent * sd, * tmp;
566
567 if (!dentry)
568 return;
569
570 pr_debug("configfs %s: dropping attrs for dir\n",
571 dentry->d_name.name);
572
573 parent_sd = dentry->d_fsdata;
574 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
575 if (!sd->s_element || !(sd->s_type & CONFIGFS_NOT_PINNED))
576 continue;
577 spin_lock(&configfs_dirent_lock);
578 list_del_init(&sd->s_sibling);
579 spin_unlock(&configfs_dirent_lock);
580 configfs_drop_dentry(sd, dentry);
581 configfs_put(sd);
582 }
583
584
585
586
587 dput(dentry);
588}
589
590static int populate_attrs(struct config_item *item)
591{
592 struct config_item_type *t = item->ci_type;
593 struct configfs_attribute *attr;
594 int error = 0;
595 int i;
596
597 if (!t)
598 return -EINVAL;
599 if (t->ct_attrs) {
600 for (i = 0; (attr = t->ct_attrs[i]) != NULL; i++) {
601 if ((error = configfs_create_file(item, attr)))
602 break;
603 }
604 }
605
606 if (error)
607 detach_attrs(item);
608
609 return error;
610}
611
612static int configfs_attach_group(struct config_item *parent_item,
613 struct config_item *item,
614 struct dentry *dentry);
615static void configfs_detach_group(struct config_item *item);
616
617static void detach_groups(struct config_group *group)
618{
619 struct dentry * dentry = dget(group->cg_item.ci_dentry);
620 struct dentry *child;
621 struct configfs_dirent *parent_sd;
622 struct configfs_dirent *sd, *tmp;
623
624 if (!dentry)
625 return;
626
627 parent_sd = dentry->d_fsdata;
628 list_for_each_entry_safe(sd, tmp, &parent_sd->s_children, s_sibling) {
629 if (!sd->s_element ||
630 !(sd->s_type & CONFIGFS_USET_DEFAULT))
631 continue;
632
633 child = sd->s_dentry;
634
635 mutex_lock(&child->d_inode->i_mutex);
636
637 configfs_detach_group(sd->s_element);
638 child->d_inode->i_flags |= S_DEAD;
639 dont_mount(child);
640
641 mutex_unlock(&child->d_inode->i_mutex);
642
643 d_delete(child);
644 dput(child);
645 }
646
647
648
649
650 dput(dentry);
651}
652
653
654
655
656
657
658
659
660
661static int create_default_group(struct config_group *parent_group,
662 struct config_group *group)
663{
664 int ret;
665 struct configfs_dirent *sd;
666
667 struct dentry *child, *parent = parent_group->cg_item.ci_dentry;
668
669 if (!group->cg_item.ci_name)
670 group->cg_item.ci_name = group->cg_item.ci_namebuf;
671
672 ret = -ENOMEM;
673 child = d_alloc_name(parent, group->cg_item.ci_name);
674 if (child) {
675 d_add(child, NULL);
676
677 ret = configfs_attach_group(&parent_group->cg_item,
678 &group->cg_item, child);
679 if (!ret) {
680 sd = child->d_fsdata;
681 sd->s_type |= CONFIGFS_USET_DEFAULT;
682 } else {
683 BUG_ON(child->d_inode);
684 d_drop(child);
685 dput(child);
686 }
687 }
688
689 return ret;
690}
691
692static int populate_groups(struct config_group *group)
693{
694 struct config_group *new_group;
695 int ret = 0;
696 int i;
697
698 if (group->default_groups) {
699 for (i = 0; group->default_groups[i]; i++) {
700 new_group = group->default_groups[i];
701
702 ret = create_default_group(group, new_group);
703 if (ret) {
704 detach_groups(group);
705 break;
706 }
707 }
708 }
709
710 return ret;
711}
712
713
714
715
716
717
718static void unlink_obj(struct config_item *item)
719{
720 struct config_group *group;
721
722 group = item->ci_group;
723 if (group) {
724 list_del_init(&item->ci_entry);
725
726 item->ci_group = NULL;
727 item->ci_parent = NULL;
728
729
730 config_item_put(item);
731
732
733 config_group_put(group);
734 }
735}
736
737static void link_obj(struct config_item *parent_item, struct config_item *item)
738{
739
740
741
742
743 item->ci_parent = parent_item;
744
745
746
747
748
749 item->ci_group = config_group_get(to_config_group(parent_item));
750 list_add_tail(&item->ci_entry, &item->ci_group->cg_children);
751
752
753
754
755
756 config_item_get(item);
757}
758
759static void unlink_group(struct config_group *group)
760{
761 int i;
762 struct config_group *new_group;
763
764 if (group->default_groups) {
765 for (i = 0; group->default_groups[i]; i++) {
766 new_group = group->default_groups[i];
767 unlink_group(new_group);
768 }
769 }
770
771 group->cg_subsys = NULL;
772 unlink_obj(&group->cg_item);
773}
774
775static void link_group(struct config_group *parent_group, struct config_group *group)
776{
777 int i;
778 struct config_group *new_group;
779 struct configfs_subsystem *subsys = NULL;
780
781 link_obj(&parent_group->cg_item, &group->cg_item);
782
783 if (parent_group->cg_subsys)
784 subsys = parent_group->cg_subsys;
785 else if (configfs_is_root(&parent_group->cg_item))
786 subsys = to_configfs_subsystem(group);
787 else
788 BUG();
789 group->cg_subsys = subsys;
790
791 if (group->default_groups) {
792 for (i = 0; group->default_groups[i]; i++) {
793 new_group = group->default_groups[i];
794 link_group(group, new_group);
795 }
796 }
797}
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814static int configfs_attach_item(struct config_item *parent_item,
815 struct config_item *item,
816 struct dentry *dentry)
817{
818 int ret;
819
820 ret = configfs_create_dir(item, dentry);
821 if (!ret) {
822 ret = populate_attrs(item);
823 if (ret) {
824
825
826
827
828
829 mutex_lock(&dentry->d_inode->i_mutex);
830 configfs_remove_dir(item);
831 dentry->d_inode->i_flags |= S_DEAD;
832 dont_mount(dentry);
833 mutex_unlock(&dentry->d_inode->i_mutex);
834 d_delete(dentry);
835 }
836 }
837
838 return ret;
839}
840
841
842static void configfs_detach_item(struct config_item *item)
843{
844 detach_attrs(item);
845 configfs_remove_dir(item);
846}
847
848static int configfs_attach_group(struct config_item *parent_item,
849 struct config_item *item,
850 struct dentry *dentry)
851{
852 int ret;
853 struct configfs_dirent *sd;
854
855 ret = configfs_attach_item(parent_item, item, dentry);
856 if (!ret) {
857 sd = dentry->d_fsdata;
858 sd->s_type |= CONFIGFS_USET_DIR;
859
860
861
862
863
864
865
866
867
868
869 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
870 configfs_adjust_dir_dirent_depth_before_populate(sd);
871 ret = populate_groups(to_config_group(item));
872 if (ret) {
873 configfs_detach_item(item);
874 dentry->d_inode->i_flags |= S_DEAD;
875 dont_mount(dentry);
876 }
877 configfs_adjust_dir_dirent_depth_after_populate(sd);
878 mutex_unlock(&dentry->d_inode->i_mutex);
879 if (ret)
880 d_delete(dentry);
881 }
882
883 return ret;
884}
885
886
887static void configfs_detach_group(struct config_item *item)
888{
889 detach_groups(to_config_group(item));
890 configfs_detach_item(item);
891}
892
893
894
895
896
897
898
899
900
901
902static void client_disconnect_notify(struct config_item *parent_item,
903 struct config_item *item)
904{
905 struct config_item_type *type;
906
907 type = parent_item->ci_type;
908 BUG_ON(!type);
909
910 if (type->ct_group_ops && type->ct_group_ops->disconnect_notify)
911 type->ct_group_ops->disconnect_notify(to_config_group(parent_item),
912 item);
913}
914
915
916
917
918
919
920
921static void client_drop_item(struct config_item *parent_item,
922 struct config_item *item)
923{
924 struct config_item_type *type;
925
926 type = parent_item->ci_type;
927 BUG_ON(!type);
928
929
930
931
932
933 if (type->ct_group_ops && type->ct_group_ops->drop_item)
934 type->ct_group_ops->drop_item(to_config_group(parent_item),
935 item);
936 else
937 config_item_put(item);
938}
939
940#ifdef DEBUG
941static void configfs_dump_one(struct configfs_dirent *sd, int level)
942{
943 printk(KERN_INFO "%*s\"%s\":\n", level, " ", configfs_get_name(sd));
944
945#define type_print(_type) if (sd->s_type & _type) printk(KERN_INFO "%*s %s\n", level, " ", #_type);
946 type_print(CONFIGFS_ROOT);
947 type_print(CONFIGFS_DIR);
948 type_print(CONFIGFS_ITEM_ATTR);
949 type_print(CONFIGFS_ITEM_LINK);
950 type_print(CONFIGFS_USET_DIR);
951 type_print(CONFIGFS_USET_DEFAULT);
952 type_print(CONFIGFS_USET_DROPPING);
953#undef type_print
954}
955
956static int configfs_dump(struct configfs_dirent *sd, int level)
957{
958 struct configfs_dirent *child_sd;
959 int ret = 0;
960
961 configfs_dump_one(sd, level);
962
963 if (!(sd->s_type & (CONFIGFS_DIR|CONFIGFS_ROOT)))
964 return 0;
965
966 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
967 ret = configfs_dump(child_sd, level + 2);
968 if (ret)
969 break;
970 }
971
972 return ret;
973}
974#endif
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035static int configfs_depend_prep(struct dentry *origin,
1036 struct config_item *target)
1037{
1038 struct configfs_dirent *child_sd, *sd;
1039 int ret = 0;
1040
1041 BUG_ON(!origin || !origin->d_fsdata);
1042 sd = origin->d_fsdata;
1043
1044 if (sd->s_element == target)
1045 goto out;
1046
1047 list_for_each_entry(child_sd, &sd->s_children, s_sibling) {
1048 if ((child_sd->s_type & CONFIGFS_DIR) &&
1049 !(child_sd->s_type & CONFIGFS_USET_DROPPING) &&
1050 !(child_sd->s_type & CONFIGFS_USET_CREATING)) {
1051 ret = configfs_depend_prep(child_sd->s_dentry,
1052 target);
1053 if (!ret)
1054 goto out;
1055 }
1056 }
1057
1058
1059 ret = -ENOENT;
1060
1061out:
1062 return ret;
1063}
1064
1065int configfs_depend_item(struct configfs_subsystem *subsys,
1066 struct config_item *target)
1067{
1068 int ret;
1069 struct configfs_dirent *p, *root_sd, *subsys_sd = NULL;
1070 struct config_item *s_item = &subsys->su_group.cg_item;
1071 struct dentry *root;
1072
1073
1074
1075
1076
1077 root = configfs_pin_fs();
1078 if (IS_ERR(root))
1079 return PTR_ERR(root);
1080
1081
1082
1083
1084
1085
1086 mutex_lock(&root->d_inode->i_mutex);
1087
1088 root_sd = root->d_fsdata;
1089
1090 list_for_each_entry(p, &root_sd->s_children, s_sibling) {
1091 if (p->s_type & CONFIGFS_DIR) {
1092 if (p->s_element == s_item) {
1093 subsys_sd = p;
1094 break;
1095 }
1096 }
1097 }
1098
1099 if (!subsys_sd) {
1100 ret = -ENOENT;
1101 goto out_unlock_fs;
1102 }
1103
1104
1105
1106 spin_lock(&configfs_dirent_lock);
1107
1108 ret = configfs_depend_prep(subsys_sd->s_dentry, target);
1109 if (ret)
1110 goto out_unlock_dirent_lock;
1111
1112
1113
1114
1115
1116 p = target->ci_dentry->d_fsdata;
1117 p->s_dependent_count += 1;
1118
1119out_unlock_dirent_lock:
1120 spin_unlock(&configfs_dirent_lock);
1121out_unlock_fs:
1122 mutex_unlock(&root->d_inode->i_mutex);
1123
1124
1125
1126
1127
1128 configfs_release_fs();
1129
1130 return ret;
1131}
1132EXPORT_SYMBOL(configfs_depend_item);
1133
1134
1135
1136
1137
1138
1139void configfs_undepend_item(struct configfs_subsystem *subsys,
1140 struct config_item *target)
1141{
1142 struct configfs_dirent *sd;
1143
1144
1145
1146
1147
1148 spin_lock(&configfs_dirent_lock);
1149
1150 sd = target->ci_dentry->d_fsdata;
1151 BUG_ON(sd->s_dependent_count < 1);
1152
1153 sd->s_dependent_count -= 1;
1154
1155
1156
1157
1158
1159 spin_unlock(&configfs_dirent_lock);
1160}
1161EXPORT_SYMBOL(configfs_undepend_item);
1162
1163static int configfs_mkdir(struct inode *dir, struct dentry *dentry, umode_t mode)
1164{
1165 int ret = 0;
1166 int module_got = 0;
1167 struct config_group *group = NULL;
1168 struct config_item *item = NULL;
1169 struct config_item *parent_item;
1170 struct configfs_subsystem *subsys;
1171 struct configfs_dirent *sd;
1172 struct config_item_type *type;
1173 struct module *subsys_owner = NULL, *new_item_owner = NULL;
1174 char *name;
1175
1176 sd = dentry->d_parent->d_fsdata;
1177
1178
1179
1180
1181
1182 if (!configfs_dirent_is_ready(sd)) {
1183 ret = -ENOENT;
1184 goto out;
1185 }
1186
1187 if (!(sd->s_type & CONFIGFS_USET_DIR)) {
1188 ret = -EPERM;
1189 goto out;
1190 }
1191
1192
1193 parent_item = configfs_get_config_item(dentry->d_parent);
1194 type = parent_item->ci_type;
1195 subsys = to_config_group(parent_item)->cg_subsys;
1196 BUG_ON(!subsys);
1197
1198 if (!type || !type->ct_group_ops ||
1199 (!type->ct_group_ops->make_group &&
1200 !type->ct_group_ops->make_item)) {
1201 ret = -EPERM;
1202 goto out_put;
1203 }
1204
1205
1206
1207
1208
1209
1210 if (!subsys->su_group.cg_item.ci_type) {
1211 ret = -EINVAL;
1212 goto out_put;
1213 }
1214 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1215 if (!try_module_get(subsys_owner)) {
1216 ret = -EINVAL;
1217 goto out_put;
1218 }
1219
1220 name = kmalloc(dentry->d_name.len + 1, GFP_KERNEL);
1221 if (!name) {
1222 ret = -ENOMEM;
1223 goto out_subsys_put;
1224 }
1225
1226 snprintf(name, dentry->d_name.len + 1, "%s", dentry->d_name.name);
1227
1228 mutex_lock(&subsys->su_mutex);
1229 if (type->ct_group_ops->make_group) {
1230 group = type->ct_group_ops->make_group(to_config_group(parent_item), name);
1231 if (!group)
1232 group = ERR_PTR(-ENOMEM);
1233 if (!IS_ERR(group)) {
1234 link_group(to_config_group(parent_item), group);
1235 item = &group->cg_item;
1236 } else
1237 ret = PTR_ERR(group);
1238 } else {
1239 item = type->ct_group_ops->make_item(to_config_group(parent_item), name);
1240 if (!item)
1241 item = ERR_PTR(-ENOMEM);
1242 if (!IS_ERR(item))
1243 link_obj(parent_item, item);
1244 else
1245 ret = PTR_ERR(item);
1246 }
1247 mutex_unlock(&subsys->su_mutex);
1248
1249 kfree(name);
1250 if (ret) {
1251
1252
1253
1254
1255 goto out_subsys_put;
1256 }
1257
1258
1259
1260
1261
1262
1263 type = item->ci_type;
1264 if (!type) {
1265 ret = -EINVAL;
1266 goto out_unlink;
1267 }
1268
1269 new_item_owner = type->ct_owner;
1270 if (!try_module_get(new_item_owner)) {
1271 ret = -EINVAL;
1272 goto out_unlink;
1273 }
1274
1275
1276
1277
1278
1279
1280 module_got = 1;
1281
1282
1283
1284
1285
1286
1287
1288 spin_lock(&configfs_dirent_lock);
1289
1290 sd->s_type |= CONFIGFS_USET_IN_MKDIR;
1291 spin_unlock(&configfs_dirent_lock);
1292
1293 if (group)
1294 ret = configfs_attach_group(parent_item, item, dentry);
1295 else
1296 ret = configfs_attach_item(parent_item, item, dentry);
1297
1298 spin_lock(&configfs_dirent_lock);
1299 sd->s_type &= ~CONFIGFS_USET_IN_MKDIR;
1300 if (!ret)
1301 configfs_dir_set_ready(dentry->d_fsdata);
1302 spin_unlock(&configfs_dirent_lock);
1303
1304out_unlink:
1305 if (ret) {
1306
1307 mutex_lock(&subsys->su_mutex);
1308
1309 client_disconnect_notify(parent_item, item);
1310 if (group)
1311 unlink_group(group);
1312 else
1313 unlink_obj(item);
1314 client_drop_item(parent_item, item);
1315
1316 mutex_unlock(&subsys->su_mutex);
1317
1318 if (module_got)
1319 module_put(new_item_owner);
1320 }
1321
1322out_subsys_put:
1323 if (ret)
1324 module_put(subsys_owner);
1325
1326out_put:
1327
1328
1329
1330
1331
1332 config_item_put(parent_item);
1333
1334out:
1335 return ret;
1336}
1337
1338static int configfs_rmdir(struct inode *dir, struct dentry *dentry)
1339{
1340 struct config_item *parent_item;
1341 struct config_item *item;
1342 struct configfs_subsystem *subsys;
1343 struct configfs_dirent *sd;
1344 struct module *subsys_owner = NULL, *dead_item_owner = NULL;
1345 int ret;
1346
1347 sd = dentry->d_fsdata;
1348 if (sd->s_type & CONFIGFS_USET_DEFAULT)
1349 return -EPERM;
1350
1351
1352 parent_item = configfs_get_config_item(dentry->d_parent);
1353 subsys = to_config_group(parent_item)->cg_subsys;
1354 BUG_ON(!subsys);
1355
1356 if (!parent_item->ci_type) {
1357 config_item_put(parent_item);
1358 return -EINVAL;
1359 }
1360
1361
1362 BUG_ON(!subsys->su_group.cg_item.ci_type);
1363 subsys_owner = subsys->su_group.cg_item.ci_type->ct_owner;
1364
1365
1366
1367
1368
1369 do {
1370 struct mutex *wait_mutex;
1371
1372 mutex_lock(&configfs_symlink_mutex);
1373 spin_lock(&configfs_dirent_lock);
1374
1375
1376
1377
1378
1379 ret = sd->s_dependent_count ? -EBUSY : 0;
1380 if (!ret) {
1381 ret = configfs_detach_prep(dentry, &wait_mutex);
1382 if (ret)
1383 configfs_detach_rollback(dentry);
1384 }
1385 spin_unlock(&configfs_dirent_lock);
1386 mutex_unlock(&configfs_symlink_mutex);
1387
1388 if (ret) {
1389 if (ret != -EAGAIN) {
1390 config_item_put(parent_item);
1391 return ret;
1392 }
1393
1394
1395 mutex_lock(wait_mutex);
1396 mutex_unlock(wait_mutex);
1397 }
1398 } while (ret == -EAGAIN);
1399
1400
1401 item = configfs_get_config_item(dentry);
1402
1403
1404 config_item_put(parent_item);
1405
1406 if (item->ci_type)
1407 dead_item_owner = item->ci_type->ct_owner;
1408
1409 if (sd->s_type & CONFIGFS_USET_DIR) {
1410 configfs_detach_group(item);
1411
1412 mutex_lock(&subsys->su_mutex);
1413 client_disconnect_notify(parent_item, item);
1414 unlink_group(to_config_group(item));
1415 } else {
1416 configfs_detach_item(item);
1417
1418 mutex_lock(&subsys->su_mutex);
1419 client_disconnect_notify(parent_item, item);
1420 unlink_obj(item);
1421 }
1422
1423 client_drop_item(parent_item, item);
1424 mutex_unlock(&subsys->su_mutex);
1425
1426
1427 config_item_put(item);
1428
1429 module_put(dead_item_owner);
1430 module_put(subsys_owner);
1431
1432 return 0;
1433}
1434
1435const struct inode_operations configfs_dir_inode_operations = {
1436 .mkdir = configfs_mkdir,
1437 .rmdir = configfs_rmdir,
1438 .symlink = configfs_symlink,
1439 .unlink = configfs_unlink,
1440 .lookup = configfs_lookup,
1441 .setattr = configfs_setattr,
1442};
1443
1444const struct inode_operations configfs_root_inode_operations = {
1445 .lookup = configfs_lookup,
1446 .setattr = configfs_setattr,
1447};
1448
1449#if 0
1450int configfs_rename_dir(struct config_item * item, const char *new_name)
1451{
1452 int error = 0;
1453 struct dentry * new_dentry, * parent;
1454
1455 if (!strcmp(config_item_name(item), new_name))
1456 return -EINVAL;
1457
1458 if (!item->parent)
1459 return -EINVAL;
1460
1461 down_write(&configfs_rename_sem);
1462 parent = item->parent->dentry;
1463
1464 mutex_lock(&parent->d_inode->i_mutex);
1465
1466 new_dentry = lookup_one_len(new_name, parent, strlen(new_name));
1467 if (!IS_ERR(new_dentry)) {
1468 if (!new_dentry->d_inode) {
1469 error = config_item_set_name(item, "%s", new_name);
1470 if (!error) {
1471 d_add(new_dentry, NULL);
1472 d_move(item->dentry, new_dentry);
1473 }
1474 else
1475 d_delete(new_dentry);
1476 } else
1477 error = -EEXIST;
1478 dput(new_dentry);
1479 }
1480 mutex_unlock(&parent->d_inode->i_mutex);
1481 up_write(&configfs_rename_sem);
1482
1483 return error;
1484}
1485#endif
1486
1487static int configfs_dir_open(struct inode *inode, struct file *file)
1488{
1489 struct dentry * dentry = file->f_path.dentry;
1490 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1491 int err;
1492
1493 mutex_lock(&dentry->d_inode->i_mutex);
1494
1495
1496
1497
1498 err = -ENOENT;
1499 if (configfs_dirent_is_ready(parent_sd)) {
1500 file->private_data = configfs_new_dirent(parent_sd, NULL, 0);
1501 if (IS_ERR(file->private_data))
1502 err = PTR_ERR(file->private_data);
1503 else
1504 err = 0;
1505 }
1506 mutex_unlock(&dentry->d_inode->i_mutex);
1507
1508 return err;
1509}
1510
1511static int configfs_dir_close(struct inode *inode, struct file *file)
1512{
1513 struct dentry * dentry = file->f_path.dentry;
1514 struct configfs_dirent * cursor = file->private_data;
1515
1516 mutex_lock(&dentry->d_inode->i_mutex);
1517 spin_lock(&configfs_dirent_lock);
1518 list_del_init(&cursor->s_sibling);
1519 spin_unlock(&configfs_dirent_lock);
1520 mutex_unlock(&dentry->d_inode->i_mutex);
1521
1522 release_configfs_dirent(cursor);
1523
1524 return 0;
1525}
1526
1527
1528static inline unsigned char dt_type(struct configfs_dirent *sd)
1529{
1530 return (sd->s_mode >> 12) & 15;
1531}
1532
1533static int configfs_readdir(struct file *file, struct dir_context *ctx)
1534{
1535 struct dentry *dentry = file->f_path.dentry;
1536 struct super_block *sb = dentry->d_sb;
1537 struct configfs_dirent * parent_sd = dentry->d_fsdata;
1538 struct configfs_dirent *cursor = file->private_data;
1539 struct list_head *p, *q = &cursor->s_sibling;
1540 ino_t ino = 0;
1541
1542 if (!dir_emit_dots(file, ctx))
1543 return 0;
1544 if (ctx->pos == 2) {
1545 spin_lock(&configfs_dirent_lock);
1546 list_move(q, &parent_sd->s_children);
1547 spin_unlock(&configfs_dirent_lock);
1548 }
1549 for (p = q->next; p != &parent_sd->s_children; p = p->next) {
1550 struct configfs_dirent *next;
1551 const char *name;
1552 int len;
1553 struct inode *inode = NULL;
1554
1555 next = list_entry(p, struct configfs_dirent, s_sibling);
1556 if (!next->s_element)
1557 continue;
1558
1559 name = configfs_get_name(next);
1560 len = strlen(name);
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575 spin_lock(&configfs_dirent_lock);
1576 dentry = next->s_dentry;
1577 if (dentry)
1578 inode = dentry->d_inode;
1579 if (inode)
1580 ino = inode->i_ino;
1581 spin_unlock(&configfs_dirent_lock);
1582 if (!inode)
1583 ino = iunique(sb, 2);
1584
1585 if (!dir_emit(ctx, name, len, ino, dt_type(next)))
1586 return 0;
1587
1588 spin_lock(&configfs_dirent_lock);
1589 list_move(q, p);
1590 spin_unlock(&configfs_dirent_lock);
1591 p = q;
1592 ctx->pos++;
1593 }
1594 return 0;
1595}
1596
1597static loff_t configfs_dir_lseek(struct file *file, loff_t offset, int whence)
1598{
1599 struct dentry * dentry = file->f_path.dentry;
1600
1601 mutex_lock(&dentry->d_inode->i_mutex);
1602 switch (whence) {
1603 case 1:
1604 offset += file->f_pos;
1605 case 0:
1606 if (offset >= 0)
1607 break;
1608 default:
1609 mutex_unlock(&file_inode(file)->i_mutex);
1610 return -EINVAL;
1611 }
1612 if (offset != file->f_pos) {
1613 file->f_pos = offset;
1614 if (file->f_pos >= 2) {
1615 struct configfs_dirent *sd = dentry->d_fsdata;
1616 struct configfs_dirent *cursor = file->private_data;
1617 struct list_head *p;
1618 loff_t n = file->f_pos - 2;
1619
1620 spin_lock(&configfs_dirent_lock);
1621 list_del(&cursor->s_sibling);
1622 p = sd->s_children.next;
1623 while (n && p != &sd->s_children) {
1624 struct configfs_dirent *next;
1625 next = list_entry(p, struct configfs_dirent,
1626 s_sibling);
1627 if (next->s_element)
1628 n--;
1629 p = p->next;
1630 }
1631 list_add_tail(&cursor->s_sibling, p);
1632 spin_unlock(&configfs_dirent_lock);
1633 }
1634 }
1635 mutex_unlock(&dentry->d_inode->i_mutex);
1636 return offset;
1637}
1638
1639const struct file_operations configfs_dir_operations = {
1640 .open = configfs_dir_open,
1641 .release = configfs_dir_close,
1642 .llseek = configfs_dir_lseek,
1643 .read = generic_read_dir,
1644 .iterate = configfs_readdir,
1645};
1646
1647int configfs_register_subsystem(struct configfs_subsystem *subsys)
1648{
1649 int err;
1650 struct config_group *group = &subsys->su_group;
1651 struct dentry *dentry;
1652 struct dentry *root;
1653 struct configfs_dirent *sd;
1654
1655 root = configfs_pin_fs();
1656 if (IS_ERR(root))
1657 return PTR_ERR(root);
1658
1659 if (!group->cg_item.ci_name)
1660 group->cg_item.ci_name = group->cg_item.ci_namebuf;
1661
1662 sd = root->d_fsdata;
1663 link_group(to_config_group(sd->s_element), group);
1664
1665 mutex_lock_nested(&root->d_inode->i_mutex, I_MUTEX_PARENT);
1666
1667 err = -ENOMEM;
1668 dentry = d_alloc_name(root, group->cg_item.ci_name);
1669 if (dentry) {
1670 d_add(dentry, NULL);
1671
1672 err = configfs_attach_group(sd->s_element, &group->cg_item,
1673 dentry);
1674 if (err) {
1675 BUG_ON(dentry->d_inode);
1676 d_drop(dentry);
1677 dput(dentry);
1678 } else {
1679 spin_lock(&configfs_dirent_lock);
1680 configfs_dir_set_ready(dentry->d_fsdata);
1681 spin_unlock(&configfs_dirent_lock);
1682 }
1683 }
1684
1685 mutex_unlock(&root->d_inode->i_mutex);
1686
1687 if (err) {
1688 unlink_group(group);
1689 configfs_release_fs();
1690 }
1691
1692 return err;
1693}
1694
1695void configfs_unregister_subsystem(struct configfs_subsystem *subsys)
1696{
1697 struct config_group *group = &subsys->su_group;
1698 struct dentry *dentry = group->cg_item.ci_dentry;
1699 struct dentry *root = dentry->d_sb->s_root;
1700
1701 if (dentry->d_parent != root) {
1702 printk(KERN_ERR "configfs: Tried to unregister non-subsystem!\n");
1703 return;
1704 }
1705
1706 mutex_lock_nested(&root->d_inode->i_mutex,
1707 I_MUTEX_PARENT);
1708 mutex_lock_nested(&dentry->d_inode->i_mutex, I_MUTEX_CHILD);
1709 mutex_lock(&configfs_symlink_mutex);
1710 spin_lock(&configfs_dirent_lock);
1711 if (configfs_detach_prep(dentry, NULL)) {
1712 printk(KERN_ERR "configfs: Tried to unregister non-empty subsystem!\n");
1713 }
1714 spin_unlock(&configfs_dirent_lock);
1715 mutex_unlock(&configfs_symlink_mutex);
1716 configfs_detach_group(&group->cg_item);
1717 dentry->d_inode->i_flags |= S_DEAD;
1718 dont_mount(dentry);
1719 mutex_unlock(&dentry->d_inode->i_mutex);
1720
1721 d_delete(dentry);
1722
1723 mutex_unlock(&root->d_inode->i_mutex);
1724
1725 dput(dentry);
1726
1727 unlink_group(group);
1728 configfs_release_fs();
1729}
1730
1731EXPORT_SYMBOL(configfs_register_subsystem);
1732EXPORT_SYMBOL(configfs_unregister_subsystem);
1733