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
57 * If there is an ancestor relationship:
58 * dentry->d_parent->...->d_parent->d_lock
60 * dentry->d_parent->d_lock
63 * If no ancestor relationship:
64 * if (dentry1 < dentry2)
68 int sysctl_vfs_cache_pressure __read_mostly = 100;
69 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
71 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_hash_lock);
72 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
73 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
74 __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
76 EXPORT_SYMBOL(dcache_lock);
78 static struct kmem_cache *dentry_cache __read_mostly;
80 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
83 * This is the single most critical data structure when it comes
84 * to the dcache: the hashtable for lookups. Somebody should try
85 * to make this good - I've just made it work.
87 * This hash-function tries to avoid losing too many bits of hash
88 * information, yet avoid using a prime hash-size or similar.
90 #define D_HASHBITS d_hash_shift
91 #define D_HASHMASK d_hash_mask
93 static unsigned int d_hash_mask __read_mostly;
94 static unsigned int d_hash_shift __read_mostly;
95 static struct hlist_head *dentry_hashtable __read_mostly;
97 /* Statistics gathering. */
98 struct dentry_stat_t dentry_stat = {
102 static DEFINE_PER_CPU(unsigned int, nr_dentry);
104 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
105 static int get_nr_dentry(void)
109 for_each_possible_cpu(i)
110 sum += per_cpu(nr_dentry, i);
111 return sum < 0 ? 0 : sum;
114 int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
115 size_t *lenp, loff_t *ppos)
117 dentry_stat.nr_dentry = get_nr_dentry();
118 return proc_dointvec(table, write, buffer, lenp, ppos);
122 static void __d_free(struct rcu_head *head)
124 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
126 WARN_ON(!list_empty(&dentry->d_alias));
127 if (dname_external(dentry))
128 kfree(dentry->d_name.name);
129 kmem_cache_free(dentry_cache, dentry);
133 * no dcache_lock, please.
135 static void d_free(struct dentry *dentry)
137 BUG_ON(dentry->d_count);
138 this_cpu_dec(nr_dentry);
139 if (dentry->d_op && dentry->d_op->d_release)
140 dentry->d_op->d_release(dentry);
142 /* if dentry was never inserted into hash, immediate free is OK */
143 if (hlist_unhashed(&dentry->d_hash))
144 __d_free(&dentry->d_u.d_rcu);
146 call_rcu(&dentry->d_u.d_rcu, __d_free);
150 * Release the dentry's inode, using the filesystem
151 * d_iput() operation if defined.
153 static void dentry_iput(struct dentry * dentry)
154 __releases(dentry->d_lock)
155 __releases(dcache_lock)
157 struct inode *inode = dentry->d_inode;
159 dentry->d_inode = NULL;
160 list_del_init(&dentry->d_alias);
161 spin_unlock(&dentry->d_lock);
162 spin_unlock(&dcache_lock);
164 fsnotify_inoderemove(inode);
165 if (dentry->d_op && dentry->d_op->d_iput)
166 dentry->d_op->d_iput(dentry, inode);
170 spin_unlock(&dentry->d_lock);
171 spin_unlock(&dcache_lock);
176 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
178 static void dentry_lru_add(struct dentry *dentry)
180 if (list_empty(&dentry->d_lru)) {
181 spin_lock(&dcache_lru_lock);
182 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
183 dentry->d_sb->s_nr_dentry_unused++;
184 dentry_stat.nr_unused++;
185 spin_unlock(&dcache_lru_lock);
189 static void __dentry_lru_del(struct dentry *dentry)
191 list_del_init(&dentry->d_lru);
192 dentry->d_sb->s_nr_dentry_unused--;
193 dentry_stat.nr_unused--;
196 static void dentry_lru_del(struct dentry *dentry)
198 if (!list_empty(&dentry->d_lru)) {
199 spin_lock(&dcache_lru_lock);
200 __dentry_lru_del(dentry);
201 spin_unlock(&dcache_lru_lock);
205 static void dentry_lru_move_tail(struct dentry *dentry)
207 spin_lock(&dcache_lru_lock);
208 if (list_empty(&dentry->d_lru)) {
209 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
210 dentry->d_sb->s_nr_dentry_unused++;
211 dentry_stat.nr_unused++;
213 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
215 spin_unlock(&dcache_lru_lock);
219 * d_kill - kill dentry and return parent
220 * @dentry: dentry to kill
222 * The dentry must already be unhashed and removed from the LRU.
224 * If this is the root of the dentry tree, return NULL.
226 * dcache_lock and d_lock must be held by caller, are dropped by d_kill.
228 static struct dentry *d_kill(struct dentry *dentry)
229 __releases(dentry->d_lock)
230 __releases(dcache_lock)
232 struct dentry *parent;
234 list_del(&dentry->d_u.d_child);
237 * dentry_iput drops the locks, at which point nobody (except
238 * transient RCU lookups) can reach this dentry.
243 parent = dentry->d_parent;
249 * d_drop - drop a dentry
250 * @dentry: dentry to drop
252 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
253 * be found through a VFS lookup any more. Note that this is different from
254 * deleting the dentry - d_delete will try to mark the dentry negative if
255 * possible, giving a successful _negative_ lookup, while d_drop will
256 * just make the cache lookup fail.
258 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
259 * reason (NFS timeouts or autofs deletes).
261 * __d_drop requires dentry->d_lock.
263 void __d_drop(struct dentry *dentry)
265 if (!(dentry->d_flags & DCACHE_UNHASHED)) {
266 dentry->d_flags |= DCACHE_UNHASHED;
267 spin_lock(&dcache_hash_lock);
268 hlist_del_rcu(&dentry->d_hash);
269 spin_unlock(&dcache_hash_lock);
272 EXPORT_SYMBOL(__d_drop);
274 void d_drop(struct dentry *dentry)
276 spin_lock(&dcache_lock);
277 spin_lock(&dentry->d_lock);
279 spin_unlock(&dentry->d_lock);
280 spin_unlock(&dcache_lock);
282 EXPORT_SYMBOL(d_drop);
287 * This is complicated by the fact that we do not want to put
288 * dentries that are no longer on any hash chain on the unused
289 * list: we'd much rather just get rid of them immediately.
291 * However, that implies that we have to traverse the dentry
292 * tree upwards to the parents which might _also_ now be
293 * scheduled for deletion (it may have been only waiting for
294 * its last child to go away).
296 * This tail recursion is done by hand as we don't want to depend
297 * on the compiler to always get this right (gcc generally doesn't).
298 * Real recursion would eat up our stack space.
302 * dput - release a dentry
303 * @dentry: dentry to release
305 * Release a dentry. This will drop the usage count and if appropriate
306 * call the dentry unlink method as well as removing it from the queues and
307 * releasing its resources. If the parent dentries were scheduled for release
308 * they too may now get deleted.
310 * no dcache lock, please.
313 void dput(struct dentry *dentry)
319 if (dentry->d_count == 1)
321 spin_lock(&dentry->d_lock);
322 if (dentry->d_count == 1) {
323 if (!spin_trylock(&dcache_lock)) {
325 * Something of a livelock possibility we could avoid
326 * by taking dcache_lock and trying again, but we
327 * want to reduce dcache_lock anyway so this will
330 spin_unlock(&dentry->d_lock);
335 if (dentry->d_count) {
336 spin_unlock(&dentry->d_lock);
337 spin_unlock(&dcache_lock);
342 * AV: ->d_delete() is _NOT_ allowed to block now.
344 if (dentry->d_op && dentry->d_op->d_delete) {
345 if (dentry->d_op->d_delete(dentry))
349 /* Unreachable? Get rid of it */
350 if (d_unhashed(dentry))
353 /* Otherwise leave it cached and ensure it's on the LRU */
354 dentry->d_flags |= DCACHE_REFERENCED;
355 dentry_lru_add(dentry);
357 spin_unlock(&dentry->d_lock);
358 spin_unlock(&dcache_lock);
364 /* if dentry was on the d_lru list delete it from there */
365 dentry_lru_del(dentry);
366 dentry = d_kill(dentry);
373 * d_invalidate - invalidate a dentry
374 * @dentry: dentry to invalidate
376 * Try to invalidate the dentry if it turns out to be
377 * possible. If there are other dentries that can be
378 * reached through this one we can't delete it and we
379 * return -EBUSY. On success we return 0.
384 int d_invalidate(struct dentry * dentry)
387 * If it's already been dropped, return OK.
389 spin_lock(&dcache_lock);
390 spin_lock(&dentry->d_lock);
391 if (d_unhashed(dentry)) {
392 spin_unlock(&dentry->d_lock);
393 spin_unlock(&dcache_lock);
397 * Check whether to do a partial shrink_dcache
398 * to get rid of unused child entries.
400 if (!list_empty(&dentry->d_subdirs)) {
401 spin_unlock(&dentry->d_lock);
402 spin_unlock(&dcache_lock);
403 shrink_dcache_parent(dentry);
404 spin_lock(&dcache_lock);
405 spin_lock(&dentry->d_lock);
409 * Somebody else still using it?
411 * If it's a directory, we can't drop it
412 * for fear of somebody re-populating it
413 * with children (even though dropping it
414 * would make it unreachable from the root,
415 * we might still populate it if it was a
416 * working directory or similar).
418 if (dentry->d_count > 1) {
419 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
420 spin_unlock(&dentry->d_lock);
421 spin_unlock(&dcache_lock);
427 spin_unlock(&dentry->d_lock);
428 spin_unlock(&dcache_lock);
431 EXPORT_SYMBOL(d_invalidate);
433 /* This must be called with dcache_lock and d_lock held */
434 static inline struct dentry * __dget_locked_dlock(struct dentry *dentry)
437 dentry_lru_del(dentry);
441 /* This should be called _only_ with dcache_lock held */
442 static inline struct dentry * __dget_locked(struct dentry *dentry)
444 spin_lock(&dentry->d_lock);
445 __dget_locked_dlock(dentry);
446 spin_unlock(&dentry->d_lock);
450 struct dentry * dget_locked_dlock(struct dentry *dentry)
452 return __dget_locked_dlock(dentry);
455 struct dentry * dget_locked(struct dentry *dentry)
457 return __dget_locked(dentry);
459 EXPORT_SYMBOL(dget_locked);
461 struct dentry *dget_parent(struct dentry *dentry)
466 spin_lock(&dentry->d_lock);
467 ret = dentry->d_parent;
474 if (!spin_trylock(&ret->d_lock)) {
475 spin_unlock(&dentry->d_lock);
479 BUG_ON(!ret->d_count);
481 spin_unlock(&ret->d_lock);
483 spin_unlock(&dentry->d_lock);
486 EXPORT_SYMBOL(dget_parent);
489 * d_find_alias - grab a hashed alias of inode
490 * @inode: inode in question
491 * @want_discon: flag, used by d_splice_alias, to request
492 * that only a DISCONNECTED alias be returned.
494 * If inode has a hashed alias, or is a directory and has any alias,
495 * acquire the reference to alias and return it. Otherwise return NULL.
496 * Notice that if inode is a directory there can be only one alias and
497 * it can be unhashed only if it has no children, or if it is the root
500 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
501 * any other hashed alias over that one unless @want_discon is set,
502 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
504 static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
506 struct dentry *alias, *discon_alias;
510 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
511 spin_lock(&alias->d_lock);
512 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
513 if (IS_ROOT(alias) &&
514 (alias->d_flags & DCACHE_DISCONNECTED)) {
515 discon_alias = alias;
516 } else if (!want_discon) {
517 __dget_locked_dlock(alias);
518 spin_unlock(&alias->d_lock);
522 spin_unlock(&alias->d_lock);
525 alias = discon_alias;
526 spin_lock(&alias->d_lock);
527 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
528 if (IS_ROOT(alias) &&
529 (alias->d_flags & DCACHE_DISCONNECTED)) {
530 __dget_locked_dlock(alias);
531 spin_unlock(&alias->d_lock);
535 spin_unlock(&alias->d_lock);
541 struct dentry *d_find_alias(struct inode *inode)
543 struct dentry *de = NULL;
545 if (!list_empty(&inode->i_dentry)) {
546 spin_lock(&dcache_lock);
547 de = __d_find_alias(inode, 0);
548 spin_unlock(&dcache_lock);
552 EXPORT_SYMBOL(d_find_alias);
555 * Try to kill dentries associated with this inode.
556 * WARNING: you must own a reference to inode.
558 void d_prune_aliases(struct inode *inode)
560 struct dentry *dentry;
562 spin_lock(&dcache_lock);
563 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
564 spin_lock(&dentry->d_lock);
565 if (!dentry->d_count) {
566 __dget_locked_dlock(dentry);
568 spin_unlock(&dentry->d_lock);
569 spin_unlock(&dcache_lock);
573 spin_unlock(&dentry->d_lock);
575 spin_unlock(&dcache_lock);
577 EXPORT_SYMBOL(d_prune_aliases);
580 * Throw away a dentry - free the inode, dput the parent. This requires that
581 * the LRU list has already been removed.
583 * Try to prune ancestors as well. This is necessary to prevent
584 * quadratic behavior of shrink_dcache_parent(), but is also expected
585 * to be beneficial in reducing dentry cache fragmentation.
587 static void prune_one_dentry(struct dentry * dentry)
588 __releases(dentry->d_lock)
589 __releases(dcache_lock)
592 dentry = d_kill(dentry);
595 * Prune ancestors. Locking is simpler than in dput(),
596 * because dcache_lock needs to be taken anyway.
599 spin_lock(&dcache_lock);
600 spin_lock(&dentry->d_lock);
602 if (dentry->d_count) {
603 spin_unlock(&dentry->d_lock);
604 spin_unlock(&dcache_lock);
608 dentry_lru_del(dentry);
610 dentry = d_kill(dentry);
614 static void shrink_dentry_list(struct list_head *list)
616 struct dentry *dentry;
618 while (!list_empty(list)) {
619 dentry = list_entry(list->prev, struct dentry, d_lru);
621 if (!spin_trylock(&dentry->d_lock)) {
622 spin_unlock(&dcache_lru_lock);
624 spin_lock(&dcache_lru_lock);
628 __dentry_lru_del(dentry);
631 * We found an inuse dentry which was not removed from
632 * the LRU because of laziness during lookup. Do not free
633 * it - just keep it off the LRU list.
635 if (dentry->d_count) {
636 spin_unlock(&dentry->d_lock);
639 spin_unlock(&dcache_lru_lock);
641 prune_one_dentry(dentry);
642 /* dcache_lock and dentry->d_lock dropped */
643 spin_lock(&dcache_lock);
644 spin_lock(&dcache_lru_lock);
649 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
650 * @sb: superblock to shrink dentry LRU.
651 * @count: number of entries to prune
652 * @flags: flags to control the dentry processing
654 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
656 static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
658 /* called from prune_dcache() and shrink_dcache_parent() */
659 struct dentry *dentry;
660 LIST_HEAD(referenced);
664 spin_lock(&dcache_lock);
666 spin_lock(&dcache_lru_lock);
667 while (!list_empty(&sb->s_dentry_lru)) {
668 dentry = list_entry(sb->s_dentry_lru.prev,
669 struct dentry, d_lru);
670 BUG_ON(dentry->d_sb != sb);
672 if (!spin_trylock(&dentry->d_lock)) {
673 spin_unlock(&dcache_lru_lock);
679 * If we are honouring the DCACHE_REFERENCED flag and the
680 * dentry has this flag set, don't free it. Clear the flag
681 * and put it back on the LRU.
683 if (flags & DCACHE_REFERENCED &&
684 dentry->d_flags & DCACHE_REFERENCED) {
685 dentry->d_flags &= ~DCACHE_REFERENCED;
686 list_move(&dentry->d_lru, &referenced);
687 spin_unlock(&dentry->d_lock);
689 list_move_tail(&dentry->d_lru, &tmp);
690 spin_unlock(&dentry->d_lock);
694 /* XXX: re-add cond_resched_lock when dcache_lock goes away */
698 shrink_dentry_list(&tmp);
700 if (!list_empty(&referenced))
701 list_splice(&referenced, &sb->s_dentry_lru);
702 spin_unlock(&dcache_lru_lock);
703 spin_unlock(&dcache_lock);
708 * prune_dcache - shrink the dcache
709 * @count: number of entries to try to free
711 * Shrink the dcache. This is done when we need more memory, or simply when we
712 * need to unmount something (at which point we need to unuse all dentries).
714 * This function may fail to free any resources if all the dentries are in use.
716 static void prune_dcache(int count)
718 struct super_block *sb, *p = NULL;
720 int unused = dentry_stat.nr_unused;
724 if (unused == 0 || count == 0)
726 spin_lock(&dcache_lock);
730 prune_ratio = unused / count;
732 list_for_each_entry(sb, &super_blocks, s_list) {
733 if (list_empty(&sb->s_instances))
735 if (sb->s_nr_dentry_unused == 0)
738 /* Now, we reclaim unused dentrins with fairness.
739 * We reclaim them same percentage from each superblock.
740 * We calculate number of dentries to scan on this sb
741 * as follows, but the implementation is arranged to avoid
743 * number of dentries to scan on this sb =
744 * count * (number of dentries on this sb /
745 * number of dentries in the machine)
747 spin_unlock(&sb_lock);
748 if (prune_ratio != 1)
749 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
751 w_count = sb->s_nr_dentry_unused;
754 * We need to be sure this filesystem isn't being unmounted,
755 * otherwise we could race with generic_shutdown_super(), and
756 * end up holding a reference to an inode while the filesystem
757 * is unmounted. So we try to get s_umount, and make sure
760 if (down_read_trylock(&sb->s_umount)) {
761 if ((sb->s_root != NULL) &&
762 (!list_empty(&sb->s_dentry_lru))) {
763 spin_unlock(&dcache_lock);
764 __shrink_dcache_sb(sb, &w_count,
767 spin_lock(&dcache_lock);
769 up_read(&sb->s_umount);
776 /* more work left to do? */
782 spin_unlock(&sb_lock);
783 spin_unlock(&dcache_lock);
787 * shrink_dcache_sb - shrink dcache for a superblock
790 * Shrink the dcache for the specified super block. This is used to free
791 * the dcache before unmounting a file system.
793 void shrink_dcache_sb(struct super_block *sb)
797 spin_lock(&dcache_lock);
798 spin_lock(&dcache_lru_lock);
799 while (!list_empty(&sb->s_dentry_lru)) {
800 list_splice_init(&sb->s_dentry_lru, &tmp);
801 shrink_dentry_list(&tmp);
803 spin_unlock(&dcache_lru_lock);
804 spin_unlock(&dcache_lock);
806 EXPORT_SYMBOL(shrink_dcache_sb);
809 * destroy a single subtree of dentries for unmount
810 * - see the comments on shrink_dcache_for_umount() for a description of the
813 static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
815 struct dentry *parent;
816 unsigned detached = 0;
818 BUG_ON(!IS_ROOT(dentry));
820 /* detach this root from the system */
821 spin_lock(&dcache_lock);
822 spin_lock(&dentry->d_lock);
823 dentry_lru_del(dentry);
825 spin_unlock(&dentry->d_lock);
826 spin_unlock(&dcache_lock);
829 /* descend to the first leaf in the current subtree */
830 while (!list_empty(&dentry->d_subdirs)) {
833 /* this is a branch with children - detach all of them
834 * from the system in one go */
835 spin_lock(&dcache_lock);
836 list_for_each_entry(loop, &dentry->d_subdirs,
838 spin_lock(&loop->d_lock);
839 dentry_lru_del(loop);
841 spin_unlock(&loop->d_lock);
842 cond_resched_lock(&dcache_lock);
844 spin_unlock(&dcache_lock);
846 /* move to the first child */
847 dentry = list_entry(dentry->d_subdirs.next,
848 struct dentry, d_u.d_child);
851 /* consume the dentries from this leaf up through its parents
852 * until we find one with children or run out altogether */
856 if (dentry->d_count != 0) {
858 "BUG: Dentry %p{i=%lx,n=%s}"
860 " [unmount of %s %s]\n",
863 dentry->d_inode->i_ino : 0UL,
866 dentry->d_sb->s_type->name,
874 parent = dentry->d_parent;
875 spin_lock(&parent->d_lock);
877 spin_unlock(&parent->d_lock);
880 list_del(&dentry->d_u.d_child);
883 inode = dentry->d_inode;
885 dentry->d_inode = NULL;
886 list_del_init(&dentry->d_alias);
887 if (dentry->d_op && dentry->d_op->d_iput)
888 dentry->d_op->d_iput(dentry, inode);
895 /* finished when we fall off the top of the tree,
896 * otherwise we ascend to the parent and move to the
897 * next sibling if there is one */
901 } while (list_empty(&dentry->d_subdirs));
903 dentry = list_entry(dentry->d_subdirs.next,
904 struct dentry, d_u.d_child);
909 * destroy the dentries attached to a superblock on unmounting
910 * - we don't need to use dentry->d_lock, and only need dcache_lock when
911 * removing the dentry from the system lists and hashes because:
912 * - the superblock is detached from all mountings and open files, so the
913 * dentry trees will not be rearranged by the VFS
914 * - s_umount is write-locked, so the memory pressure shrinker will ignore
915 * any dentries belonging to this superblock that it comes across
916 * - the filesystem itself is no longer permitted to rearrange the dentries
919 void shrink_dcache_for_umount(struct super_block *sb)
921 struct dentry *dentry;
923 if (down_read_trylock(&sb->s_umount))
928 spin_lock(&dentry->d_lock);
930 spin_unlock(&dentry->d_lock);
931 shrink_dcache_for_umount_subtree(dentry);
933 while (!hlist_empty(&sb->s_anon)) {
934 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
935 shrink_dcache_for_umount_subtree(dentry);
940 * Search for at least 1 mount point in the dentry's subdirs.
941 * We descend to the next level whenever the d_subdirs
942 * list is non-empty and continue searching.
946 * have_submounts - check for mounts over a dentry
947 * @parent: dentry to check.
949 * Return true if the parent or its subdirectories contain
953 int have_submounts(struct dentry *parent)
955 struct dentry *this_parent = parent;
956 struct list_head *next;
958 spin_lock(&dcache_lock);
959 if (d_mountpoint(parent))
962 next = this_parent->d_subdirs.next;
964 while (next != &this_parent->d_subdirs) {
965 struct list_head *tmp = next;
966 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
968 /* Have we found a mount point ? */
969 if (d_mountpoint(dentry))
971 if (!list_empty(&dentry->d_subdirs)) {
972 this_parent = dentry;
977 * All done at this level ... ascend and resume the search.
979 if (this_parent != parent) {
980 next = this_parent->d_u.d_child.next;
981 this_parent = this_parent->d_parent;
984 spin_unlock(&dcache_lock);
985 return 0; /* No mount points found in tree */
987 spin_unlock(&dcache_lock);
990 EXPORT_SYMBOL(have_submounts);
993 * Search the dentry child list for the specified parent,
994 * and move any unused dentries to the end of the unused
995 * list for prune_dcache(). We descend to the next level
996 * whenever the d_subdirs list is non-empty and continue
999 * It returns zero iff there are no unused children,
1000 * otherwise it returns the number of children moved to
1001 * the end of the unused list. This may not be the total
1002 * number of unused children, because select_parent can
1003 * drop the lock and return early due to latency
1006 static int select_parent(struct dentry * parent)
1008 struct dentry *this_parent = parent;
1009 struct list_head *next;
1012 spin_lock(&dcache_lock);
1014 next = this_parent->d_subdirs.next;
1016 while (next != &this_parent->d_subdirs) {
1017 struct list_head *tmp = next;
1018 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1021 spin_lock(&dentry->d_lock);
1024 * move only zero ref count dentries to the end
1025 * of the unused list for prune_dcache
1027 if (!dentry->d_count) {
1028 dentry_lru_move_tail(dentry);
1031 dentry_lru_del(dentry);
1034 spin_unlock(&dentry->d_lock);
1037 * We can return to the caller if we have found some (this
1038 * ensures forward progress). We'll be coming back to find
1041 if (found && need_resched())
1045 * Descend a level if the d_subdirs list is non-empty.
1047 if (!list_empty(&dentry->d_subdirs)) {
1048 this_parent = dentry;
1053 * All done at this level ... ascend and resume the search.
1055 if (this_parent != parent) {
1056 next = this_parent->d_u.d_child.next;
1057 this_parent = this_parent->d_parent;
1061 spin_unlock(&dcache_lock);
1066 * shrink_dcache_parent - prune dcache
1067 * @parent: parent of entries to prune
1069 * Prune the dcache to remove unused children of the parent dentry.
1072 void shrink_dcache_parent(struct dentry * parent)
1074 struct super_block *sb = parent->d_sb;
1077 while ((found = select_parent(parent)) != 0)
1078 __shrink_dcache_sb(sb, &found, 0);
1080 EXPORT_SYMBOL(shrink_dcache_parent);
1083 * Scan `nr' dentries and return the number which remain.
1085 * We need to avoid reentering the filesystem if the caller is performing a
1086 * GFP_NOFS allocation attempt. One example deadlock is:
1088 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1089 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1090 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1092 * In this case we return -1 to tell the caller that we baled.
1094 static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
1097 if (!(gfp_mask & __GFP_FS))
1102 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
1105 static struct shrinker dcache_shrinker = {
1106 .shrink = shrink_dcache_memory,
1107 .seeks = DEFAULT_SEEKS,
1111 * d_alloc - allocate a dcache entry
1112 * @parent: parent of entry to allocate
1113 * @name: qstr of the name
1115 * Allocates a dentry. It returns %NULL if there is insufficient memory
1116 * available. On a success the dentry is returned. The name passed in is
1117 * copied and the copy passed in may be reused after this call.
1120 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1122 struct dentry *dentry;
1125 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
1129 if (name->len > DNAME_INLINE_LEN-1) {
1130 dname = kmalloc(name->len + 1, GFP_KERNEL);
1132 kmem_cache_free(dentry_cache, dentry);
1136 dname = dentry->d_iname;
1138 dentry->d_name.name = dname;
1140 dentry->d_name.len = name->len;
1141 dentry->d_name.hash = name->hash;
1142 memcpy(dname, name->name, name->len);
1143 dname[name->len] = 0;
1145 dentry->d_count = 1;
1146 dentry->d_flags = DCACHE_UNHASHED;
1147 spin_lock_init(&dentry->d_lock);
1148 dentry->d_inode = NULL;
1149 dentry->d_parent = NULL;
1150 dentry->d_sb = NULL;
1151 dentry->d_op = NULL;
1152 dentry->d_fsdata = NULL;
1153 dentry->d_mounted = 0;
1154 INIT_HLIST_NODE(&dentry->d_hash);
1155 INIT_LIST_HEAD(&dentry->d_lru);
1156 INIT_LIST_HEAD(&dentry->d_subdirs);
1157 INIT_LIST_HEAD(&dentry->d_alias);
1160 dentry->d_parent = dget(parent);
1161 dentry->d_sb = parent->d_sb;
1163 INIT_LIST_HEAD(&dentry->d_u.d_child);
1166 spin_lock(&dcache_lock);
1168 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
1169 spin_unlock(&dcache_lock);
1171 this_cpu_inc(nr_dentry);
1175 EXPORT_SYMBOL(d_alloc);
1177 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1182 q.len = strlen(name);
1183 q.hash = full_name_hash(q.name, q.len);
1184 return d_alloc(parent, &q);
1186 EXPORT_SYMBOL(d_alloc_name);
1188 /* the caller must hold dcache_lock */
1189 static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1192 list_add(&dentry->d_alias, &inode->i_dentry);
1193 dentry->d_inode = inode;
1194 fsnotify_d_instantiate(dentry, inode);
1198 * d_instantiate - fill in inode information for a dentry
1199 * @entry: dentry to complete
1200 * @inode: inode to attach to this dentry
1202 * Fill in inode information in the entry.
1204 * This turns negative dentries into productive full members
1207 * NOTE! This assumes that the inode count has been incremented
1208 * (or otherwise set) by the caller to indicate that it is now
1209 * in use by the dcache.
1212 void d_instantiate(struct dentry *entry, struct inode * inode)
1214 BUG_ON(!list_empty(&entry->d_alias));
1215 spin_lock(&dcache_lock);
1216 __d_instantiate(entry, inode);
1217 spin_unlock(&dcache_lock);
1218 security_d_instantiate(entry, inode);
1220 EXPORT_SYMBOL(d_instantiate);
1223 * d_instantiate_unique - instantiate a non-aliased dentry
1224 * @entry: dentry to instantiate
1225 * @inode: inode to attach to this dentry
1227 * Fill in inode information in the entry. On success, it returns NULL.
1228 * If an unhashed alias of "entry" already exists, then we return the
1229 * aliased dentry instead and drop one reference to inode.
1231 * Note that in order to avoid conflicts with rename() etc, the caller
1232 * had better be holding the parent directory semaphore.
1234 * This also assumes that the inode count has been incremented
1235 * (or otherwise set) by the caller to indicate that it is now
1236 * in use by the dcache.
1238 static struct dentry *__d_instantiate_unique(struct dentry *entry,
1239 struct inode *inode)
1241 struct dentry *alias;
1242 int len = entry->d_name.len;
1243 const char *name = entry->d_name.name;
1244 unsigned int hash = entry->d_name.hash;
1247 __d_instantiate(entry, NULL);
1251 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1252 struct qstr *qstr = &alias->d_name;
1254 if (qstr->hash != hash)
1256 if (alias->d_parent != entry->d_parent)
1258 if (qstr->len != len)
1260 if (memcmp(qstr->name, name, len))
1266 __d_instantiate(entry, inode);
1270 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1272 struct dentry *result;
1274 BUG_ON(!list_empty(&entry->d_alias));
1276 spin_lock(&dcache_lock);
1277 result = __d_instantiate_unique(entry, inode);
1278 spin_unlock(&dcache_lock);
1281 security_d_instantiate(entry, inode);
1285 BUG_ON(!d_unhashed(result));
1290 EXPORT_SYMBOL(d_instantiate_unique);
1293 * d_alloc_root - allocate root dentry
1294 * @root_inode: inode to allocate the root for
1296 * Allocate a root ("/") dentry for the inode given. The inode is
1297 * instantiated and returned. %NULL is returned if there is insufficient
1298 * memory or the inode passed is %NULL.
1301 struct dentry * d_alloc_root(struct inode * root_inode)
1303 struct dentry *res = NULL;
1306 static const struct qstr name = { .name = "/", .len = 1 };
1308 res = d_alloc(NULL, &name);
1310 res->d_sb = root_inode->i_sb;
1311 res->d_parent = res;
1312 d_instantiate(res, root_inode);
1317 EXPORT_SYMBOL(d_alloc_root);
1319 static inline struct hlist_head *d_hash(struct dentry *parent,
1322 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1323 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1324 return dentry_hashtable + (hash & D_HASHMASK);
1328 * d_obtain_alias - find or allocate a dentry for a given inode
1329 * @inode: inode to allocate the dentry for
1331 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1332 * similar open by handle operations. The returned dentry may be anonymous,
1333 * or may have a full name (if the inode was already in the cache).
1335 * When called on a directory inode, we must ensure that the inode only ever
1336 * has one dentry. If a dentry is found, that is returned instead of
1337 * allocating a new one.
1339 * On successful return, the reference to the inode has been transferred
1340 * to the dentry. In case of an error the reference on the inode is released.
1341 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1342 * be passed in and will be the error will be propagate to the return value,
1343 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1345 struct dentry *d_obtain_alias(struct inode *inode)
1347 static const struct qstr anonstring = { .name = "" };
1352 return ERR_PTR(-ESTALE);
1354 return ERR_CAST(inode);
1356 res = d_find_alias(inode);
1360 tmp = d_alloc(NULL, &anonstring);
1362 res = ERR_PTR(-ENOMEM);
1365 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1367 spin_lock(&dcache_lock);
1368 res = __d_find_alias(inode, 0);
1370 spin_unlock(&dcache_lock);
1375 /* attach a disconnected dentry */
1376 spin_lock(&tmp->d_lock);
1377 tmp->d_sb = inode->i_sb;
1378 tmp->d_inode = inode;
1379 tmp->d_flags |= DCACHE_DISCONNECTED;
1380 tmp->d_flags &= ~DCACHE_UNHASHED;
1381 list_add(&tmp->d_alias, &inode->i_dentry);
1382 spin_lock(&dcache_hash_lock);
1383 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1384 spin_unlock(&dcache_hash_lock);
1385 spin_unlock(&tmp->d_lock);
1387 spin_unlock(&dcache_lock);
1394 EXPORT_SYMBOL(d_obtain_alias);
1397 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1398 * @inode: the inode which may have a disconnected dentry
1399 * @dentry: a negative dentry which we want to point to the inode.
1401 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1402 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1403 * and return it, else simply d_add the inode to the dentry and return NULL.
1405 * This is needed in the lookup routine of any filesystem that is exportable
1406 * (via knfsd) so that we can build dcache paths to directories effectively.
1408 * If a dentry was found and moved, then it is returned. Otherwise NULL
1409 * is returned. This matches the expected return value of ->lookup.
1412 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1414 struct dentry *new = NULL;
1416 if (inode && S_ISDIR(inode->i_mode)) {
1417 spin_lock(&dcache_lock);
1418 new = __d_find_alias(inode, 1);
1420 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1421 spin_unlock(&dcache_lock);
1422 security_d_instantiate(new, inode);
1423 d_move(new, dentry);
1426 /* already taking dcache_lock, so d_add() by hand */
1427 __d_instantiate(dentry, inode);
1428 spin_unlock(&dcache_lock);
1429 security_d_instantiate(dentry, inode);
1433 d_add(dentry, inode);
1436 EXPORT_SYMBOL(d_splice_alias);
1439 * d_add_ci - lookup or allocate new dentry with case-exact name
1440 * @inode: the inode case-insensitive lookup has found
1441 * @dentry: the negative dentry that was passed to the parent's lookup func
1442 * @name: the case-exact name to be associated with the returned dentry
1444 * This is to avoid filling the dcache with case-insensitive names to the
1445 * same inode, only the actual correct case is stored in the dcache for
1446 * case-insensitive filesystems.
1448 * For a case-insensitive lookup match and if the the case-exact dentry
1449 * already exists in in the dcache, use it and return it.
1451 * If no entry exists with the exact case name, allocate new dentry with
1452 * the exact case, and return the spliced entry.
1454 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
1458 struct dentry *found;
1462 * First check if a dentry matching the name already exists,
1463 * if not go ahead and create it now.
1465 found = d_hash_and_lookup(dentry->d_parent, name);
1467 new = d_alloc(dentry->d_parent, name);
1473 found = d_splice_alias(inode, new);
1482 * If a matching dentry exists, and it's not negative use it.
1484 * Decrement the reference count to balance the iget() done
1487 if (found->d_inode) {
1488 if (unlikely(found->d_inode != inode)) {
1489 /* This can't happen because bad inodes are unhashed. */
1490 BUG_ON(!is_bad_inode(inode));
1491 BUG_ON(!is_bad_inode(found->d_inode));
1498 * Negative dentry: instantiate it unless the inode is a directory and
1499 * already has a dentry.
1501 spin_lock(&dcache_lock);
1502 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
1503 __d_instantiate(found, inode);
1504 spin_unlock(&dcache_lock);
1505 security_d_instantiate(found, inode);
1510 * In case a directory already has a (disconnected) entry grab a
1511 * reference to it, move it in place and use it.
1513 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1515 spin_unlock(&dcache_lock);
1516 security_d_instantiate(found, inode);
1524 return ERR_PTR(error);
1526 EXPORT_SYMBOL(d_add_ci);
1529 * d_lookup - search for a dentry
1530 * @parent: parent dentry
1531 * @name: qstr of name we wish to find
1532 * Returns: dentry, or NULL
1534 * d_lookup searches the children of the parent dentry for the name in
1535 * question. If the dentry is found its reference count is incremented and the
1536 * dentry is returned. The caller must use dput to free the entry when it has
1537 * finished using it. %NULL is returned if the dentry does not exist.
1539 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1541 struct dentry * dentry = NULL;
1545 seq = read_seqbegin(&rename_lock);
1546 dentry = __d_lookup(parent, name);
1549 } while (read_seqretry(&rename_lock, seq));
1552 EXPORT_SYMBOL(d_lookup);
1555 * __d_lookup - search for a dentry (racy)
1556 * @parent: parent dentry
1557 * @name: qstr of name we wish to find
1558 * Returns: dentry, or NULL
1560 * __d_lookup is like d_lookup, however it may (rarely) return a
1561 * false-negative result due to unrelated rename activity.
1563 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1564 * however it must be used carefully, eg. with a following d_lookup in
1565 * the case of failure.
1567 * __d_lookup callers must be commented.
1569 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1571 unsigned int len = name->len;
1572 unsigned int hash = name->hash;
1573 const unsigned char *str = name->name;
1574 struct hlist_head *head = d_hash(parent,hash);
1575 struct dentry *found = NULL;
1576 struct hlist_node *node;
1577 struct dentry *dentry;
1580 * The hash list is protected using RCU.
1582 * Take d_lock when comparing a candidate dentry, to avoid races
1585 * It is possible that concurrent renames can mess up our list
1586 * walk here and result in missing our dentry, resulting in the
1587 * false-negative result. d_lookup() protects against concurrent
1588 * renames using rename_lock seqlock.
1590 * See Documentation/vfs/dcache-locking.txt for more details.
1594 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1597 if (dentry->d_name.hash != hash)
1599 if (dentry->d_parent != parent)
1602 spin_lock(&dentry->d_lock);
1605 * Recheck the dentry after taking the lock - d_move may have
1606 * changed things. Don't bother checking the hash because
1607 * we're about to compare the whole name anyway.
1609 if (dentry->d_parent != parent)
1612 /* non-existing due to RCU? */
1613 if (d_unhashed(dentry))
1617 * It is safe to compare names since d_move() cannot
1618 * change the qstr (protected by d_lock).
1620 qstr = &dentry->d_name;
1621 if (parent->d_op && parent->d_op->d_compare) {
1622 if (parent->d_op->d_compare(parent, parent->d_inode,
1623 dentry, dentry->d_inode,
1624 qstr->len, qstr->name, name))
1627 if (qstr->len != len)
1629 if (memcmp(qstr->name, str, len))
1635 spin_unlock(&dentry->d_lock);
1638 spin_unlock(&dentry->d_lock);
1646 * d_hash_and_lookup - hash the qstr then search for a dentry
1647 * @dir: Directory to search in
1648 * @name: qstr of name we wish to find
1650 * On hash failure or on lookup failure NULL is returned.
1652 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1654 struct dentry *dentry = NULL;
1657 * Check for a fs-specific hash function. Note that we must
1658 * calculate the standard hash first, as the d_op->d_hash()
1659 * routine may choose to leave the hash value unchanged.
1661 name->hash = full_name_hash(name->name, name->len);
1662 if (dir->d_op && dir->d_op->d_hash) {
1663 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
1666 dentry = d_lookup(dir, name);
1672 * d_validate - verify dentry provided from insecure source (deprecated)
1673 * @dentry: The dentry alleged to be valid child of @dparent
1674 * @dparent: The parent dentry (known to be valid)
1676 * An insecure source has sent us a dentry, here we verify it and dget() it.
1677 * This is used by ncpfs in its readdir implementation.
1678 * Zero is returned in the dentry is invalid.
1680 * This function is slow for big directories, and deprecated, do not use it.
1682 int d_validate(struct dentry *dentry, struct dentry *dparent)
1684 struct dentry *child;
1686 spin_lock(&dcache_lock);
1687 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1688 if (dentry == child) {
1689 __dget_locked(dentry);
1690 spin_unlock(&dcache_lock);
1694 spin_unlock(&dcache_lock);
1698 EXPORT_SYMBOL(d_validate);
1701 * When a file is deleted, we have two options:
1702 * - turn this dentry into a negative dentry
1703 * - unhash this dentry and free it.
1705 * Usually, we want to just turn this into
1706 * a negative dentry, but if anybody else is
1707 * currently using the dentry or the inode
1708 * we can't do that and we fall back on removing
1709 * it from the hash queues and waiting for
1710 * it to be deleted later when it has no users
1714 * d_delete - delete a dentry
1715 * @dentry: The dentry to delete
1717 * Turn the dentry into a negative dentry if possible, otherwise
1718 * remove it from the hash queues so it can be deleted later
1721 void d_delete(struct dentry * dentry)
1725 * Are we the only user?
1727 spin_lock(&dcache_lock);
1728 spin_lock(&dentry->d_lock);
1729 isdir = S_ISDIR(dentry->d_inode->i_mode);
1730 if (dentry->d_count == 1) {
1731 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
1732 dentry_iput(dentry);
1733 fsnotify_nameremove(dentry, isdir);
1737 if (!d_unhashed(dentry))
1740 spin_unlock(&dentry->d_lock);
1741 spin_unlock(&dcache_lock);
1743 fsnotify_nameremove(dentry, isdir);
1745 EXPORT_SYMBOL(d_delete);
1747 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1750 entry->d_flags &= ~DCACHE_UNHASHED;
1751 hlist_add_head_rcu(&entry->d_hash, list);
1754 static void _d_rehash(struct dentry * entry)
1756 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1760 * d_rehash - add an entry back to the hash
1761 * @entry: dentry to add to the hash
1763 * Adds a dentry to the hash according to its name.
1766 void d_rehash(struct dentry * entry)
1768 spin_lock(&dcache_lock);
1769 spin_lock(&entry->d_lock);
1770 spin_lock(&dcache_hash_lock);
1772 spin_unlock(&dcache_hash_lock);
1773 spin_unlock(&entry->d_lock);
1774 spin_unlock(&dcache_lock);
1776 EXPORT_SYMBOL(d_rehash);
1779 * dentry_update_name_case - update case insensitive dentry with a new name
1780 * @dentry: dentry to be updated
1783 * Update a case insensitive dentry with new case of name.
1785 * dentry must have been returned by d_lookup with name @name. Old and new
1786 * name lengths must match (ie. no d_compare which allows mismatched name
1789 * Parent inode i_mutex must be held over d_lookup and into this call (to
1790 * keep renames and concurrent inserts, and readdir(2) away).
1792 void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
1794 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
1795 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
1797 spin_lock(&dcache_lock);
1798 spin_lock(&dentry->d_lock);
1799 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
1800 spin_unlock(&dentry->d_lock);
1801 spin_unlock(&dcache_lock);
1803 EXPORT_SYMBOL(dentry_update_name_case);
1806 * When switching names, the actual string doesn't strictly have to
1807 * be preserved in the target - because we're dropping the target
1808 * anyway. As such, we can just do a simple memcpy() to copy over
1809 * the new name before we switch.
1811 * Note that we have to be a lot more careful about getting the hash
1812 * switched - we have to switch the hash value properly even if it
1813 * then no longer matches the actual (corrupted) string of the target.
1814 * The hash value has to match the hash queue that the dentry is on..
1816 static void switch_names(struct dentry *dentry, struct dentry *target)
1818 if (dname_external(target)) {
1819 if (dname_external(dentry)) {
1821 * Both external: swap the pointers
1823 swap(target->d_name.name, dentry->d_name.name);
1826 * dentry:internal, target:external. Steal target's
1827 * storage and make target internal.
1829 memcpy(target->d_iname, dentry->d_name.name,
1830 dentry->d_name.len + 1);
1831 dentry->d_name.name = target->d_name.name;
1832 target->d_name.name = target->d_iname;
1835 if (dname_external(dentry)) {
1837 * dentry:external, target:internal. Give dentry's
1838 * storage to target and make dentry internal
1840 memcpy(dentry->d_iname, target->d_name.name,
1841 target->d_name.len + 1);
1842 target->d_name.name = dentry->d_name.name;
1843 dentry->d_name.name = dentry->d_iname;
1846 * Both are internal. Just copy target to dentry
1848 memcpy(dentry->d_iname, target->d_name.name,
1849 target->d_name.len + 1);
1850 dentry->d_name.len = target->d_name.len;
1854 swap(dentry->d_name.len, target->d_name.len);
1858 * We cannibalize "target" when moving dentry on top of it,
1859 * because it's going to be thrown away anyway. We could be more
1860 * polite about it, though.
1862 * This forceful removal will result in ugly /proc output if
1863 * somebody holds a file open that got deleted due to a rename.
1864 * We could be nicer about the deleted file, and let it show
1865 * up under the name it had before it was deleted rather than
1866 * under the original name of the file that was moved on top of it.
1870 * d_move_locked - move a dentry
1871 * @dentry: entry to move
1872 * @target: new dentry
1874 * Update the dcache to reflect the move of a file name. Negative
1875 * dcache entries should not be moved in this way.
1877 static void d_move_locked(struct dentry * dentry, struct dentry * target)
1879 if (!dentry->d_inode)
1880 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1882 write_seqlock(&rename_lock);
1884 * XXXX: do we really need to take target->d_lock?
1886 if (d_ancestor(dentry, target)) {
1887 spin_lock(&dentry->d_lock);
1888 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
1889 } else if (d_ancestor(target, dentry) || target < dentry) {
1890 spin_lock(&target->d_lock);
1891 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1893 spin_lock(&dentry->d_lock);
1894 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
1897 /* Move the dentry to the target hash queue, if on different bucket */
1898 spin_lock(&dcache_hash_lock);
1899 if (!d_unhashed(dentry))
1900 hlist_del_rcu(&dentry->d_hash);
1901 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
1902 spin_unlock(&dcache_hash_lock);
1904 /* Unhash the target: dput() will then get rid of it */
1907 list_del(&dentry->d_u.d_child);
1908 list_del(&target->d_u.d_child);
1910 /* Switch the names.. */
1911 switch_names(dentry, target);
1912 swap(dentry->d_name.hash, target->d_name.hash);
1914 /* ... and switch the parents */
1915 if (IS_ROOT(dentry)) {
1916 dentry->d_parent = target->d_parent;
1917 target->d_parent = target;
1918 INIT_LIST_HEAD(&target->d_u.d_child);
1920 swap(dentry->d_parent, target->d_parent);
1922 /* And add them back to the (new) parent lists */
1923 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1926 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1927 spin_unlock(&target->d_lock);
1928 fsnotify_d_move(dentry);
1929 spin_unlock(&dentry->d_lock);
1930 write_sequnlock(&rename_lock);
1934 * d_move - move a dentry
1935 * @dentry: entry to move
1936 * @target: new dentry
1938 * Update the dcache to reflect the move of a file name. Negative
1939 * dcache entries should not be moved in this way.
1942 void d_move(struct dentry * dentry, struct dentry * target)
1944 spin_lock(&dcache_lock);
1945 d_move_locked(dentry, target);
1946 spin_unlock(&dcache_lock);
1948 EXPORT_SYMBOL(d_move);
1951 * d_ancestor - search for an ancestor
1952 * @p1: ancestor dentry
1955 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1956 * an ancestor of p2, else NULL.
1958 struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
1962 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
1963 if (p->d_parent == p1)
1970 * This helper attempts to cope with remotely renamed directories
1972 * It assumes that the caller is already holding
1973 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1975 * Note: If ever the locking in lock_rename() changes, then please
1976 * remember to update this too...
1978 static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1979 __releases(dcache_lock)
1981 struct mutex *m1 = NULL, *m2 = NULL;
1984 /* If alias and dentry share a parent, then no extra locks required */
1985 if (alias->d_parent == dentry->d_parent)
1988 /* Check for loops */
1989 ret = ERR_PTR(-ELOOP);
1990 if (d_ancestor(alias, dentry))
1993 /* See lock_rename() */
1994 ret = ERR_PTR(-EBUSY);
1995 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1997 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1998 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2000 m2 = &alias->d_parent->d_inode->i_mutex;
2002 d_move_locked(alias, dentry);
2005 spin_unlock(&dcache_lock);
2014 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2015 * named dentry in place of the dentry to be replaced.
2017 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2019 struct dentry *dparent, *aparent;
2021 switch_names(dentry, anon);
2022 swap(dentry->d_name.hash, anon->d_name.hash);
2024 dparent = dentry->d_parent;
2025 aparent = anon->d_parent;
2027 dentry->d_parent = (aparent == anon) ? dentry : aparent;
2028 list_del(&dentry->d_u.d_child);
2029 if (!IS_ROOT(dentry))
2030 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2032 INIT_LIST_HEAD(&dentry->d_u.d_child);
2034 anon->d_parent = (dparent == dentry) ? anon : dparent;
2035 list_del(&anon->d_u.d_child);
2037 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
2039 INIT_LIST_HEAD(&anon->d_u.d_child);
2041 anon->d_flags &= ~DCACHE_DISCONNECTED;
2045 * d_materialise_unique - introduce an inode into the tree
2046 * @dentry: candidate dentry
2047 * @inode: inode to bind to the dentry, to which aliases may be attached
2049 * Introduces an dentry into the tree, substituting an extant disconnected
2050 * root directory alias in its place if there is one
2052 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2054 struct dentry *actual;
2056 BUG_ON(!d_unhashed(dentry));
2058 spin_lock(&dcache_lock);
2062 __d_instantiate(dentry, NULL);
2066 if (S_ISDIR(inode->i_mode)) {
2067 struct dentry *alias;
2069 /* Does an aliased dentry already exist? */
2070 alias = __d_find_alias(inode, 0);
2073 /* Is this an anonymous mountpoint that we could splice
2075 if (IS_ROOT(alias)) {
2076 spin_lock(&alias->d_lock);
2077 __d_materialise_dentry(dentry, alias);
2081 /* Nope, but we must(!) avoid directory aliasing */
2082 actual = __d_unalias(dentry, alias);
2089 /* Add a unique reference */
2090 actual = __d_instantiate_unique(dentry, inode);
2093 else if (unlikely(!d_unhashed(actual)))
2094 goto shouldnt_be_hashed;
2097 spin_lock(&actual->d_lock);
2099 spin_lock(&dcache_hash_lock);
2101 spin_unlock(&dcache_hash_lock);
2102 spin_unlock(&actual->d_lock);
2103 spin_unlock(&dcache_lock);
2105 if (actual == dentry) {
2106 security_d_instantiate(dentry, inode);
2114 spin_unlock(&dcache_lock);
2117 EXPORT_SYMBOL_GPL(d_materialise_unique);
2119 static int prepend(char **buffer, int *buflen, const char *str, int namelen)
2123 return -ENAMETOOLONG;
2125 memcpy(*buffer, str, namelen);
2129 static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2131 return prepend(buffer, buflen, name->name, name->len);
2135 * Prepend path string to a buffer
2137 * @path: the dentry/vfsmount to report
2138 * @root: root vfsmnt/dentry (may be modified by this function)
2139 * @buffer: pointer to the end of the buffer
2140 * @buflen: pointer to buffer length
2142 * Caller holds the dcache_lock.
2144 * If path is not reachable from the supplied root, then the value of
2145 * root is changed (without modifying refcounts).
2147 static int prepend_path(const struct path *path, struct path *root,
2148 char **buffer, int *buflen)
2150 struct dentry *dentry = path->dentry;
2151 struct vfsmount *vfsmnt = path->mnt;
2155 br_read_lock(vfsmount_lock);
2156 while (dentry != root->dentry || vfsmnt != root->mnt) {
2157 struct dentry * parent;
2159 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2161 if (vfsmnt->mnt_parent == vfsmnt) {
2164 dentry = vfsmnt->mnt_mountpoint;
2165 vfsmnt = vfsmnt->mnt_parent;
2168 parent = dentry->d_parent;
2170 error = prepend_name(buffer, buflen, &dentry->d_name);
2172 error = prepend(buffer, buflen, "/", 1);
2181 if (!error && !slash)
2182 error = prepend(buffer, buflen, "/", 1);
2184 br_read_unlock(vfsmount_lock);
2189 * Filesystems needing to implement special "root names"
2190 * should do so with ->d_dname()
2192 if (IS_ROOT(dentry) &&
2193 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2194 WARN(1, "Root dentry has weird name <%.*s>\n",
2195 (int) dentry->d_name.len, dentry->d_name.name);
2198 root->dentry = dentry;
2203 * __d_path - return the path of a dentry
2204 * @path: the dentry/vfsmount to report
2205 * @root: root vfsmnt/dentry (may be modified by this function)
2206 * @buf: buffer to return value in
2207 * @buflen: buffer length
2209 * Convert a dentry into an ASCII path name.
2211 * Returns a pointer into the buffer or an error code if the
2212 * path was too long.
2214 * "buflen" should be positive.
2216 * If path is not reachable from the supplied root, then the value of
2217 * root is changed (without modifying refcounts).
2219 char *__d_path(const struct path *path, struct path *root,
2220 char *buf, int buflen)
2222 char *res = buf + buflen;
2225 prepend(&res, &buflen, "\0", 1);
2226 spin_lock(&dcache_lock);
2227 error = prepend_path(path, root, &res, &buflen);
2228 spin_unlock(&dcache_lock);
2231 return ERR_PTR(error);
2236 * same as __d_path but appends "(deleted)" for unlinked files.
2238 static int path_with_deleted(const struct path *path, struct path *root,
2239 char **buf, int *buflen)
2241 prepend(buf, buflen, "\0", 1);
2242 if (d_unlinked(path->dentry)) {
2243 int error = prepend(buf, buflen, " (deleted)", 10);
2248 return prepend_path(path, root, buf, buflen);
2251 static int prepend_unreachable(char **buffer, int *buflen)
2253 return prepend(buffer, buflen, "(unreachable)", 13);
2257 * d_path - return the path of a dentry
2258 * @path: path to report
2259 * @buf: buffer to return value in
2260 * @buflen: buffer length
2262 * Convert a dentry into an ASCII path name. If the entry has been deleted
2263 * the string " (deleted)" is appended. Note that this is ambiguous.
2265 * Returns a pointer into the buffer or an error code if the path was
2266 * too long. Note: Callers should use the returned pointer, not the passed
2267 * in buffer, to use the name! The implementation often starts at an offset
2268 * into the buffer, and may leave 0 bytes at the start.
2270 * "buflen" should be positive.
2272 char *d_path(const struct path *path, char *buf, int buflen)
2274 char *res = buf + buflen;
2280 * We have various synthetic filesystems that never get mounted. On
2281 * these filesystems dentries are never used for lookup purposes, and
2282 * thus don't need to be hashed. They also don't need a name until a
2283 * user wants to identify the object in /proc/pid/fd/. The little hack
2284 * below allows us to generate a name for these objects on demand:
2286 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2287 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2289 get_fs_root(current->fs, &root);
2290 spin_lock(&dcache_lock);
2292 error = path_with_deleted(path, &tmp, &res, &buflen);
2294 res = ERR_PTR(error);
2295 spin_unlock(&dcache_lock);
2299 EXPORT_SYMBOL(d_path);
2302 * d_path_with_unreachable - return the path of a dentry
2303 * @path: path to report
2304 * @buf: buffer to return value in
2305 * @buflen: buffer length
2307 * The difference from d_path() is that this prepends "(unreachable)"
2308 * to paths which are unreachable from the current process' root.
2310 char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2312 char *res = buf + buflen;
2317 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2318 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2320 get_fs_root(current->fs, &root);
2321 spin_lock(&dcache_lock);
2323 error = path_with_deleted(path, &tmp, &res, &buflen);
2324 if (!error && !path_equal(&tmp, &root))
2325 error = prepend_unreachable(&res, &buflen);
2326 spin_unlock(&dcache_lock);
2329 res = ERR_PTR(error);
2335 * Helper function for dentry_operations.d_dname() members
2337 char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2338 const char *fmt, ...)
2344 va_start(args, fmt);
2345 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2348 if (sz > sizeof(temp) || sz > buflen)
2349 return ERR_PTR(-ENAMETOOLONG);
2351 buffer += buflen - sz;
2352 return memcpy(buffer, temp, sz);
2356 * Write full pathname from the root of the filesystem into the buffer.
2358 static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
2360 char *end = buf + buflen;
2363 prepend(&end, &buflen, "\0", 1);
2370 while (!IS_ROOT(dentry)) {
2371 struct dentry *parent = dentry->d_parent;
2374 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
2375 (prepend(&end, &buflen, "/", 1) != 0))
2383 return ERR_PTR(-ENAMETOOLONG);
2386 char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2390 spin_lock(&dcache_lock);
2391 retval = __dentry_path(dentry, buf, buflen);
2392 spin_unlock(&dcache_lock);
2396 EXPORT_SYMBOL(dentry_path_raw);
2398 char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2403 spin_lock(&dcache_lock);
2404 if (d_unlinked(dentry)) {
2406 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2410 retval = __dentry_path(dentry, buf, buflen);
2411 spin_unlock(&dcache_lock);
2412 if (!IS_ERR(retval) && p)
2413 *p = '/'; /* restore '/' overriden with '\0' */
2416 spin_unlock(&dcache_lock);
2417 return ERR_PTR(-ENAMETOOLONG);
2421 * NOTE! The user-level library version returns a
2422 * character pointer. The kernel system call just
2423 * returns the length of the buffer filled (which
2424 * includes the ending '\0' character), or a negative
2425 * error value. So libc would do something like
2427 * char *getcwd(char * buf, size_t size)
2431 * retval = sys_getcwd(buf, size);
2438 SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
2441 struct path pwd, root;
2442 char *page = (char *) __get_free_page(GFP_USER);
2447 get_fs_root_and_pwd(current->fs, &root, &pwd);
2450 spin_lock(&dcache_lock);
2451 if (!d_unlinked(pwd.dentry)) {
2453 struct path tmp = root;
2454 char *cwd = page + PAGE_SIZE;
2455 int buflen = PAGE_SIZE;
2457 prepend(&cwd, &buflen, "\0", 1);
2458 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
2459 spin_unlock(&dcache_lock);
2464 /* Unreachable from current root */
2465 if (!path_equal(&tmp, &root)) {
2466 error = prepend_unreachable(&cwd, &buflen);
2472 len = PAGE_SIZE + page - cwd;
2475 if (copy_to_user(buf, cwd, len))
2479 spin_unlock(&dcache_lock);
2484 free_page((unsigned long) page);
2489 * Test whether new_dentry is a subdirectory of old_dentry.
2491 * Trivially implemented using the dcache structure
2495 * is_subdir - is new dentry a subdirectory of old_dentry
2496 * @new_dentry: new dentry
2497 * @old_dentry: old dentry
2499 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2500 * Returns 0 otherwise.
2501 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2504 int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
2509 if (new_dentry == old_dentry)
2513 * Need rcu_readlock to protect against the d_parent trashing
2518 /* for restarting inner loop in case of seq retry */
2519 seq = read_seqbegin(&rename_lock);
2520 if (d_ancestor(old_dentry, new_dentry))
2524 } while (read_seqretry(&rename_lock, seq));
2530 int path_is_under(struct path *path1, struct path *path2)
2532 struct vfsmount *mnt = path1->mnt;
2533 struct dentry *dentry = path1->dentry;
2536 br_read_lock(vfsmount_lock);
2537 if (mnt != path2->mnt) {
2539 if (mnt->mnt_parent == mnt) {
2540 br_read_unlock(vfsmount_lock);
2543 if (mnt->mnt_parent == path2->mnt)
2545 mnt = mnt->mnt_parent;
2547 dentry = mnt->mnt_mountpoint;
2549 res = is_subdir(dentry, path2->dentry);
2550 br_read_unlock(vfsmount_lock);
2553 EXPORT_SYMBOL(path_is_under);
2555 void d_genocide(struct dentry *root)
2557 struct dentry *this_parent = root;
2558 struct list_head *next;
2560 spin_lock(&dcache_lock);
2562 next = this_parent->d_subdirs.next;
2564 while (next != &this_parent->d_subdirs) {
2565 struct list_head *tmp = next;
2566 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
2568 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2569 if (d_unhashed(dentry) || !dentry->d_inode) {
2570 spin_unlock(&dentry->d_lock);
2573 if (!list_empty(&dentry->d_subdirs)) {
2574 spin_unlock(&dentry->d_lock);
2575 this_parent = dentry;
2579 spin_unlock(&dentry->d_lock);
2581 if (this_parent != root) {
2582 next = this_parent->d_u.d_child.next;
2583 spin_lock(&this_parent->d_lock);
2584 this_parent->d_count--;
2585 spin_unlock(&this_parent->d_lock);
2586 this_parent = this_parent->d_parent;
2589 spin_unlock(&dcache_lock);
2593 * find_inode_number - check for dentry with name
2594 * @dir: directory to check
2595 * @name: Name to find.
2597 * Check whether a dentry already exists for the given name,
2598 * and return the inode number if it has an inode. Otherwise
2601 * This routine is used to post-process directory listings for
2602 * filesystems using synthetic inode numbers, and is necessary
2603 * to keep getcwd() working.
2606 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2608 struct dentry * dentry;
2611 dentry = d_hash_and_lookup(dir, name);
2613 if (dentry->d_inode)
2614 ino = dentry->d_inode->i_ino;
2619 EXPORT_SYMBOL(find_inode_number);
2621 static __initdata unsigned long dhash_entries;
2622 static int __init set_dhash_entries(char *str)
2626 dhash_entries = simple_strtoul(str, &str, 0);
2629 __setup("dhash_entries=", set_dhash_entries);
2631 static void __init dcache_init_early(void)
2635 /* If hashes are distributed across NUMA nodes, defer
2636 * hash allocation until vmalloc space is available.
2642 alloc_large_system_hash("Dentry cache",
2643 sizeof(struct hlist_head),
2651 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2652 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2655 static void __init dcache_init(void)
2660 * A constructor could be added for stable state like the lists,
2661 * but it is probably not worth it because of the cache nature
2664 dentry_cache = KMEM_CACHE(dentry,
2665 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
2667 register_shrinker(&dcache_shrinker);
2669 /* Hash may have been set up in dcache_init_early */
2674 alloc_large_system_hash("Dentry cache",
2675 sizeof(struct hlist_head),
2683 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2684 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2687 /* SLAB cache for __getname() consumers */
2688 struct kmem_cache *names_cachep __read_mostly;
2689 EXPORT_SYMBOL(names_cachep);
2691 EXPORT_SYMBOL(d_genocide);
2693 void __init vfs_caches_init_early(void)
2695 dcache_init_early();
2699 void __init vfs_caches_init(unsigned long mempages)
2701 unsigned long reserve;
2703 /* Base hash sizes on available memory, with a reserve equal to
2704 150% of current kernel size */
2706 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2707 mempages -= reserve;
2709 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2710 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2714 files_init(mempages);