4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
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>
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>
40 * dcache_inode_lock protects:
41 * - i_dentry, d_alias, d_inode
42 * dcache_hash_lock protects:
43 * - the dcache hash table, s_anon lists
44 * dcache_lru_lock protects:
45 * - the dcache lru lists and counters
52 * - d_parent and d_subdirs
53 * - childrens' d_child and d_parent
63 * If there is an ancestor relationship:
64 * dentry->d_parent->...->d_parent->d_lock
66 * dentry->d_parent->d_lock
69 * If no ancestor relationship:
70 * if (dentry1 < dentry2)
74 int sysctl_vfs_cache_pressure __read_mostly = 100;
75 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
77 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_inode_lock);
78 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_hash_lock);
79 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
80 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
81 __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
83 EXPORT_SYMBOL(dcache_inode_lock);
84 EXPORT_SYMBOL(dcache_lock);
86 static struct kmem_cache *dentry_cache __read_mostly;
88 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
91 * This is the single most critical data structure when it comes
92 * to the dcache: the hashtable for lookups. Somebody should try
93 * to make this good - I've just made it work.
95 * This hash-function tries to avoid losing too many bits of hash
96 * information, yet avoid using a prime hash-size or similar.
98 #define D_HASHBITS d_hash_shift
99 #define D_HASHMASK d_hash_mask
101 static unsigned int d_hash_mask __read_mostly;
102 static unsigned int d_hash_shift __read_mostly;
103 static struct hlist_head *dentry_hashtable __read_mostly;
105 /* Statistics gathering. */
106 struct dentry_stat_t dentry_stat = {
110 static DEFINE_PER_CPU(unsigned int, nr_dentry);
112 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
113 static int get_nr_dentry(void)
117 for_each_possible_cpu(i)
118 sum += per_cpu(nr_dentry, i);
119 return sum < 0 ? 0 : sum;
122 int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
123 size_t *lenp, loff_t *ppos)
125 dentry_stat.nr_dentry = get_nr_dentry();
126 return proc_dointvec(table, write, buffer, lenp, ppos);
130 static void __d_free(struct rcu_head *head)
132 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
134 WARN_ON(!list_empty(&dentry->d_alias));
135 if (dname_external(dentry))
136 kfree(dentry->d_name.name);
137 kmem_cache_free(dentry_cache, dentry);
141 * no dcache_lock, please.
143 static void d_free(struct dentry *dentry)
145 BUG_ON(dentry->d_count);
146 this_cpu_dec(nr_dentry);
147 if (dentry->d_op && dentry->d_op->d_release)
148 dentry->d_op->d_release(dentry);
150 /* if dentry was never inserted into hash, immediate free is OK */
151 if (hlist_unhashed(&dentry->d_hash))
152 __d_free(&dentry->d_u.d_rcu);
154 call_rcu(&dentry->d_u.d_rcu, __d_free);
158 * Release the dentry's inode, using the filesystem
159 * d_iput() operation if defined.
161 static void dentry_iput(struct dentry * dentry)
162 __releases(dentry->d_lock)
163 __releases(dcache_inode_lock)
164 __releases(dcache_lock)
166 struct inode *inode = dentry->d_inode;
168 dentry->d_inode = NULL;
169 list_del_init(&dentry->d_alias);
170 spin_unlock(&dentry->d_lock);
171 spin_unlock(&dcache_inode_lock);
172 spin_unlock(&dcache_lock);
174 fsnotify_inoderemove(inode);
175 if (dentry->d_op && dentry->d_op->d_iput)
176 dentry->d_op->d_iput(dentry, inode);
180 spin_unlock(&dentry->d_lock);
181 spin_unlock(&dcache_inode_lock);
182 spin_unlock(&dcache_lock);
187 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
189 static void dentry_lru_add(struct dentry *dentry)
191 if (list_empty(&dentry->d_lru)) {
192 spin_lock(&dcache_lru_lock);
193 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
194 dentry->d_sb->s_nr_dentry_unused++;
195 dentry_stat.nr_unused++;
196 spin_unlock(&dcache_lru_lock);
200 static void __dentry_lru_del(struct dentry *dentry)
202 list_del_init(&dentry->d_lru);
203 dentry->d_sb->s_nr_dentry_unused--;
204 dentry_stat.nr_unused--;
207 static void dentry_lru_del(struct dentry *dentry)
209 if (!list_empty(&dentry->d_lru)) {
210 spin_lock(&dcache_lru_lock);
211 __dentry_lru_del(dentry);
212 spin_unlock(&dcache_lru_lock);
216 static void dentry_lru_move_tail(struct dentry *dentry)
218 spin_lock(&dcache_lru_lock);
219 if (list_empty(&dentry->d_lru)) {
220 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
221 dentry->d_sb->s_nr_dentry_unused++;
222 dentry_stat.nr_unused++;
224 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
226 spin_unlock(&dcache_lru_lock);
230 * d_kill - kill dentry and return parent
231 * @dentry: dentry to kill
233 * The dentry must already be unhashed and removed from the LRU.
235 * If this is the root of the dentry tree, return NULL.
237 * dcache_lock and d_lock and d_parent->d_lock must be held by caller, and
238 * are dropped by d_kill.
240 static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
241 __releases(dentry->d_lock)
242 __releases(parent->d_lock)
243 __releases(dcache_inode_lock)
244 __releases(dcache_lock)
246 list_del(&dentry->d_u.d_child);
248 spin_unlock(&parent->d_lock);
251 * dentry_iput drops the locks, at which point nobody (except
252 * transient RCU lookups) can reach this dentry.
259 * d_drop - drop a dentry
260 * @dentry: dentry to drop
262 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
263 * be found through a VFS lookup any more. Note that this is different from
264 * deleting the dentry - d_delete will try to mark the dentry negative if
265 * possible, giving a successful _negative_ lookup, while d_drop will
266 * just make the cache lookup fail.
268 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
269 * reason (NFS timeouts or autofs deletes).
271 * __d_drop requires dentry->d_lock.
273 void __d_drop(struct dentry *dentry)
275 if (!(dentry->d_flags & DCACHE_UNHASHED)) {
276 dentry->d_flags |= DCACHE_UNHASHED;
277 spin_lock(&dcache_hash_lock);
278 hlist_del_rcu(&dentry->d_hash);
279 spin_unlock(&dcache_hash_lock);
282 EXPORT_SYMBOL(__d_drop);
284 void d_drop(struct dentry *dentry)
286 spin_lock(&dcache_lock);
287 spin_lock(&dentry->d_lock);
289 spin_unlock(&dentry->d_lock);
290 spin_unlock(&dcache_lock);
292 EXPORT_SYMBOL(d_drop);
297 * This is complicated by the fact that we do not want to put
298 * dentries that are no longer on any hash chain on the unused
299 * list: we'd much rather just get rid of them immediately.
301 * However, that implies that we have to traverse the dentry
302 * tree upwards to the parents which might _also_ now be
303 * scheduled for deletion (it may have been only waiting for
304 * its last child to go away).
306 * This tail recursion is done by hand as we don't want to depend
307 * on the compiler to always get this right (gcc generally doesn't).
308 * Real recursion would eat up our stack space.
312 * dput - release a dentry
313 * @dentry: dentry to release
315 * Release a dentry. This will drop the usage count and if appropriate
316 * call the dentry unlink method as well as removing it from the queues and
317 * releasing its resources. If the parent dentries were scheduled for release
318 * they too may now get deleted.
320 * no dcache lock, please.
323 void dput(struct dentry *dentry)
325 struct dentry *parent;
330 if (dentry->d_count == 1)
332 spin_lock(&dentry->d_lock);
336 parent = dentry->d_parent;
337 if (dentry->d_count == 1) {
338 if (!spin_trylock(&dcache_lock)) {
340 * Something of a livelock possibility we could avoid
341 * by taking dcache_lock and trying again, but we
342 * want to reduce dcache_lock anyway so this will
346 spin_unlock(&dentry->d_lock);
349 if (!spin_trylock(&dcache_inode_lock)) {
351 spin_unlock(&dcache_lock);
354 if (parent && !spin_trylock(&parent->d_lock)) {
355 spin_unlock(&dcache_inode_lock);
360 if (dentry->d_count) {
361 spin_unlock(&dentry->d_lock);
363 spin_unlock(&parent->d_lock);
364 spin_unlock(&dcache_lock);
369 * AV: ->d_delete() is _NOT_ allowed to block now.
371 if (dentry->d_op && dentry->d_op->d_delete) {
372 if (dentry->d_op->d_delete(dentry))
376 /* Unreachable? Get rid of it */
377 if (d_unhashed(dentry))
380 /* Otherwise leave it cached and ensure it's on the LRU */
381 dentry->d_flags |= DCACHE_REFERENCED;
382 dentry_lru_add(dentry);
384 spin_unlock(&dentry->d_lock);
386 spin_unlock(&parent->d_lock);
387 spin_unlock(&dcache_inode_lock);
388 spin_unlock(&dcache_lock);
394 /* if dentry was on the d_lru list delete it from there */
395 dentry_lru_del(dentry);
396 dentry = d_kill(dentry, parent);
403 * d_invalidate - invalidate a dentry
404 * @dentry: dentry to invalidate
406 * Try to invalidate the dentry if it turns out to be
407 * possible. If there are other dentries that can be
408 * reached through this one we can't delete it and we
409 * return -EBUSY. On success we return 0.
414 int d_invalidate(struct dentry * dentry)
417 * If it's already been dropped, return OK.
419 spin_lock(&dcache_lock);
420 spin_lock(&dentry->d_lock);
421 if (d_unhashed(dentry)) {
422 spin_unlock(&dentry->d_lock);
423 spin_unlock(&dcache_lock);
427 * Check whether to do a partial shrink_dcache
428 * to get rid of unused child entries.
430 if (!list_empty(&dentry->d_subdirs)) {
431 spin_unlock(&dentry->d_lock);
432 spin_unlock(&dcache_lock);
433 shrink_dcache_parent(dentry);
434 spin_lock(&dcache_lock);
435 spin_lock(&dentry->d_lock);
439 * Somebody else still using it?
441 * If it's a directory, we can't drop it
442 * for fear of somebody re-populating it
443 * with children (even though dropping it
444 * would make it unreachable from the root,
445 * we might still populate it if it was a
446 * working directory or similar).
448 if (dentry->d_count > 1) {
449 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
450 spin_unlock(&dentry->d_lock);
451 spin_unlock(&dcache_lock);
457 spin_unlock(&dentry->d_lock);
458 spin_unlock(&dcache_lock);
461 EXPORT_SYMBOL(d_invalidate);
463 /* This must be called with dcache_lock and d_lock held */
464 static inline struct dentry * __dget_locked_dlock(struct dentry *dentry)
467 dentry_lru_del(dentry);
471 /* This should be called _only_ with dcache_lock held */
472 static inline struct dentry * __dget_locked(struct dentry *dentry)
474 spin_lock(&dentry->d_lock);
475 __dget_locked_dlock(dentry);
476 spin_unlock(&dentry->d_lock);
480 struct dentry * dget_locked_dlock(struct dentry *dentry)
482 return __dget_locked_dlock(dentry);
485 struct dentry * dget_locked(struct dentry *dentry)
487 return __dget_locked(dentry);
489 EXPORT_SYMBOL(dget_locked);
491 struct dentry *dget_parent(struct dentry *dentry)
496 spin_lock(&dentry->d_lock);
497 ret = dentry->d_parent;
504 if (!spin_trylock(&ret->d_lock)) {
505 spin_unlock(&dentry->d_lock);
509 BUG_ON(!ret->d_count);
511 spin_unlock(&ret->d_lock);
513 spin_unlock(&dentry->d_lock);
516 EXPORT_SYMBOL(dget_parent);
519 * d_find_alias - grab a hashed alias of inode
520 * @inode: inode in question
521 * @want_discon: flag, used by d_splice_alias, to request
522 * that only a DISCONNECTED alias be returned.
524 * If inode has a hashed alias, or is a directory and has any alias,
525 * acquire the reference to alias and return it. Otherwise return NULL.
526 * Notice that if inode is a directory there can be only one alias and
527 * it can be unhashed only if it has no children, or if it is the root
530 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
531 * any other hashed alias over that one unless @want_discon is set,
532 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
534 static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
536 struct dentry *alias, *discon_alias;
540 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
541 spin_lock(&alias->d_lock);
542 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
543 if (IS_ROOT(alias) &&
544 (alias->d_flags & DCACHE_DISCONNECTED)) {
545 discon_alias = alias;
546 } else if (!want_discon) {
547 __dget_locked_dlock(alias);
548 spin_unlock(&alias->d_lock);
552 spin_unlock(&alias->d_lock);
555 alias = discon_alias;
556 spin_lock(&alias->d_lock);
557 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
558 if (IS_ROOT(alias) &&
559 (alias->d_flags & DCACHE_DISCONNECTED)) {
560 __dget_locked_dlock(alias);
561 spin_unlock(&alias->d_lock);
565 spin_unlock(&alias->d_lock);
571 struct dentry *d_find_alias(struct inode *inode)
573 struct dentry *de = NULL;
575 if (!list_empty(&inode->i_dentry)) {
576 spin_lock(&dcache_lock);
577 spin_lock(&dcache_inode_lock);
578 de = __d_find_alias(inode, 0);
579 spin_unlock(&dcache_inode_lock);
580 spin_unlock(&dcache_lock);
584 EXPORT_SYMBOL(d_find_alias);
587 * Try to kill dentries associated with this inode.
588 * WARNING: you must own a reference to inode.
590 void d_prune_aliases(struct inode *inode)
592 struct dentry *dentry;
594 spin_lock(&dcache_lock);
595 spin_lock(&dcache_inode_lock);
596 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
597 spin_lock(&dentry->d_lock);
598 if (!dentry->d_count) {
599 __dget_locked_dlock(dentry);
601 spin_unlock(&dentry->d_lock);
602 spin_unlock(&dcache_inode_lock);
603 spin_unlock(&dcache_lock);
607 spin_unlock(&dentry->d_lock);
609 spin_unlock(&dcache_inode_lock);
610 spin_unlock(&dcache_lock);
612 EXPORT_SYMBOL(d_prune_aliases);
615 * Throw away a dentry - free the inode, dput the parent. This requires that
616 * the LRU list has already been removed.
618 * Try to prune ancestors as well. This is necessary to prevent
619 * quadratic behavior of shrink_dcache_parent(), but is also expected
620 * to be beneficial in reducing dentry cache fragmentation.
622 static void prune_one_dentry(struct dentry *dentry, struct dentry *parent)
623 __releases(dentry->d_lock)
624 __releases(parent->d_lock)
625 __releases(dcache_inode_lock)
626 __releases(dcache_lock)
629 dentry = d_kill(dentry, parent);
632 * Prune ancestors. Locking is simpler than in dput(),
633 * because dcache_lock needs to be taken anyway.
636 spin_lock(&dcache_lock);
637 spin_lock(&dcache_inode_lock);
639 spin_lock(&dentry->d_lock);
643 parent = dentry->d_parent;
644 if (parent && !spin_trylock(&parent->d_lock)) {
645 spin_unlock(&dentry->d_lock);
649 if (dentry->d_count) {
651 spin_unlock(&parent->d_lock);
652 spin_unlock(&dentry->d_lock);
653 spin_unlock(&dcache_inode_lock);
654 spin_unlock(&dcache_lock);
658 dentry_lru_del(dentry);
660 dentry = d_kill(dentry, parent);
664 static void shrink_dentry_list(struct list_head *list)
666 struct dentry *dentry;
668 while (!list_empty(list)) {
669 struct dentry *parent;
671 dentry = list_entry(list->prev, struct dentry, d_lru);
673 if (!spin_trylock(&dentry->d_lock)) {
675 spin_unlock(&dcache_lru_lock);
677 spin_lock(&dcache_lru_lock);
682 * We found an inuse dentry which was not removed from
683 * the LRU because of laziness during lookup. Do not free
684 * it - just keep it off the LRU list.
686 if (dentry->d_count) {
687 __dentry_lru_del(dentry);
688 spin_unlock(&dentry->d_lock);
694 parent = dentry->d_parent;
695 if (parent && !spin_trylock(&parent->d_lock)) {
696 spin_unlock(&dentry->d_lock);
699 __dentry_lru_del(dentry);
700 spin_unlock(&dcache_lru_lock);
702 prune_one_dentry(dentry, parent);
703 /* dcache_lock, dcache_inode_lock and dentry->d_lock dropped */
704 spin_lock(&dcache_lock);
705 spin_lock(&dcache_inode_lock);
706 spin_lock(&dcache_lru_lock);
711 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
712 * @sb: superblock to shrink dentry LRU.
713 * @count: number of entries to prune
714 * @flags: flags to control the dentry processing
716 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
718 static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
720 /* called from prune_dcache() and shrink_dcache_parent() */
721 struct dentry *dentry;
722 LIST_HEAD(referenced);
726 spin_lock(&dcache_lock);
727 spin_lock(&dcache_inode_lock);
729 spin_lock(&dcache_lru_lock);
730 while (!list_empty(&sb->s_dentry_lru)) {
731 dentry = list_entry(sb->s_dentry_lru.prev,
732 struct dentry, d_lru);
733 BUG_ON(dentry->d_sb != sb);
735 if (!spin_trylock(&dentry->d_lock)) {
736 spin_unlock(&dcache_lru_lock);
742 * If we are honouring the DCACHE_REFERENCED flag and the
743 * dentry has this flag set, don't free it. Clear the flag
744 * and put it back on the LRU.
746 if (flags & DCACHE_REFERENCED &&
747 dentry->d_flags & DCACHE_REFERENCED) {
748 dentry->d_flags &= ~DCACHE_REFERENCED;
749 list_move(&dentry->d_lru, &referenced);
750 spin_unlock(&dentry->d_lock);
752 list_move_tail(&dentry->d_lru, &tmp);
753 spin_unlock(&dentry->d_lock);
757 /* XXX: re-add cond_resched_lock when dcache_lock goes away */
761 shrink_dentry_list(&tmp);
763 if (!list_empty(&referenced))
764 list_splice(&referenced, &sb->s_dentry_lru);
765 spin_unlock(&dcache_lru_lock);
766 spin_unlock(&dcache_inode_lock);
767 spin_unlock(&dcache_lock);
771 * prune_dcache - shrink the dcache
772 * @count: number of entries to try to free
774 * Shrink the dcache. This is done when we need more memory, or simply when we
775 * need to unmount something (at which point we need to unuse all dentries).
777 * This function may fail to free any resources if all the dentries are in use.
779 static void prune_dcache(int count)
781 struct super_block *sb, *p = NULL;
783 int unused = dentry_stat.nr_unused;
787 if (unused == 0 || count == 0)
789 spin_lock(&dcache_lock);
793 prune_ratio = unused / count;
795 list_for_each_entry(sb, &super_blocks, s_list) {
796 if (list_empty(&sb->s_instances))
798 if (sb->s_nr_dentry_unused == 0)
801 /* Now, we reclaim unused dentrins with fairness.
802 * We reclaim them same percentage from each superblock.
803 * We calculate number of dentries to scan on this sb
804 * as follows, but the implementation is arranged to avoid
806 * number of dentries to scan on this sb =
807 * count * (number of dentries on this sb /
808 * number of dentries in the machine)
810 spin_unlock(&sb_lock);
811 if (prune_ratio != 1)
812 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
814 w_count = sb->s_nr_dentry_unused;
817 * We need to be sure this filesystem isn't being unmounted,
818 * otherwise we could race with generic_shutdown_super(), and
819 * end up holding a reference to an inode while the filesystem
820 * is unmounted. So we try to get s_umount, and make sure
823 if (down_read_trylock(&sb->s_umount)) {
824 if ((sb->s_root != NULL) &&
825 (!list_empty(&sb->s_dentry_lru))) {
826 spin_unlock(&dcache_lock);
827 __shrink_dcache_sb(sb, &w_count,
830 spin_lock(&dcache_lock);
832 up_read(&sb->s_umount);
839 /* more work left to do? */
845 spin_unlock(&sb_lock);
846 spin_unlock(&dcache_lock);
850 * shrink_dcache_sb - shrink dcache for a superblock
853 * Shrink the dcache for the specified super block. This is used to free
854 * the dcache before unmounting a file system.
856 void shrink_dcache_sb(struct super_block *sb)
860 spin_lock(&dcache_lock);
861 spin_lock(&dcache_inode_lock);
862 spin_lock(&dcache_lru_lock);
863 while (!list_empty(&sb->s_dentry_lru)) {
864 list_splice_init(&sb->s_dentry_lru, &tmp);
865 shrink_dentry_list(&tmp);
867 spin_unlock(&dcache_lru_lock);
868 spin_unlock(&dcache_inode_lock);
869 spin_unlock(&dcache_lock);
871 EXPORT_SYMBOL(shrink_dcache_sb);
874 * destroy a single subtree of dentries for unmount
875 * - see the comments on shrink_dcache_for_umount() for a description of the
878 static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
880 struct dentry *parent;
881 unsigned detached = 0;
883 BUG_ON(!IS_ROOT(dentry));
885 /* detach this root from the system */
886 spin_lock(&dcache_lock);
887 spin_lock(&dentry->d_lock);
888 dentry_lru_del(dentry);
890 spin_unlock(&dentry->d_lock);
891 spin_unlock(&dcache_lock);
894 /* descend to the first leaf in the current subtree */
895 while (!list_empty(&dentry->d_subdirs)) {
898 /* this is a branch with children - detach all of them
899 * from the system in one go */
900 spin_lock(&dcache_lock);
901 spin_lock(&dentry->d_lock);
902 list_for_each_entry(loop, &dentry->d_subdirs,
904 spin_lock_nested(&loop->d_lock,
905 DENTRY_D_LOCK_NESTED);
906 dentry_lru_del(loop);
908 spin_unlock(&loop->d_lock);
910 spin_unlock(&dentry->d_lock);
911 spin_unlock(&dcache_lock);
913 /* move to the first child */
914 dentry = list_entry(dentry->d_subdirs.next,
915 struct dentry, d_u.d_child);
918 /* consume the dentries from this leaf up through its parents
919 * until we find one with children or run out altogether */
923 if (dentry->d_count != 0) {
925 "BUG: Dentry %p{i=%lx,n=%s}"
927 " [unmount of %s %s]\n",
930 dentry->d_inode->i_ino : 0UL,
933 dentry->d_sb->s_type->name,
938 if (IS_ROOT(dentry)) {
940 list_del(&dentry->d_u.d_child);
942 parent = dentry->d_parent;
943 spin_lock(&parent->d_lock);
945 list_del(&dentry->d_u.d_child);
946 spin_unlock(&parent->d_lock);
951 inode = dentry->d_inode;
953 dentry->d_inode = NULL;
954 list_del_init(&dentry->d_alias);
955 if (dentry->d_op && dentry->d_op->d_iput)
956 dentry->d_op->d_iput(dentry, inode);
963 /* finished when we fall off the top of the tree,
964 * otherwise we ascend to the parent and move to the
965 * next sibling if there is one */
969 } while (list_empty(&dentry->d_subdirs));
971 dentry = list_entry(dentry->d_subdirs.next,
972 struct dentry, d_u.d_child);
977 * destroy the dentries attached to a superblock on unmounting
978 * - we don't need to use dentry->d_lock, and only need dcache_lock when
979 * removing the dentry from the system lists and hashes because:
980 * - the superblock is detached from all mountings and open files, so the
981 * dentry trees will not be rearranged by the VFS
982 * - s_umount is write-locked, so the memory pressure shrinker will ignore
983 * any dentries belonging to this superblock that it comes across
984 * - the filesystem itself is no longer permitted to rearrange the dentries
987 void shrink_dcache_for_umount(struct super_block *sb)
989 struct dentry *dentry;
991 if (down_read_trylock(&sb->s_umount))
996 spin_lock(&dentry->d_lock);
998 spin_unlock(&dentry->d_lock);
999 shrink_dcache_for_umount_subtree(dentry);
1001 while (!hlist_empty(&sb->s_anon)) {
1002 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
1003 shrink_dcache_for_umount_subtree(dentry);
1008 * Search for at least 1 mount point in the dentry's subdirs.
1009 * We descend to the next level whenever the d_subdirs
1010 * list is non-empty and continue searching.
1014 * have_submounts - check for mounts over a dentry
1015 * @parent: dentry to check.
1017 * Return true if the parent or its subdirectories contain
1021 int have_submounts(struct dentry *parent)
1023 struct dentry *this_parent = parent;
1024 struct list_head *next;
1026 spin_lock(&dcache_lock);
1027 if (d_mountpoint(parent))
1029 spin_lock(&this_parent->d_lock);
1031 next = this_parent->d_subdirs.next;
1033 while (next != &this_parent->d_subdirs) {
1034 struct list_head *tmp = next;
1035 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1038 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1039 /* Have we found a mount point ? */
1040 if (d_mountpoint(dentry)) {
1041 spin_unlock(&dentry->d_lock);
1042 spin_unlock(&this_parent->d_lock);
1045 if (!list_empty(&dentry->d_subdirs)) {
1046 spin_unlock(&this_parent->d_lock);
1047 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
1048 this_parent = dentry;
1049 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1052 spin_unlock(&dentry->d_lock);
1055 * All done at this level ... ascend and resume the search.
1057 if (this_parent != parent) {
1058 next = this_parent->d_u.d_child.next;
1059 spin_unlock(&this_parent->d_lock);
1060 this_parent = this_parent->d_parent;
1061 spin_lock(&this_parent->d_lock);
1064 spin_unlock(&this_parent->d_lock);
1065 spin_unlock(&dcache_lock);
1066 return 0; /* No mount points found in tree */
1068 spin_unlock(&dcache_lock);
1071 EXPORT_SYMBOL(have_submounts);
1074 * Search the dentry child list for the specified parent,
1075 * and move any unused dentries to the end of the unused
1076 * list for prune_dcache(). We descend to the next level
1077 * whenever the d_subdirs list is non-empty and continue
1080 * It returns zero iff there are no unused children,
1081 * otherwise it returns the number of children moved to
1082 * the end of the unused list. This may not be the total
1083 * number of unused children, because select_parent can
1084 * drop the lock and return early due to latency
1087 static int select_parent(struct dentry * parent)
1089 struct dentry *this_parent = parent;
1090 struct list_head *next;
1093 spin_lock(&dcache_lock);
1094 spin_lock(&this_parent->d_lock);
1096 next = this_parent->d_subdirs.next;
1098 while (next != &this_parent->d_subdirs) {
1099 struct list_head *tmp = next;
1100 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1102 BUG_ON(this_parent == dentry);
1104 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1107 * move only zero ref count dentries to the end
1108 * of the unused list for prune_dcache
1110 if (!dentry->d_count) {
1111 dentry_lru_move_tail(dentry);
1114 dentry_lru_del(dentry);
1118 * We can return to the caller if we have found some (this
1119 * ensures forward progress). We'll be coming back to find
1122 if (found && need_resched()) {
1123 spin_unlock(&dentry->d_lock);
1128 * Descend a level if the d_subdirs list is non-empty.
1130 if (!list_empty(&dentry->d_subdirs)) {
1131 spin_unlock(&this_parent->d_lock);
1132 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
1133 this_parent = dentry;
1134 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1138 spin_unlock(&dentry->d_lock);
1141 * All done at this level ... ascend and resume the search.
1143 if (this_parent != parent) {
1145 next = this_parent->d_u.d_child.next;
1146 tmp = this_parent->d_parent;
1147 spin_unlock(&this_parent->d_lock);
1148 BUG_ON(tmp == this_parent);
1150 spin_lock(&this_parent->d_lock);
1154 spin_unlock(&this_parent->d_lock);
1155 spin_unlock(&dcache_lock);
1160 * shrink_dcache_parent - prune dcache
1161 * @parent: parent of entries to prune
1163 * Prune the dcache to remove unused children of the parent dentry.
1166 void shrink_dcache_parent(struct dentry * parent)
1168 struct super_block *sb = parent->d_sb;
1171 while ((found = select_parent(parent)) != 0)
1172 __shrink_dcache_sb(sb, &found, 0);
1174 EXPORT_SYMBOL(shrink_dcache_parent);
1177 * Scan `nr' dentries and return the number which remain.
1179 * We need to avoid reentering the filesystem if the caller is performing a
1180 * GFP_NOFS allocation attempt. One example deadlock is:
1182 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1183 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1184 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1186 * In this case we return -1 to tell the caller that we baled.
1188 static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
1191 if (!(gfp_mask & __GFP_FS))
1196 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
1199 static struct shrinker dcache_shrinker = {
1200 .shrink = shrink_dcache_memory,
1201 .seeks = DEFAULT_SEEKS,
1205 * d_alloc - allocate a dcache entry
1206 * @parent: parent of entry to allocate
1207 * @name: qstr of the name
1209 * Allocates a dentry. It returns %NULL if there is insufficient memory
1210 * available. On a success the dentry is returned. The name passed in is
1211 * copied and the copy passed in may be reused after this call.
1214 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1216 struct dentry *dentry;
1219 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
1223 if (name->len > DNAME_INLINE_LEN-1) {
1224 dname = kmalloc(name->len + 1, GFP_KERNEL);
1226 kmem_cache_free(dentry_cache, dentry);
1230 dname = dentry->d_iname;
1232 dentry->d_name.name = dname;
1234 dentry->d_name.len = name->len;
1235 dentry->d_name.hash = name->hash;
1236 memcpy(dname, name->name, name->len);
1237 dname[name->len] = 0;
1239 dentry->d_count = 1;
1240 dentry->d_flags = DCACHE_UNHASHED;
1241 spin_lock_init(&dentry->d_lock);
1242 dentry->d_inode = NULL;
1243 dentry->d_parent = NULL;
1244 dentry->d_sb = NULL;
1245 dentry->d_op = NULL;
1246 dentry->d_fsdata = NULL;
1247 dentry->d_mounted = 0;
1248 INIT_HLIST_NODE(&dentry->d_hash);
1249 INIT_LIST_HEAD(&dentry->d_lru);
1250 INIT_LIST_HEAD(&dentry->d_subdirs);
1251 INIT_LIST_HEAD(&dentry->d_alias);
1252 INIT_LIST_HEAD(&dentry->d_u.d_child);
1255 spin_lock(&dcache_lock);
1256 spin_lock(&parent->d_lock);
1257 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1258 dentry->d_parent = dget_dlock(parent);
1259 dentry->d_sb = parent->d_sb;
1260 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
1261 spin_unlock(&dentry->d_lock);
1262 spin_unlock(&parent->d_lock);
1263 spin_unlock(&dcache_lock);
1266 this_cpu_inc(nr_dentry);
1270 EXPORT_SYMBOL(d_alloc);
1272 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1277 q.len = strlen(name);
1278 q.hash = full_name_hash(q.name, q.len);
1279 return d_alloc(parent, &q);
1281 EXPORT_SYMBOL(d_alloc_name);
1283 /* the caller must hold dcache_lock */
1284 static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1286 spin_lock(&dentry->d_lock);
1288 list_add(&dentry->d_alias, &inode->i_dentry);
1289 dentry->d_inode = inode;
1290 spin_unlock(&dentry->d_lock);
1291 fsnotify_d_instantiate(dentry, inode);
1295 * d_instantiate - fill in inode information for a dentry
1296 * @entry: dentry to complete
1297 * @inode: inode to attach to this dentry
1299 * Fill in inode information in the entry.
1301 * This turns negative dentries into productive full members
1304 * NOTE! This assumes that the inode count has been incremented
1305 * (or otherwise set) by the caller to indicate that it is now
1306 * in use by the dcache.
1309 void d_instantiate(struct dentry *entry, struct inode * inode)
1311 BUG_ON(!list_empty(&entry->d_alias));
1312 spin_lock(&dcache_lock);
1313 spin_lock(&dcache_inode_lock);
1314 __d_instantiate(entry, inode);
1315 spin_unlock(&dcache_inode_lock);
1316 spin_unlock(&dcache_lock);
1317 security_d_instantiate(entry, inode);
1319 EXPORT_SYMBOL(d_instantiate);
1322 * d_instantiate_unique - instantiate a non-aliased dentry
1323 * @entry: dentry to instantiate
1324 * @inode: inode to attach to this dentry
1326 * Fill in inode information in the entry. On success, it returns NULL.
1327 * If an unhashed alias of "entry" already exists, then we return the
1328 * aliased dentry instead and drop one reference to inode.
1330 * Note that in order to avoid conflicts with rename() etc, the caller
1331 * had better be holding the parent directory semaphore.
1333 * This also assumes that the inode count has been incremented
1334 * (or otherwise set) by the caller to indicate that it is now
1335 * in use by the dcache.
1337 static struct dentry *__d_instantiate_unique(struct dentry *entry,
1338 struct inode *inode)
1340 struct dentry *alias;
1341 int len = entry->d_name.len;
1342 const char *name = entry->d_name.name;
1343 unsigned int hash = entry->d_name.hash;
1346 __d_instantiate(entry, NULL);
1350 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1351 struct qstr *qstr = &alias->d_name;
1353 if (qstr->hash != hash)
1355 if (alias->d_parent != entry->d_parent)
1357 if (qstr->len != len)
1359 if (memcmp(qstr->name, name, len))
1365 __d_instantiate(entry, inode);
1369 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1371 struct dentry *result;
1373 BUG_ON(!list_empty(&entry->d_alias));
1375 spin_lock(&dcache_lock);
1376 spin_lock(&dcache_inode_lock);
1377 result = __d_instantiate_unique(entry, inode);
1378 spin_unlock(&dcache_inode_lock);
1379 spin_unlock(&dcache_lock);
1382 security_d_instantiate(entry, inode);
1386 BUG_ON(!d_unhashed(result));
1391 EXPORT_SYMBOL(d_instantiate_unique);
1394 * d_alloc_root - allocate root dentry
1395 * @root_inode: inode to allocate the root for
1397 * Allocate a root ("/") dentry for the inode given. The inode is
1398 * instantiated and returned. %NULL is returned if there is insufficient
1399 * memory or the inode passed is %NULL.
1402 struct dentry * d_alloc_root(struct inode * root_inode)
1404 struct dentry *res = NULL;
1407 static const struct qstr name = { .name = "/", .len = 1 };
1409 res = d_alloc(NULL, &name);
1411 res->d_sb = root_inode->i_sb;
1412 res->d_parent = res;
1413 d_instantiate(res, root_inode);
1418 EXPORT_SYMBOL(d_alloc_root);
1420 static inline struct hlist_head *d_hash(struct dentry *parent,
1423 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1424 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1425 return dentry_hashtable + (hash & D_HASHMASK);
1429 * d_obtain_alias - find or allocate a dentry for a given inode
1430 * @inode: inode to allocate the dentry for
1432 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1433 * similar open by handle operations. The returned dentry may be anonymous,
1434 * or may have a full name (if the inode was already in the cache).
1436 * When called on a directory inode, we must ensure that the inode only ever
1437 * has one dentry. If a dentry is found, that is returned instead of
1438 * allocating a new one.
1440 * On successful return, the reference to the inode has been transferred
1441 * to the dentry. In case of an error the reference on the inode is released.
1442 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1443 * be passed in and will be the error will be propagate to the return value,
1444 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1446 struct dentry *d_obtain_alias(struct inode *inode)
1448 static const struct qstr anonstring = { .name = "" };
1453 return ERR_PTR(-ESTALE);
1455 return ERR_CAST(inode);
1457 res = d_find_alias(inode);
1461 tmp = d_alloc(NULL, &anonstring);
1463 res = ERR_PTR(-ENOMEM);
1466 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1468 spin_lock(&dcache_lock);
1469 spin_lock(&dcache_inode_lock);
1470 res = __d_find_alias(inode, 0);
1472 spin_unlock(&dcache_inode_lock);
1473 spin_unlock(&dcache_lock);
1478 /* attach a disconnected dentry */
1479 spin_lock(&tmp->d_lock);
1480 tmp->d_sb = inode->i_sb;
1481 tmp->d_inode = inode;
1482 tmp->d_flags |= DCACHE_DISCONNECTED;
1483 tmp->d_flags &= ~DCACHE_UNHASHED;
1484 list_add(&tmp->d_alias, &inode->i_dentry);
1485 spin_lock(&dcache_hash_lock);
1486 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1487 spin_unlock(&dcache_hash_lock);
1488 spin_unlock(&tmp->d_lock);
1489 spin_unlock(&dcache_inode_lock);
1491 spin_unlock(&dcache_lock);
1498 EXPORT_SYMBOL(d_obtain_alias);
1501 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1502 * @inode: the inode which may have a disconnected dentry
1503 * @dentry: a negative dentry which we want to point to the inode.
1505 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1506 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1507 * and return it, else simply d_add the inode to the dentry and return NULL.
1509 * This is needed in the lookup routine of any filesystem that is exportable
1510 * (via knfsd) so that we can build dcache paths to directories effectively.
1512 * If a dentry was found and moved, then it is returned. Otherwise NULL
1513 * is returned. This matches the expected return value of ->lookup.
1516 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1518 struct dentry *new = NULL;
1520 if (inode && S_ISDIR(inode->i_mode)) {
1521 spin_lock(&dcache_lock);
1522 spin_lock(&dcache_inode_lock);
1523 new = __d_find_alias(inode, 1);
1525 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1526 spin_unlock(&dcache_inode_lock);
1527 spin_unlock(&dcache_lock);
1528 security_d_instantiate(new, inode);
1529 d_move(new, dentry);
1532 /* already taking dcache_lock, so d_add() by hand */
1533 __d_instantiate(dentry, inode);
1534 spin_unlock(&dcache_inode_lock);
1535 spin_unlock(&dcache_lock);
1536 security_d_instantiate(dentry, inode);
1540 d_add(dentry, inode);
1543 EXPORT_SYMBOL(d_splice_alias);
1546 * d_add_ci - lookup or allocate new dentry with case-exact name
1547 * @inode: the inode case-insensitive lookup has found
1548 * @dentry: the negative dentry that was passed to the parent's lookup func
1549 * @name: the case-exact name to be associated with the returned dentry
1551 * This is to avoid filling the dcache with case-insensitive names to the
1552 * same inode, only the actual correct case is stored in the dcache for
1553 * case-insensitive filesystems.
1555 * For a case-insensitive lookup match and if the the case-exact dentry
1556 * already exists in in the dcache, use it and return it.
1558 * If no entry exists with the exact case name, allocate new dentry with
1559 * the exact case, and return the spliced entry.
1561 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
1565 struct dentry *found;
1569 * First check if a dentry matching the name already exists,
1570 * if not go ahead and create it now.
1572 found = d_hash_and_lookup(dentry->d_parent, name);
1574 new = d_alloc(dentry->d_parent, name);
1580 found = d_splice_alias(inode, new);
1589 * If a matching dentry exists, and it's not negative use it.
1591 * Decrement the reference count to balance the iget() done
1594 if (found->d_inode) {
1595 if (unlikely(found->d_inode != inode)) {
1596 /* This can't happen because bad inodes are unhashed. */
1597 BUG_ON(!is_bad_inode(inode));
1598 BUG_ON(!is_bad_inode(found->d_inode));
1605 * Negative dentry: instantiate it unless the inode is a directory and
1606 * already has a dentry.
1608 spin_lock(&dcache_lock);
1609 spin_lock(&dcache_inode_lock);
1610 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
1611 __d_instantiate(found, inode);
1612 spin_unlock(&dcache_inode_lock);
1613 spin_unlock(&dcache_lock);
1614 security_d_instantiate(found, inode);
1619 * In case a directory already has a (disconnected) entry grab a
1620 * reference to it, move it in place and use it.
1622 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1624 spin_unlock(&dcache_inode_lock);
1625 spin_unlock(&dcache_lock);
1626 security_d_instantiate(found, inode);
1634 return ERR_PTR(error);
1636 EXPORT_SYMBOL(d_add_ci);
1639 * d_lookup - search for a dentry
1640 * @parent: parent dentry
1641 * @name: qstr of name we wish to find
1642 * Returns: dentry, or NULL
1644 * d_lookup searches the children of the parent dentry for the name in
1645 * question. If the dentry is found its reference count is incremented and the
1646 * dentry is returned. The caller must use dput to free the entry when it has
1647 * finished using it. %NULL is returned if the dentry does not exist.
1649 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1651 struct dentry * dentry = NULL;
1655 seq = read_seqbegin(&rename_lock);
1656 dentry = __d_lookup(parent, name);
1659 } while (read_seqretry(&rename_lock, seq));
1662 EXPORT_SYMBOL(d_lookup);
1665 * __d_lookup - search for a dentry (racy)
1666 * @parent: parent dentry
1667 * @name: qstr of name we wish to find
1668 * Returns: dentry, or NULL
1670 * __d_lookup is like d_lookup, however it may (rarely) return a
1671 * false-negative result due to unrelated rename activity.
1673 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1674 * however it must be used carefully, eg. with a following d_lookup in
1675 * the case of failure.
1677 * __d_lookup callers must be commented.
1679 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1681 unsigned int len = name->len;
1682 unsigned int hash = name->hash;
1683 const unsigned char *str = name->name;
1684 struct hlist_head *head = d_hash(parent,hash);
1685 struct dentry *found = NULL;
1686 struct hlist_node *node;
1687 struct dentry *dentry;
1690 * The hash list is protected using RCU.
1692 * Take d_lock when comparing a candidate dentry, to avoid races
1695 * It is possible that concurrent renames can mess up our list
1696 * walk here and result in missing our dentry, resulting in the
1697 * false-negative result. d_lookup() protects against concurrent
1698 * renames using rename_lock seqlock.
1700 * See Documentation/vfs/dcache-locking.txt for more details.
1704 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1707 if (dentry->d_name.hash != hash)
1709 if (dentry->d_parent != parent)
1712 spin_lock(&dentry->d_lock);
1715 * Recheck the dentry after taking the lock - d_move may have
1716 * changed things. Don't bother checking the hash because
1717 * we're about to compare the whole name anyway.
1719 if (dentry->d_parent != parent)
1722 /* non-existing due to RCU? */
1723 if (d_unhashed(dentry))
1727 * It is safe to compare names since d_move() cannot
1728 * change the qstr (protected by d_lock).
1730 qstr = &dentry->d_name;
1731 if (parent->d_op && parent->d_op->d_compare) {
1732 if (parent->d_op->d_compare(parent, parent->d_inode,
1733 dentry, dentry->d_inode,
1734 qstr->len, qstr->name, name))
1737 if (qstr->len != len)
1739 if (memcmp(qstr->name, str, len))
1745 spin_unlock(&dentry->d_lock);
1748 spin_unlock(&dentry->d_lock);
1756 * d_hash_and_lookup - hash the qstr then search for a dentry
1757 * @dir: Directory to search in
1758 * @name: qstr of name we wish to find
1760 * On hash failure or on lookup failure NULL is returned.
1762 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1764 struct dentry *dentry = NULL;
1767 * Check for a fs-specific hash function. Note that we must
1768 * calculate the standard hash first, as the d_op->d_hash()
1769 * routine may choose to leave the hash value unchanged.
1771 name->hash = full_name_hash(name->name, name->len);
1772 if (dir->d_op && dir->d_op->d_hash) {
1773 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
1776 dentry = d_lookup(dir, name);
1782 * d_validate - verify dentry provided from insecure source (deprecated)
1783 * @dentry: The dentry alleged to be valid child of @dparent
1784 * @dparent: The parent dentry (known to be valid)
1786 * An insecure source has sent us a dentry, here we verify it and dget() it.
1787 * This is used by ncpfs in its readdir implementation.
1788 * Zero is returned in the dentry is invalid.
1790 * This function is slow for big directories, and deprecated, do not use it.
1792 int d_validate(struct dentry *dentry, struct dentry *dparent)
1794 struct dentry *child;
1796 spin_lock(&dcache_lock);
1797 spin_lock(&dparent->d_lock);
1798 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1799 if (dentry == child) {
1800 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1801 __dget_locked_dlock(dentry);
1802 spin_unlock(&dentry->d_lock);
1803 spin_unlock(&dparent->d_lock);
1804 spin_unlock(&dcache_lock);
1808 spin_unlock(&dparent->d_lock);
1809 spin_unlock(&dcache_lock);
1813 EXPORT_SYMBOL(d_validate);
1816 * When a file is deleted, we have two options:
1817 * - turn this dentry into a negative dentry
1818 * - unhash this dentry and free it.
1820 * Usually, we want to just turn this into
1821 * a negative dentry, but if anybody else is
1822 * currently using the dentry or the inode
1823 * we can't do that and we fall back on removing
1824 * it from the hash queues and waiting for
1825 * it to be deleted later when it has no users
1829 * d_delete - delete a dentry
1830 * @dentry: The dentry to delete
1832 * Turn the dentry into a negative dentry if possible, otherwise
1833 * remove it from the hash queues so it can be deleted later
1836 void d_delete(struct dentry * dentry)
1840 * Are we the only user?
1842 spin_lock(&dcache_lock);
1843 spin_lock(&dcache_inode_lock);
1844 spin_lock(&dentry->d_lock);
1845 isdir = S_ISDIR(dentry->d_inode->i_mode);
1846 if (dentry->d_count == 1) {
1847 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
1848 dentry_iput(dentry);
1849 fsnotify_nameremove(dentry, isdir);
1853 if (!d_unhashed(dentry))
1856 spin_unlock(&dentry->d_lock);
1857 spin_unlock(&dcache_inode_lock);
1858 spin_unlock(&dcache_lock);
1860 fsnotify_nameremove(dentry, isdir);
1862 EXPORT_SYMBOL(d_delete);
1864 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1867 entry->d_flags &= ~DCACHE_UNHASHED;
1868 hlist_add_head_rcu(&entry->d_hash, list);
1871 static void _d_rehash(struct dentry * entry)
1873 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1877 * d_rehash - add an entry back to the hash
1878 * @entry: dentry to add to the hash
1880 * Adds a dentry to the hash according to its name.
1883 void d_rehash(struct dentry * entry)
1885 spin_lock(&dcache_lock);
1886 spin_lock(&entry->d_lock);
1887 spin_lock(&dcache_hash_lock);
1889 spin_unlock(&dcache_hash_lock);
1890 spin_unlock(&entry->d_lock);
1891 spin_unlock(&dcache_lock);
1893 EXPORT_SYMBOL(d_rehash);
1896 * dentry_update_name_case - update case insensitive dentry with a new name
1897 * @dentry: dentry to be updated
1900 * Update a case insensitive dentry with new case of name.
1902 * dentry must have been returned by d_lookup with name @name. Old and new
1903 * name lengths must match (ie. no d_compare which allows mismatched name
1906 * Parent inode i_mutex must be held over d_lookup and into this call (to
1907 * keep renames and concurrent inserts, and readdir(2) away).
1909 void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
1911 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
1912 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
1914 spin_lock(&dcache_lock);
1915 spin_lock(&dentry->d_lock);
1916 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
1917 spin_unlock(&dentry->d_lock);
1918 spin_unlock(&dcache_lock);
1920 EXPORT_SYMBOL(dentry_update_name_case);
1922 static void switch_names(struct dentry *dentry, struct dentry *target)
1924 if (dname_external(target)) {
1925 if (dname_external(dentry)) {
1927 * Both external: swap the pointers
1929 swap(target->d_name.name, dentry->d_name.name);
1932 * dentry:internal, target:external. Steal target's
1933 * storage and make target internal.
1935 memcpy(target->d_iname, dentry->d_name.name,
1936 dentry->d_name.len + 1);
1937 dentry->d_name.name = target->d_name.name;
1938 target->d_name.name = target->d_iname;
1941 if (dname_external(dentry)) {
1943 * dentry:external, target:internal. Give dentry's
1944 * storage to target and make dentry internal
1946 memcpy(dentry->d_iname, target->d_name.name,
1947 target->d_name.len + 1);
1948 target->d_name.name = dentry->d_name.name;
1949 dentry->d_name.name = dentry->d_iname;
1952 * Both are internal. Just copy target to dentry
1954 memcpy(dentry->d_iname, target->d_name.name,
1955 target->d_name.len + 1);
1956 dentry->d_name.len = target->d_name.len;
1960 swap(dentry->d_name.len, target->d_name.len);
1963 static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
1966 * XXXX: do we really need to take target->d_lock?
1968 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
1969 spin_lock(&target->d_parent->d_lock);
1971 if (d_ancestor(dentry->d_parent, target->d_parent)) {
1972 spin_lock(&dentry->d_parent->d_lock);
1973 spin_lock_nested(&target->d_parent->d_lock,
1974 DENTRY_D_LOCK_NESTED);
1976 spin_lock(&target->d_parent->d_lock);
1977 spin_lock_nested(&dentry->d_parent->d_lock,
1978 DENTRY_D_LOCK_NESTED);
1981 if (target < dentry) {
1982 spin_lock_nested(&target->d_lock, 2);
1983 spin_lock_nested(&dentry->d_lock, 3);
1985 spin_lock_nested(&dentry->d_lock, 2);
1986 spin_lock_nested(&target->d_lock, 3);
1990 static void dentry_unlock_parents_for_move(struct dentry *dentry,
1991 struct dentry *target)
1993 if (target->d_parent != dentry->d_parent)
1994 spin_unlock(&dentry->d_parent->d_lock);
1995 if (target->d_parent != target)
1996 spin_unlock(&target->d_parent->d_lock);
2000 * When switching names, the actual string doesn't strictly have to
2001 * be preserved in the target - because we're dropping the target
2002 * anyway. As such, we can just do a simple memcpy() to copy over
2003 * the new name before we switch.
2005 * Note that we have to be a lot more careful about getting the hash
2006 * switched - we have to switch the hash value properly even if it
2007 * then no longer matches the actual (corrupted) string of the target.
2008 * The hash value has to match the hash queue that the dentry is on..
2011 * d_move_locked - move a dentry
2012 * @dentry: entry to move
2013 * @target: new dentry
2015 * Update the dcache to reflect the move of a file name. Negative
2016 * dcache entries should not be moved in this way.
2018 static void d_move_locked(struct dentry * dentry, struct dentry * target)
2020 if (!dentry->d_inode)
2021 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2023 BUG_ON(d_ancestor(dentry, target));
2024 BUG_ON(d_ancestor(target, dentry));
2026 write_seqlock(&rename_lock);
2028 dentry_lock_for_move(dentry, target);
2030 /* Move the dentry to the target hash queue, if on different bucket */
2031 spin_lock(&dcache_hash_lock);
2032 if (!d_unhashed(dentry))
2033 hlist_del_rcu(&dentry->d_hash);
2034 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
2035 spin_unlock(&dcache_hash_lock);
2037 /* Unhash the target: dput() will then get rid of it */
2040 list_del(&dentry->d_u.d_child);
2041 list_del(&target->d_u.d_child);
2043 /* Switch the names.. */
2044 switch_names(dentry, target);
2045 swap(dentry->d_name.hash, target->d_name.hash);
2047 /* ... and switch the parents */
2048 if (IS_ROOT(dentry)) {
2049 dentry->d_parent = target->d_parent;
2050 target->d_parent = target;
2051 INIT_LIST_HEAD(&target->d_u.d_child);
2053 swap(dentry->d_parent, target->d_parent);
2055 /* And add them back to the (new) parent lists */
2056 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
2059 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2061 dentry_unlock_parents_for_move(dentry, target);
2062 spin_unlock(&target->d_lock);
2063 fsnotify_d_move(dentry);
2064 spin_unlock(&dentry->d_lock);
2065 write_sequnlock(&rename_lock);
2069 * d_move - move a dentry
2070 * @dentry: entry to move
2071 * @target: new dentry
2073 * Update the dcache to reflect the move of a file name. Negative
2074 * dcache entries should not be moved in this way.
2077 void d_move(struct dentry * dentry, struct dentry * target)
2079 spin_lock(&dcache_lock);
2080 d_move_locked(dentry, target);
2081 spin_unlock(&dcache_lock);
2083 EXPORT_SYMBOL(d_move);
2086 * d_ancestor - search for an ancestor
2087 * @p1: ancestor dentry
2090 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2091 * an ancestor of p2, else NULL.
2093 struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
2097 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
2098 if (p->d_parent == p1)
2105 * This helper attempts to cope with remotely renamed directories
2107 * It assumes that the caller is already holding
2108 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
2110 * Note: If ever the locking in lock_rename() changes, then please
2111 * remember to update this too...
2113 static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
2114 __releases(dcache_lock)
2115 __releases(dcache_inode_lock)
2117 struct mutex *m1 = NULL, *m2 = NULL;
2120 /* If alias and dentry share a parent, then no extra locks required */
2121 if (alias->d_parent == dentry->d_parent)
2124 /* Check for loops */
2125 ret = ERR_PTR(-ELOOP);
2126 if (d_ancestor(alias, dentry))
2129 /* See lock_rename() */
2130 ret = ERR_PTR(-EBUSY);
2131 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2133 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2134 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2136 m2 = &alias->d_parent->d_inode->i_mutex;
2138 d_move_locked(alias, dentry);
2141 spin_unlock(&dcache_inode_lock);
2142 spin_unlock(&dcache_lock);
2151 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2152 * named dentry in place of the dentry to be replaced.
2153 * returns with anon->d_lock held!
2155 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2157 struct dentry *dparent, *aparent;
2159 dentry_lock_for_move(anon, dentry);
2161 dparent = dentry->d_parent;
2162 aparent = anon->d_parent;
2164 switch_names(dentry, anon);
2165 swap(dentry->d_name.hash, anon->d_name.hash);
2167 dentry->d_parent = (aparent == anon) ? dentry : aparent;
2168 list_del(&dentry->d_u.d_child);
2169 if (!IS_ROOT(dentry))
2170 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2172 INIT_LIST_HEAD(&dentry->d_u.d_child);
2174 anon->d_parent = (dparent == dentry) ? anon : dparent;
2175 list_del(&anon->d_u.d_child);
2177 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
2179 INIT_LIST_HEAD(&anon->d_u.d_child);
2181 dentry_unlock_parents_for_move(anon, dentry);
2182 spin_unlock(&dentry->d_lock);
2184 /* anon->d_lock still locked, returns locked */
2185 anon->d_flags &= ~DCACHE_DISCONNECTED;
2189 * d_materialise_unique - introduce an inode into the tree
2190 * @dentry: candidate dentry
2191 * @inode: inode to bind to the dentry, to which aliases may be attached
2193 * Introduces an dentry into the tree, substituting an extant disconnected
2194 * root directory alias in its place if there is one
2196 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2198 struct dentry *actual;
2200 BUG_ON(!d_unhashed(dentry));
2202 spin_lock(&dcache_lock);
2203 spin_lock(&dcache_inode_lock);
2207 __d_instantiate(dentry, NULL);
2211 if (S_ISDIR(inode->i_mode)) {
2212 struct dentry *alias;
2214 /* Does an aliased dentry already exist? */
2215 alias = __d_find_alias(inode, 0);
2218 /* Is this an anonymous mountpoint that we could splice
2220 if (IS_ROOT(alias)) {
2221 __d_materialise_dentry(dentry, alias);
2225 /* Nope, but we must(!) avoid directory aliasing */
2226 actual = __d_unalias(dentry, alias);
2233 /* Add a unique reference */
2234 actual = __d_instantiate_unique(dentry, inode);
2237 else if (unlikely(!d_unhashed(actual)))
2238 goto shouldnt_be_hashed;
2241 spin_lock(&actual->d_lock);
2243 spin_lock(&dcache_hash_lock);
2245 spin_unlock(&dcache_hash_lock);
2246 spin_unlock(&actual->d_lock);
2247 spin_unlock(&dcache_inode_lock);
2248 spin_unlock(&dcache_lock);
2250 if (actual == dentry) {
2251 security_d_instantiate(dentry, inode);
2259 spin_unlock(&dcache_inode_lock);
2260 spin_unlock(&dcache_lock);
2263 EXPORT_SYMBOL_GPL(d_materialise_unique);
2265 static int prepend(char **buffer, int *buflen, const char *str, int namelen)
2269 return -ENAMETOOLONG;
2271 memcpy(*buffer, str, namelen);
2275 static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2277 return prepend(buffer, buflen, name->name, name->len);
2281 * Prepend path string to a buffer
2283 * @path: the dentry/vfsmount to report
2284 * @root: root vfsmnt/dentry (may be modified by this function)
2285 * @buffer: pointer to the end of the buffer
2286 * @buflen: pointer to buffer length
2288 * Caller holds the dcache_lock.
2290 * If path is not reachable from the supplied root, then the value of
2291 * root is changed (without modifying refcounts).
2293 static int prepend_path(const struct path *path, struct path *root,
2294 char **buffer, int *buflen)
2296 struct dentry *dentry = path->dentry;
2297 struct vfsmount *vfsmnt = path->mnt;
2301 br_read_lock(vfsmount_lock);
2302 while (dentry != root->dentry || vfsmnt != root->mnt) {
2303 struct dentry * parent;
2305 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2307 if (vfsmnt->mnt_parent == vfsmnt) {
2310 dentry = vfsmnt->mnt_mountpoint;
2311 vfsmnt = vfsmnt->mnt_parent;
2314 parent = dentry->d_parent;
2316 error = prepend_name(buffer, buflen, &dentry->d_name);
2318 error = prepend(buffer, buflen, "/", 1);
2327 if (!error && !slash)
2328 error = prepend(buffer, buflen, "/", 1);
2330 br_read_unlock(vfsmount_lock);
2335 * Filesystems needing to implement special "root names"
2336 * should do so with ->d_dname()
2338 if (IS_ROOT(dentry) &&
2339 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2340 WARN(1, "Root dentry has weird name <%.*s>\n",
2341 (int) dentry->d_name.len, dentry->d_name.name);
2344 root->dentry = dentry;
2349 * __d_path - return the path of a dentry
2350 * @path: the dentry/vfsmount to report
2351 * @root: root vfsmnt/dentry (may be modified by this function)
2352 * @buf: buffer to return value in
2353 * @buflen: buffer length
2355 * Convert a dentry into an ASCII path name.
2357 * Returns a pointer into the buffer or an error code if the
2358 * path was too long.
2360 * "buflen" should be positive.
2362 * If path is not reachable from the supplied root, then the value of
2363 * root is changed (without modifying refcounts).
2365 char *__d_path(const struct path *path, struct path *root,
2366 char *buf, int buflen)
2368 char *res = buf + buflen;
2371 prepend(&res, &buflen, "\0", 1);
2372 spin_lock(&dcache_lock);
2373 error = prepend_path(path, root, &res, &buflen);
2374 spin_unlock(&dcache_lock);
2377 return ERR_PTR(error);
2382 * same as __d_path but appends "(deleted)" for unlinked files.
2384 static int path_with_deleted(const struct path *path, struct path *root,
2385 char **buf, int *buflen)
2387 prepend(buf, buflen, "\0", 1);
2388 if (d_unlinked(path->dentry)) {
2389 int error = prepend(buf, buflen, " (deleted)", 10);
2394 return prepend_path(path, root, buf, buflen);
2397 static int prepend_unreachable(char **buffer, int *buflen)
2399 return prepend(buffer, buflen, "(unreachable)", 13);
2403 * d_path - return the path of a dentry
2404 * @path: path to report
2405 * @buf: buffer to return value in
2406 * @buflen: buffer length
2408 * Convert a dentry into an ASCII path name. If the entry has been deleted
2409 * the string " (deleted)" is appended. Note that this is ambiguous.
2411 * Returns a pointer into the buffer or an error code if the path was
2412 * too long. Note: Callers should use the returned pointer, not the passed
2413 * in buffer, to use the name! The implementation often starts at an offset
2414 * into the buffer, and may leave 0 bytes at the start.
2416 * "buflen" should be positive.
2418 char *d_path(const struct path *path, char *buf, int buflen)
2420 char *res = buf + buflen;
2426 * We have various synthetic filesystems that never get mounted. On
2427 * these filesystems dentries are never used for lookup purposes, and
2428 * thus don't need to be hashed. They also don't need a name until a
2429 * user wants to identify the object in /proc/pid/fd/. The little hack
2430 * below allows us to generate a name for these objects on demand:
2432 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2433 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2435 get_fs_root(current->fs, &root);
2436 spin_lock(&dcache_lock);
2438 error = path_with_deleted(path, &tmp, &res, &buflen);
2440 res = ERR_PTR(error);
2441 spin_unlock(&dcache_lock);
2445 EXPORT_SYMBOL(d_path);
2448 * d_path_with_unreachable - return the path of a dentry
2449 * @path: path to report
2450 * @buf: buffer to return value in
2451 * @buflen: buffer length
2453 * The difference from d_path() is that this prepends "(unreachable)"
2454 * to paths which are unreachable from the current process' root.
2456 char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2458 char *res = buf + buflen;
2463 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2464 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2466 get_fs_root(current->fs, &root);
2467 spin_lock(&dcache_lock);
2469 error = path_with_deleted(path, &tmp, &res, &buflen);
2470 if (!error && !path_equal(&tmp, &root))
2471 error = prepend_unreachable(&res, &buflen);
2472 spin_unlock(&dcache_lock);
2475 res = ERR_PTR(error);
2481 * Helper function for dentry_operations.d_dname() members
2483 char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2484 const char *fmt, ...)
2490 va_start(args, fmt);
2491 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2494 if (sz > sizeof(temp) || sz > buflen)
2495 return ERR_PTR(-ENAMETOOLONG);
2497 buffer += buflen - sz;
2498 return memcpy(buffer, temp, sz);
2502 * Write full pathname from the root of the filesystem into the buffer.
2504 static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
2506 char *end = buf + buflen;
2509 prepend(&end, &buflen, "\0", 1);
2516 while (!IS_ROOT(dentry)) {
2517 struct dentry *parent = dentry->d_parent;
2520 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
2521 (prepend(&end, &buflen, "/", 1) != 0))
2529 return ERR_PTR(-ENAMETOOLONG);
2532 char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2536 spin_lock(&dcache_lock);
2537 retval = __dentry_path(dentry, buf, buflen);
2538 spin_unlock(&dcache_lock);
2542 EXPORT_SYMBOL(dentry_path_raw);
2544 char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2549 spin_lock(&dcache_lock);
2550 if (d_unlinked(dentry)) {
2552 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2556 retval = __dentry_path(dentry, buf, buflen);
2557 spin_unlock(&dcache_lock);
2558 if (!IS_ERR(retval) && p)
2559 *p = '/'; /* restore '/' overriden with '\0' */
2562 spin_unlock(&dcache_lock);
2563 return ERR_PTR(-ENAMETOOLONG);
2567 * NOTE! The user-level library version returns a
2568 * character pointer. The kernel system call just
2569 * returns the length of the buffer filled (which
2570 * includes the ending '\0' character), or a negative
2571 * error value. So libc would do something like
2573 * char *getcwd(char * buf, size_t size)
2577 * retval = sys_getcwd(buf, size);
2584 SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
2587 struct path pwd, root;
2588 char *page = (char *) __get_free_page(GFP_USER);
2593 get_fs_root_and_pwd(current->fs, &root, &pwd);
2596 spin_lock(&dcache_lock);
2597 if (!d_unlinked(pwd.dentry)) {
2599 struct path tmp = root;
2600 char *cwd = page + PAGE_SIZE;
2601 int buflen = PAGE_SIZE;
2603 prepend(&cwd, &buflen, "\0", 1);
2604 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
2605 spin_unlock(&dcache_lock);
2610 /* Unreachable from current root */
2611 if (!path_equal(&tmp, &root)) {
2612 error = prepend_unreachable(&cwd, &buflen);
2618 len = PAGE_SIZE + page - cwd;
2621 if (copy_to_user(buf, cwd, len))
2625 spin_unlock(&dcache_lock);
2630 free_page((unsigned long) page);
2635 * Test whether new_dentry is a subdirectory of old_dentry.
2637 * Trivially implemented using the dcache structure
2641 * is_subdir - is new dentry a subdirectory of old_dentry
2642 * @new_dentry: new dentry
2643 * @old_dentry: old dentry
2645 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2646 * Returns 0 otherwise.
2647 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2650 int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
2655 if (new_dentry == old_dentry)
2659 * Need rcu_readlock to protect against the d_parent trashing
2664 /* for restarting inner loop in case of seq retry */
2665 seq = read_seqbegin(&rename_lock);
2666 if (d_ancestor(old_dentry, new_dentry))
2670 } while (read_seqretry(&rename_lock, seq));
2676 int path_is_under(struct path *path1, struct path *path2)
2678 struct vfsmount *mnt = path1->mnt;
2679 struct dentry *dentry = path1->dentry;
2682 br_read_lock(vfsmount_lock);
2683 if (mnt != path2->mnt) {
2685 if (mnt->mnt_parent == mnt) {
2686 br_read_unlock(vfsmount_lock);
2689 if (mnt->mnt_parent == path2->mnt)
2691 mnt = mnt->mnt_parent;
2693 dentry = mnt->mnt_mountpoint;
2695 res = is_subdir(dentry, path2->dentry);
2696 br_read_unlock(vfsmount_lock);
2699 EXPORT_SYMBOL(path_is_under);
2701 void d_genocide(struct dentry *root)
2703 struct dentry *this_parent = root;
2704 struct list_head *next;
2706 spin_lock(&dcache_lock);
2707 spin_lock(&this_parent->d_lock);
2709 next = this_parent->d_subdirs.next;
2711 while (next != &this_parent->d_subdirs) {
2712 struct list_head *tmp = next;
2713 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
2715 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2716 if (d_unhashed(dentry) || !dentry->d_inode) {
2717 spin_unlock(&dentry->d_lock);
2720 if (!list_empty(&dentry->d_subdirs)) {
2721 spin_unlock(&this_parent->d_lock);
2722 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
2723 this_parent = dentry;
2724 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
2728 spin_unlock(&dentry->d_lock);
2730 if (this_parent != root) {
2731 next = this_parent->d_u.d_child.next;
2732 this_parent->d_count--;
2733 spin_unlock(&this_parent->d_lock);
2734 this_parent = this_parent->d_parent;
2735 spin_lock(&this_parent->d_lock);
2738 spin_unlock(&this_parent->d_lock);
2739 spin_unlock(&dcache_lock);
2743 * find_inode_number - check for dentry with name
2744 * @dir: directory to check
2745 * @name: Name to find.
2747 * Check whether a dentry already exists for the given name,
2748 * and return the inode number if it has an inode. Otherwise
2751 * This routine is used to post-process directory listings for
2752 * filesystems using synthetic inode numbers, and is necessary
2753 * to keep getcwd() working.
2756 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2758 struct dentry * dentry;
2761 dentry = d_hash_and_lookup(dir, name);
2763 if (dentry->d_inode)
2764 ino = dentry->d_inode->i_ino;
2769 EXPORT_SYMBOL(find_inode_number);
2771 static __initdata unsigned long dhash_entries;
2772 static int __init set_dhash_entries(char *str)
2776 dhash_entries = simple_strtoul(str, &str, 0);
2779 __setup("dhash_entries=", set_dhash_entries);
2781 static void __init dcache_init_early(void)
2785 /* If hashes are distributed across NUMA nodes, defer
2786 * hash allocation until vmalloc space is available.
2792 alloc_large_system_hash("Dentry cache",
2793 sizeof(struct hlist_head),
2801 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2802 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2805 static void __init dcache_init(void)
2810 * A constructor could be added for stable state like the lists,
2811 * but it is probably not worth it because of the cache nature
2814 dentry_cache = KMEM_CACHE(dentry,
2815 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
2817 register_shrinker(&dcache_shrinker);
2819 /* Hash may have been set up in dcache_init_early */
2824 alloc_large_system_hash("Dentry cache",
2825 sizeof(struct hlist_head),
2833 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2834 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2837 /* SLAB cache for __getname() consumers */
2838 struct kmem_cache *names_cachep __read_mostly;
2839 EXPORT_SYMBOL(names_cachep);
2841 EXPORT_SYMBOL(d_genocide);
2843 void __init vfs_caches_init_early(void)
2845 dcache_init_early();
2849 void __init vfs_caches_init(unsigned long mempages)
2851 unsigned long reserve;
2853 /* Base hash sizes on available memory, with a reserve equal to
2854 150% of current kernel size */
2856 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2857 mempages -= reserve;
2859 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2860 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2864 files_init(mempages);