]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blame - fs/dcache.c
ocfs2: get rid of impossible checks
[mirror_ubuntu-bionic-kernel.git] / fs / dcache.c
CommitLineData
1da177e4
LT
1/*
2 * fs/dcache.c
3 *
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
7 */
8
9/*
10 * Notes on the allocation strategy:
11 *
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
15 */
16
1da177e4
LT
17#include <linux/syscalls.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
7a91bf7f 21#include <linux/fsnotify.h>
1da177e4
LT
22#include <linux/slab.h>
23#include <linux/init.h>
1da177e4
LT
24#include <linux/hash.h>
25#include <linux/cache.h>
630d9c47 26#include <linux/export.h>
1da177e4
LT
27#include <linux/mount.h>
28#include <linux/file.h>
29#include <asm/uaccess.h>
30#include <linux/security.h>
31#include <linux/seqlock.h>
32#include <linux/swap.h>
33#include <linux/bootmem.h>
5ad4e53b 34#include <linux/fs_struct.h>
613afbf8 35#include <linux/hardirq.h>
ceb5bdc2
NP
36#include <linux/bit_spinlock.h>
37#include <linux/rculist_bl.h>
268bb0ce 38#include <linux/prefetch.h>
dd179946 39#include <linux/ratelimit.h>
f6041567 40#include <linux/list_lru.h>
07f3f05c 41#include "internal.h"
b2dba1af 42#include "mount.h"
1da177e4 43
789680d1
NP
44/*
45 * Usage:
873feea0
NP
46 * dcache->d_inode->i_lock protects:
47 * - i_dentry, d_alias, d_inode of aliases
ceb5bdc2
NP
48 * dcache_hash_bucket lock protects:
49 * - the dcache hash table
50 * s_anon bl list spinlock protects:
51 * - the s_anon list (see __d_drop)
19156840 52 * dentry->d_sb->s_dentry_lru_lock protects:
23044507
NP
53 * - the dcache lru lists and counters
54 * d_lock protects:
55 * - d_flags
56 * - d_name
57 * - d_lru
b7ab39f6 58 * - d_count
da502956 59 * - d_unhashed()
2fd6b7f5
NP
60 * - d_parent and d_subdirs
61 * - childrens' d_child and d_parent
b23fb0a6 62 * - d_alias, d_inode
789680d1
NP
63 *
64 * Ordering:
873feea0 65 * dentry->d_inode->i_lock
b5c84bf6 66 * dentry->d_lock
19156840 67 * dentry->d_sb->s_dentry_lru_lock
ceb5bdc2
NP
68 * dcache_hash_bucket lock
69 * s_anon lock
789680d1 70 *
da502956
NP
71 * If there is an ancestor relationship:
72 * dentry->d_parent->...->d_parent->d_lock
73 * ...
74 * dentry->d_parent->d_lock
75 * dentry->d_lock
76 *
77 * If no ancestor relationship:
789680d1
NP
78 * if (dentry1 < dentry2)
79 * dentry1->d_lock
80 * dentry2->d_lock
81 */
fa3536cc 82int sysctl_vfs_cache_pressure __read_mostly = 100;
1da177e4
LT
83EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
84
74c3cbe3 85__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
1da177e4 86
949854d0 87EXPORT_SYMBOL(rename_lock);
1da177e4 88
e18b890b 89static struct kmem_cache *dentry_cache __read_mostly;
1da177e4 90
232d2d60
WL
91/**
92 * read_seqbegin_or_lock - begin a sequence number check or locking block
18129977
WL
93 * @lock: sequence lock
94 * @seq : sequence number to be checked
232d2d60
WL
95 *
96 * First try it once optimistically without taking the lock. If that fails,
97 * take the lock. The sequence number is also used as a marker for deciding
98 * whether to be a reader (even) or writer (odd).
99 * N.B. seq must be initialized to an even number to begin with.
100 */
101static inline void read_seqbegin_or_lock(seqlock_t *lock, int *seq)
102{
48f5ec21 103 if (!(*seq & 1)) /* Even */
232d2d60 104 *seq = read_seqbegin(lock);
48f5ec21 105 else /* Odd */
18129977 106 read_seqlock_excl(lock);
232d2d60
WL
107}
108
48f5ec21 109static inline int need_seqretry(seqlock_t *lock, int seq)
232d2d60 110{
48f5ec21
AV
111 return !(seq & 1) && read_seqretry(lock, seq);
112}
113
114static inline void done_seqretry(seqlock_t *lock, int seq)
115{
116 if (seq & 1)
18129977 117 read_sequnlock_excl(lock);
232d2d60
WL
118}
119
1da177e4
LT
120/*
121 * This is the single most critical data structure when it comes
122 * to the dcache: the hashtable for lookups. Somebody should try
123 * to make this good - I've just made it work.
124 *
125 * This hash-function tries to avoid losing too many bits of hash
126 * information, yet avoid using a prime hash-size or similar.
127 */
128#define D_HASHBITS d_hash_shift
129#define D_HASHMASK d_hash_mask
130
fa3536cc
ED
131static unsigned int d_hash_mask __read_mostly;
132static unsigned int d_hash_shift __read_mostly;
ceb5bdc2 133
b07ad996 134static struct hlist_bl_head *dentry_hashtable __read_mostly;
ceb5bdc2 135
8966be90 136static inline struct hlist_bl_head *d_hash(const struct dentry *parent,
6d7d1a0d 137 unsigned int hash)
ceb5bdc2 138{
6d7d1a0d
LT
139 hash += (unsigned long) parent / L1_CACHE_BYTES;
140 hash = hash + (hash >> D_HASHBITS);
ceb5bdc2
NP
141 return dentry_hashtable + (hash & D_HASHMASK);
142}
143
1da177e4
LT
144/* Statistics gathering. */
145struct dentry_stat_t dentry_stat = {
146 .age_limit = 45,
147};
148
3942c07c 149static DEFINE_PER_CPU(long, nr_dentry);
62d36c77 150static DEFINE_PER_CPU(long, nr_dentry_unused);
312d3ca8
CH
151
152#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
62d36c77
DC
153
154/*
155 * Here we resort to our own counters instead of using generic per-cpu counters
156 * for consistency with what the vfs inode code does. We are expected to harvest
157 * better code and performance by having our own specialized counters.
158 *
159 * Please note that the loop is done over all possible CPUs, not over all online
160 * CPUs. The reason for this is that we don't want to play games with CPUs going
161 * on and off. If one of them goes off, we will just keep their counters.
162 *
163 * glommer: See cffbc8a for details, and if you ever intend to change this,
164 * please update all vfs counters to match.
165 */
3942c07c 166static long get_nr_dentry(void)
3e880fb5
NP
167{
168 int i;
3942c07c 169 long sum = 0;
3e880fb5
NP
170 for_each_possible_cpu(i)
171 sum += per_cpu(nr_dentry, i);
172 return sum < 0 ? 0 : sum;
173}
174
62d36c77
DC
175static long get_nr_dentry_unused(void)
176{
177 int i;
178 long sum = 0;
179 for_each_possible_cpu(i)
180 sum += per_cpu(nr_dentry_unused, i);
181 return sum < 0 ? 0 : sum;
182}
183
312d3ca8
CH
184int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
185 size_t *lenp, loff_t *ppos)
186{
3e880fb5 187 dentry_stat.nr_dentry = get_nr_dentry();
62d36c77 188 dentry_stat.nr_unused = get_nr_dentry_unused();
3942c07c 189 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
312d3ca8
CH
190}
191#endif
192
5483f18e
LT
193/*
194 * Compare 2 name strings, return 0 if they match, otherwise non-zero.
195 * The strings are both count bytes long, and count is non-zero.
196 */
e419b4cc
LT
197#ifdef CONFIG_DCACHE_WORD_ACCESS
198
199#include <asm/word-at-a-time.h>
200/*
201 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
202 * aligned allocation for this particular component. We don't
203 * strictly need the load_unaligned_zeropad() safety, but it
204 * doesn't hurt either.
205 *
206 * In contrast, 'ct' and 'tcount' can be from a pathname, and do
207 * need the careful unaligned handling.
208 */
94753db5 209static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
5483f18e 210{
bfcfaa77 211 unsigned long a,b,mask;
bfcfaa77
LT
212
213 for (;;) {
12f8ad4b 214 a = *(unsigned long *)cs;
e419b4cc 215 b = load_unaligned_zeropad(ct);
bfcfaa77
LT
216 if (tcount < sizeof(unsigned long))
217 break;
218 if (unlikely(a != b))
219 return 1;
220 cs += sizeof(unsigned long);
221 ct += sizeof(unsigned long);
222 tcount -= sizeof(unsigned long);
223 if (!tcount)
224 return 0;
225 }
226 mask = ~(~0ul << tcount*8);
227 return unlikely(!!((a ^ b) & mask));
e419b4cc
LT
228}
229
bfcfaa77 230#else
e419b4cc 231
94753db5 232static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
e419b4cc 233{
5483f18e
LT
234 do {
235 if (*cs != *ct)
236 return 1;
237 cs++;
238 ct++;
239 tcount--;
240 } while (tcount);
241 return 0;
242}
243
e419b4cc
LT
244#endif
245
94753db5
LT
246static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
247{
6326c71f 248 const unsigned char *cs;
94753db5
LT
249 /*
250 * Be careful about RCU walk racing with rename:
251 * use ACCESS_ONCE to fetch the name pointer.
252 *
253 * NOTE! Even if a rename will mean that the length
254 * was not loaded atomically, we don't care. The
255 * RCU walk will check the sequence count eventually,
256 * and catch it. And we won't overrun the buffer,
257 * because we're reading the name pointer atomically,
258 * and a dentry name is guaranteed to be properly
259 * terminated with a NUL byte.
260 *
261 * End result: even if 'len' is wrong, we'll exit
262 * early because the data cannot match (there can
263 * be no NUL in the ct/tcount data)
264 */
6326c71f
LT
265 cs = ACCESS_ONCE(dentry->d_name.name);
266 smp_read_barrier_depends();
267 return dentry_string_cmp(cs, ct, tcount);
94753db5
LT
268}
269
9c82ab9c 270static void __d_free(struct rcu_head *head)
1da177e4 271{
9c82ab9c
CH
272 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
273
b3d9b7a3 274 WARN_ON(!hlist_unhashed(&dentry->d_alias));
1da177e4
LT
275 if (dname_external(dentry))
276 kfree(dentry->d_name.name);
277 kmem_cache_free(dentry_cache, dentry);
278}
279
280/*
b5c84bf6 281 * no locks, please.
1da177e4
LT
282 */
283static void d_free(struct dentry *dentry)
284{
0d98439e 285 BUG_ON((int)dentry->d_lockref.count > 0);
3e880fb5 286 this_cpu_dec(nr_dentry);
1da177e4
LT
287 if (dentry->d_op && dentry->d_op->d_release)
288 dentry->d_op->d_release(dentry);
312d3ca8 289
dea3667b
LT
290 /* if dentry was never visible to RCU, immediate free is OK */
291 if (!(dentry->d_flags & DCACHE_RCUACCESS))
9c82ab9c 292 __d_free(&dentry->d_u.d_rcu);
b3423415 293 else
9c82ab9c 294 call_rcu(&dentry->d_u.d_rcu, __d_free);
1da177e4
LT
295}
296
31e6b01f
NP
297/**
298 * dentry_rcuwalk_barrier - invalidate in-progress rcu-walk lookups
ff5fdb61 299 * @dentry: the target dentry
31e6b01f
NP
300 * After this call, in-progress rcu-walk path lookup will fail. This
301 * should be called after unhashing, and after changing d_inode (if
302 * the dentry has not already been unhashed).
303 */
304static inline void dentry_rcuwalk_barrier(struct dentry *dentry)
305{
306 assert_spin_locked(&dentry->d_lock);
307 /* Go through a barrier */
308 write_seqcount_barrier(&dentry->d_seq);
309}
310
1da177e4
LT
311/*
312 * Release the dentry's inode, using the filesystem
31e6b01f
NP
313 * d_iput() operation if defined. Dentry has no refcount
314 * and is unhashed.
1da177e4 315 */
858119e1 316static void dentry_iput(struct dentry * dentry)
31f3e0b3 317 __releases(dentry->d_lock)
873feea0 318 __releases(dentry->d_inode->i_lock)
1da177e4
LT
319{
320 struct inode *inode = dentry->d_inode;
321 if (inode) {
322 dentry->d_inode = NULL;
b3d9b7a3 323 hlist_del_init(&dentry->d_alias);
1da177e4 324 spin_unlock(&dentry->d_lock);
873feea0 325 spin_unlock(&inode->i_lock);
f805fbda
LT
326 if (!inode->i_nlink)
327 fsnotify_inoderemove(inode);
1da177e4
LT
328 if (dentry->d_op && dentry->d_op->d_iput)
329 dentry->d_op->d_iput(dentry, inode);
330 else
331 iput(inode);
332 } else {
333 spin_unlock(&dentry->d_lock);
1da177e4
LT
334 }
335}
336
31e6b01f
NP
337/*
338 * Release the dentry's inode, using the filesystem
339 * d_iput() operation if defined. dentry remains in-use.
340 */
341static void dentry_unlink_inode(struct dentry * dentry)
342 __releases(dentry->d_lock)
873feea0 343 __releases(dentry->d_inode->i_lock)
31e6b01f
NP
344{
345 struct inode *inode = dentry->d_inode;
b18825a7 346 __d_clear_type(dentry);
31e6b01f 347 dentry->d_inode = NULL;
b3d9b7a3 348 hlist_del_init(&dentry->d_alias);
31e6b01f
NP
349 dentry_rcuwalk_barrier(dentry);
350 spin_unlock(&dentry->d_lock);
873feea0 351 spin_unlock(&inode->i_lock);
31e6b01f
NP
352 if (!inode->i_nlink)
353 fsnotify_inoderemove(inode);
354 if (dentry->d_op && dentry->d_op->d_iput)
355 dentry->d_op->d_iput(dentry, inode);
356 else
357 iput(inode);
358}
359
89dc77bc
LT
360/*
361 * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry
362 * is in use - which includes both the "real" per-superblock
363 * LRU list _and_ the DCACHE_SHRINK_LIST use.
364 *
365 * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
366 * on the shrink list (ie not on the superblock LRU list).
367 *
368 * The per-cpu "nr_dentry_unused" counters are updated with
369 * the DCACHE_LRU_LIST bit.
370 *
371 * These helper functions make sure we always follow the
372 * rules. d_lock must be held by the caller.
373 */
374#define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
375static void d_lru_add(struct dentry *dentry)
376{
377 D_FLAG_VERIFY(dentry, 0);
378 dentry->d_flags |= DCACHE_LRU_LIST;
379 this_cpu_inc(nr_dentry_unused);
380 WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
381}
382
383static void d_lru_del(struct dentry *dentry)
384{
385 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
386 dentry->d_flags &= ~DCACHE_LRU_LIST;
387 this_cpu_dec(nr_dentry_unused);
388 WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
389}
390
391static void d_shrink_del(struct dentry *dentry)
392{
393 D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
394 list_del_init(&dentry->d_lru);
395 dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
396 this_cpu_dec(nr_dentry_unused);
397}
398
399static void d_shrink_add(struct dentry *dentry, struct list_head *list)
400{
401 D_FLAG_VERIFY(dentry, 0);
402 list_add(&dentry->d_lru, list);
403 dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
404 this_cpu_inc(nr_dentry_unused);
405}
406
407/*
408 * These can only be called under the global LRU lock, ie during the
409 * callback for freeing the LRU list. "isolate" removes it from the
410 * LRU lists entirely, while shrink_move moves it to the indicated
411 * private list.
412 */
413static void d_lru_isolate(struct dentry *dentry)
414{
415 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
416 dentry->d_flags &= ~DCACHE_LRU_LIST;
417 this_cpu_dec(nr_dentry_unused);
418 list_del_init(&dentry->d_lru);
419}
420
421static void d_lru_shrink_move(struct dentry *dentry, struct list_head *list)
422{
423 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
424 dentry->d_flags |= DCACHE_SHRINK_LIST;
425 list_move_tail(&dentry->d_lru, list);
426}
427
da3bbdd4 428/*
f6041567 429 * dentry_lru_(add|del)_list) must be called with d_lock held.
da3bbdd4
KM
430 */
431static void dentry_lru_add(struct dentry *dentry)
432{
89dc77bc
LT
433 if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
434 d_lru_add(dentry);
da3bbdd4
KM
435}
436
f0023bc6
SW
437/*
438 * Remove a dentry with references from the LRU.
dd1f6b2e
DC
439 *
440 * If we are on the shrink list, then we can get to try_prune_one_dentry() and
441 * lose our last reference through the parent walk. In this case, we need to
442 * remove ourselves from the shrink list, not the LRU.
f0023bc6 443 */
da3bbdd4
KM
444static void dentry_lru_del(struct dentry *dentry)
445{
89dc77bc
LT
446 if (dentry->d_flags & DCACHE_LRU_LIST) {
447 if (dentry->d_flags & DCACHE_SHRINK_LIST)
448 return d_shrink_del(dentry);
449 d_lru_del(dentry);
da3bbdd4 450 }
da3bbdd4
KM
451}
452
d52b9086
MS
453/**
454 * d_kill - kill dentry and return parent
455 * @dentry: dentry to kill
ff5fdb61 456 * @parent: parent dentry
d52b9086 457 *
31f3e0b3 458 * The dentry must already be unhashed and removed from the LRU.
d52b9086
MS
459 *
460 * If this is the root of the dentry tree, return NULL.
23044507 461 *
b5c84bf6
NP
462 * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by
463 * d_kill.
d52b9086 464 */
2fd6b7f5 465static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
31f3e0b3 466 __releases(dentry->d_lock)
2fd6b7f5 467 __releases(parent->d_lock)
873feea0 468 __releases(dentry->d_inode->i_lock)
d52b9086 469{
d52b9086 470 list_del(&dentry->d_u.d_child);
c83ce989
TM
471 /*
472 * Inform try_to_ascend() that we are no longer attached to the
473 * dentry tree
474 */
b161dfa6 475 dentry->d_flags |= DCACHE_DENTRY_KILLED;
2fd6b7f5
NP
476 if (parent)
477 spin_unlock(&parent->d_lock);
d52b9086 478 dentry_iput(dentry);
b7ab39f6
NP
479 /*
480 * dentry_iput drops the locks, at which point nobody (except
481 * transient RCU lookups) can reach this dentry.
482 */
d52b9086 483 d_free(dentry);
871c0067 484 return parent;
d52b9086
MS
485}
486
789680d1
NP
487/**
488 * d_drop - drop a dentry
489 * @dentry: dentry to drop
490 *
491 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
492 * be found through a VFS lookup any more. Note that this is different from
493 * deleting the dentry - d_delete will try to mark the dentry negative if
494 * possible, giving a successful _negative_ lookup, while d_drop will
495 * just make the cache lookup fail.
496 *
497 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
498 * reason (NFS timeouts or autofs deletes).
499 *
500 * __d_drop requires dentry->d_lock.
501 */
502void __d_drop(struct dentry *dentry)
503{
dea3667b 504 if (!d_unhashed(dentry)) {
b61625d2
AV
505 struct hlist_bl_head *b;
506 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
507 b = &dentry->d_sb->s_anon;
508 else
509 b = d_hash(dentry->d_parent, dentry->d_name.hash);
510
511 hlist_bl_lock(b);
512 __hlist_bl_del(&dentry->d_hash);
513 dentry->d_hash.pprev = NULL;
514 hlist_bl_unlock(b);
dea3667b 515 dentry_rcuwalk_barrier(dentry);
789680d1
NP
516 }
517}
518EXPORT_SYMBOL(__d_drop);
519
520void d_drop(struct dentry *dentry)
521{
789680d1
NP
522 spin_lock(&dentry->d_lock);
523 __d_drop(dentry);
524 spin_unlock(&dentry->d_lock);
789680d1
NP
525}
526EXPORT_SYMBOL(d_drop);
527
77812a1e
NP
528/*
529 * Finish off a dentry we've decided to kill.
530 * dentry->d_lock must be held, returns with it unlocked.
531 * If ref is non-zero, then decrement the refcount too.
532 * Returns dentry requiring refcount drop, or NULL if we're done.
533 */
dd1f6b2e
DC
534static inline struct dentry *
535dentry_kill(struct dentry *dentry, int unlock_on_failure)
77812a1e
NP
536 __releases(dentry->d_lock)
537{
873feea0 538 struct inode *inode;
77812a1e
NP
539 struct dentry *parent;
540
873feea0
NP
541 inode = dentry->d_inode;
542 if (inode && !spin_trylock(&inode->i_lock)) {
77812a1e 543relock:
dd1f6b2e
DC
544 if (unlock_on_failure) {
545 spin_unlock(&dentry->d_lock);
546 cpu_relax();
547 }
77812a1e
NP
548 return dentry; /* try again with same dentry */
549 }
550 if (IS_ROOT(dentry))
551 parent = NULL;
552 else
553 parent = dentry->d_parent;
554 if (parent && !spin_trylock(&parent->d_lock)) {
873feea0
NP
555 if (inode)
556 spin_unlock(&inode->i_lock);
77812a1e
NP
557 goto relock;
558 }
31e6b01f 559
0d98439e
LT
560 /*
561 * The dentry is now unrecoverably dead to the world.
562 */
563 lockref_mark_dead(&dentry->d_lockref);
564
f0023bc6 565 /*
f0023bc6
SW
566 * inform the fs via d_prune that this dentry is about to be
567 * unhashed and destroyed.
568 */
590fb51f 569 if ((dentry->d_flags & DCACHE_OP_PRUNE) && !d_unhashed(dentry))
61572bb1
YZ
570 dentry->d_op->d_prune(dentry);
571
572 dentry_lru_del(dentry);
77812a1e
NP
573 /* if it was on the hash then remove it */
574 __d_drop(dentry);
575 return d_kill(dentry, parent);
576}
577
1da177e4
LT
578/*
579 * This is dput
580 *
581 * This is complicated by the fact that we do not want to put
582 * dentries that are no longer on any hash chain on the unused
583 * list: we'd much rather just get rid of them immediately.
584 *
585 * However, that implies that we have to traverse the dentry
586 * tree upwards to the parents which might _also_ now be
587 * scheduled for deletion (it may have been only waiting for
588 * its last child to go away).
589 *
590 * This tail recursion is done by hand as we don't want to depend
591 * on the compiler to always get this right (gcc generally doesn't).
592 * Real recursion would eat up our stack space.
593 */
594
595/*
596 * dput - release a dentry
597 * @dentry: dentry to release
598 *
599 * Release a dentry. This will drop the usage count and if appropriate
600 * call the dentry unlink method as well as removing it from the queues and
601 * releasing its resources. If the parent dentries were scheduled for release
602 * they too may now get deleted.
1da177e4 603 */
1da177e4
LT
604void dput(struct dentry *dentry)
605{
8aab6a27 606 if (unlikely(!dentry))
1da177e4
LT
607 return;
608
609repeat:
98474236 610 if (lockref_put_or_lock(&dentry->d_lockref))
1da177e4 611 return;
1da177e4 612
8aab6a27
LT
613 /* Unreachable? Get rid of it */
614 if (unlikely(d_unhashed(dentry)))
615 goto kill_it;
616
617 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
1da177e4 618 if (dentry->d_op->d_delete(dentry))
61f3dee4 619 goto kill_it;
1da177e4 620 }
265ac902 621
39e3c955 622 dentry->d_flags |= DCACHE_REFERENCED;
a4633357 623 dentry_lru_add(dentry);
265ac902 624
98474236 625 dentry->d_lockref.count--;
61f3dee4 626 spin_unlock(&dentry->d_lock);
1da177e4
LT
627 return;
628
d52b9086 629kill_it:
dd1f6b2e 630 dentry = dentry_kill(dentry, 1);
d52b9086
MS
631 if (dentry)
632 goto repeat;
1da177e4 633}
ec4f8605 634EXPORT_SYMBOL(dput);
1da177e4
LT
635
636/**
637 * d_invalidate - invalidate a dentry
638 * @dentry: dentry to invalidate
639 *
640 * Try to invalidate the dentry if it turns out to be
641 * possible. If there are other dentries that can be
642 * reached through this one we can't delete it and we
643 * return -EBUSY. On success we return 0.
644 *
645 * no dcache lock.
646 */
647
648int d_invalidate(struct dentry * dentry)
649{
650 /*
651 * If it's already been dropped, return OK.
652 */
da502956 653 spin_lock(&dentry->d_lock);
1da177e4 654 if (d_unhashed(dentry)) {
da502956 655 spin_unlock(&dentry->d_lock);
1da177e4
LT
656 return 0;
657 }
658 /*
659 * Check whether to do a partial shrink_dcache
660 * to get rid of unused child entries.
661 */
662 if (!list_empty(&dentry->d_subdirs)) {
da502956 663 spin_unlock(&dentry->d_lock);
1da177e4 664 shrink_dcache_parent(dentry);
da502956 665 spin_lock(&dentry->d_lock);
1da177e4
LT
666 }
667
668 /*
669 * Somebody else still using it?
670 *
671 * If it's a directory, we can't drop it
672 * for fear of somebody re-populating it
673 * with children (even though dropping it
674 * would make it unreachable from the root,
675 * we might still populate it if it was a
676 * working directory or similar).
50e69630
AV
677 * We also need to leave mountpoints alone,
678 * directory or not.
1da177e4 679 */
98474236 680 if (dentry->d_lockref.count > 1 && dentry->d_inode) {
50e69630 681 if (S_ISDIR(dentry->d_inode->i_mode) || d_mountpoint(dentry)) {
1da177e4 682 spin_unlock(&dentry->d_lock);
1da177e4
LT
683 return -EBUSY;
684 }
685 }
686
687 __d_drop(dentry);
688 spin_unlock(&dentry->d_lock);
1da177e4
LT
689 return 0;
690}
ec4f8605 691EXPORT_SYMBOL(d_invalidate);
1da177e4 692
b5c84bf6 693/* This must be called with d_lock held */
dc0474be 694static inline void __dget_dlock(struct dentry *dentry)
23044507 695{
98474236 696 dentry->d_lockref.count++;
23044507
NP
697}
698
dc0474be 699static inline void __dget(struct dentry *dentry)
1da177e4 700{
98474236 701 lockref_get(&dentry->d_lockref);
1da177e4
LT
702}
703
b7ab39f6
NP
704struct dentry *dget_parent(struct dentry *dentry)
705{
df3d0bbc 706 int gotref;
b7ab39f6
NP
707 struct dentry *ret;
708
df3d0bbc
WL
709 /*
710 * Do optimistic parent lookup without any
711 * locking.
712 */
713 rcu_read_lock();
714 ret = ACCESS_ONCE(dentry->d_parent);
715 gotref = lockref_get_not_zero(&ret->d_lockref);
716 rcu_read_unlock();
717 if (likely(gotref)) {
718 if (likely(ret == ACCESS_ONCE(dentry->d_parent)))
719 return ret;
720 dput(ret);
721 }
722
b7ab39f6 723repeat:
a734eb45
NP
724 /*
725 * Don't need rcu_dereference because we re-check it was correct under
726 * the lock.
727 */
728 rcu_read_lock();
b7ab39f6 729 ret = dentry->d_parent;
a734eb45
NP
730 spin_lock(&ret->d_lock);
731 if (unlikely(ret != dentry->d_parent)) {
732 spin_unlock(&ret->d_lock);
733 rcu_read_unlock();
b7ab39f6
NP
734 goto repeat;
735 }
a734eb45 736 rcu_read_unlock();
98474236
WL
737 BUG_ON(!ret->d_lockref.count);
738 ret->d_lockref.count++;
b7ab39f6 739 spin_unlock(&ret->d_lock);
b7ab39f6
NP
740 return ret;
741}
742EXPORT_SYMBOL(dget_parent);
743
1da177e4
LT
744/**
745 * d_find_alias - grab a hashed alias of inode
746 * @inode: inode in question
32ba9c3f
LT
747 * @want_discon: flag, used by d_splice_alias, to request
748 * that only a DISCONNECTED alias be returned.
1da177e4
LT
749 *
750 * If inode has a hashed alias, or is a directory and has any alias,
751 * acquire the reference to alias and return it. Otherwise return NULL.
752 * Notice that if inode is a directory there can be only one alias and
753 * it can be unhashed only if it has no children, or if it is the root
754 * of a filesystem.
755 *
21c0d8fd 756 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
32ba9c3f
LT
757 * any other hashed alias over that one unless @want_discon is set,
758 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
1da177e4 759 */
32ba9c3f 760static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
1da177e4 761{
da502956 762 struct dentry *alias, *discon_alias;
1da177e4 763
da502956
NP
764again:
765 discon_alias = NULL;
b67bfe0d 766 hlist_for_each_entry(alias, &inode->i_dentry, d_alias) {
da502956 767 spin_lock(&alias->d_lock);
1da177e4 768 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
21c0d8fd 769 if (IS_ROOT(alias) &&
da502956 770 (alias->d_flags & DCACHE_DISCONNECTED)) {
1da177e4 771 discon_alias = alias;
32ba9c3f 772 } else if (!want_discon) {
dc0474be 773 __dget_dlock(alias);
da502956
NP
774 spin_unlock(&alias->d_lock);
775 return alias;
776 }
777 }
778 spin_unlock(&alias->d_lock);
779 }
780 if (discon_alias) {
781 alias = discon_alias;
782 spin_lock(&alias->d_lock);
783 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
784 if (IS_ROOT(alias) &&
785 (alias->d_flags & DCACHE_DISCONNECTED)) {
dc0474be 786 __dget_dlock(alias);
da502956 787 spin_unlock(&alias->d_lock);
1da177e4
LT
788 return alias;
789 }
790 }
da502956
NP
791 spin_unlock(&alias->d_lock);
792 goto again;
1da177e4 793 }
da502956 794 return NULL;
1da177e4
LT
795}
796
da502956 797struct dentry *d_find_alias(struct inode *inode)
1da177e4 798{
214fda1f
DH
799 struct dentry *de = NULL;
800
b3d9b7a3 801 if (!hlist_empty(&inode->i_dentry)) {
873feea0 802 spin_lock(&inode->i_lock);
32ba9c3f 803 de = __d_find_alias(inode, 0);
873feea0 804 spin_unlock(&inode->i_lock);
214fda1f 805 }
1da177e4
LT
806 return de;
807}
ec4f8605 808EXPORT_SYMBOL(d_find_alias);
1da177e4
LT
809
810/*
811 * Try to kill dentries associated with this inode.
812 * WARNING: you must own a reference to inode.
813 */
814void d_prune_aliases(struct inode *inode)
815{
0cdca3f9 816 struct dentry *dentry;
1da177e4 817restart:
873feea0 818 spin_lock(&inode->i_lock);
b67bfe0d 819 hlist_for_each_entry(dentry, &inode->i_dentry, d_alias) {
1da177e4 820 spin_lock(&dentry->d_lock);
98474236 821 if (!dentry->d_lockref.count) {
590fb51f
YZ
822 /*
823 * inform the fs via d_prune that this dentry
824 * is about to be unhashed and destroyed.
825 */
826 if ((dentry->d_flags & DCACHE_OP_PRUNE) &&
827 !d_unhashed(dentry))
828 dentry->d_op->d_prune(dentry);
829
dc0474be 830 __dget_dlock(dentry);
1da177e4
LT
831 __d_drop(dentry);
832 spin_unlock(&dentry->d_lock);
873feea0 833 spin_unlock(&inode->i_lock);
1da177e4
LT
834 dput(dentry);
835 goto restart;
836 }
837 spin_unlock(&dentry->d_lock);
838 }
873feea0 839 spin_unlock(&inode->i_lock);
1da177e4 840}
ec4f8605 841EXPORT_SYMBOL(d_prune_aliases);
1da177e4
LT
842
843/*
77812a1e
NP
844 * Try to throw away a dentry - free the inode, dput the parent.
845 * Requires dentry->d_lock is held, and dentry->d_count == 0.
846 * Releases dentry->d_lock.
d702ccb3 847 *
77812a1e 848 * This may fail if locks cannot be acquired no problem, just try again.
1da177e4 849 */
dd1f6b2e 850static struct dentry * try_prune_one_dentry(struct dentry *dentry)
31f3e0b3 851 __releases(dentry->d_lock)
1da177e4 852{
77812a1e 853 struct dentry *parent;
d52b9086 854
dd1f6b2e 855 parent = dentry_kill(dentry, 0);
d52b9086 856 /*
77812a1e
NP
857 * If dentry_kill returns NULL, we have nothing more to do.
858 * if it returns the same dentry, trylocks failed. In either
859 * case, just loop again.
860 *
861 * Otherwise, we need to prune ancestors too. This is necessary
862 * to prevent quadratic behavior of shrink_dcache_parent(), but
863 * is also expected to be beneficial in reducing dentry cache
864 * fragmentation.
d52b9086 865 */
77812a1e 866 if (!parent)
dd1f6b2e 867 return NULL;
77812a1e 868 if (parent == dentry)
dd1f6b2e 869 return dentry;
77812a1e
NP
870
871 /* Prune ancestors. */
872 dentry = parent;
d52b9086 873 while (dentry) {
98474236 874 if (lockref_put_or_lock(&dentry->d_lockref))
dd1f6b2e
DC
875 return NULL;
876 dentry = dentry_kill(dentry, 1);
d52b9086 877 }
dd1f6b2e 878 return NULL;
1da177e4
LT
879}
880
3049cfe2 881static void shrink_dentry_list(struct list_head *list)
1da177e4 882{
da3bbdd4 883 struct dentry *dentry;
da3bbdd4 884
ec33679d
NP
885 rcu_read_lock();
886 for (;;) {
ec33679d
NP
887 dentry = list_entry_rcu(list->prev, struct dentry, d_lru);
888 if (&dentry->d_lru == list)
889 break; /* empty */
89dc77bc
LT
890
891 /*
892 * Get the dentry lock, and re-verify that the dentry is
893 * this on the shrinking list. If it is, we know that
894 * DCACHE_SHRINK_LIST and DCACHE_LRU_LIST are set.
895 */
ec33679d
NP
896 spin_lock(&dentry->d_lock);
897 if (dentry != list_entry(list->prev, struct dentry, d_lru)) {
898 spin_unlock(&dentry->d_lock);
23044507
NP
899 continue;
900 }
901
dd1f6b2e
DC
902 /*
903 * The dispose list is isolated and dentries are not accounted
904 * to the LRU here, so we can simply remove it from the list
905 * here regardless of whether it is referenced or not.
906 */
89dc77bc 907 d_shrink_del(dentry);
dd1f6b2e 908
1da177e4
LT
909 /*
910 * We found an inuse dentry which was not removed from
dd1f6b2e 911 * the LRU because of laziness during lookup. Do not free it.
1da177e4 912 */
98474236 913 if (dentry->d_lockref.count) {
da3bbdd4 914 spin_unlock(&dentry->d_lock);
1da177e4
LT
915 continue;
916 }
ec33679d 917 rcu_read_unlock();
77812a1e 918
89dc77bc
LT
919 /*
920 * If 'try_to_prune()' returns a dentry, it will
921 * be the same one we passed in, and d_lock will
922 * have been held the whole time, so it will not
923 * have been added to any other lists. We failed
924 * to get the inode lock.
925 *
926 * We just add it back to the shrink list.
927 */
dd1f6b2e 928 dentry = try_prune_one_dentry(dentry);
77812a1e 929
ec33679d 930 rcu_read_lock();
dd1f6b2e 931 if (dentry) {
89dc77bc 932 d_shrink_add(dentry, list);
dd1f6b2e
DC
933 spin_unlock(&dentry->d_lock);
934 }
da3bbdd4 935 }
ec33679d 936 rcu_read_unlock();
3049cfe2
CH
937}
938
f6041567
DC
939static enum lru_status
940dentry_lru_isolate(struct list_head *item, spinlock_t *lru_lock, void *arg)
941{
942 struct list_head *freeable = arg;
943 struct dentry *dentry = container_of(item, struct dentry, d_lru);
944
945
946 /*
947 * we are inverting the lru lock/dentry->d_lock here,
948 * so use a trylock. If we fail to get the lock, just skip
949 * it
950 */
951 if (!spin_trylock(&dentry->d_lock))
952 return LRU_SKIP;
953
954 /*
955 * Referenced dentries are still in use. If they have active
956 * counts, just remove them from the LRU. Otherwise give them
957 * another pass through the LRU.
958 */
959 if (dentry->d_lockref.count) {
89dc77bc 960 d_lru_isolate(dentry);
f6041567
DC
961 spin_unlock(&dentry->d_lock);
962 return LRU_REMOVED;
963 }
964
965 if (dentry->d_flags & DCACHE_REFERENCED) {
966 dentry->d_flags &= ~DCACHE_REFERENCED;
967 spin_unlock(&dentry->d_lock);
968
969 /*
970 * The list move itself will be made by the common LRU code. At
971 * this point, we've dropped the dentry->d_lock but keep the
972 * lru lock. This is safe to do, since every list movement is
973 * protected by the lru lock even if both locks are held.
974 *
975 * This is guaranteed by the fact that all LRU management
976 * functions are intermediated by the LRU API calls like
977 * list_lru_add and list_lru_del. List movement in this file
978 * only ever occur through this functions or through callbacks
979 * like this one, that are called from the LRU API.
980 *
981 * The only exceptions to this are functions like
982 * shrink_dentry_list, and code that first checks for the
983 * DCACHE_SHRINK_LIST flag. Those are guaranteed to be
984 * operating only with stack provided lists after they are
985 * properly isolated from the main list. It is thus, always a
986 * local access.
987 */
988 return LRU_ROTATE;
989 }
990
89dc77bc 991 d_lru_shrink_move(dentry, freeable);
f6041567
DC
992 spin_unlock(&dentry->d_lock);
993
994 return LRU_REMOVED;
995}
996
3049cfe2 997/**
b48f03b3
DC
998 * prune_dcache_sb - shrink the dcache
999 * @sb: superblock
f6041567 1000 * @nr_to_scan : number of entries to try to free
9b17c623 1001 * @nid: which node to scan for freeable entities
b48f03b3 1002 *
f6041567 1003 * Attempt to shrink the superblock dcache LRU by @nr_to_scan entries. This is
b48f03b3
DC
1004 * done when we need more memory an called from the superblock shrinker
1005 * function.
3049cfe2 1006 *
b48f03b3
DC
1007 * This function may fail to free any resources if all the dentries are in
1008 * use.
3049cfe2 1009 */
9b17c623
DC
1010long prune_dcache_sb(struct super_block *sb, unsigned long nr_to_scan,
1011 int nid)
3049cfe2 1012{
f6041567
DC
1013 LIST_HEAD(dispose);
1014 long freed;
3049cfe2 1015
9b17c623
DC
1016 freed = list_lru_walk_node(&sb->s_dentry_lru, nid, dentry_lru_isolate,
1017 &dispose, &nr_to_scan);
f6041567 1018 shrink_dentry_list(&dispose);
0a234c6d 1019 return freed;
da3bbdd4 1020}
23044507 1021
4e717f5c
GC
1022static enum lru_status dentry_lru_isolate_shrink(struct list_head *item,
1023 spinlock_t *lru_lock, void *arg)
dd1f6b2e 1024{
4e717f5c
GC
1025 struct list_head *freeable = arg;
1026 struct dentry *dentry = container_of(item, struct dentry, d_lru);
dd1f6b2e 1027
4e717f5c
GC
1028 /*
1029 * we are inverting the lru lock/dentry->d_lock here,
1030 * so use a trylock. If we fail to get the lock, just skip
1031 * it
1032 */
1033 if (!spin_trylock(&dentry->d_lock))
1034 return LRU_SKIP;
1035
89dc77bc 1036 d_lru_shrink_move(dentry, freeable);
4e717f5c 1037 spin_unlock(&dentry->d_lock);
ec33679d 1038
4e717f5c 1039 return LRU_REMOVED;
da3bbdd4
KM
1040}
1041
4e717f5c 1042
1da177e4
LT
1043/**
1044 * shrink_dcache_sb - shrink dcache for a superblock
1045 * @sb: superblock
1046 *
3049cfe2
CH
1047 * Shrink the dcache for the specified super block. This is used to free
1048 * the dcache before unmounting a file system.
1da177e4 1049 */
3049cfe2 1050void shrink_dcache_sb(struct super_block *sb)
1da177e4 1051{
4e717f5c
GC
1052 long freed;
1053
1054 do {
1055 LIST_HEAD(dispose);
1056
1057 freed = list_lru_walk(&sb->s_dentry_lru,
1058 dentry_lru_isolate_shrink, &dispose, UINT_MAX);
3049cfe2 1059
4e717f5c
GC
1060 this_cpu_sub(nr_dentry_unused, freed);
1061 shrink_dentry_list(&dispose);
1062 } while (freed > 0);
1da177e4 1063}
ec4f8605 1064EXPORT_SYMBOL(shrink_dcache_sb);
1da177e4 1065
c826cb7d
LT
1066/*
1067 * This tries to ascend one level of parenthood, but
1068 * we can race with renaming, so we need to re-check
1069 * the parenthood after dropping the lock and check
1070 * that the sequence number still matches.
1071 */
48f5ec21 1072static struct dentry *try_to_ascend(struct dentry *old, unsigned seq)
c826cb7d
LT
1073{
1074 struct dentry *new = old->d_parent;
1075
1076 rcu_read_lock();
1077 spin_unlock(&old->d_lock);
1078 spin_lock(&new->d_lock);
1079
1080 /*
1081 * might go back up the wrong parent if we have had a rename
1082 * or deletion
1083 */
1084 if (new != old->d_parent ||
b161dfa6 1085 (old->d_flags & DCACHE_DENTRY_KILLED) ||
48f5ec21 1086 need_seqretry(&rename_lock, seq)) {
c826cb7d
LT
1087 spin_unlock(&new->d_lock);
1088 new = NULL;
1089 }
1090 rcu_read_unlock();
1091 return new;
1092}
1093
db14fc3a
MS
1094/**
1095 * enum d_walk_ret - action to talke during tree walk
1096 * @D_WALK_CONTINUE: contrinue walk
1097 * @D_WALK_QUIT: quit walk
1098 * @D_WALK_NORETRY: quit when retry is needed
1099 * @D_WALK_SKIP: skip this dentry and its children
1100 */
1101enum d_walk_ret {
1102 D_WALK_CONTINUE,
1103 D_WALK_QUIT,
1104 D_WALK_NORETRY,
1105 D_WALK_SKIP,
1106};
c826cb7d 1107
1da177e4 1108/**
db14fc3a
MS
1109 * d_walk - walk the dentry tree
1110 * @parent: start of walk
1111 * @data: data passed to @enter() and @finish()
1112 * @enter: callback when first entering the dentry
1113 * @finish: callback when successfully finished the walk
1da177e4 1114 *
db14fc3a 1115 * The @enter() and @finish() callbacks are called with d_lock held.
1da177e4 1116 */
db14fc3a
MS
1117static void d_walk(struct dentry *parent, void *data,
1118 enum d_walk_ret (*enter)(void *, struct dentry *),
1119 void (*finish)(void *))
1da177e4 1120{
949854d0 1121 struct dentry *this_parent;
1da177e4 1122 struct list_head *next;
48f5ec21 1123 unsigned seq = 0;
db14fc3a
MS
1124 enum d_walk_ret ret;
1125 bool retry = true;
949854d0 1126
58db63d0 1127again:
48f5ec21 1128 read_seqbegin_or_lock(&rename_lock, &seq);
58db63d0 1129 this_parent = parent;
2fd6b7f5 1130 spin_lock(&this_parent->d_lock);
db14fc3a
MS
1131
1132 ret = enter(data, this_parent);
1133 switch (ret) {
1134 case D_WALK_CONTINUE:
1135 break;
1136 case D_WALK_QUIT:
1137 case D_WALK_SKIP:
1138 goto out_unlock;
1139 case D_WALK_NORETRY:
1140 retry = false;
1141 break;
1142 }
1da177e4
LT
1143repeat:
1144 next = this_parent->d_subdirs.next;
1145resume:
1146 while (next != &this_parent->d_subdirs) {
1147 struct list_head *tmp = next;
5160ee6f 1148 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1da177e4 1149 next = tmp->next;
2fd6b7f5
NP
1150
1151 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
db14fc3a
MS
1152
1153 ret = enter(data, dentry);
1154 switch (ret) {
1155 case D_WALK_CONTINUE:
1156 break;
1157 case D_WALK_QUIT:
2fd6b7f5 1158 spin_unlock(&dentry->d_lock);
db14fc3a
MS
1159 goto out_unlock;
1160 case D_WALK_NORETRY:
1161 retry = false;
1162 break;
1163 case D_WALK_SKIP:
1164 spin_unlock(&dentry->d_lock);
1165 continue;
2fd6b7f5 1166 }
db14fc3a 1167
1da177e4 1168 if (!list_empty(&dentry->d_subdirs)) {
2fd6b7f5
NP
1169 spin_unlock(&this_parent->d_lock);
1170 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
1da177e4 1171 this_parent = dentry;
2fd6b7f5 1172 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1da177e4
LT
1173 goto repeat;
1174 }
2fd6b7f5 1175 spin_unlock(&dentry->d_lock);
1da177e4
LT
1176 }
1177 /*
1178 * All done at this level ... ascend and resume the search.
1179 */
1180 if (this_parent != parent) {
c826cb7d 1181 struct dentry *child = this_parent;
48f5ec21 1182 this_parent = try_to_ascend(this_parent, seq);
c826cb7d 1183 if (!this_parent)
949854d0 1184 goto rename_retry;
949854d0 1185 next = child->d_u.d_child.next;
1da177e4
LT
1186 goto resume;
1187 }
48f5ec21 1188 if (need_seqretry(&rename_lock, seq)) {
db14fc3a 1189 spin_unlock(&this_parent->d_lock);
949854d0 1190 goto rename_retry;
db14fc3a
MS
1191 }
1192 if (finish)
1193 finish(data);
1194
1195out_unlock:
1196 spin_unlock(&this_parent->d_lock);
48f5ec21 1197 done_seqretry(&rename_lock, seq);
db14fc3a 1198 return;
58db63d0
NP
1199
1200rename_retry:
db14fc3a
MS
1201 if (!retry)
1202 return;
48f5ec21 1203 seq = 1;
58db63d0 1204 goto again;
1da177e4 1205}
db14fc3a
MS
1206
1207/*
1208 * Search for at least 1 mount point in the dentry's subdirs.
1209 * We descend to the next level whenever the d_subdirs
1210 * list is non-empty and continue searching.
1211 */
1212
1213/**
1214 * have_submounts - check for mounts over a dentry
1215 * @parent: dentry to check.
1216 *
1217 * Return true if the parent or its subdirectories contain
1218 * a mount point
1219 */
1220
1221static enum d_walk_ret check_mount(void *data, struct dentry *dentry)
1222{
1223 int *ret = data;
1224 if (d_mountpoint(dentry)) {
1225 *ret = 1;
1226 return D_WALK_QUIT;
1227 }
1228 return D_WALK_CONTINUE;
1229}
1230
1231int have_submounts(struct dentry *parent)
1232{
1233 int ret = 0;
1234
1235 d_walk(parent, &ret, check_mount, NULL);
1236
1237 return ret;
1238}
ec4f8605 1239EXPORT_SYMBOL(have_submounts);
1da177e4 1240
eed81007
MS
1241/*
1242 * Called by mount code to set a mountpoint and check if the mountpoint is
1243 * reachable (e.g. NFS can unhash a directory dentry and then the complete
1244 * subtree can become unreachable).
1245 *
1246 * Only one of check_submounts_and_drop() and d_set_mounted() must succeed. For
1247 * this reason take rename_lock and d_lock on dentry and ancestors.
1248 */
1249int d_set_mounted(struct dentry *dentry)
1250{
1251 struct dentry *p;
1252 int ret = -ENOENT;
1253 write_seqlock(&rename_lock);
1254 for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
1255 /* Need exclusion wrt. check_submounts_and_drop() */
1256 spin_lock(&p->d_lock);
1257 if (unlikely(d_unhashed(p))) {
1258 spin_unlock(&p->d_lock);
1259 goto out;
1260 }
1261 spin_unlock(&p->d_lock);
1262 }
1263 spin_lock(&dentry->d_lock);
1264 if (!d_unlinked(dentry)) {
1265 dentry->d_flags |= DCACHE_MOUNTED;
1266 ret = 0;
1267 }
1268 spin_unlock(&dentry->d_lock);
1269out:
1270 write_sequnlock(&rename_lock);
1271 return ret;
1272}
1273
1da177e4 1274/*
fd517909 1275 * Search the dentry child list of the specified parent,
1da177e4
LT
1276 * and move any unused dentries to the end of the unused
1277 * list for prune_dcache(). We descend to the next level
1278 * whenever the d_subdirs list is non-empty and continue
1279 * searching.
1280 *
1281 * It returns zero iff there are no unused children,
1282 * otherwise it returns the number of children moved to
1283 * the end of the unused list. This may not be the total
1284 * number of unused children, because select_parent can
1285 * drop the lock and return early due to latency
1286 * constraints.
1287 */
1da177e4 1288
db14fc3a
MS
1289struct select_data {
1290 struct dentry *start;
1291 struct list_head dispose;
1292 int found;
1293};
23044507 1294
db14fc3a
MS
1295static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
1296{
1297 struct select_data *data = _data;
1298 enum d_walk_ret ret = D_WALK_CONTINUE;
1da177e4 1299
db14fc3a
MS
1300 if (data->start == dentry)
1301 goto out;
2fd6b7f5 1302
1da177e4 1303 /*
db14fc3a
MS
1304 * move only zero ref count dentries to the dispose list.
1305 *
1306 * Those which are presently on the shrink list, being processed
1307 * by shrink_dentry_list(), shouldn't be moved. Otherwise the
1308 * loop in shrink_dcache_parent() might not make any progress
1309 * and loop forever.
1da177e4 1310 */
db14fc3a
MS
1311 if (dentry->d_lockref.count) {
1312 dentry_lru_del(dentry);
1313 } else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
89dc77bc
LT
1314 /*
1315 * We can't use d_lru_shrink_move() because we
1316 * need to get the global LRU lock and do the
05a8252b 1317 * LRU accounting.
89dc77bc
LT
1318 */
1319 d_lru_del(dentry);
1320 d_shrink_add(dentry, &data->dispose);
db14fc3a
MS
1321 data->found++;
1322 ret = D_WALK_NORETRY;
1da177e4 1323 }
db14fc3a
MS
1324 /*
1325 * We can return to the caller if we have found some (this
1326 * ensures forward progress). We'll be coming back to find
1327 * the rest.
1328 */
1329 if (data->found && need_resched())
1330 ret = D_WALK_QUIT;
1da177e4 1331out:
db14fc3a 1332 return ret;
1da177e4
LT
1333}
1334
1335/**
1336 * shrink_dcache_parent - prune dcache
1337 * @parent: parent of entries to prune
1338 *
1339 * Prune the dcache to remove unused children of the parent dentry.
1340 */
db14fc3a 1341void shrink_dcache_parent(struct dentry *parent)
1da177e4 1342{
db14fc3a
MS
1343 for (;;) {
1344 struct select_data data;
1da177e4 1345
db14fc3a
MS
1346 INIT_LIST_HEAD(&data.dispose);
1347 data.start = parent;
1348 data.found = 0;
1349
1350 d_walk(parent, &data, select_collect, NULL);
1351 if (!data.found)
1352 break;
1353
1354 shrink_dentry_list(&data.dispose);
421348f1
GT
1355 cond_resched();
1356 }
1da177e4 1357}
ec4f8605 1358EXPORT_SYMBOL(shrink_dcache_parent);
1da177e4 1359
42c32608
AV
1360static enum d_walk_ret umount_collect(void *_data, struct dentry *dentry)
1361{
1362 struct select_data *data = _data;
1363 enum d_walk_ret ret = D_WALK_CONTINUE;
1364
1365 if (dentry->d_lockref.count) {
1366 dentry_lru_del(dentry);
1367 if (likely(!list_empty(&dentry->d_subdirs)))
1368 goto out;
1369 if (dentry == data->start && dentry->d_lockref.count == 1)
1370 goto out;
1371 printk(KERN_ERR
1372 "BUG: Dentry %p{i=%lx,n=%s}"
1373 " still in use (%d)"
1374 " [unmount of %s %s]\n",
1375 dentry,
1376 dentry->d_inode ?
1377 dentry->d_inode->i_ino : 0UL,
1378 dentry->d_name.name,
1379 dentry->d_lockref.count,
1380 dentry->d_sb->s_type->name,
1381 dentry->d_sb->s_id);
1382 BUG();
1383 } else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
1384 /*
1385 * We can't use d_lru_shrink_move() because we
1386 * need to get the global LRU lock and do the
1387 * LRU accounting.
1388 */
1389 if (dentry->d_flags & DCACHE_LRU_LIST)
1390 d_lru_del(dentry);
1391 d_shrink_add(dentry, &data->dispose);
1392 data->found++;
1393 ret = D_WALK_NORETRY;
1394 }
1395out:
1396 if (data->found && need_resched())
1397 ret = D_WALK_QUIT;
1398 return ret;
1399}
1400
1401/*
1402 * destroy the dentries attached to a superblock on unmounting
1403 */
1404void shrink_dcache_for_umount(struct super_block *sb)
1405{
1406 struct dentry *dentry;
1407
1408 if (down_read_trylock(&sb->s_umount))
1409 BUG();
1410
1411 dentry = sb->s_root;
1412 sb->s_root = NULL;
1413 for (;;) {
1414 struct select_data data;
1415
1416 INIT_LIST_HEAD(&data.dispose);
1417 data.start = dentry;
1418 data.found = 0;
1419
1420 d_walk(dentry, &data, umount_collect, NULL);
1421 if (!data.found)
1422 break;
1423
1424 shrink_dentry_list(&data.dispose);
1425 cond_resched();
1426 }
1427 d_drop(dentry);
1428 dput(dentry);
1429
1430 while (!hlist_bl_empty(&sb->s_anon)) {
1431 struct select_data data;
1432 dentry = hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash);
1433
1434 INIT_LIST_HEAD(&data.dispose);
1435 data.start = NULL;
1436 data.found = 0;
1437
1438 d_walk(dentry, &data, umount_collect, NULL);
1439 if (data.found)
1440 shrink_dentry_list(&data.dispose);
1441 cond_resched();
1442 }
1443}
1444
848ac114
MS
1445static enum d_walk_ret check_and_collect(void *_data, struct dentry *dentry)
1446{
1447 struct select_data *data = _data;
1448
1449 if (d_mountpoint(dentry)) {
1450 data->found = -EBUSY;
1451 return D_WALK_QUIT;
1452 }
1453
1454 return select_collect(_data, dentry);
1455}
1456
1457static void check_and_drop(void *_data)
1458{
1459 struct select_data *data = _data;
1460
1461 if (d_mountpoint(data->start))
1462 data->found = -EBUSY;
1463 if (!data->found)
1464 __d_drop(data->start);
1465}
1466
1467/**
1468 * check_submounts_and_drop - prune dcache, check for submounts and drop
1469 *
1470 * All done as a single atomic operation relative to has_unlinked_ancestor().
1471 * Returns 0 if successfully unhashed @parent. If there were submounts then
1472 * return -EBUSY.
1473 *
1474 * @dentry: dentry to prune and drop
1475 */
1476int check_submounts_and_drop(struct dentry *dentry)
1477{
1478 int ret = 0;
1479
1480 /* Negative dentries can be dropped without further checks */
1481 if (!dentry->d_inode) {
1482 d_drop(dentry);
1483 goto out;
1484 }
1485
1486 for (;;) {
1487 struct select_data data;
1488
1489 INIT_LIST_HEAD(&data.dispose);
1490 data.start = dentry;
1491 data.found = 0;
1492
1493 d_walk(dentry, &data, check_and_collect, check_and_drop);
1494 ret = data.found;
1495
1496 if (!list_empty(&data.dispose))
1497 shrink_dentry_list(&data.dispose);
1498
1499 if (ret <= 0)
1500 break;
1501
1502 cond_resched();
1503 }
1504
1505out:
1506 return ret;
1507}
1508EXPORT_SYMBOL(check_submounts_and_drop);
1509
1da177e4 1510/**
a4464dbc
AV
1511 * __d_alloc - allocate a dcache entry
1512 * @sb: filesystem it will belong to
1da177e4
LT
1513 * @name: qstr of the name
1514 *
1515 * Allocates a dentry. It returns %NULL if there is insufficient memory
1516 * available. On a success the dentry is returned. The name passed in is
1517 * copied and the copy passed in may be reused after this call.
1518 */
1519
a4464dbc 1520struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
1da177e4
LT
1521{
1522 struct dentry *dentry;
1523 char *dname;
1524
e12ba74d 1525 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
1da177e4
LT
1526 if (!dentry)
1527 return NULL;
1528
6326c71f
LT
1529 /*
1530 * We guarantee that the inline name is always NUL-terminated.
1531 * This way the memcpy() done by the name switching in rename
1532 * will still always have a NUL at the end, even if we might
1533 * be overwriting an internal NUL character
1534 */
1535 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
1da177e4
LT
1536 if (name->len > DNAME_INLINE_LEN-1) {
1537 dname = kmalloc(name->len + 1, GFP_KERNEL);
1538 if (!dname) {
1539 kmem_cache_free(dentry_cache, dentry);
1540 return NULL;
1541 }
1542 } else {
1543 dname = dentry->d_iname;
1544 }
1da177e4
LT
1545
1546 dentry->d_name.len = name->len;
1547 dentry->d_name.hash = name->hash;
1548 memcpy(dname, name->name, name->len);
1549 dname[name->len] = 0;
1550
6326c71f
LT
1551 /* Make sure we always see the terminating NUL character */
1552 smp_wmb();
1553 dentry->d_name.name = dname;
1554
98474236 1555 dentry->d_lockref.count = 1;
dea3667b 1556 dentry->d_flags = 0;
1da177e4 1557 spin_lock_init(&dentry->d_lock);
31e6b01f 1558 seqcount_init(&dentry->d_seq);
1da177e4 1559 dentry->d_inode = NULL;
a4464dbc
AV
1560 dentry->d_parent = dentry;
1561 dentry->d_sb = sb;
1da177e4
LT
1562 dentry->d_op = NULL;
1563 dentry->d_fsdata = NULL;
ceb5bdc2 1564 INIT_HLIST_BL_NODE(&dentry->d_hash);
1da177e4
LT
1565 INIT_LIST_HEAD(&dentry->d_lru);
1566 INIT_LIST_HEAD(&dentry->d_subdirs);
b3d9b7a3 1567 INIT_HLIST_NODE(&dentry->d_alias);
2fd6b7f5 1568 INIT_LIST_HEAD(&dentry->d_u.d_child);
a4464dbc 1569 d_set_d_op(dentry, dentry->d_sb->s_d_op);
1da177e4 1570
3e880fb5 1571 this_cpu_inc(nr_dentry);
312d3ca8 1572
1da177e4
LT
1573 return dentry;
1574}
a4464dbc
AV
1575
1576/**
1577 * d_alloc - allocate a dcache entry
1578 * @parent: parent of entry to allocate
1579 * @name: qstr of the name
1580 *
1581 * Allocates a dentry. It returns %NULL if there is insufficient memory
1582 * available. On a success the dentry is returned. The name passed in is
1583 * copied and the copy passed in may be reused after this call.
1584 */
1585struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1586{
1587 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1588 if (!dentry)
1589 return NULL;
1590
1591 spin_lock(&parent->d_lock);
1592 /*
1593 * don't need child lock because it is not subject
1594 * to concurrency here
1595 */
1596 __dget_dlock(parent);
1597 dentry->d_parent = parent;
1598 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
1599 spin_unlock(&parent->d_lock);
1600
1601 return dentry;
1602}
ec4f8605 1603EXPORT_SYMBOL(d_alloc);
1da177e4 1604
4b936885
NP
1605struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1606{
a4464dbc
AV
1607 struct dentry *dentry = __d_alloc(sb, name);
1608 if (dentry)
4b936885 1609 dentry->d_flags |= DCACHE_DISCONNECTED;
4b936885
NP
1610 return dentry;
1611}
1612EXPORT_SYMBOL(d_alloc_pseudo);
1613
1da177e4
LT
1614struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1615{
1616 struct qstr q;
1617
1618 q.name = name;
1619 q.len = strlen(name);
1620 q.hash = full_name_hash(q.name, q.len);
1621 return d_alloc(parent, &q);
1622}
ef26ca97 1623EXPORT_SYMBOL(d_alloc_name);
1da177e4 1624
fb045adb
NP
1625void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1626{
6f7f7caa
LT
1627 WARN_ON_ONCE(dentry->d_op);
1628 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
fb045adb
NP
1629 DCACHE_OP_COMPARE |
1630 DCACHE_OP_REVALIDATE |
ecf3d1f1 1631 DCACHE_OP_WEAK_REVALIDATE |
fb045adb
NP
1632 DCACHE_OP_DELETE ));
1633 dentry->d_op = op;
1634 if (!op)
1635 return;
1636 if (op->d_hash)
1637 dentry->d_flags |= DCACHE_OP_HASH;
1638 if (op->d_compare)
1639 dentry->d_flags |= DCACHE_OP_COMPARE;
1640 if (op->d_revalidate)
1641 dentry->d_flags |= DCACHE_OP_REVALIDATE;
ecf3d1f1
JL
1642 if (op->d_weak_revalidate)
1643 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
fb045adb
NP
1644 if (op->d_delete)
1645 dentry->d_flags |= DCACHE_OP_DELETE;
f0023bc6
SW
1646 if (op->d_prune)
1647 dentry->d_flags |= DCACHE_OP_PRUNE;
fb045adb
NP
1648
1649}
1650EXPORT_SYMBOL(d_set_d_op);
1651
b18825a7
DH
1652static unsigned d_flags_for_inode(struct inode *inode)
1653{
1654 unsigned add_flags = DCACHE_FILE_TYPE;
1655
1656 if (!inode)
1657 return DCACHE_MISS_TYPE;
1658
1659 if (S_ISDIR(inode->i_mode)) {
1660 add_flags = DCACHE_DIRECTORY_TYPE;
1661 if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) {
1662 if (unlikely(!inode->i_op->lookup))
1663 add_flags = DCACHE_AUTODIR_TYPE;
1664 else
1665 inode->i_opflags |= IOP_LOOKUP;
1666 }
1667 } else if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1668 if (unlikely(inode->i_op->follow_link))
1669 add_flags = DCACHE_SYMLINK_TYPE;
1670 else
1671 inode->i_opflags |= IOP_NOFOLLOW;
1672 }
1673
1674 if (unlikely(IS_AUTOMOUNT(inode)))
1675 add_flags |= DCACHE_NEED_AUTOMOUNT;
1676 return add_flags;
1677}
1678
360da900
OH
1679static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1680{
b18825a7
DH
1681 unsigned add_flags = d_flags_for_inode(inode);
1682
b23fb0a6 1683 spin_lock(&dentry->d_lock);
b18825a7
DH
1684 dentry->d_flags &= ~DCACHE_ENTRY_TYPE;
1685 dentry->d_flags |= add_flags;
1686 if (inode)
b3d9b7a3 1687 hlist_add_head(&dentry->d_alias, &inode->i_dentry);
360da900 1688 dentry->d_inode = inode;
31e6b01f 1689 dentry_rcuwalk_barrier(dentry);
b23fb0a6 1690 spin_unlock(&dentry->d_lock);
360da900
OH
1691 fsnotify_d_instantiate(dentry, inode);
1692}
1693
1da177e4
LT
1694/**
1695 * d_instantiate - fill in inode information for a dentry
1696 * @entry: dentry to complete
1697 * @inode: inode to attach to this dentry
1698 *
1699 * Fill in inode information in the entry.
1700 *
1701 * This turns negative dentries into productive full members
1702 * of society.
1703 *
1704 * NOTE! This assumes that the inode count has been incremented
1705 * (or otherwise set) by the caller to indicate that it is now
1706 * in use by the dcache.
1707 */
1708
1709void d_instantiate(struct dentry *entry, struct inode * inode)
1710{
b3d9b7a3 1711 BUG_ON(!hlist_unhashed(&entry->d_alias));
873feea0
NP
1712 if (inode)
1713 spin_lock(&inode->i_lock);
360da900 1714 __d_instantiate(entry, inode);
873feea0
NP
1715 if (inode)
1716 spin_unlock(&inode->i_lock);
1da177e4
LT
1717 security_d_instantiate(entry, inode);
1718}
ec4f8605 1719EXPORT_SYMBOL(d_instantiate);
1da177e4
LT
1720
1721/**
1722 * d_instantiate_unique - instantiate a non-aliased dentry
1723 * @entry: dentry to instantiate
1724 * @inode: inode to attach to this dentry
1725 *
1726 * Fill in inode information in the entry. On success, it returns NULL.
1727 * If an unhashed alias of "entry" already exists, then we return the
e866cfa9 1728 * aliased dentry instead and drop one reference to inode.
1da177e4
LT
1729 *
1730 * Note that in order to avoid conflicts with rename() etc, the caller
1731 * had better be holding the parent directory semaphore.
e866cfa9
OD
1732 *
1733 * This also assumes that the inode count has been incremented
1734 * (or otherwise set) by the caller to indicate that it is now
1735 * in use by the dcache.
1da177e4 1736 */
770bfad8
DH
1737static struct dentry *__d_instantiate_unique(struct dentry *entry,
1738 struct inode *inode)
1da177e4
LT
1739{
1740 struct dentry *alias;
1741 int len = entry->d_name.len;
1742 const char *name = entry->d_name.name;
1743 unsigned int hash = entry->d_name.hash;
1744
770bfad8 1745 if (!inode) {
360da900 1746 __d_instantiate(entry, NULL);
770bfad8
DH
1747 return NULL;
1748 }
1749
b67bfe0d 1750 hlist_for_each_entry(alias, &inode->i_dentry, d_alias) {
9abca360
NP
1751 /*
1752 * Don't need alias->d_lock here, because aliases with
1753 * d_parent == entry->d_parent are not subject to name or
1754 * parent changes, because the parent inode i_mutex is held.
1755 */
12f8ad4b 1756 if (alias->d_name.hash != hash)
1da177e4
LT
1757 continue;
1758 if (alias->d_parent != entry->d_parent)
1759 continue;
ee983e89
LT
1760 if (alias->d_name.len != len)
1761 continue;
12f8ad4b 1762 if (dentry_cmp(alias, name, len))
1da177e4 1763 continue;
dc0474be 1764 __dget(alias);
1da177e4
LT
1765 return alias;
1766 }
770bfad8 1767
360da900 1768 __d_instantiate(entry, inode);
1da177e4
LT
1769 return NULL;
1770}
770bfad8
DH
1771
1772struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1773{
1774 struct dentry *result;
1775
b3d9b7a3 1776 BUG_ON(!hlist_unhashed(&entry->d_alias));
770bfad8 1777
873feea0
NP
1778 if (inode)
1779 spin_lock(&inode->i_lock);
770bfad8 1780 result = __d_instantiate_unique(entry, inode);
873feea0
NP
1781 if (inode)
1782 spin_unlock(&inode->i_lock);
770bfad8
DH
1783
1784 if (!result) {
1785 security_d_instantiate(entry, inode);
1786 return NULL;
1787 }
1788
1789 BUG_ON(!d_unhashed(result));
1790 iput(inode);
1791 return result;
1792}
1793
1da177e4
LT
1794EXPORT_SYMBOL(d_instantiate_unique);
1795
b70a80e7
MS
1796/**
1797 * d_instantiate_no_diralias - instantiate a non-aliased dentry
1798 * @entry: dentry to complete
1799 * @inode: inode to attach to this dentry
1800 *
1801 * Fill in inode information in the entry. If a directory alias is found, then
1802 * return an error (and drop inode). Together with d_materialise_unique() this
1803 * guarantees that a directory inode may never have more than one alias.
1804 */
1805int d_instantiate_no_diralias(struct dentry *entry, struct inode *inode)
1806{
1807 BUG_ON(!hlist_unhashed(&entry->d_alias));
1808
1809 spin_lock(&inode->i_lock);
1810 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry)) {
1811 spin_unlock(&inode->i_lock);
1812 iput(inode);
1813 return -EBUSY;
1814 }
1815 __d_instantiate(entry, inode);
1816 spin_unlock(&inode->i_lock);
1817 security_d_instantiate(entry, inode);
1818
1819 return 0;
1820}
1821EXPORT_SYMBOL(d_instantiate_no_diralias);
1822
adc0e91a
AV
1823struct dentry *d_make_root(struct inode *root_inode)
1824{
1825 struct dentry *res = NULL;
1826
1827 if (root_inode) {
26fe5750 1828 static const struct qstr name = QSTR_INIT("/", 1);
adc0e91a
AV
1829
1830 res = __d_alloc(root_inode->i_sb, &name);
1831 if (res)
1832 d_instantiate(res, root_inode);
1833 else
1834 iput(root_inode);
1835 }
1836 return res;
1837}
1838EXPORT_SYMBOL(d_make_root);
1839
d891eedb
BF
1840static struct dentry * __d_find_any_alias(struct inode *inode)
1841{
1842 struct dentry *alias;
1843
b3d9b7a3 1844 if (hlist_empty(&inode->i_dentry))
d891eedb 1845 return NULL;
b3d9b7a3 1846 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_alias);
d891eedb
BF
1847 __dget(alias);
1848 return alias;
1849}
1850
46f72b34
SW
1851/**
1852 * d_find_any_alias - find any alias for a given inode
1853 * @inode: inode to find an alias for
1854 *
1855 * If any aliases exist for the given inode, take and return a
1856 * reference for one of them. If no aliases exist, return %NULL.
1857 */
1858struct dentry *d_find_any_alias(struct inode *inode)
d891eedb
BF
1859{
1860 struct dentry *de;
1861
1862 spin_lock(&inode->i_lock);
1863 de = __d_find_any_alias(inode);
1864 spin_unlock(&inode->i_lock);
1865 return de;
1866}
46f72b34 1867EXPORT_SYMBOL(d_find_any_alias);
d891eedb 1868
4ea3ada2
CH
1869/**
1870 * d_obtain_alias - find or allocate a dentry for a given inode
1871 * @inode: inode to allocate the dentry for
1872 *
1873 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1874 * similar open by handle operations. The returned dentry may be anonymous,
1875 * or may have a full name (if the inode was already in the cache).
1876 *
1877 * When called on a directory inode, we must ensure that the inode only ever
1878 * has one dentry. If a dentry is found, that is returned instead of
1879 * allocating a new one.
1880 *
1881 * On successful return, the reference to the inode has been transferred
44003728
CH
1882 * to the dentry. In case of an error the reference on the inode is released.
1883 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1884 * be passed in and will be the error will be propagate to the return value,
1885 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
4ea3ada2
CH
1886 */
1887struct dentry *d_obtain_alias(struct inode *inode)
1888{
b911a6bd 1889 static const struct qstr anonstring = QSTR_INIT("/", 1);
9308a612
CH
1890 struct dentry *tmp;
1891 struct dentry *res;
b18825a7 1892 unsigned add_flags;
4ea3ada2
CH
1893
1894 if (!inode)
44003728 1895 return ERR_PTR(-ESTALE);
4ea3ada2
CH
1896 if (IS_ERR(inode))
1897 return ERR_CAST(inode);
1898
d891eedb 1899 res = d_find_any_alias(inode);
9308a612
CH
1900 if (res)
1901 goto out_iput;
1902
a4464dbc 1903 tmp = __d_alloc(inode->i_sb, &anonstring);
9308a612
CH
1904 if (!tmp) {
1905 res = ERR_PTR(-ENOMEM);
1906 goto out_iput;
4ea3ada2 1907 }
b5c84bf6 1908
873feea0 1909 spin_lock(&inode->i_lock);
d891eedb 1910 res = __d_find_any_alias(inode);
9308a612 1911 if (res) {
873feea0 1912 spin_unlock(&inode->i_lock);
9308a612
CH
1913 dput(tmp);
1914 goto out_iput;
1915 }
1916
1917 /* attach a disconnected dentry */
b18825a7
DH
1918 add_flags = d_flags_for_inode(inode) | DCACHE_DISCONNECTED;
1919
9308a612 1920 spin_lock(&tmp->d_lock);
9308a612 1921 tmp->d_inode = inode;
b18825a7 1922 tmp->d_flags |= add_flags;
b3d9b7a3 1923 hlist_add_head(&tmp->d_alias, &inode->i_dentry);
1879fd6a 1924 hlist_bl_lock(&tmp->d_sb->s_anon);
ceb5bdc2 1925 hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon);
1879fd6a 1926 hlist_bl_unlock(&tmp->d_sb->s_anon);
9308a612 1927 spin_unlock(&tmp->d_lock);
873feea0 1928 spin_unlock(&inode->i_lock);
24ff6663 1929 security_d_instantiate(tmp, inode);
9308a612 1930
9308a612
CH
1931 return tmp;
1932
1933 out_iput:
24ff6663
JB
1934 if (res && !IS_ERR(res))
1935 security_d_instantiate(res, inode);
9308a612
CH
1936 iput(inode);
1937 return res;
4ea3ada2 1938}
adc48720 1939EXPORT_SYMBOL(d_obtain_alias);
1da177e4
LT
1940
1941/**
1942 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1943 * @inode: the inode which may have a disconnected dentry
1944 * @dentry: a negative dentry which we want to point to the inode.
1945 *
1946 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1947 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1948 * and return it, else simply d_add the inode to the dentry and return NULL.
1949 *
1950 * This is needed in the lookup routine of any filesystem that is exportable
1951 * (via knfsd) so that we can build dcache paths to directories effectively.
1952 *
1953 * If a dentry was found and moved, then it is returned. Otherwise NULL
1954 * is returned. This matches the expected return value of ->lookup.
1955 *
6d4ade98
SW
1956 * Cluster filesystems may call this function with a negative, hashed dentry.
1957 * In that case, we know that the inode will be a regular file, and also this
1958 * will only occur during atomic_open. So we need to check for the dentry
1959 * being already hashed only in the final case.
1da177e4
LT
1960 */
1961struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1962{
1963 struct dentry *new = NULL;
1964
a9049376
AV
1965 if (IS_ERR(inode))
1966 return ERR_CAST(inode);
1967
21c0d8fd 1968 if (inode && S_ISDIR(inode->i_mode)) {
873feea0 1969 spin_lock(&inode->i_lock);
32ba9c3f 1970 new = __d_find_alias(inode, 1);
1da177e4 1971 if (new) {
32ba9c3f 1972 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
873feea0 1973 spin_unlock(&inode->i_lock);
1da177e4 1974 security_d_instantiate(new, inode);
1da177e4
LT
1975 d_move(new, dentry);
1976 iput(inode);
1977 } else {
873feea0 1978 /* already taking inode->i_lock, so d_add() by hand */
360da900 1979 __d_instantiate(dentry, inode);
873feea0 1980 spin_unlock(&inode->i_lock);
1da177e4
LT
1981 security_d_instantiate(dentry, inode);
1982 d_rehash(dentry);
1983 }
6d4ade98
SW
1984 } else {
1985 d_instantiate(dentry, inode);
1986 if (d_unhashed(dentry))
1987 d_rehash(dentry);
1988 }
1da177e4
LT
1989 return new;
1990}
ec4f8605 1991EXPORT_SYMBOL(d_splice_alias);
1da177e4 1992
9403540c
BN
1993/**
1994 * d_add_ci - lookup or allocate new dentry with case-exact name
1995 * @inode: the inode case-insensitive lookup has found
1996 * @dentry: the negative dentry that was passed to the parent's lookup func
1997 * @name: the case-exact name to be associated with the returned dentry
1998 *
1999 * This is to avoid filling the dcache with case-insensitive names to the
2000 * same inode, only the actual correct case is stored in the dcache for
2001 * case-insensitive filesystems.
2002 *
2003 * For a case-insensitive lookup match and if the the case-exact dentry
2004 * already exists in in the dcache, use it and return it.
2005 *
2006 * If no entry exists with the exact case name, allocate new dentry with
2007 * the exact case, and return the spliced entry.
2008 */
e45b590b 2009struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
9403540c
BN
2010 struct qstr *name)
2011{
9403540c
BN
2012 struct dentry *found;
2013 struct dentry *new;
2014
b6520c81
CH
2015 /*
2016 * First check if a dentry matching the name already exists,
2017 * if not go ahead and create it now.
2018 */
9403540c 2019 found = d_hash_and_lookup(dentry->d_parent, name);
4f522a24
AV
2020 if (unlikely(IS_ERR(found)))
2021 goto err_out;
9403540c
BN
2022 if (!found) {
2023 new = d_alloc(dentry->d_parent, name);
2024 if (!new) {
4f522a24 2025 found = ERR_PTR(-ENOMEM);
9403540c
BN
2026 goto err_out;
2027 }
b6520c81 2028
9403540c
BN
2029 found = d_splice_alias(inode, new);
2030 if (found) {
2031 dput(new);
2032 return found;
2033 }
2034 return new;
2035 }
b6520c81
CH
2036
2037 /*
2038 * If a matching dentry exists, and it's not negative use it.
2039 *
2040 * Decrement the reference count to balance the iget() done
2041 * earlier on.
2042 */
9403540c
BN
2043 if (found->d_inode) {
2044 if (unlikely(found->d_inode != inode)) {
2045 /* This can't happen because bad inodes are unhashed. */
2046 BUG_ON(!is_bad_inode(inode));
2047 BUG_ON(!is_bad_inode(found->d_inode));
2048 }
9403540c
BN
2049 iput(inode);
2050 return found;
2051 }
b6520c81 2052
9403540c 2053 /*
9403540c 2054 * Negative dentry: instantiate it unless the inode is a directory and
b6520c81 2055 * already has a dentry.
9403540c 2056 */
4513d899
AV
2057 new = d_splice_alias(inode, found);
2058 if (new) {
2059 dput(found);
2060 found = new;
9403540c 2061 }
4513d899 2062 return found;
9403540c
BN
2063
2064err_out:
2065 iput(inode);
4f522a24 2066 return found;
9403540c 2067}
ec4f8605 2068EXPORT_SYMBOL(d_add_ci);
1da177e4 2069
12f8ad4b
LT
2070/*
2071 * Do the slow-case of the dentry name compare.
2072 *
2073 * Unlike the dentry_cmp() function, we need to atomically
da53be12 2074 * load the name and length information, so that the
12f8ad4b
LT
2075 * filesystem can rely on them, and can use the 'name' and
2076 * 'len' information without worrying about walking off the
2077 * end of memory etc.
2078 *
2079 * Thus the read_seqcount_retry() and the "duplicate" info
2080 * in arguments (the low-level filesystem should not look
2081 * at the dentry inode or name contents directly, since
2082 * rename can change them while we're in RCU mode).
2083 */
2084enum slow_d_compare {
2085 D_COMP_OK,
2086 D_COMP_NOMATCH,
2087 D_COMP_SEQRETRY,
2088};
2089
2090static noinline enum slow_d_compare slow_dentry_cmp(
2091 const struct dentry *parent,
12f8ad4b
LT
2092 struct dentry *dentry,
2093 unsigned int seq,
2094 const struct qstr *name)
2095{
2096 int tlen = dentry->d_name.len;
2097 const char *tname = dentry->d_name.name;
12f8ad4b
LT
2098
2099 if (read_seqcount_retry(&dentry->d_seq, seq)) {
2100 cpu_relax();
2101 return D_COMP_SEQRETRY;
2102 }
da53be12 2103 if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
12f8ad4b
LT
2104 return D_COMP_NOMATCH;
2105 return D_COMP_OK;
2106}
2107
31e6b01f
NP
2108/**
2109 * __d_lookup_rcu - search for a dentry (racy, store-free)
2110 * @parent: parent dentry
2111 * @name: qstr of name we wish to find
1f1e6e52 2112 * @seqp: returns d_seq value at the point where the dentry was found
31e6b01f
NP
2113 * Returns: dentry, or NULL
2114 *
2115 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
2116 * resolution (store-free path walking) design described in
2117 * Documentation/filesystems/path-lookup.txt.
2118 *
2119 * This is not to be used outside core vfs.
2120 *
2121 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
2122 * held, and rcu_read_lock held. The returned dentry must not be stored into
2123 * without taking d_lock and checking d_seq sequence count against @seq
2124 * returned here.
2125 *
15570086 2126 * A refcount may be taken on the found dentry with the d_rcu_to_refcount
31e6b01f
NP
2127 * function.
2128 *
2129 * Alternatively, __d_lookup_rcu may be called again to look up the child of
2130 * the returned dentry, so long as its parent's seqlock is checked after the
2131 * child is looked up. Thus, an interlocking stepping of sequence lock checks
2132 * is formed, giving integrity down the path walk.
12f8ad4b
LT
2133 *
2134 * NOTE! The caller *has* to check the resulting dentry against the sequence
2135 * number we've returned before using any of the resulting dentry state!
31e6b01f 2136 */
8966be90
LT
2137struct dentry *__d_lookup_rcu(const struct dentry *parent,
2138 const struct qstr *name,
da53be12 2139 unsigned *seqp)
31e6b01f 2140{
26fe5750 2141 u64 hashlen = name->hash_len;
31e6b01f 2142 const unsigned char *str = name->name;
26fe5750 2143 struct hlist_bl_head *b = d_hash(parent, hashlen_hash(hashlen));
ceb5bdc2 2144 struct hlist_bl_node *node;
31e6b01f
NP
2145 struct dentry *dentry;
2146
2147 /*
2148 * Note: There is significant duplication with __d_lookup_rcu which is
2149 * required to prevent single threaded performance regressions
2150 * especially on architectures where smp_rmb (in seqcounts) are costly.
2151 * Keep the two functions in sync.
2152 */
2153
2154 /*
2155 * The hash list is protected using RCU.
2156 *
2157 * Carefully use d_seq when comparing a candidate dentry, to avoid
2158 * races with d_move().
2159 *
2160 * It is possible that concurrent renames can mess up our list
2161 * walk here and result in missing our dentry, resulting in the
2162 * false-negative result. d_lookup() protects against concurrent
2163 * renames using rename_lock seqlock.
2164 *
b0a4bb83 2165 * See Documentation/filesystems/path-lookup.txt for more details.
31e6b01f 2166 */
b07ad996 2167 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
8966be90 2168 unsigned seq;
31e6b01f 2169
31e6b01f 2170seqretry:
12f8ad4b
LT
2171 /*
2172 * The dentry sequence count protects us from concurrent
da53be12 2173 * renames, and thus protects parent and name fields.
12f8ad4b
LT
2174 *
2175 * The caller must perform a seqcount check in order
da53be12 2176 * to do anything useful with the returned dentry.
12f8ad4b
LT
2177 *
2178 * NOTE! We do a "raw" seqcount_begin here. That means that
2179 * we don't wait for the sequence count to stabilize if it
2180 * is in the middle of a sequence change. If we do the slow
2181 * dentry compare, we will do seqretries until it is stable,
2182 * and if we end up with a successful lookup, we actually
2183 * want to exit RCU lookup anyway.
2184 */
2185 seq = raw_seqcount_begin(&dentry->d_seq);
31e6b01f
NP
2186 if (dentry->d_parent != parent)
2187 continue;
2e321806
LT
2188 if (d_unhashed(dentry))
2189 continue;
12f8ad4b 2190
830c0f0e 2191 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
26fe5750
LT
2192 if (dentry->d_name.hash != hashlen_hash(hashlen))
2193 continue;
da53be12
LT
2194 *seqp = seq;
2195 switch (slow_dentry_cmp(parent, dentry, seq, name)) {
12f8ad4b
LT
2196 case D_COMP_OK:
2197 return dentry;
2198 case D_COMP_NOMATCH:
31e6b01f 2199 continue;
12f8ad4b
LT
2200 default:
2201 goto seqretry;
2202 }
31e6b01f 2203 }
12f8ad4b 2204
26fe5750 2205 if (dentry->d_name.hash_len != hashlen)
ee983e89 2206 continue;
da53be12 2207 *seqp = seq;
26fe5750 2208 if (!dentry_cmp(dentry, str, hashlen_len(hashlen)))
12f8ad4b 2209 return dentry;
31e6b01f
NP
2210 }
2211 return NULL;
2212}
2213
1da177e4
LT
2214/**
2215 * d_lookup - search for a dentry
2216 * @parent: parent dentry
2217 * @name: qstr of name we wish to find
b04f784e 2218 * Returns: dentry, or NULL
1da177e4 2219 *
b04f784e
NP
2220 * d_lookup searches the children of the parent dentry for the name in
2221 * question. If the dentry is found its reference count is incremented and the
2222 * dentry is returned. The caller must use dput to free the entry when it has
2223 * finished using it. %NULL is returned if the dentry does not exist.
1da177e4 2224 */
da2d8455 2225struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
1da177e4 2226{
31e6b01f 2227 struct dentry *dentry;
949854d0 2228 unsigned seq;
1da177e4
LT
2229
2230 do {
2231 seq = read_seqbegin(&rename_lock);
2232 dentry = __d_lookup(parent, name);
2233 if (dentry)
2234 break;
2235 } while (read_seqretry(&rename_lock, seq));
2236 return dentry;
2237}
ec4f8605 2238EXPORT_SYMBOL(d_lookup);
1da177e4 2239
31e6b01f 2240/**
b04f784e
NP
2241 * __d_lookup - search for a dentry (racy)
2242 * @parent: parent dentry
2243 * @name: qstr of name we wish to find
2244 * Returns: dentry, or NULL
2245 *
2246 * __d_lookup is like d_lookup, however it may (rarely) return a
2247 * false-negative result due to unrelated rename activity.
2248 *
2249 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
2250 * however it must be used carefully, eg. with a following d_lookup in
2251 * the case of failure.
2252 *
2253 * __d_lookup callers must be commented.
2254 */
a713ca2a 2255struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
1da177e4
LT
2256{
2257 unsigned int len = name->len;
2258 unsigned int hash = name->hash;
2259 const unsigned char *str = name->name;
b07ad996 2260 struct hlist_bl_head *b = d_hash(parent, hash);
ceb5bdc2 2261 struct hlist_bl_node *node;
31e6b01f 2262 struct dentry *found = NULL;
665a7583 2263 struct dentry *dentry;
1da177e4 2264
31e6b01f
NP
2265 /*
2266 * Note: There is significant duplication with __d_lookup_rcu which is
2267 * required to prevent single threaded performance regressions
2268 * especially on architectures where smp_rmb (in seqcounts) are costly.
2269 * Keep the two functions in sync.
2270 */
2271
b04f784e
NP
2272 /*
2273 * The hash list is protected using RCU.
2274 *
2275 * Take d_lock when comparing a candidate dentry, to avoid races
2276 * with d_move().
2277 *
2278 * It is possible that concurrent renames can mess up our list
2279 * walk here and result in missing our dentry, resulting in the
2280 * false-negative result. d_lookup() protects against concurrent
2281 * renames using rename_lock seqlock.
2282 *
b0a4bb83 2283 * See Documentation/filesystems/path-lookup.txt for more details.
b04f784e 2284 */
1da177e4
LT
2285 rcu_read_lock();
2286
b07ad996 2287 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
1da177e4 2288
1da177e4
LT
2289 if (dentry->d_name.hash != hash)
2290 continue;
1da177e4
LT
2291
2292 spin_lock(&dentry->d_lock);
1da177e4
LT
2293 if (dentry->d_parent != parent)
2294 goto next;
d0185c08
LT
2295 if (d_unhashed(dentry))
2296 goto next;
2297
1da177e4
LT
2298 /*
2299 * It is safe to compare names since d_move() cannot
2300 * change the qstr (protected by d_lock).
2301 */
fb045adb 2302 if (parent->d_flags & DCACHE_OP_COMPARE) {
12f8ad4b
LT
2303 int tlen = dentry->d_name.len;
2304 const char *tname = dentry->d_name.name;
da53be12 2305 if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
1da177e4
LT
2306 goto next;
2307 } else {
ee983e89
LT
2308 if (dentry->d_name.len != len)
2309 goto next;
12f8ad4b 2310 if (dentry_cmp(dentry, str, len))
1da177e4
LT
2311 goto next;
2312 }
2313
98474236 2314 dentry->d_lockref.count++;
d0185c08 2315 found = dentry;
1da177e4
LT
2316 spin_unlock(&dentry->d_lock);
2317 break;
2318next:
2319 spin_unlock(&dentry->d_lock);
2320 }
2321 rcu_read_unlock();
2322
2323 return found;
2324}
2325
3e7e241f
EB
2326/**
2327 * d_hash_and_lookup - hash the qstr then search for a dentry
2328 * @dir: Directory to search in
2329 * @name: qstr of name we wish to find
2330 *
4f522a24 2331 * On lookup failure NULL is returned; on bad name - ERR_PTR(-error)
3e7e241f
EB
2332 */
2333struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2334{
3e7e241f
EB
2335 /*
2336 * Check for a fs-specific hash function. Note that we must
2337 * calculate the standard hash first, as the d_op->d_hash()
2338 * routine may choose to leave the hash value unchanged.
2339 */
2340 name->hash = full_name_hash(name->name, name->len);
fb045adb 2341 if (dir->d_flags & DCACHE_OP_HASH) {
da53be12 2342 int err = dir->d_op->d_hash(dir, name);
4f522a24
AV
2343 if (unlikely(err < 0))
2344 return ERR_PTR(err);
3e7e241f 2345 }
4f522a24 2346 return d_lookup(dir, name);
3e7e241f 2347}
4f522a24 2348EXPORT_SYMBOL(d_hash_and_lookup);
3e7e241f 2349
1da177e4 2350/**
786a5e15 2351 * d_validate - verify dentry provided from insecure source (deprecated)
1da177e4 2352 * @dentry: The dentry alleged to be valid child of @dparent
ff5fdb61 2353 * @dparent: The parent dentry (known to be valid)
1da177e4
LT
2354 *
2355 * An insecure source has sent us a dentry, here we verify it and dget() it.
2356 * This is used by ncpfs in its readdir implementation.
2357 * Zero is returned in the dentry is invalid.
786a5e15
NP
2358 *
2359 * This function is slow for big directories, and deprecated, do not use it.
1da177e4 2360 */
d3a23e16 2361int d_validate(struct dentry *dentry, struct dentry *dparent)
1da177e4 2362{
786a5e15 2363 struct dentry *child;
d3a23e16 2364
2fd6b7f5 2365 spin_lock(&dparent->d_lock);
786a5e15
NP
2366 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
2367 if (dentry == child) {
2fd6b7f5 2368 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
dc0474be 2369 __dget_dlock(dentry);
2fd6b7f5
NP
2370 spin_unlock(&dentry->d_lock);
2371 spin_unlock(&dparent->d_lock);
1da177e4
LT
2372 return 1;
2373 }
2374 }
2fd6b7f5 2375 spin_unlock(&dparent->d_lock);
786a5e15 2376
1da177e4
LT
2377 return 0;
2378}
ec4f8605 2379EXPORT_SYMBOL(d_validate);
1da177e4
LT
2380
2381/*
2382 * When a file is deleted, we have two options:
2383 * - turn this dentry into a negative dentry
2384 * - unhash this dentry and free it.
2385 *
2386 * Usually, we want to just turn this into
2387 * a negative dentry, but if anybody else is
2388 * currently using the dentry or the inode
2389 * we can't do that and we fall back on removing
2390 * it from the hash queues and waiting for
2391 * it to be deleted later when it has no users
2392 */
2393
2394/**
2395 * d_delete - delete a dentry
2396 * @dentry: The dentry to delete
2397 *
2398 * Turn the dentry into a negative dentry if possible, otherwise
2399 * remove it from the hash queues so it can be deleted later
2400 */
2401
2402void d_delete(struct dentry * dentry)
2403{
873feea0 2404 struct inode *inode;
7a91bf7f 2405 int isdir = 0;
1da177e4
LT
2406 /*
2407 * Are we the only user?
2408 */
357f8e65 2409again:
1da177e4 2410 spin_lock(&dentry->d_lock);
873feea0
NP
2411 inode = dentry->d_inode;
2412 isdir = S_ISDIR(inode->i_mode);
98474236 2413 if (dentry->d_lockref.count == 1) {
1fe0c023 2414 if (!spin_trylock(&inode->i_lock)) {
357f8e65
NP
2415 spin_unlock(&dentry->d_lock);
2416 cpu_relax();
2417 goto again;
2418 }
13e3c5e5 2419 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
31e6b01f 2420 dentry_unlink_inode(dentry);
7a91bf7f 2421 fsnotify_nameremove(dentry, isdir);
1da177e4
LT
2422 return;
2423 }
2424
2425 if (!d_unhashed(dentry))
2426 __d_drop(dentry);
2427
2428 spin_unlock(&dentry->d_lock);
7a91bf7f
JM
2429
2430 fsnotify_nameremove(dentry, isdir);
1da177e4 2431}
ec4f8605 2432EXPORT_SYMBOL(d_delete);
1da177e4 2433
b07ad996 2434static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b)
1da177e4 2435{
ceb5bdc2 2436 BUG_ON(!d_unhashed(entry));
1879fd6a 2437 hlist_bl_lock(b);
dea3667b 2438 entry->d_flags |= DCACHE_RCUACCESS;
b07ad996 2439 hlist_bl_add_head_rcu(&entry->d_hash, b);
1879fd6a 2440 hlist_bl_unlock(b);
1da177e4
LT
2441}
2442
770bfad8
DH
2443static void _d_rehash(struct dentry * entry)
2444{
2445 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
2446}
2447
1da177e4
LT
2448/**
2449 * d_rehash - add an entry back to the hash
2450 * @entry: dentry to add to the hash
2451 *
2452 * Adds a dentry to the hash according to its name.
2453 */
2454
2455void d_rehash(struct dentry * entry)
2456{
1da177e4 2457 spin_lock(&entry->d_lock);
770bfad8 2458 _d_rehash(entry);
1da177e4 2459 spin_unlock(&entry->d_lock);
1da177e4 2460}
ec4f8605 2461EXPORT_SYMBOL(d_rehash);
1da177e4 2462
fb2d5b86
NP
2463/**
2464 * dentry_update_name_case - update case insensitive dentry with a new name
2465 * @dentry: dentry to be updated
2466 * @name: new name
2467 *
2468 * Update a case insensitive dentry with new case of name.
2469 *
2470 * dentry must have been returned by d_lookup with name @name. Old and new
2471 * name lengths must match (ie. no d_compare which allows mismatched name
2472 * lengths).
2473 *
2474 * Parent inode i_mutex must be held over d_lookup and into this call (to
2475 * keep renames and concurrent inserts, and readdir(2) away).
2476 */
2477void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
2478{
7ebfa57f 2479 BUG_ON(!mutex_is_locked(&dentry->d_parent->d_inode->i_mutex));
fb2d5b86
NP
2480 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
2481
fb2d5b86 2482 spin_lock(&dentry->d_lock);
31e6b01f 2483 write_seqcount_begin(&dentry->d_seq);
fb2d5b86 2484 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
31e6b01f 2485 write_seqcount_end(&dentry->d_seq);
fb2d5b86 2486 spin_unlock(&dentry->d_lock);
fb2d5b86
NP
2487}
2488EXPORT_SYMBOL(dentry_update_name_case);
2489
1da177e4
LT
2490static void switch_names(struct dentry *dentry, struct dentry *target)
2491{
2492 if (dname_external(target)) {
2493 if (dname_external(dentry)) {
2494 /*
2495 * Both external: swap the pointers
2496 */
9a8d5bb4 2497 swap(target->d_name.name, dentry->d_name.name);
1da177e4
LT
2498 } else {
2499 /*
2500 * dentry:internal, target:external. Steal target's
2501 * storage and make target internal.
2502 */
321bcf92
BF
2503 memcpy(target->d_iname, dentry->d_name.name,
2504 dentry->d_name.len + 1);
1da177e4
LT
2505 dentry->d_name.name = target->d_name.name;
2506 target->d_name.name = target->d_iname;
2507 }
2508 } else {
2509 if (dname_external(dentry)) {
2510 /*
2511 * dentry:external, target:internal. Give dentry's
2512 * storage to target and make dentry internal
2513 */
2514 memcpy(dentry->d_iname, target->d_name.name,
2515 target->d_name.len + 1);
2516 target->d_name.name = dentry->d_name.name;
2517 dentry->d_name.name = dentry->d_iname;
2518 } else {
2519 /*
2520 * Both are internal. Just copy target to dentry
2521 */
2522 memcpy(dentry->d_iname, target->d_name.name,
2523 target->d_name.len + 1);
dc711ca3
AV
2524 dentry->d_name.len = target->d_name.len;
2525 return;
1da177e4
LT
2526 }
2527 }
9a8d5bb4 2528 swap(dentry->d_name.len, target->d_name.len);
1da177e4
LT
2529}
2530
2fd6b7f5
NP
2531static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
2532{
2533 /*
2534 * XXXX: do we really need to take target->d_lock?
2535 */
2536 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
2537 spin_lock(&target->d_parent->d_lock);
2538 else {
2539 if (d_ancestor(dentry->d_parent, target->d_parent)) {
2540 spin_lock(&dentry->d_parent->d_lock);
2541 spin_lock_nested(&target->d_parent->d_lock,
2542 DENTRY_D_LOCK_NESTED);
2543 } else {
2544 spin_lock(&target->d_parent->d_lock);
2545 spin_lock_nested(&dentry->d_parent->d_lock,
2546 DENTRY_D_LOCK_NESTED);
2547 }
2548 }
2549 if (target < dentry) {
2550 spin_lock_nested(&target->d_lock, 2);
2551 spin_lock_nested(&dentry->d_lock, 3);
2552 } else {
2553 spin_lock_nested(&dentry->d_lock, 2);
2554 spin_lock_nested(&target->d_lock, 3);
2555 }
2556}
2557
2558static void dentry_unlock_parents_for_move(struct dentry *dentry,
2559 struct dentry *target)
2560{
2561 if (target->d_parent != dentry->d_parent)
2562 spin_unlock(&dentry->d_parent->d_lock);
2563 if (target->d_parent != target)
2564 spin_unlock(&target->d_parent->d_lock);
2565}
2566
1da177e4 2567/*
2fd6b7f5
NP
2568 * When switching names, the actual string doesn't strictly have to
2569 * be preserved in the target - because we're dropping the target
2570 * anyway. As such, we can just do a simple memcpy() to copy over
2571 * the new name before we switch.
2572 *
2573 * Note that we have to be a lot more careful about getting the hash
2574 * switched - we have to switch the hash value properly even if it
2575 * then no longer matches the actual (corrupted) string of the target.
2576 * The hash value has to match the hash queue that the dentry is on..
1da177e4 2577 */
9eaef27b 2578/*
18367501 2579 * __d_move - move a dentry
1da177e4
LT
2580 * @dentry: entry to move
2581 * @target: new dentry
2582 *
2583 * Update the dcache to reflect the move of a file name. Negative
c46c8877
JL
2584 * dcache entries should not be moved in this way. Caller must hold
2585 * rename_lock, the i_mutex of the source and target directories,
2586 * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
1da177e4 2587 */
18367501 2588static void __d_move(struct dentry * dentry, struct dentry * target)
1da177e4 2589{
1da177e4
LT
2590 if (!dentry->d_inode)
2591 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2592
2fd6b7f5
NP
2593 BUG_ON(d_ancestor(dentry, target));
2594 BUG_ON(d_ancestor(target, dentry));
2595
2fd6b7f5 2596 dentry_lock_for_move(dentry, target);
1da177e4 2597
31e6b01f
NP
2598 write_seqcount_begin(&dentry->d_seq);
2599 write_seqcount_begin(&target->d_seq);
2600
ceb5bdc2
NP
2601 /* __d_drop does write_seqcount_barrier, but they're OK to nest. */
2602
2603 /*
2604 * Move the dentry to the target hash queue. Don't bother checking
2605 * for the same hash queue because of how unlikely it is.
2606 */
2607 __d_drop(dentry);
789680d1 2608 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
1da177e4
LT
2609
2610 /* Unhash the target: dput() will then get rid of it */
2611 __d_drop(target);
2612
5160ee6f
ED
2613 list_del(&dentry->d_u.d_child);
2614 list_del(&target->d_u.d_child);
1da177e4
LT
2615
2616 /* Switch the names.. */
2617 switch_names(dentry, target);
9a8d5bb4 2618 swap(dentry->d_name.hash, target->d_name.hash);
1da177e4
LT
2619
2620 /* ... and switch the parents */
2621 if (IS_ROOT(dentry)) {
2622 dentry->d_parent = target->d_parent;
2623 target->d_parent = target;
5160ee6f 2624 INIT_LIST_HEAD(&target->d_u.d_child);
1da177e4 2625 } else {
9a8d5bb4 2626 swap(dentry->d_parent, target->d_parent);
1da177e4
LT
2627
2628 /* And add them back to the (new) parent lists */
5160ee6f 2629 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1da177e4
LT
2630 }
2631
5160ee6f 2632 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2fd6b7f5 2633
31e6b01f
NP
2634 write_seqcount_end(&target->d_seq);
2635 write_seqcount_end(&dentry->d_seq);
2636
2fd6b7f5 2637 dentry_unlock_parents_for_move(dentry, target);
1da177e4 2638 spin_unlock(&target->d_lock);
c32ccd87 2639 fsnotify_d_move(dentry);
1da177e4 2640 spin_unlock(&dentry->d_lock);
18367501
AV
2641}
2642
2643/*
2644 * d_move - move a dentry
2645 * @dentry: entry to move
2646 * @target: new dentry
2647 *
2648 * Update the dcache to reflect the move of a file name. Negative
c46c8877
JL
2649 * dcache entries should not be moved in this way. See the locking
2650 * requirements for __d_move.
18367501
AV
2651 */
2652void d_move(struct dentry *dentry, struct dentry *target)
2653{
2654 write_seqlock(&rename_lock);
2655 __d_move(dentry, target);
1da177e4 2656 write_sequnlock(&rename_lock);
9eaef27b 2657}
ec4f8605 2658EXPORT_SYMBOL(d_move);
1da177e4 2659
e2761a11
OH
2660/**
2661 * d_ancestor - search for an ancestor
2662 * @p1: ancestor dentry
2663 * @p2: child dentry
2664 *
2665 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2666 * an ancestor of p2, else NULL.
9eaef27b 2667 */
e2761a11 2668struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
9eaef27b
TM
2669{
2670 struct dentry *p;
2671
871c0067 2672 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
9eaef27b 2673 if (p->d_parent == p1)
e2761a11 2674 return p;
9eaef27b 2675 }
e2761a11 2676 return NULL;
9eaef27b
TM
2677}
2678
2679/*
2680 * This helper attempts to cope with remotely renamed directories
2681 *
2682 * It assumes that the caller is already holding
18367501 2683 * dentry->d_parent->d_inode->i_mutex, inode->i_lock and rename_lock
9eaef27b
TM
2684 *
2685 * Note: If ever the locking in lock_rename() changes, then please
2686 * remember to update this too...
9eaef27b 2687 */
873feea0
NP
2688static struct dentry *__d_unalias(struct inode *inode,
2689 struct dentry *dentry, struct dentry *alias)
9eaef27b
TM
2690{
2691 struct mutex *m1 = NULL, *m2 = NULL;
ee3efa91 2692 struct dentry *ret = ERR_PTR(-EBUSY);
9eaef27b
TM
2693
2694 /* If alias and dentry share a parent, then no extra locks required */
2695 if (alias->d_parent == dentry->d_parent)
2696 goto out_unalias;
2697
9eaef27b 2698 /* See lock_rename() */
9eaef27b
TM
2699 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2700 goto out_err;
2701 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2702 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2703 goto out_err;
2704 m2 = &alias->d_parent->d_inode->i_mutex;
2705out_unalias:
ee3efa91
AV
2706 if (likely(!d_mountpoint(alias))) {
2707 __d_move(alias, dentry);
2708 ret = alias;
2709 }
9eaef27b 2710out_err:
873feea0 2711 spin_unlock(&inode->i_lock);
9eaef27b
TM
2712 if (m2)
2713 mutex_unlock(m2);
2714 if (m1)
2715 mutex_unlock(m1);
2716 return ret;
2717}
2718
770bfad8
DH
2719/*
2720 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2721 * named dentry in place of the dentry to be replaced.
2fd6b7f5 2722 * returns with anon->d_lock held!
770bfad8
DH
2723 */
2724static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2725{
740da42e 2726 struct dentry *dparent;
770bfad8 2727
2fd6b7f5 2728 dentry_lock_for_move(anon, dentry);
770bfad8 2729
31e6b01f
NP
2730 write_seqcount_begin(&dentry->d_seq);
2731 write_seqcount_begin(&anon->d_seq);
2732
770bfad8 2733 dparent = dentry->d_parent;
770bfad8 2734
2fd6b7f5
NP
2735 switch_names(dentry, anon);
2736 swap(dentry->d_name.hash, anon->d_name.hash);
2737
740da42e
AV
2738 dentry->d_parent = dentry;
2739 list_del_init(&dentry->d_u.d_child);
2740 anon->d_parent = dparent;
9ed53b12 2741 list_move(&anon->d_u.d_child, &dparent->d_subdirs);
770bfad8 2742
31e6b01f
NP
2743 write_seqcount_end(&dentry->d_seq);
2744 write_seqcount_end(&anon->d_seq);
2745
2fd6b7f5
NP
2746 dentry_unlock_parents_for_move(anon, dentry);
2747 spin_unlock(&dentry->d_lock);
2748
2749 /* anon->d_lock still locked, returns locked */
770bfad8
DH
2750 anon->d_flags &= ~DCACHE_DISCONNECTED;
2751}
2752
2753/**
2754 * d_materialise_unique - introduce an inode into the tree
2755 * @dentry: candidate dentry
2756 * @inode: inode to bind to the dentry, to which aliases may be attached
2757 *
2758 * Introduces an dentry into the tree, substituting an extant disconnected
c46c8877
JL
2759 * root directory alias in its place if there is one. Caller must hold the
2760 * i_mutex of the parent directory.
770bfad8
DH
2761 */
2762struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2763{
9eaef27b 2764 struct dentry *actual;
770bfad8
DH
2765
2766 BUG_ON(!d_unhashed(dentry));
2767
770bfad8
DH
2768 if (!inode) {
2769 actual = dentry;
360da900 2770 __d_instantiate(dentry, NULL);
357f8e65
NP
2771 d_rehash(actual);
2772 goto out_nolock;
770bfad8
DH
2773 }
2774
873feea0 2775 spin_lock(&inode->i_lock);
357f8e65 2776
9eaef27b
TM
2777 if (S_ISDIR(inode->i_mode)) {
2778 struct dentry *alias;
2779
2780 /* Does an aliased dentry already exist? */
32ba9c3f 2781 alias = __d_find_alias(inode, 0);
9eaef27b
TM
2782 if (alias) {
2783 actual = alias;
18367501
AV
2784 write_seqlock(&rename_lock);
2785
2786 if (d_ancestor(alias, dentry)) {
2787 /* Check for loops */
2788 actual = ERR_PTR(-ELOOP);
b18dafc8 2789 spin_unlock(&inode->i_lock);
18367501
AV
2790 } else if (IS_ROOT(alias)) {
2791 /* Is this an anonymous mountpoint that we
2792 * could splice into our tree? */
9eaef27b 2793 __d_materialise_dentry(dentry, alias);
18367501 2794 write_sequnlock(&rename_lock);
9eaef27b
TM
2795 __d_drop(alias);
2796 goto found;
18367501
AV
2797 } else {
2798 /* Nope, but we must(!) avoid directory
b18dafc8 2799 * aliasing. This drops inode->i_lock */
18367501 2800 actual = __d_unalias(inode, dentry, alias);
9eaef27b 2801 }
18367501 2802 write_sequnlock(&rename_lock);
dd179946
DH
2803 if (IS_ERR(actual)) {
2804 if (PTR_ERR(actual) == -ELOOP)
2805 pr_warn_ratelimited(
2806 "VFS: Lookup of '%s' in %s %s"
2807 " would have caused loop\n",
2808 dentry->d_name.name,
2809 inode->i_sb->s_type->name,
2810 inode->i_sb->s_id);
9eaef27b 2811 dput(alias);
dd179946 2812 }
9eaef27b
TM
2813 goto out_nolock;
2814 }
770bfad8
DH
2815 }
2816
2817 /* Add a unique reference */
2818 actual = __d_instantiate_unique(dentry, inode);
2819 if (!actual)
2820 actual = dentry;
357f8e65
NP
2821 else
2822 BUG_ON(!d_unhashed(actual));
770bfad8 2823
770bfad8
DH
2824 spin_lock(&actual->d_lock);
2825found:
2826 _d_rehash(actual);
2827 spin_unlock(&actual->d_lock);
873feea0 2828 spin_unlock(&inode->i_lock);
9eaef27b 2829out_nolock:
770bfad8
DH
2830 if (actual == dentry) {
2831 security_d_instantiate(dentry, inode);
2832 return NULL;
2833 }
2834
2835 iput(inode);
2836 return actual;
770bfad8 2837}
ec4f8605 2838EXPORT_SYMBOL_GPL(d_materialise_unique);
770bfad8 2839
cdd16d02 2840static int prepend(char **buffer, int *buflen, const char *str, int namelen)
6092d048
RP
2841{
2842 *buflen -= namelen;
2843 if (*buflen < 0)
2844 return -ENAMETOOLONG;
2845 *buffer -= namelen;
2846 memcpy(*buffer, str, namelen);
2847 return 0;
2848}
2849
232d2d60
WL
2850/**
2851 * prepend_name - prepend a pathname in front of current buffer pointer
18129977
WL
2852 * @buffer: buffer pointer
2853 * @buflen: allocated length of the buffer
2854 * @name: name string and length qstr structure
232d2d60
WL
2855 *
2856 * With RCU path tracing, it may race with d_move(). Use ACCESS_ONCE() to
2857 * make sure that either the old or the new name pointer and length are
2858 * fetched. However, there may be mismatch between length and pointer.
2859 * The length cannot be trusted, we need to copy it byte-by-byte until
2860 * the length is reached or a null byte is found. It also prepends "/" at
2861 * the beginning of the name. The sequence number check at the caller will
2862 * retry it again when a d_move() does happen. So any garbage in the buffer
2863 * due to mismatched pointer and length will be discarded.
2864 */
cdd16d02
MS
2865static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2866{
232d2d60
WL
2867 const char *dname = ACCESS_ONCE(name->name);
2868 u32 dlen = ACCESS_ONCE(name->len);
2869 char *p;
2870
2871 if (*buflen < dlen + 1)
2872 return -ENAMETOOLONG;
2873 *buflen -= dlen + 1;
2874 p = *buffer -= dlen + 1;
2875 *p++ = '/';
2876 while (dlen--) {
2877 char c = *dname++;
2878 if (!c)
2879 break;
2880 *p++ = c;
2881 }
2882 return 0;
cdd16d02
MS
2883}
2884
1da177e4 2885/**
208898c1 2886 * prepend_path - Prepend path string to a buffer
9d1bc601 2887 * @path: the dentry/vfsmount to report
02125a82 2888 * @root: root vfsmnt/dentry
f2eb6575
MS
2889 * @buffer: pointer to the end of the buffer
2890 * @buflen: pointer to buffer length
552ce544 2891 *
18129977
WL
2892 * The function will first try to write out the pathname without taking any
2893 * lock other than the RCU read lock to make sure that dentries won't go away.
2894 * It only checks the sequence number of the global rename_lock as any change
2895 * in the dentry's d_seq will be preceded by changes in the rename_lock
2896 * sequence number. If the sequence number had been changed, it will restart
2897 * the whole pathname back-tracing sequence again by taking the rename_lock.
2898 * In this case, there is no need to take the RCU read lock as the recursive
2899 * parent pointer references will keep the dentry chain alive as long as no
2900 * rename operation is performed.
1da177e4 2901 */
02125a82
AV
2902static int prepend_path(const struct path *path,
2903 const struct path *root,
f2eb6575 2904 char **buffer, int *buflen)
1da177e4 2905{
9d1bc601
MS
2906 struct dentry *dentry = path->dentry;
2907 struct vfsmount *vfsmnt = path->mnt;
0714a533 2908 struct mount *mnt = real_mount(vfsmnt);
f2eb6575 2909 int error = 0;
48a066e7 2910 unsigned seq, m_seq = 0;
232d2d60
WL
2911 char *bptr;
2912 int blen;
6092d048 2913
48f5ec21 2914 rcu_read_lock();
48a066e7
AV
2915restart_mnt:
2916 read_seqbegin_or_lock(&mount_lock, &m_seq);
2917 seq = 0;
232d2d60
WL
2918restart:
2919 bptr = *buffer;
2920 blen = *buflen;
48a066e7 2921 error = 0;
232d2d60 2922 read_seqbegin_or_lock(&rename_lock, &seq);
f2eb6575 2923 while (dentry != root->dentry || vfsmnt != root->mnt) {
1da177e4
LT
2924 struct dentry * parent;
2925
1da177e4 2926 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
48a066e7 2927 struct mount *parent = ACCESS_ONCE(mnt->mnt_parent);
552ce544 2928 /* Global root? */
48a066e7
AV
2929 if (mnt != parent) {
2930 dentry = ACCESS_ONCE(mnt->mnt_mountpoint);
2931 mnt = parent;
232d2d60
WL
2932 vfsmnt = &mnt->mnt;
2933 continue;
2934 }
2935 /*
2936 * Filesystems needing to implement special "root names"
2937 * should do so with ->d_dname()
2938 */
2939 if (IS_ROOT(dentry) &&
2940 (dentry->d_name.len != 1 ||
2941 dentry->d_name.name[0] != '/')) {
2942 WARN(1, "Root dentry has weird name <%.*s>\n",
2943 (int) dentry->d_name.len,
2944 dentry->d_name.name);
2945 }
2946 if (!error)
2947 error = is_mounted(vfsmnt) ? 1 : 2;
2948 break;
1da177e4
LT
2949 }
2950 parent = dentry->d_parent;
2951 prefetch(parent);
232d2d60 2952 error = prepend_name(&bptr, &blen, &dentry->d_name);
f2eb6575
MS
2953 if (error)
2954 break;
2955
1da177e4
LT
2956 dentry = parent;
2957 }
48f5ec21
AV
2958 if (!(seq & 1))
2959 rcu_read_unlock();
2960 if (need_seqretry(&rename_lock, seq)) {
2961 seq = 1;
232d2d60 2962 goto restart;
48f5ec21
AV
2963 }
2964 done_seqretry(&rename_lock, seq);
48a066e7
AV
2965 if (need_seqretry(&mount_lock, m_seq)) {
2966 m_seq = 1;
2967 goto restart_mnt;
2968 }
2969 done_seqretry(&mount_lock, m_seq);
1da177e4 2970
232d2d60
WL
2971 if (error >= 0 && bptr == *buffer) {
2972 if (--blen < 0)
2973 error = -ENAMETOOLONG;
2974 else
2975 *--bptr = '/';
2976 }
2977 *buffer = bptr;
2978 *buflen = blen;
7ea600b5 2979 return error;
f2eb6575 2980}
be285c71 2981
f2eb6575
MS
2982/**
2983 * __d_path - return the path of a dentry
2984 * @path: the dentry/vfsmount to report
02125a82 2985 * @root: root vfsmnt/dentry
cd956a1c 2986 * @buf: buffer to return value in
f2eb6575
MS
2987 * @buflen: buffer length
2988 *
ffd1f4ed 2989 * Convert a dentry into an ASCII path name.
f2eb6575
MS
2990 *
2991 * Returns a pointer into the buffer or an error code if the
2992 * path was too long.
2993 *
be148247 2994 * "buflen" should be positive.
f2eb6575 2995 *
02125a82 2996 * If the path is not reachable from the supplied root, return %NULL.
f2eb6575 2997 */
02125a82
AV
2998char *__d_path(const struct path *path,
2999 const struct path *root,
f2eb6575
MS
3000 char *buf, int buflen)
3001{
3002 char *res = buf + buflen;
3003 int error;
3004
3005 prepend(&res, &buflen, "\0", 1);
f2eb6575 3006 error = prepend_path(path, root, &res, &buflen);
be148247 3007
02125a82
AV
3008 if (error < 0)
3009 return ERR_PTR(error);
3010 if (error > 0)
3011 return NULL;
3012 return res;
3013}
3014
3015char *d_absolute_path(const struct path *path,
3016 char *buf, int buflen)
3017{
3018 struct path root = {};
3019 char *res = buf + buflen;
3020 int error;
3021
3022 prepend(&res, &buflen, "\0", 1);
02125a82 3023 error = prepend_path(path, &root, &res, &buflen);
02125a82
AV
3024
3025 if (error > 1)
3026 error = -EINVAL;
3027 if (error < 0)
f2eb6575 3028 return ERR_PTR(error);
f2eb6575 3029 return res;
1da177e4
LT
3030}
3031
ffd1f4ed
MS
3032/*
3033 * same as __d_path but appends "(deleted)" for unlinked files.
3034 */
02125a82
AV
3035static int path_with_deleted(const struct path *path,
3036 const struct path *root,
3037 char **buf, int *buflen)
ffd1f4ed
MS
3038{
3039 prepend(buf, buflen, "\0", 1);
3040 if (d_unlinked(path->dentry)) {
3041 int error = prepend(buf, buflen, " (deleted)", 10);
3042 if (error)
3043 return error;
3044 }
3045
3046 return prepend_path(path, root, buf, buflen);
3047}
3048
8df9d1a4
MS
3049static int prepend_unreachable(char **buffer, int *buflen)
3050{
3051 return prepend(buffer, buflen, "(unreachable)", 13);
3052}
3053
68f0d9d9
LT
3054static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
3055{
3056 unsigned seq;
3057
3058 do {
3059 seq = read_seqcount_begin(&fs->seq);
3060 *root = fs->root;
3061 } while (read_seqcount_retry(&fs->seq, seq));
3062}
3063
a03a8a70
JB
3064/**
3065 * d_path - return the path of a dentry
cf28b486 3066 * @path: path to report
a03a8a70
JB
3067 * @buf: buffer to return value in
3068 * @buflen: buffer length
3069 *
3070 * Convert a dentry into an ASCII path name. If the entry has been deleted
3071 * the string " (deleted)" is appended. Note that this is ambiguous.
3072 *
52afeefb
AV
3073 * Returns a pointer into the buffer or an error code if the path was
3074 * too long. Note: Callers should use the returned pointer, not the passed
3075 * in buffer, to use the name! The implementation often starts at an offset
3076 * into the buffer, and may leave 0 bytes at the start.
a03a8a70 3077 *
31f3e0b3 3078 * "buflen" should be positive.
a03a8a70 3079 */
20d4fdc1 3080char *d_path(const struct path *path, char *buf, int buflen)
1da177e4 3081{
ffd1f4ed 3082 char *res = buf + buflen;
6ac08c39 3083 struct path root;
ffd1f4ed 3084 int error;
1da177e4 3085
c23fbb6b
ED
3086 /*
3087 * We have various synthetic filesystems that never get mounted. On
3088 * these filesystems dentries are never used for lookup purposes, and
3089 * thus don't need to be hashed. They also don't need a name until a
3090 * user wants to identify the object in /proc/pid/fd/. The little hack
3091 * below allows us to generate a name for these objects on demand:
3092 */
cf28b486
JB
3093 if (path->dentry->d_op && path->dentry->d_op->d_dname)
3094 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
c23fbb6b 3095
68f0d9d9
LT
3096 rcu_read_lock();
3097 get_fs_root_rcu(current->fs, &root);
02125a82 3098 error = path_with_deleted(path, &root, &res, &buflen);
68f0d9d9
LT
3099 rcu_read_unlock();
3100
02125a82 3101 if (error < 0)
ffd1f4ed 3102 res = ERR_PTR(error);
1da177e4
LT
3103 return res;
3104}
ec4f8605 3105EXPORT_SYMBOL(d_path);
1da177e4 3106
c23fbb6b
ED
3107/*
3108 * Helper function for dentry_operations.d_dname() members
3109 */
3110char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
3111 const char *fmt, ...)
3112{
3113 va_list args;
3114 char temp[64];
3115 int sz;
3116
3117 va_start(args, fmt);
3118 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
3119 va_end(args);
3120
3121 if (sz > sizeof(temp) || sz > buflen)
3122 return ERR_PTR(-ENAMETOOLONG);
3123
3124 buffer += buflen - sz;
3125 return memcpy(buffer, temp, sz);
3126}
3127
118b2302
AV
3128char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
3129{
3130 char *end = buffer + buflen;
3131 /* these dentries are never renamed, so d_lock is not needed */
3132 if (prepend(&end, &buflen, " (deleted)", 11) ||
232d2d60 3133 prepend(&end, &buflen, dentry->d_name.name, dentry->d_name.len) ||
118b2302
AV
3134 prepend(&end, &buflen, "/", 1))
3135 end = ERR_PTR(-ENAMETOOLONG);
232d2d60 3136 return end;
118b2302
AV
3137}
3138
6092d048
RP
3139/*
3140 * Write full pathname from the root of the filesystem into the buffer.
3141 */
ec2447c2 3142static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
6092d048 3143{
232d2d60
WL
3144 char *end, *retval;
3145 int len, seq = 0;
3146 int error = 0;
6092d048 3147
48f5ec21 3148 rcu_read_lock();
232d2d60
WL
3149restart:
3150 end = buf + buflen;
3151 len = buflen;
3152 prepend(&end, &len, "\0", 1);
6092d048
RP
3153 if (buflen < 1)
3154 goto Elong;
3155 /* Get '/' right */
3156 retval = end-1;
3157 *retval = '/';
232d2d60 3158 read_seqbegin_or_lock(&rename_lock, &seq);
cdd16d02
MS
3159 while (!IS_ROOT(dentry)) {
3160 struct dentry *parent = dentry->d_parent;
9abca360 3161 int error;
6092d048 3162
6092d048 3163 prefetch(parent);
232d2d60
WL
3164 error = prepend_name(&end, &len, &dentry->d_name);
3165 if (error)
3166 break;
6092d048
RP
3167
3168 retval = end;
3169 dentry = parent;
3170 }
48f5ec21
AV
3171 if (!(seq & 1))
3172 rcu_read_unlock();
3173 if (need_seqretry(&rename_lock, seq)) {
3174 seq = 1;
232d2d60 3175 goto restart;
48f5ec21
AV
3176 }
3177 done_seqretry(&rename_lock, seq);
232d2d60
WL
3178 if (error)
3179 goto Elong;
c103135c
AV
3180 return retval;
3181Elong:
3182 return ERR_PTR(-ENAMETOOLONG);
3183}
ec2447c2
NP
3184
3185char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
3186{
232d2d60 3187 return __dentry_path(dentry, buf, buflen);
ec2447c2
NP
3188}
3189EXPORT_SYMBOL(dentry_path_raw);
c103135c
AV
3190
3191char *dentry_path(struct dentry *dentry, char *buf, int buflen)
3192{
3193 char *p = NULL;
3194 char *retval;
3195
c103135c
AV
3196 if (d_unlinked(dentry)) {
3197 p = buf + buflen;
3198 if (prepend(&p, &buflen, "//deleted", 10) != 0)
3199 goto Elong;
3200 buflen++;
3201 }
3202 retval = __dentry_path(dentry, buf, buflen);
c103135c
AV
3203 if (!IS_ERR(retval) && p)
3204 *p = '/'; /* restore '/' overriden with '\0' */
6092d048
RP
3205 return retval;
3206Elong:
6092d048
RP
3207 return ERR_PTR(-ENAMETOOLONG);
3208}
3209
8b19e341
LT
3210static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
3211 struct path *pwd)
5762482f 3212{
8b19e341
LT
3213 unsigned seq;
3214
3215 do {
3216 seq = read_seqcount_begin(&fs->seq);
3217 *root = fs->root;
3218 *pwd = fs->pwd;
3219 } while (read_seqcount_retry(&fs->seq, seq));
5762482f
LT
3220}
3221
1da177e4
LT
3222/*
3223 * NOTE! The user-level library version returns a
3224 * character pointer. The kernel system call just
3225 * returns the length of the buffer filled (which
3226 * includes the ending '\0' character), or a negative
3227 * error value. So libc would do something like
3228 *
3229 * char *getcwd(char * buf, size_t size)
3230 * {
3231 * int retval;
3232 *
3233 * retval = sys_getcwd(buf, size);
3234 * if (retval >= 0)
3235 * return buf;
3236 * errno = -retval;
3237 * return NULL;
3238 * }
3239 */
3cdad428 3240SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
1da177e4 3241{
552ce544 3242 int error;
6ac08c39 3243 struct path pwd, root;
3272c544 3244 char *page = __getname();
1da177e4
LT
3245
3246 if (!page)
3247 return -ENOMEM;
3248
8b19e341
LT
3249 rcu_read_lock();
3250 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
1da177e4 3251
552ce544 3252 error = -ENOENT;
f3da392e 3253 if (!d_unlinked(pwd.dentry)) {
552ce544 3254 unsigned long len;
3272c544
LT
3255 char *cwd = page + PATH_MAX;
3256 int buflen = PATH_MAX;
1da177e4 3257
8df9d1a4 3258 prepend(&cwd, &buflen, "\0", 1);
02125a82 3259 error = prepend_path(&pwd, &root, &cwd, &buflen);
ff812d72 3260 rcu_read_unlock();
552ce544 3261
02125a82 3262 if (error < 0)
552ce544
LT
3263 goto out;
3264
8df9d1a4 3265 /* Unreachable from current root */
02125a82 3266 if (error > 0) {
8df9d1a4
MS
3267 error = prepend_unreachable(&cwd, &buflen);
3268 if (error)
3269 goto out;
3270 }
3271
552ce544 3272 error = -ERANGE;
3272c544 3273 len = PATH_MAX + page - cwd;
552ce544
LT
3274 if (len <= size) {
3275 error = len;
3276 if (copy_to_user(buf, cwd, len))
3277 error = -EFAULT;
3278 }
949854d0 3279 } else {
ff812d72 3280 rcu_read_unlock();
949854d0 3281 }
1da177e4
LT
3282
3283out:
3272c544 3284 __putname(page);
1da177e4
LT
3285 return error;
3286}
3287
3288/*
3289 * Test whether new_dentry is a subdirectory of old_dentry.
3290 *
3291 * Trivially implemented using the dcache structure
3292 */
3293
3294/**
3295 * is_subdir - is new dentry a subdirectory of old_dentry
3296 * @new_dentry: new dentry
3297 * @old_dentry: old dentry
3298 *
3299 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
3300 * Returns 0 otherwise.
3301 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
3302 */
3303
e2761a11 3304int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
1da177e4
LT
3305{
3306 int result;
949854d0 3307 unsigned seq;
1da177e4 3308
e2761a11
OH
3309 if (new_dentry == old_dentry)
3310 return 1;
3311
e2761a11 3312 do {
1da177e4 3313 /* for restarting inner loop in case of seq retry */
1da177e4 3314 seq = read_seqbegin(&rename_lock);
949854d0
NP
3315 /*
3316 * Need rcu_readlock to protect against the d_parent trashing
3317 * due to d_move
3318 */
3319 rcu_read_lock();
e2761a11 3320 if (d_ancestor(old_dentry, new_dentry))
1da177e4 3321 result = 1;
e2761a11
OH
3322 else
3323 result = 0;
949854d0 3324 rcu_read_unlock();
1da177e4 3325 } while (read_seqretry(&rename_lock, seq));
1da177e4
LT
3326
3327 return result;
3328}
3329
db14fc3a 3330static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
1da177e4 3331{
db14fc3a
MS
3332 struct dentry *root = data;
3333 if (dentry != root) {
3334 if (d_unhashed(dentry) || !dentry->d_inode)
3335 return D_WALK_SKIP;
1da177e4 3336
01ddc4ed
MS
3337 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3338 dentry->d_flags |= DCACHE_GENOCIDE;
3339 dentry->d_lockref.count--;
3340 }
1da177e4 3341 }
db14fc3a
MS
3342 return D_WALK_CONTINUE;
3343}
58db63d0 3344
db14fc3a
MS
3345void d_genocide(struct dentry *parent)
3346{
3347 d_walk(parent, parent, d_genocide_kill, NULL);
1da177e4
LT
3348}
3349
60545d0d 3350void d_tmpfile(struct dentry *dentry, struct inode *inode)
1da177e4 3351{
60545d0d
AV
3352 inode_dec_link_count(inode);
3353 BUG_ON(dentry->d_name.name != dentry->d_iname ||
3354 !hlist_unhashed(&dentry->d_alias) ||
3355 !d_unlinked(dentry));
3356 spin_lock(&dentry->d_parent->d_lock);
3357 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3358 dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3359 (unsigned long long)inode->i_ino);
3360 spin_unlock(&dentry->d_lock);
3361 spin_unlock(&dentry->d_parent->d_lock);
3362 d_instantiate(dentry, inode);
1da177e4 3363}
60545d0d 3364EXPORT_SYMBOL(d_tmpfile);
1da177e4
LT
3365
3366static __initdata unsigned long dhash_entries;
3367static int __init set_dhash_entries(char *str)
3368{
3369 if (!str)
3370 return 0;
3371 dhash_entries = simple_strtoul(str, &str, 0);
3372 return 1;
3373}
3374__setup("dhash_entries=", set_dhash_entries);
3375
3376static void __init dcache_init_early(void)
3377{
074b8517 3378 unsigned int loop;
1da177e4
LT
3379
3380 /* If hashes are distributed across NUMA nodes, defer
3381 * hash allocation until vmalloc space is available.
3382 */
3383 if (hashdist)
3384 return;
3385
3386 dentry_hashtable =
3387 alloc_large_system_hash("Dentry cache",
b07ad996 3388 sizeof(struct hlist_bl_head),
1da177e4
LT
3389 dhash_entries,
3390 13,
3391 HASH_EARLY,
3392 &d_hash_shift,
3393 &d_hash_mask,
31fe62b9 3394 0,
1da177e4
LT
3395 0);
3396
074b8517 3397 for (loop = 0; loop < (1U << d_hash_shift); loop++)
b07ad996 3398 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
1da177e4
LT
3399}
3400
74bf17cf 3401static void __init dcache_init(void)
1da177e4 3402{
074b8517 3403 unsigned int loop;
1da177e4
LT
3404
3405 /*
3406 * A constructor could be added for stable state like the lists,
3407 * but it is probably not worth it because of the cache nature
3408 * of the dcache.
3409 */
0a31bd5f
CL
3410 dentry_cache = KMEM_CACHE(dentry,
3411 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
1da177e4
LT
3412
3413 /* Hash may have been set up in dcache_init_early */
3414 if (!hashdist)
3415 return;
3416
3417 dentry_hashtable =
3418 alloc_large_system_hash("Dentry cache",
b07ad996 3419 sizeof(struct hlist_bl_head),
1da177e4
LT
3420 dhash_entries,
3421 13,
3422 0,
3423 &d_hash_shift,
3424 &d_hash_mask,
31fe62b9 3425 0,
1da177e4
LT
3426 0);
3427
074b8517 3428 for (loop = 0; loop < (1U << d_hash_shift); loop++)
b07ad996 3429 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
1da177e4
LT
3430}
3431
3432/* SLAB cache for __getname() consumers */
e18b890b 3433struct kmem_cache *names_cachep __read_mostly;
ec4f8605 3434EXPORT_SYMBOL(names_cachep);
1da177e4 3435
1da177e4
LT
3436EXPORT_SYMBOL(d_genocide);
3437
1da177e4
LT
3438void __init vfs_caches_init_early(void)
3439{
3440 dcache_init_early();
3441 inode_init_early();
3442}
3443
3444void __init vfs_caches_init(unsigned long mempages)
3445{
3446 unsigned long reserve;
3447
3448 /* Base hash sizes on available memory, with a reserve equal to
3449 150% of current kernel size */
3450
3451 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
3452 mempages -= reserve;
3453
3454 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
20c2df83 3455 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
1da177e4 3456
74bf17cf
DC
3457 dcache_init();
3458 inode_init();
1da177e4 3459 files_init(mempages);
74bf17cf 3460 mnt_init();
1da177e4
LT
3461 bdev_cache_init();
3462 chrdev_init();
3463}