1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/string.h>
18#include <linux/mm.h>
19#include <linux/fs.h>
20#include <linux/malloc.h>
21#include <linux/slab.h>
22#include <linux/init.h>
23
24#include <asm/uaccess.h>
25#include <asm/cache.h>
26
27#define DCACHE_PARANOIA 1
28
29
30
31extern unsigned long num_physpages, page_cache_size;
32extern int inodes_stat[];
33#define nr_inodes (inodes_stat[0])
34
35kmem_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
53struct {
54 int nr_dentry;
55 int nr_unused;
56 int age_limit;
57 int want_pages;
58 int dummy[2];
59} dentry_stat = {0, 0, 45, 0,};
60
61static inline void d_free(struct dentry *dentry)
62{
63 if (dentry->d_op && dentry->d_op->d_release)
64 dentry->d_op->d_release(dentry);
65 if (dname_external(dentry))
66 kfree(dentry->d_name.name);
67 kmem_cache_free(dentry_cache, dentry);
68}
69
70
71
72
73
74static inline int dentry_iput(struct dentry * dentry)
75{
76 struct inode *inode = dentry->d_inode;
77 int ret = 0;
78
79 if (inode) {
80 dentry->d_inode = NULL;
81 list_del(&dentry->d_alias);
82 INIT_LIST_HEAD(&dentry->d_alias);
83 ret = inode->i_count == 1;
84 if (dentry->d_op && dentry->d_op->d_iput)
85 dentry->d_op->d_iput(dentry, inode);
86 else
87 iput(inode);
88 }
89
90 return ret;
91}
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109void dput(struct dentry *dentry)
110{
111 int count;
112
113 if (!dentry)
114 return;
115
116repeat:
117 count = dentry->d_count - 1;
118 if (count != 0)
119 goto out;
120
121
122
123
124
125
126 if (dentry->d_op && dentry->d_op->d_delete) {
127 dentry->d_op->d_delete(dentry);
128
129 count = dentry->d_count - 1;
130 if (count != 0)
131 goto out;
132 }
133
134 if (!list_empty(&dentry->d_lru)) {
135 dentry_stat.nr_unused--;
136 list_del(&dentry->d_lru);
137 }
138 if (list_empty(&dentry->d_hash)) {
139 struct dentry * parent;
140
141 list_del(&dentry->d_child);
142 dentry_iput(dentry);
143 parent = dentry->d_parent;
144 d_free(dentry);
145 if (dentry == parent)
146 return;
147 dentry = parent;
148 goto repeat;
149 }
150 list_add(&dentry->d_lru, &dentry_unused);
151 dentry_stat.nr_unused++;
152
153
154
155 dentry->d_reftime = jiffies;
156
157out:
158 if (count >= 0) {
159 dentry->d_count = count;
160 return;
161 }
162
163 printk(KERN_CRIT "Negative d_count (%d) for %s/%s\n",
164 count,
165 dentry->d_parent->d_name.name,
166 dentry->d_name.name);
167 *(int *)0 = 0;
168}
169
170
171
172
173
174
175int d_invalidate(struct dentry * dentry)
176{
177
178
179
180 if (list_empty(&dentry->d_hash))
181 return 0;
182
183
184
185
186 if (!list_empty(&dentry->d_subdirs)) {
187 shrink_dcache_parent(dentry);
188 }
189
190
191
192
193
194
195
196
197
198
199
200 if (dentry->d_count > 1) {
201 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
202 return -EBUSY;
203 }
204
205 d_drop(dentry);
206 return 0;
207}
208
209
210
211
212
213
214static inline int prune_one_dentry(struct dentry * dentry)
215{
216 struct dentry * parent;
217 int ret;
218
219 list_del(&dentry->d_hash);
220 list_del(&dentry->d_child);
221 ret = dentry_iput(dentry);
222 parent = dentry->d_parent;
223 d_free(dentry);
224 if (parent != dentry)
225 dput(parent);
226
227 return ret;
228}
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246int prune_dcache(int d_nr, int i_nr)
247{
248 int __i_nr = i_nr;
249
250 for (;;) {
251 struct dentry *dentry;
252 struct list_head *tmp = dentry_unused.prev;
253
254 if (tmp == &dentry_unused)
255 break;
256 list_del(tmp);
257 dentry = list_entry(tmp, struct dentry, d_lru);
258 if (dentry->d_flags & DCACHE_REFERENCED) {
259 dentry->d_flags &= ~DCACHE_REFERENCED;
260 list_add(&dentry->d_lru, &dentry_unused);
261 continue;
262 }
263 dentry_stat.nr_unused--;
264 INIT_LIST_HEAD(tmp);
265 if (!dentry->d_count) {
266 i_nr -= prune_one_dentry(dentry);
267 if (!i_nr)
268 break;
269 if (!--d_nr)
270 break;
271 }
272 }
273
274 return __i_nr - i_nr;
275}
276
277
278
279
280
281
282
283
284
285
286
287
288
289void shrink_dcache_sb(struct super_block * sb)
290{
291 struct list_head *tmp, *next;
292 struct dentry *dentry;
293
294
295
296
297
298 next = dentry_unused.next;
299 while (next != &dentry_unused) {
300 tmp = next;
301 next = tmp->next;
302 dentry = list_entry(tmp, struct dentry, d_lru);
303 if (dentry->d_sb != sb)
304 continue;
305 list_del(tmp);
306 list_add(tmp, &dentry_unused);
307 }
308
309
310
311
312repeat:
313 next = dentry_unused.next;
314 while (next != &dentry_unused) {
315 tmp = next;
316 next = tmp->next;
317 dentry = list_entry(tmp, struct dentry, d_lru);
318 if (dentry->d_sb != sb)
319 continue;
320 if (dentry->d_count)
321 continue;
322 dentry_stat.nr_unused--;
323 list_del(tmp);
324 INIT_LIST_HEAD(tmp);
325 prune_one_dentry(dentry);
326 goto repeat;
327 }
328}
329
330
331
332
333
334
335int is_root_busy(struct dentry *root)
336{
337 struct dentry *this_parent = root;
338 struct list_head *next;
339 int count = root->d_count;
340
341repeat:
342 next = this_parent->d_subdirs.next;
343resume:
344 while (next != &this_parent->d_subdirs) {
345 struct list_head *tmp = next;
346 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
347 next = tmp->next;
348
349 count += (dentry->d_count - 1);
350 if (!list_empty(&dentry->d_subdirs)) {
351 this_parent = dentry;
352 goto repeat;
353 }
354
355 if (dentry->d_count)
356 return 1;
357 }
358
359
360
361 if (this_parent != root) {
362 next = this_parent->d_child.next;
363 this_parent = this_parent->d_parent;
364 goto resume;
365 }
366 return (count > 1);
367}
368
369
370
371
372
373
374int have_submounts(struct dentry *parent)
375{
376 struct dentry *this_parent = parent;
377 struct list_head *next;
378
379 if (parent->d_mounts != parent)
380 return 1;
381repeat:
382 next = this_parent->d_subdirs.next;
383resume:
384 while (next != &this_parent->d_subdirs) {
385 struct list_head *tmp = next;
386 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
387 next = tmp->next;
388
389 if (dentry->d_mounts != dentry)
390 return 1;
391 if (!list_empty(&dentry->d_subdirs)) {
392 this_parent = dentry;
393 goto repeat;
394 }
395 }
396
397
398
399 if (this_parent != parent) {
400 next = this_parent->d_child.next;
401 this_parent = this_parent->d_parent;
402 goto resume;
403 }
404 return 0;
405}
406
407
408
409
410
411
412
413
414static int select_parent(struct dentry * parent)
415{
416 struct dentry *this_parent = parent;
417 struct list_head *next;
418 int found = 0;
419
420repeat:
421 next = this_parent->d_subdirs.next;
422resume:
423 while (next != &this_parent->d_subdirs) {
424 struct list_head *tmp = next;
425 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
426 next = tmp->next;
427 if (!dentry->d_count) {
428 list_del(&dentry->d_lru);
429 list_add(&dentry->d_lru, dentry_unused.prev);
430 found++;
431 }
432
433
434
435 if (!list_empty(&dentry->d_subdirs)) {
436 this_parent = dentry;
437#ifdef DCACHE_DEBUG
438printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
439dentry->d_parent->d_name.name, dentry->d_name.name, found);
440#endif
441 goto repeat;
442 }
443 }
444
445
446
447 if (this_parent != parent) {
448 next = this_parent->d_child.next;
449 this_parent = this_parent->d_parent;
450#ifdef DCACHE_DEBUG
451printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
452this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
453#endif
454 goto resume;
455 }
456 return found;
457}
458
459
460
461
462void shrink_dcache_parent(struct dentry * parent)
463{
464 int found;
465
466 while ((found = select_parent(parent)) != 0)
467 prune_dcache(found, -1);
468}
469
470
471
472
473
474
475
476
477
478
479
480
481void shrink_dcache_memory(int priority, unsigned int gfp_mask)
482{
483 if (gfp_mask & __GFP_IO && !current->fs_locks) {
484 int count = 0;
485 if (priority > 1)
486 count = dentry_stat.nr_unused / priority;
487 prune_dcache(count, -1);
488 }
489}
490
491#define NAME_ALLOC_LEN(len) ((len+16) & ~15)
492
493struct dentry * d_alloc(struct dentry * parent, const struct qstr *name)
494{
495 char * str;
496 struct dentry *dentry;
497
498 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
499 if (!dentry)
500 return NULL;
501
502 if (name->len > DNAME_INLINE_LEN-1) {
503 str = kmalloc(NAME_ALLOC_LEN(name->len), GFP_KERNEL);
504 if (!str) {
505 kmem_cache_free(dentry_cache, dentry);
506 return NULL;
507 }
508 } else
509 str = dentry->d_iname;
510
511 memcpy(str, name->name, name->len);
512 str[name->len] = 0;
513
514 dentry->d_count = 1;
515 dentry->d_flags = 0;
516 dentry->d_inode = NULL;
517 dentry->d_parent = NULL;
518 dentry->d_sb = NULL;
519 if (parent) {
520 dentry->d_parent = dget(parent);
521 dentry->d_sb = parent->d_sb;
522 list_add(&dentry->d_child, &parent->d_subdirs);
523 } else
524 INIT_LIST_HEAD(&dentry->d_child);
525
526 dentry->d_mounts = dentry;
527 dentry->d_covers = dentry;
528 INIT_LIST_HEAD(&dentry->d_hash);
529 INIT_LIST_HEAD(&dentry->d_lru);
530 INIT_LIST_HEAD(&dentry->d_subdirs);
531 INIT_LIST_HEAD(&dentry->d_alias);
532
533 dentry->d_name.name = str;
534 dentry->d_name.len = name->len;
535 dentry->d_name.hash = name->hash;
536 dentry->d_op = NULL;
537 dentry->d_fsdata = NULL;
538 return dentry;
539}
540
541
542
543
544
545
546
547
548
549
550
551void d_instantiate(struct dentry *entry, struct inode * inode)
552{
553 if (inode)
554 list_add(&entry->d_alias, &inode->i_dentry);
555 entry->d_inode = inode;
556}
557
558struct dentry * d_alloc_root(struct inode * root_inode, struct dentry *old_root)
559{
560 struct dentry *res = NULL;
561
562 if (root_inode) {
563 res = d_alloc(NULL, &(const struct qstr) { "/", 1, 0 });
564 if (res) {
565 res->d_sb = root_inode->i_sb;
566 res->d_parent = res;
567 d_instantiate(res, root_inode);
568 }
569 }
570 return res;
571}
572
573static inline struct list_head * d_hash(struct dentry * parent, unsigned long hash)
574{
575 hash += (unsigned long) parent / L1_CACHE_BYTES;
576 hash = hash ^ (hash >> D_HASHBITS);
577 return dentry_hashtable + (hash & D_HASHMASK);
578}
579
580struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
581{
582 unsigned int len = name->len;
583 unsigned int hash = name->hash;
584 const unsigned char *str = name->name;
585 struct list_head *head = d_hash(parent,hash);
586 struct list_head *tmp = head->next;
587
588 for (;;) {
589 struct dentry * dentry = list_entry(tmp, struct dentry, d_hash);
590 if (tmp == head)
591 break;
592 tmp = tmp->next;
593 if (dentry->d_name.hash != hash)
594 continue;
595 if (dentry->d_parent != parent)
596 continue;
597 if (parent->d_op && parent->d_op->d_compare) {
598 if (parent->d_op->d_compare(parent, &dentry->d_name, name))
599 continue;
600 } else {
601 if (dentry->d_name.len != len)
602 continue;
603 if (memcmp(dentry->d_name.name, str, len))
604 continue;
605 }
606 dentry->d_flags |= DCACHE_REFERENCED;
607 return dget(dentry);
608 }
609 return NULL;
610}
611
612
613
614
615
616
617
618
619
620
621
622
623int d_validate(struct dentry *dentry, struct dentry *dparent,
624 unsigned int hash, unsigned int len)
625{
626 struct list_head *base, *lhp;
627 int valid = 1;
628
629 if (dentry != dparent) {
630 base = d_hash(dparent, hash);
631 lhp = base;
632 while ((lhp = lhp->next) != base) {
633 if (dentry == list_entry(lhp, struct dentry, d_hash))
634 goto out;
635 }
636 } else {
637
638
639
640
641 struct super_block *sb = sb_entry(super_blocks.next);
642
643 for (; sb != sb_entry(&super_blocks);
644 sb = sb_entry(sb->s_list.next)) {
645 if (!sb->s_dev)
646 continue;
647 if (sb->s_root == dentry)
648 goto out;
649 }
650 }
651 valid = 0;
652out:
653 return valid;
654}
655
656
657
658
659
660
661
662
663
664
665
666
667
668void d_delete(struct dentry * dentry)
669{
670
671
672
673 if (dentry->d_count == 1) {
674 dentry_iput(dentry);
675 return;
676 }
677
678
679
680
681
682 d_drop(dentry);
683}
684
685void d_rehash(struct dentry * entry)
686{
687 struct dentry * parent = entry->d_parent;
688
689 list_add(&entry->d_hash, d_hash(parent, entry->d_name.hash));
690}
691
692#define do_switch(x,y) do { \
693 __typeof__ (x) __tmp = x; \
694 x = y; y = __tmp; } while (0)
695
696
697
698
699
700
701
702
703
704
705
706
707static inline void switch_names(struct dentry * dentry, struct dentry * target)
708{
709 const unsigned char *old_name, *new_name;
710
711 memcpy(dentry->d_iname, target->d_iname, DNAME_INLINE_LEN);
712 old_name = target->d_name.name;
713 new_name = dentry->d_name.name;
714 if (old_name == target->d_iname)
715 old_name = dentry->d_iname;
716 if (new_name == dentry->d_iname)
717 new_name = target->d_iname;
718 target->d_name.name = new_name;
719 dentry->d_name.name = old_name;
720}
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737void d_move(struct dentry * dentry, struct dentry * target)
738{
739 if (!dentry->d_inode)
740 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
741
742
743 list_del(&dentry->d_hash);
744 list_add(&dentry->d_hash, &target->d_hash);
745
746
747 list_del(&target->d_hash);
748 INIT_LIST_HEAD(&target->d_hash);
749
750 list_del(&dentry->d_child);
751 list_del(&target->d_child);
752
753
754 switch_names(dentry, target);
755 do_switch(dentry->d_parent, target->d_parent);
756 do_switch(dentry->d_name.len, target->d_name.len);
757 do_switch(dentry->d_name.hash, target->d_name.hash);
758
759
760 list_add(&target->d_child, &target->d_parent->d_subdirs);
761 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
762}
763
764
765
766
767static char * d_path_error(struct dentry *dentry,
768 char *buffer, int buflen, int *error)
769{
770 char * end = buffer+buflen;
771 char * retval;
772 struct dentry * root = current->fs->root;
773
774 *error = 0;
775
776 *--end = '\0';
777 buflen--;
778 if (dentry->d_parent != dentry && list_empty(&dentry->d_hash)) {
779 buflen -= 10;
780 end -= 10;
781 memcpy(end, " (deleted)", 10);
782 }
783
784
785 retval = end-1;
786 *retval = '/';
787
788 for (;;) {
789 struct dentry * parent;
790 int namelen;
791
792 if (dentry == root)
793 break;
794 dentry = dentry->d_covers;
795 parent = dentry->d_parent;
796 if (dentry == parent)
797 break;
798 namelen = dentry->d_name.len;
799 buflen -= namelen + 1;
800 if (buflen < 0) {
801 *error = -ENAMETOOLONG;
802 break;
803 }
804 end -= namelen;
805 memcpy(end, dentry->d_name.name, namelen);
806 *--end = '/';
807 retval = end;
808 dentry = parent;
809 }
810 return retval;
811}
812
813char * d_path(struct dentry *dentry, char *buffer, int buflen)
814{
815 int error;
816
817 return d_path_error(dentry, buffer, buflen, &error);
818}
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838asmlinkage int sys_getcwd(char *buf, unsigned long size)
839{
840 int error;
841 struct dentry *pwd = current->fs->pwd;
842
843 error = -ENOENT;
844
845 if (pwd->d_parent == pwd || !list_empty(&pwd->d_hash)) {
846 char *page = (char *) __get_free_page(GFP_USER);
847 error = -ENOMEM;
848 if (page) {
849 unsigned long len;
850 char * cwd;
851
852 cwd = d_path_error(pwd, page, PAGE_SIZE, &error);
853 if (!error) {
854 error = -ERANGE;
855 len = PAGE_SIZE + page - cwd;
856 if (len <= size) {
857 error = len;
858 if (copy_to_user(buf, cwd, len))
859 error = -EFAULT;
860 }
861 }
862 free_page((unsigned long) page);
863 }
864 }
865 return error;
866}
867
868
869
870
871
872
873int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
874{
875 int result;
876
877 result = 0;
878 for (;;) {
879 if (new_dentry != old_dentry) {
880 struct dentry * parent = new_dentry->d_parent;
881 if (parent == new_dentry)
882 break;
883 new_dentry = parent;
884 continue;
885 }
886 result = 1;
887 break;
888 }
889 return result;
890}
891
892
893
894
895
896
897
898
899
900ino_t find_inode_number(struct dentry *dir, struct qstr *name)
901{
902 struct dentry * dentry;
903 ino_t ino = 0;
904
905
906
907
908
909
910 name->hash = full_name_hash(name->name, name->len);
911 if (dir->d_op && dir->d_op->d_hash)
912 {
913 if (dir->d_op->d_hash(dir, name) != 0)
914 goto out;
915 }
916
917 dentry = d_lookup(dir, name);
918 if (dentry)
919 {
920 if (dentry->d_inode)
921 ino = dentry->d_inode->i_ino;
922 dput(dentry);
923 }
924out:
925 return ino;
926}
927
928void __init dcache_init(void)
929{
930 int i, order;
931 struct list_head *d;
932 unsigned int nr_hash;
933 unsigned long memory_size;
934
935
936
937
938
939
940
941
942
943 dentry_cache = kmem_cache_create("dentry_cache",
944 sizeof(struct dentry),
945 0,
946 SLAB_HWCACHE_ALIGN,
947 NULL, NULL);
948 if (!dentry_cache)
949 panic("Cannot create dentry cache");
950
951 memory_size = num_physpages << PAGE_SHIFT;
952 memory_size >>= 13;
953 memory_size *= 2 * sizeof(void *);
954 for (order = 0; ((1UL << order) << PAGE_SHIFT) < memory_size; order++);
955
956 do {
957 unsigned long tmp;
958
959 nr_hash = (1UL << order) * PAGE_SIZE /
960 sizeof(struct list_head);
961 d_hash_mask = (nr_hash - 1);
962
963 tmp = nr_hash;
964 d_hash_shift = 0;
965 while((tmp >>= 1UL) != 0UL)
966 d_hash_shift++;
967
968 dentry_hashtable = (struct list_head *)
969 __get_free_pages(GFP_ATOMIC, order);
970 } while(dentry_hashtable == NULL && --order >= 0);
971 printk("Dentry hash table entries: %d (order %d, %ldk)\n",
972 nr_hash, order, (1UL<<order) * PAGE_SIZE / 1024);
973
974 if (!dentry_hashtable)
975 panic("Failed to allocate dcache hash table\n");
976
977 d = dentry_hashtable;
978 i = nr_hash;
979 do {
980 INIT_LIST_HEAD(d);
981 d++;
982 i--;
983 } while (i);
984}
985