tracing: Have preempt(irqs)off trace preempt disabled functions
[linux/fpc-iii.git] / fs / dcache.c
blobf9dcd166d878435a050f8574cf6900b46293ea3f
1 /*
2 * fs/dcache.c
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
7 */
9 /*
10 * Notes on the allocation strategy:
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.
17 #include <linux/syscalls.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/fs.h>
21 #include <linux/fsnotify.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/hash.h>
25 #include <linux/cache.h>
26 #include <linux/export.h>
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>
34 #include <linux/fs_struct.h>
35 #include <linux/hardirq.h>
36 #include <linux/bit_spinlock.h>
37 #include <linux/rculist_bl.h>
38 #include <linux/prefetch.h>
39 #include <linux/ratelimit.h>
40 #include <linux/list_lru.h>
41 #include "internal.h"
42 #include "mount.h"
45 * Usage:
46 * dcache->d_inode->i_lock protects:
47 * - i_dentry, d_u.d_alias, d_inode of aliases
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)
52 * dentry->d_sb->s_dentry_lru_lock protects:
53 * - the dcache lru lists and counters
54 * d_lock protects:
55 * - d_flags
56 * - d_name
57 * - d_lru
58 * - d_count
59 * - d_unhashed()
60 * - d_parent and d_subdirs
61 * - childrens' d_child and d_parent
62 * - d_u.d_alias, d_inode
64 * Ordering:
65 * dentry->d_inode->i_lock
66 * dentry->d_lock
67 * dentry->d_sb->s_dentry_lru_lock
68 * dcache_hash_bucket lock
69 * s_anon lock
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
77 * If no ancestor relationship:
78 * if (dentry1 < dentry2)
79 * dentry1->d_lock
80 * dentry2->d_lock
82 int sysctl_vfs_cache_pressure __read_mostly = 100;
83 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
85 __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
87 EXPORT_SYMBOL(rename_lock);
89 static struct kmem_cache *dentry_cache __read_mostly;
92 * This is the single most critical data structure when it comes
93 * to the dcache: the hashtable for lookups. Somebody should try
94 * to make this good - I've just made it work.
96 * This hash-function tries to avoid losing too many bits of hash
97 * information, yet avoid using a prime hash-size or similar.
100 static unsigned int d_hash_mask __read_mostly;
101 static unsigned int d_hash_shift __read_mostly;
103 static struct hlist_bl_head *dentry_hashtable __read_mostly;
105 static inline struct hlist_bl_head *d_hash(const struct dentry *parent,
106 unsigned int hash)
108 hash += (unsigned long) parent / L1_CACHE_BYTES;
109 return dentry_hashtable + hash_32(hash, d_hash_shift);
112 /* Statistics gathering. */
113 struct dentry_stat_t dentry_stat = {
114 .age_limit = 45,
117 static DEFINE_PER_CPU(long, nr_dentry);
118 static DEFINE_PER_CPU(long, nr_dentry_unused);
120 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
123 * Here we resort to our own counters instead of using generic per-cpu counters
124 * for consistency with what the vfs inode code does. We are expected to harvest
125 * better code and performance by having our own specialized counters.
127 * Please note that the loop is done over all possible CPUs, not over all online
128 * CPUs. The reason for this is that we don't want to play games with CPUs going
129 * on and off. If one of them goes off, we will just keep their counters.
131 * glommer: See cffbc8a for details, and if you ever intend to change this,
132 * please update all vfs counters to match.
134 static long get_nr_dentry(void)
136 int i;
137 long sum = 0;
138 for_each_possible_cpu(i)
139 sum += per_cpu(nr_dentry, i);
140 return sum < 0 ? 0 : sum;
143 static long get_nr_dentry_unused(void)
145 int i;
146 long sum = 0;
147 for_each_possible_cpu(i)
148 sum += per_cpu(nr_dentry_unused, i);
149 return sum < 0 ? 0 : sum;
152 int proc_nr_dentry(struct ctl_table *table, int write, void __user *buffer,
153 size_t *lenp, loff_t *ppos)
155 dentry_stat.nr_dentry = get_nr_dentry();
156 dentry_stat.nr_unused = get_nr_dentry_unused();
157 return proc_doulongvec_minmax(table, write, buffer, lenp, ppos);
159 #endif
162 * Compare 2 name strings, return 0 if they match, otherwise non-zero.
163 * The strings are both count bytes long, and count is non-zero.
165 #ifdef CONFIG_DCACHE_WORD_ACCESS
167 #include <asm/word-at-a-time.h>
169 * NOTE! 'cs' and 'scount' come from a dentry, so it has a
170 * aligned allocation for this particular component. We don't
171 * strictly need the load_unaligned_zeropad() safety, but it
172 * doesn't hurt either.
174 * In contrast, 'ct' and 'tcount' can be from a pathname, and do
175 * need the careful unaligned handling.
177 static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
179 unsigned long a,b,mask;
181 for (;;) {
182 a = *(unsigned long *)cs;
183 b = load_unaligned_zeropad(ct);
184 if (tcount < sizeof(unsigned long))
185 break;
186 if (unlikely(a != b))
187 return 1;
188 cs += sizeof(unsigned long);
189 ct += sizeof(unsigned long);
190 tcount -= sizeof(unsigned long);
191 if (!tcount)
192 return 0;
194 mask = bytemask_from_count(tcount);
195 return unlikely(!!((a ^ b) & mask));
198 #else
200 static inline int dentry_string_cmp(const unsigned char *cs, const unsigned char *ct, unsigned tcount)
202 do {
203 if (*cs != *ct)
204 return 1;
205 cs++;
206 ct++;
207 tcount--;
208 } while (tcount);
209 return 0;
212 #endif
214 static inline int dentry_cmp(const struct dentry *dentry, const unsigned char *ct, unsigned tcount)
216 const unsigned char *cs;
218 * Be careful about RCU walk racing with rename:
219 * use ACCESS_ONCE to fetch the name pointer.
221 * NOTE! Even if a rename will mean that the length
222 * was not loaded atomically, we don't care. The
223 * RCU walk will check the sequence count eventually,
224 * and catch it. And we won't overrun the buffer,
225 * because we're reading the name pointer atomically,
226 * and a dentry name is guaranteed to be properly
227 * terminated with a NUL byte.
229 * End result: even if 'len' is wrong, we'll exit
230 * early because the data cannot match (there can
231 * be no NUL in the ct/tcount data)
233 cs = ACCESS_ONCE(dentry->d_name.name);
234 smp_read_barrier_depends();
235 return dentry_string_cmp(cs, ct, tcount);
238 static void __d_free(struct rcu_head *head)
240 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
242 if (dname_external(dentry))
243 kfree(dentry->d_name.name);
244 kmem_cache_free(dentry_cache, dentry);
247 static void dentry_free(struct dentry *dentry)
249 WARN_ON(!hlist_unhashed(&dentry->d_u.d_alias));
251 /* if dentry was never visible to RCU, immediate free is OK */
252 if (!(dentry->d_flags & DCACHE_RCUACCESS))
253 __d_free(&dentry->d_u.d_rcu);
254 else
255 call_rcu(&dentry->d_u.d_rcu, __d_free);
259 * dentry_rcuwalk_barrier - invalidate in-progress rcu-walk lookups
260 * @dentry: the target dentry
261 * After this call, in-progress rcu-walk path lookup will fail. This
262 * should be called after unhashing, and after changing d_inode (if
263 * the dentry has not already been unhashed).
265 static inline void dentry_rcuwalk_barrier(struct dentry *dentry)
267 assert_spin_locked(&dentry->d_lock);
268 /* Go through a barrier */
269 write_seqcount_barrier(&dentry->d_seq);
273 * Release the dentry's inode, using the filesystem
274 * d_iput() operation if defined. Dentry has no refcount
275 * and is unhashed.
277 static void dentry_iput(struct dentry * dentry)
278 __releases(dentry->d_lock)
279 __releases(dentry->d_inode->i_lock)
281 struct inode *inode = dentry->d_inode;
282 if (inode) {
283 dentry->d_inode = NULL;
284 hlist_del_init(&dentry->d_u.d_alias);
285 spin_unlock(&dentry->d_lock);
286 spin_unlock(&inode->i_lock);
287 if (!inode->i_nlink)
288 fsnotify_inoderemove(inode);
289 if (dentry->d_op && dentry->d_op->d_iput)
290 dentry->d_op->d_iput(dentry, inode);
291 else
292 iput(inode);
293 } else {
294 spin_unlock(&dentry->d_lock);
299 * Release the dentry's inode, using the filesystem
300 * d_iput() operation if defined. dentry remains in-use.
302 static void dentry_unlink_inode(struct dentry * dentry)
303 __releases(dentry->d_lock)
304 __releases(dentry->d_inode->i_lock)
306 struct inode *inode = dentry->d_inode;
307 __d_clear_type(dentry);
308 dentry->d_inode = NULL;
309 hlist_del_init(&dentry->d_u.d_alias);
310 dentry_rcuwalk_barrier(dentry);
311 spin_unlock(&dentry->d_lock);
312 spin_unlock(&inode->i_lock);
313 if (!inode->i_nlink)
314 fsnotify_inoderemove(inode);
315 if (dentry->d_op && dentry->d_op->d_iput)
316 dentry->d_op->d_iput(dentry, inode);
317 else
318 iput(inode);
322 * The DCACHE_LRU_LIST bit is set whenever the 'd_lru' entry
323 * is in use - which includes both the "real" per-superblock
324 * LRU list _and_ the DCACHE_SHRINK_LIST use.
326 * The DCACHE_SHRINK_LIST bit is set whenever the dentry is
327 * on the shrink list (ie not on the superblock LRU list).
329 * The per-cpu "nr_dentry_unused" counters are updated with
330 * the DCACHE_LRU_LIST bit.
332 * These helper functions make sure we always follow the
333 * rules. d_lock must be held by the caller.
335 #define D_FLAG_VERIFY(dentry,x) WARN_ON_ONCE(((dentry)->d_flags & (DCACHE_LRU_LIST | DCACHE_SHRINK_LIST)) != (x))
336 static void d_lru_add(struct dentry *dentry)
338 D_FLAG_VERIFY(dentry, 0);
339 dentry->d_flags |= DCACHE_LRU_LIST;
340 this_cpu_inc(nr_dentry_unused);
341 WARN_ON_ONCE(!list_lru_add(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
344 static void d_lru_del(struct dentry *dentry)
346 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
347 dentry->d_flags &= ~DCACHE_LRU_LIST;
348 this_cpu_dec(nr_dentry_unused);
349 WARN_ON_ONCE(!list_lru_del(&dentry->d_sb->s_dentry_lru, &dentry->d_lru));
352 static void d_shrink_del(struct dentry *dentry)
354 D_FLAG_VERIFY(dentry, DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
355 list_del_init(&dentry->d_lru);
356 dentry->d_flags &= ~(DCACHE_SHRINK_LIST | DCACHE_LRU_LIST);
357 this_cpu_dec(nr_dentry_unused);
360 static void d_shrink_add(struct dentry *dentry, struct list_head *list)
362 D_FLAG_VERIFY(dentry, 0);
363 list_add(&dentry->d_lru, list);
364 dentry->d_flags |= DCACHE_SHRINK_LIST | DCACHE_LRU_LIST;
365 this_cpu_inc(nr_dentry_unused);
369 * These can only be called under the global LRU lock, ie during the
370 * callback for freeing the LRU list. "isolate" removes it from the
371 * LRU lists entirely, while shrink_move moves it to the indicated
372 * private list.
374 static void d_lru_isolate(struct dentry *dentry)
376 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
377 dentry->d_flags &= ~DCACHE_LRU_LIST;
378 this_cpu_dec(nr_dentry_unused);
379 list_del_init(&dentry->d_lru);
382 static void d_lru_shrink_move(struct dentry *dentry, struct list_head *list)
384 D_FLAG_VERIFY(dentry, DCACHE_LRU_LIST);
385 dentry->d_flags |= DCACHE_SHRINK_LIST;
386 list_move_tail(&dentry->d_lru, list);
390 * dentry_lru_(add|del)_list) must be called with d_lock held.
392 static void dentry_lru_add(struct dentry *dentry)
394 if (unlikely(!(dentry->d_flags & DCACHE_LRU_LIST)))
395 d_lru_add(dentry);
399 * d_drop - drop a dentry
400 * @dentry: dentry to drop
402 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
403 * be found through a VFS lookup any more. Note that this is different from
404 * deleting the dentry - d_delete will try to mark the dentry negative if
405 * possible, giving a successful _negative_ lookup, while d_drop will
406 * just make the cache lookup fail.
408 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
409 * reason (NFS timeouts or autofs deletes).
411 * __d_drop requires dentry->d_lock.
413 void __d_drop(struct dentry *dentry)
415 if (!d_unhashed(dentry)) {
416 struct hlist_bl_head *b;
418 * Hashed dentries are normally on the dentry hashtable,
419 * with the exception of those newly allocated by
420 * d_obtain_alias, which are always IS_ROOT:
422 if (unlikely(IS_ROOT(dentry)))
423 b = &dentry->d_sb->s_anon;
424 else
425 b = d_hash(dentry->d_parent, dentry->d_name.hash);
427 hlist_bl_lock(b);
428 __hlist_bl_del(&dentry->d_hash);
429 dentry->d_hash.pprev = NULL;
430 hlist_bl_unlock(b);
431 dentry_rcuwalk_barrier(dentry);
434 EXPORT_SYMBOL(__d_drop);
436 void d_drop(struct dentry *dentry)
438 spin_lock(&dentry->d_lock);
439 __d_drop(dentry);
440 spin_unlock(&dentry->d_lock);
442 EXPORT_SYMBOL(d_drop);
444 static void __dentry_kill(struct dentry *dentry)
446 struct dentry *parent = NULL;
447 bool can_free = true;
448 if (!IS_ROOT(dentry))
449 parent = dentry->d_parent;
452 * The dentry is now unrecoverably dead to the world.
454 lockref_mark_dead(&dentry->d_lockref);
457 * inform the fs via d_prune that this dentry is about to be
458 * unhashed and destroyed.
460 if ((dentry->d_flags & DCACHE_OP_PRUNE) && !d_unhashed(dentry))
461 dentry->d_op->d_prune(dentry);
463 if (dentry->d_flags & DCACHE_LRU_LIST) {
464 if (!(dentry->d_flags & DCACHE_SHRINK_LIST))
465 d_lru_del(dentry);
467 /* if it was on the hash then remove it */
468 __d_drop(dentry);
469 __list_del_entry(&dentry->d_child);
471 * Inform d_walk() that we are no longer attached to the
472 * dentry tree
474 dentry->d_flags |= DCACHE_DENTRY_KILLED;
475 if (parent)
476 spin_unlock(&parent->d_lock);
477 dentry_iput(dentry);
479 * dentry_iput drops the locks, at which point nobody (except
480 * transient RCU lookups) can reach this dentry.
482 BUG_ON((int)dentry->d_lockref.count > 0);
483 this_cpu_dec(nr_dentry);
484 if (dentry->d_op && dentry->d_op->d_release)
485 dentry->d_op->d_release(dentry);
487 spin_lock(&dentry->d_lock);
488 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
489 dentry->d_flags |= DCACHE_MAY_FREE;
490 can_free = false;
492 spin_unlock(&dentry->d_lock);
493 if (likely(can_free))
494 dentry_free(dentry);
498 * Finish off a dentry we've decided to kill.
499 * dentry->d_lock must be held, returns with it unlocked.
500 * If ref is non-zero, then decrement the refcount too.
501 * Returns dentry requiring refcount drop, or NULL if we're done.
503 static struct dentry *dentry_kill(struct dentry *dentry)
504 __releases(dentry->d_lock)
506 struct inode *inode = dentry->d_inode;
507 struct dentry *parent = NULL;
509 if (inode && unlikely(!spin_trylock(&inode->i_lock)))
510 goto failed;
512 if (!IS_ROOT(dentry)) {
513 parent = dentry->d_parent;
514 if (unlikely(!spin_trylock(&parent->d_lock))) {
515 if (inode)
516 spin_unlock(&inode->i_lock);
517 goto failed;
521 __dentry_kill(dentry);
522 return parent;
524 failed:
525 spin_unlock(&dentry->d_lock);
526 cpu_relax();
527 return dentry; /* try again with same dentry */
530 static inline struct dentry *lock_parent(struct dentry *dentry)
532 struct dentry *parent = dentry->d_parent;
533 if (IS_ROOT(dentry))
534 return NULL;
535 if (unlikely((int)dentry->d_lockref.count < 0))
536 return NULL;
537 if (likely(spin_trylock(&parent->d_lock)))
538 return parent;
539 rcu_read_lock();
540 spin_unlock(&dentry->d_lock);
541 again:
542 parent = ACCESS_ONCE(dentry->d_parent);
543 spin_lock(&parent->d_lock);
545 * We can't blindly lock dentry until we are sure
546 * that we won't violate the locking order.
547 * Any changes of dentry->d_parent must have
548 * been done with parent->d_lock held, so
549 * spin_lock() above is enough of a barrier
550 * for checking if it's still our child.
552 if (unlikely(parent != dentry->d_parent)) {
553 spin_unlock(&parent->d_lock);
554 goto again;
556 rcu_read_unlock();
557 if (parent != dentry)
558 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
559 else
560 parent = NULL;
561 return parent;
565 * This is dput
567 * This is complicated by the fact that we do not want to put
568 * dentries that are no longer on any hash chain on the unused
569 * list: we'd much rather just get rid of them immediately.
571 * However, that implies that we have to traverse the dentry
572 * tree upwards to the parents which might _also_ now be
573 * scheduled for deletion (it may have been only waiting for
574 * its last child to go away).
576 * This tail recursion is done by hand as we don't want to depend
577 * on the compiler to always get this right (gcc generally doesn't).
578 * Real recursion would eat up our stack space.
582 * dput - release a dentry
583 * @dentry: dentry to release
585 * Release a dentry. This will drop the usage count and if appropriate
586 * call the dentry unlink method as well as removing it from the queues and
587 * releasing its resources. If the parent dentries were scheduled for release
588 * they too may now get deleted.
590 void dput(struct dentry *dentry)
592 if (unlikely(!dentry))
593 return;
595 repeat:
596 if (lockref_put_or_lock(&dentry->d_lockref))
597 return;
599 /* Unreachable? Get rid of it */
600 if (unlikely(d_unhashed(dentry)))
601 goto kill_it;
603 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
604 goto kill_it;
606 if (unlikely(dentry->d_flags & DCACHE_OP_DELETE)) {
607 if (dentry->d_op->d_delete(dentry))
608 goto kill_it;
611 if (!(dentry->d_flags & DCACHE_REFERENCED))
612 dentry->d_flags |= DCACHE_REFERENCED;
613 dentry_lru_add(dentry);
615 dentry->d_lockref.count--;
616 spin_unlock(&dentry->d_lock);
617 return;
619 kill_it:
620 dentry = dentry_kill(dentry);
621 if (dentry)
622 goto repeat;
624 EXPORT_SYMBOL(dput);
627 * d_invalidate - invalidate a dentry
628 * @dentry: dentry to invalidate
630 * Try to invalidate the dentry if it turns out to be
631 * possible. If there are other dentries that can be
632 * reached through this one we can't delete it and we
633 * return -EBUSY. On success we return 0.
635 * no dcache lock.
638 int d_invalidate(struct dentry * dentry)
641 * If it's already been dropped, return OK.
643 spin_lock(&dentry->d_lock);
644 if (d_unhashed(dentry)) {
645 spin_unlock(&dentry->d_lock);
646 return 0;
649 * Check whether to do a partial shrink_dcache
650 * to get rid of unused child entries.
652 if (!list_empty(&dentry->d_subdirs)) {
653 spin_unlock(&dentry->d_lock);
654 shrink_dcache_parent(dentry);
655 spin_lock(&dentry->d_lock);
659 * Somebody else still using it?
661 * If it's a directory, we can't drop it
662 * for fear of somebody re-populating it
663 * with children (even though dropping it
664 * would make it unreachable from the root,
665 * we might still populate it if it was a
666 * working directory or similar).
667 * We also need to leave mountpoints alone,
668 * directory or not.
670 if (dentry->d_lockref.count > 1 && dentry->d_inode) {
671 if (S_ISDIR(dentry->d_inode->i_mode) || d_mountpoint(dentry)) {
672 spin_unlock(&dentry->d_lock);
673 return -EBUSY;
677 __d_drop(dentry);
678 spin_unlock(&dentry->d_lock);
679 return 0;
681 EXPORT_SYMBOL(d_invalidate);
683 /* This must be called with d_lock held */
684 static inline void __dget_dlock(struct dentry *dentry)
686 dentry->d_lockref.count++;
689 static inline void __dget(struct dentry *dentry)
691 lockref_get(&dentry->d_lockref);
694 struct dentry *dget_parent(struct dentry *dentry)
696 int gotref;
697 struct dentry *ret;
700 * Do optimistic parent lookup without any
701 * locking.
703 rcu_read_lock();
704 ret = ACCESS_ONCE(dentry->d_parent);
705 gotref = lockref_get_not_zero(&ret->d_lockref);
706 rcu_read_unlock();
707 if (likely(gotref)) {
708 if (likely(ret == ACCESS_ONCE(dentry->d_parent)))
709 return ret;
710 dput(ret);
713 repeat:
715 * Don't need rcu_dereference because we re-check it was correct under
716 * the lock.
718 rcu_read_lock();
719 ret = dentry->d_parent;
720 spin_lock(&ret->d_lock);
721 if (unlikely(ret != dentry->d_parent)) {
722 spin_unlock(&ret->d_lock);
723 rcu_read_unlock();
724 goto repeat;
726 rcu_read_unlock();
727 BUG_ON(!ret->d_lockref.count);
728 ret->d_lockref.count++;
729 spin_unlock(&ret->d_lock);
730 return ret;
732 EXPORT_SYMBOL(dget_parent);
735 * d_find_alias - grab a hashed alias of inode
736 * @inode: inode in question
737 * @want_discon: flag, used by d_splice_alias, to request
738 * that only a DISCONNECTED alias be returned.
740 * If inode has a hashed alias, or is a directory and has any alias,
741 * acquire the reference to alias and return it. Otherwise return NULL.
742 * Notice that if inode is a directory there can be only one alias and
743 * it can be unhashed only if it has no children, or if it is the root
744 * of a filesystem.
746 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
747 * any other hashed alias over that one unless @want_discon is set,
748 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
750 static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
752 struct dentry *alias, *discon_alias;
754 again:
755 discon_alias = NULL;
756 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
757 spin_lock(&alias->d_lock);
758 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
759 if (IS_ROOT(alias) &&
760 (alias->d_flags & DCACHE_DISCONNECTED)) {
761 discon_alias = alias;
762 } else if (!want_discon) {
763 __dget_dlock(alias);
764 spin_unlock(&alias->d_lock);
765 return alias;
768 spin_unlock(&alias->d_lock);
770 if (discon_alias) {
771 alias = discon_alias;
772 spin_lock(&alias->d_lock);
773 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
774 if (IS_ROOT(alias) &&
775 (alias->d_flags & DCACHE_DISCONNECTED)) {
776 __dget_dlock(alias);
777 spin_unlock(&alias->d_lock);
778 return alias;
781 spin_unlock(&alias->d_lock);
782 goto again;
784 return NULL;
787 struct dentry *d_find_alias(struct inode *inode)
789 struct dentry *de = NULL;
791 if (!hlist_empty(&inode->i_dentry)) {
792 spin_lock(&inode->i_lock);
793 de = __d_find_alias(inode, 0);
794 spin_unlock(&inode->i_lock);
796 return de;
798 EXPORT_SYMBOL(d_find_alias);
801 * Try to kill dentries associated with this inode.
802 * WARNING: you must own a reference to inode.
804 void d_prune_aliases(struct inode *inode)
806 struct dentry *dentry;
807 restart:
808 spin_lock(&inode->i_lock);
809 hlist_for_each_entry(dentry, &inode->i_dentry, d_u.d_alias) {
810 spin_lock(&dentry->d_lock);
811 if (!dentry->d_lockref.count) {
813 * inform the fs via d_prune that this dentry
814 * is about to be unhashed and destroyed.
816 if ((dentry->d_flags & DCACHE_OP_PRUNE) &&
817 !d_unhashed(dentry))
818 dentry->d_op->d_prune(dentry);
820 __dget_dlock(dentry);
821 __d_drop(dentry);
822 spin_unlock(&dentry->d_lock);
823 spin_unlock(&inode->i_lock);
824 dput(dentry);
825 goto restart;
827 spin_unlock(&dentry->d_lock);
829 spin_unlock(&inode->i_lock);
831 EXPORT_SYMBOL(d_prune_aliases);
833 static void shrink_dentry_list(struct list_head *list)
835 struct dentry *dentry, *parent;
837 while (!list_empty(list)) {
838 struct inode *inode;
839 dentry = list_entry(list->prev, struct dentry, d_lru);
840 spin_lock(&dentry->d_lock);
841 parent = lock_parent(dentry);
844 * The dispose list is isolated and dentries are not accounted
845 * to the LRU here, so we can simply remove it from the list
846 * here regardless of whether it is referenced or not.
848 d_shrink_del(dentry);
851 * We found an inuse dentry which was not removed from
852 * the LRU because of laziness during lookup. Do not free it.
854 if ((int)dentry->d_lockref.count > 0) {
855 spin_unlock(&dentry->d_lock);
856 if (parent)
857 spin_unlock(&parent->d_lock);
858 continue;
862 if (unlikely(dentry->d_flags & DCACHE_DENTRY_KILLED)) {
863 bool can_free = dentry->d_flags & DCACHE_MAY_FREE;
864 spin_unlock(&dentry->d_lock);
865 if (parent)
866 spin_unlock(&parent->d_lock);
867 if (can_free)
868 dentry_free(dentry);
869 continue;
872 inode = dentry->d_inode;
873 if (inode && unlikely(!spin_trylock(&inode->i_lock))) {
874 d_shrink_add(dentry, list);
875 spin_unlock(&dentry->d_lock);
876 if (parent)
877 spin_unlock(&parent->d_lock);
878 continue;
881 __dentry_kill(dentry);
884 * We need to prune ancestors too. This is necessary to prevent
885 * quadratic behavior of shrink_dcache_parent(), but is also
886 * expected to be beneficial in reducing dentry cache
887 * fragmentation.
889 dentry = parent;
890 while (dentry && !lockref_put_or_lock(&dentry->d_lockref)) {
891 parent = lock_parent(dentry);
892 if (dentry->d_lockref.count != 1) {
893 dentry->d_lockref.count--;
894 spin_unlock(&dentry->d_lock);
895 if (parent)
896 spin_unlock(&parent->d_lock);
897 break;
899 inode = dentry->d_inode; /* can't be NULL */
900 if (unlikely(!spin_trylock(&inode->i_lock))) {
901 spin_unlock(&dentry->d_lock);
902 if (parent)
903 spin_unlock(&parent->d_lock);
904 cpu_relax();
905 continue;
907 __dentry_kill(dentry);
908 dentry = parent;
913 static enum lru_status
914 dentry_lru_isolate(struct list_head *item, spinlock_t *lru_lock, void *arg)
916 struct list_head *freeable = arg;
917 struct dentry *dentry = container_of(item, struct dentry, d_lru);
921 * we are inverting the lru lock/dentry->d_lock here,
922 * so use a trylock. If we fail to get the lock, just skip
923 * it
925 if (!spin_trylock(&dentry->d_lock))
926 return LRU_SKIP;
929 * Referenced dentries are still in use. If they have active
930 * counts, just remove them from the LRU. Otherwise give them
931 * another pass through the LRU.
933 if (dentry->d_lockref.count) {
934 d_lru_isolate(dentry);
935 spin_unlock(&dentry->d_lock);
936 return LRU_REMOVED;
939 if (dentry->d_flags & DCACHE_REFERENCED) {
940 dentry->d_flags &= ~DCACHE_REFERENCED;
941 spin_unlock(&dentry->d_lock);
944 * The list move itself will be made by the common LRU code. At
945 * this point, we've dropped the dentry->d_lock but keep the
946 * lru lock. This is safe to do, since every list movement is
947 * protected by the lru lock even if both locks are held.
949 * This is guaranteed by the fact that all LRU management
950 * functions are intermediated by the LRU API calls like
951 * list_lru_add and list_lru_del. List movement in this file
952 * only ever occur through this functions or through callbacks
953 * like this one, that are called from the LRU API.
955 * The only exceptions to this are functions like
956 * shrink_dentry_list, and code that first checks for the
957 * DCACHE_SHRINK_LIST flag. Those are guaranteed to be
958 * operating only with stack provided lists after they are
959 * properly isolated from the main list. It is thus, always a
960 * local access.
962 return LRU_ROTATE;
965 d_lru_shrink_move(dentry, freeable);
966 spin_unlock(&dentry->d_lock);
968 return LRU_REMOVED;
972 * prune_dcache_sb - shrink the dcache
973 * @sb: superblock
974 * @nr_to_scan : number of entries to try to free
975 * @nid: which node to scan for freeable entities
977 * Attempt to shrink the superblock dcache LRU by @nr_to_scan entries. This is
978 * done when we need more memory an called from the superblock shrinker
979 * function.
981 * This function may fail to free any resources if all the dentries are in
982 * use.
984 long prune_dcache_sb(struct super_block *sb, unsigned long nr_to_scan,
985 int nid)
987 LIST_HEAD(dispose);
988 long freed;
990 freed = list_lru_walk_node(&sb->s_dentry_lru, nid, dentry_lru_isolate,
991 &dispose, &nr_to_scan);
992 shrink_dentry_list(&dispose);
993 return freed;
996 static enum lru_status dentry_lru_isolate_shrink(struct list_head *item,
997 spinlock_t *lru_lock, void *arg)
999 struct list_head *freeable = arg;
1000 struct dentry *dentry = container_of(item, struct dentry, d_lru);
1003 * we are inverting the lru lock/dentry->d_lock here,
1004 * so use a trylock. If we fail to get the lock, just skip
1005 * it
1007 if (!spin_trylock(&dentry->d_lock))
1008 return LRU_SKIP;
1010 d_lru_shrink_move(dentry, freeable);
1011 spin_unlock(&dentry->d_lock);
1013 return LRU_REMOVED;
1018 * shrink_dcache_sb - shrink dcache for a superblock
1019 * @sb: superblock
1021 * Shrink the dcache for the specified super block. This is used to free
1022 * the dcache before unmounting a file system.
1024 void shrink_dcache_sb(struct super_block *sb)
1026 long freed;
1028 do {
1029 LIST_HEAD(dispose);
1031 freed = list_lru_walk(&sb->s_dentry_lru,
1032 dentry_lru_isolate_shrink, &dispose, UINT_MAX);
1034 this_cpu_sub(nr_dentry_unused, freed);
1035 shrink_dentry_list(&dispose);
1036 } while (freed > 0);
1038 EXPORT_SYMBOL(shrink_dcache_sb);
1041 * enum d_walk_ret - action to talke during tree walk
1042 * @D_WALK_CONTINUE: contrinue walk
1043 * @D_WALK_QUIT: quit walk
1044 * @D_WALK_NORETRY: quit when retry is needed
1045 * @D_WALK_SKIP: skip this dentry and its children
1047 enum d_walk_ret {
1048 D_WALK_CONTINUE,
1049 D_WALK_QUIT,
1050 D_WALK_NORETRY,
1051 D_WALK_SKIP,
1055 * d_walk - walk the dentry tree
1056 * @parent: start of walk
1057 * @data: data passed to @enter() and @finish()
1058 * @enter: callback when first entering the dentry
1059 * @finish: callback when successfully finished the walk
1061 * The @enter() and @finish() callbacks are called with d_lock held.
1063 static void d_walk(struct dentry *parent, void *data,
1064 enum d_walk_ret (*enter)(void *, struct dentry *),
1065 void (*finish)(void *))
1067 struct dentry *this_parent;
1068 struct list_head *next;
1069 unsigned seq = 0;
1070 enum d_walk_ret ret;
1071 bool retry = true;
1073 again:
1074 read_seqbegin_or_lock(&rename_lock, &seq);
1075 this_parent = parent;
1076 spin_lock(&this_parent->d_lock);
1078 ret = enter(data, this_parent);
1079 switch (ret) {
1080 case D_WALK_CONTINUE:
1081 break;
1082 case D_WALK_QUIT:
1083 case D_WALK_SKIP:
1084 goto out_unlock;
1085 case D_WALK_NORETRY:
1086 retry = false;
1087 break;
1089 repeat:
1090 next = this_parent->d_subdirs.next;
1091 resume:
1092 while (next != &this_parent->d_subdirs) {
1093 struct list_head *tmp = next;
1094 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1095 next = tmp->next;
1097 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1099 ret = enter(data, dentry);
1100 switch (ret) {
1101 case D_WALK_CONTINUE:
1102 break;
1103 case D_WALK_QUIT:
1104 spin_unlock(&dentry->d_lock);
1105 goto out_unlock;
1106 case D_WALK_NORETRY:
1107 retry = false;
1108 break;
1109 case D_WALK_SKIP:
1110 spin_unlock(&dentry->d_lock);
1111 continue;
1114 if (!list_empty(&dentry->d_subdirs)) {
1115 spin_unlock(&this_parent->d_lock);
1116 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
1117 this_parent = dentry;
1118 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1119 goto repeat;
1121 spin_unlock(&dentry->d_lock);
1124 * All done at this level ... ascend and resume the search.
1126 rcu_read_lock();
1127 ascend:
1128 if (this_parent != parent) {
1129 struct dentry *child = this_parent;
1130 this_parent = child->d_parent;
1132 spin_unlock(&child->d_lock);
1133 spin_lock(&this_parent->d_lock);
1135 /* might go back up the wrong parent if we have had a rename. */
1136 if (need_seqretry(&rename_lock, seq))
1137 goto rename_retry;
1138 /* go into the first sibling still alive */
1139 do {
1140 next = child->d_child.next;
1141 if (next == &this_parent->d_subdirs)
1142 goto ascend;
1143 child = list_entry(next, struct dentry, d_child);
1144 } while (unlikely(child->d_flags & DCACHE_DENTRY_KILLED));
1145 rcu_read_unlock();
1146 goto resume;
1148 if (need_seqretry(&rename_lock, seq))
1149 goto rename_retry;
1150 rcu_read_unlock();
1151 if (finish)
1152 finish(data);
1154 out_unlock:
1155 spin_unlock(&this_parent->d_lock);
1156 done_seqretry(&rename_lock, seq);
1157 return;
1159 rename_retry:
1160 spin_unlock(&this_parent->d_lock);
1161 rcu_read_unlock();
1162 BUG_ON(seq & 1);
1163 if (!retry)
1164 return;
1165 seq = 1;
1166 goto again;
1170 * Search for at least 1 mount point in the dentry's subdirs.
1171 * We descend to the next level whenever the d_subdirs
1172 * list is non-empty and continue searching.
1175 static enum d_walk_ret check_mount(void *data, struct dentry *dentry)
1177 int *ret = data;
1178 if (d_mountpoint(dentry)) {
1179 *ret = 1;
1180 return D_WALK_QUIT;
1182 return D_WALK_CONTINUE;
1186 * have_submounts - check for mounts over a dentry
1187 * @parent: dentry to check.
1189 * Return true if the parent or its subdirectories contain
1190 * a mount point
1192 int have_submounts(struct dentry *parent)
1194 int ret = 0;
1196 d_walk(parent, &ret, check_mount, NULL);
1198 return ret;
1200 EXPORT_SYMBOL(have_submounts);
1203 * Called by mount code to set a mountpoint and check if the mountpoint is
1204 * reachable (e.g. NFS can unhash a directory dentry and then the complete
1205 * subtree can become unreachable).
1207 * Only one of check_submounts_and_drop() and d_set_mounted() must succeed. For
1208 * this reason take rename_lock and d_lock on dentry and ancestors.
1210 int d_set_mounted(struct dentry *dentry)
1212 struct dentry *p;
1213 int ret = -ENOENT;
1214 write_seqlock(&rename_lock);
1215 for (p = dentry->d_parent; !IS_ROOT(p); p = p->d_parent) {
1216 /* Need exclusion wrt. check_submounts_and_drop() */
1217 spin_lock(&p->d_lock);
1218 if (unlikely(d_unhashed(p))) {
1219 spin_unlock(&p->d_lock);
1220 goto out;
1222 spin_unlock(&p->d_lock);
1224 spin_lock(&dentry->d_lock);
1225 if (!d_unlinked(dentry)) {
1226 dentry->d_flags |= DCACHE_MOUNTED;
1227 ret = 0;
1229 spin_unlock(&dentry->d_lock);
1230 out:
1231 write_sequnlock(&rename_lock);
1232 return ret;
1236 * Search the dentry child list of the specified parent,
1237 * and move any unused dentries to the end of the unused
1238 * list for prune_dcache(). We descend to the next level
1239 * whenever the d_subdirs list is non-empty and continue
1240 * searching.
1242 * It returns zero iff there are no unused children,
1243 * otherwise it returns the number of children moved to
1244 * the end of the unused list. This may not be the total
1245 * number of unused children, because select_parent can
1246 * drop the lock and return early due to latency
1247 * constraints.
1250 struct select_data {
1251 struct dentry *start;
1252 struct list_head dispose;
1253 int found;
1256 static enum d_walk_ret select_collect(void *_data, struct dentry *dentry)
1258 struct select_data *data = _data;
1259 enum d_walk_ret ret = D_WALK_CONTINUE;
1261 if (data->start == dentry)
1262 goto out;
1264 if (dentry->d_flags & DCACHE_SHRINK_LIST) {
1265 data->found++;
1266 } else {
1267 if (dentry->d_flags & DCACHE_LRU_LIST)
1268 d_lru_del(dentry);
1269 if (!dentry->d_lockref.count) {
1270 d_shrink_add(dentry, &data->dispose);
1271 data->found++;
1275 * We can return to the caller if we have found some (this
1276 * ensures forward progress). We'll be coming back to find
1277 * the rest.
1279 if (!list_empty(&data->dispose))
1280 ret = need_resched() ? D_WALK_QUIT : D_WALK_NORETRY;
1281 out:
1282 return ret;
1286 * shrink_dcache_parent - prune dcache
1287 * @parent: parent of entries to prune
1289 * Prune the dcache to remove unused children of the parent dentry.
1291 void shrink_dcache_parent(struct dentry *parent)
1293 for (;;) {
1294 struct select_data data;
1296 INIT_LIST_HEAD(&data.dispose);
1297 data.start = parent;
1298 data.found = 0;
1300 d_walk(parent, &data, select_collect, NULL);
1301 if (!data.found)
1302 break;
1304 shrink_dentry_list(&data.dispose);
1305 cond_resched();
1308 EXPORT_SYMBOL(shrink_dcache_parent);
1310 static enum d_walk_ret umount_check(void *_data, struct dentry *dentry)
1312 /* it has busy descendents; complain about those instead */
1313 if (!list_empty(&dentry->d_subdirs))
1314 return D_WALK_CONTINUE;
1316 /* root with refcount 1 is fine */
1317 if (dentry == _data && dentry->d_lockref.count == 1)
1318 return D_WALK_CONTINUE;
1320 printk(KERN_ERR "BUG: Dentry %p{i=%lx,n=%pd} "
1321 " still in use (%d) [unmount of %s %s]\n",
1322 dentry,
1323 dentry->d_inode ?
1324 dentry->d_inode->i_ino : 0UL,
1325 dentry,
1326 dentry->d_lockref.count,
1327 dentry->d_sb->s_type->name,
1328 dentry->d_sb->s_id);
1329 WARN_ON(1);
1330 return D_WALK_CONTINUE;
1333 static void do_one_tree(struct dentry *dentry)
1335 shrink_dcache_parent(dentry);
1336 d_walk(dentry, dentry, umount_check, NULL);
1337 d_drop(dentry);
1338 dput(dentry);
1342 * destroy the dentries attached to a superblock on unmounting
1344 void shrink_dcache_for_umount(struct super_block *sb)
1346 struct dentry *dentry;
1348 WARN(down_read_trylock(&sb->s_umount), "s_umount should've been locked");
1350 dentry = sb->s_root;
1351 sb->s_root = NULL;
1352 do_one_tree(dentry);
1354 while (!hlist_bl_empty(&sb->s_anon)) {
1355 dentry = dget(hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash));
1356 do_one_tree(dentry);
1360 static enum d_walk_ret check_and_collect(void *_data, struct dentry *dentry)
1362 struct select_data *data = _data;
1364 if (d_mountpoint(dentry)) {
1365 data->found = -EBUSY;
1366 return D_WALK_QUIT;
1369 return select_collect(_data, dentry);
1372 static void check_and_drop(void *_data)
1374 struct select_data *data = _data;
1376 if (d_mountpoint(data->start))
1377 data->found = -EBUSY;
1378 if (!data->found)
1379 __d_drop(data->start);
1383 * check_submounts_and_drop - prune dcache, check for submounts and drop
1385 * All done as a single atomic operation relative to has_unlinked_ancestor().
1386 * Returns 0 if successfully unhashed @parent. If there were submounts then
1387 * return -EBUSY.
1389 * @dentry: dentry to prune and drop
1391 int check_submounts_and_drop(struct dentry *dentry)
1393 int ret = 0;
1395 /* Negative dentries can be dropped without further checks */
1396 if (!dentry->d_inode) {
1397 d_drop(dentry);
1398 goto out;
1401 for (;;) {
1402 struct select_data data;
1404 INIT_LIST_HEAD(&data.dispose);
1405 data.start = dentry;
1406 data.found = 0;
1408 d_walk(dentry, &data, check_and_collect, check_and_drop);
1409 ret = data.found;
1411 if (!list_empty(&data.dispose))
1412 shrink_dentry_list(&data.dispose);
1414 if (ret <= 0)
1415 break;
1417 cond_resched();
1420 out:
1421 return ret;
1423 EXPORT_SYMBOL(check_submounts_and_drop);
1426 * __d_alloc - allocate a dcache entry
1427 * @sb: filesystem it will belong to
1428 * @name: qstr of the name
1430 * Allocates a dentry. It returns %NULL if there is insufficient memory
1431 * available. On a success the dentry is returned. The name passed in is
1432 * copied and the copy passed in may be reused after this call.
1435 struct dentry *__d_alloc(struct super_block *sb, const struct qstr *name)
1437 struct dentry *dentry;
1438 char *dname;
1440 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
1441 if (!dentry)
1442 return NULL;
1445 * We guarantee that the inline name is always NUL-terminated.
1446 * This way the memcpy() done by the name switching in rename
1447 * will still always have a NUL at the end, even if we might
1448 * be overwriting an internal NUL character
1450 dentry->d_iname[DNAME_INLINE_LEN-1] = 0;
1451 if (name->len > DNAME_INLINE_LEN-1) {
1452 dname = kmalloc(name->len + 1, GFP_KERNEL);
1453 if (!dname) {
1454 kmem_cache_free(dentry_cache, dentry);
1455 return NULL;
1457 } else {
1458 dname = dentry->d_iname;
1461 dentry->d_name.len = name->len;
1462 dentry->d_name.hash = name->hash;
1463 memcpy(dname, name->name, name->len);
1464 dname[name->len] = 0;
1466 /* Make sure we always see the terminating NUL character */
1467 smp_wmb();
1468 dentry->d_name.name = dname;
1470 dentry->d_lockref.count = 1;
1471 dentry->d_flags = 0;
1472 spin_lock_init(&dentry->d_lock);
1473 seqcount_init(&dentry->d_seq);
1474 dentry->d_inode = NULL;
1475 dentry->d_parent = dentry;
1476 dentry->d_sb = sb;
1477 dentry->d_op = NULL;
1478 dentry->d_fsdata = NULL;
1479 INIT_HLIST_BL_NODE(&dentry->d_hash);
1480 INIT_LIST_HEAD(&dentry->d_lru);
1481 INIT_LIST_HEAD(&dentry->d_subdirs);
1482 INIT_HLIST_NODE(&dentry->d_u.d_alias);
1483 INIT_LIST_HEAD(&dentry->d_child);
1484 d_set_d_op(dentry, dentry->d_sb->s_d_op);
1486 this_cpu_inc(nr_dentry);
1488 return dentry;
1492 * d_alloc - allocate a dcache entry
1493 * @parent: parent of entry to allocate
1494 * @name: qstr of the name
1496 * Allocates a dentry. It returns %NULL if there is insufficient memory
1497 * available. On a success the dentry is returned. The name passed in is
1498 * copied and the copy passed in may be reused after this call.
1500 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1502 struct dentry *dentry = __d_alloc(parent->d_sb, name);
1503 if (!dentry)
1504 return NULL;
1506 spin_lock(&parent->d_lock);
1508 * don't need child lock because it is not subject
1509 * to concurrency here
1511 __dget_dlock(parent);
1512 dentry->d_parent = parent;
1513 list_add(&dentry->d_child, &parent->d_subdirs);
1514 spin_unlock(&parent->d_lock);
1516 return dentry;
1518 EXPORT_SYMBOL(d_alloc);
1521 * d_alloc_pseudo - allocate a dentry (for lookup-less filesystems)
1522 * @sb: the superblock
1523 * @name: qstr of the name
1525 * For a filesystem that just pins its dentries in memory and never
1526 * performs lookups at all, return an unhashed IS_ROOT dentry.
1528 struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1530 return __d_alloc(sb, name);
1532 EXPORT_SYMBOL(d_alloc_pseudo);
1534 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1536 struct qstr q;
1538 q.name = name;
1539 q.len = strlen(name);
1540 q.hash = full_name_hash(q.name, q.len);
1541 return d_alloc(parent, &q);
1543 EXPORT_SYMBOL(d_alloc_name);
1545 void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1547 WARN_ON_ONCE(dentry->d_op);
1548 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
1549 DCACHE_OP_COMPARE |
1550 DCACHE_OP_REVALIDATE |
1551 DCACHE_OP_WEAK_REVALIDATE |
1552 DCACHE_OP_DELETE ));
1553 dentry->d_op = op;
1554 if (!op)
1555 return;
1556 if (op->d_hash)
1557 dentry->d_flags |= DCACHE_OP_HASH;
1558 if (op->d_compare)
1559 dentry->d_flags |= DCACHE_OP_COMPARE;
1560 if (op->d_revalidate)
1561 dentry->d_flags |= DCACHE_OP_REVALIDATE;
1562 if (op->d_weak_revalidate)
1563 dentry->d_flags |= DCACHE_OP_WEAK_REVALIDATE;
1564 if (op->d_delete)
1565 dentry->d_flags |= DCACHE_OP_DELETE;
1566 if (op->d_prune)
1567 dentry->d_flags |= DCACHE_OP_PRUNE;
1570 EXPORT_SYMBOL(d_set_d_op);
1572 static unsigned d_flags_for_inode(struct inode *inode)
1574 unsigned add_flags = DCACHE_FILE_TYPE;
1576 if (!inode)
1577 return DCACHE_MISS_TYPE;
1579 if (S_ISDIR(inode->i_mode)) {
1580 add_flags = DCACHE_DIRECTORY_TYPE;
1581 if (unlikely(!(inode->i_opflags & IOP_LOOKUP))) {
1582 if (unlikely(!inode->i_op->lookup))
1583 add_flags = DCACHE_AUTODIR_TYPE;
1584 else
1585 inode->i_opflags |= IOP_LOOKUP;
1587 } else if (unlikely(!(inode->i_opflags & IOP_NOFOLLOW))) {
1588 if (unlikely(inode->i_op->follow_link))
1589 add_flags = DCACHE_SYMLINK_TYPE;
1590 else
1591 inode->i_opflags |= IOP_NOFOLLOW;
1594 if (unlikely(IS_AUTOMOUNT(inode)))
1595 add_flags |= DCACHE_NEED_AUTOMOUNT;
1596 return add_flags;
1599 static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1601 unsigned add_flags = d_flags_for_inode(inode);
1603 spin_lock(&dentry->d_lock);
1604 __d_set_type(dentry, add_flags);
1605 if (inode)
1606 hlist_add_head(&dentry->d_u.d_alias, &inode->i_dentry);
1607 dentry->d_inode = inode;
1608 dentry_rcuwalk_barrier(dentry);
1609 spin_unlock(&dentry->d_lock);
1610 fsnotify_d_instantiate(dentry, inode);
1614 * d_instantiate - fill in inode information for a dentry
1615 * @entry: dentry to complete
1616 * @inode: inode to attach to this dentry
1618 * Fill in inode information in the entry.
1620 * This turns negative dentries into productive full members
1621 * of society.
1623 * NOTE! This assumes that the inode count has been incremented
1624 * (or otherwise set) by the caller to indicate that it is now
1625 * in use by the dcache.
1628 void d_instantiate(struct dentry *entry, struct inode * inode)
1630 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
1631 if (inode)
1632 spin_lock(&inode->i_lock);
1633 __d_instantiate(entry, inode);
1634 if (inode)
1635 spin_unlock(&inode->i_lock);
1636 security_d_instantiate(entry, inode);
1638 EXPORT_SYMBOL(d_instantiate);
1641 * d_instantiate_unique - instantiate a non-aliased dentry
1642 * @entry: dentry to instantiate
1643 * @inode: inode to attach to this dentry
1645 * Fill in inode information in the entry. On success, it returns NULL.
1646 * If an unhashed alias of "entry" already exists, then we return the
1647 * aliased dentry instead and drop one reference to inode.
1649 * Note that in order to avoid conflicts with rename() etc, the caller
1650 * had better be holding the parent directory semaphore.
1652 * This also assumes that the inode count has been incremented
1653 * (or otherwise set) by the caller to indicate that it is now
1654 * in use by the dcache.
1656 static struct dentry *__d_instantiate_unique(struct dentry *entry,
1657 struct inode *inode)
1659 struct dentry *alias;
1660 int len = entry->d_name.len;
1661 const char *name = entry->d_name.name;
1662 unsigned int hash = entry->d_name.hash;
1664 if (!inode) {
1665 __d_instantiate(entry, NULL);
1666 return NULL;
1669 hlist_for_each_entry(alias, &inode->i_dentry, d_u.d_alias) {
1671 * Don't need alias->d_lock here, because aliases with
1672 * d_parent == entry->d_parent are not subject to name or
1673 * parent changes, because the parent inode i_mutex is held.
1675 if (alias->d_name.hash != hash)
1676 continue;
1677 if (alias->d_parent != entry->d_parent)
1678 continue;
1679 if (alias->d_name.len != len)
1680 continue;
1681 if (dentry_cmp(alias, name, len))
1682 continue;
1683 __dget(alias);
1684 return alias;
1687 __d_instantiate(entry, inode);
1688 return NULL;
1691 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1693 struct dentry *result;
1695 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
1697 if (inode)
1698 spin_lock(&inode->i_lock);
1699 result = __d_instantiate_unique(entry, inode);
1700 if (inode)
1701 spin_unlock(&inode->i_lock);
1703 if (!result) {
1704 security_d_instantiate(entry, inode);
1705 return NULL;
1708 BUG_ON(!d_unhashed(result));
1709 iput(inode);
1710 return result;
1713 EXPORT_SYMBOL(d_instantiate_unique);
1716 * d_instantiate_no_diralias - instantiate a non-aliased dentry
1717 * @entry: dentry to complete
1718 * @inode: inode to attach to this dentry
1720 * Fill in inode information in the entry. If a directory alias is found, then
1721 * return an error (and drop inode). Together with d_materialise_unique() this
1722 * guarantees that a directory inode may never have more than one alias.
1724 int d_instantiate_no_diralias(struct dentry *entry, struct inode *inode)
1726 BUG_ON(!hlist_unhashed(&entry->d_u.d_alias));
1728 spin_lock(&inode->i_lock);
1729 if (S_ISDIR(inode->i_mode) && !hlist_empty(&inode->i_dentry)) {
1730 spin_unlock(&inode->i_lock);
1731 iput(inode);
1732 return -EBUSY;
1734 __d_instantiate(entry, inode);
1735 spin_unlock(&inode->i_lock);
1736 security_d_instantiate(entry, inode);
1738 return 0;
1740 EXPORT_SYMBOL(d_instantiate_no_diralias);
1742 struct dentry *d_make_root(struct inode *root_inode)
1744 struct dentry *res = NULL;
1746 if (root_inode) {
1747 static const struct qstr name = QSTR_INIT("/", 1);
1749 res = __d_alloc(root_inode->i_sb, &name);
1750 if (res)
1751 d_instantiate(res, root_inode);
1752 else
1753 iput(root_inode);
1755 return res;
1757 EXPORT_SYMBOL(d_make_root);
1759 static struct dentry * __d_find_any_alias(struct inode *inode)
1761 struct dentry *alias;
1763 if (hlist_empty(&inode->i_dentry))
1764 return NULL;
1765 alias = hlist_entry(inode->i_dentry.first, struct dentry, d_u.d_alias);
1766 __dget(alias);
1767 return alias;
1771 * d_find_any_alias - find any alias for a given inode
1772 * @inode: inode to find an alias for
1774 * If any aliases exist for the given inode, take and return a
1775 * reference for one of them. If no aliases exist, return %NULL.
1777 struct dentry *d_find_any_alias(struct inode *inode)
1779 struct dentry *de;
1781 spin_lock(&inode->i_lock);
1782 de = __d_find_any_alias(inode);
1783 spin_unlock(&inode->i_lock);
1784 return de;
1786 EXPORT_SYMBOL(d_find_any_alias);
1789 * d_obtain_alias - find or allocate a dentry for a given inode
1790 * @inode: inode to allocate the dentry for
1792 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1793 * similar open by handle operations. The returned dentry may be anonymous,
1794 * or may have a full name (if the inode was already in the cache).
1796 * When called on a directory inode, we must ensure that the inode only ever
1797 * has one dentry. If a dentry is found, that is returned instead of
1798 * allocating a new one.
1800 * On successful return, the reference to the inode has been transferred
1801 * to the dentry. In case of an error the reference on the inode is released.
1802 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1803 * be passed in and will be the error will be propagate to the return value,
1804 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1806 struct dentry *d_obtain_alias(struct inode *inode)
1808 static const struct qstr anonstring = QSTR_INIT("/", 1);
1809 struct dentry *tmp;
1810 struct dentry *res;
1811 unsigned add_flags;
1813 if (!inode)
1814 return ERR_PTR(-ESTALE);
1815 if (IS_ERR(inode))
1816 return ERR_CAST(inode);
1818 res = d_find_any_alias(inode);
1819 if (res)
1820 goto out_iput;
1822 tmp = __d_alloc(inode->i_sb, &anonstring);
1823 if (!tmp) {
1824 res = ERR_PTR(-ENOMEM);
1825 goto out_iput;
1828 spin_lock(&inode->i_lock);
1829 res = __d_find_any_alias(inode);
1830 if (res) {
1831 spin_unlock(&inode->i_lock);
1832 dput(tmp);
1833 goto out_iput;
1836 /* attach a disconnected dentry */
1837 add_flags = d_flags_for_inode(inode) | DCACHE_DISCONNECTED;
1839 spin_lock(&tmp->d_lock);
1840 tmp->d_inode = inode;
1841 tmp->d_flags |= add_flags;
1842 hlist_add_head(&tmp->d_u.d_alias, &inode->i_dentry);
1843 hlist_bl_lock(&tmp->d_sb->s_anon);
1844 hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon);
1845 hlist_bl_unlock(&tmp->d_sb->s_anon);
1846 spin_unlock(&tmp->d_lock);
1847 spin_unlock(&inode->i_lock);
1848 security_d_instantiate(tmp, inode);
1850 return tmp;
1852 out_iput:
1853 if (res && !IS_ERR(res))
1854 security_d_instantiate(res, inode);
1855 iput(inode);
1856 return res;
1858 EXPORT_SYMBOL(d_obtain_alias);
1861 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1862 * @inode: the inode which may have a disconnected dentry
1863 * @dentry: a negative dentry which we want to point to the inode.
1865 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1866 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1867 * and return it, else simply d_add the inode to the dentry and return NULL.
1869 * This is needed in the lookup routine of any filesystem that is exportable
1870 * (via knfsd) so that we can build dcache paths to directories effectively.
1872 * If a dentry was found and moved, then it is returned. Otherwise NULL
1873 * is returned. This matches the expected return value of ->lookup.
1875 * Cluster filesystems may call this function with a negative, hashed dentry.
1876 * In that case, we know that the inode will be a regular file, and also this
1877 * will only occur during atomic_open. So we need to check for the dentry
1878 * being already hashed only in the final case.
1880 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1882 struct dentry *new = NULL;
1884 if (IS_ERR(inode))
1885 return ERR_CAST(inode);
1887 if (inode && S_ISDIR(inode->i_mode)) {
1888 spin_lock(&inode->i_lock);
1889 new = __d_find_alias(inode, 1);
1890 if (new) {
1891 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1892 spin_unlock(&inode->i_lock);
1893 security_d_instantiate(new, inode);
1894 d_move(new, dentry);
1895 iput(inode);
1896 } else {
1897 /* already taking inode->i_lock, so d_add() by hand */
1898 __d_instantiate(dentry, inode);
1899 spin_unlock(&inode->i_lock);
1900 security_d_instantiate(dentry, inode);
1901 d_rehash(dentry);
1903 } else {
1904 d_instantiate(dentry, inode);
1905 if (d_unhashed(dentry))
1906 d_rehash(dentry);
1908 return new;
1910 EXPORT_SYMBOL(d_splice_alias);
1913 * d_add_ci - lookup or allocate new dentry with case-exact name
1914 * @inode: the inode case-insensitive lookup has found
1915 * @dentry: the negative dentry that was passed to the parent's lookup func
1916 * @name: the case-exact name to be associated with the returned dentry
1918 * This is to avoid filling the dcache with case-insensitive names to the
1919 * same inode, only the actual correct case is stored in the dcache for
1920 * case-insensitive filesystems.
1922 * For a case-insensitive lookup match and if the the case-exact dentry
1923 * already exists in in the dcache, use it and return it.
1925 * If no entry exists with the exact case name, allocate new dentry with
1926 * the exact case, and return the spliced entry.
1928 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
1929 struct qstr *name)
1931 struct dentry *found;
1932 struct dentry *new;
1935 * First check if a dentry matching the name already exists,
1936 * if not go ahead and create it now.
1938 found = d_hash_and_lookup(dentry->d_parent, name);
1939 if (unlikely(IS_ERR(found)))
1940 goto err_out;
1941 if (!found) {
1942 new = d_alloc(dentry->d_parent, name);
1943 if (!new) {
1944 found = ERR_PTR(-ENOMEM);
1945 goto err_out;
1948 found = d_splice_alias(inode, new);
1949 if (found) {
1950 dput(new);
1951 return found;
1953 return new;
1957 * If a matching dentry exists, and it's not negative use it.
1959 * Decrement the reference count to balance the iget() done
1960 * earlier on.
1962 if (found->d_inode) {
1963 if (unlikely(found->d_inode != inode)) {
1964 /* This can't happen because bad inodes are unhashed. */
1965 BUG_ON(!is_bad_inode(inode));
1966 BUG_ON(!is_bad_inode(found->d_inode));
1968 iput(inode);
1969 return found;
1973 * Negative dentry: instantiate it unless the inode is a directory and
1974 * already has a dentry.
1976 new = d_splice_alias(inode, found);
1977 if (new) {
1978 dput(found);
1979 found = new;
1981 return found;
1983 err_out:
1984 iput(inode);
1985 return found;
1987 EXPORT_SYMBOL(d_add_ci);
1990 * Do the slow-case of the dentry name compare.
1992 * Unlike the dentry_cmp() function, we need to atomically
1993 * load the name and length information, so that the
1994 * filesystem can rely on them, and can use the 'name' and
1995 * 'len' information without worrying about walking off the
1996 * end of memory etc.
1998 * Thus the read_seqcount_retry() and the "duplicate" info
1999 * in arguments (the low-level filesystem should not look
2000 * at the dentry inode or name contents directly, since
2001 * rename can change them while we're in RCU mode).
2003 enum slow_d_compare {
2004 D_COMP_OK,
2005 D_COMP_NOMATCH,
2006 D_COMP_SEQRETRY,
2009 static noinline enum slow_d_compare slow_dentry_cmp(
2010 const struct dentry *parent,
2011 struct dentry *dentry,
2012 unsigned int seq,
2013 const struct qstr *name)
2015 int tlen = dentry->d_name.len;
2016 const char *tname = dentry->d_name.name;
2018 if (read_seqcount_retry(&dentry->d_seq, seq)) {
2019 cpu_relax();
2020 return D_COMP_SEQRETRY;
2022 if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
2023 return D_COMP_NOMATCH;
2024 return D_COMP_OK;
2028 * __d_lookup_rcu - search for a dentry (racy, store-free)
2029 * @parent: parent dentry
2030 * @name: qstr of name we wish to find
2031 * @seqp: returns d_seq value at the point where the dentry was found
2032 * Returns: dentry, or NULL
2034 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
2035 * resolution (store-free path walking) design described in
2036 * Documentation/filesystems/path-lookup.txt.
2038 * This is not to be used outside core vfs.
2040 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
2041 * held, and rcu_read_lock held. The returned dentry must not be stored into
2042 * without taking d_lock and checking d_seq sequence count against @seq
2043 * returned here.
2045 * A refcount may be taken on the found dentry with the d_rcu_to_refcount
2046 * function.
2048 * Alternatively, __d_lookup_rcu may be called again to look up the child of
2049 * the returned dentry, so long as its parent's seqlock is checked after the
2050 * child is looked up. Thus, an interlocking stepping of sequence lock checks
2051 * is formed, giving integrity down the path walk.
2053 * NOTE! The caller *has* to check the resulting dentry against the sequence
2054 * number we've returned before using any of the resulting dentry state!
2056 struct dentry *__d_lookup_rcu(const struct dentry *parent,
2057 const struct qstr *name,
2058 unsigned *seqp)
2060 u64 hashlen = name->hash_len;
2061 const unsigned char *str = name->name;
2062 struct hlist_bl_head *b = d_hash(parent, hashlen_hash(hashlen));
2063 struct hlist_bl_node *node;
2064 struct dentry *dentry;
2067 * Note: There is significant duplication with __d_lookup_rcu which is
2068 * required to prevent single threaded performance regressions
2069 * especially on architectures where smp_rmb (in seqcounts) are costly.
2070 * Keep the two functions in sync.
2074 * The hash list is protected using RCU.
2076 * Carefully use d_seq when comparing a candidate dentry, to avoid
2077 * races with d_move().
2079 * It is possible that concurrent renames can mess up our list
2080 * walk here and result in missing our dentry, resulting in the
2081 * false-negative result. d_lookup() protects against concurrent
2082 * renames using rename_lock seqlock.
2084 * See Documentation/filesystems/path-lookup.txt for more details.
2086 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2087 unsigned seq;
2089 seqretry:
2091 * The dentry sequence count protects us from concurrent
2092 * renames, and thus protects parent and name fields.
2094 * The caller must perform a seqcount check in order
2095 * to do anything useful with the returned dentry.
2097 * NOTE! We do a "raw" seqcount_begin here. That means that
2098 * we don't wait for the sequence count to stabilize if it
2099 * is in the middle of a sequence change. If we do the slow
2100 * dentry compare, we will do seqretries until it is stable,
2101 * and if we end up with a successful lookup, we actually
2102 * want to exit RCU lookup anyway.
2104 seq = raw_seqcount_begin(&dentry->d_seq);
2105 if (dentry->d_parent != parent)
2106 continue;
2107 if (d_unhashed(dentry))
2108 continue;
2110 if (unlikely(parent->d_flags & DCACHE_OP_COMPARE)) {
2111 if (dentry->d_name.hash != hashlen_hash(hashlen))
2112 continue;
2113 *seqp = seq;
2114 switch (slow_dentry_cmp(parent, dentry, seq, name)) {
2115 case D_COMP_OK:
2116 return dentry;
2117 case D_COMP_NOMATCH:
2118 continue;
2119 default:
2120 goto seqretry;
2124 if (dentry->d_name.hash_len != hashlen)
2125 continue;
2126 *seqp = seq;
2127 if (!dentry_cmp(dentry, str, hashlen_len(hashlen)))
2128 return dentry;
2130 return NULL;
2134 * d_lookup - search for a dentry
2135 * @parent: parent dentry
2136 * @name: qstr of name we wish to find
2137 * Returns: dentry, or NULL
2139 * d_lookup searches the children of the parent dentry for the name in
2140 * question. If the dentry is found its reference count is incremented and the
2141 * dentry is returned. The caller must use dput to free the entry when it has
2142 * finished using it. %NULL is returned if the dentry does not exist.
2144 struct dentry *d_lookup(const struct dentry *parent, const struct qstr *name)
2146 struct dentry *dentry;
2147 unsigned seq;
2149 do {
2150 seq = read_seqbegin(&rename_lock);
2151 dentry = __d_lookup(parent, name);
2152 if (dentry)
2153 break;
2154 } while (read_seqretry(&rename_lock, seq));
2155 return dentry;
2157 EXPORT_SYMBOL(d_lookup);
2160 * __d_lookup - search for a dentry (racy)
2161 * @parent: parent dentry
2162 * @name: qstr of name we wish to find
2163 * Returns: dentry, or NULL
2165 * __d_lookup is like d_lookup, however it may (rarely) return a
2166 * false-negative result due to unrelated rename activity.
2168 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
2169 * however it must be used carefully, eg. with a following d_lookup in
2170 * the case of failure.
2172 * __d_lookup callers must be commented.
2174 struct dentry *__d_lookup(const struct dentry *parent, const struct qstr *name)
2176 unsigned int len = name->len;
2177 unsigned int hash = name->hash;
2178 const unsigned char *str = name->name;
2179 struct hlist_bl_head *b = d_hash(parent, hash);
2180 struct hlist_bl_node *node;
2181 struct dentry *found = NULL;
2182 struct dentry *dentry;
2185 * Note: There is significant duplication with __d_lookup_rcu which is
2186 * required to prevent single threaded performance regressions
2187 * especially on architectures where smp_rmb (in seqcounts) are costly.
2188 * Keep the two functions in sync.
2192 * The hash list is protected using RCU.
2194 * Take d_lock when comparing a candidate dentry, to avoid races
2195 * with d_move().
2197 * It is possible that concurrent renames can mess up our list
2198 * walk here and result in missing our dentry, resulting in the
2199 * false-negative result. d_lookup() protects against concurrent
2200 * renames using rename_lock seqlock.
2202 * See Documentation/filesystems/path-lookup.txt for more details.
2204 rcu_read_lock();
2206 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
2208 if (dentry->d_name.hash != hash)
2209 continue;
2211 spin_lock(&dentry->d_lock);
2212 if (dentry->d_parent != parent)
2213 goto next;
2214 if (d_unhashed(dentry))
2215 goto next;
2218 * It is safe to compare names since d_move() cannot
2219 * change the qstr (protected by d_lock).
2221 if (parent->d_flags & DCACHE_OP_COMPARE) {
2222 int tlen = dentry->d_name.len;
2223 const char *tname = dentry->d_name.name;
2224 if (parent->d_op->d_compare(parent, dentry, tlen, tname, name))
2225 goto next;
2226 } else {
2227 if (dentry->d_name.len != len)
2228 goto next;
2229 if (dentry_cmp(dentry, str, len))
2230 goto next;
2233 dentry->d_lockref.count++;
2234 found = dentry;
2235 spin_unlock(&dentry->d_lock);
2236 break;
2237 next:
2238 spin_unlock(&dentry->d_lock);
2240 rcu_read_unlock();
2242 return found;
2246 * d_hash_and_lookup - hash the qstr then search for a dentry
2247 * @dir: Directory to search in
2248 * @name: qstr of name we wish to find
2250 * On lookup failure NULL is returned; on bad name - ERR_PTR(-error)
2252 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
2255 * Check for a fs-specific hash function. Note that we must
2256 * calculate the standard hash first, as the d_op->d_hash()
2257 * routine may choose to leave the hash value unchanged.
2259 name->hash = full_name_hash(name->name, name->len);
2260 if (dir->d_flags & DCACHE_OP_HASH) {
2261 int err = dir->d_op->d_hash(dir, name);
2262 if (unlikely(err < 0))
2263 return ERR_PTR(err);
2265 return d_lookup(dir, name);
2267 EXPORT_SYMBOL(d_hash_and_lookup);
2270 * d_validate - verify dentry provided from insecure source (deprecated)
2271 * @dentry: The dentry alleged to be valid child of @dparent
2272 * @dparent: The parent dentry (known to be valid)
2274 * An insecure source has sent us a dentry, here we verify it and dget() it.
2275 * This is used by ncpfs in its readdir implementation.
2276 * Zero is returned in the dentry is invalid.
2278 * This function is slow for big directories, and deprecated, do not use it.
2280 int d_validate(struct dentry *dentry, struct dentry *dparent)
2282 struct dentry *child;
2284 spin_lock(&dparent->d_lock);
2285 list_for_each_entry(child, &dparent->d_subdirs, d_child) {
2286 if (dentry == child) {
2287 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2288 __dget_dlock(dentry);
2289 spin_unlock(&dentry->d_lock);
2290 spin_unlock(&dparent->d_lock);
2291 return 1;
2294 spin_unlock(&dparent->d_lock);
2296 return 0;
2298 EXPORT_SYMBOL(d_validate);
2301 * When a file is deleted, we have two options:
2302 * - turn this dentry into a negative dentry
2303 * - unhash this dentry and free it.
2305 * Usually, we want to just turn this into
2306 * a negative dentry, but if anybody else is
2307 * currently using the dentry or the inode
2308 * we can't do that and we fall back on removing
2309 * it from the hash queues and waiting for
2310 * it to be deleted later when it has no users
2314 * d_delete - delete a dentry
2315 * @dentry: The dentry to delete
2317 * Turn the dentry into a negative dentry if possible, otherwise
2318 * remove it from the hash queues so it can be deleted later
2321 void d_delete(struct dentry * dentry)
2323 struct inode *inode;
2324 int isdir = 0;
2326 * Are we the only user?
2328 again:
2329 spin_lock(&dentry->d_lock);
2330 inode = dentry->d_inode;
2331 isdir = S_ISDIR(inode->i_mode);
2332 if (dentry->d_lockref.count == 1) {
2333 if (!spin_trylock(&inode->i_lock)) {
2334 spin_unlock(&dentry->d_lock);
2335 cpu_relax();
2336 goto again;
2338 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
2339 dentry_unlink_inode(dentry);
2340 fsnotify_nameremove(dentry, isdir);
2341 return;
2344 if (!d_unhashed(dentry))
2345 __d_drop(dentry);
2347 spin_unlock(&dentry->d_lock);
2349 fsnotify_nameremove(dentry, isdir);
2351 EXPORT_SYMBOL(d_delete);
2353 static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b)
2355 BUG_ON(!d_unhashed(entry));
2356 hlist_bl_lock(b);
2357 entry->d_flags |= DCACHE_RCUACCESS;
2358 hlist_bl_add_head_rcu(&entry->d_hash, b);
2359 hlist_bl_unlock(b);
2362 static void _d_rehash(struct dentry * entry)
2364 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
2368 * d_rehash - add an entry back to the hash
2369 * @entry: dentry to add to the hash
2371 * Adds a dentry to the hash according to its name.
2374 void d_rehash(struct dentry * entry)
2376 spin_lock(&entry->d_lock);
2377 _d_rehash(entry);
2378 spin_unlock(&entry->d_lock);
2380 EXPORT_SYMBOL(d_rehash);
2383 * dentry_update_name_case - update case insensitive dentry with a new name
2384 * @dentry: dentry to be updated
2385 * @name: new name
2387 * Update a case insensitive dentry with new case of name.
2389 * dentry must have been returned by d_lookup with name @name. Old and new
2390 * name lengths must match (ie. no d_compare which allows mismatched name
2391 * lengths).
2393 * Parent inode i_mutex must be held over d_lookup and into this call (to
2394 * keep renames and concurrent inserts, and readdir(2) away).
2396 void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
2398 BUG_ON(!mutex_is_locked(&dentry->d_parent->d_inode->i_mutex));
2399 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
2401 spin_lock(&dentry->d_lock);
2402 write_seqcount_begin(&dentry->d_seq);
2403 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
2404 write_seqcount_end(&dentry->d_seq);
2405 spin_unlock(&dentry->d_lock);
2407 EXPORT_SYMBOL(dentry_update_name_case);
2409 static void switch_names(struct dentry *dentry, struct dentry *target,
2410 bool exchange)
2412 if (dname_external(target)) {
2413 if (dname_external(dentry)) {
2415 * Both external: swap the pointers
2417 swap(target->d_name.name, dentry->d_name.name);
2418 } else {
2420 * dentry:internal, target:external. Steal target's
2421 * storage and make target internal.
2423 memcpy(target->d_iname, dentry->d_name.name,
2424 dentry->d_name.len + 1);
2425 dentry->d_name.name = target->d_name.name;
2426 target->d_name.name = target->d_iname;
2428 } else {
2429 if (dname_external(dentry)) {
2431 * dentry:external, target:internal. Give dentry's
2432 * storage to target and make dentry internal
2434 memcpy(dentry->d_iname, target->d_name.name,
2435 target->d_name.len + 1);
2436 target->d_name.name = dentry->d_name.name;
2437 dentry->d_name.name = dentry->d_iname;
2438 } else {
2440 * Both are internal.
2442 unsigned int i;
2443 BUILD_BUG_ON(!IS_ALIGNED(DNAME_INLINE_LEN, sizeof(long)));
2444 kmemcheck_mark_initialized(dentry->d_iname, DNAME_INLINE_LEN);
2445 kmemcheck_mark_initialized(target->d_iname, DNAME_INLINE_LEN);
2446 if (!exchange) {
2447 memcpy(dentry->d_iname, target->d_name.name,
2448 target->d_name.len + 1);
2449 dentry->d_name.hash_len = target->d_name.hash_len;
2450 return;
2452 for (i = 0; i < DNAME_INLINE_LEN / sizeof(long); i++) {
2453 swap(((long *) &dentry->d_iname)[i],
2454 ((long *) &target->d_iname)[i]);
2458 swap(dentry->d_name.hash_len, target->d_name.hash_len);
2461 static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
2464 * XXXX: do we really need to take target->d_lock?
2466 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
2467 spin_lock(&target->d_parent->d_lock);
2468 else {
2469 if (d_ancestor(dentry->d_parent, target->d_parent)) {
2470 spin_lock(&dentry->d_parent->d_lock);
2471 spin_lock_nested(&target->d_parent->d_lock,
2472 DENTRY_D_LOCK_NESTED);
2473 } else {
2474 spin_lock(&target->d_parent->d_lock);
2475 spin_lock_nested(&dentry->d_parent->d_lock,
2476 DENTRY_D_LOCK_NESTED);
2479 if (target < dentry) {
2480 spin_lock_nested(&target->d_lock, 2);
2481 spin_lock_nested(&dentry->d_lock, 3);
2482 } else {
2483 spin_lock_nested(&dentry->d_lock, 2);
2484 spin_lock_nested(&target->d_lock, 3);
2488 static void dentry_unlock_parents_for_move(struct dentry *dentry,
2489 struct dentry *target)
2491 if (target->d_parent != dentry->d_parent)
2492 spin_unlock(&dentry->d_parent->d_lock);
2493 if (target->d_parent != target)
2494 spin_unlock(&target->d_parent->d_lock);
2498 * When switching names, the actual string doesn't strictly have to
2499 * be preserved in the target - because we're dropping the target
2500 * anyway. As such, we can just do a simple memcpy() to copy over
2501 * the new name before we switch, unless we are going to rehash
2502 * it. Note that if we *do* unhash the target, we are not allowed
2503 * to rehash it without giving it a new name/hash key - whether
2504 * we swap or overwrite the names here, resulting name won't match
2505 * the reality in filesystem; it's only there for d_path() purposes.
2506 * Note that all of this is happening under rename_lock, so the
2507 * any hash lookup seeing it in the middle of manipulations will
2508 * be discarded anyway. So we do not care what happens to the hash
2509 * key in that case.
2512 * __d_move - move a dentry
2513 * @dentry: entry to move
2514 * @target: new dentry
2515 * @exchange: exchange the two dentries
2517 * Update the dcache to reflect the move of a file name. Negative
2518 * dcache entries should not be moved in this way. Caller must hold
2519 * rename_lock, the i_mutex of the source and target directories,
2520 * and the sb->s_vfs_rename_mutex if they differ. See lock_rename().
2522 static void __d_move(struct dentry *dentry, struct dentry *target,
2523 bool exchange)
2525 if (!dentry->d_inode)
2526 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2528 BUG_ON(d_ancestor(dentry, target));
2529 BUG_ON(d_ancestor(target, dentry));
2531 dentry_lock_for_move(dentry, target);
2533 write_seqcount_begin(&dentry->d_seq);
2534 write_seqcount_begin_nested(&target->d_seq, DENTRY_D_LOCK_NESTED);
2536 /* __d_drop does write_seqcount_barrier, but they're OK to nest. */
2539 * Move the dentry to the target hash queue. Don't bother checking
2540 * for the same hash queue because of how unlikely it is.
2542 __d_drop(dentry);
2543 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
2546 * Unhash the target (d_delete() is not usable here). If exchanging
2547 * the two dentries, then rehash onto the other's hash queue.
2549 __d_drop(target);
2550 if (exchange) {
2551 __d_rehash(target,
2552 d_hash(dentry->d_parent, dentry->d_name.hash));
2555 list_del(&dentry->d_child);
2556 list_del(&target->d_child);
2558 /* Switch the names.. */
2559 switch_names(dentry, target, exchange);
2561 /* ... and switch the parents */
2562 if (IS_ROOT(dentry)) {
2563 dentry->d_parent = target->d_parent;
2564 target->d_parent = target;
2565 INIT_LIST_HEAD(&target->d_child);
2566 } else {
2567 swap(dentry->d_parent, target->d_parent);
2569 /* And add them back to the (new) parent lists */
2570 list_add(&target->d_child, &target->d_parent->d_subdirs);
2573 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
2575 write_seqcount_end(&target->d_seq);
2576 write_seqcount_end(&dentry->d_seq);
2578 dentry_unlock_parents_for_move(dentry, target);
2579 if (exchange)
2580 fsnotify_d_move(target);
2581 spin_unlock(&target->d_lock);
2582 fsnotify_d_move(dentry);
2583 spin_unlock(&dentry->d_lock);
2587 * d_move - move a dentry
2588 * @dentry: entry to move
2589 * @target: new dentry
2591 * Update the dcache to reflect the move of a file name. Negative
2592 * dcache entries should not be moved in this way. See the locking
2593 * requirements for __d_move.
2595 void d_move(struct dentry *dentry, struct dentry *target)
2597 write_seqlock(&rename_lock);
2598 __d_move(dentry, target, false);
2599 write_sequnlock(&rename_lock);
2601 EXPORT_SYMBOL(d_move);
2604 * d_exchange - exchange two dentries
2605 * @dentry1: first dentry
2606 * @dentry2: second dentry
2608 void d_exchange(struct dentry *dentry1, struct dentry *dentry2)
2610 write_seqlock(&rename_lock);
2612 WARN_ON(!dentry1->d_inode);
2613 WARN_ON(!dentry2->d_inode);
2614 WARN_ON(IS_ROOT(dentry1));
2615 WARN_ON(IS_ROOT(dentry2));
2617 __d_move(dentry1, dentry2, true);
2619 write_sequnlock(&rename_lock);
2623 * d_ancestor - search for an ancestor
2624 * @p1: ancestor dentry
2625 * @p2: child dentry
2627 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2628 * an ancestor of p2, else NULL.
2630 struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
2632 struct dentry *p;
2634 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
2635 if (p->d_parent == p1)
2636 return p;
2638 return NULL;
2642 * This helper attempts to cope with remotely renamed directories
2644 * It assumes that the caller is already holding
2645 * dentry->d_parent->d_inode->i_mutex, inode->i_lock and rename_lock
2647 * Note: If ever the locking in lock_rename() changes, then please
2648 * remember to update this too...
2650 static struct dentry *__d_unalias(struct inode *inode,
2651 struct dentry *dentry, struct dentry *alias)
2653 struct mutex *m1 = NULL, *m2 = NULL;
2654 struct dentry *ret = ERR_PTR(-EBUSY);
2656 /* If alias and dentry share a parent, then no extra locks required */
2657 if (alias->d_parent == dentry->d_parent)
2658 goto out_unalias;
2660 /* See lock_rename() */
2661 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2662 goto out_err;
2663 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2664 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2665 goto out_err;
2666 m2 = &alias->d_parent->d_inode->i_mutex;
2667 out_unalias:
2668 if (likely(!d_mountpoint(alias))) {
2669 __d_move(alias, dentry, false);
2670 ret = alias;
2672 out_err:
2673 spin_unlock(&inode->i_lock);
2674 if (m2)
2675 mutex_unlock(m2);
2676 if (m1)
2677 mutex_unlock(m1);
2678 return ret;
2682 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2683 * named dentry in place of the dentry to be replaced.
2684 * returns with anon->d_lock held!
2686 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2688 struct dentry *dparent;
2690 dentry_lock_for_move(anon, dentry);
2692 write_seqcount_begin(&dentry->d_seq);
2693 write_seqcount_begin_nested(&anon->d_seq, DENTRY_D_LOCK_NESTED);
2695 dparent = dentry->d_parent;
2697 switch_names(dentry, anon, false);
2699 dentry->d_parent = dentry;
2700 list_del_init(&dentry->d_child);
2701 anon->d_parent = dparent;
2702 list_move(&anon->d_child, &dparent->d_subdirs);
2704 write_seqcount_end(&dentry->d_seq);
2705 write_seqcount_end(&anon->d_seq);
2707 dentry_unlock_parents_for_move(anon, dentry);
2708 spin_unlock(&dentry->d_lock);
2710 /* anon->d_lock still locked, returns locked */
2714 * d_materialise_unique - introduce an inode into the tree
2715 * @dentry: candidate dentry
2716 * @inode: inode to bind to the dentry, to which aliases may be attached
2718 * Introduces an dentry into the tree, substituting an extant disconnected
2719 * root directory alias in its place if there is one. Caller must hold the
2720 * i_mutex of the parent directory.
2722 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2724 struct dentry *actual;
2726 BUG_ON(!d_unhashed(dentry));
2728 if (!inode) {
2729 actual = dentry;
2730 __d_instantiate(dentry, NULL);
2731 d_rehash(actual);
2732 goto out_nolock;
2735 spin_lock(&inode->i_lock);
2737 if (S_ISDIR(inode->i_mode)) {
2738 struct dentry *alias;
2740 /* Does an aliased dentry already exist? */
2741 alias = __d_find_alias(inode, 0);
2742 if (alias) {
2743 actual = alias;
2744 write_seqlock(&rename_lock);
2746 if (d_ancestor(alias, dentry)) {
2747 /* Check for loops */
2748 actual = ERR_PTR(-ELOOP);
2749 spin_unlock(&inode->i_lock);
2750 } else if (IS_ROOT(alias)) {
2751 /* Is this an anonymous mountpoint that we
2752 * could splice into our tree? */
2753 __d_materialise_dentry(dentry, alias);
2754 write_sequnlock(&rename_lock);
2755 __d_drop(alias);
2756 goto found;
2757 } else {
2758 /* Nope, but we must(!) avoid directory
2759 * aliasing. This drops inode->i_lock */
2760 actual = __d_unalias(inode, dentry, alias);
2762 write_sequnlock(&rename_lock);
2763 if (IS_ERR(actual)) {
2764 if (PTR_ERR(actual) == -ELOOP)
2765 pr_warn_ratelimited(
2766 "VFS: Lookup of '%s' in %s %s"
2767 " would have caused loop\n",
2768 dentry->d_name.name,
2769 inode->i_sb->s_type->name,
2770 inode->i_sb->s_id);
2771 dput(alias);
2773 goto out_nolock;
2777 /* Add a unique reference */
2778 actual = __d_instantiate_unique(dentry, inode);
2779 if (!actual)
2780 actual = dentry;
2781 else
2782 BUG_ON(!d_unhashed(actual));
2784 spin_lock(&actual->d_lock);
2785 found:
2786 _d_rehash(actual);
2787 spin_unlock(&actual->d_lock);
2788 spin_unlock(&inode->i_lock);
2789 out_nolock:
2790 if (actual == dentry) {
2791 security_d_instantiate(dentry, inode);
2792 return NULL;
2795 iput(inode);
2796 return actual;
2798 EXPORT_SYMBOL_GPL(d_materialise_unique);
2800 static int prepend(char **buffer, int *buflen, const char *str, int namelen)
2802 *buflen -= namelen;
2803 if (*buflen < 0)
2804 return -ENAMETOOLONG;
2805 *buffer -= namelen;
2806 memcpy(*buffer, str, namelen);
2807 return 0;
2811 * prepend_name - prepend a pathname in front of current buffer pointer
2812 * @buffer: buffer pointer
2813 * @buflen: allocated length of the buffer
2814 * @name: name string and length qstr structure
2816 * With RCU path tracing, it may race with d_move(). Use ACCESS_ONCE() to
2817 * make sure that either the old or the new name pointer and length are
2818 * fetched. However, there may be mismatch between length and pointer.
2819 * The length cannot be trusted, we need to copy it byte-by-byte until
2820 * the length is reached or a null byte is found. It also prepends "/" at
2821 * the beginning of the name. The sequence number check at the caller will
2822 * retry it again when a d_move() does happen. So any garbage in the buffer
2823 * due to mismatched pointer and length will be discarded.
2825 * Data dependency barrier is needed to make sure that we see that terminating
2826 * NUL. Alpha strikes again, film at 11...
2828 static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2830 const char *dname = ACCESS_ONCE(name->name);
2831 u32 dlen = ACCESS_ONCE(name->len);
2832 char *p;
2834 smp_read_barrier_depends();
2836 *buflen -= dlen + 1;
2837 if (*buflen < 0)
2838 return -ENAMETOOLONG;
2839 p = *buffer -= dlen + 1;
2840 *p++ = '/';
2841 while (dlen--) {
2842 char c = *dname++;
2843 if (!c)
2844 break;
2845 *p++ = c;
2847 return 0;
2851 * prepend_path - Prepend path string to a buffer
2852 * @path: the dentry/vfsmount to report
2853 * @root: root vfsmnt/dentry
2854 * @buffer: pointer to the end of the buffer
2855 * @buflen: pointer to buffer length
2857 * The function will first try to write out the pathname without taking any
2858 * lock other than the RCU read lock to make sure that dentries won't go away.
2859 * It only checks the sequence number of the global rename_lock as any change
2860 * in the dentry's d_seq will be preceded by changes in the rename_lock
2861 * sequence number. If the sequence number had been changed, it will restart
2862 * the whole pathname back-tracing sequence again by taking the rename_lock.
2863 * In this case, there is no need to take the RCU read lock as the recursive
2864 * parent pointer references will keep the dentry chain alive as long as no
2865 * rename operation is performed.
2867 static int prepend_path(const struct path *path,
2868 const struct path *root,
2869 char **buffer, int *buflen)
2871 struct dentry *dentry;
2872 struct vfsmount *vfsmnt;
2873 struct mount *mnt;
2874 int error = 0;
2875 unsigned seq, m_seq = 0;
2876 char *bptr;
2877 int blen;
2879 rcu_read_lock();
2880 restart_mnt:
2881 read_seqbegin_or_lock(&mount_lock, &m_seq);
2882 seq = 0;
2883 rcu_read_lock();
2884 restart:
2885 bptr = *buffer;
2886 blen = *buflen;
2887 error = 0;
2888 dentry = path->dentry;
2889 vfsmnt = path->mnt;
2890 mnt = real_mount(vfsmnt);
2891 read_seqbegin_or_lock(&rename_lock, &seq);
2892 while (dentry != root->dentry || vfsmnt != root->mnt) {
2893 struct dentry * parent;
2895 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2896 struct mount *parent = ACCESS_ONCE(mnt->mnt_parent);
2897 /* Escaped? */
2898 if (dentry != vfsmnt->mnt_root) {
2899 bptr = *buffer;
2900 blen = *buflen;
2901 error = 3;
2902 break;
2904 /* Global root? */
2905 if (mnt != parent) {
2906 dentry = ACCESS_ONCE(mnt->mnt_mountpoint);
2907 mnt = parent;
2908 vfsmnt = &mnt->mnt;
2909 continue;
2911 if (!error)
2912 error = is_mounted(vfsmnt) ? 1 : 2;
2913 break;
2915 parent = dentry->d_parent;
2916 prefetch(parent);
2917 error = prepend_name(&bptr, &blen, &dentry->d_name);
2918 if (error)
2919 break;
2921 dentry = parent;
2923 if (!(seq & 1))
2924 rcu_read_unlock();
2925 if (need_seqretry(&rename_lock, seq)) {
2926 seq = 1;
2927 goto restart;
2929 done_seqretry(&rename_lock, seq);
2931 if (!(m_seq & 1))
2932 rcu_read_unlock();
2933 if (need_seqretry(&mount_lock, m_seq)) {
2934 m_seq = 1;
2935 goto restart_mnt;
2937 done_seqretry(&mount_lock, m_seq);
2939 if (error >= 0 && bptr == *buffer) {
2940 if (--blen < 0)
2941 error = -ENAMETOOLONG;
2942 else
2943 *--bptr = '/';
2945 *buffer = bptr;
2946 *buflen = blen;
2947 return error;
2951 * __d_path - return the path of a dentry
2952 * @path: the dentry/vfsmount to report
2953 * @root: root vfsmnt/dentry
2954 * @buf: buffer to return value in
2955 * @buflen: buffer length
2957 * Convert a dentry into an ASCII path name.
2959 * Returns a pointer into the buffer or an error code if the
2960 * path was too long.
2962 * "buflen" should be positive.
2964 * If the path is not reachable from the supplied root, return %NULL.
2966 char *__d_path(const struct path *path,
2967 const struct path *root,
2968 char *buf, int buflen)
2970 char *res = buf + buflen;
2971 int error;
2973 prepend(&res, &buflen, "\0", 1);
2974 error = prepend_path(path, root, &res, &buflen);
2976 if (error < 0)
2977 return ERR_PTR(error);
2978 if (error > 0)
2979 return NULL;
2980 return res;
2983 char *d_absolute_path(const struct path *path,
2984 char *buf, int buflen)
2986 struct path root = {};
2987 char *res = buf + buflen;
2988 int error;
2990 prepend(&res, &buflen, "\0", 1);
2991 error = prepend_path(path, &root, &res, &buflen);
2993 if (error > 1)
2994 error = -EINVAL;
2995 if (error < 0)
2996 return ERR_PTR(error);
2997 return res;
3001 * same as __d_path but appends "(deleted)" for unlinked files.
3003 static int path_with_deleted(const struct path *path,
3004 const struct path *root,
3005 char **buf, int *buflen)
3007 prepend(buf, buflen, "\0", 1);
3008 if (d_unlinked(path->dentry)) {
3009 int error = prepend(buf, buflen, " (deleted)", 10);
3010 if (error)
3011 return error;
3014 return prepend_path(path, root, buf, buflen);
3017 static int prepend_unreachable(char **buffer, int *buflen)
3019 return prepend(buffer, buflen, "(unreachable)", 13);
3022 static void get_fs_root_rcu(struct fs_struct *fs, struct path *root)
3024 unsigned seq;
3026 do {
3027 seq = read_seqcount_begin(&fs->seq);
3028 *root = fs->root;
3029 } while (read_seqcount_retry(&fs->seq, seq));
3033 * d_path - return the path of a dentry
3034 * @path: path to report
3035 * @buf: buffer to return value in
3036 * @buflen: buffer length
3038 * Convert a dentry into an ASCII path name. If the entry has been deleted
3039 * the string " (deleted)" is appended. Note that this is ambiguous.
3041 * Returns a pointer into the buffer or an error code if the path was
3042 * too long. Note: Callers should use the returned pointer, not the passed
3043 * in buffer, to use the name! The implementation often starts at an offset
3044 * into the buffer, and may leave 0 bytes at the start.
3046 * "buflen" should be positive.
3048 char *d_path(const struct path *path, char *buf, int buflen)
3050 char *res = buf + buflen;
3051 struct path root;
3052 int error;
3055 * We have various synthetic filesystems that never get mounted. On
3056 * these filesystems dentries are never used for lookup purposes, and
3057 * thus don't need to be hashed. They also don't need a name until a
3058 * user wants to identify the object in /proc/pid/fd/. The little hack
3059 * below allows us to generate a name for these objects on demand:
3061 * Some pseudo inodes are mountable. When they are mounted
3062 * path->dentry == path->mnt->mnt_root. In that case don't call d_dname
3063 * and instead have d_path return the mounted path.
3065 if (path->dentry->d_op && path->dentry->d_op->d_dname &&
3066 (!IS_ROOT(path->dentry) || path->dentry != path->mnt->mnt_root))
3067 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
3069 rcu_read_lock();
3070 get_fs_root_rcu(current->fs, &root);
3071 error = path_with_deleted(path, &root, &res, &buflen);
3072 rcu_read_unlock();
3074 if (error < 0)
3075 res = ERR_PTR(error);
3076 return res;
3078 EXPORT_SYMBOL(d_path);
3081 * Helper function for dentry_operations.d_dname() members
3083 char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
3084 const char *fmt, ...)
3086 va_list args;
3087 char temp[64];
3088 int sz;
3090 va_start(args, fmt);
3091 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
3092 va_end(args);
3094 if (sz > sizeof(temp) || sz > buflen)
3095 return ERR_PTR(-ENAMETOOLONG);
3097 buffer += buflen - sz;
3098 return memcpy(buffer, temp, sz);
3101 char *simple_dname(struct dentry *dentry, char *buffer, int buflen)
3103 char *end = buffer + buflen;
3104 /* these dentries are never renamed, so d_lock is not needed */
3105 if (prepend(&end, &buflen, " (deleted)", 11) ||
3106 prepend(&end, &buflen, dentry->d_name.name, dentry->d_name.len) ||
3107 prepend(&end, &buflen, "/", 1))
3108 end = ERR_PTR(-ENAMETOOLONG);
3109 return end;
3111 EXPORT_SYMBOL(simple_dname);
3114 * Write full pathname from the root of the filesystem into the buffer.
3116 static char *__dentry_path(struct dentry *d, char *buf, int buflen)
3118 struct dentry *dentry;
3119 char *end, *retval;
3120 int len, seq = 0;
3121 int error = 0;
3123 if (buflen < 2)
3124 goto Elong;
3126 rcu_read_lock();
3127 restart:
3128 dentry = d;
3129 end = buf + buflen;
3130 len = buflen;
3131 prepend(&end, &len, "\0", 1);
3132 /* Get '/' right */
3133 retval = end-1;
3134 *retval = '/';
3135 read_seqbegin_or_lock(&rename_lock, &seq);
3136 while (!IS_ROOT(dentry)) {
3137 struct dentry *parent = dentry->d_parent;
3139 prefetch(parent);
3140 error = prepend_name(&end, &len, &dentry->d_name);
3141 if (error)
3142 break;
3144 retval = end;
3145 dentry = parent;
3147 if (!(seq & 1))
3148 rcu_read_unlock();
3149 if (need_seqretry(&rename_lock, seq)) {
3150 seq = 1;
3151 goto restart;
3153 done_seqretry(&rename_lock, seq);
3154 if (error)
3155 goto Elong;
3156 return retval;
3157 Elong:
3158 return ERR_PTR(-ENAMETOOLONG);
3161 char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
3163 return __dentry_path(dentry, buf, buflen);
3165 EXPORT_SYMBOL(dentry_path_raw);
3167 char *dentry_path(struct dentry *dentry, char *buf, int buflen)
3169 char *p = NULL;
3170 char *retval;
3172 if (d_unlinked(dentry)) {
3173 p = buf + buflen;
3174 if (prepend(&p, &buflen, "//deleted", 10) != 0)
3175 goto Elong;
3176 buflen++;
3178 retval = __dentry_path(dentry, buf, buflen);
3179 if (!IS_ERR(retval) && p)
3180 *p = '/'; /* restore '/' overriden with '\0' */
3181 return retval;
3182 Elong:
3183 return ERR_PTR(-ENAMETOOLONG);
3186 static void get_fs_root_and_pwd_rcu(struct fs_struct *fs, struct path *root,
3187 struct path *pwd)
3189 unsigned seq;
3191 do {
3192 seq = read_seqcount_begin(&fs->seq);
3193 *root = fs->root;
3194 *pwd = fs->pwd;
3195 } while (read_seqcount_retry(&fs->seq, seq));
3199 * NOTE! The user-level library version returns a
3200 * character pointer. The kernel system call just
3201 * returns the length of the buffer filled (which
3202 * includes the ending '\0' character), or a negative
3203 * error value. So libc would do something like
3205 * char *getcwd(char * buf, size_t size)
3207 * int retval;
3209 * retval = sys_getcwd(buf, size);
3210 * if (retval >= 0)
3211 * return buf;
3212 * errno = -retval;
3213 * return NULL;
3216 SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
3218 int error;
3219 struct path pwd, root;
3220 char *page = __getname();
3222 if (!page)
3223 return -ENOMEM;
3225 rcu_read_lock();
3226 get_fs_root_and_pwd_rcu(current->fs, &root, &pwd);
3228 error = -ENOENT;
3229 if (!d_unlinked(pwd.dentry)) {
3230 unsigned long len;
3231 char *cwd = page + PATH_MAX;
3232 int buflen = PATH_MAX;
3234 prepend(&cwd, &buflen, "\0", 1);
3235 error = prepend_path(&pwd, &root, &cwd, &buflen);
3236 rcu_read_unlock();
3238 if (error < 0)
3239 goto out;
3241 /* Unreachable from current root */
3242 if (error > 0) {
3243 error = prepend_unreachable(&cwd, &buflen);
3244 if (error)
3245 goto out;
3248 error = -ERANGE;
3249 len = PATH_MAX + page - cwd;
3250 if (len <= size) {
3251 error = len;
3252 if (copy_to_user(buf, cwd, len))
3253 error = -EFAULT;
3255 } else {
3256 rcu_read_unlock();
3259 out:
3260 __putname(page);
3261 return error;
3265 * Test whether new_dentry is a subdirectory of old_dentry.
3267 * Trivially implemented using the dcache structure
3271 * is_subdir - is new dentry a subdirectory of old_dentry
3272 * @new_dentry: new dentry
3273 * @old_dentry: old dentry
3275 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
3276 * Returns 0 otherwise.
3277 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
3280 int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
3282 int result;
3283 unsigned seq;
3285 if (new_dentry == old_dentry)
3286 return 1;
3288 do {
3289 /* for restarting inner loop in case of seq retry */
3290 seq = read_seqbegin(&rename_lock);
3292 * Need rcu_readlock to protect against the d_parent trashing
3293 * due to d_move
3295 rcu_read_lock();
3296 if (d_ancestor(old_dentry, new_dentry))
3297 result = 1;
3298 else
3299 result = 0;
3300 rcu_read_unlock();
3301 } while (read_seqretry(&rename_lock, seq));
3303 return result;
3306 static enum d_walk_ret d_genocide_kill(void *data, struct dentry *dentry)
3308 struct dentry *root = data;
3309 if (dentry != root) {
3310 if (d_unhashed(dentry) || !dentry->d_inode)
3311 return D_WALK_SKIP;
3313 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
3314 dentry->d_flags |= DCACHE_GENOCIDE;
3315 dentry->d_lockref.count--;
3318 return D_WALK_CONTINUE;
3321 void d_genocide(struct dentry *parent)
3323 d_walk(parent, parent, d_genocide_kill, NULL);
3326 void d_tmpfile(struct dentry *dentry, struct inode *inode)
3328 inode_dec_link_count(inode);
3329 BUG_ON(dentry->d_name.name != dentry->d_iname ||
3330 !hlist_unhashed(&dentry->d_u.d_alias) ||
3331 !d_unlinked(dentry));
3332 spin_lock(&dentry->d_parent->d_lock);
3333 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
3334 dentry->d_name.len = sprintf(dentry->d_iname, "#%llu",
3335 (unsigned long long)inode->i_ino);
3336 spin_unlock(&dentry->d_lock);
3337 spin_unlock(&dentry->d_parent->d_lock);
3338 d_instantiate(dentry, inode);
3340 EXPORT_SYMBOL(d_tmpfile);
3342 static __initdata unsigned long dhash_entries;
3343 static int __init set_dhash_entries(char *str)
3345 if (!str)
3346 return 0;
3347 dhash_entries = simple_strtoul(str, &str, 0);
3348 return 1;
3350 __setup("dhash_entries=", set_dhash_entries);
3352 static void __init dcache_init_early(void)
3354 unsigned int loop;
3356 /* If hashes are distributed across NUMA nodes, defer
3357 * hash allocation until vmalloc space is available.
3359 if (hashdist)
3360 return;
3362 dentry_hashtable =
3363 alloc_large_system_hash("Dentry cache",
3364 sizeof(struct hlist_bl_head),
3365 dhash_entries,
3367 HASH_EARLY,
3368 &d_hash_shift,
3369 &d_hash_mask,
3373 for (loop = 0; loop < (1U << d_hash_shift); loop++)
3374 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
3377 static void __init dcache_init(void)
3379 unsigned int loop;
3382 * A constructor could be added for stable state like the lists,
3383 * but it is probably not worth it because of the cache nature
3384 * of the dcache.
3386 dentry_cache = KMEM_CACHE(dentry,
3387 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
3389 /* Hash may have been set up in dcache_init_early */
3390 if (!hashdist)
3391 return;
3393 dentry_hashtable =
3394 alloc_large_system_hash("Dentry cache",
3395 sizeof(struct hlist_bl_head),
3396 dhash_entries,
3399 &d_hash_shift,
3400 &d_hash_mask,
3404 for (loop = 0; loop < (1U << d_hash_shift); loop++)
3405 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
3408 /* SLAB cache for __getname() consumers */
3409 struct kmem_cache *names_cachep __read_mostly;
3410 EXPORT_SYMBOL(names_cachep);
3412 EXPORT_SYMBOL(d_genocide);
3414 void __init vfs_caches_init_early(void)
3416 dcache_init_early();
3417 inode_init_early();
3420 void __init vfs_caches_init(unsigned long mempages)
3422 unsigned long reserve;
3424 /* Base hash sizes on available memory, with a reserve equal to
3425 150% of current kernel size */
3427 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
3428 mempages -= reserve;
3430 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
3431 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
3433 dcache_init();
3434 inode_init();
3435 files_init(mempages);
3436 mnt_init();
3437 bdev_cache_init();
3438 chrdev_init();