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/slab.h>
23 #include <linux/init.h>
24 #include <linux/smp_lock.h>
25 #include <linux/hash.h>
26 #include <linux/cache.h>
27 #include <linux/module.h>
28 #include <linux/mount.h>
29 #include <linux/file.h>
30 #include <asm/uaccess.h>
31 #include <linux/security.h>
32 #include <linux/seqlock.h>
33 #include <linux/swap.h>
34 #include <linux/bootmem.h>
36 /* #define DCACHE_DEBUG 1 */
38 int sysctl_vfs_cache_pressure = 100;
39 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
41 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
42 static seqlock_t rename_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
44 EXPORT_SYMBOL(dcache_lock);
46 static kmem_cache_t *dentry_cache;
48 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
51 * This is the single most critical data structure when it comes
52 * to the dcache: the hashtable for lookups. Somebody should try
53 * to make this good - I've just made it work.
55 * This hash-function tries to avoid losing too many bits of hash
56 * information, yet avoid using a prime hash-size or similar.
58 #define D_HASHBITS d_hash_shift
59 #define D_HASHMASK d_hash_mask
61 static unsigned int d_hash_mask;
62 static unsigned int d_hash_shift;
63 static struct hlist_head *dentry_hashtable;
64 static LIST_HEAD(dentry_unused);
66 /* Statistics gathering. */
67 struct dentry_stat_t dentry_stat = {
71 static void d_callback(struct rcu_head *head)
73 struct dentry * dentry = container_of(head, struct dentry, d_rcu);
75 if (dname_external(dentry))
76 kfree(dentry->d_name.name);
77 kmem_cache_free(dentry_cache, dentry);
81 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
84 static void d_free(struct dentry *dentry)
86 if (dentry->d_op && dentry->d_op->d_release)
87 dentry->d_op->d_release(dentry);
88 call_rcu(&dentry->d_rcu, d_callback);
92 * Release the dentry's inode, using the filesystem
93 * d_iput() operation if defined.
94 * Called with dcache_lock and per dentry lock held, drops both.
96 static inline void dentry_iput(struct dentry * dentry)
98 struct inode *inode = dentry->d_inode;
100 dentry->d_inode = NULL;
101 list_del_init(&dentry->d_alias);
102 spin_unlock(&dentry->d_lock);
103 spin_unlock(&dcache_lock);
104 if (dentry->d_op && dentry->d_op->d_iput)
105 dentry->d_op->d_iput(dentry, inode);
109 spin_unlock(&dentry->d_lock);
110 spin_unlock(&dcache_lock);
117 * This is complicated by the fact that we do not want to put
118 * dentries that are no longer on any hash chain on the unused
119 * list: we'd much rather just get rid of them immediately.
121 * However, that implies that we have to traverse the dentry
122 * tree upwards to the parents which might _also_ now be
123 * scheduled for deletion (it may have been only waiting for
124 * its last child to go away).
126 * This tail recursion is done by hand as we don't want to depend
127 * on the compiler to always get this right (gcc generally doesn't).
128 * Real recursion would eat up our stack space.
132 * dput - release a dentry
133 * @dentry: dentry to release
135 * Release a dentry. This will drop the usage count and if appropriate
136 * call the dentry unlink method as well as removing it from the queues and
137 * releasing its resources. If the parent dentries were scheduled for release
138 * they too may now get deleted.
140 * no dcache lock, please.
143 void dput(struct dentry *dentry)
149 if (atomic_read(&dentry->d_count) == 1)
151 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
154 spin_lock(&dentry->d_lock);
155 if (atomic_read(&dentry->d_count)) {
156 spin_unlock(&dentry->d_lock);
157 spin_unlock(&dcache_lock);
162 * AV: ->d_delete() is _NOT_ allowed to block now.
164 if (dentry->d_op && dentry->d_op->d_delete) {
165 if (dentry->d_op->d_delete(dentry))
168 /* Unreachable? Get rid of it */
169 if (d_unhashed(dentry))
171 if (list_empty(&dentry->d_lru)) {
172 dentry->d_flags |= DCACHE_REFERENCED;
173 list_add(&dentry->d_lru, &dentry_unused);
174 dentry_stat.nr_unused++;
176 spin_unlock(&dentry->d_lock);
177 spin_unlock(&dcache_lock);
184 struct dentry *parent;
186 /* If dentry was on d_lru list
187 * delete it from there
189 if (!list_empty(&dentry->d_lru)) {
190 list_del(&dentry->d_lru);
191 dentry_stat.nr_unused--;
193 list_del(&dentry->d_child);
194 dentry_stat.nr_dentry--; /* For d_free, below */
195 /*drops the locks, at that point nobody can reach this dentry */
197 parent = dentry->d_parent;
199 if (dentry == parent)
207 * d_invalidate - invalidate a dentry
208 * @dentry: dentry to invalidate
210 * Try to invalidate the dentry if it turns out to be
211 * possible. If there are other dentries that can be
212 * reached through this one we can't delete it and we
213 * return -EBUSY. On success we return 0.
218 int d_invalidate(struct dentry * dentry)
221 * If it's already been dropped, return OK.
223 spin_lock(&dcache_lock);
224 if (d_unhashed(dentry)) {
225 spin_unlock(&dcache_lock);
229 * Check whether to do a partial shrink_dcache
230 * to get rid of unused child entries.
232 if (!list_empty(&dentry->d_subdirs)) {
233 spin_unlock(&dcache_lock);
234 shrink_dcache_parent(dentry);
235 spin_lock(&dcache_lock);
239 * Somebody else still using it?
241 * If it's a directory, we can't drop it
242 * for fear of somebody re-populating it
243 * with children (even though dropping it
244 * would make it unreachable from the root,
245 * we might still populate it if it was a
246 * working directory or similar).
248 spin_lock(&dentry->d_lock);
249 if (atomic_read(&dentry->d_count) > 1) {
250 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
251 spin_unlock(&dentry->d_lock);
252 spin_unlock(&dcache_lock);
258 spin_unlock(&dentry->d_lock);
259 spin_unlock(&dcache_lock);
263 /* This should be called _only_ with dcache_lock held */
265 static inline struct dentry * __dget_locked(struct dentry *dentry)
267 atomic_inc(&dentry->d_count);
268 if (!list_empty(&dentry->d_lru)) {
269 dentry_stat.nr_unused--;
270 list_del_init(&dentry->d_lru);
275 struct dentry * dget_locked(struct dentry *dentry)
277 return __dget_locked(dentry);
281 * d_find_alias - grab a hashed alias of inode
282 * @inode: inode in question
283 * @want_discon: flag, used by d_splice_alias, to request
284 * that only a DISCONNECTED alias be returned.
286 * If inode has a hashed alias, or is a directory and has any alias,
287 * acquire the reference to alias and return it. Otherwise return NULL.
288 * Notice that if inode is a directory there can be only one alias and
289 * it can be unhashed only if it has no children, or if it is the root
292 * If the inode has a DCACHE_DISCONNECTED alias, then prefer
293 * any other hashed alias over that one unless @want_discon is set,
294 * in which case only return a DCACHE_DISCONNECTED alias.
297 static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
299 struct list_head *head, *next, *tmp;
300 struct dentry *alias, *discon_alias=NULL;
302 head = &inode->i_dentry;
303 next = inode->i_dentry.next;
304 while (next != head) {
308 alias = list_entry(tmp, struct dentry, d_alias);
309 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
310 if (alias->d_flags & DCACHE_DISCONNECTED)
311 discon_alias = alias;
312 else if (!want_discon) {
313 __dget_locked(alias);
319 __dget_locked(discon_alias);
323 struct dentry * d_find_alias(struct inode *inode)
326 spin_lock(&dcache_lock);
327 de = __d_find_alias(inode, 0);
328 spin_unlock(&dcache_lock);
333 * Try to kill dentries associated with this inode.
334 * WARNING: you must own a reference to inode.
336 void d_prune_aliases(struct inode *inode)
338 struct list_head *tmp, *head = &inode->i_dentry;
340 spin_lock(&dcache_lock);
342 while ((tmp = tmp->next) != head) {
343 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
344 spin_lock(&dentry->d_lock);
345 if (!atomic_read(&dentry->d_count)) {
346 __dget_locked(dentry);
348 spin_unlock(&dentry->d_lock);
349 spin_unlock(&dcache_lock);
353 spin_unlock(&dentry->d_lock);
355 spin_unlock(&dcache_lock);
359 * Throw away a dentry - free the inode, dput the parent.
360 * This requires that the LRU list has already been
362 * Called with dcache_lock, drops it and then regains.
364 static inline void prune_one_dentry(struct dentry * dentry)
366 struct dentry * parent;
369 list_del(&dentry->d_child);
370 dentry_stat.nr_dentry--; /* For d_free, below */
372 parent = dentry->d_parent;
374 if (parent != dentry)
376 spin_lock(&dcache_lock);
380 * prune_dcache - shrink the dcache
381 * @count: number of entries to try and free
383 * Shrink the dcache. This is done when we need
384 * more memory, or simply when we need to unmount
385 * something (at which point we need to unuse
388 * This function may fail to free any resources if
389 * all the dentries are in use.
392 static void prune_dcache(int count)
394 spin_lock(&dcache_lock);
395 for (; count ; count--) {
396 struct dentry *dentry;
397 struct list_head *tmp;
399 cond_resched_lock(&dcache_lock);
401 tmp = dentry_unused.prev;
402 if (tmp == &dentry_unused)
405 prefetch(dentry_unused.prev);
406 dentry_stat.nr_unused--;
407 dentry = list_entry(tmp, struct dentry, d_lru);
409 spin_lock(&dentry->d_lock);
411 * We found an inuse dentry which was not removed from
412 * dentry_unused because of laziness during lookup. Do not free
413 * it - just keep it off the dentry_unused list.
415 if (atomic_read(&dentry->d_count)) {
416 spin_unlock(&dentry->d_lock);
419 /* If the dentry was recently referenced, don't free it. */
420 if (dentry->d_flags & DCACHE_REFERENCED) {
421 dentry->d_flags &= ~DCACHE_REFERENCED;
422 list_add(&dentry->d_lru, &dentry_unused);
423 dentry_stat.nr_unused++;
424 spin_unlock(&dentry->d_lock);
427 prune_one_dentry(dentry);
429 spin_unlock(&dcache_lock);
433 * Shrink the dcache for the specified super block.
434 * This allows us to unmount a device without disturbing
435 * the dcache for the other devices.
437 * This implementation makes just two traversals of the
438 * unused list. On the first pass we move the selected
439 * dentries to the most recent end, and on the second
440 * pass we free them. The second pass must restart after
441 * each dput(), but since the target dentries are all at
442 * the end, it's really just a single traversal.
446 * shrink_dcache_sb - shrink dcache for a superblock
449 * Shrink the dcache for the specified super block. This
450 * is used to free the dcache before unmounting a file
454 void shrink_dcache_sb(struct super_block * sb)
456 struct list_head *tmp, *next;
457 struct dentry *dentry;
460 * Pass one ... move the dentries for the specified
461 * superblock to the most recent end of the unused list.
463 spin_lock(&dcache_lock);
464 next = dentry_unused.next;
465 while (next != &dentry_unused) {
468 dentry = list_entry(tmp, struct dentry, d_lru);
469 if (dentry->d_sb != sb)
472 list_add(tmp, &dentry_unused);
476 * Pass two ... free the dentries for this superblock.
479 next = dentry_unused.next;
480 while (next != &dentry_unused) {
483 dentry = list_entry(tmp, struct dentry, d_lru);
484 if (dentry->d_sb != sb)
486 dentry_stat.nr_unused--;
488 spin_lock(&dentry->d_lock);
489 if (atomic_read(&dentry->d_count)) {
490 spin_unlock(&dentry->d_lock);
493 prune_one_dentry(dentry);
496 spin_unlock(&dcache_lock);
500 * Search for at least 1 mount point in the dentry's subdirs.
501 * We descend to the next level whenever the d_subdirs
502 * list is non-empty and continue searching.
506 * have_submounts - check for mounts over a dentry
507 * @parent: dentry to check.
509 * Return true if the parent or its subdirectories contain
513 int have_submounts(struct dentry *parent)
515 struct dentry *this_parent = parent;
516 struct list_head *next;
518 spin_lock(&dcache_lock);
519 if (d_mountpoint(parent))
522 next = this_parent->d_subdirs.next;
524 while (next != &this_parent->d_subdirs) {
525 struct list_head *tmp = next;
526 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
528 /* Have we found a mount point ? */
529 if (d_mountpoint(dentry))
531 if (!list_empty(&dentry->d_subdirs)) {
532 this_parent = dentry;
537 * All done at this level ... ascend and resume the search.
539 if (this_parent != parent) {
540 next = this_parent->d_child.next;
541 this_parent = this_parent->d_parent;
544 spin_unlock(&dcache_lock);
545 return 0; /* No mount points found in tree */
547 spin_unlock(&dcache_lock);
552 * Search the dentry child list for the specified parent,
553 * and move any unused dentries to the end of the unused
554 * list for prune_dcache(). We descend to the next level
555 * whenever the d_subdirs list is non-empty and continue
558 * It returns zero iff there are no unused children,
559 * otherwise it returns the number of children moved to
560 * the end of the unused list. This may not be the total
561 * number of unused children, because select_parent can
562 * drop the lock and return early due to latency
565 static int select_parent(struct dentry * parent)
567 struct dentry *this_parent = parent;
568 struct list_head *next;
571 spin_lock(&dcache_lock);
573 next = this_parent->d_subdirs.next;
575 while (next != &this_parent->d_subdirs) {
576 struct list_head *tmp = next;
577 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
580 if (!list_empty(&dentry->d_lru)) {
581 dentry_stat.nr_unused--;
582 list_del_init(&dentry->d_lru);
585 * move only zero ref count dentries to the end
586 * of the unused list for prune_dcache
588 if (!atomic_read(&dentry->d_count)) {
589 list_add(&dentry->d_lru, dentry_unused.prev);
590 dentry_stat.nr_unused++;
595 * We can return to the caller if we have found some (this
596 * ensures forward progress). We'll be coming back to find
599 if (found && need_resched())
603 * Descend a level if the d_subdirs list is non-empty.
605 if (!list_empty(&dentry->d_subdirs)) {
606 this_parent = dentry;
608 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
609 dentry->d_parent->d_name.name, dentry->d_name.name, found);
615 * All done at this level ... ascend and resume the search.
617 if (this_parent != parent) {
618 next = this_parent->d_child.next;
619 this_parent = this_parent->d_parent;
621 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
622 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
627 spin_unlock(&dcache_lock);
632 * shrink_dcache_parent - prune dcache
633 * @parent: parent of entries to prune
635 * Prune the dcache to remove unused children of the parent dentry.
638 void shrink_dcache_parent(struct dentry * parent)
642 while ((found = select_parent(parent)) != 0)
647 * shrink_dcache_anon - further prune the cache
648 * @head: head of d_hash list of dentries to prune
650 * Prune the dentries that are anonymous
652 * parsing d_hash list does not hlist_for_each_rcu() as it
653 * done under dcache_lock.
656 void shrink_dcache_anon(struct hlist_head *head)
658 struct hlist_node *lp;
662 spin_lock(&dcache_lock);
663 hlist_for_each(lp, head) {
664 struct dentry *this = hlist_entry(lp, struct dentry, d_hash);
665 if (!list_empty(&this->d_lru)) {
666 dentry_stat.nr_unused--;
667 list_del_init(&this->d_lru);
671 * move only zero ref count dentries to the end
672 * of the unused list for prune_dcache
674 if (!atomic_read(&this->d_count)) {
675 list_add_tail(&this->d_lru, &dentry_unused);
676 dentry_stat.nr_unused++;
680 spin_unlock(&dcache_lock);
686 * Scan `nr' dentries and return the number which remain.
688 * We need to avoid reentering the filesystem if the caller is performing a
689 * GFP_NOFS allocation attempt. One example deadlock is:
691 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
692 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
693 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
695 * In this case we return -1 to tell the caller that we baled.
697 static int shrink_dcache_memory(int nr, unsigned int gfp_mask)
700 if (!(gfp_mask & __GFP_FS))
704 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
708 * d_alloc - allocate a dcache entry
709 * @parent: parent of entry to allocate
710 * @name: qstr of the name
712 * Allocates a dentry. It returns %NULL if there is insufficient memory
713 * available. On a success the dentry is returned. The name passed in is
714 * copied and the copy passed in may be reused after this call.
717 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
719 struct dentry *dentry;
722 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
726 if (name->len > DNAME_INLINE_LEN-1) {
727 dname = kmalloc(name->len + 1, GFP_KERNEL);
729 kmem_cache_free(dentry_cache, dentry);
733 dname = dentry->d_iname;
735 dentry->d_name.name = dname;
737 dentry->d_name.len = name->len;
738 dentry->d_name.hash = name->hash;
739 memcpy(dname, name->name, name->len);
740 dname[name->len] = 0;
742 atomic_set(&dentry->d_count, 1);
743 dentry->d_flags = DCACHE_UNHASHED;
744 spin_lock_init(&dentry->d_lock);
745 dentry->d_inode = NULL;
746 dentry->d_parent = NULL;
749 dentry->d_fsdata = NULL;
750 dentry->d_mounted = 0;
751 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_child);
764 spin_lock(&dcache_lock);
766 list_add(&dentry->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.
818 * Note that in order to avoid conflicts with rename() etc, the caller
819 * had better be holding the parent directory semaphore.
821 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
823 struct dentry *alias;
824 int len = entry->d_name.len;
825 const char *name = entry->d_name.name;
826 unsigned int hash = entry->d_name.hash;
828 BUG_ON(!list_empty(&entry->d_alias));
829 spin_lock(&dcache_lock);
832 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
833 struct qstr *qstr = &alias->d_name;
835 if (qstr->hash != hash)
837 if (alias->d_parent != entry->d_parent)
839 if (qstr->len != len)
841 if (memcmp(qstr->name, name, len))
844 spin_unlock(&dcache_lock);
845 BUG_ON(!d_unhashed(alias));
848 list_add(&entry->d_alias, &inode->i_dentry);
850 entry->d_inode = inode;
851 spin_unlock(&dcache_lock);
852 security_d_instantiate(entry, inode);
855 EXPORT_SYMBOL(d_instantiate_unique);
858 * d_alloc_root - allocate root dentry
859 * @root_inode: inode to allocate the root for
861 * Allocate a root ("/") dentry for the inode given. The inode is
862 * instantiated and returned. %NULL is returned if there is insufficient
863 * memory or the inode passed is %NULL.
866 struct dentry * d_alloc_root(struct inode * root_inode)
868 struct dentry *res = NULL;
871 static const struct qstr name = { .name = "/", .len = 1 };
873 res = d_alloc(NULL, &name);
875 res->d_sb = root_inode->i_sb;
877 d_instantiate(res, root_inode);
883 static inline struct hlist_head *d_hash(struct dentry *parent,
886 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
887 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
888 return dentry_hashtable + (hash & D_HASHMASK);
892 * d_alloc_anon - allocate an anonymous dentry
893 * @inode: inode to allocate the dentry for
895 * This is similar to d_alloc_root. It is used by filesystems when
896 * creating a dentry for a given inode, often in the process of
897 * mapping a filehandle to a dentry. The returned dentry may be
898 * anonymous, or may have a full name (if the inode was already
899 * in the cache). The file system may need to make further
900 * efforts to connect this dentry into the dcache properly.
902 * When called on a directory inode, we must ensure that
903 * the inode only ever has one dentry. If a dentry is
904 * found, that is returned instead of allocating a new one.
906 * On successful return, the reference to the inode has been transferred
907 * to the dentry. If %NULL is returned (indicating kmalloc failure),
908 * the reference on the inode has not been released.
911 struct dentry * d_alloc_anon(struct inode *inode)
913 static const struct qstr anonstring = { .name = "" };
917 if ((res = d_find_alias(inode))) {
922 tmp = d_alloc(NULL, &anonstring);
926 tmp->d_parent = tmp; /* make sure dput doesn't croak */
928 spin_lock(&dcache_lock);
929 res = __d_find_alias(inode, 0);
931 /* attach a disconnected dentry */
934 spin_lock(&res->d_lock);
935 res->d_sb = inode->i_sb;
937 res->d_inode = inode;
938 res->d_flags |= DCACHE_DISCONNECTED;
939 res->d_flags &= ~DCACHE_UNHASHED;
940 list_add(&res->d_alias, &inode->i_dentry);
941 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
942 spin_unlock(&res->d_lock);
944 inode = NULL; /* don't drop reference */
946 spin_unlock(&dcache_lock);
957 * d_splice_alias - splice a disconnected dentry into the tree if one exists
958 * @inode: the inode which may have a disconnected dentry
959 * @dentry: a negative dentry which we want to point to the inode.
961 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
962 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
963 * and return it, else simply d_add the inode to the dentry and return NULL.
965 * This is needed in the lookup routine of any filesystem that is exportable
966 * (via knfsd) so that we can build dcache paths to directories effectively.
968 * If a dentry was found and moved, then it is returned. Otherwise NULL
969 * is returned. This matches the expected return value of ->lookup.
972 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
974 struct dentry *new = NULL;
977 spin_lock(&dcache_lock);
978 new = __d_find_alias(inode, 1);
980 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
981 spin_unlock(&dcache_lock);
982 security_d_instantiate(new, inode);
987 /* d_instantiate takes dcache_lock, so we do it by hand */
988 list_add(&dentry->d_alias, &inode->i_dentry);
989 dentry->d_inode = inode;
990 spin_unlock(&dcache_lock);
991 security_d_instantiate(dentry, inode);
995 d_add(dentry, inode);
1001 * d_lookup - search for a dentry
1002 * @parent: parent dentry
1003 * @name: qstr of name we wish to find
1005 * Searches the children of the parent dentry for the name in question. If
1006 * the dentry is found its reference count is incremented and the dentry
1007 * is returned. The caller must use d_put to free the entry when it has
1008 * finished using it. %NULL is returned on failure.
1010 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1011 * Memory barriers are used while updating and doing lockless traversal.
1012 * To avoid races with d_move while rename is happening, d_lock is used.
1014 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1015 * and name pointer in one structure pointed by d_qstr.
1017 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1018 * lookup is going on.
1020 * dentry_unused list is not updated even if lookup finds the required dentry
1021 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1022 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1025 * d_lookup() is protected against the concurrent renames in some unrelated
1026 * directory using the seqlockt_t rename_lock.
1029 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1031 struct dentry * dentry = NULL;
1035 seq = read_seqbegin(&rename_lock);
1036 dentry = __d_lookup(parent, name);
1039 } while (read_seqretry(&rename_lock, seq));
1043 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1045 unsigned int len = name->len;
1046 unsigned int hash = name->hash;
1047 const unsigned char *str = name->name;
1048 struct hlist_head *head = d_hash(parent,hash);
1049 struct dentry *found = NULL;
1050 struct hlist_node *node;
1054 hlist_for_each_rcu(node, head) {
1055 struct dentry *dentry;
1058 dentry = hlist_entry(node, struct dentry, d_hash);
1060 if (dentry->d_name.hash != hash)
1062 if (dentry->d_parent != parent)
1065 spin_lock(&dentry->d_lock);
1068 * Recheck the dentry after taking the lock - d_move may have
1069 * changed things. Don't bother checking the hash because we're
1070 * about to compare the whole name anyway.
1072 if (dentry->d_parent != parent)
1076 * It is safe to compare names since d_move() cannot
1077 * change the qstr (protected by d_lock).
1079 qstr = &dentry->d_name;
1080 if (parent->d_op && parent->d_op->d_compare) {
1081 if (parent->d_op->d_compare(parent, qstr, name))
1084 if (qstr->len != len)
1086 if (memcmp(qstr->name, str, len))
1090 if (!d_unhashed(dentry)) {
1091 atomic_inc(&dentry->d_count);
1094 spin_unlock(&dentry->d_lock);
1097 spin_unlock(&dentry->d_lock);
1105 * d_validate - verify dentry provided from insecure source
1106 * @dentry: The dentry alleged to be valid child of @dparent
1107 * @dparent: The parent dentry (known to be valid)
1108 * @hash: Hash of the dentry
1109 * @len: Length of the name
1111 * An insecure source has sent us a dentry, here we verify it and dget() it.
1112 * This is used by ncpfs in its readdir implementation.
1113 * Zero is returned in the dentry is invalid.
1116 int d_validate(struct dentry *dentry, struct dentry *dparent)
1118 struct hlist_head *base;
1119 struct hlist_node *lhp;
1121 /* Check whether the ptr might be valid at all.. */
1122 if (!kmem_ptr_validate(dentry_cache, dentry))
1125 if (dentry->d_parent != dparent)
1128 spin_lock(&dcache_lock);
1129 base = d_hash(dparent, dentry->d_name.hash);
1130 hlist_for_each(lhp,base) {
1131 /* hlist_for_each_rcu() not required for d_hash list
1132 * as it is parsed under dcache_lock
1134 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1135 __dget_locked(dentry);
1136 spin_unlock(&dcache_lock);
1140 spin_unlock(&dcache_lock);
1146 * When a file is deleted, we have two options:
1147 * - turn this dentry into a negative dentry
1148 * - unhash this dentry and free it.
1150 * Usually, we want to just turn this into
1151 * a negative dentry, but if anybody else is
1152 * currently using the dentry or the inode
1153 * we can't do that and we fall back on removing
1154 * it from the hash queues and waiting for
1155 * it to be deleted later when it has no users
1159 * d_delete - delete a dentry
1160 * @dentry: The dentry to delete
1162 * Turn the dentry into a negative dentry if possible, otherwise
1163 * remove it from the hash queues so it can be deleted later
1166 void d_delete(struct dentry * dentry)
1169 * Are we the only user?
1171 spin_lock(&dcache_lock);
1172 spin_lock(&dentry->d_lock);
1173 if (atomic_read(&dentry->d_count) == 1) {
1174 dentry_iput(dentry);
1178 if (!d_unhashed(dentry))
1181 spin_unlock(&dentry->d_lock);
1182 spin_unlock(&dcache_lock);
1185 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1188 entry->d_flags &= ~DCACHE_UNHASHED;
1189 hlist_add_head_rcu(&entry->d_hash, list);
1193 * d_rehash - add an entry back to the hash
1194 * @entry: dentry to add to the hash
1196 * Adds a dentry to the hash according to its name.
1199 void d_rehash(struct dentry * entry)
1201 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1203 spin_lock(&dcache_lock);
1204 spin_lock(&entry->d_lock);
1205 __d_rehash(entry, list);
1206 spin_unlock(&entry->d_lock);
1207 spin_unlock(&dcache_lock);
1210 #define do_switch(x,y) do { \
1211 __typeof__ (x) __tmp = x; \
1212 x = y; y = __tmp; } while (0)
1215 * When switching names, the actual string doesn't strictly have to
1216 * be preserved in the target - because we're dropping the target
1217 * anyway. As such, we can just do a simple memcpy() to copy over
1218 * the new name before we switch.
1220 * Note that we have to be a lot more careful about getting the hash
1221 * switched - we have to switch the hash value properly even if it
1222 * then no longer matches the actual (corrupted) string of the target.
1223 * The hash value has to match the hash queue that the dentry is on..
1225 static void switch_names(struct dentry *dentry, struct dentry *target)
1227 if (dname_external(target)) {
1228 if (dname_external(dentry)) {
1230 * Both external: swap the pointers
1232 do_switch(target->d_name.name, dentry->d_name.name);
1235 * dentry:internal, target:external. Steal target's
1236 * storage and make target internal.
1238 dentry->d_name.name = target->d_name.name;
1239 target->d_name.name = target->d_iname;
1242 if (dname_external(dentry)) {
1244 * dentry:external, target:internal. Give dentry's
1245 * storage to target and make dentry internal
1247 memcpy(dentry->d_iname, target->d_name.name,
1248 target->d_name.len + 1);
1249 target->d_name.name = dentry->d_name.name;
1250 dentry->d_name.name = dentry->d_iname;
1253 * Both are internal. Just copy target to dentry
1255 memcpy(dentry->d_iname, target->d_name.name,
1256 target->d_name.len + 1);
1262 * We cannibalize "target" when moving dentry on top of it,
1263 * because it's going to be thrown away anyway. We could be more
1264 * polite about it, though.
1266 * This forceful removal will result in ugly /proc output if
1267 * somebody holds a file open that got deleted due to a rename.
1268 * We could be nicer about the deleted file, and let it show
1269 * up under the name it got deleted rather than the name that
1274 * d_move - move a dentry
1275 * @dentry: entry to move
1276 * @target: new dentry
1278 * Update the dcache to reflect the move of a file name. Negative
1279 * dcache entries should not be moved in this way.
1282 void d_move(struct dentry * dentry, struct dentry * target)
1284 struct hlist_head *list;
1286 if (!dentry->d_inode)
1287 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1289 spin_lock(&dcache_lock);
1290 write_seqlock(&rename_lock);
1292 * XXXX: do we really need to take target->d_lock?
1294 if (target < dentry) {
1295 spin_lock(&target->d_lock);
1296 spin_lock(&dentry->d_lock);
1298 spin_lock(&dentry->d_lock);
1299 spin_lock(&target->d_lock);
1302 /* Move the dentry to the target hash queue, if on different bucket */
1303 if (dentry->d_flags & DCACHE_UNHASHED)
1304 goto already_unhashed;
1306 hlist_del_rcu(&dentry->d_hash);
1309 list = d_hash(target->d_parent, target->d_name.hash);
1310 __d_rehash(dentry, list);
1312 /* Unhash the target: dput() will then get rid of it */
1315 list_del(&dentry->d_child);
1316 list_del(&target->d_child);
1318 /* Switch the names.. */
1319 switch_names(dentry, target);
1320 do_switch(dentry->d_name.len, target->d_name.len);
1321 do_switch(dentry->d_name.hash, target->d_name.hash);
1323 /* ... and switch the parents */
1324 if (IS_ROOT(dentry)) {
1325 dentry->d_parent = target->d_parent;
1326 target->d_parent = target;
1327 INIT_LIST_HEAD(&target->d_child);
1329 do_switch(dentry->d_parent, target->d_parent);
1331 /* And add them back to the (new) parent lists */
1332 list_add(&target->d_child, &target->d_parent->d_subdirs);
1335 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
1336 spin_unlock(&target->d_lock);
1337 spin_unlock(&dentry->d_lock);
1338 write_sequnlock(&rename_lock);
1339 spin_unlock(&dcache_lock);
1343 * d_path - return the path of a dentry
1344 * @dentry: dentry to report
1345 * @vfsmnt: vfsmnt to which the dentry belongs
1346 * @root: root dentry
1347 * @rootmnt: vfsmnt to which the root dentry belongs
1348 * @buffer: buffer to return value in
1349 * @buflen: buffer length
1351 * Convert a dentry into an ASCII path name. If the entry has been deleted
1352 * the string " (deleted)" is appended. Note that this is ambiguous.
1354 * Returns the buffer or an error code if the path was too long.
1356 * "buflen" should be positive. Caller holds the dcache_lock.
1358 static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1359 struct dentry *root, struct vfsmount *rootmnt,
1360 char *buffer, int buflen)
1362 char * end = buffer+buflen;
1368 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1373 memcpy(end, " (deleted)", 10);
1383 struct dentry * parent;
1385 if (dentry == root && vfsmnt == rootmnt)
1387 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1389 spin_lock(&vfsmount_lock);
1390 if (vfsmnt->mnt_parent == vfsmnt) {
1391 spin_unlock(&vfsmount_lock);
1394 dentry = vfsmnt->mnt_mountpoint;
1395 vfsmnt = vfsmnt->mnt_parent;
1396 spin_unlock(&vfsmount_lock);
1399 parent = dentry->d_parent;
1401 namelen = dentry->d_name.len;
1402 buflen -= namelen + 1;
1406 memcpy(end, dentry->d_name.name, namelen);
1415 namelen = dentry->d_name.len;
1419 retval -= namelen-1; /* hit the slash */
1420 memcpy(retval, dentry->d_name.name, namelen);
1423 return ERR_PTR(-ENAMETOOLONG);
1426 /* write full pathname into buffer and return start of pathname */
1427 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1428 char *buf, int buflen)
1431 struct vfsmount *rootmnt;
1432 struct dentry *root;
1434 read_lock(¤t->fs->lock);
1435 rootmnt = mntget(current->fs->rootmnt);
1436 root = dget(current->fs->root);
1437 read_unlock(¤t->fs->lock);
1438 spin_lock(&dcache_lock);
1439 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1440 spin_unlock(&dcache_lock);
1447 * NOTE! The user-level library version returns a
1448 * character pointer. The kernel system call just
1449 * returns the length of the buffer filled (which
1450 * includes the ending '\0' character), or a negative
1451 * error value. So libc would do something like
1453 * char *getcwd(char * buf, size_t size)
1457 * retval = sys_getcwd(buf, size);
1464 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1467 struct vfsmount *pwdmnt, *rootmnt;
1468 struct dentry *pwd, *root;
1469 char *page = (char *) __get_free_page(GFP_USER);
1474 read_lock(¤t->fs->lock);
1475 pwdmnt = mntget(current->fs->pwdmnt);
1476 pwd = dget(current->fs->pwd);
1477 rootmnt = mntget(current->fs->rootmnt);
1478 root = dget(current->fs->root);
1479 read_unlock(¤t->fs->lock);
1482 /* Has the current directory has been unlinked? */
1483 spin_lock(&dcache_lock);
1484 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1488 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1489 spin_unlock(&dcache_lock);
1491 error = PTR_ERR(cwd);
1496 len = PAGE_SIZE + page - cwd;
1499 if (copy_to_user(buf, cwd, len))
1503 spin_unlock(&dcache_lock);
1510 free_page((unsigned long) page);
1515 * Test whether new_dentry is a subdirectory of old_dentry.
1517 * Trivially implemented using the dcache structure
1521 * is_subdir - is new dentry a subdirectory of old_dentry
1522 * @new_dentry: new dentry
1523 * @old_dentry: old dentry
1525 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1526 * Returns 0 otherwise.
1527 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1530 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1533 struct dentry * saved = new_dentry;
1536 /* need rcu_readlock to protect against the d_parent trashing due to
1541 /* for restarting inner loop in case of seq retry */
1544 seq = read_seqbegin(&rename_lock);
1546 if (new_dentry != old_dentry) {
1547 struct dentry * parent = new_dentry->d_parent;
1548 if (parent == new_dentry)
1550 new_dentry = parent;
1556 } while (read_seqretry(&rename_lock, seq));
1562 void d_genocide(struct dentry *root)
1564 struct dentry *this_parent = root;
1565 struct list_head *next;
1567 spin_lock(&dcache_lock);
1569 next = this_parent->d_subdirs.next;
1571 while (next != &this_parent->d_subdirs) {
1572 struct list_head *tmp = next;
1573 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1575 if (d_unhashed(dentry)||!dentry->d_inode)
1577 if (!list_empty(&dentry->d_subdirs)) {
1578 this_parent = dentry;
1581 atomic_dec(&dentry->d_count);
1583 if (this_parent != root) {
1584 next = this_parent->d_child.next;
1585 atomic_dec(&this_parent->d_count);
1586 this_parent = this_parent->d_parent;
1589 spin_unlock(&dcache_lock);
1593 * find_inode_number - check for dentry with name
1594 * @dir: directory to check
1595 * @name: Name to find.
1597 * Check whether a dentry already exists for the given name,
1598 * and return the inode number if it has an inode. Otherwise
1601 * This routine is used to post-process directory listings for
1602 * filesystems using synthetic inode numbers, and is necessary
1603 * to keep getcwd() working.
1606 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1608 struct dentry * dentry;
1612 * Check for a fs-specific hash function. Note that we must
1613 * calculate the standard hash first, as the d_op->d_hash()
1614 * routine may choose to leave the hash value unchanged.
1616 name->hash = full_name_hash(name->name, name->len);
1617 if (dir->d_op && dir->d_op->d_hash)
1619 if (dir->d_op->d_hash(dir, name) != 0)
1623 dentry = d_lookup(dir, name);
1626 if (dentry->d_inode)
1627 ino = dentry->d_inode->i_ino;
1634 static __initdata unsigned long dhash_entries;
1635 static int __init set_dhash_entries(char *str)
1639 dhash_entries = simple_strtoul(str, &str, 0);
1642 __setup("dhash_entries=", set_dhash_entries);
1644 static void __init dcache_init_early(void)
1648 /* If hashes are distributed across NUMA nodes, defer
1649 * hash allocation until vmalloc space is available.
1655 alloc_large_system_hash("Dentry cache",
1656 sizeof(struct hlist_head),
1664 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1665 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1668 static void __init dcache_init(unsigned long mempages)
1673 * A constructor could be added for stable state like the lists,
1674 * but it is probably not worth it because of the cache nature
1677 dentry_cache = kmem_cache_create("dentry_cache",
1678 sizeof(struct dentry),
1680 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC,
1683 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1685 /* Hash may have been set up in dcache_init_early */
1690 alloc_large_system_hash("Dentry cache",
1691 sizeof(struct hlist_head),
1699 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1700 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1703 /* SLAB cache for __getname() consumers */
1704 kmem_cache_t *names_cachep;
1706 /* SLAB cache for file structures */
1707 kmem_cache_t *filp_cachep;
1709 EXPORT_SYMBOL(d_genocide);
1711 extern void bdev_cache_init(void);
1712 extern void chrdev_init(void);
1714 void __init vfs_caches_init_early(void)
1716 dcache_init_early();
1720 void __init vfs_caches_init(unsigned long mempages)
1722 unsigned long reserve;
1724 /* Base hash sizes on available memory, with a reserve equal to
1725 150% of current kernel size */
1727 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1728 mempages -= reserve;
1730 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1731 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1733 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
1734 SLAB_HWCACHE_ALIGN|SLAB_PANIC, filp_ctor, filp_dtor);
1736 dcache_init(mempages);
1737 inode_init(mempages);
1738 files_init(mempages);
1744 EXPORT_SYMBOL(d_alloc);
1745 EXPORT_SYMBOL(d_alloc_anon);
1746 EXPORT_SYMBOL(d_alloc_root);
1747 EXPORT_SYMBOL(d_delete);
1748 EXPORT_SYMBOL(d_find_alias);
1749 EXPORT_SYMBOL(d_instantiate);
1750 EXPORT_SYMBOL(d_invalidate);
1751 EXPORT_SYMBOL(d_lookup);
1752 EXPORT_SYMBOL(d_move);
1753 EXPORT_SYMBOL(d_path);
1754 EXPORT_SYMBOL(d_prune_aliases);
1755 EXPORT_SYMBOL(d_rehash);
1756 EXPORT_SYMBOL(d_splice_alias);
1757 EXPORT_SYMBOL(d_validate);
1758 EXPORT_SYMBOL(dget_locked);
1759 EXPORT_SYMBOL(dput);
1760 EXPORT_SYMBOL(find_inode_number);
1761 EXPORT_SYMBOL(have_submounts);
1762 EXPORT_SYMBOL(names_cachep);
1763 EXPORT_SYMBOL(shrink_dcache_parent);
1764 EXPORT_SYMBOL(shrink_dcache_sb);