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>
34 #define DCACHE_PARANOIA 1
35 /* #define DCACHE_DEBUG 1 */
37 spinlock_t dcache_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
38 seqlock_t rename_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
40 EXPORT_SYMBOL(dcache_lock);
42 static kmem_cache_t *dentry_cache;
44 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
47 * This is the single most critical data structure when it comes
48 * to the dcache: the hashtable for lookups. Somebody should try
49 * to make this good - I've just made it work.
51 * This hash-function tries to avoid losing too many bits of hash
52 * information, yet avoid using a prime hash-size or similar.
54 #define D_HASHBITS d_hash_shift
55 #define D_HASHMASK d_hash_mask
57 static unsigned int d_hash_mask;
58 static unsigned int d_hash_shift;
59 static struct hlist_head *dentry_hashtable;
60 static LIST_HEAD(dentry_unused);
62 /* Statistics gathering. */
63 struct dentry_stat_t dentry_stat = {
67 static void d_callback(void *arg)
69 struct dentry * dentry = (struct dentry *)arg;
71 if (dname_external(dentry))
72 kfree(dentry->d_name.name);
73 kmem_cache_free(dentry_cache, dentry);
77 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
80 static void d_free(struct dentry *dentry)
82 if (dentry->d_op && dentry->d_op->d_release)
83 dentry->d_op->d_release(dentry);
84 call_rcu(&dentry->d_rcu, d_callback, dentry);
88 * Release the dentry's inode, using the filesystem
89 * d_iput() operation if defined.
90 * Called with dcache_lock and per dentry lock held, drops both.
92 static inline void dentry_iput(struct dentry * dentry)
94 struct inode *inode = dentry->d_inode;
96 dentry->d_inode = NULL;
97 list_del_init(&dentry->d_alias);
98 spin_unlock(&dentry->d_lock);
99 spin_unlock(&dcache_lock);
100 if (dentry->d_op && dentry->d_op->d_iput)
101 dentry->d_op->d_iput(dentry, inode);
105 spin_unlock(&dentry->d_lock);
106 spin_unlock(&dcache_lock);
113 * This is complicated by the fact that we do not want to put
114 * dentries that are no longer on any hash chain on the unused
115 * list: we'd much rather just get rid of them immediately.
117 * However, that implies that we have to traverse the dentry
118 * tree upwards to the parents which might _also_ now be
119 * scheduled for deletion (it may have been only waiting for
120 * its last child to go away).
122 * This tail recursion is done by hand as we don't want to depend
123 * on the compiler to always get this right (gcc generally doesn't).
124 * Real recursion would eat up our stack space.
128 * dput - release a dentry
129 * @dentry: dentry to release
131 * Release a dentry. This will drop the usage count and if appropriate
132 * call the dentry unlink method as well as removing it from the queues and
133 * releasing its resources. If the parent dentries were scheduled for release
134 * they too may now get deleted.
136 * no dcache lock, please.
139 void dput(struct dentry *dentry)
145 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
148 spin_lock(&dentry->d_lock);
149 if (atomic_read(&dentry->d_count)) {
150 spin_unlock(&dentry->d_lock);
151 spin_unlock(&dcache_lock);
156 * AV: ->d_delete() is _NOT_ allowed to block now.
158 if (dentry->d_op && dentry->d_op->d_delete) {
159 if (dentry->d_op->d_delete(dentry))
162 /* Unreachable? Get rid of it */
163 if (d_unhashed(dentry))
165 if (list_empty(&dentry->d_lru)) {
166 dentry->d_flags |= DCACHE_REFERENCED;
167 list_add(&dentry->d_lru, &dentry_unused);
168 dentry_stat.nr_unused++;
170 spin_unlock(&dentry->d_lock);
171 spin_unlock(&dcache_lock);
178 struct dentry *parent;
180 /* If dentry was on d_lru list
181 * delete it from there
183 if (!list_empty(&dentry->d_lru)) {
184 list_del(&dentry->d_lru);
185 dentry_stat.nr_unused--;
187 list_del(&dentry->d_child);
188 dentry_stat.nr_dentry--; /* For d_free, below */
189 /*drops the locks, at that point nobody can reach this dentry */
191 parent = dentry->d_parent;
193 if (dentry == parent)
201 * d_invalidate - invalidate a dentry
202 * @dentry: dentry to invalidate
204 * Try to invalidate the dentry if it turns out to be
205 * possible. If there are other dentries that can be
206 * reached through this one we can't delete it and we
207 * return -EBUSY. On success we return 0.
212 int d_invalidate(struct dentry * dentry)
215 * If it's already been dropped, return OK.
217 spin_lock(&dcache_lock);
218 if (d_unhashed(dentry)) {
219 spin_unlock(&dcache_lock);
223 * Check whether to do a partial shrink_dcache
224 * to get rid of unused child entries.
226 if (!list_empty(&dentry->d_subdirs)) {
227 spin_unlock(&dcache_lock);
228 shrink_dcache_parent(dentry);
229 spin_lock(&dcache_lock);
233 * Somebody else still using it?
235 * If it's a directory, we can't drop it
236 * for fear of somebody re-populating it
237 * with children (even though dropping it
238 * would make it unreachable from the root,
239 * we might still populate it if it was a
240 * working directory or similar).
242 spin_lock(&dentry->d_lock);
243 if (atomic_read(&dentry->d_count) > 1) {
244 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
245 spin_unlock(&dentry->d_lock);
246 spin_unlock(&dcache_lock);
252 spin_unlock(&dentry->d_lock);
253 spin_unlock(&dcache_lock);
257 /* This should be called _only_ with dcache_lock held */
259 static inline struct dentry * __dget_locked(struct dentry *dentry)
261 atomic_inc(&dentry->d_count);
262 if (!list_empty(&dentry->d_lru)) {
263 dentry_stat.nr_unused--;
264 list_del_init(&dentry->d_lru);
269 struct dentry * dget_locked(struct dentry *dentry)
271 return __dget_locked(dentry);
275 * d_find_alias - grab a hashed alias of inode
276 * @inode: inode in question
278 * If inode has a hashed alias - acquire the reference to alias and
279 * return it. Otherwise return NULL. Notice that if inode is a directory
280 * there can be only one alias and it can be unhashed only if it has
283 * If the inode has a DCACHE_DISCONNECTED alias, then prefer
284 * any other hashed alias over that one.
287 struct dentry * d_find_alias(struct inode *inode)
289 struct list_head *head, *next, *tmp;
290 struct dentry *alias, *discon_alias=NULL;
292 spin_lock(&dcache_lock);
293 head = &inode->i_dentry;
294 next = inode->i_dentry.next;
295 while (next != head) {
299 alias = list_entry(tmp, struct dentry, d_alias);
300 if (!d_unhashed(alias)) {
301 if (alias->d_flags & DCACHE_DISCONNECTED)
302 discon_alias = alias;
304 __dget_locked(alias);
305 spin_unlock(&dcache_lock);
311 __dget_locked(discon_alias);
312 spin_unlock(&dcache_lock);
317 * Try to kill dentries associated with this inode.
318 * WARNING: you must own a reference to inode.
320 void d_prune_aliases(struct inode *inode)
322 struct list_head *tmp, *head = &inode->i_dentry;
324 spin_lock(&dcache_lock);
326 while ((tmp = tmp->next) != head) {
327 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
328 if (!atomic_read(&dentry->d_count)) {
329 __dget_locked(dentry);
331 spin_unlock(&dcache_lock);
336 spin_unlock(&dcache_lock);
340 * Throw away a dentry - free the inode, dput the parent.
341 * This requires that the LRU list has already been
343 * Called with dcache_lock, drops it and then regains.
345 static inline void prune_one_dentry(struct dentry * dentry)
347 struct dentry * parent;
350 list_del(&dentry->d_child);
351 dentry_stat.nr_dentry--; /* For d_free, below */
353 parent = dentry->d_parent;
355 if (parent != dentry)
357 spin_lock(&dcache_lock);
361 * prune_dcache - shrink the dcache
362 * @count: number of entries to try and free
364 * Shrink the dcache. This is done when we need
365 * more memory, or simply when we need to unmount
366 * something (at which point we need to unuse
369 * This function may fail to free any resources if
370 * all the dentries are in use.
373 static void prune_dcache(int count)
375 spin_lock(&dcache_lock);
376 for (; count ; count--) {
377 struct dentry *dentry;
378 struct list_head *tmp;
380 tmp = dentry_unused.prev;
381 if (tmp == &dentry_unused)
384 prefetch(dentry_unused.prev);
385 dentry_stat.nr_unused--;
386 dentry = list_entry(tmp, struct dentry, d_lru);
388 spin_lock(&dentry->d_lock);
390 * We found an inuse dentry which was not removed from
391 * dentry_unused because of laziness during lookup. Do not free
392 * it - just keep it off the dentry_unused list.
394 if (atomic_read(&dentry->d_count)) {
395 spin_unlock(&dentry->d_lock);
398 /* If the dentry was recently referenced, don't free it. */
399 if (dentry->d_flags & DCACHE_REFERENCED) {
400 dentry->d_flags &= ~DCACHE_REFERENCED;
401 list_add(&dentry->d_lru, &dentry_unused);
402 dentry_stat.nr_unused++;
403 spin_unlock(&dentry->d_lock);
406 prune_one_dentry(dentry);
408 spin_unlock(&dcache_lock);
412 * Shrink the dcache for the specified super block.
413 * This allows us to unmount a device without disturbing
414 * the dcache for the other devices.
416 * This implementation makes just two traversals of the
417 * unused list. On the first pass we move the selected
418 * dentries to the most recent end, and on the second
419 * pass we free them. The second pass must restart after
420 * each dput(), but since the target dentries are all at
421 * the end, it's really just a single traversal.
425 * shrink_dcache_sb - shrink dcache for a superblock
428 * Shrink the dcache for the specified super block. This
429 * is used to free the dcache before unmounting a file
433 void shrink_dcache_sb(struct super_block * sb)
435 struct list_head *tmp, *next;
436 struct dentry *dentry;
439 * Pass one ... move the dentries for the specified
440 * superblock to the most recent end of the unused list.
442 spin_lock(&dcache_lock);
443 next = dentry_unused.next;
444 while (next != &dentry_unused) {
447 dentry = list_entry(tmp, struct dentry, d_lru);
448 if (dentry->d_sb != sb)
451 list_add(tmp, &dentry_unused);
455 * Pass two ... free the dentries for this superblock.
458 next = dentry_unused.next;
459 while (next != &dentry_unused) {
462 dentry = list_entry(tmp, struct dentry, d_lru);
463 if (dentry->d_sb != sb)
465 dentry_stat.nr_unused--;
467 spin_lock(&dentry->d_lock);
468 if (atomic_read(&dentry->d_count)) {
469 spin_unlock(&dentry->d_lock);
472 prune_one_dentry(dentry);
475 spin_unlock(&dcache_lock);
479 * Search for at least 1 mount point in the dentry's subdirs.
480 * We descend to the next level whenever the d_subdirs
481 * list is non-empty and continue searching.
485 * have_submounts - check for mounts over a dentry
486 * @parent: dentry to check.
488 * Return true if the parent or its subdirectories contain
492 int have_submounts(struct dentry *parent)
494 struct dentry *this_parent = parent;
495 struct list_head *next;
497 spin_lock(&dcache_lock);
498 if (d_mountpoint(parent))
501 next = this_parent->d_subdirs.next;
503 while (next != &this_parent->d_subdirs) {
504 struct list_head *tmp = next;
505 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
507 /* Have we found a mount point ? */
508 if (d_mountpoint(dentry))
510 if (!list_empty(&dentry->d_subdirs)) {
511 this_parent = dentry;
516 * All done at this level ... ascend and resume the search.
518 if (this_parent != parent) {
519 next = this_parent->d_child.next;
520 this_parent = this_parent->d_parent;
523 spin_unlock(&dcache_lock);
524 return 0; /* No mount points found in tree */
526 spin_unlock(&dcache_lock);
531 * Search the dentry child list for the specified parent,
532 * and move any unused dentries to the end of the unused
533 * list for prune_dcache(). We descend to the next level
534 * whenever the d_subdirs list is non-empty and continue
537 static int select_parent(struct dentry * parent)
539 struct dentry *this_parent = parent;
540 struct list_head *next;
543 spin_lock(&dcache_lock);
545 next = this_parent->d_subdirs.next;
547 while (next != &this_parent->d_subdirs) {
548 struct list_head *tmp = next;
549 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
552 if (!list_empty(&dentry->d_lru)) {
553 dentry_stat.nr_unused--;
554 list_del_init(&dentry->d_lru);
557 * move only zero ref count dentries to the end
558 * of the unused list for prune_dcache
560 if (!atomic_read(&dentry->d_count)) {
561 list_add(&dentry->d_lru, dentry_unused.prev);
562 dentry_stat.nr_unused++;
566 * Descend a level if the d_subdirs list is non-empty.
568 if (!list_empty(&dentry->d_subdirs)) {
569 this_parent = dentry;
571 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
572 dentry->d_parent->d_name.name, dentry->d_name.name, found);
578 * All done at this level ... ascend and resume the search.
580 if (this_parent != parent) {
581 next = this_parent->d_child.next;
582 this_parent = this_parent->d_parent;
584 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
585 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
589 spin_unlock(&dcache_lock);
594 * shrink_dcache_parent - prune dcache
595 * @parent: parent of entries to prune
597 * Prune the dcache to remove unused children of the parent dentry.
600 void shrink_dcache_parent(struct dentry * parent)
604 while ((found = select_parent(parent)) != 0)
609 * shrink_dcache_anon - further prune the cache
610 * @head: head of d_hash list of dentries to prune
612 * Prune the dentries that are anonymous
614 * parsing d_hash list does not read_barrier_depends() as it
615 * done under dcache_lock.
618 void shrink_dcache_anon(struct hlist_head *head)
620 struct hlist_node *lp;
624 spin_lock(&dcache_lock);
625 hlist_for_each(lp, head) {
626 struct dentry *this = hlist_entry(lp, struct dentry, d_hash);
627 if (!list_empty(&this->d_lru)) {
628 dentry_stat.nr_unused--;
629 list_del(&this->d_lru);
633 * move only zero ref count dentries to the end
634 * of the unused list for prune_dcache
636 if (!atomic_read(&this->d_count)) {
637 list_add_tail(&this->d_lru, &dentry_unused);
638 dentry_stat.nr_unused++;
642 spin_unlock(&dcache_lock);
648 * Scan `nr' dentries and return the number which remain.
650 * We need to avoid reentering the filesystem if the caller is performing a
651 * GFP_NOFS allocation attempt. One example deadlock is:
653 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
654 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
655 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
657 * In this case we return -1 to tell the caller that we baled.
659 static int shrink_dcache_memory(int nr, unsigned int gfp_mask)
662 if (!(gfp_mask & __GFP_FS))
666 return dentry_stat.nr_unused;
670 * d_alloc - allocate a dcache entry
671 * @parent: parent of entry to allocate
672 * @name: qstr of the name
674 * Allocates a dentry. It returns %NULL if there is insufficient memory
675 * available. On a success the dentry is returned. The name passed in is
676 * copied and the copy passed in may be reused after this call.
679 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
681 struct dentry *dentry;
684 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
688 if (name->len > DNAME_INLINE_LEN-1) {
689 dname = kmalloc(name->len + 1, GFP_KERNEL);
691 kmem_cache_free(dentry_cache, dentry);
695 dname = dentry->d_iname;
697 dentry->d_name.name = dname;
699 dentry->d_name.len = name->len;
700 dentry->d_name.hash = name->hash;
701 memcpy(dname, name->name, name->len);
702 dname[name->len] = 0;
704 atomic_set(&dentry->d_count, 1);
705 dentry->d_flags = DCACHE_UNHASHED;
706 dentry->d_lock = SPIN_LOCK_UNLOCKED;
707 dentry->d_inode = NULL;
708 dentry->d_parent = NULL;
711 dentry->d_fsdata = NULL;
712 dentry->d_mounted = 0;
713 dentry->d_cookie = NULL;
714 dentry->d_bucket = NULL;
715 INIT_HLIST_NODE(&dentry->d_hash);
716 INIT_LIST_HEAD(&dentry->d_lru);
717 INIT_LIST_HEAD(&dentry->d_subdirs);
718 INIT_LIST_HEAD(&dentry->d_alias);
721 dentry->d_parent = dget(parent);
722 dentry->d_sb = parent->d_sb;
724 INIT_LIST_HEAD(&dentry->d_child);
727 spin_lock(&dcache_lock);
729 list_add(&dentry->d_child, &parent->d_subdirs);
730 dentry_stat.nr_dentry++;
731 spin_unlock(&dcache_lock);
737 * d_instantiate - fill in inode information for a dentry
738 * @entry: dentry to complete
739 * @inode: inode to attach to this dentry
741 * Fill in inode information in the entry.
743 * This turns negative dentries into productive full members
746 * NOTE! This assumes that the inode count has been incremented
747 * (or otherwise set) by the caller to indicate that it is now
748 * in use by the dcache.
751 void d_instantiate(struct dentry *entry, struct inode * inode)
753 if (!list_empty(&entry->d_alias)) BUG();
754 spin_lock(&dcache_lock);
756 list_add(&entry->d_alias, &inode->i_dentry);
757 entry->d_inode = inode;
758 spin_unlock(&dcache_lock);
759 security_d_instantiate(entry, inode);
763 * d_alloc_root - allocate root dentry
764 * @root_inode: inode to allocate the root for
766 * Allocate a root ("/") dentry for the inode given. The inode is
767 * instantiated and returned. %NULL is returned if there is insufficient
768 * memory or the inode passed is %NULL.
771 struct dentry * d_alloc_root(struct inode * root_inode)
773 struct dentry *res = NULL;
776 static const struct qstr name = { .name = "/", .len = 1 };
778 res = d_alloc(NULL, &name);
780 res->d_sb = root_inode->i_sb;
782 d_instantiate(res, root_inode);
788 static inline struct hlist_head *d_hash(struct dentry *parent,
791 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
792 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
793 return dentry_hashtable + (hash & D_HASHMASK);
797 * d_alloc_anon - allocate an anonymous dentry
798 * @inode: inode to allocate the dentry for
800 * This is similar to d_alloc_root. It is used by filesystems when
801 * creating a dentry for a given inode, often in the process of
802 * mapping a filehandle to a dentry. The returned dentry may be
803 * anonymous, or may have a full name (if the inode was already
804 * in the cache). The file system may need to make further
805 * efforts to connect this dentry into the dcache properly.
807 * When called on a directory inode, we must ensure that
808 * the inode only ever has one dentry. If a dentry is
809 * found, that is returned instead of allocating a new one.
811 * On successful return, the reference to the inode has been transferred
812 * to the dentry. If %NULL is returned (indicating kmalloc failure),
813 * the reference on the inode has not been released.
816 struct dentry * d_alloc_anon(struct inode *inode)
818 static const struct qstr anonstring = { .name = "" };
822 if ((res = d_find_alias(inode))) {
827 tmp = d_alloc(NULL, &anonstring);
831 tmp->d_parent = tmp; /* make sure dput doesn't croak */
833 spin_lock(&dcache_lock);
834 if (S_ISDIR(inode->i_mode) && !list_empty(&inode->i_dentry)) {
835 /* A directory can only have one dentry.
836 * This (now) has one, so use it.
838 res = list_entry(inode->i_dentry.next, struct dentry, d_alias);
841 /* attach a disconnected dentry */
845 spin_lock(&res->d_lock);
846 res->d_sb = inode->i_sb;
848 res->d_inode = inode;
851 * Set d_bucket to an "impossible" bucket address so
852 * that d_move() doesn't get a false positive
854 res->d_bucket = NULL;
855 res->d_flags |= DCACHE_DISCONNECTED;
856 res->d_flags &= ~DCACHE_UNHASHED;
857 list_add(&res->d_alias, &inode->i_dentry);
858 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
859 spin_unlock(&res->d_lock);
861 inode = NULL; /* don't drop reference */
863 spin_unlock(&dcache_lock);
874 * d_splice_alias - splice a disconnected dentry into the tree if one exists
875 * @inode: the inode which may have a disconnected dentry
876 * @dentry: a negative dentry which we want to point to the inode.
878 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
879 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
880 * and return it, else simply d_add the inode to the dentry and return NULL.
882 * This is (will be) needed in the lookup routine of any filesystem that is exportable
883 * (via knfsd) so that we can build dcache paths to directories effectively.
885 * If a dentry was found and moved, then it is returned. Otherwise NULL
886 * is returned. This matches the expected return value of ->lookup.
889 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
891 struct dentry *new = NULL;
893 if (inode && S_ISDIR(inode->i_mode)) {
894 spin_lock(&dcache_lock);
895 if (!list_empty(&inode->i_dentry)) {
896 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
898 spin_unlock(&dcache_lock);
899 security_d_instantiate(new, inode);
904 /* d_instantiate takes dcache_lock, so we do it by hand */
905 list_add(&dentry->d_alias, &inode->i_dentry);
906 dentry->d_inode = inode;
907 spin_unlock(&dcache_lock);
908 security_d_instantiate(dentry, inode);
912 d_add(dentry, inode);
918 * d_lookup - search for a dentry
919 * @parent: parent dentry
920 * @name: qstr of name we wish to find
922 * Searches the children of the parent dentry for the name in question. If
923 * the dentry is found its reference count is incremented and the dentry
924 * is returned. The caller must use d_put to free the entry when it has
925 * finished using it. %NULL is returned on failure.
927 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
928 * Memory barriers are used while updating and doing lockless traversal.
929 * To avoid races with d_move while rename is happening, d_lock is used.
931 * Overflows in memcmp(), while d_move, are avoided by keeping the length
932 * and name pointer in one structure pointed by d_qstr.
934 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
935 * lookup is going on.
937 * dentry_unused list is not updated even if lookup finds the required dentry
938 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
939 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
942 * d_lookup() is protected against the concurrent renames in some unrelated
943 * directory using the seqlockt_t rename_lock.
946 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
948 struct dentry * dentry = NULL;
952 seq = read_seqbegin(&rename_lock);
953 dentry = __d_lookup(parent, name);
956 } while (read_seqretry(&rename_lock, seq));
960 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
962 unsigned int len = name->len;
963 unsigned int hash = name->hash;
964 const unsigned char *str = name->name;
965 struct hlist_head *head = d_hash(parent,hash);
966 struct dentry *found = NULL;
967 struct hlist_node *node;
971 hlist_for_each (node, head) {
972 struct dentry *dentry;
975 smp_read_barrier_depends();
976 dentry = hlist_entry(node, struct dentry, d_hash);
980 if (dentry->d_name.hash != hash)
982 if (dentry->d_parent != parent)
985 spin_lock(&dentry->d_lock);
988 * If lookup ends up in a different bucket due to concurrent
991 if (unlikely(dentry->d_bucket != head))
995 * Recheck the dentry after taking the lock - d_move may have
996 * changed things. Don't bother checking the hash because we're
997 * about to compare the whole name anyway.
999 if (dentry->d_parent != parent)
1002 qstr = &dentry->d_name;
1003 smp_read_barrier_depends();
1004 if (parent->d_op && parent->d_op->d_compare) {
1005 if (parent->d_op->d_compare(parent, qstr, name))
1008 if (qstr->len != len)
1010 if (memcmp(qstr->name, str, len))
1014 if (!d_unhashed(dentry)) {
1015 atomic_inc(&dentry->d_count);
1019 spin_unlock(&dentry->d_lock);
1022 spin_unlock(&dentry->d_lock);
1030 * d_validate - verify dentry provided from insecure source
1031 * @dentry: The dentry alleged to be valid child of @dparent
1032 * @dparent: The parent dentry (known to be valid)
1033 * @hash: Hash of the dentry
1034 * @len: Length of the name
1036 * An insecure source has sent us a dentry, here we verify it and dget() it.
1037 * This is used by ncpfs in its readdir implementation.
1038 * Zero is returned in the dentry is invalid.
1041 int d_validate(struct dentry *dentry, struct dentry *dparent)
1043 struct hlist_head *base;
1044 struct hlist_node *lhp;
1046 /* Check whether the ptr might be valid at all.. */
1047 if (!kmem_ptr_validate(dentry_cache, dentry))
1050 if (dentry->d_parent != dparent)
1053 spin_lock(&dcache_lock);
1054 base = d_hash(dparent, dentry->d_name.hash);
1055 hlist_for_each(lhp,base) {
1056 /* read_barrier_depends() not required for d_hash list
1057 * as it is parsed under dcache_lock
1059 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1060 __dget_locked(dentry);
1061 spin_unlock(&dcache_lock);
1065 spin_unlock(&dcache_lock);
1071 * When a file is deleted, we have two options:
1072 * - turn this dentry into a negative dentry
1073 * - unhash this dentry and free it.
1075 * Usually, we want to just turn this into
1076 * a negative dentry, but if anybody else is
1077 * currently using the dentry or the inode
1078 * we can't do that and we fall back on removing
1079 * it from the hash queues and waiting for
1080 * it to be deleted later when it has no users
1084 * d_delete - delete a dentry
1085 * @dentry: The dentry to delete
1087 * Turn the dentry into a negative dentry if possible, otherwise
1088 * remove it from the hash queues so it can be deleted later
1091 void d_delete(struct dentry * dentry)
1094 * Are we the only user?
1096 spin_lock(&dcache_lock);
1097 spin_lock(&dentry->d_lock);
1098 if (atomic_read(&dentry->d_count) == 1) {
1099 dentry_iput(dentry);
1103 if (!d_unhashed(dentry))
1106 spin_unlock(&dentry->d_lock);
1107 spin_unlock(&dcache_lock);
1111 * d_rehash - add an entry back to the hash
1112 * @entry: dentry to add to the hash
1114 * Adds a dentry to the hash according to its name.
1117 void d_rehash(struct dentry * entry)
1119 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1121 spin_lock(&dcache_lock);
1122 spin_lock(&entry->d_lock);
1123 entry->d_flags &= ~DCACHE_UNHASHED;
1124 spin_unlock(&entry->d_lock);
1125 entry->d_bucket = list;
1126 hlist_add_head_rcu(&entry->d_hash, list);
1127 spin_unlock(&dcache_lock);
1130 #define do_switch(x,y) do { \
1131 __typeof__ (x) __tmp = x; \
1132 x = y; y = __tmp; } while (0)
1135 * When switching names, the actual string doesn't strictly have to
1136 * be preserved in the target - because we're dropping the target
1137 * anyway. As such, we can just do a simple memcpy() to copy over
1138 * the new name before we switch.
1140 * Note that we have to be a lot more careful about getting the hash
1141 * switched - we have to switch the hash value properly even if it
1142 * then no longer matches the actual (corrupted) string of the target.
1143 * The hash value has to match the hash queue that the dentry is on..
1145 static void switch_names(struct dentry *dentry, struct dentry *target)
1147 if (dname_external(target)) {
1148 if (dname_external(dentry)) {
1150 * Both external: swap the pointers
1152 do_switch(target->d_name.name, dentry->d_name.name);
1155 * dentry:internal, target:external. Steal target's
1156 * storage and make target internal.
1158 dentry->d_name.name = target->d_name.name;
1159 target->d_name.name = target->d_iname;
1162 if (dname_external(dentry)) {
1164 * dentry:external, target:internal. Give dentry's
1165 * storage to target and make dentry internal
1167 memcpy(dentry->d_iname, target->d_name.name,
1168 target->d_name.len + 1);
1169 target->d_name.name = dentry->d_name.name;
1170 dentry->d_name.name = dentry->d_iname;
1173 * Both are internal. Just copy target to dentry
1175 memcpy(dentry->d_iname, target->d_name.name,
1176 target->d_name.len + 1);
1182 * We cannibalize "target" when moving dentry on top of it,
1183 * because it's going to be thrown away anyway. We could be more
1184 * polite about it, though.
1186 * This forceful removal will result in ugly /proc output if
1187 * somebody holds a file open that got deleted due to a rename.
1188 * We could be nicer about the deleted file, and let it show
1189 * up under the name it got deleted rather than the name that
1194 * d_move - move a dentry
1195 * @dentry: entry to move
1196 * @target: new dentry
1198 * Update the dcache to reflect the move of a file name. Negative
1199 * dcache entries should not be moved in this way.
1202 void d_move(struct dentry * dentry, struct dentry * target)
1204 if (!dentry->d_inode)
1205 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1207 spin_lock(&dcache_lock);
1208 write_seqlock(&rename_lock);
1210 * XXXX: do we really need to take target->d_lock?
1212 if (target < dentry) {
1213 spin_lock(&target->d_lock);
1214 spin_lock(&dentry->d_lock);
1216 spin_lock(&dentry->d_lock);
1217 spin_lock(&target->d_lock);
1220 /* Move the dentry to the target hash queue, if on different bucket */
1221 if (dentry->d_flags & DCACHE_UNHASHED)
1222 goto already_unhashed;
1223 if (dentry->d_bucket != target->d_bucket) {
1224 hlist_del_rcu(&dentry->d_hash);
1226 dentry->d_bucket = target->d_bucket;
1227 hlist_add_head_rcu(&dentry->d_hash, target->d_bucket);
1228 dentry->d_flags &= ~DCACHE_UNHASHED;
1231 /* Unhash the target: dput() will then get rid of it */
1234 list_del(&dentry->d_child);
1235 list_del(&target->d_child);
1237 /* Switch the names.. */
1238 switch_names(dentry, target);
1240 do_switch(dentry->d_name.len, target->d_name.len);
1241 do_switch(dentry->d_name.hash, target->d_name.hash);
1243 /* ... and switch the parents */
1244 if (IS_ROOT(dentry)) {
1245 dentry->d_parent = target->d_parent;
1246 target->d_parent = target;
1247 INIT_LIST_HEAD(&target->d_child);
1249 do_switch(dentry->d_parent, target->d_parent);
1251 /* And add them back to the (new) parent lists */
1252 list_add(&target->d_child, &target->d_parent->d_subdirs);
1255 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
1256 spin_unlock(&target->d_lock);
1257 spin_unlock(&dentry->d_lock);
1258 write_sequnlock(&rename_lock);
1259 spin_unlock(&dcache_lock);
1263 * d_path - return the path of a dentry
1264 * @dentry: dentry to report
1265 * @vfsmnt: vfsmnt to which the dentry belongs
1266 * @root: root dentry
1267 * @rootmnt: vfsmnt to which the root dentry belongs
1268 * @buffer: buffer to return value in
1269 * @buflen: buffer length
1271 * Convert a dentry into an ASCII path name. If the entry has been deleted
1272 * the string " (deleted)" is appended. Note that this is ambiguous.
1274 * Returns the buffer or an error code if the path was too long.
1276 * "buflen" should be positive. Caller holds the dcache_lock.
1278 static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1279 struct dentry *root, struct vfsmount *rootmnt,
1280 char *buffer, int buflen)
1282 char * end = buffer+buflen;
1288 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1293 memcpy(end, " (deleted)", 10);
1303 struct dentry * parent;
1305 if (dentry == root && vfsmnt == rootmnt)
1307 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1309 spin_lock(&vfsmount_lock);
1310 if (vfsmnt->mnt_parent == vfsmnt) {
1311 spin_unlock(&vfsmount_lock);
1314 dentry = vfsmnt->mnt_mountpoint;
1315 vfsmnt = vfsmnt->mnt_parent;
1316 spin_unlock(&vfsmount_lock);
1319 parent = dentry->d_parent;
1321 namelen = dentry->d_name.len;
1322 buflen -= namelen + 1;
1326 memcpy(end, dentry->d_name.name, namelen);
1335 namelen = dentry->d_name.len;
1339 retval -= namelen-1; /* hit the slash */
1340 memcpy(retval, dentry->d_name.name, namelen);
1343 return ERR_PTR(-ENAMETOOLONG);
1346 /* write full pathname into buffer and return start of pathname */
1347 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1348 char *buf, int buflen)
1351 struct vfsmount *rootmnt;
1352 struct dentry *root;
1354 read_lock(¤t->fs->lock);
1355 rootmnt = mntget(current->fs->rootmnt);
1356 root = dget(current->fs->root);
1357 read_unlock(¤t->fs->lock);
1358 spin_lock(&dcache_lock);
1359 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1360 spin_unlock(&dcache_lock);
1367 * NOTE! The user-level library version returns a
1368 * character pointer. The kernel system call just
1369 * returns the length of the buffer filled (which
1370 * includes the ending '\0' character), or a negative
1371 * error value. So libc would do something like
1373 * char *getcwd(char * buf, size_t size)
1377 * retval = sys_getcwd(buf, size);
1384 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1387 struct vfsmount *pwdmnt, *rootmnt;
1388 struct dentry *pwd, *root;
1389 char *page = (char *) __get_free_page(GFP_USER);
1394 read_lock(¤t->fs->lock);
1395 pwdmnt = mntget(current->fs->pwdmnt);
1396 pwd = dget(current->fs->pwd);
1397 rootmnt = mntget(current->fs->rootmnt);
1398 root = dget(current->fs->root);
1399 read_unlock(¤t->fs->lock);
1402 /* Has the current directory has been unlinked? */
1403 spin_lock(&dcache_lock);
1404 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1408 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1409 spin_unlock(&dcache_lock);
1411 error = PTR_ERR(cwd);
1416 len = PAGE_SIZE + page - cwd;
1419 if (copy_to_user(buf, cwd, len))
1423 spin_unlock(&dcache_lock);
1430 free_page((unsigned long) page);
1435 * Test whether new_dentry is a subdirectory of old_dentry.
1437 * Trivially implemented using the dcache structure
1441 * is_subdir - is new dentry a subdirectory of old_dentry
1442 * @new_dentry: new dentry
1443 * @old_dentry: old dentry
1445 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1446 * Returns 0 otherwise.
1447 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1450 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1453 struct dentry * saved = new_dentry;
1457 /* need rcu_readlock to protect against the d_parent trashing due to
1462 /* for restarting inner loop in case of seq retry */
1464 seq = read_seqbegin(&rename_lock);
1466 if (new_dentry != old_dentry) {
1467 struct dentry * parent = new_dentry->d_parent;
1468 if (parent == new_dentry)
1470 new_dentry = parent;
1476 } while (read_seqretry(&rename_lock, seq));
1482 void d_genocide(struct dentry *root)
1484 struct dentry *this_parent = root;
1485 struct list_head *next;
1487 spin_lock(&dcache_lock);
1489 next = this_parent->d_subdirs.next;
1491 while (next != &this_parent->d_subdirs) {
1492 struct list_head *tmp = next;
1493 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1495 if (d_unhashed(dentry)||!dentry->d_inode)
1497 if (!list_empty(&dentry->d_subdirs)) {
1498 this_parent = dentry;
1501 atomic_dec(&dentry->d_count);
1503 if (this_parent != root) {
1504 next = this_parent->d_child.next;
1505 atomic_dec(&this_parent->d_count);
1506 this_parent = this_parent->d_parent;
1509 spin_unlock(&dcache_lock);
1513 * find_inode_number - check for dentry with name
1514 * @dir: directory to check
1515 * @name: Name to find.
1517 * Check whether a dentry already exists for the given name,
1518 * and return the inode number if it has an inode. Otherwise
1521 * This routine is used to post-process directory listings for
1522 * filesystems using synthetic inode numbers, and is necessary
1523 * to keep getcwd() working.
1526 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1528 struct dentry * dentry;
1532 * Check for a fs-specific hash function. Note that we must
1533 * calculate the standard hash first, as the d_op->d_hash()
1534 * routine may choose to leave the hash value unchanged.
1536 name->hash = full_name_hash(name->name, name->len);
1537 if (dir->d_op && dir->d_op->d_hash)
1539 if (dir->d_op->d_hash(dir, name) != 0)
1543 dentry = d_lookup(dir, name);
1546 if (dentry->d_inode)
1547 ino = dentry->d_inode->i_ino;
1554 static __initdata unsigned long dhash_entries;
1555 static int __init set_dhash_entries(char *str)
1559 dhash_entries = simple_strtoul(str, &str, 0);
1562 __setup("dhash_entries=", set_dhash_entries);
1564 static void __init dcache_init(unsigned long mempages)
1566 struct hlist_head *d;
1567 unsigned long order;
1568 unsigned int nr_hash;
1572 * A constructor could be added for stable state like the lists,
1573 * but it is probably not worth it because of the cache nature
1576 dentry_cache = kmem_cache_create("dentry_cache",
1577 sizeof(struct dentry),
1579 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC,
1582 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1585 dhash_entries = PAGE_SHIFT < 13 ?
1586 mempages >> (13 - PAGE_SHIFT) :
1587 mempages << (PAGE_SHIFT - 13);
1589 dhash_entries *= sizeof(struct hlist_head);
1590 for (order = 0; ((1UL << order) << PAGE_SHIFT) < dhash_entries; order++)
1596 nr_hash = (1UL << order) * PAGE_SIZE /
1597 sizeof(struct hlist_head);
1598 d_hash_mask = (nr_hash - 1);
1602 while ((tmp >>= 1UL) != 0UL)
1605 dentry_hashtable = (struct hlist_head *)
1606 __get_free_pages(GFP_ATOMIC, order);
1607 } while (dentry_hashtable == NULL && --order >= 0);
1609 printk(KERN_INFO "Dentry cache hash table entries: %d (order: %ld, %ld bytes)\n",
1610 nr_hash, order, (PAGE_SIZE << order));
1612 if (!dentry_hashtable)
1613 panic("Failed to allocate dcache hash table\n");
1615 d = dentry_hashtable;
1624 /* SLAB cache for __getname() consumers */
1625 kmem_cache_t *names_cachep;
1627 /* SLAB cache for file structures */
1628 kmem_cache_t *filp_cachep;
1630 EXPORT_SYMBOL(d_genocide);
1632 extern void bdev_cache_init(void);
1633 extern void chrdev_init(void);
1635 void __init vfs_caches_init(unsigned long mempages)
1637 unsigned long reserve;
1639 /* Base hash sizes on available memory, with a reserve equal to
1640 150% of current kernel size */
1642 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1643 mempages -= reserve;
1645 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1646 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1648 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
1649 SLAB_HWCACHE_ALIGN|SLAB_PANIC, filp_ctor, filp_dtor);
1651 dcache_init(mempages);
1652 inode_init(mempages);
1653 files_init(mempages);
1659 EXPORT_SYMBOL(d_alloc);
1660 EXPORT_SYMBOL(d_alloc_anon);
1661 EXPORT_SYMBOL(d_alloc_root);
1662 EXPORT_SYMBOL(d_delete);
1663 EXPORT_SYMBOL(d_find_alias);
1664 EXPORT_SYMBOL(d_instantiate);
1665 EXPORT_SYMBOL(d_invalidate);
1666 EXPORT_SYMBOL(d_lookup);
1667 EXPORT_SYMBOL(d_move);
1668 EXPORT_SYMBOL(d_path);
1669 EXPORT_SYMBOL(d_prune_aliases);
1670 EXPORT_SYMBOL(d_rehash);
1671 EXPORT_SYMBOL(d_splice_alias);
1672 EXPORT_SYMBOL(d_validate);
1673 EXPORT_SYMBOL(dget_locked);
1674 EXPORT_SYMBOL(dput);
1675 EXPORT_SYMBOL(find_inode_number);
1676 EXPORT_SYMBOL(have_submounts);
1677 EXPORT_SYMBOL(is_subdir);
1678 EXPORT_SYMBOL(names_cachep);
1679 EXPORT_SYMBOL(shrink_dcache_anon);
1680 EXPORT_SYMBOL(shrink_dcache_parent);
1681 EXPORT_SYMBOL(shrink_dcache_sb);