1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#include <linux/syscalls.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fdtable.h>
21#include <linux/fs.h>
22#include <linux/fsnotify.h>
23#include <linux/slab.h>
24#include <linux/init.h>
25#include <linux/hash.h>
26#include <linux/cache.h>
27#include <linux/module.h>
28#include <linux/mount.h>
29#include <linux/file.h>
30#include <asm/uaccess.h>
31#include <linux/security.h>
32#include <linux/seqlock.h>
33#include <linux/swap.h>
34#include <linux/bootmem.h>
35#include "internal.h"
36
37int sysctl_vfs_cache_pressure __read_mostly = 100;
38EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
39
40 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
41__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
42
43EXPORT_SYMBOL(dcache_lock);
44
45static struct kmem_cache *dentry_cache __read_mostly;
46
47#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
48
49
50
51
52
53
54
55
56
57#define D_HASHBITS d_hash_shift
58#define D_HASHMASK d_hash_mask
59
60static unsigned int d_hash_mask __read_mostly;
61static unsigned int d_hash_shift __read_mostly;
62static struct hlist_head *dentry_hashtable __read_mostly;
63
64
65struct dentry_stat_t dentry_stat = {
66 .age_limit = 45,
67};
68
69static void __d_free(struct dentry *dentry)
70{
71 WARN_ON(!list_empty(&dentry->d_alias));
72 if (dname_external(dentry))
73 kfree(dentry->d_name.name);
74 kmem_cache_free(dentry_cache, dentry);
75}
76
77static void d_callback(struct rcu_head *head)
78{
79 struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
80 __d_free(dentry);
81}
82
83
84
85
86
87static void d_free(struct dentry *dentry)
88{
89 if (dentry->d_op && dentry->d_op->d_release)
90 dentry->d_op->d_release(dentry);
91
92 if (hlist_unhashed(&dentry->d_hash))
93 __d_free(dentry);
94 else
95 call_rcu(&dentry->d_u.d_rcu, d_callback);
96}
97
98
99
100
101
102static void dentry_iput(struct dentry * dentry)
103 __releases(dentry->d_lock)
104 __releases(dcache_lock)
105{
106 struct inode *inode = dentry->d_inode;
107 if (inode) {
108 dentry->d_inode = NULL;
109 list_del_init(&dentry->d_alias);
110 spin_unlock(&dentry->d_lock);
111 spin_unlock(&dcache_lock);
112 if (!inode->i_nlink)
113 fsnotify_inoderemove(inode);
114 if (dentry->d_op && dentry->d_op->d_iput)
115 dentry->d_op->d_iput(dentry, inode);
116 else
117 iput(inode);
118 } else {
119 spin_unlock(&dentry->d_lock);
120 spin_unlock(&dcache_lock);
121 }
122}
123
124
125
126
127static void dentry_lru_add(struct dentry *dentry)
128{
129 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
130 dentry->d_sb->s_nr_dentry_unused++;
131 dentry_stat.nr_unused++;
132}
133
134static void dentry_lru_add_tail(struct dentry *dentry)
135{
136 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
137 dentry->d_sb->s_nr_dentry_unused++;
138 dentry_stat.nr_unused++;
139}
140
141static void dentry_lru_del(struct dentry *dentry)
142{
143 if (!list_empty(&dentry->d_lru)) {
144 list_del(&dentry->d_lru);
145 dentry->d_sb->s_nr_dentry_unused--;
146 dentry_stat.nr_unused--;
147 }
148}
149
150static void dentry_lru_del_init(struct dentry *dentry)
151{
152 if (likely(!list_empty(&dentry->d_lru))) {
153 list_del_init(&dentry->d_lru);
154 dentry->d_sb->s_nr_dentry_unused--;
155 dentry_stat.nr_unused--;
156 }
157}
158
159
160
161
162
163
164
165
166
167static struct dentry *d_kill(struct dentry *dentry)
168 __releases(dentry->d_lock)
169 __releases(dcache_lock)
170{
171 struct dentry *parent;
172
173 list_del(&dentry->d_u.d_child);
174 dentry_stat.nr_dentry--;
175
176 dentry_iput(dentry);
177 if (IS_ROOT(dentry))
178 parent = NULL;
179 else
180 parent = dentry->d_parent;
181 d_free(dentry);
182 return parent;
183}
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214void dput(struct dentry *dentry)
215{
216 if (!dentry)
217 return;
218
219repeat:
220 if (atomic_read(&dentry->d_count) == 1)
221 might_sleep();
222 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
223 return;
224
225 spin_lock(&dentry->d_lock);
226 if (atomic_read(&dentry->d_count)) {
227 spin_unlock(&dentry->d_lock);
228 spin_unlock(&dcache_lock);
229 return;
230 }
231
232
233
234
235 if (dentry->d_op && dentry->d_op->d_delete) {
236 if (dentry->d_op->d_delete(dentry))
237 goto unhash_it;
238 }
239
240 if (d_unhashed(dentry))
241 goto kill_it;
242 if (list_empty(&dentry->d_lru)) {
243 dentry->d_flags |= DCACHE_REFERENCED;
244 dentry_lru_add(dentry);
245 }
246 spin_unlock(&dentry->d_lock);
247 spin_unlock(&dcache_lock);
248 return;
249
250unhash_it:
251 __d_drop(dentry);
252kill_it:
253
254 dentry_lru_del(dentry);
255 dentry = d_kill(dentry);
256 if (dentry)
257 goto repeat;
258}
259
260
261
262
263
264
265
266
267
268
269
270
271
272int d_invalidate(struct dentry * dentry)
273{
274
275
276
277 spin_lock(&dcache_lock);
278 if (d_unhashed(dentry)) {
279 spin_unlock(&dcache_lock);
280 return 0;
281 }
282
283
284
285
286 if (!list_empty(&dentry->d_subdirs)) {
287 spin_unlock(&dcache_lock);
288 shrink_dcache_parent(dentry);
289 spin_lock(&dcache_lock);
290 }
291
292
293
294
295
296
297
298
299
300
301
302 spin_lock(&dentry->d_lock);
303 if (atomic_read(&dentry->d_count) > 1) {
304 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
305 spin_unlock(&dentry->d_lock);
306 spin_unlock(&dcache_lock);
307 return -EBUSY;
308 }
309 }
310
311 __d_drop(dentry);
312 spin_unlock(&dentry->d_lock);
313 spin_unlock(&dcache_lock);
314 return 0;
315}
316
317
318
319static inline struct dentry * __dget_locked(struct dentry *dentry)
320{
321 atomic_inc(&dentry->d_count);
322 dentry_lru_del_init(dentry);
323 return dentry;
324}
325
326struct dentry * dget_locked(struct dentry *dentry)
327{
328 return __dget_locked(dentry);
329}
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
349{
350 struct list_head *head, *next, *tmp;
351 struct dentry *alias, *discon_alias=NULL;
352
353 head = &inode->i_dentry;
354 next = inode->i_dentry.next;
355 while (next != head) {
356 tmp = next;
357 next = tmp->next;
358 prefetch(next);
359 alias = list_entry(tmp, struct dentry, d_alias);
360 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
361 if (IS_ROOT(alias) &&
362 (alias->d_flags & DCACHE_DISCONNECTED))
363 discon_alias = alias;
364 else if (!want_discon) {
365 __dget_locked(alias);
366 return alias;
367 }
368 }
369 }
370 if (discon_alias)
371 __dget_locked(discon_alias);
372 return discon_alias;
373}
374
375struct dentry * d_find_alias(struct inode *inode)
376{
377 struct dentry *de = NULL;
378
379 if (!list_empty(&inode->i_dentry)) {
380 spin_lock(&dcache_lock);
381 de = __d_find_alias(inode, 0);
382 spin_unlock(&dcache_lock);
383 }
384 return de;
385}
386
387
388
389
390
391void d_prune_aliases(struct inode *inode)
392{
393 struct dentry *dentry;
394restart:
395 spin_lock(&dcache_lock);
396 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
397 spin_lock(&dentry->d_lock);
398 if (!atomic_read(&dentry->d_count)) {
399 __dget_locked(dentry);
400 __d_drop(dentry);
401 spin_unlock(&dentry->d_lock);
402 spin_unlock(&dcache_lock);
403 dput(dentry);
404 goto restart;
405 }
406 spin_unlock(&dentry->d_lock);
407 }
408 spin_unlock(&dcache_lock);
409}
410
411
412
413
414
415
416
417
418
419static void prune_one_dentry(struct dentry * dentry)
420 __releases(dentry->d_lock)
421 __releases(dcache_lock)
422 __acquires(dcache_lock)
423{
424 __d_drop(dentry);
425 dentry = d_kill(dentry);
426
427
428
429
430
431 spin_lock(&dcache_lock);
432 while (dentry) {
433 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
434 return;
435
436 if (dentry->d_op && dentry->d_op->d_delete)
437 dentry->d_op->d_delete(dentry);
438 dentry_lru_del_init(dentry);
439 __d_drop(dentry);
440 dentry = d_kill(dentry);
441 spin_lock(&dcache_lock);
442 }
443}
444
445
446
447
448
449
450
451
452
453static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
454{
455 LIST_HEAD(referenced);
456 LIST_HEAD(tmp);
457 struct dentry *dentry;
458 int cnt = 0;
459
460 BUG_ON(!sb);
461 BUG_ON((flags & DCACHE_REFERENCED) && count == NULL);
462 spin_lock(&dcache_lock);
463 if (count != NULL)
464
465 cnt = *count;
466restart:
467 if (count == NULL)
468 list_splice_init(&sb->s_dentry_lru, &tmp);
469 else {
470 while (!list_empty(&sb->s_dentry_lru)) {
471 dentry = list_entry(sb->s_dentry_lru.prev,
472 struct dentry, d_lru);
473 BUG_ON(dentry->d_sb != sb);
474
475 spin_lock(&dentry->d_lock);
476
477
478
479
480
481 if ((flags & DCACHE_REFERENCED)
482 && (dentry->d_flags & DCACHE_REFERENCED)) {
483 dentry->d_flags &= ~DCACHE_REFERENCED;
484 list_move_tail(&dentry->d_lru, &referenced);
485 spin_unlock(&dentry->d_lock);
486 } else {
487 list_move_tail(&dentry->d_lru, &tmp);
488 spin_unlock(&dentry->d_lock);
489 cnt--;
490 if (!cnt)
491 break;
492 }
493 cond_resched_lock(&dcache_lock);
494 }
495 }
496 while (!list_empty(&tmp)) {
497 dentry = list_entry(tmp.prev, struct dentry, d_lru);
498 dentry_lru_del_init(dentry);
499 spin_lock(&dentry->d_lock);
500
501
502
503
504
505 if (atomic_read(&dentry->d_count)) {
506 spin_unlock(&dentry->d_lock);
507 continue;
508 }
509 prune_one_dentry(dentry);
510
511 cond_resched_lock(&dcache_lock);
512 }
513 if (count == NULL && !list_empty(&sb->s_dentry_lru))
514 goto restart;
515 if (count != NULL)
516 *count = cnt;
517 if (!list_empty(&referenced))
518 list_splice(&referenced, &sb->s_dentry_lru);
519 spin_unlock(&dcache_lock);
520}
521
522
523
524
525
526
527
528
529
530
531static void prune_dcache(int count)
532{
533 struct super_block *sb;
534 int w_count;
535 int unused = dentry_stat.nr_unused;
536 int prune_ratio;
537 int pruned;
538
539 if (unused == 0 || count == 0)
540 return;
541 spin_lock(&dcache_lock);
542restart:
543 if (count >= unused)
544 prune_ratio = 1;
545 else
546 prune_ratio = unused / count;
547 spin_lock(&sb_lock);
548 list_for_each_entry(sb, &super_blocks, s_list) {
549 if (sb->s_nr_dentry_unused == 0)
550 continue;
551 sb->s_count++;
552
553
554
555
556
557
558
559
560
561 spin_unlock(&sb_lock);
562 if (prune_ratio != 1)
563 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
564 else
565 w_count = sb->s_nr_dentry_unused;
566 pruned = w_count;
567
568
569
570
571
572
573
574 if (down_read_trylock(&sb->s_umount)) {
575 if ((sb->s_root != NULL) &&
576 (!list_empty(&sb->s_dentry_lru))) {
577 spin_unlock(&dcache_lock);
578 __shrink_dcache_sb(sb, &w_count,
579 DCACHE_REFERENCED);
580 pruned -= w_count;
581 spin_lock(&dcache_lock);
582 }
583 up_read(&sb->s_umount);
584 }
585 spin_lock(&sb_lock);
586 count -= pruned;
587
588
589
590
591 if (__put_super_and_need_restart(sb) && count > 0) {
592 spin_unlock(&sb_lock);
593 goto restart;
594 }
595 }
596 spin_unlock(&sb_lock);
597 spin_unlock(&dcache_lock);
598}
599
600
601
602
603
604
605
606
607
608void shrink_dcache_sb(struct super_block * sb)
609{
610 __shrink_dcache_sb(sb, NULL, 0);
611}
612
613
614
615
616
617
618static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
619{
620 struct dentry *parent;
621 unsigned detached = 0;
622
623 BUG_ON(!IS_ROOT(dentry));
624
625
626 spin_lock(&dcache_lock);
627 dentry_lru_del_init(dentry);
628 __d_drop(dentry);
629 spin_unlock(&dcache_lock);
630
631 for (;;) {
632
633 while (!list_empty(&dentry->d_subdirs)) {
634 struct dentry *loop;
635
636
637
638 spin_lock(&dcache_lock);
639 list_for_each_entry(loop, &dentry->d_subdirs,
640 d_u.d_child) {
641 dentry_lru_del_init(loop);
642 __d_drop(loop);
643 cond_resched_lock(&dcache_lock);
644 }
645 spin_unlock(&dcache_lock);
646
647
648 dentry = list_entry(dentry->d_subdirs.next,
649 struct dentry, d_u.d_child);
650 }
651
652
653
654 do {
655 struct inode *inode;
656
657 if (atomic_read(&dentry->d_count) != 0) {
658 printk(KERN_ERR
659 "BUG: Dentry %p{i=%lx,n=%s}"
660 " still in use (%d)"
661 " [unmount of %s %s]\n",
662 dentry,
663 dentry->d_inode ?
664 dentry->d_inode->i_ino : 0UL,
665 dentry->d_name.name,
666 atomic_read(&dentry->d_count),
667 dentry->d_sb->s_type->name,
668 dentry->d_sb->s_id);
669 BUG();
670 }
671
672 if (IS_ROOT(dentry))
673 parent = NULL;
674 else {
675 parent = dentry->d_parent;
676 atomic_dec(&parent->d_count);
677 }
678
679 list_del(&dentry->d_u.d_child);
680 detached++;
681
682 inode = dentry->d_inode;
683 if (inode) {
684 dentry->d_inode = NULL;
685 list_del_init(&dentry->d_alias);
686 if (dentry->d_op && dentry->d_op->d_iput)
687 dentry->d_op->d_iput(dentry, inode);
688 else
689 iput(inode);
690 }
691
692 d_free(dentry);
693
694
695
696
697 if (!parent)
698 goto out;
699
700 dentry = parent;
701
702 } while (list_empty(&dentry->d_subdirs));
703
704 dentry = list_entry(dentry->d_subdirs.next,
705 struct dentry, d_u.d_child);
706 }
707out:
708
709 spin_lock(&dcache_lock);
710 dentry_stat.nr_dentry -= detached;
711 spin_unlock(&dcache_lock);
712}
713
714
715
716
717
718
719
720
721
722
723
724
725void shrink_dcache_for_umount(struct super_block *sb)
726{
727 struct dentry *dentry;
728
729 if (down_read_trylock(&sb->s_umount))
730 BUG();
731
732 dentry = sb->s_root;
733 sb->s_root = NULL;
734 atomic_dec(&dentry->d_count);
735 shrink_dcache_for_umount_subtree(dentry);
736
737 while (!hlist_empty(&sb->s_anon)) {
738 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
739 shrink_dcache_for_umount_subtree(dentry);
740 }
741}
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757int have_submounts(struct dentry *parent)
758{
759 struct dentry *this_parent = parent;
760 struct list_head *next;
761
762 spin_lock(&dcache_lock);
763 if (d_mountpoint(parent))
764 goto positive;
765repeat:
766 next = this_parent->d_subdirs.next;
767resume:
768 while (next != &this_parent->d_subdirs) {
769 struct list_head *tmp = next;
770 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
771 next = tmp->next;
772
773 if (d_mountpoint(dentry))
774 goto positive;
775 if (!list_empty(&dentry->d_subdirs)) {
776 this_parent = dentry;
777 goto repeat;
778 }
779 }
780
781
782
783 if (this_parent != parent) {
784 next = this_parent->d_u.d_child.next;
785 this_parent = this_parent->d_parent;
786 goto resume;
787 }
788 spin_unlock(&dcache_lock);
789 return 0;
790positive:
791 spin_unlock(&dcache_lock);
792 return 1;
793}
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809static int select_parent(struct dentry * parent)
810{
811 struct dentry *this_parent = parent;
812 struct list_head *next;
813 int found = 0;
814
815 spin_lock(&dcache_lock);
816repeat:
817 next = this_parent->d_subdirs.next;
818resume:
819 while (next != &this_parent->d_subdirs) {
820 struct list_head *tmp = next;
821 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
822 next = tmp->next;
823
824 dentry_lru_del_init(dentry);
825
826
827
828
829 if (!atomic_read(&dentry->d_count)) {
830 dentry_lru_add_tail(dentry);
831 found++;
832 }
833
834
835
836
837
838
839 if (found && need_resched())
840 goto out;
841
842
843
844
845 if (!list_empty(&dentry->d_subdirs)) {
846 this_parent = dentry;
847 goto repeat;
848 }
849 }
850
851
852
853 if (this_parent != parent) {
854 next = this_parent->d_u.d_child.next;
855 this_parent = this_parent->d_parent;
856 goto resume;
857 }
858out:
859 spin_unlock(&dcache_lock);
860 return found;
861}
862
863
864
865
866
867
868
869
870void shrink_dcache_parent(struct dentry * parent)
871{
872 struct super_block *sb = parent->d_sb;
873 int found;
874
875 while ((found = select_parent(parent)) != 0)
876 __shrink_dcache_sb(sb, &found, 0);
877}
878
879
880
881
882
883
884
885
886
887
888
889
890
891static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
892{
893 if (nr) {
894 if (!(gfp_mask & __GFP_FS))
895 return -1;
896 prune_dcache(nr);
897 }
898 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
899}
900
901static struct shrinker dcache_shrinker = {
902 .shrink = shrink_dcache_memory,
903 .seeks = DEFAULT_SEEKS,
904};
905
906
907
908
909
910
911
912
913
914
915
916struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
917{
918 struct dentry *dentry;
919 char *dname;
920
921 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
922 if (!dentry)
923 return NULL;
924
925 if (name->len > DNAME_INLINE_LEN-1) {
926 dname = kmalloc(name->len + 1, GFP_KERNEL);
927 if (!dname) {
928 kmem_cache_free(dentry_cache, dentry);
929 return NULL;
930 }
931 } else {
932 dname = dentry->d_iname;
933 }
934 dentry->d_name.name = dname;
935
936 dentry->d_name.len = name->len;
937 dentry->d_name.hash = name->hash;
938 memcpy(dname, name->name, name->len);
939 dname[name->len] = 0;
940
941 atomic_set(&dentry->d_count, 1);
942 dentry->d_flags = DCACHE_UNHASHED;
943 spin_lock_init(&dentry->d_lock);
944 dentry->d_inode = NULL;
945 dentry->d_parent = NULL;
946 dentry->d_sb = NULL;
947 dentry->d_op = NULL;
948 dentry->d_fsdata = NULL;
949 dentry->d_mounted = 0;
950 INIT_HLIST_NODE(&dentry->d_hash);
951 INIT_LIST_HEAD(&dentry->d_lru);
952 INIT_LIST_HEAD(&dentry->d_subdirs);
953 INIT_LIST_HEAD(&dentry->d_alias);
954
955 if (parent) {
956 dentry->d_parent = dget(parent);
957 dentry->d_sb = parent->d_sb;
958 } else {
959 INIT_LIST_HEAD(&dentry->d_u.d_child);
960 }
961
962 spin_lock(&dcache_lock);
963 if (parent)
964 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
965 dentry_stat.nr_dentry++;
966 spin_unlock(&dcache_lock);
967
968 return dentry;
969}
970
971struct dentry *d_alloc_name(struct dentry *parent, const char *name)
972{
973 struct qstr q;
974
975 q.name = name;
976 q.len = strlen(name);
977 q.hash = full_name_hash(q.name, q.len);
978 return d_alloc(parent, &q);
979}
980
981
982static void __d_instantiate(struct dentry *dentry, struct inode *inode)
983{
984 if (inode)
985 list_add(&dentry->d_alias, &inode->i_dentry);
986 dentry->d_inode = inode;
987 fsnotify_d_instantiate(dentry, inode);
988}
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005void d_instantiate(struct dentry *entry, struct inode * inode)
1006{
1007 BUG_ON(!list_empty(&entry->d_alias));
1008 spin_lock(&dcache_lock);
1009 __d_instantiate(entry, inode);
1010 spin_unlock(&dcache_lock);
1011 security_d_instantiate(entry, inode);
1012}
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030static struct dentry *__d_instantiate_unique(struct dentry *entry,
1031 struct inode *inode)
1032{
1033 struct dentry *alias;
1034 int len = entry->d_name.len;
1035 const char *name = entry->d_name.name;
1036 unsigned int hash = entry->d_name.hash;
1037
1038 if (!inode) {
1039 __d_instantiate(entry, NULL);
1040 return NULL;
1041 }
1042
1043 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1044 struct qstr *qstr = &alias->d_name;
1045
1046 if (qstr->hash != hash)
1047 continue;
1048 if (alias->d_parent != entry->d_parent)
1049 continue;
1050 if (qstr->len != len)
1051 continue;
1052 if (memcmp(qstr->name, name, len))
1053 continue;
1054 dget_locked(alias);
1055 return alias;
1056 }
1057
1058 __d_instantiate(entry, inode);
1059 return NULL;
1060}
1061
1062struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1063{
1064 struct dentry *result;
1065
1066 BUG_ON(!list_empty(&entry->d_alias));
1067
1068 spin_lock(&dcache_lock);
1069 result = __d_instantiate_unique(entry, inode);
1070 spin_unlock(&dcache_lock);
1071
1072 if (!result) {
1073 security_d_instantiate(entry, inode);
1074 return NULL;
1075 }
1076
1077 BUG_ON(!d_unhashed(result));
1078 iput(inode);
1079 return result;
1080}
1081
1082EXPORT_SYMBOL(d_instantiate_unique);
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093struct dentry * d_alloc_root(struct inode * root_inode)
1094{
1095 struct dentry *res = NULL;
1096
1097 if (root_inode) {
1098 static const struct qstr name = { .name = "/", .len = 1 };
1099
1100 res = d_alloc(NULL, &name);
1101 if (res) {
1102 res->d_sb = root_inode->i_sb;
1103 res->d_parent = res;
1104 d_instantiate(res, root_inode);
1105 }
1106 }
1107 return res;
1108}
1109
1110static inline struct hlist_head *d_hash(struct dentry *parent,
1111 unsigned long hash)
1112{
1113 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1114 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1115 return dentry_hashtable + (hash & D_HASHMASK);
1116}
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136struct dentry *d_obtain_alias(struct inode *inode)
1137{
1138 static const struct qstr anonstring = { .name = "" };
1139 struct dentry *tmp;
1140 struct dentry *res;
1141
1142 if (!inode)
1143 return ERR_PTR(-ESTALE);
1144 if (IS_ERR(inode))
1145 return ERR_CAST(inode);
1146
1147 res = d_find_alias(inode);
1148 if (res)
1149 goto out_iput;
1150
1151 tmp = d_alloc(NULL, &anonstring);
1152 if (!tmp) {
1153 res = ERR_PTR(-ENOMEM);
1154 goto out_iput;
1155 }
1156 tmp->d_parent = tmp;
1157
1158 spin_lock(&dcache_lock);
1159 res = __d_find_alias(inode, 0);
1160 if (res) {
1161 spin_unlock(&dcache_lock);
1162 dput(tmp);
1163 goto out_iput;
1164 }
1165
1166
1167 spin_lock(&tmp->d_lock);
1168 tmp->d_sb = inode->i_sb;
1169 tmp->d_inode = inode;
1170 tmp->d_flags |= DCACHE_DISCONNECTED;
1171 tmp->d_flags &= ~DCACHE_UNHASHED;
1172 list_add(&tmp->d_alias, &inode->i_dentry);
1173 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1174 spin_unlock(&tmp->d_lock);
1175
1176 spin_unlock(&dcache_lock);
1177 return tmp;
1178
1179 out_iput:
1180 iput(inode);
1181 return res;
1182}
1183EXPORT_SYMBOL(d_obtain_alias);
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1202{
1203 struct dentry *new = NULL;
1204
1205 if (inode && S_ISDIR(inode->i_mode)) {
1206 spin_lock(&dcache_lock);
1207 new = __d_find_alias(inode, 1);
1208 if (new) {
1209 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1210 spin_unlock(&dcache_lock);
1211 security_d_instantiate(new, inode);
1212 d_rehash(dentry);
1213 d_move(new, dentry);
1214 iput(inode);
1215 } else {
1216
1217 __d_instantiate(dentry, inode);
1218 spin_unlock(&dcache_lock);
1219 security_d_instantiate(dentry, inode);
1220 d_rehash(dentry);
1221 }
1222 } else
1223 d_add(dentry, inode);
1224 return new;
1225}
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
1244 struct qstr *name)
1245{
1246 int error;
1247 struct dentry *found;
1248 struct dentry *new;
1249
1250
1251 found = d_hash_and_lookup(dentry->d_parent, name);
1252
1253 if (!found) {
1254 new = d_alloc(dentry->d_parent, name);
1255 if (!new) {
1256 error = -ENOMEM;
1257 goto err_out;
1258 }
1259 found = d_splice_alias(inode, new);
1260 if (found) {
1261 dput(new);
1262 return found;
1263 }
1264 return new;
1265 }
1266
1267 if (found->d_inode) {
1268 if (unlikely(found->d_inode != inode)) {
1269
1270 BUG_ON(!is_bad_inode(inode));
1271 BUG_ON(!is_bad_inode(found->d_inode));
1272 }
1273
1274
1275
1276
1277
1278
1279
1280 iput(inode);
1281 return found;
1282 }
1283
1284
1285
1286
1287
1288 if (!S_ISDIR(inode->i_mode)) {
1289
1290 d_instantiate(found, inode);
1291 return found;
1292 }
1293 spin_lock(&dcache_lock);
1294 if (list_empty(&inode->i_dentry)) {
1295
1296
1297
1298
1299
1300 __d_instantiate(found, inode);
1301 spin_unlock(&dcache_lock);
1302 security_d_instantiate(found, inode);
1303 return found;
1304 }
1305
1306
1307
1308
1309 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1310 dget_locked(new);
1311 spin_unlock(&dcache_lock);
1312
1313 security_d_instantiate(found, inode);
1314
1315 d_move(new, found);
1316
1317 iput(inode);
1318
1319 dput(found);
1320
1321 return new;
1322
1323err_out:
1324 iput(inode);
1325 return ERR_PTR(error);
1326}
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1358{
1359 struct dentry * dentry = NULL;
1360 unsigned long seq;
1361
1362 do {
1363 seq = read_seqbegin(&rename_lock);
1364 dentry = __d_lookup(parent, name);
1365 if (dentry)
1366 break;
1367 } while (read_seqretry(&rename_lock, seq));
1368 return dentry;
1369}
1370
1371struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1372{
1373 unsigned int len = name->len;
1374 unsigned int hash = name->hash;
1375 const unsigned char *str = name->name;
1376 struct hlist_head *head = d_hash(parent,hash);
1377 struct dentry *found = NULL;
1378 struct hlist_node *node;
1379 struct dentry *dentry;
1380
1381 rcu_read_lock();
1382
1383 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1384 struct qstr *qstr;
1385
1386 if (dentry->d_name.hash != hash)
1387 continue;
1388 if (dentry->d_parent != parent)
1389 continue;
1390
1391 spin_lock(&dentry->d_lock);
1392
1393
1394
1395
1396
1397
1398 if (dentry->d_parent != parent)
1399 goto next;
1400
1401
1402 if (d_unhashed(dentry))
1403 goto next;
1404
1405
1406
1407
1408
1409 qstr = &dentry->d_name;
1410 if (parent->d_op && parent->d_op->d_compare) {
1411 if (parent->d_op->d_compare(parent, qstr, name))
1412 goto next;
1413 } else {
1414 if (qstr->len != len)
1415 goto next;
1416 if (memcmp(qstr->name, str, len))
1417 goto next;
1418 }
1419
1420 atomic_inc(&dentry->d_count);
1421 found = dentry;
1422 spin_unlock(&dentry->d_lock);
1423 break;
1424next:
1425 spin_unlock(&dentry->d_lock);
1426 }
1427 rcu_read_unlock();
1428
1429 return found;
1430}
1431
1432
1433
1434
1435
1436
1437
1438
1439struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1440{
1441 struct dentry *dentry = NULL;
1442
1443
1444
1445
1446
1447
1448 name->hash = full_name_hash(name->name, name->len);
1449 if (dir->d_op && dir->d_op->d_hash) {
1450 if (dir->d_op->d_hash(dir, name) < 0)
1451 goto out;
1452 }
1453 dentry = d_lookup(dir, name);
1454out:
1455 return dentry;
1456}
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468int d_validate(struct dentry *dentry, struct dentry *dparent)
1469{
1470 struct hlist_head *base;
1471 struct hlist_node *lhp;
1472
1473
1474 if (!kmem_ptr_validate(dentry_cache, dentry))
1475 goto out;
1476
1477 if (dentry->d_parent != dparent)
1478 goto out;
1479
1480 spin_lock(&dcache_lock);
1481 base = d_hash(dparent, dentry->d_name.hash);
1482 hlist_for_each(lhp,base) {
1483
1484
1485
1486 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1487 __dget_locked(dentry);
1488 spin_unlock(&dcache_lock);
1489 return 1;
1490 }
1491 }
1492 spin_unlock(&dcache_lock);
1493out:
1494 return 0;
1495}
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518void d_delete(struct dentry * dentry)
1519{
1520 int isdir = 0;
1521
1522
1523
1524 spin_lock(&dcache_lock);
1525 spin_lock(&dentry->d_lock);
1526 isdir = S_ISDIR(dentry->d_inode->i_mode);
1527 if (atomic_read(&dentry->d_count) == 1) {
1528 dentry_iput(dentry);
1529 fsnotify_nameremove(dentry, isdir);
1530 return;
1531 }
1532
1533 if (!d_unhashed(dentry))
1534 __d_drop(dentry);
1535
1536 spin_unlock(&dentry->d_lock);
1537 spin_unlock(&dcache_lock);
1538
1539 fsnotify_nameremove(dentry, isdir);
1540}
1541
1542static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1543{
1544
1545 entry->d_flags &= ~DCACHE_UNHASHED;
1546 hlist_add_head_rcu(&entry->d_hash, list);
1547}
1548
1549static void _d_rehash(struct dentry * entry)
1550{
1551 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1552}
1553
1554
1555
1556
1557
1558
1559
1560
1561void d_rehash(struct dentry * entry)
1562{
1563 spin_lock(&dcache_lock);
1564 spin_lock(&entry->d_lock);
1565 _d_rehash(entry);
1566 spin_unlock(&entry->d_lock);
1567 spin_unlock(&dcache_lock);
1568}
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581static void switch_names(struct dentry *dentry, struct dentry *target)
1582{
1583 if (dname_external(target)) {
1584 if (dname_external(dentry)) {
1585
1586
1587
1588 swap(target->d_name.name, dentry->d_name.name);
1589 } else {
1590
1591
1592
1593
1594 memcpy(target->d_iname, dentry->d_name.name,
1595 dentry->d_name.len + 1);
1596 dentry->d_name.name = target->d_name.name;
1597 target->d_name.name = target->d_iname;
1598 }
1599 } else {
1600 if (dname_external(dentry)) {
1601
1602
1603
1604
1605 memcpy(dentry->d_iname, target->d_name.name,
1606 target->d_name.len + 1);
1607 target->d_name.name = dentry->d_name.name;
1608 dentry->d_name.name = dentry->d_iname;
1609 } else {
1610
1611
1612
1613 memcpy(dentry->d_iname, target->d_name.name,
1614 target->d_name.len + 1);
1615 dentry->d_name.len = target->d_name.len;
1616 return;
1617 }
1618 }
1619 swap(dentry->d_name.len, target->d_name.len);
1620}
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642static void d_move_locked(struct dentry * dentry, struct dentry * target)
1643{
1644 struct hlist_head *list;
1645
1646 if (!dentry->d_inode)
1647 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1648
1649 write_seqlock(&rename_lock);
1650
1651
1652
1653 if (target < dentry) {
1654 spin_lock(&target->d_lock);
1655 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1656 } else {
1657 spin_lock(&dentry->d_lock);
1658 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
1659 }
1660
1661
1662 if (d_unhashed(dentry))
1663 goto already_unhashed;
1664
1665 hlist_del_rcu(&dentry->d_hash);
1666
1667already_unhashed:
1668 list = d_hash(target->d_parent, target->d_name.hash);
1669 __d_rehash(dentry, list);
1670
1671
1672 __d_drop(target);
1673
1674 list_del(&dentry->d_u.d_child);
1675 list_del(&target->d_u.d_child);
1676
1677
1678 switch_names(dentry, target);
1679 swap(dentry->d_name.hash, target->d_name.hash);
1680
1681
1682 if (IS_ROOT(dentry)) {
1683 dentry->d_parent = target->d_parent;
1684 target->d_parent = target;
1685 INIT_LIST_HEAD(&target->d_u.d_child);
1686 } else {
1687 swap(dentry->d_parent, target->d_parent);
1688
1689
1690 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1691 }
1692
1693 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1694 spin_unlock(&target->d_lock);
1695 fsnotify_d_move(dentry);
1696 spin_unlock(&dentry->d_lock);
1697 write_sequnlock(&rename_lock);
1698}
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709void d_move(struct dentry * dentry, struct dentry * target)
1710{
1711 spin_lock(&dcache_lock);
1712 d_move_locked(dentry, target);
1713 spin_unlock(&dcache_lock);
1714}
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
1725{
1726 struct dentry *p;
1727
1728 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
1729 if (p->d_parent == p1)
1730 return p;
1731 }
1732 return NULL;
1733}
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1745 __releases(dcache_lock)
1746{
1747 struct mutex *m1 = NULL, *m2 = NULL;
1748 struct dentry *ret;
1749
1750
1751 if (alias->d_parent == dentry->d_parent)
1752 goto out_unalias;
1753
1754
1755 ret = ERR_PTR(-ELOOP);
1756 if (d_ancestor(alias, dentry))
1757 goto out_err;
1758
1759
1760 ret = ERR_PTR(-EBUSY);
1761 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1762 goto out_err;
1763 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1764 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1765 goto out_err;
1766 m2 = &alias->d_parent->d_inode->i_mutex;
1767out_unalias:
1768 d_move_locked(alias, dentry);
1769 ret = alias;
1770out_err:
1771 spin_unlock(&dcache_lock);
1772 if (m2)
1773 mutex_unlock(m2);
1774 if (m1)
1775 mutex_unlock(m1);
1776 return ret;
1777}
1778
1779
1780
1781
1782
1783static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1784{
1785 struct dentry *dparent, *aparent;
1786
1787 switch_names(dentry, anon);
1788 swap(dentry->d_name.hash, anon->d_name.hash);
1789
1790 dparent = dentry->d_parent;
1791 aparent = anon->d_parent;
1792
1793 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1794 list_del(&dentry->d_u.d_child);
1795 if (!IS_ROOT(dentry))
1796 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1797 else
1798 INIT_LIST_HEAD(&dentry->d_u.d_child);
1799
1800 anon->d_parent = (dparent == dentry) ? anon : dparent;
1801 list_del(&anon->d_u.d_child);
1802 if (!IS_ROOT(anon))
1803 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1804 else
1805 INIT_LIST_HEAD(&anon->d_u.d_child);
1806
1807 anon->d_flags &= ~DCACHE_DISCONNECTED;
1808}
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1819{
1820 struct dentry *actual;
1821
1822 BUG_ON(!d_unhashed(dentry));
1823
1824 spin_lock(&dcache_lock);
1825
1826 if (!inode) {
1827 actual = dentry;
1828 __d_instantiate(dentry, NULL);
1829 goto found_lock;
1830 }
1831
1832 if (S_ISDIR(inode->i_mode)) {
1833 struct dentry *alias;
1834
1835
1836 alias = __d_find_alias(inode, 0);
1837 if (alias) {
1838 actual = alias;
1839
1840
1841 if (IS_ROOT(alias)) {
1842 spin_lock(&alias->d_lock);
1843 __d_materialise_dentry(dentry, alias);
1844 __d_drop(alias);
1845 goto found;
1846 }
1847
1848 actual = __d_unalias(dentry, alias);
1849 if (IS_ERR(actual))
1850 dput(alias);
1851 goto out_nolock;
1852 }
1853 }
1854
1855
1856 actual = __d_instantiate_unique(dentry, inode);
1857 if (!actual)
1858 actual = dentry;
1859 else if (unlikely(!d_unhashed(actual)))
1860 goto shouldnt_be_hashed;
1861
1862found_lock:
1863 spin_lock(&actual->d_lock);
1864found:
1865 _d_rehash(actual);
1866 spin_unlock(&actual->d_lock);
1867 spin_unlock(&dcache_lock);
1868out_nolock:
1869 if (actual == dentry) {
1870 security_d_instantiate(dentry, inode);
1871 return NULL;
1872 }
1873
1874 iput(inode);
1875 return actual;
1876
1877shouldnt_be_hashed:
1878 spin_unlock(&dcache_lock);
1879 BUG();
1880}
1881
1882static int prepend(char **buffer, int *buflen, const char *str, int namelen)
1883{
1884 *buflen -= namelen;
1885 if (*buflen < 0)
1886 return -ENAMETOOLONG;
1887 *buffer -= namelen;
1888 memcpy(*buffer, str, namelen);
1889 return 0;
1890}
1891
1892static int prepend_name(char **buffer, int *buflen, struct qstr *name)
1893{
1894 return prepend(buffer, buflen, name->name, name->len);
1895}
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915char *__d_path(const struct path *path, struct path *root,
1916 char *buffer, int buflen)
1917{
1918 struct dentry *dentry = path->dentry;
1919 struct vfsmount *vfsmnt = path->mnt;
1920 char *end = buffer + buflen;
1921 char *retval;
1922
1923 spin_lock(&vfsmount_lock);
1924 prepend(&end, &buflen, "\0", 1);
1925 if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
1926 (prepend(&end, &buflen, " (deleted)", 10) != 0))
1927 goto Elong;
1928
1929 if (buflen < 1)
1930 goto Elong;
1931
1932 retval = end-1;
1933 *retval = '/';
1934
1935 for (;;) {
1936 struct dentry * parent;
1937
1938 if (dentry == root->dentry && vfsmnt == root->mnt)
1939 break;
1940 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1941
1942 if (vfsmnt->mnt_parent == vfsmnt) {
1943 goto global_root;
1944 }
1945 dentry = vfsmnt->mnt_mountpoint;
1946 vfsmnt = vfsmnt->mnt_parent;
1947 continue;
1948 }
1949 parent = dentry->d_parent;
1950 prefetch(parent);
1951 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
1952 (prepend(&end, &buflen, "/", 1) != 0))
1953 goto Elong;
1954 retval = end;
1955 dentry = parent;
1956 }
1957
1958out:
1959 spin_unlock(&vfsmount_lock);
1960 return retval;
1961
1962global_root:
1963 retval += 1;
1964 if (prepend_name(&retval, &buflen, &dentry->d_name) != 0)
1965 goto Elong;
1966 root->mnt = vfsmnt;
1967 root->dentry = dentry;
1968 goto out;
1969
1970Elong:
1971 retval = ERR_PTR(-ENAMETOOLONG);
1972 goto out;
1973}
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991char *d_path(const struct path *path, char *buf, int buflen)
1992{
1993 char *res;
1994 struct path root;
1995 struct path tmp;
1996
1997
1998
1999
2000
2001
2002
2003
2004 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2005 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2006
2007 read_lock(¤t->fs->lock);
2008 root = current->fs->root;
2009 path_get(&root);
2010 read_unlock(¤t->fs->lock);
2011 spin_lock(&dcache_lock);
2012 tmp = root;
2013 res = __d_path(path, &tmp, buf, buflen);
2014 spin_unlock(&dcache_lock);
2015 path_put(&root);
2016 return res;
2017}
2018
2019
2020
2021
2022char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2023 const char *fmt, ...)
2024{
2025 va_list args;
2026 char temp[64];
2027 int sz;
2028
2029 va_start(args, fmt);
2030 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2031 va_end(args);
2032
2033 if (sz > sizeof(temp) || sz > buflen)
2034 return ERR_PTR(-ENAMETOOLONG);
2035
2036 buffer += buflen - sz;
2037 return memcpy(buffer, temp, sz);
2038}
2039
2040
2041
2042
2043char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2044{
2045 char *end = buf + buflen;
2046 char *retval;
2047
2048 spin_lock(&dcache_lock);
2049 prepend(&end, &buflen, "\0", 1);
2050 if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
2051 (prepend(&end, &buflen, "//deleted", 9) != 0))
2052 goto Elong;
2053 if (buflen < 1)
2054 goto Elong;
2055
2056 retval = end-1;
2057 *retval = '/';
2058
2059 while (!IS_ROOT(dentry)) {
2060 struct dentry *parent = dentry->d_parent;
2061
2062 prefetch(parent);
2063 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
2064 (prepend(&end, &buflen, "/", 1) != 0))
2065 goto Elong;
2066
2067 retval = end;
2068 dentry = parent;
2069 }
2070 spin_unlock(&dcache_lock);
2071 return retval;
2072Elong:
2073 spin_unlock(&dcache_lock);
2074 return ERR_PTR(-ENAMETOOLONG);
2075}
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
2096{
2097 int error;
2098 struct path pwd, root;
2099 char *page = (char *) __get_free_page(GFP_USER);
2100
2101 if (!page)
2102 return -ENOMEM;
2103
2104 read_lock(¤t->fs->lock);
2105 pwd = current->fs->pwd;
2106 path_get(&pwd);
2107 root = current->fs->root;
2108 path_get(&root);
2109 read_unlock(¤t->fs->lock);
2110
2111 error = -ENOENT;
2112
2113 spin_lock(&dcache_lock);
2114 if (IS_ROOT(pwd.dentry) || !d_unhashed(pwd.dentry)) {
2115 unsigned long len;
2116 struct path tmp = root;
2117 char * cwd;
2118
2119 cwd = __d_path(&pwd, &tmp, page, PAGE_SIZE);
2120 spin_unlock(&dcache_lock);
2121
2122 error = PTR_ERR(cwd);
2123 if (IS_ERR(cwd))
2124 goto out;
2125
2126 error = -ERANGE;
2127 len = PAGE_SIZE + page - cwd;
2128 if (len <= size) {
2129 error = len;
2130 if (copy_to_user(buf, cwd, len))
2131 error = -EFAULT;
2132 }
2133 } else
2134 spin_unlock(&dcache_lock);
2135
2136out:
2137 path_put(&pwd);
2138 path_put(&root);
2139 free_page((unsigned long) page);
2140 return error;
2141}
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
2160{
2161 int result;
2162 unsigned long seq;
2163
2164
2165 if (new_dentry == old_dentry)
2166 return 1;
2167
2168
2169
2170
2171
2172 rcu_read_lock();
2173 do {
2174
2175 seq = read_seqbegin(&rename_lock);
2176 if (d_ancestor(old_dentry, new_dentry))
2177 result = 1;
2178 else
2179 result = 0;
2180 } while (read_seqretry(&rename_lock, seq));
2181 rcu_read_unlock();
2182
2183 return result;
2184}
2185
2186void d_genocide(struct dentry *root)
2187{
2188 struct dentry *this_parent = root;
2189 struct list_head *next;
2190
2191 spin_lock(&dcache_lock);
2192repeat:
2193 next = this_parent->d_subdirs.next;
2194resume:
2195 while (next != &this_parent->d_subdirs) {
2196 struct list_head *tmp = next;
2197 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
2198 next = tmp->next;
2199 if (d_unhashed(dentry)||!dentry->d_inode)
2200 continue;
2201 if (!list_empty(&dentry->d_subdirs)) {
2202 this_parent = dentry;
2203 goto repeat;
2204 }
2205 atomic_dec(&dentry->d_count);
2206 }
2207 if (this_parent != root) {
2208 next = this_parent->d_u.d_child.next;
2209 atomic_dec(&this_parent->d_count);
2210 this_parent = this_parent->d_parent;
2211 goto resume;
2212 }
2213 spin_unlock(&dcache_lock);
2214}
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2231{
2232 struct dentry * dentry;
2233 ino_t ino = 0;
2234
2235 dentry = d_hash_and_lookup(dir, name);
2236 if (dentry) {
2237 if (dentry->d_inode)
2238 ino = dentry->d_inode->i_ino;
2239 dput(dentry);
2240 }
2241 return ino;
2242}
2243
2244static __initdata unsigned long dhash_entries;
2245static int __init set_dhash_entries(char *str)
2246{
2247 if (!str)
2248 return 0;
2249 dhash_entries = simple_strtoul(str, &str, 0);
2250 return 1;
2251}
2252__setup("dhash_entries=", set_dhash_entries);
2253
2254static void __init dcache_init_early(void)
2255{
2256 int loop;
2257
2258
2259
2260
2261 if (hashdist)
2262 return;
2263
2264 dentry_hashtable =
2265 alloc_large_system_hash("Dentry cache",
2266 sizeof(struct hlist_head),
2267 dhash_entries,
2268 13,
2269 HASH_EARLY,
2270 &d_hash_shift,
2271 &d_hash_mask,
2272 0);
2273
2274 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2275 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2276}
2277
2278static void __init dcache_init(void)
2279{
2280 int loop;
2281
2282
2283
2284
2285
2286
2287 dentry_cache = KMEM_CACHE(dentry,
2288 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
2289
2290 register_shrinker(&dcache_shrinker);
2291
2292
2293 if (!hashdist)
2294 return;
2295
2296 dentry_hashtable =
2297 alloc_large_system_hash("Dentry cache",
2298 sizeof(struct hlist_head),
2299 dhash_entries,
2300 13,
2301 0,
2302 &d_hash_shift,
2303 &d_hash_mask,
2304 0);
2305
2306 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2307 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2308}
2309
2310
2311struct kmem_cache *names_cachep __read_mostly;
2312
2313EXPORT_SYMBOL(d_genocide);
2314
2315void __init vfs_caches_init_early(void)
2316{
2317 dcache_init_early();
2318 inode_init_early();
2319}
2320
2321void __init vfs_caches_init(unsigned long mempages)
2322{
2323 unsigned long reserve;
2324
2325
2326
2327
2328 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2329 mempages -= reserve;
2330
2331 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2332 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2333
2334 dcache_init();
2335 inode_init();
2336 files_init(mempages);
2337 mnt_init();
2338 bdev_cache_init();
2339 chrdev_init();
2340}
2341
2342EXPORT_SYMBOL(d_alloc);
2343EXPORT_SYMBOL(d_alloc_root);
2344EXPORT_SYMBOL(d_delete);
2345EXPORT_SYMBOL(d_find_alias);
2346EXPORT_SYMBOL(d_instantiate);
2347EXPORT_SYMBOL(d_invalidate);
2348EXPORT_SYMBOL(d_lookup);
2349EXPORT_SYMBOL(d_move);
2350EXPORT_SYMBOL_GPL(d_materialise_unique);
2351EXPORT_SYMBOL(d_path);
2352EXPORT_SYMBOL(d_prune_aliases);
2353EXPORT_SYMBOL(d_rehash);
2354EXPORT_SYMBOL(d_splice_alias);
2355EXPORT_SYMBOL(d_add_ci);
2356EXPORT_SYMBOL(d_validate);
2357EXPORT_SYMBOL(dget_locked);
2358EXPORT_SYMBOL(dput);
2359EXPORT_SYMBOL(find_inode_number);
2360EXPORT_SYMBOL(have_submounts);
2361EXPORT_SYMBOL(names_cachep);
2362EXPORT_SYMBOL(shrink_dcache_parent);
2363EXPORT_SYMBOL(shrink_dcache_sb);
2364