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_hash_lock protects:
41 * - the dcache hash table, s_anon lists
42 * dcache_lru_lock protects:
43 * - the dcache lru lists and counters
55 * if (dentry1 < dentry2)
59 int sysctl_vfs_cache_pressure __read_mostly = 100;
60 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
62 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_hash_lock);
63 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
64 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
65 __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
67 EXPORT_SYMBOL(dcache_lock);
69 static struct kmem_cache *dentry_cache __read_mostly;
71 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
74 * This is the single most critical data structure when it comes
75 * to the dcache: the hashtable for lookups. Somebody should try
76 * to make this good - I've just made it work.
78 * This hash-function tries to avoid losing too many bits of hash
79 * information, yet avoid using a prime hash-size or similar.
81 #define D_HASHBITS d_hash_shift
82 #define D_HASHMASK d_hash_mask
84 static unsigned int d_hash_mask __read_mostly;
85 static unsigned int d_hash_shift __read_mostly;
86 static struct hlist_head *dentry_hashtable __read_mostly;
88 /* Statistics gathering. */
89 struct dentry_stat_t dentry_stat = {
93 static DEFINE_PER_CPU(unsigned int, nr_dentry);
95 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
96 static int get_nr_dentry(void)
100 for_each_possible_cpu(i)
101 sum += per_cpu(nr_dentry, i);
102 return sum < 0 ? 0 : sum;
105 int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
106 size_t *lenp, loff_t *ppos)
108 dentry_stat.nr_dentry = get_nr_dentry();
109 return proc_dointvec(table, write, buffer, lenp, ppos);
113 static void __d_free(struct rcu_head *head)
115 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
117 WARN_ON(!list_empty(&dentry->d_alias));
118 if (dname_external(dentry))
119 kfree(dentry->d_name.name);
120 kmem_cache_free(dentry_cache, dentry);
124 * no dcache_lock, please.
126 static void d_free(struct dentry *dentry)
128 this_cpu_dec(nr_dentry);
129 if (dentry->d_op && dentry->d_op->d_release)
130 dentry->d_op->d_release(dentry);
132 /* if dentry was never inserted into hash, immediate free is OK */
133 if (hlist_unhashed(&dentry->d_hash))
134 __d_free(&dentry->d_u.d_rcu);
136 call_rcu(&dentry->d_u.d_rcu, __d_free);
140 * Release the dentry's inode, using the filesystem
141 * d_iput() operation if defined.
143 static void dentry_iput(struct dentry * dentry)
144 __releases(dentry->d_lock)
145 __releases(dcache_lock)
147 struct inode *inode = dentry->d_inode;
149 dentry->d_inode = NULL;
150 list_del_init(&dentry->d_alias);
151 spin_unlock(&dentry->d_lock);
152 spin_unlock(&dcache_lock);
154 fsnotify_inoderemove(inode);
155 if (dentry->d_op && dentry->d_op->d_iput)
156 dentry->d_op->d_iput(dentry, inode);
160 spin_unlock(&dentry->d_lock);
161 spin_unlock(&dcache_lock);
166 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
168 static void dentry_lru_add(struct dentry *dentry)
170 if (list_empty(&dentry->d_lru)) {
171 spin_lock(&dcache_lru_lock);
172 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
173 dentry->d_sb->s_nr_dentry_unused++;
174 dentry_stat.nr_unused++;
175 spin_unlock(&dcache_lru_lock);
179 static void __dentry_lru_del(struct dentry *dentry)
181 list_del_init(&dentry->d_lru);
182 dentry->d_sb->s_nr_dentry_unused--;
183 dentry_stat.nr_unused--;
186 static void dentry_lru_del(struct dentry *dentry)
188 if (!list_empty(&dentry->d_lru)) {
189 spin_lock(&dcache_lru_lock);
190 __dentry_lru_del(dentry);
191 spin_unlock(&dcache_lru_lock);
195 static void dentry_lru_move_tail(struct dentry *dentry)
197 spin_lock(&dcache_lru_lock);
198 if (list_empty(&dentry->d_lru)) {
199 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
200 dentry->d_sb->s_nr_dentry_unused++;
201 dentry_stat.nr_unused++;
203 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
205 spin_unlock(&dcache_lru_lock);
209 * d_kill - kill dentry and return parent
210 * @dentry: dentry to kill
212 * The dentry must already be unhashed and removed from the LRU.
214 * If this is the root of the dentry tree, return NULL.
216 * dcache_lock and d_lock must be held by caller, are dropped by d_kill.
218 static struct dentry *d_kill(struct dentry *dentry)
219 __releases(dentry->d_lock)
220 __releases(dcache_lock)
222 struct dentry *parent;
224 list_del(&dentry->d_u.d_child);
225 /*drops the locks, at that point nobody can reach this dentry */
230 parent = dentry->d_parent;
236 * d_drop - drop a dentry
237 * @dentry: dentry to drop
239 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
240 * be found through a VFS lookup any more. Note that this is different from
241 * deleting the dentry - d_delete will try to mark the dentry negative if
242 * possible, giving a successful _negative_ lookup, while d_drop will
243 * just make the cache lookup fail.
245 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
246 * reason (NFS timeouts or autofs deletes).
248 * __d_drop requires dentry->d_lock.
250 void __d_drop(struct dentry *dentry)
252 if (!(dentry->d_flags & DCACHE_UNHASHED)) {
253 dentry->d_flags |= DCACHE_UNHASHED;
254 spin_lock(&dcache_hash_lock);
255 hlist_del_rcu(&dentry->d_hash);
256 spin_unlock(&dcache_hash_lock);
259 EXPORT_SYMBOL(__d_drop);
261 void d_drop(struct dentry *dentry)
263 spin_lock(&dcache_lock);
264 spin_lock(&dentry->d_lock);
266 spin_unlock(&dentry->d_lock);
267 spin_unlock(&dcache_lock);
269 EXPORT_SYMBOL(d_drop);
274 * This is complicated by the fact that we do not want to put
275 * dentries that are no longer on any hash chain on the unused
276 * list: we'd much rather just get rid of them immediately.
278 * However, that implies that we have to traverse the dentry
279 * tree upwards to the parents which might _also_ now be
280 * scheduled for deletion (it may have been only waiting for
281 * its last child to go away).
283 * This tail recursion is done by hand as we don't want to depend
284 * on the compiler to always get this right (gcc generally doesn't).
285 * Real recursion would eat up our stack space.
289 * dput - release a dentry
290 * @dentry: dentry to release
292 * Release a dentry. This will drop the usage count and if appropriate
293 * call the dentry unlink method as well as removing it from the queues and
294 * releasing its resources. If the parent dentries were scheduled for release
295 * they too may now get deleted.
297 * no dcache lock, please.
300 void dput(struct dentry *dentry)
306 if (atomic_read(&dentry->d_count) == 1)
308 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
311 spin_lock(&dentry->d_lock);
312 if (atomic_read(&dentry->d_count)) {
313 spin_unlock(&dentry->d_lock);
314 spin_unlock(&dcache_lock);
319 * AV: ->d_delete() is _NOT_ allowed to block now.
321 if (dentry->d_op && dentry->d_op->d_delete) {
322 if (dentry->d_op->d_delete(dentry))
326 /* Unreachable? Get rid of it */
327 if (d_unhashed(dentry))
330 /* Otherwise leave it cached and ensure it's on the LRU */
331 dentry->d_flags |= DCACHE_REFERENCED;
332 dentry_lru_add(dentry);
334 spin_unlock(&dentry->d_lock);
335 spin_unlock(&dcache_lock);
341 /* if dentry was on the d_lru list delete it from there */
342 dentry_lru_del(dentry);
343 dentry = d_kill(dentry);
350 * d_invalidate - invalidate a dentry
351 * @dentry: dentry to invalidate
353 * Try to invalidate the dentry if it turns out to be
354 * possible. If there are other dentries that can be
355 * reached through this one we can't delete it and we
356 * return -EBUSY. On success we return 0.
361 int d_invalidate(struct dentry * dentry)
364 * If it's already been dropped, return OK.
366 spin_lock(&dcache_lock);
367 if (d_unhashed(dentry)) {
368 spin_unlock(&dcache_lock);
372 * Check whether to do a partial shrink_dcache
373 * to get rid of unused child entries.
375 if (!list_empty(&dentry->d_subdirs)) {
376 spin_unlock(&dcache_lock);
377 shrink_dcache_parent(dentry);
378 spin_lock(&dcache_lock);
382 * Somebody else still using it?
384 * If it's a directory, we can't drop it
385 * for fear of somebody re-populating it
386 * with children (even though dropping it
387 * would make it unreachable from the root,
388 * we might still populate it if it was a
389 * working directory or similar).
391 spin_lock(&dentry->d_lock);
392 if (atomic_read(&dentry->d_count) > 1) {
393 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
394 spin_unlock(&dentry->d_lock);
395 spin_unlock(&dcache_lock);
401 spin_unlock(&dentry->d_lock);
402 spin_unlock(&dcache_lock);
405 EXPORT_SYMBOL(d_invalidate);
407 /* This should be called _only_ with dcache_lock held */
408 static inline struct dentry * __dget_locked_dlock(struct dentry *dentry)
410 atomic_inc(&dentry->d_count);
411 dentry_lru_del(dentry);
415 static inline struct dentry * __dget_locked(struct dentry *dentry)
417 atomic_inc(&dentry->d_count);
418 spin_lock(&dentry->d_lock);
419 dentry_lru_del(dentry);
420 spin_unlock(&dentry->d_lock);
424 struct dentry * dget_locked(struct dentry *dentry)
426 return __dget_locked(dentry);
428 EXPORT_SYMBOL(dget_locked);
431 * d_find_alias - grab a hashed alias of inode
432 * @inode: inode in question
433 * @want_discon: flag, used by d_splice_alias, to request
434 * that only a DISCONNECTED alias be returned.
436 * If inode has a hashed alias, or is a directory and has any alias,
437 * acquire the reference to alias and return it. Otherwise return NULL.
438 * Notice that if inode is a directory there can be only one alias and
439 * it can be unhashed only if it has no children, or if it is the root
442 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
443 * any other hashed alias over that one unless @want_discon is set,
444 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
447 static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
449 struct list_head *head, *next, *tmp;
450 struct dentry *alias, *discon_alias=NULL;
452 head = &inode->i_dentry;
453 next = inode->i_dentry.next;
454 while (next != head) {
458 alias = list_entry(tmp, struct dentry, d_alias);
459 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
460 if (IS_ROOT(alias) &&
461 (alias->d_flags & DCACHE_DISCONNECTED))
462 discon_alias = alias;
463 else if (!want_discon) {
464 __dget_locked(alias);
470 __dget_locked(discon_alias);
474 struct dentry * d_find_alias(struct inode *inode)
476 struct dentry *de = NULL;
478 if (!list_empty(&inode->i_dentry)) {
479 spin_lock(&dcache_lock);
480 de = __d_find_alias(inode, 0);
481 spin_unlock(&dcache_lock);
485 EXPORT_SYMBOL(d_find_alias);
488 * Try to kill dentries associated with this inode.
489 * WARNING: you must own a reference to inode.
491 void d_prune_aliases(struct inode *inode)
493 struct dentry *dentry;
495 spin_lock(&dcache_lock);
496 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
497 spin_lock(&dentry->d_lock);
498 if (!atomic_read(&dentry->d_count)) {
499 __dget_locked_dlock(dentry);
501 spin_unlock(&dentry->d_lock);
502 spin_unlock(&dcache_lock);
506 spin_unlock(&dentry->d_lock);
508 spin_unlock(&dcache_lock);
510 EXPORT_SYMBOL(d_prune_aliases);
513 * Throw away a dentry - free the inode, dput the parent. This requires that
514 * the LRU list has already been removed.
516 * Try to prune ancestors as well. This is necessary to prevent
517 * quadratic behavior of shrink_dcache_parent(), but is also expected
518 * to be beneficial in reducing dentry cache fragmentation.
520 static void prune_one_dentry(struct dentry * dentry)
521 __releases(dentry->d_lock)
522 __releases(dcache_lock)
525 dentry = d_kill(dentry);
528 * Prune ancestors. Locking is simpler than in dput(),
529 * because dcache_lock needs to be taken anyway.
532 spin_lock(&dcache_lock);
533 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock)) {
534 spin_unlock(&dcache_lock);
538 dentry_lru_del(dentry);
540 dentry = d_kill(dentry);
544 static void shrink_dentry_list(struct list_head *list)
546 struct dentry *dentry;
548 while (!list_empty(list)) {
549 dentry = list_entry(list->prev, struct dentry, d_lru);
551 if (!spin_trylock(&dentry->d_lock)) {
552 spin_unlock(&dcache_lru_lock);
554 spin_lock(&dcache_lru_lock);
558 __dentry_lru_del(dentry);
561 * We found an inuse dentry which was not removed from
562 * the LRU because of laziness during lookup. Do not free
563 * it - just keep it off the LRU list.
565 if (atomic_read(&dentry->d_count)) {
566 spin_unlock(&dentry->d_lock);
569 spin_unlock(&dcache_lru_lock);
571 prune_one_dentry(dentry);
572 /* dcache_lock and dentry->d_lock dropped */
573 spin_lock(&dcache_lock);
574 spin_lock(&dcache_lru_lock);
579 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
580 * @sb: superblock to shrink dentry LRU.
581 * @count: number of entries to prune
582 * @flags: flags to control the dentry processing
584 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
586 static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
588 /* called from prune_dcache() and shrink_dcache_parent() */
589 struct dentry *dentry;
590 LIST_HEAD(referenced);
594 spin_lock(&dcache_lock);
596 spin_lock(&dcache_lru_lock);
597 while (!list_empty(&sb->s_dentry_lru)) {
598 dentry = list_entry(sb->s_dentry_lru.prev,
599 struct dentry, d_lru);
600 BUG_ON(dentry->d_sb != sb);
602 if (!spin_trylock(&dentry->d_lock)) {
603 spin_unlock(&dcache_lru_lock);
609 * If we are honouring the DCACHE_REFERENCED flag and the
610 * dentry has this flag set, don't free it. Clear the flag
611 * and put it back on the LRU.
613 if (flags & DCACHE_REFERENCED &&
614 dentry->d_flags & DCACHE_REFERENCED) {
615 dentry->d_flags &= ~DCACHE_REFERENCED;
616 list_move(&dentry->d_lru, &referenced);
617 spin_unlock(&dentry->d_lock);
619 list_move_tail(&dentry->d_lru, &tmp);
620 spin_unlock(&dentry->d_lock);
624 /* XXX: re-add cond_resched_lock when dcache_lock goes away */
628 shrink_dentry_list(&tmp);
630 if (!list_empty(&referenced))
631 list_splice(&referenced, &sb->s_dentry_lru);
632 spin_unlock(&dcache_lru_lock);
633 spin_unlock(&dcache_lock);
638 * prune_dcache - shrink the dcache
639 * @count: number of entries to try to free
641 * Shrink the dcache. This is done when we need more memory, or simply when we
642 * need to unmount something (at which point we need to unuse all dentries).
644 * This function may fail to free any resources if all the dentries are in use.
646 static void prune_dcache(int count)
648 struct super_block *sb, *p = NULL;
650 int unused = dentry_stat.nr_unused;
654 if (unused == 0 || count == 0)
656 spin_lock(&dcache_lock);
660 prune_ratio = unused / count;
662 list_for_each_entry(sb, &super_blocks, s_list) {
663 if (list_empty(&sb->s_instances))
665 if (sb->s_nr_dentry_unused == 0)
668 /* Now, we reclaim unused dentrins with fairness.
669 * We reclaim them same percentage from each superblock.
670 * We calculate number of dentries to scan on this sb
671 * as follows, but the implementation is arranged to avoid
673 * number of dentries to scan on this sb =
674 * count * (number of dentries on this sb /
675 * number of dentries in the machine)
677 spin_unlock(&sb_lock);
678 if (prune_ratio != 1)
679 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
681 w_count = sb->s_nr_dentry_unused;
684 * We need to be sure this filesystem isn't being unmounted,
685 * otherwise we could race with generic_shutdown_super(), and
686 * end up holding a reference to an inode while the filesystem
687 * is unmounted. So we try to get s_umount, and make sure
690 if (down_read_trylock(&sb->s_umount)) {
691 if ((sb->s_root != NULL) &&
692 (!list_empty(&sb->s_dentry_lru))) {
693 spin_unlock(&dcache_lock);
694 __shrink_dcache_sb(sb, &w_count,
697 spin_lock(&dcache_lock);
699 up_read(&sb->s_umount);
706 /* more work left to do? */
712 spin_unlock(&sb_lock);
713 spin_unlock(&dcache_lock);
717 * shrink_dcache_sb - shrink dcache for a superblock
720 * Shrink the dcache for the specified super block. This is used to free
721 * the dcache before unmounting a file system.
723 void shrink_dcache_sb(struct super_block *sb)
727 spin_lock(&dcache_lock);
728 spin_lock(&dcache_lru_lock);
729 while (!list_empty(&sb->s_dentry_lru)) {
730 list_splice_init(&sb->s_dentry_lru, &tmp);
731 shrink_dentry_list(&tmp);
733 spin_unlock(&dcache_lru_lock);
734 spin_unlock(&dcache_lock);
736 EXPORT_SYMBOL(shrink_dcache_sb);
739 * destroy a single subtree of dentries for unmount
740 * - see the comments on shrink_dcache_for_umount() for a description of the
743 static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
745 struct dentry *parent;
746 unsigned detached = 0;
748 BUG_ON(!IS_ROOT(dentry));
750 /* detach this root from the system */
751 spin_lock(&dcache_lock);
752 spin_lock(&dentry->d_lock);
753 dentry_lru_del(dentry);
754 spin_unlock(&dentry->d_lock);
756 spin_unlock(&dcache_lock);
759 /* descend to the first leaf in the current subtree */
760 while (!list_empty(&dentry->d_subdirs)) {
763 /* this is a branch with children - detach all of them
764 * from the system in one go */
765 spin_lock(&dcache_lock);
766 list_for_each_entry(loop, &dentry->d_subdirs,
768 spin_lock(&loop->d_lock);
769 dentry_lru_del(loop);
770 spin_unlock(&loop->d_lock);
772 cond_resched_lock(&dcache_lock);
774 spin_unlock(&dcache_lock);
776 /* move to the first child */
777 dentry = list_entry(dentry->d_subdirs.next,
778 struct dentry, d_u.d_child);
781 /* consume the dentries from this leaf up through its parents
782 * until we find one with children or run out altogether */
786 if (atomic_read(&dentry->d_count) != 0) {
788 "BUG: Dentry %p{i=%lx,n=%s}"
790 " [unmount of %s %s]\n",
793 dentry->d_inode->i_ino : 0UL,
795 atomic_read(&dentry->d_count),
796 dentry->d_sb->s_type->name,
804 parent = dentry->d_parent;
805 atomic_dec(&parent->d_count);
808 list_del(&dentry->d_u.d_child);
811 inode = dentry->d_inode;
813 dentry->d_inode = NULL;
814 list_del_init(&dentry->d_alias);
815 if (dentry->d_op && dentry->d_op->d_iput)
816 dentry->d_op->d_iput(dentry, inode);
823 /* finished when we fall off the top of the tree,
824 * otherwise we ascend to the parent and move to the
825 * next sibling if there is one */
829 } while (list_empty(&dentry->d_subdirs));
831 dentry = list_entry(dentry->d_subdirs.next,
832 struct dentry, d_u.d_child);
837 * destroy the dentries attached to a superblock on unmounting
838 * - we don't need to use dentry->d_lock, and only need dcache_lock when
839 * removing the dentry from the system lists and hashes because:
840 * - the superblock is detached from all mountings and open files, so the
841 * dentry trees will not be rearranged by the VFS
842 * - s_umount is write-locked, so the memory pressure shrinker will ignore
843 * any dentries belonging to this superblock that it comes across
844 * - the filesystem itself is no longer permitted to rearrange the dentries
847 void shrink_dcache_for_umount(struct super_block *sb)
849 struct dentry *dentry;
851 if (down_read_trylock(&sb->s_umount))
856 atomic_dec(&dentry->d_count);
857 shrink_dcache_for_umount_subtree(dentry);
859 while (!hlist_empty(&sb->s_anon)) {
860 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
861 shrink_dcache_for_umount_subtree(dentry);
866 * Search for at least 1 mount point in the dentry's subdirs.
867 * We descend to the next level whenever the d_subdirs
868 * list is non-empty and continue searching.
872 * have_submounts - check for mounts over a dentry
873 * @parent: dentry to check.
875 * Return true if the parent or its subdirectories contain
879 int have_submounts(struct dentry *parent)
881 struct dentry *this_parent = parent;
882 struct list_head *next;
884 spin_lock(&dcache_lock);
885 if (d_mountpoint(parent))
888 next = this_parent->d_subdirs.next;
890 while (next != &this_parent->d_subdirs) {
891 struct list_head *tmp = next;
892 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
894 /* Have we found a mount point ? */
895 if (d_mountpoint(dentry))
897 if (!list_empty(&dentry->d_subdirs)) {
898 this_parent = dentry;
903 * All done at this level ... ascend and resume the search.
905 if (this_parent != parent) {
906 next = this_parent->d_u.d_child.next;
907 this_parent = this_parent->d_parent;
910 spin_unlock(&dcache_lock);
911 return 0; /* No mount points found in tree */
913 spin_unlock(&dcache_lock);
916 EXPORT_SYMBOL(have_submounts);
919 * Search the dentry child list for the specified parent,
920 * and move any unused dentries to the end of the unused
921 * list for prune_dcache(). We descend to the next level
922 * whenever the d_subdirs list is non-empty and continue
925 * It returns zero iff there are no unused children,
926 * otherwise it returns the number of children moved to
927 * the end of the unused list. This may not be the total
928 * number of unused children, because select_parent can
929 * drop the lock and return early due to latency
932 static int select_parent(struct dentry * parent)
934 struct dentry *this_parent = parent;
935 struct list_head *next;
938 spin_lock(&dcache_lock);
940 next = this_parent->d_subdirs.next;
942 while (next != &this_parent->d_subdirs) {
943 struct list_head *tmp = next;
944 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
947 spin_lock(&dentry->d_lock);
950 * move only zero ref count dentries to the end
951 * of the unused list for prune_dcache
953 if (!atomic_read(&dentry->d_count)) {
954 dentry_lru_move_tail(dentry);
957 dentry_lru_del(dentry);
960 spin_unlock(&dentry->d_lock);
963 * We can return to the caller if we have found some (this
964 * ensures forward progress). We'll be coming back to find
967 if (found && need_resched())
971 * Descend a level if the d_subdirs list is non-empty.
973 if (!list_empty(&dentry->d_subdirs)) {
974 this_parent = dentry;
979 * All done at this level ... ascend and resume the search.
981 if (this_parent != parent) {
982 next = this_parent->d_u.d_child.next;
983 this_parent = this_parent->d_parent;
987 spin_unlock(&dcache_lock);
992 * shrink_dcache_parent - prune dcache
993 * @parent: parent of entries to prune
995 * Prune the dcache to remove unused children of the parent dentry.
998 void shrink_dcache_parent(struct dentry * parent)
1000 struct super_block *sb = parent->d_sb;
1003 while ((found = select_parent(parent)) != 0)
1004 __shrink_dcache_sb(sb, &found, 0);
1006 EXPORT_SYMBOL(shrink_dcache_parent);
1009 * Scan `nr' dentries and return the number which remain.
1011 * We need to avoid reentering the filesystem if the caller is performing a
1012 * GFP_NOFS allocation attempt. One example deadlock is:
1014 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1015 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1016 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1018 * In this case we return -1 to tell the caller that we baled.
1020 static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
1023 if (!(gfp_mask & __GFP_FS))
1028 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
1031 static struct shrinker dcache_shrinker = {
1032 .shrink = shrink_dcache_memory,
1033 .seeks = DEFAULT_SEEKS,
1037 * d_alloc - allocate a dcache entry
1038 * @parent: parent of entry to allocate
1039 * @name: qstr of the name
1041 * Allocates a dentry. It returns %NULL if there is insufficient memory
1042 * available. On a success the dentry is returned. The name passed in is
1043 * copied and the copy passed in may be reused after this call.
1046 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1048 struct dentry *dentry;
1051 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
1055 if (name->len > DNAME_INLINE_LEN-1) {
1056 dname = kmalloc(name->len + 1, GFP_KERNEL);
1058 kmem_cache_free(dentry_cache, dentry);
1062 dname = dentry->d_iname;
1064 dentry->d_name.name = dname;
1066 dentry->d_name.len = name->len;
1067 dentry->d_name.hash = name->hash;
1068 memcpy(dname, name->name, name->len);
1069 dname[name->len] = 0;
1071 atomic_set(&dentry->d_count, 1);
1072 dentry->d_flags = DCACHE_UNHASHED;
1073 spin_lock_init(&dentry->d_lock);
1074 dentry->d_inode = NULL;
1075 dentry->d_parent = NULL;
1076 dentry->d_sb = NULL;
1077 dentry->d_op = NULL;
1078 dentry->d_fsdata = NULL;
1079 dentry->d_mounted = 0;
1080 INIT_HLIST_NODE(&dentry->d_hash);
1081 INIT_LIST_HEAD(&dentry->d_lru);
1082 INIT_LIST_HEAD(&dentry->d_subdirs);
1083 INIT_LIST_HEAD(&dentry->d_alias);
1086 dentry->d_parent = dget(parent);
1087 dentry->d_sb = parent->d_sb;
1089 INIT_LIST_HEAD(&dentry->d_u.d_child);
1092 spin_lock(&dcache_lock);
1094 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
1095 spin_unlock(&dcache_lock);
1097 this_cpu_inc(nr_dentry);
1101 EXPORT_SYMBOL(d_alloc);
1103 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1108 q.len = strlen(name);
1109 q.hash = full_name_hash(q.name, q.len);
1110 return d_alloc(parent, &q);
1112 EXPORT_SYMBOL(d_alloc_name);
1114 /* the caller must hold dcache_lock */
1115 static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1118 list_add(&dentry->d_alias, &inode->i_dentry);
1119 dentry->d_inode = inode;
1120 fsnotify_d_instantiate(dentry, inode);
1124 * d_instantiate - fill in inode information for a dentry
1125 * @entry: dentry to complete
1126 * @inode: inode to attach to this dentry
1128 * Fill in inode information in the entry.
1130 * This turns negative dentries into productive full members
1133 * NOTE! This assumes that the inode count has been incremented
1134 * (or otherwise set) by the caller to indicate that it is now
1135 * in use by the dcache.
1138 void d_instantiate(struct dentry *entry, struct inode * inode)
1140 BUG_ON(!list_empty(&entry->d_alias));
1141 spin_lock(&dcache_lock);
1142 __d_instantiate(entry, inode);
1143 spin_unlock(&dcache_lock);
1144 security_d_instantiate(entry, inode);
1146 EXPORT_SYMBOL(d_instantiate);
1149 * d_instantiate_unique - instantiate a non-aliased dentry
1150 * @entry: dentry to instantiate
1151 * @inode: inode to attach to this dentry
1153 * Fill in inode information in the entry. On success, it returns NULL.
1154 * If an unhashed alias of "entry" already exists, then we return the
1155 * aliased dentry instead and drop one reference to inode.
1157 * Note that in order to avoid conflicts with rename() etc, the caller
1158 * had better be holding the parent directory semaphore.
1160 * This also assumes that the inode count has been incremented
1161 * (or otherwise set) by the caller to indicate that it is now
1162 * in use by the dcache.
1164 static struct dentry *__d_instantiate_unique(struct dentry *entry,
1165 struct inode *inode)
1167 struct dentry *alias;
1168 int len = entry->d_name.len;
1169 const char *name = entry->d_name.name;
1170 unsigned int hash = entry->d_name.hash;
1173 __d_instantiate(entry, NULL);
1177 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1178 struct qstr *qstr = &alias->d_name;
1180 if (qstr->hash != hash)
1182 if (alias->d_parent != entry->d_parent)
1184 if (qstr->len != len)
1186 if (memcmp(qstr->name, name, len))
1192 __d_instantiate(entry, inode);
1196 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1198 struct dentry *result;
1200 BUG_ON(!list_empty(&entry->d_alias));
1202 spin_lock(&dcache_lock);
1203 result = __d_instantiate_unique(entry, inode);
1204 spin_unlock(&dcache_lock);
1207 security_d_instantiate(entry, inode);
1211 BUG_ON(!d_unhashed(result));
1216 EXPORT_SYMBOL(d_instantiate_unique);
1219 * d_alloc_root - allocate root dentry
1220 * @root_inode: inode to allocate the root for
1222 * Allocate a root ("/") dentry for the inode given. The inode is
1223 * instantiated and returned. %NULL is returned if there is insufficient
1224 * memory or the inode passed is %NULL.
1227 struct dentry * d_alloc_root(struct inode * root_inode)
1229 struct dentry *res = NULL;
1232 static const struct qstr name = { .name = "/", .len = 1 };
1234 res = d_alloc(NULL, &name);
1236 res->d_sb = root_inode->i_sb;
1237 res->d_parent = res;
1238 d_instantiate(res, root_inode);
1243 EXPORT_SYMBOL(d_alloc_root);
1245 static inline struct hlist_head *d_hash(struct dentry *parent,
1248 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1249 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1250 return dentry_hashtable + (hash & D_HASHMASK);
1254 * d_obtain_alias - find or allocate a dentry for a given inode
1255 * @inode: inode to allocate the dentry for
1257 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1258 * similar open by handle operations. The returned dentry may be anonymous,
1259 * or may have a full name (if the inode was already in the cache).
1261 * When called on a directory inode, we must ensure that the inode only ever
1262 * has one dentry. If a dentry is found, that is returned instead of
1263 * allocating a new one.
1265 * On successful return, the reference to the inode has been transferred
1266 * to the dentry. In case of an error the reference on the inode is released.
1267 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1268 * be passed in and will be the error will be propagate to the return value,
1269 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1271 struct dentry *d_obtain_alias(struct inode *inode)
1273 static const struct qstr anonstring = { .name = "" };
1278 return ERR_PTR(-ESTALE);
1280 return ERR_CAST(inode);
1282 res = d_find_alias(inode);
1286 tmp = d_alloc(NULL, &anonstring);
1288 res = ERR_PTR(-ENOMEM);
1291 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1293 spin_lock(&dcache_lock);
1294 res = __d_find_alias(inode, 0);
1296 spin_unlock(&dcache_lock);
1301 /* attach a disconnected dentry */
1302 spin_lock(&tmp->d_lock);
1303 tmp->d_sb = inode->i_sb;
1304 tmp->d_inode = inode;
1305 tmp->d_flags |= DCACHE_DISCONNECTED;
1306 tmp->d_flags &= ~DCACHE_UNHASHED;
1307 list_add(&tmp->d_alias, &inode->i_dentry);
1308 spin_lock(&dcache_hash_lock);
1309 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1310 spin_unlock(&dcache_hash_lock);
1311 spin_unlock(&tmp->d_lock);
1313 spin_unlock(&dcache_lock);
1320 EXPORT_SYMBOL(d_obtain_alias);
1323 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1324 * @inode: the inode which may have a disconnected dentry
1325 * @dentry: a negative dentry which we want to point to the inode.
1327 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1328 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1329 * and return it, else simply d_add the inode to the dentry and return NULL.
1331 * This is needed in the lookup routine of any filesystem that is exportable
1332 * (via knfsd) so that we can build dcache paths to directories effectively.
1334 * If a dentry was found and moved, then it is returned. Otherwise NULL
1335 * is returned. This matches the expected return value of ->lookup.
1338 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1340 struct dentry *new = NULL;
1342 if (inode && S_ISDIR(inode->i_mode)) {
1343 spin_lock(&dcache_lock);
1344 new = __d_find_alias(inode, 1);
1346 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1347 spin_unlock(&dcache_lock);
1348 security_d_instantiate(new, inode);
1349 d_move(new, dentry);
1352 /* already taking dcache_lock, so d_add() by hand */
1353 __d_instantiate(dentry, inode);
1354 spin_unlock(&dcache_lock);
1355 security_d_instantiate(dentry, inode);
1359 d_add(dentry, inode);
1362 EXPORT_SYMBOL(d_splice_alias);
1365 * d_add_ci - lookup or allocate new dentry with case-exact name
1366 * @inode: the inode case-insensitive lookup has found
1367 * @dentry: the negative dentry that was passed to the parent's lookup func
1368 * @name: the case-exact name to be associated with the returned dentry
1370 * This is to avoid filling the dcache with case-insensitive names to the
1371 * same inode, only the actual correct case is stored in the dcache for
1372 * case-insensitive filesystems.
1374 * For a case-insensitive lookup match and if the the case-exact dentry
1375 * already exists in in the dcache, use it and return it.
1377 * If no entry exists with the exact case name, allocate new dentry with
1378 * the exact case, and return the spliced entry.
1380 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
1384 struct dentry *found;
1388 * First check if a dentry matching the name already exists,
1389 * if not go ahead and create it now.
1391 found = d_hash_and_lookup(dentry->d_parent, name);
1393 new = d_alloc(dentry->d_parent, name);
1399 found = d_splice_alias(inode, new);
1408 * If a matching dentry exists, and it's not negative use it.
1410 * Decrement the reference count to balance the iget() done
1413 if (found->d_inode) {
1414 if (unlikely(found->d_inode != inode)) {
1415 /* This can't happen because bad inodes are unhashed. */
1416 BUG_ON(!is_bad_inode(inode));
1417 BUG_ON(!is_bad_inode(found->d_inode));
1424 * Negative dentry: instantiate it unless the inode is a directory and
1425 * already has a dentry.
1427 spin_lock(&dcache_lock);
1428 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
1429 __d_instantiate(found, inode);
1430 spin_unlock(&dcache_lock);
1431 security_d_instantiate(found, inode);
1436 * In case a directory already has a (disconnected) entry grab a
1437 * reference to it, move it in place and use it.
1439 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1441 spin_unlock(&dcache_lock);
1442 security_d_instantiate(found, inode);
1450 return ERR_PTR(error);
1452 EXPORT_SYMBOL(d_add_ci);
1455 * d_lookup - search for a dentry
1456 * @parent: parent dentry
1457 * @name: qstr of name we wish to find
1458 * Returns: dentry, or NULL
1460 * d_lookup searches the children of the parent dentry for the name in
1461 * question. If the dentry is found its reference count is incremented and the
1462 * dentry is returned. The caller must use dput to free the entry when it has
1463 * finished using it. %NULL is returned if the dentry does not exist.
1465 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1467 struct dentry * dentry = NULL;
1471 seq = read_seqbegin(&rename_lock);
1472 dentry = __d_lookup(parent, name);
1475 } while (read_seqretry(&rename_lock, seq));
1478 EXPORT_SYMBOL(d_lookup);
1481 * __d_lookup - search for a dentry (racy)
1482 * @parent: parent dentry
1483 * @name: qstr of name we wish to find
1484 * Returns: dentry, or NULL
1486 * __d_lookup is like d_lookup, however it may (rarely) return a
1487 * false-negative result due to unrelated rename activity.
1489 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1490 * however it must be used carefully, eg. with a following d_lookup in
1491 * the case of failure.
1493 * __d_lookup callers must be commented.
1495 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1497 unsigned int len = name->len;
1498 unsigned int hash = name->hash;
1499 const unsigned char *str = name->name;
1500 struct hlist_head *head = d_hash(parent,hash);
1501 struct dentry *found = NULL;
1502 struct hlist_node *node;
1503 struct dentry *dentry;
1506 * The hash list is protected using RCU.
1508 * Take d_lock when comparing a candidate dentry, to avoid races
1511 * It is possible that concurrent renames can mess up our list
1512 * walk here and result in missing our dentry, resulting in the
1513 * false-negative result. d_lookup() protects against concurrent
1514 * renames using rename_lock seqlock.
1516 * See Documentation/vfs/dcache-locking.txt for more details.
1520 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1523 if (dentry->d_name.hash != hash)
1525 if (dentry->d_parent != parent)
1528 spin_lock(&dentry->d_lock);
1531 * Recheck the dentry after taking the lock - d_move may have
1532 * changed things. Don't bother checking the hash because
1533 * we're about to compare the whole name anyway.
1535 if (dentry->d_parent != parent)
1538 /* non-existing due to RCU? */
1539 if (d_unhashed(dentry))
1543 * It is safe to compare names since d_move() cannot
1544 * change the qstr (protected by d_lock).
1546 qstr = &dentry->d_name;
1547 if (parent->d_op && parent->d_op->d_compare) {
1548 if (parent->d_op->d_compare(parent, parent->d_inode,
1549 dentry, dentry->d_inode,
1550 qstr->len, qstr->name, name))
1553 if (qstr->len != len)
1555 if (memcmp(qstr->name, str, len))
1559 atomic_inc(&dentry->d_count);
1561 spin_unlock(&dentry->d_lock);
1564 spin_unlock(&dentry->d_lock);
1572 * d_hash_and_lookup - hash the qstr then search for a dentry
1573 * @dir: Directory to search in
1574 * @name: qstr of name we wish to find
1576 * On hash failure or on lookup failure NULL is returned.
1578 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1580 struct dentry *dentry = NULL;
1583 * Check for a fs-specific hash function. Note that we must
1584 * calculate the standard hash first, as the d_op->d_hash()
1585 * routine may choose to leave the hash value unchanged.
1587 name->hash = full_name_hash(name->name, name->len);
1588 if (dir->d_op && dir->d_op->d_hash) {
1589 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
1592 dentry = d_lookup(dir, name);
1598 * d_validate - verify dentry provided from insecure source (deprecated)
1599 * @dentry: The dentry alleged to be valid child of @dparent
1600 * @dparent: The parent dentry (known to be valid)
1602 * An insecure source has sent us a dentry, here we verify it and dget() it.
1603 * This is used by ncpfs in its readdir implementation.
1604 * Zero is returned in the dentry is invalid.
1606 * This function is slow for big directories, and deprecated, do not use it.
1608 int d_validate(struct dentry *dentry, struct dentry *dparent)
1610 struct dentry *child;
1612 spin_lock(&dcache_lock);
1613 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1614 if (dentry == child) {
1615 __dget_locked(dentry);
1616 spin_unlock(&dcache_lock);
1620 spin_unlock(&dcache_lock);
1624 EXPORT_SYMBOL(d_validate);
1627 * When a file is deleted, we have two options:
1628 * - turn this dentry into a negative dentry
1629 * - unhash this dentry and free it.
1631 * Usually, we want to just turn this into
1632 * a negative dentry, but if anybody else is
1633 * currently using the dentry or the inode
1634 * we can't do that and we fall back on removing
1635 * it from the hash queues and waiting for
1636 * it to be deleted later when it has no users
1640 * d_delete - delete a dentry
1641 * @dentry: The dentry to delete
1643 * Turn the dentry into a negative dentry if possible, otherwise
1644 * remove it from the hash queues so it can be deleted later
1647 void d_delete(struct dentry * dentry)
1651 * Are we the only user?
1653 spin_lock(&dcache_lock);
1654 spin_lock(&dentry->d_lock);
1655 isdir = S_ISDIR(dentry->d_inode->i_mode);
1656 if (atomic_read(&dentry->d_count) == 1) {
1657 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
1658 dentry_iput(dentry);
1659 fsnotify_nameremove(dentry, isdir);
1663 if (!d_unhashed(dentry))
1666 spin_unlock(&dentry->d_lock);
1667 spin_unlock(&dcache_lock);
1669 fsnotify_nameremove(dentry, isdir);
1671 EXPORT_SYMBOL(d_delete);
1673 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1676 entry->d_flags &= ~DCACHE_UNHASHED;
1677 hlist_add_head_rcu(&entry->d_hash, list);
1680 static void _d_rehash(struct dentry * entry)
1682 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1686 * d_rehash - add an entry back to the hash
1687 * @entry: dentry to add to the hash
1689 * Adds a dentry to the hash according to its name.
1692 void d_rehash(struct dentry * entry)
1694 spin_lock(&dcache_lock);
1695 spin_lock(&entry->d_lock);
1696 spin_lock(&dcache_hash_lock);
1698 spin_unlock(&dcache_hash_lock);
1699 spin_unlock(&entry->d_lock);
1700 spin_unlock(&dcache_lock);
1702 EXPORT_SYMBOL(d_rehash);
1705 * dentry_update_name_case - update case insensitive dentry with a new name
1706 * @dentry: dentry to be updated
1709 * Update a case insensitive dentry with new case of name.
1711 * dentry must have been returned by d_lookup with name @name. Old and new
1712 * name lengths must match (ie. no d_compare which allows mismatched name
1715 * Parent inode i_mutex must be held over d_lookup and into this call (to
1716 * keep renames and concurrent inserts, and readdir(2) away).
1718 void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
1720 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
1721 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
1723 spin_lock(&dcache_lock);
1724 spin_lock(&dentry->d_lock);
1725 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
1726 spin_unlock(&dentry->d_lock);
1727 spin_unlock(&dcache_lock);
1729 EXPORT_SYMBOL(dentry_update_name_case);
1732 * When switching names, the actual string doesn't strictly have to
1733 * be preserved in the target - because we're dropping the target
1734 * anyway. As such, we can just do a simple memcpy() to copy over
1735 * the new name before we switch.
1737 * Note that we have to be a lot more careful about getting the hash
1738 * switched - we have to switch the hash value properly even if it
1739 * then no longer matches the actual (corrupted) string of the target.
1740 * The hash value has to match the hash queue that the dentry is on..
1742 static void switch_names(struct dentry *dentry, struct dentry *target)
1744 if (dname_external(target)) {
1745 if (dname_external(dentry)) {
1747 * Both external: swap the pointers
1749 swap(target->d_name.name, dentry->d_name.name);
1752 * dentry:internal, target:external. Steal target's
1753 * storage and make target internal.
1755 memcpy(target->d_iname, dentry->d_name.name,
1756 dentry->d_name.len + 1);
1757 dentry->d_name.name = target->d_name.name;
1758 target->d_name.name = target->d_iname;
1761 if (dname_external(dentry)) {
1763 * dentry:external, target:internal. Give dentry's
1764 * storage to target and make dentry internal
1766 memcpy(dentry->d_iname, target->d_name.name,
1767 target->d_name.len + 1);
1768 target->d_name.name = dentry->d_name.name;
1769 dentry->d_name.name = dentry->d_iname;
1772 * Both are internal. Just copy target to dentry
1774 memcpy(dentry->d_iname, target->d_name.name,
1775 target->d_name.len + 1);
1776 dentry->d_name.len = target->d_name.len;
1780 swap(dentry->d_name.len, target->d_name.len);
1784 * We cannibalize "target" when moving dentry on top of it,
1785 * because it's going to be thrown away anyway. We could be more
1786 * polite about it, though.
1788 * This forceful removal will result in ugly /proc output if
1789 * somebody holds a file open that got deleted due to a rename.
1790 * We could be nicer about the deleted file, and let it show
1791 * up under the name it had before it was deleted rather than
1792 * under the original name of the file that was moved on top of it.
1796 * d_move_locked - move a dentry
1797 * @dentry: entry to move
1798 * @target: new dentry
1800 * Update the dcache to reflect the move of a file name. Negative
1801 * dcache entries should not be moved in this way.
1803 static void d_move_locked(struct dentry * dentry, struct dentry * target)
1805 if (!dentry->d_inode)
1806 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1808 write_seqlock(&rename_lock);
1810 * XXXX: do we really need to take target->d_lock?
1812 if (target < dentry) {
1813 spin_lock(&target->d_lock);
1814 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1816 spin_lock(&dentry->d_lock);
1817 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
1820 /* Move the dentry to the target hash queue, if on different bucket */
1821 spin_lock(&dcache_hash_lock);
1822 if (!d_unhashed(dentry))
1823 hlist_del_rcu(&dentry->d_hash);
1824 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
1825 spin_unlock(&dcache_hash_lock);
1827 /* Unhash the target: dput() will then get rid of it */
1830 list_del(&dentry->d_u.d_child);
1831 list_del(&target->d_u.d_child);
1833 /* Switch the names.. */
1834 switch_names(dentry, target);
1835 swap(dentry->d_name.hash, target->d_name.hash);
1837 /* ... and switch the parents */
1838 if (IS_ROOT(dentry)) {
1839 dentry->d_parent = target->d_parent;
1840 target->d_parent = target;
1841 INIT_LIST_HEAD(&target->d_u.d_child);
1843 swap(dentry->d_parent, target->d_parent);
1845 /* And add them back to the (new) parent lists */
1846 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1849 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1850 spin_unlock(&target->d_lock);
1851 fsnotify_d_move(dentry);
1852 spin_unlock(&dentry->d_lock);
1853 write_sequnlock(&rename_lock);
1857 * d_move - move a dentry
1858 * @dentry: entry to move
1859 * @target: new dentry
1861 * Update the dcache to reflect the move of a file name. Negative
1862 * dcache entries should not be moved in this way.
1865 void d_move(struct dentry * dentry, struct dentry * target)
1867 spin_lock(&dcache_lock);
1868 d_move_locked(dentry, target);
1869 spin_unlock(&dcache_lock);
1871 EXPORT_SYMBOL(d_move);
1874 * d_ancestor - search for an ancestor
1875 * @p1: ancestor dentry
1878 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1879 * an ancestor of p2, else NULL.
1881 struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
1885 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
1886 if (p->d_parent == p1)
1893 * This helper attempts to cope with remotely renamed directories
1895 * It assumes that the caller is already holding
1896 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1898 * Note: If ever the locking in lock_rename() changes, then please
1899 * remember to update this too...
1901 static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1902 __releases(dcache_lock)
1904 struct mutex *m1 = NULL, *m2 = NULL;
1907 /* If alias and dentry share a parent, then no extra locks required */
1908 if (alias->d_parent == dentry->d_parent)
1911 /* Check for loops */
1912 ret = ERR_PTR(-ELOOP);
1913 if (d_ancestor(alias, dentry))
1916 /* See lock_rename() */
1917 ret = ERR_PTR(-EBUSY);
1918 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1920 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1921 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1923 m2 = &alias->d_parent->d_inode->i_mutex;
1925 d_move_locked(alias, dentry);
1928 spin_unlock(&dcache_lock);
1937 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1938 * named dentry in place of the dentry to be replaced.
1940 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1942 struct dentry *dparent, *aparent;
1944 switch_names(dentry, anon);
1945 swap(dentry->d_name.hash, anon->d_name.hash);
1947 dparent = dentry->d_parent;
1948 aparent = anon->d_parent;
1950 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1951 list_del(&dentry->d_u.d_child);
1952 if (!IS_ROOT(dentry))
1953 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1955 INIT_LIST_HEAD(&dentry->d_u.d_child);
1957 anon->d_parent = (dparent == dentry) ? anon : dparent;
1958 list_del(&anon->d_u.d_child);
1960 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1962 INIT_LIST_HEAD(&anon->d_u.d_child);
1964 anon->d_flags &= ~DCACHE_DISCONNECTED;
1968 * d_materialise_unique - introduce an inode into the tree
1969 * @dentry: candidate dentry
1970 * @inode: inode to bind to the dentry, to which aliases may be attached
1972 * Introduces an dentry into the tree, substituting an extant disconnected
1973 * root directory alias in its place if there is one
1975 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1977 struct dentry *actual;
1979 BUG_ON(!d_unhashed(dentry));
1981 spin_lock(&dcache_lock);
1985 __d_instantiate(dentry, NULL);
1989 if (S_ISDIR(inode->i_mode)) {
1990 struct dentry *alias;
1992 /* Does an aliased dentry already exist? */
1993 alias = __d_find_alias(inode, 0);
1996 /* Is this an anonymous mountpoint that we could splice
1998 if (IS_ROOT(alias)) {
1999 spin_lock(&alias->d_lock);
2000 __d_materialise_dentry(dentry, alias);
2004 /* Nope, but we must(!) avoid directory aliasing */
2005 actual = __d_unalias(dentry, alias);
2012 /* Add a unique reference */
2013 actual = __d_instantiate_unique(dentry, inode);
2016 else if (unlikely(!d_unhashed(actual)))
2017 goto shouldnt_be_hashed;
2020 spin_lock(&actual->d_lock);
2022 spin_lock(&dcache_hash_lock);
2024 spin_unlock(&dcache_hash_lock);
2025 spin_unlock(&actual->d_lock);
2026 spin_unlock(&dcache_lock);
2028 if (actual == dentry) {
2029 security_d_instantiate(dentry, inode);
2037 spin_unlock(&dcache_lock);
2040 EXPORT_SYMBOL_GPL(d_materialise_unique);
2042 static int prepend(char **buffer, int *buflen, const char *str, int namelen)
2046 return -ENAMETOOLONG;
2048 memcpy(*buffer, str, namelen);
2052 static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2054 return prepend(buffer, buflen, name->name, name->len);
2058 * Prepend path string to a buffer
2060 * @path: the dentry/vfsmount to report
2061 * @root: root vfsmnt/dentry (may be modified by this function)
2062 * @buffer: pointer to the end of the buffer
2063 * @buflen: pointer to buffer length
2065 * Caller holds the dcache_lock.
2067 * If path is not reachable from the supplied root, then the value of
2068 * root is changed (without modifying refcounts).
2070 static int prepend_path(const struct path *path, struct path *root,
2071 char **buffer, int *buflen)
2073 struct dentry *dentry = path->dentry;
2074 struct vfsmount *vfsmnt = path->mnt;
2078 br_read_lock(vfsmount_lock);
2079 while (dentry != root->dentry || vfsmnt != root->mnt) {
2080 struct dentry * parent;
2082 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2084 if (vfsmnt->mnt_parent == vfsmnt) {
2087 dentry = vfsmnt->mnt_mountpoint;
2088 vfsmnt = vfsmnt->mnt_parent;
2091 parent = dentry->d_parent;
2093 error = prepend_name(buffer, buflen, &dentry->d_name);
2095 error = prepend(buffer, buflen, "/", 1);
2104 if (!error && !slash)
2105 error = prepend(buffer, buflen, "/", 1);
2107 br_read_unlock(vfsmount_lock);
2112 * Filesystems needing to implement special "root names"
2113 * should do so with ->d_dname()
2115 if (IS_ROOT(dentry) &&
2116 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2117 WARN(1, "Root dentry has weird name <%.*s>\n",
2118 (int) dentry->d_name.len, dentry->d_name.name);
2121 root->dentry = dentry;
2126 * __d_path - return the path of a dentry
2127 * @path: the dentry/vfsmount to report
2128 * @root: root vfsmnt/dentry (may be modified by this function)
2129 * @buf: buffer to return value in
2130 * @buflen: buffer length
2132 * Convert a dentry into an ASCII path name.
2134 * Returns a pointer into the buffer or an error code if the
2135 * path was too long.
2137 * "buflen" should be positive.
2139 * If path is not reachable from the supplied root, then the value of
2140 * root is changed (without modifying refcounts).
2142 char *__d_path(const struct path *path, struct path *root,
2143 char *buf, int buflen)
2145 char *res = buf + buflen;
2148 prepend(&res, &buflen, "\0", 1);
2149 spin_lock(&dcache_lock);
2150 error = prepend_path(path, root, &res, &buflen);
2151 spin_unlock(&dcache_lock);
2154 return ERR_PTR(error);
2159 * same as __d_path but appends "(deleted)" for unlinked files.
2161 static int path_with_deleted(const struct path *path, struct path *root,
2162 char **buf, int *buflen)
2164 prepend(buf, buflen, "\0", 1);
2165 if (d_unlinked(path->dentry)) {
2166 int error = prepend(buf, buflen, " (deleted)", 10);
2171 return prepend_path(path, root, buf, buflen);
2174 static int prepend_unreachable(char **buffer, int *buflen)
2176 return prepend(buffer, buflen, "(unreachable)", 13);
2180 * d_path - return the path of a dentry
2181 * @path: path to report
2182 * @buf: buffer to return value in
2183 * @buflen: buffer length
2185 * Convert a dentry into an ASCII path name. If the entry has been deleted
2186 * the string " (deleted)" is appended. Note that this is ambiguous.
2188 * Returns a pointer into the buffer or an error code if the path was
2189 * too long. Note: Callers should use the returned pointer, not the passed
2190 * in buffer, to use the name! The implementation often starts at an offset
2191 * into the buffer, and may leave 0 bytes at the start.
2193 * "buflen" should be positive.
2195 char *d_path(const struct path *path, char *buf, int buflen)
2197 char *res = buf + buflen;
2203 * We have various synthetic filesystems that never get mounted. On
2204 * these filesystems dentries are never used for lookup purposes, and
2205 * thus don't need to be hashed. They also don't need a name until a
2206 * user wants to identify the object in /proc/pid/fd/. The little hack
2207 * below allows us to generate a name for these objects on demand:
2209 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2210 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2212 get_fs_root(current->fs, &root);
2213 spin_lock(&dcache_lock);
2215 error = path_with_deleted(path, &tmp, &res, &buflen);
2217 res = ERR_PTR(error);
2218 spin_unlock(&dcache_lock);
2222 EXPORT_SYMBOL(d_path);
2225 * d_path_with_unreachable - return the path of a dentry
2226 * @path: path to report
2227 * @buf: buffer to return value in
2228 * @buflen: buffer length
2230 * The difference from d_path() is that this prepends "(unreachable)"
2231 * to paths which are unreachable from the current process' root.
2233 char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2235 char *res = buf + buflen;
2240 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2241 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2243 get_fs_root(current->fs, &root);
2244 spin_lock(&dcache_lock);
2246 error = path_with_deleted(path, &tmp, &res, &buflen);
2247 if (!error && !path_equal(&tmp, &root))
2248 error = prepend_unreachable(&res, &buflen);
2249 spin_unlock(&dcache_lock);
2252 res = ERR_PTR(error);
2258 * Helper function for dentry_operations.d_dname() members
2260 char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2261 const char *fmt, ...)
2267 va_start(args, fmt);
2268 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2271 if (sz > sizeof(temp) || sz > buflen)
2272 return ERR_PTR(-ENAMETOOLONG);
2274 buffer += buflen - sz;
2275 return memcpy(buffer, temp, sz);
2279 * Write full pathname from the root of the filesystem into the buffer.
2281 static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
2283 char *end = buf + buflen;
2286 prepend(&end, &buflen, "\0", 1);
2293 while (!IS_ROOT(dentry)) {
2294 struct dentry *parent = dentry->d_parent;
2297 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
2298 (prepend(&end, &buflen, "/", 1) != 0))
2306 return ERR_PTR(-ENAMETOOLONG);
2309 char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2313 spin_lock(&dcache_lock);
2314 retval = __dentry_path(dentry, buf, buflen);
2315 spin_unlock(&dcache_lock);
2319 EXPORT_SYMBOL(dentry_path_raw);
2321 char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2326 spin_lock(&dcache_lock);
2327 if (d_unlinked(dentry)) {
2329 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2333 retval = __dentry_path(dentry, buf, buflen);
2334 spin_unlock(&dcache_lock);
2335 if (!IS_ERR(retval) && p)
2336 *p = '/'; /* restore '/' overriden with '\0' */
2339 spin_unlock(&dcache_lock);
2340 return ERR_PTR(-ENAMETOOLONG);
2344 * NOTE! The user-level library version returns a
2345 * character pointer. The kernel system call just
2346 * returns the length of the buffer filled (which
2347 * includes the ending '\0' character), or a negative
2348 * error value. So libc would do something like
2350 * char *getcwd(char * buf, size_t size)
2354 * retval = sys_getcwd(buf, size);
2361 SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
2364 struct path pwd, root;
2365 char *page = (char *) __get_free_page(GFP_USER);
2370 get_fs_root_and_pwd(current->fs, &root, &pwd);
2373 spin_lock(&dcache_lock);
2374 if (!d_unlinked(pwd.dentry)) {
2376 struct path tmp = root;
2377 char *cwd = page + PAGE_SIZE;
2378 int buflen = PAGE_SIZE;
2380 prepend(&cwd, &buflen, "\0", 1);
2381 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
2382 spin_unlock(&dcache_lock);
2387 /* Unreachable from current root */
2388 if (!path_equal(&tmp, &root)) {
2389 error = prepend_unreachable(&cwd, &buflen);
2395 len = PAGE_SIZE + page - cwd;
2398 if (copy_to_user(buf, cwd, len))
2402 spin_unlock(&dcache_lock);
2407 free_page((unsigned long) page);
2412 * Test whether new_dentry is a subdirectory of old_dentry.
2414 * Trivially implemented using the dcache structure
2418 * is_subdir - is new dentry a subdirectory of old_dentry
2419 * @new_dentry: new dentry
2420 * @old_dentry: old dentry
2422 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2423 * Returns 0 otherwise.
2424 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2427 int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
2432 if (new_dentry == old_dentry)
2436 * Need rcu_readlock to protect against the d_parent trashing
2441 /* for restarting inner loop in case of seq retry */
2442 seq = read_seqbegin(&rename_lock);
2443 if (d_ancestor(old_dentry, new_dentry))
2447 } while (read_seqretry(&rename_lock, seq));
2453 int path_is_under(struct path *path1, struct path *path2)
2455 struct vfsmount *mnt = path1->mnt;
2456 struct dentry *dentry = path1->dentry;
2459 br_read_lock(vfsmount_lock);
2460 if (mnt != path2->mnt) {
2462 if (mnt->mnt_parent == mnt) {
2463 br_read_unlock(vfsmount_lock);
2466 if (mnt->mnt_parent == path2->mnt)
2468 mnt = mnt->mnt_parent;
2470 dentry = mnt->mnt_mountpoint;
2472 res = is_subdir(dentry, path2->dentry);
2473 br_read_unlock(vfsmount_lock);
2476 EXPORT_SYMBOL(path_is_under);
2478 void d_genocide(struct dentry *root)
2480 struct dentry *this_parent = root;
2481 struct list_head *next;
2483 spin_lock(&dcache_lock);
2485 next = this_parent->d_subdirs.next;
2487 while (next != &this_parent->d_subdirs) {
2488 struct list_head *tmp = next;
2489 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
2491 if (d_unhashed(dentry)||!dentry->d_inode)
2493 if (!list_empty(&dentry->d_subdirs)) {
2494 this_parent = dentry;
2497 atomic_dec(&dentry->d_count);
2499 if (this_parent != root) {
2500 next = this_parent->d_u.d_child.next;
2501 atomic_dec(&this_parent->d_count);
2502 this_parent = this_parent->d_parent;
2505 spin_unlock(&dcache_lock);
2509 * find_inode_number - check for dentry with name
2510 * @dir: directory to check
2511 * @name: Name to find.
2513 * Check whether a dentry already exists for the given name,
2514 * and return the inode number if it has an inode. Otherwise
2517 * This routine is used to post-process directory listings for
2518 * filesystems using synthetic inode numbers, and is necessary
2519 * to keep getcwd() working.
2522 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2524 struct dentry * dentry;
2527 dentry = d_hash_and_lookup(dir, name);
2529 if (dentry->d_inode)
2530 ino = dentry->d_inode->i_ino;
2535 EXPORT_SYMBOL(find_inode_number);
2537 static __initdata unsigned long dhash_entries;
2538 static int __init set_dhash_entries(char *str)
2542 dhash_entries = simple_strtoul(str, &str, 0);
2545 __setup("dhash_entries=", set_dhash_entries);
2547 static void __init dcache_init_early(void)
2551 /* If hashes are distributed across NUMA nodes, defer
2552 * hash allocation until vmalloc space is available.
2558 alloc_large_system_hash("Dentry cache",
2559 sizeof(struct hlist_head),
2567 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2568 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2571 static void __init dcache_init(void)
2576 * A constructor could be added for stable state like the lists,
2577 * but it is probably not worth it because of the cache nature
2580 dentry_cache = KMEM_CACHE(dentry,
2581 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
2583 register_shrinker(&dcache_shrinker);
2585 /* Hash may have been set up in dcache_init_early */
2590 alloc_large_system_hash("Dentry cache",
2591 sizeof(struct hlist_head),
2599 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2600 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2603 /* SLAB cache for __getname() consumers */
2604 struct kmem_cache *names_cachep __read_mostly;
2605 EXPORT_SYMBOL(names_cachep);
2607 EXPORT_SYMBOL(d_genocide);
2609 void __init vfs_caches_init_early(void)
2611 dcache_init_early();
2615 void __init vfs_caches_init(unsigned long mempages)
2617 unsigned long reserve;
2619 /* Base hash sizes on available memory, with a reserve equal to
2620 150% of current kernel size */
2622 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2623 mempages -= reserve;
2625 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2626 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2630 files_init(mempages);