4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
10 * Notes on the allocation strategy:
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
17 #include <linux/config.h>
18 #include <linux/syscalls.h>
19 #include <linux/string.h>
22 #include <linux/fsnotify.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/smp_lock.h>
26 #include <linux/hash.h>
27 #include <linux/cache.h>
28 #include <linux/module.h>
29 #include <linux/mount.h>
30 #include <linux/file.h>
31 #include <asm/uaccess.h>
32 #include <linux/security.h>
33 #include <linux/seqlock.h>
34 #include <linux/swap.h>
35 #include <linux/bootmem.h>
38 int sysctl_vfs_cache_pressure __read_mostly = 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 __read_mostly;
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 __read_mostly;
62 static unsigned int d_hash_shift __read_mostly;
63 static struct hlist_head *dentry_hashtable __read_mostly;
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_u.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_u.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 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);
105 fsnotify_inoderemove(inode);
106 if (dentry->d_op && dentry->d_op->d_iput)
107 dentry->d_op->d_iput(dentry, inode);
111 spin_unlock(&dentry->d_lock);
112 spin_unlock(&dcache_lock);
119 * This is complicated by the fact that we do not want to put
120 * dentries that are no longer on any hash chain on the unused
121 * list: we'd much rather just get rid of them immediately.
123 * However, that implies that we have to traverse the dentry
124 * tree upwards to the parents which might _also_ now be
125 * scheduled for deletion (it may have been only waiting for
126 * its last child to go away).
128 * This tail recursion is done by hand as we don't want to depend
129 * on the compiler to always get this right (gcc generally doesn't).
130 * Real recursion would eat up our stack space.
134 * dput - release a dentry
135 * @dentry: dentry to release
137 * Release a dentry. This will drop the usage count and if appropriate
138 * call the dentry unlink method as well as removing it from the queues and
139 * releasing its resources. If the parent dentries were scheduled for release
140 * they too may now get deleted.
142 * no dcache lock, please.
145 void dput(struct dentry *dentry)
151 if (atomic_read(&dentry->d_count) == 1)
153 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
156 spin_lock(&dentry->d_lock);
157 if (atomic_read(&dentry->d_count)) {
158 spin_unlock(&dentry->d_lock);
159 spin_unlock(&dcache_lock);
164 * AV: ->d_delete() is _NOT_ allowed to block now.
166 if (dentry->d_op && dentry->d_op->d_delete) {
167 if (dentry->d_op->d_delete(dentry))
170 /* Unreachable? Get rid of it */
171 if (d_unhashed(dentry))
173 if (list_empty(&dentry->d_lru)) {
174 dentry->d_flags |= DCACHE_REFERENCED;
175 list_add(&dentry->d_lru, &dentry_unused);
176 dentry_stat.nr_unused++;
178 spin_unlock(&dentry->d_lock);
179 spin_unlock(&dcache_lock);
186 struct dentry *parent;
188 /* If dentry was on d_lru list
189 * delete it from there
191 if (!list_empty(&dentry->d_lru)) {
192 list_del(&dentry->d_lru);
193 dentry_stat.nr_unused--;
195 list_del(&dentry->d_u.d_child);
196 dentry_stat.nr_dentry--; /* For d_free, below */
197 /*drops the locks, at that point nobody can reach this dentry */
199 parent = dentry->d_parent;
201 if (dentry == parent)
209 * d_invalidate - invalidate a dentry
210 * @dentry: dentry to invalidate
212 * Try to invalidate the dentry if it turns out to be
213 * possible. If there are other dentries that can be
214 * reached through this one we can't delete it and we
215 * return -EBUSY. On success we return 0.
220 int d_invalidate(struct dentry * dentry)
223 * If it's already been dropped, return OK.
225 spin_lock(&dcache_lock);
226 if (d_unhashed(dentry)) {
227 spin_unlock(&dcache_lock);
231 * Check whether to do a partial shrink_dcache
232 * to get rid of unused child entries.
234 if (!list_empty(&dentry->d_subdirs)) {
235 spin_unlock(&dcache_lock);
236 shrink_dcache_parent(dentry);
237 spin_lock(&dcache_lock);
241 * Somebody else still using it?
243 * If it's a directory, we can't drop it
244 * for fear of somebody re-populating it
245 * with children (even though dropping it
246 * would make it unreachable from the root,
247 * we might still populate it if it was a
248 * working directory or similar).
250 spin_lock(&dentry->d_lock);
251 if (atomic_read(&dentry->d_count) > 1) {
252 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
253 spin_unlock(&dentry->d_lock);
254 spin_unlock(&dcache_lock);
260 spin_unlock(&dentry->d_lock);
261 spin_unlock(&dcache_lock);
265 /* This should be called _only_ with dcache_lock held */
267 static inline struct dentry * __dget_locked(struct dentry *dentry)
269 atomic_inc(&dentry->d_count);
270 if (!list_empty(&dentry->d_lru)) {
271 dentry_stat.nr_unused--;
272 list_del_init(&dentry->d_lru);
277 struct dentry * dget_locked(struct dentry *dentry)
279 return __dget_locked(dentry);
283 * d_find_alias - grab a hashed alias of inode
284 * @inode: inode in question
285 * @want_discon: flag, used by d_splice_alias, to request
286 * that only a DISCONNECTED alias be returned.
288 * If inode has a hashed alias, or is a directory and has any alias,
289 * acquire the reference to alias and return it. Otherwise return NULL.
290 * Notice that if inode is a directory there can be only one alias and
291 * it can be unhashed only if it has no children, or if it is the root
294 * If the inode has a DCACHE_DISCONNECTED alias, then prefer
295 * any other hashed alias over that one unless @want_discon is set,
296 * in which case only return a DCACHE_DISCONNECTED alias.
299 static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
301 struct list_head *head, *next, *tmp;
302 struct dentry *alias, *discon_alias=NULL;
304 head = &inode->i_dentry;
305 next = inode->i_dentry.next;
306 while (next != head) {
310 alias = list_entry(tmp, struct dentry, d_alias);
311 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
312 if (alias->d_flags & DCACHE_DISCONNECTED)
313 discon_alias = alias;
314 else if (!want_discon) {
315 __dget_locked(alias);
321 __dget_locked(discon_alias);
325 struct dentry * d_find_alias(struct inode *inode)
327 struct dentry *de = NULL;
329 if (!list_empty(&inode->i_dentry)) {
330 spin_lock(&dcache_lock);
331 de = __d_find_alias(inode, 0);
332 spin_unlock(&dcache_lock);
338 * Try to kill dentries associated with this inode.
339 * WARNING: you must own a reference to inode.
341 void d_prune_aliases(struct inode *inode)
343 struct dentry *dentry;
345 spin_lock(&dcache_lock);
346 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
347 spin_lock(&dentry->d_lock);
348 if (!atomic_read(&dentry->d_count)) {
349 __dget_locked(dentry);
351 spin_unlock(&dentry->d_lock);
352 spin_unlock(&dcache_lock);
356 spin_unlock(&dentry->d_lock);
358 spin_unlock(&dcache_lock);
362 * Throw away a dentry - free the inode, dput the parent. This requires that
363 * the LRU list has already been removed.
365 * Called with dcache_lock, drops it and then regains.
366 * Called with dentry->d_lock held, drops it.
368 static void prune_one_dentry(struct dentry * dentry)
370 struct dentry * parent;
373 list_del(&dentry->d_u.d_child);
374 dentry_stat.nr_dentry--; /* For d_free, below */
376 parent = dentry->d_parent;
378 if (parent != dentry)
380 spin_lock(&dcache_lock);
384 * prune_dcache - shrink the dcache
385 * @count: number of entries to try and free
386 * @sb: if given, ignore dentries for other superblocks
387 * which are being unmounted.
389 * Shrink the dcache. This is done when we need
390 * more memory, or simply when we need to unmount
391 * something (at which point we need to unuse
394 * This function may fail to free any resources if
395 * all the dentries are in use.
398 static void prune_dcache(int count, struct super_block *sb)
400 spin_lock(&dcache_lock);
401 for (; count ; count--) {
402 struct dentry *dentry;
403 struct list_head *tmp;
404 struct rw_semaphore *s_umount;
406 cond_resched_lock(&dcache_lock);
408 tmp = dentry_unused.prev;
410 /* Try to find a dentry for this sb, but don't try
411 * too hard, if they aren't near the tail they will
412 * be moved down again soon
415 while (skip && tmp != &dentry_unused &&
416 list_entry(tmp, struct dentry, d_lru)->d_sb != sb) {
421 if (tmp == &dentry_unused)
424 prefetch(dentry_unused.prev);
425 dentry_stat.nr_unused--;
426 dentry = list_entry(tmp, struct dentry, d_lru);
428 spin_lock(&dentry->d_lock);
430 * We found an inuse dentry which was not removed from
431 * dentry_unused because of laziness during lookup. Do not free
432 * it - just keep it off the dentry_unused list.
434 if (atomic_read(&dentry->d_count)) {
435 spin_unlock(&dentry->d_lock);
438 /* If the dentry was recently referenced, don't free it. */
439 if (dentry->d_flags & DCACHE_REFERENCED) {
440 dentry->d_flags &= ~DCACHE_REFERENCED;
441 list_add(&dentry->d_lru, &dentry_unused);
442 dentry_stat.nr_unused++;
443 spin_unlock(&dentry->d_lock);
447 * If the dentry is not DCACHED_REFERENCED, it is time
448 * to remove it from the dcache, provided the super block is
449 * NULL (which means we are trying to reclaim memory)
450 * or this dentry belongs to the same super block that
454 * If this dentry is for "my" filesystem, then I can prune it
455 * without taking the s_umount lock (I already hold it).
457 if (sb && dentry->d_sb == sb) {
458 prune_one_dentry(dentry);
462 * ...otherwise we need to be sure this filesystem isn't being
463 * unmounted, otherwise we could race with
464 * generic_shutdown_super(), and end up holding a reference to
465 * an inode while the filesystem is unmounted.
466 * So we try to get s_umount, and make sure s_root isn't NULL.
467 * (Take a local copy of s_umount to avoid a use-after-free of
470 s_umount = &dentry->d_sb->s_umount;
471 if (down_read_trylock(s_umount)) {
472 if (dentry->d_sb->s_root != NULL) {
473 prune_one_dentry(dentry);
479 spin_unlock(&dentry->d_lock);
480 /* Cannot remove the first dentry, and it isn't appropriate
481 * to move it to the head of the list, so give up, and try
486 spin_unlock(&dcache_lock);
490 * Shrink the dcache for the specified super block.
491 * This allows us to unmount a device without disturbing
492 * the dcache for the other devices.
494 * This implementation makes just two traversals of the
495 * unused list. On the first pass we move the selected
496 * dentries to the most recent end, and on the second
497 * pass we free them. The second pass must restart after
498 * each dput(), but since the target dentries are all at
499 * the end, it's really just a single traversal.
503 * shrink_dcache_sb - shrink dcache for a superblock
506 * Shrink the dcache for the specified super block. This
507 * is used to free the dcache before unmounting a file
511 void shrink_dcache_sb(struct super_block * sb)
513 struct list_head *tmp, *next;
514 struct dentry *dentry;
517 * Pass one ... move the dentries for the specified
518 * superblock to the most recent end of the unused list.
520 spin_lock(&dcache_lock);
521 list_for_each_safe(tmp, next, &dentry_unused) {
522 dentry = list_entry(tmp, struct dentry, d_lru);
523 if (dentry->d_sb != sb)
526 list_add(tmp, &dentry_unused);
530 * Pass two ... free the dentries for this superblock.
533 list_for_each_safe(tmp, next, &dentry_unused) {
534 dentry = list_entry(tmp, struct dentry, d_lru);
535 if (dentry->d_sb != sb)
537 dentry_stat.nr_unused--;
539 spin_lock(&dentry->d_lock);
540 if (atomic_read(&dentry->d_count)) {
541 spin_unlock(&dentry->d_lock);
544 prune_one_dentry(dentry);
545 cond_resched_lock(&dcache_lock);
548 spin_unlock(&dcache_lock);
552 * Search for at least 1 mount point in the dentry's subdirs.
553 * We descend to the next level whenever the d_subdirs
554 * list is non-empty and continue searching.
558 * have_submounts - check for mounts over a dentry
559 * @parent: dentry to check.
561 * Return true if the parent or its subdirectories contain
565 int have_submounts(struct dentry *parent)
567 struct dentry *this_parent = parent;
568 struct list_head *next;
570 spin_lock(&dcache_lock);
571 if (d_mountpoint(parent))
574 next = this_parent->d_subdirs.next;
576 while (next != &this_parent->d_subdirs) {
577 struct list_head *tmp = next;
578 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
580 /* Have we found a mount point ? */
581 if (d_mountpoint(dentry))
583 if (!list_empty(&dentry->d_subdirs)) {
584 this_parent = dentry;
589 * All done at this level ... ascend and resume the search.
591 if (this_parent != parent) {
592 next = this_parent->d_u.d_child.next;
593 this_parent = this_parent->d_parent;
596 spin_unlock(&dcache_lock);
597 return 0; /* No mount points found in tree */
599 spin_unlock(&dcache_lock);
604 * Search the dentry child list for the specified parent,
605 * and move any unused dentries to the end of the unused
606 * list for prune_dcache(). We descend to the next level
607 * whenever the d_subdirs list is non-empty and continue
610 * It returns zero iff there are no unused children,
611 * otherwise it returns the number of children moved to
612 * the end of the unused list. This may not be the total
613 * number of unused children, because select_parent can
614 * drop the lock and return early due to latency
617 static int select_parent(struct dentry * parent)
619 struct dentry *this_parent = parent;
620 struct list_head *next;
623 spin_lock(&dcache_lock);
625 next = this_parent->d_subdirs.next;
627 while (next != &this_parent->d_subdirs) {
628 struct list_head *tmp = next;
629 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
632 if (!list_empty(&dentry->d_lru)) {
633 dentry_stat.nr_unused--;
634 list_del_init(&dentry->d_lru);
637 * move only zero ref count dentries to the end
638 * of the unused list for prune_dcache
640 if (!atomic_read(&dentry->d_count)) {
641 list_add(&dentry->d_lru, dentry_unused.prev);
642 dentry_stat.nr_unused++;
647 * We can return to the caller if we have found some (this
648 * ensures forward progress). We'll be coming back to find
651 if (found && need_resched())
655 * Descend a level if the d_subdirs list is non-empty.
657 if (!list_empty(&dentry->d_subdirs)) {
658 this_parent = dentry;
663 * All done at this level ... ascend and resume the search.
665 if (this_parent != parent) {
666 next = this_parent->d_u.d_child.next;
667 this_parent = this_parent->d_parent;
671 spin_unlock(&dcache_lock);
676 * shrink_dcache_parent - prune dcache
677 * @parent: parent of entries to prune
679 * Prune the dcache to remove unused children of the parent dentry.
682 void shrink_dcache_parent(struct dentry * parent)
686 while ((found = select_parent(parent)) != 0)
687 prune_dcache(found, parent->d_sb);
691 * Scan `nr' dentries and return the number which remain.
693 * We need to avoid reentering the filesystem if the caller is performing a
694 * GFP_NOFS allocation attempt. One example deadlock is:
696 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
697 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
698 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
700 * In this case we return -1 to tell the caller that we baled.
702 static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
705 if (!(gfp_mask & __GFP_FS))
707 prune_dcache(nr, NULL);
709 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
713 * d_alloc - allocate a dcache entry
714 * @parent: parent of entry to allocate
715 * @name: qstr of the name
717 * Allocates a dentry. It returns %NULL if there is insufficient memory
718 * available. On a success the dentry is returned. The name passed in is
719 * copied and the copy passed in may be reused after this call.
722 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
724 struct dentry *dentry;
727 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
731 if (name->len > DNAME_INLINE_LEN-1) {
732 dname = kmalloc(name->len + 1, GFP_KERNEL);
734 kmem_cache_free(dentry_cache, dentry);
738 dname = dentry->d_iname;
740 dentry->d_name.name = dname;
742 dentry->d_name.len = name->len;
743 dentry->d_name.hash = name->hash;
744 memcpy(dname, name->name, name->len);
745 dname[name->len] = 0;
747 atomic_set(&dentry->d_count, 1);
748 dentry->d_flags = DCACHE_UNHASHED;
749 spin_lock_init(&dentry->d_lock);
750 dentry->d_inode = NULL;
751 dentry->d_parent = NULL;
754 dentry->d_fsdata = NULL;
755 dentry->d_mounted = 0;
756 #ifdef CONFIG_PROFILING
757 dentry->d_cookie = NULL;
759 INIT_HLIST_NODE(&dentry->d_hash);
760 INIT_LIST_HEAD(&dentry->d_lru);
761 INIT_LIST_HEAD(&dentry->d_subdirs);
762 INIT_LIST_HEAD(&dentry->d_alias);
765 dentry->d_parent = dget(parent);
766 dentry->d_sb = parent->d_sb;
768 INIT_LIST_HEAD(&dentry->d_u.d_child);
771 spin_lock(&dcache_lock);
773 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
774 dentry_stat.nr_dentry++;
775 spin_unlock(&dcache_lock);
780 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
785 q.len = strlen(name);
786 q.hash = full_name_hash(q.name, q.len);
787 return d_alloc(parent, &q);
791 * d_instantiate - fill in inode information for a dentry
792 * @entry: dentry to complete
793 * @inode: inode to attach to this dentry
795 * Fill in inode information in the entry.
797 * This turns negative dentries into productive full members
800 * NOTE! This assumes that the inode count has been incremented
801 * (or otherwise set) by the caller to indicate that it is now
802 * in use by the dcache.
805 void d_instantiate(struct dentry *entry, struct inode * inode)
807 BUG_ON(!list_empty(&entry->d_alias));
808 spin_lock(&dcache_lock);
810 list_add(&entry->d_alias, &inode->i_dentry);
811 entry->d_inode = inode;
812 fsnotify_d_instantiate(entry, inode);
813 spin_unlock(&dcache_lock);
814 security_d_instantiate(entry, inode);
818 * d_instantiate_unique - instantiate a non-aliased dentry
819 * @entry: dentry to instantiate
820 * @inode: inode to attach to this dentry
822 * Fill in inode information in the entry. On success, it returns NULL.
823 * If an unhashed alias of "entry" already exists, then we return the
824 * aliased dentry instead and drop one reference to inode.
826 * Note that in order to avoid conflicts with rename() etc, the caller
827 * had better be holding the parent directory semaphore.
829 * This also assumes that the inode count has been incremented
830 * (or otherwise set) by the caller to indicate that it is now
831 * in use by the dcache.
833 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
835 struct dentry *alias;
836 int len = entry->d_name.len;
837 const char *name = entry->d_name.name;
838 unsigned int hash = entry->d_name.hash;
840 BUG_ON(!list_empty(&entry->d_alias));
841 spin_lock(&dcache_lock);
844 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
845 struct qstr *qstr = &alias->d_name;
847 if (qstr->hash != hash)
849 if (alias->d_parent != entry->d_parent)
851 if (qstr->len != len)
853 if (memcmp(qstr->name, name, len))
856 spin_unlock(&dcache_lock);
857 BUG_ON(!d_unhashed(alias));
861 list_add(&entry->d_alias, &inode->i_dentry);
863 entry->d_inode = inode;
864 fsnotify_d_instantiate(entry, inode);
865 spin_unlock(&dcache_lock);
866 security_d_instantiate(entry, inode);
869 EXPORT_SYMBOL(d_instantiate_unique);
872 * d_alloc_root - allocate root dentry
873 * @root_inode: inode to allocate the root for
875 * Allocate a root ("/") dentry for the inode given. The inode is
876 * instantiated and returned. %NULL is returned if there is insufficient
877 * memory or the inode passed is %NULL.
880 struct dentry * d_alloc_root(struct inode * root_inode)
882 struct dentry *res = NULL;
885 static const struct qstr name = { .name = "/", .len = 1 };
887 res = d_alloc(NULL, &name);
889 res->d_sb = root_inode->i_sb;
891 d_instantiate(res, root_inode);
897 static inline struct hlist_head *d_hash(struct dentry *parent,
900 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
901 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
902 return dentry_hashtable + (hash & D_HASHMASK);
906 * d_alloc_anon - allocate an anonymous dentry
907 * @inode: inode to allocate the dentry for
909 * This is similar to d_alloc_root. It is used by filesystems when
910 * creating a dentry for a given inode, often in the process of
911 * mapping a filehandle to a dentry. The returned dentry may be
912 * anonymous, or may have a full name (if the inode was already
913 * in the cache). The file system may need to make further
914 * efforts to connect this dentry into the dcache properly.
916 * When called on a directory inode, we must ensure that
917 * the inode only ever has one dentry. If a dentry is
918 * found, that is returned instead of allocating a new one.
920 * On successful return, the reference to the inode has been transferred
921 * to the dentry. If %NULL is returned (indicating kmalloc failure),
922 * the reference on the inode has not been released.
925 struct dentry * d_alloc_anon(struct inode *inode)
927 static const struct qstr anonstring = { .name = "" };
931 if ((res = d_find_alias(inode))) {
936 tmp = d_alloc(NULL, &anonstring);
940 tmp->d_parent = tmp; /* make sure dput doesn't croak */
942 spin_lock(&dcache_lock);
943 res = __d_find_alias(inode, 0);
945 /* attach a disconnected dentry */
948 spin_lock(&res->d_lock);
949 res->d_sb = inode->i_sb;
951 res->d_inode = inode;
952 res->d_flags |= DCACHE_DISCONNECTED;
953 res->d_flags &= ~DCACHE_UNHASHED;
954 list_add(&res->d_alias, &inode->i_dentry);
955 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
956 spin_unlock(&res->d_lock);
958 inode = NULL; /* don't drop reference */
960 spin_unlock(&dcache_lock);
971 * d_splice_alias - splice a disconnected dentry into the tree if one exists
972 * @inode: the inode which may have a disconnected dentry
973 * @dentry: a negative dentry which we want to point to the inode.
975 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
976 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
977 * and return it, else simply d_add the inode to the dentry and return NULL.
979 * This is needed in the lookup routine of any filesystem that is exportable
980 * (via knfsd) so that we can build dcache paths to directories effectively.
982 * If a dentry was found and moved, then it is returned. Otherwise NULL
983 * is returned. This matches the expected return value of ->lookup.
986 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
988 struct dentry *new = NULL;
991 spin_lock(&dcache_lock);
992 new = __d_find_alias(inode, 1);
994 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
995 fsnotify_d_instantiate(new, inode);
996 spin_unlock(&dcache_lock);
997 security_d_instantiate(new, inode);
1002 /* d_instantiate takes dcache_lock, so we do it by hand */
1003 list_add(&dentry->d_alias, &inode->i_dentry);
1004 dentry->d_inode = inode;
1005 fsnotify_d_instantiate(dentry, inode);
1006 spin_unlock(&dcache_lock);
1007 security_d_instantiate(dentry, inode);
1011 d_add(dentry, inode);
1017 * d_lookup - search for a dentry
1018 * @parent: parent dentry
1019 * @name: qstr of name we wish to find
1021 * Searches the children of the parent dentry for the name in question. If
1022 * the dentry is found its reference count is incremented and the dentry
1023 * is returned. The caller must use d_put to free the entry when it has
1024 * finished using it. %NULL is returned on failure.
1026 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1027 * Memory barriers are used while updating and doing lockless traversal.
1028 * To avoid races with d_move while rename is happening, d_lock is used.
1030 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1031 * and name pointer in one structure pointed by d_qstr.
1033 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1034 * lookup is going on.
1036 * dentry_unused list is not updated even if lookup finds the required dentry
1037 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1038 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1041 * d_lookup() is protected against the concurrent renames in some unrelated
1042 * directory using the seqlockt_t rename_lock.
1045 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1047 struct dentry * dentry = NULL;
1051 seq = read_seqbegin(&rename_lock);
1052 dentry = __d_lookup(parent, name);
1055 } while (read_seqretry(&rename_lock, seq));
1059 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1061 unsigned int len = name->len;
1062 unsigned int hash = name->hash;
1063 const unsigned char *str = name->name;
1064 struct hlist_head *head = d_hash(parent,hash);
1065 struct dentry *found = NULL;
1066 struct hlist_node *node;
1067 struct dentry *dentry;
1071 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1074 if (dentry->d_name.hash != hash)
1076 if (dentry->d_parent != parent)
1079 spin_lock(&dentry->d_lock);
1082 * Recheck the dentry after taking the lock - d_move may have
1083 * changed things. Don't bother checking the hash because we're
1084 * about to compare the whole name anyway.
1086 if (dentry->d_parent != parent)
1090 * It is safe to compare names since d_move() cannot
1091 * change the qstr (protected by d_lock).
1093 qstr = &dentry->d_name;
1094 if (parent->d_op && parent->d_op->d_compare) {
1095 if (parent->d_op->d_compare(parent, qstr, name))
1098 if (qstr->len != len)
1100 if (memcmp(qstr->name, str, len))
1104 if (!d_unhashed(dentry)) {
1105 atomic_inc(&dentry->d_count);
1108 spin_unlock(&dentry->d_lock);
1111 spin_unlock(&dentry->d_lock);
1119 * d_hash_and_lookup - hash the qstr then search for a dentry
1120 * @dir: Directory to search in
1121 * @name: qstr of name we wish to find
1123 * On hash failure or on lookup failure NULL is returned.
1125 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1127 struct dentry *dentry = NULL;
1130 * Check for a fs-specific hash function. Note that we must
1131 * calculate the standard hash first, as the d_op->d_hash()
1132 * routine may choose to leave the hash value unchanged.
1134 name->hash = full_name_hash(name->name, name->len);
1135 if (dir->d_op && dir->d_op->d_hash) {
1136 if (dir->d_op->d_hash(dir, name) < 0)
1139 dentry = d_lookup(dir, name);
1145 * d_validate - verify dentry provided from insecure source
1146 * @dentry: The dentry alleged to be valid child of @dparent
1147 * @dparent: The parent dentry (known to be valid)
1148 * @hash: Hash of the dentry
1149 * @len: Length of the name
1151 * An insecure source has sent us a dentry, here we verify it and dget() it.
1152 * This is used by ncpfs in its readdir implementation.
1153 * Zero is returned in the dentry is invalid.
1156 int d_validate(struct dentry *dentry, struct dentry *dparent)
1158 struct hlist_head *base;
1159 struct hlist_node *lhp;
1161 /* Check whether the ptr might be valid at all.. */
1162 if (!kmem_ptr_validate(dentry_cache, dentry))
1165 if (dentry->d_parent != dparent)
1168 spin_lock(&dcache_lock);
1169 base = d_hash(dparent, dentry->d_name.hash);
1170 hlist_for_each(lhp,base) {
1171 /* hlist_for_each_entry_rcu() not required for d_hash list
1172 * as it is parsed under dcache_lock
1174 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1175 __dget_locked(dentry);
1176 spin_unlock(&dcache_lock);
1180 spin_unlock(&dcache_lock);
1186 * When a file is deleted, we have two options:
1187 * - turn this dentry into a negative dentry
1188 * - unhash this dentry and free it.
1190 * Usually, we want to just turn this into
1191 * a negative dentry, but if anybody else is
1192 * currently using the dentry or the inode
1193 * we can't do that and we fall back on removing
1194 * it from the hash queues and waiting for
1195 * it to be deleted later when it has no users
1199 * d_delete - delete a dentry
1200 * @dentry: The dentry to delete
1202 * Turn the dentry into a negative dentry if possible, otherwise
1203 * remove it from the hash queues so it can be deleted later
1206 void d_delete(struct dentry * dentry)
1210 * Are we the only user?
1212 spin_lock(&dcache_lock);
1213 spin_lock(&dentry->d_lock);
1214 isdir = S_ISDIR(dentry->d_inode->i_mode);
1215 if (atomic_read(&dentry->d_count) == 1) {
1216 dentry_iput(dentry);
1217 fsnotify_nameremove(dentry, isdir);
1219 /* remove this and other inotify debug checks after 2.6.18 */
1220 dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
1224 if (!d_unhashed(dentry))
1227 spin_unlock(&dentry->d_lock);
1228 spin_unlock(&dcache_lock);
1230 fsnotify_nameremove(dentry, isdir);
1233 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1236 entry->d_flags &= ~DCACHE_UNHASHED;
1237 hlist_add_head_rcu(&entry->d_hash, list);
1241 * d_rehash - add an entry back to the hash
1242 * @entry: dentry to add to the hash
1244 * Adds a dentry to the hash according to its name.
1247 void d_rehash(struct dentry * entry)
1249 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1251 spin_lock(&dcache_lock);
1252 spin_lock(&entry->d_lock);
1253 __d_rehash(entry, list);
1254 spin_unlock(&entry->d_lock);
1255 spin_unlock(&dcache_lock);
1258 #define do_switch(x,y) do { \
1259 __typeof__ (x) __tmp = x; \
1260 x = y; y = __tmp; } while (0)
1263 * When switching names, the actual string doesn't strictly have to
1264 * be preserved in the target - because we're dropping the target
1265 * anyway. As such, we can just do a simple memcpy() to copy over
1266 * the new name before we switch.
1268 * Note that we have to be a lot more careful about getting the hash
1269 * switched - we have to switch the hash value properly even if it
1270 * then no longer matches the actual (corrupted) string of the target.
1271 * The hash value has to match the hash queue that the dentry is on..
1273 static void switch_names(struct dentry *dentry, struct dentry *target)
1275 if (dname_external(target)) {
1276 if (dname_external(dentry)) {
1278 * Both external: swap the pointers
1280 do_switch(target->d_name.name, dentry->d_name.name);
1283 * dentry:internal, target:external. Steal target's
1284 * storage and make target internal.
1286 dentry->d_name.name = target->d_name.name;
1287 target->d_name.name = target->d_iname;
1290 if (dname_external(dentry)) {
1292 * dentry:external, target:internal. Give dentry's
1293 * storage to target and make dentry internal
1295 memcpy(dentry->d_iname, target->d_name.name,
1296 target->d_name.len + 1);
1297 target->d_name.name = dentry->d_name.name;
1298 dentry->d_name.name = dentry->d_iname;
1301 * Both are internal. Just copy target to dentry
1303 memcpy(dentry->d_iname, target->d_name.name,
1304 target->d_name.len + 1);
1310 * We cannibalize "target" when moving dentry on top of it,
1311 * because it's going to be thrown away anyway. We could be more
1312 * polite about it, though.
1314 * This forceful removal will result in ugly /proc output if
1315 * somebody holds a file open that got deleted due to a rename.
1316 * We could be nicer about the deleted file, and let it show
1317 * up under the name it got deleted rather than the name that
1322 * d_move - move a dentry
1323 * @dentry: entry to move
1324 * @target: new dentry
1326 * Update the dcache to reflect the move of a file name. Negative
1327 * dcache entries should not be moved in this way.
1330 void d_move(struct dentry * dentry, struct dentry * target)
1332 struct hlist_head *list;
1334 if (!dentry->d_inode)
1335 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1337 spin_lock(&dcache_lock);
1338 write_seqlock(&rename_lock);
1340 * XXXX: do we really need to take target->d_lock?
1342 if (target < dentry) {
1343 spin_lock(&target->d_lock);
1344 spin_lock(&dentry->d_lock);
1346 spin_lock(&dentry->d_lock);
1347 spin_lock(&target->d_lock);
1350 /* Move the dentry to the target hash queue, if on different bucket */
1351 if (dentry->d_flags & DCACHE_UNHASHED)
1352 goto already_unhashed;
1354 hlist_del_rcu(&dentry->d_hash);
1357 list = d_hash(target->d_parent, target->d_name.hash);
1358 __d_rehash(dentry, list);
1360 /* Unhash the target: dput() will then get rid of it */
1363 list_del(&dentry->d_u.d_child);
1364 list_del(&target->d_u.d_child);
1366 /* Switch the names.. */
1367 switch_names(dentry, target);
1368 do_switch(dentry->d_name.len, target->d_name.len);
1369 do_switch(dentry->d_name.hash, target->d_name.hash);
1371 /* ... and switch the parents */
1372 if (IS_ROOT(dentry)) {
1373 dentry->d_parent = target->d_parent;
1374 target->d_parent = target;
1375 INIT_LIST_HEAD(&target->d_u.d_child);
1377 do_switch(dentry->d_parent, target->d_parent);
1379 /* And add them back to the (new) parent lists */
1380 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1383 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1384 spin_unlock(&target->d_lock);
1385 fsnotify_d_move(dentry);
1386 spin_unlock(&dentry->d_lock);
1387 write_sequnlock(&rename_lock);
1388 spin_unlock(&dcache_lock);
1392 * d_path - return the path of a dentry
1393 * @dentry: dentry to report
1394 * @vfsmnt: vfsmnt to which the dentry belongs
1395 * @root: root dentry
1396 * @rootmnt: vfsmnt to which the root dentry belongs
1397 * @buffer: buffer to return value in
1398 * @buflen: buffer length
1400 * Convert a dentry into an ASCII path name. If the entry has been deleted
1401 * the string " (deleted)" is appended. Note that this is ambiguous.
1403 * Returns the buffer or an error code if the path was too long.
1405 * "buflen" should be positive. Caller holds the dcache_lock.
1407 static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1408 struct dentry *root, struct vfsmount *rootmnt,
1409 char *buffer, int buflen)
1411 char * end = buffer+buflen;
1417 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1422 memcpy(end, " (deleted)", 10);
1432 struct dentry * parent;
1434 if (dentry == root && vfsmnt == rootmnt)
1436 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1438 spin_lock(&vfsmount_lock);
1439 if (vfsmnt->mnt_parent == vfsmnt) {
1440 spin_unlock(&vfsmount_lock);
1443 dentry = vfsmnt->mnt_mountpoint;
1444 vfsmnt = vfsmnt->mnt_parent;
1445 spin_unlock(&vfsmount_lock);
1448 parent = dentry->d_parent;
1450 namelen = dentry->d_name.len;
1451 buflen -= namelen + 1;
1455 memcpy(end, dentry->d_name.name, namelen);
1464 namelen = dentry->d_name.len;
1468 retval -= namelen-1; /* hit the slash */
1469 memcpy(retval, dentry->d_name.name, namelen);
1472 return ERR_PTR(-ENAMETOOLONG);
1475 /* write full pathname into buffer and return start of pathname */
1476 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1477 char *buf, int buflen)
1480 struct vfsmount *rootmnt;
1481 struct dentry *root;
1483 read_lock(¤t->fs->lock);
1484 rootmnt = mntget(current->fs->rootmnt);
1485 root = dget(current->fs->root);
1486 read_unlock(¤t->fs->lock);
1487 spin_lock(&dcache_lock);
1488 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1489 spin_unlock(&dcache_lock);
1496 * NOTE! The user-level library version returns a
1497 * character pointer. The kernel system call just
1498 * returns the length of the buffer filled (which
1499 * includes the ending '\0' character), or a negative
1500 * error value. So libc would do something like
1502 * char *getcwd(char * buf, size_t size)
1506 * retval = sys_getcwd(buf, size);
1513 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1516 struct vfsmount *pwdmnt, *rootmnt;
1517 struct dentry *pwd, *root;
1518 char *page = (char *) __get_free_page(GFP_USER);
1523 read_lock(¤t->fs->lock);
1524 pwdmnt = mntget(current->fs->pwdmnt);
1525 pwd = dget(current->fs->pwd);
1526 rootmnt = mntget(current->fs->rootmnt);
1527 root = dget(current->fs->root);
1528 read_unlock(¤t->fs->lock);
1531 /* Has the current directory has been unlinked? */
1532 spin_lock(&dcache_lock);
1533 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1537 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1538 spin_unlock(&dcache_lock);
1540 error = PTR_ERR(cwd);
1545 len = PAGE_SIZE + page - cwd;
1548 if (copy_to_user(buf, cwd, len))
1552 spin_unlock(&dcache_lock);
1559 free_page((unsigned long) page);
1564 * Test whether new_dentry is a subdirectory of old_dentry.
1566 * Trivially implemented using the dcache structure
1570 * is_subdir - is new dentry a subdirectory of old_dentry
1571 * @new_dentry: new dentry
1572 * @old_dentry: old dentry
1574 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1575 * Returns 0 otherwise.
1576 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1579 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1582 struct dentry * saved = new_dentry;
1585 /* need rcu_readlock to protect against the d_parent trashing due to
1590 /* for restarting inner loop in case of seq retry */
1593 seq = read_seqbegin(&rename_lock);
1595 if (new_dentry != old_dentry) {
1596 struct dentry * parent = new_dentry->d_parent;
1597 if (parent == new_dentry)
1599 new_dentry = parent;
1605 } while (read_seqretry(&rename_lock, seq));
1611 void d_genocide(struct dentry *root)
1613 struct dentry *this_parent = root;
1614 struct list_head *next;
1616 spin_lock(&dcache_lock);
1618 next = this_parent->d_subdirs.next;
1620 while (next != &this_parent->d_subdirs) {
1621 struct list_head *tmp = next;
1622 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1624 if (d_unhashed(dentry)||!dentry->d_inode)
1626 if (!list_empty(&dentry->d_subdirs)) {
1627 this_parent = dentry;
1630 atomic_dec(&dentry->d_count);
1632 if (this_parent != root) {
1633 next = this_parent->d_u.d_child.next;
1634 atomic_dec(&this_parent->d_count);
1635 this_parent = this_parent->d_parent;
1638 spin_unlock(&dcache_lock);
1642 * find_inode_number - check for dentry with name
1643 * @dir: directory to check
1644 * @name: Name to find.
1646 * Check whether a dentry already exists for the given name,
1647 * and return the inode number if it has an inode. Otherwise
1650 * This routine is used to post-process directory listings for
1651 * filesystems using synthetic inode numbers, and is necessary
1652 * to keep getcwd() working.
1655 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1657 struct dentry * dentry;
1660 dentry = d_hash_and_lookup(dir, name);
1662 if (dentry->d_inode)
1663 ino = dentry->d_inode->i_ino;
1669 static __initdata unsigned long dhash_entries;
1670 static int __init set_dhash_entries(char *str)
1674 dhash_entries = simple_strtoul(str, &str, 0);
1677 __setup("dhash_entries=", set_dhash_entries);
1679 static void __init dcache_init_early(void)
1683 /* If hashes are distributed across NUMA nodes, defer
1684 * hash allocation until vmalloc space is available.
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 static void __init dcache_init(unsigned long mempages)
1708 * A constructor could be added for stable state like the lists,
1709 * but it is probably not worth it because of the cache nature
1712 dentry_cache = kmem_cache_create("dentry_cache",
1713 sizeof(struct dentry),
1715 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1719 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1721 /* Hash may have been set up in dcache_init_early */
1726 alloc_large_system_hash("Dentry cache",
1727 sizeof(struct hlist_head),
1735 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1736 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1739 /* SLAB cache for __getname() consumers */
1740 kmem_cache_t *names_cachep __read_mostly;
1742 /* SLAB cache for file structures */
1743 kmem_cache_t *filp_cachep __read_mostly;
1745 EXPORT_SYMBOL(d_genocide);
1747 extern void bdev_cache_init(void);
1748 extern void chrdev_init(void);
1750 void __init vfs_caches_init_early(void)
1752 dcache_init_early();
1756 void __init vfs_caches_init(unsigned long mempages)
1758 unsigned long reserve;
1760 /* Base hash sizes on available memory, with a reserve equal to
1761 150% of current kernel size */
1763 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1764 mempages -= reserve;
1766 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1767 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1769 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
1770 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1772 dcache_init(mempages);
1773 inode_init(mempages);
1774 files_init(mempages);
1780 EXPORT_SYMBOL(d_alloc);
1781 EXPORT_SYMBOL(d_alloc_anon);
1782 EXPORT_SYMBOL(d_alloc_root);
1783 EXPORT_SYMBOL(d_delete);
1784 EXPORT_SYMBOL(d_find_alias);
1785 EXPORT_SYMBOL(d_instantiate);
1786 EXPORT_SYMBOL(d_invalidate);
1787 EXPORT_SYMBOL(d_lookup);
1788 EXPORT_SYMBOL(d_move);
1789 EXPORT_SYMBOL(d_path);
1790 EXPORT_SYMBOL(d_prune_aliases);
1791 EXPORT_SYMBOL(d_rehash);
1792 EXPORT_SYMBOL(d_splice_alias);
1793 EXPORT_SYMBOL(d_validate);
1794 EXPORT_SYMBOL(dget_locked);
1795 EXPORT_SYMBOL(dput);
1796 EXPORT_SYMBOL(find_inode_number);
1797 EXPORT_SYMBOL(have_submounts);
1798 EXPORT_SYMBOL(names_cachep);
1799 EXPORT_SYMBOL(shrink_dcache_parent);
1800 EXPORT_SYMBOL(shrink_dcache_sb);