blob: 78081bdc493117a3d550dd1cb93f715335af03fe [file] [log] [blame]
b.liue9582032025-04-17 19:18:16 +08001// SPDX-License-Identifier: GPL-2.0-only
2/*
3 * fs/dcache.c
4 *
5 * Complete reimplementation
6 * (C) 1997 Thomas Schoebel-Theuer,
7 * with heavy changes by Linus Torvalds
8 */
9
10/*
11 * Notes on the allocation strategy:
12 *
13 * The dcache is a master of the icache - whenever a dcache entry
14 * exists, the inode will always exist. "iput()" is done either when
15 * the dcache entry is deleted or garbage collected.
16 */
17
18#include <linux/ratelimit.h>
19#include <linux/string.h>
20#include <linux/mm.h>
21#include <linux/fs.h>
22#include <linux/fscrypt.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/security.h>
30#include <linux/seqlock.h>
31#include <linux/memblock.h>
32#include <linux/bit_spinlock.h>
33#include <linux/rculist_bl.h>
34#include <linux/list_lru.h>
35#include "internal.h"
36#include "mount.h"
37
38/*
39 * Usage:
40 * dcache->d_inode->i_lock protects:
41 * - i_dentry, d_u.d_alias, d_inode of aliases
42 * dcache_hash_bucket lock protects:
43 * - the dcache hash table
44 * s_roots bl list spinlock protects:
45 * - the s_roots list (see __d_drop)
46 * dentry->d_sb->s_dentry_lru_lock protects:
47 * - the dcache lru lists and counters
48 * d_lock protects:
49 * - d_flags
50 * - d_name
51 * - d_lru
52 * - d_count
53 * - d_unhashed()
54 * - d_parent and d_subdirs
55 * - childrens' d_child and d_parent
56 * - d_u.d_alias, d_inode
57 *
58 * Ordering:
59 * dentry->d_inode->i_lock
60 * dentry->d_lock
61 * dentry->d_sb->s_dentry_lru_lock
62 * dcache_hash_bucket lock
63 * s_roots lock
64 *
65 * If there is an ancestor relationship:
66 * dentry->d_parent->...->d_parent->d_lock
67 * ...
68 * dentry->d_parent->d_lock
69 * dentry->d_lock
70 *
71 * If no ancestor relationship:
72 * arbitrary, since it's serialized on rename_lock
73 */
74int sysctl_vfs_cache_pressure __read_mostly = 100;
75EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
76
77__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
78
79EXPORT_SYMBOL(rename_lock);
80
81static struct kmem_cache *dentry_cache __read_mostly;
82
83const struct qstr empty_name = QSTR_INIT("", 0);
84EXPORT_SYMBOL(empty_name);
85const struct qstr slash_name = QSTR_INIT("/", 1);
86EXPORT_SYMBOL(slash_name);
87
88/*
89 * This is the single most critical data structure when it comes
90 * to the dcache: the hashtable for lookups. Somebody should try
91 * to make this good - I've just made it work.
92 *
93 * This hash-function tries to avoid losing too many bits of hash
94 * information, yet avoid using a prime hash-size or similar.
95 */
96
97static unsigned int d_hash_shift __read_mostly;
98
99static struct hlist_bl_head *dentry_hashtable __read_mostly;
100
101static inline struct hlist_bl_head *d_hash(unsigned int hash)
102{
103 return dentry_hashtable + (hash >> d_hash_shift);
104}
105
106#define IN_LOOKUP_SHIFT 10
107static struct hlist_bl_head in_lookup_hashtable[1 << IN_LOOKUP_SHIFT];
108
109static inline struct hlist_bl_head *in_lookup_hash(const struct dentry *parent,
110 unsigned int hash)
111{
112 hash += (unsigned long) parent / L1_CACHE_BYTES;
113 return in_lookup_hashtable + hash_32(hash, IN_LOOKUP_SHIFT);
114}
115
116
117/* Statistics gathering. */
118struct dentry_stat_t dentry_stat = {
119 .age_limit = 45,
120};
121
122static DEFINE_PER_CPU(long, nr_dentry);
123static DEFINE_PER_CPU(long, nr_dentry_unused);
124static DEFINE_PER_CPU(long, nr_dentry_negative);
125
126#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
127
128/*
129 * Here we resort to our own counters instead of using generic per-cpu counters
130 * for consistency with what the vfs inode code does. We are expected to harvest
131 * better code and performance by having our own specialized counters.
132 *
133 * Please note that the loop is done over all possible CPUs, not over all online
134 * CPUs. The reason for this is that we don't want to play games with CPUs going
135 * on and off. If one of them goes off, we will just keep their counters.
136 *
137 * glommer: See cffbc8a for details, and if you ever intend to change this,
138 * please update all vfs counters to match.
139 */
140static long get_nr_dentry(void)
141{
142 int i;
143 long sum = 0;
144 for_each_possible_cpu(i)
145 sum += per_cpu(nr_dentry, i);
146 return sum < 0 ? 0 : sum;
147}
148
149static long get_nr_dentry_unused(void)
150{
151 int i;
152 long sum = 0;
153 for_each_possible_cpu(i)
154 sum += per_cpu(nr_dentry_unused, i);
155 return sum < 0 ? 0 : sum;
156}
157
158static long get_nr_dentry_negative(void)
159{
160 int i;
161 long sum = 0;
162
163 for_each_possible_cpu(i)
164 sum += per_cpu(nr_dentry_negative, i);
165 return sum < 0 ? 0 : sum;
166}
167
168int proc_nr_dentry(struct ctl_table *table, int write, void __user *buffer,
169 size_t *lenp, loff_t *ppos)
170{
171 dentry_stat.nr_dentry = get_nr_dentry();
172 dentry_stat.nr_unused = get_nr_dentry_unused();
173 dentry_stat.nr_negative = get_nr_dentry_negative();
174 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
175}
176#endif
177
178/*
179 * Compare 2 name strings, return 0 if they match, otherwise non-zero.
180 * The strings are both count bytes long, and count is non-zero.
181 */
182#ifdef CONFIG_DCACHE_WORD_ACCESS
183
184#include <asm/word-at-a-time.h>
185/*
186 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
187 * aligned allocation for this particular component. We don't
188 * strictly need the load_unaligned_zeropad() safety, but it
189 * doesn't hurt either.
190 *
191 * In contrast, 'ct' and 'tcount' can be from a pathname, and do
192 * need the careful unaligned handling.
193 */
194static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
195{
196 unsigned long a,b,mask;
197
198 for (;;) {
199 a = read_word_at_a_time(cs);
200 b = load_unaligned_zeropad(ct);
201 if (tcount < sizeof(unsigned long))
202 break;
203 if (unlikely(a != b))
204 return 1;
205 cs += sizeof(unsigned long);
206 ct += sizeof(unsigned long);
207 tcount -= sizeof(unsigned long);
208 if (!tcount)
209 return 0;
210 }
211 mask = bytemask_from_count(tcount);
212 return unlikely(!!((a ^ b) & mask));
213}
214
215#else
216
217static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
218{
219 do {
220 if (*cs != *ct)
221 return 1;
222 cs++;
223 ct++;
224 tcount--;
225 } while (tcount);
226 return 0;
227}
228
229#endif
230
231static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
232{
233 /*
234 * Be careful about RCU walk racing with rename:
235 * use 'READ_ONCE' to fetch the name pointer.
236 *
237 * NOTE! Even if a rename will mean that the length
238 * was not loaded atomically, we don't care. The
239 * RCU walk will check the sequence count eventually,
240 * and catch it. And we won't overrun the buffer,
241 * because we're reading the name pointer atomically,
242 * and a dentry name is guaranteed to be properly
243 * terminated with a NUL byte.
244 *
245 * End result: even if 'len' is wrong, we'll exit
246 * early because the data cannot match (there can
247 * be no NUL in the ct/tcount data)
248 */
249 const unsigned char *cs = READ_ONCE(dentry->d_name.name);
250
251 return dentry_string_cmp(cs, ct, tcount);
252}
253
254struct external_name {
255 union {
256 atomic_t count;
257 struct rcu_head head;
258 } u;
259 unsigned char name[];
260};
261
262static inline struct external_name *external_name(struct dentry *dentry)
263{
264 return container_of(dentry->d_name.name, struct external_name, name[0]);
265}
266
267static void __d_free(struct rcu_head *head)
268{
269 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
270
271 kmem_cache_free(dentry_cache, dentry);
272}
273
274static void __d_free_external(struct rcu_head *head)
275{
276 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
277 kfree(external_name(dentry));
278 kmem_cache_free(dentry_cache, dentry);
279}
280
281static inline int dname_external(const struct dentry *dentry)
282{
283 return dentry->d_name.name != dentry->d_iname;
284}
285
286void take_dentry_name_snapshot(struct name_snapshot *name, struct dentry *dentry)
287{
288 spin_lock(&dentry->d_lock);
289 name->name = dentry->d_name;
290 if (unlikely(dname_external(dentry))) {
291 atomic_inc(&external_name(dentry)->u.count);
292 } else {
293 memcpy(name->inline_name, dentry->d_iname,
294 dentry->d_name.len + 1);
295 name->name.name = name->inline_name;
296 }
297 spin_unlock(&dentry->d_lock);
298}
299EXPORT_SYMBOL(take_dentry_name_snapshot);
300
301void release_dentry_name_snapshot(struct name_snapshot *name)
302{
303 if (unlikely(name->name.name != name->inline_name)) {
304 struct external_name *p;
305 p = container_of(name->name.name, struct external_name, name[0]);
306 if (unlikely(atomic_dec_and_test(&p->u.count)))
307 kfree_rcu(p, u.head);
308 }
309}
310EXPORT_SYMBOL(release_dentry_name_snapshot);
311
312static inline void __d_set_inode_and_type(struct dentry *dentry,
313 struct inode *inode,
314 unsigned type_flags)
315{
316 unsigned flags;
317
318 dentry->d_inode = inode;
319 flags = READ_ONCE(dentry->d_flags);
320 flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
321 flags |= type_flags;
322 WRITE_ONCE(dentry->d_flags, flags);
323}
324
325static inline void __d_clear_type_and_inode(struct dentry *dentry)
326{
327 unsigned flags = READ_ONCE(dentry->d_flags);
328
329 flags &= ~(DCACHE_ENTRY_TYPE | DCACHE_FALLTHRU);
330 WRITE_ONCE(dentry->d_flags, flags);
331 dentry->d_inode = NULL;
332 /*
333 * The negative counter only tracks dentries on the LRU. Don't inc if
334 * d_lru is on another list.
335 */
336 if ((flags & (DCACHE_LRU_LIST|DCACHE_SHRINK_LIST)) == DCACHE_LRU_LIST)
337 this_cpu_inc(nr_dentry_negative);
338}
339
340static void dentry_free(struct dentry *dentry)
341{
342 WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias));
343 if (unlikely(dname_external(dentry))) {
344 struct external_name *p = external_name(dentry);
345 if (likely(atomic_dec_and_test(&p->u.count))) {
346 call_rcu(&dentry->d_u.d_rcu, __d_free_external);
347 return;
348 }
349 }
350 /* if dentry was never visible to RCU, immediate free is OK */
351 if (dentry->d_flags & DCACHE_NORCU)
352 __d_free(&dentry->d_u.d_rcu);
353 else
354 call_rcu(&dentry->d_u.d_rcu, __d_free);
355}
356
357/*
358 * Release the dentry's inode, using the filesystem
359 * d_iput() operation if defined.
360 */
361static void dentry_unlink_inode(struct dentry * dentry)
362 __releases(dentry->d_lock)
363 __releases(dentry->d_inode->i_lock)
364{
365 struct inode *inode = dentry->d_inode;
366
367 raw_write_seqcount_begin(&dentry->d_seq);
368 __d_clear_type_and_inode(dentry);
369 hlist_del_init(&dentry->d_u.d_alias);
370 raw_write_seqcount_end(&dentry->d_seq);
371 spin_unlock(&dentry->d_lock);
372 spin_unlock(&inode->i_lock);
373 if (!inode->i_nlink)
374 fsnotify_inoderemove(inode);
375 if (dentry->d_op && dentry->d_op->d_iput)
376 dentry->d_op->d_iput(dentry, inode);
377 else
378 iput(inode);
379}
380
381/*
382 * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry
383 * is in use - which includes both the "real" per-superblock
384 * LRU list _and_ the DCACHE_SHRINK_LIST use.
385 *
386 * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
387 * on the shrink list (ie not on the superblock LRU list).
388 *
389 * The per-cpu "nr_dentry_unused" counters are updated with
390 * the DCACHE_LRU_LIST bit.
391 *
392 * The per-cpu "nr_dentry_negative" counters are only updated
393 * when deleted from or added to the per-superblock LRU list, not
394 * from/to the shrink list. That is to avoid an unneeded dec/inc
395 * pair when moving from LRU to shrink list in select_collect().
396 *
397 * These helper functions make sure we always follow the
398 * rules. d_lock must be held by the caller.
399 */
400#define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
401static void d_lru_add(struct dentry *dentry)
402{
403 D_FLAG_VERIFY(dentry, 0);
404 dentry->d_flags |= DCACHE_LRU_LIST;
405 this_cpu_inc(nr_dentry_unused);
406 if (d_is_negative(dentry))
407 this_cpu_inc(nr_dentry_negative);
408 WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
409}
410
411static void d_lru_del(struct dentry *dentry)
412{
413 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
414 dentry->d_flags &= ~DCACHE_LRU_LIST;
415 this_cpu_dec(nr_dentry_unused);
416 if (d_is_negative(dentry))
417 this_cpu_dec(nr_dentry_negative);
418 WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
419}
420
421static void d_shrink_del(struct dentry *dentry)
422{
423 D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
424 list_del_init(&dentry->d_lru);
425 dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
426 this_cpu_dec(nr_dentry_unused);
427}
428
429static void d_shrink_add(struct dentry *dentry, struct list_head *list)
430{
431 D_FLAG_VERIFY(dentry, 0);
432 list_add(&dentry->d_lru, list);
433 dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
434 this_cpu_inc(nr_dentry_unused);
435}
436
437/*
438 * These can only be called under the global LRU lock, ie during the
439 * callback for freeing the LRU list. "isolate" removes it from the
440 * LRU lists entirely, while shrink_move moves it to the indicated
441 * private list.
442 */
443static void d_lru_isolate(struct list_lru_one *lru, struct dentry *dentry)
444{
445 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
446 dentry->d_flags &= ~DCACHE_LRU_LIST;
447 this_cpu_dec(nr_dentry_unused);
448 if (d_is_negative(dentry))
449 this_cpu_dec(nr_dentry_negative);
450 list_lru_isolate(lru, &dentry->d_lru);
451}
452
453static void d_lru_shrink_move(struct list_lru_one *lru, struct dentry *dentry,
454 struct list_head *list)
455{
456 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
457 dentry->d_flags |= DCACHE_SHRINK_LIST;
458 if (d_is_negative(dentry))
459 this_cpu_dec(nr_dentry_negative);
460 list_lru_isolate_move(lru, &dentry->d_lru, list);
461}
462
463/**
464 * d_drop - drop a dentry
465 * @dentry: dentry to drop
466 *
467 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
468 * be found through a VFS lookup any more. Note that this is different from
469 * deleting the dentry - d_delete will try to mark the dentry negative if
470 * possible, giving a successful _negative_ lookup, while d_drop will
471 * just make the cache lookup fail.
472 *
473 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
474 * reason (NFS timeouts or autofs deletes).
475 *
476 * __d_drop requires dentry->d_lock
477 * ___d_drop doesn't mark dentry as "unhashed"
478 * (dentry->d_hash.pprev will be LIST_POISON2, not NULL).
479 */
480static void ___d_drop(struct dentry *dentry)
481{
482 struct hlist_bl_head *b;
483 /*
484 * Hashed dentries are normally on the dentry hashtable,
485 * with the exception of those newly allocated by
486 * d_obtain_root, which are always IS_ROOT:
487 */
488 if (unlikely(IS_ROOT(dentry)))
489 b = &dentry->d_sb->s_roots;
490 else
491 b = d_hash(dentry->d_name.hash);
492
493 hlist_bl_lock(b);
494 __hlist_bl_del(&dentry->d_hash);
495 hlist_bl_unlock(b);
496}
497
498void __d_drop(struct dentry *dentry)
499{
500 if (!d_unhashed(dentry)) {
501 ___d_drop(dentry);
502 dentry->d_hash.pprev = NULL;
503 write_seqcount_invalidate(&dentry->d_seq);
504 }
505}
506EXPORT_SYMBOL(__d_drop);
507
508void d_drop(struct dentry *dentry)
509{
510 spin_lock(&dentry->d_lock);
511 __d_drop(dentry);
512 spin_unlock(&dentry->d_lock);
513}
514EXPORT_SYMBOL(d_drop);
515
516static inline void dentry_unlist(struct dentry *dentry, struct dentry *parent)
517{
518 struct dentry *next;
519 /*
520 * Inform d_walk() and shrink_dentry_list() that we are no longer
521 * attached to the dentry tree
522 */
523 dentry->d_flags |= DCACHE_DENTRY_KILLED;
524 if (unlikely(list_empty(&dentry->d_child)))
525 return;
526 __list_del_entry(&dentry->d_child);
527 /*
528 * Cursors can move around the list of children. While we'd been
529 * a normal list member, it didn't matter - ->d_child.next would've
530 * been updated. However, from now on it won't be and for the
531 * things like d_walk() it might end up with a nasty surprise.
532 * Normally d_walk() doesn't care about cursors moving around -
533 * ->d_lock on parent prevents that and since a cursor has no children
534 * of its own, we get through it without ever unlocking the parent.
535 * There is one exception, though - if we ascend from a child that
536 * gets killed as soon as we unlock it, the next sibling is found
537 * using the value left in its ->d_child.next. And if _that_
538 * pointed to a cursor, and cursor got moved (e.g. by lseek())
539 * before d_walk() regains parent->d_lock, we'll end up skipping
540 * everything the cursor had been moved past.
541 *
542 * Solution: make sure that the pointer left behind in ->d_child.next
543 * points to something that won't be moving around. I.e. skip the
544 * cursors.
545 */
546 while (dentry->d_child.next != &parent->d_subdirs) {
547 next = list_entry(dentry->d_child.next, struct dentry, d_child);
548 if (likely(!(next->d_flags & DCACHE_DENTRY_CURSOR)))
549 break;
550 dentry->d_child.next = next->d_child.next;
551 }
552}
553
554static void __dentry_kill(struct dentry *dentry)
555{
556 struct dentry *parent = NULL;
557 bool can_free = true;
558 if (!IS_ROOT(dentry))
559 parent = dentry->d_parent;
560
561 /*
562 * The dentry is now unrecoverably dead to the world.
563 */
564 lockref_mark_dead(&dentry->d_lockref);
565
566 /*
567 * inform the fs via d_prune that this dentry is about to be
568 * unhashed and destroyed.
569 */
570 if (dentry->d_flags & DCACHE_OP_PRUNE)
571 dentry->d_op->d_prune(dentry);
572
573 if (dentry->d_flags & DCACHE_LRU_LIST) {
574 if (!(dentry->d_flags & DCACHE_SHRINK_LIST))
575 d_lru_del(dentry);
576 }
577 /* if it was on the hash then remove it */
578 __d_drop(dentry);
579 dentry_unlist(dentry, parent);
580 if (parent)
581 spin_unlock(&parent->d_lock);
582 if (dentry->d_inode)
583 dentry_unlink_inode(dentry);
584 else
585 spin_unlock(&dentry->d_lock);
586 this_cpu_dec(nr_dentry);
587 if (dentry->d_op && dentry->d_op->d_release)
588 dentry->d_op->d_release(dentry);
589
590 spin_lock(&dentry->d_lock);
591 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
592 dentry->d_flags |= DCACHE_MAY_FREE;
593 can_free = false;
594 }
595 spin_unlock(&dentry->d_lock);
596 if (likely(can_free))
597 dentry_free(dentry);
598 cond_resched();
599}
600
601static struct dentry *__lock_parent(struct dentry *dentry)
602{
603 struct dentry *parent;
604 rcu_read_lock();
605 spin_unlock(&dentry->d_lock);
606again:
607 parent = READ_ONCE(dentry->d_parent);
608 spin_lock(&parent->d_lock);
609 /*
610 * We can't blindly lock dentry until we are sure
611 * that we won't violate the locking order.
612 * Any changes of dentry->d_parent must have
613 * been done with parent->d_lock held, so
614 * spin_lock() above is enough of a barrier
615 * for checking if it's still our child.
616 */
617 if (unlikely(parent != dentry->d_parent)) {
618 spin_unlock(&parent->d_lock);
619 goto again;
620 }
621 rcu_read_unlock();
622 if (parent != dentry)
623 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
624 else
625 parent = NULL;
626 return parent;
627}
628
629static inline struct dentry *lock_parent(struct dentry *dentry)
630{
631 struct dentry *parent = dentry->d_parent;
632 if (IS_ROOT(dentry))
633 return NULL;
634 if (likely(spin_trylock(&parent->d_lock)))
635 return parent;
636 return __lock_parent(dentry);
637}
638
639static inline bool retain_dentry(struct dentry *dentry)
640{
641 WARN_ON(d_in_lookup(dentry));
642
643 /* Unreachable? Get rid of it */
644 if (unlikely(d_unhashed(dentry)))
645 return false;
646
647 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
648 return false;
649
650 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
651 if (dentry->d_op->d_delete(dentry))
652 return false;
653 }
654 /* retain; LRU fodder */
655 dentry->d_lockref.count--;
656 if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
657 d_lru_add(dentry);
658 else if (unlikely(!(dentry->d_flags & DCACHE_REFERENCED)))
659 dentry->d_flags |= DCACHE_REFERENCED;
660 return true;
661}
662
663/*
664 * Finish off a dentry we've decided to kill.
665 * dentry->d_lock must be held, returns with it unlocked.
666 * Returns dentry requiring refcount drop, or NULL if we're done.
667 */
668static struct dentry *dentry_kill(struct dentry *dentry)
669 __releases(dentry->d_lock)
670{
671 struct inode *inode = dentry->d_inode;
672 struct dentry *parent = NULL;
673
674 if (inode && unlikely(!spin_trylock(&inode->i_lock)))
675 goto slow_positive;
676
677 if (!IS_ROOT(dentry)) {
678 parent = dentry->d_parent;
679 if (unlikely(!spin_trylock(&parent->d_lock))) {
680 parent = __lock_parent(dentry);
681 if (likely(inode || !dentry->d_inode))
682 goto got_locks;
683 /* negative that became positive */
684 if (parent)
685 spin_unlock(&parent->d_lock);
686 inode = dentry->d_inode;
687 goto slow_positive;
688 }
689 }
690 __dentry_kill(dentry);
691 return parent;
692
693slow_positive:
694 spin_unlock(&dentry->d_lock);
695 spin_lock(&inode->i_lock);
696 spin_lock(&dentry->d_lock);
697 parent = lock_parent(dentry);
698got_locks:
699 if (unlikely(dentry->d_lockref.count != 1)) {
700 dentry->d_lockref.count--;
701 } else if (likely(!retain_dentry(dentry))) {
702 __dentry_kill(dentry);
703 return parent;
704 }
705 /* we are keeping it, after all */
706 if (inode)
707 spin_unlock(&inode->i_lock);
708 if (parent)
709 spin_unlock(&parent->d_lock);
710 spin_unlock(&dentry->d_lock);
711 return NULL;
712}
713
714/*
715 * Try to do a lockless dput(), and return whether that was successful.
716 *
717 * If unsuccessful, we return false, having already taken the dentry lock.
718 *
719 * The caller needs to hold the RCU read lock, so that the dentry is
720 * guaranteed to stay around even if the refcount goes down to zero!
721 */
722static inline bool fast_dput(struct dentry *dentry)
723{
724 int ret;
725 unsigned int d_flags;
726
727 /*
728 * If we have a d_op->d_delete() operation, we sould not
729 * let the dentry count go to zero, so use "put_or_lock".
730 */
731 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE))
732 return lockref_put_or_lock(&dentry->d_lockref);
733
734 /*
735 * .. otherwise, we can try to just decrement the
736 * lockref optimistically.
737 */
738 ret = lockref_put_return(&dentry->d_lockref);
739
740 /*
741 * If the lockref_put_return() failed due to the lock being held
742 * by somebody else, the fast path has failed. We will need to
743 * get the lock, and then check the count again.
744 */
745 if (unlikely(ret < 0)) {
746 spin_lock(&dentry->d_lock);
747 if (WARN_ON_ONCE(dentry->d_lockref.count <= 0)) {
748 spin_unlock(&dentry->d_lock);
749 return true;
750 }
751 dentry->d_lockref.count--;
752 goto locked;
753 }
754
755 /*
756 * If we weren't the last ref, we're done.
757 */
758 if (ret)
759 return true;
760
761 /*
762 * Careful, careful. The reference count went down
763 * to zero, but we don't hold the dentry lock, so
764 * somebody else could get it again, and do another
765 * dput(), and we need to not race with that.
766 *
767 * However, there is a very special and common case
768 * where we don't care, because there is nothing to
769 * do: the dentry is still hashed, it does not have
770 * a 'delete' op, and it's referenced and already on
771 * the LRU list.
772 *
773 * NOTE! Since we aren't locked, these values are
774 * not "stable". However, it is sufficient that at
775 * some point after we dropped the reference the
776 * dentry was hashed and the flags had the proper
777 * value. Other dentry users may have re-gotten
778 * a reference to the dentry and change that, but
779 * our work is done - we can leave the dentry
780 * around with a zero refcount.
781 */
782 smp_rmb();
783 d_flags = READ_ONCE(dentry->d_flags);
784 d_flags &= DCACHE_REFERENCED | DCACHE_LRU_LIST | DCACHE_DISCONNECTED;
785
786 /* Nothing to do? Dropping the reference was all we needed? */
787 if (d_flags == (DCACHE_REFERENCED | DCACHE_LRU_LIST) && !d_unhashed(dentry))
788 return true;
789
790 /*
791 * Not the fast normal case? Get the lock. We've already decremented
792 * the refcount, but we'll need to re-check the situation after
793 * getting the lock.
794 */
795 spin_lock(&dentry->d_lock);
796
797 /*
798 * Did somebody else grab a reference to it in the meantime, and
799 * we're no longer the last user after all? Alternatively, somebody
800 * else could have killed it and marked it dead. Either way, we
801 * don't need to do anything else.
802 */
803locked:
804 if (dentry->d_lockref.count) {
805 spin_unlock(&dentry->d_lock);
806 return true;
807 }
808
809 /*
810 * Re-get the reference we optimistically dropped. We hold the
811 * lock, and we just tested that it was zero, so we can just
812 * set it to 1.
813 */
814 dentry->d_lockref.count = 1;
815 return false;
816}
817
818
819/*
820 * This is dput
821 *
822 * This is complicated by the fact that we do not want to put
823 * dentries that are no longer on any hash chain on the unused
824 * list: we'd much rather just get rid of them immediately.
825 *
826 * However, that implies that we have to traverse the dentry
827 * tree upwards to the parents which might _also_ now be
828 * scheduled for deletion (it may have been only waiting for
829 * its last child to go away).
830 *
831 * This tail recursion is done by hand as we don't want to depend
832 * on the compiler to always get this right (gcc generally doesn't).
833 * Real recursion would eat up our stack space.
834 */
835
836/*
837 * dput - release a dentry
838 * @dentry: dentry to release
839 *
840 * Release a dentry. This will drop the usage count and if appropriate
841 * call the dentry unlink method as well as removing it from the queues and
842 * releasing its resources. If the parent dentries were scheduled for release
843 * they too may now get deleted.
844 */
845void dput(struct dentry *dentry)
846{
847 while (dentry) {
848 might_sleep();
849
850 rcu_read_lock();
851 if (likely(fast_dput(dentry))) {
852 rcu_read_unlock();
853 return;
854 }
855
856 /* Slow case: now with the dentry lock held */
857 rcu_read_unlock();
858
859 if (likely(retain_dentry(dentry))) {
860 spin_unlock(&dentry->d_lock);
861 return;
862 }
863
864 dentry = dentry_kill(dentry);
865 }
866}
867EXPORT_SYMBOL(dput);
868
869static void __dput_to_list(struct dentry *dentry, struct list_head *list)
870__must_hold(&dentry->d_lock)
871{
872 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
873 /* let the owner of the list it's on deal with it */
874 --dentry->d_lockref.count;
875 } else {
876 if (dentry->d_flags & DCACHE_LRU_LIST)
877 d_lru_del(dentry);
878 if (!--dentry->d_lockref.count)
879 d_shrink_add(dentry, list);
880 }
881}
882
883void dput_to_list(struct dentry *dentry, struct list_head *list)
884{
885 rcu_read_lock();
886 if (likely(fast_dput(dentry))) {
887 rcu_read_unlock();
888 return;
889 }
890 rcu_read_unlock();
891 if (!retain_dentry(dentry))
892 __dput_to_list(dentry, list);
893 spin_unlock(&dentry->d_lock);
894}
895
896/* This must be called with d_lock held */
897static inline void __dget_dlock(struct dentry *dentry)
898{
899 dentry->d_lockref.count++;
900}
901
902static inline void __dget(struct dentry *dentry)
903{
904 lockref_get(&dentry->d_lockref);
905}
906
907struct dentry *dget_parent(struct dentry *dentry)
908{
909 int gotref;
910 struct dentry *ret;
911 unsigned seq;
912
913 /*
914 * Do optimistic parent lookup without any
915 * locking.
916 */
917 rcu_read_lock();
918 seq = raw_seqcount_begin(&dentry->d_seq);
919 ret = READ_ONCE(dentry->d_parent);
920 gotref = lockref_get_not_zero(&ret->d_lockref);
921 rcu_read_unlock();
922 if (likely(gotref)) {
923 if (!read_seqcount_retry(&dentry->d_seq, seq))
924 return ret;
925 dput(ret);
926 }
927
928repeat:
929 /*
930 * Don't need rcu_dereference because we re-check it was correct under
931 * the lock.
932 */
933 rcu_read_lock();
934 ret = dentry->d_parent;
935 spin_lock(&ret->d_lock);
936 if (unlikely(ret != dentry->d_parent)) {
937 spin_unlock(&ret->d_lock);
938 rcu_read_unlock();
939 goto repeat;
940 }
941 rcu_read_unlock();
942 BUG_ON(!ret->d_lockref.count);
943 ret->d_lockref.count++;
944 spin_unlock(&ret->d_lock);
945 return ret;
946}
947EXPORT_SYMBOL(dget_parent);
948
949static struct dentry * __d_find_any_alias(struct inode *inode)
950{
951 struct dentry *alias;
952
953 if (hlist_empty(&inode->i_dentry))
954 return NULL;
955 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
956 __dget(alias);
957 return alias;
958}
959
960/**
961 * d_find_any_alias - find any alias for a given inode
962 * @inode: inode to find an alias for
963 *
964 * If any aliases exist for the given inode, take and return a
965 * reference for one of them. If no aliases exist, return %NULL.
966 */
967struct dentry *d_find_any_alias(struct inode *inode)
968{
969 struct dentry *de;
970
971 spin_lock(&inode->i_lock);
972 de = __d_find_any_alias(inode);
973 spin_unlock(&inode->i_lock);
974 return de;
975}
976EXPORT_SYMBOL(d_find_any_alias);
977
978/**
979 * d_find_alias - grab a hashed alias of inode
980 * @inode: inode in question
981 *
982 * If inode has a hashed alias, or is a directory and has any alias,
983 * acquire the reference to alias and return it. Otherwise return NULL.
984 * Notice that if inode is a directory there can be only one alias and
985 * it can be unhashed only if it has no children, or if it is the root
986 * of a filesystem, or if the directory was renamed and d_revalidate
987 * was the first vfs operation to notice.
988 *
989 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
990 * any other hashed alias over that one.
991 */
992static struct dentry *__d_find_alias(struct inode *inode)
993{
994 struct dentry *alias;
995
996 if (S_ISDIR(inode->i_mode))
997 return __d_find_any_alias(inode);
998
999 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1000 spin_lock(&alias->d_lock);
1001 if (!d_unhashed(alias)) {
1002 __dget_dlock(alias);
1003 spin_unlock(&alias->d_lock);
1004 return alias;
1005 }
1006 spin_unlock(&alias->d_lock);
1007 }
1008 return NULL;
1009}
1010
1011struct dentry *d_find_alias(struct inode *inode)
1012{
1013 struct dentry *de = NULL;
1014
1015 if (!hlist_empty(&inode->i_dentry)) {
1016 spin_lock(&inode->i_lock);
1017 de = __d_find_alias(inode);
1018 spin_unlock(&inode->i_lock);
1019 }
1020 return de;
1021}
1022EXPORT_SYMBOL(d_find_alias);
1023
1024/*
1025 * Try to kill dentries associated with this inode.
1026 * WARNING: you must own a reference to inode.
1027 */
1028void d_prune_aliases(struct inode *inode)
1029{
1030 struct dentry *dentry;
1031restart:
1032 spin_lock(&inode->i_lock);
1033 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
1034 spin_lock(&dentry->d_lock);
1035 if (!dentry->d_lockref.count) {
1036 struct dentry *parent = lock_parent(dentry);
1037 if (likely(!dentry->d_lockref.count)) {
1038 __dentry_kill(dentry);
1039 dput(parent);
1040 goto restart;
1041 }
1042 if (parent)
1043 spin_unlock(&parent->d_lock);
1044 }
1045 spin_unlock(&dentry->d_lock);
1046 }
1047 spin_unlock(&inode->i_lock);
1048}
1049EXPORT_SYMBOL(d_prune_aliases);
1050
1051/*
1052 * Lock a dentry from shrink list.
1053 * Called under rcu_read_lock() and dentry->d_lock; the former
1054 * guarantees that nothing we access will be freed under us.
1055 * Note that dentry is *not* protected from concurrent dentry_kill(),
1056 * d_delete(), etc.
1057 *
1058 * Return false if dentry has been disrupted or grabbed, leaving
1059 * the caller to kick it off-list. Otherwise, return true and have
1060 * that dentry's inode and parent both locked.
1061 */
1062static bool shrink_lock_dentry(struct dentry *dentry)
1063{
1064 struct inode *inode;
1065 struct dentry *parent;
1066
1067 if (dentry->d_lockref.count)
1068 return false;
1069
1070 inode = dentry->d_inode;
1071 if (inode && unlikely(!spin_trylock(&inode->i_lock))) {
1072 spin_unlock(&dentry->d_lock);
1073 spin_lock(&inode->i_lock);
1074 spin_lock(&dentry->d_lock);
1075 if (unlikely(dentry->d_lockref.count))
1076 goto out;
1077 /* changed inode means that somebody had grabbed it */
1078 if (unlikely(inode != dentry->d_inode))
1079 goto out;
1080 }
1081
1082 parent = dentry->d_parent;
1083 if (IS_ROOT(dentry) || likely(spin_trylock(&parent->d_lock)))
1084 return true;
1085
1086 spin_unlock(&dentry->d_lock);
1087 spin_lock(&parent->d_lock);
1088 if (unlikely(parent != dentry->d_parent)) {
1089 spin_unlock(&parent->d_lock);
1090 spin_lock(&dentry->d_lock);
1091 goto out;
1092 }
1093 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1094 if (likely(!dentry->d_lockref.count))
1095 return true;
1096 spin_unlock(&parent->d_lock);
1097out:
1098 if (inode)
1099 spin_unlock(&inode->i_lock);
1100 return false;
1101}
1102
1103void shrink_dentry_list(struct list_head *list)
1104{
1105 while (!list_empty(list)) {
1106 struct dentry *dentry, *parent;
1107
1108 dentry = list_entry(list->prev, struct dentry, d_lru);
1109 spin_lock(&dentry->d_lock);
1110 rcu_read_lock();
1111 if (!shrink_lock_dentry(dentry)) {
1112 bool can_free = false;
1113 rcu_read_unlock();
1114 d_shrink_del(dentry);
1115 if (dentry->d_lockref.count < 0)
1116 can_free = dentry->d_flags & DCACHE_MAY_FREE;
1117 spin_unlock(&dentry->d_lock);
1118 if (can_free)
1119 dentry_free(dentry);
1120 continue;
1121 }
1122 rcu_read_unlock();
1123 d_shrink_del(dentry);
1124 parent = dentry->d_parent;
1125 if (parent != dentry)
1126 __dput_to_list(parent, list);
1127 __dentry_kill(dentry);
1128 }
1129}
1130
1131static enum lru_status dentry_lru_isolate(struct list_head *item,
1132 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
1133{
1134 struct list_head *freeable = arg;
1135 struct dentry *dentry = container_of(item, struct dentry, d_lru);
1136
1137
1138 /*
1139 * we are inverting the lru lock/dentry->d_lock here,
1140 * so use a trylock. If we fail to get the lock, just skip
1141 * it
1142 */
1143 if (!spin_trylock(&dentry->d_lock))
1144 return LRU_SKIP;
1145
1146 /*
1147 * Referenced dentries are still in use. If they have active
1148 * counts, just remove them from the LRU. Otherwise give them
1149 * another pass through the LRU.
1150 */
1151 if (dentry->d_lockref.count) {
1152 d_lru_isolate(lru, dentry);
1153 spin_unlock(&dentry->d_lock);
1154 return LRU_REMOVED;
1155 }
1156
1157 if (dentry->d_flags & DCACHE_REFERENCED) {
1158 dentry->d_flags &= ~DCACHE_REFERENCED;
1159 spin_unlock(&dentry->d_lock);
1160
1161 /*
1162 * The list move itself will be made by the common LRU code. At
1163 * this point, we've dropped the dentry->d_lock but keep the
1164 * lru lock. This is safe to do, since every list movement is
1165 * protected by the lru lock even if both locks are held.
1166 *
1167 * This is guaranteed by the fact that all LRU management
1168 * functions are intermediated by the LRU API calls like
1169 * list_lru_add and list_lru_del. List movement in this file
1170 * only ever occur through this functions or through callbacks
1171 * like this one, that are called from the LRU API.
1172 *
1173 * The only exceptions to this are functions like
1174 * shrink_dentry_list, and code that first checks for the
1175 * DCACHE_SHRINK_LIST flag. Those are guaranteed to be
1176 * operating only with stack provided lists after they are
1177 * properly isolated from the main list. It is thus, always a
1178 * local access.
1179 */
1180 return LRU_ROTATE;
1181 }
1182
1183 d_lru_shrink_move(lru, dentry, freeable);
1184 spin_unlock(&dentry->d_lock);
1185
1186 return LRU_REMOVED;
1187}
1188
1189/**
1190 * prune_dcache_sb - shrink the dcache
1191 * @sb: superblock
1192 * @sc: shrink control, passed to list_lru_shrink_walk()
1193 *
1194 * Attempt to shrink the superblock dcache LRU by @sc->nr_to_scan entries. This
1195 * is done when we need more memory and called from the superblock shrinker
1196 * function.
1197 *
1198 * This function may fail to free any resources if all the dentries are in
1199 * use.
1200 */
1201long prune_dcache_sb(struct super_block *sb, struct shrink_control *sc)
1202{
1203 LIST_HEAD(dispose);
1204 long freed;
1205
1206 freed = list_lru_shrink_walk(&sb->s_dentry_lru, sc,
1207 dentry_lru_isolate, &dispose);
1208 shrink_dentry_list(&dispose);
1209 return freed;
1210}
1211
1212static enum lru_status dentry_lru_isolate_shrink(struct list_head *item,
1213 struct list_lru_one *lru, spinlock_t *lru_lock, void *arg)
1214{
1215 struct list_head *freeable = arg;
1216 struct dentry *dentry = container_of(item, struct dentry, d_lru);
1217
1218 /*
1219 * we are inverting the lru lock/dentry->d_lock here,
1220 * so use a trylock. If we fail to get the lock, just skip
1221 * it
1222 */
1223 if (!spin_trylock(&dentry->d_lock))
1224 return LRU_SKIP;
1225
1226 d_lru_shrink_move(lru, dentry, freeable);
1227 spin_unlock(&dentry->d_lock);
1228
1229 return LRU_REMOVED;
1230}
1231
1232
1233/**
1234 * shrink_dcache_sb - shrink dcache for a superblock
1235 * @sb: superblock
1236 *
1237 * Shrink the dcache for the specified super block. This is used to free
1238 * the dcache before unmounting a file system.
1239 */
1240void shrink_dcache_sb(struct super_block *sb)
1241{
1242 do {
1243 LIST_HEAD(dispose);
1244
1245 list_lru_walk(&sb->s_dentry_lru,
1246 dentry_lru_isolate_shrink, &dispose, 1024);
1247 shrink_dentry_list(&dispose);
1248 } while (list_lru_count(&sb->s_dentry_lru) > 0);
1249}
1250EXPORT_SYMBOL(shrink_dcache_sb);
1251
1252/**
1253 * enum d_walk_ret - action to talke during tree walk
1254 * @D_WALK_CONTINUE: contrinue walk
1255 * @D_WALK_QUIT: quit walk
1256 * @D_WALK_NORETRY: quit when retry is needed
1257 * @D_WALK_SKIP: skip this dentry and its children
1258 */
1259enum d_walk_ret {
1260 D_WALK_CONTINUE,
1261 D_WALK_QUIT,
1262 D_WALK_NORETRY,
1263 D_WALK_SKIP,
1264};
1265
1266/**
1267 * d_walk - walk the dentry tree
1268 * @parent: start of walk
1269 * @data: data passed to @enter() and @finish()
1270 * @enter: callback when first entering the dentry
1271 *
1272 * The @enter() callbacks are called with d_lock held.
1273 */
1274static void d_walk(struct dentry *parent, void *data,
1275 enum d_walk_ret (*enter)(void *, struct dentry *))
1276{
1277 struct dentry *this_parent;
1278 struct list_head *next;
1279 unsigned seq = 0;
1280 enum d_walk_ret ret;
1281 bool retry = true;
1282
1283again:
1284 read_seqbegin_or_lock(&rename_lock, &seq);
1285 this_parent = parent;
1286 spin_lock(&this_parent->d_lock);
1287
1288 ret = enter(data, this_parent);
1289 switch (ret) {
1290 case D_WALK_CONTINUE:
1291 break;
1292 case D_WALK_QUIT:
1293 case D_WALK_SKIP:
1294 goto out_unlock;
1295 case D_WALK_NORETRY:
1296 retry = false;
1297 break;
1298 }
1299repeat:
1300 next = this_parent->d_subdirs.next;
1301resume:
1302 while (next != &this_parent->d_subdirs) {
1303 struct list_head *tmp = next;
1304 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1305 next = tmp->next;
1306
1307 if (unlikely(dentry->d_flags & DCACHE_DENTRY_CURSOR))
1308 continue;
1309
1310 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1311
1312 ret = enter(data, dentry);
1313 switch (ret) {
1314 case D_WALK_CONTINUE:
1315 break;
1316 case D_WALK_QUIT:
1317 spin_unlock(&dentry->d_lock);
1318 goto out_unlock;
1319 case D_WALK_NORETRY:
1320 retry = false;
1321 break;
1322 case D_WALK_SKIP:
1323 spin_unlock(&dentry->d_lock);
1324 continue;
1325 }
1326
1327 if (!list_empty(&dentry->d_subdirs)) {
1328 spin_unlock(&this_parent->d_lock);
1329 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
1330 this_parent = dentry;
1331 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1332 goto repeat;
1333 }
1334 spin_unlock(&dentry->d_lock);
1335 }
1336 /*
1337 * All done at this level ... ascend and resume the search.
1338 */
1339 rcu_read_lock();
1340ascend:
1341 if (this_parent != parent) {
1342 struct dentry *child = this_parent;
1343 this_parent = child->d_parent;
1344
1345 spin_unlock(&child->d_lock);
1346 spin_lock(&this_parent->d_lock);
1347
1348 /* might go back up the wrong parent if we have had a rename. */
1349 if (need_seqretry(&rename_lock, seq))
1350 goto rename_retry;
1351 /* go into the first sibling still alive */
1352 do {
1353 next = child->d_child.next;
1354 if (next == &this_parent->d_subdirs)
1355 goto ascend;
1356 child = list_entry(next, struct dentry, d_child);
1357 } while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
1358 rcu_read_unlock();
1359 goto resume;
1360 }
1361 if (need_seqretry(&rename_lock, seq))
1362 goto rename_retry;
1363 rcu_read_unlock();
1364
1365out_unlock:
1366 spin_unlock(&this_parent->d_lock);
1367 done_seqretry(&rename_lock, seq);
1368 return;
1369
1370rename_retry:
1371 spin_unlock(&this_parent->d_lock);
1372 rcu_read_unlock();
1373 BUG_ON(seq & 1);
1374 if (!retry)
1375 return;
1376 seq = 1;
1377 goto again;
1378}
1379
1380struct check_mount {
1381 struct vfsmount *mnt;
1382 unsigned int mounted;
1383};
1384
1385static enum d_walk_ret path_check_mount(void *data, struct dentry *dentry)
1386{
1387 struct check_mount *info = data;
1388 struct path path = { .mnt = info->mnt, .dentry = dentry };
1389
1390 if (likely(!d_mountpoint(dentry)))
1391 return D_WALK_CONTINUE;
1392 if (__path_is_mountpoint(&path)) {
1393 info->mounted = 1;
1394 return D_WALK_QUIT;
1395 }
1396 return D_WALK_CONTINUE;
1397}
1398
1399/**
1400 * path_has_submounts - check for mounts over a dentry in the
1401 * current namespace.
1402 * @parent: path to check.
1403 *
1404 * Return true if the parent or its subdirectories contain
1405 * a mount point in the current namespace.
1406 */
1407int path_has_submounts(const struct path *parent)
1408{
1409 struct check_mount data = { .mnt = parent->mnt, .mounted = 0 };
1410
1411 read_seqlock_excl(&mount_lock);
1412 d_walk(parent->dentry, &data, path_check_mount);
1413 read_sequnlock_excl(&mount_lock);
1414
1415 return data.mounted;
1416}
1417EXPORT_SYMBOL(path_has_submounts);
1418
1419/*
1420 * Called by mount code to set a mountpoint and check if the mountpoint is
1421 * reachable (e.g. NFS can unhash a directory dentry and then the complete
1422 * subtree can become unreachable).
1423 *
1424 * Only one of d_invalidate() and d_set_mounted() must succeed. For
1425 * this reason take rename_lock and d_lock on dentry and ancestors.
1426 */
1427int d_set_mounted(struct dentry *dentry)
1428{
1429 struct dentry *p;
1430 int ret = -ENOENT;
1431 write_seqlock(&rename_lock);
1432 for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
1433 /* Need exclusion wrt. d_invalidate() */
1434 spin_lock(&p->d_lock);
1435 if (unlikely(d_unhashed(p))) {
1436 spin_unlock(&p->d_lock);
1437 goto out;
1438 }
1439 spin_unlock(&p->d_lock);
1440 }
1441 spin_lock(&dentry->d_lock);
1442 if (!d_unlinked(dentry)) {
1443 ret = -EBUSY;
1444 if (!d_mountpoint(dentry)) {
1445 dentry->d_flags |= DCACHE_MOUNTED;
1446 ret = 0;
1447 }
1448 }
1449 spin_unlock(&dentry->d_lock);
1450out:
1451 write_sequnlock(&rename_lock);
1452 return ret;
1453}
1454
1455/*
1456 * Search the dentry child list of the specified parent,
1457 * and move any unused dentries to the end of the unused
1458 * list for prune_dcache(). We descend to the next level
1459 * whenever the d_subdirs list is non-empty and continue
1460 * searching.
1461 *
1462 * It returns zero iff there are no unused children,
1463 * otherwise it returns the number of children moved to
1464 * the end of the unused list. This may not be the total
1465 * number of unused children, because select_parent can
1466 * drop the lock and return early due to latency
1467 * constraints.
1468 */
1469
1470struct select_data {
1471 struct dentry *start;
1472 union {
1473 long found;
1474 struct dentry *victim;
1475 };
1476 struct list_head dispose;
1477};
1478
1479static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
1480{
1481 struct select_data *data = _data;
1482 enum d_walk_ret ret = D_WALK_CONTINUE;
1483
1484 if (data->start == dentry)
1485 goto out;
1486
1487 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
1488 data->found++;
1489 } else {
1490 if (dentry->d_flags & DCACHE_LRU_LIST)
1491 d_lru_del(dentry);
1492 if (!dentry->d_lockref.count) {
1493 d_shrink_add(dentry, &data->dispose);
1494 data->found++;
1495 }
1496 }
1497 /*
1498 * We can return to the caller if we have found some (this
1499 * ensures forward progress). We'll be coming back to find
1500 * the rest.
1501 */
1502 if (!list_empty(&data->dispose))
1503 ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY;
1504out:
1505 return ret;
1506}
1507
1508static enum d_walk_ret select_collect2(void *_data, struct dentry *dentry)
1509{
1510 struct select_data *data = _data;
1511 enum d_walk_ret ret = D_WALK_CONTINUE;
1512
1513 if (data->start == dentry)
1514 goto out;
1515
1516 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
1517 if (!dentry->d_lockref.count) {
1518 rcu_read_lock();
1519 data->victim = dentry;
1520 return D_WALK_QUIT;
1521 }
1522 } else {
1523 if (dentry->d_flags & DCACHE_LRU_LIST)
1524 d_lru_del(dentry);
1525 if (!dentry->d_lockref.count)
1526 d_shrink_add(dentry, &data->dispose);
1527 }
1528 /*
1529 * We can return to the caller if we have found some (this
1530 * ensures forward progress). We'll be coming back to find
1531 * the rest.
1532 */
1533 if (!list_empty(&data->dispose))
1534 ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY;
1535out:
1536 return ret;
1537}
1538
1539/**
1540 * shrink_dcache_parent - prune dcache
1541 * @parent: parent of entries to prune
1542 *
1543 * Prune the dcache to remove unused children of the parent dentry.
1544 */
1545void shrink_dcache_parent(struct dentry *parent)
1546{
1547 for (;;) {
1548 struct select_data data = {.start = parent};
1549
1550 INIT_LIST_HEAD(&data.dispose);
1551 d_walk(parent, &data, select_collect);
1552
1553 if (!list_empty(&data.dispose)) {
1554 shrink_dentry_list(&data.dispose);
1555 continue;
1556 }
1557
1558 cond_resched();
1559 if (!data.found)
1560 break;
1561 data.victim = NULL;
1562 d_walk(parent, &data, select_collect2);
1563 if (data.victim) {
1564 struct dentry *parent;
1565 spin_lock(&data.victim->d_lock);
1566 if (!shrink_lock_dentry(data.victim)) {
1567 spin_unlock(&data.victim->d_lock);
1568 rcu_read_unlock();
1569 } else {
1570 rcu_read_unlock();
1571 parent = data.victim->d_parent;
1572 if (parent != data.victim)
1573 __dput_to_list(parent, &data.dispose);
1574 __dentry_kill(data.victim);
1575 }
1576 }
1577 if (!list_empty(&data.dispose))
1578 shrink_dentry_list(&data.dispose);
1579 }
1580}
1581EXPORT_SYMBOL(shrink_dcache_parent);
1582
1583static enum d_walk_ret umount_check(void *_data, struct dentry *dentry)
1584{
1585 /* it has busy descendents; complain about those instead */
1586 if (!list_empty(&dentry->d_subdirs))
1587 return D_WALK_CONTINUE;
1588
1589 /* root with refcount 1 is fine */
1590 if (dentry == _data && dentry->d_lockref.count == 1)
1591 return D_WALK_CONTINUE;
1592
1593 printk(KERN_ERR "BUG: Dentry %p{i=%lx,n=%pd} "
1594 " still in use (%d) [unmount of %s %s]\n",
1595 dentry,
1596 dentry->d_inode ?
1597 dentry->d_inode->i_ino : 0UL,
1598 dentry,
1599 dentry->d_lockref.count,
1600 dentry->d_sb->s_type->name,
1601 dentry->d_sb->s_id);
1602 WARN_ON(1);
1603 return D_WALK_CONTINUE;
1604}
1605
1606static void do_one_tree(struct dentry *dentry)
1607{
1608 shrink_dcache_parent(dentry);
1609 d_walk(dentry, dentry, umount_check);
1610 d_drop(dentry);
1611 dput(dentry);
1612}
1613
1614/*
1615 * destroy the dentries attached to a superblock on unmounting
1616 */
1617void shrink_dcache_for_umount(struct super_block *sb)
1618{
1619 struct dentry *dentry;
1620
1621 WARN(down_read_trylock(&sb->s_umount), "s_umount should've been locked");
1622
1623 dentry = sb->s_root;
1624 sb->s_root = NULL;
1625 do_one_tree(dentry);
1626
1627 while (!hlist_bl_empty(&sb->s_roots)) {
1628 dentry = dget(hlist_bl_entry(hlist_bl_first(&sb->s_roots), struct dentry, d_hash));
1629 do_one_tree(dentry);
1630 }
1631}
1632
1633static enum d_walk_ret find_submount(void *_data, struct dentry *dentry)
1634{
1635 struct dentry **victim = _data;
1636 if (d_mountpoint(dentry)) {
1637 __dget_dlock(dentry);
1638 *victim = dentry;
1639 return D_WALK_QUIT;
1640 }
1641 return D_WALK_CONTINUE;
1642}
1643
1644/**
1645 * d_invalidate - detach submounts, prune dcache, and drop
1646 * @dentry: dentry to invalidate (aka detach, prune and drop)
1647 */
1648void d_invalidate(struct dentry *dentry)
1649{
1650 bool had_submounts = false;
1651 spin_lock(&dentry->d_lock);
1652 if (d_unhashed(dentry)) {
1653 spin_unlock(&dentry->d_lock);
1654 return;
1655 }
1656 __d_drop(dentry);
1657 spin_unlock(&dentry->d_lock);
1658
1659 /* Negative dentries can be dropped without further checks */
1660 if (!dentry->d_inode)
1661 return;
1662
1663 shrink_dcache_parent(dentry);
1664 for (;;) {
1665 struct dentry *victim = NULL;
1666 d_walk(dentry, &victim, find_submount);
1667 if (!victim) {
1668 if (had_submounts)
1669 shrink_dcache_parent(dentry);
1670 return;
1671 }
1672 had_submounts = true;
1673 detach_mounts(victim);
1674 dput(victim);
1675 }
1676}
1677EXPORT_SYMBOL(d_invalidate);
1678
1679/**
1680 * __d_alloc - allocate a dcache entry
1681 * @sb: filesystem it will belong to
1682 * @name: qstr of the name
1683 *
1684 * Allocates a dentry. It returns %NULL if there is insufficient memory
1685 * available. On a success the dentry is returned. The name passed in is
1686 * copied and the copy passed in may be reused after this call.
1687 */
1688
1689struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
1690{
1691 struct dentry *dentry;
1692 char *dname;
1693 int err;
1694
1695 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
1696 if (!dentry)
1697 return NULL;
1698
1699 /*
1700 * We guarantee that the inline name is always NUL-terminated.
1701 * This way the memcpy() done by the name switching in rename
1702 * will still always have a NUL at the end, even if we might
1703 * be overwriting an internal NUL character
1704 */
1705 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
1706 if (unlikely(!name)) {
1707 name = &slash_name;
1708 dname = dentry->d_iname;
1709 } else if (name->len > DNAME_INLINE_LEN-1) {
1710 size_t size = offsetof(struct external_name, name[1]);
1711 struct external_name *p = kmalloc(size + name->len,
1712 GFP_KERNEL_ACCOUNT |
1713 __GFP_RECLAIMABLE);
1714 if (!p) {
1715 kmem_cache_free(dentry_cache, dentry);
1716 return NULL;
1717 }
1718 atomic_set(&p->u.count, 1);
1719 dname = p->name;
1720 } else {
1721 dname = dentry->d_iname;
1722 }
1723
1724 dentry->d_name.len = name->len;
1725 dentry->d_name.hash = name->hash;
1726 memcpy(dname, name->name, name->len);
1727 dname[name->len] = 0;
1728
1729 /* Make sure we always see the terminating NUL character */
1730 smp_store_release(&dentry->d_name.name, dname); /* ^^^ */
1731
1732 dentry->d_lockref.count = 1;
1733 dentry->d_flags = 0;
1734 spin_lock_init(&dentry->d_lock);
1735 seqcount_init(&dentry->d_seq);
1736 dentry->d_inode = NULL;
1737 dentry->d_parent = dentry;
1738 dentry->d_sb = sb;
1739 dentry->d_op = NULL;
1740 dentry->d_fsdata = NULL;
1741 INIT_HLIST_BL_NODE(&dentry->d_hash);
1742 INIT_LIST_HEAD(&dentry->d_lru);
1743 INIT_LIST_HEAD(&dentry->d_subdirs);
1744 INIT_HLIST_NODE(&dentry->d_u.d_alias);
1745 INIT_LIST_HEAD(&dentry->d_child);
1746 d_set_d_op(dentry, dentry->d_sb->s_d_op);
1747
1748 if (dentry->d_op && dentry->d_op->d_init) {
1749 err = dentry->d_op->d_init(dentry);
1750 if (err) {
1751 if (dname_external(dentry))
1752 kfree(external_name(dentry));
1753 kmem_cache_free(dentry_cache, dentry);
1754 return NULL;
1755 }
1756 }
1757
1758 this_cpu_inc(nr_dentry);
1759
1760 return dentry;
1761}
1762
1763/**
1764 * d_alloc - allocate a dcache entry
1765 * @parent: parent of entry to allocate
1766 * @name: qstr of the name
1767 *
1768 * Allocates a dentry. It returns %NULL if there is insufficient memory
1769 * available. On a success the dentry is returned. The name passed in is
1770 * copied and the copy passed in may be reused after this call.
1771 */
1772struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1773{
1774 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1775 if (!dentry)
1776 return NULL;
1777 spin_lock(&parent->d_lock);
1778 /*
1779 * don't need child lock because it is not subject
1780 * to concurrency here
1781 */
1782 __dget_dlock(parent);
1783 dentry->d_parent = parent;
1784 list_add(&dentry->d_child, &parent->d_subdirs);
1785 spin_unlock(&parent->d_lock);
1786
1787 return dentry;
1788}
1789EXPORT_SYMBOL(d_alloc);
1790
1791struct dentry *d_alloc_anon(struct super_block *sb)
1792{
1793 return __d_alloc(sb, NULL);
1794}
1795EXPORT_SYMBOL(d_alloc_anon);
1796
1797struct dentry *d_alloc_cursor(struct dentry * parent)
1798{
1799 struct dentry *dentry = d_alloc_anon(parent->d_sb);
1800 if (dentry) {
1801 dentry->d_flags |= DCACHE_DENTRY_CURSOR;
1802 dentry->d_parent = dget(parent);
1803 }
1804 return dentry;
1805}
1806
1807/**
1808 * d_alloc_pseudo - allocate a dentry (for lookup-less filesystems)
1809 * @sb: the superblock
1810 * @name: qstr of the name
1811 *
1812 * For a filesystem that just pins its dentries in memory and never
1813 * performs lookups at all, return an unhashed IS_ROOT dentry.
1814 * This is used for pipes, sockets et.al. - the stuff that should
1815 * never be anyone's children or parents. Unlike all other
1816 * dentries, these will not have RCU delay between dropping the
1817 * last reference and freeing them.
1818 *
1819 * The only user is alloc_file_pseudo() and that's what should
1820 * be considered a public interface. Don't use directly.
1821 */
1822struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1823{
1824 struct dentry *dentry = __d_alloc(sb, name);
1825 if (likely(dentry))
1826 dentry->d_flags |= DCACHE_NORCU;
1827 return dentry;
1828}
1829
1830struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1831{
1832 struct qstr q;
1833
1834 q.name = name;
1835 q.hash_len = hashlen_string(parent, name);
1836 return d_alloc(parent, &q);
1837}
1838EXPORT_SYMBOL(d_alloc_name);
1839
1840void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1841{
1842 WARN_ON_ONCE(dentry->d_op);
1843 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
1844 DCACHE_OP_COMPARE |
1845 DCACHE_OP_REVALIDATE |
1846 DCACHE_OP_WEAK_REVALIDATE |
1847 DCACHE_OP_DELETE |
1848 DCACHE_OP_REAL));
1849 dentry->d_op = op;
1850 if (!op)
1851 return;
1852 if (op->d_hash)
1853 dentry->d_flags |= DCACHE_OP_HASH;
1854 if (op->d_compare)
1855 dentry->d_flags |= DCACHE_OP_COMPARE;
1856 if (op->d_revalidate)
1857 dentry->d_flags |= DCACHE_OP_REVALIDATE;
1858 if (op->d_weak_revalidate)
1859 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
1860 if (op->d_delete)
1861 dentry->d_flags |= DCACHE_OP_DELETE;
1862 if (op->d_prune)
1863 dentry->d_flags |= DCACHE_OP_PRUNE;
1864 if (op->d_real)
1865 dentry->d_flags |= DCACHE_OP_REAL;
1866
1867}
1868EXPORT_SYMBOL(d_set_d_op);
1869
1870
1871/*
1872 * d_set_fallthru - Mark a dentry as falling through to a lower layer
1873 * @dentry - The dentry to mark
1874 *
1875 * Mark a dentry as falling through to the lower layer (as set with
1876 * d_pin_lower()). This flag may be recorded on the medium.
1877 */
1878void d_set_fallthru(struct dentry *dentry)
1879{
1880 spin_lock(&dentry->d_lock);
1881 dentry->d_flags |= DCACHE_FALLTHRU;
1882 spin_unlock(&dentry->d_lock);
1883}
1884EXPORT_SYMBOL(d_set_fallthru);
1885
1886static unsigned d_flags_for_inode(struct inode *inode)
1887{
1888 unsigned add_flags = DCACHE_REGULAR_TYPE;
1889
1890 if (!inode)
1891 return DCACHE_MISS_TYPE;
1892
1893 if (S_ISDIR(inode->i_mode)) {
1894 add_flags = DCACHE_DIRECTORY_TYPE;
1895 if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) {
1896 if (unlikely(!inode->i_op->lookup))
1897 add_flags = DCACHE_AUTODIR_TYPE;
1898 else
1899 inode->i_opflags |= IOP_LOOKUP;
1900 }
1901 goto type_determined;
1902 }
1903
1904 if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1905 if (unlikely(inode->i_op->get_link)) {
1906 add_flags = DCACHE_SYMLINK_TYPE;
1907 goto type_determined;
1908 }
1909 inode->i_opflags |= IOP_NOFOLLOW;
1910 }
1911
1912 if (unlikely(!S_ISREG(inode->i_mode)))
1913 add_flags = DCACHE_SPECIAL_TYPE;
1914
1915type_determined:
1916 if (unlikely(IS_AUTOMOUNT(inode)))
1917 add_flags |= DCACHE_NEED_AUTOMOUNT;
1918 return add_flags;
1919}
1920
1921static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1922{
1923 unsigned add_flags = d_flags_for_inode(inode);
1924 WARN_ON(d_in_lookup(dentry));
1925
1926 spin_lock(&dentry->d_lock);
1927 /*
1928 * The negative counter only tracks dentries on the LRU. Don't dec if
1929 * d_lru is on another list.
1930 */
1931 if ((dentry->d_flags &
1932 (DCACHE_LRU_LIST|DCACHE_SHRINK_LIST)) == DCACHE_LRU_LIST)
1933 this_cpu_dec(nr_dentry_negative);
1934 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
1935 raw_write_seqcount_begin(&dentry->d_seq);
1936 __d_set_inode_and_type(dentry, inode, add_flags);
1937 raw_write_seqcount_end(&dentry->d_seq);
1938 fsnotify_update_flags(dentry);
1939 spin_unlock(&dentry->d_lock);
1940}
1941
1942/**
1943 * d_instantiate - fill in inode information for a dentry
1944 * @entry: dentry to complete
1945 * @inode: inode to attach to this dentry
1946 *
1947 * Fill in inode information in the entry.
1948 *
1949 * This turns negative dentries into productive full members
1950 * of society.
1951 *
1952 * NOTE! This assumes that the inode count has been incremented
1953 * (or otherwise set) by the caller to indicate that it is now
1954 * in use by the dcache.
1955 */
1956
1957void d_instantiate(struct dentry *entry, struct inode * inode)
1958{
1959 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
1960 if (inode) {
1961 security_d_instantiate(entry, inode);
1962 spin_lock(&inode->i_lock);
1963 __d_instantiate(entry, inode);
1964 spin_unlock(&inode->i_lock);
1965 }
1966}
1967EXPORT_SYMBOL(d_instantiate);
1968
1969/*
1970 * This should be equivalent to d_instantiate() + unlock_new_inode(),
1971 * with lockdep-related part of unlock_new_inode() done before
1972 * anything else. Use that instead of open-coding d_instantiate()/
1973 * unlock_new_inode() combinations.
1974 */
1975void d_instantiate_new(struct dentry *entry, struct inode *inode)
1976{
1977 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
1978 BUG_ON(!inode);
1979 lockdep_annotate_inode_mutex_key(inode);
1980 security_d_instantiate(entry, inode);
1981 spin_lock(&inode->i_lock);
1982 __d_instantiate(entry, inode);
1983 WARN_ON(!(inode->i_state & I_NEW));
1984 inode->i_state &= ~I_NEW & ~I_CREATING;
1985 smp_mb();
1986 wake_up_bit(&inode->i_state, __I_NEW);
1987 spin_unlock(&inode->i_lock);
1988}
1989EXPORT_SYMBOL(d_instantiate_new);
1990
1991struct dentry *d_make_root(struct inode *root_inode)
1992{
1993 struct dentry *res = NULL;
1994
1995 if (root_inode) {
1996 res = d_alloc_anon(root_inode->i_sb);
1997 if (res)
1998 d_instantiate(res, root_inode);
1999 else
2000 iput(root_inode);
2001 }
2002 return res;
2003}
2004EXPORT_SYMBOL(d_make_root);
2005
2006static struct dentry *__d_instantiate_anon(struct dentry *dentry,
2007 struct inode *inode,
2008 bool disconnected)
2009{
2010 struct dentry *res;
2011 unsigned add_flags;
2012
2013 security_d_instantiate(dentry, inode);
2014 spin_lock(&inode->i_lock);
2015 res = __d_find_any_alias(inode);
2016 if (res) {
2017 spin_unlock(&inode->i_lock);
2018 dput(dentry);
2019 goto out_iput;
2020 }
2021
2022 /* attach a disconnected dentry */
2023 add_flags = d_flags_for_inode(inode);
2024
2025 if (disconnected)
2026 add_flags |= DCACHE_DISCONNECTED;
2027
2028 spin_lock(&dentry->d_lock);
2029 __d_set_inode_and_type(dentry, inode, add_flags);
2030 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2031 if (!disconnected) {
2032 hlist_bl_lock(&dentry->d_sb->s_roots);
2033 hlist_bl_add_head(&dentry->d_hash, &dentry->d_sb->s_roots);
2034 hlist_bl_unlock(&dentry->d_sb->s_roots);
2035 }
2036 spin_unlock(&dentry->d_lock);
2037 spin_unlock(&inode->i_lock);
2038
2039 return dentry;
2040
2041 out_iput:
2042 iput(inode);
2043 return res;
2044}
2045
2046struct dentry *d_instantiate_anon(struct dentry *dentry, struct inode *inode)
2047{
2048 return __d_instantiate_anon(dentry, inode, true);
2049}
2050EXPORT_SYMBOL(d_instantiate_anon);
2051
2052static struct dentry *__d_obtain_alias(struct inode *inode, bool disconnected)
2053{
2054 struct dentry *tmp;
2055 struct dentry *res;
2056
2057 if (!inode)
2058 return ERR_PTR(-ESTALE);
2059 if (IS_ERR(inode))
2060 return ERR_CAST(inode);
2061
2062 res = d_find_any_alias(inode);
2063 if (res)
2064 goto out_iput;
2065
2066 tmp = d_alloc_anon(inode->i_sb);
2067 if (!tmp) {
2068 res = ERR_PTR(-ENOMEM);
2069 goto out_iput;
2070 }
2071
2072 return __d_instantiate_anon(tmp, inode, disconnected);
2073
2074out_iput:
2075 iput(inode);
2076 return res;
2077}
2078
2079/**
2080 * d_obtain_alias - find or allocate a DISCONNECTED dentry for a given inode
2081 * @inode: inode to allocate the dentry for
2082 *
2083 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
2084 * similar open by handle operations. The returned dentry may be anonymous,
2085 * or may have a full name (if the inode was already in the cache).
2086 *
2087 * When called on a directory inode, we must ensure that the inode only ever
2088 * has one dentry. If a dentry is found, that is returned instead of
2089 * allocating a new one.
2090 *
2091 * On successful return, the reference to the inode has been transferred
2092 * to the dentry. In case of an error the reference on the inode is released.
2093 * To make it easier to use in export operations a %NULL or IS_ERR inode may
2094 * be passed in and the error will be propagated to the return value,
2095 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
2096 */
2097struct dentry *d_obtain_alias(struct inode *inode)
2098{
2099 return __d_obtain_alias(inode, true);
2100}
2101EXPORT_SYMBOL(d_obtain_alias);
2102
2103/**
2104 * d_obtain_root - find or allocate a dentry for a given inode
2105 * @inode: inode to allocate the dentry for
2106 *
2107 * Obtain an IS_ROOT dentry for the root of a filesystem.
2108 *
2109 * We must ensure that directory inodes only ever have one dentry. If a
2110 * dentry is found, that is returned instead of allocating a new one.
2111 *
2112 * On successful return, the reference to the inode has been transferred
2113 * to the dentry. In case of an error the reference on the inode is
2114 * released. A %NULL or IS_ERR inode may be passed in and will be the
2115 * error will be propagate to the return value, with a %NULL @inode
2116 * replaced by ERR_PTR(-ESTALE).
2117 */
2118struct dentry *d_obtain_root(struct inode *inode)
2119{
2120 return __d_obtain_alias(inode, false);
2121}
2122EXPORT_SYMBOL(d_obtain_root);
2123
2124/**
2125 * d_add_ci - lookup or allocate new dentry with case-exact name
2126 * @inode: the inode case-insensitive lookup has found
2127 * @dentry: the negative dentry that was passed to the parent's lookup func
2128 * @name: the case-exact name to be associated with the returned dentry
2129 *
2130 * This is to avoid filling the dcache with case-insensitive names to the
2131 * same inode, only the actual correct case is stored in the dcache for
2132 * case-insensitive filesystems.
2133 *
2134 * For a case-insensitive lookup match and if the the case-exact dentry
2135 * already exists in in the dcache, use it and return it.
2136 *
2137 * If no entry exists with the exact case name, allocate new dentry with
2138 * the exact case, and return the spliced entry.
2139 */
2140struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
2141 struct qstr *name)
2142{
2143 struct dentry *found, *res;
2144
2145 /*
2146 * First check if a dentry matching the name already exists,
2147 * if not go ahead and create it now.
2148 */
2149 found = d_hash_and_lookup(dentry->d_parent, name);
2150 if (found) {
2151 iput(inode);
2152 return found;
2153 }
2154 if (d_in_lookup(dentry)) {
2155 found = d_alloc_parallel(dentry->d_parent, name,
2156 dentry->d_wait);
2157 if (IS_ERR(found) || !d_in_lookup(found)) {
2158 iput(inode);
2159 return found;
2160 }
2161 } else {
2162 found = d_alloc(dentry->d_parent, name);
2163 if (!found) {
2164 iput(inode);
2165 return ERR_PTR(-ENOMEM);
2166 }
2167 }
2168 res = d_splice_alias(inode, found);
2169 if (res) {
2170 dput(found);
2171 return res;
2172 }
2173 return found;
2174}
2175EXPORT_SYMBOL(d_add_ci);
2176
2177
2178static inline bool d_same_name(const struct dentry *dentry,
2179 const struct dentry *parent,
2180 const struct qstr *name)
2181{
2182 if (likely(!(parent->d_flags & DCACHE_OP_COMPARE))) {
2183 if (dentry->d_name.len != name->len)
2184 return false;
2185 return dentry_cmp(dentry, name->name, name->len) == 0;
2186 }
2187 return parent->d_op->d_compare(dentry,
2188 dentry->d_name.len, dentry->d_name.name,
2189 name) == 0;
2190}
2191
2192/**
2193 * __d_lookup_rcu - search for a dentry (racy, store-free)
2194 * @parent: parent dentry
2195 * @name: qstr of name we wish to find
2196 * @seqp: returns d_seq value at the point where the dentry was found
2197 * Returns: dentry, or NULL
2198 *
2199 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
2200 * resolution (store-free path walking) design described in
2201 * Documentation/filesystems/path-lookup.txt.
2202 *
2203 * This is not to be used outside core vfs.
2204 *
2205 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
2206 * held, and rcu_read_lock held. The returned dentry must not be stored into
2207 * without taking d_lock and checking d_seq sequence count against @seq
2208 * returned here.
2209 *
2210 * A refcount may be taken on the found dentry with the d_rcu_to_refcount
2211 * function.
2212 *
2213 * Alternatively, __d_lookup_rcu may be called again to look up the child of
2214 * the returned dentry, so long as its parent's seqlock is checked after the
2215 * child is looked up. Thus, an interlocking stepping of sequence lock checks
2216 * is formed, giving integrity down the path walk.
2217 *
2218 * NOTE! The caller *has* to check the resulting dentry against the sequence
2219 * number we've returned before using any of the resulting dentry state!
2220 */
2221struct dentry *__d_lookup_rcu(const struct dentry *parent,
2222 const struct qstr *name,
2223 unsigned *seqp)
2224{
2225 u64 hashlen = name->hash_len;
2226 const unsigned char *str = name->name;
2227 struct hlist_bl_head *b = d_hash(hashlen_hash(hashlen));
2228 struct hlist_bl_node *node;
2229 struct dentry *dentry;
2230
2231 /*
2232 * Note: There is significant duplication with __d_lookup_rcu which is
2233 * required to prevent single threaded performance regressions
2234 * especially on architectures where smp_rmb (in seqcounts) are costly.
2235 * Keep the two functions in sync.
2236 */
2237
2238 /*
2239 * The hash list is protected using RCU.
2240 *
2241 * Carefully use d_seq when comparing a candidate dentry, to avoid
2242 * races with d_move().
2243 *
2244 * It is possible that concurrent renames can mess up our list
2245 * walk here and result in missing our dentry, resulting in the
2246 * false-negative result. d_lookup() protects against concurrent
2247 * renames using rename_lock seqlock.
2248 *
2249 * See Documentation/filesystems/path-lookup.txt for more details.
2250 */
2251 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2252 unsigned seq;
2253
2254seqretry:
2255 /*
2256 * The dentry sequence count protects us from concurrent
2257 * renames, and thus protects parent and name fields.
2258 *
2259 * The caller must perform a seqcount check in order
2260 * to do anything useful with the returned dentry.
2261 *
2262 * NOTE! We do a "raw" seqcount_begin here. That means that
2263 * we don't wait for the sequence count to stabilize if it
2264 * is in the middle of a sequence change. If we do the slow
2265 * dentry compare, we will do seqretries until it is stable,
2266 * and if we end up with a successful lookup, we actually
2267 * want to exit RCU lookup anyway.
2268 *
2269 * Note that raw_seqcount_begin still *does* smp_rmb(), so
2270 * we are still guaranteed NUL-termination of ->d_name.name.
2271 */
2272 seq = raw_seqcount_begin(&dentry->d_seq);
2273 if (dentry->d_parent != parent)
2274 continue;
2275 if (d_unhashed(dentry))
2276 continue;
2277
2278 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
2279 int tlen;
2280 const char *tname;
2281 if (dentry->d_name.hash != hashlen_hash(hashlen))
2282 continue;
2283 tlen = dentry->d_name.len;
2284 tname = dentry->d_name.name;
2285 /* we want a consistent (name,len) pair */
2286 if (read_seqcount_retry(&dentry->d_seq, seq)) {
2287 cpu_relax();
2288 goto seqretry;
2289 }
2290 if (parent->d_op->d_compare(dentry,
2291 tlen, tname, name) != 0)
2292 continue;
2293 } else {
2294 if (dentry->d_name.hash_len != hashlen)
2295 continue;
2296 if (dentry_cmp(dentry, str, hashlen_len(hashlen)) != 0)
2297 continue;
2298 }
2299 *seqp = seq;
2300 return dentry;
2301 }
2302 return NULL;
2303}
2304
2305/**
2306 * d_lookup - search for a dentry
2307 * @parent: parent dentry
2308 * @name: qstr of name we wish to find
2309 * Returns: dentry, or NULL
2310 *
2311 * d_lookup searches the children of the parent dentry for the name in
2312 * question. If the dentry is found its reference count is incremented and the
2313 * dentry is returned. The caller must use dput to free the entry when it has
2314 * finished using it. %NULL is returned if the dentry does not exist.
2315 */
2316struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
2317{
2318 struct dentry *dentry;
2319 unsigned seq;
2320
2321 do {
2322 seq = read_seqbegin(&rename_lock);
2323 dentry = __d_lookup(parent, name);
2324 if (dentry)
2325 break;
2326 } while (read_seqretry(&rename_lock, seq));
2327 return dentry;
2328}
2329EXPORT_SYMBOL(d_lookup);
2330
2331/**
2332 * __d_lookup - search for a dentry (racy)
2333 * @parent: parent dentry
2334 * @name: qstr of name we wish to find
2335 * Returns: dentry, or NULL
2336 *
2337 * __d_lookup is like d_lookup, however it may (rarely) return a
2338 * false-negative result due to unrelated rename activity.
2339 *
2340 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
2341 * however it must be used carefully, eg. with a following d_lookup in
2342 * the case of failure.
2343 *
2344 * __d_lookup callers must be commented.
2345 */
2346struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
2347{
2348 unsigned int hash = name->hash;
2349 struct hlist_bl_head *b = d_hash(hash);
2350 struct hlist_bl_node *node;
2351 struct dentry *found = NULL;
2352 struct dentry *dentry;
2353
2354 /*
2355 * Note: There is significant duplication with __d_lookup_rcu which is
2356 * required to prevent single threaded performance regressions
2357 * especially on architectures where smp_rmb (in seqcounts) are costly.
2358 * Keep the two functions in sync.
2359 */
2360
2361 /*
2362 * The hash list is protected using RCU.
2363 *
2364 * Take d_lock when comparing a candidate dentry, to avoid races
2365 * with d_move().
2366 *
2367 * It is possible that concurrent renames can mess up our list
2368 * walk here and result in missing our dentry, resulting in the
2369 * false-negative result. d_lookup() protects against concurrent
2370 * renames using rename_lock seqlock.
2371 *
2372 * See Documentation/filesystems/path-lookup.txt for more details.
2373 */
2374 rcu_read_lock();
2375
2376 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2377
2378 if (dentry->d_name.hash != hash)
2379 continue;
2380
2381 spin_lock(&dentry->d_lock);
2382 if (dentry->d_parent != parent)
2383 goto next;
2384 if (d_unhashed(dentry))
2385 goto next;
2386
2387 if (!d_same_name(dentry, parent, name))
2388 goto next;
2389
2390 dentry->d_lockref.count++;
2391 found = dentry;
2392 spin_unlock(&dentry->d_lock);
2393 break;
2394next:
2395 spin_unlock(&dentry->d_lock);
2396 }
2397 rcu_read_unlock();
2398
2399 return found;
2400}
2401
2402/**
2403 * d_hash_and_lookup - hash the qstr then search for a dentry
2404 * @dir: Directory to search in
2405 * @name: qstr of name we wish to find
2406 *
2407 * On lookup failure NULL is returned; on bad name - ERR_PTR(-error)
2408 */
2409struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2410{
2411 /*
2412 * Check for a fs-specific hash function. Note that we must
2413 * calculate the standard hash first, as the d_op->d_hash()
2414 * routine may choose to leave the hash value unchanged.
2415 */
2416 name->hash = full_name_hash(dir, name->name, name->len);
2417 if (dir->d_flags & DCACHE_OP_HASH) {
2418 int err = dir->d_op->d_hash(dir, name);
2419 if (unlikely(err < 0))
2420 return ERR_PTR(err);
2421 }
2422 return d_lookup(dir, name);
2423}
2424EXPORT_SYMBOL(d_hash_and_lookup);
2425
2426/*
2427 * When a file is deleted, we have two options:
2428 * - turn this dentry into a negative dentry
2429 * - unhash this dentry and free it.
2430 *
2431 * Usually, we want to just turn this into
2432 * a negative dentry, but if anybody else is
2433 * currently using the dentry or the inode
2434 * we can't do that and we fall back on removing
2435 * it from the hash queues and waiting for
2436 * it to be deleted later when it has no users
2437 */
2438
2439/**
2440 * d_delete - delete a dentry
2441 * @dentry: The dentry to delete
2442 *
2443 * Turn the dentry into a negative dentry if possible, otherwise
2444 * remove it from the hash queues so it can be deleted later
2445 */
2446
2447void d_delete(struct dentry * dentry)
2448{
2449 struct inode *inode = dentry->d_inode;
2450
2451 spin_lock(&inode->i_lock);
2452 spin_lock(&dentry->d_lock);
2453 /*
2454 * Are we the only user?
2455 */
2456 if (dentry->d_lockref.count == 1) {
2457 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
2458 dentry_unlink_inode(dentry);
2459 } else {
2460 __d_drop(dentry);
2461 spin_unlock(&dentry->d_lock);
2462 spin_unlock(&inode->i_lock);
2463 }
2464}
2465EXPORT_SYMBOL(d_delete);
2466
2467static void __d_rehash(struct dentry *entry)
2468{
2469 struct hlist_bl_head *b = d_hash(entry->d_name.hash);
2470
2471 hlist_bl_lock(b);
2472 hlist_bl_add_head_rcu(&entry->d_hash, b);
2473 hlist_bl_unlock(b);
2474}
2475
2476/**
2477 * d_rehash - add an entry back to the hash
2478 * @entry: dentry to add to the hash
2479 *
2480 * Adds a dentry to the hash according to its name.
2481 */
2482
2483void d_rehash(struct dentry * entry)
2484{
2485 spin_lock(&entry->d_lock);
2486 __d_rehash(entry);
2487 spin_unlock(&entry->d_lock);
2488}
2489EXPORT_SYMBOL(d_rehash);
2490
2491static inline unsigned start_dir_add(struct inode *dir)
2492{
2493
2494 for (;;) {
2495 unsigned n = dir->i_dir_seq;
2496 if (!(n & 1) && cmpxchg(&dir->i_dir_seq, n, n + 1) == n)
2497 return n;
2498 cpu_relax();
2499 }
2500}
2501
2502static inline void end_dir_add(struct inode *dir, unsigned n)
2503{
2504 smp_store_release(&dir->i_dir_seq, n + 2);
2505}
2506
2507static void d_wait_lookup(struct dentry *dentry)
2508{
2509 if (d_in_lookup(dentry)) {
2510 DECLARE_WAITQUEUE(wait, current);
2511 add_wait_queue(dentry->d_wait, &wait);
2512 do {
2513 set_current_state(TASK_UNINTERRUPTIBLE);
2514 spin_unlock(&dentry->d_lock);
2515 schedule();
2516 spin_lock(&dentry->d_lock);
2517 } while (d_in_lookup(dentry));
2518 }
2519}
2520
2521struct dentry *d_alloc_parallel(struct dentry *parent,
2522 const struct qstr *name,
2523 wait_queue_head_t *wq)
2524{
2525 unsigned int hash = name->hash;
2526 struct hlist_bl_head *b = in_lookup_hash(parent, hash);
2527 struct hlist_bl_node *node;
2528 struct dentry *new = d_alloc(parent, name);
2529 struct dentry *dentry;
2530 unsigned seq, r_seq, d_seq;
2531
2532 if (unlikely(!new))
2533 return ERR_PTR(-ENOMEM);
2534
2535retry:
2536 rcu_read_lock();
2537 seq = smp_load_acquire(&parent->d_inode->i_dir_seq);
2538 r_seq = read_seqbegin(&rename_lock);
2539 dentry = __d_lookup_rcu(parent, name, &d_seq);
2540 if (unlikely(dentry)) {
2541 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2542 rcu_read_unlock();
2543 goto retry;
2544 }
2545 if (read_seqcount_retry(&dentry->d_seq, d_seq)) {
2546 rcu_read_unlock();
2547 dput(dentry);
2548 goto retry;
2549 }
2550 rcu_read_unlock();
2551 dput(new);
2552 return dentry;
2553 }
2554 if (unlikely(read_seqretry(&rename_lock, r_seq))) {
2555 rcu_read_unlock();
2556 goto retry;
2557 }
2558
2559 if (unlikely(seq & 1)) {
2560 rcu_read_unlock();
2561 goto retry;
2562 }
2563
2564 hlist_bl_lock(b);
2565 if (unlikely(READ_ONCE(parent->d_inode->i_dir_seq) != seq)) {
2566 hlist_bl_unlock(b);
2567 rcu_read_unlock();
2568 goto retry;
2569 }
2570 /*
2571 * No changes for the parent since the beginning of d_lookup().
2572 * Since all removals from the chain happen with hlist_bl_lock(),
2573 * any potential in-lookup matches are going to stay here until
2574 * we unlock the chain. All fields are stable in everything
2575 * we encounter.
2576 */
2577 hlist_bl_for_each_entry(dentry, node, b, d_u.d_in_lookup_hash) {
2578 if (dentry->d_name.hash != hash)
2579 continue;
2580 if (dentry->d_parent != parent)
2581 continue;
2582 if (!d_same_name(dentry, parent, name))
2583 continue;
2584 hlist_bl_unlock(b);
2585 /* now we can try to grab a reference */
2586 if (!lockref_get_not_dead(&dentry->d_lockref)) {
2587 rcu_read_unlock();
2588 goto retry;
2589 }
2590
2591 rcu_read_unlock();
2592 /*
2593 * somebody is likely to be still doing lookup for it;
2594 * wait for them to finish
2595 */
2596 spin_lock(&dentry->d_lock);
2597 d_wait_lookup(dentry);
2598 /*
2599 * it's not in-lookup anymore; in principle we should repeat
2600 * everything from dcache lookup, but it's likely to be what
2601 * d_lookup() would've found anyway. If it is, just return it;
2602 * otherwise we really have to repeat the whole thing.
2603 */
2604 if (unlikely(dentry->d_name.hash != hash))
2605 goto mismatch;
2606 if (unlikely(dentry->d_parent != parent))
2607 goto mismatch;
2608 if (unlikely(d_unhashed(dentry)))
2609 goto mismatch;
2610 if (unlikely(!d_same_name(dentry, parent, name)))
2611 goto mismatch;
2612 /* OK, it *is* a hashed match; return it */
2613 spin_unlock(&dentry->d_lock);
2614 dput(new);
2615 return dentry;
2616 }
2617 rcu_read_unlock();
2618 /* we can't take ->d_lock here; it's OK, though. */
2619 new->d_flags |= DCACHE_PAR_LOOKUP;
2620 new->d_wait = wq;
2621 hlist_bl_add_head_rcu(&new->d_u.d_in_lookup_hash, b);
2622 hlist_bl_unlock(b);
2623 return new;
2624mismatch:
2625 spin_unlock(&dentry->d_lock);
2626 dput(dentry);
2627 goto retry;
2628}
2629EXPORT_SYMBOL(d_alloc_parallel);
2630
2631void __d_lookup_done(struct dentry *dentry)
2632{
2633 struct hlist_bl_head *b = in_lookup_hash(dentry->d_parent,
2634 dentry->d_name.hash);
2635 hlist_bl_lock(b);
2636 dentry->d_flags &= ~DCACHE_PAR_LOOKUP;
2637 __hlist_bl_del(&dentry->d_u.d_in_lookup_hash);
2638 wake_up_all(dentry->d_wait);
2639 dentry->d_wait = NULL;
2640 hlist_bl_unlock(b);
2641 INIT_HLIST_NODE(&dentry->d_u.d_alias);
2642 INIT_LIST_HEAD(&dentry->d_lru);
2643}
2644EXPORT_SYMBOL(__d_lookup_done);
2645
2646/* inode->i_lock held if inode is non-NULL */
2647
2648static inline void __d_add(struct dentry *dentry, struct inode *inode)
2649{
2650 struct inode *dir = NULL;
2651 unsigned n;
2652 spin_lock(&dentry->d_lock);
2653 if (unlikely(d_in_lookup(dentry))) {
2654 dir = dentry->d_parent->d_inode;
2655 n = start_dir_add(dir);
2656 __d_lookup_done(dentry);
2657 }
2658 if (inode) {
2659 unsigned add_flags = d_flags_for_inode(inode);
2660 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
2661 raw_write_seqcount_begin(&dentry->d_seq);
2662 __d_set_inode_and_type(dentry, inode, add_flags);
2663 raw_write_seqcount_end(&dentry->d_seq);
2664 fsnotify_update_flags(dentry);
2665 }
2666 __d_rehash(dentry);
2667 if (dir)
2668 end_dir_add(dir, n);
2669 spin_unlock(&dentry->d_lock);
2670 if (inode)
2671 spin_unlock(&inode->i_lock);
2672}
2673
2674/**
2675 * d_add - add dentry to hash queues
2676 * @entry: dentry to add
2677 * @inode: The inode to attach to this dentry
2678 *
2679 * This adds the entry to the hash queues and initializes @inode.
2680 * The entry was actually filled in earlier during d_alloc().
2681 */
2682
2683void d_add(struct dentry *entry, struct inode *inode)
2684{
2685 if (inode) {
2686 security_d_instantiate(entry, inode);
2687 spin_lock(&inode->i_lock);
2688 }
2689 __d_add(entry, inode);
2690}
2691EXPORT_SYMBOL(d_add);
2692
2693/**
2694 * d_exact_alias - find and hash an exact unhashed alias
2695 * @entry: dentry to add
2696 * @inode: The inode to go with this dentry
2697 *
2698 * If an unhashed dentry with the same name/parent and desired
2699 * inode already exists, hash and return it. Otherwise, return
2700 * NULL.
2701 *
2702 * Parent directory should be locked.
2703 */
2704struct dentry *d_exact_alias(struct dentry *entry, struct inode *inode)
2705{
2706 struct dentry *alias;
2707 unsigned int hash = entry->d_name.hash;
2708
2709 spin_lock(&inode->i_lock);
2710 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
2711 /*
2712 * Don't need alias->d_lock here, because aliases with
2713 * d_parent == entry->d_parent are not subject to name or
2714 * parent changes, because the parent inode i_mutex is held.
2715 */
2716 if (alias->d_name.hash != hash)
2717 continue;
2718 if (alias->d_parent != entry->d_parent)
2719 continue;
2720 if (!d_same_name(alias, entry->d_parent, &entry->d_name))
2721 continue;
2722 spin_lock(&alias->d_lock);
2723 if (!d_unhashed(alias)) {
2724 spin_unlock(&alias->d_lock);
2725 alias = NULL;
2726 } else {
2727 __dget_dlock(alias);
2728 __d_rehash(alias);
2729 spin_unlock(&alias->d_lock);
2730 }
2731 spin_unlock(&inode->i_lock);
2732 return alias;
2733 }
2734 spin_unlock(&inode->i_lock);
2735 return NULL;
2736}
2737EXPORT_SYMBOL(d_exact_alias);
2738
2739static void swap_names(struct dentry *dentry, struct dentry *target)
2740{
2741 if (unlikely(dname_external(target))) {
2742 if (unlikely(dname_external(dentry))) {
2743 /*
2744 * Both external: swap the pointers
2745 */
2746 swap(target->d_name.name, dentry->d_name.name);
2747 } else {
2748 /*
2749 * dentry:internal, target:external. Steal target's
2750 * storage and make target internal.
2751 */
2752 memcpy(target->d_iname, dentry->d_name.name,
2753 dentry->d_name.len + 1);
2754 dentry->d_name.name = target->d_name.name;
2755 target->d_name.name = target->d_iname;
2756 }
2757 } else {
2758 if (unlikely(dname_external(dentry))) {
2759 /*
2760 * dentry:external, target:internal. Give dentry's
2761 * storage to target and make dentry internal
2762 */
2763 memcpy(dentry->d_iname, target->d_name.name,
2764 target->d_name.len + 1);
2765 target->d_name.name = dentry->d_name.name;
2766 dentry->d_name.name = dentry->d_iname;
2767 } else {
2768 /*
2769 * Both are internal.
2770 */
2771 unsigned int i;
2772 BUILD_BUG_ON(!IS_ALIGNED(DNAME_INLINE_LEN, sizeof(long)));
2773 for (i = 0; i < DNAME_INLINE_LEN / sizeof(long); i++) {
2774 swap(((long *) &dentry->d_iname)[i],
2775 ((long *) &target->d_iname)[i]);
2776 }
2777 }
2778 }
2779 swap(dentry->d_name.hash_len, target->d_name.hash_len);
2780}
2781
2782static void copy_name(struct dentry *dentry, struct dentry *target)
2783{
2784 struct external_name *old_name = NULL;
2785 if (unlikely(dname_external(dentry)))
2786 old_name = external_name(dentry);
2787 if (unlikely(dname_external(target))) {
2788 atomic_inc(&external_name(target)->u.count);
2789 dentry->d_name = target->d_name;
2790 } else {
2791 memcpy(dentry->d_iname, target->d_name.name,
2792 target->d_name.len + 1);
2793 dentry->d_name.name = dentry->d_iname;
2794 dentry->d_name.hash_len = target->d_name.hash_len;
2795 }
2796 if (old_name && likely(atomic_dec_and_test(&old_name->u.count)))
2797 kfree_rcu(old_name, u.head);
2798}
2799
2800/*
2801 * __d_move - move a dentry
2802 * @dentry: entry to move
2803 * @target: new dentry
2804 * @exchange: exchange the two dentries
2805 *
2806 * Update the dcache to reflect the move of a file name. Negative
2807 * dcache entries should not be moved in this way. Caller must hold
2808 * rename_lock, the i_mutex of the source and target directories,
2809 * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
2810 */
2811static void __d_move(struct dentry *dentry, struct dentry *target,
2812 bool exchange)
2813{
2814 struct dentry *old_parent, *p;
2815 struct inode *dir = NULL;
2816 unsigned n;
2817
2818 WARN_ON(!dentry->d_inode);
2819 if (WARN_ON(dentry == target))
2820 return;
2821
2822 BUG_ON(d_ancestor(target, dentry));
2823 old_parent = dentry->d_parent;
2824 p = d_ancestor(old_parent, target);
2825 if (IS_ROOT(dentry)) {
2826 BUG_ON(p);
2827 spin_lock(&target->d_parent->d_lock);
2828 } else if (!p) {
2829 /* target is not a descendent of dentry->d_parent */
2830 spin_lock(&target->d_parent->d_lock);
2831 spin_lock_nested(&old_parent->d_lock, DENTRY_D_LOCK_NESTED);
2832 } else {
2833 BUG_ON(p == dentry);
2834 spin_lock(&old_parent->d_lock);
2835 if (p != target)
2836 spin_lock_nested(&target->d_parent->d_lock,
2837 DENTRY_D_LOCK_NESTED);
2838 }
2839 spin_lock_nested(&dentry->d_lock, 2);
2840 spin_lock_nested(&target->d_lock, 3);
2841
2842 if (unlikely(d_in_lookup(target))) {
2843 dir = target->d_parent->d_inode;
2844 n = start_dir_add(dir);
2845 __d_lookup_done(target);
2846 }
2847
2848 write_seqcount_begin(&dentry->d_seq);
2849 write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED);
2850
2851 /* unhash both */
2852 if (!d_unhashed(dentry))
2853 ___d_drop(dentry);
2854 if (!d_unhashed(target))
2855 ___d_drop(target);
2856
2857 /* ... and switch them in the tree */
2858 dentry->d_parent = target->d_parent;
2859 if (!exchange) {
2860 copy_name(dentry, target);
2861 target->d_hash.pprev = NULL;
2862 dentry->d_parent->d_lockref.count++;
2863 if (dentry != old_parent) /* wasn't IS_ROOT */
2864 WARN_ON(!--old_parent->d_lockref.count);
2865 } else {
2866 target->d_parent = old_parent;
2867 swap_names(dentry, target);
2868 list_move(&target->d_child, &target->d_parent->d_subdirs);
2869 __d_rehash(target);
2870 fsnotify_update_flags(target);
2871 }
2872 list_move(&dentry->d_child, &dentry->d_parent->d_subdirs);
2873 __d_rehash(dentry);
2874 fsnotify_update_flags(dentry);
2875 fscrypt_handle_d_move(dentry);
2876
2877 write_seqcount_end(&target->d_seq);
2878 write_seqcount_end(&dentry->d_seq);
2879
2880 if (dir)
2881 end_dir_add(dir, n);
2882
2883 if (dentry->d_parent != old_parent)
2884 spin_unlock(&dentry->d_parent->d_lock);
2885 if (dentry != old_parent)
2886 spin_unlock(&old_parent->d_lock);
2887 spin_unlock(&target->d_lock);
2888 spin_unlock(&dentry->d_lock);
2889}
2890
2891/*
2892 * d_move - move a dentry
2893 * @dentry: entry to move
2894 * @target: new dentry
2895 *
2896 * Update the dcache to reflect the move of a file name. Negative
2897 * dcache entries should not be moved in this way. See the locking
2898 * requirements for __d_move.
2899 */
2900void d_move(struct dentry *dentry, struct dentry *target)
2901{
2902 write_seqlock(&rename_lock);
2903 __d_move(dentry, target, false);
2904 write_sequnlock(&rename_lock);
2905}
2906EXPORT_SYMBOL(d_move);
2907
2908/*
2909 * d_exchange - exchange two dentries
2910 * @dentry1: first dentry
2911 * @dentry2: second dentry
2912 */
2913void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
2914{
2915 write_seqlock(&rename_lock);
2916
2917 WARN_ON(!dentry1->d_inode);
2918 WARN_ON(!dentry2->d_inode);
2919 WARN_ON(IS_ROOT(dentry1));
2920 WARN_ON(IS_ROOT(dentry2));
2921
2922 __d_move(dentry1, dentry2, true);
2923
2924 write_sequnlock(&rename_lock);
2925}
2926
2927/**
2928 * d_ancestor - search for an ancestor
2929 * @p1: ancestor dentry
2930 * @p2: child dentry
2931 *
2932 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2933 * an ancestor of p2, else NULL.
2934 */
2935struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
2936{
2937 struct dentry *p;
2938
2939 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
2940 if (p->d_parent == p1)
2941 return p;
2942 }
2943 return NULL;
2944}
2945
2946/*
2947 * This helper attempts to cope with remotely renamed directories
2948 *
2949 * It assumes that the caller is already holding
2950 * dentry->d_parent->d_inode->i_mutex, and rename_lock
2951 *
2952 * Note: If ever the locking in lock_rename() changes, then please
2953 * remember to update this too...
2954 */
2955static int __d_unalias(struct inode *inode,
2956 struct dentry *dentry, struct dentry *alias)
2957{
2958 struct mutex *m1 = NULL;
2959 struct rw_semaphore *m2 = NULL;
2960 int ret = -ESTALE;
2961
2962 /* If alias and dentry share a parent, then no extra locks required */
2963 if (alias->d_parent == dentry->d_parent)
2964 goto out_unalias;
2965
2966 /* See lock_rename() */
2967 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2968 goto out_err;
2969 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2970 if (!inode_trylock_shared(alias->d_parent->d_inode))
2971 goto out_err;
2972 m2 = &alias->d_parent->d_inode->i_rwsem;
2973out_unalias:
2974 __d_move(alias, dentry, false);
2975 ret = 0;
2976out_err:
2977 if (m2)
2978 up_read(m2);
2979 if (m1)
2980 mutex_unlock(m1);
2981 return ret;
2982}
2983
2984/**
2985 * d_splice_alias - splice a disconnected dentry into the tree if one exists
2986 * @inode: the inode which may have a disconnected dentry
2987 * @dentry: a negative dentry which we want to point to the inode.
2988 *
2989 * If inode is a directory and has an IS_ROOT alias, then d_move that in
2990 * place of the given dentry and return it, else simply d_add the inode
2991 * to the dentry and return NULL.
2992 *
2993 * If a non-IS_ROOT directory is found, the filesystem is corrupt, and
2994 * we should error out: directories can't have multiple aliases.
2995 *
2996 * This is needed in the lookup routine of any filesystem that is exportable
2997 * (via knfsd) so that we can build dcache paths to directories effectively.
2998 *
2999 * If a dentry was found and moved, then it is returned. Otherwise NULL
3000 * is returned. This matches the expected return value of ->lookup.
3001 *
3002 * Cluster filesystems may call this function with a negative, hashed dentry.
3003 * In that case, we know that the inode will be a regular file, and also this
3004 * will only occur during atomic_open. So we need to check for the dentry
3005 * being already hashed only in the final case.
3006 */
3007struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
3008{
3009 if (IS_ERR(inode))
3010 return ERR_CAST(inode);
3011
3012 BUG_ON(!d_unhashed(dentry));
3013
3014 if (!inode)
3015 goto out;
3016
3017 security_d_instantiate(dentry, inode);
3018 spin_lock(&inode->i_lock);
3019 if (S_ISDIR(inode->i_mode)) {
3020 struct dentry *new = __d_find_any_alias(inode);
3021 if (unlikely(new)) {
3022 /* The reference to new ensures it remains an alias */
3023 spin_unlock(&inode->i_lock);
3024 write_seqlock(&rename_lock);
3025 if (unlikely(d_ancestor(new, dentry))) {
3026 write_sequnlock(&rename_lock);
3027 dput(new);
3028 new = ERR_PTR(-ELOOP);
3029 pr_warn_ratelimited(
3030 "VFS: Lookup of '%s' in %s %s"
3031 " would have caused loop\n",
3032 dentry->d_name.name,
3033 inode->i_sb->s_type->name,
3034 inode->i_sb->s_id);
3035 } else if (!IS_ROOT(new)) {
3036 struct dentry *old_parent = dget(new->d_parent);
3037 int err = __d_unalias(inode, dentry, new);
3038 write_sequnlock(&rename_lock);
3039 if (err) {
3040 dput(new);
3041 new = ERR_PTR(err);
3042 }
3043 dput(old_parent);
3044 } else {
3045 __d_move(new, dentry, false);
3046 write_sequnlock(&rename_lock);
3047 }
3048 iput(inode);
3049 return new;
3050 }
3051 }
3052out:
3053 __d_add(dentry, inode);
3054 return NULL;
3055}
3056EXPORT_SYMBOL(d_splice_alias);
3057
3058/*
3059 * Test whether new_dentry is a subdirectory of old_dentry.
3060 *
3061 * Trivially implemented using the dcache structure
3062 */
3063
3064/**
3065 * is_subdir - is new dentry a subdirectory of old_dentry
3066 * @new_dentry: new dentry
3067 * @old_dentry: old dentry
3068 *
3069 * Returns true if new_dentry is a subdirectory of the parent (at any depth).
3070 * Returns false otherwise.
3071 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
3072 */
3073
3074bool is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
3075{
3076 bool subdir;
3077 unsigned seq;
3078
3079 if (new_dentry == old_dentry)
3080 return true;
3081
3082 /* Access d_parent under rcu as d_move() may change it. */
3083 rcu_read_lock();
3084 seq = read_seqbegin(&rename_lock);
3085 subdir = d_ancestor(old_dentry, new_dentry);
3086 /* Try lockless once... */
3087 if (read_seqretry(&rename_lock, seq)) {
3088 /* ...else acquire lock for progress even on deep chains. */
3089 read_seqlock_excl(&rename_lock);
3090 subdir = d_ancestor(old_dentry, new_dentry);
3091 read_sequnlock_excl(&rename_lock);
3092 }
3093 rcu_read_unlock();
3094 return subdir;
3095}
3096EXPORT_SYMBOL(is_subdir);
3097
3098static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
3099{
3100 struct dentry *root = data;
3101 if (dentry != root) {
3102 if (d_unhashed(dentry) || !dentry->d_inode)
3103 return D_WALK_SKIP;
3104
3105 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3106 dentry->d_flags |= DCACHE_GENOCIDE;
3107 dentry->d_lockref.count--;
3108 }
3109 }
3110 return D_WALK_CONTINUE;
3111}
3112
3113void d_genocide(struct dentry *parent)
3114{
3115 d_walk(parent, parent, d_genocide_kill);
3116}
3117
3118EXPORT_SYMBOL(d_genocide);
3119
3120void d_tmpfile(struct dentry *dentry, struct inode *inode)
3121{
3122 inode_dec_link_count(inode);
3123 BUG_ON(dentry->d_name.name != dentry->d_iname ||
3124 !hlist_unhashed(&dentry->d_u.d_alias) ||
3125 !d_unlinked(dentry));
3126 spin_lock(&dentry->d_parent->d_lock);
3127 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3128 dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3129 (unsigned long long)inode->i_ino);
3130 spin_unlock(&dentry->d_lock);
3131 spin_unlock(&dentry->d_parent->d_lock);
3132 d_instantiate(dentry, inode);
3133}
3134EXPORT_SYMBOL(d_tmpfile);
3135
3136static __initdata unsigned long dhash_entries;
3137static int __init set_dhash_entries(char *str)
3138{
3139 if (!str)
3140 return 0;
3141 dhash_entries = simple_strtoul(str, &str, 0);
3142 return 1;
3143}
3144__setup("dhash_entries=", set_dhash_entries);
3145
3146static void __init dcache_init_early(void)
3147{
3148 /* If hashes are distributed across NUMA nodes, defer
3149 * hash allocation until vmalloc space is available.
3150 */
3151 if (hashdist)
3152 return;
3153
3154 dentry_hashtable =
3155 alloc_large_system_hash("Dentry cache",
3156 sizeof(struct hlist_bl_head),
3157 dhash_entries,
3158 13,
3159 HASH_EARLY | HASH_ZERO,
3160 &d_hash_shift,
3161 NULL,
3162 0,
3163 0);
3164 d_hash_shift = 32 - d_hash_shift;
3165}
3166
3167static void __init dcache_init(void)
3168{
3169 /*
3170 * A constructor could be added for stable state like the lists,
3171 * but it is probably not worth it because of the cache nature
3172 * of the dcache.
3173 */
3174 dentry_cache = KMEM_CACHE_USERCOPY(dentry,
3175 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD|SLAB_ACCOUNT,
3176 d_iname);
3177
3178 /* Hash may have been set up in dcache_init_early */
3179 if (!hashdist)
3180 return;
3181
3182 dentry_hashtable =
3183 alloc_large_system_hash("Dentry cache",
3184 sizeof(struct hlist_bl_head),
3185 dhash_entries,
3186 13,
3187 HASH_ZERO,
3188 &d_hash_shift,
3189 NULL,
3190 0,
3191 0);
3192 d_hash_shift = 32 - d_hash_shift;
3193}
3194
3195/* SLAB cache for __getname() consumers */
3196struct kmem_cache *names_cachep __read_mostly;
3197EXPORT_SYMBOL(names_cachep);
3198
3199void __init vfs_caches_init_early(void)
3200{
3201 int i;
3202
3203 for (i = 0; i < ARRAY_SIZE(in_lookup_hashtable); i++)
3204 INIT_HLIST_BL_HEAD(&in_lookup_hashtable[i]);
3205
3206 dcache_init_early();
3207 inode_init_early();
3208}
3209
3210void __init vfs_caches_init(void)
3211{
3212 names_cachep = kmem_cache_create_usercopy("names_cache", PATH_MAX, 0,
3213 SLAB_HWCACHE_ALIGN|SLAB_PANIC, 0, PATH_MAX, NULL);
3214
3215 dcache_init();
3216 inode_init();
3217 files_init();
3218 files_maxfiles_init();
3219 mnt_init();
3220 bdev_cache_init();
3221 chrdev_init();
3222}