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