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>
37 /* #define DCACHE_DEBUG 1 */
39 int sysctl_vfs_cache_pressure = 100;
40 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
42 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
43 static seqlock_t rename_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
45 EXPORT_SYMBOL(dcache_lock);
47 static kmem_cache_t *dentry_cache;
49 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
52 * This is the single most critical data structure when it comes
53 * to the dcache: the hashtable for lookups. Somebody should try
54 * to make this good - I've just made it work.
56 * This hash-function tries to avoid losing too many bits of hash
57 * information, yet avoid using a prime hash-size or similar.
59 #define D_HASHBITS d_hash_shift
60 #define D_HASHMASK d_hash_mask
62 static unsigned int d_hash_mask;
63 static unsigned int d_hash_shift;
64 static struct hlist_head *dentry_hashtable;
65 static LIST_HEAD(dentry_unused);
67 /* Statistics gathering. */
68 struct dentry_stat_t dentry_stat = {
72 static void d_callback(struct rcu_head *head)
74 struct dentry * dentry = container_of(head, struct dentry, d_rcu);
76 if (dname_external(dentry))
77 kfree(dentry->d_name.name);
78 kmem_cache_free(dentry_cache, dentry);
82 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
85 static void d_free(struct dentry *dentry)
87 if (dentry->d_op && dentry->d_op->d_release)
88 dentry->d_op->d_release(dentry);
89 call_rcu(&dentry->d_rcu, d_callback);
93 * Release the dentry's inode, using the filesystem
94 * d_iput() operation if defined.
95 * Called with dcache_lock and per dentry lock held, drops both.
97 static inline void dentry_iput(struct dentry * dentry)
99 struct inode *inode = dentry->d_inode;
101 dentry->d_inode = NULL;
102 list_del_init(&dentry->d_alias);
103 spin_unlock(&dentry->d_lock);
104 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_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)
328 spin_lock(&dcache_lock);
329 de = __d_find_alias(inode, 0);
330 spin_unlock(&dcache_lock);
335 * Try to kill dentries associated with this inode.
336 * WARNING: you must own a reference to inode.
338 void d_prune_aliases(struct inode *inode)
340 struct dentry *dentry;
342 spin_lock(&dcache_lock);
343 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
344 spin_lock(&dentry->d_lock);
345 if (!atomic_read(&dentry->d_count)) {
346 __dget_locked(dentry);
348 spin_unlock(&dentry->d_lock);
349 spin_unlock(&dcache_lock);
353 spin_unlock(&dentry->d_lock);
355 spin_unlock(&dcache_lock);
359 * Throw away a dentry - free the inode, dput the parent.
360 * This requires that the LRU list has already been
362 * Called with dcache_lock, drops it and then regains.
364 static inline void prune_one_dentry(struct dentry * dentry)
366 struct dentry * parent;
369 list_del(&dentry->d_child);
370 dentry_stat.nr_dentry--; /* For d_free, below */
372 parent = dentry->d_parent;
374 if (parent != dentry)
376 spin_lock(&dcache_lock);
380 * prune_dcache - shrink the dcache
381 * @count: number of entries to try and free
383 * Shrink the dcache. This is done when we need
384 * more memory, or simply when we need to unmount
385 * something (at which point we need to unuse
388 * This function may fail to free any resources if
389 * all the dentries are in use.
392 static void prune_dcache(int count)
394 spin_lock(&dcache_lock);
395 for (; count ; count--) {
396 struct dentry *dentry;
397 struct list_head *tmp;
399 cond_resched_lock(&dcache_lock);
401 tmp = dentry_unused.prev;
402 if (tmp == &dentry_unused)
405 prefetch(dentry_unused.prev);
406 dentry_stat.nr_unused--;
407 dentry = list_entry(tmp, struct dentry, d_lru);
409 spin_lock(&dentry->d_lock);
411 * We found an inuse dentry which was not removed from
412 * dentry_unused because of laziness during lookup. Do not free
413 * it - just keep it off the dentry_unused list.
415 if (atomic_read(&dentry->d_count)) {
416 spin_unlock(&dentry->d_lock);
419 /* If the dentry was recently referenced, don't free it. */
420 if (dentry->d_flags & DCACHE_REFERENCED) {
421 dentry->d_flags &= ~DCACHE_REFERENCED;
422 list_add(&dentry->d_lru, &dentry_unused);
423 dentry_stat.nr_unused++;
424 spin_unlock(&dentry->d_lock);
427 prune_one_dentry(dentry);
429 spin_unlock(&dcache_lock);
433 * Shrink the dcache for the specified super block.
434 * This allows us to unmount a device without disturbing
435 * the dcache for the other devices.
437 * This implementation makes just two traversals of the
438 * unused list. On the first pass we move the selected
439 * dentries to the most recent end, and on the second
440 * pass we free them. The second pass must restart after
441 * each dput(), but since the target dentries are all at
442 * the end, it's really just a single traversal.
446 * shrink_dcache_sb - shrink dcache for a superblock
449 * Shrink the dcache for the specified super block. This
450 * is used to free the dcache before unmounting a file
454 void shrink_dcache_sb(struct super_block * sb)
456 struct list_head *tmp, *next;
457 struct dentry *dentry;
460 * Pass one ... move the dentries for the specified
461 * superblock to the most recent end of the unused list.
463 spin_lock(&dcache_lock);
464 list_for_each_safe(tmp, next, &dentry_unused) {
465 dentry = list_entry(tmp, struct dentry, d_lru);
466 if (dentry->d_sb != sb)
469 list_add(tmp, &dentry_unused);
473 * Pass two ... free the dentries for this superblock.
476 list_for_each_safe(tmp, next, &dentry_unused) {
477 dentry = list_entry(tmp, struct dentry, d_lru);
478 if (dentry->d_sb != sb)
480 dentry_stat.nr_unused--;
482 spin_lock(&dentry->d_lock);
483 if (atomic_read(&dentry->d_count)) {
484 spin_unlock(&dentry->d_lock);
487 prune_one_dentry(dentry);
490 spin_unlock(&dcache_lock);
494 * Search for at least 1 mount point in the dentry's subdirs.
495 * We descend to the next level whenever the d_subdirs
496 * list is non-empty and continue searching.
500 * have_submounts - check for mounts over a dentry
501 * @parent: dentry to check.
503 * Return true if the parent or its subdirectories contain
507 int have_submounts(struct dentry *parent)
509 struct dentry *this_parent = parent;
510 struct list_head *next;
512 spin_lock(&dcache_lock);
513 if (d_mountpoint(parent))
516 next = this_parent->d_subdirs.next;
518 while (next != &this_parent->d_subdirs) {
519 struct list_head *tmp = next;
520 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
522 /* Have we found a mount point ? */
523 if (d_mountpoint(dentry))
525 if (!list_empty(&dentry->d_subdirs)) {
526 this_parent = dentry;
531 * All done at this level ... ascend and resume the search.
533 if (this_parent != parent) {
534 next = this_parent->d_child.next;
535 this_parent = this_parent->d_parent;
538 spin_unlock(&dcache_lock);
539 return 0; /* No mount points found in tree */
541 spin_unlock(&dcache_lock);
546 * Search the dentry child list for the specified parent,
547 * and move any unused dentries to the end of the unused
548 * list for prune_dcache(). We descend to the next level
549 * whenever the d_subdirs list is non-empty and continue
552 * It returns zero iff there are no unused children,
553 * otherwise it returns the number of children moved to
554 * the end of the unused list. This may not be the total
555 * number of unused children, because select_parent can
556 * drop the lock and return early due to latency
559 static int select_parent(struct dentry * parent)
561 struct dentry *this_parent = parent;
562 struct list_head *next;
565 spin_lock(&dcache_lock);
567 next = this_parent->d_subdirs.next;
569 while (next != &this_parent->d_subdirs) {
570 struct list_head *tmp = next;
571 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
574 if (!list_empty(&dentry->d_lru)) {
575 dentry_stat.nr_unused--;
576 list_del_init(&dentry->d_lru);
579 * move only zero ref count dentries to the end
580 * of the unused list for prune_dcache
582 if (!atomic_read(&dentry->d_count)) {
583 list_add(&dentry->d_lru, dentry_unused.prev);
584 dentry_stat.nr_unused++;
589 * We can return to the caller if we have found some (this
590 * ensures forward progress). We'll be coming back to find
593 if (found && need_resched())
597 * Descend a level if the d_subdirs list is non-empty.
599 if (!list_empty(&dentry->d_subdirs)) {
600 this_parent = dentry;
602 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
603 dentry->d_parent->d_name.name, dentry->d_name.name, found);
609 * All done at this level ... ascend and resume the search.
611 if (this_parent != parent) {
612 next = this_parent->d_child.next;
613 this_parent = this_parent->d_parent;
615 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
616 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
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_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, unsigned int 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_mounted = 0;
745 dentry->d_cookie = NULL;
746 INIT_HLIST_NODE(&dentry->d_hash);
747 INIT_LIST_HEAD(&dentry->d_lru);
748 INIT_LIST_HEAD(&dentry->d_subdirs);
749 INIT_LIST_HEAD(&dentry->d_alias);
752 dentry->d_parent = dget(parent);
753 dentry->d_sb = parent->d_sb;
755 INIT_LIST_HEAD(&dentry->d_child);
758 spin_lock(&dcache_lock);
760 list_add(&dentry->d_child, &parent->d_subdirs);
761 dentry_stat.nr_dentry++;
762 spin_unlock(&dcache_lock);
767 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
772 q.len = strlen(name);
773 q.hash = full_name_hash(q.name, q.len);
774 return d_alloc(parent, &q);
778 * d_instantiate - fill in inode information for a dentry
779 * @entry: dentry to complete
780 * @inode: inode to attach to this dentry
782 * Fill in inode information in the entry.
784 * This turns negative dentries into productive full members
787 * NOTE! This assumes that the inode count has been incremented
788 * (or otherwise set) by the caller to indicate that it is now
789 * in use by the dcache.
792 void d_instantiate(struct dentry *entry, struct inode * inode)
794 if (!list_empty(&entry->d_alias)) BUG();
795 spin_lock(&dcache_lock);
797 list_add(&entry->d_alias, &inode->i_dentry);
798 entry->d_inode = inode;
799 spin_unlock(&dcache_lock);
800 security_d_instantiate(entry, inode);
804 * d_instantiate_unique - instantiate a non-aliased dentry
805 * @entry: dentry to instantiate
806 * @inode: inode to attach to this dentry
808 * Fill in inode information in the entry. On success, it returns NULL.
809 * If an unhashed alias of "entry" already exists, then we return the
810 * aliased dentry instead.
812 * Note that in order to avoid conflicts with rename() etc, the caller
813 * had better be holding the parent directory semaphore.
815 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
817 struct dentry *alias;
818 int len = entry->d_name.len;
819 const char *name = entry->d_name.name;
820 unsigned int hash = entry->d_name.hash;
822 BUG_ON(!list_empty(&entry->d_alias));
823 spin_lock(&dcache_lock);
826 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
827 struct qstr *qstr = &alias->d_name;
829 if (qstr->hash != hash)
831 if (alias->d_parent != entry->d_parent)
833 if (qstr->len != len)
835 if (memcmp(qstr->name, name, len))
838 spin_unlock(&dcache_lock);
839 BUG_ON(!d_unhashed(alias));
842 list_add(&entry->d_alias, &inode->i_dentry);
844 entry->d_inode = inode;
845 spin_unlock(&dcache_lock);
846 security_d_instantiate(entry, inode);
849 EXPORT_SYMBOL(d_instantiate_unique);
852 * d_alloc_root - allocate root dentry
853 * @root_inode: inode to allocate the root for
855 * Allocate a root ("/") dentry for the inode given. The inode is
856 * instantiated and returned. %NULL is returned if there is insufficient
857 * memory or the inode passed is %NULL.
860 struct dentry * d_alloc_root(struct inode * root_inode)
862 struct dentry *res = NULL;
865 static const struct qstr name = { .name = "/", .len = 1 };
867 res = d_alloc(NULL, &name);
869 res->d_sb = root_inode->i_sb;
871 d_instantiate(res, root_inode);
877 static inline struct hlist_head *d_hash(struct dentry *parent,
880 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
881 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
882 return dentry_hashtable + (hash & D_HASHMASK);
886 * d_alloc_anon - allocate an anonymous dentry
887 * @inode: inode to allocate the dentry for
889 * This is similar to d_alloc_root. It is used by filesystems when
890 * creating a dentry for a given inode, often in the process of
891 * mapping a filehandle to a dentry. The returned dentry may be
892 * anonymous, or may have a full name (if the inode was already
893 * in the cache). The file system may need to make further
894 * efforts to connect this dentry into the dcache properly.
896 * When called on a directory inode, we must ensure that
897 * the inode only ever has one dentry. If a dentry is
898 * found, that is returned instead of allocating a new one.
900 * On successful return, the reference to the inode has been transferred
901 * to the dentry. If %NULL is returned (indicating kmalloc failure),
902 * the reference on the inode has not been released.
905 struct dentry * d_alloc_anon(struct inode *inode)
907 static const struct qstr anonstring = { .name = "" };
911 if ((res = d_find_alias(inode))) {
916 tmp = d_alloc(NULL, &anonstring);
920 tmp->d_parent = tmp; /* make sure dput doesn't croak */
922 spin_lock(&dcache_lock);
923 res = __d_find_alias(inode, 0);
925 /* attach a disconnected dentry */
928 spin_lock(&res->d_lock);
929 res->d_sb = inode->i_sb;
931 res->d_inode = inode;
932 res->d_flags |= DCACHE_DISCONNECTED;
933 res->d_flags &= ~DCACHE_UNHASHED;
934 list_add(&res->d_alias, &inode->i_dentry);
935 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
936 spin_unlock(&res->d_lock);
938 inode = NULL; /* don't drop reference */
940 spin_unlock(&dcache_lock);
951 * d_splice_alias - splice a disconnected dentry into the tree if one exists
952 * @inode: the inode which may have a disconnected dentry
953 * @dentry: a negative dentry which we want to point to the inode.
955 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
956 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
957 * and return it, else simply d_add the inode to the dentry and return NULL.
959 * This is needed in the lookup routine of any filesystem that is exportable
960 * (via knfsd) so that we can build dcache paths to directories effectively.
962 * If a dentry was found and moved, then it is returned. Otherwise NULL
963 * is returned. This matches the expected return value of ->lookup.
966 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
968 struct dentry *new = NULL;
971 spin_lock(&dcache_lock);
972 new = __d_find_alias(inode, 1);
974 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
975 spin_unlock(&dcache_lock);
976 security_d_instantiate(new, inode);
981 /* d_instantiate takes dcache_lock, so we do it by hand */
982 list_add(&dentry->d_alias, &inode->i_dentry);
983 dentry->d_inode = inode;
984 spin_unlock(&dcache_lock);
985 security_d_instantiate(dentry, inode);
989 d_add(dentry, inode);
995 * d_lookup - search for a dentry
996 * @parent: parent dentry
997 * @name: qstr of name we wish to find
999 * Searches the children of the parent dentry for the name in question. If
1000 * the dentry is found its reference count is incremented and the dentry
1001 * is returned. The caller must use d_put to free the entry when it has
1002 * finished using it. %NULL is returned on failure.
1004 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1005 * Memory barriers are used while updating and doing lockless traversal.
1006 * To avoid races with d_move while rename is happening, d_lock is used.
1008 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1009 * and name pointer in one structure pointed by d_qstr.
1011 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1012 * lookup is going on.
1014 * dentry_unused list is not updated even if lookup finds the required dentry
1015 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1016 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1019 * d_lookup() is protected against the concurrent renames in some unrelated
1020 * directory using the seqlockt_t rename_lock.
1023 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1025 struct dentry * dentry = NULL;
1029 seq = read_seqbegin(&rename_lock);
1030 dentry = __d_lookup(parent, name);
1033 } while (read_seqretry(&rename_lock, seq));
1037 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1039 unsigned int len = name->len;
1040 unsigned int hash = name->hash;
1041 const unsigned char *str = name->name;
1042 struct hlist_head *head = d_hash(parent,hash);
1043 struct dentry *found = NULL;
1044 struct hlist_node *node;
1048 hlist_for_each_rcu(node, head) {
1049 struct dentry *dentry;
1052 dentry = hlist_entry(node, struct dentry, d_hash);
1054 if (dentry->d_name.hash != hash)
1056 if (dentry->d_parent != parent)
1059 spin_lock(&dentry->d_lock);
1062 * Recheck the dentry after taking the lock - d_move may have
1063 * changed things. Don't bother checking the hash because we're
1064 * about to compare the whole name anyway.
1066 if (dentry->d_parent != parent)
1070 * It is safe to compare names since d_move() cannot
1071 * change the qstr (protected by d_lock).
1073 qstr = &dentry->d_name;
1074 if (parent->d_op && parent->d_op->d_compare) {
1075 if (parent->d_op->d_compare(parent, qstr, name))
1078 if (qstr->len != len)
1080 if (memcmp(qstr->name, str, len))
1084 if (!d_unhashed(dentry)) {
1085 atomic_inc(&dentry->d_count);
1088 spin_unlock(&dentry->d_lock);
1091 spin_unlock(&dentry->d_lock);
1099 * d_validate - verify dentry provided from insecure source
1100 * @dentry: The dentry alleged to be valid child of @dparent
1101 * @dparent: The parent dentry (known to be valid)
1102 * @hash: Hash of the dentry
1103 * @len: Length of the name
1105 * An insecure source has sent us a dentry, here we verify it and dget() it.
1106 * This is used by ncpfs in its readdir implementation.
1107 * Zero is returned in the dentry is invalid.
1110 int d_validate(struct dentry *dentry, struct dentry *dparent)
1112 struct hlist_head *base;
1113 struct hlist_node *lhp;
1115 /* Check whether the ptr might be valid at all.. */
1116 if (!kmem_ptr_validate(dentry_cache, dentry))
1119 if (dentry->d_parent != dparent)
1122 spin_lock(&dcache_lock);
1123 base = d_hash(dparent, dentry->d_name.hash);
1124 hlist_for_each(lhp,base) {
1125 /* hlist_for_each_rcu() not required for d_hash list
1126 * as it is parsed under dcache_lock
1128 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1129 __dget_locked(dentry);
1130 spin_unlock(&dcache_lock);
1134 spin_unlock(&dcache_lock);
1140 * When a file is deleted, we have two options:
1141 * - turn this dentry into a negative dentry
1142 * - unhash this dentry and free it.
1144 * Usually, we want to just turn this into
1145 * a negative dentry, but if anybody else is
1146 * currently using the dentry or the inode
1147 * we can't do that and we fall back on removing
1148 * it from the hash queues and waiting for
1149 * it to be deleted later when it has no users
1153 * d_delete - delete a dentry
1154 * @dentry: The dentry to delete
1156 * Turn the dentry into a negative dentry if possible, otherwise
1157 * remove it from the hash queues so it can be deleted later
1160 void d_delete(struct dentry * dentry)
1164 * Are we the only user?
1166 spin_lock(&dcache_lock);
1167 spin_lock(&dentry->d_lock);
1168 isdir = S_ISDIR(dentry->d_inode->i_mode);
1169 if (atomic_read(&dentry->d_count) == 1) {
1170 dentry_iput(dentry);
1171 fsnotify_nameremove(dentry, isdir);
1175 if (!d_unhashed(dentry))
1178 spin_unlock(&dentry->d_lock);
1179 spin_unlock(&dcache_lock);
1181 fsnotify_nameremove(dentry, isdir);
1184 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1187 entry->d_flags &= ~DCACHE_UNHASHED;
1188 hlist_add_head_rcu(&entry->d_hash, list);
1192 * d_rehash - add an entry back to the hash
1193 * @entry: dentry to add to the hash
1195 * Adds a dentry to the hash according to its name.
1198 void d_rehash(struct dentry * entry)
1200 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1202 spin_lock(&dcache_lock);
1203 spin_lock(&entry->d_lock);
1204 __d_rehash(entry, list);
1205 spin_unlock(&entry->d_lock);
1206 spin_unlock(&dcache_lock);
1209 #define do_switch(x,y) do { \
1210 __typeof__ (x) __tmp = x; \
1211 x = y; y = __tmp; } while (0)
1214 * When switching names, the actual string doesn't strictly have to
1215 * be preserved in the target - because we're dropping the target
1216 * anyway. As such, we can just do a simple memcpy() to copy over
1217 * the new name before we switch.
1219 * Note that we have to be a lot more careful about getting the hash
1220 * switched - we have to switch the hash value properly even if it
1221 * then no longer matches the actual (corrupted) string of the target.
1222 * The hash value has to match the hash queue that the dentry is on..
1224 static void switch_names(struct dentry *dentry, struct dentry *target)
1226 if (dname_external(target)) {
1227 if (dname_external(dentry)) {
1229 * Both external: swap the pointers
1231 do_switch(target->d_name.name, dentry->d_name.name);
1234 * dentry:internal, target:external. Steal target's
1235 * storage and make target internal.
1237 dentry->d_name.name = target->d_name.name;
1238 target->d_name.name = target->d_iname;
1241 if (dname_external(dentry)) {
1243 * dentry:external, target:internal. Give dentry's
1244 * storage to target and make dentry internal
1246 memcpy(dentry->d_iname, target->d_name.name,
1247 target->d_name.len + 1);
1248 target->d_name.name = dentry->d_name.name;
1249 dentry->d_name.name = dentry->d_iname;
1252 * Both are internal. Just copy target to dentry
1254 memcpy(dentry->d_iname, target->d_name.name,
1255 target->d_name.len + 1);
1261 * We cannibalize "target" when moving dentry on top of it,
1262 * because it's going to be thrown away anyway. We could be more
1263 * polite about it, though.
1265 * This forceful removal will result in ugly /proc output if
1266 * somebody holds a file open that got deleted due to a rename.
1267 * We could be nicer about the deleted file, and let it show
1268 * up under the name it got deleted rather than the name that
1273 * d_move - move a dentry
1274 * @dentry: entry to move
1275 * @target: new dentry
1277 * Update the dcache to reflect the move of a file name. Negative
1278 * dcache entries should not be moved in this way.
1281 void d_move(struct dentry * dentry, struct dentry * target)
1283 struct hlist_head *list;
1285 if (!dentry->d_inode)
1286 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1288 spin_lock(&dcache_lock);
1289 write_seqlock(&rename_lock);
1291 * XXXX: do we really need to take target->d_lock?
1293 if (target < dentry) {
1294 spin_lock(&target->d_lock);
1295 spin_lock(&dentry->d_lock);
1297 spin_lock(&dentry->d_lock);
1298 spin_lock(&target->d_lock);
1301 /* Move the dentry to the target hash queue, if on different bucket */
1302 if (dentry->d_flags & DCACHE_UNHASHED)
1303 goto already_unhashed;
1305 hlist_del_rcu(&dentry->d_hash);
1308 list = d_hash(target->d_parent, target->d_name.hash);
1309 __d_rehash(dentry, list);
1311 /* Unhash the target: dput() will then get rid of it */
1314 list_del(&dentry->d_child);
1315 list_del(&target->d_child);
1317 /* Switch the names.. */
1318 switch_names(dentry, target);
1319 do_switch(dentry->d_name.len, target->d_name.len);
1320 do_switch(dentry->d_name.hash, target->d_name.hash);
1322 /* ... and switch the parents */
1323 if (IS_ROOT(dentry)) {
1324 dentry->d_parent = target->d_parent;
1325 target->d_parent = target;
1326 INIT_LIST_HEAD(&target->d_child);
1328 do_switch(dentry->d_parent, target->d_parent);
1330 /* And add them back to the (new) parent lists */
1331 list_add(&target->d_child, &target->d_parent->d_subdirs);
1334 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
1335 spin_unlock(&target->d_lock);
1336 spin_unlock(&dentry->d_lock);
1337 write_sequnlock(&rename_lock);
1338 spin_unlock(&dcache_lock);
1342 * d_path - return the path of a dentry
1343 * @dentry: dentry to report
1344 * @vfsmnt: vfsmnt to which the dentry belongs
1345 * @root: root dentry
1346 * @rootmnt: vfsmnt to which the root dentry belongs
1347 * @buffer: buffer to return value in
1348 * @buflen: buffer length
1350 * Convert a dentry into an ASCII path name. If the entry has been deleted
1351 * the string " (deleted)" is appended. Note that this is ambiguous.
1353 * Returns the buffer or an error code if the path was too long.
1355 * "buflen" should be positive. Caller holds the dcache_lock.
1357 static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1358 struct dentry *root, struct vfsmount *rootmnt,
1359 char *buffer, int buflen)
1361 char * end = buffer+buflen;
1367 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1372 memcpy(end, " (deleted)", 10);
1382 struct dentry * parent;
1384 if (dentry == root && vfsmnt == rootmnt)
1386 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1388 spin_lock(&vfsmount_lock);
1389 if (vfsmnt->mnt_parent == vfsmnt) {
1390 spin_unlock(&vfsmount_lock);
1393 dentry = vfsmnt->mnt_mountpoint;
1394 vfsmnt = vfsmnt->mnt_parent;
1395 spin_unlock(&vfsmount_lock);
1398 parent = dentry->d_parent;
1400 namelen = dentry->d_name.len;
1401 buflen -= namelen + 1;
1405 memcpy(end, dentry->d_name.name, namelen);
1414 namelen = dentry->d_name.len;
1418 retval -= namelen-1; /* hit the slash */
1419 memcpy(retval, dentry->d_name.name, namelen);
1422 return ERR_PTR(-ENAMETOOLONG);
1425 /* write full pathname into buffer and return start of pathname */
1426 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1427 char *buf, int buflen)
1430 struct vfsmount *rootmnt;
1431 struct dentry *root;
1433 read_lock(¤t->fs->lock);
1434 rootmnt = mntget(current->fs->rootmnt);
1435 root = dget(current->fs->root);
1436 read_unlock(¤t->fs->lock);
1437 spin_lock(&dcache_lock);
1438 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1439 spin_unlock(&dcache_lock);
1446 * NOTE! The user-level library version returns a
1447 * character pointer. The kernel system call just
1448 * returns the length of the buffer filled (which
1449 * includes the ending '\0' character), or a negative
1450 * error value. So libc would do something like
1452 * char *getcwd(char * buf, size_t size)
1456 * retval = sys_getcwd(buf, size);
1463 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1466 struct vfsmount *pwdmnt, *rootmnt;
1467 struct dentry *pwd, *root;
1468 char *page = (char *) __get_free_page(GFP_USER);
1473 read_lock(¤t->fs->lock);
1474 pwdmnt = mntget(current->fs->pwdmnt);
1475 pwd = dget(current->fs->pwd);
1476 rootmnt = mntget(current->fs->rootmnt);
1477 root = dget(current->fs->root);
1478 read_unlock(¤t->fs->lock);
1481 /* Has the current directory has been unlinked? */
1482 spin_lock(&dcache_lock);
1483 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1487 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1488 spin_unlock(&dcache_lock);
1490 error = PTR_ERR(cwd);
1495 len = PAGE_SIZE + page - cwd;
1498 if (copy_to_user(buf, cwd, len))
1502 spin_unlock(&dcache_lock);
1509 free_page((unsigned long) page);
1514 * Test whether new_dentry is a subdirectory of old_dentry.
1516 * Trivially implemented using the dcache structure
1520 * is_subdir - is new dentry a subdirectory of old_dentry
1521 * @new_dentry: new dentry
1522 * @old_dentry: old dentry
1524 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1525 * Returns 0 otherwise.
1526 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1529 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1532 struct dentry * saved = new_dentry;
1535 /* need rcu_readlock to protect against the d_parent trashing due to
1540 /* for restarting inner loop in case of seq retry */
1543 seq = read_seqbegin(&rename_lock);
1545 if (new_dentry != old_dentry) {
1546 struct dentry * parent = new_dentry->d_parent;
1547 if (parent == new_dentry)
1549 new_dentry = parent;
1555 } while (read_seqretry(&rename_lock, seq));
1561 void d_genocide(struct dentry *root)
1563 struct dentry *this_parent = root;
1564 struct list_head *next;
1566 spin_lock(&dcache_lock);
1568 next = this_parent->d_subdirs.next;
1570 while (next != &this_parent->d_subdirs) {
1571 struct list_head *tmp = next;
1572 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1574 if (d_unhashed(dentry)||!dentry->d_inode)
1576 if (!list_empty(&dentry->d_subdirs)) {
1577 this_parent = dentry;
1580 atomic_dec(&dentry->d_count);
1582 if (this_parent != root) {
1583 next = this_parent->d_child.next;
1584 atomic_dec(&this_parent->d_count);
1585 this_parent = this_parent->d_parent;
1588 spin_unlock(&dcache_lock);
1592 * find_inode_number - check for dentry with name
1593 * @dir: directory to check
1594 * @name: Name to find.
1596 * Check whether a dentry already exists for the given name,
1597 * and return the inode number if it has an inode. Otherwise
1600 * This routine is used to post-process directory listings for
1601 * filesystems using synthetic inode numbers, and is necessary
1602 * to keep getcwd() working.
1605 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1607 struct dentry * dentry;
1611 * Check for a fs-specific hash function. Note that we must
1612 * calculate the standard hash first, as the d_op->d_hash()
1613 * routine may choose to leave the hash value unchanged.
1615 name->hash = full_name_hash(name->name, name->len);
1616 if (dir->d_op && dir->d_op->d_hash)
1618 if (dir->d_op->d_hash(dir, name) != 0)
1622 dentry = d_lookup(dir, name);
1625 if (dentry->d_inode)
1626 ino = dentry->d_inode->i_ino;
1633 static __initdata unsigned long dhash_entries;
1634 static int __init set_dhash_entries(char *str)
1638 dhash_entries = simple_strtoul(str, &str, 0);
1641 __setup("dhash_entries=", set_dhash_entries);
1643 static void __init dcache_init_early(void)
1647 /* If hashes are distributed across NUMA nodes, defer
1648 * hash allocation until vmalloc space is available.
1654 alloc_large_system_hash("Dentry cache",
1655 sizeof(struct hlist_head),
1663 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1664 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1667 static void __init dcache_init(unsigned long mempages)
1672 * A constructor could be added for stable state like the lists,
1673 * but it is probably not worth it because of the cache nature
1676 dentry_cache = kmem_cache_create("dentry_cache",
1677 sizeof(struct dentry),
1679 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC,
1682 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1684 /* Hash may have been set up in dcache_init_early */
1689 alloc_large_system_hash("Dentry cache",
1690 sizeof(struct hlist_head),
1698 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1699 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1702 /* SLAB cache for __getname() consumers */
1703 kmem_cache_t *names_cachep;
1705 /* SLAB cache for file structures */
1706 kmem_cache_t *filp_cachep;
1708 EXPORT_SYMBOL(d_genocide);
1710 extern void bdev_cache_init(void);
1711 extern void chrdev_init(void);
1713 void __init vfs_caches_init_early(void)
1715 dcache_init_early();
1719 void __init vfs_caches_init(unsigned long mempages)
1721 unsigned long reserve;
1723 /* Base hash sizes on available memory, with a reserve equal to
1724 150% of current kernel size */
1726 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1727 mempages -= reserve;
1729 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1730 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1732 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
1733 SLAB_HWCACHE_ALIGN|SLAB_PANIC, filp_ctor, filp_dtor);
1735 dcache_init(mempages);
1736 inode_init(mempages);
1737 files_init(mempages);
1743 EXPORT_SYMBOL(d_alloc);
1744 EXPORT_SYMBOL(d_alloc_anon);
1745 EXPORT_SYMBOL(d_alloc_root);
1746 EXPORT_SYMBOL(d_delete);
1747 EXPORT_SYMBOL(d_find_alias);
1748 EXPORT_SYMBOL(d_instantiate);
1749 EXPORT_SYMBOL(d_invalidate);
1750 EXPORT_SYMBOL(d_lookup);
1751 EXPORT_SYMBOL(d_move);
1752 EXPORT_SYMBOL(d_path);
1753 EXPORT_SYMBOL(d_prune_aliases);
1754 EXPORT_SYMBOL(d_rehash);
1755 EXPORT_SYMBOL(d_splice_alias);
1756 EXPORT_SYMBOL(d_validate);
1757 EXPORT_SYMBOL(dget_locked);
1758 EXPORT_SYMBOL(dput);
1759 EXPORT_SYMBOL(find_inode_number);
1760 EXPORT_SYMBOL(have_submounts);
1761 EXPORT_SYMBOL(names_cachep);
1762 EXPORT_SYMBOL(shrink_dcache_parent);
1763 EXPORT_SYMBOL(shrink_dcache_sb);