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/config.h>
18 #include <linux/syscalls.h>
19 #include <linux/string.h>
22 #include <linux/fsnotify.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/smp_lock.h>
26 #include <linux/hash.h>
27 #include <linux/cache.h>
28 #include <linux/module.h>
29 #include <linux/mount.h>
30 #include <linux/file.h>
31 #include <asm/uaccess.h>
32 #include <linux/security.h>
33 #include <linux/seqlock.h>
34 #include <linux/swap.h>
35 #include <linux/bootmem.h>
37 /* #define DCACHE_DEBUG 1 */
39 int sysctl_vfs_cache_pressure = 100;
40 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
42 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
43 static seqlock_t rename_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
45 EXPORT_SYMBOL(dcache_lock);
47 static kmem_cache_t *dentry_cache;
49 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
52 * This is the single most critical data structure when it comes
53 * to the dcache: the hashtable for lookups. Somebody should try
54 * to make this good - I've just made it work.
56 * This hash-function tries to avoid losing too many bits of hash
57 * information, yet avoid using a prime hash-size or similar.
59 #define D_HASHBITS d_hash_shift
60 #define D_HASHMASK d_hash_mask
62 static unsigned int d_hash_mask;
63 static unsigned int d_hash_shift;
64 static struct hlist_head *dentry_hashtable;
65 static LIST_HEAD(dentry_unused);
67 /* Statistics gathering. */
68 struct dentry_stat_t dentry_stat = {
72 static void d_callback(struct rcu_head *head)
74 struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
76 if (dname_external(dentry))
77 kfree(dentry->d_name.name);
78 kmem_cache_free(dentry_cache, dentry);
82 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
85 static void d_free(struct dentry *dentry)
87 if (dentry->d_op && dentry->d_op->d_release)
88 dentry->d_op->d_release(dentry);
89 call_rcu(&dentry->d_u.d_rcu, d_callback);
93 * Release the dentry's inode, using the filesystem
94 * d_iput() operation if defined.
95 * Called with dcache_lock and per dentry lock held, drops both.
97 static void dentry_iput(struct dentry * dentry)
99 struct inode *inode = dentry->d_inode;
101 dentry->d_inode = NULL;
102 list_del_init(&dentry->d_alias);
103 spin_unlock(&dentry->d_lock);
104 spin_unlock(&dcache_lock);
106 fsnotify_inoderemove(inode);
107 if (dentry->d_op && dentry->d_op->d_iput)
108 dentry->d_op->d_iput(dentry, inode);
112 spin_unlock(&dentry->d_lock);
113 spin_unlock(&dcache_lock);
120 * This is complicated by the fact that we do not want to put
121 * dentries that are no longer on any hash chain on the unused
122 * list: we'd much rather just get rid of them immediately.
124 * However, that implies that we have to traverse the dentry
125 * tree upwards to the parents which might _also_ now be
126 * scheduled for deletion (it may have been only waiting for
127 * its last child to go away).
129 * This tail recursion is done by hand as we don't want to depend
130 * on the compiler to always get this right (gcc generally doesn't).
131 * Real recursion would eat up our stack space.
135 * dput - release a dentry
136 * @dentry: dentry to release
138 * Release a dentry. This will drop the usage count and if appropriate
139 * call the dentry unlink method as well as removing it from the queues and
140 * releasing its resources. If the parent dentries were scheduled for release
141 * they too may now get deleted.
143 * no dcache lock, please.
146 void dput(struct dentry *dentry)
152 if (atomic_read(&dentry->d_count) == 1)
154 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
157 spin_lock(&dentry->d_lock);
158 if (atomic_read(&dentry->d_count)) {
159 spin_unlock(&dentry->d_lock);
160 spin_unlock(&dcache_lock);
165 * AV: ->d_delete() is _NOT_ allowed to block now.
167 if (dentry->d_op && dentry->d_op->d_delete) {
168 if (dentry->d_op->d_delete(dentry))
171 /* Unreachable? Get rid of it */
172 if (d_unhashed(dentry))
174 if (list_empty(&dentry->d_lru)) {
175 dentry->d_flags |= DCACHE_REFERENCED;
176 list_add(&dentry->d_lru, &dentry_unused);
177 dentry_stat.nr_unused++;
179 spin_unlock(&dentry->d_lock);
180 spin_unlock(&dcache_lock);
187 struct dentry *parent;
189 /* If dentry was on d_lru list
190 * delete it from there
192 if (!list_empty(&dentry->d_lru)) {
193 list_del(&dentry->d_lru);
194 dentry_stat.nr_unused--;
196 list_del(&dentry->d_u.d_child);
197 dentry_stat.nr_dentry--; /* For d_free, below */
198 /*drops the locks, at that point nobody can reach this dentry */
200 parent = dentry->d_parent;
202 if (dentry == parent)
210 * d_invalidate - invalidate a dentry
211 * @dentry: dentry to invalidate
213 * Try to invalidate the dentry if it turns out to be
214 * possible. If there are other dentries that can be
215 * reached through this one we can't delete it and we
216 * return -EBUSY. On success we return 0.
221 int d_invalidate(struct dentry * dentry)
224 * If it's already been dropped, return OK.
226 spin_lock(&dcache_lock);
227 if (d_unhashed(dentry)) {
228 spin_unlock(&dcache_lock);
232 * Check whether to do a partial shrink_dcache
233 * to get rid of unused child entries.
235 if (!list_empty(&dentry->d_subdirs)) {
236 spin_unlock(&dcache_lock);
237 shrink_dcache_parent(dentry);
238 spin_lock(&dcache_lock);
242 * Somebody else still using it?
244 * If it's a directory, we can't drop it
245 * for fear of somebody re-populating it
246 * with children (even though dropping it
247 * would make it unreachable from the root,
248 * we might still populate it if it was a
249 * working directory or similar).
251 spin_lock(&dentry->d_lock);
252 if (atomic_read(&dentry->d_count) > 1) {
253 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
254 spin_unlock(&dentry->d_lock);
255 spin_unlock(&dcache_lock);
261 spin_unlock(&dentry->d_lock);
262 spin_unlock(&dcache_lock);
266 /* This should be called _only_ with dcache_lock held */
268 static inline struct dentry * __dget_locked(struct dentry *dentry)
270 atomic_inc(&dentry->d_count);
271 if (!list_empty(&dentry->d_lru)) {
272 dentry_stat.nr_unused--;
273 list_del_init(&dentry->d_lru);
278 struct dentry * dget_locked(struct dentry *dentry)
280 return __dget_locked(dentry);
284 * d_find_alias - grab a hashed alias of inode
285 * @inode: inode in question
286 * @want_discon: flag, used by d_splice_alias, to request
287 * that only a DISCONNECTED alias be returned.
289 * If inode has a hashed alias, or is a directory and has any alias,
290 * acquire the reference to alias and return it. Otherwise return NULL.
291 * Notice that if inode is a directory there can be only one alias and
292 * it can be unhashed only if it has no children, or if it is the root
295 * If the inode has a DCACHE_DISCONNECTED alias, then prefer
296 * any other hashed alias over that one unless @want_discon is set,
297 * in which case only return a DCACHE_DISCONNECTED alias.
300 static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
302 struct list_head *head, *next, *tmp;
303 struct dentry *alias, *discon_alias=NULL;
305 head = &inode->i_dentry;
306 next = inode->i_dentry.next;
307 while (next != head) {
311 alias = list_entry(tmp, struct dentry, d_alias);
312 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
313 if (alias->d_flags & DCACHE_DISCONNECTED)
314 discon_alias = alias;
315 else if (!want_discon) {
316 __dget_locked(alias);
322 __dget_locked(discon_alias);
326 struct dentry * d_find_alias(struct inode *inode)
328 struct dentry *de = NULL;
330 if (!list_empty(&inode->i_dentry)) {
331 spin_lock(&dcache_lock);
332 de = __d_find_alias(inode, 0);
333 spin_unlock(&dcache_lock);
339 * Try to kill dentries associated with this inode.
340 * WARNING: you must own a reference to inode.
342 void d_prune_aliases(struct inode *inode)
344 struct dentry *dentry;
346 spin_lock(&dcache_lock);
347 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
348 spin_lock(&dentry->d_lock);
349 if (!atomic_read(&dentry->d_count)) {
350 __dget_locked(dentry);
352 spin_unlock(&dentry->d_lock);
353 spin_unlock(&dcache_lock);
357 spin_unlock(&dentry->d_lock);
359 spin_unlock(&dcache_lock);
363 * Throw away a dentry - free the inode, dput the parent.
364 * This requires that the LRU list has already been
366 * Called with dcache_lock, drops it and then regains.
368 static inline void prune_one_dentry(struct dentry * dentry)
370 struct dentry * parent;
373 list_del(&dentry->d_u.d_child);
374 dentry_stat.nr_dentry--; /* For d_free, below */
376 parent = dentry->d_parent;
378 if (parent != dentry)
380 spin_lock(&dcache_lock);
384 * prune_dcache - shrink the dcache
385 * @count: number of entries to try and free
387 * Shrink the dcache. This is done when we need
388 * more memory, or simply when we need to unmount
389 * something (at which point we need to unuse
392 * This function may fail to free any resources if
393 * all the dentries are in use.
396 static void prune_dcache(int count)
398 spin_lock(&dcache_lock);
399 for (; count ; count--) {
400 struct dentry *dentry;
401 struct list_head *tmp;
403 cond_resched_lock(&dcache_lock);
405 tmp = dentry_unused.prev;
406 if (tmp == &dentry_unused)
409 prefetch(dentry_unused.prev);
410 dentry_stat.nr_unused--;
411 dentry = list_entry(tmp, struct dentry, d_lru);
413 spin_lock(&dentry->d_lock);
415 * We found an inuse dentry which was not removed from
416 * dentry_unused because of laziness during lookup. Do not free
417 * it - just keep it off the dentry_unused list.
419 if (atomic_read(&dentry->d_count)) {
420 spin_unlock(&dentry->d_lock);
423 /* If the dentry was recently referenced, don't free it. */
424 if (dentry->d_flags & DCACHE_REFERENCED) {
425 dentry->d_flags &= ~DCACHE_REFERENCED;
426 list_add(&dentry->d_lru, &dentry_unused);
427 dentry_stat.nr_unused++;
428 spin_unlock(&dentry->d_lock);
431 prune_one_dentry(dentry);
433 spin_unlock(&dcache_lock);
437 * Shrink the dcache for the specified super block.
438 * This allows us to unmount a device without disturbing
439 * the dcache for the other devices.
441 * This implementation makes just two traversals of the
442 * unused list. On the first pass we move the selected
443 * dentries to the most recent end, and on the second
444 * pass we free them. The second pass must restart after
445 * each dput(), but since the target dentries are all at
446 * the end, it's really just a single traversal.
450 * shrink_dcache_sb - shrink dcache for a superblock
453 * Shrink the dcache for the specified super block. This
454 * is used to free the dcache before unmounting a file
458 void shrink_dcache_sb(struct super_block * sb)
460 struct list_head *tmp, *next;
461 struct dentry *dentry;
464 * Pass one ... move the dentries for the specified
465 * superblock to the most recent end of the unused list.
467 spin_lock(&dcache_lock);
468 list_for_each_safe(tmp, next, &dentry_unused) {
469 dentry = list_entry(tmp, struct dentry, d_lru);
470 if (dentry->d_sb != sb)
473 list_add(tmp, &dentry_unused);
477 * Pass two ... free the dentries for this superblock.
480 list_for_each_safe(tmp, next, &dentry_unused) {
481 dentry = list_entry(tmp, struct dentry, d_lru);
482 if (dentry->d_sb != sb)
484 dentry_stat.nr_unused--;
486 spin_lock(&dentry->d_lock);
487 if (atomic_read(&dentry->d_count)) {
488 spin_unlock(&dentry->d_lock);
491 prune_one_dentry(dentry);
494 spin_unlock(&dcache_lock);
498 * Search for at least 1 mount point in the dentry's subdirs.
499 * We descend to the next level whenever the d_subdirs
500 * list is non-empty and continue searching.
504 * have_submounts - check for mounts over a dentry
505 * @parent: dentry to check.
507 * Return true if the parent or its subdirectories contain
511 int have_submounts(struct dentry *parent)
513 struct dentry *this_parent = parent;
514 struct list_head *next;
516 spin_lock(&dcache_lock);
517 if (d_mountpoint(parent))
520 next = this_parent->d_subdirs.next;
522 while (next != &this_parent->d_subdirs) {
523 struct list_head *tmp = next;
524 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
526 /* Have we found a mount point ? */
527 if (d_mountpoint(dentry))
529 if (!list_empty(&dentry->d_subdirs)) {
530 this_parent = dentry;
535 * All done at this level ... ascend and resume the search.
537 if (this_parent != parent) {
538 next = this_parent->d_u.d_child.next;
539 this_parent = this_parent->d_parent;
542 spin_unlock(&dcache_lock);
543 return 0; /* No mount points found in tree */
545 spin_unlock(&dcache_lock);
550 * Search the dentry child list for the specified parent,
551 * and move any unused dentries to the end of the unused
552 * list for prune_dcache(). We descend to the next level
553 * whenever the d_subdirs list is non-empty and continue
556 * It returns zero iff there are no unused children,
557 * otherwise it returns the number of children moved to
558 * the end of the unused list. This may not be the total
559 * number of unused children, because select_parent can
560 * drop the lock and return early due to latency
563 static int select_parent(struct dentry * parent)
565 struct dentry *this_parent = parent;
566 struct list_head *next;
569 spin_lock(&dcache_lock);
571 next = this_parent->d_subdirs.next;
573 while (next != &this_parent->d_subdirs) {
574 struct list_head *tmp = next;
575 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
578 if (!list_empty(&dentry->d_lru)) {
579 dentry_stat.nr_unused--;
580 list_del_init(&dentry->d_lru);
583 * move only zero ref count dentries to the end
584 * of the unused list for prune_dcache
586 if (!atomic_read(&dentry->d_count)) {
587 list_add(&dentry->d_lru, dentry_unused.prev);
588 dentry_stat.nr_unused++;
593 * We can return to the caller if we have found some (this
594 * ensures forward progress). We'll be coming back to find
597 if (found && need_resched())
601 * Descend a level if the d_subdirs list is non-empty.
603 if (!list_empty(&dentry->d_subdirs)) {
604 this_parent = dentry;
606 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
607 dentry->d_parent->d_name.name, dentry->d_name.name, found);
613 * All done at this level ... ascend and resume the search.
615 if (this_parent != parent) {
616 next = this_parent->d_u.d_child.next;
617 this_parent = this_parent->d_parent;
619 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
620 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
625 spin_unlock(&dcache_lock);
630 * shrink_dcache_parent - prune dcache
631 * @parent: parent of entries to prune
633 * Prune the dcache to remove unused children of the parent dentry.
636 void shrink_dcache_parent(struct dentry * parent)
640 while ((found = select_parent(parent)) != 0)
645 * shrink_dcache_anon - further prune the cache
646 * @head: head of d_hash list of dentries to prune
648 * Prune the dentries that are anonymous
650 * parsing d_hash list does not hlist_for_each_entry_rcu() as it
651 * done under dcache_lock.
654 void shrink_dcache_anon(struct hlist_head *head)
656 struct hlist_node *lp;
660 spin_lock(&dcache_lock);
661 hlist_for_each(lp, head) {
662 struct dentry *this = hlist_entry(lp, struct dentry, d_hash);
663 if (!list_empty(&this->d_lru)) {
664 dentry_stat.nr_unused--;
665 list_del_init(&this->d_lru);
669 * move only zero ref count dentries to the end
670 * of the unused list for prune_dcache
672 if (!atomic_read(&this->d_count)) {
673 list_add_tail(&this->d_lru, &dentry_unused);
674 dentry_stat.nr_unused++;
678 spin_unlock(&dcache_lock);
684 * Scan `nr' dentries and return the number which remain.
686 * We need to avoid reentering the filesystem if the caller is performing a
687 * GFP_NOFS allocation attempt. One example deadlock is:
689 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
690 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
691 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
693 * In this case we return -1 to tell the caller that we baled.
695 static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
698 if (!(gfp_mask & __GFP_FS))
702 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
706 * d_alloc - allocate a dcache entry
707 * @parent: parent of entry to allocate
708 * @name: qstr of the name
710 * Allocates a dentry. It returns %NULL if there is insufficient memory
711 * available. On a success the dentry is returned. The name passed in is
712 * copied and the copy passed in may be reused after this call.
715 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
717 struct dentry *dentry;
720 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
724 if (name->len > DNAME_INLINE_LEN-1) {
725 dname = kmalloc(name->len + 1, GFP_KERNEL);
727 kmem_cache_free(dentry_cache, dentry);
731 dname = dentry->d_iname;
733 dentry->d_name.name = dname;
735 dentry->d_name.len = name->len;
736 dentry->d_name.hash = name->hash;
737 memcpy(dname, name->name, name->len);
738 dname[name->len] = 0;
740 atomic_set(&dentry->d_count, 1);
741 dentry->d_flags = DCACHE_UNHASHED;
742 spin_lock_init(&dentry->d_lock);
743 dentry->d_inode = NULL;
744 dentry->d_parent = NULL;
747 dentry->d_fsdata = NULL;
748 dentry->d_mounted = 0;
749 #ifdef CONFIG_PROFILING
750 dentry->d_cookie = NULL;
752 INIT_HLIST_NODE(&dentry->d_hash);
753 INIT_LIST_HEAD(&dentry->d_lru);
754 INIT_LIST_HEAD(&dentry->d_subdirs);
755 INIT_LIST_HEAD(&dentry->d_alias);
758 dentry->d_parent = dget(parent);
759 dentry->d_sb = parent->d_sb;
761 INIT_LIST_HEAD(&dentry->d_u.d_child);
764 spin_lock(&dcache_lock);
766 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
767 dentry_stat.nr_dentry++;
768 spin_unlock(&dcache_lock);
773 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
778 q.len = strlen(name);
779 q.hash = full_name_hash(q.name, q.len);
780 return d_alloc(parent, &q);
784 * d_instantiate - fill in inode information for a dentry
785 * @entry: dentry to complete
786 * @inode: inode to attach to this dentry
788 * Fill in inode information in the entry.
790 * This turns negative dentries into productive full members
793 * NOTE! This assumes that the inode count has been incremented
794 * (or otherwise set) by the caller to indicate that it is now
795 * in use by the dcache.
798 void d_instantiate(struct dentry *entry, struct inode * inode)
800 if (!list_empty(&entry->d_alias)) BUG();
801 spin_lock(&dcache_lock);
803 list_add(&entry->d_alias, &inode->i_dentry);
804 entry->d_inode = inode;
805 spin_unlock(&dcache_lock);
806 security_d_instantiate(entry, inode);
810 * d_instantiate_unique - instantiate a non-aliased dentry
811 * @entry: dentry to instantiate
812 * @inode: inode to attach to this dentry
814 * Fill in inode information in the entry. On success, it returns NULL.
815 * If an unhashed alias of "entry" already exists, then we return the
816 * aliased dentry instead and drop one reference to inode.
818 * Note that in order to avoid conflicts with rename() etc, the caller
819 * had better be holding the parent directory semaphore.
821 * This also assumes that the inode count has been incremented
822 * (or otherwise set) by the caller to indicate that it is now
823 * in use by the dcache.
825 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
827 struct dentry *alias;
828 int len = entry->d_name.len;
829 const char *name = entry->d_name.name;
830 unsigned int hash = entry->d_name.hash;
832 BUG_ON(!list_empty(&entry->d_alias));
833 spin_lock(&dcache_lock);
836 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
837 struct qstr *qstr = &alias->d_name;
839 if (qstr->hash != hash)
841 if (alias->d_parent != entry->d_parent)
843 if (qstr->len != len)
845 if (memcmp(qstr->name, name, len))
848 spin_unlock(&dcache_lock);
849 BUG_ON(!d_unhashed(alias));
853 list_add(&entry->d_alias, &inode->i_dentry);
855 entry->d_inode = inode;
856 spin_unlock(&dcache_lock);
857 security_d_instantiate(entry, inode);
860 EXPORT_SYMBOL(d_instantiate_unique);
863 * d_alloc_root - allocate root dentry
864 * @root_inode: inode to allocate the root for
866 * Allocate a root ("/") dentry for the inode given. The inode is
867 * instantiated and returned. %NULL is returned if there is insufficient
868 * memory or the inode passed is %NULL.
871 struct dentry * d_alloc_root(struct inode * root_inode)
873 struct dentry *res = NULL;
876 static const struct qstr name = { .name = "/", .len = 1 };
878 res = d_alloc(NULL, &name);
880 res->d_sb = root_inode->i_sb;
882 d_instantiate(res, root_inode);
888 static inline struct hlist_head *d_hash(struct dentry *parent,
891 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
892 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
893 return dentry_hashtable + (hash & D_HASHMASK);
897 * d_alloc_anon - allocate an anonymous dentry
898 * @inode: inode to allocate the dentry for
900 * This is similar to d_alloc_root. It is used by filesystems when
901 * creating a dentry for a given inode, often in the process of
902 * mapping a filehandle to a dentry. The returned dentry may be
903 * anonymous, or may have a full name (if the inode was already
904 * in the cache). The file system may need to make further
905 * efforts to connect this dentry into the dcache properly.
907 * When called on a directory inode, we must ensure that
908 * the inode only ever has one dentry. If a dentry is
909 * found, that is returned instead of allocating a new one.
911 * On successful return, the reference to the inode has been transferred
912 * to the dentry. If %NULL is returned (indicating kmalloc failure),
913 * the reference on the inode has not been released.
916 struct dentry * d_alloc_anon(struct inode *inode)
918 static const struct qstr anonstring = { .name = "" };
922 if ((res = d_find_alias(inode))) {
927 tmp = d_alloc(NULL, &anonstring);
931 tmp->d_parent = tmp; /* make sure dput doesn't croak */
933 spin_lock(&dcache_lock);
934 res = __d_find_alias(inode, 0);
936 /* attach a disconnected dentry */
939 spin_lock(&res->d_lock);
940 res->d_sb = inode->i_sb;
942 res->d_inode = inode;
943 res->d_flags |= DCACHE_DISCONNECTED;
944 res->d_flags &= ~DCACHE_UNHASHED;
945 list_add(&res->d_alias, &inode->i_dentry);
946 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
947 spin_unlock(&res->d_lock);
949 inode = NULL; /* don't drop reference */
951 spin_unlock(&dcache_lock);
962 * d_splice_alias - splice a disconnected dentry into the tree if one exists
963 * @inode: the inode which may have a disconnected dentry
964 * @dentry: a negative dentry which we want to point to the inode.
966 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
967 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
968 * and return it, else simply d_add the inode to the dentry and return NULL.
970 * This is needed in the lookup routine of any filesystem that is exportable
971 * (via knfsd) so that we can build dcache paths to directories effectively.
973 * If a dentry was found and moved, then it is returned. Otherwise NULL
974 * is returned. This matches the expected return value of ->lookup.
977 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
979 struct dentry *new = NULL;
982 spin_lock(&dcache_lock);
983 new = __d_find_alias(inode, 1);
985 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
986 spin_unlock(&dcache_lock);
987 security_d_instantiate(new, inode);
992 /* d_instantiate takes dcache_lock, so we do it by hand */
993 list_add(&dentry->d_alias, &inode->i_dentry);
994 dentry->d_inode = inode;
995 spin_unlock(&dcache_lock);
996 security_d_instantiate(dentry, inode);
1000 d_add(dentry, inode);
1006 * d_lookup - search for a dentry
1007 * @parent: parent dentry
1008 * @name: qstr of name we wish to find
1010 * Searches the children of the parent dentry for the name in question. If
1011 * the dentry is found its reference count is incremented and the dentry
1012 * is returned. The caller must use d_put to free the entry when it has
1013 * finished using it. %NULL is returned on failure.
1015 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1016 * Memory barriers are used while updating and doing lockless traversal.
1017 * To avoid races with d_move while rename is happening, d_lock is used.
1019 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1020 * and name pointer in one structure pointed by d_qstr.
1022 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1023 * lookup is going on.
1025 * dentry_unused list is not updated even if lookup finds the required dentry
1026 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1027 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1030 * d_lookup() is protected against the concurrent renames in some unrelated
1031 * directory using the seqlockt_t rename_lock.
1034 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1036 struct dentry * dentry = NULL;
1040 seq = read_seqbegin(&rename_lock);
1041 dentry = __d_lookup(parent, name);
1044 } while (read_seqretry(&rename_lock, seq));
1048 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1050 unsigned int len = name->len;
1051 unsigned int hash = name->hash;
1052 const unsigned char *str = name->name;
1053 struct hlist_head *head = d_hash(parent,hash);
1054 struct dentry *found = NULL;
1055 struct hlist_node *node;
1056 struct dentry *dentry;
1060 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1063 if (dentry->d_name.hash != hash)
1065 if (dentry->d_parent != parent)
1068 spin_lock(&dentry->d_lock);
1071 * Recheck the dentry after taking the lock - d_move may have
1072 * changed things. Don't bother checking the hash because we're
1073 * about to compare the whole name anyway.
1075 if (dentry->d_parent != parent)
1079 * It is safe to compare names since d_move() cannot
1080 * change the qstr (protected by d_lock).
1082 qstr = &dentry->d_name;
1083 if (parent->d_op && parent->d_op->d_compare) {
1084 if (parent->d_op->d_compare(parent, qstr, name))
1087 if (qstr->len != len)
1089 if (memcmp(qstr->name, str, len))
1093 if (!d_unhashed(dentry)) {
1094 atomic_inc(&dentry->d_count);
1097 spin_unlock(&dentry->d_lock);
1100 spin_unlock(&dentry->d_lock);
1108 * d_validate - verify dentry provided from insecure source
1109 * @dentry: The dentry alleged to be valid child of @dparent
1110 * @dparent: The parent dentry (known to be valid)
1111 * @hash: Hash of the dentry
1112 * @len: Length of the name
1114 * An insecure source has sent us a dentry, here we verify it and dget() it.
1115 * This is used by ncpfs in its readdir implementation.
1116 * Zero is returned in the dentry is invalid.
1119 int d_validate(struct dentry *dentry, struct dentry *dparent)
1121 struct hlist_head *base;
1122 struct hlist_node *lhp;
1124 /* Check whether the ptr might be valid at all.. */
1125 if (!kmem_ptr_validate(dentry_cache, dentry))
1128 if (dentry->d_parent != dparent)
1131 spin_lock(&dcache_lock);
1132 base = d_hash(dparent, dentry->d_name.hash);
1133 hlist_for_each(lhp,base) {
1134 /* hlist_for_each_entry_rcu() not required for d_hash list
1135 * as it is parsed under dcache_lock
1137 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1138 __dget_locked(dentry);
1139 spin_unlock(&dcache_lock);
1143 spin_unlock(&dcache_lock);
1149 * When a file is deleted, we have two options:
1150 * - turn this dentry into a negative dentry
1151 * - unhash this dentry and free it.
1153 * Usually, we want to just turn this into
1154 * a negative dentry, but if anybody else is
1155 * currently using the dentry or the inode
1156 * we can't do that and we fall back on removing
1157 * it from the hash queues and waiting for
1158 * it to be deleted later when it has no users
1162 * d_delete - delete a dentry
1163 * @dentry: The dentry to delete
1165 * Turn the dentry into a negative dentry if possible, otherwise
1166 * remove it from the hash queues so it can be deleted later
1169 void d_delete(struct dentry * dentry)
1173 * Are we the only user?
1175 spin_lock(&dcache_lock);
1176 spin_lock(&dentry->d_lock);
1177 isdir = S_ISDIR(dentry->d_inode->i_mode);
1178 if (atomic_read(&dentry->d_count) == 1) {
1179 dentry_iput(dentry);
1180 fsnotify_nameremove(dentry, isdir);
1184 if (!d_unhashed(dentry))
1187 spin_unlock(&dentry->d_lock);
1188 spin_unlock(&dcache_lock);
1190 fsnotify_nameremove(dentry, isdir);
1193 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1196 entry->d_flags &= ~DCACHE_UNHASHED;
1197 hlist_add_head_rcu(&entry->d_hash, list);
1201 * d_rehash - add an entry back to the hash
1202 * @entry: dentry to add to the hash
1204 * Adds a dentry to the hash according to its name.
1207 void d_rehash(struct dentry * entry)
1209 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1211 spin_lock(&dcache_lock);
1212 spin_lock(&entry->d_lock);
1213 __d_rehash(entry, list);
1214 spin_unlock(&entry->d_lock);
1215 spin_unlock(&dcache_lock);
1218 #define do_switch(x,y) do { \
1219 __typeof__ (x) __tmp = x; \
1220 x = y; y = __tmp; } while (0)
1223 * When switching names, the actual string doesn't strictly have to
1224 * be preserved in the target - because we're dropping the target
1225 * anyway. As such, we can just do a simple memcpy() to copy over
1226 * the new name before we switch.
1228 * Note that we have to be a lot more careful about getting the hash
1229 * switched - we have to switch the hash value properly even if it
1230 * then no longer matches the actual (corrupted) string of the target.
1231 * The hash value has to match the hash queue that the dentry is on..
1233 static void switch_names(struct dentry *dentry, struct dentry *target)
1235 if (dname_external(target)) {
1236 if (dname_external(dentry)) {
1238 * Both external: swap the pointers
1240 do_switch(target->d_name.name, dentry->d_name.name);
1243 * dentry:internal, target:external. Steal target's
1244 * storage and make target internal.
1246 dentry->d_name.name = target->d_name.name;
1247 target->d_name.name = target->d_iname;
1250 if (dname_external(dentry)) {
1252 * dentry:external, target:internal. Give dentry's
1253 * storage to target and make dentry internal
1255 memcpy(dentry->d_iname, target->d_name.name,
1256 target->d_name.len + 1);
1257 target->d_name.name = dentry->d_name.name;
1258 dentry->d_name.name = dentry->d_iname;
1261 * Both are internal. Just copy target to dentry
1263 memcpy(dentry->d_iname, target->d_name.name,
1264 target->d_name.len + 1);
1270 * We cannibalize "target" when moving dentry on top of it,
1271 * because it's going to be thrown away anyway. We could be more
1272 * polite about it, though.
1274 * This forceful removal will result in ugly /proc output if
1275 * somebody holds a file open that got deleted due to a rename.
1276 * We could be nicer about the deleted file, and let it show
1277 * up under the name it got deleted rather than the name that
1282 * d_move - move a dentry
1283 * @dentry: entry to move
1284 * @target: new dentry
1286 * Update the dcache to reflect the move of a file name. Negative
1287 * dcache entries should not be moved in this way.
1290 void d_move(struct dentry * dentry, struct dentry * target)
1292 struct hlist_head *list;
1294 if (!dentry->d_inode)
1295 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1297 spin_lock(&dcache_lock);
1298 write_seqlock(&rename_lock);
1300 * XXXX: do we really need to take target->d_lock?
1302 if (target < dentry) {
1303 spin_lock(&target->d_lock);
1304 spin_lock(&dentry->d_lock);
1306 spin_lock(&dentry->d_lock);
1307 spin_lock(&target->d_lock);
1310 /* Move the dentry to the target hash queue, if on different bucket */
1311 if (dentry->d_flags & DCACHE_UNHASHED)
1312 goto already_unhashed;
1314 hlist_del_rcu(&dentry->d_hash);
1317 list = d_hash(target->d_parent, target->d_name.hash);
1318 __d_rehash(dentry, list);
1320 /* Unhash the target: dput() will then get rid of it */
1323 list_del(&dentry->d_u.d_child);
1324 list_del(&target->d_u.d_child);
1326 /* Switch the names.. */
1327 switch_names(dentry, target);
1328 do_switch(dentry->d_name.len, target->d_name.len);
1329 do_switch(dentry->d_name.hash, target->d_name.hash);
1331 /* ... and switch the parents */
1332 if (IS_ROOT(dentry)) {
1333 dentry->d_parent = target->d_parent;
1334 target->d_parent = target;
1335 INIT_LIST_HEAD(&target->d_u.d_child);
1337 do_switch(dentry->d_parent, target->d_parent);
1339 /* And add them back to the (new) parent lists */
1340 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1343 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1344 spin_unlock(&target->d_lock);
1345 spin_unlock(&dentry->d_lock);
1346 write_sequnlock(&rename_lock);
1347 spin_unlock(&dcache_lock);
1351 * d_path - return the path of a dentry
1352 * @dentry: dentry to report
1353 * @vfsmnt: vfsmnt to which the dentry belongs
1354 * @root: root dentry
1355 * @rootmnt: vfsmnt to which the root dentry belongs
1356 * @buffer: buffer to return value in
1357 * @buflen: buffer length
1359 * Convert a dentry into an ASCII path name. If the entry has been deleted
1360 * the string " (deleted)" is appended. Note that this is ambiguous.
1362 * Returns the buffer or an error code if the path was too long.
1364 * "buflen" should be positive. Caller holds the dcache_lock.
1366 static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1367 struct dentry *root, struct vfsmount *rootmnt,
1368 char *buffer, int buflen)
1370 char * end = buffer+buflen;
1376 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1381 memcpy(end, " (deleted)", 10);
1391 struct dentry * parent;
1393 if (dentry == root && vfsmnt == rootmnt)
1395 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1397 spin_lock(&vfsmount_lock);
1398 if (vfsmnt->mnt_parent == vfsmnt) {
1399 spin_unlock(&vfsmount_lock);
1402 dentry = vfsmnt->mnt_mountpoint;
1403 vfsmnt = vfsmnt->mnt_parent;
1404 spin_unlock(&vfsmount_lock);
1407 parent = dentry->d_parent;
1409 namelen = dentry->d_name.len;
1410 buflen -= namelen + 1;
1414 memcpy(end, dentry->d_name.name, namelen);
1423 namelen = dentry->d_name.len;
1427 retval -= namelen-1; /* hit the slash */
1428 memcpy(retval, dentry->d_name.name, namelen);
1431 return ERR_PTR(-ENAMETOOLONG);
1434 /* write full pathname into buffer and return start of pathname */
1435 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1436 char *buf, int buflen)
1439 struct vfsmount *rootmnt;
1440 struct dentry *root;
1442 read_lock(¤t->fs->lock);
1443 rootmnt = mntget(current->fs->rootmnt);
1444 root = dget(current->fs->root);
1445 read_unlock(¤t->fs->lock);
1446 spin_lock(&dcache_lock);
1447 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1448 spin_unlock(&dcache_lock);
1455 * NOTE! The user-level library version returns a
1456 * character pointer. The kernel system call just
1457 * returns the length of the buffer filled (which
1458 * includes the ending '\0' character), or a negative
1459 * error value. So libc would do something like
1461 * char *getcwd(char * buf, size_t size)
1465 * retval = sys_getcwd(buf, size);
1472 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1475 struct vfsmount *pwdmnt, *rootmnt;
1476 struct dentry *pwd, *root;
1477 char *page = (char *) __get_free_page(GFP_USER);
1482 read_lock(¤t->fs->lock);
1483 pwdmnt = mntget(current->fs->pwdmnt);
1484 pwd = dget(current->fs->pwd);
1485 rootmnt = mntget(current->fs->rootmnt);
1486 root = dget(current->fs->root);
1487 read_unlock(¤t->fs->lock);
1490 /* Has the current directory has been unlinked? */
1491 spin_lock(&dcache_lock);
1492 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1496 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1497 spin_unlock(&dcache_lock);
1499 error = PTR_ERR(cwd);
1504 len = PAGE_SIZE + page - cwd;
1507 if (copy_to_user(buf, cwd, len))
1511 spin_unlock(&dcache_lock);
1518 free_page((unsigned long) page);
1523 * Test whether new_dentry is a subdirectory of old_dentry.
1525 * Trivially implemented using the dcache structure
1529 * is_subdir - is new dentry a subdirectory of old_dentry
1530 * @new_dentry: new dentry
1531 * @old_dentry: old dentry
1533 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1534 * Returns 0 otherwise.
1535 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1538 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1541 struct dentry * saved = new_dentry;
1544 /* need rcu_readlock to protect against the d_parent trashing due to
1549 /* for restarting inner loop in case of seq retry */
1552 seq = read_seqbegin(&rename_lock);
1554 if (new_dentry != old_dentry) {
1555 struct dentry * parent = new_dentry->d_parent;
1556 if (parent == new_dentry)
1558 new_dentry = parent;
1564 } while (read_seqretry(&rename_lock, seq));
1570 void d_genocide(struct dentry *root)
1572 struct dentry *this_parent = root;
1573 struct list_head *next;
1575 spin_lock(&dcache_lock);
1577 next = this_parent->d_subdirs.next;
1579 while (next != &this_parent->d_subdirs) {
1580 struct list_head *tmp = next;
1581 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1583 if (d_unhashed(dentry)||!dentry->d_inode)
1585 if (!list_empty(&dentry->d_subdirs)) {
1586 this_parent = dentry;
1589 atomic_dec(&dentry->d_count);
1591 if (this_parent != root) {
1592 next = this_parent->d_u.d_child.next;
1593 atomic_dec(&this_parent->d_count);
1594 this_parent = this_parent->d_parent;
1597 spin_unlock(&dcache_lock);
1601 * find_inode_number - check for dentry with name
1602 * @dir: directory to check
1603 * @name: Name to find.
1605 * Check whether a dentry already exists for the given name,
1606 * and return the inode number if it has an inode. Otherwise
1609 * This routine is used to post-process directory listings for
1610 * filesystems using synthetic inode numbers, and is necessary
1611 * to keep getcwd() working.
1614 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1616 struct dentry * dentry;
1620 * Check for a fs-specific hash function. Note that we must
1621 * calculate the standard hash first, as the d_op->d_hash()
1622 * routine may choose to leave the hash value unchanged.
1624 name->hash = full_name_hash(name->name, name->len);
1625 if (dir->d_op && dir->d_op->d_hash)
1627 if (dir->d_op->d_hash(dir, name) != 0)
1631 dentry = d_lookup(dir, name);
1634 if (dentry->d_inode)
1635 ino = dentry->d_inode->i_ino;
1642 static __initdata unsigned long dhash_entries;
1643 static int __init set_dhash_entries(char *str)
1647 dhash_entries = simple_strtoul(str, &str, 0);
1650 __setup("dhash_entries=", set_dhash_entries);
1652 static void __init dcache_init_early(void)
1656 /* If hashes are distributed across NUMA nodes, defer
1657 * hash allocation until vmalloc space is available.
1663 alloc_large_system_hash("Dentry cache",
1664 sizeof(struct hlist_head),
1672 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1673 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1676 static void __init dcache_init(unsigned long mempages)
1681 * A constructor could be added for stable state like the lists,
1682 * but it is probably not worth it because of the cache nature
1685 dentry_cache = kmem_cache_create("dentry_cache",
1686 sizeof(struct dentry),
1688 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1692 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1694 /* Hash may have been set up in dcache_init_early */
1699 alloc_large_system_hash("Dentry cache",
1700 sizeof(struct hlist_head),
1708 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1709 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1712 /* SLAB cache for __getname() consumers */
1713 kmem_cache_t *names_cachep;
1715 /* SLAB cache for file structures */
1716 kmem_cache_t *filp_cachep;
1718 EXPORT_SYMBOL(d_genocide);
1720 extern void bdev_cache_init(void);
1721 extern void chrdev_init(void);
1723 void __init vfs_caches_init_early(void)
1725 dcache_init_early();
1729 void __init vfs_caches_init(unsigned long mempages)
1731 unsigned long reserve;
1733 /* Base hash sizes on available memory, with a reserve equal to
1734 150% of current kernel size */
1736 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1737 mempages -= reserve;
1739 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1740 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1742 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
1743 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1745 dcache_init(mempages);
1746 inode_init(mempages);
1747 files_init(mempages);
1753 EXPORT_SYMBOL(d_alloc);
1754 EXPORT_SYMBOL(d_alloc_anon);
1755 EXPORT_SYMBOL(d_alloc_root);
1756 EXPORT_SYMBOL(d_delete);
1757 EXPORT_SYMBOL(d_find_alias);
1758 EXPORT_SYMBOL(d_instantiate);
1759 EXPORT_SYMBOL(d_invalidate);
1760 EXPORT_SYMBOL(d_lookup);
1761 EXPORT_SYMBOL(d_move);
1762 EXPORT_SYMBOL(d_path);
1763 EXPORT_SYMBOL(d_prune_aliases);
1764 EXPORT_SYMBOL(d_rehash);
1765 EXPORT_SYMBOL(d_splice_alias);
1766 EXPORT_SYMBOL(d_validate);
1767 EXPORT_SYMBOL(dget_locked);
1768 EXPORT_SYMBOL(dput);
1769 EXPORT_SYMBOL(find_inode_number);
1770 EXPORT_SYMBOL(have_submounts);
1771 EXPORT_SYMBOL(names_cachep);
1772 EXPORT_SYMBOL(shrink_dcache_parent);
1773 EXPORT_SYMBOL(shrink_dcache_sb);