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 spinlock_t dcache_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
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 if (dentry->d_extra_attributes) {
88 kfree(dentry->d_extra_attributes);
89 dentry->d_extra_attributes = NULL;
91 call_rcu(&dentry->d_rcu, d_callback);
95 * Release the dentry's inode, using the filesystem
96 * d_iput() operation if defined.
97 * Called with dcache_lock and per dentry lock held, drops both.
99 static inline void dentry_iput(struct dentry * dentry)
101 struct inode *inode = dentry->d_inode;
103 dentry->d_inode = NULL;
104 list_del_init(&dentry->d_alias);
105 spin_unlock(&dentry->d_lock);
106 spin_unlock(&dcache_lock);
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_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)
329 spin_lock(&dcache_lock);
330 de = __d_find_alias(inode, 0);
331 spin_unlock(&dcache_lock);
336 * Try to kill dentries associated with this inode.
337 * WARNING: you must own a reference to inode.
339 void d_prune_aliases(struct inode *inode)
341 struct list_head *tmp, *head = &inode->i_dentry;
343 spin_lock(&dcache_lock);
345 while ((tmp = tmp->next) != head) {
346 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
347 if (!atomic_read(&dentry->d_count)) {
348 __dget_locked(dentry);
350 spin_unlock(&dcache_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 tmp = dentry_unused.prev;
400 if (tmp == &dentry_unused)
403 prefetch(dentry_unused.prev);
404 dentry_stat.nr_unused--;
405 dentry = list_entry(tmp, struct dentry, d_lru);
407 spin_lock(&dentry->d_lock);
409 * We found an inuse dentry which was not removed from
410 * dentry_unused because of laziness during lookup. Do not free
411 * it - just keep it off the dentry_unused list.
413 if (atomic_read(&dentry->d_count)) {
414 spin_unlock(&dentry->d_lock);
417 /* If the dentry was recently referenced, don't free it. */
418 if (dentry->d_flags & DCACHE_REFERENCED) {
419 dentry->d_flags &= ~DCACHE_REFERENCED;
420 list_add(&dentry->d_lru, &dentry_unused);
421 dentry_stat.nr_unused++;
422 spin_unlock(&dentry->d_lock);
425 prune_one_dentry(dentry);
427 spin_unlock(&dcache_lock);
431 * Shrink the dcache for the specified super block.
432 * This allows us to unmount a device without disturbing
433 * the dcache for the other devices.
435 * This implementation makes just two traversals of the
436 * unused list. On the first pass we move the selected
437 * dentries to the most recent end, and on the second
438 * pass we free them. The second pass must restart after
439 * each dput(), but since the target dentries are all at
440 * the end, it's really just a single traversal.
444 * shrink_dcache_sb - shrink dcache for a superblock
447 * Shrink the dcache for the specified super block. This
448 * is used to free the dcache before unmounting a file
452 void shrink_dcache_sb(struct super_block * sb)
454 struct list_head *tmp, *next;
455 struct dentry *dentry;
458 * Pass one ... move the dentries for the specified
459 * superblock to the most recent end of the unused list.
461 spin_lock(&dcache_lock);
462 next = dentry_unused.next;
463 while (next != &dentry_unused) {
466 dentry = list_entry(tmp, struct dentry, d_lru);
467 if (dentry->d_sb != sb)
470 list_add(tmp, &dentry_unused);
474 * Pass two ... free the dentries for this superblock.
477 next = dentry_unused.next;
478 while (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_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_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 static int select_parent(struct dentry * parent)
558 struct dentry *this_parent = parent;
559 struct list_head *next;
562 spin_lock(&dcache_lock);
564 next = this_parent->d_subdirs.next;
566 while (next != &this_parent->d_subdirs) {
567 struct list_head *tmp = next;
568 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
571 if (!list_empty(&dentry->d_lru)) {
572 dentry_stat.nr_unused--;
573 list_del_init(&dentry->d_lru);
576 * move only zero ref count dentries to the end
577 * of the unused list for prune_dcache
579 if (!atomic_read(&dentry->d_count)) {
580 list_add(&dentry->d_lru, dentry_unused.prev);
581 dentry_stat.nr_unused++;
585 * Descend a level if the d_subdirs list is non-empty.
587 if (!list_empty(&dentry->d_subdirs)) {
588 this_parent = dentry;
590 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
591 dentry->d_parent->d_name.name, dentry->d_name.name, found);
597 * All done at this level ... ascend and resume the search.
599 if (this_parent != parent) {
600 next = this_parent->d_child.next;
601 this_parent = this_parent->d_parent;
603 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
604 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
608 spin_unlock(&dcache_lock);
613 * shrink_dcache_parent - prune dcache
614 * @parent: parent of entries to prune
616 * Prune the dcache to remove unused children of the parent dentry.
619 void shrink_dcache_parent(struct dentry * parent)
623 while ((found = select_parent(parent)) != 0)
628 * shrink_dcache_anon - further prune the cache
629 * @head: head of d_hash list of dentries to prune
631 * Prune the dentries that are anonymous
633 * parsing d_hash list does not hlist_for_each_rcu() as it
634 * done under dcache_lock.
637 void shrink_dcache_anon(struct hlist_head *head)
639 struct hlist_node *lp;
643 spin_lock(&dcache_lock);
644 hlist_for_each(lp, head) {
645 struct dentry *this = hlist_entry(lp, struct dentry, d_hash);
646 if (!list_empty(&this->d_lru)) {
647 dentry_stat.nr_unused--;
648 list_del_init(&this->d_lru);
652 * move only zero ref count dentries to the end
653 * of the unused list for prune_dcache
655 if (!atomic_read(&this->d_count)) {
656 list_add_tail(&this->d_lru, &dentry_unused);
657 dentry_stat.nr_unused++;
661 spin_unlock(&dcache_lock);
667 * Scan `nr' dentries and return the number which remain.
669 * We need to avoid reentering the filesystem if the caller is performing a
670 * GFP_NOFS allocation attempt. One example deadlock is:
672 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
673 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
674 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
676 * In this case we return -1 to tell the caller that we baled.
678 static int shrink_dcache_memory(int nr, unsigned int gfp_mask)
681 if (!(gfp_mask & __GFP_FS))
685 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
689 * d_alloc - allocate a dcache entry
690 * @parent: parent of entry to allocate
691 * @name: qstr of the name
693 * Allocates a dentry. It returns %NULL if there is insufficient memory
694 * available. On a success the dentry is returned. The name passed in is
695 * copied and the copy passed in may be reused after this call.
698 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
700 struct dentry *dentry;
703 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
707 if (name->len > DNAME_INLINE_LEN-1) {
708 dname = kmalloc(name->len + 1, GFP_KERNEL);
710 kmem_cache_free(dentry_cache, dentry);
714 dname = dentry->d_iname;
716 dentry->d_name.name = dname;
718 dentry->d_name.len = name->len;
719 dentry->d_name.hash = name->hash;
720 memcpy(dname, name->name, name->len);
721 dname[name->len] = 0;
723 atomic_set(&dentry->d_count, 1);
724 dentry->d_flags = DCACHE_UNHASHED;
725 dentry->d_lock = SPIN_LOCK_UNLOCKED;
726 dentry->d_inode = NULL;
727 dentry->d_parent = NULL;
730 dentry->d_fsdata = NULL;
731 dentry->d_extra_attributes = NULL;
732 dentry->d_mounted = 0;
733 dentry->d_cookie = NULL;
734 INIT_HLIST_NODE(&dentry->d_hash);
735 INIT_LIST_HEAD(&dentry->d_lru);
736 INIT_LIST_HEAD(&dentry->d_subdirs);
737 INIT_LIST_HEAD(&dentry->d_alias);
740 dentry->d_parent = dget(parent);
741 dentry->d_sb = parent->d_sb;
743 INIT_LIST_HEAD(&dentry->d_child);
746 spin_lock(&dcache_lock);
748 list_add(&dentry->d_child, &parent->d_subdirs);
749 dentry_stat.nr_dentry++;
750 spin_unlock(&dcache_lock);
755 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
760 q.len = strlen(name);
761 q.hash = full_name_hash(q.name, q.len);
762 return d_alloc(parent, &q);
766 * d_instantiate - fill in inode information for a dentry
767 * @entry: dentry to complete
768 * @inode: inode to attach to this dentry
770 * Fill in inode information in the entry.
772 * This turns negative dentries into productive full members
775 * NOTE! This assumes that the inode count has been incremented
776 * (or otherwise set) by the caller to indicate that it is now
777 * in use by the dcache.
780 void d_instantiate(struct dentry *entry, struct inode * inode)
782 if (!list_empty(&entry->d_alias)) BUG();
783 spin_lock(&dcache_lock);
785 list_add(&entry->d_alias, &inode->i_dentry);
786 entry->d_inode = inode;
787 spin_unlock(&dcache_lock);
788 security_d_instantiate(entry, inode);
792 * d_alloc_root - allocate root dentry
793 * @root_inode: inode to allocate the root for
795 * Allocate a root ("/") dentry for the inode given. The inode is
796 * instantiated and returned. %NULL is returned if there is insufficient
797 * memory or the inode passed is %NULL.
800 struct dentry * d_alloc_root(struct inode * root_inode)
802 struct dentry *res = NULL;
805 static const struct qstr name = { .name = "/", .len = 1 };
807 res = d_alloc(NULL, &name);
809 res->d_sb = root_inode->i_sb;
811 d_instantiate(res, root_inode);
817 static inline struct hlist_head *d_hash(struct dentry *parent,
820 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
821 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
822 return dentry_hashtable + (hash & D_HASHMASK);
826 * d_alloc_anon - allocate an anonymous dentry
827 * @inode: inode to allocate the dentry for
829 * This is similar to d_alloc_root. It is used by filesystems when
830 * creating a dentry for a given inode, often in the process of
831 * mapping a filehandle to a dentry. The returned dentry may be
832 * anonymous, or may have a full name (if the inode was already
833 * in the cache). The file system may need to make further
834 * efforts to connect this dentry into the dcache properly.
836 * When called on a directory inode, we must ensure that
837 * the inode only ever has one dentry. If a dentry is
838 * found, that is returned instead of allocating a new one.
840 * On successful return, the reference to the inode has been transferred
841 * to the dentry. If %NULL is returned (indicating kmalloc failure),
842 * the reference on the inode has not been released.
845 struct dentry * d_alloc_anon(struct inode *inode)
847 static const struct qstr anonstring = { .name = "" };
851 if ((res = d_find_alias(inode))) {
856 tmp = d_alloc(NULL, &anonstring);
860 tmp->d_parent = tmp; /* make sure dput doesn't croak */
862 spin_lock(&dcache_lock);
863 res = __d_find_alias(inode, 0);
865 /* attach a disconnected dentry */
868 spin_lock(&res->d_lock);
869 res->d_sb = inode->i_sb;
871 res->d_inode = inode;
872 res->d_flags |= DCACHE_DISCONNECTED;
873 res->d_flags &= ~DCACHE_UNHASHED;
874 list_add(&res->d_alias, &inode->i_dentry);
875 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
876 spin_unlock(&res->d_lock);
878 inode = NULL; /* don't drop reference */
880 spin_unlock(&dcache_lock);
891 * d_splice_alias - splice a disconnected dentry into the tree if one exists
892 * @inode: the inode which may have a disconnected dentry
893 * @dentry: a negative dentry which we want to point to the inode.
895 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
896 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
897 * and return it, else simply d_add the inode to the dentry and return NULL.
899 * This is needed in the lookup routine of any filesystem that is exportable
900 * (via knfsd) so that we can build dcache paths to directories effectively.
902 * If a dentry was found and moved, then it is returned. Otherwise NULL
903 * is returned. This matches the expected return value of ->lookup.
906 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
908 struct dentry *new = NULL;
911 spin_lock(&dcache_lock);
912 new = __d_find_alias(inode, 1);
914 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
915 spin_unlock(&dcache_lock);
916 security_d_instantiate(new, inode);
921 /* d_instantiate takes dcache_lock, so we do it by hand */
922 list_add(&dentry->d_alias, &inode->i_dentry);
923 dentry->d_inode = inode;
924 spin_unlock(&dcache_lock);
925 security_d_instantiate(dentry, inode);
929 d_add(dentry, inode);
935 * d_lookup - search for a dentry
936 * @parent: parent dentry
937 * @name: qstr of name we wish to find
939 * Searches the children of the parent dentry for the name in question. If
940 * the dentry is found its reference count is incremented and the dentry
941 * is returned. The caller must use d_put to free the entry when it has
942 * finished using it. %NULL is returned on failure.
944 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
945 * Memory barriers are used while updating and doing lockless traversal.
946 * To avoid races with d_move while rename is happening, d_lock is used.
948 * Overflows in memcmp(), while d_move, are avoided by keeping the length
949 * and name pointer in one structure pointed by d_qstr.
951 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
952 * lookup is going on.
954 * dentry_unused list is not updated even if lookup finds the required dentry
955 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
956 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
959 * d_lookup() is protected against the concurrent renames in some unrelated
960 * directory using the seqlockt_t rename_lock.
963 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
965 struct dentry * dentry = NULL;
969 seq = read_seqbegin(&rename_lock);
970 dentry = __d_lookup(parent, name);
973 } while (read_seqretry(&rename_lock, seq));
977 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
979 unsigned int len = name->len;
980 unsigned int hash = name->hash;
981 const unsigned char *str = name->name;
982 struct hlist_head *head = d_hash(parent,hash);
983 struct dentry *found = NULL;
984 struct hlist_node *node;
988 hlist_for_each_rcu(node, head) {
989 struct dentry *dentry;
992 dentry = hlist_entry(node, struct dentry, d_hash);
994 if (dentry->d_name.hash != hash)
996 if (dentry->d_parent != parent)
999 spin_lock(&dentry->d_lock);
1002 * Recheck the dentry after taking the lock - d_move may have
1003 * changed things. Don't bother checking the hash because we're
1004 * about to compare the whole name anyway.
1006 if (dentry->d_parent != parent)
1010 * It is safe to compare names since d_move() cannot
1011 * change the qstr (protected by d_lock).
1013 qstr = &dentry->d_name;
1014 if (parent->d_op && parent->d_op->d_compare) {
1015 if (parent->d_op->d_compare(parent, qstr, name))
1018 if (qstr->len != len)
1020 if (memcmp(qstr->name, str, len))
1024 if (!d_unhashed(dentry)) {
1025 atomic_inc(&dentry->d_count);
1028 spin_unlock(&dentry->d_lock);
1031 spin_unlock(&dentry->d_lock);
1039 * d_validate - verify dentry provided from insecure source
1040 * @dentry: The dentry alleged to be valid child of @dparent
1041 * @dparent: The parent dentry (known to be valid)
1042 * @hash: Hash of the dentry
1043 * @len: Length of the name
1045 * An insecure source has sent us a dentry, here we verify it and dget() it.
1046 * This is used by ncpfs in its readdir implementation.
1047 * Zero is returned in the dentry is invalid.
1050 int d_validate(struct dentry *dentry, struct dentry *dparent)
1052 struct hlist_head *base;
1053 struct hlist_node *lhp;
1055 /* Check whether the ptr might be valid at all.. */
1056 if (!kmem_ptr_validate(dentry_cache, dentry))
1059 if (dentry->d_parent != dparent)
1062 spin_lock(&dcache_lock);
1063 base = d_hash(dparent, dentry->d_name.hash);
1064 hlist_for_each(lhp,base) {
1065 /* hlist_for_each_rcu() not required for d_hash list
1066 * as it is parsed under dcache_lock
1068 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1069 __dget_locked(dentry);
1070 spin_unlock(&dcache_lock);
1074 spin_unlock(&dcache_lock);
1080 * When a file is deleted, we have two options:
1081 * - turn this dentry into a negative dentry
1082 * - unhash this dentry and free it.
1084 * Usually, we want to just turn this into
1085 * a negative dentry, but if anybody else is
1086 * currently using the dentry or the inode
1087 * we can't do that and we fall back on removing
1088 * it from the hash queues and waiting for
1089 * it to be deleted later when it has no users
1093 * d_delete - delete a dentry
1094 * @dentry: The dentry to delete
1096 * Turn the dentry into a negative dentry if possible, otherwise
1097 * remove it from the hash queues so it can be deleted later
1100 void d_delete(struct dentry * dentry)
1103 * Are we the only user?
1105 spin_lock(&dcache_lock);
1106 spin_lock(&dentry->d_lock);
1107 if (atomic_read(&dentry->d_count) == 1) {
1108 dentry_iput(dentry);
1112 if (!d_unhashed(dentry))
1115 spin_unlock(&dentry->d_lock);
1116 spin_unlock(&dcache_lock);
1119 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1122 entry->d_flags &= ~DCACHE_UNHASHED;
1123 hlist_add_head_rcu(&entry->d_hash, list);
1127 * d_rehash - add an entry back to the hash
1128 * @entry: dentry to add to the hash
1130 * Adds a dentry to the hash according to its name.
1133 void d_rehash(struct dentry * entry)
1135 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1137 spin_lock(&dcache_lock);
1138 spin_lock(&entry->d_lock);
1139 __d_rehash(entry, list);
1140 spin_unlock(&entry->d_lock);
1141 spin_unlock(&dcache_lock);
1144 #define do_switch(x,y) do { \
1145 __typeof__ (x) __tmp = x; \
1146 x = y; y = __tmp; } while (0)
1149 * When switching names, the actual string doesn't strictly have to
1150 * be preserved in the target - because we're dropping the target
1151 * anyway. As such, we can just do a simple memcpy() to copy over
1152 * the new name before we switch.
1154 * Note that we have to be a lot more careful about getting the hash
1155 * switched - we have to switch the hash value properly even if it
1156 * then no longer matches the actual (corrupted) string of the target.
1157 * The hash value has to match the hash queue that the dentry is on..
1159 static void switch_names(struct dentry *dentry, struct dentry *target)
1161 if (dname_external(target)) {
1162 if (dname_external(dentry)) {
1164 * Both external: swap the pointers
1166 do_switch(target->d_name.name, dentry->d_name.name);
1169 * dentry:internal, target:external. Steal target's
1170 * storage and make target internal.
1172 dentry->d_name.name = target->d_name.name;
1173 target->d_name.name = target->d_iname;
1176 if (dname_external(dentry)) {
1178 * dentry:external, target:internal. Give dentry's
1179 * storage to target and make dentry internal
1181 memcpy(dentry->d_iname, target->d_name.name,
1182 target->d_name.len + 1);
1183 target->d_name.name = dentry->d_name.name;
1184 dentry->d_name.name = dentry->d_iname;
1187 * Both are internal. Just copy target to dentry
1189 memcpy(dentry->d_iname, target->d_name.name,
1190 target->d_name.len + 1);
1196 * We cannibalize "target" when moving dentry on top of it,
1197 * because it's going to be thrown away anyway. We could be more
1198 * polite about it, though.
1200 * This forceful removal will result in ugly /proc output if
1201 * somebody holds a file open that got deleted due to a rename.
1202 * We could be nicer about the deleted file, and let it show
1203 * up under the name it got deleted rather than the name that
1208 * d_move - move a dentry
1209 * @dentry: entry to move
1210 * @target: new dentry
1212 * Update the dcache to reflect the move of a file name. Negative
1213 * dcache entries should not be moved in this way.
1216 void d_move(struct dentry * dentry, struct dentry * target)
1218 struct hlist_head *list;
1220 if (!dentry->d_inode)
1221 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1223 spin_lock(&dcache_lock);
1224 write_seqlock(&rename_lock);
1226 * XXXX: do we really need to take target->d_lock?
1228 if (target < dentry) {
1229 spin_lock(&target->d_lock);
1230 spin_lock(&dentry->d_lock);
1232 spin_lock(&dentry->d_lock);
1233 spin_lock(&target->d_lock);
1236 /* Move the dentry to the target hash queue, if on different bucket */
1237 if (dentry->d_flags & DCACHE_UNHASHED)
1238 goto already_unhashed;
1240 hlist_del_rcu(&dentry->d_hash);
1243 list = d_hash(target->d_parent, target->d_name.hash);
1244 __d_rehash(dentry, list);
1246 /* Unhash the target: dput() will then get rid of it */
1249 /* flush any possible attributes */
1250 if (dentry->d_extra_attributes) {
1251 kfree(dentry->d_extra_attributes);
1252 dentry->d_extra_attributes = NULL;
1254 if (target->d_extra_attributes) {
1255 kfree(target->d_extra_attributes);
1256 target->d_extra_attributes = NULL;
1259 list_del(&dentry->d_child);
1260 list_del(&target->d_child);
1262 /* Switch the names.. */
1263 switch_names(dentry, target);
1264 do_switch(dentry->d_name.len, target->d_name.len);
1265 do_switch(dentry->d_name.hash, target->d_name.hash);
1267 /* ... and switch the parents */
1268 if (IS_ROOT(dentry)) {
1269 dentry->d_parent = target->d_parent;
1270 target->d_parent = target;
1271 INIT_LIST_HEAD(&target->d_child);
1273 do_switch(dentry->d_parent, target->d_parent);
1275 /* And add them back to the (new) parent lists */
1276 list_add(&target->d_child, &target->d_parent->d_subdirs);
1279 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
1280 spin_unlock(&target->d_lock);
1281 spin_unlock(&dentry->d_lock);
1282 write_sequnlock(&rename_lock);
1283 spin_unlock(&dcache_lock);
1287 * d_path - return the path of a dentry
1288 * @dentry: dentry to report
1289 * @vfsmnt: vfsmnt to which the dentry belongs
1290 * @root: root dentry
1291 * @rootmnt: vfsmnt to which the root dentry belongs
1292 * @buffer: buffer to return value in
1293 * @buflen: buffer length
1295 * Convert a dentry into an ASCII path name. If the entry has been deleted
1296 * the string " (deleted)" is appended. Note that this is ambiguous.
1298 * Returns the buffer or an error code if the path was too long.
1300 * "buflen" should be positive. Caller holds the dcache_lock.
1302 char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1303 struct dentry *root, struct vfsmount *rootmnt,
1304 char *buffer, int buflen)
1306 char * end = buffer+buflen;
1312 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1317 memcpy(end, " (deleted)", 10);
1327 struct dentry * parent;
1329 if (dentry == root && vfsmnt == rootmnt)
1331 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1333 spin_lock(&vfsmount_lock);
1334 if (vfsmnt->mnt_parent == vfsmnt) {
1335 spin_unlock(&vfsmount_lock);
1338 dentry = vfsmnt->mnt_mountpoint;
1339 vfsmnt = vfsmnt->mnt_parent;
1340 spin_unlock(&vfsmount_lock);
1343 parent = dentry->d_parent;
1345 namelen = dentry->d_name.len;
1346 buflen -= namelen + 1;
1350 memcpy(end, dentry->d_name.name, namelen);
1359 namelen = dentry->d_name.len;
1363 retval -= namelen-1; /* hit the slash */
1364 memcpy(retval, dentry->d_name.name, namelen);
1367 return ERR_PTR(-ENAMETOOLONG);
1370 EXPORT_SYMBOL_GPL(__d_path);
1372 /* write full pathname into buffer and return start of pathname */
1373 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1374 char *buf, int buflen)
1377 struct vfsmount *rootmnt;
1378 struct dentry *root;
1380 read_lock(¤t->fs->lock);
1381 rootmnt = mntget(current->fs->rootmnt);
1382 root = dget(current->fs->root);
1383 read_unlock(¤t->fs->lock);
1384 spin_lock(&dcache_lock);
1385 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1386 spin_unlock(&dcache_lock);
1393 * NOTE! The user-level library version returns a
1394 * character pointer. The kernel system call just
1395 * returns the length of the buffer filled (which
1396 * includes the ending '\0' character), or a negative
1397 * error value. So libc would do something like
1399 * char *getcwd(char * buf, size_t size)
1403 * retval = sys_getcwd(buf, size);
1410 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1413 struct vfsmount *pwdmnt, *rootmnt;
1414 struct dentry *pwd, *root;
1415 char *page = (char *) __get_free_page(GFP_USER);
1420 read_lock(¤t->fs->lock);
1421 pwdmnt = mntget(current->fs->pwdmnt);
1422 pwd = dget(current->fs->pwd);
1423 rootmnt = mntget(current->fs->rootmnt);
1424 root = dget(current->fs->root);
1425 read_unlock(¤t->fs->lock);
1428 /* Has the current directory has been unlinked? */
1429 spin_lock(&dcache_lock);
1430 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1434 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1435 spin_unlock(&dcache_lock);
1437 error = PTR_ERR(cwd);
1442 len = PAGE_SIZE + page - cwd;
1445 if (copy_to_user(buf, cwd, len))
1449 spin_unlock(&dcache_lock);
1456 free_page((unsigned long) page);
1461 * Test whether new_dentry is a subdirectory of old_dentry.
1463 * Trivially implemented using the dcache structure
1467 * is_subdir - is new dentry a subdirectory of old_dentry
1468 * @new_dentry: new dentry
1469 * @old_dentry: old dentry
1471 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1472 * Returns 0 otherwise.
1473 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1476 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1479 struct dentry * saved = new_dentry;
1483 /* need rcu_readlock to protect against the d_parent trashing due to
1488 /* for restarting inner loop in case of seq retry */
1490 seq = read_seqbegin(&rename_lock);
1492 if (new_dentry != old_dentry) {
1493 struct dentry * parent = new_dentry->d_parent;
1494 if (parent == new_dentry)
1496 new_dentry = parent;
1502 } while (read_seqretry(&rename_lock, seq));
1508 void d_genocide(struct dentry *root)
1510 struct dentry *this_parent = root;
1511 struct list_head *next;
1513 spin_lock(&dcache_lock);
1515 next = this_parent->d_subdirs.next;
1517 while (next != &this_parent->d_subdirs) {
1518 struct list_head *tmp = next;
1519 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1521 if (d_unhashed(dentry)||!dentry->d_inode)
1523 if (!list_empty(&dentry->d_subdirs)) {
1524 this_parent = dentry;
1527 atomic_dec(&dentry->d_count);
1529 if (this_parent != root) {
1530 next = this_parent->d_child.next;
1531 atomic_dec(&this_parent->d_count);
1532 this_parent = this_parent->d_parent;
1535 spin_unlock(&dcache_lock);
1539 * find_inode_number - check for dentry with name
1540 * @dir: directory to check
1541 * @name: Name to find.
1543 * Check whether a dentry already exists for the given name,
1544 * and return the inode number if it has an inode. Otherwise
1547 * This routine is used to post-process directory listings for
1548 * filesystems using synthetic inode numbers, and is necessary
1549 * to keep getcwd() working.
1552 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1554 struct dentry * dentry;
1558 * Check for a fs-specific hash function. Note that we must
1559 * calculate the standard hash first, as the d_op->d_hash()
1560 * routine may choose to leave the hash value unchanged.
1562 name->hash = full_name_hash(name->name, name->len);
1563 if (dir->d_op && dir->d_op->d_hash)
1565 if (dir->d_op->d_hash(dir, name) != 0)
1569 dentry = d_lookup(dir, name);
1572 if (dentry->d_inode)
1573 ino = dentry->d_inode->i_ino;
1580 static __initdata unsigned long dhash_entries;
1581 static int __init set_dhash_entries(char *str)
1585 dhash_entries = simple_strtoul(str, &str, 0);
1588 __setup("dhash_entries=", set_dhash_entries);
1590 static void __init dcache_init_early(void)
1595 alloc_large_system_hash("Dentry cache",
1596 sizeof(struct hlist_head),
1603 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1604 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1607 void flush_dentry_attributes (void)
1609 struct hlist_node *tmp;
1610 struct dentry *dentry;
1613 spin_lock(&dcache_lock);
1614 for (i = 0; i <= d_hash_mask; i++)
1615 hlist_for_each_entry(dentry, tmp, dentry_hashtable+i, d_hash) {
1616 kfree(dentry->d_extra_attributes);
1617 dentry->d_extra_attributes = NULL;
1619 spin_unlock(&dcache_lock);
1622 EXPORT_SYMBOL_GPL(flush_dentry_attributes);
1624 static void __init dcache_init(unsigned long mempages)
1627 * A constructor could be added for stable state like the lists,
1628 * but it is probably not worth it because of the cache nature
1631 dentry_cache = kmem_cache_create("dentry_cache",
1632 sizeof(struct dentry),
1634 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC,
1637 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1640 /* SLAB cache for __getname() consumers */
1641 kmem_cache_t *names_cachep;
1643 /* SLAB cache for file structures */
1644 kmem_cache_t *filp_cachep;
1646 EXPORT_SYMBOL(d_genocide);
1648 extern void bdev_cache_init(void);
1649 extern void chrdev_init(void);
1651 void __init vfs_caches_init_early(void)
1653 dcache_init_early();
1657 void __init vfs_caches_init(unsigned long mempages)
1659 unsigned long reserve;
1661 /* Base hash sizes on available memory, with a reserve equal to
1662 150% of current kernel size */
1664 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1665 mempages -= reserve;
1667 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1668 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1670 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
1671 SLAB_HWCACHE_ALIGN|SLAB_PANIC, filp_ctor, filp_dtor);
1673 dcache_init(mempages);
1674 inode_init(mempages);
1675 files_init(mempages);
1681 EXPORT_SYMBOL(d_alloc);
1682 EXPORT_SYMBOL(d_alloc_anon);
1683 EXPORT_SYMBOL(d_alloc_root);
1684 EXPORT_SYMBOL(d_delete);
1685 EXPORT_SYMBOL(d_find_alias);
1686 EXPORT_SYMBOL(d_instantiate);
1687 EXPORT_SYMBOL(d_invalidate);
1688 EXPORT_SYMBOL(d_lookup);
1689 EXPORT_SYMBOL(d_move);
1690 EXPORT_SYMBOL(d_path);
1691 EXPORT_SYMBOL(d_prune_aliases);
1692 EXPORT_SYMBOL(d_rehash);
1693 EXPORT_SYMBOL(d_splice_alias);
1694 EXPORT_SYMBOL(d_validate);
1695 EXPORT_SYMBOL(dget_locked);
1696 EXPORT_SYMBOL(dput);
1697 EXPORT_SYMBOL(find_inode_number);
1698 EXPORT_SYMBOL(have_submounts);
1699 EXPORT_SYMBOL(names_cachep);
1700 EXPORT_SYMBOL(shrink_dcache_parent);
1701 EXPORT_SYMBOL(shrink_dcache_sb);