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