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