drm/i915: make the panel fitter work on pipes B and C on IVB
[linux/fpc-iii.git] / fs / dcache.c
blob8b64f383570b1db7af68a1c39f65e07fb5217d04
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/module.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 "internal.h"
42 * Usage:
43 * dcache->d_inode->i_lock protects:
44 * - i_dentry, d_alias, d_inode of aliases
45 * dcache_hash_bucket lock protects:
46 * - the dcache hash table
47 * s_anon bl list spinlock protects:
48 * - the s_anon list (see __d_drop)
49 * dcache_lru_lock protects:
50 * - the dcache lru lists and counters
51 * d_lock protects:
52 * - d_flags
53 * - d_name
54 * - d_lru
55 * - d_count
56 * - d_unhashed()
57 * - d_parent and d_subdirs
58 * - childrens' d_child and d_parent
59 * - d_alias, d_inode
61 * Ordering:
62 * dentry->d_inode->i_lock
63 * dentry->d_lock
64 * dcache_lru_lock
65 * dcache_hash_bucket lock
66 * s_anon lock
68 * If there is an ancestor relationship:
69 * dentry->d_parent->...->d_parent->d_lock
70 * ...
71 * dentry->d_parent->d_lock
72 * dentry->d_lock
74 * If no ancestor relationship:
75 * if (dentry1 < dentry2)
76 * dentry1->d_lock
77 * dentry2->d_lock
79 int sysctl_vfs_cache_pressure __read_mostly = 100;
80 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
82 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
83 __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
85 EXPORT_SYMBOL(rename_lock);
87 static struct kmem_cache *dentry_cache __read_mostly;
90 * This is the single most critical data structure when it comes
91 * to the dcache: the hashtable for lookups. Somebody should try
92 * to make this good - I've just made it work.
94 * This hash-function tries to avoid losing too many bits of hash
95 * information, yet avoid using a prime hash-size or similar.
97 #define D_HASHBITS d_hash_shift
98 #define D_HASHMASK d_hash_mask
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(struct dentry *parent,
106 unsigned long hash)
108 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
109 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
110 return dentry_hashtable + (hash & D_HASHMASK);
113 /* Statistics gathering. */
114 struct dentry_stat_t dentry_stat = {
115 .age_limit = 45,
118 static DEFINE_PER_CPU(unsigned int, nr_dentry);
120 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
121 static int get_nr_dentry(void)
123 int i;
124 int sum = 0;
125 for_each_possible_cpu(i)
126 sum += per_cpu(nr_dentry, i);
127 return sum < 0 ? 0 : sum;
130 int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
131 size_t *lenp, loff_t *ppos)
133 dentry_stat.nr_dentry = get_nr_dentry();
134 return proc_dointvec(table, write, buffer, lenp, ppos);
136 #endif
138 static void __d_free(struct rcu_head *head)
140 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
142 WARN_ON(!list_empty(&dentry->d_alias));
143 if (dname_external(dentry))
144 kfree(dentry->d_name.name);
145 kmem_cache_free(dentry_cache, dentry);
149 * no locks, please.
151 static void d_free(struct dentry *dentry)
153 BUG_ON(dentry->d_count);
154 this_cpu_dec(nr_dentry);
155 if (dentry->d_op && dentry->d_op->d_release)
156 dentry->d_op->d_release(dentry);
158 /* if dentry was never visible to RCU, immediate free is OK */
159 if (!(dentry->d_flags & DCACHE_RCUACCESS))
160 __d_free(&dentry->d_u.d_rcu);
161 else
162 call_rcu(&dentry->d_u.d_rcu, __d_free);
166 * dentry_rcuwalk_barrier - invalidate in-progress rcu-walk lookups
167 * @dentry: the target dentry
168 * After this call, in-progress rcu-walk path lookup will fail. This
169 * should be called after unhashing, and after changing d_inode (if
170 * the dentry has not already been unhashed).
172 static inline void dentry_rcuwalk_barrier(struct dentry *dentry)
174 assert_spin_locked(&dentry->d_lock);
175 /* Go through a barrier */
176 write_seqcount_barrier(&dentry->d_seq);
180 * Release the dentry's inode, using the filesystem
181 * d_iput() operation if defined. Dentry has no refcount
182 * and is unhashed.
184 static void dentry_iput(struct dentry * dentry)
185 __releases(dentry->d_lock)
186 __releases(dentry->d_inode->i_lock)
188 struct inode *inode = dentry->d_inode;
189 if (inode) {
190 dentry->d_inode = NULL;
191 list_del_init(&dentry->d_alias);
192 spin_unlock(&dentry->d_lock);
193 spin_unlock(&inode->i_lock);
194 if (!inode->i_nlink)
195 fsnotify_inoderemove(inode);
196 if (dentry->d_op && dentry->d_op->d_iput)
197 dentry->d_op->d_iput(dentry, inode);
198 else
199 iput(inode);
200 } else {
201 spin_unlock(&dentry->d_lock);
206 * Release the dentry's inode, using the filesystem
207 * d_iput() operation if defined. dentry remains in-use.
209 static void dentry_unlink_inode(struct dentry * dentry)
210 __releases(dentry->d_lock)
211 __releases(dentry->d_inode->i_lock)
213 struct inode *inode = dentry->d_inode;
214 dentry->d_inode = NULL;
215 list_del_init(&dentry->d_alias);
216 dentry_rcuwalk_barrier(dentry);
217 spin_unlock(&dentry->d_lock);
218 spin_unlock(&inode->i_lock);
219 if (!inode->i_nlink)
220 fsnotify_inoderemove(inode);
221 if (dentry->d_op && dentry->d_op->d_iput)
222 dentry->d_op->d_iput(dentry, inode);
223 else
224 iput(inode);
228 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
230 static void dentry_lru_add(struct dentry *dentry)
232 if (list_empty(&dentry->d_lru)) {
233 spin_lock(&dcache_lru_lock);
234 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
235 dentry->d_sb->s_nr_dentry_unused++;
236 dentry_stat.nr_unused++;
237 spin_unlock(&dcache_lru_lock);
241 static void __dentry_lru_del(struct dentry *dentry)
243 list_del_init(&dentry->d_lru);
244 dentry->d_flags &= ~DCACHE_SHRINK_LIST;
245 dentry->d_sb->s_nr_dentry_unused--;
246 dentry_stat.nr_unused--;
249 static void dentry_lru_del(struct dentry *dentry)
251 if (!list_empty(&dentry->d_lru)) {
252 spin_lock(&dcache_lru_lock);
253 __dentry_lru_del(dentry);
254 spin_unlock(&dcache_lru_lock);
258 static void dentry_lru_move_tail(struct dentry *dentry)
260 spin_lock(&dcache_lru_lock);
261 if (list_empty(&dentry->d_lru)) {
262 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
263 dentry->d_sb->s_nr_dentry_unused++;
264 dentry_stat.nr_unused++;
265 } else {
266 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
268 spin_unlock(&dcache_lru_lock);
272 * d_kill - kill dentry and return parent
273 * @dentry: dentry to kill
274 * @parent: parent dentry
276 * The dentry must already be unhashed and removed from the LRU.
278 * If this is the root of the dentry tree, return NULL.
280 * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by
281 * d_kill.
283 static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
284 __releases(dentry->d_lock)
285 __releases(parent->d_lock)
286 __releases(dentry->d_inode->i_lock)
288 list_del(&dentry->d_u.d_child);
290 * Inform try_to_ascend() that we are no longer attached to the
291 * dentry tree
293 dentry->d_flags |= DCACHE_DENTRY_KILLED;
294 if (parent)
295 spin_unlock(&parent->d_lock);
296 dentry_iput(dentry);
298 * dentry_iput drops the locks, at which point nobody (except
299 * transient RCU lookups) can reach this dentry.
301 d_free(dentry);
302 return parent;
306 * d_drop - drop a dentry
307 * @dentry: dentry to drop
309 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
310 * be found through a VFS lookup any more. Note that this is different from
311 * deleting the dentry - d_delete will try to mark the dentry negative if
312 * possible, giving a successful _negative_ lookup, while d_drop will
313 * just make the cache lookup fail.
315 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
316 * reason (NFS timeouts or autofs deletes).
318 * __d_drop requires dentry->d_lock.
320 void __d_drop(struct dentry *dentry)
322 if (!d_unhashed(dentry)) {
323 struct hlist_bl_head *b;
324 if (unlikely(dentry->d_flags & DCACHE_DISCONNECTED))
325 b = &dentry->d_sb->s_anon;
326 else
327 b = d_hash(dentry->d_parent, dentry->d_name.hash);
329 hlist_bl_lock(b);
330 __hlist_bl_del(&dentry->d_hash);
331 dentry->d_hash.pprev = NULL;
332 hlist_bl_unlock(b);
334 dentry_rcuwalk_barrier(dentry);
337 EXPORT_SYMBOL(__d_drop);
339 void d_drop(struct dentry *dentry)
341 spin_lock(&dentry->d_lock);
342 __d_drop(dentry);
343 spin_unlock(&dentry->d_lock);
345 EXPORT_SYMBOL(d_drop);
348 * Finish off a dentry we've decided to kill.
349 * dentry->d_lock must be held, returns with it unlocked.
350 * If ref is non-zero, then decrement the refcount too.
351 * Returns dentry requiring refcount drop, or NULL if we're done.
353 static inline struct dentry *dentry_kill(struct dentry *dentry, int ref)
354 __releases(dentry->d_lock)
356 struct inode *inode;
357 struct dentry *parent;
359 inode = dentry->d_inode;
360 if (inode && !spin_trylock(&inode->i_lock)) {
361 relock:
362 spin_unlock(&dentry->d_lock);
363 cpu_relax();
364 return dentry; /* try again with same dentry */
366 if (IS_ROOT(dentry))
367 parent = NULL;
368 else
369 parent = dentry->d_parent;
370 if (parent && !spin_trylock(&parent->d_lock)) {
371 if (inode)
372 spin_unlock(&inode->i_lock);
373 goto relock;
376 if (ref)
377 dentry->d_count--;
378 /* if dentry was on the d_lru list delete it from there */
379 dentry_lru_del(dentry);
380 /* if it was on the hash then remove it */
381 __d_drop(dentry);
382 return d_kill(dentry, parent);
386 * This is dput
388 * This is complicated by the fact that we do not want to put
389 * dentries that are no longer on any hash chain on the unused
390 * list: we'd much rather just get rid of them immediately.
392 * However, that implies that we have to traverse the dentry
393 * tree upwards to the parents which might _also_ now be
394 * scheduled for deletion (it may have been only waiting for
395 * its last child to go away).
397 * This tail recursion is done by hand as we don't want to depend
398 * on the compiler to always get this right (gcc generally doesn't).
399 * Real recursion would eat up our stack space.
403 * dput - release a dentry
404 * @dentry: dentry to release
406 * Release a dentry. This will drop the usage count and if appropriate
407 * call the dentry unlink method as well as removing it from the queues and
408 * releasing its resources. If the parent dentries were scheduled for release
409 * they too may now get deleted.
411 void dput(struct dentry *dentry)
413 if (!dentry)
414 return;
416 repeat:
417 if (dentry->d_count == 1)
418 might_sleep();
419 spin_lock(&dentry->d_lock);
420 BUG_ON(!dentry->d_count);
421 if (dentry->d_count > 1) {
422 dentry->d_count--;
423 spin_unlock(&dentry->d_lock);
424 return;
427 if (dentry->d_flags & DCACHE_OP_DELETE) {
428 if (dentry->d_op->d_delete(dentry))
429 goto kill_it;
432 /* Unreachable? Get rid of it */
433 if (d_unhashed(dentry))
434 goto kill_it;
436 /* Otherwise leave it cached and ensure it's on the LRU */
437 dentry->d_flags |= DCACHE_REFERENCED;
438 dentry_lru_add(dentry);
440 dentry->d_count--;
441 spin_unlock(&dentry->d_lock);
442 return;
444 kill_it:
445 dentry = dentry_kill(dentry, 1);
446 if (dentry)
447 goto repeat;
449 EXPORT_SYMBOL(dput);
452 * d_invalidate - invalidate a dentry
453 * @dentry: dentry to invalidate
455 * Try to invalidate the dentry if it turns out to be
456 * possible. If there are other dentries that can be
457 * reached through this one we can't delete it and we
458 * return -EBUSY. On success we return 0.
460 * no dcache lock.
463 int d_invalidate(struct dentry * dentry)
466 * If it's already been dropped, return OK.
468 spin_lock(&dentry->d_lock);
469 if (d_unhashed(dentry)) {
470 spin_unlock(&dentry->d_lock);
471 return 0;
474 * Check whether to do a partial shrink_dcache
475 * to get rid of unused child entries.
477 if (!list_empty(&dentry->d_subdirs)) {
478 spin_unlock(&dentry->d_lock);
479 shrink_dcache_parent(dentry);
480 spin_lock(&dentry->d_lock);
484 * Somebody else still using it?
486 * If it's a directory, we can't drop it
487 * for fear of somebody re-populating it
488 * with children (even though dropping it
489 * would make it unreachable from the root,
490 * we might still populate it if it was a
491 * working directory or similar).
493 if (dentry->d_count > 1) {
494 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
495 spin_unlock(&dentry->d_lock);
496 return -EBUSY;
500 __d_drop(dentry);
501 spin_unlock(&dentry->d_lock);
502 return 0;
504 EXPORT_SYMBOL(d_invalidate);
506 /* This must be called with d_lock held */
507 static inline void __dget_dlock(struct dentry *dentry)
509 dentry->d_count++;
512 static inline void __dget(struct dentry *dentry)
514 spin_lock(&dentry->d_lock);
515 __dget_dlock(dentry);
516 spin_unlock(&dentry->d_lock);
519 struct dentry *dget_parent(struct dentry *dentry)
521 struct dentry *ret;
523 repeat:
525 * Don't need rcu_dereference because we re-check it was correct under
526 * the lock.
528 rcu_read_lock();
529 ret = dentry->d_parent;
530 if (!ret) {
531 rcu_read_unlock();
532 goto out;
534 spin_lock(&ret->d_lock);
535 if (unlikely(ret != dentry->d_parent)) {
536 spin_unlock(&ret->d_lock);
537 rcu_read_unlock();
538 goto repeat;
540 rcu_read_unlock();
541 BUG_ON(!ret->d_count);
542 ret->d_count++;
543 spin_unlock(&ret->d_lock);
544 out:
545 return ret;
547 EXPORT_SYMBOL(dget_parent);
550 * d_find_alias - grab a hashed alias of inode
551 * @inode: inode in question
552 * @want_discon: flag, used by d_splice_alias, to request
553 * that only a DISCONNECTED alias be returned.
555 * If inode has a hashed alias, or is a directory and has any alias,
556 * acquire the reference to alias and return it. Otherwise return NULL.
557 * Notice that if inode is a directory there can be only one alias and
558 * it can be unhashed only if it has no children, or if it is the root
559 * of a filesystem.
561 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
562 * any other hashed alias over that one unless @want_discon is set,
563 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
565 static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
567 struct dentry *alias, *discon_alias;
569 again:
570 discon_alias = NULL;
571 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
572 spin_lock(&alias->d_lock);
573 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
574 if (IS_ROOT(alias) &&
575 (alias->d_flags & DCACHE_DISCONNECTED)) {
576 discon_alias = alias;
577 } else if (!want_discon) {
578 __dget_dlock(alias);
579 spin_unlock(&alias->d_lock);
580 return alias;
583 spin_unlock(&alias->d_lock);
585 if (discon_alias) {
586 alias = discon_alias;
587 spin_lock(&alias->d_lock);
588 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
589 if (IS_ROOT(alias) &&
590 (alias->d_flags & DCACHE_DISCONNECTED)) {
591 __dget_dlock(alias);
592 spin_unlock(&alias->d_lock);
593 return alias;
596 spin_unlock(&alias->d_lock);
597 goto again;
599 return NULL;
602 struct dentry *d_find_alias(struct inode *inode)
604 struct dentry *de = NULL;
606 if (!list_empty(&inode->i_dentry)) {
607 spin_lock(&inode->i_lock);
608 de = __d_find_alias(inode, 0);
609 spin_unlock(&inode->i_lock);
611 return de;
613 EXPORT_SYMBOL(d_find_alias);
616 * Try to kill dentries associated with this inode.
617 * WARNING: you must own a reference to inode.
619 void d_prune_aliases(struct inode *inode)
621 struct dentry *dentry;
622 restart:
623 spin_lock(&inode->i_lock);
624 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
625 spin_lock(&dentry->d_lock);
626 if (!dentry->d_count) {
627 __dget_dlock(dentry);
628 __d_drop(dentry);
629 spin_unlock(&dentry->d_lock);
630 spin_unlock(&inode->i_lock);
631 dput(dentry);
632 goto restart;
634 spin_unlock(&dentry->d_lock);
636 spin_unlock(&inode->i_lock);
638 EXPORT_SYMBOL(d_prune_aliases);
641 * Try to throw away a dentry - free the inode, dput the parent.
642 * Requires dentry->d_lock is held, and dentry->d_count == 0.
643 * Releases dentry->d_lock.
645 * This may fail if locks cannot be acquired no problem, just try again.
647 static void try_prune_one_dentry(struct dentry *dentry)
648 __releases(dentry->d_lock)
650 struct dentry *parent;
652 parent = dentry_kill(dentry, 0);
654 * If dentry_kill returns NULL, we have nothing more to do.
655 * if it returns the same dentry, trylocks failed. In either
656 * case, just loop again.
658 * Otherwise, we need to prune ancestors too. This is necessary
659 * to prevent quadratic behavior of shrink_dcache_parent(), but
660 * is also expected to be beneficial in reducing dentry cache
661 * fragmentation.
663 if (!parent)
664 return;
665 if (parent == dentry)
666 return;
668 /* Prune ancestors. */
669 dentry = parent;
670 while (dentry) {
671 spin_lock(&dentry->d_lock);
672 if (dentry->d_count > 1) {
673 dentry->d_count--;
674 spin_unlock(&dentry->d_lock);
675 return;
677 dentry = dentry_kill(dentry, 1);
681 static void shrink_dentry_list(struct list_head *list)
683 struct dentry *dentry;
685 rcu_read_lock();
686 for (;;) {
687 dentry = list_entry_rcu(list->prev, struct dentry, d_lru);
688 if (&dentry->d_lru == list)
689 break; /* empty */
690 spin_lock(&dentry->d_lock);
691 if (dentry != list_entry(list->prev, struct dentry, d_lru)) {
692 spin_unlock(&dentry->d_lock);
693 continue;
697 * We found an inuse dentry which was not removed from
698 * the LRU because of laziness during lookup. Do not free
699 * it - just keep it off the LRU list.
701 if (dentry->d_count) {
702 dentry_lru_del(dentry);
703 spin_unlock(&dentry->d_lock);
704 continue;
707 rcu_read_unlock();
709 try_prune_one_dentry(dentry);
711 rcu_read_lock();
713 rcu_read_unlock();
717 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
718 * @sb: superblock to shrink dentry LRU.
719 * @count: number of entries to prune
720 * @flags: flags to control the dentry processing
722 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
724 static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
726 /* called from prune_dcache() and shrink_dcache_parent() */
727 struct dentry *dentry;
728 LIST_HEAD(referenced);
729 LIST_HEAD(tmp);
730 int cnt = *count;
732 relock:
733 spin_lock(&dcache_lru_lock);
734 while (!list_empty(&sb->s_dentry_lru)) {
735 dentry = list_entry(sb->s_dentry_lru.prev,
736 struct dentry, d_lru);
737 BUG_ON(dentry->d_sb != sb);
739 if (!spin_trylock(&dentry->d_lock)) {
740 spin_unlock(&dcache_lru_lock);
741 cpu_relax();
742 goto relock;
746 * If we are honouring the DCACHE_REFERENCED flag and the
747 * dentry has this flag set, don't free it. Clear the flag
748 * and put it back on the LRU.
750 if (flags & DCACHE_REFERENCED &&
751 dentry->d_flags & DCACHE_REFERENCED) {
752 dentry->d_flags &= ~DCACHE_REFERENCED;
753 list_move(&dentry->d_lru, &referenced);
754 spin_unlock(&dentry->d_lock);
755 } else {
756 list_move_tail(&dentry->d_lru, &tmp);
757 dentry->d_flags |= DCACHE_SHRINK_LIST;
758 spin_unlock(&dentry->d_lock);
759 if (!--cnt)
760 break;
762 cond_resched_lock(&dcache_lru_lock);
764 if (!list_empty(&referenced))
765 list_splice(&referenced, &sb->s_dentry_lru);
766 spin_unlock(&dcache_lru_lock);
768 shrink_dentry_list(&tmp);
770 *count = cnt;
774 * prune_dcache - shrink the dcache
775 * @count: number of entries to try to free
777 * Shrink the dcache. This is done when we need more memory, or simply when we
778 * need to unmount something (at which point we need to unuse all dentries).
780 * This function may fail to free any resources if all the dentries are in use.
782 static void prune_dcache(int count)
784 struct super_block *sb, *p = NULL;
785 int w_count;
786 int unused = dentry_stat.nr_unused;
787 int prune_ratio;
788 int pruned;
790 if (unused == 0 || count == 0)
791 return;
792 if (count >= unused)
793 prune_ratio = 1;
794 else
795 prune_ratio = unused / count;
796 spin_lock(&sb_lock);
797 list_for_each_entry(sb, &super_blocks, s_list) {
798 if (list_empty(&sb->s_instances))
799 continue;
800 if (sb->s_nr_dentry_unused == 0)
801 continue;
802 sb->s_count++;
803 /* Now, we reclaim unused dentrins with fairness.
804 * We reclaim them same percentage from each superblock.
805 * We calculate number of dentries to scan on this sb
806 * as follows, but the implementation is arranged to avoid
807 * overflows:
808 * number of dentries to scan on this sb =
809 * count * (number of dentries on this sb /
810 * number of dentries in the machine)
812 spin_unlock(&sb_lock);
813 if (prune_ratio != 1)
814 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
815 else
816 w_count = sb->s_nr_dentry_unused;
817 pruned = w_count;
819 * We need to be sure this filesystem isn't being unmounted,
820 * otherwise we could race with generic_shutdown_super(), and
821 * end up holding a reference to an inode while the filesystem
822 * is unmounted. So we try to get s_umount, and make sure
823 * s_root isn't NULL.
825 if (down_read_trylock(&sb->s_umount)) {
826 if ((sb->s_root != NULL) &&
827 (!list_empty(&sb->s_dentry_lru))) {
828 __shrink_dcache_sb(sb, &w_count,
829 DCACHE_REFERENCED);
830 pruned -= w_count;
832 up_read(&sb->s_umount);
834 spin_lock(&sb_lock);
835 if (p)
836 __put_super(p);
837 count -= pruned;
838 p = sb;
839 /* more work left to do? */
840 if (count <= 0)
841 break;
843 if (p)
844 __put_super(p);
845 spin_unlock(&sb_lock);
849 * shrink_dcache_sb - shrink dcache for a superblock
850 * @sb: superblock
852 * Shrink the dcache for the specified super block. This is used to free
853 * the dcache before unmounting a file system.
855 void shrink_dcache_sb(struct super_block *sb)
857 LIST_HEAD(tmp);
859 spin_lock(&dcache_lru_lock);
860 while (!list_empty(&sb->s_dentry_lru)) {
861 list_splice_init(&sb->s_dentry_lru, &tmp);
862 spin_unlock(&dcache_lru_lock);
863 shrink_dentry_list(&tmp);
864 spin_lock(&dcache_lru_lock);
866 spin_unlock(&dcache_lru_lock);
868 EXPORT_SYMBOL(shrink_dcache_sb);
871 * destroy a single subtree of dentries for unmount
872 * - see the comments on shrink_dcache_for_umount() for a description of the
873 * locking
875 static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
877 struct dentry *parent;
878 unsigned detached = 0;
880 BUG_ON(!IS_ROOT(dentry));
882 /* detach this root from the system */
883 spin_lock(&dentry->d_lock);
884 dentry_lru_del(dentry);
885 __d_drop(dentry);
886 spin_unlock(&dentry->d_lock);
888 for (;;) {
889 /* descend to the first leaf in the current subtree */
890 while (!list_empty(&dentry->d_subdirs)) {
891 struct dentry *loop;
893 /* this is a branch with children - detach all of them
894 * from the system in one go */
895 spin_lock(&dentry->d_lock);
896 list_for_each_entry(loop, &dentry->d_subdirs,
897 d_u.d_child) {
898 spin_lock_nested(&loop->d_lock,
899 DENTRY_D_LOCK_NESTED);
900 dentry_lru_del(loop);
901 __d_drop(loop);
902 spin_unlock(&loop->d_lock);
904 spin_unlock(&dentry->d_lock);
906 /* move to the first child */
907 dentry = list_entry(dentry->d_subdirs.next,
908 struct dentry, d_u.d_child);
911 /* consume the dentries from this leaf up through its parents
912 * until we find one with children or run out altogether */
913 do {
914 struct inode *inode;
916 if (dentry->d_count != 0) {
917 printk(KERN_ERR
918 "BUG: Dentry %p{i=%lx,n=%s}"
919 " still in use (%d)"
920 " [unmount of %s %s]\n",
921 dentry,
922 dentry->d_inode ?
923 dentry->d_inode->i_ino : 0UL,
924 dentry->d_name.name,
925 dentry->d_count,
926 dentry->d_sb->s_type->name,
927 dentry->d_sb->s_id);
928 BUG();
931 if (IS_ROOT(dentry)) {
932 parent = NULL;
933 list_del(&dentry->d_u.d_child);
934 } else {
935 parent = dentry->d_parent;
936 spin_lock(&parent->d_lock);
937 parent->d_count--;
938 list_del(&dentry->d_u.d_child);
939 spin_unlock(&parent->d_lock);
942 detached++;
944 inode = dentry->d_inode;
945 if (inode) {
946 dentry->d_inode = NULL;
947 list_del_init(&dentry->d_alias);
948 if (dentry->d_op && dentry->d_op->d_iput)
949 dentry->d_op->d_iput(dentry, inode);
950 else
951 iput(inode);
954 d_free(dentry);
956 /* finished when we fall off the top of the tree,
957 * otherwise we ascend to the parent and move to the
958 * next sibling if there is one */
959 if (!parent)
960 return;
961 dentry = parent;
962 } while (list_empty(&dentry->d_subdirs));
964 dentry = list_entry(dentry->d_subdirs.next,
965 struct dentry, d_u.d_child);
970 * destroy the dentries attached to a superblock on unmounting
971 * - we don't need to use dentry->d_lock because:
972 * - the superblock is detached from all mountings and open files, so the
973 * dentry trees will not be rearranged by the VFS
974 * - s_umount is write-locked, so the memory pressure shrinker will ignore
975 * any dentries belonging to this superblock that it comes across
976 * - the filesystem itself is no longer permitted to rearrange the dentries
977 * in this superblock
979 void shrink_dcache_for_umount(struct super_block *sb)
981 struct dentry *dentry;
983 if (down_read_trylock(&sb->s_umount))
984 BUG();
986 dentry = sb->s_root;
987 sb->s_root = NULL;
988 spin_lock(&dentry->d_lock);
989 dentry->d_count--;
990 spin_unlock(&dentry->d_lock);
991 shrink_dcache_for_umount_subtree(dentry);
993 while (!hlist_bl_empty(&sb->s_anon)) {
994 dentry = hlist_bl_entry(hlist_bl_first(&sb->s_anon), struct dentry, d_hash);
995 shrink_dcache_for_umount_subtree(dentry);
1000 * This tries to ascend one level of parenthood, but
1001 * we can race with renaming, so we need to re-check
1002 * the parenthood after dropping the lock and check
1003 * that the sequence number still matches.
1005 static struct dentry *try_to_ascend(struct dentry *old, int locked, unsigned seq)
1007 struct dentry *new = old->d_parent;
1009 rcu_read_lock();
1010 spin_unlock(&old->d_lock);
1011 spin_lock(&new->d_lock);
1014 * might go back up the wrong parent if we have had a rename
1015 * or deletion
1017 if (new != old->d_parent ||
1018 (old->d_flags & DCACHE_DENTRY_KILLED) ||
1019 (!locked && read_seqretry(&rename_lock, seq))) {
1020 spin_unlock(&new->d_lock);
1021 new = NULL;
1023 rcu_read_unlock();
1024 return new;
1029 * Search for at least 1 mount point in the dentry's subdirs.
1030 * We descend to the next level whenever the d_subdirs
1031 * list is non-empty and continue searching.
1035 * have_submounts - check for mounts over a dentry
1036 * @parent: dentry to check.
1038 * Return true if the parent or its subdirectories contain
1039 * a mount point
1041 int have_submounts(struct dentry *parent)
1043 struct dentry *this_parent;
1044 struct list_head *next;
1045 unsigned seq;
1046 int locked = 0;
1048 seq = read_seqbegin(&rename_lock);
1049 again:
1050 this_parent = parent;
1052 if (d_mountpoint(parent))
1053 goto positive;
1054 spin_lock(&this_parent->d_lock);
1055 repeat:
1056 next = this_parent->d_subdirs.next;
1057 resume:
1058 while (next != &this_parent->d_subdirs) {
1059 struct list_head *tmp = next;
1060 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1061 next = tmp->next;
1063 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1064 /* Have we found a mount point ? */
1065 if (d_mountpoint(dentry)) {
1066 spin_unlock(&dentry->d_lock);
1067 spin_unlock(&this_parent->d_lock);
1068 goto positive;
1070 if (!list_empty(&dentry->d_subdirs)) {
1071 spin_unlock(&this_parent->d_lock);
1072 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
1073 this_parent = dentry;
1074 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1075 goto repeat;
1077 spin_unlock(&dentry->d_lock);
1080 * All done at this level ... ascend and resume the search.
1082 if (this_parent != parent) {
1083 struct dentry *child = this_parent;
1084 this_parent = try_to_ascend(this_parent, locked, seq);
1085 if (!this_parent)
1086 goto rename_retry;
1087 next = child->d_u.d_child.next;
1088 goto resume;
1090 spin_unlock(&this_parent->d_lock);
1091 if (!locked && read_seqretry(&rename_lock, seq))
1092 goto rename_retry;
1093 if (locked)
1094 write_sequnlock(&rename_lock);
1095 return 0; /* No mount points found in tree */
1096 positive:
1097 if (!locked && read_seqretry(&rename_lock, seq))
1098 goto rename_retry;
1099 if (locked)
1100 write_sequnlock(&rename_lock);
1101 return 1;
1103 rename_retry:
1104 if (locked)
1105 goto again;
1106 locked = 1;
1107 write_seqlock(&rename_lock);
1108 goto again;
1110 EXPORT_SYMBOL(have_submounts);
1113 * Search the dentry child list for the specified parent,
1114 * and move any unused dentries to the end of the unused
1115 * list for prune_dcache(). We descend to the next level
1116 * whenever the d_subdirs list is non-empty and continue
1117 * searching.
1119 * It returns zero iff there are no unused children,
1120 * otherwise it returns the number of children moved to
1121 * the end of the unused list. This may not be the total
1122 * number of unused children, because select_parent can
1123 * drop the lock and return early due to latency
1124 * constraints.
1126 static int select_parent(struct dentry * parent)
1128 struct dentry *this_parent;
1129 struct list_head *next;
1130 unsigned seq;
1131 int found = 0;
1132 int locked = 0;
1134 seq = read_seqbegin(&rename_lock);
1135 again:
1136 this_parent = parent;
1137 spin_lock(&this_parent->d_lock);
1138 repeat:
1139 next = this_parent->d_subdirs.next;
1140 resume:
1141 while (next != &this_parent->d_subdirs) {
1142 struct list_head *tmp = next;
1143 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1144 next = tmp->next;
1146 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1149 * move only zero ref count dentries to the end
1150 * of the unused list for prune_dcache
1152 * Those which are presently on the shrink list, being processed
1153 * by shrink_dentry_list(), shouldn't be moved. Otherwise the
1154 * loop in shrink_dcache_parent() might not make any progress
1155 * and loop forever.
1157 if (dentry->d_count) {
1158 dentry_lru_del(dentry);
1159 } else if (!(dentry->d_flags & DCACHE_SHRINK_LIST)) {
1160 dentry_lru_move_tail(dentry);
1161 found++;
1164 * We can return to the caller if we have found some (this
1165 * ensures forward progress). We'll be coming back to find
1166 * the rest.
1168 if (found && need_resched()) {
1169 spin_unlock(&dentry->d_lock);
1170 goto out;
1174 * Descend a level if the d_subdirs list is non-empty.
1176 if (!list_empty(&dentry->d_subdirs)) {
1177 spin_unlock(&this_parent->d_lock);
1178 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
1179 this_parent = dentry;
1180 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1181 goto repeat;
1184 spin_unlock(&dentry->d_lock);
1187 * All done at this level ... ascend and resume the search.
1189 if (this_parent != parent) {
1190 struct dentry *child = this_parent;
1191 this_parent = try_to_ascend(this_parent, locked, seq);
1192 if (!this_parent)
1193 goto rename_retry;
1194 next = child->d_u.d_child.next;
1195 goto resume;
1197 out:
1198 spin_unlock(&this_parent->d_lock);
1199 if (!locked && read_seqretry(&rename_lock, seq))
1200 goto rename_retry;
1201 if (locked)
1202 write_sequnlock(&rename_lock);
1203 return found;
1205 rename_retry:
1206 if (found)
1207 return found;
1208 if (locked)
1209 goto again;
1210 locked = 1;
1211 write_seqlock(&rename_lock);
1212 goto again;
1216 * shrink_dcache_parent - prune dcache
1217 * @parent: parent of entries to prune
1219 * Prune the dcache to remove unused children of the parent dentry.
1222 void shrink_dcache_parent(struct dentry * parent)
1224 struct super_block *sb = parent->d_sb;
1225 int found;
1227 while ((found = select_parent(parent)) != 0)
1228 __shrink_dcache_sb(sb, &found, 0);
1230 EXPORT_SYMBOL(shrink_dcache_parent);
1233 * Scan `sc->nr_slab_to_reclaim' dentries and return the number which remain.
1235 * We need to avoid reentering the filesystem if the caller is performing a
1236 * GFP_NOFS allocation attempt. One example deadlock is:
1238 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1239 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1240 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1242 * In this case we return -1 to tell the caller that we baled.
1244 static int shrink_dcache_memory(struct shrinker *shrink,
1245 struct shrink_control *sc)
1247 int nr = sc->nr_to_scan;
1248 gfp_t gfp_mask = sc->gfp_mask;
1250 if (nr) {
1251 if (!(gfp_mask & __GFP_FS))
1252 return -1;
1253 prune_dcache(nr);
1256 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
1259 static struct shrinker dcache_shrinker = {
1260 .shrink = shrink_dcache_memory,
1261 .seeks = DEFAULT_SEEKS,
1265 * d_alloc - allocate a dcache entry
1266 * @parent: parent of entry to allocate
1267 * @name: qstr of the name
1269 * Allocates a dentry. It returns %NULL if there is insufficient memory
1270 * available. On a success the dentry is returned. The name passed in is
1271 * copied and the copy passed in may be reused after this call.
1274 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1276 struct dentry *dentry;
1277 char *dname;
1279 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
1280 if (!dentry)
1281 return NULL;
1283 if (name->len > DNAME_INLINE_LEN-1) {
1284 dname = kmalloc(name->len + 1, GFP_KERNEL);
1285 if (!dname) {
1286 kmem_cache_free(dentry_cache, dentry);
1287 return NULL;
1289 } else {
1290 dname = dentry->d_iname;
1292 dentry->d_name.name = dname;
1294 dentry->d_name.len = name->len;
1295 dentry->d_name.hash = name->hash;
1296 memcpy(dname, name->name, name->len);
1297 dname[name->len] = 0;
1299 dentry->d_count = 1;
1300 dentry->d_flags = 0;
1301 spin_lock_init(&dentry->d_lock);
1302 seqcount_init(&dentry->d_seq);
1303 dentry->d_inode = NULL;
1304 dentry->d_parent = NULL;
1305 dentry->d_sb = NULL;
1306 dentry->d_op = NULL;
1307 dentry->d_fsdata = NULL;
1308 INIT_HLIST_BL_NODE(&dentry->d_hash);
1309 INIT_LIST_HEAD(&dentry->d_lru);
1310 INIT_LIST_HEAD(&dentry->d_subdirs);
1311 INIT_LIST_HEAD(&dentry->d_alias);
1312 INIT_LIST_HEAD(&dentry->d_u.d_child);
1314 if (parent) {
1315 spin_lock(&parent->d_lock);
1317 * don't need child lock because it is not subject
1318 * to concurrency here
1320 __dget_dlock(parent);
1321 dentry->d_parent = parent;
1322 dentry->d_sb = parent->d_sb;
1323 d_set_d_op(dentry, dentry->d_sb->s_d_op);
1324 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
1325 spin_unlock(&parent->d_lock);
1328 this_cpu_inc(nr_dentry);
1330 return dentry;
1332 EXPORT_SYMBOL(d_alloc);
1334 struct dentry *d_alloc_pseudo(struct super_block *sb, const struct qstr *name)
1336 struct dentry *dentry = d_alloc(NULL, name);
1337 if (dentry) {
1338 dentry->d_sb = sb;
1339 d_set_d_op(dentry, dentry->d_sb->s_d_op);
1340 dentry->d_parent = dentry;
1341 dentry->d_flags |= DCACHE_DISCONNECTED;
1343 return dentry;
1345 EXPORT_SYMBOL(d_alloc_pseudo);
1347 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1349 struct qstr q;
1351 q.name = name;
1352 q.len = strlen(name);
1353 q.hash = full_name_hash(q.name, q.len);
1354 return d_alloc(parent, &q);
1356 EXPORT_SYMBOL(d_alloc_name);
1358 void d_set_d_op(struct dentry *dentry, const struct dentry_operations *op)
1360 WARN_ON_ONCE(dentry->d_op);
1361 WARN_ON_ONCE(dentry->d_flags & (DCACHE_OP_HASH |
1362 DCACHE_OP_COMPARE |
1363 DCACHE_OP_REVALIDATE |
1364 DCACHE_OP_DELETE ));
1365 dentry->d_op = op;
1366 if (!op)
1367 return;
1368 if (op->d_hash)
1369 dentry->d_flags |= DCACHE_OP_HASH;
1370 if (op->d_compare)
1371 dentry->d_flags |= DCACHE_OP_COMPARE;
1372 if (op->d_revalidate)
1373 dentry->d_flags |= DCACHE_OP_REVALIDATE;
1374 if (op->d_delete)
1375 dentry->d_flags |= DCACHE_OP_DELETE;
1378 EXPORT_SYMBOL(d_set_d_op);
1380 static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1382 spin_lock(&dentry->d_lock);
1383 if (inode) {
1384 if (unlikely(IS_AUTOMOUNT(inode)))
1385 dentry->d_flags |= DCACHE_NEED_AUTOMOUNT;
1386 list_add(&dentry->d_alias, &inode->i_dentry);
1388 dentry->d_inode = inode;
1389 dentry_rcuwalk_barrier(dentry);
1390 spin_unlock(&dentry->d_lock);
1391 fsnotify_d_instantiate(dentry, inode);
1395 * d_instantiate - fill in inode information for a dentry
1396 * @entry: dentry to complete
1397 * @inode: inode to attach to this dentry
1399 * Fill in inode information in the entry.
1401 * This turns negative dentries into productive full members
1402 * of society.
1404 * NOTE! This assumes that the inode count has been incremented
1405 * (or otherwise set) by the caller to indicate that it is now
1406 * in use by the dcache.
1409 void d_instantiate(struct dentry *entry, struct inode * inode)
1411 BUG_ON(!list_empty(&entry->d_alias));
1412 if (inode)
1413 spin_lock(&inode->i_lock);
1414 __d_instantiate(entry, inode);
1415 if (inode)
1416 spin_unlock(&inode->i_lock);
1417 security_d_instantiate(entry, inode);
1419 EXPORT_SYMBOL(d_instantiate);
1422 * d_instantiate_unique - instantiate a non-aliased dentry
1423 * @entry: dentry to instantiate
1424 * @inode: inode to attach to this dentry
1426 * Fill in inode information in the entry. On success, it returns NULL.
1427 * If an unhashed alias of "entry" already exists, then we return the
1428 * aliased dentry instead and drop one reference to inode.
1430 * Note that in order to avoid conflicts with rename() etc, the caller
1431 * had better be holding the parent directory semaphore.
1433 * This also assumes that the inode count has been incremented
1434 * (or otherwise set) by the caller to indicate that it is now
1435 * in use by the dcache.
1437 static struct dentry *__d_instantiate_unique(struct dentry *entry,
1438 struct inode *inode)
1440 struct dentry *alias;
1441 int len = entry->d_name.len;
1442 const char *name = entry->d_name.name;
1443 unsigned int hash = entry->d_name.hash;
1445 if (!inode) {
1446 __d_instantiate(entry, NULL);
1447 return NULL;
1450 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1451 struct qstr *qstr = &alias->d_name;
1454 * Don't need alias->d_lock here, because aliases with
1455 * d_parent == entry->d_parent are not subject to name or
1456 * parent changes, because the parent inode i_mutex is held.
1458 if (qstr->hash != hash)
1459 continue;
1460 if (alias->d_parent != entry->d_parent)
1461 continue;
1462 if (dentry_cmp(qstr->name, qstr->len, name, len))
1463 continue;
1464 __dget(alias);
1465 return alias;
1468 __d_instantiate(entry, inode);
1469 return NULL;
1472 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1474 struct dentry *result;
1476 BUG_ON(!list_empty(&entry->d_alias));
1478 if (inode)
1479 spin_lock(&inode->i_lock);
1480 result = __d_instantiate_unique(entry, inode);
1481 if (inode)
1482 spin_unlock(&inode->i_lock);
1484 if (!result) {
1485 security_d_instantiate(entry, inode);
1486 return NULL;
1489 BUG_ON(!d_unhashed(result));
1490 iput(inode);
1491 return result;
1494 EXPORT_SYMBOL(d_instantiate_unique);
1497 * d_alloc_root - allocate root dentry
1498 * @root_inode: inode to allocate the root for
1500 * Allocate a root ("/") dentry for the inode given. The inode is
1501 * instantiated and returned. %NULL is returned if there is insufficient
1502 * memory or the inode passed is %NULL.
1505 struct dentry * d_alloc_root(struct inode * root_inode)
1507 struct dentry *res = NULL;
1509 if (root_inode) {
1510 static const struct qstr name = { .name = "/", .len = 1 };
1512 res = d_alloc(NULL, &name);
1513 if (res) {
1514 res->d_sb = root_inode->i_sb;
1515 d_set_d_op(res, res->d_sb->s_d_op);
1516 res->d_parent = res;
1517 d_instantiate(res, root_inode);
1520 return res;
1522 EXPORT_SYMBOL(d_alloc_root);
1524 static struct dentry * __d_find_any_alias(struct inode *inode)
1526 struct dentry *alias;
1528 if (list_empty(&inode->i_dentry))
1529 return NULL;
1530 alias = list_first_entry(&inode->i_dentry, struct dentry, d_alias);
1531 __dget(alias);
1532 return alias;
1535 static struct dentry * d_find_any_alias(struct inode *inode)
1537 struct dentry *de;
1539 spin_lock(&inode->i_lock);
1540 de = __d_find_any_alias(inode);
1541 spin_unlock(&inode->i_lock);
1542 return de;
1547 * d_obtain_alias - find or allocate a dentry for a given inode
1548 * @inode: inode to allocate the dentry for
1550 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1551 * similar open by handle operations. The returned dentry may be anonymous,
1552 * or may have a full name (if the inode was already in the cache).
1554 * When called on a directory inode, we must ensure that the inode only ever
1555 * has one dentry. If a dentry is found, that is returned instead of
1556 * allocating a new one.
1558 * On successful return, the reference to the inode has been transferred
1559 * to the dentry. In case of an error the reference on the inode is released.
1560 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1561 * be passed in and will be the error will be propagate to the return value,
1562 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1564 struct dentry *d_obtain_alias(struct inode *inode)
1566 static const struct qstr anonstring = { .name = "" };
1567 struct dentry *tmp;
1568 struct dentry *res;
1570 if (!inode)
1571 return ERR_PTR(-ESTALE);
1572 if (IS_ERR(inode))
1573 return ERR_CAST(inode);
1575 res = d_find_any_alias(inode);
1576 if (res)
1577 goto out_iput;
1579 tmp = d_alloc(NULL, &anonstring);
1580 if (!tmp) {
1581 res = ERR_PTR(-ENOMEM);
1582 goto out_iput;
1584 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1587 spin_lock(&inode->i_lock);
1588 res = __d_find_any_alias(inode);
1589 if (res) {
1590 spin_unlock(&inode->i_lock);
1591 dput(tmp);
1592 goto out_iput;
1595 /* attach a disconnected dentry */
1596 spin_lock(&tmp->d_lock);
1597 tmp->d_sb = inode->i_sb;
1598 d_set_d_op(tmp, tmp->d_sb->s_d_op);
1599 tmp->d_inode = inode;
1600 tmp->d_flags |= DCACHE_DISCONNECTED;
1601 list_add(&tmp->d_alias, &inode->i_dentry);
1602 hlist_bl_lock(&tmp->d_sb->s_anon);
1603 hlist_bl_add_head(&tmp->d_hash, &tmp->d_sb->s_anon);
1604 hlist_bl_unlock(&tmp->d_sb->s_anon);
1605 spin_unlock(&tmp->d_lock);
1606 spin_unlock(&inode->i_lock);
1607 security_d_instantiate(tmp, inode);
1609 return tmp;
1611 out_iput:
1612 if (res && !IS_ERR(res))
1613 security_d_instantiate(res, inode);
1614 iput(inode);
1615 return res;
1617 EXPORT_SYMBOL(d_obtain_alias);
1620 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1621 * @inode: the inode which may have a disconnected dentry
1622 * @dentry: a negative dentry which we want to point to the inode.
1624 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1625 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1626 * and return it, else simply d_add the inode to the dentry and return NULL.
1628 * This is needed in the lookup routine of any filesystem that is exportable
1629 * (via knfsd) so that we can build dcache paths to directories effectively.
1631 * If a dentry was found and moved, then it is returned. Otherwise NULL
1632 * is returned. This matches the expected return value of ->lookup.
1635 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1637 struct dentry *new = NULL;
1639 if (inode && S_ISDIR(inode->i_mode)) {
1640 spin_lock(&inode->i_lock);
1641 new = __d_find_alias(inode, 1);
1642 if (new) {
1643 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1644 spin_unlock(&inode->i_lock);
1645 security_d_instantiate(new, inode);
1646 d_move(new, dentry);
1647 iput(inode);
1648 } else {
1649 /* already taking inode->i_lock, so d_add() by hand */
1650 __d_instantiate(dentry, inode);
1651 spin_unlock(&inode->i_lock);
1652 security_d_instantiate(dentry, inode);
1653 d_rehash(dentry);
1655 } else
1656 d_add(dentry, inode);
1657 return new;
1659 EXPORT_SYMBOL(d_splice_alias);
1662 * d_add_ci - lookup or allocate new dentry with case-exact name
1663 * @inode: the inode case-insensitive lookup has found
1664 * @dentry: the negative dentry that was passed to the parent's lookup func
1665 * @name: the case-exact name to be associated with the returned dentry
1667 * This is to avoid filling the dcache with case-insensitive names to the
1668 * same inode, only the actual correct case is stored in the dcache for
1669 * case-insensitive filesystems.
1671 * For a case-insensitive lookup match and if the the case-exact dentry
1672 * already exists in in the dcache, use it and return it.
1674 * If no entry exists with the exact case name, allocate new dentry with
1675 * the exact case, and return the spliced entry.
1677 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
1678 struct qstr *name)
1680 int error;
1681 struct dentry *found;
1682 struct dentry *new;
1685 * First check if a dentry matching the name already exists,
1686 * if not go ahead and create it now.
1688 found = d_hash_and_lookup(dentry->d_parent, name);
1689 if (!found) {
1690 new = d_alloc(dentry->d_parent, name);
1691 if (!new) {
1692 error = -ENOMEM;
1693 goto err_out;
1696 found = d_splice_alias(inode, new);
1697 if (found) {
1698 dput(new);
1699 return found;
1701 return new;
1705 * If a matching dentry exists, and it's not negative use it.
1707 * Decrement the reference count to balance the iget() done
1708 * earlier on.
1710 if (found->d_inode) {
1711 if (unlikely(found->d_inode != inode)) {
1712 /* This can't happen because bad inodes are unhashed. */
1713 BUG_ON(!is_bad_inode(inode));
1714 BUG_ON(!is_bad_inode(found->d_inode));
1716 iput(inode);
1717 return found;
1721 * Negative dentry: instantiate it unless the inode is a directory and
1722 * already has a dentry.
1724 spin_lock(&inode->i_lock);
1725 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
1726 __d_instantiate(found, inode);
1727 spin_unlock(&inode->i_lock);
1728 security_d_instantiate(found, inode);
1729 return found;
1733 * In case a directory already has a (disconnected) entry grab a
1734 * reference to it, move it in place and use it.
1736 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1737 __dget(new);
1738 spin_unlock(&inode->i_lock);
1739 security_d_instantiate(found, inode);
1740 d_move(new, found);
1741 iput(inode);
1742 dput(found);
1743 return new;
1745 err_out:
1746 iput(inode);
1747 return ERR_PTR(error);
1749 EXPORT_SYMBOL(d_add_ci);
1752 * __d_lookup_rcu - search for a dentry (racy, store-free)
1753 * @parent: parent dentry
1754 * @name: qstr of name we wish to find
1755 * @seq: returns d_seq value at the point where the dentry was found
1756 * @inode: returns dentry->d_inode when the inode was found valid.
1757 * Returns: dentry, or NULL
1759 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
1760 * resolution (store-free path walking) design described in
1761 * Documentation/filesystems/path-lookup.txt.
1763 * This is not to be used outside core vfs.
1765 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
1766 * held, and rcu_read_lock held. The returned dentry must not be stored into
1767 * without taking d_lock and checking d_seq sequence count against @seq
1768 * returned here.
1770 * A refcount may be taken on the found dentry with the __d_rcu_to_refcount
1771 * function.
1773 * Alternatively, __d_lookup_rcu may be called again to look up the child of
1774 * the returned dentry, so long as its parent's seqlock is checked after the
1775 * child is looked up. Thus, an interlocking stepping of sequence lock checks
1776 * is formed, giving integrity down the path walk.
1778 struct dentry *__d_lookup_rcu(struct dentry *parent, struct qstr *name,
1779 unsigned *seq, struct inode **inode)
1781 unsigned int len = name->len;
1782 unsigned int hash = name->hash;
1783 const unsigned char *str = name->name;
1784 struct hlist_bl_head *b = d_hash(parent, hash);
1785 struct hlist_bl_node *node;
1786 struct dentry *dentry;
1789 * Note: There is significant duplication with __d_lookup_rcu which is
1790 * required to prevent single threaded performance regressions
1791 * especially on architectures where smp_rmb (in seqcounts) are costly.
1792 * Keep the two functions in sync.
1796 * The hash list is protected using RCU.
1798 * Carefully use d_seq when comparing a candidate dentry, to avoid
1799 * races with d_move().
1801 * It is possible that concurrent renames can mess up our list
1802 * walk here and result in missing our dentry, resulting in the
1803 * false-negative result. d_lookup() protects against concurrent
1804 * renames using rename_lock seqlock.
1806 * See Documentation/filesystems/path-lookup.txt for more details.
1808 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
1809 struct inode *i;
1810 const char *tname;
1811 int tlen;
1813 if (dentry->d_name.hash != hash)
1814 continue;
1816 seqretry:
1817 *seq = read_seqcount_begin(&dentry->d_seq);
1818 if (dentry->d_parent != parent)
1819 continue;
1820 if (d_unhashed(dentry))
1821 continue;
1822 tlen = dentry->d_name.len;
1823 tname = dentry->d_name.name;
1824 i = dentry->d_inode;
1825 prefetch(tname);
1827 * This seqcount check is required to ensure name and
1828 * len are loaded atomically, so as not to walk off the
1829 * edge of memory when walking. If we could load this
1830 * atomically some other way, we could drop this check.
1832 if (read_seqcount_retry(&dentry->d_seq, *seq))
1833 goto seqretry;
1834 if (parent->d_flags & DCACHE_OP_COMPARE) {
1835 if (parent->d_op->d_compare(parent, *inode,
1836 dentry, i,
1837 tlen, tname, name))
1838 continue;
1839 } else {
1840 if (dentry_cmp(tname, tlen, str, len))
1841 continue;
1844 * No extra seqcount check is required after the name
1845 * compare. The caller must perform a seqcount check in
1846 * order to do anything useful with the returned dentry
1847 * anyway.
1849 *inode = i;
1850 return dentry;
1852 return NULL;
1856 * d_lookup - search for a dentry
1857 * @parent: parent dentry
1858 * @name: qstr of name we wish to find
1859 * Returns: dentry, or NULL
1861 * d_lookup searches the children of the parent dentry for the name in
1862 * question. If the dentry is found its reference count is incremented and the
1863 * dentry is returned. The caller must use dput to free the entry when it has
1864 * finished using it. %NULL is returned if the dentry does not exist.
1866 struct dentry *d_lookup(struct dentry *parent, struct qstr *name)
1868 struct dentry *dentry;
1869 unsigned seq;
1871 do {
1872 seq = read_seqbegin(&rename_lock);
1873 dentry = __d_lookup(parent, name);
1874 if (dentry)
1875 break;
1876 } while (read_seqretry(&rename_lock, seq));
1877 return dentry;
1879 EXPORT_SYMBOL(d_lookup);
1882 * __d_lookup - search for a dentry (racy)
1883 * @parent: parent dentry
1884 * @name: qstr of name we wish to find
1885 * Returns: dentry, or NULL
1887 * __d_lookup is like d_lookup, however it may (rarely) return a
1888 * false-negative result due to unrelated rename activity.
1890 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1891 * however it must be used carefully, eg. with a following d_lookup in
1892 * the case of failure.
1894 * __d_lookup callers must be commented.
1896 struct dentry *__d_lookup(struct dentry *parent, struct qstr *name)
1898 unsigned int len = name->len;
1899 unsigned int hash = name->hash;
1900 const unsigned char *str = name->name;
1901 struct hlist_bl_head *b = d_hash(parent, hash);
1902 struct hlist_bl_node *node;
1903 struct dentry *found = NULL;
1904 struct dentry *dentry;
1907 * Note: There is significant duplication with __d_lookup_rcu which is
1908 * required to prevent single threaded performance regressions
1909 * especially on architectures where smp_rmb (in seqcounts) are costly.
1910 * Keep the two functions in sync.
1914 * The hash list is protected using RCU.
1916 * Take d_lock when comparing a candidate dentry, to avoid races
1917 * with d_move().
1919 * It is possible that concurrent renames can mess up our list
1920 * walk here and result in missing our dentry, resulting in the
1921 * false-negative result. d_lookup() protects against concurrent
1922 * renames using rename_lock seqlock.
1924 * See Documentation/filesystems/path-lookup.txt for more details.
1926 rcu_read_lock();
1928 hlist_bl_for_each_entry_rcu(dentry, node, b, d_hash) {
1929 const char *tname;
1930 int tlen;
1932 if (dentry->d_name.hash != hash)
1933 continue;
1935 spin_lock(&dentry->d_lock);
1936 if (dentry->d_parent != parent)
1937 goto next;
1938 if (d_unhashed(dentry))
1939 goto next;
1942 * It is safe to compare names since d_move() cannot
1943 * change the qstr (protected by d_lock).
1945 tlen = dentry->d_name.len;
1946 tname = dentry->d_name.name;
1947 if (parent->d_flags & DCACHE_OP_COMPARE) {
1948 if (parent->d_op->d_compare(parent, parent->d_inode,
1949 dentry, dentry->d_inode,
1950 tlen, tname, name))
1951 goto next;
1952 } else {
1953 if (dentry_cmp(tname, tlen, str, len))
1954 goto next;
1957 dentry->d_count++;
1958 found = dentry;
1959 spin_unlock(&dentry->d_lock);
1960 break;
1961 next:
1962 spin_unlock(&dentry->d_lock);
1964 rcu_read_unlock();
1966 return found;
1970 * d_hash_and_lookup - hash the qstr then search for a dentry
1971 * @dir: Directory to search in
1972 * @name: qstr of name we wish to find
1974 * On hash failure or on lookup failure NULL is returned.
1976 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1978 struct dentry *dentry = NULL;
1981 * Check for a fs-specific hash function. Note that we must
1982 * calculate the standard hash first, as the d_op->d_hash()
1983 * routine may choose to leave the hash value unchanged.
1985 name->hash = full_name_hash(name->name, name->len);
1986 if (dir->d_flags & DCACHE_OP_HASH) {
1987 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
1988 goto out;
1990 dentry = d_lookup(dir, name);
1991 out:
1992 return dentry;
1996 * d_validate - verify dentry provided from insecure source (deprecated)
1997 * @dentry: The dentry alleged to be valid child of @dparent
1998 * @dparent: The parent dentry (known to be valid)
2000 * An insecure source has sent us a dentry, here we verify it and dget() it.
2001 * This is used by ncpfs in its readdir implementation.
2002 * Zero is returned in the dentry is invalid.
2004 * This function is slow for big directories, and deprecated, do not use it.
2006 int d_validate(struct dentry *dentry, struct dentry *dparent)
2008 struct dentry *child;
2010 spin_lock(&dparent->d_lock);
2011 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
2012 if (dentry == child) {
2013 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2014 __dget_dlock(dentry);
2015 spin_unlock(&dentry->d_lock);
2016 spin_unlock(&dparent->d_lock);
2017 return 1;
2020 spin_unlock(&dparent->d_lock);
2022 return 0;
2024 EXPORT_SYMBOL(d_validate);
2027 * When a file is deleted, we have two options:
2028 * - turn this dentry into a negative dentry
2029 * - unhash this dentry and free it.
2031 * Usually, we want to just turn this into
2032 * a negative dentry, but if anybody else is
2033 * currently using the dentry or the inode
2034 * we can't do that and we fall back on removing
2035 * it from the hash queues and waiting for
2036 * it to be deleted later when it has no users
2040 * d_delete - delete a dentry
2041 * @dentry: The dentry to delete
2043 * Turn the dentry into a negative dentry if possible, otherwise
2044 * remove it from the hash queues so it can be deleted later
2047 void d_delete(struct dentry * dentry)
2049 struct inode *inode;
2050 int isdir = 0;
2052 * Are we the only user?
2054 again:
2055 spin_lock(&dentry->d_lock);
2056 inode = dentry->d_inode;
2057 isdir = S_ISDIR(inode->i_mode);
2058 if (dentry->d_count == 1) {
2059 if (inode && !spin_trylock(&inode->i_lock)) {
2060 spin_unlock(&dentry->d_lock);
2061 cpu_relax();
2062 goto again;
2064 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
2065 dentry_unlink_inode(dentry);
2066 fsnotify_nameremove(dentry, isdir);
2067 return;
2070 if (!d_unhashed(dentry))
2071 __d_drop(dentry);
2073 spin_unlock(&dentry->d_lock);
2075 fsnotify_nameremove(dentry, isdir);
2077 EXPORT_SYMBOL(d_delete);
2079 static void __d_rehash(struct dentry * entry, struct hlist_bl_head *b)
2081 BUG_ON(!d_unhashed(entry));
2082 hlist_bl_lock(b);
2083 entry->d_flags |= DCACHE_RCUACCESS;
2084 hlist_bl_add_head_rcu(&entry->d_hash, b);
2085 hlist_bl_unlock(b);
2088 static void _d_rehash(struct dentry * entry)
2090 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
2094 * d_rehash - add an entry back to the hash
2095 * @entry: dentry to add to the hash
2097 * Adds a dentry to the hash according to its name.
2100 void d_rehash(struct dentry * entry)
2102 spin_lock(&entry->d_lock);
2103 _d_rehash(entry);
2104 spin_unlock(&entry->d_lock);
2106 EXPORT_SYMBOL(d_rehash);
2109 * dentry_update_name_case - update case insensitive dentry with a new name
2110 * @dentry: dentry to be updated
2111 * @name: new name
2113 * Update a case insensitive dentry with new case of name.
2115 * dentry must have been returned by d_lookup with name @name. Old and new
2116 * name lengths must match (ie. no d_compare which allows mismatched name
2117 * lengths).
2119 * Parent inode i_mutex must be held over d_lookup and into this call (to
2120 * keep renames and concurrent inserts, and readdir(2) away).
2122 void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
2124 BUG_ON(!mutex_is_locked(&dentry->d_parent->d_inode->i_mutex));
2125 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
2127 spin_lock(&dentry->d_lock);
2128 write_seqcount_begin(&dentry->d_seq);
2129 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
2130 write_seqcount_end(&dentry->d_seq);
2131 spin_unlock(&dentry->d_lock);
2133 EXPORT_SYMBOL(dentry_update_name_case);
2135 static void switch_names(struct dentry *dentry, struct dentry *target)
2137 if (dname_external(target)) {
2138 if (dname_external(dentry)) {
2140 * Both external: swap the pointers
2142 swap(target->d_name.name, dentry->d_name.name);
2143 } else {
2145 * dentry:internal, target:external. Steal target's
2146 * storage and make target internal.
2148 memcpy(target->d_iname, dentry->d_name.name,
2149 dentry->d_name.len + 1);
2150 dentry->d_name.name = target->d_name.name;
2151 target->d_name.name = target->d_iname;
2153 } else {
2154 if (dname_external(dentry)) {
2156 * dentry:external, target:internal. Give dentry's
2157 * storage to target and make dentry internal
2159 memcpy(dentry->d_iname, target->d_name.name,
2160 target->d_name.len + 1);
2161 target->d_name.name = dentry->d_name.name;
2162 dentry->d_name.name = dentry->d_iname;
2163 } else {
2165 * Both are internal. Just copy target to dentry
2167 memcpy(dentry->d_iname, target->d_name.name,
2168 target->d_name.len + 1);
2169 dentry->d_name.len = target->d_name.len;
2170 return;
2173 swap(dentry->d_name.len, target->d_name.len);
2176 static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
2179 * XXXX: do we really need to take target->d_lock?
2181 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
2182 spin_lock(&target->d_parent->d_lock);
2183 else {
2184 if (d_ancestor(dentry->d_parent, target->d_parent)) {
2185 spin_lock(&dentry->d_parent->d_lock);
2186 spin_lock_nested(&target->d_parent->d_lock,
2187 DENTRY_D_LOCK_NESTED);
2188 } else {
2189 spin_lock(&target->d_parent->d_lock);
2190 spin_lock_nested(&dentry->d_parent->d_lock,
2191 DENTRY_D_LOCK_NESTED);
2194 if (target < dentry) {
2195 spin_lock_nested(&target->d_lock, 2);
2196 spin_lock_nested(&dentry->d_lock, 3);
2197 } else {
2198 spin_lock_nested(&dentry->d_lock, 2);
2199 spin_lock_nested(&target->d_lock, 3);
2203 static void dentry_unlock_parents_for_move(struct dentry *dentry,
2204 struct dentry *target)
2206 if (target->d_parent != dentry->d_parent)
2207 spin_unlock(&dentry->d_parent->d_lock);
2208 if (target->d_parent != target)
2209 spin_unlock(&target->d_parent->d_lock);
2213 * When switching names, the actual string doesn't strictly have to
2214 * be preserved in the target - because we're dropping the target
2215 * anyway. As such, we can just do a simple memcpy() to copy over
2216 * the new name before we switch.
2218 * Note that we have to be a lot more careful about getting the hash
2219 * switched - we have to switch the hash value properly even if it
2220 * then no longer matches the actual (corrupted) string of the target.
2221 * The hash value has to match the hash queue that the dentry is on..
2224 * __d_move - move a dentry
2225 * @dentry: entry to move
2226 * @target: new dentry
2228 * Update the dcache to reflect the move of a file name. Negative
2229 * dcache entries should not be moved in this way. Caller hold
2230 * rename_lock.
2232 static void __d_move(struct dentry * dentry, struct dentry * target)
2234 if (!dentry->d_inode)
2235 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2237 BUG_ON(d_ancestor(dentry, target));
2238 BUG_ON(d_ancestor(target, dentry));
2240 dentry_lock_for_move(dentry, target);
2242 write_seqcount_begin(&dentry->d_seq);
2243 write_seqcount_begin(&target->d_seq);
2245 /* __d_drop does write_seqcount_barrier, but they're OK to nest. */
2248 * Move the dentry to the target hash queue. Don't bother checking
2249 * for the same hash queue because of how unlikely it is.
2251 __d_drop(dentry);
2252 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
2254 /* Unhash the target: dput() will then get rid of it */
2255 __d_drop(target);
2257 list_del(&dentry->d_u.d_child);
2258 list_del(&target->d_u.d_child);
2260 /* Switch the names.. */
2261 switch_names(dentry, target);
2262 swap(dentry->d_name.hash, target->d_name.hash);
2264 /* ... and switch the parents */
2265 if (IS_ROOT(dentry)) {
2266 dentry->d_parent = target->d_parent;
2267 target->d_parent = target;
2268 INIT_LIST_HEAD(&target->d_u.d_child);
2269 } else {
2270 swap(dentry->d_parent, target->d_parent);
2272 /* And add them back to the (new) parent lists */
2273 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
2276 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2278 write_seqcount_end(&target->d_seq);
2279 write_seqcount_end(&dentry->d_seq);
2281 dentry_unlock_parents_for_move(dentry, target);
2282 spin_unlock(&target->d_lock);
2283 fsnotify_d_move(dentry);
2284 spin_unlock(&dentry->d_lock);
2288 * d_move - move a dentry
2289 * @dentry: entry to move
2290 * @target: new dentry
2292 * Update the dcache to reflect the move of a file name. Negative
2293 * dcache entries should not be moved in this way.
2295 void d_move(struct dentry *dentry, struct dentry *target)
2297 write_seqlock(&rename_lock);
2298 __d_move(dentry, target);
2299 write_sequnlock(&rename_lock);
2301 EXPORT_SYMBOL(d_move);
2304 * d_ancestor - search for an ancestor
2305 * @p1: ancestor dentry
2306 * @p2: child dentry
2308 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2309 * an ancestor of p2, else NULL.
2311 struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
2313 struct dentry *p;
2315 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
2316 if (p->d_parent == p1)
2317 return p;
2319 return NULL;
2323 * This helper attempts to cope with remotely renamed directories
2325 * It assumes that the caller is already holding
2326 * dentry->d_parent->d_inode->i_mutex, inode->i_lock and rename_lock
2328 * Note: If ever the locking in lock_rename() changes, then please
2329 * remember to update this too...
2331 static struct dentry *__d_unalias(struct inode *inode,
2332 struct dentry *dentry, struct dentry *alias)
2334 struct mutex *m1 = NULL, *m2 = NULL;
2335 struct dentry *ret;
2337 /* If alias and dentry share a parent, then no extra locks required */
2338 if (alias->d_parent == dentry->d_parent)
2339 goto out_unalias;
2341 /* See lock_rename() */
2342 ret = ERR_PTR(-EBUSY);
2343 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2344 goto out_err;
2345 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2346 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2347 goto out_err;
2348 m2 = &alias->d_parent->d_inode->i_mutex;
2349 out_unalias:
2350 __d_move(alias, dentry);
2351 ret = alias;
2352 out_err:
2353 spin_unlock(&inode->i_lock);
2354 if (m2)
2355 mutex_unlock(m2);
2356 if (m1)
2357 mutex_unlock(m1);
2358 return ret;
2362 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2363 * named dentry in place of the dentry to be replaced.
2364 * returns with anon->d_lock held!
2366 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2368 struct dentry *dparent, *aparent;
2370 dentry_lock_for_move(anon, dentry);
2372 write_seqcount_begin(&dentry->d_seq);
2373 write_seqcount_begin(&anon->d_seq);
2375 dparent = dentry->d_parent;
2376 aparent = anon->d_parent;
2378 switch_names(dentry, anon);
2379 swap(dentry->d_name.hash, anon->d_name.hash);
2381 dentry->d_parent = (aparent == anon) ? dentry : aparent;
2382 list_del(&dentry->d_u.d_child);
2383 if (!IS_ROOT(dentry))
2384 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2385 else
2386 INIT_LIST_HEAD(&dentry->d_u.d_child);
2388 anon->d_parent = (dparent == dentry) ? anon : dparent;
2389 list_del(&anon->d_u.d_child);
2390 if (!IS_ROOT(anon))
2391 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
2392 else
2393 INIT_LIST_HEAD(&anon->d_u.d_child);
2395 write_seqcount_end(&dentry->d_seq);
2396 write_seqcount_end(&anon->d_seq);
2398 dentry_unlock_parents_for_move(anon, dentry);
2399 spin_unlock(&dentry->d_lock);
2401 /* anon->d_lock still locked, returns locked */
2402 anon->d_flags &= ~DCACHE_DISCONNECTED;
2406 * d_materialise_unique - introduce an inode into the tree
2407 * @dentry: candidate dentry
2408 * @inode: inode to bind to the dentry, to which aliases may be attached
2410 * Introduces an dentry into the tree, substituting an extant disconnected
2411 * root directory alias in its place if there is one
2413 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2415 struct dentry *actual;
2417 BUG_ON(!d_unhashed(dentry));
2419 if (!inode) {
2420 actual = dentry;
2421 __d_instantiate(dentry, NULL);
2422 d_rehash(actual);
2423 goto out_nolock;
2426 spin_lock(&inode->i_lock);
2428 if (S_ISDIR(inode->i_mode)) {
2429 struct dentry *alias;
2431 /* Does an aliased dentry already exist? */
2432 alias = __d_find_alias(inode, 0);
2433 if (alias) {
2434 actual = alias;
2435 write_seqlock(&rename_lock);
2437 if (d_ancestor(alias, dentry)) {
2438 /* Check for loops */
2439 actual = ERR_PTR(-ELOOP);
2440 spin_unlock(&inode->i_lock);
2441 } else if (IS_ROOT(alias)) {
2442 /* Is this an anonymous mountpoint that we
2443 * could splice into our tree? */
2444 __d_materialise_dentry(dentry, alias);
2445 write_sequnlock(&rename_lock);
2446 __d_drop(alias);
2447 goto found;
2448 } else {
2449 /* Nope, but we must(!) avoid directory
2450 * aliasing. This drops inode->i_lock */
2451 actual = __d_unalias(inode, dentry, alias);
2453 write_sequnlock(&rename_lock);
2454 if (IS_ERR(actual))
2455 dput(alias);
2456 goto out_nolock;
2460 /* Add a unique reference */
2461 actual = __d_instantiate_unique(dentry, inode);
2462 if (!actual)
2463 actual = dentry;
2464 else
2465 BUG_ON(!d_unhashed(actual));
2467 spin_lock(&actual->d_lock);
2468 found:
2469 _d_rehash(actual);
2470 spin_unlock(&actual->d_lock);
2471 spin_unlock(&inode->i_lock);
2472 out_nolock:
2473 if (actual == dentry) {
2474 security_d_instantiate(dentry, inode);
2475 return NULL;
2478 iput(inode);
2479 return actual;
2481 EXPORT_SYMBOL_GPL(d_materialise_unique);
2483 static int prepend(char **buffer, int *buflen, const char *str, int namelen)
2485 *buflen -= namelen;
2486 if (*buflen < 0)
2487 return -ENAMETOOLONG;
2488 *buffer -= namelen;
2489 memcpy(*buffer, str, namelen);
2490 return 0;
2493 static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2495 return prepend(buffer, buflen, name->name, name->len);
2499 * prepend_path - Prepend path string to a buffer
2500 * @path: the dentry/vfsmount to report
2501 * @root: root vfsmnt/dentry
2502 * @buffer: pointer to the end of the buffer
2503 * @buflen: pointer to buffer length
2505 * Caller holds the rename_lock.
2507 static int prepend_path(const struct path *path,
2508 const struct path *root,
2509 char **buffer, int *buflen)
2511 struct dentry *dentry = path->dentry;
2512 struct vfsmount *vfsmnt = path->mnt;
2513 bool slash = false;
2514 int error = 0;
2516 br_read_lock(vfsmount_lock);
2517 while (dentry != root->dentry || vfsmnt != root->mnt) {
2518 struct dentry * parent;
2520 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2521 /* Global root? */
2522 if (vfsmnt->mnt_parent == vfsmnt) {
2523 goto global_root;
2525 dentry = vfsmnt->mnt_mountpoint;
2526 vfsmnt = vfsmnt->mnt_parent;
2527 continue;
2529 parent = dentry->d_parent;
2530 prefetch(parent);
2531 spin_lock(&dentry->d_lock);
2532 error = prepend_name(buffer, buflen, &dentry->d_name);
2533 spin_unlock(&dentry->d_lock);
2534 if (!error)
2535 error = prepend(buffer, buflen, "/", 1);
2536 if (error)
2537 break;
2539 slash = true;
2540 dentry = parent;
2543 if (!error && !slash)
2544 error = prepend(buffer, buflen, "/", 1);
2546 out:
2547 br_read_unlock(vfsmount_lock);
2548 return error;
2550 global_root:
2552 * Filesystems needing to implement special "root names"
2553 * should do so with ->d_dname()
2555 if (IS_ROOT(dentry) &&
2556 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2557 WARN(1, "Root dentry has weird name <%.*s>\n",
2558 (int) dentry->d_name.len, dentry->d_name.name);
2560 if (!slash)
2561 error = prepend(buffer, buflen, "/", 1);
2562 if (!error)
2563 error = vfsmnt->mnt_ns ? 1 : 2;
2564 goto out;
2568 * __d_path - return the path of a dentry
2569 * @path: the dentry/vfsmount to report
2570 * @root: root vfsmnt/dentry
2571 * @buf: buffer to return value in
2572 * @buflen: buffer length
2574 * Convert a dentry into an ASCII path name.
2576 * Returns a pointer into the buffer or an error code if the
2577 * path was too long.
2579 * "buflen" should be positive.
2581 * If the path is not reachable from the supplied root, return %NULL.
2583 char *__d_path(const struct path *path,
2584 const struct path *root,
2585 char *buf, int buflen)
2587 char *res = buf + buflen;
2588 int error;
2590 prepend(&res, &buflen, "\0", 1);
2591 write_seqlock(&rename_lock);
2592 error = prepend_path(path, root, &res, &buflen);
2593 write_sequnlock(&rename_lock);
2595 if (error < 0)
2596 return ERR_PTR(error);
2597 if (error > 0)
2598 return NULL;
2599 return res;
2602 char *d_absolute_path(const struct path *path,
2603 char *buf, int buflen)
2605 struct path root = {};
2606 char *res = buf + buflen;
2607 int error;
2609 prepend(&res, &buflen, "\0", 1);
2610 write_seqlock(&rename_lock);
2611 error = prepend_path(path, &root, &res, &buflen);
2612 write_sequnlock(&rename_lock);
2614 if (error > 1)
2615 error = -EINVAL;
2616 if (error < 0)
2617 return ERR_PTR(error);
2618 return res;
2622 * same as __d_path but appends "(deleted)" for unlinked files.
2624 static int path_with_deleted(const struct path *path,
2625 const struct path *root,
2626 char **buf, int *buflen)
2628 prepend(buf, buflen, "\0", 1);
2629 if (d_unlinked(path->dentry)) {
2630 int error = prepend(buf, buflen, " (deleted)", 10);
2631 if (error)
2632 return error;
2635 return prepend_path(path, root, buf, buflen);
2638 static int prepend_unreachable(char **buffer, int *buflen)
2640 return prepend(buffer, buflen, "(unreachable)", 13);
2644 * d_path - return the path of a dentry
2645 * @path: path to report
2646 * @buf: buffer to return value in
2647 * @buflen: buffer length
2649 * Convert a dentry into an ASCII path name. If the entry has been deleted
2650 * the string " (deleted)" is appended. Note that this is ambiguous.
2652 * Returns a pointer into the buffer or an error code if the path was
2653 * too long. Note: Callers should use the returned pointer, not the passed
2654 * in buffer, to use the name! The implementation often starts at an offset
2655 * into the buffer, and may leave 0 bytes at the start.
2657 * "buflen" should be positive.
2659 char *d_path(const struct path *path, char *buf, int buflen)
2661 char *res = buf + buflen;
2662 struct path root;
2663 int error;
2666 * We have various synthetic filesystems that never get mounted. On
2667 * these filesystems dentries are never used for lookup purposes, and
2668 * thus don't need to be hashed. They also don't need a name until a
2669 * user wants to identify the object in /proc/pid/fd/. The little hack
2670 * below allows us to generate a name for these objects on demand:
2672 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2673 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2675 get_fs_root(current->fs, &root);
2676 write_seqlock(&rename_lock);
2677 error = path_with_deleted(path, &root, &res, &buflen);
2678 if (error < 0)
2679 res = ERR_PTR(error);
2680 write_sequnlock(&rename_lock);
2681 path_put(&root);
2682 return res;
2684 EXPORT_SYMBOL(d_path);
2687 * d_path_with_unreachable - return the path of a dentry
2688 * @path: path to report
2689 * @buf: buffer to return value in
2690 * @buflen: buffer length
2692 * The difference from d_path() is that this prepends "(unreachable)"
2693 * to paths which are unreachable from the current process' root.
2695 char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2697 char *res = buf + buflen;
2698 struct path root;
2699 int error;
2701 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2702 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2704 get_fs_root(current->fs, &root);
2705 write_seqlock(&rename_lock);
2706 error = path_with_deleted(path, &root, &res, &buflen);
2707 if (error > 0)
2708 error = prepend_unreachable(&res, &buflen);
2709 write_sequnlock(&rename_lock);
2710 path_put(&root);
2711 if (error)
2712 res = ERR_PTR(error);
2714 return res;
2718 * Helper function for dentry_operations.d_dname() members
2720 char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2721 const char *fmt, ...)
2723 va_list args;
2724 char temp[64];
2725 int sz;
2727 va_start(args, fmt);
2728 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2729 va_end(args);
2731 if (sz > sizeof(temp) || sz > buflen)
2732 return ERR_PTR(-ENAMETOOLONG);
2734 buffer += buflen - sz;
2735 return memcpy(buffer, temp, sz);
2739 * Write full pathname from the root of the filesystem into the buffer.
2741 static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
2743 char *end = buf + buflen;
2744 char *retval;
2746 prepend(&end, &buflen, "\0", 1);
2747 if (buflen < 1)
2748 goto Elong;
2749 /* Get '/' right */
2750 retval = end-1;
2751 *retval = '/';
2753 while (!IS_ROOT(dentry)) {
2754 struct dentry *parent = dentry->d_parent;
2755 int error;
2757 prefetch(parent);
2758 spin_lock(&dentry->d_lock);
2759 error = prepend_name(&end, &buflen, &dentry->d_name);
2760 spin_unlock(&dentry->d_lock);
2761 if (error != 0 || prepend(&end, &buflen, "/", 1) != 0)
2762 goto Elong;
2764 retval = end;
2765 dentry = parent;
2767 return retval;
2768 Elong:
2769 return ERR_PTR(-ENAMETOOLONG);
2772 char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2774 char *retval;
2776 write_seqlock(&rename_lock);
2777 retval = __dentry_path(dentry, buf, buflen);
2778 write_sequnlock(&rename_lock);
2780 return retval;
2782 EXPORT_SYMBOL(dentry_path_raw);
2784 char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2786 char *p = NULL;
2787 char *retval;
2789 write_seqlock(&rename_lock);
2790 if (d_unlinked(dentry)) {
2791 p = buf + buflen;
2792 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2793 goto Elong;
2794 buflen++;
2796 retval = __dentry_path(dentry, buf, buflen);
2797 write_sequnlock(&rename_lock);
2798 if (!IS_ERR(retval) && p)
2799 *p = '/'; /* restore '/' overriden with '\0' */
2800 return retval;
2801 Elong:
2802 return ERR_PTR(-ENAMETOOLONG);
2806 * NOTE! The user-level library version returns a
2807 * character pointer. The kernel system call just
2808 * returns the length of the buffer filled (which
2809 * includes the ending '\0' character), or a negative
2810 * error value. So libc would do something like
2812 * char *getcwd(char * buf, size_t size)
2814 * int retval;
2816 * retval = sys_getcwd(buf, size);
2817 * if (retval >= 0)
2818 * return buf;
2819 * errno = -retval;
2820 * return NULL;
2823 SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
2825 int error;
2826 struct path pwd, root;
2827 char *page = (char *) __get_free_page(GFP_USER);
2829 if (!page)
2830 return -ENOMEM;
2832 get_fs_root_and_pwd(current->fs, &root, &pwd);
2834 error = -ENOENT;
2835 write_seqlock(&rename_lock);
2836 if (!d_unlinked(pwd.dentry)) {
2837 unsigned long len;
2838 char *cwd = page + PAGE_SIZE;
2839 int buflen = PAGE_SIZE;
2841 prepend(&cwd, &buflen, "\0", 1);
2842 error = prepend_path(&pwd, &root, &cwd, &buflen);
2843 write_sequnlock(&rename_lock);
2845 if (error < 0)
2846 goto out;
2848 /* Unreachable from current root */
2849 if (error > 0) {
2850 error = prepend_unreachable(&cwd, &buflen);
2851 if (error)
2852 goto out;
2855 error = -ERANGE;
2856 len = PAGE_SIZE + page - cwd;
2857 if (len <= size) {
2858 error = len;
2859 if (copy_to_user(buf, cwd, len))
2860 error = -EFAULT;
2862 } else {
2863 write_sequnlock(&rename_lock);
2866 out:
2867 path_put(&pwd);
2868 path_put(&root);
2869 free_page((unsigned long) page);
2870 return error;
2874 * Test whether new_dentry is a subdirectory of old_dentry.
2876 * Trivially implemented using the dcache structure
2880 * is_subdir - is new dentry a subdirectory of old_dentry
2881 * @new_dentry: new dentry
2882 * @old_dentry: old dentry
2884 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2885 * Returns 0 otherwise.
2886 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2889 int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
2891 int result;
2892 unsigned seq;
2894 if (new_dentry == old_dentry)
2895 return 1;
2897 do {
2898 /* for restarting inner loop in case of seq retry */
2899 seq = read_seqbegin(&rename_lock);
2901 * Need rcu_readlock to protect against the d_parent trashing
2902 * due to d_move
2904 rcu_read_lock();
2905 if (d_ancestor(old_dentry, new_dentry))
2906 result = 1;
2907 else
2908 result = 0;
2909 rcu_read_unlock();
2910 } while (read_seqretry(&rename_lock, seq));
2912 return result;
2915 int path_is_under(struct path *path1, struct path *path2)
2917 struct vfsmount *mnt = path1->mnt;
2918 struct dentry *dentry = path1->dentry;
2919 int res;
2921 br_read_lock(vfsmount_lock);
2922 if (mnt != path2->mnt) {
2923 for (;;) {
2924 if (mnt->mnt_parent == mnt) {
2925 br_read_unlock(vfsmount_lock);
2926 return 0;
2928 if (mnt->mnt_parent == path2->mnt)
2929 break;
2930 mnt = mnt->mnt_parent;
2932 dentry = mnt->mnt_mountpoint;
2934 res = is_subdir(dentry, path2->dentry);
2935 br_read_unlock(vfsmount_lock);
2936 return res;
2938 EXPORT_SYMBOL(path_is_under);
2940 void d_genocide(struct dentry *root)
2942 struct dentry *this_parent;
2943 struct list_head *next;
2944 unsigned seq;
2945 int locked = 0;
2947 seq = read_seqbegin(&rename_lock);
2948 again:
2949 this_parent = root;
2950 spin_lock(&this_parent->d_lock);
2951 repeat:
2952 next = this_parent->d_subdirs.next;
2953 resume:
2954 while (next != &this_parent->d_subdirs) {
2955 struct list_head *tmp = next;
2956 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
2957 next = tmp->next;
2959 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2960 if (d_unhashed(dentry) || !dentry->d_inode) {
2961 spin_unlock(&dentry->d_lock);
2962 continue;
2964 if (!list_empty(&dentry->d_subdirs)) {
2965 spin_unlock(&this_parent->d_lock);
2966 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
2967 this_parent = dentry;
2968 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
2969 goto repeat;
2971 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
2972 dentry->d_flags |= DCACHE_GENOCIDE;
2973 dentry->d_count--;
2975 spin_unlock(&dentry->d_lock);
2977 if (this_parent != root) {
2978 struct dentry *child = this_parent;
2979 if (!(this_parent->d_flags & DCACHE_GENOCIDE)) {
2980 this_parent->d_flags |= DCACHE_GENOCIDE;
2981 this_parent->d_count--;
2983 this_parent = try_to_ascend(this_parent, locked, seq);
2984 if (!this_parent)
2985 goto rename_retry;
2986 next = child->d_u.d_child.next;
2987 goto resume;
2989 spin_unlock(&this_parent->d_lock);
2990 if (!locked && read_seqretry(&rename_lock, seq))
2991 goto rename_retry;
2992 if (locked)
2993 write_sequnlock(&rename_lock);
2994 return;
2996 rename_retry:
2997 if (locked)
2998 goto again;
2999 locked = 1;
3000 write_seqlock(&rename_lock);
3001 goto again;
3005 * find_inode_number - check for dentry with name
3006 * @dir: directory to check
3007 * @name: Name to find.
3009 * Check whether a dentry already exists for the given name,
3010 * and return the inode number if it has an inode. Otherwise
3011 * 0 is returned.
3013 * This routine is used to post-process directory listings for
3014 * filesystems using synthetic inode numbers, and is necessary
3015 * to keep getcwd() working.
3018 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
3020 struct dentry * dentry;
3021 ino_t ino = 0;
3023 dentry = d_hash_and_lookup(dir, name);
3024 if (dentry) {
3025 if (dentry->d_inode)
3026 ino = dentry->d_inode->i_ino;
3027 dput(dentry);
3029 return ino;
3031 EXPORT_SYMBOL(find_inode_number);
3033 static __initdata unsigned long dhash_entries;
3034 static int __init set_dhash_entries(char *str)
3036 if (!str)
3037 return 0;
3038 dhash_entries = simple_strtoul(str, &str, 0);
3039 return 1;
3041 __setup("dhash_entries=", set_dhash_entries);
3043 static void __init dcache_init_early(void)
3045 int loop;
3047 /* If hashes are distributed across NUMA nodes, defer
3048 * hash allocation until vmalloc space is available.
3050 if (hashdist)
3051 return;
3053 dentry_hashtable =
3054 alloc_large_system_hash("Dentry cache",
3055 sizeof(struct hlist_bl_head),
3056 dhash_entries,
3058 HASH_EARLY,
3059 &d_hash_shift,
3060 &d_hash_mask,
3063 for (loop = 0; loop < (1 << d_hash_shift); loop++)
3064 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
3067 static void __init dcache_init(void)
3069 int loop;
3072 * A constructor could be added for stable state like the lists,
3073 * but it is probably not worth it because of the cache nature
3074 * of the dcache.
3076 dentry_cache = KMEM_CACHE(dentry,
3077 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
3079 register_shrinker(&dcache_shrinker);
3081 /* Hash may have been set up in dcache_init_early */
3082 if (!hashdist)
3083 return;
3085 dentry_hashtable =
3086 alloc_large_system_hash("Dentry cache",
3087 sizeof(struct hlist_bl_head),
3088 dhash_entries,
3091 &d_hash_shift,
3092 &d_hash_mask,
3095 for (loop = 0; loop < (1 << d_hash_shift); loop++)
3096 INIT_HLIST_BL_HEAD(dentry_hashtable + loop);
3099 /* SLAB cache for __getname() consumers */
3100 struct kmem_cache *names_cachep __read_mostly;
3101 EXPORT_SYMBOL(names_cachep);
3103 EXPORT_SYMBOL(d_genocide);
3105 void __init vfs_caches_init_early(void)
3107 dcache_init_early();
3108 inode_init_early();
3111 void __init vfs_caches_init(unsigned long mempages)
3113 unsigned long reserve;
3115 /* Base hash sizes on available memory, with a reserve equal to
3116 150% of current kernel size */
3118 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
3119 mempages -= reserve;
3121 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
3122 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
3124 dcache_init();
3125 inode_init();
3126 files_init(mempages);
3127 mnt_init();
3128 bdev_cache_init();
3129 chrdev_init();