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>
38 int sysctl_vfs_cache_pressure __read_mostly = 100;
39 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
41 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
42 __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
44 EXPORT_SYMBOL(dcache_lock);
46 static struct kmem_cache *dentry_cache __read_mostly;
48 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
51 * This is the single most critical data structure when it comes
52 * to the dcache: the hashtable for lookups. Somebody should try
53 * to make this good - I've just made it work.
55 * This hash-function tries to avoid losing too many bits of hash
56 * information, yet avoid using a prime hash-size or similar.
58 #define D_HASHBITS d_hash_shift
59 #define D_HASHMASK d_hash_mask
61 static unsigned int d_hash_mask __read_mostly;
62 static unsigned int d_hash_shift __read_mostly;
63 static struct hlist_head *dentry_hashtable __read_mostly;
65 /* Statistics gathering. */
66 struct dentry_stat_t dentry_stat = {
70 static struct percpu_counter nr_dentry __cacheline_aligned_in_smp;
71 static struct percpu_counter nr_dentry_unused __cacheline_aligned_in_smp;
73 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
74 int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
75 size_t *lenp, loff_t *ppos)
77 dentry_stat.nr_dentry = percpu_counter_sum_positive(&nr_dentry);
78 dentry_stat.nr_unused = percpu_counter_sum_positive(&nr_dentry_unused);
79 return proc_dointvec(table, write, buffer, lenp, ppos);
83 static void __d_free(struct rcu_head *head)
85 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
87 WARN_ON(!list_empty(&dentry->d_alias));
88 if (dname_external(dentry))
89 kfree(dentry->d_name.name);
90 kmem_cache_free(dentry_cache, dentry);
94 * no dcache_lock, please.
96 static void d_free(struct dentry *dentry)
98 percpu_counter_dec(&nr_dentry);
99 if (dentry->d_op && dentry->d_op->d_release)
100 dentry->d_op->d_release(dentry);
102 /* if dentry was never inserted into hash, immediate free is OK */
103 if (hlist_unhashed(&dentry->d_hash))
104 __d_free(&dentry->d_u.d_rcu);
106 call_rcu(&dentry->d_u.d_rcu, __d_free);
110 * Release the dentry's inode, using the filesystem
111 * d_iput() operation if defined.
113 static void dentry_iput(struct dentry * dentry)
114 __releases(dentry->d_lock)
115 __releases(dcache_lock)
117 struct inode *inode = dentry->d_inode;
119 dentry->d_inode = NULL;
120 list_del_init(&dentry->d_alias);
121 spin_unlock(&dentry->d_lock);
122 spin_unlock(&dcache_lock);
124 fsnotify_inoderemove(inode);
125 if (dentry->d_op && dentry->d_op->d_iput)
126 dentry->d_op->d_iput(dentry, inode);
130 spin_unlock(&dentry->d_lock);
131 spin_unlock(&dcache_lock);
136 * dentry_lru_(add|add_tail|del|del_init) must be called with dcache_lock held.
138 static void dentry_lru_add(struct dentry *dentry)
140 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
141 dentry->d_sb->s_nr_dentry_unused++;
142 percpu_counter_inc(&nr_dentry_unused);
145 static void dentry_lru_add_tail(struct dentry *dentry)
147 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
148 dentry->d_sb->s_nr_dentry_unused++;
149 percpu_counter_inc(&nr_dentry_unused);
152 static void dentry_lru_del(struct dentry *dentry)
154 if (!list_empty(&dentry->d_lru)) {
155 list_del(&dentry->d_lru);
156 dentry->d_sb->s_nr_dentry_unused--;
157 percpu_counter_dec(&nr_dentry_unused);
161 static void dentry_lru_del_init(struct dentry *dentry)
163 if (likely(!list_empty(&dentry->d_lru))) {
164 list_del_init(&dentry->d_lru);
165 dentry->d_sb->s_nr_dentry_unused--;
166 percpu_counter_dec(&nr_dentry_unused);
171 * d_kill - kill dentry and return parent
172 * @dentry: dentry to kill
174 * The dentry must already be unhashed and removed from the LRU.
176 * If this is the root of the dentry tree, return NULL.
178 static struct dentry *d_kill(struct dentry *dentry)
179 __releases(dentry->d_lock)
180 __releases(dcache_lock)
182 struct dentry *parent;
184 list_del(&dentry->d_u.d_child);
185 /*drops the locks, at that point nobody can reach this dentry */
190 parent = dentry->d_parent;
198 * This is complicated by the fact that we do not want to put
199 * dentries that are no longer on any hash chain on the unused
200 * list: we'd much rather just get rid of them immediately.
202 * However, that implies that we have to traverse the dentry
203 * tree upwards to the parents which might _also_ now be
204 * scheduled for deletion (it may have been only waiting for
205 * its last child to go away).
207 * This tail recursion is done by hand as we don't want to depend
208 * on the compiler to always get this right (gcc generally doesn't).
209 * Real recursion would eat up our stack space.
213 * dput - release a dentry
214 * @dentry: dentry to release
216 * Release a dentry. This will drop the usage count and if appropriate
217 * call the dentry unlink method as well as removing it from the queues and
218 * releasing its resources. If the parent dentries were scheduled for release
219 * they too may now get deleted.
221 * no dcache lock, please.
224 void dput(struct dentry *dentry)
230 if (atomic_read(&dentry->d_count) == 1)
232 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
235 spin_lock(&dentry->d_lock);
236 if (atomic_read(&dentry->d_count)) {
237 spin_unlock(&dentry->d_lock);
238 spin_unlock(&dcache_lock);
243 * AV: ->d_delete() is _NOT_ allowed to block now.
245 if (dentry->d_op && dentry->d_op->d_delete) {
246 if (dentry->d_op->d_delete(dentry))
250 /* Unreachable? Get rid of it */
251 if (d_unhashed(dentry))
254 /* Otherwise leave it cached and ensure it's on the LRU */
255 dentry->d_flags |= DCACHE_REFERENCED;
256 if (list_empty(&dentry->d_lru))
257 dentry_lru_add(dentry);
259 spin_unlock(&dentry->d_lock);
260 spin_unlock(&dcache_lock);
266 /* if dentry was on the d_lru list delete it from there */
267 dentry_lru_del(dentry);
268 dentry = d_kill(dentry);
275 * d_invalidate - invalidate a dentry
276 * @dentry: dentry to invalidate
278 * Try to invalidate the dentry if it turns out to be
279 * possible. If there are other dentries that can be
280 * reached through this one we can't delete it and we
281 * return -EBUSY. On success we return 0.
286 int d_invalidate(struct dentry * dentry)
289 * If it's already been dropped, return OK.
291 spin_lock(&dcache_lock);
292 if (d_unhashed(dentry)) {
293 spin_unlock(&dcache_lock);
297 * Check whether to do a partial shrink_dcache
298 * to get rid of unused child entries.
300 if (!list_empty(&dentry->d_subdirs)) {
301 spin_unlock(&dcache_lock);
302 shrink_dcache_parent(dentry);
303 spin_lock(&dcache_lock);
307 * Somebody else still using it?
309 * If it's a directory, we can't drop it
310 * for fear of somebody re-populating it
311 * with children (even though dropping it
312 * would make it unreachable from the root,
313 * we might still populate it if it was a
314 * working directory or similar).
316 spin_lock(&dentry->d_lock);
317 if (atomic_read(&dentry->d_count) > 1) {
318 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
319 spin_unlock(&dentry->d_lock);
320 spin_unlock(&dcache_lock);
326 spin_unlock(&dentry->d_lock);
327 spin_unlock(&dcache_lock);
330 EXPORT_SYMBOL(d_invalidate);
332 /* This should be called _only_ with dcache_lock held */
333 static inline struct dentry * __dget_locked(struct dentry *dentry)
335 atomic_inc(&dentry->d_count);
336 dentry_lru_del_init(dentry);
340 struct dentry * dget_locked(struct dentry *dentry)
342 return __dget_locked(dentry);
344 EXPORT_SYMBOL(dget_locked);
347 * d_find_alias - grab a hashed alias of inode
348 * @inode: inode in question
349 * @want_discon: flag, used by d_splice_alias, to request
350 * that only a DISCONNECTED alias be returned.
352 * If inode has a hashed alias, or is a directory and has any alias,
353 * acquire the reference to alias and return it. Otherwise return NULL.
354 * Notice that if inode is a directory there can be only one alias and
355 * it can be unhashed only if it has no children, or if it is the root
358 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
359 * any other hashed alias over that one unless @want_discon is set,
360 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
363 static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
365 struct list_head *head, *next, *tmp;
366 struct dentry *alias, *discon_alias=NULL;
368 head = &inode->i_dentry;
369 next = inode->i_dentry.next;
370 while (next != head) {
374 alias = list_entry(tmp, struct dentry, d_alias);
375 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
376 if (IS_ROOT(alias) &&
377 (alias->d_flags & DCACHE_DISCONNECTED))
378 discon_alias = alias;
379 else if (!want_discon) {
380 __dget_locked(alias);
386 __dget_locked(discon_alias);
390 struct dentry * d_find_alias(struct inode *inode)
392 struct dentry *de = NULL;
394 if (!list_empty(&inode->i_dentry)) {
395 spin_lock(&dcache_lock);
396 de = __d_find_alias(inode, 0);
397 spin_unlock(&dcache_lock);
401 EXPORT_SYMBOL(d_find_alias);
404 * Try to kill dentries associated with this inode.
405 * WARNING: you must own a reference to inode.
407 void d_prune_aliases(struct inode *inode)
409 struct dentry *dentry;
411 spin_lock(&dcache_lock);
412 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
413 spin_lock(&dentry->d_lock);
414 if (!atomic_read(&dentry->d_count)) {
415 __dget_locked(dentry);
417 spin_unlock(&dentry->d_lock);
418 spin_unlock(&dcache_lock);
422 spin_unlock(&dentry->d_lock);
424 spin_unlock(&dcache_lock);
426 EXPORT_SYMBOL(d_prune_aliases);
429 * Throw away a dentry - free the inode, dput the parent. This requires that
430 * the LRU list has already been removed.
432 * Try to prune ancestors as well. This is necessary to prevent
433 * quadratic behavior of shrink_dcache_parent(), but is also expected
434 * to be beneficial in reducing dentry cache fragmentation.
436 static void prune_one_dentry(struct dentry * dentry)
437 __releases(dentry->d_lock)
438 __releases(dcache_lock)
439 __acquires(dcache_lock)
442 dentry = d_kill(dentry);
445 * Prune ancestors. Locking is simpler than in dput(),
446 * because dcache_lock needs to be taken anyway.
448 spin_lock(&dcache_lock);
450 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
453 if (dentry->d_op && dentry->d_op->d_delete)
454 dentry->d_op->d_delete(dentry);
455 dentry_lru_del_init(dentry);
457 dentry = d_kill(dentry);
458 spin_lock(&dcache_lock);
462 static void shrink_dentry_list(struct list_head *list)
464 struct dentry *dentry;
466 while (!list_empty(list)) {
467 dentry = list_entry(list->prev, struct dentry, d_lru);
468 dentry_lru_del_init(dentry);
471 * We found an inuse dentry which was not removed from
472 * the LRU because of laziness during lookup. Do not free
473 * it - just keep it off the LRU list.
475 spin_lock(&dentry->d_lock);
476 if (atomic_read(&dentry->d_count)) {
477 spin_unlock(&dentry->d_lock);
480 prune_one_dentry(dentry);
481 /* dentry->d_lock was dropped in prune_one_dentry() */
482 cond_resched_lock(&dcache_lock);
487 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
488 * @sb: superblock to shrink dentry LRU.
489 * @count: number of entries to prune
490 * @flags: flags to control the dentry processing
492 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
494 static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
496 /* called from prune_dcache() and shrink_dcache_parent() */
497 struct dentry *dentry;
498 LIST_HEAD(referenced);
502 spin_lock(&dcache_lock);
503 while (!list_empty(&sb->s_dentry_lru)) {
504 dentry = list_entry(sb->s_dentry_lru.prev,
505 struct dentry, d_lru);
506 BUG_ON(dentry->d_sb != sb);
509 * If we are honouring the DCACHE_REFERENCED flag and the
510 * dentry has this flag set, don't free it. Clear the flag
511 * and put it back on the LRU.
513 if (flags & DCACHE_REFERENCED) {
514 spin_lock(&dentry->d_lock);
515 if (dentry->d_flags & DCACHE_REFERENCED) {
516 dentry->d_flags &= ~DCACHE_REFERENCED;
517 list_move(&dentry->d_lru, &referenced);
518 spin_unlock(&dentry->d_lock);
519 cond_resched_lock(&dcache_lock);
522 spin_unlock(&dentry->d_lock);
525 list_move_tail(&dentry->d_lru, &tmp);
528 cond_resched_lock(&dcache_lock);
532 shrink_dentry_list(&tmp);
534 if (!list_empty(&referenced))
535 list_splice(&referenced, &sb->s_dentry_lru);
536 spin_unlock(&dcache_lock);
541 * prune_dcache - shrink the dcache
542 * @count: number of entries to try to free
544 * Shrink the dcache. This is done when we need more memory, or simply when we
545 * need to unmount something (at which point we need to unuse all dentries).
547 * This function may fail to free any resources if all the dentries are in use.
549 static void prune_dcache(int count)
551 struct super_block *sb, *p = NULL;
553 int unused = percpu_counter_sum_positive(&nr_dentry_unused);
557 if (unused == 0 || count == 0)
559 spin_lock(&dcache_lock);
563 prune_ratio = unused / count;
565 list_for_each_entry(sb, &super_blocks, s_list) {
566 if (list_empty(&sb->s_instances))
568 if (sb->s_nr_dentry_unused == 0)
571 /* Now, we reclaim unused dentrins with fairness.
572 * We reclaim them same percentage from each superblock.
573 * We calculate number of dentries to scan on this sb
574 * as follows, but the implementation is arranged to avoid
576 * number of dentries to scan on this sb =
577 * count * (number of dentries on this sb /
578 * number of dentries in the machine)
580 spin_unlock(&sb_lock);
581 if (prune_ratio != 1)
582 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
584 w_count = sb->s_nr_dentry_unused;
587 * We need to be sure this filesystem isn't being unmounted,
588 * otherwise we could race with generic_shutdown_super(), and
589 * end up holding a reference to an inode while the filesystem
590 * is unmounted. So we try to get s_umount, and make sure
593 if (down_read_trylock(&sb->s_umount)) {
594 if ((sb->s_root != NULL) &&
595 (!list_empty(&sb->s_dentry_lru))) {
596 spin_unlock(&dcache_lock);
597 __shrink_dcache_sb(sb, &w_count,
600 spin_lock(&dcache_lock);
602 up_read(&sb->s_umount);
609 /* more work left to do? */
615 spin_unlock(&sb_lock);
616 spin_unlock(&dcache_lock);
620 * shrink_dcache_sb - shrink dcache for a superblock
623 * Shrink the dcache for the specified super block. This is used to free
624 * the dcache before unmounting a file system.
626 void shrink_dcache_sb(struct super_block *sb)
630 spin_lock(&dcache_lock);
631 while (!list_empty(&sb->s_dentry_lru)) {
632 list_splice_init(&sb->s_dentry_lru, &tmp);
633 shrink_dentry_list(&tmp);
635 spin_unlock(&dcache_lock);
637 EXPORT_SYMBOL(shrink_dcache_sb);
640 * destroy a single subtree of dentries for unmount
641 * - see the comments on shrink_dcache_for_umount() for a description of the
644 static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
646 struct dentry *parent;
647 unsigned detached = 0;
649 BUG_ON(!IS_ROOT(dentry));
651 /* detach this root from the system */
652 spin_lock(&dcache_lock);
653 dentry_lru_del_init(dentry);
655 spin_unlock(&dcache_lock);
658 /* descend to the first leaf in the current subtree */
659 while (!list_empty(&dentry->d_subdirs)) {
662 /* this is a branch with children - detach all of them
663 * from the system in one go */
664 spin_lock(&dcache_lock);
665 list_for_each_entry(loop, &dentry->d_subdirs,
667 dentry_lru_del_init(loop);
669 cond_resched_lock(&dcache_lock);
671 spin_unlock(&dcache_lock);
673 /* move to the first child */
674 dentry = list_entry(dentry->d_subdirs.next,
675 struct dentry, d_u.d_child);
678 /* consume the dentries from this leaf up through its parents
679 * until we find one with children or run out altogether */
683 if (atomic_read(&dentry->d_count) != 0) {
685 "BUG: Dentry %p{i=%lx,n=%s}"
687 " [unmount of %s %s]\n",
690 dentry->d_inode->i_ino : 0UL,
692 atomic_read(&dentry->d_count),
693 dentry->d_sb->s_type->name,
701 parent = dentry->d_parent;
702 atomic_dec(&parent->d_count);
705 list_del(&dentry->d_u.d_child);
708 inode = dentry->d_inode;
710 dentry->d_inode = NULL;
711 list_del_init(&dentry->d_alias);
712 if (dentry->d_op && dentry->d_op->d_iput)
713 dentry->d_op->d_iput(dentry, inode);
720 /* finished when we fall off the top of the tree,
721 * otherwise we ascend to the parent and move to the
722 * next sibling if there is one */
726 } while (list_empty(&dentry->d_subdirs));
728 dentry = list_entry(dentry->d_subdirs.next,
729 struct dentry, d_u.d_child);
734 * destroy the dentries attached to a superblock on unmounting
735 * - we don't need to use dentry->d_lock, and only need dcache_lock when
736 * removing the dentry from the system lists and hashes because:
737 * - the superblock is detached from all mountings and open files, so the
738 * dentry trees will not be rearranged by the VFS
739 * - s_umount is write-locked, so the memory pressure shrinker will ignore
740 * any dentries belonging to this superblock that it comes across
741 * - the filesystem itself is no longer permitted to rearrange the dentries
744 void shrink_dcache_for_umount(struct super_block *sb)
746 struct dentry *dentry;
748 if (down_read_trylock(&sb->s_umount))
753 atomic_dec(&dentry->d_count);
754 shrink_dcache_for_umount_subtree(dentry);
756 while (!hlist_empty(&sb->s_anon)) {
757 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
758 shrink_dcache_for_umount_subtree(dentry);
763 * Search for at least 1 mount point in the dentry's subdirs.
764 * We descend to the next level whenever the d_subdirs
765 * list is non-empty and continue searching.
769 * have_submounts - check for mounts over a dentry
770 * @parent: dentry to check.
772 * Return true if the parent or its subdirectories contain
776 int have_submounts(struct dentry *parent)
778 struct dentry *this_parent = parent;
779 struct list_head *next;
781 spin_lock(&dcache_lock);
782 if (d_mountpoint(parent))
785 next = this_parent->d_subdirs.next;
787 while (next != &this_parent->d_subdirs) {
788 struct list_head *tmp = next;
789 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
791 /* Have we found a mount point ? */
792 if (d_mountpoint(dentry))
794 if (!list_empty(&dentry->d_subdirs)) {
795 this_parent = dentry;
800 * All done at this level ... ascend and resume the search.
802 if (this_parent != parent) {
803 next = this_parent->d_u.d_child.next;
804 this_parent = this_parent->d_parent;
807 spin_unlock(&dcache_lock);
808 return 0; /* No mount points found in tree */
810 spin_unlock(&dcache_lock);
813 EXPORT_SYMBOL(have_submounts);
816 * Search the dentry child list for the specified parent,
817 * and move any unused dentries to the end of the unused
818 * list for prune_dcache(). We descend to the next level
819 * whenever the d_subdirs list is non-empty and continue
822 * It returns zero iff there are no unused children,
823 * otherwise it returns the number of children moved to
824 * the end of the unused list. This may not be the total
825 * number of unused children, because select_parent can
826 * drop the lock and return early due to latency
829 static int select_parent(struct dentry * parent)
831 struct dentry *this_parent = parent;
832 struct list_head *next;
835 spin_lock(&dcache_lock);
837 next = this_parent->d_subdirs.next;
839 while (next != &this_parent->d_subdirs) {
840 struct list_head *tmp = next;
841 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
844 dentry_lru_del_init(dentry);
846 * move only zero ref count dentries to the end
847 * of the unused list for prune_dcache
849 if (!atomic_read(&dentry->d_count)) {
850 dentry_lru_add_tail(dentry);
855 * We can return to the caller if we have found some (this
856 * ensures forward progress). We'll be coming back to find
859 if (found && need_resched())
863 * Descend a level if the d_subdirs list is non-empty.
865 if (!list_empty(&dentry->d_subdirs)) {
866 this_parent = dentry;
871 * All done at this level ... ascend and resume the search.
873 if (this_parent != parent) {
874 next = this_parent->d_u.d_child.next;
875 this_parent = this_parent->d_parent;
879 spin_unlock(&dcache_lock);
884 * shrink_dcache_parent - prune dcache
885 * @parent: parent of entries to prune
887 * Prune the dcache to remove unused children of the parent dentry.
890 void shrink_dcache_parent(struct dentry * parent)
892 struct super_block *sb = parent->d_sb;
895 while ((found = select_parent(parent)) != 0)
896 __shrink_dcache_sb(sb, &found, 0);
898 EXPORT_SYMBOL(shrink_dcache_parent);
901 * Scan `nr' dentries and return the number which remain.
903 * We need to avoid reentering the filesystem if the caller is performing a
904 * GFP_NOFS allocation attempt. One example deadlock is:
906 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
907 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
908 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
910 * In this case we return -1 to tell the caller that we baled.
912 static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
917 if (!(gfp_mask & __GFP_FS))
922 nr_unused = percpu_counter_sum_positive(&nr_dentry_unused);
923 return (nr_unused / 100) * sysctl_vfs_cache_pressure;
926 static struct shrinker dcache_shrinker = {
927 .shrink = shrink_dcache_memory,
928 .seeks = DEFAULT_SEEKS,
932 * d_alloc - allocate a dcache entry
933 * @parent: parent of entry to allocate
934 * @name: qstr of the name
936 * Allocates a dentry. It returns %NULL if there is insufficient memory
937 * available. On a success the dentry is returned. The name passed in is
938 * copied and the copy passed in may be reused after this call.
941 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
943 struct dentry *dentry;
946 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
950 if (name->len > DNAME_INLINE_LEN-1) {
951 dname = kmalloc(name->len + 1, GFP_KERNEL);
953 kmem_cache_free(dentry_cache, dentry);
957 dname = dentry->d_iname;
959 dentry->d_name.name = dname;
961 dentry->d_name.len = name->len;
962 dentry->d_name.hash = name->hash;
963 memcpy(dname, name->name, name->len);
964 dname[name->len] = 0;
966 atomic_set(&dentry->d_count, 1);
967 dentry->d_flags = DCACHE_UNHASHED;
968 spin_lock_init(&dentry->d_lock);
969 dentry->d_inode = NULL;
970 dentry->d_parent = NULL;
973 dentry->d_fsdata = NULL;
974 dentry->d_mounted = 0;
975 INIT_HLIST_NODE(&dentry->d_hash);
976 INIT_LIST_HEAD(&dentry->d_lru);
977 INIT_LIST_HEAD(&dentry->d_subdirs);
978 INIT_LIST_HEAD(&dentry->d_alias);
981 dentry->d_parent = dget(parent);
982 dentry->d_sb = parent->d_sb;
984 INIT_LIST_HEAD(&dentry->d_u.d_child);
987 spin_lock(&dcache_lock);
989 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
990 spin_unlock(&dcache_lock);
992 percpu_counter_inc(&nr_dentry);
996 EXPORT_SYMBOL(d_alloc);
998 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1003 q.len = strlen(name);
1004 q.hash = full_name_hash(q.name, q.len);
1005 return d_alloc(parent, &q);
1007 EXPORT_SYMBOL(d_alloc_name);
1009 /* the caller must hold dcache_lock */
1010 static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1013 list_add(&dentry->d_alias, &inode->i_dentry);
1014 dentry->d_inode = inode;
1015 fsnotify_d_instantiate(dentry, inode);
1019 * d_instantiate - fill in inode information for a dentry
1020 * @entry: dentry to complete
1021 * @inode: inode to attach to this dentry
1023 * Fill in inode information in the entry.
1025 * This turns negative dentries into productive full members
1028 * NOTE! This assumes that the inode count has been incremented
1029 * (or otherwise set) by the caller to indicate that it is now
1030 * in use by the dcache.
1033 void d_instantiate(struct dentry *entry, struct inode * inode)
1035 BUG_ON(!list_empty(&entry->d_alias));
1036 spin_lock(&dcache_lock);
1037 __d_instantiate(entry, inode);
1038 spin_unlock(&dcache_lock);
1039 security_d_instantiate(entry, inode);
1041 EXPORT_SYMBOL(d_instantiate);
1044 * d_instantiate_unique - instantiate a non-aliased dentry
1045 * @entry: dentry to instantiate
1046 * @inode: inode to attach to this dentry
1048 * Fill in inode information in the entry. On success, it returns NULL.
1049 * If an unhashed alias of "entry" already exists, then we return the
1050 * aliased dentry instead and drop one reference to inode.
1052 * Note that in order to avoid conflicts with rename() etc, the caller
1053 * had better be holding the parent directory semaphore.
1055 * This also assumes that the inode count has been incremented
1056 * (or otherwise set) by the caller to indicate that it is now
1057 * in use by the dcache.
1059 static struct dentry *__d_instantiate_unique(struct dentry *entry,
1060 struct inode *inode)
1062 struct dentry *alias;
1063 int len = entry->d_name.len;
1064 const char *name = entry->d_name.name;
1065 unsigned int hash = entry->d_name.hash;
1068 __d_instantiate(entry, NULL);
1072 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1073 struct qstr *qstr = &alias->d_name;
1075 if (qstr->hash != hash)
1077 if (alias->d_parent != entry->d_parent)
1079 if (qstr->len != len)
1081 if (memcmp(qstr->name, name, len))
1087 __d_instantiate(entry, inode);
1091 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1093 struct dentry *result;
1095 BUG_ON(!list_empty(&entry->d_alias));
1097 spin_lock(&dcache_lock);
1098 result = __d_instantiate_unique(entry, inode);
1099 spin_unlock(&dcache_lock);
1102 security_d_instantiate(entry, inode);
1106 BUG_ON(!d_unhashed(result));
1111 EXPORT_SYMBOL(d_instantiate_unique);
1114 * d_alloc_root - allocate root dentry
1115 * @root_inode: inode to allocate the root for
1117 * Allocate a root ("/") dentry for the inode given. The inode is
1118 * instantiated and returned. %NULL is returned if there is insufficient
1119 * memory or the inode passed is %NULL.
1122 struct dentry * d_alloc_root(struct inode * root_inode)
1124 struct dentry *res = NULL;
1127 static const struct qstr name = { .name = "/", .len = 1 };
1129 res = d_alloc(NULL, &name);
1131 res->d_sb = root_inode->i_sb;
1132 res->d_parent = res;
1133 d_instantiate(res, root_inode);
1138 EXPORT_SYMBOL(d_alloc_root);
1140 static inline struct hlist_head *d_hash(struct dentry *parent,
1143 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1144 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1145 return dentry_hashtable + (hash & D_HASHMASK);
1149 * d_obtain_alias - find or allocate a dentry for a given inode
1150 * @inode: inode to allocate the dentry for
1152 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1153 * similar open by handle operations. The returned dentry may be anonymous,
1154 * or may have a full name (if the inode was already in the cache).
1156 * When called on a directory inode, we must ensure that the inode only ever
1157 * has one dentry. If a dentry is found, that is returned instead of
1158 * allocating a new one.
1160 * On successful return, the reference to the inode has been transferred
1161 * to the dentry. In case of an error the reference on the inode is released.
1162 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1163 * be passed in and will be the error will be propagate to the return value,
1164 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1166 struct dentry *d_obtain_alias(struct inode *inode)
1168 static const struct qstr anonstring = { .name = "" };
1173 return ERR_PTR(-ESTALE);
1175 return ERR_CAST(inode);
1177 res = d_find_alias(inode);
1181 tmp = d_alloc(NULL, &anonstring);
1183 res = ERR_PTR(-ENOMEM);
1186 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1188 spin_lock(&dcache_lock);
1189 res = __d_find_alias(inode, 0);
1191 spin_unlock(&dcache_lock);
1196 /* attach a disconnected dentry */
1197 spin_lock(&tmp->d_lock);
1198 tmp->d_sb = inode->i_sb;
1199 tmp->d_inode = inode;
1200 tmp->d_flags |= DCACHE_DISCONNECTED;
1201 tmp->d_flags &= ~DCACHE_UNHASHED;
1202 list_add(&tmp->d_alias, &inode->i_dentry);
1203 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1204 spin_unlock(&tmp->d_lock);
1206 spin_unlock(&dcache_lock);
1213 EXPORT_SYMBOL(d_obtain_alias);
1216 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1217 * @inode: the inode which may have a disconnected dentry
1218 * @dentry: a negative dentry which we want to point to the inode.
1220 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1221 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1222 * and return it, else simply d_add the inode to the dentry and return NULL.
1224 * This is needed in the lookup routine of any filesystem that is exportable
1225 * (via knfsd) so that we can build dcache paths to directories effectively.
1227 * If a dentry was found and moved, then it is returned. Otherwise NULL
1228 * is returned. This matches the expected return value of ->lookup.
1231 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1233 struct dentry *new = NULL;
1235 if (inode && S_ISDIR(inode->i_mode)) {
1236 spin_lock(&dcache_lock);
1237 new = __d_find_alias(inode, 1);
1239 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1240 spin_unlock(&dcache_lock);
1241 security_d_instantiate(new, inode);
1242 d_move(new, dentry);
1245 /* already taking dcache_lock, so d_add() by hand */
1246 __d_instantiate(dentry, inode);
1247 spin_unlock(&dcache_lock);
1248 security_d_instantiate(dentry, inode);
1252 d_add(dentry, inode);
1255 EXPORT_SYMBOL(d_splice_alias);
1258 * d_add_ci - lookup or allocate new dentry with case-exact name
1259 * @inode: the inode case-insensitive lookup has found
1260 * @dentry: the negative dentry that was passed to the parent's lookup func
1261 * @name: the case-exact name to be associated with the returned dentry
1263 * This is to avoid filling the dcache with case-insensitive names to the
1264 * same inode, only the actual correct case is stored in the dcache for
1265 * case-insensitive filesystems.
1267 * For a case-insensitive lookup match and if the the case-exact dentry
1268 * already exists in in the dcache, use it and return it.
1270 * If no entry exists with the exact case name, allocate new dentry with
1271 * the exact case, and return the spliced entry.
1273 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
1277 struct dentry *found;
1281 * First check if a dentry matching the name already exists,
1282 * if not go ahead and create it now.
1284 found = d_hash_and_lookup(dentry->d_parent, name);
1286 new = d_alloc(dentry->d_parent, name);
1292 found = d_splice_alias(inode, new);
1301 * If a matching dentry exists, and it's not negative use it.
1303 * Decrement the reference count to balance the iget() done
1306 if (found->d_inode) {
1307 if (unlikely(found->d_inode != inode)) {
1308 /* This can't happen because bad inodes are unhashed. */
1309 BUG_ON(!is_bad_inode(inode));
1310 BUG_ON(!is_bad_inode(found->d_inode));
1317 * Negative dentry: instantiate it unless the inode is a directory and
1318 * already has a dentry.
1320 spin_lock(&dcache_lock);
1321 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
1322 __d_instantiate(found, inode);
1323 spin_unlock(&dcache_lock);
1324 security_d_instantiate(found, inode);
1329 * In case a directory already has a (disconnected) entry grab a
1330 * reference to it, move it in place and use it.
1332 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1334 spin_unlock(&dcache_lock);
1335 security_d_instantiate(found, inode);
1343 return ERR_PTR(error);
1345 EXPORT_SYMBOL(d_add_ci);
1348 * d_lookup - search for a dentry
1349 * @parent: parent dentry
1350 * @name: qstr of name we wish to find
1351 * Returns: dentry, or NULL
1353 * d_lookup searches the children of the parent dentry for the name in
1354 * question. If the dentry is found its reference count is incremented and the
1355 * dentry is returned. The caller must use dput to free the entry when it has
1356 * finished using it. %NULL is returned if the dentry does not exist.
1358 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1360 struct dentry * dentry = NULL;
1364 seq = read_seqbegin(&rename_lock);
1365 dentry = __d_lookup(parent, name);
1368 } while (read_seqretry(&rename_lock, seq));
1371 EXPORT_SYMBOL(d_lookup);
1374 * __d_lookup - search for a dentry (racy)
1375 * @parent: parent dentry
1376 * @name: qstr of name we wish to find
1377 * Returns: dentry, or NULL
1379 * __d_lookup is like d_lookup, however it may (rarely) return a
1380 * false-negative result due to unrelated rename activity.
1382 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1383 * however it must be used carefully, eg. with a following d_lookup in
1384 * the case of failure.
1386 * __d_lookup callers must be commented.
1388 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1390 unsigned int len = name->len;
1391 unsigned int hash = name->hash;
1392 const unsigned char *str = name->name;
1393 struct hlist_head *head = d_hash(parent,hash);
1394 struct dentry *found = NULL;
1395 struct hlist_node *node;
1396 struct dentry *dentry;
1399 * The hash list is protected using RCU.
1401 * Take d_lock when comparing a candidate dentry, to avoid races
1404 * It is possible that concurrent renames can mess up our list
1405 * walk here and result in missing our dentry, resulting in the
1406 * false-negative result. d_lookup() protects against concurrent
1407 * renames using rename_lock seqlock.
1409 * See Documentation/vfs/dcache-locking.txt for more details.
1413 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1416 if (dentry->d_name.hash != hash)
1418 if (dentry->d_parent != parent)
1421 spin_lock(&dentry->d_lock);
1424 * Recheck the dentry after taking the lock - d_move may have
1425 * changed things. Don't bother checking the hash because
1426 * we're about to compare the whole name anyway.
1428 if (dentry->d_parent != parent)
1431 /* non-existing due to RCU? */
1432 if (d_unhashed(dentry))
1436 * It is safe to compare names since d_move() cannot
1437 * change the qstr (protected by d_lock).
1439 qstr = &dentry->d_name;
1440 if (parent->d_op && parent->d_op->d_compare) {
1441 if (parent->d_op->d_compare(parent, qstr, name))
1444 if (qstr->len != len)
1446 if (memcmp(qstr->name, str, len))
1450 atomic_inc(&dentry->d_count);
1452 spin_unlock(&dentry->d_lock);
1455 spin_unlock(&dentry->d_lock);
1463 * d_hash_and_lookup - hash the qstr then search for a dentry
1464 * @dir: Directory to search in
1465 * @name: qstr of name we wish to find
1467 * On hash failure or on lookup failure NULL is returned.
1469 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1471 struct dentry *dentry = NULL;
1474 * Check for a fs-specific hash function. Note that we must
1475 * calculate the standard hash first, as the d_op->d_hash()
1476 * routine may choose to leave the hash value unchanged.
1478 name->hash = full_name_hash(name->name, name->len);
1479 if (dir->d_op && dir->d_op->d_hash) {
1480 if (dir->d_op->d_hash(dir, name) < 0)
1483 dentry = d_lookup(dir, name);
1489 * d_validate - verify dentry provided from insecure source
1490 * @dentry: The dentry alleged to be valid child of @dparent
1491 * @dparent: The parent dentry (known to be valid)
1493 * An insecure source has sent us a dentry, here we verify it and dget() it.
1494 * This is used by ncpfs in its readdir implementation.
1495 * Zero is returned in the dentry is invalid.
1498 int d_validate(struct dentry *dentry, struct dentry *dparent)
1500 struct hlist_head *base;
1501 struct hlist_node *lhp;
1503 /* Check whether the ptr might be valid at all.. */
1504 if (!kmem_ptr_validate(dentry_cache, dentry))
1507 if (dentry->d_parent != dparent)
1510 spin_lock(&dcache_lock);
1511 base = d_hash(dparent, dentry->d_name.hash);
1512 hlist_for_each(lhp,base) {
1513 /* hlist_for_each_entry_rcu() not required for d_hash list
1514 * as it is parsed under dcache_lock
1516 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1517 __dget_locked(dentry);
1518 spin_unlock(&dcache_lock);
1522 spin_unlock(&dcache_lock);
1526 EXPORT_SYMBOL(d_validate);
1529 * When a file is deleted, we have two options:
1530 * - turn this dentry into a negative dentry
1531 * - unhash this dentry and free it.
1533 * Usually, we want to just turn this into
1534 * a negative dentry, but if anybody else is
1535 * currently using the dentry or the inode
1536 * we can't do that and we fall back on removing
1537 * it from the hash queues and waiting for
1538 * it to be deleted later when it has no users
1542 * d_delete - delete a dentry
1543 * @dentry: The dentry to delete
1545 * Turn the dentry into a negative dentry if possible, otherwise
1546 * remove it from the hash queues so it can be deleted later
1549 void d_delete(struct dentry * dentry)
1553 * Are we the only user?
1555 spin_lock(&dcache_lock);
1556 spin_lock(&dentry->d_lock);
1557 isdir = S_ISDIR(dentry->d_inode->i_mode);
1558 if (atomic_read(&dentry->d_count) == 1) {
1559 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
1560 dentry_iput(dentry);
1561 fsnotify_nameremove(dentry, isdir);
1565 if (!d_unhashed(dentry))
1568 spin_unlock(&dentry->d_lock);
1569 spin_unlock(&dcache_lock);
1571 fsnotify_nameremove(dentry, isdir);
1573 EXPORT_SYMBOL(d_delete);
1575 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1578 entry->d_flags &= ~DCACHE_UNHASHED;
1579 hlist_add_head_rcu(&entry->d_hash, list);
1582 static void _d_rehash(struct dentry * entry)
1584 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1588 * d_rehash - add an entry back to the hash
1589 * @entry: dentry to add to the hash
1591 * Adds a dentry to the hash according to its name.
1594 void d_rehash(struct dentry * entry)
1596 spin_lock(&dcache_lock);
1597 spin_lock(&entry->d_lock);
1599 spin_unlock(&entry->d_lock);
1600 spin_unlock(&dcache_lock);
1602 EXPORT_SYMBOL(d_rehash);
1605 * When switching names, the actual string doesn't strictly have to
1606 * be preserved in the target - because we're dropping the target
1607 * anyway. As such, we can just do a simple memcpy() to copy over
1608 * the new name before we switch.
1610 * Note that we have to be a lot more careful about getting the hash
1611 * switched - we have to switch the hash value properly even if it
1612 * then no longer matches the actual (corrupted) string of the target.
1613 * The hash value has to match the hash queue that the dentry is on..
1615 static void switch_names(struct dentry *dentry, struct dentry *target)
1617 if (dname_external(target)) {
1618 if (dname_external(dentry)) {
1620 * Both external: swap the pointers
1622 swap(target->d_name.name, dentry->d_name.name);
1625 * dentry:internal, target:external. Steal target's
1626 * storage and make target internal.
1628 memcpy(target->d_iname, dentry->d_name.name,
1629 dentry->d_name.len + 1);
1630 dentry->d_name.name = target->d_name.name;
1631 target->d_name.name = target->d_iname;
1634 if (dname_external(dentry)) {
1636 * dentry:external, target:internal. Give dentry's
1637 * storage to target and make dentry internal
1639 memcpy(dentry->d_iname, target->d_name.name,
1640 target->d_name.len + 1);
1641 target->d_name.name = dentry->d_name.name;
1642 dentry->d_name.name = dentry->d_iname;
1645 * Both are internal. Just copy target to dentry
1647 memcpy(dentry->d_iname, target->d_name.name,
1648 target->d_name.len + 1);
1649 dentry->d_name.len = target->d_name.len;
1653 swap(dentry->d_name.len, target->d_name.len);
1657 * We cannibalize "target" when moving dentry on top of it,
1658 * because it's going to be thrown away anyway. We could be more
1659 * polite about it, though.
1661 * This forceful removal will result in ugly /proc output if
1662 * somebody holds a file open that got deleted due to a rename.
1663 * We could be nicer about the deleted file, and let it show
1664 * up under the name it had before it was deleted rather than
1665 * under the original name of the file that was moved on top of it.
1669 * d_move_locked - move a dentry
1670 * @dentry: entry to move
1671 * @target: new dentry
1673 * Update the dcache to reflect the move of a file name. Negative
1674 * dcache entries should not be moved in this way.
1676 static void d_move_locked(struct dentry * dentry, struct dentry * target)
1678 struct hlist_head *list;
1680 if (!dentry->d_inode)
1681 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1683 write_seqlock(&rename_lock);
1685 * XXXX: do we really need to take target->d_lock?
1687 if (target < dentry) {
1688 spin_lock(&target->d_lock);
1689 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1691 spin_lock(&dentry->d_lock);
1692 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
1695 /* Move the dentry to the target hash queue, if on different bucket */
1696 if (d_unhashed(dentry))
1697 goto already_unhashed;
1699 hlist_del_rcu(&dentry->d_hash);
1702 list = d_hash(target->d_parent, target->d_name.hash);
1703 __d_rehash(dentry, list);
1705 /* Unhash the target: dput() will then get rid of it */
1708 list_del(&dentry->d_u.d_child);
1709 list_del(&target->d_u.d_child);
1711 /* Switch the names.. */
1712 switch_names(dentry, target);
1713 swap(dentry->d_name.hash, target->d_name.hash);
1715 /* ... and switch the parents */
1716 if (IS_ROOT(dentry)) {
1717 dentry->d_parent = target->d_parent;
1718 target->d_parent = target;
1719 INIT_LIST_HEAD(&target->d_u.d_child);
1721 swap(dentry->d_parent, target->d_parent);
1723 /* And add them back to the (new) parent lists */
1724 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1727 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1728 spin_unlock(&target->d_lock);
1729 fsnotify_d_move(dentry);
1730 spin_unlock(&dentry->d_lock);
1731 write_sequnlock(&rename_lock);
1735 * d_move - move a dentry
1736 * @dentry: entry to move
1737 * @target: new dentry
1739 * Update the dcache to reflect the move of a file name. Negative
1740 * dcache entries should not be moved in this way.
1743 void d_move(struct dentry * dentry, struct dentry * target)
1745 spin_lock(&dcache_lock);
1746 d_move_locked(dentry, target);
1747 spin_unlock(&dcache_lock);
1749 EXPORT_SYMBOL(d_move);
1752 * d_ancestor - search for an ancestor
1753 * @p1: ancestor dentry
1756 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1757 * an ancestor of p2, else NULL.
1759 struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
1763 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
1764 if (p->d_parent == p1)
1771 * This helper attempts to cope with remotely renamed directories
1773 * It assumes that the caller is already holding
1774 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1776 * Note: If ever the locking in lock_rename() changes, then please
1777 * remember to update this too...
1779 static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1780 __releases(dcache_lock)
1782 struct mutex *m1 = NULL, *m2 = NULL;
1785 /* If alias and dentry share a parent, then no extra locks required */
1786 if (alias->d_parent == dentry->d_parent)
1789 /* Check for loops */
1790 ret = ERR_PTR(-ELOOP);
1791 if (d_ancestor(alias, dentry))
1794 /* See lock_rename() */
1795 ret = ERR_PTR(-EBUSY);
1796 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1798 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1799 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1801 m2 = &alias->d_parent->d_inode->i_mutex;
1803 d_move_locked(alias, dentry);
1806 spin_unlock(&dcache_lock);
1815 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1816 * named dentry in place of the dentry to be replaced.
1818 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1820 struct dentry *dparent, *aparent;
1822 switch_names(dentry, anon);
1823 swap(dentry->d_name.hash, anon->d_name.hash);
1825 dparent = dentry->d_parent;
1826 aparent = anon->d_parent;
1828 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1829 list_del(&dentry->d_u.d_child);
1830 if (!IS_ROOT(dentry))
1831 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1833 INIT_LIST_HEAD(&dentry->d_u.d_child);
1835 anon->d_parent = (dparent == dentry) ? anon : dparent;
1836 list_del(&anon->d_u.d_child);
1838 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1840 INIT_LIST_HEAD(&anon->d_u.d_child);
1842 anon->d_flags &= ~DCACHE_DISCONNECTED;
1846 * d_materialise_unique - introduce an inode into the tree
1847 * @dentry: candidate dentry
1848 * @inode: inode to bind to the dentry, to which aliases may be attached
1850 * Introduces an dentry into the tree, substituting an extant disconnected
1851 * root directory alias in its place if there is one
1853 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1855 struct dentry *actual;
1857 BUG_ON(!d_unhashed(dentry));
1859 spin_lock(&dcache_lock);
1863 __d_instantiate(dentry, NULL);
1867 if (S_ISDIR(inode->i_mode)) {
1868 struct dentry *alias;
1870 /* Does an aliased dentry already exist? */
1871 alias = __d_find_alias(inode, 0);
1874 /* Is this an anonymous mountpoint that we could splice
1876 if (IS_ROOT(alias)) {
1877 spin_lock(&alias->d_lock);
1878 __d_materialise_dentry(dentry, alias);
1882 /* Nope, but we must(!) avoid directory aliasing */
1883 actual = __d_unalias(dentry, alias);
1890 /* Add a unique reference */
1891 actual = __d_instantiate_unique(dentry, inode);
1894 else if (unlikely(!d_unhashed(actual)))
1895 goto shouldnt_be_hashed;
1898 spin_lock(&actual->d_lock);
1901 spin_unlock(&actual->d_lock);
1902 spin_unlock(&dcache_lock);
1904 if (actual == dentry) {
1905 security_d_instantiate(dentry, inode);
1913 spin_unlock(&dcache_lock);
1916 EXPORT_SYMBOL_GPL(d_materialise_unique);
1918 static int prepend(char **buffer, int *buflen, const char *str, int namelen)
1922 return -ENAMETOOLONG;
1924 memcpy(*buffer, str, namelen);
1928 static int prepend_name(char **buffer, int *buflen, struct qstr *name)
1930 return prepend(buffer, buflen, name->name, name->len);
1934 * Prepend path string to a buffer
1936 * @path: the dentry/vfsmount to report
1937 * @root: root vfsmnt/dentry (may be modified by this function)
1938 * @buffer: pointer to the end of the buffer
1939 * @buflen: pointer to buffer length
1941 * Caller holds the dcache_lock.
1943 * If path is not reachable from the supplied root, then the value of
1944 * root is changed (without modifying refcounts).
1946 static int prepend_path(const struct path *path, struct path *root,
1947 char **buffer, int *buflen)
1949 struct dentry *dentry = path->dentry;
1950 struct vfsmount *vfsmnt = path->mnt;
1954 br_read_lock(vfsmount_lock);
1955 while (dentry != root->dentry || vfsmnt != root->mnt) {
1956 struct dentry * parent;
1958 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1960 if (vfsmnt->mnt_parent == vfsmnt) {
1963 dentry = vfsmnt->mnt_mountpoint;
1964 vfsmnt = vfsmnt->mnt_parent;
1967 parent = dentry->d_parent;
1969 error = prepend_name(buffer, buflen, &dentry->d_name);
1971 error = prepend(buffer, buflen, "/", 1);
1980 if (!error && !slash)
1981 error = prepend(buffer, buflen, "/", 1);
1983 br_read_unlock(vfsmount_lock);
1988 * Filesystems needing to implement special "root names"
1989 * should do so with ->d_dname()
1991 if (IS_ROOT(dentry) &&
1992 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
1993 WARN(1, "Root dentry has weird name <%.*s>\n",
1994 (int) dentry->d_name.len, dentry->d_name.name);
1997 root->dentry = dentry;
2002 * __d_path - return the path of a dentry
2003 * @path: the dentry/vfsmount to report
2004 * @root: root vfsmnt/dentry (may be modified by this function)
2005 * @buf: buffer to return value in
2006 * @buflen: buffer length
2008 * Convert a dentry into an ASCII path name.
2010 * Returns a pointer into the buffer or an error code if the
2011 * path was too long.
2013 * "buflen" should be positive.
2015 * If path is not reachable from the supplied root, then the value of
2016 * root is changed (without modifying refcounts).
2018 char *__d_path(const struct path *path, struct path *root,
2019 char *buf, int buflen)
2021 char *res = buf + buflen;
2024 prepend(&res, &buflen, "\0", 1);
2025 spin_lock(&dcache_lock);
2026 error = prepend_path(path, root, &res, &buflen);
2027 spin_unlock(&dcache_lock);
2030 return ERR_PTR(error);
2035 * same as __d_path but appends "(deleted)" for unlinked files.
2037 static int path_with_deleted(const struct path *path, struct path *root,
2038 char **buf, int *buflen)
2040 prepend(buf, buflen, "\0", 1);
2041 if (d_unlinked(path->dentry)) {
2042 int error = prepend(buf, buflen, " (deleted)", 10);
2047 return prepend_path(path, root, buf, buflen);
2050 static int prepend_unreachable(char **buffer, int *buflen)
2052 return prepend(buffer, buflen, "(unreachable)", 13);
2056 * d_path - return the path of a dentry
2057 * @path: path to report
2058 * @buf: buffer to return value in
2059 * @buflen: buffer length
2061 * Convert a dentry into an ASCII path name. If the entry has been deleted
2062 * the string " (deleted)" is appended. Note that this is ambiguous.
2064 * Returns a pointer into the buffer or an error code if the path was
2065 * too long. Note: Callers should use the returned pointer, not the passed
2066 * in buffer, to use the name! The implementation often starts at an offset
2067 * into the buffer, and may leave 0 bytes at the start.
2069 * "buflen" should be positive.
2071 char *d_path(const struct path *path, char *buf, int buflen)
2073 char *res = buf + buflen;
2079 * We have various synthetic filesystems that never get mounted. On
2080 * these filesystems dentries are never used for lookup purposes, and
2081 * thus don't need to be hashed. They also don't need a name until a
2082 * user wants to identify the object in /proc/pid/fd/. The little hack
2083 * below allows us to generate a name for these objects on demand:
2085 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2086 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2088 get_fs_root(current->fs, &root);
2089 spin_lock(&dcache_lock);
2091 error = path_with_deleted(path, &tmp, &res, &buflen);
2093 res = ERR_PTR(error);
2094 spin_unlock(&dcache_lock);
2098 EXPORT_SYMBOL(d_path);
2101 * d_path_with_unreachable - return the path of a dentry
2102 * @path: path to report
2103 * @buf: buffer to return value in
2104 * @buflen: buffer length
2106 * The difference from d_path() is that this prepends "(unreachable)"
2107 * to paths which are unreachable from the current process' root.
2109 char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2111 char *res = buf + buflen;
2116 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2117 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2119 get_fs_root(current->fs, &root);
2120 spin_lock(&dcache_lock);
2122 error = path_with_deleted(path, &tmp, &res, &buflen);
2123 if (!error && !path_equal(&tmp, &root))
2124 error = prepend_unreachable(&res, &buflen);
2125 spin_unlock(&dcache_lock);
2128 res = ERR_PTR(error);
2134 * Helper function for dentry_operations.d_dname() members
2136 char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2137 const char *fmt, ...)
2143 va_start(args, fmt);
2144 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2147 if (sz > sizeof(temp) || sz > buflen)
2148 return ERR_PTR(-ENAMETOOLONG);
2150 buffer += buflen - sz;
2151 return memcpy(buffer, temp, sz);
2155 * Write full pathname from the root of the filesystem into the buffer.
2157 char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
2159 char *end = buf + buflen;
2162 prepend(&end, &buflen, "\0", 1);
2169 while (!IS_ROOT(dentry)) {
2170 struct dentry *parent = dentry->d_parent;
2173 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
2174 (prepend(&end, &buflen, "/", 1) != 0))
2182 return ERR_PTR(-ENAMETOOLONG);
2184 EXPORT_SYMBOL(__dentry_path);
2186 char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2191 spin_lock(&dcache_lock);
2192 if (d_unlinked(dentry)) {
2194 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2198 retval = __dentry_path(dentry, buf, buflen);
2199 spin_unlock(&dcache_lock);
2200 if (!IS_ERR(retval) && p)
2201 *p = '/'; /* restore '/' overriden with '\0' */
2204 spin_unlock(&dcache_lock);
2205 return ERR_PTR(-ENAMETOOLONG);
2209 * NOTE! The user-level library version returns a
2210 * character pointer. The kernel system call just
2211 * returns the length of the buffer filled (which
2212 * includes the ending '\0' character), or a negative
2213 * error value. So libc would do something like
2215 * char *getcwd(char * buf, size_t size)
2219 * retval = sys_getcwd(buf, size);
2226 SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
2229 struct path pwd, root;
2230 char *page = (char *) __get_free_page(GFP_USER);
2235 get_fs_root_and_pwd(current->fs, &root, &pwd);
2238 spin_lock(&dcache_lock);
2239 if (!d_unlinked(pwd.dentry)) {
2241 struct path tmp = root;
2242 char *cwd = page + PAGE_SIZE;
2243 int buflen = PAGE_SIZE;
2245 prepend(&cwd, &buflen, "\0", 1);
2246 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
2247 spin_unlock(&dcache_lock);
2252 /* Unreachable from current root */
2253 if (!path_equal(&tmp, &root)) {
2254 error = prepend_unreachable(&cwd, &buflen);
2260 len = PAGE_SIZE + page - cwd;
2263 if (copy_to_user(buf, cwd, len))
2267 spin_unlock(&dcache_lock);
2272 free_page((unsigned long) page);
2277 * Test whether new_dentry is a subdirectory of old_dentry.
2279 * Trivially implemented using the dcache structure
2283 * is_subdir - is new dentry a subdirectory of old_dentry
2284 * @new_dentry: new dentry
2285 * @old_dentry: old dentry
2287 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2288 * Returns 0 otherwise.
2289 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2292 int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
2297 if (new_dentry == old_dentry)
2301 * Need rcu_readlock to protect against the d_parent trashing
2306 /* for restarting inner loop in case of seq retry */
2307 seq = read_seqbegin(&rename_lock);
2308 if (d_ancestor(old_dentry, new_dentry))
2312 } while (read_seqretry(&rename_lock, seq));
2318 int path_is_under(struct path *path1, struct path *path2)
2320 struct vfsmount *mnt = path1->mnt;
2321 struct dentry *dentry = path1->dentry;
2324 br_read_lock(vfsmount_lock);
2325 if (mnt != path2->mnt) {
2327 if (mnt->mnt_parent == mnt) {
2328 br_read_unlock(vfsmount_lock);
2331 if (mnt->mnt_parent == path2->mnt)
2333 mnt = mnt->mnt_parent;
2335 dentry = mnt->mnt_mountpoint;
2337 res = is_subdir(dentry, path2->dentry);
2338 br_read_unlock(vfsmount_lock);
2341 EXPORT_SYMBOL(path_is_under);
2343 void d_genocide(struct dentry *root)
2345 struct dentry *this_parent = root;
2346 struct list_head *next;
2348 spin_lock(&dcache_lock);
2350 next = this_parent->d_subdirs.next;
2352 while (next != &this_parent->d_subdirs) {
2353 struct list_head *tmp = next;
2354 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
2356 if (d_unhashed(dentry)||!dentry->d_inode)
2358 if (!list_empty(&dentry->d_subdirs)) {
2359 this_parent = dentry;
2362 atomic_dec(&dentry->d_count);
2364 if (this_parent != root) {
2365 next = this_parent->d_u.d_child.next;
2366 atomic_dec(&this_parent->d_count);
2367 this_parent = this_parent->d_parent;
2370 spin_unlock(&dcache_lock);
2374 * find_inode_number - check for dentry with name
2375 * @dir: directory to check
2376 * @name: Name to find.
2378 * Check whether a dentry already exists for the given name,
2379 * and return the inode number if it has an inode. Otherwise
2382 * This routine is used to post-process directory listings for
2383 * filesystems using synthetic inode numbers, and is necessary
2384 * to keep getcwd() working.
2387 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2389 struct dentry * dentry;
2392 dentry = d_hash_and_lookup(dir, name);
2394 if (dentry->d_inode)
2395 ino = dentry->d_inode->i_ino;
2400 EXPORT_SYMBOL(find_inode_number);
2402 static __initdata unsigned long dhash_entries;
2403 static int __init set_dhash_entries(char *str)
2407 dhash_entries = simple_strtoul(str, &str, 0);
2410 __setup("dhash_entries=", set_dhash_entries);
2412 static void __init dcache_init_early(void)
2416 /* If hashes are distributed across NUMA nodes, defer
2417 * hash allocation until vmalloc space is available.
2423 alloc_large_system_hash("Dentry cache",
2424 sizeof(struct hlist_head),
2432 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2433 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2436 static void __init dcache_init(void)
2440 percpu_counter_init(&nr_dentry, 0);
2441 percpu_counter_init(&nr_dentry_unused, 0);
2444 * A constructor could be added for stable state like the lists,
2445 * but it is probably not worth it because of the cache nature
2448 dentry_cache = KMEM_CACHE(dentry,
2449 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
2451 register_shrinker(&dcache_shrinker);
2453 /* Hash may have been set up in dcache_init_early */
2458 alloc_large_system_hash("Dentry cache",
2459 sizeof(struct hlist_head),
2467 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2468 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2471 /* SLAB cache for __getname() consumers */
2472 struct kmem_cache *names_cachep __read_mostly;
2473 EXPORT_SYMBOL(names_cachep);
2475 EXPORT_SYMBOL(d_genocide);
2477 void __init vfs_caches_init_early(void)
2479 dcache_init_early();
2483 void __init vfs_caches_init(unsigned long mempages)
2485 unsigned long reserve;
2487 /* Base hash sizes on available memory, with a reserve equal to
2488 150% of current kernel size */
2490 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2491 mempages -= reserve;
2493 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2494 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2498 files_init(mempages);