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/syscalls.h>
18 #include <linux/string.h>
21 #include <linux/fsnotify.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/smp_lock.h>
25 #include <linux/hash.h>
26 #include <linux/cache.h>
27 #include <linux/module.h>
28 #include <linux/mount.h>
29 #include <linux/file.h>
30 #include <asm/uaccess.h>
31 #include <linux/security.h>
32 #include <linux/seqlock.h>
33 #include <linux/swap.h>
34 #include <linux/bootmem.h>
35 #include <linux/vs_limit.h>
39 int sysctl_vfs_cache_pressure __read_mostly = 100;
40 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
42 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
43 static __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
45 EXPORT_SYMBOL(dcache_lock);
47 static struct kmem_cache *dentry_cache __read_mostly;
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 __read_mostly;
63 static unsigned int d_hash_shift __read_mostly;
64 static struct hlist_head *dentry_hashtable __read_mostly;
65 static LIST_HEAD(dentry_unused);
67 /* Statistics gathering. */
68 struct dentry_stat_t dentry_stat = {
72 static void __d_free(struct dentry *dentry)
74 if (dname_external(dentry))
75 kfree(dentry->d_name.name);
76 kmem_cache_free(dentry_cache, dentry);
79 static void d_callback(struct rcu_head *head)
81 struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
86 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
89 static void d_free(struct dentry *dentry)
91 if (dentry->d_op && dentry->d_op->d_release)
92 dentry->d_op->d_release(dentry);
93 /* if dentry was never inserted into hash, immediate free is OK */
94 if (dentry->d_hash.pprev == NULL)
97 call_rcu(&dentry->d_u.d_rcu, d_callback);
101 * Release the dentry's inode, using the filesystem
102 * d_iput() operation if defined.
103 * Called with dcache_lock and per dentry lock held, drops both.
105 static void dentry_iput(struct dentry * dentry)
107 struct inode *inode = dentry->d_inode;
109 dentry->d_inode = NULL;
110 list_del_init(&dentry->d_alias);
111 spin_unlock(&dentry->d_lock);
112 spin_unlock(&dcache_lock);
114 fsnotify_inoderemove(inode);
115 if (dentry->d_op && dentry->d_op->d_iput)
116 dentry->d_op->d_iput(dentry, inode);
120 spin_unlock(&dentry->d_lock);
121 spin_unlock(&dcache_lock);
128 * This is complicated by the fact that we do not want to put
129 * dentries that are no longer on any hash chain on the unused
130 * list: we'd much rather just get rid of them immediately.
132 * However, that implies that we have to traverse the dentry
133 * tree upwards to the parents which might _also_ now be
134 * scheduled for deletion (it may have been only waiting for
135 * its last child to go away).
137 * This tail recursion is done by hand as we don't want to depend
138 * on the compiler to always get this right (gcc generally doesn't).
139 * Real recursion would eat up our stack space.
143 * dput - release a dentry
144 * @dentry: dentry to release
146 * Release a dentry. This will drop the usage count and if appropriate
147 * call the dentry unlink method as well as removing it from the queues and
148 * releasing its resources. If the parent dentries were scheduled for release
149 * they too may now get deleted.
151 * no dcache lock, please.
154 void dput(struct dentry *dentry)
159 vx_dentry_dec(dentry);
161 if (atomic_read(&dentry->d_count) == 1)
163 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
166 spin_lock(&dentry->d_lock);
167 if (atomic_read(&dentry->d_count)) {
168 spin_unlock(&dentry->d_lock);
169 spin_unlock(&dcache_lock);
173 vx_dentry_dec(dentry);
176 * AV: ->d_delete() is _NOT_ allowed to block now.
178 if (dentry->d_op && dentry->d_op->d_delete) {
179 if (dentry->d_op->d_delete(dentry))
182 /* Unreachable? Get rid of it */
183 if (d_unhashed(dentry))
185 if (list_empty(&dentry->d_lru)) {
186 dentry->d_flags |= DCACHE_REFERENCED;
187 list_add(&dentry->d_lru, &dentry_unused);
188 dentry_stat.nr_unused++;
190 spin_unlock(&dentry->d_lock);
191 spin_unlock(&dcache_lock);
198 struct dentry *parent;
200 /* If dentry was on d_lru list
201 * delete it from there
203 if (!list_empty(&dentry->d_lru)) {
204 list_del(&dentry->d_lru);
205 dentry_stat.nr_unused--;
207 list_del(&dentry->d_u.d_child);
208 dentry_stat.nr_dentry--; /* For d_free, below */
209 /*drops the locks, at that point nobody can reach this dentry */
211 parent = dentry->d_parent;
213 if (dentry == parent)
221 * d_invalidate - invalidate a dentry
222 * @dentry: dentry to invalidate
224 * Try to invalidate the dentry if it turns out to be
225 * possible. If there are other dentries that can be
226 * reached through this one we can't delete it and we
227 * return -EBUSY. On success we return 0.
232 int d_invalidate(struct dentry * dentry)
235 * If it's already been dropped, return OK.
237 spin_lock(&dcache_lock);
238 if (d_unhashed(dentry)) {
239 spin_unlock(&dcache_lock);
243 * Check whether to do a partial shrink_dcache
244 * to get rid of unused child entries.
246 if (!list_empty(&dentry->d_subdirs)) {
247 spin_unlock(&dcache_lock);
248 shrink_dcache_parent(dentry);
249 spin_lock(&dcache_lock);
253 * Somebody else still using it?
255 * If it's a directory, we can't drop it
256 * for fear of somebody re-populating it
257 * with children (even though dropping it
258 * would make it unreachable from the root,
259 * we might still populate it if it was a
260 * working directory or similar).
262 spin_lock(&dentry->d_lock);
263 if (atomic_read(&dentry->d_count) > 1) {
264 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
265 spin_unlock(&dentry->d_lock);
266 spin_unlock(&dcache_lock);
272 spin_unlock(&dentry->d_lock);
273 spin_unlock(&dcache_lock);
277 /* This should be called _only_ with dcache_lock held */
279 static inline struct dentry * __dget_locked(struct dentry *dentry)
281 atomic_inc(&dentry->d_count);
282 if (!list_empty(&dentry->d_lru)) {
283 dentry_stat.nr_unused--;
284 list_del_init(&dentry->d_lru);
285 vx_dentry_inc(dentry);
290 struct dentry * dget_locked(struct dentry *dentry)
292 return __dget_locked(dentry);
296 * d_find_alias - grab a hashed alias of inode
297 * @inode: inode in question
298 * @want_discon: flag, used by d_splice_alias, to request
299 * that only a DISCONNECTED alias be returned.
301 * If inode has a hashed alias, or is a directory and has any alias,
302 * acquire the reference to alias and return it. Otherwise return NULL.
303 * Notice that if inode is a directory there can be only one alias and
304 * it can be unhashed only if it has no children, or if it is the root
307 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
308 * any other hashed alias over that one unless @want_discon is set,
309 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
312 static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
314 struct list_head *head, *next, *tmp;
315 struct dentry *alias, *discon_alias=NULL;
317 head = &inode->i_dentry;
318 next = inode->i_dentry.next;
319 while (next != head) {
323 alias = list_entry(tmp, struct dentry, d_alias);
324 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
325 if (IS_ROOT(alias) &&
326 (alias->d_flags & DCACHE_DISCONNECTED))
327 discon_alias = alias;
328 else if (!want_discon) {
329 __dget_locked(alias);
335 __dget_locked(discon_alias);
339 struct dentry * d_find_alias(struct inode *inode)
341 struct dentry *de = NULL;
343 if (!list_empty(&inode->i_dentry)) {
344 spin_lock(&dcache_lock);
345 de = __d_find_alias(inode, 0);
346 spin_unlock(&dcache_lock);
352 * Try to kill dentries associated with this inode.
353 * WARNING: you must own a reference to inode.
355 void d_prune_aliases(struct inode *inode)
357 struct dentry *dentry;
359 spin_lock(&dcache_lock);
360 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
361 spin_lock(&dentry->d_lock);
362 if (!atomic_read(&dentry->d_count)) {
363 __dget_locked(dentry);
365 spin_unlock(&dentry->d_lock);
366 spin_unlock(&dcache_lock);
370 spin_unlock(&dentry->d_lock);
372 spin_unlock(&dcache_lock);
376 * Throw away a dentry - free the inode, dput the parent. This requires that
377 * the LRU list has already been removed.
379 * Called with dcache_lock, drops it and then regains.
380 * Called with dentry->d_lock held, drops it.
382 static void prune_one_dentry(struct dentry * dentry)
384 struct dentry * parent;
387 list_del(&dentry->d_u.d_child);
388 dentry_stat.nr_dentry--; /* For d_free, below */
390 parent = dentry->d_parent;
392 if (parent != dentry)
394 spin_lock(&dcache_lock);
398 * prune_dcache - shrink the dcache
399 * @count: number of entries to try and free
400 * @sb: if given, ignore dentries for other superblocks
401 * which are being unmounted.
403 * Shrink the dcache. This is done when we need
404 * more memory, or simply when we need to unmount
405 * something (at which point we need to unuse
408 * This function may fail to free any resources if
409 * all the dentries are in use.
412 static void prune_dcache(int count, struct super_block *sb)
414 spin_lock(&dcache_lock);
415 for (; count ; count--) {
416 struct dentry *dentry;
417 struct list_head *tmp;
418 struct rw_semaphore *s_umount;
420 cond_resched_lock(&dcache_lock);
422 tmp = dentry_unused.prev;
424 /* Try to find a dentry for this sb, but don't try
425 * too hard, if they aren't near the tail they will
426 * be moved down again soon
429 while (skip && tmp != &dentry_unused &&
430 list_entry(tmp, struct dentry, d_lru)->d_sb != sb) {
435 if (tmp == &dentry_unused)
438 prefetch(dentry_unused.prev);
439 dentry_stat.nr_unused--;
440 dentry = list_entry(tmp, struct dentry, d_lru);
442 spin_lock(&dentry->d_lock);
444 * We found an inuse dentry which was not removed from
445 * dentry_unused because of laziness during lookup. Do not free
446 * it - just keep it off the dentry_unused list.
448 if (atomic_read(&dentry->d_count)) {
449 spin_unlock(&dentry->d_lock);
452 /* If the dentry was recently referenced, don't free it. */
453 if (dentry->d_flags & DCACHE_REFERENCED) {
454 dentry->d_flags &= ~DCACHE_REFERENCED;
455 list_add(&dentry->d_lru, &dentry_unused);
456 dentry_stat.nr_unused++;
457 spin_unlock(&dentry->d_lock);
461 * If the dentry is not DCACHED_REFERENCED, it is time
462 * to remove it from the dcache, provided the super block is
463 * NULL (which means we are trying to reclaim memory)
464 * or this dentry belongs to the same super block that
468 * If this dentry is for "my" filesystem, then I can prune it
469 * without taking the s_umount lock (I already hold it).
471 if (sb && dentry->d_sb == sb) {
472 prune_one_dentry(dentry);
476 * ...otherwise we need to be sure this filesystem isn't being
477 * unmounted, otherwise we could race with
478 * generic_shutdown_super(), and end up holding a reference to
479 * an inode while the filesystem is unmounted.
480 * So we try to get s_umount, and make sure s_root isn't NULL.
481 * (Take a local copy of s_umount to avoid a use-after-free of
484 s_umount = &dentry->d_sb->s_umount;
485 if (down_read_trylock(s_umount)) {
486 if (dentry->d_sb->s_root != NULL) {
487 prune_one_dentry(dentry);
493 spin_unlock(&dentry->d_lock);
495 * Insert dentry at the head of the list as inserting at the
496 * tail leads to a cycle.
498 list_add(&dentry->d_lru, &dentry_unused);
499 dentry_stat.nr_unused++;
501 spin_unlock(&dcache_lock);
505 * Shrink the dcache for the specified super block.
506 * This allows us to unmount a device without disturbing
507 * the dcache for the other devices.
509 * This implementation makes just two traversals of the
510 * unused list. On the first pass we move the selected
511 * dentries to the most recent end, and on the second
512 * pass we free them. The second pass must restart after
513 * each dput(), but since the target dentries are all at
514 * the end, it's really just a single traversal.
518 * shrink_dcache_sb - shrink dcache for a superblock
521 * Shrink the dcache for the specified super block. This
522 * is used to free the dcache before unmounting a file
526 void shrink_dcache_sb(struct super_block * sb)
528 struct list_head *tmp, *next;
529 struct dentry *dentry;
532 * Pass one ... move the dentries for the specified
533 * superblock to the most recent end of the unused list.
535 spin_lock(&dcache_lock);
536 list_for_each_safe(tmp, next, &dentry_unused) {
537 dentry = list_entry(tmp, struct dentry, d_lru);
538 if (dentry->d_sb != sb)
540 list_move(tmp, &dentry_unused);
544 * Pass two ... free the dentries for this superblock.
547 list_for_each_safe(tmp, next, &dentry_unused) {
548 dentry = list_entry(tmp, struct dentry, d_lru);
549 if (dentry->d_sb != sb)
551 dentry_stat.nr_unused--;
553 spin_lock(&dentry->d_lock);
554 if (atomic_read(&dentry->d_count)) {
555 spin_unlock(&dentry->d_lock);
558 prune_one_dentry(dentry);
559 cond_resched_lock(&dcache_lock);
562 spin_unlock(&dcache_lock);
566 * destroy a single subtree of dentries for unmount
567 * - see the comments on shrink_dcache_for_umount() for a description of the
570 static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
572 struct dentry *parent;
573 unsigned detached = 0;
575 BUG_ON(!IS_ROOT(dentry));
577 /* detach this root from the system */
578 spin_lock(&dcache_lock);
579 if (!list_empty(&dentry->d_lru)) {
580 dentry_stat.nr_unused--;
581 list_del_init(&dentry->d_lru);
584 spin_unlock(&dcache_lock);
587 /* descend to the first leaf in the current subtree */
588 while (!list_empty(&dentry->d_subdirs)) {
591 /* this is a branch with children - detach all of them
592 * from the system in one go */
593 spin_lock(&dcache_lock);
594 list_for_each_entry(loop, &dentry->d_subdirs,
596 if (!list_empty(&loop->d_lru)) {
597 dentry_stat.nr_unused--;
598 list_del_init(&loop->d_lru);
602 cond_resched_lock(&dcache_lock);
604 spin_unlock(&dcache_lock);
606 /* move to the first child */
607 dentry = list_entry(dentry->d_subdirs.next,
608 struct dentry, d_u.d_child);
611 /* consume the dentries from this leaf up through its parents
612 * until we find one with children or run out altogether */
616 if (atomic_read(&dentry->d_count) != 0) {
618 "BUG: Dentry %p{i=%lx,n=%s}"
620 " [unmount of %s %s]\n",
623 dentry->d_inode->i_ino : 0UL,
625 atomic_read(&dentry->d_count),
626 dentry->d_sb->s_type->name,
631 parent = dentry->d_parent;
632 if (parent == dentry)
635 atomic_dec(&parent->d_count);
637 list_del(&dentry->d_u.d_child);
640 inode = dentry->d_inode;
642 dentry->d_inode = NULL;
643 list_del_init(&dentry->d_alias);
644 if (dentry->d_op && dentry->d_op->d_iput)
645 dentry->d_op->d_iput(dentry, inode);
652 /* finished when we fall off the top of the tree,
653 * otherwise we ascend to the parent and move to the
654 * next sibling if there is one */
660 } while (list_empty(&dentry->d_subdirs));
662 dentry = list_entry(dentry->d_subdirs.next,
663 struct dentry, d_u.d_child);
666 /* several dentries were freed, need to correct nr_dentry */
667 spin_lock(&dcache_lock);
668 dentry_stat.nr_dentry -= detached;
669 spin_unlock(&dcache_lock);
673 * destroy the dentries attached to a superblock on unmounting
674 * - we don't need to use dentry->d_lock, and only need dcache_lock when
675 * removing the dentry from the system lists and hashes because:
676 * - the superblock is detached from all mountings and open files, so the
677 * dentry trees will not be rearranged by the VFS
678 * - s_umount is write-locked, so the memory pressure shrinker will ignore
679 * any dentries belonging to this superblock that it comes across
680 * - the filesystem itself is no longer permitted to rearrange the dentries
683 void shrink_dcache_for_umount(struct super_block *sb)
685 struct dentry *dentry;
687 if (down_read_trylock(&sb->s_umount))
692 atomic_dec(&dentry->d_count);
693 shrink_dcache_for_umount_subtree(dentry);
695 while (!hlist_empty(&sb->s_anon)) {
696 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
697 shrink_dcache_for_umount_subtree(dentry);
702 * Search for at least 1 mount point in the dentry's subdirs.
703 * We descend to the next level whenever the d_subdirs
704 * list is non-empty and continue searching.
708 * have_submounts - check for mounts over a dentry
709 * @parent: dentry to check.
711 * Return true if the parent or its subdirectories contain
715 int have_submounts(struct dentry *parent)
717 struct dentry *this_parent = parent;
718 struct list_head *next;
720 spin_lock(&dcache_lock);
721 if (d_mountpoint(parent))
724 next = this_parent->d_subdirs.next;
726 while (next != &this_parent->d_subdirs) {
727 struct list_head *tmp = next;
728 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
730 /* Have we found a mount point ? */
731 if (d_mountpoint(dentry))
733 if (!list_empty(&dentry->d_subdirs)) {
734 this_parent = dentry;
739 * All done at this level ... ascend and resume the search.
741 if (this_parent != parent) {
742 next = this_parent->d_u.d_child.next;
743 this_parent = this_parent->d_parent;
746 spin_unlock(&dcache_lock);
747 return 0; /* No mount points found in tree */
749 spin_unlock(&dcache_lock);
754 * Search the dentry child list for the specified parent,
755 * and move any unused dentries to the end of the unused
756 * list for prune_dcache(). We descend to the next level
757 * whenever the d_subdirs list is non-empty and continue
760 * It returns zero iff there are no unused children,
761 * otherwise it returns the number of children moved to
762 * the end of the unused list. This may not be the total
763 * number of unused children, because select_parent can
764 * drop the lock and return early due to latency
767 static int select_parent(struct dentry * parent)
769 struct dentry *this_parent = parent;
770 struct list_head *next;
773 spin_lock(&dcache_lock);
775 next = this_parent->d_subdirs.next;
777 while (next != &this_parent->d_subdirs) {
778 struct list_head *tmp = next;
779 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
782 if (!list_empty(&dentry->d_lru)) {
783 dentry_stat.nr_unused--;
784 list_del_init(&dentry->d_lru);
787 * move only zero ref count dentries to the end
788 * of the unused list for prune_dcache
790 if (!atomic_read(&dentry->d_count)) {
791 list_add_tail(&dentry->d_lru, &dentry_unused);
792 dentry_stat.nr_unused++;
797 * We can return to the caller if we have found some (this
798 * ensures forward progress). We'll be coming back to find
801 if (found && need_resched())
805 * Descend a level if the d_subdirs list is non-empty.
807 if (!list_empty(&dentry->d_subdirs)) {
808 this_parent = dentry;
813 * All done at this level ... ascend and resume the search.
815 if (this_parent != parent) {
816 next = this_parent->d_u.d_child.next;
817 this_parent = this_parent->d_parent;
821 spin_unlock(&dcache_lock);
826 * shrink_dcache_parent - prune dcache
827 * @parent: parent of entries to prune
829 * Prune the dcache to remove unused children of the parent dentry.
832 void shrink_dcache_parent(struct dentry * parent)
836 while ((found = select_parent(parent)) != 0)
837 prune_dcache(found, parent->d_sb);
841 * Scan `nr' dentries and return the number which remain.
843 * We need to avoid reentering the filesystem if the caller is performing a
844 * GFP_NOFS allocation attempt. One example deadlock is:
846 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
847 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
848 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
850 * In this case we return -1 to tell the caller that we baled.
852 static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
855 if (!(gfp_mask & __GFP_FS))
857 prune_dcache(nr, NULL);
859 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
863 * d_alloc - allocate a dcache entry
864 * @parent: parent of entry to allocate
865 * @name: qstr of the name
867 * Allocates a dentry. It returns %NULL if there is insufficient memory
868 * available. On a success the dentry is returned. The name passed in is
869 * copied and the copy passed in may be reused after this call.
872 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
874 struct dentry *dentry;
877 if (!vx_dentry_avail(1))
880 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
884 if (name->len > DNAME_INLINE_LEN-1) {
885 dname = kmalloc(name->len + 1, GFP_KERNEL);
887 kmem_cache_free(dentry_cache, dentry);
891 dname = dentry->d_iname;
893 dentry->d_name.name = dname;
895 dentry->d_name.len = name->len;
896 dentry->d_name.hash = name->hash;
897 memcpy(dname, name->name, name->len);
898 dname[name->len] = 0;
900 atomic_set(&dentry->d_count, 1);
901 dentry->d_flags = DCACHE_UNHASHED;
902 spin_lock_init(&dentry->d_lock);
903 dentry->d_inode = NULL;
904 dentry->d_parent = NULL;
907 dentry->d_fsdata = NULL;
908 dentry->d_mounted = 0;
909 #ifdef CONFIG_PROFILING
910 dentry->d_cookie = NULL;
912 INIT_HLIST_NODE(&dentry->d_hash);
913 INIT_LIST_HEAD(&dentry->d_lru);
914 INIT_LIST_HEAD(&dentry->d_subdirs);
915 INIT_LIST_HEAD(&dentry->d_alias);
918 dentry->d_parent = dget(parent);
919 dentry->d_sb = parent->d_sb;
921 INIT_LIST_HEAD(&dentry->d_u.d_child);
924 spin_lock(&dcache_lock);
926 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
927 dentry_stat.nr_dentry++;
928 vx_dentry_inc(dentry);
929 spin_unlock(&dcache_lock);
934 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
939 q.len = strlen(name);
940 q.hash = full_name_hash(q.name, q.len);
941 return d_alloc(parent, &q);
945 * d_instantiate - fill in inode information for a dentry
946 * @entry: dentry to complete
947 * @inode: inode to attach to this dentry
949 * Fill in inode information in the entry.
951 * This turns negative dentries into productive full members
954 * NOTE! This assumes that the inode count has been incremented
955 * (or otherwise set) by the caller to indicate that it is now
956 * in use by the dcache.
959 void d_instantiate(struct dentry *entry, struct inode * inode)
961 BUG_ON(!list_empty(&entry->d_alias));
962 spin_lock(&dcache_lock);
964 list_add(&entry->d_alias, &inode->i_dentry);
965 entry->d_inode = inode;
966 fsnotify_d_instantiate(entry, inode);
967 spin_unlock(&dcache_lock);
968 security_d_instantiate(entry, inode);
972 * d_instantiate_unique - instantiate a non-aliased dentry
973 * @entry: dentry to instantiate
974 * @inode: inode to attach to this dentry
976 * Fill in inode information in the entry. On success, it returns NULL.
977 * If an unhashed alias of "entry" already exists, then we return the
978 * aliased dentry instead and drop one reference to inode.
980 * Note that in order to avoid conflicts with rename() etc, the caller
981 * had better be holding the parent directory semaphore.
983 * This also assumes that the inode count has been incremented
984 * (or otherwise set) by the caller to indicate that it is now
985 * in use by the dcache.
987 static struct dentry *__d_instantiate_unique(struct dentry *entry,
990 struct dentry *alias;
991 int len = entry->d_name.len;
992 const char *name = entry->d_name.name;
993 unsigned int hash = entry->d_name.hash;
996 entry->d_inode = NULL;
1000 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1001 struct qstr *qstr = &alias->d_name;
1003 if (qstr->hash != hash)
1005 if (alias->d_parent != entry->d_parent)
1007 if (qstr->len != len)
1009 if (memcmp(qstr->name, name, len))
1015 list_add(&entry->d_alias, &inode->i_dentry);
1016 entry->d_inode = inode;
1017 fsnotify_d_instantiate(entry, inode);
1021 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1023 struct dentry *result;
1025 BUG_ON(!list_empty(&entry->d_alias));
1027 spin_lock(&dcache_lock);
1028 result = __d_instantiate_unique(entry, inode);
1029 spin_unlock(&dcache_lock);
1032 security_d_instantiate(entry, inode);
1036 BUG_ON(!d_unhashed(result));
1041 EXPORT_SYMBOL(d_instantiate_unique);
1044 * d_alloc_root - allocate root dentry
1045 * @root_inode: inode to allocate the root for
1047 * Allocate a root ("/") dentry for the inode given. The inode is
1048 * instantiated and returned. %NULL is returned if there is insufficient
1049 * memory or the inode passed is %NULL.
1052 struct dentry * d_alloc_root(struct inode * root_inode)
1054 struct dentry *res = NULL;
1057 static const struct qstr name = { .name = "/", .len = 1 };
1059 res = d_alloc(NULL, &name);
1061 res->d_sb = root_inode->i_sb;
1062 res->d_parent = res;
1063 d_instantiate(res, root_inode);
1069 static inline struct hlist_head *d_hash(struct dentry *parent,
1072 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1073 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1074 return dentry_hashtable + (hash & D_HASHMASK);
1078 * d_alloc_anon - allocate an anonymous dentry
1079 * @inode: inode to allocate the dentry for
1081 * This is similar to d_alloc_root. It is used by filesystems when
1082 * creating a dentry for a given inode, often in the process of
1083 * mapping a filehandle to a dentry. The returned dentry may be
1084 * anonymous, or may have a full name (if the inode was already
1085 * in the cache). The file system may need to make further
1086 * efforts to connect this dentry into the dcache properly.
1088 * When called on a directory inode, we must ensure that
1089 * the inode only ever has one dentry. If a dentry is
1090 * found, that is returned instead of allocating a new one.
1092 * On successful return, the reference to the inode has been transferred
1093 * to the dentry. If %NULL is returned (indicating kmalloc failure),
1094 * the reference on the inode has not been released.
1097 struct dentry * d_alloc_anon(struct inode *inode)
1099 static const struct qstr anonstring = { .name = "" };
1103 if ((res = d_find_alias(inode))) {
1108 tmp = d_alloc(NULL, &anonstring);
1112 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1114 spin_lock(&dcache_lock);
1115 res = __d_find_alias(inode, 0);
1117 /* attach a disconnected dentry */
1120 spin_lock(&res->d_lock);
1121 res->d_sb = inode->i_sb;
1122 res->d_parent = res;
1123 res->d_inode = inode;
1124 res->d_flags |= DCACHE_DISCONNECTED;
1125 res->d_flags &= ~DCACHE_UNHASHED;
1126 list_add(&res->d_alias, &inode->i_dentry);
1127 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
1128 spin_unlock(&res->d_lock);
1130 inode = NULL; /* don't drop reference */
1132 spin_unlock(&dcache_lock);
1143 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1144 * @inode: the inode which may have a disconnected dentry
1145 * @dentry: a negative dentry which we want to point to the inode.
1147 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1148 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1149 * and return it, else simply d_add the inode to the dentry and return NULL.
1151 * This is needed in the lookup routine of any filesystem that is exportable
1152 * (via knfsd) so that we can build dcache paths to directories effectively.
1154 * If a dentry was found and moved, then it is returned. Otherwise NULL
1155 * is returned. This matches the expected return value of ->lookup.
1158 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1160 struct dentry *new = NULL;
1162 if (inode && S_ISDIR(inode->i_mode)) {
1163 spin_lock(&dcache_lock);
1164 new = __d_find_alias(inode, 1);
1166 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1167 fsnotify_d_instantiate(new, inode);
1168 spin_unlock(&dcache_lock);
1169 security_d_instantiate(new, inode);
1171 d_move(new, dentry);
1174 /* d_instantiate takes dcache_lock, so we do it by hand */
1175 list_add(&dentry->d_alias, &inode->i_dentry);
1176 dentry->d_inode = inode;
1177 fsnotify_d_instantiate(dentry, inode);
1178 spin_unlock(&dcache_lock);
1179 security_d_instantiate(dentry, inode);
1183 d_add(dentry, inode);
1189 * d_lookup - search for a dentry
1190 * @parent: parent dentry
1191 * @name: qstr of name we wish to find
1193 * Searches the children of the parent dentry for the name in question. If
1194 * the dentry is found its reference count is incremented and the dentry
1195 * is returned. The caller must use d_put to free the entry when it has
1196 * finished using it. %NULL is returned on failure.
1198 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1199 * Memory barriers are used while updating and doing lockless traversal.
1200 * To avoid races with d_move while rename is happening, d_lock is used.
1202 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1203 * and name pointer in one structure pointed by d_qstr.
1205 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1206 * lookup is going on.
1208 * dentry_unused list is not updated even if lookup finds the required dentry
1209 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1210 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1213 * d_lookup() is protected against the concurrent renames in some unrelated
1214 * directory using the seqlockt_t rename_lock.
1217 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1219 struct dentry * dentry = NULL;
1223 seq = read_seqbegin(&rename_lock);
1224 dentry = __d_lookup(parent, name);
1227 } while (read_seqretry(&rename_lock, seq));
1231 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1233 unsigned int len = name->len;
1234 unsigned int hash = name->hash;
1235 const unsigned char *str = name->name;
1236 struct hlist_head *head = d_hash(parent,hash);
1237 struct dentry *found = NULL;
1238 struct hlist_node *node;
1239 struct dentry *dentry;
1243 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1246 if (dentry->d_name.hash != hash)
1248 if (dentry->d_parent != parent)
1251 spin_lock(&dentry->d_lock);
1254 * Recheck the dentry after taking the lock - d_move may have
1255 * changed things. Don't bother checking the hash because we're
1256 * about to compare the whole name anyway.
1258 if (dentry->d_parent != parent)
1262 * It is safe to compare names since d_move() cannot
1263 * change the qstr (protected by d_lock).
1265 qstr = &dentry->d_name;
1266 if (parent->d_op && parent->d_op->d_compare) {
1267 if (parent->d_op->d_compare(parent, qstr, name))
1270 if (qstr->len != len)
1272 if (memcmp(qstr->name, str, len))
1276 if (!d_unhashed(dentry)) {
1277 atomic_inc(&dentry->d_count);
1278 vx_dentry_inc(dentry);
1281 spin_unlock(&dentry->d_lock);
1284 spin_unlock(&dentry->d_lock);
1292 * d_hash_and_lookup - hash the qstr then search for a dentry
1293 * @dir: Directory to search in
1294 * @name: qstr of name we wish to find
1296 * On hash failure or on lookup failure NULL is returned.
1298 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1300 struct dentry *dentry = NULL;
1303 * Check for a fs-specific hash function. Note that we must
1304 * calculate the standard hash first, as the d_op->d_hash()
1305 * routine may choose to leave the hash value unchanged.
1307 name->hash = full_name_hash(name->name, name->len);
1308 if (dir->d_op && dir->d_op->d_hash) {
1309 if (dir->d_op->d_hash(dir, name) < 0)
1312 dentry = d_lookup(dir, name);
1318 * d_validate - verify dentry provided from insecure source
1319 * @dentry: The dentry alleged to be valid child of @dparent
1320 * @dparent: The parent dentry (known to be valid)
1321 * @hash: Hash of the dentry
1322 * @len: Length of the name
1324 * An insecure source has sent us a dentry, here we verify it and dget() it.
1325 * This is used by ncpfs in its readdir implementation.
1326 * Zero is returned in the dentry is invalid.
1329 int d_validate(struct dentry *dentry, struct dentry *dparent)
1331 struct hlist_head *base;
1332 struct hlist_node *lhp;
1334 /* Check whether the ptr might be valid at all.. */
1335 if (!kmem_ptr_validate(dentry_cache, dentry))
1338 if (dentry->d_parent != dparent)
1341 spin_lock(&dcache_lock);
1342 base = d_hash(dparent, dentry->d_name.hash);
1343 hlist_for_each(lhp,base) {
1344 /* hlist_for_each_entry_rcu() not required for d_hash list
1345 * as it is parsed under dcache_lock
1347 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1348 __dget_locked(dentry);
1349 spin_unlock(&dcache_lock);
1353 spin_unlock(&dcache_lock);
1359 * When a file is deleted, we have two options:
1360 * - turn this dentry into a negative dentry
1361 * - unhash this dentry and free it.
1363 * Usually, we want to just turn this into
1364 * a negative dentry, but if anybody else is
1365 * currently using the dentry or the inode
1366 * we can't do that and we fall back on removing
1367 * it from the hash queues and waiting for
1368 * it to be deleted later when it has no users
1372 * d_delete - delete a dentry
1373 * @dentry: The dentry to delete
1375 * Turn the dentry into a negative dentry if possible, otherwise
1376 * remove it from the hash queues so it can be deleted later
1379 void d_delete(struct dentry * dentry)
1383 * Are we the only user?
1385 spin_lock(&dcache_lock);
1386 spin_lock(&dentry->d_lock);
1387 isdir = S_ISDIR(dentry->d_inode->i_mode);
1388 if (atomic_read(&dentry->d_count) == 1) {
1389 dentry_iput(dentry);
1390 fsnotify_nameremove(dentry, isdir);
1392 /* remove this and other inotify debug checks after 2.6.18 */
1393 dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
1397 if (!d_unhashed(dentry))
1400 spin_unlock(&dentry->d_lock);
1401 spin_unlock(&dcache_lock);
1403 fsnotify_nameremove(dentry, isdir);
1406 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1409 entry->d_flags &= ~DCACHE_UNHASHED;
1410 hlist_add_head_rcu(&entry->d_hash, list);
1413 static void _d_rehash(struct dentry * entry)
1415 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1419 * d_rehash - add an entry back to the hash
1420 * @entry: dentry to add to the hash
1422 * Adds a dentry to the hash according to its name.
1425 void d_rehash(struct dentry * entry)
1427 spin_lock(&dcache_lock);
1428 spin_lock(&entry->d_lock);
1430 spin_unlock(&entry->d_lock);
1431 spin_unlock(&dcache_lock);
1434 #define do_switch(x,y) do { \
1435 __typeof__ (x) __tmp = x; \
1436 x = y; y = __tmp; } while (0)
1439 * When switching names, the actual string doesn't strictly have to
1440 * be preserved in the target - because we're dropping the target
1441 * anyway. As such, we can just do a simple memcpy() to copy over
1442 * the new name before we switch.
1444 * Note that we have to be a lot more careful about getting the hash
1445 * switched - we have to switch the hash value properly even if it
1446 * then no longer matches the actual (corrupted) string of the target.
1447 * The hash value has to match the hash queue that the dentry is on..
1449 static void switch_names(struct dentry *dentry, struct dentry *target)
1451 if (dname_external(target)) {
1452 if (dname_external(dentry)) {
1454 * Both external: swap the pointers
1456 do_switch(target->d_name.name, dentry->d_name.name);
1459 * dentry:internal, target:external. Steal target's
1460 * storage and make target internal.
1462 dentry->d_name.name = target->d_name.name;
1463 target->d_name.name = target->d_iname;
1466 if (dname_external(dentry)) {
1468 * dentry:external, target:internal. Give dentry's
1469 * storage to target and make dentry internal
1471 memcpy(dentry->d_iname, target->d_name.name,
1472 target->d_name.len + 1);
1473 target->d_name.name = dentry->d_name.name;
1474 dentry->d_name.name = dentry->d_iname;
1477 * Both are internal. Just copy target to dentry
1479 memcpy(dentry->d_iname, target->d_name.name,
1480 target->d_name.len + 1);
1486 * We cannibalize "target" when moving dentry on top of it,
1487 * because it's going to be thrown away anyway. We could be more
1488 * polite about it, though.
1490 * This forceful removal will result in ugly /proc output if
1491 * somebody holds a file open that got deleted due to a rename.
1492 * We could be nicer about the deleted file, and let it show
1493 * up under the name it got deleted rather than the name that
1498 * d_move_locked - move a dentry
1499 * @dentry: entry to move
1500 * @target: new dentry
1502 * Update the dcache to reflect the move of a file name. Negative
1503 * dcache entries should not be moved in this way.
1505 static void d_move_locked(struct dentry * dentry, struct dentry * target)
1507 struct hlist_head *list;
1509 if (!dentry->d_inode)
1510 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1512 write_seqlock(&rename_lock);
1514 * XXXX: do we really need to take target->d_lock?
1516 if (target < dentry) {
1517 spin_lock(&target->d_lock);
1518 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1520 spin_lock(&dentry->d_lock);
1521 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
1524 /* Move the dentry to the target hash queue, if on different bucket */
1525 if (dentry->d_flags & DCACHE_UNHASHED)
1526 goto already_unhashed;
1528 hlist_del_rcu(&dentry->d_hash);
1531 list = d_hash(target->d_parent, target->d_name.hash);
1532 __d_rehash(dentry, list);
1534 /* Unhash the target: dput() will then get rid of it */
1537 list_del(&dentry->d_u.d_child);
1538 list_del(&target->d_u.d_child);
1540 /* Switch the names.. */
1541 switch_names(dentry, target);
1542 do_switch(dentry->d_name.len, target->d_name.len);
1543 do_switch(dentry->d_name.hash, target->d_name.hash);
1545 /* ... and switch the parents */
1546 if (IS_ROOT(dentry)) {
1547 dentry->d_parent = target->d_parent;
1548 target->d_parent = target;
1549 INIT_LIST_HEAD(&target->d_u.d_child);
1551 do_switch(dentry->d_parent, target->d_parent);
1553 /* And add them back to the (new) parent lists */
1554 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1557 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1558 spin_unlock(&target->d_lock);
1559 fsnotify_d_move(dentry);
1560 spin_unlock(&dentry->d_lock);
1561 write_sequnlock(&rename_lock);
1565 * d_move - move a dentry
1566 * @dentry: entry to move
1567 * @target: new dentry
1569 * Update the dcache to reflect the move of a file name. Negative
1570 * dcache entries should not be moved in this way.
1573 void d_move(struct dentry * dentry, struct dentry * target)
1575 spin_lock(&dcache_lock);
1576 d_move_locked(dentry, target);
1577 spin_unlock(&dcache_lock);
1581 * Helper that returns 1 if p1 is a parent of p2, else 0
1583 static int d_isparent(struct dentry *p1, struct dentry *p2)
1587 for (p = p2; p->d_parent != p; p = p->d_parent) {
1588 if (p->d_parent == p1)
1595 * This helper attempts to cope with remotely renamed directories
1597 * It assumes that the caller is already holding
1598 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1600 * Note: If ever the locking in lock_rename() changes, then please
1601 * remember to update this too...
1603 * On return, dcache_lock will have been unlocked.
1605 static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1607 struct mutex *m1 = NULL, *m2 = NULL;
1610 /* If alias and dentry share a parent, then no extra locks required */
1611 if (alias->d_parent == dentry->d_parent)
1614 /* Check for loops */
1615 ret = ERR_PTR(-ELOOP);
1616 if (d_isparent(alias, dentry))
1619 /* See lock_rename() */
1620 ret = ERR_PTR(-EBUSY);
1621 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1623 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1624 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1626 m2 = &alias->d_parent->d_inode->i_mutex;
1628 d_move_locked(alias, dentry);
1631 spin_unlock(&dcache_lock);
1640 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1641 * named dentry in place of the dentry to be replaced.
1643 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1645 struct dentry *dparent, *aparent;
1647 switch_names(dentry, anon);
1648 do_switch(dentry->d_name.len, anon->d_name.len);
1649 do_switch(dentry->d_name.hash, anon->d_name.hash);
1651 dparent = dentry->d_parent;
1652 aparent = anon->d_parent;
1654 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1655 list_del(&dentry->d_u.d_child);
1656 if (!IS_ROOT(dentry))
1657 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1659 INIT_LIST_HEAD(&dentry->d_u.d_child);
1661 anon->d_parent = (dparent == dentry) ? anon : dparent;
1662 list_del(&anon->d_u.d_child);
1664 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1666 INIT_LIST_HEAD(&anon->d_u.d_child);
1668 anon->d_flags &= ~DCACHE_DISCONNECTED;
1672 * d_materialise_unique - introduce an inode into the tree
1673 * @dentry: candidate dentry
1674 * @inode: inode to bind to the dentry, to which aliases may be attached
1676 * Introduces an dentry into the tree, substituting an extant disconnected
1677 * root directory alias in its place if there is one
1679 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1681 struct dentry *actual;
1683 BUG_ON(!d_unhashed(dentry));
1685 spin_lock(&dcache_lock);
1689 dentry->d_inode = NULL;
1693 if (S_ISDIR(inode->i_mode)) {
1694 struct dentry *alias;
1696 /* Does an aliased dentry already exist? */
1697 alias = __d_find_alias(inode, 0);
1700 /* Is this an anonymous mountpoint that we could splice
1702 if (IS_ROOT(alias)) {
1703 spin_lock(&alias->d_lock);
1704 __d_materialise_dentry(dentry, alias);
1708 /* Nope, but we must(!) avoid directory aliasing */
1709 actual = __d_unalias(dentry, alias);
1716 /* Add a unique reference */
1717 actual = __d_instantiate_unique(dentry, inode);
1720 else if (unlikely(!d_unhashed(actual)))
1721 goto shouldnt_be_hashed;
1724 spin_lock(&actual->d_lock);
1727 spin_unlock(&actual->d_lock);
1728 spin_unlock(&dcache_lock);
1730 if (actual == dentry) {
1731 security_d_instantiate(dentry, inode);
1739 spin_unlock(&dcache_lock);
1741 goto shouldnt_be_hashed;
1745 * d_path - return the path of a dentry
1746 * @dentry: dentry to report
1747 * @vfsmnt: vfsmnt to which the dentry belongs
1748 * @root: root dentry
1749 * @rootmnt: vfsmnt to which the root dentry belongs
1750 * @buffer: buffer to return value in
1751 * @buflen: buffer length
1753 * Convert a dentry into an ASCII path name. If the entry has been deleted
1754 * the string " (deleted)" is appended. Note that this is ambiguous.
1756 * Returns the buffer or an error code if the path was too long.
1758 * "buflen" should be positive. Caller holds the dcache_lock.
1760 static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1761 struct dentry *root, struct vfsmount *rootmnt,
1762 char *buffer, int buflen)
1764 char * end = buffer+buflen;
1770 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1775 memcpy(end, " (deleted)", 10);
1785 struct dentry * parent;
1787 if (dentry == root && vfsmnt == rootmnt)
1789 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1791 spin_lock(&vfsmount_lock);
1792 if (vfsmnt->mnt_parent == vfsmnt) {
1793 spin_unlock(&vfsmount_lock);
1796 dentry = vfsmnt->mnt_mountpoint;
1797 vfsmnt = vfsmnt->mnt_parent;
1798 spin_unlock(&vfsmount_lock);
1801 parent = dentry->d_parent;
1803 namelen = dentry->d_name.len;
1804 buflen -= namelen + 1;
1808 memcpy(end, dentry->d_name.name, namelen);
1817 namelen = dentry->d_name.len;
1821 retval -= namelen-1; /* hit the slash */
1822 memcpy(retval, dentry->d_name.name, namelen);
1825 return ERR_PTR(-ENAMETOOLONG);
1828 /* write full pathname into buffer and return start of pathname */
1829 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1830 char *buf, int buflen)
1833 struct vfsmount *rootmnt;
1834 struct dentry *root;
1836 read_lock(¤t->fs->lock);
1837 rootmnt = mntget(current->fs->rootmnt);
1838 root = dget(current->fs->root);
1839 read_unlock(¤t->fs->lock);
1840 spin_lock(&dcache_lock);
1841 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1842 spin_unlock(&dcache_lock);
1849 * NOTE! The user-level library version returns a
1850 * character pointer. The kernel system call just
1851 * returns the length of the buffer filled (which
1852 * includes the ending '\0' character), or a negative
1853 * error value. So libc would do something like
1855 * char *getcwd(char * buf, size_t size)
1859 * retval = sys_getcwd(buf, size);
1866 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1869 struct vfsmount *pwdmnt, *rootmnt;
1870 struct dentry *pwd, *root;
1871 char *page = (char *) __get_free_page(GFP_USER);
1876 read_lock(¤t->fs->lock);
1877 pwdmnt = mntget(current->fs->pwdmnt);
1878 pwd = dget(current->fs->pwd);
1879 rootmnt = mntget(current->fs->rootmnt);
1880 root = dget(current->fs->root);
1881 read_unlock(¤t->fs->lock);
1884 /* Has the current directory has been unlinked? */
1885 spin_lock(&dcache_lock);
1886 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1890 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1891 spin_unlock(&dcache_lock);
1893 error = PTR_ERR(cwd);
1898 len = PAGE_SIZE + page - cwd;
1901 if (copy_to_user(buf, cwd, len))
1905 spin_unlock(&dcache_lock);
1912 free_page((unsigned long) page);
1917 * Test whether new_dentry is a subdirectory of old_dentry.
1919 * Trivially implemented using the dcache structure
1923 * is_subdir - is new dentry a subdirectory of old_dentry
1924 * @new_dentry: new dentry
1925 * @old_dentry: old dentry
1927 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1928 * Returns 0 otherwise.
1929 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1932 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1935 struct dentry * saved = new_dentry;
1938 /* need rcu_readlock to protect against the d_parent trashing due to
1943 /* for restarting inner loop in case of seq retry */
1946 seq = read_seqbegin(&rename_lock);
1948 if (new_dentry != old_dentry) {
1949 struct dentry * parent = new_dentry->d_parent;
1950 if (parent == new_dentry)
1952 new_dentry = parent;
1958 } while (read_seqretry(&rename_lock, seq));
1964 void d_genocide(struct dentry *root)
1966 struct dentry *this_parent = root;
1967 struct list_head *next;
1969 spin_lock(&dcache_lock);
1971 next = this_parent->d_subdirs.next;
1973 while (next != &this_parent->d_subdirs) {
1974 struct list_head *tmp = next;
1975 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1977 if (d_unhashed(dentry)||!dentry->d_inode)
1979 if (!list_empty(&dentry->d_subdirs)) {
1980 this_parent = dentry;
1983 atomic_dec(&dentry->d_count);
1985 if (this_parent != root) {
1986 next = this_parent->d_u.d_child.next;
1987 atomic_dec(&this_parent->d_count);
1988 this_parent = this_parent->d_parent;
1991 spin_unlock(&dcache_lock);
1995 * find_inode_number - check for dentry with name
1996 * @dir: directory to check
1997 * @name: Name to find.
1999 * Check whether a dentry already exists for the given name,
2000 * and return the inode number if it has an inode. Otherwise
2003 * This routine is used to post-process directory listings for
2004 * filesystems using synthetic inode numbers, and is necessary
2005 * to keep getcwd() working.
2008 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2010 struct dentry * dentry;
2013 dentry = d_hash_and_lookup(dir, name);
2015 if (dentry->d_inode)
2016 ino = dentry->d_inode->i_ino;
2022 static __initdata unsigned long dhash_entries;
2023 static int __init set_dhash_entries(char *str)
2027 dhash_entries = simple_strtoul(str, &str, 0);
2030 __setup("dhash_entries=", set_dhash_entries);
2032 static void __init dcache_init_early(void)
2036 /* If hashes are distributed across NUMA nodes, defer
2037 * hash allocation until vmalloc space is available.
2043 alloc_large_system_hash("Dentry cache",
2044 sizeof(struct hlist_head),
2052 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2053 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2056 static void __init dcache_init(unsigned long mempages)
2061 * A constructor could be added for stable state like the lists,
2062 * but it is probably not worth it because of the cache nature
2065 dentry_cache = kmem_cache_create("dentry_cache",
2066 sizeof(struct dentry),
2068 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
2072 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
2074 /* Hash may have been set up in dcache_init_early */
2079 alloc_large_system_hash("Dentry cache",
2080 sizeof(struct hlist_head),
2088 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2089 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2092 /* SLAB cache for __getname() consumers */
2093 struct kmem_cache *names_cachep __read_mostly;
2095 /* SLAB cache for file structures */
2096 struct kmem_cache *filp_cachep __read_mostly;
2098 EXPORT_SYMBOL(d_genocide);
2100 void __init vfs_caches_init_early(void)
2102 dcache_init_early();
2106 void __init vfs_caches_init(unsigned long mempages)
2108 unsigned long reserve;
2110 /* Base hash sizes on available memory, with a reserve equal to
2111 150% of current kernel size */
2113 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2114 mempages -= reserve;
2116 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2117 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
2119 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
2120 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
2122 dcache_init(mempages);
2123 inode_init(mempages);
2124 files_init(mempages);
2130 EXPORT_SYMBOL(d_alloc);
2131 EXPORT_SYMBOL(d_alloc_anon);
2132 EXPORT_SYMBOL(d_alloc_root);
2133 EXPORT_SYMBOL(d_delete);
2134 EXPORT_SYMBOL(d_find_alias);
2135 EXPORT_SYMBOL(d_instantiate);
2136 EXPORT_SYMBOL(d_invalidate);
2137 EXPORT_SYMBOL(d_lookup);
2138 EXPORT_SYMBOL(d_move);
2139 EXPORT_SYMBOL_GPL(d_materialise_unique);
2140 EXPORT_SYMBOL(d_path);
2141 EXPORT_SYMBOL(d_prune_aliases);
2142 EXPORT_SYMBOL(d_rehash);
2143 EXPORT_SYMBOL(d_splice_alias);
2144 EXPORT_SYMBOL(d_validate);
2145 EXPORT_SYMBOL(dget_locked);
2146 EXPORT_SYMBOL(dput);
2147 EXPORT_SYMBOL(find_inode_number);
2148 EXPORT_SYMBOL(have_submounts);
2149 EXPORT_SYMBOL(names_cachep);
2150 EXPORT_SYMBOL(shrink_dcache_parent);
2151 EXPORT_SYMBOL(shrink_dcache_sb);