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 if (dentry->d_extra_attributes) {
89 kfree(dentry->d_extra_attributes);
90 dentry->d_extra_attributes = NULL;
92 call_rcu(&dentry->d_u.d_rcu, d_callback);
96 * Release the dentry's inode, using the filesystem
97 * d_iput() operation if defined.
98 * Called with dcache_lock and per dentry lock held, drops both.
100 static void dentry_iput(struct dentry * dentry)
102 struct inode *inode = dentry->d_inode;
104 dentry->d_inode = NULL;
105 list_del_init(&dentry->d_alias);
106 spin_unlock(&dentry->d_lock);
107 spin_unlock(&dcache_lock);
109 fsnotify_inoderemove(inode);
110 if (dentry->d_op && dentry->d_op->d_iput)
111 dentry->d_op->d_iput(dentry, inode);
115 spin_unlock(&dentry->d_lock);
116 spin_unlock(&dcache_lock);
123 * This is complicated by the fact that we do not want to put
124 * dentries that are no longer on any hash chain on the unused
125 * list: we'd much rather just get rid of them immediately.
127 * However, that implies that we have to traverse the dentry
128 * tree upwards to the parents which might _also_ now be
129 * scheduled for deletion (it may have been only waiting for
130 * its last child to go away).
132 * This tail recursion is done by hand as we don't want to depend
133 * on the compiler to always get this right (gcc generally doesn't).
134 * Real recursion would eat up our stack space.
138 * dput - release a dentry
139 * @dentry: dentry to release
141 * Release a dentry. This will drop the usage count and if appropriate
142 * call the dentry unlink method as well as removing it from the queues and
143 * releasing its resources. If the parent dentries were scheduled for release
144 * they too may now get deleted.
146 * no dcache lock, please.
149 void dput(struct dentry *dentry)
155 if (atomic_read(&dentry->d_count) == 1)
157 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
160 spin_lock(&dentry->d_lock);
161 if (atomic_read(&dentry->d_count)) {
162 spin_unlock(&dentry->d_lock);
163 spin_unlock(&dcache_lock);
168 * AV: ->d_delete() is _NOT_ allowed to block now.
170 if (dentry->d_op && dentry->d_op->d_delete) {
171 if (dentry->d_op->d_delete(dentry))
174 /* Unreachable? Get rid of it */
175 if (d_unhashed(dentry))
177 if (list_empty(&dentry->d_lru)) {
178 dentry->d_flags |= DCACHE_REFERENCED;
179 list_add(&dentry->d_lru, &dentry_unused);
180 dentry_stat.nr_unused++;
182 spin_unlock(&dentry->d_lock);
183 spin_unlock(&dcache_lock);
190 struct dentry *parent;
192 /* If dentry was on d_lru list
193 * delete it from there
195 if (!list_empty(&dentry->d_lru)) {
196 list_del(&dentry->d_lru);
197 dentry_stat.nr_unused--;
199 list_del(&dentry->d_u.d_child);
200 dentry_stat.nr_dentry--; /* For d_free, below */
201 /*drops the locks, at that point nobody can reach this dentry */
203 parent = dentry->d_parent;
205 if (dentry == parent)
213 * d_invalidate - invalidate a dentry
214 * @dentry: dentry to invalidate
216 * Try to invalidate the dentry if it turns out to be
217 * possible. If there are other dentries that can be
218 * reached through this one we can't delete it and we
219 * return -EBUSY. On success we return 0.
224 int d_invalidate(struct dentry * dentry)
227 * If it's already been dropped, return OK.
229 spin_lock(&dcache_lock);
230 if (d_unhashed(dentry)) {
231 spin_unlock(&dcache_lock);
235 * Check whether to do a partial shrink_dcache
236 * to get rid of unused child entries.
238 if (!list_empty(&dentry->d_subdirs)) {
239 spin_unlock(&dcache_lock);
240 shrink_dcache_parent(dentry);
241 spin_lock(&dcache_lock);
245 * Somebody else still using it?
247 * If it's a directory, we can't drop it
248 * for fear of somebody re-populating it
249 * with children (even though dropping it
250 * would make it unreachable from the root,
251 * we might still populate it if it was a
252 * working directory or similar).
254 spin_lock(&dentry->d_lock);
255 if (atomic_read(&dentry->d_count) > 1) {
256 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
257 spin_unlock(&dentry->d_lock);
258 spin_unlock(&dcache_lock);
264 spin_unlock(&dentry->d_lock);
265 spin_unlock(&dcache_lock);
269 /* This should be called _only_ with dcache_lock held */
271 static inline struct dentry * __dget_locked(struct dentry *dentry)
273 atomic_inc(&dentry->d_count);
274 if (!list_empty(&dentry->d_lru)) {
275 dentry_stat.nr_unused--;
276 list_del_init(&dentry->d_lru);
281 struct dentry * dget_locked(struct dentry *dentry)
283 return __dget_locked(dentry);
287 * d_find_alias - grab a hashed alias of inode
288 * @inode: inode in question
289 * @want_discon: flag, used by d_splice_alias, to request
290 * that only a DISCONNECTED alias be returned.
292 * If inode has a hashed alias, or is a directory and has any alias,
293 * acquire the reference to alias and return it. Otherwise return NULL.
294 * Notice that if inode is a directory there can be only one alias and
295 * it can be unhashed only if it has no children, or if it is the root
298 * If the inode has a DCACHE_DISCONNECTED alias, then prefer
299 * any other hashed alias over that one unless @want_discon is set,
300 * in which case only return a DCACHE_DISCONNECTED alias.
303 static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
305 struct list_head *head, *next, *tmp;
306 struct dentry *alias, *discon_alias=NULL;
308 head = &inode->i_dentry;
309 next = inode->i_dentry.next;
310 while (next != head) {
314 alias = list_entry(tmp, struct dentry, d_alias);
315 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
316 if (alias->d_flags & DCACHE_DISCONNECTED)
317 discon_alias = alias;
318 else if (!want_discon) {
319 __dget_locked(alias);
325 __dget_locked(discon_alias);
329 struct dentry * d_find_alias(struct inode *inode)
331 struct dentry *de = NULL;
333 if (!list_empty(&inode->i_dentry)) {
334 spin_lock(&dcache_lock);
335 de = __d_find_alias(inode, 0);
336 spin_unlock(&dcache_lock);
342 * Try to kill dentries associated with this inode.
343 * WARNING: you must own a reference to inode.
345 void d_prune_aliases(struct inode *inode)
347 struct dentry *dentry;
349 spin_lock(&dcache_lock);
350 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
351 spin_lock(&dentry->d_lock);
352 if (!atomic_read(&dentry->d_count)) {
353 __dget_locked(dentry);
355 spin_unlock(&dentry->d_lock);
356 spin_unlock(&dcache_lock);
360 spin_unlock(&dentry->d_lock);
362 spin_unlock(&dcache_lock);
366 * Throw away a dentry - free the inode, dput the parent.
367 * This requires that the LRU list has already been
369 * Called with dcache_lock, drops it and then regains.
371 static inline void prune_one_dentry(struct dentry * dentry)
373 struct dentry * parent;
376 list_del(&dentry->d_u.d_child);
377 dentry_stat.nr_dentry--; /* For d_free, below */
379 parent = dentry->d_parent;
381 if (parent != dentry)
383 spin_lock(&dcache_lock);
387 * prune_dcache - shrink the dcache
388 * @count: number of entries to try and free
390 * Shrink the dcache. This is done when we need
391 * more memory, or simply when we need to unmount
392 * something (at which point we need to unuse
395 * This function may fail to free any resources if
396 * all the dentries are in use.
399 static void prune_dcache(int count)
401 spin_lock(&dcache_lock);
402 for (; count ; count--) {
403 struct dentry *dentry;
404 struct list_head *tmp;
406 cond_resched_lock(&dcache_lock);
408 tmp = dentry_unused.prev;
409 if (tmp == &dentry_unused)
412 prefetch(dentry_unused.prev);
413 dentry_stat.nr_unused--;
414 dentry = list_entry(tmp, struct dentry, d_lru);
416 spin_lock(&dentry->d_lock);
418 * We found an inuse dentry which was not removed from
419 * dentry_unused because of laziness during lookup. Do not free
420 * it - just keep it off the dentry_unused list.
422 if (atomic_read(&dentry->d_count)) {
423 spin_unlock(&dentry->d_lock);
426 /* If the dentry was recently referenced, don't free it. */
427 if (dentry->d_flags & DCACHE_REFERENCED) {
428 dentry->d_flags &= ~DCACHE_REFERENCED;
429 list_add(&dentry->d_lru, &dentry_unused);
430 dentry_stat.nr_unused++;
431 spin_unlock(&dentry->d_lock);
434 prune_one_dentry(dentry);
436 spin_unlock(&dcache_lock);
440 * Shrink the dcache for the specified super block.
441 * This allows us to unmount a device without disturbing
442 * the dcache for the other devices.
444 * This implementation makes just two traversals of the
445 * unused list. On the first pass we move the selected
446 * dentries to the most recent end, and on the second
447 * pass we free them. The second pass must restart after
448 * each dput(), but since the target dentries are all at
449 * the end, it's really just a single traversal.
453 * shrink_dcache_sb - shrink dcache for a superblock
456 * Shrink the dcache for the specified super block. This
457 * is used to free the dcache before unmounting a file
461 void shrink_dcache_sb(struct super_block * sb)
463 struct list_head *tmp, *next;
464 struct dentry *dentry;
467 * Pass one ... move the dentries for the specified
468 * superblock to the most recent end of the unused list.
470 spin_lock(&dcache_lock);
471 list_for_each_safe(tmp, next, &dentry_unused) {
472 dentry = list_entry(tmp, struct dentry, d_lru);
473 if (dentry->d_sb != sb)
476 list_add(tmp, &dentry_unused);
480 * Pass two ... free the dentries for this superblock.
483 list_for_each_safe(tmp, next, &dentry_unused) {
484 dentry = list_entry(tmp, struct dentry, d_lru);
485 if (dentry->d_sb != sb)
487 dentry_stat.nr_unused--;
489 spin_lock(&dentry->d_lock);
490 if (atomic_read(&dentry->d_count)) {
491 spin_unlock(&dentry->d_lock);
494 prune_one_dentry(dentry);
495 cond_resched_lock(&dcache_lock);
498 spin_unlock(&dcache_lock);
502 * Search for at least 1 mount point in the dentry's subdirs.
503 * We descend to the next level whenever the d_subdirs
504 * list is non-empty and continue searching.
508 * have_submounts - check for mounts over a dentry
509 * @parent: dentry to check.
511 * Return true if the parent or its subdirectories contain
515 int have_submounts(struct dentry *parent)
517 struct dentry *this_parent = parent;
518 struct list_head *next;
520 spin_lock(&dcache_lock);
521 if (d_mountpoint(parent))
524 next = this_parent->d_subdirs.next;
526 while (next != &this_parent->d_subdirs) {
527 struct list_head *tmp = next;
528 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
530 /* Have we found a mount point ? */
531 if (d_mountpoint(dentry))
533 if (!list_empty(&dentry->d_subdirs)) {
534 this_parent = dentry;
539 * All done at this level ... ascend and resume the search.
541 if (this_parent != parent) {
542 next = this_parent->d_u.d_child.next;
543 this_parent = this_parent->d_parent;
546 spin_unlock(&dcache_lock);
547 return 0; /* No mount points found in tree */
549 spin_unlock(&dcache_lock);
554 * Search the dentry child list for the specified parent,
555 * and move any unused dentries to the end of the unused
556 * list for prune_dcache(). We descend to the next level
557 * whenever the d_subdirs list is non-empty and continue
560 * It returns zero iff there are no unused children,
561 * otherwise it returns the number of children moved to
562 * the end of the unused list. This may not be the total
563 * number of unused children, because select_parent can
564 * drop the lock and return early due to latency
567 static int select_parent(struct dentry * parent)
569 struct dentry *this_parent = parent;
570 struct list_head *next;
573 spin_lock(&dcache_lock);
575 next = this_parent->d_subdirs.next;
577 while (next != &this_parent->d_subdirs) {
578 struct list_head *tmp = next;
579 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
582 if (!list_empty(&dentry->d_lru)) {
583 dentry_stat.nr_unused--;
584 list_del_init(&dentry->d_lru);
587 * move only zero ref count dentries to the end
588 * of the unused list for prune_dcache
590 if (!atomic_read(&dentry->d_count)) {
591 list_add(&dentry->d_lru, dentry_unused.prev);
592 dentry_stat.nr_unused++;
597 * We can return to the caller if we have found some (this
598 * ensures forward progress). We'll be coming back to find
601 if (found && need_resched())
605 * Descend a level if the d_subdirs list is non-empty.
607 if (!list_empty(&dentry->d_subdirs)) {
608 this_parent = dentry;
613 * All done at this level ... ascend and resume the search.
615 if (this_parent != parent) {
616 next = this_parent->d_u.d_child.next;
617 this_parent = this_parent->d_parent;
621 spin_unlock(&dcache_lock);
626 * shrink_dcache_parent - prune dcache
627 * @parent: parent of entries to prune
629 * Prune the dcache to remove unused children of the parent dentry.
632 void shrink_dcache_parent(struct dentry * parent)
636 while ((found = select_parent(parent)) != 0)
641 * shrink_dcache_anon - further prune the cache
642 * @head: head of d_hash list of dentries to prune
644 * Prune the dentries that are anonymous
646 * parsing d_hash list does not hlist_for_each_entry_rcu() as it
647 * done under dcache_lock.
650 void shrink_dcache_anon(struct hlist_head *head)
652 struct hlist_node *lp;
656 spin_lock(&dcache_lock);
657 hlist_for_each(lp, head) {
658 struct dentry *this = hlist_entry(lp, struct dentry, d_hash);
659 if (!list_empty(&this->d_lru)) {
660 dentry_stat.nr_unused--;
661 list_del_init(&this->d_lru);
665 * move only zero ref count dentries to the end
666 * of the unused list for prune_dcache
668 if (!atomic_read(&this->d_count)) {
669 list_add_tail(&this->d_lru, &dentry_unused);
670 dentry_stat.nr_unused++;
674 spin_unlock(&dcache_lock);
680 * Scan `nr' dentries and return the number which remain.
682 * We need to avoid reentering the filesystem if the caller is performing a
683 * GFP_NOFS allocation attempt. One example deadlock is:
685 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
686 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
687 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
689 * In this case we return -1 to tell the caller that we baled.
691 static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
694 if (!(gfp_mask & __GFP_FS))
698 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
702 * d_alloc - allocate a dcache entry
703 * @parent: parent of entry to allocate
704 * @name: qstr of the name
706 * Allocates a dentry. It returns %NULL if there is insufficient memory
707 * available. On a success the dentry is returned. The name passed in is
708 * copied and the copy passed in may be reused after this call.
711 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
713 struct dentry *dentry;
716 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
720 if (name->len > DNAME_INLINE_LEN-1) {
721 dname = kmalloc(name->len + 1, GFP_KERNEL);
723 kmem_cache_free(dentry_cache, dentry);
727 dname = dentry->d_iname;
729 dentry->d_name.name = dname;
731 dentry->d_name.len = name->len;
732 dentry->d_name.hash = name->hash;
733 memcpy(dname, name->name, name->len);
734 dname[name->len] = 0;
736 atomic_set(&dentry->d_count, 1);
737 dentry->d_flags = DCACHE_UNHASHED;
738 spin_lock_init(&dentry->d_lock);
739 dentry->d_inode = NULL;
740 dentry->d_parent = NULL;
743 dentry->d_fsdata = NULL;
744 dentry->d_extra_attributes = NULL;
745 dentry->d_mounted = 0;
746 #ifdef CONFIG_PROFILING
747 dentry->d_cookie = NULL;
749 INIT_HLIST_NODE(&dentry->d_hash);
750 INIT_LIST_HEAD(&dentry->d_lru);
751 INIT_LIST_HEAD(&dentry->d_subdirs);
752 INIT_LIST_HEAD(&dentry->d_alias);
755 dentry->d_parent = dget(parent);
756 dentry->d_sb = parent->d_sb;
758 INIT_LIST_HEAD(&dentry->d_u.d_child);
761 spin_lock(&dcache_lock);
763 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
764 dentry_stat.nr_dentry++;
765 spin_unlock(&dcache_lock);
770 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
775 q.len = strlen(name);
776 q.hash = full_name_hash(q.name, q.len);
777 return d_alloc(parent, &q);
781 * d_instantiate - fill in inode information for a dentry
782 * @entry: dentry to complete
783 * @inode: inode to attach to this dentry
785 * Fill in inode information in the entry.
787 * This turns negative dentries into productive full members
790 * NOTE! This assumes that the inode count has been incremented
791 * (or otherwise set) by the caller to indicate that it is now
792 * in use by the dcache.
795 void d_instantiate(struct dentry *entry, struct inode * inode)
797 BUG_ON(!list_empty(&entry->d_alias));
798 spin_lock(&dcache_lock);
800 list_add(&entry->d_alias, &inode->i_dentry);
801 entry->d_inode = inode;
802 fsnotify_d_instantiate(entry, inode);
803 spin_unlock(&dcache_lock);
804 security_d_instantiate(entry, inode);
808 * d_instantiate_unique - instantiate a non-aliased dentry
809 * @entry: dentry to instantiate
810 * @inode: inode to attach to this dentry
812 * Fill in inode information in the entry. On success, it returns NULL.
813 * If an unhashed alias of "entry" already exists, then we return the
814 * aliased dentry instead and drop one reference to inode.
816 * Note that in order to avoid conflicts with rename() etc, the caller
817 * had better be holding the parent directory semaphore.
819 * This also assumes that the inode count has been incremented
820 * (or otherwise set) by the caller to indicate that it is now
821 * in use by the dcache.
823 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
825 struct dentry *alias;
826 int len = entry->d_name.len;
827 const char *name = entry->d_name.name;
828 unsigned int hash = entry->d_name.hash;
830 BUG_ON(!list_empty(&entry->d_alias));
831 spin_lock(&dcache_lock);
834 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
835 struct qstr *qstr = &alias->d_name;
837 if (qstr->hash != hash)
839 if (alias->d_parent != entry->d_parent)
841 if (qstr->len != len)
843 if (memcmp(qstr->name, name, len))
846 spin_unlock(&dcache_lock);
847 BUG_ON(!d_unhashed(alias));
851 list_add(&entry->d_alias, &inode->i_dentry);
853 entry->d_inode = inode;
854 fsnotify_d_instantiate(entry, inode);
855 spin_unlock(&dcache_lock);
856 security_d_instantiate(entry, inode);
859 EXPORT_SYMBOL(d_instantiate_unique);
862 * d_alloc_root - allocate root dentry
863 * @root_inode: inode to allocate the root for
865 * Allocate a root ("/") dentry for the inode given. The inode is
866 * instantiated and returned. %NULL is returned if there is insufficient
867 * memory or the inode passed is %NULL.
870 struct dentry * d_alloc_root(struct inode * root_inode)
872 struct dentry *res = NULL;
875 static const struct qstr name = { .name = "/", .len = 1 };
877 res = d_alloc(NULL, &name);
879 res->d_sb = root_inode->i_sb;
881 d_instantiate(res, root_inode);
887 static inline struct hlist_head *d_hash(struct dentry *parent,
890 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
891 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
892 return dentry_hashtable + (hash & D_HASHMASK);
896 * d_alloc_anon - allocate an anonymous dentry
897 * @inode: inode to allocate the dentry for
899 * This is similar to d_alloc_root. It is used by filesystems when
900 * creating a dentry for a given inode, often in the process of
901 * mapping a filehandle to a dentry. The returned dentry may be
902 * anonymous, or may have a full name (if the inode was already
903 * in the cache). The file system may need to make further
904 * efforts to connect this dentry into the dcache properly.
906 * When called on a directory inode, we must ensure that
907 * the inode only ever has one dentry. If a dentry is
908 * found, that is returned instead of allocating a new one.
910 * On successful return, the reference to the inode has been transferred
911 * to the dentry. If %NULL is returned (indicating kmalloc failure),
912 * the reference on the inode has not been released.
915 struct dentry * d_alloc_anon(struct inode *inode)
917 static const struct qstr anonstring = { .name = "" };
921 if ((res = d_find_alias(inode))) {
926 tmp = d_alloc(NULL, &anonstring);
930 tmp->d_parent = tmp; /* make sure dput doesn't croak */
932 spin_lock(&dcache_lock);
933 res = __d_find_alias(inode, 0);
935 /* attach a disconnected dentry */
938 spin_lock(&res->d_lock);
939 res->d_sb = inode->i_sb;
941 res->d_inode = inode;
942 res->d_flags |= DCACHE_DISCONNECTED;
943 res->d_flags &= ~DCACHE_UNHASHED;
944 list_add(&res->d_alias, &inode->i_dentry);
945 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
946 spin_unlock(&res->d_lock);
948 inode = NULL; /* don't drop reference */
950 spin_unlock(&dcache_lock);
961 * d_splice_alias - splice a disconnected dentry into the tree if one exists
962 * @inode: the inode which may have a disconnected dentry
963 * @dentry: a negative dentry which we want to point to the inode.
965 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
966 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
967 * and return it, else simply d_add the inode to the dentry and return NULL.
969 * This is needed in the lookup routine of any filesystem that is exportable
970 * (via knfsd) so that we can build dcache paths to directories effectively.
972 * If a dentry was found and moved, then it is returned. Otherwise NULL
973 * is returned. This matches the expected return value of ->lookup.
976 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
978 struct dentry *new = NULL;
981 spin_lock(&dcache_lock);
982 new = __d_find_alias(inode, 1);
984 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
985 fsnotify_d_instantiate(new, inode);
986 spin_unlock(&dcache_lock);
987 security_d_instantiate(new, inode);
992 /* d_instantiate takes dcache_lock, so we do it by hand */
993 list_add(&dentry->d_alias, &inode->i_dentry);
994 dentry->d_inode = inode;
995 fsnotify_d_instantiate(dentry, inode);
996 spin_unlock(&dcache_lock);
997 security_d_instantiate(dentry, inode);
1001 d_add(dentry, inode);
1007 * d_lookup - search for a dentry
1008 * @parent: parent dentry
1009 * @name: qstr of name we wish to find
1011 * Searches the children of the parent dentry for the name in question. If
1012 * the dentry is found its reference count is incremented and the dentry
1013 * is returned. The caller must use d_put to free the entry when it has
1014 * finished using it. %NULL is returned on failure.
1016 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1017 * Memory barriers are used while updating and doing lockless traversal.
1018 * To avoid races with d_move while rename is happening, d_lock is used.
1020 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1021 * and name pointer in one structure pointed by d_qstr.
1023 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1024 * lookup is going on.
1026 * dentry_unused list is not updated even if lookup finds the required dentry
1027 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1028 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1031 * d_lookup() is protected against the concurrent renames in some unrelated
1032 * directory using the seqlockt_t rename_lock.
1035 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1037 struct dentry * dentry = NULL;
1041 seq = read_seqbegin(&rename_lock);
1042 dentry = __d_lookup(parent, name);
1045 } while (read_seqretry(&rename_lock, seq));
1049 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1051 unsigned int len = name->len;
1052 unsigned int hash = name->hash;
1053 const unsigned char *str = name->name;
1054 struct hlist_head *head = d_hash(parent,hash);
1055 struct dentry *found = NULL;
1056 struct hlist_node *node;
1057 struct dentry *dentry;
1061 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1064 if (dentry->d_name.hash != hash)
1066 if (dentry->d_parent != parent)
1069 spin_lock(&dentry->d_lock);
1072 * Recheck the dentry after taking the lock - d_move may have
1073 * changed things. Don't bother checking the hash because we're
1074 * about to compare the whole name anyway.
1076 if (dentry->d_parent != parent)
1080 * It is safe to compare names since d_move() cannot
1081 * change the qstr (protected by d_lock).
1083 qstr = &dentry->d_name;
1084 if (parent->d_op && parent->d_op->d_compare) {
1085 if (parent->d_op->d_compare(parent, qstr, name))
1088 if (qstr->len != len)
1090 if (memcmp(qstr->name, str, len))
1094 if (!d_unhashed(dentry)) {
1095 atomic_inc(&dentry->d_count);
1098 spin_unlock(&dentry->d_lock);
1101 spin_unlock(&dentry->d_lock);
1109 * d_hash_and_lookup - hash the qstr then search for a dentry
1110 * @dir: Directory to search in
1111 * @name: qstr of name we wish to find
1113 * On hash failure or on lookup failure NULL is returned.
1115 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1117 struct dentry *dentry = NULL;
1120 * Check for a fs-specific hash function. Note that we must
1121 * calculate the standard hash first, as the d_op->d_hash()
1122 * routine may choose to leave the hash value unchanged.
1124 name->hash = full_name_hash(name->name, name->len);
1125 if (dir->d_op && dir->d_op->d_hash) {
1126 if (dir->d_op->d_hash(dir, name) < 0)
1129 dentry = d_lookup(dir, name);
1135 * d_validate - verify dentry provided from insecure source
1136 * @dentry: The dentry alleged to be valid child of @dparent
1137 * @dparent: The parent dentry (known to be valid)
1138 * @hash: Hash of the dentry
1139 * @len: Length of the name
1141 * An insecure source has sent us a dentry, here we verify it and dget() it.
1142 * This is used by ncpfs in its readdir implementation.
1143 * Zero is returned in the dentry is invalid.
1146 int d_validate(struct dentry *dentry, struct dentry *dparent)
1148 struct hlist_head *base;
1149 struct hlist_node *lhp;
1151 /* Check whether the ptr might be valid at all.. */
1152 if (!kmem_ptr_validate(dentry_cache, dentry))
1155 if (dentry->d_parent != dparent)
1158 spin_lock(&dcache_lock);
1159 base = d_hash(dparent, dentry->d_name.hash);
1160 hlist_for_each(lhp,base) {
1161 /* hlist_for_each_entry_rcu() not required for d_hash list
1162 * as it is parsed under dcache_lock
1164 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1165 __dget_locked(dentry);
1166 spin_unlock(&dcache_lock);
1170 spin_unlock(&dcache_lock);
1176 * When a file is deleted, we have two options:
1177 * - turn this dentry into a negative dentry
1178 * - unhash this dentry and free it.
1180 * Usually, we want to just turn this into
1181 * a negative dentry, but if anybody else is
1182 * currently using the dentry or the inode
1183 * we can't do that and we fall back on removing
1184 * it from the hash queues and waiting for
1185 * it to be deleted later when it has no users
1189 * d_delete - delete a dentry
1190 * @dentry: The dentry to delete
1192 * Turn the dentry into a negative dentry if possible, otherwise
1193 * remove it from the hash queues so it can be deleted later
1196 void d_delete(struct dentry * dentry)
1200 * Are we the only user?
1202 spin_lock(&dcache_lock);
1203 spin_lock(&dentry->d_lock);
1204 isdir = S_ISDIR(dentry->d_inode->i_mode);
1205 if (atomic_read(&dentry->d_count) == 1) {
1206 dentry_iput(dentry);
1207 fsnotify_nameremove(dentry, isdir);
1209 /* remove this and other inotify debug checks after 2.6.18 */
1210 dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
1214 if (!d_unhashed(dentry))
1217 spin_unlock(&dentry->d_lock);
1218 spin_unlock(&dcache_lock);
1220 fsnotify_nameremove(dentry, isdir);
1223 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1226 entry->d_flags &= ~DCACHE_UNHASHED;
1227 hlist_add_head_rcu(&entry->d_hash, list);
1231 * d_rehash - add an entry back to the hash
1232 * @entry: dentry to add to the hash
1234 * Adds a dentry to the hash according to its name.
1237 void d_rehash(struct dentry * entry)
1239 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1241 spin_lock(&dcache_lock);
1242 spin_lock(&entry->d_lock);
1243 __d_rehash(entry, list);
1244 spin_unlock(&entry->d_lock);
1245 spin_unlock(&dcache_lock);
1248 #define do_switch(x,y) do { \
1249 __typeof__ (x) __tmp = x; \
1250 x = y; y = __tmp; } while (0)
1253 * When switching names, the actual string doesn't strictly have to
1254 * be preserved in the target - because we're dropping the target
1255 * anyway. As such, we can just do a simple memcpy() to copy over
1256 * the new name before we switch.
1258 * Note that we have to be a lot more careful about getting the hash
1259 * switched - we have to switch the hash value properly even if it
1260 * then no longer matches the actual (corrupted) string of the target.
1261 * The hash value has to match the hash queue that the dentry is on..
1263 static void switch_names(struct dentry *dentry, struct dentry *target)
1265 if (dname_external(target)) {
1266 if (dname_external(dentry)) {
1268 * Both external: swap the pointers
1270 do_switch(target->d_name.name, dentry->d_name.name);
1273 * dentry:internal, target:external. Steal target's
1274 * storage and make target internal.
1276 dentry->d_name.name = target->d_name.name;
1277 target->d_name.name = target->d_iname;
1280 if (dname_external(dentry)) {
1282 * dentry:external, target:internal. Give dentry's
1283 * storage to target and make dentry internal
1285 memcpy(dentry->d_iname, target->d_name.name,
1286 target->d_name.len + 1);
1287 target->d_name.name = dentry->d_name.name;
1288 dentry->d_name.name = dentry->d_iname;
1291 * Both are internal. Just copy target to dentry
1293 memcpy(dentry->d_iname, target->d_name.name,
1294 target->d_name.len + 1);
1300 * We cannibalize "target" when moving dentry on top of it,
1301 * because it's going to be thrown away anyway. We could be more
1302 * polite about it, though.
1304 * This forceful removal will result in ugly /proc output if
1305 * somebody holds a file open that got deleted due to a rename.
1306 * We could be nicer about the deleted file, and let it show
1307 * up under the name it got deleted rather than the name that
1312 * d_move - move a dentry
1313 * @dentry: entry to move
1314 * @target: new dentry
1316 * Update the dcache to reflect the move of a file name. Negative
1317 * dcache entries should not be moved in this way.
1320 void d_move(struct dentry * dentry, struct dentry * target)
1322 struct hlist_head *list;
1324 if (!dentry->d_inode)
1325 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1327 spin_lock(&dcache_lock);
1328 write_seqlock(&rename_lock);
1330 * XXXX: do we really need to take target->d_lock?
1332 if (target < dentry) {
1333 spin_lock(&target->d_lock);
1334 spin_lock(&dentry->d_lock);
1336 spin_lock(&dentry->d_lock);
1337 spin_lock(&target->d_lock);
1340 /* Move the dentry to the target hash queue, if on different bucket */
1341 if (dentry->d_flags & DCACHE_UNHASHED)
1342 goto already_unhashed;
1344 hlist_del_rcu(&dentry->d_hash);
1347 list = d_hash(target->d_parent, target->d_name.hash);
1348 __d_rehash(dentry, list);
1350 /* Unhash the target: dput() will then get rid of it */
1353 /* flush any possible attributes */
1354 if (dentry->d_extra_attributes) {
1355 kfree(dentry->d_extra_attributes);
1356 dentry->d_extra_attributes = NULL;
1358 if (target->d_extra_attributes) {
1359 kfree(target->d_extra_attributes);
1360 target->d_extra_attributes = NULL;
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 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 EXPORT_SYMBOL_GPL(__d_path);
1477 /* write full pathname into buffer and return start of pathname */
1478 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1479 char *buf, int buflen)
1482 struct vfsmount *rootmnt;
1483 struct dentry *root;
1485 read_lock(¤t->fs->lock);
1486 rootmnt = mntget(current->fs->rootmnt);
1487 root = dget(current->fs->root);
1488 read_unlock(¤t->fs->lock);
1489 spin_lock(&dcache_lock);
1490 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1491 spin_unlock(&dcache_lock);
1498 * NOTE! The user-level library version returns a
1499 * character pointer. The kernel system call just
1500 * returns the length of the buffer filled (which
1501 * includes the ending '\0' character), or a negative
1502 * error value. So libc would do something like
1504 * char *getcwd(char * buf, size_t size)
1508 * retval = sys_getcwd(buf, size);
1515 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1518 struct vfsmount *pwdmnt, *rootmnt;
1519 struct dentry *pwd, *root;
1520 char *page = (char *) __get_free_page(GFP_USER);
1525 read_lock(¤t->fs->lock);
1526 pwdmnt = mntget(current->fs->pwdmnt);
1527 pwd = dget(current->fs->pwd);
1528 rootmnt = mntget(current->fs->rootmnt);
1529 root = dget(current->fs->root);
1530 read_unlock(¤t->fs->lock);
1533 /* Has the current directory has been unlinked? */
1534 spin_lock(&dcache_lock);
1535 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1539 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1540 spin_unlock(&dcache_lock);
1542 error = PTR_ERR(cwd);
1547 len = PAGE_SIZE + page - cwd;
1550 if (copy_to_user(buf, cwd, len))
1554 spin_unlock(&dcache_lock);
1561 free_page((unsigned long) page);
1566 * Test whether new_dentry is a subdirectory of old_dentry.
1568 * Trivially implemented using the dcache structure
1572 * is_subdir - is new dentry a subdirectory of old_dentry
1573 * @new_dentry: new dentry
1574 * @old_dentry: old dentry
1576 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1577 * Returns 0 otherwise.
1578 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1581 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1584 struct dentry * saved = new_dentry;
1587 /* need rcu_readlock to protect against the d_parent trashing due to
1592 /* for restarting inner loop in case of seq retry */
1595 seq = read_seqbegin(&rename_lock);
1597 if (new_dentry != old_dentry) {
1598 struct dentry * parent = new_dentry->d_parent;
1599 if (parent == new_dentry)
1601 new_dentry = parent;
1607 } while (read_seqretry(&rename_lock, seq));
1613 void d_genocide(struct dentry *root)
1615 struct dentry *this_parent = root;
1616 struct list_head *next;
1618 spin_lock(&dcache_lock);
1620 next = this_parent->d_subdirs.next;
1622 while (next != &this_parent->d_subdirs) {
1623 struct list_head *tmp = next;
1624 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1626 if (d_unhashed(dentry)||!dentry->d_inode)
1628 if (!list_empty(&dentry->d_subdirs)) {
1629 this_parent = dentry;
1632 atomic_dec(&dentry->d_count);
1634 if (this_parent != root) {
1635 next = this_parent->d_u.d_child.next;
1636 atomic_dec(&this_parent->d_count);
1637 this_parent = this_parent->d_parent;
1640 spin_unlock(&dcache_lock);
1644 * find_inode_number - check for dentry with name
1645 * @dir: directory to check
1646 * @name: Name to find.
1648 * Check whether a dentry already exists for the given name,
1649 * and return the inode number if it has an inode. Otherwise
1652 * This routine is used to post-process directory listings for
1653 * filesystems using synthetic inode numbers, and is necessary
1654 * to keep getcwd() working.
1657 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1659 struct dentry * dentry;
1662 dentry = d_hash_and_lookup(dir, name);
1664 if (dentry->d_inode)
1665 ino = dentry->d_inode->i_ino;
1671 static __initdata unsigned long dhash_entries;
1672 static int __init set_dhash_entries(char *str)
1676 dhash_entries = simple_strtoul(str, &str, 0);
1679 __setup("dhash_entries=", set_dhash_entries);
1681 static void __init dcache_init_early(void)
1685 /* If hashes are distributed across NUMA nodes, defer
1686 * hash allocation until vmalloc space is available.
1692 alloc_large_system_hash("Dentry cache",
1693 sizeof(struct hlist_head),
1701 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1702 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1705 void flush_dentry_attributes (void)
1707 struct hlist_node *tmp;
1708 struct dentry *dentry;
1711 spin_lock(&dcache_lock);
1712 for (i = 0; i <= d_hash_mask; i++)
1713 hlist_for_each_entry(dentry, tmp, dentry_hashtable+i, d_hash) {
1714 kfree(dentry->d_extra_attributes);
1715 dentry->d_extra_attributes = NULL;
1717 spin_unlock(&dcache_lock);
1720 EXPORT_SYMBOL_GPL(flush_dentry_attributes);
1722 static void __init dcache_init(unsigned long mempages)
1727 * A constructor could be added for stable state like the lists,
1728 * but it is probably not worth it because of the cache nature
1731 dentry_cache = kmem_cache_create("dentry_cache",
1732 sizeof(struct dentry),
1734 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1738 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1740 /* Hash may have been set up in dcache_init_early */
1745 alloc_large_system_hash("Dentry cache",
1746 sizeof(struct hlist_head),
1754 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1755 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1758 /* SLAB cache for __getname() consumers */
1759 kmem_cache_t *names_cachep __read_mostly;
1761 /* SLAB cache for file structures */
1762 kmem_cache_t *filp_cachep __read_mostly;
1764 EXPORT_SYMBOL(d_genocide);
1766 extern void bdev_cache_init(void);
1767 extern void chrdev_init(void);
1769 void __init vfs_caches_init_early(void)
1771 dcache_init_early();
1775 void __init vfs_caches_init(unsigned long mempages)
1777 unsigned long reserve;
1779 /* Base hash sizes on available memory, with a reserve equal to
1780 150% of current kernel size */
1782 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1783 mempages -= reserve;
1785 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1786 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1788 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
1789 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1791 dcache_init(mempages);
1792 inode_init(mempages);
1793 files_init(mempages);
1799 EXPORT_SYMBOL(d_alloc);
1800 EXPORT_SYMBOL(d_alloc_anon);
1801 EXPORT_SYMBOL(d_alloc_root);
1802 EXPORT_SYMBOL(d_delete);
1803 EXPORT_SYMBOL(d_find_alias);
1804 EXPORT_SYMBOL(d_instantiate);
1805 EXPORT_SYMBOL(d_invalidate);
1806 EXPORT_SYMBOL(d_lookup);
1807 EXPORT_SYMBOL(d_move);
1808 EXPORT_SYMBOL(d_path);
1809 EXPORT_SYMBOL(d_prune_aliases);
1810 EXPORT_SYMBOL(d_rehash);
1811 EXPORT_SYMBOL(d_splice_alias);
1812 EXPORT_SYMBOL(d_validate);
1813 EXPORT_SYMBOL(dget_locked);
1814 EXPORT_SYMBOL(dput);
1815 EXPORT_SYMBOL(find_inode_number);
1816 EXPORT_SYMBOL(have_submounts);
1817 EXPORT_SYMBOL(names_cachep);
1818 EXPORT_SYMBOL(shrink_dcache_parent);
1819 EXPORT_SYMBOL(shrink_dcache_sb);