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/string.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/smp_lock.h>
24 #include <linux/hash.h>
25 #include <linux/cache.h>
26 #include <linux/module.h>
27 #include <linux/mount.h>
28 #include <linux/file.h>
29 #include <asm/uaccess.h>
30 #include <linux/security.h>
31 #include <linux/seqlock.h>
32 #include <linux/swap.h>
33 #include <linux/bootmem.h>
35 /* #define DCACHE_DEBUG 1 */
37 int sysctl_vfs_cache_pressure = 100;
39 spinlock_t dcache_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
40 seqlock_t rename_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
42 EXPORT_SYMBOL(dcache_lock);
44 static kmem_cache_t *dentry_cache;
46 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
49 * This is the single most critical data structure when it comes
50 * to the dcache: the hashtable for lookups. Somebody should try
51 * to make this good - I've just made it work.
53 * This hash-function tries to avoid losing too many bits of hash
54 * information, yet avoid using a prime hash-size or similar.
56 #define D_HASHBITS d_hash_shift
57 #define D_HASHMASK d_hash_mask
59 static unsigned int d_hash_mask;
60 static unsigned int d_hash_shift;
61 static struct hlist_head *dentry_hashtable;
62 static LIST_HEAD(dentry_unused);
64 static void prune_dcache(int count);
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 if (dentry->d_op && dentry->d_op->d_iput)
106 dentry->d_op->d_iput(dentry, inode);
110 spin_unlock(&dentry->d_lock);
111 spin_unlock(&dcache_lock);
118 * This is complicated by the fact that we do not want to put
119 * dentries that are no longer on any hash chain on the unused
120 * list: we'd much rather just get rid of them immediately.
122 * However, that implies that we have to traverse the dentry
123 * tree upwards to the parents which might _also_ now be
124 * scheduled for deletion (it may have been only waiting for
125 * its last child to go away).
127 * This tail recursion is done by hand as we don't want to depend
128 * on the compiler to always get this right (gcc generally doesn't).
129 * Real recursion would eat up our stack space.
133 * dput - release a dentry
134 * @dentry: dentry to release
136 * Release a dentry. This will drop the usage count and if appropriate
137 * call the dentry unlink method as well as removing it from the queues and
138 * releasing its resources. If the parent dentries were scheduled for release
139 * they too may now get deleted.
141 * no dcache lock, please.
144 void dput(struct dentry *dentry)
150 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
153 spin_lock(&dentry->d_lock);
154 if (atomic_read(&dentry->d_count)) {
155 spin_unlock(&dentry->d_lock);
156 spin_unlock(&dcache_lock);
161 * AV: ->d_delete() is _NOT_ allowed to block now.
163 if (dentry->d_op && dentry->d_op->d_delete) {
164 if (dentry->d_op->d_delete(dentry))
167 /* Unreachable? Get rid of it */
168 if (d_unhashed(dentry))
170 if (list_empty(&dentry->d_lru)) {
171 dentry->d_flags |= DCACHE_REFERENCED;
172 list_add(&dentry->d_lru, &dentry_unused);
173 dentry_stat.nr_unused++;
175 spin_unlock(&dentry->d_lock);
176 spin_unlock(&dcache_lock);
183 struct dentry *parent;
185 /* If dentry was on d_lru list
186 * delete it from there
188 if (!list_empty(&dentry->d_lru)) {
189 list_del(&dentry->d_lru);
190 dentry_stat.nr_unused--;
192 list_del(&dentry->d_child);
193 dentry_stat.nr_dentry--; /* For d_free, below */
194 /*drops the locks, at that point nobody can reach this dentry */
196 parent = dentry->d_parent;
198 if (dentry == parent)
206 * d_invalidate - invalidate a dentry
207 * @dentry: dentry to invalidate
209 * Try to invalidate the dentry if it turns out to be
210 * possible. If there are other dentries that can be
211 * reached through this one we can't delete it and we
212 * return -EBUSY. On success we return 0.
217 int d_invalidate(struct dentry * dentry)
220 * If it's already been dropped, return OK.
222 spin_lock(&dcache_lock);
223 if (d_unhashed(dentry)) {
224 spin_unlock(&dcache_lock);
228 * Check whether to do a partial shrink_dcache
229 * to get rid of unused child entries.
231 if (!list_empty(&dentry->d_subdirs)) {
232 spin_unlock(&dcache_lock);
233 shrink_dcache_parent(dentry);
234 spin_lock(&dcache_lock);
238 * Somebody else still using it?
240 * If it's a directory, we can't drop it
241 * for fear of somebody re-populating it
242 * with children (even though dropping it
243 * would make it unreachable from the root,
244 * we might still populate it if it was a
245 * working directory or similar).
247 spin_lock(&dentry->d_lock);
248 if (atomic_read(&dentry->d_count) > 1) {
249 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
250 spin_unlock(&dentry->d_lock);
251 spin_unlock(&dcache_lock);
257 spin_unlock(&dentry->d_lock);
258 spin_unlock(&dcache_lock);
262 /* This should be called _only_ with dcache_lock held */
264 static inline struct dentry * __dget_locked(struct dentry *dentry)
266 atomic_inc(&dentry->d_count);
267 if (!list_empty(&dentry->d_lru)) {
268 dentry_stat.nr_unused--;
269 list_del_init(&dentry->d_lru);
274 struct dentry * dget_locked(struct dentry *dentry)
276 return __dget_locked(dentry);
280 * d_find_alias - grab a hashed alias of inode
281 * @inode: inode in question
283 * If inode has a hashed alias - acquire the reference to alias and
284 * return it. Otherwise return NULL. Notice that if inode is a directory
285 * there can be only one alias and it can be unhashed only if it has
288 * If the inode has a DCACHE_DISCONNECTED alias, then prefer
289 * any other hashed alias over that one.
292 struct dentry * d_find_alias(struct inode *inode)
294 struct list_head *head, *next, *tmp;
295 struct dentry *alias, *discon_alias=NULL;
297 spin_lock(&dcache_lock);
298 head = &inode->i_dentry;
299 next = inode->i_dentry.next;
300 while (next != head) {
304 alias = list_entry(tmp, struct dentry, d_alias);
305 if (!d_unhashed(alias)) {
306 if (alias->d_flags & DCACHE_DISCONNECTED)
307 discon_alias = alias;
309 __dget_locked(alias);
310 spin_unlock(&dcache_lock);
316 __dget_locked(discon_alias);
317 spin_unlock(&dcache_lock);
322 * Try to kill dentries associated with this inode.
323 * WARNING: you must own a reference to inode.
325 void d_prune_aliases(struct inode *inode)
327 struct list_head *tmp, *head = &inode->i_dentry;
329 spin_lock(&dcache_lock);
331 while ((tmp = tmp->next) != head) {
332 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
333 if (!atomic_read(&dentry->d_count)) {
334 __dget_locked(dentry);
336 spin_unlock(&dcache_lock);
341 spin_unlock(&dcache_lock);
345 * Throw away a dentry - free the inode, dput the parent.
346 * This requires that the LRU list has already been
348 * Called with dcache_lock, drops it and then regains.
350 static inline void prune_one_dentry(struct dentry * dentry)
352 struct dentry * parent;
355 list_del(&dentry->d_child);
356 dentry_stat.nr_dentry--; /* For d_free, below */
358 parent = dentry->d_parent;
360 if (parent != dentry)
362 spin_lock(&dcache_lock);
366 * prune_dcache - shrink the dcache
367 * @count: number of entries to try and free
369 * Shrink the dcache. This is done when we need
370 * more memory, or simply when we need to unmount
371 * something (at which point we need to unuse
374 * This function may fail to free any resources if
375 * all the dentries are in use.
378 static void prune_dcache(int count)
380 spin_lock(&dcache_lock);
381 for (; count ; count--) {
382 struct dentry *dentry;
383 struct list_head *tmp;
385 cond_resched_lock(&dcache_lock);
387 tmp = dentry_unused.prev;
388 if (tmp == &dentry_unused)
391 prefetch(dentry_unused.prev);
392 dentry_stat.nr_unused--;
393 dentry = list_entry(tmp, struct dentry, d_lru);
395 spin_lock(&dentry->d_lock);
397 * We found an inuse dentry which was not removed from
398 * dentry_unused because of laziness during lookup. Do not free
399 * it - just keep it off the dentry_unused list.
401 if (atomic_read(&dentry->d_count)) {
402 spin_unlock(&dentry->d_lock);
405 /* If the dentry was recently referenced, don't free it. */
406 if (dentry->d_flags & DCACHE_REFERENCED) {
407 dentry->d_flags &= ~DCACHE_REFERENCED;
408 list_add(&dentry->d_lru, &dentry_unused);
409 dentry_stat.nr_unused++;
410 spin_unlock(&dentry->d_lock);
413 prune_one_dentry(dentry);
415 spin_unlock(&dcache_lock);
419 * Shrink the dcache for the specified super block.
420 * This allows us to unmount a device without disturbing
421 * the dcache for the other devices.
423 * This implementation makes just two traversals of the
424 * unused list. On the first pass we move the selected
425 * dentries to the most recent end, and on the second
426 * pass we free them. The second pass must restart after
427 * each dput(), but since the target dentries are all at
428 * the end, it's really just a single traversal.
432 * shrink_dcache_sb - shrink dcache for a superblock
435 * Shrink the dcache for the specified super block. This
436 * is used to free the dcache before unmounting a file
440 void shrink_dcache_sb(struct super_block * sb)
442 struct list_head *tmp, *next;
443 struct dentry *dentry;
446 * Pass one ... move the dentries for the specified
447 * superblock to the most recent end of the unused list.
449 spin_lock(&dcache_lock);
450 next = dentry_unused.next;
451 while (next != &dentry_unused) {
454 dentry = list_entry(tmp, struct dentry, d_lru);
455 if (dentry->d_sb != sb)
458 list_add(tmp, &dentry_unused);
462 * Pass two ... free the dentries for this superblock.
465 next = dentry_unused.next;
466 while (next != &dentry_unused) {
469 dentry = list_entry(tmp, struct dentry, d_lru);
470 if (dentry->d_sb != sb)
472 dentry_stat.nr_unused--;
474 spin_lock(&dentry->d_lock);
475 if (atomic_read(&dentry->d_count)) {
476 spin_unlock(&dentry->d_lock);
479 prune_one_dentry(dentry);
482 spin_unlock(&dcache_lock);
486 * Search for at least 1 mount point in the dentry's subdirs.
487 * We descend to the next level whenever the d_subdirs
488 * list is non-empty and continue searching.
492 * have_submounts - check for mounts over a dentry
493 * @parent: dentry to check.
495 * Return true if the parent or its subdirectories contain
499 int have_submounts(struct dentry *parent)
501 struct dentry *this_parent = parent;
502 struct list_head *next;
504 spin_lock(&dcache_lock);
505 if (d_mountpoint(parent))
508 next = this_parent->d_subdirs.next;
510 while (next != &this_parent->d_subdirs) {
511 struct list_head *tmp = next;
512 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
514 /* Have we found a mount point ? */
515 if (d_mountpoint(dentry))
517 if (!list_empty(&dentry->d_subdirs)) {
518 this_parent = dentry;
523 * All done at this level ... ascend and resume the search.
525 if (this_parent != parent) {
526 next = this_parent->d_child.next;
527 this_parent = this_parent->d_parent;
530 spin_unlock(&dcache_lock);
531 return 0; /* No mount points found in tree */
533 spin_unlock(&dcache_lock);
538 * Search the dentry child list for the specified parent,
539 * and move any unused dentries to the end of the unused
540 * list for prune_dcache(). We descend to the next level
541 * whenever the d_subdirs list is non-empty and continue
544 static int select_parent(struct dentry * parent)
546 struct dentry *this_parent = parent;
547 struct list_head *next;
550 spin_lock(&dcache_lock);
552 next = this_parent->d_subdirs.next;
554 while (next != &this_parent->d_subdirs) {
555 struct list_head *tmp = next;
556 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
559 if (!list_empty(&dentry->d_lru)) {
560 dentry_stat.nr_unused--;
561 list_del_init(&dentry->d_lru);
564 * move only zero ref count dentries to the end
565 * of the unused list for prune_dcache
567 if (!atomic_read(&dentry->d_count)) {
568 list_add(&dentry->d_lru, dentry_unused.prev);
569 dentry_stat.nr_unused++;
573 * Descend a level if the d_subdirs list is non-empty.
575 if (!list_empty(&dentry->d_subdirs)) {
576 this_parent = dentry;
578 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
579 dentry->d_parent->d_name.name, dentry->d_name.name, found);
585 * All done at this level ... ascend and resume the search.
587 if (this_parent != parent) {
588 next = this_parent->d_child.next;
589 this_parent = this_parent->d_parent;
591 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
592 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
596 spin_unlock(&dcache_lock);
601 * shrink_dcache_parent - prune dcache
602 * @parent: parent of entries to prune
604 * Prune the dcache to remove unused children of the parent dentry.
607 void shrink_dcache_parent(struct dentry * parent)
611 while ((found = select_parent(parent)) != 0)
616 * shrink_dcache_anon - further prune the cache
617 * @head: head of d_hash list of dentries to prune
619 * Prune the dentries that are anonymous
621 * parsing d_hash list does not read_barrier_depends() as it
622 * done under dcache_lock.
625 void shrink_dcache_anon(struct hlist_head *head)
627 struct hlist_node *lp;
631 spin_lock(&dcache_lock);
632 hlist_for_each(lp, head) {
633 struct dentry *this = hlist_entry(lp, struct dentry, d_hash);
634 if (!list_empty(&this->d_lru)) {
635 dentry_stat.nr_unused--;
636 list_del(&this->d_lru);
640 * move only zero ref count dentries to the end
641 * of the unused list for prune_dcache
643 if (!atomic_read(&this->d_count)) {
644 list_add_tail(&this->d_lru, &dentry_unused);
645 dentry_stat.nr_unused++;
649 spin_unlock(&dcache_lock);
655 * Scan `nr' dentries and return the number which remain.
657 * We need to avoid reentering the filesystem if the caller is performing a
658 * GFP_NOFS allocation attempt. One example deadlock is:
660 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
661 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
662 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
664 * In this case we return -1 to tell the caller that we baled.
666 static int shrink_dcache_memory(int nr, unsigned int gfp_mask)
669 if (!(gfp_mask & __GFP_FS))
673 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
677 * d_alloc - allocate a dcache entry
678 * @parent: parent of entry to allocate
679 * @name: qstr of the name
681 * Allocates a dentry. It returns %NULL if there is insufficient memory
682 * available. On a success the dentry is returned. The name passed in is
683 * copied and the copy passed in may be reused after this call.
686 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
688 struct dentry *dentry;
691 #define DENTRY_UNUSED_THRESHOLD 30000
692 #define DENTRY_BATCH_COUNT 32
694 if (dentry_stat.nr_unused > DENTRY_UNUSED_THRESHOLD) {
696 spin_lock(&dcache_lock);
697 if (dentry_stat.nr_unused < DENTRY_UNUSED_THRESHOLD)
699 spin_unlock(&dcache_lock);
701 prune_dcache(DENTRY_BATCH_COUNT);
704 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
708 if (name->len > DNAME_INLINE_LEN-1) {
709 dname = kmalloc(name->len + 1, GFP_KERNEL);
711 kmem_cache_free(dentry_cache, dentry);
715 dname = dentry->d_iname;
717 dentry->d_name.name = dname;
719 dentry->d_name.len = name->len;
720 dentry->d_name.hash = name->hash;
721 memcpy(dname, name->name, name->len);
722 dname[name->len] = 0;
724 atomic_set(&dentry->d_count, 1);
725 dentry->d_flags = DCACHE_UNHASHED;
726 dentry->d_lock = SPIN_LOCK_UNLOCKED;
727 dentry->d_inode = NULL;
728 dentry->d_parent = NULL;
731 dentry->d_fsdata = NULL;
732 dentry->d_mounted = 0;
733 dentry->d_cookie = NULL;
734 dentry->d_bucket = NULL;
735 INIT_HLIST_NODE(&dentry->d_hash);
736 INIT_LIST_HEAD(&dentry->d_lru);
737 INIT_LIST_HEAD(&dentry->d_subdirs);
738 INIT_LIST_HEAD(&dentry->d_alias);
741 dentry->d_parent = dget(parent);
742 dentry->d_sb = parent->d_sb;
744 INIT_LIST_HEAD(&dentry->d_child);
747 spin_lock(&dcache_lock);
749 list_add(&dentry->d_child, &parent->d_subdirs);
750 dentry_stat.nr_dentry++;
751 spin_unlock(&dcache_lock);
757 * d_instantiate - fill in inode information for a dentry
758 * @entry: dentry to complete
759 * @inode: inode to attach to this dentry
761 * Fill in inode information in the entry.
763 * This turns negative dentries into productive full members
766 * NOTE! This assumes that the inode count has been incremented
767 * (or otherwise set) by the caller to indicate that it is now
768 * in use by the dcache.
771 void d_instantiate(struct dentry *entry, struct inode * inode)
773 if (!list_empty(&entry->d_alias)) BUG();
774 spin_lock(&dcache_lock);
776 list_add(&entry->d_alias, &inode->i_dentry);
777 entry->d_inode = inode;
778 spin_unlock(&dcache_lock);
779 security_d_instantiate(entry, inode);
783 * d_alloc_root - allocate root dentry
784 * @root_inode: inode to allocate the root for
786 * Allocate a root ("/") dentry for the inode given. The inode is
787 * instantiated and returned. %NULL is returned if there is insufficient
788 * memory or the inode passed is %NULL.
791 struct dentry * d_alloc_root(struct inode * root_inode)
793 struct dentry *res = NULL;
796 static const struct qstr name = { .name = "/", .len = 1 };
798 res = d_alloc(NULL, &name);
800 res->d_sb = root_inode->i_sb;
802 d_instantiate(res, root_inode);
808 static inline struct hlist_head *d_hash(struct dentry *parent,
811 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
812 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
813 return dentry_hashtable + (hash & D_HASHMASK);
817 * d_alloc_anon - allocate an anonymous dentry
818 * @inode: inode to allocate the dentry for
820 * This is similar to d_alloc_root. It is used by filesystems when
821 * creating a dentry for a given inode, often in the process of
822 * mapping a filehandle to a dentry. The returned dentry may be
823 * anonymous, or may have a full name (if the inode was already
824 * in the cache). The file system may need to make further
825 * efforts to connect this dentry into the dcache properly.
827 * When called on a directory inode, we must ensure that
828 * the inode only ever has one dentry. If a dentry is
829 * found, that is returned instead of allocating a new one.
831 * On successful return, the reference to the inode has been transferred
832 * to the dentry. If %NULL is returned (indicating kmalloc failure),
833 * the reference on the inode has not been released.
836 struct dentry * d_alloc_anon(struct inode *inode)
838 static const struct qstr anonstring = { .name = "" };
842 if ((res = d_find_alias(inode))) {
847 tmp = d_alloc(NULL, &anonstring);
851 tmp->d_parent = tmp; /* make sure dput doesn't croak */
853 spin_lock(&dcache_lock);
854 if (S_ISDIR(inode->i_mode) && !list_empty(&inode->i_dentry)) {
855 /* A directory can only have one dentry.
856 * This (now) has one, so use it.
858 res = list_entry(inode->i_dentry.next, struct dentry, d_alias);
861 /* attach a disconnected dentry */
865 spin_lock(&res->d_lock);
866 res->d_sb = inode->i_sb;
868 res->d_inode = inode;
871 * Set d_bucket to an "impossible" bucket address so
872 * that d_move() doesn't get a false positive
874 res->d_bucket = NULL;
875 res->d_flags |= DCACHE_DISCONNECTED;
876 res->d_flags &= ~DCACHE_UNHASHED;
877 list_add(&res->d_alias, &inode->i_dentry);
878 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
879 spin_unlock(&res->d_lock);
881 inode = NULL; /* don't drop reference */
883 spin_unlock(&dcache_lock);
894 * d_splice_alias - splice a disconnected dentry into the tree if one exists
895 * @inode: the inode which may have a disconnected dentry
896 * @dentry: a negative dentry which we want to point to the inode.
898 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
899 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
900 * and return it, else simply d_add the inode to the dentry and return NULL.
902 * This is (will be) needed in the lookup routine of any filesystem that is exportable
903 * (via knfsd) so that we can build dcache paths to directories effectively.
905 * If a dentry was found and moved, then it is returned. Otherwise NULL
906 * is returned. This matches the expected return value of ->lookup.
909 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
911 struct dentry *new = NULL;
913 if (inode && S_ISDIR(inode->i_mode)) {
914 spin_lock(&dcache_lock);
915 if (!list_empty(&inode->i_dentry)) {
916 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
918 spin_unlock(&dcache_lock);
919 security_d_instantiate(new, inode);
924 /* d_instantiate takes dcache_lock, so we do it by hand */
925 list_add(&dentry->d_alias, &inode->i_dentry);
926 dentry->d_inode = inode;
927 spin_unlock(&dcache_lock);
928 security_d_instantiate(dentry, inode);
932 d_add(dentry, inode);
938 * d_lookup - search for a dentry
939 * @parent: parent dentry
940 * @name: qstr of name we wish to find
942 * Searches the children of the parent dentry for the name in question. If
943 * the dentry is found its reference count is incremented and the dentry
944 * is returned. The caller must use d_put to free the entry when it has
945 * finished using it. %NULL is returned on failure.
947 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
948 * Memory barriers are used while updating and doing lockless traversal.
949 * To avoid races with d_move while rename is happening, d_lock is used.
951 * Overflows in memcmp(), while d_move, are avoided by keeping the length
952 * and name pointer in one structure pointed by d_qstr.
954 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
955 * lookup is going on.
957 * dentry_unused list is not updated even if lookup finds the required dentry
958 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
959 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
962 * d_lookup() is protected against the concurrent renames in some unrelated
963 * directory using the seqlockt_t rename_lock.
966 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
968 struct dentry * dentry = NULL;
972 seq = read_seqbegin(&rename_lock);
973 dentry = __d_lookup(parent, name);
976 } while (read_seqretry(&rename_lock, seq));
980 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
982 unsigned int len = name->len;
983 unsigned int hash = name->hash;
984 const unsigned char *str = name->name;
985 struct hlist_head *head = d_hash(parent,hash);
986 struct dentry *found = NULL;
987 struct hlist_node *node;
991 hlist_for_each (node, head) {
992 struct dentry *dentry;
995 smp_read_barrier_depends();
996 dentry = hlist_entry(node, struct dentry, d_hash);
1000 if (dentry->d_name.hash != hash)
1002 if (dentry->d_parent != parent)
1005 spin_lock(&dentry->d_lock);
1008 * If lookup ends up in a different bucket due to concurrent
1011 if (unlikely(dentry->d_bucket != head))
1015 * Recheck the dentry after taking the lock - d_move may have
1016 * changed things. Don't bother checking the hash because we're
1017 * about to compare the whole name anyway.
1019 if (dentry->d_parent != parent)
1022 qstr = &dentry->d_name;
1023 smp_read_barrier_depends();
1024 if (parent->d_op && parent->d_op->d_compare) {
1025 if (parent->d_op->d_compare(parent, qstr, name))
1028 if (qstr->len != len)
1030 if (memcmp(qstr->name, str, len))
1034 if (!d_unhashed(dentry)) {
1035 atomic_inc(&dentry->d_count);
1039 spin_unlock(&dentry->d_lock);
1042 spin_unlock(&dentry->d_lock);
1050 * d_validate - verify dentry provided from insecure source
1051 * @dentry: The dentry alleged to be valid child of @dparent
1052 * @dparent: The parent dentry (known to be valid)
1053 * @hash: Hash of the dentry
1054 * @len: Length of the name
1056 * An insecure source has sent us a dentry, here we verify it and dget() it.
1057 * This is used by ncpfs in its readdir implementation.
1058 * Zero is returned in the dentry is invalid.
1061 int d_validate(struct dentry *dentry, struct dentry *dparent)
1063 struct hlist_head *base;
1064 struct hlist_node *lhp;
1066 /* Check whether the ptr might be valid at all.. */
1067 if (!kmem_ptr_validate(dentry_cache, dentry))
1070 if (dentry->d_parent != dparent)
1073 spin_lock(&dcache_lock);
1074 base = d_hash(dparent, dentry->d_name.hash);
1075 hlist_for_each(lhp,base) {
1076 /* read_barrier_depends() not required for d_hash list
1077 * as it is parsed under dcache_lock
1079 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1080 __dget_locked(dentry);
1081 spin_unlock(&dcache_lock);
1085 spin_unlock(&dcache_lock);
1091 * When a file is deleted, we have two options:
1092 * - turn this dentry into a negative dentry
1093 * - unhash this dentry and free it.
1095 * Usually, we want to just turn this into
1096 * a negative dentry, but if anybody else is
1097 * currently using the dentry or the inode
1098 * we can't do that and we fall back on removing
1099 * it from the hash queues and waiting for
1100 * it to be deleted later when it has no users
1104 * d_delete - delete a dentry
1105 * @dentry: The dentry to delete
1107 * Turn the dentry into a negative dentry if possible, otherwise
1108 * remove it from the hash queues so it can be deleted later
1111 void d_delete(struct dentry * dentry)
1114 * Are we the only user?
1116 spin_lock(&dcache_lock);
1117 spin_lock(&dentry->d_lock);
1118 if (atomic_read(&dentry->d_count) == 1) {
1119 dentry_iput(dentry);
1123 if (!d_unhashed(dentry))
1126 spin_unlock(&dentry->d_lock);
1127 spin_unlock(&dcache_lock);
1131 * d_rehash - add an entry back to the hash
1132 * @entry: dentry to add to the hash
1134 * Adds a dentry to the hash according to its name.
1137 void d_rehash(struct dentry * entry)
1139 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1141 spin_lock(&dcache_lock);
1142 spin_lock(&entry->d_lock);
1143 entry->d_flags &= ~DCACHE_UNHASHED;
1144 spin_unlock(&entry->d_lock);
1145 entry->d_bucket = list;
1146 hlist_add_head_rcu(&entry->d_hash, list);
1147 spin_unlock(&dcache_lock);
1150 #define do_switch(x,y) do { \
1151 __typeof__ (x) __tmp = x; \
1152 x = y; y = __tmp; } while (0)
1155 * When switching names, the actual string doesn't strictly have to
1156 * be preserved in the target - because we're dropping the target
1157 * anyway. As such, we can just do a simple memcpy() to copy over
1158 * the new name before we switch.
1160 * Note that we have to be a lot more careful about getting the hash
1161 * switched - we have to switch the hash value properly even if it
1162 * then no longer matches the actual (corrupted) string of the target.
1163 * The hash value has to match the hash queue that the dentry is on..
1165 static void switch_names(struct dentry *dentry, struct dentry *target)
1167 if (dname_external(target)) {
1168 if (dname_external(dentry)) {
1170 * Both external: swap the pointers
1172 do_switch(target->d_name.name, dentry->d_name.name);
1175 * dentry:internal, target:external. Steal target's
1176 * storage and make target internal.
1178 dentry->d_name.name = target->d_name.name;
1179 target->d_name.name = target->d_iname;
1182 if (dname_external(dentry)) {
1184 * dentry:external, target:internal. Give dentry's
1185 * storage to target and make dentry internal
1187 memcpy(dentry->d_iname, target->d_name.name,
1188 target->d_name.len + 1);
1189 target->d_name.name = dentry->d_name.name;
1190 dentry->d_name.name = dentry->d_iname;
1193 * Both are internal. Just copy target to dentry
1195 memcpy(dentry->d_iname, target->d_name.name,
1196 target->d_name.len + 1);
1202 * We cannibalize "target" when moving dentry on top of it,
1203 * because it's going to be thrown away anyway. We could be more
1204 * polite about it, though.
1206 * This forceful removal will result in ugly /proc output if
1207 * somebody holds a file open that got deleted due to a rename.
1208 * We could be nicer about the deleted file, and let it show
1209 * up under the name it got deleted rather than the name that
1214 * d_move - move a dentry
1215 * @dentry: entry to move
1216 * @target: new dentry
1218 * Update the dcache to reflect the move of a file name. Negative
1219 * dcache entries should not be moved in this way.
1222 void d_move(struct dentry * dentry, struct dentry * target)
1224 if (!dentry->d_inode)
1225 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1227 spin_lock(&dcache_lock);
1228 write_seqlock(&rename_lock);
1230 * XXXX: do we really need to take target->d_lock?
1232 if (target < dentry) {
1233 spin_lock(&target->d_lock);
1234 spin_lock(&dentry->d_lock);
1236 spin_lock(&dentry->d_lock);
1237 spin_lock(&target->d_lock);
1240 /* Move the dentry to the target hash queue, if on different bucket */
1241 if (dentry->d_flags & DCACHE_UNHASHED)
1242 goto already_unhashed;
1243 if (dentry->d_bucket != target->d_bucket) {
1244 hlist_del_rcu(&dentry->d_hash);
1246 dentry->d_bucket = target->d_bucket;
1247 hlist_add_head_rcu(&dentry->d_hash, target->d_bucket);
1248 dentry->d_flags &= ~DCACHE_UNHASHED;
1251 /* Unhash the target: dput() will then get rid of it */
1254 list_del(&dentry->d_child);
1255 list_del(&target->d_child);
1257 /* Switch the names.. */
1258 switch_names(dentry, target);
1260 do_switch(dentry->d_name.len, target->d_name.len);
1261 do_switch(dentry->d_name.hash, target->d_name.hash);
1263 /* ... and switch the parents */
1264 if (IS_ROOT(dentry)) {
1265 dentry->d_parent = target->d_parent;
1266 target->d_parent = target;
1267 INIT_LIST_HEAD(&target->d_child);
1269 do_switch(dentry->d_parent, target->d_parent);
1271 /* And add them back to the (new) parent lists */
1272 list_add(&target->d_child, &target->d_parent->d_subdirs);
1275 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
1276 spin_unlock(&target->d_lock);
1277 spin_unlock(&dentry->d_lock);
1278 write_sequnlock(&rename_lock);
1279 spin_unlock(&dcache_lock);
1283 * d_path - return the path of a dentry
1284 * @dentry: dentry to report
1285 * @vfsmnt: vfsmnt to which the dentry belongs
1286 * @root: root dentry
1287 * @rootmnt: vfsmnt to which the root dentry belongs
1288 * @buffer: buffer to return value in
1289 * @buflen: buffer length
1291 * Convert a dentry into an ASCII path name. If the entry has been deleted
1292 * the string " (deleted)" is appended. Note that this is ambiguous.
1294 * Returns the buffer or an error code if the path was too long.
1296 * "buflen" should be positive. Caller holds the dcache_lock.
1298 static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1299 struct dentry *root, struct vfsmount *rootmnt,
1300 char *buffer, int buflen)
1302 char * end = buffer+buflen;
1308 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1313 memcpy(end, " (deleted)", 10);
1323 struct dentry * parent;
1325 if (dentry == root && vfsmnt == rootmnt)
1327 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1329 spin_lock(&vfsmount_lock);
1330 if (vfsmnt->mnt_parent == vfsmnt) {
1331 spin_unlock(&vfsmount_lock);
1334 dentry = vfsmnt->mnt_mountpoint;
1335 vfsmnt = vfsmnt->mnt_parent;
1336 spin_unlock(&vfsmount_lock);
1339 parent = dentry->d_parent;
1341 namelen = dentry->d_name.len;
1342 buflen -= namelen + 1;
1346 memcpy(end, dentry->d_name.name, namelen);
1355 namelen = dentry->d_name.len;
1359 retval -= namelen-1; /* hit the slash */
1360 memcpy(retval, dentry->d_name.name, namelen);
1363 return ERR_PTR(-ENAMETOOLONG);
1366 /* write full pathname into buffer and return start of pathname */
1367 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1368 char *buf, int buflen)
1371 struct vfsmount *rootmnt;
1372 struct dentry *root;
1374 read_lock(¤t->fs->lock);
1375 rootmnt = mntget(current->fs->rootmnt);
1376 root = dget(current->fs->root);
1377 read_unlock(¤t->fs->lock);
1378 spin_lock(&dcache_lock);
1379 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1380 spin_unlock(&dcache_lock);
1387 * NOTE! The user-level library version returns a
1388 * character pointer. The kernel system call just
1389 * returns the length of the buffer filled (which
1390 * includes the ending '\0' character), or a negative
1391 * error value. So libc would do something like
1393 * char *getcwd(char * buf, size_t size)
1397 * retval = sys_getcwd(buf, size);
1404 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1407 struct vfsmount *pwdmnt, *rootmnt;
1408 struct dentry *pwd, *root;
1409 char *page = (char *) __get_free_page(GFP_USER);
1414 read_lock(¤t->fs->lock);
1415 pwdmnt = mntget(current->fs->pwdmnt);
1416 pwd = dget(current->fs->pwd);
1417 rootmnt = mntget(current->fs->rootmnt);
1418 root = dget(current->fs->root);
1419 read_unlock(¤t->fs->lock);
1422 /* Has the current directory has been unlinked? */
1423 spin_lock(&dcache_lock);
1424 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1428 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1429 spin_unlock(&dcache_lock);
1431 error = PTR_ERR(cwd);
1436 len = PAGE_SIZE + page - cwd;
1439 if (copy_to_user(buf, cwd, len))
1443 spin_unlock(&dcache_lock);
1450 free_page((unsigned long) page);
1455 * Test whether new_dentry is a subdirectory of old_dentry.
1457 * Trivially implemented using the dcache structure
1461 * is_subdir - is new dentry a subdirectory of old_dentry
1462 * @new_dentry: new dentry
1463 * @old_dentry: old dentry
1465 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1466 * Returns 0 otherwise.
1467 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1470 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1473 struct dentry * saved = new_dentry;
1477 /* need rcu_readlock to protect against the d_parent trashing due to
1482 /* for restarting inner loop in case of seq retry */
1484 seq = read_seqbegin(&rename_lock);
1486 if (new_dentry != old_dentry) {
1487 struct dentry * parent = new_dentry->d_parent;
1488 if (parent == new_dentry)
1490 new_dentry = parent;
1496 } while (read_seqretry(&rename_lock, seq));
1502 void d_genocide(struct dentry *root)
1504 struct dentry *this_parent = root;
1505 struct list_head *next;
1507 spin_lock(&dcache_lock);
1509 next = this_parent->d_subdirs.next;
1511 while (next != &this_parent->d_subdirs) {
1512 struct list_head *tmp = next;
1513 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1515 if (d_unhashed(dentry)||!dentry->d_inode)
1517 if (!list_empty(&dentry->d_subdirs)) {
1518 this_parent = dentry;
1521 atomic_dec(&dentry->d_count);
1523 if (this_parent != root) {
1524 next = this_parent->d_child.next;
1525 atomic_dec(&this_parent->d_count);
1526 this_parent = this_parent->d_parent;
1529 spin_unlock(&dcache_lock);
1533 * find_inode_number - check for dentry with name
1534 * @dir: directory to check
1535 * @name: Name to find.
1537 * Check whether a dentry already exists for the given name,
1538 * and return the inode number if it has an inode. Otherwise
1541 * This routine is used to post-process directory listings for
1542 * filesystems using synthetic inode numbers, and is necessary
1543 * to keep getcwd() working.
1546 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1548 struct dentry * dentry;
1552 * Check for a fs-specific hash function. Note that we must
1553 * calculate the standard hash first, as the d_op->d_hash()
1554 * routine may choose to leave the hash value unchanged.
1556 name->hash = full_name_hash(name->name, name->len);
1557 if (dir->d_op && dir->d_op->d_hash)
1559 if (dir->d_op->d_hash(dir, name) != 0)
1563 dentry = d_lookup(dir, name);
1566 if (dentry->d_inode)
1567 ino = dentry->d_inode->i_ino;
1574 static __initdata unsigned long dhash_entries;
1575 static int __init set_dhash_entries(char *str)
1579 dhash_entries = simple_strtoul(str, &str, 0);
1582 __setup("dhash_entries=", set_dhash_entries);
1584 static void __init dcache_init_early(void)
1589 alloc_large_system_hash("Dentry cache",
1590 sizeof(struct hlist_head),
1597 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1598 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1601 static void __init dcache_init(unsigned long mempages)
1604 * A constructor could be added for stable state like the lists,
1605 * but it is probably not worth it because of the cache nature
1608 dentry_cache = kmem_cache_create("dentry_cache",
1609 sizeof(struct dentry),
1611 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC,
1614 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1617 /* SLAB cache for __getname() consumers */
1618 kmem_cache_t *names_cachep;
1620 /* SLAB cache for file structures */
1621 kmem_cache_t *filp_cachep;
1623 EXPORT_SYMBOL(d_genocide);
1625 extern void bdev_cache_init(void);
1626 extern void chrdev_init(void);
1628 void __init vfs_caches_init_early(void)
1630 dcache_init_early();
1634 void __init vfs_caches_init(unsigned long mempages)
1636 unsigned long reserve;
1638 /* Base hash sizes on available memory, with a reserve equal to
1639 150% of current kernel size */
1641 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1642 mempages -= reserve;
1644 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1645 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1647 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
1648 SLAB_HWCACHE_ALIGN|SLAB_PANIC, filp_ctor, filp_dtor);
1650 dcache_init(mempages);
1651 inode_init(mempages);
1652 files_init(mempages);
1658 EXPORT_SYMBOL(d_alloc);
1659 EXPORT_SYMBOL(d_alloc_anon);
1660 EXPORT_SYMBOL(d_alloc_root);
1661 EXPORT_SYMBOL(d_delete);
1662 EXPORT_SYMBOL(d_find_alias);
1663 EXPORT_SYMBOL(d_instantiate);
1664 EXPORT_SYMBOL(d_invalidate);
1665 EXPORT_SYMBOL(d_lookup);
1666 EXPORT_SYMBOL(d_move);
1667 EXPORT_SYMBOL(d_path);
1668 EXPORT_SYMBOL(d_prune_aliases);
1669 EXPORT_SYMBOL(d_rehash);
1670 EXPORT_SYMBOL(d_splice_alias);
1671 EXPORT_SYMBOL(d_validate);
1672 EXPORT_SYMBOL(dget_locked);
1673 EXPORT_SYMBOL(dput);
1674 EXPORT_SYMBOL(find_inode_number);
1675 EXPORT_SYMBOL(have_submounts);
1676 EXPORT_SYMBOL(is_subdir);
1677 EXPORT_SYMBOL(names_cachep);
1678 EXPORT_SYMBOL(shrink_dcache_anon);
1679 EXPORT_SYMBOL(shrink_dcache_parent);
1680 EXPORT_SYMBOL(shrink_dcache_sb);