1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17#define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
18
19#include <linux/syscalls.h>
20#include <linux/string.h>
21#include <linux/mm.h>
22#include <linux/fs.h>
23#include <linux/fsnotify.h>
24#include <linux/slab.h>
25#include <linux/init.h>
26#include <linux/hash.h>
27#include <linux/cache.h>
28#include <linux/export.h>
29#include <linux/mount.h>
30#include <linux/file.h>
31#include <asm/uaccess.h>
32#include <linux/security.h>
33#include <linux/seqlock.h>
34#include <linux/swap.h>
35#include <linux/bootmem.h>
36#include <linux/fs_struct.h>
37#include <linux/hardirq.h>
38#include <linux/bit_spinlock.h>
39#include <linux/rculist_bl.h>
40#include <linux/prefetch.h>
41#include <linux/ratelimit.h>
42#include "internal.h"
43#include "mount.h"
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83int sysctl_vfs_cache_pressure __read_mostly = 100;
84EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
85
86static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
87__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
88
89EXPORT_SYMBOL(rename_lock);
90
91static struct kmem_cache *dentry_cache __read_mostly;
92
93
94
95
96
97
98
99
100
101#define D_HASHBITS d_hash_shift
102#define D_HASHMASK d_hash_mask
103
104static unsigned int d_hash_mask __read_mostly;
105static unsigned int d_hash_shift __read_mostly;
106
107static struct hlist_bl_head *dentry_hashtable __read_mostly;
108
109static inline struct hlist_bl_head *d_hash(const struct dentry *parent,
110 unsigned int hash)
111{
112 hash += (unsigned long) parent / L1_CACHE_BYTES;
113 hash = hash + (hash >> D_HASHBITS);
114 return dentry_hashtable + (hash & D_HASHMASK);
115}
116
117
118struct dentry_stat_t dentry_stat = {
119 .age_limit = 45,
120};
121
122
123
124
125
126
127
128
129
130
131
132
133
134static long negative_dentry_limit;
135int dcache_negative_dentry_limit_sysctl;
136EXPORT_SYMBOL_GPL(dcache_negative_dentry_limit_sysctl);
137
138
139
140
141
142#define NEGATIVE_DENTRY_CHECK_PERIOD (15 * HZ)
143static void prune_negative_dentry(struct work_struct *work);
144static DECLARE_DELAYED_WORK(prune_negative_dentry_work, prune_negative_dentry);
145
146static DEFINE_PER_CPU(long, nr_dentry);
147static DEFINE_PER_CPU(long, nr_dentry_unused);
148static DEFINE_PER_CPU(long, nr_dentry_negative);
149
150#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
151
152
153
154
155
156
157
158
159
160
161
162
163
164static long get_nr_dentry(void)
165{
166 int i;
167 long sum = 0;
168 for_each_possible_cpu(i)
169 sum += per_cpu(nr_dentry, i);
170 return sum < 0 ? 0 : sum;
171}
172
173static long get_nr_dentry_unused(void)
174{
175 int i;
176 long sum = 0;
177 for_each_possible_cpu(i)
178 sum += per_cpu(nr_dentry_unused, i);
179 return sum < 0 ? 0 : sum;
180}
181
182static long get_nr_dentry_negative(void)
183{
184 int i;
185 long sum = 0;
186
187 for_each_possible_cpu(i)
188 sum += per_cpu(nr_dentry_negative, i);
189 return sum < 0 ? 0 : sum;
190}
191
192int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
193 size_t *lenp, loff_t *ppos)
194{
195 dentry_stat.nr_dentry = get_nr_dentry();
196 dentry_stat.nr_unused = get_nr_dentry_unused();
197 dentry_stat.nr_negative = get_nr_dentry_negative();
198 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
199}
200#endif
201
202
203
204
205int proc_dcache_negative_dentry_limit(struct ctl_table *ctl, int write,
206 void __user *buffer, size_t *lenp,
207 loff_t *ppos)
208{
209
210 const unsigned int nr_dentry_page = PAGE_SIZE/sizeof(struct dentry);
211 int old = dcache_negative_dentry_limit_sysctl;
212 int ret;
213
214 ret = proc_dointvec_minmax(ctl, write, buffer, lenp, ppos);
215
216 if (!write || ret || (dcache_negative_dentry_limit_sysctl == old))
217 return ret;
218
219 negative_dentry_limit = totalram_pages * nr_dentry_page *
220 dcache_negative_dentry_limit_sysctl / 1000;
221
222
223
224
225
226
227
228 if (dcache_negative_dentry_limit_sysctl && !old)
229 schedule_delayed_work(&prune_negative_dentry_work, 0);
230
231 if (!dcache_negative_dentry_limit_sysctl && old)
232 cancel_delayed_work_sync(&prune_negative_dentry_work);
233
234 pr_info("Negative dentry limits = %ld\n", negative_dentry_limit);
235 return 0;
236}
237EXPORT_SYMBOL_GPL(proc_dcache_negative_dentry_limit);
238
239
240
241
242
243#ifdef CONFIG_DCACHE_WORD_ACCESS
244
245#include <asm/word-at-a-time.h>
246
247
248
249
250
251
252
253
254
255static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
256{
257 unsigned long a,b,mask;
258
259 for (;;) {
260 a = *(unsigned long *)cs;
261 b = load_unaligned_zeropad(ct);
262 if (tcount < sizeof(unsigned long))
263 break;
264 if (unlikely(a != b))
265 return 1;
266 cs += sizeof(unsigned long);
267 ct += sizeof(unsigned long);
268 tcount -= sizeof(unsigned long);
269 if (!tcount)
270 return 0;
271 }
272 mask = bytemask_from_count(tcount);
273 return unlikely(!!((a ^ b) & mask));
274}
275
276#else
277
278static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
279{
280 do {
281 if (*cs != *ct)
282 return 1;
283 cs++;
284 ct++;
285 tcount--;
286 } while (tcount);
287 return 0;
288}
289
290#endif
291
292static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
293{
294 const unsigned char *cs;
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311 cs = ACCESS_ONCE(dentry->d_name.name);
312 smp_read_barrier_depends();
313 return dentry_string_cmp(cs, ct, tcount);
314}
315
316void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
317{
318 size_t size = 0;
319 char *buf = NULL;
320
321 if (unlikely(dname_external(dentry))) {
322 size = READ_ONCE(dentry->d_name.len);
323retry:
324
325 name->name = buf = kmalloc(size + 1, GFP_KERNEL);
326 if (!buf)
327 return;
328 }
329
330 spin_lock(&dentry->d_lock);
331 if (unlikely(dname_external(dentry))) {
332 if (size < dentry->d_name.len) {
333
334 size = dentry->d_name.len;
335 spin_unlock(&dentry->d_lock);
336 kfree(buf);
337 goto retry;
338 }
339 strcpy(buf, dentry->d_name.name);
340 buf = NULL;
341 } else {
342 memcpy(name->inline_name, dentry->d_iname, DNAME_INLINE_LEN);
343 name->name = name->inline_name;
344 }
345 spin_unlock(&dentry->d_lock);
346 kfree(buf);
347}
348EXPORT_SYMBOL(take_dentry_name_snapshot);
349
350void release_dentry_name_snapshot(struct name_snapshot *name)
351{
352 if (unlikely(name->name != name->inline_name))
353 kfree(name->name);
354}
355EXPORT_SYMBOL(release_dentry_name_snapshot);
356
357static void __d_free(struct rcu_head *head)
358{
359 struct dentry *dentry = container_of(
360 (struct hlist_node *)head, struct dentry, d_alias);
361
362 if (dname_external(dentry))
363 kfree(dentry->d_name.name);
364 kmem_cache_free(dentry_cache, dentry);
365}
366
367
368
369
370static void d_free(struct dentry *dentry)
371{
372 struct rcu_head *p = (struct rcu_head *)&dentry->d_alias;
373 BUG_ON((int)dentry->d_lockref.count > 0);
374 this_cpu_dec(nr_dentry);
375 if (dentry->d_op && dentry->d_op->d_release)
376 dentry->d_op->d_release(dentry);
377
378
379 if (!(dentry->d_flags & DCACHE_RCUACCESS))
380 __d_free(p);
381 else
382 call_rcu(p, __d_free);
383}
384
385
386
387
388
389
390
391
392static inline void dentry_rcuwalk_invalidate(struct dentry *dentry)
393{
394 lockdep_assert_held(&dentry->d_lock);
395
396 write_seqcount_invalidate(&dentry->d_seq);
397}
398
399
400
401
402
403
404static void dentry_iput(struct dentry * dentry)
405 __releases(dentry->d_lock)
406 __releases(dentry->d_inode->i_lock)
407{
408 struct inode *inode = dentry->d_inode;
409 if (inode) {
410 dentry->d_inode = NULL;
411 hlist_del_init(&dentry->d_alias);
412 spin_unlock(&dentry->d_lock);
413 spin_unlock(&inode->i_lock);
414 if (!inode->i_nlink)
415 fsnotify_inoderemove(inode);
416 if (dentry->d_op && dentry->d_op->d_iput)
417 dentry->d_op->d_iput(dentry, inode);
418 else
419 iput(inode);
420 } else {
421 spin_unlock(&dentry->d_lock);
422 }
423}
424
425
426
427
428
429static void dentry_unlink_inode(struct dentry * dentry)
430 __releases(dentry->d_lock)
431 __releases(dentry->d_inode->i_lock)
432{
433 struct inode *inode = dentry->d_inode;
434 __d_clear_type(dentry);
435 dentry->d_inode = NULL;
436 if (dentry->d_flags & DCACHE_LRU_LIST)
437 this_cpu_inc(nr_dentry_negative);
438 hlist_del_init(&dentry->d_alias);
439 dentry_rcuwalk_invalidate(dentry);
440 spin_unlock(&dentry->d_lock);
441 spin_unlock(&inode->i_lock);
442 if (!inode->i_nlink)
443 fsnotify_inoderemove(inode);
444 if (dentry->d_op && dentry->d_op->d_iput)
445 dentry->d_op->d_iput(dentry, inode);
446 else
447 iput(inode);
448}
449
450
451
452
453static void dentry_lru_add(struct dentry *dentry)
454{
455 if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST))) {
456 bool negative = d_is_negative(dentry);
457
458 spin_lock(&dcache_lru_lock);
459 dentry->d_flags |= DCACHE_LRU_LIST;
460
461
462
463
464
465
466
467 if (negative)
468 list_add_tail(&dentry->d_lru,
469 &dentry->d_sb->s_dentry_lru);
470 else
471 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
472 dentry->d_sb->s_nr_dentry_unused++;
473 spin_unlock(&dcache_lru_lock);
474 this_cpu_inc(nr_dentry_unused);
475 if (d_is_negative(dentry))
476 this_cpu_inc(nr_dentry_negative);
477 } else {
478 dentry->d_flags |= DCACHE_REFERENCED;
479 }
480}
481
482static void __dentry_lru_del(struct dentry *dentry)
483{
484 list_del_init(&dentry->d_lru);
485 dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
486 dentry->d_sb->s_nr_dentry_unused--;
487}
488
489
490
491
492static void dentry_lru_del(struct dentry *dentry)
493{
494 if (!list_empty(&dentry->d_lru)) {
495 spin_lock(&dcache_lru_lock);
496 __dentry_lru_del(dentry);
497 spin_unlock(&dcache_lru_lock);
498 this_cpu_dec(nr_dentry_unused);
499 if (d_is_negative(dentry))
500 this_cpu_dec(nr_dentry_negative);
501 }
502}
503
504static void dentry_lru_move_list(struct dentry *dentry, struct list_head *list)
505{
506 spin_lock(&dcache_lru_lock);
507 if (list_empty(&dentry->d_lru)) {
508 dentry->d_flags |= DCACHE_LRU_LIST;
509 list_add_tail(&dentry->d_lru, list);
510 dentry->d_sb->s_nr_dentry_unused++;
511 this_cpu_inc(nr_dentry_unused);
512 } else {
513 list_move_tail(&dentry->d_lru, list);
514 }
515 spin_unlock(&dcache_lru_lock);
516}
517
518
519
520
521
522
523static void __d_shrink(struct dentry *dentry)
524{
525 if (!d_unhashed(dentry)) {
526 struct hlist_bl_head *b;
527
528
529
530
531
532 if (unlikely(IS_ROOT(dentry)))
533 b = &dentry->d_sb->s_anon;
534 else
535 b = d_hash(dentry->d_parent, dentry->d_name.hash);
536
537 hlist_bl_lock(b);
538 __hlist_bl_del(&dentry->d_hash);
539 dentry->d_hash.pprev = NULL;
540 hlist_bl_unlock(b);
541 }
542}
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559void __d_drop(struct dentry *dentry)
560{
561 if (!d_unhashed(dentry)) {
562 __d_shrink(dentry);
563 dentry_rcuwalk_invalidate(dentry);
564 }
565}
566EXPORT_SYMBOL(__d_drop);
567
568static void __d_hash_move(struct dentry *dentry, struct hlist_bl_head *new)
569{
570 if (!d_unhashed(dentry)) {
571 struct hlist_bl_head *b;
572
573 if (unlikely(IS_ROOT(dentry)))
574 b = &dentry->d_sb->s_anon;
575 else
576 b = d_hash(dentry->d_parent, dentry->d_name.hash);
577
578 hlist_bl_lock(b);
579 __hlist_bl_del(&dentry->d_hash);
580 hlist_bl_unlock(b);
581 dentry_rcuwalk_invalidate(dentry);
582 }
583 hlist_bl_lock(new);
584 hlist_bl_add_head_rcu(&dentry->d_hash, new);
585 hlist_bl_unlock(new);
586}
587
588
589void d_drop(struct dentry *dentry)
590{
591 spin_lock(&dentry->d_lock);
592 __d_drop(dentry);
593 spin_unlock(&dentry->d_lock);
594}
595EXPORT_SYMBOL(d_drop);
596
597static inline void dentry_unlist(struct dentry *dentry, struct dentry *parent)
598{
599 struct dentry *next;
600
601
602
603
604 dentry->d_flags |= DCACHE_DENTRY_KILLED;
605 if (unlikely(list_empty(&dentry->d_u.d_child)))
606 return;
607 __list_del_entry(&dentry->d_u.d_child);
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627 while (dentry->d_u.d_child.next != &parent->d_subdirs) {
628 next = list_entry(dentry->d_u.d_child.next, struct dentry, d_u.d_child);
629 if (likely(!(next->d_flags & DCACHE_DENTRY_CURSOR)))
630 break;
631 dentry->d_u.d_child.next = next->d_u.d_child.next;
632 }
633}
634
635static void __dentry_kill(struct dentry *dentry)
636{
637 struct dentry *parent;
638 if (IS_ROOT(dentry))
639 parent = NULL;
640 else
641 parent = dentry->d_parent;
642
643
644
645
646 lockref_mark_dead(&dentry->d_lockref);
647
648
649
650
651
652 if ((dentry->d_flags & DCACHE_OP_PRUNE) && !d_unhashed(dentry))
653 dentry->d_op->d_prune(dentry);
654
655 dentry_lru_del(dentry);
656
657 __d_drop(dentry);
658 dentry_unlist(dentry, parent);
659 if (parent)
660 spin_unlock(&parent->d_lock);
661 dentry_iput(dentry);
662
663
664
665
666 d_free(dentry);
667}
668
669
670
671
672
673
674
675static inline struct dentry *dentry_kill(struct dentry *dentry)
676 __releases(dentry->d_lock)
677{
678 struct inode *inode = dentry->d_inode;
679 struct dentry *parent = NULL;
680
681 if (inode && unlikely(!spin_trylock(&inode->i_lock)))
682 goto failed;
683
684 if (!IS_ROOT(dentry)) {
685 parent = dentry->d_parent;
686 if (unlikely(!spin_trylock(&parent->d_lock))) {
687 if (inode)
688 spin_unlock(&inode->i_lock);
689 goto failed;
690 }
691 }
692
693 __dentry_kill(dentry);
694 return parent;
695
696failed:
697 spin_unlock(&dentry->d_lock);
698 return dentry;
699}
700
701static inline struct dentry *lock_parent(struct dentry *dentry)
702{
703 struct dentry *parent = dentry->d_parent;
704 if (IS_ROOT(dentry))
705 return NULL;
706 if (likely(spin_trylock(&parent->d_lock)))
707 return parent;
708 spin_unlock(&dentry->d_lock);
709 rcu_read_lock();
710again:
711 parent = ACCESS_ONCE(dentry->d_parent);
712 spin_lock(&parent->d_lock);
713
714
715
716
717
718
719
720
721 if (unlikely(parent != dentry->d_parent)) {
722 spin_unlock(&parent->d_lock);
723 goto again;
724 }
725 rcu_read_unlock();
726 if (parent != dentry)
727 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
728 else
729 parent = NULL;
730 return parent;
731}
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759void dput(struct dentry *dentry)
760{
761 if (unlikely(!dentry))
762 return;
763
764repeat:
765 might_sleep();
766
767 if (lockref_put_or_lock(&dentry->d_lockref))
768 return;
769
770
771 if (unlikely(d_unhashed(dentry)))
772 goto kill_it;
773
774 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
775 goto kill_it;
776
777 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
778 if (dentry->d_op->d_delete(dentry))
779 goto kill_it;
780 }
781
782 dentry_lru_add(dentry);
783
784 dentry->d_lockref.count--;
785 spin_unlock(&dentry->d_lock);
786 return;
787
788kill_it:
789 dentry = dentry_kill(dentry);
790 if (dentry) {
791 cond_resched();
792 goto repeat;
793 }
794}
795EXPORT_SYMBOL(dput);
796
797
798
799static inline void __dget_dlock(struct dentry *dentry)
800{
801 dentry->d_lockref.count++;
802}
803
804static inline void __dget(struct dentry *dentry)
805{
806 lockref_get(&dentry->d_lockref);
807}
808
809struct dentry *dget_parent(struct dentry *dentry)
810{
811 int gotref;
812 struct dentry *ret;
813
814
815
816
817
818 rcu_read_lock();
819 ret = ACCESS_ONCE(dentry->d_parent);
820 gotref = lockref_get_not_zero(&ret->d_lockref);
821 rcu_read_unlock();
822 if (likely(gotref)) {
823 if (likely(ret == ACCESS_ONCE(dentry->d_parent)))
824 return ret;
825 dput(ret);
826 }
827
828repeat:
829
830
831
832
833 rcu_read_lock();
834 ret = dentry->d_parent;
835 spin_lock(&ret->d_lock);
836 if (unlikely(ret != dentry->d_parent)) {
837 spin_unlock(&ret->d_lock);
838 rcu_read_unlock();
839 goto repeat;
840 }
841 rcu_read_unlock();
842 BUG_ON(!ret->d_lockref.count);
843 ret->d_lockref.count++;
844 spin_unlock(&ret->d_lock);
845 return ret;
846}
847EXPORT_SYMBOL(dget_parent);
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
867{
868 struct dentry *alias, *discon_alias;
869
870again:
871 discon_alias = NULL;
872 hlist_for_each_entry(alias, &inode->i_dentry, d_alias) {
873 spin_lock(&alias->d_lock);
874 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
875 if (IS_ROOT(alias) &&
876 (alias->d_flags & DCACHE_DISCONNECTED)) {
877 discon_alias = alias;
878 } else if (!want_discon) {
879 __dget_dlock(alias);
880 spin_unlock(&alias->d_lock);
881 return alias;
882 }
883 }
884 spin_unlock(&alias->d_lock);
885 }
886 if (discon_alias) {
887 alias = discon_alias;
888 spin_lock(&alias->d_lock);
889 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
890 if (IS_ROOT(alias) &&
891 (alias->d_flags & DCACHE_DISCONNECTED)) {
892 __dget_dlock(alias);
893 spin_unlock(&alias->d_lock);
894 return alias;
895 }
896 }
897 spin_unlock(&alias->d_lock);
898 goto again;
899 }
900 return NULL;
901}
902
903struct dentry *d_find_alias(struct inode *inode)
904{
905 struct dentry *de = NULL;
906
907 if (!hlist_empty(&inode->i_dentry)) {
908 spin_lock(&inode->i_lock);
909 de = __d_find_alias(inode, 0);
910 spin_unlock(&inode->i_lock);
911 }
912 return de;
913}
914EXPORT_SYMBOL(d_find_alias);
915
916
917
918
919
920void d_prune_aliases(struct inode *inode)
921{
922 struct dentry *dentry;
923restart:
924 spin_lock(&inode->i_lock);
925 hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
926 spin_lock(&dentry->d_lock);
927 if (!dentry->d_lockref.count) {
928
929
930
931
932 if ((dentry->d_flags & DCACHE_OP_PRUNE) &&
933 !d_unhashed(dentry))
934 dentry->d_op->d_prune(dentry);
935
936 __dget_dlock(dentry);
937 __d_drop(dentry);
938 spin_unlock(&dentry->d_lock);
939 spin_unlock(&inode->i_lock);
940 dput(dentry);
941 goto restart;
942 }
943 spin_unlock(&dentry->d_lock);
944 }
945 spin_unlock(&inode->i_lock);
946}
947EXPORT_SYMBOL(d_prune_aliases);
948
949
950static void shrink_dentry_list(struct list_head *list)
951{
952 struct dentry *dentry, *parent;
953
954 for (;;) {
955 struct inode *inode;
956
957 cond_resched();
958 rcu_read_lock();
959
960 dentry = list_entry_rcu(list->prev, struct dentry, d_lru);
961 if (&dentry->d_lru == list) {
962 rcu_read_unlock();
963 break;
964 }
965 spin_lock(&dentry->d_lock);
966 if (dentry != list_entry(list->prev, struct dentry, d_lru)) {
967 spin_unlock(&dentry->d_lock);
968 rcu_read_unlock();
969 continue;
970 }
971
972 parent = lock_parent(dentry);
973
974
975
976
977
978
979 if (dentry->d_lockref.count) {
980 dentry_lru_del(dentry);
981 spin_unlock(&dentry->d_lock);
982 if (parent)
983 spin_unlock(&parent->d_lock);
984 rcu_read_unlock();
985 continue;
986 }
987
988 rcu_read_unlock();
989
990 inode = dentry->d_inode;
991 if (inode && unlikely(!spin_trylock(&inode->i_lock))) {
992 spin_unlock(&dentry->d_lock);
993 if (parent)
994 spin_unlock(&parent->d_lock);
995 cpu_relax();
996 continue;
997 }
998
999 __dentry_kill(dentry);
1000
1001
1002
1003
1004
1005
1006
1007 dentry = parent;
1008 while (dentry && !lockref_put_or_lock(&dentry->d_lockref)) {
1009 parent = lock_parent(dentry);
1010 if (dentry->d_lockref.count != 1) {
1011 dentry->d_lockref.count--;
1012 spin_unlock(&dentry->d_lock);
1013 if (parent)
1014 spin_unlock(&parent->d_lock);
1015 break;
1016 }
1017 inode = dentry->d_inode;
1018 if (unlikely(!spin_trylock(&inode->i_lock))) {
1019 spin_unlock(&dentry->d_lock);
1020 if (parent)
1021 spin_unlock(&parent->d_lock);
1022 cpu_relax();
1023 continue;
1024 }
1025 __dentry_kill(dentry);
1026 dentry = parent;
1027 }
1028 }
1029}
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049static long __prune_dcache_sb(struct super_block *sb, long count,
1050 bool negative_only)
1051{
1052 struct dentry *dentry;
1053 long orig_cnt = count;
1054 LIST_HEAD(referenced);
1055 LIST_HEAD(positive);
1056 LIST_HEAD(tmp);
1057
1058relock:
1059 spin_lock(&dcache_lru_lock);
1060 while (!list_empty(&sb->s_dentry_lru)) {
1061 dentry = list_entry(sb->s_dentry_lru.prev,
1062 struct dentry, d_lru);
1063 BUG_ON(dentry->d_sb != sb);
1064
1065 if (!spin_trylock(&dentry->d_lock)) {
1066 spin_unlock(&dcache_lru_lock);
1067 cpu_relax();
1068 goto relock;
1069 }
1070
1071 if (dentry->d_flags & DCACHE_REFERENCED) {
1072 dentry->d_flags &= ~DCACHE_REFERENCED;
1073 list_move(&dentry->d_lru, &referenced);
1074 spin_unlock(&dentry->d_lock);
1075 } else if (negative_only && !d_is_negative(dentry)) {
1076 list_move(&dentry->d_lru, &positive);
1077 spin_unlock(&dentry->d_lock);
1078 } else {
1079 list_move_tail(&dentry->d_lru, &tmp);
1080 dentry->d_flags |= DCACHE_SHRINK_LIST;
1081 spin_unlock(&dentry->d_lock);
1082 if (!--count)
1083 break;
1084 }
1085 cond_resched_lock(&dcache_lru_lock);
1086 }
1087 if (!list_empty(&referenced))
1088 list_splice(&referenced, &sb->s_dentry_lru);
1089 if (!list_empty(&positive))
1090 list_splice_tail(&positive, &sb->s_dentry_lru);
1091 spin_unlock(&dcache_lru_lock);
1092
1093 shrink_dentry_list(&tmp);
1094 return orig_cnt - count;
1095}
1096
1097void prune_dcache_sb(struct super_block *sb, int count)
1098{
1099 __prune_dcache_sb(sb, count, false);
1100}
1101
1102
1103
1104
1105
1106
1107
1108
1109void shrink_dcache_sb(struct super_block *sb)
1110{
1111 LIST_HEAD(tmp);
1112
1113 spin_lock(&dcache_lru_lock);
1114 while (!list_empty(&sb->s_dentry_lru)) {
1115 list_splice_init(&sb->s_dentry_lru, &tmp);
1116 spin_unlock(&dcache_lru_lock);
1117 shrink_dentry_list(&tmp);
1118 spin_lock(&dcache_lru_lock);
1119 }
1120 spin_unlock(&dcache_lru_lock);
1121}
1122EXPORT_SYMBOL(shrink_dcache_sb);
1123
1124struct prune_negative_ctrl
1125{
1126 unsigned long prune_count;
1127 int prune_percent;
1128};
1129
1130
1131
1132
1133static void prune_negative_one_sb(struct super_block *sb, void *arg)
1134{
1135 unsigned long count = sb->s_nr_dentry_unused;
1136 struct prune_negative_ctrl *ctrl = arg;
1137 long scan = (count * ctrl->prune_percent) / 10000;
1138
1139 if (scan)
1140 ctrl->prune_count += __prune_dcache_sb(sb, scan, true);
1141}
1142
1143
1144
1145
1146static void prune_negative_dentry(struct work_struct *work)
1147{
1148 long count = get_nr_dentry_negative();
1149 long limit = negative_dentry_limit;
1150 struct prune_negative_ctrl ctrl;
1151 unsigned long start;
1152
1153 if (!limit || count <= limit)
1154 goto requeue_work;
1155
1156
1157
1158
1159
1160 ctrl.prune_count = 0;
1161 ctrl.prune_percent = ((count - limit) * 10000 / count) + 100;
1162 start = jiffies;
1163
1164
1165
1166
1167
1168 iterate_supers(prune_negative_one_sb, &ctrl);
1169
1170
1171
1172
1173 pr_debug("%ld negative dentries freed in %d ms\n",
1174 ctrl.prune_count, jiffies_to_msecs(jiffies - start));
1175
1176requeue_work:
1177
1178
1179
1180
1181
1182 schedule_delayed_work(&prune_negative_dentry_work,
1183 NEGATIVE_DENTRY_CHECK_PERIOD);
1184}
1185
1186
1187
1188
1189
1190
1191#define RESCHED_CHECK_BATCH 1024
1192static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
1193{
1194 struct dentry *parent;
1195 int batch = RESCHED_CHECK_BATCH;
1196
1197 BUG_ON(!IS_ROOT(dentry));
1198
1199 for (;;) {
1200
1201 while (!list_empty(&dentry->d_subdirs))
1202 dentry = list_entry(dentry->d_subdirs.next,
1203 struct dentry, d_u.d_child);
1204
1205
1206
1207 do {
1208 struct inode *inode;
1209
1210
1211
1212
1213
1214 if ((dentry->d_flags & DCACHE_OP_PRUNE) &&
1215 !d_unhashed(dentry))
1216 dentry->d_op->d_prune(dentry);
1217
1218 dentry_lru_del(dentry);
1219 __d_shrink(dentry);
1220
1221 if (dentry->d_lockref.count != 0) {
1222 printk(KERN_ERR
1223 "BUG: Dentry %p{i=%lx,n=%s}"
1224 " still in use (%d)"
1225 " [unmount of %s %s]\n",
1226 dentry,
1227 dentry->d_inode ?
1228 dentry->d_inode->i_ino : 0UL,
1229 dentry->d_name.name,
1230 dentry->d_lockref.count,
1231 dentry->d_sb->s_type->name,
1232 dentry->d_sb->s_id);
1233 BUG();
1234 }
1235
1236 if (IS_ROOT(dentry)) {
1237 parent = NULL;
1238 list_del(&dentry->d_u.d_child);
1239 } else {
1240 parent = dentry->d_parent;
1241 parent->d_lockref.count--;
1242 list_del(&dentry->d_u.d_child);
1243 }
1244
1245 inode = dentry->d_inode;
1246 if (inode) {
1247 dentry->d_inode = NULL;
1248 hlist_del_init(&dentry->d_alias);
1249 if (dentry->d_op && dentry->d_op->d_iput)
1250 dentry->d_op->d_iput(dentry, inode);
1251 else
1252 iput(inode);
1253 }
1254
1255 d_free(dentry);
1256
1257
1258
1259
1260 if (!parent)
1261 return;
1262 dentry = parent;
1263 if (!--batch) {
1264 cond_resched();
1265 batch = RESCHED_CHECK_BATCH;
1266 }
1267 } while (list_empty(&dentry->d_subdirs));
1268
1269 dentry = list_entry(dentry->d_subdirs.next,
1270 struct dentry, d_u.d_child);
1271 }
1272}
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284void shrink_dcache_for_umount(struct super_block *sb)
1285{
1286 struct dentry *dentry;
1287
1288 if (down_read_trylock(&sb->s_umount))
1289 BUG();
1290
1291 dentry = sb->s_root;
1292 sb->s_root = NULL;
1293 dentry->d_lockref.count--;
1294 shrink_dcache_for_umount_subtree(dentry);
1295
1296 while (!hlist_bl_empty(&sb->s_anon)) {
1297 dentry = hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash);
1298 shrink_dcache_for_umount_subtree(dentry);
1299 }
1300}
1301
1302
1303
1304
1305
1306
1307
1308
1309enum d_walk_ret {
1310 D_WALK_CONTINUE,
1311 D_WALK_QUIT,
1312 D_WALK_NORETRY,
1313 D_WALK_SKIP,
1314};
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325static void d_walk(struct dentry *parent, void *data,
1326 enum d_walk_ret (*enter)(void *, struct dentry *),
1327 void (*finish)(void *))
1328{
1329 struct dentry *this_parent;
1330 struct list_head *next;
1331 unsigned seq = 0;
1332 enum d_walk_ret ret;
1333 bool retry = true;
1334
1335again:
1336 read_seqbegin_or_lock(&rename_lock, &seq);
1337 this_parent = parent;
1338 spin_lock(&this_parent->d_lock);
1339
1340 ret = enter(data, this_parent);
1341 switch (ret) {
1342 case D_WALK_CONTINUE:
1343 break;
1344 case D_WALK_QUIT:
1345 case D_WALK_SKIP:
1346 goto out_unlock;
1347 case D_WALK_NORETRY:
1348 retry = false;
1349 break;
1350 }
1351repeat:
1352 next = this_parent->d_subdirs.next;
1353resume:
1354 while (next != &this_parent->d_subdirs) {
1355 struct list_head *tmp = next;
1356 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1357 next = tmp->next;
1358
1359 if (unlikely(dentry->d_flags & DCACHE_DENTRY_CURSOR))
1360 continue;
1361
1362 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1363
1364 ret = enter(data, dentry);
1365 switch (ret) {
1366 case D_WALK_CONTINUE:
1367 break;
1368 case D_WALK_QUIT:
1369 spin_unlock(&dentry->d_lock);
1370 goto out_unlock;
1371 case D_WALK_NORETRY:
1372 retry = false;
1373 break;
1374 case D_WALK_SKIP:
1375 spin_unlock(&dentry->d_lock);
1376 continue;
1377 }
1378
1379 if (!list_empty(&dentry->d_subdirs)) {
1380 spin_unlock(&this_parent->d_lock);
1381 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
1382 this_parent = dentry;
1383 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1384 goto repeat;
1385 }
1386 spin_unlock(&dentry->d_lock);
1387 }
1388
1389
1390
1391 rcu_read_lock();
1392ascend:
1393 if (this_parent != parent) {
1394 struct dentry *child = this_parent;
1395 this_parent = child->d_parent;
1396
1397 spin_unlock(&child->d_lock);
1398 spin_lock(&this_parent->d_lock);
1399
1400
1401 if (need_seqretry(&rename_lock, seq))
1402 goto rename_retry;
1403
1404 do {
1405 next = child->d_u.d_child.next;
1406 if (next == &this_parent->d_subdirs)
1407 goto ascend;
1408 child = list_entry(next, struct dentry, d_u.d_child);
1409 } while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
1410 rcu_read_unlock();
1411 goto resume;
1412 }
1413 if (need_seqretry(&rename_lock, seq))
1414 goto rename_retry;
1415 rcu_read_unlock();
1416 if (finish)
1417 finish(data);
1418
1419out_unlock:
1420 spin_unlock(&this_parent->d_lock);
1421 done_seqretry(&rename_lock, seq);
1422 return;
1423
1424rename_retry:
1425 spin_unlock(&this_parent->d_lock);
1426 rcu_read_unlock();
1427 BUG_ON(seq & 1);
1428 if (!retry)
1429 return;
1430 seq = 1;
1431 goto again;
1432}
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448static enum d_walk_ret check_mount(void *data, struct dentry *dentry)
1449{
1450 int *ret = data;
1451 if (d_mountpoint(dentry)) {
1452 *ret = 1;
1453 return D_WALK_QUIT;
1454 }
1455 return D_WALK_CONTINUE;
1456}
1457
1458int have_submounts(struct dentry *parent)
1459{
1460 int ret = 0;
1461
1462 d_walk(parent, &ret, check_mount, NULL);
1463
1464 return ret;
1465}
1466EXPORT_SYMBOL(have_submounts);
1467
1468struct check_mount {
1469 struct vfsmount *mnt;
1470 unsigned int mounted;
1471};
1472
1473static enum d_walk_ret path_check_mount(void *data, struct dentry *dentry)
1474{
1475 struct check_mount *info = data;
1476 struct path path = { .mnt = info->mnt, .dentry = dentry };
1477
1478 if (likely(!d_mountpoint(dentry)))
1479 return D_WALK_CONTINUE;
1480 if (__path_is_mountpoint(&path)) {
1481 info->mounted = 1;
1482 return D_WALK_QUIT;
1483 }
1484 return D_WALK_CONTINUE;
1485}
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495int path_has_submounts(const struct path *parent)
1496{
1497 struct check_mount data = { .mnt = parent->mnt, .mounted = 0 };
1498
1499 read_seqlock_excl(&mount_lock);
1500 d_walk(parent->dentry, &data, path_check_mount, NULL);
1501 read_sequnlock_excl(&mount_lock);
1502
1503 return data.mounted;
1504}
1505EXPORT_SYMBOL(path_has_submounts);
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515int d_set_mounted(struct dentry *dentry)
1516{
1517 struct dentry *p;
1518 int ret = -ENOENT;
1519 write_seqlock(&rename_lock);
1520 for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
1521
1522 spin_lock(&p->d_lock);
1523 if (unlikely(d_unhashed(p))) {
1524 spin_unlock(&p->d_lock);
1525 goto out;
1526 }
1527 spin_unlock(&p->d_lock);
1528 }
1529 spin_lock(&dentry->d_lock);
1530 if (!d_unlinked(dentry)) {
1531 ret = -EBUSY;
1532 if (!d_mountpoint(dentry)) {
1533 dentry->d_flags |= DCACHE_MOUNTED;
1534 ret = 0;
1535 }
1536 }
1537 spin_unlock(&dentry->d_lock);
1538out:
1539 write_sequnlock(&rename_lock);
1540 return ret;
1541}
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558struct select_data {
1559 struct dentry *start;
1560 struct list_head dispose;
1561 int found;
1562};
1563
1564static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
1565{
1566 struct select_data *data = _data;
1567 enum d_walk_ret ret = D_WALK_CONTINUE;
1568
1569 if (data->start == dentry)
1570 goto out;
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580 if (dentry->d_lockref.count) {
1581 dentry_lru_del(dentry);
1582 } else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
1583 dentry_lru_move_list(dentry, &data->dispose);
1584 dentry->d_flags |= DCACHE_SHRINK_LIST;
1585 data->found++;
1586 ret = D_WALK_NORETRY;
1587 }
1588
1589
1590
1591
1592
1593 if (data->found && need_resched())
1594 ret = D_WALK_QUIT;
1595out:
1596 return ret;
1597}
1598
1599
1600
1601
1602
1603
1604
1605void shrink_dcache_parent(struct dentry *parent)
1606{
1607 for (;;) {
1608 struct select_data data;
1609
1610 INIT_LIST_HEAD(&data.dispose);
1611 data.start = parent;
1612 data.found = 0;
1613
1614 d_walk(parent, &data, select_collect, NULL);
1615 if (!data.found)
1616 break;
1617
1618 shrink_dentry_list(&data.dispose);
1619 }
1620}
1621EXPORT_SYMBOL(shrink_dcache_parent);
1622
1623struct detach_data {
1624 struct select_data select;
1625 struct dentry *mountpoint;
1626};
1627static enum d_walk_ret detach_and_collect(void *_data, struct dentry *dentry)
1628{
1629 struct detach_data *data = _data;
1630
1631 if (d_mountpoint(dentry)) {
1632 __dget_dlock(dentry);
1633 data->mountpoint = dentry;
1634 return D_WALK_QUIT;
1635 }
1636
1637 return select_collect(&data->select, dentry);
1638}
1639
1640static void check_and_drop(void *_data)
1641{
1642 struct detach_data *data = _data;
1643
1644 if (!data->mountpoint && !data->select.found)
1645 __d_drop(data->select.start);
1646}
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658int d_invalidate(struct dentry *dentry)
1659{
1660
1661
1662
1663 spin_lock(&dentry->d_lock);
1664 if (d_unhashed(dentry)) {
1665 spin_unlock(&dentry->d_lock);
1666 return 0;
1667 }
1668 spin_unlock(&dentry->d_lock);
1669
1670
1671 if (!dentry->d_inode) {
1672 d_drop(dentry);
1673 return 0;
1674 }
1675
1676 for (;;) {
1677 struct detach_data data;
1678
1679 data.mountpoint = NULL;
1680 INIT_LIST_HEAD(&data.select.dispose);
1681 data.select.start = dentry;
1682 data.select.found = 0;
1683
1684 d_walk(dentry, &data, detach_and_collect, check_and_drop);
1685
1686 if (data.select.found)
1687 shrink_dentry_list(&data.select.dispose);
1688
1689 if (data.mountpoint) {
1690 if (may_detach_mounts) {
1691 detach_mounts(data.mountpoint);
1692 dput(data.mountpoint);
1693 } else {
1694 dput(data.mountpoint);
1695 return -EBUSY;
1696 }
1697 }
1698
1699 if (!data.mountpoint && !data.select.found)
1700 return 0;
1701 }
1702}
1703EXPORT_SYMBOL(d_invalidate);
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
1716{
1717 struct dentry *dentry;
1718 char *dname;
1719
1720 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
1721 if (!dentry)
1722 return NULL;
1723
1724
1725
1726
1727
1728
1729
1730 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
1731 if (name->len > DNAME_INLINE_LEN-1) {
1732 dname = kmalloc(name->len + 1, GFP_KERNEL);
1733 if (!dname) {
1734 kmem_cache_free(dentry_cache, dentry);
1735 return NULL;
1736 }
1737 } else {
1738 dname = dentry->d_iname;
1739 }
1740
1741 dentry->d_name.len = name->len;
1742 dentry->d_name.hash = name->hash;
1743 memcpy(dname, name->name, name->len);
1744 dname[name->len] = 0;
1745
1746
1747 smp_wmb();
1748 dentry->d_name.name = dname;
1749
1750 dentry->d_lockref.count = 1;
1751 dentry->d_flags = 0;
1752 spin_lock_init(&dentry->d_lock);
1753 seqcount_init(&dentry->d_seq);
1754 dentry->d_inode = NULL;
1755 dentry->d_parent = dentry;
1756 dentry->d_sb = sb;
1757 dentry->d_op = NULL;
1758 dentry->d_fsdata = NULL;
1759 INIT_HLIST_BL_NODE(&dentry->d_hash);
1760 INIT_LIST_HEAD(&dentry->d_lru);
1761 INIT_LIST_HEAD(&dentry->d_subdirs);
1762 INIT_HLIST_NODE(&dentry->d_alias);
1763 INIT_LIST_HEAD(&dentry->d_u.d_child);
1764 d_set_d_op(dentry, dentry->d_sb->s_d_op);
1765
1766 this_cpu_inc(nr_dentry);
1767
1768 return dentry;
1769}
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1781{
1782 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1783 if (!dentry)
1784 return NULL;
1785
1786 dentry->d_flags |= DCACHE_RCUACCESS;
1787 spin_lock(&parent->d_lock);
1788
1789
1790
1791
1792 __dget_dlock(parent);
1793 dentry->d_parent = parent;
1794 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
1795 spin_unlock(&parent->d_lock);
1796
1797 return dentry;
1798}
1799EXPORT_SYMBOL(d_alloc);
1800
1801struct dentry *d_alloc_anon(struct super_block *sb)
1802{
1803 static const struct qstr anonstring = QSTR_INIT("/", 1);
1804
1805 return __d_alloc(sb, &anonstring);
1806}
1807EXPORT_SYMBOL(d_alloc_anon);
1808
1809struct dentry *d_alloc_cursor(struct dentry * parent)
1810{
1811 struct dentry *dentry = d_alloc_anon(parent->d_sb);
1812 if (dentry) {
1813 dentry->d_flags |= DCACHE_RCUACCESS | DCACHE_DENTRY_CURSOR;
1814 dentry->d_parent = dget(parent);
1815 }
1816 return dentry;
1817}
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1828{
1829 return __d_alloc(sb, name);
1830}
1831EXPORT_SYMBOL(d_alloc_pseudo);
1832
1833struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1834{
1835 struct qstr q;
1836
1837 q.name = name;
1838 q.len = strlen(name);
1839 q.hash = full_name_hash(q.name, q.len);
1840 return d_alloc(parent, &q);
1841}
1842EXPORT_SYMBOL(d_alloc_name);
1843
1844void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1845{
1846 WARN_ON_ONCE(dentry->d_op);
1847 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
1848 DCACHE_OP_COMPARE |
1849 DCACHE_OP_REVALIDATE |
1850 DCACHE_OP_WEAK_REVALIDATE |
1851 DCACHE_OP_DELETE |
1852 DCACHE_OP_REAL));
1853 dentry->d_op = op;
1854 if (!op)
1855 return;
1856 if (op->d_hash)
1857 dentry->d_flags |= DCACHE_OP_HASH;
1858 if (op->d_compare)
1859 dentry->d_flags |= DCACHE_OP_COMPARE;
1860 if (op->d_revalidate)
1861 dentry->d_flags |= DCACHE_OP_REVALIDATE;
1862 if (op->d_weak_revalidate)
1863 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
1864 if (op->d_delete)
1865 dentry->d_flags |= DCACHE_OP_DELETE;
1866 if (op->d_prune)
1867 dentry->d_flags |= DCACHE_OP_PRUNE;
1868
1869 if (get_real_dop(dentry))
1870 dentry->d_flags |= DCACHE_OP_REAL;
1871}
1872EXPORT_SYMBOL(d_set_d_op);
1873
1874static unsigned d_flags_for_inode(struct inode *inode)
1875{
1876 unsigned add_flags = DCACHE_FILE_TYPE;
1877
1878 if (!inode)
1879 return DCACHE_MISS_TYPE;
1880
1881 if (S_ISDIR(inode->i_mode)) {
1882 add_flags = DCACHE_DIRECTORY_TYPE;
1883 if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) {
1884 if (unlikely(!inode->i_op->lookup))
1885 add_flags = DCACHE_AUTODIR_TYPE;
1886 else
1887 inode->i_opflags |= IOP_LOOKUP;
1888 }
1889 } else if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1890 if (unlikely(inode->i_op->follow_link))
1891 add_flags = DCACHE_SYMLINK_TYPE;
1892 else
1893 inode->i_opflags |= IOP_NOFOLLOW;
1894 }
1895
1896 if (unlikely(IS_AUTOMOUNT(inode)))
1897 add_flags |= DCACHE_NEED_AUTOMOUNT;
1898 return add_flags;
1899}
1900
1901static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1902{
1903 unsigned add_flags = d_flags_for_inode(inode);
1904
1905 spin_lock(&dentry->d_lock);
1906
1907
1908
1909 if (dentry->d_flags & DCACHE_LRU_LIST)
1910 this_cpu_dec(nr_dentry_negative);
1911 __d_set_type(dentry, add_flags);
1912 if (inode)
1913 hlist_add_head(&dentry->d_alias, &inode->i_dentry);
1914 dentry->d_inode = inode;
1915 dentry_rcuwalk_invalidate(dentry);
1916 if (inode)
1917 fsnotify_update_flags(dentry);
1918 spin_unlock(&dentry->d_lock);
1919}
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936void d_instantiate(struct dentry *entry, struct inode * inode)
1937{
1938 BUG_ON(!hlist_unhashed(&entry->d_alias));
1939 if (inode)
1940 spin_lock(&inode->i_lock);
1941 __d_instantiate(entry, inode);
1942 if (inode)
1943 spin_unlock(&inode->i_lock);
1944 security_d_instantiate(entry, inode);
1945}
1946EXPORT_SYMBOL(d_instantiate);
1947
1948
1949
1950
1951
1952
1953
1954void d_instantiate_new(struct dentry *entry, struct inode *inode)
1955{
1956 BUG_ON(!hlist_unhashed(&entry->d_alias));
1957 BUG_ON(!inode);
1958 lockdep_annotate_inode_mutex_key(inode);
1959 spin_lock(&inode->i_lock);
1960 __d_instantiate(entry, inode);
1961 WARN_ON(!(inode->i_state & I_NEW));
1962 inode->i_state &= ~I_NEW & ~I_CREATING;
1963 smp_mb();
1964 wake_up_bit(&inode->i_state, __I_NEW);
1965 spin_unlock(&inode->i_lock);
1966 security_d_instantiate(entry, inode);
1967}
1968EXPORT_SYMBOL(d_instantiate_new);
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986static struct dentry *__d_instantiate_unique(struct dentry *entry,
1987 struct inode *inode)
1988{
1989 struct dentry *alias;
1990 int len = entry->d_name.len;
1991 const char *name = entry->d_name.name;
1992 unsigned int hash = entry->d_name.hash;
1993
1994 if (!inode) {
1995 __d_instantiate(entry, NULL);
1996 return NULL;
1997 }
1998
1999 hlist_for_each_entry(alias, &inode->i_dentry, d_alias) {
2000
2001
2002
2003
2004
2005 if (alias->d_name.hash != hash)
2006 continue;
2007 if (alias->d_parent != entry->d_parent)
2008 continue;
2009 if (alias->d_name.len != len)
2010 continue;
2011 if (dentry_cmp(alias, name, len))
2012 continue;
2013 __dget(alias);
2014 return alias;
2015 }
2016
2017 __d_instantiate(entry, inode);
2018 return NULL;
2019}
2020
2021struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
2022{
2023 struct dentry *result;
2024
2025 BUG_ON(!hlist_unhashed(&entry->d_alias));
2026
2027 if (inode)
2028 spin_lock(&inode->i_lock);
2029 result = __d_instantiate_unique(entry, inode);
2030 if (inode)
2031 spin_unlock(&inode->i_lock);
2032
2033 if (!result) {
2034 security_d_instantiate(entry, inode);
2035 return NULL;
2036 }
2037
2038 BUG_ON(!d_unhashed(result));
2039 iput(inode);
2040 return result;
2041}
2042
2043EXPORT_SYMBOL(d_instantiate_unique);
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054int d_instantiate_no_diralias(struct dentry *entry, struct inode *inode)
2055{
2056 BUG_ON(!hlist_unhashed(&entry->d_alias));
2057
2058 spin_lock(&inode->i_lock);
2059 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry)) {
2060 spin_unlock(&inode->i_lock);
2061 iput(inode);
2062 return -EBUSY;
2063 }
2064 __d_instantiate(entry, inode);
2065 spin_unlock(&inode->i_lock);
2066 security_d_instantiate(entry, inode);
2067
2068 return 0;
2069}
2070EXPORT_SYMBOL(d_instantiate_no_diralias);
2071
2072struct dentry *d_make_root(struct inode *root_inode)
2073{
2074 struct dentry *res = NULL;
2075
2076 if (root_inode) {
2077 res = d_alloc_anon(root_inode->i_sb);
2078 if (res)
2079 d_instantiate(res, root_inode);
2080 else
2081 iput(root_inode);
2082 }
2083 return res;
2084}
2085EXPORT_SYMBOL(d_make_root);
2086
2087static struct dentry * __d_find_any_alias(struct inode *inode)
2088{
2089 struct dentry *alias;
2090
2091 if (hlist_empty(&inode->i_dentry))
2092 return NULL;
2093 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
2094 __dget(alias);
2095 return alias;
2096}
2097
2098
2099
2100
2101
2102
2103
2104
2105struct dentry *d_find_any_alias(struct inode *inode)
2106{
2107 struct dentry *de;
2108
2109 spin_lock(&inode->i_lock);
2110 de = __d_find_any_alias(inode);
2111 spin_unlock(&inode->i_lock);
2112 return de;
2113}
2114EXPORT_SYMBOL(d_find_any_alias);
2115
2116struct dentry *d_instantiate_anon(struct dentry *dentry, struct inode *inode)
2117{
2118 struct dentry *res;
2119 unsigned add_flags;
2120
2121 spin_lock(&inode->i_lock);
2122 res = __d_find_any_alias(inode);
2123 if (res) {
2124 spin_unlock(&inode->i_lock);
2125 security_d_instantiate(res, inode);
2126 dput(dentry);
2127 goto out_iput;
2128 }
2129
2130
2131 add_flags = d_flags_for_inode(inode) | DCACHE_DISCONNECTED;
2132
2133 spin_lock(&dentry->d_lock);
2134 dentry->d_inode = inode;
2135 dentry->d_flags |= add_flags;
2136 hlist_add_head(&dentry->d_alias, &inode->i_dentry);
2137 hlist_bl_lock(&dentry->d_sb->s_anon);
2138 hlist_bl_add_head(&dentry->d_hash, &dentry->d_sb->s_anon);
2139 hlist_bl_unlock(&dentry->d_sb->s_anon);
2140 spin_unlock(&dentry->d_lock);
2141 spin_unlock(&inode->i_lock);
2142 security_d_instantiate(dentry, inode);
2143
2144 return dentry;
2145
2146 out_iput:
2147 iput(inode);
2148 return res;
2149}
2150EXPORT_SYMBOL(d_instantiate_anon);
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170struct dentry *d_obtain_alias(struct inode *inode)
2171{
2172 struct dentry *tmp;
2173 struct dentry *res;
2174
2175 if (!inode)
2176 return ERR_PTR(-ESTALE);
2177 if (IS_ERR(inode))
2178 return ERR_CAST(inode);
2179
2180 res = d_find_any_alias(inode);
2181 if (res)
2182 goto out_iput;
2183
2184 tmp = d_alloc_anon(inode->i_sb);
2185 if (!tmp) {
2186 res = ERR_PTR(-ENOMEM);
2187 goto out_iput;
2188 }
2189
2190 return d_instantiate_anon(tmp, inode);
2191
2192 out_iput:
2193 if (res && !IS_ERR(res))
2194 security_d_instantiate(res, inode);
2195 iput(inode);
2196 return res;
2197}
2198EXPORT_SYMBOL(d_obtain_alias);
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
2217 struct qstr *name)
2218{
2219 struct dentry *found;
2220 struct dentry *new;
2221
2222
2223
2224
2225
2226 found = d_hash_and_lookup(dentry->d_parent, name);
2227 if (unlikely(IS_ERR(found)))
2228 goto err_out;
2229 if (!found) {
2230 new = d_alloc(dentry->d_parent, name);
2231 if (!new) {
2232 found = ERR_PTR(-ENOMEM);
2233 goto err_out;
2234 }
2235
2236 found = d_splice_alias(inode, new);
2237 if (found) {
2238 dput(new);
2239 return found;
2240 }
2241 return new;
2242 }
2243
2244
2245
2246
2247
2248
2249
2250 if (found->d_inode) {
2251 if (unlikely(found->d_inode != inode)) {
2252
2253 BUG_ON(!is_bad_inode(inode));
2254 BUG_ON(!is_bad_inode(found->d_inode));
2255 }
2256 iput(inode);
2257 return found;
2258 }
2259
2260
2261
2262
2263
2264 new = d_splice_alias(inode, found);
2265 if (new) {
2266 dput(found);
2267 found = new;
2268 }
2269 return found;
2270
2271err_out:
2272 iput(inode);
2273 return found;
2274}
2275EXPORT_SYMBOL(d_add_ci);
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291enum slow_d_compare {
2292 D_COMP_OK,
2293 D_COMP_NOMATCH,
2294 D_COMP_SEQRETRY,
2295};
2296
2297static noinline enum slow_d_compare slow_dentry_cmp(
2298 const struct dentry *parent,
2299 struct dentry *dentry,
2300 unsigned int seq,
2301 const struct qstr *name)
2302{
2303 int tlen = dentry->d_name.len;
2304 const char *tname = dentry->d_name.name;
2305
2306 if (read_seqcount_retry(&dentry->d_seq, seq)) {
2307 cpu_relax();
2308 return D_COMP_SEQRETRY;
2309 }
2310 if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
2311 return D_COMP_NOMATCH;
2312 return D_COMP_OK;
2313}
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344struct dentry *__d_lookup_rcu(const struct dentry *parent,
2345 const struct qstr *name,
2346 unsigned *seqp)
2347{
2348 u64 hashlen = name->hash_len;
2349 const unsigned char *str = name->name;
2350 struct hlist_bl_head *b = d_hash(parent, hashlen_hash(hashlen));
2351 struct hlist_bl_node *node;
2352 struct dentry *dentry;
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2375 unsigned seq;
2376
2377seqretry:
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392 seq = raw_seqcount_begin(&dentry->d_seq);
2393 if (dentry->d_parent != parent)
2394 continue;
2395 if (d_unhashed(dentry))
2396 continue;
2397
2398 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
2399 if (dentry->d_name.hash != hashlen_hash(hashlen))
2400 continue;
2401 *seqp = seq;
2402 switch (slow_dentry_cmp(parent, dentry, seq, name)) {
2403 case D_COMP_OK:
2404 return dentry;
2405 case D_COMP_NOMATCH:
2406 continue;
2407 default:
2408 goto seqretry;
2409 }
2410 }
2411
2412 if (dentry->d_name.hash_len != hashlen)
2413 continue;
2414 *seqp = seq;
2415 if (!dentry_cmp(dentry, str, hashlen_len(hashlen)))
2416 return dentry;
2417 }
2418 return NULL;
2419}
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
2433{
2434 struct dentry *dentry;
2435 unsigned seq;
2436
2437 do {
2438 seq = read_seqbegin(&rename_lock);
2439 dentry = __d_lookup(parent, name);
2440 if (dentry)
2441 break;
2442 } while (read_seqretry(&rename_lock, seq));
2443 return dentry;
2444}
2445EXPORT_SYMBOL(d_lookup);
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
2463{
2464 unsigned int len = name->len;
2465 unsigned int hash = name->hash;
2466 const unsigned char *str = name->name;
2467 struct hlist_bl_head *b = d_hash(parent, hash);
2468 struct hlist_bl_node *node;
2469 struct dentry *found = NULL;
2470 struct dentry *dentry;
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492 rcu_read_lock();
2493
2494 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2495
2496 if (dentry->d_name.hash != hash)
2497 continue;
2498
2499 spin_lock(&dentry->d_lock);
2500 if (dentry->d_parent != parent)
2501 goto next;
2502 if (d_unhashed(dentry))
2503 goto next;
2504
2505
2506
2507
2508
2509 if (parent->d_flags & DCACHE_OP_COMPARE) {
2510 int tlen = dentry->d_name.len;
2511 const char *tname = dentry->d_name.name;
2512 if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
2513 goto next;
2514 } else {
2515 if (dentry->d_name.len != len)
2516 goto next;
2517 if (dentry_cmp(dentry, str, len))
2518 goto next;
2519 }
2520
2521 dentry->d_lockref.count++;
2522 found = dentry;
2523 spin_unlock(&dentry->d_lock);
2524 break;
2525next:
2526 spin_unlock(&dentry->d_lock);
2527 }
2528 rcu_read_unlock();
2529
2530 return found;
2531}
2532
2533
2534
2535
2536
2537
2538
2539
2540struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2541{
2542
2543
2544
2545
2546
2547 name->hash = full_name_hash(name->name, name->len);
2548 if (dir->d_flags & DCACHE_OP_HASH) {
2549 int err = dir->d_op->d_hash(dir, name);
2550 if (unlikely(err < 0))
2551 return ERR_PTR(err);
2552 }
2553 return d_lookup(dir, name);
2554}
2555EXPORT_SYMBOL(d_hash_and_lookup);
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568int d_validate(struct dentry *dentry, struct dentry *dparent)
2569{
2570 struct dentry *child;
2571
2572 spin_lock(&dparent->d_lock);
2573 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
2574 if (dentry == child) {
2575 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2576 __dget_dlock(dentry);
2577 spin_unlock(&dentry->d_lock);
2578 spin_unlock(&dparent->d_lock);
2579 return 1;
2580 }
2581 }
2582 spin_unlock(&dparent->d_lock);
2583
2584 return 0;
2585}
2586EXPORT_SYMBOL(d_validate);
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609void d_delete(struct dentry * dentry)
2610{
2611 struct inode *inode;
2612 int isdir = 0;
2613
2614
2615
2616again:
2617 spin_lock(&dentry->d_lock);
2618 inode = dentry->d_inode;
2619 isdir = S_ISDIR(inode->i_mode);
2620 if (dentry->d_lockref.count == 1) {
2621 if (!spin_trylock(&inode->i_lock)) {
2622 spin_unlock(&dentry->d_lock);
2623 cpu_relax();
2624 goto again;
2625 }
2626 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
2627 dentry_unlink_inode(dentry);
2628 fsnotify_nameremove(dentry, isdir);
2629 return;
2630 }
2631
2632 if (!d_unhashed(dentry))
2633 __d_drop(dentry);
2634
2635 spin_unlock(&dentry->d_lock);
2636
2637 fsnotify_nameremove(dentry, isdir);
2638}
2639EXPORT_SYMBOL(d_delete);
2640
2641static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b)
2642{
2643 BUG_ON(!d_unhashed(entry));
2644 hlist_bl_lock(b);
2645 hlist_bl_add_head_rcu(&entry->d_hash, b);
2646 hlist_bl_unlock(b);
2647}
2648
2649static void _d_rehash(struct dentry * entry)
2650{
2651 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
2652}
2653
2654
2655
2656
2657
2658
2659
2660
2661void d_rehash(struct dentry * entry)
2662{
2663 spin_lock(&entry->d_lock);
2664 _d_rehash(entry);
2665 spin_unlock(&entry->d_lock);
2666}
2667EXPORT_SYMBOL(d_rehash);
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
2684{
2685 BUG_ON(!mutex_is_locked(&dentry->d_parent->d_inode->i_mutex));
2686 BUG_ON(dentry->d_name.len != name->len);
2687
2688 spin_lock(&dentry->d_lock);
2689 write_seqcount_begin(&dentry->d_seq);
2690 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
2691 write_seqcount_end(&dentry->d_seq);
2692 spin_unlock(&dentry->d_lock);
2693}
2694EXPORT_SYMBOL(dentry_update_name_case);
2695
2696static void switch_names(struct dentry *dentry, struct dentry *target)
2697{
2698 if (dname_external(target)) {
2699 if (dname_external(dentry)) {
2700
2701
2702
2703 swap(target->d_name.name, dentry->d_name.name);
2704 } else {
2705
2706
2707
2708
2709 memcpy(target->d_iname, dentry->d_name.name,
2710 dentry->d_name.len + 1);
2711 dentry->d_name.name = target->d_name.name;
2712 target->d_name.name = target->d_iname;
2713 }
2714 } else {
2715 if (dname_external(dentry)) {
2716
2717
2718
2719
2720 memcpy(dentry->d_iname, target->d_name.name,
2721 target->d_name.len + 1);
2722 target->d_name.name = dentry->d_name.name;
2723 dentry->d_name.name = dentry->d_iname;
2724 } else {
2725
2726
2727
2728 unsigned int i;
2729 BUILD_BUG_ON(!IS_ALIGNED(DNAME_INLINE_LEN, sizeof(long)));
2730 for (i = 0; i < DNAME_INLINE_LEN / sizeof(long); i++) {
2731 swap(((long *) &dentry->d_iname)[i],
2732 ((long *) &target->d_iname)[i]);
2733 }
2734 }
2735 }
2736 swap(dentry->d_name.len, target->d_name.len);
2737}
2738
2739static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
2740{
2741
2742
2743
2744 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
2745 spin_lock(&target->d_parent->d_lock);
2746 else {
2747 if (d_ancestor(dentry->d_parent, target->d_parent)) {
2748 spin_lock(&dentry->d_parent->d_lock);
2749 spin_lock_nested(&target->d_parent->d_lock,
2750 DENTRY_D_LOCK_NESTED);
2751 } else {
2752 spin_lock(&target->d_parent->d_lock);
2753 spin_lock_nested(&dentry->d_parent->d_lock,
2754 DENTRY_D_LOCK_NESTED);
2755 }
2756 }
2757 if (target < dentry) {
2758 spin_lock_nested(&target->d_lock, 2);
2759 spin_lock_nested(&dentry->d_lock, 3);
2760 } else {
2761 spin_lock_nested(&dentry->d_lock, 2);
2762 spin_lock_nested(&target->d_lock, 3);
2763 }
2764}
2765
2766static void dentry_unlock_parents_for_move(struct dentry *dentry,
2767 struct dentry *target)
2768{
2769 if (target->d_parent != dentry->d_parent)
2770 spin_unlock(&dentry->d_parent->d_lock);
2771 if (target->d_parent != target)
2772 spin_unlock(&target->d_parent->d_lock);
2773}
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797static void __d_move(struct dentry *dentry, struct dentry *target,
2798 bool exchange)
2799{
2800 if (!dentry->d_inode)
2801 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2802
2803 BUG_ON(d_ancestor(dentry, target));
2804 BUG_ON(d_ancestor(target, dentry));
2805
2806 dentry_lock_for_move(dentry, target);
2807
2808 write_seqcount_begin(&dentry->d_seq);
2809 write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED);
2810
2811
2812
2813
2814
2815
2816
2817 __d_hash_move(dentry, d_hash(target->d_parent, target->d_name.hash));
2818
2819
2820
2821
2822
2823 if (exchange) {
2824 __d_hash_move(target,
2825 d_hash(dentry->d_parent, dentry->d_name.hash));
2826 } else {
2827 __d_drop(target);
2828 }
2829
2830 list_del(&dentry->d_u.d_child);
2831 list_del(&target->d_u.d_child);
2832
2833
2834 switch_names(dentry, target);
2835 swap(dentry->d_name.hash, target->d_name.hash);
2836
2837
2838 if (IS_ROOT(dentry)) {
2839 dentry->d_flags |= DCACHE_RCUACCESS;
2840 dentry->d_parent = target->d_parent;
2841 target->d_parent = target;
2842 INIT_LIST_HEAD(&target->d_u.d_child);
2843 } else {
2844 swap(dentry->d_parent, target->d_parent);
2845
2846
2847 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
2848 }
2849
2850 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2851
2852 write_seqcount_end(&target->d_seq);
2853 write_seqcount_end(&dentry->d_seq);
2854
2855 dentry_unlock_parents_for_move(dentry, target);
2856 if (exchange)
2857 fsnotify_update_flags(target);
2858 spin_unlock(&target->d_lock);
2859 fsnotify_update_flags(dentry);
2860 spin_unlock(&dentry->d_lock);
2861}
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872void d_move(struct dentry *dentry, struct dentry *target)
2873{
2874 write_seqlock(&rename_lock);
2875 __d_move(dentry, target, false);
2876 write_sequnlock(&rename_lock);
2877}
2878EXPORT_SYMBOL(d_move);
2879
2880
2881
2882
2883
2884
2885void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
2886{
2887 write_seqlock(&rename_lock);
2888
2889 WARN_ON(!dentry1->d_inode);
2890 WARN_ON(!dentry2->d_inode);
2891 WARN_ON(IS_ROOT(dentry1));
2892 WARN_ON(IS_ROOT(dentry2));
2893
2894 __d_move(dentry1, dentry2, true);
2895
2896 write_sequnlock(&rename_lock);
2897}
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
2908{
2909 struct dentry *p;
2910
2911 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
2912 if (p->d_parent == p1)
2913 return p;
2914 }
2915 return NULL;
2916}
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927static struct dentry *__d_unalias(struct inode *inode,
2928 struct dentry *dentry, struct dentry *alias)
2929{
2930 struct mutex *m1 = NULL, *m2 = NULL;
2931 struct dentry *ret = ERR_PTR(-EBUSY);
2932
2933
2934 if (alias->d_parent == dentry->d_parent)
2935 goto out_unalias;
2936
2937
2938 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2939 goto out_err;
2940 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2941 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2942 goto out_err;
2943 m2 = &alias->d_parent->d_inode->i_mutex;
2944out_unalias:
2945 __d_move(alias, dentry, false);
2946 ret = alias;
2947out_err:
2948 spin_unlock(&inode->i_lock);
2949 if (m2)
2950 mutex_unlock(m2);
2951 if (m1)
2952 mutex_unlock(m1);
2953 return ret;
2954}
2955
2956
2957
2958
2959
2960
2961static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2962{
2963 struct dentry *dparent;
2964
2965 dentry_lock_for_move(anon, dentry);
2966
2967 write_seqcount_begin(&dentry->d_seq);
2968 write_seqcount_begin_nested(&anon->d_seq, DENTRY_D_LOCK_NESTED);
2969
2970 dparent = dentry->d_parent;
2971
2972 switch_names(dentry, anon);
2973 swap(dentry->d_name.hash, anon->d_name.hash);
2974
2975 dentry->d_parent = dentry;
2976 list_del_init(&dentry->d_u.d_child);
2977 anon->d_flags |= DCACHE_RCUACCESS;
2978 anon->d_parent = dparent;
2979 if (likely(!d_unhashed(anon))) {
2980 hlist_bl_lock(&anon->d_sb->s_anon);
2981 __hlist_bl_del(&anon->d_hash);
2982 anon->d_hash.pprev = NULL;
2983 hlist_bl_unlock(&anon->d_sb->s_anon);
2984 }
2985 list_move(&anon->d_u.d_child, &dparent->d_subdirs);
2986
2987 write_seqcount_end(&dentry->d_seq);
2988 write_seqcount_end(&anon->d_seq);
2989
2990 dentry_unlock_parents_for_move(anon, dentry);
2991 spin_unlock(&dentry->d_lock);
2992
2993
2994}
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
3020{
3021 struct dentry *new = NULL;
3022
3023 if (IS_ERR(inode))
3024 return ERR_CAST(inode);
3025
3026 if (inode && S_ISDIR(inode->i_mode)) {
3027 spin_lock(&inode->i_lock);
3028 new = __d_find_any_alias(inode);
3029 if (new) {
3030 if (!IS_ROOT(new)) {
3031 spin_unlock(&inode->i_lock);
3032 dput(new);
3033 iput(inode);
3034 return ERR_PTR(-EIO);
3035 }
3036 if (d_ancestor(new, dentry)) {
3037 spin_unlock(&inode->i_lock);
3038 dput(new);
3039 iput(inode);
3040 return ERR_PTR(-EIO);
3041 }
3042 write_seqlock(&rename_lock);
3043 __d_materialise_dentry(dentry, new);
3044 write_sequnlock(&rename_lock);
3045 _d_rehash(new);
3046 spin_unlock(&new->d_lock);
3047 spin_unlock(&inode->i_lock);
3048 security_d_instantiate(new, inode);
3049 iput(inode);
3050 } else {
3051
3052 __d_instantiate(dentry, inode);
3053 spin_unlock(&inode->i_lock);
3054 security_d_instantiate(dentry, inode);
3055 d_rehash(dentry);
3056 }
3057 } else {
3058 d_instantiate(dentry, inode);
3059 if (d_unhashed(dentry))
3060 d_rehash(dentry);
3061 }
3062 return new;
3063}
3064EXPORT_SYMBOL(d_splice_alias);
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
3076{
3077 struct dentry *actual;
3078
3079 BUG_ON(!d_unhashed(dentry));
3080
3081 if (!inode) {
3082 actual = dentry;
3083 __d_instantiate(dentry, NULL);
3084 d_rehash(actual);
3085 goto out_nolock;
3086 }
3087
3088 spin_lock(&inode->i_lock);
3089
3090 if (S_ISDIR(inode->i_mode)) {
3091 struct dentry *alias;
3092
3093
3094 alias = __d_find_alias(inode, 0);
3095 if (alias) {
3096 actual = alias;
3097 write_seqlock(&rename_lock);
3098
3099 if (d_ancestor(alias, dentry)) {
3100
3101 actual = ERR_PTR(-ELOOP);
3102 spin_unlock(&inode->i_lock);
3103 } else if (IS_ROOT(alias)) {
3104
3105
3106 __d_materialise_dentry(dentry, alias);
3107 write_sequnlock(&rename_lock);
3108 goto found;
3109 } else {
3110
3111
3112 actual = __d_unalias(inode, dentry, alias);
3113 }
3114 write_sequnlock(&rename_lock);
3115 if (IS_ERR(actual)) {
3116 if (PTR_ERR(actual) == -ELOOP)
3117 pr_warn_ratelimited(
3118 "VFS: Lookup of '%s' in %s %s"
3119 " would have caused loop\n",
3120 dentry->d_name.name,
3121 inode->i_sb->s_type->name,
3122 inode->i_sb->s_id);
3123 dput(alias);
3124 }
3125 goto out_nolock;
3126 }
3127 }
3128
3129
3130 actual = __d_instantiate_unique(dentry, inode);
3131 if (!actual)
3132 actual = dentry;
3133 else
3134 BUG_ON(!d_unhashed(actual));
3135
3136 spin_lock(&actual->d_lock);
3137found:
3138 _d_rehash(actual);
3139 spin_unlock(&actual->d_lock);
3140 spin_unlock(&inode->i_lock);
3141out_nolock:
3142 if (actual == dentry) {
3143 security_d_instantiate(dentry, inode);
3144 return NULL;
3145 }
3146
3147 iput(inode);
3148 return actual;
3149}
3150EXPORT_SYMBOL_GPL(d_materialise_unique);
3151
3152static int prepend(char **buffer, int *buflen, const char *str, int namelen)
3153{
3154 *buflen -= namelen;
3155 if (*buflen < 0)
3156 return -ENAMETOOLONG;
3157 *buffer -= namelen;
3158 memcpy(*buffer, str, namelen);
3159 return 0;
3160}
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177static int prepend_name(char **buffer, int *buflen, struct qstr *name)
3178{
3179 const char *dname = ACCESS_ONCE(name->name);
3180 u32 dlen = ACCESS_ONCE(name->len);
3181 char *p;
3182
3183 *buflen -= dlen + 1;
3184 if (*buflen < 0)
3185 return -ENAMETOOLONG;
3186 p = *buffer -= dlen + 1;
3187 *p++ = '/';
3188 while (dlen--) {
3189 char c = *dname++;
3190 if (!c)
3191 break;
3192 *p++ = c;
3193 }
3194 return 0;
3195}
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214static int prepend_path(const struct path *path,
3215 const struct path *root,
3216 char **buffer, int *buflen)
3217{
3218 struct dentry *dentry;
3219 struct vfsmount *vfsmnt;
3220 struct mount *mnt;
3221 int error = 0;
3222 unsigned seq, m_seq = 0;
3223 char *bptr;
3224 int blen;
3225
3226 rcu_read_lock();
3227restart_mnt:
3228 read_seqbegin_or_lock(&mount_lock, &m_seq);
3229 seq = 0;
3230 rcu_read_lock();
3231restart:
3232 bptr = *buffer;
3233 blen = *buflen;
3234 error = 0;
3235 dentry = path->dentry;
3236 vfsmnt = path->mnt;
3237 mnt = real_mount(vfsmnt);
3238 read_seqbegin_or_lock(&rename_lock, &seq);
3239 while (dentry != root->dentry || vfsmnt != root->mnt) {
3240 struct dentry * parent;
3241
3242 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
3243 struct mount *parent = ACCESS_ONCE(mnt->mnt_parent);
3244
3245 if (dentry != vfsmnt->mnt_root) {
3246 bptr = *buffer;
3247 blen = *buflen;
3248 error = 3;
3249 break;
3250 }
3251
3252 if (mnt != parent) {
3253 dentry = ACCESS_ONCE(mnt->mnt_mountpoint);
3254 mnt = parent;
3255 vfsmnt = &mnt->mnt;
3256 continue;
3257 }
3258 if (!error)
3259 error = is_mounted(vfsmnt) ? 1 : 2;
3260 break;
3261 }
3262 parent = dentry->d_parent;
3263 prefetch(parent);
3264 error = prepend_name(&bptr, &blen, &dentry->d_name);
3265 if (error)
3266 break;
3267
3268 dentry = parent;
3269 }
3270 if (!(seq & 1))
3271 rcu_read_unlock();
3272 if (need_seqretry(&rename_lock, seq)) {
3273 seq = 1;
3274 goto restart;
3275 }
3276 done_seqretry(&rename_lock, seq);
3277
3278 if (!(m_seq & 1))
3279 rcu_read_unlock();
3280 if (need_seqretry(&mount_lock, m_seq)) {
3281 m_seq = 1;
3282 goto restart_mnt;
3283 }
3284 done_seqretry(&mount_lock, m_seq);
3285
3286 if (error >= 0 && bptr == *buffer) {
3287 if (--blen < 0)
3288 error = -ENAMETOOLONG;
3289 else
3290 *--bptr = '/';
3291 }
3292 *buffer = bptr;
3293 *buflen = blen;
3294 return error;
3295}
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313char *__d_path(const struct path *path,
3314 const struct path *root,
3315 char *buf, int buflen)
3316{
3317 char *res = buf + buflen;
3318 int error;
3319
3320 prepend(&res, &buflen, "\0", 1);
3321 error = prepend_path(path, root, &res, &buflen);
3322
3323 if (error < 0)
3324 return ERR_PTR(error);
3325 if (error > 0)
3326 return NULL;
3327 return res;
3328}
3329
3330char *d_absolute_path(const struct path *path,
3331 char *buf, int buflen)
3332{
3333 struct path root = {};
3334 char *res = buf + buflen;
3335 int error;
3336
3337 prepend(&res, &buflen, "\0", 1);
3338 error = prepend_path(path, &root, &res, &buflen);
3339
3340 if (error > 1)
3341 error = -EINVAL;
3342 if (error < 0)
3343 return ERR_PTR(error);
3344 return res;
3345}
3346
3347
3348
3349
3350static int path_with_deleted(const struct path *path,
3351 const struct path *root,
3352 char **buf, int *buflen)
3353{
3354 prepend(buf, buflen, "\0", 1);
3355 if (d_unlinked(path->dentry)) {
3356 int error = prepend(buf, buflen, " (deleted)", 10);
3357 if (error)
3358 return error;
3359 }
3360
3361 return prepend_path(path, root, buf, buflen);
3362}
3363
3364static int prepend_unreachable(char **buffer, int *buflen)
3365{
3366 return prepend(buffer, buflen, "(unreachable)", 13);
3367}
3368
3369static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
3370{
3371 unsigned seq;
3372
3373 do {
3374 seq = read_seqcount_begin(&fs->seq);
3375 *root = fs->root;
3376 } while (read_seqcount_retry(&fs->seq, seq));
3377}
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395char *d_path(const struct path *path, char *buf, int buflen)
3396{
3397 char *res = buf + buflen;
3398 struct path root;
3399 int error;
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412 if (path->dentry->d_op && path->dentry->d_op->d_dname &&
3413 (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
3414 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
3415
3416 rcu_read_lock();
3417 get_fs_root_rcu(current->fs, &root);
3418 error = path_with_deleted(path, &root, &res, &buflen);
3419 rcu_read_unlock();
3420
3421 if (error < 0)
3422 res = ERR_PTR(error);
3423 return res;
3424}
3425EXPORT_SYMBOL(d_path);
3426
3427
3428
3429
3430char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
3431 const char *fmt, ...)
3432{
3433 va_list args;
3434 char temp[64];
3435 int sz;
3436
3437 va_start(args, fmt);
3438 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
3439 va_end(args);
3440
3441 if (sz > sizeof(temp) || sz > buflen)
3442 return ERR_PTR(-ENAMETOOLONG);
3443
3444 buffer += buflen - sz;
3445 return memcpy(buffer, temp, sz);
3446}
3447
3448char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
3449{
3450 char *end = buffer + buflen;
3451
3452 if (prepend(&end, &buflen, " (deleted)", 11) ||
3453 prepend(&end, &buflen, dentry->d_name.name, dentry->d_name.len) ||
3454 prepend(&end, &buflen, "/", 1))
3455 end = ERR_PTR(-ENAMETOOLONG);
3456 return end;
3457}
3458EXPORT_SYMBOL(simple_dname);
3459
3460
3461
3462
3463static char *__dentry_path(struct dentry *d, char *buf, int buflen)
3464{
3465 struct dentry *dentry;
3466 char *end, *retval;
3467 int len, seq = 0;
3468 int error = 0;
3469
3470 if (buflen < 2)
3471 goto Elong;
3472
3473 rcu_read_lock();
3474restart:
3475 dentry = d;
3476 end = buf + buflen;
3477 len = buflen;
3478 prepend(&end, &len, "\0", 1);
3479
3480 retval = end-1;
3481 *retval = '/';
3482 read_seqbegin_or_lock(&rename_lock, &seq);
3483 while (!IS_ROOT(dentry)) {
3484 struct dentry *parent = dentry->d_parent;
3485 int error;
3486
3487 prefetch(parent);
3488 error = prepend_name(&end, &len, &dentry->d_name);
3489 if (error)
3490 break;
3491
3492 retval = end;
3493 dentry = parent;
3494 }
3495 if (!(seq & 1))
3496 rcu_read_unlock();
3497 if (need_seqretry(&rename_lock, seq)) {
3498 seq = 1;
3499 goto restart;
3500 }
3501 done_seqretry(&rename_lock, seq);
3502 if (error)
3503 goto Elong;
3504 return retval;
3505Elong:
3506 return ERR_PTR(-ENAMETOOLONG);
3507}
3508
3509char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
3510{
3511 return __dentry_path(dentry, buf, buflen);
3512}
3513EXPORT_SYMBOL(dentry_path_raw);
3514
3515char *dentry_path(struct dentry *dentry, char *buf, int buflen)
3516{
3517 char *p = NULL;
3518 char *retval;
3519
3520 if (d_unlinked(dentry)) {
3521 p = buf + buflen;
3522 if (prepend(&p, &buflen, "//deleted", 10) != 0)
3523 goto Elong;
3524 buflen++;
3525 }
3526 retval = __dentry_path(dentry, buf, buflen);
3527 if (!IS_ERR(retval) && p)
3528 *p = '/';
3529 return retval;
3530Elong:
3531 return ERR_PTR(-ENAMETOOLONG);
3532}
3533
3534static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
3535 struct path *pwd)
3536{
3537 unsigned seq;
3538
3539 do {
3540 seq = read_seqcount_begin(&fs->seq);
3541 *root = fs->root;
3542 *pwd = fs->pwd;
3543 } while (read_seqcount_retry(&fs->seq, seq));
3544}
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
3565{
3566 int error;
3567 struct path pwd, root;
3568 char *page = (char *) __get_free_page(GFP_USER);
3569
3570 if (!page)
3571 return -ENOMEM;
3572
3573 rcu_read_lock();
3574 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
3575
3576 error = -ENOENT;
3577 if (!d_unlinked(pwd.dentry)) {
3578 unsigned long len;
3579 char *cwd = page + PAGE_SIZE;
3580 int buflen = PAGE_SIZE;
3581
3582 prepend(&cwd, &buflen, "\0", 1);
3583 error = prepend_path(&pwd, &root, &cwd, &buflen);
3584 rcu_read_unlock();
3585
3586 if (error < 0)
3587 goto out;
3588
3589
3590 if (error > 0) {
3591 error = prepend_unreachable(&cwd, &buflen);
3592 if (error)
3593 goto out;
3594 }
3595
3596 error = -ERANGE;
3597 len = PAGE_SIZE + page - cwd;
3598 if (len <= size) {
3599 error = len;
3600 if (copy_to_user(buf, cwd, len))
3601 error = -EFAULT;
3602 }
3603 } else {
3604 rcu_read_unlock();
3605 }
3606
3607out:
3608 free_page((unsigned long) page);
3609 return error;
3610}
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
3629{
3630 int result;
3631 unsigned seq;
3632
3633 if (new_dentry == old_dentry)
3634 return 1;
3635
3636 do {
3637
3638 seq = read_seqbegin(&rename_lock);
3639
3640
3641
3642
3643 rcu_read_lock();
3644 if (d_ancestor(old_dentry, new_dentry))
3645 result = 1;
3646 else
3647 result = 0;
3648 rcu_read_unlock();
3649 } while (read_seqretry(&rename_lock, seq));
3650
3651 return result;
3652}
3653EXPORT_SYMBOL(is_subdir);
3654
3655static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
3656{
3657 struct dentry *root = data;
3658 if (dentry != root) {
3659 if (d_unhashed(dentry) || !dentry->d_inode)
3660 return D_WALK_SKIP;
3661
3662 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3663 dentry->d_flags |= DCACHE_GENOCIDE;
3664 dentry->d_lockref.count--;
3665 }
3666 }
3667 return D_WALK_CONTINUE;
3668}
3669
3670void d_genocide(struct dentry *parent)
3671{
3672 d_walk(parent, parent, d_genocide_kill, NULL);
3673}
3674
3675void d_tmpfile(struct dentry *dentry, struct inode *inode)
3676{
3677 inode_dec_link_count(inode);
3678 BUG_ON(dentry->d_name.name != dentry->d_iname ||
3679 !hlist_unhashed(&dentry->d_alias) ||
3680 !d_unlinked(dentry));
3681 spin_lock(&dentry->d_parent->d_lock);
3682 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3683 dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3684 (unsigned long long)inode->i_ino);
3685 spin_unlock(&dentry->d_lock);
3686 spin_unlock(&dentry->d_parent->d_lock);
3687 d_instantiate(dentry, inode);
3688}
3689EXPORT_SYMBOL(d_tmpfile);
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705ino_t find_inode_number(struct dentry *dir, struct qstr *name)
3706{
3707 struct dentry * dentry;
3708 ino_t ino = 0;
3709
3710 dentry = d_hash_and_lookup(dir, name);
3711 if (!IS_ERR_OR_NULL(dentry)) {
3712 if (dentry->d_inode)
3713 ino = dentry->d_inode->i_ino;
3714 dput(dentry);
3715 }
3716 return ino;
3717}
3718EXPORT_SYMBOL(find_inode_number);
3719
3720static __initdata unsigned long dhash_entries;
3721static int __init set_dhash_entries(char *str)
3722{
3723 if (!str)
3724 return 0;
3725 dhash_entries = simple_strtoul(str, &str, 0);
3726 return 1;
3727}
3728__setup("dhash_entries=", set_dhash_entries);
3729
3730static void __init dcache_init_early(void)
3731{
3732 unsigned int loop;
3733
3734
3735
3736
3737 if (hashdist)
3738 return;
3739
3740 dentry_hashtable =
3741 alloc_large_system_hash("Dentry cache",
3742 sizeof(struct hlist_bl_head),
3743 dhash_entries,
3744 13,
3745 HASH_EARLY,
3746 &d_hash_shift,
3747 &d_hash_mask,
3748 0,
3749 0);
3750
3751 for (loop = 0; loop < (1U << d_hash_shift); loop++)
3752 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
3753}
3754
3755static void __init dcache_init(void)
3756{
3757 unsigned int loop;
3758
3759
3760
3761
3762
3763
3764 dentry_cache = KMEM_CACHE(dentry,
3765 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
3766
3767
3768 if (!hashdist)
3769 return;
3770
3771 dentry_hashtable =
3772 alloc_large_system_hash("Dentry cache",
3773 sizeof(struct hlist_bl_head),
3774 dhash_entries,
3775 13,
3776 0,
3777 &d_hash_shift,
3778 &d_hash_mask,
3779 0,
3780 0);
3781
3782 for (loop = 0; loop < (1U << d_hash_shift); loop++)
3783 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
3784}
3785
3786
3787struct kmem_cache *names_cachep __read_mostly;
3788EXPORT_SYMBOL(names_cachep);
3789
3790EXPORT_SYMBOL(d_genocide);
3791
3792void __init vfs_caches_init_early(void)
3793{
3794 dcache_init_early();
3795 inode_init_early();
3796}
3797
3798void __init vfs_caches_init(void)
3799{
3800 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
3801 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
3802
3803 dcache_init();
3804 inode_init();
3805 files_init();
3806 files_maxfiles_init();
3807 mnt_init();
3808 bdev_cache_init();
3809 chrdev_init();
3810}
3811