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