1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/config.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
21#include <linux/slab.h>
22#include <linux/init.h>
23#include <linux/smp_lock.h>
24#include <linux/cache.h>
25#include <linux/module.h>
26
27#include <asm/uaccess.h>
28
29#define DCACHE_PARANOIA 1
30
31
32spinlock_t dcache_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
33rwlock_t dparent_lock __cacheline_aligned_in_smp = RW_LOCK_UNLOCKED;
34
35static kmem_cache_t *dentry_cache;
36
37
38
39
40
41
42
43
44
45#define D_HASHBITS d_hash_shift
46#define D_HASHMASK d_hash_mask
47
48static unsigned int d_hash_mask;
49static unsigned int d_hash_shift;
50static struct list_head *dentry_hashtable;
51static LIST_HEAD(dentry_unused);
52
53
54struct dentry_stat_t dentry_stat = {0, 0, 45, 0,};
55
56
57static inline void d_free(struct dentry *dentry)
58{
59 if (dentry->d_op && dentry->d_op->d_release)
60 dentry->d_op->d_release(dentry);
61 if (dname_external(dentry))
62 kfree(dentry->d_name.name);
63 kmem_cache_free(dentry_cache, dentry);
64 dentry_stat.nr_dentry--;
65}
66
67
68
69
70
71
72static inline void dentry_iput(struct dentry * dentry)
73{
74 struct inode *inode = dentry->d_inode;
75 if (inode) {
76 dentry->d_inode = NULL;
77 list_del_init(&dentry->d_alias);
78 spin_unlock(&dcache_lock);
79 if (dentry->d_op && dentry->d_op->d_iput)
80 dentry->d_op->d_iput(dentry, inode);
81 else
82 iput(inode);
83 } else
84 spin_unlock(&dcache_lock);
85}
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116void dput(struct dentry *dentry)
117{
118 if (!dentry)
119 return;
120
121repeat:
122 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
123 return;
124
125
126 if (!list_empty(&dentry->d_lru))
127 BUG();
128
129
130
131 if (dentry->d_op && dentry->d_op->d_delete) {
132 if (dentry->d_op->d_delete(dentry))
133 goto unhash_it;
134 }
135
136 if (list_empty(&dentry->d_hash))
137 goto kill_it;
138 list_add(&dentry->d_lru, &dentry_unused);
139 dentry_stat.nr_unused++;
140 dentry->d_vfs_flags |= DCACHE_REFERENCED;
141 spin_unlock(&dcache_lock);
142 return;
143
144unhash_it:
145 list_del_init(&dentry->d_hash);
146
147kill_it: {
148 struct dentry *parent;
149 list_del(&dentry->d_child);
150
151 dentry_iput(dentry);
152 parent = dentry->d_parent;
153 d_free(dentry);
154 if (dentry == parent)
155 return;
156 dentry = parent;
157 goto repeat;
158 }
159}
160
161
162
163
164
165
166
167
168
169
170
171
172
173int d_invalidate(struct dentry * dentry)
174{
175
176
177
178 spin_lock(&dcache_lock);
179 if (list_empty(&dentry->d_hash)) {
180 spin_unlock(&dcache_lock);
181 return 0;
182 }
183
184
185
186
187 if (!list_empty(&dentry->d_subdirs)) {
188 spin_unlock(&dcache_lock);
189 shrink_dcache_parent(dentry);
190 spin_lock(&dcache_lock);
191 }
192
193
194
195
196
197
198
199
200
201
202
203 if (atomic_read(&dentry->d_count) > 1) {
204 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
205 spin_unlock(&dcache_lock);
206 return -EBUSY;
207 }
208 }
209
210 list_del_init(&dentry->d_hash);
211 spin_unlock(&dcache_lock);
212 return 0;
213}
214
215
216
217static inline struct dentry * __dget_locked(struct dentry *dentry)
218{
219 atomic_inc(&dentry->d_count);
220 if (atomic_read(&dentry->d_count) == 1) {
221 dentry_stat.nr_unused--;
222 list_del_init(&dentry->d_lru);
223 }
224 return dentry;
225}
226
227struct dentry * dget_locked(struct dentry *dentry)
228{
229 return __dget_locked(dentry);
230}
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245struct dentry * d_find_alias(struct inode *inode)
246{
247 struct list_head *head, *next, *tmp;
248 struct dentry *alias, *discon_alias=NULL;
249
250 spin_lock(&dcache_lock);
251 head = &inode->i_dentry;
252 next = inode->i_dentry.next;
253 while (next != head) {
254 tmp = next;
255 next = tmp->next;
256 alias = list_entry(tmp, struct dentry, d_alias);
257 if (!list_empty(&alias->d_hash)) {
258 if (alias->d_flags & DCACHE_DISCONNECTED)
259 discon_alias = alias;
260 else {
261 __dget_locked(alias);
262 spin_unlock(&dcache_lock);
263 return alias;
264 }
265 }
266 }
267 if (discon_alias)
268 __dget_locked(discon_alias);
269 spin_unlock(&dcache_lock);
270 return discon_alias;
271}
272
273
274
275
276
277void d_prune_aliases(struct inode *inode)
278{
279 struct list_head *tmp, *head = &inode->i_dentry;
280restart:
281 spin_lock(&dcache_lock);
282 tmp = head;
283 while ((tmp = tmp->next) != head) {
284 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
285 if (!atomic_read(&dentry->d_count)) {
286 __dget_locked(dentry);
287 spin_unlock(&dcache_lock);
288 d_drop(dentry);
289 dput(dentry);
290 goto restart;
291 }
292 }
293 spin_unlock(&dcache_lock);
294}
295
296
297
298
299
300
301
302static inline void prune_one_dentry(struct dentry * dentry)
303{
304 struct dentry * parent;
305
306 list_del_init(&dentry->d_hash);
307 list_del(&dentry->d_child);
308 dentry_iput(dentry);
309 parent = dentry->d_parent;
310 d_free(dentry);
311 if (parent != dentry)
312 dput(parent);
313 spin_lock(&dcache_lock);
314}
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329void prune_dcache(int count)
330{
331 spin_lock(&dcache_lock);
332 for (;;) {
333 struct dentry *dentry;
334 struct list_head *tmp;
335
336 tmp = dentry_unused.prev;
337
338 if (tmp == &dentry_unused)
339 break;
340 list_del_init(tmp);
341 dentry = list_entry(tmp, struct dentry, d_lru);
342
343
344 if (dentry->d_vfs_flags & DCACHE_REFERENCED) {
345 dentry->d_vfs_flags &= ~DCACHE_REFERENCED;
346 list_add(&dentry->d_lru, &dentry_unused);
347 continue;
348 }
349 dentry_stat.nr_unused--;
350
351
352 if (atomic_read(&dentry->d_count))
353 BUG();
354
355 prune_one_dentry(dentry);
356 if (!--count)
357 break;
358 }
359 spin_unlock(&dcache_lock);
360}
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384void shrink_dcache_sb(struct super_block * sb)
385{
386 struct list_head *tmp, *next;
387 struct dentry *dentry;
388
389
390
391
392
393 spin_lock(&dcache_lock);
394 next = dentry_unused.next;
395 while (next != &dentry_unused) {
396 tmp = next;
397 next = tmp->next;
398 dentry = list_entry(tmp, struct dentry, d_lru);
399 if (dentry->d_sb != sb)
400 continue;
401 list_del(tmp);
402 list_add(tmp, &dentry_unused);
403 }
404
405
406
407
408repeat:
409 next = dentry_unused.next;
410 while (next != &dentry_unused) {
411 tmp = next;
412 next = tmp->next;
413 dentry = list_entry(tmp, struct dentry, d_lru);
414 if (dentry->d_sb != sb)
415 continue;
416 if (atomic_read(&dentry->d_count))
417 continue;
418 dentry_stat.nr_unused--;
419 list_del_init(tmp);
420 prune_one_dentry(dentry);
421 goto repeat;
422 }
423 spin_unlock(&dcache_lock);
424}
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440int have_submounts(struct dentry *parent)
441{
442 struct dentry *this_parent = parent;
443 struct list_head *next;
444
445 spin_lock(&dcache_lock);
446 if (d_mountpoint(parent))
447 goto positive;
448repeat:
449 next = this_parent->d_subdirs.next;
450resume:
451 while (next != &this_parent->d_subdirs) {
452 struct list_head *tmp = next;
453 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
454 next = tmp->next;
455
456 if (d_mountpoint(dentry))
457 goto positive;
458 if (!list_empty(&dentry->d_subdirs)) {
459 this_parent = dentry;
460 goto repeat;
461 }
462 }
463
464
465
466 if (this_parent != parent) {
467 next = this_parent->d_child.next;
468 this_parent = this_parent->d_parent;
469 goto resume;
470 }
471 spin_unlock(&dcache_lock);
472 return 0;
473positive:
474 spin_unlock(&dcache_lock);
475 return 1;
476}
477
478
479
480
481
482
483
484
485static int select_parent(struct dentry * parent)
486{
487 struct dentry *this_parent = parent;
488 struct list_head *next;
489 int found = 0;
490
491 spin_lock(&dcache_lock);
492repeat:
493 next = this_parent->d_subdirs.next;
494resume:
495 while (next != &this_parent->d_subdirs) {
496 struct list_head *tmp = next;
497 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
498 next = tmp->next;
499 if (!atomic_read(&dentry->d_count)) {
500 list_del(&dentry->d_lru);
501 list_add(&dentry->d_lru, dentry_unused.prev);
502 found++;
503 }
504
505
506
507 if (!list_empty(&dentry->d_subdirs)) {
508 this_parent = dentry;
509#ifdef DCACHE_DEBUG
510printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
511dentry->d_parent->d_name.name, dentry->d_name.name, found);
512#endif
513 goto repeat;
514 }
515 }
516
517
518
519 if (this_parent != parent) {
520 next = this_parent->d_child.next;
521 this_parent = this_parent->d_parent;
522#ifdef DCACHE_DEBUG
523printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
524this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
525#endif
526 goto resume;
527 }
528 spin_unlock(&dcache_lock);
529 return found;
530}
531
532
533
534
535
536
537
538
539void shrink_dcache_parent(struct dentry * parent)
540{
541 int found;
542
543 while ((found = select_parent(parent)) != 0)
544 prune_dcache(found);
545}
546
547
548
549
550
551
552
553
554void shrink_dcache_anon(struct list_head *head)
555{
556 struct list_head *lp;
557 int found;
558 do {
559 found = 0;
560 spin_lock(&dcache_lock);
561 list_for_each(lp, head) {
562 struct dentry *this = list_entry(lp, struct dentry, d_hash);
563 if (!atomic_read(&this->d_count)) {
564 list_del(&this->d_lru);
565 list_add_tail(&this->d_lru, &dentry_unused);
566 found++;
567 }
568 }
569 spin_unlock(&dcache_lock);
570 prune_dcache(found);
571 } while(found);
572}
573
574
575
576
577
578
579
580
581
582
583
584
585int shrink_dcache_memory(int priority, unsigned int gfp_mask)
586{
587 int count = 0;
588
589
590
591
592
593
594
595
596
597
598
599
600 if (!(gfp_mask & __GFP_FS))
601 return 0;
602
603 count = dentry_stat.nr_unused / priority;
604
605 prune_dcache(count);
606 kmem_cache_shrink(dentry_cache);
607 return 0;
608}
609
610#define NAME_ALLOC_LEN(len) ((len+16) & ~15)
611
612
613
614
615
616
617
618
619
620
621
622struct dentry * d_alloc(struct dentry * parent, const struct qstr *name)
623{
624 char * str;
625 struct dentry *dentry;
626
627 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
628 if (!dentry)
629 return NULL;
630
631 if (name->len > DNAME_INLINE_LEN-1) {
632 str = kmalloc(NAME_ALLOC_LEN(name->len), GFP_KERNEL);
633 if (!str) {
634 kmem_cache_free(dentry_cache, dentry);
635 return NULL;
636 }
637 } else
638 str = dentry->d_iname;
639
640 memcpy(str, name->name, name->len);
641 str[name->len] = 0;
642
643 atomic_set(&dentry->d_count, 1);
644 dentry->d_vfs_flags = 0;
645 dentry->d_flags = 0;
646 dentry->d_inode = NULL;
647 dentry->d_parent = NULL;
648 dentry->d_sb = NULL;
649 dentry->d_name.name = str;
650 dentry->d_name.len = name->len;
651 dentry->d_name.hash = name->hash;
652 dentry->d_op = NULL;
653 dentry->d_fsdata = NULL;
654 dentry->d_mounted = 0;
655 INIT_LIST_HEAD(&dentry->d_hash);
656 INIT_LIST_HEAD(&dentry->d_lru);
657 INIT_LIST_HEAD(&dentry->d_subdirs);
658 INIT_LIST_HEAD(&dentry->d_alias);
659 if (parent) {
660 dentry->d_parent = dget(parent);
661 dentry->d_sb = parent->d_sb;
662 spin_lock(&dcache_lock);
663 list_add(&dentry->d_child, &parent->d_subdirs);
664 spin_unlock(&dcache_lock);
665 } else
666 INIT_LIST_HEAD(&dentry->d_child);
667
668 dentry_stat.nr_dentry++;
669 return dentry;
670}
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687void d_instantiate(struct dentry *entry, struct inode * inode)
688{
689 if (!list_empty(&entry->d_alias)) BUG();
690 spin_lock(&dcache_lock);
691 if (inode)
692 list_add(&entry->d_alias, &inode->i_dentry);
693 entry->d_inode = inode;
694 spin_unlock(&dcache_lock);
695}
696
697
698
699
700
701
702
703
704
705
706struct dentry * d_alloc_root(struct inode * root_inode)
707{
708 struct dentry *res = NULL;
709
710 if (root_inode) {
711 res = d_alloc(NULL, &(const struct qstr) { "/", 1, 0 });
712 if (res) {
713 res->d_sb = root_inode->i_sb;
714 res->d_parent = res;
715 d_instantiate(res, root_inode);
716 }
717 }
718 return res;
719}
720
721static inline struct list_head * d_hash(struct dentry * parent, unsigned long hash)
722{
723 hash += (unsigned long) parent / L1_CACHE_BYTES;
724 hash = hash ^ (hash >> D_HASHBITS);
725 return dentry_hashtable + (hash & D_HASHMASK);
726}
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748struct dentry * d_alloc_anon(struct inode *inode)
749{
750 struct dentry *tmp;
751 struct dentry *res;
752
753 if ((res = d_find_alias(inode))) {
754 iput(inode);
755 return res;
756 }
757
758 tmp = d_alloc(NULL, &(const struct qstr) {"",0,0});
759 if (!tmp)
760 return NULL;
761
762 tmp->d_parent = tmp;
763
764 spin_lock(&dcache_lock);
765 if (S_ISDIR(inode->i_mode) && !list_empty(&inode->i_dentry)) {
766
767
768
769 res = list_entry(inode->i_dentry.next, struct dentry, d_alias);
770 __dget_locked(res);
771 } else {
772
773 res = tmp;
774 tmp = NULL;
775 if (res) {
776 res->d_sb = inode->i_sb;
777 res->d_parent = res;
778 res->d_inode = inode;
779 res->d_flags |= DCACHE_DISCONNECTED;
780 list_add(&res->d_alias, &inode->i_dentry);
781 list_add(&res->d_hash, &inode->i_sb->s_anon);
782 }
783 inode = NULL;
784 }
785 spin_unlock(&dcache_lock);
786
787 if (inode)
788 iput(inode);
789 if (tmp)
790 dput(tmp);
791 return res;
792}
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
812{
813 struct dentry *new = NULL;
814
815 if (inode && S_ISDIR(inode->i_mode)) {
816 spin_lock(&dcache_lock);
817 if (!list_empty(&inode->i_dentry)) {
818 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
819 __dget_locked(new);
820 spin_unlock(&dcache_lock);
821 d_rehash(dentry);
822 d_move(new, dentry);
823 iput(inode);
824 } else {
825
826 list_add(&dentry->d_alias, &inode->i_dentry);
827 dentry->d_inode = inode;
828 spin_unlock(&dcache_lock);
829 d_rehash(dentry);
830 }
831 } else
832 d_add(dentry, inode);
833 return new;
834}
835
836
837
838
839
840
841
842
843
844
845
846
847
848struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
849{
850 struct dentry * dentry;
851 spin_lock(&dcache_lock);
852 dentry = __d_lookup(parent,name);
853 if (dentry)
854 __dget_locked(dentry);
855 spin_unlock(&dcache_lock);
856 return dentry;
857}
858
859struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
860{
861
862 unsigned int len = name->len;
863 unsigned int hash = name->hash;
864 const unsigned char *str = name->name;
865 struct list_head *head = d_hash(parent,hash);
866 struct list_head *tmp;
867
868 tmp = head->next;
869 for (;;) {
870 struct dentry * dentry = list_entry(tmp, struct dentry, d_hash);
871 if (tmp == head)
872 break;
873 tmp = tmp->next;
874 if (dentry->d_name.hash != hash)
875 continue;
876 if (dentry->d_parent != parent)
877 continue;
878 if (parent->d_op && parent->d_op->d_compare) {
879 if (parent->d_op->d_compare(parent, &dentry->d_name, name))
880 continue;
881 } else {
882 if (dentry->d_name.len != len)
883 continue;
884 if (memcmp(dentry->d_name.name, str, len))
885 continue;
886 }
887 return dentry;
888 }
889 return NULL;
890}
891
892
893
894
895
896
897
898
899
900
901
902
903
904int d_validate(struct dentry *dentry, struct dentry *dparent)
905{
906 unsigned long dent_addr = (unsigned long) dentry;
907 unsigned long min_addr = PAGE_OFFSET;
908 unsigned long align_mask = 0x0F;
909 struct list_head *base, *lhp;
910
911 if (dent_addr < min_addr)
912 goto out;
913 if (dent_addr > (unsigned long)high_memory - sizeof(struct dentry))
914 goto out;
915 if (dent_addr & align_mask)
916 goto out;
917 if ((!kern_addr_valid(dent_addr)) || (!kern_addr_valid(dent_addr -1 +
918 sizeof(struct dentry))))
919 goto out;
920
921 if (dentry->d_parent != dparent)
922 goto out;
923
924 spin_lock(&dcache_lock);
925 lhp = base = d_hash(dparent, dentry->d_name.hash);
926 while ((lhp = lhp->next) != base) {
927 if (dentry == list_entry(lhp, struct dentry, d_hash)) {
928 __dget_locked(dentry);
929 spin_unlock(&dcache_lock);
930 return 1;
931 }
932 }
933 spin_unlock(&dcache_lock);
934out:
935 return 0;
936}
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959void d_delete(struct dentry * dentry)
960{
961
962
963
964 spin_lock(&dcache_lock);
965 if (atomic_read(&dentry->d_count) == 1) {
966 dentry_iput(dentry);
967 return;
968 }
969 spin_unlock(&dcache_lock);
970
971
972
973
974
975 d_drop(dentry);
976}
977
978
979
980
981
982
983
984
985void d_rehash(struct dentry * entry)
986{
987 struct list_head *list = d_hash(entry->d_parent, entry->d_name.hash);
988 if (!list_empty(&entry->d_hash)) BUG();
989 spin_lock(&dcache_lock);
990 list_add(&entry->d_hash, list);
991 spin_unlock(&dcache_lock);
992}
993
994#define do_switch(x,y) do { \
995 __typeof__ (x) __tmp = x; \
996 x = y; y = __tmp; } while (0)
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009static inline void switch_names(struct dentry * dentry, struct dentry * target)
1010{
1011 const unsigned char *old_name, *new_name;
1012
1013 memcpy(dentry->d_iname, target->d_iname, DNAME_INLINE_LEN);
1014 old_name = target->d_name.name;
1015 new_name = dentry->d_name.name;
1016 if (old_name == target->d_iname)
1017 old_name = dentry->d_iname;
1018 if (new_name == dentry->d_iname)
1019 new_name = target->d_iname;
1020 target->d_name.name = new_name;
1021 dentry->d_name.name = old_name;
1022}
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049void d_move(struct dentry * dentry, struct dentry * target)
1050{
1051 if (!dentry->d_inode)
1052 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1053
1054 spin_lock(&dcache_lock);
1055
1056 list_del(&dentry->d_hash);
1057 list_add(&dentry->d_hash, &target->d_hash);
1058
1059
1060 list_del_init(&target->d_hash);
1061
1062 list_del(&dentry->d_child);
1063 list_del(&target->d_child);
1064
1065
1066 switch_names(dentry, target);
1067 do_switch(dentry->d_name.len, target->d_name.len);
1068 do_switch(dentry->d_name.hash, target->d_name.hash);
1069
1070 write_lock(&dparent_lock);
1071 if (IS_ROOT(dentry)) {
1072 dentry->d_parent = target->d_parent;
1073 target->d_parent = target;
1074 INIT_LIST_HEAD(&target->d_child);
1075 } else {
1076 do_switch(dentry->d_parent, target->d_parent);
1077
1078
1079 list_add(&target->d_child, &target->d_parent->d_subdirs);
1080 }
1081 write_unlock(&dparent_lock);
1082
1083 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
1084 spin_unlock(&dcache_lock);
1085}
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102char * __d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1103 struct dentry *root, struct vfsmount *rootmnt,
1104 char *buffer, int buflen)
1105{
1106 char * end = buffer+buflen;
1107 char * retval;
1108 int namelen;
1109
1110 *--end = '\0';
1111 buflen--;
1112 if (!IS_ROOT(dentry) && list_empty(&dentry->d_hash)) {
1113 buflen -= 10;
1114 end -= 10;
1115 memcpy(end, " (deleted)", 10);
1116 }
1117
1118
1119 retval = end-1;
1120 *retval = '/';
1121
1122 for (;;) {
1123 struct dentry * parent;
1124
1125 if (dentry == root && vfsmnt == rootmnt)
1126 break;
1127 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1128
1129 if (vfsmnt->mnt_parent == vfsmnt)
1130 goto global_root;
1131 dentry = vfsmnt->mnt_mountpoint;
1132 vfsmnt = vfsmnt->mnt_parent;
1133 continue;
1134 }
1135 parent = dentry->d_parent;
1136 namelen = dentry->d_name.len;
1137 buflen -= namelen + 1;
1138 if (buflen < 0)
1139 break;
1140 end -= namelen;
1141 memcpy(end, dentry->d_name.name, namelen);
1142 *--end = '/';
1143 retval = end;
1144 dentry = parent;
1145 }
1146 return retval;
1147global_root:
1148 namelen = dentry->d_name.len;
1149 buflen -= namelen;
1150 if (buflen >= 0) {
1151 retval -= namelen-1;
1152 memcpy(retval, dentry->d_name.name, namelen);
1153 }
1154 return retval;
1155}
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175asmlinkage long sys_getcwd(char *buf, unsigned long size)
1176{
1177 int error;
1178 struct vfsmount *pwdmnt, *rootmnt;
1179 struct dentry *pwd, *root;
1180 char *page = (char *) __get_free_page(GFP_USER);
1181
1182 if (!page)
1183 return -ENOMEM;
1184
1185 read_lock(¤t->fs->lock);
1186 pwdmnt = mntget(current->fs->pwdmnt);
1187 pwd = dget(current->fs->pwd);
1188 rootmnt = mntget(current->fs->rootmnt);
1189 root = dget(current->fs->root);
1190 read_unlock(¤t->fs->lock);
1191
1192 error = -ENOENT;
1193
1194 spin_lock(&dcache_lock);
1195 if (pwd->d_parent == pwd || !list_empty(&pwd->d_hash)) {
1196 unsigned long len;
1197 char * cwd;
1198
1199 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1200 spin_unlock(&dcache_lock);
1201
1202 error = -ERANGE;
1203 len = PAGE_SIZE + page - cwd;
1204 if (len <= size) {
1205 error = len;
1206 if (copy_to_user(buf, cwd, len))
1207 error = -EFAULT;
1208 }
1209 } else
1210 spin_unlock(&dcache_lock);
1211 dput(pwd);
1212 mntput(pwdmnt);
1213 dput(root);
1214 mntput(rootmnt);
1215 free_page((unsigned long) page);
1216 return error;
1217}
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1235{
1236 int result;
1237
1238 result = 0;
1239 for (;;) {
1240 if (new_dentry != old_dentry) {
1241 struct dentry * parent = new_dentry->d_parent;
1242 if (parent == new_dentry)
1243 break;
1244 new_dentry = parent;
1245 continue;
1246 }
1247 result = 1;
1248 break;
1249 }
1250 return result;
1251}
1252
1253void d_genocide(struct dentry *root)
1254{
1255 struct dentry *this_parent = root;
1256 struct list_head *next;
1257
1258 spin_lock(&dcache_lock);
1259repeat:
1260 next = this_parent->d_subdirs.next;
1261resume:
1262 while (next != &this_parent->d_subdirs) {
1263 struct list_head *tmp = next;
1264 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1265 next = tmp->next;
1266 if (d_unhashed(dentry)||!dentry->d_inode)
1267 continue;
1268 if (!list_empty(&dentry->d_subdirs)) {
1269 this_parent = dentry;
1270 goto repeat;
1271 }
1272 atomic_dec(&dentry->d_count);
1273 }
1274 if (this_parent != root) {
1275 next = this_parent->d_child.next;
1276 atomic_dec(&this_parent->d_count);
1277 this_parent = this_parent->d_parent;
1278 goto resume;
1279 }
1280 spin_unlock(&dcache_lock);
1281}
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1298{
1299 struct dentry * dentry;
1300 ino_t ino = 0;
1301
1302
1303
1304
1305
1306
1307 name->hash = full_name_hash(name->name, name->len);
1308 if (dir->d_op && dir->d_op->d_hash)
1309 {
1310 if (dir->d_op->d_hash(dir, name) != 0)
1311 goto out;
1312 }
1313
1314 dentry = d_lookup(dir, name);
1315 if (dentry)
1316 {
1317 if (dentry->d_inode)
1318 ino = dentry->d_inode->i_ino;
1319 dput(dentry);
1320 }
1321out:
1322 return ino;
1323}
1324
1325static void __init dcache_init(unsigned long mempages)
1326{
1327 struct list_head *d;
1328 unsigned long order;
1329 unsigned int nr_hash;
1330 int i;
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340 dentry_cache = kmem_cache_create("dentry_cache",
1341 sizeof(struct dentry),
1342 0,
1343 SLAB_HWCACHE_ALIGN,
1344 NULL, NULL);
1345 if (!dentry_cache)
1346 panic("Cannot create dentry cache");
1347
1348#if PAGE_SHIFT < 13
1349 mempages >>= (13 - PAGE_SHIFT);
1350#endif
1351 mempages *= sizeof(struct list_head);
1352 for (order = 0; ((1UL << order) << PAGE_SHIFT) < mempages; order++)
1353 ;
1354
1355 do {
1356 unsigned long tmp;
1357
1358 nr_hash = (1UL << order) * PAGE_SIZE /
1359 sizeof(struct list_head);
1360 d_hash_mask = (nr_hash - 1);
1361
1362 tmp = nr_hash;
1363 d_hash_shift = 0;
1364 while ((tmp >>= 1UL) != 0UL)
1365 d_hash_shift++;
1366
1367 dentry_hashtable = (struct list_head *)
1368 __get_free_pages(GFP_ATOMIC, order);
1369 } while (dentry_hashtable == NULL && --order >= 0);
1370
1371 printk("Dentry-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1372 nr_hash, order, (PAGE_SIZE << order));
1373
1374 if (!dentry_hashtable)
1375 panic("Failed to allocate dcache hash table\n");
1376
1377 d = dentry_hashtable;
1378 i = nr_hash;
1379 do {
1380 INIT_LIST_HEAD(d);
1381 d++;
1382 i--;
1383 } while (i);
1384}
1385
1386
1387kmem_cache_t *names_cachep;
1388
1389
1390kmem_cache_t *filp_cachep;
1391
1392
1393kmem_cache_t *dquot_cachep;
1394
1395EXPORT_SYMBOL(d_genocide);
1396
1397extern void bdev_cache_init(void);
1398extern void cdev_cache_init(void);
1399
1400void __init vfs_caches_init(unsigned long mempages)
1401{
1402 names_cachep = kmem_cache_create("names_cache",
1403 PATH_MAX, 0,
1404 SLAB_HWCACHE_ALIGN, NULL, NULL);
1405 if (!names_cachep)
1406 panic("Cannot create names SLAB cache");
1407
1408 filp_cachep = kmem_cache_create("filp",
1409 sizeof(struct file), 0,
1410 SLAB_HWCACHE_ALIGN, NULL, NULL);
1411 if(!filp_cachep)
1412 panic("Cannot create filp SLAB cache");
1413
1414#if defined (CONFIG_QUOTA)
1415 dquot_cachep = kmem_cache_create("dquot",
1416 sizeof(struct dquot), sizeof(unsigned long) * 4,
1417 SLAB_HWCACHE_ALIGN, NULL, NULL);
1418 if (!dquot_cachep)
1419 panic("Cannot create dquot SLAB cache");
1420#endif
1421
1422 dcache_init(mempages);
1423 inode_init(mempages);
1424 files_init(mempages);
1425 mnt_init(mempages);
1426 bdev_cache_init();
1427 cdev_cache_init();
1428}
1429