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;
40 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
41 seqlock_t rename_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
43 EXPORT_SYMBOL(dcache_lock);
45 static kmem_cache_t *dentry_cache;
47 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
50 * This is the single most critical data structure when it comes
51 * to the dcache: the hashtable for lookups. Somebody should try
52 * to make this good - I've just made it work.
54 * This hash-function tries to avoid losing too many bits of hash
55 * information, yet avoid using a prime hash-size or similar.
57 #define D_HASHBITS d_hash_shift
58 #define D_HASHMASK d_hash_mask
60 static unsigned int d_hash_mask;
61 static unsigned int d_hash_shift;
62 static struct hlist_head *dentry_hashtable;
63 static LIST_HEAD(dentry_unused);
65 /* Statistics gathering. */
66 struct dentry_stat_t dentry_stat = {
70 static void d_callback(struct rcu_head *head)
72 struct dentry * dentry = container_of(head, struct dentry, d_rcu);
74 if (dname_external(dentry))
75 kfree(dentry->d_name.name);
76 kmem_cache_free(dentry_cache, dentry);
80 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
83 static void d_free(struct dentry *dentry)
85 if (dentry->d_op && dentry->d_op->d_release)
86 dentry->d_op->d_release(dentry);
87 call_rcu(&dentry->d_rcu, d_callback);
91 * Release the dentry's inode, using the filesystem
92 * d_iput() operation if defined.
93 * Called with dcache_lock and per dentry lock held, drops both.
95 static inline void dentry_iput(struct dentry * dentry)
97 struct inode *inode = dentry->d_inode;
99 dentry->d_inode = NULL;
100 list_del_init(&dentry->d_alias);
101 spin_unlock(&dentry->d_lock);
102 spin_unlock(&dcache_lock);
103 if (dentry->d_op && dentry->d_op->d_iput)
104 dentry->d_op->d_iput(dentry, inode);
108 spin_unlock(&dentry->d_lock);
109 spin_unlock(&dcache_lock);
116 * This is complicated by the fact that we do not want to put
117 * dentries that are no longer on any hash chain on the unused
118 * list: we'd much rather just get rid of them immediately.
120 * However, that implies that we have to traverse the dentry
121 * tree upwards to the parents which might _also_ now be
122 * scheduled for deletion (it may have been only waiting for
123 * its last child to go away).
125 * This tail recursion is done by hand as we don't want to depend
126 * on the compiler to always get this right (gcc generally doesn't).
127 * Real recursion would eat up our stack space.
131 * dput - release a dentry
132 * @dentry: dentry to release
134 * Release a dentry. This will drop the usage count and if appropriate
135 * call the dentry unlink method as well as removing it from the queues and
136 * releasing its resources. If the parent dentries were scheduled for release
137 * they too may now get deleted.
139 * no dcache lock, please.
142 void dput(struct dentry *dentry)
148 if (atomic_read(&dentry->d_count) == 1)
150 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
153 spin_lock(&dentry->d_lock);
154 if (atomic_read(&dentry->d_count)) {
155 spin_unlock(&dentry->d_lock);
156 spin_unlock(&dcache_lock);
161 * AV: ->d_delete() is _NOT_ allowed to block now.
163 if (dentry->d_op && dentry->d_op->d_delete) {
164 if (dentry->d_op->d_delete(dentry))
167 /* Unreachable? Get rid of it */
168 if (d_unhashed(dentry))
170 if (list_empty(&dentry->d_lru)) {
171 dentry->d_flags |= DCACHE_REFERENCED;
172 list_add(&dentry->d_lru, &dentry_unused);
173 dentry_stat.nr_unused++;
175 spin_unlock(&dentry->d_lock);
176 spin_unlock(&dcache_lock);
183 struct dentry *parent;
185 /* If dentry was on d_lru list
186 * delete it from there
188 if (!list_empty(&dentry->d_lru)) {
189 list_del(&dentry->d_lru);
190 dentry_stat.nr_unused--;
192 list_del(&dentry->d_child);
193 dentry_stat.nr_dentry--; /* For d_free, below */
194 /*drops the locks, at that point nobody can reach this dentry */
196 parent = dentry->d_parent;
198 if (dentry == parent)
206 * d_invalidate - invalidate a dentry
207 * @dentry: dentry to invalidate
209 * Try to invalidate the dentry if it turns out to be
210 * possible. If there are other dentries that can be
211 * reached through this one we can't delete it and we
212 * return -EBUSY. On success we return 0.
217 int d_invalidate(struct dentry * dentry)
220 * If it's already been dropped, return OK.
222 spin_lock(&dcache_lock);
223 if (d_unhashed(dentry)) {
224 spin_unlock(&dcache_lock);
228 * Check whether to do a partial shrink_dcache
229 * to get rid of unused child entries.
231 if (!list_empty(&dentry->d_subdirs)) {
232 spin_unlock(&dcache_lock);
233 shrink_dcache_parent(dentry);
234 spin_lock(&dcache_lock);
238 * Somebody else still using it?
240 * If it's a directory, we can't drop it
241 * for fear of somebody re-populating it
242 * with children (even though dropping it
243 * would make it unreachable from the root,
244 * we might still populate it if it was a
245 * working directory or similar).
247 spin_lock(&dentry->d_lock);
248 if (atomic_read(&dentry->d_count) > 1) {
249 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
250 spin_unlock(&dentry->d_lock);
251 spin_unlock(&dcache_lock);
257 spin_unlock(&dentry->d_lock);
258 spin_unlock(&dcache_lock);
262 /* This should be called _only_ with dcache_lock held */
264 static inline struct dentry * __dget_locked(struct dentry *dentry)
266 atomic_inc(&dentry->d_count);
267 if (!list_empty(&dentry->d_lru)) {
268 dentry_stat.nr_unused--;
269 list_del_init(&dentry->d_lru);
274 struct dentry * dget_locked(struct dentry *dentry)
276 return __dget_locked(dentry);
280 * d_find_alias - grab a hashed alias of inode
281 * @inode: inode in question
282 * @want_discon: flag, used by d_splice_alias, to request
283 * that only a DISCONNECTED alias be returned.
285 * If inode has a hashed alias, or is a directory and has any alias,
286 * acquire the reference to alias and return it. Otherwise return NULL.
287 * Notice that if inode is a directory there can be only one alias and
288 * it can be unhashed only if it has no children, or if it is the root
291 * If the inode has a DCACHE_DISCONNECTED alias, then prefer
292 * any other hashed alias over that one unless @want_discon is set,
293 * in which case only return a DCACHE_DISCONNECTED alias.
296 static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
298 struct list_head *head, *next, *tmp;
299 struct dentry *alias, *discon_alias=NULL;
301 head = &inode->i_dentry;
302 next = inode->i_dentry.next;
303 while (next != head) {
307 alias = list_entry(tmp, struct dentry, d_alias);
308 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
309 if (alias->d_flags & DCACHE_DISCONNECTED)
310 discon_alias = alias;
311 else if (!want_discon) {
312 __dget_locked(alias);
318 __dget_locked(discon_alias);
322 struct dentry * d_find_alias(struct inode *inode)
325 spin_lock(&dcache_lock);
326 de = __d_find_alias(inode, 0);
327 spin_unlock(&dcache_lock);
332 * Try to kill dentries associated with this inode.
333 * WARNING: you must own a reference to inode.
335 void d_prune_aliases(struct inode *inode)
337 struct list_head *tmp, *head = &inode->i_dentry;
339 spin_lock(&dcache_lock);
341 while ((tmp = tmp->next) != head) {
342 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
343 if (!atomic_read(&dentry->d_count)) {
344 __dget_locked(dentry);
346 spin_unlock(&dcache_lock);
351 spin_unlock(&dcache_lock);
355 * Throw away a dentry - free the inode, dput the parent.
356 * This requires that the LRU list has already been
358 * Called with dcache_lock, drops it and then regains.
360 static inline void prune_one_dentry(struct dentry * dentry)
362 struct dentry * parent;
365 list_del(&dentry->d_child);
366 dentry_stat.nr_dentry--; /* For d_free, below */
368 parent = dentry->d_parent;
370 if (parent != dentry)
372 spin_lock(&dcache_lock);
376 * prune_dcache - shrink the dcache
377 * @count: number of entries to try and free
379 * Shrink the dcache. This is done when we need
380 * more memory, or simply when we need to unmount
381 * something (at which point we need to unuse
384 * This function may fail to free any resources if
385 * all the dentries are in use.
388 static void prune_dcache(int count)
390 spin_lock(&dcache_lock);
391 for (; count ; count--) {
392 struct dentry *dentry;
393 struct list_head *tmp;
395 cond_resched_lock(&dcache_lock);
397 tmp = dentry_unused.prev;
398 if (tmp == &dentry_unused)
401 prefetch(dentry_unused.prev);
402 dentry_stat.nr_unused--;
403 dentry = list_entry(tmp, struct dentry, d_lru);
405 spin_lock(&dentry->d_lock);
407 * We found an inuse dentry which was not removed from
408 * dentry_unused because of laziness during lookup. Do not free
409 * it - just keep it off the dentry_unused list.
411 if (atomic_read(&dentry->d_count)) {
412 spin_unlock(&dentry->d_lock);
415 /* If the dentry was recently referenced, don't free it. */
416 if (dentry->d_flags & DCACHE_REFERENCED) {
417 dentry->d_flags &= ~DCACHE_REFERENCED;
418 list_add(&dentry->d_lru, &dentry_unused);
419 dentry_stat.nr_unused++;
420 spin_unlock(&dentry->d_lock);
423 prune_one_dentry(dentry);
425 spin_unlock(&dcache_lock);
429 * Shrink the dcache for the specified super block.
430 * This allows us to unmount a device without disturbing
431 * the dcache for the other devices.
433 * This implementation makes just two traversals of the
434 * unused list. On the first pass we move the selected
435 * dentries to the most recent end, and on the second
436 * pass we free them. The second pass must restart after
437 * each dput(), but since the target dentries are all at
438 * the end, it's really just a single traversal.
442 * shrink_dcache_sb - shrink dcache for a superblock
445 * Shrink the dcache for the specified super block. This
446 * is used to free the dcache before unmounting a file
450 void shrink_dcache_sb(struct super_block * sb)
452 struct list_head *tmp, *next;
453 struct dentry *dentry;
456 * Pass one ... move the dentries for the specified
457 * superblock to the most recent end of the unused list.
459 spin_lock(&dcache_lock);
460 next = dentry_unused.next;
461 while (next != &dentry_unused) {
464 dentry = list_entry(tmp, struct dentry, d_lru);
465 if (dentry->d_sb != sb)
468 list_add(tmp, &dentry_unused);
472 * Pass two ... free the dentries for this superblock.
475 next = dentry_unused.next;
476 while (next != &dentry_unused) {
479 dentry = list_entry(tmp, struct dentry, d_lru);
480 if (dentry->d_sb != sb)
482 dentry_stat.nr_unused--;
484 spin_lock(&dentry->d_lock);
485 if (atomic_read(&dentry->d_count)) {
486 spin_unlock(&dentry->d_lock);
489 prune_one_dentry(dentry);
492 spin_unlock(&dcache_lock);
496 * Search for at least 1 mount point in the dentry's subdirs.
497 * We descend to the next level whenever the d_subdirs
498 * list is non-empty and continue searching.
502 * have_submounts - check for mounts over a dentry
503 * @parent: dentry to check.
505 * Return true if the parent or its subdirectories contain
509 int have_submounts(struct dentry *parent)
511 struct dentry *this_parent = parent;
512 struct list_head *next;
514 spin_lock(&dcache_lock);
515 if (d_mountpoint(parent))
518 next = this_parent->d_subdirs.next;
520 while (next != &this_parent->d_subdirs) {
521 struct list_head *tmp = next;
522 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
524 /* Have we found a mount point ? */
525 if (d_mountpoint(dentry))
527 if (!list_empty(&dentry->d_subdirs)) {
528 this_parent = dentry;
533 * All done at this level ... ascend and resume the search.
535 if (this_parent != parent) {
536 next = this_parent->d_child.next;
537 this_parent = this_parent->d_parent;
540 spin_unlock(&dcache_lock);
541 return 0; /* No mount points found in tree */
543 spin_unlock(&dcache_lock);
548 * Search the dentry child list for the specified parent,
549 * and move any unused dentries to the end of the unused
550 * list for prune_dcache(). We descend to the next level
551 * whenever the d_subdirs list is non-empty and continue
554 * It returns zero iff there are no unused children,
555 * otherwise it returns the number of children moved to
556 * the end of the unused list. This may not be the total
557 * number of unused children, because select_parent can
558 * drop the lock and return early due to latency
561 static int select_parent(struct dentry * parent)
563 struct dentry *this_parent = parent;
564 struct list_head *next;
567 spin_lock(&dcache_lock);
569 next = this_parent->d_subdirs.next;
571 while (next != &this_parent->d_subdirs) {
572 struct list_head *tmp = next;
573 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
576 if (!list_empty(&dentry->d_lru)) {
577 dentry_stat.nr_unused--;
578 list_del_init(&dentry->d_lru);
581 * move only zero ref count dentries to the end
582 * of the unused list for prune_dcache
584 if (!atomic_read(&dentry->d_count)) {
585 list_add(&dentry->d_lru, dentry_unused.prev);
586 dentry_stat.nr_unused++;
591 * We can return to the caller if we have found some (this
592 * ensures forward progress). We'll be coming back to find
595 if (found && need_resched())
599 * Descend a level if the d_subdirs list is non-empty.
601 if (!list_empty(&dentry->d_subdirs)) {
602 this_parent = dentry;
604 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
605 dentry->d_parent->d_name.name, dentry->d_name.name, found);
611 * All done at this level ... ascend and resume the search.
613 if (this_parent != parent) {
614 next = this_parent->d_child.next;
615 this_parent = this_parent->d_parent;
617 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
618 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
623 spin_unlock(&dcache_lock);
628 * shrink_dcache_parent - prune dcache
629 * @parent: parent of entries to prune
631 * Prune the dcache to remove unused children of the parent dentry.
634 void shrink_dcache_parent(struct dentry * parent)
638 while ((found = select_parent(parent)) != 0)
643 * shrink_dcache_anon - further prune the cache
644 * @head: head of d_hash list of dentries to prune
646 * Prune the dentries that are anonymous
648 * parsing d_hash list does not hlist_for_each_rcu() as it
649 * done under dcache_lock.
652 void shrink_dcache_anon(struct hlist_head *head)
654 struct hlist_node *lp;
658 spin_lock(&dcache_lock);
659 hlist_for_each(lp, head) {
660 struct dentry *this = hlist_entry(lp, struct dentry, d_hash);
661 if (!list_empty(&this->d_lru)) {
662 dentry_stat.nr_unused--;
663 list_del_init(&this->d_lru);
667 * move only zero ref count dentries to the end
668 * of the unused list for prune_dcache
670 if (!atomic_read(&this->d_count)) {
671 list_add_tail(&this->d_lru, &dentry_unused);
672 dentry_stat.nr_unused++;
676 spin_unlock(&dcache_lock);
682 * Scan `nr' dentries and return the number which remain.
684 * We need to avoid reentering the filesystem if the caller is performing a
685 * GFP_NOFS allocation attempt. One example deadlock is:
687 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
688 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
689 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
691 * In this case we return -1 to tell the caller that we baled.
693 static int shrink_dcache_memory(int nr, unsigned int gfp_mask)
696 if (!(gfp_mask & __GFP_FS))
700 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
704 * d_alloc - allocate a dcache entry
705 * @parent: parent of entry to allocate
706 * @name: qstr of the name
708 * Allocates a dentry. It returns %NULL if there is insufficient memory
709 * available. On a success the dentry is returned. The name passed in is
710 * copied and the copy passed in may be reused after this call.
713 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
715 struct dentry *dentry;
718 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
722 if (name->len > DNAME_INLINE_LEN-1) {
723 dname = kmalloc(name->len + 1, GFP_KERNEL);
725 kmem_cache_free(dentry_cache, dentry);
729 dname = dentry->d_iname;
731 dentry->d_name.name = dname;
733 dentry->d_name.len = name->len;
734 dentry->d_name.hash = name->hash;
735 memcpy(dname, name->name, name->len);
736 dname[name->len] = 0;
738 atomic_set(&dentry->d_count, 1);
739 dentry->d_flags = DCACHE_UNHASHED;
740 spin_lock_init(&dentry->d_lock);
741 dentry->d_inode = NULL;
742 dentry->d_parent = NULL;
745 dentry->d_fsdata = NULL;
746 dentry->d_mounted = 0;
747 dentry->d_cookie = NULL;
748 INIT_HLIST_NODE(&dentry->d_hash);
749 INIT_LIST_HEAD(&dentry->d_lru);
750 INIT_LIST_HEAD(&dentry->d_subdirs);
751 INIT_LIST_HEAD(&dentry->d_alias);
754 dentry->d_parent = dget(parent);
755 dentry->d_sb = parent->d_sb;
757 INIT_LIST_HEAD(&dentry->d_child);
760 spin_lock(&dcache_lock);
762 list_add(&dentry->d_child, &parent->d_subdirs);
763 dentry_stat.nr_dentry++;
764 spin_unlock(&dcache_lock);
769 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
774 q.len = strlen(name);
775 q.hash = full_name_hash(q.name, q.len);
776 return d_alloc(parent, &q);
780 * d_instantiate - fill in inode information for a dentry
781 * @entry: dentry to complete
782 * @inode: inode to attach to this dentry
784 * Fill in inode information in the entry.
786 * This turns negative dentries into productive full members
789 * NOTE! This assumes that the inode count has been incremented
790 * (or otherwise set) by the caller to indicate that it is now
791 * in use by the dcache.
794 void d_instantiate(struct dentry *entry, struct inode * inode)
796 if (!list_empty(&entry->d_alias)) BUG();
797 spin_lock(&dcache_lock);
799 list_add(&entry->d_alias, &inode->i_dentry);
800 entry->d_inode = inode;
801 spin_unlock(&dcache_lock);
802 security_d_instantiate(entry, inode);
806 * d_instantiate_unique - instantiate a non-aliased dentry
807 * @entry: dentry to instantiate
808 * @inode: inode to attach to this dentry
810 * Fill in inode information in the entry. On success, it returns NULL.
811 * If an unhashed alias of "entry" already exists, then we return the
812 * aliased dentry instead.
814 * Note that in order to avoid conflicts with rename() etc, the caller
815 * had better be holding the parent directory semaphore.
817 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
819 struct dentry *alias;
820 int len = entry->d_name.len;
821 const char *name = entry->d_name.name;
822 unsigned int hash = entry->d_name.hash;
824 BUG_ON(!list_empty(&entry->d_alias));
825 spin_lock(&dcache_lock);
828 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
829 struct qstr *qstr = &alias->d_name;
831 if (qstr->hash != hash)
833 if (alias->d_parent != entry->d_parent)
835 if (qstr->len != len)
837 if (memcmp(qstr->name, name, len))
840 spin_unlock(&dcache_lock);
841 BUG_ON(!d_unhashed(alias));
844 list_add(&entry->d_alias, &inode->i_dentry);
846 entry->d_inode = inode;
847 spin_unlock(&dcache_lock);
848 security_d_instantiate(entry, inode);
851 EXPORT_SYMBOL(d_instantiate_unique);
854 * d_alloc_root - allocate root dentry
855 * @root_inode: inode to allocate the root for
857 * Allocate a root ("/") dentry for the inode given. The inode is
858 * instantiated and returned. %NULL is returned if there is insufficient
859 * memory or the inode passed is %NULL.
862 struct dentry * d_alloc_root(struct inode * root_inode)
864 struct dentry *res = NULL;
867 static const struct qstr name = { .name = "/", .len = 1 };
869 res = d_alloc(NULL, &name);
871 res->d_sb = root_inode->i_sb;
873 d_instantiate(res, root_inode);
879 static inline struct hlist_head *d_hash(struct dentry *parent,
882 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
883 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
884 return dentry_hashtable + (hash & D_HASHMASK);
888 * d_alloc_anon - allocate an anonymous dentry
889 * @inode: inode to allocate the dentry for
891 * This is similar to d_alloc_root. It is used by filesystems when
892 * creating a dentry for a given inode, often in the process of
893 * mapping a filehandle to a dentry. The returned dentry may be
894 * anonymous, or may have a full name (if the inode was already
895 * in the cache). The file system may need to make further
896 * efforts to connect this dentry into the dcache properly.
898 * When called on a directory inode, we must ensure that
899 * the inode only ever has one dentry. If a dentry is
900 * found, that is returned instead of allocating a new one.
902 * On successful return, the reference to the inode has been transferred
903 * to the dentry. If %NULL is returned (indicating kmalloc failure),
904 * the reference on the inode has not been released.
907 struct dentry * d_alloc_anon(struct inode *inode)
909 static const struct qstr anonstring = { .name = "" };
913 if ((res = d_find_alias(inode))) {
918 tmp = d_alloc(NULL, &anonstring);
922 tmp->d_parent = tmp; /* make sure dput doesn't croak */
924 spin_lock(&dcache_lock);
925 res = __d_find_alias(inode, 0);
927 /* attach a disconnected dentry */
930 spin_lock(&res->d_lock);
931 res->d_sb = inode->i_sb;
933 res->d_inode = inode;
934 res->d_flags |= DCACHE_DISCONNECTED;
935 res->d_flags &= ~DCACHE_UNHASHED;
936 list_add(&res->d_alias, &inode->i_dentry);
937 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
938 spin_unlock(&res->d_lock);
940 inode = NULL; /* don't drop reference */
942 spin_unlock(&dcache_lock);
953 * d_splice_alias - splice a disconnected dentry into the tree if one exists
954 * @inode: the inode which may have a disconnected dentry
955 * @dentry: a negative dentry which we want to point to the inode.
957 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
958 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
959 * and return it, else simply d_add the inode to the dentry and return NULL.
961 * This is needed in the lookup routine of any filesystem that is exportable
962 * (via knfsd) so that we can build dcache paths to directories effectively.
964 * If a dentry was found and moved, then it is returned. Otherwise NULL
965 * is returned. This matches the expected return value of ->lookup.
968 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
970 struct dentry *new = NULL;
973 spin_lock(&dcache_lock);
974 new = __d_find_alias(inode, 1);
976 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
977 spin_unlock(&dcache_lock);
978 security_d_instantiate(new, inode);
983 /* d_instantiate takes dcache_lock, so we do it by hand */
984 list_add(&dentry->d_alias, &inode->i_dentry);
985 dentry->d_inode = inode;
986 spin_unlock(&dcache_lock);
987 security_d_instantiate(dentry, inode);
991 d_add(dentry, inode);
997 * d_lookup - search for a dentry
998 * @parent: parent dentry
999 * @name: qstr of name we wish to find
1001 * Searches the children of the parent dentry for the name in question. If
1002 * the dentry is found its reference count is incremented and the dentry
1003 * is returned. The caller must use d_put to free the entry when it has
1004 * finished using it. %NULL is returned on failure.
1006 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1007 * Memory barriers are used while updating and doing lockless traversal.
1008 * To avoid races with d_move while rename is happening, d_lock is used.
1010 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1011 * and name pointer in one structure pointed by d_qstr.
1013 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1014 * lookup is going on.
1016 * dentry_unused list is not updated even if lookup finds the required dentry
1017 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1018 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1021 * d_lookup() is protected against the concurrent renames in some unrelated
1022 * directory using the seqlockt_t rename_lock.
1025 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1027 struct dentry * dentry = NULL;
1031 seq = read_seqbegin(&rename_lock);
1032 dentry = __d_lookup(parent, name);
1035 } while (read_seqretry(&rename_lock, seq));
1039 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1041 unsigned int len = name->len;
1042 unsigned int hash = name->hash;
1043 const unsigned char *str = name->name;
1044 struct hlist_head *head = d_hash(parent,hash);
1045 struct dentry *found = NULL;
1046 struct hlist_node *node;
1050 hlist_for_each_rcu(node, head) {
1051 struct dentry *dentry;
1054 dentry = hlist_entry(node, struct dentry, d_hash);
1056 if (dentry->d_name.hash != hash)
1058 if (dentry->d_parent != parent)
1061 spin_lock(&dentry->d_lock);
1064 * Recheck the dentry after taking the lock - d_move may have
1065 * changed things. Don't bother checking the hash because we're
1066 * about to compare the whole name anyway.
1068 if (dentry->d_parent != parent)
1072 * It is safe to compare names since d_move() cannot
1073 * change the qstr (protected by d_lock).
1075 qstr = &dentry->d_name;
1076 if (parent->d_op && parent->d_op->d_compare) {
1077 if (parent->d_op->d_compare(parent, qstr, name))
1080 if (qstr->len != len)
1082 if (memcmp(qstr->name, str, len))
1086 if (!d_unhashed(dentry)) {
1087 atomic_inc(&dentry->d_count);
1090 spin_unlock(&dentry->d_lock);
1093 spin_unlock(&dentry->d_lock);
1101 * d_validate - verify dentry provided from insecure source
1102 * @dentry: The dentry alleged to be valid child of @dparent
1103 * @dparent: The parent dentry (known to be valid)
1104 * @hash: Hash of the dentry
1105 * @len: Length of the name
1107 * An insecure source has sent us a dentry, here we verify it and dget() it.
1108 * This is used by ncpfs in its readdir implementation.
1109 * Zero is returned in the dentry is invalid.
1112 int d_validate(struct dentry *dentry, struct dentry *dparent)
1114 struct hlist_head *base;
1115 struct hlist_node *lhp;
1117 /* Check whether the ptr might be valid at all.. */
1118 if (!kmem_ptr_validate(dentry_cache, dentry))
1121 if (dentry->d_parent != dparent)
1124 spin_lock(&dcache_lock);
1125 base = d_hash(dparent, dentry->d_name.hash);
1126 hlist_for_each(lhp,base) {
1127 /* hlist_for_each_rcu() not required for d_hash list
1128 * as it is parsed under dcache_lock
1130 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1131 __dget_locked(dentry);
1132 spin_unlock(&dcache_lock);
1136 spin_unlock(&dcache_lock);
1142 * When a file is deleted, we have two options:
1143 * - turn this dentry into a negative dentry
1144 * - unhash this dentry and free it.
1146 * Usually, we want to just turn this into
1147 * a negative dentry, but if anybody else is
1148 * currently using the dentry or the inode
1149 * we can't do that and we fall back on removing
1150 * it from the hash queues and waiting for
1151 * it to be deleted later when it has no users
1155 * d_delete - delete a dentry
1156 * @dentry: The dentry to delete
1158 * Turn the dentry into a negative dentry if possible, otherwise
1159 * remove it from the hash queues so it can be deleted later
1162 void d_delete(struct dentry * dentry)
1165 * Are we the only user?
1167 spin_lock(&dcache_lock);
1168 spin_lock(&dentry->d_lock);
1169 if (atomic_read(&dentry->d_count) == 1) {
1170 dentry_iput(dentry);
1174 if (!d_unhashed(dentry))
1177 spin_unlock(&dentry->d_lock);
1178 spin_unlock(&dcache_lock);
1181 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1184 entry->d_flags &= ~DCACHE_UNHASHED;
1185 hlist_add_head_rcu(&entry->d_hash, list);
1189 * d_rehash - add an entry back to the hash
1190 * @entry: dentry to add to the hash
1192 * Adds a dentry to the hash according to its name.
1195 void d_rehash(struct dentry * entry)
1197 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1199 spin_lock(&dcache_lock);
1200 spin_lock(&entry->d_lock);
1201 __d_rehash(entry, list);
1202 spin_unlock(&entry->d_lock);
1203 spin_unlock(&dcache_lock);
1206 #define do_switch(x,y) do { \
1207 __typeof__ (x) __tmp = x; \
1208 x = y; y = __tmp; } while (0)
1211 * When switching names, the actual string doesn't strictly have to
1212 * be preserved in the target - because we're dropping the target
1213 * anyway. As such, we can just do a simple memcpy() to copy over
1214 * the new name before we switch.
1216 * Note that we have to be a lot more careful about getting the hash
1217 * switched - we have to switch the hash value properly even if it
1218 * then no longer matches the actual (corrupted) string of the target.
1219 * The hash value has to match the hash queue that the dentry is on..
1221 static void switch_names(struct dentry *dentry, struct dentry *target)
1223 if (dname_external(target)) {
1224 if (dname_external(dentry)) {
1226 * Both external: swap the pointers
1228 do_switch(target->d_name.name, dentry->d_name.name);
1231 * dentry:internal, target:external. Steal target's
1232 * storage and make target internal.
1234 dentry->d_name.name = target->d_name.name;
1235 target->d_name.name = target->d_iname;
1238 if (dname_external(dentry)) {
1240 * dentry:external, target:internal. Give dentry's
1241 * storage to target and make dentry internal
1243 memcpy(dentry->d_iname, target->d_name.name,
1244 target->d_name.len + 1);
1245 target->d_name.name = dentry->d_name.name;
1246 dentry->d_name.name = dentry->d_iname;
1249 * Both are internal. Just copy target to dentry
1251 memcpy(dentry->d_iname, target->d_name.name,
1252 target->d_name.len + 1);
1258 * We cannibalize "target" when moving dentry on top of it,
1259 * because it's going to be thrown away anyway. We could be more
1260 * polite about it, though.
1262 * This forceful removal will result in ugly /proc output if
1263 * somebody holds a file open that got deleted due to a rename.
1264 * We could be nicer about the deleted file, and let it show
1265 * up under the name it got deleted rather than the name that
1270 * d_move - move a dentry
1271 * @dentry: entry to move
1272 * @target: new dentry
1274 * Update the dcache to reflect the move of a file name. Negative
1275 * dcache entries should not be moved in this way.
1278 void d_move(struct dentry * dentry, struct dentry * target)
1280 struct hlist_head *list;
1282 if (!dentry->d_inode)
1283 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1285 spin_lock(&dcache_lock);
1286 write_seqlock(&rename_lock);
1288 * XXXX: do we really need to take target->d_lock?
1290 if (target < dentry) {
1291 spin_lock(&target->d_lock);
1292 spin_lock(&dentry->d_lock);
1294 spin_lock(&dentry->d_lock);
1295 spin_lock(&target->d_lock);
1298 /* Move the dentry to the target hash queue, if on different bucket */
1299 if (dentry->d_flags & DCACHE_UNHASHED)
1300 goto already_unhashed;
1302 hlist_del_rcu(&dentry->d_hash);
1305 list = d_hash(target->d_parent, target->d_name.hash);
1306 __d_rehash(dentry, list);
1308 /* Unhash the target: dput() will then get rid of it */
1311 list_del(&dentry->d_child);
1312 list_del(&target->d_child);
1314 /* Switch the names.. */
1315 switch_names(dentry, target);
1316 do_switch(dentry->d_name.len, target->d_name.len);
1317 do_switch(dentry->d_name.hash, target->d_name.hash);
1319 /* ... and switch the parents */
1320 if (IS_ROOT(dentry)) {
1321 dentry->d_parent = target->d_parent;
1322 target->d_parent = target;
1323 INIT_LIST_HEAD(&target->d_child);
1325 do_switch(dentry->d_parent, target->d_parent);
1327 /* And add them back to the (new) parent lists */
1328 list_add(&target->d_child, &target->d_parent->d_subdirs);
1331 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
1332 spin_unlock(&target->d_lock);
1333 spin_unlock(&dentry->d_lock);
1334 write_sequnlock(&rename_lock);
1335 spin_unlock(&dcache_lock);
1339 * d_path - return the path of a dentry
1340 * @dentry: dentry to report
1341 * @vfsmnt: vfsmnt to which the dentry belongs
1342 * @root: root dentry
1343 * @rootmnt: vfsmnt to which the root dentry belongs
1344 * @buffer: buffer to return value in
1345 * @buflen: buffer length
1347 * Convert a dentry into an ASCII path name. If the entry has been deleted
1348 * the string " (deleted)" is appended. Note that this is ambiguous.
1350 * Returns the buffer or an error code if the path was too long.
1352 * "buflen" should be positive. Caller holds the dcache_lock.
1354 static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1355 struct dentry *root, struct vfsmount *rootmnt,
1356 char *buffer, int buflen)
1358 char * end = buffer+buflen;
1364 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1369 memcpy(end, " (deleted)", 10);
1379 struct dentry * parent;
1381 if (dentry == root && vfsmnt == rootmnt)
1383 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1385 spin_lock(&vfsmount_lock);
1386 if (vfsmnt->mnt_parent == vfsmnt) {
1387 spin_unlock(&vfsmount_lock);
1390 dentry = vfsmnt->mnt_mountpoint;
1391 vfsmnt = vfsmnt->mnt_parent;
1392 spin_unlock(&vfsmount_lock);
1395 parent = dentry->d_parent;
1397 namelen = dentry->d_name.len;
1398 buflen -= namelen + 1;
1402 memcpy(end, dentry->d_name.name, namelen);
1411 namelen = dentry->d_name.len;
1415 retval -= namelen-1; /* hit the slash */
1416 memcpy(retval, dentry->d_name.name, namelen);
1419 return ERR_PTR(-ENAMETOOLONG);
1422 /* write full pathname into buffer and return start of pathname */
1423 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1424 char *buf, int buflen)
1427 struct vfsmount *rootmnt;
1428 struct dentry *root;
1430 read_lock(¤t->fs->lock);
1431 rootmnt = mntget(current->fs->rootmnt);
1432 root = dget(current->fs->root);
1433 read_unlock(¤t->fs->lock);
1434 spin_lock(&dcache_lock);
1435 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1436 spin_unlock(&dcache_lock);
1443 * NOTE! The user-level library version returns a
1444 * character pointer. The kernel system call just
1445 * returns the length of the buffer filled (which
1446 * includes the ending '\0' character), or a negative
1447 * error value. So libc would do something like
1449 * char *getcwd(char * buf, size_t size)
1453 * retval = sys_getcwd(buf, size);
1460 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1463 struct vfsmount *pwdmnt, *rootmnt;
1464 struct dentry *pwd, *root;
1465 char *page = (char *) __get_free_page(GFP_USER);
1470 read_lock(¤t->fs->lock);
1471 pwdmnt = mntget(current->fs->pwdmnt);
1472 pwd = dget(current->fs->pwd);
1473 rootmnt = mntget(current->fs->rootmnt);
1474 root = dget(current->fs->root);
1475 read_unlock(¤t->fs->lock);
1478 /* Has the current directory has been unlinked? */
1479 spin_lock(&dcache_lock);
1480 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1484 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1485 spin_unlock(&dcache_lock);
1487 error = PTR_ERR(cwd);
1492 len = PAGE_SIZE + page - cwd;
1495 if (copy_to_user(buf, cwd, len))
1499 spin_unlock(&dcache_lock);
1506 free_page((unsigned long) page);
1511 * Test whether new_dentry is a subdirectory of old_dentry.
1513 * Trivially implemented using the dcache structure
1517 * is_subdir - is new dentry a subdirectory of old_dentry
1518 * @new_dentry: new dentry
1519 * @old_dentry: old dentry
1521 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1522 * Returns 0 otherwise.
1523 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1526 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1529 struct dentry * saved = new_dentry;
1533 /* need rcu_readlock to protect against the d_parent trashing due to
1538 /* for restarting inner loop in case of seq retry */
1540 seq = read_seqbegin(&rename_lock);
1542 if (new_dentry != old_dentry) {
1543 struct dentry * parent = new_dentry->d_parent;
1544 if (parent == new_dentry)
1546 new_dentry = parent;
1552 } while (read_seqretry(&rename_lock, seq));
1558 void d_genocide(struct dentry *root)
1560 struct dentry *this_parent = root;
1561 struct list_head *next;
1563 spin_lock(&dcache_lock);
1565 next = this_parent->d_subdirs.next;
1567 while (next != &this_parent->d_subdirs) {
1568 struct list_head *tmp = next;
1569 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1571 if (d_unhashed(dentry)||!dentry->d_inode)
1573 if (!list_empty(&dentry->d_subdirs)) {
1574 this_parent = dentry;
1577 atomic_dec(&dentry->d_count);
1579 if (this_parent != root) {
1580 next = this_parent->d_child.next;
1581 atomic_dec(&this_parent->d_count);
1582 this_parent = this_parent->d_parent;
1585 spin_unlock(&dcache_lock);
1589 * find_inode_number - check for dentry with name
1590 * @dir: directory to check
1591 * @name: Name to find.
1593 * Check whether a dentry already exists for the given name,
1594 * and return the inode number if it has an inode. Otherwise
1597 * This routine is used to post-process directory listings for
1598 * filesystems using synthetic inode numbers, and is necessary
1599 * to keep getcwd() working.
1602 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1604 struct dentry * dentry;
1608 * Check for a fs-specific hash function. Note that we must
1609 * calculate the standard hash first, as the d_op->d_hash()
1610 * routine may choose to leave the hash value unchanged.
1612 name->hash = full_name_hash(name->name, name->len);
1613 if (dir->d_op && dir->d_op->d_hash)
1615 if (dir->d_op->d_hash(dir, name) != 0)
1619 dentry = d_lookup(dir, name);
1622 if (dentry->d_inode)
1623 ino = dentry->d_inode->i_ino;
1630 static __initdata unsigned long dhash_entries;
1631 static int __init set_dhash_entries(char *str)
1635 dhash_entries = simple_strtoul(str, &str, 0);
1638 __setup("dhash_entries=", set_dhash_entries);
1640 static void __init dcache_init_early(void)
1644 /* If hashes are distributed across NUMA nodes, defer
1645 * hash allocation until vmalloc space is available.
1651 alloc_large_system_hash("Dentry cache",
1652 sizeof(struct hlist_head),
1660 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1661 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1664 static void __init dcache_init(unsigned long mempages)
1669 * A constructor could be added for stable state like the lists,
1670 * but it is probably not worth it because of the cache nature
1673 dentry_cache = kmem_cache_create("dentry_cache",
1674 sizeof(struct dentry),
1676 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC,
1679 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1681 /* Hash may have been set up in dcache_init_early */
1686 alloc_large_system_hash("Dentry cache",
1687 sizeof(struct hlist_head),
1695 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1696 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1699 /* SLAB cache for __getname() consumers */
1700 kmem_cache_t *names_cachep;
1702 /* SLAB cache for file structures */
1703 kmem_cache_t *filp_cachep;
1705 EXPORT_SYMBOL(d_genocide);
1707 extern void bdev_cache_init(void);
1708 extern void chrdev_init(void);
1710 void __init vfs_caches_init_early(void)
1712 dcache_init_early();
1716 void __init vfs_caches_init(unsigned long mempages)
1718 unsigned long reserve;
1720 /* Base hash sizes on available memory, with a reserve equal to
1721 150% of current kernel size */
1723 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1724 mempages -= reserve;
1726 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1727 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1729 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
1730 SLAB_HWCACHE_ALIGN|SLAB_PANIC, filp_ctor, filp_dtor);
1732 dcache_init(mempages);
1733 inode_init(mempages);
1734 files_init(mempages);
1740 EXPORT_SYMBOL(d_alloc);
1741 EXPORT_SYMBOL(d_alloc_anon);
1742 EXPORT_SYMBOL(d_alloc_root);
1743 EXPORT_SYMBOL(d_delete);
1744 EXPORT_SYMBOL(d_find_alias);
1745 EXPORT_SYMBOL(d_instantiate);
1746 EXPORT_SYMBOL(d_invalidate);
1747 EXPORT_SYMBOL(d_lookup);
1748 EXPORT_SYMBOL(d_move);
1749 EXPORT_SYMBOL(d_path);
1750 EXPORT_SYMBOL(d_prune_aliases);
1751 EXPORT_SYMBOL(d_rehash);
1752 EXPORT_SYMBOL(d_splice_alias);
1753 EXPORT_SYMBOL(d_validate);
1754 EXPORT_SYMBOL(dget_locked);
1755 EXPORT_SYMBOL(dput);
1756 EXPORT_SYMBOL(find_inode_number);
1757 EXPORT_SYMBOL(have_submounts);
1758 EXPORT_SYMBOL(names_cachep);
1759 EXPORT_SYMBOL(shrink_dcache_parent);
1760 EXPORT_SYMBOL(shrink_dcache_sb);