4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
10 * Notes on the allocation strategy:
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
17 #include <linux/syscalls.h>
18 #include <linux/string.h>
21 #include <linux/fsnotify.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/hash.h>
25 #include <linux/cache.h>
26 #include <linux/module.h>
27 #include <linux/mount.h>
28 #include <linux/file.h>
29 #include <asm/uaccess.h>
30 #include <linux/security.h>
31 #include <linux/seqlock.h>
32 #include <linux/swap.h>
33 #include <linux/bootmem.h>
34 #include <linux/fs_struct.h>
35 #include <linux/hardirq.h>
40 * dcache_inode_lock protects:
41 * - i_dentry, d_alias, d_inode
42 * dcache_hash_lock protects:
43 * - the dcache hash table, s_anon lists
44 * dcache_lru_lock protects:
45 * - the dcache lru lists and counters
52 * - d_parent and d_subdirs
53 * - childrens' d_child and d_parent
62 * If there is an ancestor relationship:
63 * dentry->d_parent->...->d_parent->d_lock
65 * dentry->d_parent->d_lock
68 * If no ancestor relationship:
69 * if (dentry1 < dentry2)
73 int sysctl_vfs_cache_pressure __read_mostly = 100;
74 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
76 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_inode_lock);
77 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_hash_lock);
78 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
79 __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
81 EXPORT_SYMBOL(rename_lock);
82 EXPORT_SYMBOL(dcache_inode_lock);
84 static struct kmem_cache *dentry_cache __read_mostly;
86 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
89 * This is the single most critical data structure when it comes
90 * to the dcache: the hashtable for lookups. Somebody should try
91 * to make this good - I've just made it work.
93 * This hash-function tries to avoid losing too many bits of hash
94 * information, yet avoid using a prime hash-size or similar.
96 #define D_HASHBITS d_hash_shift
97 #define D_HASHMASK d_hash_mask
99 static unsigned int d_hash_mask __read_mostly;
100 static unsigned int d_hash_shift __read_mostly;
101 static struct hlist_head *dentry_hashtable __read_mostly;
103 /* Statistics gathering. */
104 struct dentry_stat_t dentry_stat = {
108 static DEFINE_PER_CPU(unsigned int, nr_dentry);
110 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
111 static int get_nr_dentry(void)
115 for_each_possible_cpu(i)
116 sum += per_cpu(nr_dentry, i);
117 return sum < 0 ? 0 : sum;
120 int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
121 size_t *lenp, loff_t *ppos)
123 dentry_stat.nr_dentry = get_nr_dentry();
124 return proc_dointvec(table, write, buffer, lenp, ppos);
128 static void __d_free(struct rcu_head *head)
130 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
132 WARN_ON(!list_empty(&dentry->d_alias));
133 if (dname_external(dentry))
134 kfree(dentry->d_name.name);
135 kmem_cache_free(dentry_cache, dentry);
141 static void d_free(struct dentry *dentry)
143 BUG_ON(dentry->d_count);
144 this_cpu_dec(nr_dentry);
145 if (dentry->d_op && dentry->d_op->d_release)
146 dentry->d_op->d_release(dentry);
148 /* if dentry was never inserted into hash, immediate free is OK */
149 if (hlist_unhashed(&dentry->d_hash))
150 __d_free(&dentry->d_u.d_rcu);
152 call_rcu(&dentry->d_u.d_rcu, __d_free);
156 * Release the dentry's inode, using the filesystem
157 * d_iput() operation if defined.
159 static void dentry_iput(struct dentry * dentry)
160 __releases(dentry->d_lock)
161 __releases(dcache_inode_lock)
163 struct inode *inode = dentry->d_inode;
165 dentry->d_inode = NULL;
166 list_del_init(&dentry->d_alias);
167 spin_unlock(&dentry->d_lock);
168 spin_unlock(&dcache_inode_lock);
170 fsnotify_inoderemove(inode);
171 if (dentry->d_op && dentry->d_op->d_iput)
172 dentry->d_op->d_iput(dentry, inode);
176 spin_unlock(&dentry->d_lock);
177 spin_unlock(&dcache_inode_lock);
182 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
184 static void dentry_lru_add(struct dentry *dentry)
186 if (list_empty(&dentry->d_lru)) {
187 spin_lock(&dcache_lru_lock);
188 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
189 dentry->d_sb->s_nr_dentry_unused++;
190 dentry_stat.nr_unused++;
191 spin_unlock(&dcache_lru_lock);
195 static void __dentry_lru_del(struct dentry *dentry)
197 list_del_init(&dentry->d_lru);
198 dentry->d_sb->s_nr_dentry_unused--;
199 dentry_stat.nr_unused--;
202 static void dentry_lru_del(struct dentry *dentry)
204 if (!list_empty(&dentry->d_lru)) {
205 spin_lock(&dcache_lru_lock);
206 __dentry_lru_del(dentry);
207 spin_unlock(&dcache_lru_lock);
211 static void dentry_lru_move_tail(struct dentry *dentry)
213 spin_lock(&dcache_lru_lock);
214 if (list_empty(&dentry->d_lru)) {
215 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
216 dentry->d_sb->s_nr_dentry_unused++;
217 dentry_stat.nr_unused++;
219 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
221 spin_unlock(&dcache_lru_lock);
225 * d_kill - kill dentry and return parent
226 * @dentry: dentry to kill
228 * The dentry must already be unhashed and removed from the LRU.
230 * If this is the root of the dentry tree, return NULL.
232 * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by
235 static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
236 __releases(dentry->d_lock)
237 __releases(parent->d_lock)
238 __releases(dcache_inode_lock)
240 dentry->d_parent = NULL;
241 list_del(&dentry->d_u.d_child);
243 spin_unlock(&parent->d_lock);
246 * dentry_iput drops the locks, at which point nobody (except
247 * transient RCU lookups) can reach this dentry.
254 * d_drop - drop a dentry
255 * @dentry: dentry to drop
257 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
258 * be found through a VFS lookup any more. Note that this is different from
259 * deleting the dentry - d_delete will try to mark the dentry negative if
260 * possible, giving a successful _negative_ lookup, while d_drop will
261 * just make the cache lookup fail.
263 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
264 * reason (NFS timeouts or autofs deletes).
266 * __d_drop requires dentry->d_lock.
268 void __d_drop(struct dentry *dentry)
270 if (!(dentry->d_flags & DCACHE_UNHASHED)) {
271 dentry->d_flags |= DCACHE_UNHASHED;
272 spin_lock(&dcache_hash_lock);
273 hlist_del_rcu(&dentry->d_hash);
274 spin_unlock(&dcache_hash_lock);
277 EXPORT_SYMBOL(__d_drop);
279 void d_drop(struct dentry *dentry)
281 spin_lock(&dentry->d_lock);
283 spin_unlock(&dentry->d_lock);
285 EXPORT_SYMBOL(d_drop);
288 * Finish off a dentry we've decided to kill.
289 * dentry->d_lock must be held, returns with it unlocked.
290 * If ref is non-zero, then decrement the refcount too.
291 * Returns dentry requiring refcount drop, or NULL if we're done.
293 static inline struct dentry *dentry_kill(struct dentry *dentry, int ref)
294 __releases(dentry->d_lock)
296 struct dentry *parent;
298 if (!spin_trylock(&dcache_inode_lock)) {
300 spin_unlock(&dentry->d_lock);
302 return dentry; /* try again with same dentry */
307 parent = dentry->d_parent;
308 if (parent && !spin_trylock(&parent->d_lock)) {
309 spin_unlock(&dcache_inode_lock);
314 /* if dentry was on the d_lru list delete it from there */
315 dentry_lru_del(dentry);
316 /* if it was on the hash then remove it */
318 return d_kill(dentry, parent);
324 * This is complicated by the fact that we do not want to put
325 * dentries that are no longer on any hash chain on the unused
326 * list: we'd much rather just get rid of them immediately.
328 * However, that implies that we have to traverse the dentry
329 * tree upwards to the parents which might _also_ now be
330 * scheduled for deletion (it may have been only waiting for
331 * its last child to go away).
333 * This tail recursion is done by hand as we don't want to depend
334 * on the compiler to always get this right (gcc generally doesn't).
335 * Real recursion would eat up our stack space.
339 * dput - release a dentry
340 * @dentry: dentry to release
342 * Release a dentry. This will drop the usage count and if appropriate
343 * call the dentry unlink method as well as removing it from the queues and
344 * releasing its resources. If the parent dentries were scheduled for release
345 * they too may now get deleted.
347 void dput(struct dentry *dentry)
353 if (dentry->d_count == 1)
355 spin_lock(&dentry->d_lock);
356 BUG_ON(!dentry->d_count);
357 if (dentry->d_count > 1) {
359 spin_unlock(&dentry->d_lock);
363 if (dentry->d_op && dentry->d_op->d_delete) {
364 if (dentry->d_op->d_delete(dentry))
368 /* Unreachable? Get rid of it */
369 if (d_unhashed(dentry))
372 /* Otherwise leave it cached and ensure it's on the LRU */
373 dentry->d_flags |= DCACHE_REFERENCED;
374 dentry_lru_add(dentry);
377 spin_unlock(&dentry->d_lock);
381 dentry = dentry_kill(dentry, 1);
388 * d_invalidate - invalidate a dentry
389 * @dentry: dentry to invalidate
391 * Try to invalidate the dentry if it turns out to be
392 * possible. If there are other dentries that can be
393 * reached through this one we can't delete it and we
394 * return -EBUSY. On success we return 0.
399 int d_invalidate(struct dentry * dentry)
402 * If it's already been dropped, return OK.
404 spin_lock(&dentry->d_lock);
405 if (d_unhashed(dentry)) {
406 spin_unlock(&dentry->d_lock);
410 * Check whether to do a partial shrink_dcache
411 * to get rid of unused child entries.
413 if (!list_empty(&dentry->d_subdirs)) {
414 spin_unlock(&dentry->d_lock);
415 shrink_dcache_parent(dentry);
416 spin_lock(&dentry->d_lock);
420 * Somebody else still using it?
422 * If it's a directory, we can't drop it
423 * for fear of somebody re-populating it
424 * with children (even though dropping it
425 * would make it unreachable from the root,
426 * we might still populate it if it was a
427 * working directory or similar).
429 if (dentry->d_count > 1) {
430 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
431 spin_unlock(&dentry->d_lock);
437 spin_unlock(&dentry->d_lock);
440 EXPORT_SYMBOL(d_invalidate);
442 /* This must be called with d_lock held */
443 static inline void __dget_dlock(struct dentry *dentry)
448 static inline void __dget(struct dentry *dentry)
450 spin_lock(&dentry->d_lock);
451 __dget_dlock(dentry);
452 spin_unlock(&dentry->d_lock);
455 struct dentry *dget_parent(struct dentry *dentry)
461 * Don't need rcu_dereference because we re-check it was correct under
465 ret = dentry->d_parent;
470 spin_lock(&ret->d_lock);
471 if (unlikely(ret != dentry->d_parent)) {
472 spin_unlock(&ret->d_lock);
477 BUG_ON(!ret->d_count);
479 spin_unlock(&ret->d_lock);
483 EXPORT_SYMBOL(dget_parent);
486 * d_find_alias - grab a hashed alias of inode
487 * @inode: inode in question
488 * @want_discon: flag, used by d_splice_alias, to request
489 * that only a DISCONNECTED alias be returned.
491 * If inode has a hashed alias, or is a directory and has any alias,
492 * acquire the reference to alias and return it. Otherwise return NULL.
493 * Notice that if inode is a directory there can be only one alias and
494 * it can be unhashed only if it has no children, or if it is the root
497 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
498 * any other hashed alias over that one unless @want_discon is set,
499 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
501 static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
503 struct dentry *alias, *discon_alias;
507 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
508 spin_lock(&alias->d_lock);
509 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
510 if (IS_ROOT(alias) &&
511 (alias->d_flags & DCACHE_DISCONNECTED)) {
512 discon_alias = alias;
513 } else if (!want_discon) {
515 spin_unlock(&alias->d_lock);
519 spin_unlock(&alias->d_lock);
522 alias = discon_alias;
523 spin_lock(&alias->d_lock);
524 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
525 if (IS_ROOT(alias) &&
526 (alias->d_flags & DCACHE_DISCONNECTED)) {
528 spin_unlock(&alias->d_lock);
532 spin_unlock(&alias->d_lock);
538 struct dentry *d_find_alias(struct inode *inode)
540 struct dentry *de = NULL;
542 if (!list_empty(&inode->i_dentry)) {
543 spin_lock(&dcache_inode_lock);
544 de = __d_find_alias(inode, 0);
545 spin_unlock(&dcache_inode_lock);
549 EXPORT_SYMBOL(d_find_alias);
552 * Try to kill dentries associated with this inode.
553 * WARNING: you must own a reference to inode.
555 void d_prune_aliases(struct inode *inode)
557 struct dentry *dentry;
559 spin_lock(&dcache_inode_lock);
560 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
561 spin_lock(&dentry->d_lock);
562 if (!dentry->d_count) {
563 __dget_dlock(dentry);
565 spin_unlock(&dentry->d_lock);
566 spin_unlock(&dcache_inode_lock);
570 spin_unlock(&dentry->d_lock);
572 spin_unlock(&dcache_inode_lock);
574 EXPORT_SYMBOL(d_prune_aliases);
577 * Try to throw away a dentry - free the inode, dput the parent.
578 * Requires dentry->d_lock is held, and dentry->d_count == 0.
579 * Releases dentry->d_lock.
581 * This may fail if locks cannot be acquired no problem, just try again.
583 static void try_prune_one_dentry(struct dentry *dentry)
584 __releases(dentry->d_lock)
586 struct dentry *parent;
588 parent = dentry_kill(dentry, 0);
590 * If dentry_kill returns NULL, we have nothing more to do.
591 * if it returns the same dentry, trylocks failed. In either
592 * case, just loop again.
594 * Otherwise, we need to prune ancestors too. This is necessary
595 * to prevent quadratic behavior of shrink_dcache_parent(), but
596 * is also expected to be beneficial in reducing dentry cache
601 if (parent == dentry)
604 /* Prune ancestors. */
607 spin_lock(&dentry->d_lock);
608 if (dentry->d_count > 1) {
610 spin_unlock(&dentry->d_lock);
613 dentry = dentry_kill(dentry, 1);
617 static void shrink_dentry_list(struct list_head *list)
619 struct dentry *dentry;
623 dentry = list_entry_rcu(list->prev, struct dentry, d_lru);
624 if (&dentry->d_lru == list)
626 spin_lock(&dentry->d_lock);
627 if (dentry != list_entry(list->prev, struct dentry, d_lru)) {
628 spin_unlock(&dentry->d_lock);
633 * We found an inuse dentry which was not removed from
634 * the LRU because of laziness during lookup. Do not free
635 * it - just keep it off the LRU list.
637 if (dentry->d_count) {
638 dentry_lru_del(dentry);
639 spin_unlock(&dentry->d_lock);
645 try_prune_one_dentry(dentry);
653 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
654 * @sb: superblock to shrink dentry LRU.
655 * @count: number of entries to prune
656 * @flags: flags to control the dentry processing
658 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
660 static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
662 /* called from prune_dcache() and shrink_dcache_parent() */
663 struct dentry *dentry;
664 LIST_HEAD(referenced);
669 spin_lock(&dcache_lru_lock);
670 while (!list_empty(&sb->s_dentry_lru)) {
671 dentry = list_entry(sb->s_dentry_lru.prev,
672 struct dentry, d_lru);
673 BUG_ON(dentry->d_sb != sb);
675 if (!spin_trylock(&dentry->d_lock)) {
676 spin_unlock(&dcache_lru_lock);
682 * If we are honouring the DCACHE_REFERENCED flag and the
683 * dentry has this flag set, don't free it. Clear the flag
684 * and put it back on the LRU.
686 if (flags & DCACHE_REFERENCED &&
687 dentry->d_flags & DCACHE_REFERENCED) {
688 dentry->d_flags &= ~DCACHE_REFERENCED;
689 list_move(&dentry->d_lru, &referenced);
690 spin_unlock(&dentry->d_lock);
692 list_move_tail(&dentry->d_lru, &tmp);
693 spin_unlock(&dentry->d_lock);
697 cond_resched_lock(&dcache_lru_lock);
699 if (!list_empty(&referenced))
700 list_splice(&referenced, &sb->s_dentry_lru);
701 spin_unlock(&dcache_lru_lock);
703 shrink_dentry_list(&tmp);
709 * prune_dcache - shrink the dcache
710 * @count: number of entries to try to free
712 * Shrink the dcache. This is done when we need more memory, or simply when we
713 * need to unmount something (at which point we need to unuse all dentries).
715 * This function may fail to free any resources if all the dentries are in use.
717 static void prune_dcache(int count)
719 struct super_block *sb, *p = NULL;
721 int unused = dentry_stat.nr_unused;
725 if (unused == 0 || count == 0)
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 __shrink_dcache_sb(sb, &w_count,
767 up_read(&sb->s_umount);
774 /* more work left to do? */
780 spin_unlock(&sb_lock);
784 * shrink_dcache_sb - shrink dcache for a superblock
787 * Shrink the dcache for the specified super block. This is used to free
788 * the dcache before unmounting a file system.
790 void shrink_dcache_sb(struct super_block *sb)
794 spin_lock(&dcache_lru_lock);
795 while (!list_empty(&sb->s_dentry_lru)) {
796 list_splice_init(&sb->s_dentry_lru, &tmp);
797 spin_unlock(&dcache_lru_lock);
798 shrink_dentry_list(&tmp);
799 spin_lock(&dcache_lru_lock);
801 spin_unlock(&dcache_lru_lock);
803 EXPORT_SYMBOL(shrink_dcache_sb);
806 * destroy a single subtree of dentries for unmount
807 * - see the comments on shrink_dcache_for_umount() for a description of the
810 static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
812 struct dentry *parent;
813 unsigned detached = 0;
815 BUG_ON(!IS_ROOT(dentry));
817 /* detach this root from the system */
818 spin_lock(&dentry->d_lock);
819 dentry_lru_del(dentry);
821 spin_unlock(&dentry->d_lock);
824 /* descend to the first leaf in the current subtree */
825 while (!list_empty(&dentry->d_subdirs)) {
828 /* this is a branch with children - detach all of them
829 * from the system in one go */
830 spin_lock(&dentry->d_lock);
831 list_for_each_entry(loop, &dentry->d_subdirs,
833 spin_lock_nested(&loop->d_lock,
834 DENTRY_D_LOCK_NESTED);
835 dentry_lru_del(loop);
837 spin_unlock(&loop->d_lock);
839 spin_unlock(&dentry->d_lock);
841 /* move to the first child */
842 dentry = list_entry(dentry->d_subdirs.next,
843 struct dentry, d_u.d_child);
846 /* consume the dentries from this leaf up through its parents
847 * until we find one with children or run out altogether */
851 if (dentry->d_count != 0) {
853 "BUG: Dentry %p{i=%lx,n=%s}"
855 " [unmount of %s %s]\n",
858 dentry->d_inode->i_ino : 0UL,
861 dentry->d_sb->s_type->name,
866 if (IS_ROOT(dentry)) {
868 list_del(&dentry->d_u.d_child);
870 parent = dentry->d_parent;
871 spin_lock(&parent->d_lock);
873 list_del(&dentry->d_u.d_child);
874 spin_unlock(&parent->d_lock);
879 inode = dentry->d_inode;
881 dentry->d_inode = NULL;
882 list_del_init(&dentry->d_alias);
883 if (dentry->d_op && dentry->d_op->d_iput)
884 dentry->d_op->d_iput(dentry, inode);
891 /* finished when we fall off the top of the tree,
892 * otherwise we ascend to the parent and move to the
893 * next sibling if there is one */
897 } while (list_empty(&dentry->d_subdirs));
899 dentry = list_entry(dentry->d_subdirs.next,
900 struct dentry, d_u.d_child);
905 * destroy the dentries attached to a superblock on unmounting
906 * - we don't need to use dentry->d_lock because:
907 * - the superblock is detached from all mountings and open files, so the
908 * dentry trees will not be rearranged by the VFS
909 * - s_umount is write-locked, so the memory pressure shrinker will ignore
910 * any dentries belonging to this superblock that it comes across
911 * - the filesystem itself is no longer permitted to rearrange the dentries
914 void shrink_dcache_for_umount(struct super_block *sb)
916 struct dentry *dentry;
918 if (down_read_trylock(&sb->s_umount))
923 spin_lock(&dentry->d_lock);
925 spin_unlock(&dentry->d_lock);
926 shrink_dcache_for_umount_subtree(dentry);
928 while (!hlist_empty(&sb->s_anon)) {
929 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
930 shrink_dcache_for_umount_subtree(dentry);
935 * Search for at least 1 mount point in the dentry's subdirs.
936 * We descend to the next level whenever the d_subdirs
937 * list is non-empty and continue searching.
941 * have_submounts - check for mounts over a dentry
942 * @parent: dentry to check.
944 * Return true if the parent or its subdirectories contain
947 int have_submounts(struct dentry *parent)
949 struct dentry *this_parent;
950 struct list_head *next;
954 seq = read_seqbegin(&rename_lock);
956 this_parent = parent;
958 if (d_mountpoint(parent))
960 spin_lock(&this_parent->d_lock);
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);
969 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
970 /* Have we found a mount point ? */
971 if (d_mountpoint(dentry)) {
972 spin_unlock(&dentry->d_lock);
973 spin_unlock(&this_parent->d_lock);
976 if (!list_empty(&dentry->d_subdirs)) {
977 spin_unlock(&this_parent->d_lock);
978 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
979 this_parent = dentry;
980 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
983 spin_unlock(&dentry->d_lock);
986 * All done at this level ... ascend and resume the search.
988 if (this_parent != parent) {
990 struct dentry *child;
992 tmp = this_parent->d_parent;
994 spin_unlock(&this_parent->d_lock);
997 spin_lock(&this_parent->d_lock);
998 /* might go back up the wrong parent if we have had a rename
1000 if (this_parent != child->d_parent ||
1001 (!locked && read_seqretry(&rename_lock, seq))) {
1002 spin_unlock(&this_parent->d_lock);
1007 next = child->d_u.d_child.next;
1010 spin_unlock(&this_parent->d_lock);
1011 if (!locked && read_seqretry(&rename_lock, seq))
1014 write_sequnlock(&rename_lock);
1015 return 0; /* No mount points found in tree */
1017 if (!locked && read_seqretry(&rename_lock, seq))
1020 write_sequnlock(&rename_lock);
1025 write_seqlock(&rename_lock);
1028 EXPORT_SYMBOL(have_submounts);
1031 * Search the dentry child list for the specified parent,
1032 * and move any unused dentries to the end of the unused
1033 * list for prune_dcache(). We descend to the next level
1034 * whenever the d_subdirs list is non-empty and continue
1037 * It returns zero iff there are no unused children,
1038 * otherwise it returns the number of children moved to
1039 * the end of the unused list. This may not be the total
1040 * number of unused children, because select_parent can
1041 * drop the lock and return early due to latency
1044 static int select_parent(struct dentry * parent)
1046 struct dentry *this_parent;
1047 struct list_head *next;
1052 seq = read_seqbegin(&rename_lock);
1054 this_parent = parent;
1055 spin_lock(&this_parent->d_lock);
1057 next = this_parent->d_subdirs.next;
1059 while (next != &this_parent->d_subdirs) {
1060 struct list_head *tmp = next;
1061 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1064 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1067 * move only zero ref count dentries to the end
1068 * of the unused list for prune_dcache
1070 if (!dentry->d_count) {
1071 dentry_lru_move_tail(dentry);
1074 dentry_lru_del(dentry);
1078 * We can return to the caller if we have found some (this
1079 * ensures forward progress). We'll be coming back to find
1082 if (found && need_resched()) {
1083 spin_unlock(&dentry->d_lock);
1088 * Descend a level if the d_subdirs list is non-empty.
1090 if (!list_empty(&dentry->d_subdirs)) {
1091 spin_unlock(&this_parent->d_lock);
1092 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
1093 this_parent = dentry;
1094 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1098 spin_unlock(&dentry->d_lock);
1101 * All done at this level ... ascend and resume the search.
1103 if (this_parent != parent) {
1105 struct dentry *child;
1107 tmp = this_parent->d_parent;
1109 spin_unlock(&this_parent->d_lock);
1110 child = this_parent;
1112 spin_lock(&this_parent->d_lock);
1113 /* might go back up the wrong parent if we have had a rename
1115 if (this_parent != child->d_parent ||
1116 (!locked && read_seqretry(&rename_lock, seq))) {
1117 spin_unlock(&this_parent->d_lock);
1122 next = child->d_u.d_child.next;
1126 spin_unlock(&this_parent->d_lock);
1127 if (!locked && read_seqretry(&rename_lock, seq))
1130 write_sequnlock(&rename_lock);
1137 write_seqlock(&rename_lock);
1142 * shrink_dcache_parent - prune dcache
1143 * @parent: parent of entries to prune
1145 * Prune the dcache to remove unused children of the parent dentry.
1148 void shrink_dcache_parent(struct dentry * parent)
1150 struct super_block *sb = parent->d_sb;
1153 while ((found = select_parent(parent)) != 0)
1154 __shrink_dcache_sb(sb, &found, 0);
1156 EXPORT_SYMBOL(shrink_dcache_parent);
1159 * Scan `nr' dentries and return the number which remain.
1161 * We need to avoid reentering the filesystem if the caller is performing a
1162 * GFP_NOFS allocation attempt. One example deadlock is:
1164 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1165 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1166 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1168 * In this case we return -1 to tell the caller that we baled.
1170 static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
1173 if (!(gfp_mask & __GFP_FS))
1178 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
1181 static struct shrinker dcache_shrinker = {
1182 .shrink = shrink_dcache_memory,
1183 .seeks = DEFAULT_SEEKS,
1187 * d_alloc - allocate a dcache entry
1188 * @parent: parent of entry to allocate
1189 * @name: qstr of the name
1191 * Allocates a dentry. It returns %NULL if there is insufficient memory
1192 * available. On a success the dentry is returned. The name passed in is
1193 * copied and the copy passed in may be reused after this call.
1196 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1198 struct dentry *dentry;
1201 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
1205 if (name->len > DNAME_INLINE_LEN-1) {
1206 dname = kmalloc(name->len + 1, GFP_KERNEL);
1208 kmem_cache_free(dentry_cache, dentry);
1212 dname = dentry->d_iname;
1214 dentry->d_name.name = dname;
1216 dentry->d_name.len = name->len;
1217 dentry->d_name.hash = name->hash;
1218 memcpy(dname, name->name, name->len);
1219 dname[name->len] = 0;
1221 dentry->d_count = 1;
1222 dentry->d_flags = DCACHE_UNHASHED;
1223 spin_lock_init(&dentry->d_lock);
1224 dentry->d_inode = NULL;
1225 dentry->d_parent = NULL;
1226 dentry->d_sb = NULL;
1227 dentry->d_op = NULL;
1228 dentry->d_fsdata = NULL;
1229 dentry->d_mounted = 0;
1230 INIT_HLIST_NODE(&dentry->d_hash);
1231 INIT_LIST_HEAD(&dentry->d_lru);
1232 INIT_LIST_HEAD(&dentry->d_subdirs);
1233 INIT_LIST_HEAD(&dentry->d_alias);
1234 INIT_LIST_HEAD(&dentry->d_u.d_child);
1237 spin_lock(&parent->d_lock);
1239 * don't need child lock because it is not subject
1240 * to concurrency here
1242 __dget_dlock(parent);
1243 dentry->d_parent = parent;
1244 dentry->d_sb = parent->d_sb;
1245 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
1246 spin_unlock(&parent->d_lock);
1249 this_cpu_inc(nr_dentry);
1253 EXPORT_SYMBOL(d_alloc);
1255 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1260 q.len = strlen(name);
1261 q.hash = full_name_hash(q.name, q.len);
1262 return d_alloc(parent, &q);
1264 EXPORT_SYMBOL(d_alloc_name);
1266 static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1268 spin_lock(&dentry->d_lock);
1270 list_add(&dentry->d_alias, &inode->i_dentry);
1271 dentry->d_inode = inode;
1272 spin_unlock(&dentry->d_lock);
1273 fsnotify_d_instantiate(dentry, inode);
1277 * d_instantiate - fill in inode information for a dentry
1278 * @entry: dentry to complete
1279 * @inode: inode to attach to this dentry
1281 * Fill in inode information in the entry.
1283 * This turns negative dentries into productive full members
1286 * NOTE! This assumes that the inode count has been incremented
1287 * (or otherwise set) by the caller to indicate that it is now
1288 * in use by the dcache.
1291 void d_instantiate(struct dentry *entry, struct inode * inode)
1293 BUG_ON(!list_empty(&entry->d_alias));
1294 spin_lock(&dcache_inode_lock);
1295 __d_instantiate(entry, inode);
1296 spin_unlock(&dcache_inode_lock);
1297 security_d_instantiate(entry, inode);
1299 EXPORT_SYMBOL(d_instantiate);
1302 * d_instantiate_unique - instantiate a non-aliased dentry
1303 * @entry: dentry to instantiate
1304 * @inode: inode to attach to this dentry
1306 * Fill in inode information in the entry. On success, it returns NULL.
1307 * If an unhashed alias of "entry" already exists, then we return the
1308 * aliased dentry instead and drop one reference to inode.
1310 * Note that in order to avoid conflicts with rename() etc, the caller
1311 * had better be holding the parent directory semaphore.
1313 * This also assumes that the inode count has been incremented
1314 * (or otherwise set) by the caller to indicate that it is now
1315 * in use by the dcache.
1317 static struct dentry *__d_instantiate_unique(struct dentry *entry,
1318 struct inode *inode)
1320 struct dentry *alias;
1321 int len = entry->d_name.len;
1322 const char *name = entry->d_name.name;
1323 unsigned int hash = entry->d_name.hash;
1326 __d_instantiate(entry, NULL);
1330 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1331 struct qstr *qstr = &alias->d_name;
1334 * Don't need alias->d_lock here, because aliases with
1335 * d_parent == entry->d_parent are not subject to name or
1336 * parent changes, because the parent inode i_mutex is held.
1338 if (qstr->hash != hash)
1340 if (alias->d_parent != entry->d_parent)
1342 if (qstr->len != len)
1344 if (memcmp(qstr->name, name, len))
1350 __d_instantiate(entry, inode);
1354 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1356 struct dentry *result;
1358 BUG_ON(!list_empty(&entry->d_alias));
1360 spin_lock(&dcache_inode_lock);
1361 result = __d_instantiate_unique(entry, inode);
1362 spin_unlock(&dcache_inode_lock);
1365 security_d_instantiate(entry, inode);
1369 BUG_ON(!d_unhashed(result));
1374 EXPORT_SYMBOL(d_instantiate_unique);
1377 * d_alloc_root - allocate root dentry
1378 * @root_inode: inode to allocate the root for
1380 * Allocate a root ("/") dentry for the inode given. The inode is
1381 * instantiated and returned. %NULL is returned if there is insufficient
1382 * memory or the inode passed is %NULL.
1385 struct dentry * d_alloc_root(struct inode * root_inode)
1387 struct dentry *res = NULL;
1390 static const struct qstr name = { .name = "/", .len = 1 };
1392 res = d_alloc(NULL, &name);
1394 res->d_sb = root_inode->i_sb;
1395 res->d_parent = res;
1396 d_instantiate(res, root_inode);
1401 EXPORT_SYMBOL(d_alloc_root);
1403 static inline struct hlist_head *d_hash(struct dentry *parent,
1406 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1407 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1408 return dentry_hashtable + (hash & D_HASHMASK);
1412 * d_obtain_alias - find or allocate a dentry for a given inode
1413 * @inode: inode to allocate the dentry for
1415 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1416 * similar open by handle operations. The returned dentry may be anonymous,
1417 * or may have a full name (if the inode was already in the cache).
1419 * When called on a directory inode, we must ensure that the inode only ever
1420 * has one dentry. If a dentry is found, that is returned instead of
1421 * allocating a new one.
1423 * On successful return, the reference to the inode has been transferred
1424 * to the dentry. In case of an error the reference on the inode is released.
1425 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1426 * be passed in and will be the error will be propagate to the return value,
1427 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1429 struct dentry *d_obtain_alias(struct inode *inode)
1431 static const struct qstr anonstring = { .name = "" };
1436 return ERR_PTR(-ESTALE);
1438 return ERR_CAST(inode);
1440 res = d_find_alias(inode);
1444 tmp = d_alloc(NULL, &anonstring);
1446 res = ERR_PTR(-ENOMEM);
1449 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1452 spin_lock(&dcache_inode_lock);
1453 res = __d_find_alias(inode, 0);
1455 spin_unlock(&dcache_inode_lock);
1460 /* attach a disconnected dentry */
1461 spin_lock(&tmp->d_lock);
1462 tmp->d_sb = inode->i_sb;
1463 tmp->d_inode = inode;
1464 tmp->d_flags |= DCACHE_DISCONNECTED;
1465 tmp->d_flags &= ~DCACHE_UNHASHED;
1466 list_add(&tmp->d_alias, &inode->i_dentry);
1467 spin_lock(&dcache_hash_lock);
1468 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1469 spin_unlock(&dcache_hash_lock);
1470 spin_unlock(&tmp->d_lock);
1471 spin_unlock(&dcache_inode_lock);
1479 EXPORT_SYMBOL(d_obtain_alias);
1482 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1483 * @inode: the inode which may have a disconnected dentry
1484 * @dentry: a negative dentry which we want to point to the inode.
1486 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1487 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1488 * and return it, else simply d_add the inode to the dentry and return NULL.
1490 * This is needed in the lookup routine of any filesystem that is exportable
1491 * (via knfsd) so that we can build dcache paths to directories effectively.
1493 * If a dentry was found and moved, then it is returned. Otherwise NULL
1494 * is returned. This matches the expected return value of ->lookup.
1497 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1499 struct dentry *new = NULL;
1501 if (inode && S_ISDIR(inode->i_mode)) {
1502 spin_lock(&dcache_inode_lock);
1503 new = __d_find_alias(inode, 1);
1505 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1506 spin_unlock(&dcache_inode_lock);
1507 security_d_instantiate(new, inode);
1508 d_move(new, dentry);
1511 /* already taking dcache_inode_lock, so d_add() by hand */
1512 __d_instantiate(dentry, inode);
1513 spin_unlock(&dcache_inode_lock);
1514 security_d_instantiate(dentry, inode);
1518 d_add(dentry, inode);
1521 EXPORT_SYMBOL(d_splice_alias);
1524 * d_add_ci - lookup or allocate new dentry with case-exact name
1525 * @inode: the inode case-insensitive lookup has found
1526 * @dentry: the negative dentry that was passed to the parent's lookup func
1527 * @name: the case-exact name to be associated with the returned dentry
1529 * This is to avoid filling the dcache with case-insensitive names to the
1530 * same inode, only the actual correct case is stored in the dcache for
1531 * case-insensitive filesystems.
1533 * For a case-insensitive lookup match and if the the case-exact dentry
1534 * already exists in in the dcache, use it and return it.
1536 * If no entry exists with the exact case name, allocate new dentry with
1537 * the exact case, and return the spliced entry.
1539 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
1543 struct dentry *found;
1547 * First check if a dentry matching the name already exists,
1548 * if not go ahead and create it now.
1550 found = d_hash_and_lookup(dentry->d_parent, name);
1552 new = d_alloc(dentry->d_parent, name);
1558 found = d_splice_alias(inode, new);
1567 * If a matching dentry exists, and it's not negative use it.
1569 * Decrement the reference count to balance the iget() done
1572 if (found->d_inode) {
1573 if (unlikely(found->d_inode != inode)) {
1574 /* This can't happen because bad inodes are unhashed. */
1575 BUG_ON(!is_bad_inode(inode));
1576 BUG_ON(!is_bad_inode(found->d_inode));
1583 * Negative dentry: instantiate it unless the inode is a directory and
1584 * already has a dentry.
1586 spin_lock(&dcache_inode_lock);
1587 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
1588 __d_instantiate(found, inode);
1589 spin_unlock(&dcache_inode_lock);
1590 security_d_instantiate(found, inode);
1595 * In case a directory already has a (disconnected) entry grab a
1596 * reference to it, move it in place and use it.
1598 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1600 spin_unlock(&dcache_inode_lock);
1601 security_d_instantiate(found, inode);
1609 return ERR_PTR(error);
1611 EXPORT_SYMBOL(d_add_ci);
1614 * d_lookup - search for a dentry
1615 * @parent: parent dentry
1616 * @name: qstr of name we wish to find
1617 * Returns: dentry, or NULL
1619 * d_lookup searches the children of the parent dentry for the name in
1620 * question. If the dentry is found its reference count is incremented and the
1621 * dentry is returned. The caller must use dput to free the entry when it has
1622 * finished using it. %NULL is returned if the dentry does not exist.
1624 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1626 struct dentry * dentry = NULL;
1630 seq = read_seqbegin(&rename_lock);
1631 dentry = __d_lookup(parent, name);
1634 } while (read_seqretry(&rename_lock, seq));
1637 EXPORT_SYMBOL(d_lookup);
1640 * __d_lookup - search for a dentry (racy)
1641 * @parent: parent dentry
1642 * @name: qstr of name we wish to find
1643 * Returns: dentry, or NULL
1645 * __d_lookup is like d_lookup, however it may (rarely) return a
1646 * false-negative result due to unrelated rename activity.
1648 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1649 * however it must be used carefully, eg. with a following d_lookup in
1650 * the case of failure.
1652 * __d_lookup callers must be commented.
1654 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1656 unsigned int len = name->len;
1657 unsigned int hash = name->hash;
1658 const unsigned char *str = name->name;
1659 struct hlist_head *head = d_hash(parent,hash);
1660 struct dentry *found = NULL;
1661 struct hlist_node *node;
1662 struct dentry *dentry;
1665 * The hash list is protected using RCU.
1667 * Take d_lock when comparing a candidate dentry, to avoid races
1670 * It is possible that concurrent renames can mess up our list
1671 * walk here and result in missing our dentry, resulting in the
1672 * false-negative result. d_lookup() protects against concurrent
1673 * renames using rename_lock seqlock.
1675 * See Documentation/vfs/dcache-locking.txt for more details.
1679 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1682 if (dentry->d_name.hash != hash)
1684 if (dentry->d_parent != parent)
1687 spin_lock(&dentry->d_lock);
1690 * Recheck the dentry after taking the lock - d_move may have
1691 * changed things. Don't bother checking the hash because
1692 * we're about to compare the whole name anyway.
1694 if (dentry->d_parent != parent)
1697 /* non-existing due to RCU? */
1698 if (d_unhashed(dentry))
1702 * It is safe to compare names since d_move() cannot
1703 * change the qstr (protected by d_lock).
1705 qstr = &dentry->d_name;
1706 if (parent->d_op && parent->d_op->d_compare) {
1707 if (parent->d_op->d_compare(parent, parent->d_inode,
1708 dentry, dentry->d_inode,
1709 qstr->len, qstr->name, name))
1712 if (qstr->len != len)
1714 if (memcmp(qstr->name, str, len))
1720 spin_unlock(&dentry->d_lock);
1723 spin_unlock(&dentry->d_lock);
1731 * d_hash_and_lookup - hash the qstr then search for a dentry
1732 * @dir: Directory to search in
1733 * @name: qstr of name we wish to find
1735 * On hash failure or on lookup failure NULL is returned.
1737 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1739 struct dentry *dentry = NULL;
1742 * Check for a fs-specific hash function. Note that we must
1743 * calculate the standard hash first, as the d_op->d_hash()
1744 * routine may choose to leave the hash value unchanged.
1746 name->hash = full_name_hash(name->name, name->len);
1747 if (dir->d_op && dir->d_op->d_hash) {
1748 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
1751 dentry = d_lookup(dir, name);
1757 * d_validate - verify dentry provided from insecure source (deprecated)
1758 * @dentry: The dentry alleged to be valid child of @dparent
1759 * @dparent: The parent dentry (known to be valid)
1761 * An insecure source has sent us a dentry, here we verify it and dget() it.
1762 * This is used by ncpfs in its readdir implementation.
1763 * Zero is returned in the dentry is invalid.
1765 * This function is slow for big directories, and deprecated, do not use it.
1767 int d_validate(struct dentry *dentry, struct dentry *dparent)
1769 struct dentry *child;
1771 spin_lock(&dparent->d_lock);
1772 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1773 if (dentry == child) {
1774 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1775 __dget_dlock(dentry);
1776 spin_unlock(&dentry->d_lock);
1777 spin_unlock(&dparent->d_lock);
1781 spin_unlock(&dparent->d_lock);
1785 EXPORT_SYMBOL(d_validate);
1788 * When a file is deleted, we have two options:
1789 * - turn this dentry into a negative dentry
1790 * - unhash this dentry and free it.
1792 * Usually, we want to just turn this into
1793 * a negative dentry, but if anybody else is
1794 * currently using the dentry or the inode
1795 * we can't do that and we fall back on removing
1796 * it from the hash queues and waiting for
1797 * it to be deleted later when it has no users
1801 * d_delete - delete a dentry
1802 * @dentry: The dentry to delete
1804 * Turn the dentry into a negative dentry if possible, otherwise
1805 * remove it from the hash queues so it can be deleted later
1808 void d_delete(struct dentry * dentry)
1812 * Are we the only user?
1815 spin_lock(&dentry->d_lock);
1816 isdir = S_ISDIR(dentry->d_inode->i_mode);
1817 if (dentry->d_count == 1) {
1818 if (!spin_trylock(&dcache_inode_lock)) {
1819 spin_unlock(&dentry->d_lock);
1823 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
1824 dentry_iput(dentry);
1825 fsnotify_nameremove(dentry, isdir);
1829 if (!d_unhashed(dentry))
1832 spin_unlock(&dentry->d_lock);
1834 fsnotify_nameremove(dentry, isdir);
1836 EXPORT_SYMBOL(d_delete);
1838 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1841 entry->d_flags &= ~DCACHE_UNHASHED;
1842 hlist_add_head_rcu(&entry->d_hash, list);
1845 static void _d_rehash(struct dentry * entry)
1847 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1851 * d_rehash - add an entry back to the hash
1852 * @entry: dentry to add to the hash
1854 * Adds a dentry to the hash according to its name.
1857 void d_rehash(struct dentry * entry)
1859 spin_lock(&entry->d_lock);
1860 spin_lock(&dcache_hash_lock);
1862 spin_unlock(&dcache_hash_lock);
1863 spin_unlock(&entry->d_lock);
1865 EXPORT_SYMBOL(d_rehash);
1868 * dentry_update_name_case - update case insensitive dentry with a new name
1869 * @dentry: dentry to be updated
1872 * Update a case insensitive dentry with new case of name.
1874 * dentry must have been returned by d_lookup with name @name. Old and new
1875 * name lengths must match (ie. no d_compare which allows mismatched name
1878 * Parent inode i_mutex must be held over d_lookup and into this call (to
1879 * keep renames and concurrent inserts, and readdir(2) away).
1881 void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
1883 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
1884 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
1886 spin_lock(&dentry->d_lock);
1887 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
1888 spin_unlock(&dentry->d_lock);
1890 EXPORT_SYMBOL(dentry_update_name_case);
1892 static void switch_names(struct dentry *dentry, struct dentry *target)
1894 if (dname_external(target)) {
1895 if (dname_external(dentry)) {
1897 * Both external: swap the pointers
1899 swap(target->d_name.name, dentry->d_name.name);
1902 * dentry:internal, target:external. Steal target's
1903 * storage and make target internal.
1905 memcpy(target->d_iname, dentry->d_name.name,
1906 dentry->d_name.len + 1);
1907 dentry->d_name.name = target->d_name.name;
1908 target->d_name.name = target->d_iname;
1911 if (dname_external(dentry)) {
1913 * dentry:external, target:internal. Give dentry's
1914 * storage to target and make dentry internal
1916 memcpy(dentry->d_iname, target->d_name.name,
1917 target->d_name.len + 1);
1918 target->d_name.name = dentry->d_name.name;
1919 dentry->d_name.name = dentry->d_iname;
1922 * Both are internal. Just copy target to dentry
1924 memcpy(dentry->d_iname, target->d_name.name,
1925 target->d_name.len + 1);
1926 dentry->d_name.len = target->d_name.len;
1930 swap(dentry->d_name.len, target->d_name.len);
1933 static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
1936 * XXXX: do we really need to take target->d_lock?
1938 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
1939 spin_lock(&target->d_parent->d_lock);
1941 if (d_ancestor(dentry->d_parent, target->d_parent)) {
1942 spin_lock(&dentry->d_parent->d_lock);
1943 spin_lock_nested(&target->d_parent->d_lock,
1944 DENTRY_D_LOCK_NESTED);
1946 spin_lock(&target->d_parent->d_lock);
1947 spin_lock_nested(&dentry->d_parent->d_lock,
1948 DENTRY_D_LOCK_NESTED);
1951 if (target < dentry) {
1952 spin_lock_nested(&target->d_lock, 2);
1953 spin_lock_nested(&dentry->d_lock, 3);
1955 spin_lock_nested(&dentry->d_lock, 2);
1956 spin_lock_nested(&target->d_lock, 3);
1960 static void dentry_unlock_parents_for_move(struct dentry *dentry,
1961 struct dentry *target)
1963 if (target->d_parent != dentry->d_parent)
1964 spin_unlock(&dentry->d_parent->d_lock);
1965 if (target->d_parent != target)
1966 spin_unlock(&target->d_parent->d_lock);
1970 * When switching names, the actual string doesn't strictly have to
1971 * be preserved in the target - because we're dropping the target
1972 * anyway. As such, we can just do a simple memcpy() to copy over
1973 * the new name before we switch.
1975 * Note that we have to be a lot more careful about getting the hash
1976 * switched - we have to switch the hash value properly even if it
1977 * then no longer matches the actual (corrupted) string of the target.
1978 * The hash value has to match the hash queue that the dentry is on..
1981 * d_move - move a dentry
1982 * @dentry: entry to move
1983 * @target: new dentry
1985 * Update the dcache to reflect the move of a file name. Negative
1986 * dcache entries should not be moved in this way.
1988 void d_move(struct dentry * dentry, struct dentry * target)
1990 if (!dentry->d_inode)
1991 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1993 BUG_ON(d_ancestor(dentry, target));
1994 BUG_ON(d_ancestor(target, dentry));
1996 write_seqlock(&rename_lock);
1998 dentry_lock_for_move(dentry, target);
2000 /* Move the dentry to the target hash queue, if on different bucket */
2001 spin_lock(&dcache_hash_lock);
2002 if (!d_unhashed(dentry))
2003 hlist_del_rcu(&dentry->d_hash);
2004 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
2005 spin_unlock(&dcache_hash_lock);
2007 /* Unhash the target: dput() will then get rid of it */
2010 list_del(&dentry->d_u.d_child);
2011 list_del(&target->d_u.d_child);
2013 /* Switch the names.. */
2014 switch_names(dentry, target);
2015 swap(dentry->d_name.hash, target->d_name.hash);
2017 /* ... and switch the parents */
2018 if (IS_ROOT(dentry)) {
2019 dentry->d_parent = target->d_parent;
2020 target->d_parent = target;
2021 INIT_LIST_HEAD(&target->d_u.d_child);
2023 swap(dentry->d_parent, target->d_parent);
2025 /* And add them back to the (new) parent lists */
2026 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
2029 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2031 dentry_unlock_parents_for_move(dentry, target);
2032 spin_unlock(&target->d_lock);
2033 fsnotify_d_move(dentry);
2034 spin_unlock(&dentry->d_lock);
2035 write_sequnlock(&rename_lock);
2037 EXPORT_SYMBOL(d_move);
2040 * d_ancestor - search for an ancestor
2041 * @p1: ancestor dentry
2044 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2045 * an ancestor of p2, else NULL.
2047 struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
2051 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
2052 if (p->d_parent == p1)
2059 * This helper attempts to cope with remotely renamed directories
2061 * It assumes that the caller is already holding
2062 * dentry->d_parent->d_inode->i_mutex and the dcache_inode_lock
2064 * Note: If ever the locking in lock_rename() changes, then please
2065 * remember to update this too...
2067 static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
2068 __releases(dcache_inode_lock)
2070 struct mutex *m1 = NULL, *m2 = NULL;
2073 /* If alias and dentry share a parent, then no extra locks required */
2074 if (alias->d_parent == dentry->d_parent)
2077 /* Check for loops */
2078 ret = ERR_PTR(-ELOOP);
2079 if (d_ancestor(alias, dentry))
2082 /* See lock_rename() */
2083 ret = ERR_PTR(-EBUSY);
2084 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2086 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2087 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2089 m2 = &alias->d_parent->d_inode->i_mutex;
2091 d_move(alias, dentry);
2094 spin_unlock(&dcache_inode_lock);
2103 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2104 * named dentry in place of the dentry to be replaced.
2105 * returns with anon->d_lock held!
2107 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2109 struct dentry *dparent, *aparent;
2111 dentry_lock_for_move(anon, dentry);
2113 dparent = dentry->d_parent;
2114 aparent = anon->d_parent;
2116 switch_names(dentry, anon);
2117 swap(dentry->d_name.hash, anon->d_name.hash);
2119 dentry->d_parent = (aparent == anon) ? dentry : aparent;
2120 list_del(&dentry->d_u.d_child);
2121 if (!IS_ROOT(dentry))
2122 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2124 INIT_LIST_HEAD(&dentry->d_u.d_child);
2126 anon->d_parent = (dparent == dentry) ? anon : dparent;
2127 list_del(&anon->d_u.d_child);
2129 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
2131 INIT_LIST_HEAD(&anon->d_u.d_child);
2133 dentry_unlock_parents_for_move(anon, dentry);
2134 spin_unlock(&dentry->d_lock);
2136 /* anon->d_lock still locked, returns locked */
2137 anon->d_flags &= ~DCACHE_DISCONNECTED;
2141 * d_materialise_unique - introduce an inode into the tree
2142 * @dentry: candidate dentry
2143 * @inode: inode to bind to the dentry, to which aliases may be attached
2145 * Introduces an dentry into the tree, substituting an extant disconnected
2146 * root directory alias in its place if there is one
2148 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2150 struct dentry *actual;
2152 BUG_ON(!d_unhashed(dentry));
2156 __d_instantiate(dentry, NULL);
2161 spin_lock(&dcache_inode_lock);
2163 if (S_ISDIR(inode->i_mode)) {
2164 struct dentry *alias;
2166 /* Does an aliased dentry already exist? */
2167 alias = __d_find_alias(inode, 0);
2170 /* Is this an anonymous mountpoint that we could splice
2172 if (IS_ROOT(alias)) {
2173 __d_materialise_dentry(dentry, alias);
2177 /* Nope, but we must(!) avoid directory aliasing */
2178 actual = __d_unalias(dentry, alias);
2185 /* Add a unique reference */
2186 actual = __d_instantiate_unique(dentry, inode);
2190 BUG_ON(!d_unhashed(actual));
2192 spin_lock(&actual->d_lock);
2194 spin_lock(&dcache_hash_lock);
2196 spin_unlock(&dcache_hash_lock);
2197 spin_unlock(&actual->d_lock);
2198 spin_unlock(&dcache_inode_lock);
2200 if (actual == dentry) {
2201 security_d_instantiate(dentry, inode);
2208 EXPORT_SYMBOL_GPL(d_materialise_unique);
2210 static int prepend(char **buffer, int *buflen, const char *str, int namelen)
2214 return -ENAMETOOLONG;
2216 memcpy(*buffer, str, namelen);
2220 static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2222 return prepend(buffer, buflen, name->name, name->len);
2226 * Prepend path string to a buffer
2228 * @path: the dentry/vfsmount to report
2229 * @root: root vfsmnt/dentry (may be modified by this function)
2230 * @buffer: pointer to the end of the buffer
2231 * @buflen: pointer to buffer length
2233 * Caller holds the rename_lock.
2235 * If path is not reachable from the supplied root, then the value of
2236 * root is changed (without modifying refcounts).
2238 static int prepend_path(const struct path *path, struct path *root,
2239 char **buffer, int *buflen)
2241 struct dentry *dentry = path->dentry;
2242 struct vfsmount *vfsmnt = path->mnt;
2246 br_read_lock(vfsmount_lock);
2247 while (dentry != root->dentry || vfsmnt != root->mnt) {
2248 struct dentry * parent;
2250 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2252 if (vfsmnt->mnt_parent == vfsmnt) {
2255 dentry = vfsmnt->mnt_mountpoint;
2256 vfsmnt = vfsmnt->mnt_parent;
2259 parent = dentry->d_parent;
2261 spin_lock(&dentry->d_lock);
2262 error = prepend_name(buffer, buflen, &dentry->d_name);
2263 spin_unlock(&dentry->d_lock);
2265 error = prepend(buffer, buflen, "/", 1);
2274 if (!error && !slash)
2275 error = prepend(buffer, buflen, "/", 1);
2277 br_read_unlock(vfsmount_lock);
2282 * Filesystems needing to implement special "root names"
2283 * should do so with ->d_dname()
2285 if (IS_ROOT(dentry) &&
2286 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2287 WARN(1, "Root dentry has weird name <%.*s>\n",
2288 (int) dentry->d_name.len, dentry->d_name.name);
2291 root->dentry = dentry;
2296 * __d_path - return the path of a dentry
2297 * @path: the dentry/vfsmount to report
2298 * @root: root vfsmnt/dentry (may be modified by this function)
2299 * @buf: buffer to return value in
2300 * @buflen: buffer length
2302 * Convert a dentry into an ASCII path name.
2304 * Returns a pointer into the buffer or an error code if the
2305 * path was too long.
2307 * "buflen" should be positive.
2309 * If path is not reachable from the supplied root, then the value of
2310 * root is changed (without modifying refcounts).
2312 char *__d_path(const struct path *path, struct path *root,
2313 char *buf, int buflen)
2315 char *res = buf + buflen;
2318 prepend(&res, &buflen, "\0", 1);
2319 write_seqlock(&rename_lock);
2320 error = prepend_path(path, root, &res, &buflen);
2321 write_sequnlock(&rename_lock);
2324 return ERR_PTR(error);
2329 * same as __d_path but appends "(deleted)" for unlinked files.
2331 static int path_with_deleted(const struct path *path, struct path *root,
2332 char **buf, int *buflen)
2334 prepend(buf, buflen, "\0", 1);
2335 if (d_unlinked(path->dentry)) {
2336 int error = prepend(buf, buflen, " (deleted)", 10);
2341 return prepend_path(path, root, buf, buflen);
2344 static int prepend_unreachable(char **buffer, int *buflen)
2346 return prepend(buffer, buflen, "(unreachable)", 13);
2350 * d_path - return the path of a dentry
2351 * @path: path to report
2352 * @buf: buffer to return value in
2353 * @buflen: buffer length
2355 * Convert a dentry into an ASCII path name. If the entry has been deleted
2356 * the string " (deleted)" is appended. Note that this is ambiguous.
2358 * Returns a pointer into the buffer or an error code if the path was
2359 * too long. Note: Callers should use the returned pointer, not the passed
2360 * in buffer, to use the name! The implementation often starts at an offset
2361 * into the buffer, and may leave 0 bytes at the start.
2363 * "buflen" should be positive.
2365 char *d_path(const struct path *path, char *buf, int buflen)
2367 char *res = buf + buflen;
2373 * We have various synthetic filesystems that never get mounted. On
2374 * these filesystems dentries are never used for lookup purposes, and
2375 * thus don't need to be hashed. They also don't need a name until a
2376 * user wants to identify the object in /proc/pid/fd/. The little hack
2377 * below allows us to generate a name for these objects on demand:
2379 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2380 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2382 get_fs_root(current->fs, &root);
2383 write_seqlock(&rename_lock);
2385 error = path_with_deleted(path, &tmp, &res, &buflen);
2387 res = ERR_PTR(error);
2388 write_sequnlock(&rename_lock);
2392 EXPORT_SYMBOL(d_path);
2395 * d_path_with_unreachable - return the path of a dentry
2396 * @path: path to report
2397 * @buf: buffer to return value in
2398 * @buflen: buffer length
2400 * The difference from d_path() is that this prepends "(unreachable)"
2401 * to paths which are unreachable from the current process' root.
2403 char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2405 char *res = buf + buflen;
2410 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2411 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2413 get_fs_root(current->fs, &root);
2414 write_seqlock(&rename_lock);
2416 error = path_with_deleted(path, &tmp, &res, &buflen);
2417 if (!error && !path_equal(&tmp, &root))
2418 error = prepend_unreachable(&res, &buflen);
2419 write_sequnlock(&rename_lock);
2422 res = ERR_PTR(error);
2428 * Helper function for dentry_operations.d_dname() members
2430 char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2431 const char *fmt, ...)
2437 va_start(args, fmt);
2438 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2441 if (sz > sizeof(temp) || sz > buflen)
2442 return ERR_PTR(-ENAMETOOLONG);
2444 buffer += buflen - sz;
2445 return memcpy(buffer, temp, sz);
2449 * Write full pathname from the root of the filesystem into the buffer.
2451 static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
2453 char *end = buf + buflen;
2456 prepend(&end, &buflen, "\0", 1);
2463 while (!IS_ROOT(dentry)) {
2464 struct dentry *parent = dentry->d_parent;
2468 spin_lock(&dentry->d_lock);
2469 error = prepend_name(&end, &buflen, &dentry->d_name);
2470 spin_unlock(&dentry->d_lock);
2471 if (error != 0 || prepend(&end, &buflen, "/", 1) != 0)
2479 return ERR_PTR(-ENAMETOOLONG);
2482 char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2486 write_seqlock(&rename_lock);
2487 retval = __dentry_path(dentry, buf, buflen);
2488 write_sequnlock(&rename_lock);
2492 EXPORT_SYMBOL(dentry_path_raw);
2494 char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2499 write_seqlock(&rename_lock);
2500 if (d_unlinked(dentry)) {
2502 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2506 retval = __dentry_path(dentry, buf, buflen);
2507 write_sequnlock(&rename_lock);
2508 if (!IS_ERR(retval) && p)
2509 *p = '/'; /* restore '/' overriden with '\0' */
2512 return ERR_PTR(-ENAMETOOLONG);
2516 * NOTE! The user-level library version returns a
2517 * character pointer. The kernel system call just
2518 * returns the length of the buffer filled (which
2519 * includes the ending '\0' character), or a negative
2520 * error value. So libc would do something like
2522 * char *getcwd(char * buf, size_t size)
2526 * retval = sys_getcwd(buf, size);
2533 SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
2536 struct path pwd, root;
2537 char *page = (char *) __get_free_page(GFP_USER);
2542 get_fs_root_and_pwd(current->fs, &root, &pwd);
2545 write_seqlock(&rename_lock);
2546 if (!d_unlinked(pwd.dentry)) {
2548 struct path tmp = root;
2549 char *cwd = page + PAGE_SIZE;
2550 int buflen = PAGE_SIZE;
2552 prepend(&cwd, &buflen, "\0", 1);
2553 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
2554 write_sequnlock(&rename_lock);
2559 /* Unreachable from current root */
2560 if (!path_equal(&tmp, &root)) {
2561 error = prepend_unreachable(&cwd, &buflen);
2567 len = PAGE_SIZE + page - cwd;
2570 if (copy_to_user(buf, cwd, len))
2574 write_sequnlock(&rename_lock);
2580 free_page((unsigned long) page);
2585 * Test whether new_dentry is a subdirectory of old_dentry.
2587 * Trivially implemented using the dcache structure
2591 * is_subdir - is new dentry a subdirectory of old_dentry
2592 * @new_dentry: new dentry
2593 * @old_dentry: old dentry
2595 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2596 * Returns 0 otherwise.
2597 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2600 int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
2605 if (new_dentry == old_dentry)
2609 /* for restarting inner loop in case of seq retry */
2610 seq = read_seqbegin(&rename_lock);
2612 * Need rcu_readlock to protect against the d_parent trashing
2616 if (d_ancestor(old_dentry, new_dentry))
2621 } while (read_seqretry(&rename_lock, seq));
2626 int path_is_under(struct path *path1, struct path *path2)
2628 struct vfsmount *mnt = path1->mnt;
2629 struct dentry *dentry = path1->dentry;
2632 br_read_lock(vfsmount_lock);
2633 if (mnt != path2->mnt) {
2635 if (mnt->mnt_parent == mnt) {
2636 br_read_unlock(vfsmount_lock);
2639 if (mnt->mnt_parent == path2->mnt)
2641 mnt = mnt->mnt_parent;
2643 dentry = mnt->mnt_mountpoint;
2645 res = is_subdir(dentry, path2->dentry);
2646 br_read_unlock(vfsmount_lock);
2649 EXPORT_SYMBOL(path_is_under);
2651 void d_genocide(struct dentry *root)
2653 struct dentry *this_parent;
2654 struct list_head *next;
2658 seq = read_seqbegin(&rename_lock);
2661 spin_lock(&this_parent->d_lock);
2663 next = this_parent->d_subdirs.next;
2665 while (next != &this_parent->d_subdirs) {
2666 struct list_head *tmp = next;
2667 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
2670 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2671 if (d_unhashed(dentry) || !dentry->d_inode) {
2672 spin_unlock(&dentry->d_lock);
2675 if (!list_empty(&dentry->d_subdirs)) {
2676 spin_unlock(&this_parent->d_lock);
2677 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
2678 this_parent = dentry;
2679 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
2682 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
2683 dentry->d_flags |= DCACHE_GENOCIDE;
2686 spin_unlock(&dentry->d_lock);
2688 if (this_parent != root) {
2690 struct dentry *child;
2692 tmp = this_parent->d_parent;
2693 if (!(this_parent->d_flags & DCACHE_GENOCIDE)) {
2694 this_parent->d_flags |= DCACHE_GENOCIDE;
2695 this_parent->d_count--;
2698 spin_unlock(&this_parent->d_lock);
2699 child = this_parent;
2701 spin_lock(&this_parent->d_lock);
2702 /* might go back up the wrong parent if we have had a rename
2704 if (this_parent != child->d_parent ||
2705 (!locked && read_seqretry(&rename_lock, seq))) {
2706 spin_unlock(&this_parent->d_lock);
2711 next = child->d_u.d_child.next;
2714 spin_unlock(&this_parent->d_lock);
2715 if (!locked && read_seqretry(&rename_lock, seq))
2718 write_sequnlock(&rename_lock);
2723 write_seqlock(&rename_lock);
2728 * find_inode_number - check for dentry with name
2729 * @dir: directory to check
2730 * @name: Name to find.
2732 * Check whether a dentry already exists for the given name,
2733 * and return the inode number if it has an inode. Otherwise
2736 * This routine is used to post-process directory listings for
2737 * filesystems using synthetic inode numbers, and is necessary
2738 * to keep getcwd() working.
2741 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2743 struct dentry * dentry;
2746 dentry = d_hash_and_lookup(dir, name);
2748 if (dentry->d_inode)
2749 ino = dentry->d_inode->i_ino;
2754 EXPORT_SYMBOL(find_inode_number);
2756 static __initdata unsigned long dhash_entries;
2757 static int __init set_dhash_entries(char *str)
2761 dhash_entries = simple_strtoul(str, &str, 0);
2764 __setup("dhash_entries=", set_dhash_entries);
2766 static void __init dcache_init_early(void)
2770 /* If hashes are distributed across NUMA nodes, defer
2771 * hash allocation until vmalloc space is available.
2777 alloc_large_system_hash("Dentry cache",
2778 sizeof(struct hlist_head),
2786 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2787 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2790 static void __init dcache_init(void)
2795 * A constructor could be added for stable state like the lists,
2796 * but it is probably not worth it because of the cache nature
2799 dentry_cache = KMEM_CACHE(dentry,
2800 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
2802 register_shrinker(&dcache_shrinker);
2804 /* Hash may have been set up in dcache_init_early */
2809 alloc_large_system_hash("Dentry cache",
2810 sizeof(struct hlist_head),
2818 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2819 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2822 /* SLAB cache for __getname() consumers */
2823 struct kmem_cache *names_cachep __read_mostly;
2824 EXPORT_SYMBOL(names_cachep);
2826 EXPORT_SYMBOL(d_genocide);
2828 void __init vfs_caches_init_early(void)
2830 dcache_init_early();
2834 void __init vfs_caches_init(unsigned long mempages)
2836 unsigned long reserve;
2838 /* Base hash sizes on available memory, with a reserve equal to
2839 150% of current kernel size */
2841 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2842 mempages -= reserve;
2844 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2845 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2849 files_init(mempages);