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