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>
20 #include <linux/fdtable.h>
22 #include <linux/fsnotify.h>
23 #include <linux/slab.h>
24 #include <linux/init.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>
38 int sysctl_vfs_cache_pressure __read_mostly = 100;
39 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
41 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
42 __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
44 EXPORT_SYMBOL(dcache_lock);
46 static struct kmem_cache *dentry_cache __read_mostly;
48 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
51 * This is the single most critical data structure when it comes
52 * to the dcache: the hashtable for lookups. Somebody should try
53 * to make this good - I've just made it work.
55 * This hash-function tries to avoid losing too many bits of hash
56 * information, yet avoid using a prime hash-size or similar.
58 #define D_HASHBITS d_hash_shift
59 #define D_HASHMASK d_hash_mask
61 static unsigned int d_hash_mask __read_mostly;
62 static unsigned int d_hash_shift __read_mostly;
63 static struct hlist_head *dentry_hashtable __read_mostly;
65 /* Statistics gathering. */
66 struct dentry_stat_t dentry_stat = {
70 static void __d_free(struct dentry *dentry)
72 if (dname_external(dentry))
73 kfree(dentry->d_name.name);
74 kmem_cache_free(dentry_cache, dentry);
77 static void d_callback(struct rcu_head *head)
79 struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
84 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
87 static void d_free(struct dentry *dentry)
89 if (dentry->d_op && dentry->d_op->d_release)
90 dentry->d_op->d_release(dentry);
91 /* if dentry was never inserted into hash, immediate free is OK */
92 if (hlist_unhashed(&dentry->d_hash))
95 call_rcu(&dentry->d_u.d_rcu, d_callback);
99 * Release the dentry's inode, using the filesystem
100 * d_iput() operation if defined.
102 static void dentry_iput(struct dentry * dentry)
103 __releases(dentry->d_lock)
104 __releases(dcache_lock)
106 struct inode *inode = dentry->d_inode;
108 dentry->d_inode = NULL;
109 list_del_init(&dentry->d_alias);
110 spin_unlock(&dentry->d_lock);
111 spin_unlock(&dcache_lock);
113 fsnotify_inoderemove(inode);
114 if (dentry->d_op && dentry->d_op->d_iput)
115 dentry->d_op->d_iput(dentry, inode);
119 spin_unlock(&dentry->d_lock);
120 spin_unlock(&dcache_lock);
125 * dentry_lru_(add|add_tail|del|del_init) must be called with dcache_lock held.
127 static void dentry_lru_add(struct dentry *dentry)
129 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
130 dentry->d_sb->s_nr_dentry_unused++;
131 dentry_stat.nr_unused++;
134 static void dentry_lru_add_tail(struct dentry *dentry)
136 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
137 dentry->d_sb->s_nr_dentry_unused++;
138 dentry_stat.nr_unused++;
141 static void dentry_lru_del(struct dentry *dentry)
143 if (!list_empty(&dentry->d_lru)) {
144 list_del(&dentry->d_lru);
145 dentry->d_sb->s_nr_dentry_unused--;
146 dentry_stat.nr_unused--;
150 static void dentry_lru_del_init(struct dentry *dentry)
152 if (likely(!list_empty(&dentry->d_lru))) {
153 list_del_init(&dentry->d_lru);
154 dentry->d_sb->s_nr_dentry_unused--;
155 dentry_stat.nr_unused--;
160 * d_kill - kill dentry and return parent
161 * @dentry: dentry to kill
163 * The dentry must already be unhashed and removed from the LRU.
165 * If this is the root of the dentry tree, return NULL.
167 static struct dentry *d_kill(struct dentry *dentry)
168 __releases(dentry->d_lock)
169 __releases(dcache_lock)
171 struct dentry *parent;
173 list_del(&dentry->d_u.d_child);
174 dentry_stat.nr_dentry--; /* For d_free, below */
175 /*drops the locks, at that point nobody can reach this dentry */
177 parent = dentry->d_parent;
179 return dentry == parent ? NULL : parent;
185 * This is complicated by the fact that we do not want to put
186 * dentries that are no longer on any hash chain on the unused
187 * list: we'd much rather just get rid of them immediately.
189 * However, that implies that we have to traverse the dentry
190 * tree upwards to the parents which might _also_ now be
191 * scheduled for deletion (it may have been only waiting for
192 * its last child to go away).
194 * This tail recursion is done by hand as we don't want to depend
195 * on the compiler to always get this right (gcc generally doesn't).
196 * Real recursion would eat up our stack space.
200 * dput - release a dentry
201 * @dentry: dentry to release
203 * Release a dentry. This will drop the usage count and if appropriate
204 * call the dentry unlink method as well as removing it from the queues and
205 * releasing its resources. If the parent dentries were scheduled for release
206 * they too may now get deleted.
208 * no dcache lock, please.
211 void dput(struct dentry *dentry)
217 if (atomic_read(&dentry->d_count) == 1)
219 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
222 spin_lock(&dentry->d_lock);
223 if (atomic_read(&dentry->d_count)) {
224 spin_unlock(&dentry->d_lock);
225 spin_unlock(&dcache_lock);
230 * AV: ->d_delete() is _NOT_ allowed to block now.
232 if (dentry->d_op && dentry->d_op->d_delete) {
233 if (dentry->d_op->d_delete(dentry))
236 /* Unreachable? Get rid of it */
237 if (d_unhashed(dentry))
239 if (list_empty(&dentry->d_lru)) {
240 dentry->d_flags |= DCACHE_REFERENCED;
241 dentry_lru_add(dentry);
243 spin_unlock(&dentry->d_lock);
244 spin_unlock(&dcache_lock);
250 /* if dentry was on the d_lru list delete it from there */
251 dentry_lru_del(dentry);
252 dentry = d_kill(dentry);
258 * d_invalidate - invalidate a dentry
259 * @dentry: dentry to invalidate
261 * Try to invalidate the dentry if it turns out to be
262 * possible. If there are other dentries that can be
263 * reached through this one we can't delete it and we
264 * return -EBUSY. On success we return 0.
269 int d_invalidate(struct dentry * dentry)
272 * If it's already been dropped, return OK.
274 spin_lock(&dcache_lock);
275 if (d_unhashed(dentry)) {
276 spin_unlock(&dcache_lock);
280 * Check whether to do a partial shrink_dcache
281 * to get rid of unused child entries.
283 if (!list_empty(&dentry->d_subdirs)) {
284 spin_unlock(&dcache_lock);
285 shrink_dcache_parent(dentry);
286 spin_lock(&dcache_lock);
290 * Somebody else still using it?
292 * If it's a directory, we can't drop it
293 * for fear of somebody re-populating it
294 * with children (even though dropping it
295 * would make it unreachable from the root,
296 * we might still populate it if it was a
297 * working directory or similar).
299 spin_lock(&dentry->d_lock);
300 if (atomic_read(&dentry->d_count) > 1) {
301 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
302 spin_unlock(&dentry->d_lock);
303 spin_unlock(&dcache_lock);
309 spin_unlock(&dentry->d_lock);
310 spin_unlock(&dcache_lock);
314 /* This should be called _only_ with dcache_lock held */
316 static inline struct dentry * __dget_locked(struct dentry *dentry)
318 atomic_inc(&dentry->d_count);
319 dentry_lru_del_init(dentry);
323 struct dentry * dget_locked(struct dentry *dentry)
325 return __dget_locked(dentry);
329 * d_find_alias - grab a hashed alias of inode
330 * @inode: inode in question
331 * @want_discon: flag, used by d_splice_alias, to request
332 * that only a DISCONNECTED alias be returned.
334 * If inode has a hashed alias, or is a directory and has any alias,
335 * acquire the reference to alias and return it. Otherwise return NULL.
336 * Notice that if inode is a directory there can be only one alias and
337 * it can be unhashed only if it has no children, or if it is the root
340 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
341 * any other hashed alias over that one unless @want_discon is set,
342 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
345 static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
347 struct list_head *head, *next, *tmp;
348 struct dentry *alias, *discon_alias=NULL;
350 head = &inode->i_dentry;
351 next = inode->i_dentry.next;
352 while (next != head) {
356 alias = list_entry(tmp, struct dentry, d_alias);
357 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
358 if (IS_ROOT(alias) &&
359 (alias->d_flags & DCACHE_DISCONNECTED))
360 discon_alias = alias;
361 else if (!want_discon) {
362 __dget_locked(alias);
368 __dget_locked(discon_alias);
372 struct dentry * d_find_alias(struct inode *inode)
374 struct dentry *de = NULL;
376 if (!list_empty(&inode->i_dentry)) {
377 spin_lock(&dcache_lock);
378 de = __d_find_alias(inode, 0);
379 spin_unlock(&dcache_lock);
385 * Try to kill dentries associated with this inode.
386 * WARNING: you must own a reference to inode.
388 void d_prune_aliases(struct inode *inode)
390 struct dentry *dentry;
392 spin_lock(&dcache_lock);
393 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
394 spin_lock(&dentry->d_lock);
395 if (!atomic_read(&dentry->d_count)) {
396 __dget_locked(dentry);
398 spin_unlock(&dentry->d_lock);
399 spin_unlock(&dcache_lock);
403 spin_unlock(&dentry->d_lock);
405 spin_unlock(&dcache_lock);
409 * Throw away a dentry - free the inode, dput the parent. This requires that
410 * the LRU list has already been removed.
412 * Try to prune ancestors as well. This is necessary to prevent
413 * quadratic behavior of shrink_dcache_parent(), but is also expected
414 * to be beneficial in reducing dentry cache fragmentation.
416 static void prune_one_dentry(struct dentry * dentry)
417 __releases(dentry->d_lock)
418 __releases(dcache_lock)
419 __acquires(dcache_lock)
422 dentry = d_kill(dentry);
425 * Prune ancestors. Locking is simpler than in dput(),
426 * because dcache_lock needs to be taken anyway.
428 spin_lock(&dcache_lock);
430 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
433 if (dentry->d_op && dentry->d_op->d_delete)
434 dentry->d_op->d_delete(dentry);
435 dentry_lru_del_init(dentry);
437 dentry = d_kill(dentry);
438 spin_lock(&dcache_lock);
443 * Shrink the dentry LRU on a given superblock.
444 * @sb : superblock to shrink dentry LRU.
445 * @count: If count is NULL, we prune all dentries on superblock.
446 * @flags: If flags is non-zero, we need to do special processing based on
447 * which flags are set. This means we don't need to maintain multiple
448 * similar copies of this loop.
450 static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
452 LIST_HEAD(referenced);
454 struct dentry *dentry;
458 BUG_ON((flags & DCACHE_REFERENCED) && count == NULL);
459 spin_lock(&dcache_lock);
461 /* called from prune_dcache() and shrink_dcache_parent() */
465 list_splice_init(&sb->s_dentry_lru, &tmp);
467 while (!list_empty(&sb->s_dentry_lru)) {
468 dentry = list_entry(sb->s_dentry_lru.prev,
469 struct dentry, d_lru);
470 BUG_ON(dentry->d_sb != sb);
472 spin_lock(&dentry->d_lock);
474 * If we are honouring the DCACHE_REFERENCED flag and
475 * the dentry has this flag set, don't free it. Clear
476 * the flag and put it back on the LRU.
478 if ((flags & DCACHE_REFERENCED)
479 && (dentry->d_flags & DCACHE_REFERENCED)) {
480 dentry->d_flags &= ~DCACHE_REFERENCED;
481 list_move_tail(&dentry->d_lru, &referenced);
482 spin_unlock(&dentry->d_lock);
484 list_move_tail(&dentry->d_lru, &tmp);
485 spin_unlock(&dentry->d_lock);
490 cond_resched_lock(&dcache_lock);
493 while (!list_empty(&tmp)) {
494 dentry = list_entry(tmp.prev, struct dentry, d_lru);
495 dentry_lru_del_init(dentry);
496 spin_lock(&dentry->d_lock);
498 * We found an inuse dentry which was not removed from
499 * the LRU because of laziness during lookup. Do not free
500 * it - just keep it off the LRU list.
502 if (atomic_read(&dentry->d_count)) {
503 spin_unlock(&dentry->d_lock);
506 prune_one_dentry(dentry);
507 /* dentry->d_lock was dropped in prune_one_dentry() */
508 cond_resched_lock(&dcache_lock);
510 if (count == NULL && !list_empty(&sb->s_dentry_lru))
514 if (!list_empty(&referenced))
515 list_splice(&referenced, &sb->s_dentry_lru);
516 spin_unlock(&dcache_lock);
520 * prune_dcache - shrink the dcache
521 * @count: number of entries to try to free
523 * Shrink the dcache. This is done when we need more memory, or simply when we
524 * need to unmount something (at which point we need to unuse all dentries).
526 * This function may fail to free any resources if all the dentries are in use.
528 static void prune_dcache(int count)
530 struct super_block *sb;
532 int unused = dentry_stat.nr_unused;
536 if (unused == 0 || count == 0)
538 spin_lock(&dcache_lock);
543 prune_ratio = unused / count;
545 list_for_each_entry(sb, &super_blocks, s_list) {
546 if (sb->s_nr_dentry_unused == 0)
549 /* Now, we reclaim unused dentrins with fairness.
550 * We reclaim them same percentage from each superblock.
551 * We calculate number of dentries to scan on this sb
552 * as follows, but the implementation is arranged to avoid
554 * number of dentries to scan on this sb =
555 * count * (number of dentries on this sb /
556 * number of dentries in the machine)
558 spin_unlock(&sb_lock);
559 if (prune_ratio != 1)
560 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
562 w_count = sb->s_nr_dentry_unused;
565 * We need to be sure this filesystem isn't being unmounted,
566 * otherwise we could race with generic_shutdown_super(), and
567 * end up holding a reference to an inode while the filesystem
568 * is unmounted. So we try to get s_umount, and make sure
571 if (down_read_trylock(&sb->s_umount)) {
572 if ((sb->s_root != NULL) &&
573 (!list_empty(&sb->s_dentry_lru))) {
574 spin_unlock(&dcache_lock);
575 __shrink_dcache_sb(sb, &w_count,
578 spin_lock(&dcache_lock);
580 up_read(&sb->s_umount);
585 * restart only when sb is no longer on the list and
586 * we have more work to do.
588 if (__put_super_and_need_restart(sb) && count > 0) {
589 spin_unlock(&sb_lock);
593 spin_unlock(&sb_lock);
594 spin_unlock(&dcache_lock);
598 * shrink_dcache_sb - shrink dcache for a superblock
601 * Shrink the dcache for the specified super block. This
602 * is used to free the dcache before unmounting a file
605 void shrink_dcache_sb(struct super_block * sb)
607 __shrink_dcache_sb(sb, NULL, 0);
611 * destroy a single subtree of dentries for unmount
612 * - see the comments on shrink_dcache_for_umount() for a description of the
615 static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
617 struct dentry *parent;
618 unsigned detached = 0;
620 BUG_ON(!IS_ROOT(dentry));
622 /* detach this root from the system */
623 spin_lock(&dcache_lock);
624 dentry_lru_del_init(dentry);
626 spin_unlock(&dcache_lock);
629 /* descend to the first leaf in the current subtree */
630 while (!list_empty(&dentry->d_subdirs)) {
633 /* this is a branch with children - detach all of them
634 * from the system in one go */
635 spin_lock(&dcache_lock);
636 list_for_each_entry(loop, &dentry->d_subdirs,
638 dentry_lru_del_init(loop);
640 cond_resched_lock(&dcache_lock);
642 spin_unlock(&dcache_lock);
644 /* move to the first child */
645 dentry = list_entry(dentry->d_subdirs.next,
646 struct dentry, d_u.d_child);
649 /* consume the dentries from this leaf up through its parents
650 * until we find one with children or run out altogether */
654 if (atomic_read(&dentry->d_count) != 0) {
656 "BUG: Dentry %p{i=%lx,n=%s}"
658 " [unmount of %s %s]\n",
661 dentry->d_inode->i_ino : 0UL,
663 atomic_read(&dentry->d_count),
664 dentry->d_sb->s_type->name,
669 parent = dentry->d_parent;
670 if (parent == dentry)
673 atomic_dec(&parent->d_count);
675 list_del(&dentry->d_u.d_child);
678 inode = dentry->d_inode;
680 dentry->d_inode = NULL;
681 list_del_init(&dentry->d_alias);
682 if (dentry->d_op && dentry->d_op->d_iput)
683 dentry->d_op->d_iput(dentry, inode);
690 /* finished when we fall off the top of the tree,
691 * otherwise we ascend to the parent and move to the
692 * next sibling if there is one */
698 } while (list_empty(&dentry->d_subdirs));
700 dentry = list_entry(dentry->d_subdirs.next,
701 struct dentry, d_u.d_child);
704 /* several dentries were freed, need to correct nr_dentry */
705 spin_lock(&dcache_lock);
706 dentry_stat.nr_dentry -= detached;
707 spin_unlock(&dcache_lock);
711 * destroy the dentries attached to a superblock on unmounting
712 * - we don't need to use dentry->d_lock, and only need dcache_lock when
713 * removing the dentry from the system lists and hashes because:
714 * - the superblock is detached from all mountings and open files, so the
715 * dentry trees will not be rearranged by the VFS
716 * - s_umount is write-locked, so the memory pressure shrinker will ignore
717 * any dentries belonging to this superblock that it comes across
718 * - the filesystem itself is no longer permitted to rearrange the dentries
721 void shrink_dcache_for_umount(struct super_block *sb)
723 struct dentry *dentry;
725 if (down_read_trylock(&sb->s_umount))
730 atomic_dec(&dentry->d_count);
731 shrink_dcache_for_umount_subtree(dentry);
733 while (!hlist_empty(&sb->s_anon)) {
734 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
735 shrink_dcache_for_umount_subtree(dentry);
740 * Search for at least 1 mount point in the dentry's subdirs.
741 * We descend to the next level whenever the d_subdirs
742 * list is non-empty and continue searching.
746 * have_submounts - check for mounts over a dentry
747 * @parent: dentry to check.
749 * Return true if the parent or its subdirectories contain
753 int have_submounts(struct dentry *parent)
755 struct dentry *this_parent = parent;
756 struct list_head *next;
758 spin_lock(&dcache_lock);
759 if (d_mountpoint(parent))
762 next = this_parent->d_subdirs.next;
764 while (next != &this_parent->d_subdirs) {
765 struct list_head *tmp = next;
766 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
768 /* Have we found a mount point ? */
769 if (d_mountpoint(dentry))
771 if (!list_empty(&dentry->d_subdirs)) {
772 this_parent = dentry;
777 * All done at this level ... ascend and resume the search.
779 if (this_parent != parent) {
780 next = this_parent->d_u.d_child.next;
781 this_parent = this_parent->d_parent;
784 spin_unlock(&dcache_lock);
785 return 0; /* No mount points found in tree */
787 spin_unlock(&dcache_lock);
792 * Search the dentry child list for the specified parent,
793 * and move any unused dentries to the end of the unused
794 * list for prune_dcache(). We descend to the next level
795 * whenever the d_subdirs list is non-empty and continue
798 * It returns zero iff there are no unused children,
799 * otherwise it returns the number of children moved to
800 * the end of the unused list. This may not be the total
801 * number of unused children, because select_parent can
802 * drop the lock and return early due to latency
805 static int select_parent(struct dentry * parent)
807 struct dentry *this_parent = parent;
808 struct list_head *next;
811 spin_lock(&dcache_lock);
813 next = this_parent->d_subdirs.next;
815 while (next != &this_parent->d_subdirs) {
816 struct list_head *tmp = next;
817 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
820 dentry_lru_del_init(dentry);
822 * move only zero ref count dentries to the end
823 * of the unused list for prune_dcache
825 if (!atomic_read(&dentry->d_count)) {
826 dentry_lru_add_tail(dentry);
831 * We can return to the caller if we have found some (this
832 * ensures forward progress). We'll be coming back to find
835 if (found && need_resched())
839 * Descend a level if the d_subdirs list is non-empty.
841 if (!list_empty(&dentry->d_subdirs)) {
842 this_parent = dentry;
847 * All done at this level ... ascend and resume the search.
849 if (this_parent != parent) {
850 next = this_parent->d_u.d_child.next;
851 this_parent = this_parent->d_parent;
855 spin_unlock(&dcache_lock);
860 * shrink_dcache_parent - prune dcache
861 * @parent: parent of entries to prune
863 * Prune the dcache to remove unused children of the parent dentry.
866 void shrink_dcache_parent(struct dentry * parent)
868 struct super_block *sb = parent->d_sb;
871 while ((found = select_parent(parent)) != 0)
872 __shrink_dcache_sb(sb, &found, 0);
876 * Scan `nr' dentries and return the number which remain.
878 * We need to avoid reentering the filesystem if the caller is performing a
879 * GFP_NOFS allocation attempt. One example deadlock is:
881 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
882 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
883 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
885 * In this case we return -1 to tell the caller that we baled.
887 static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
890 if (!(gfp_mask & __GFP_FS))
894 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
897 static struct shrinker dcache_shrinker = {
898 .shrink = shrink_dcache_memory,
899 .seeks = DEFAULT_SEEKS,
903 * d_alloc - allocate a dcache entry
904 * @parent: parent of entry to allocate
905 * @name: qstr of the name
907 * Allocates a dentry. It returns %NULL if there is insufficient memory
908 * available. On a success the dentry is returned. The name passed in is
909 * copied and the copy passed in may be reused after this call.
912 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
914 struct dentry *dentry;
917 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
921 if (name->len > DNAME_INLINE_LEN-1) {
922 dname = kmalloc(name->len + 1, GFP_KERNEL);
924 kmem_cache_free(dentry_cache, dentry);
928 dname = dentry->d_iname;
930 dentry->d_name.name = dname;
932 dentry->d_name.len = name->len;
933 dentry->d_name.hash = name->hash;
934 memcpy(dname, name->name, name->len);
935 dname[name->len] = 0;
937 atomic_set(&dentry->d_count, 1);
938 dentry->d_flags = DCACHE_UNHASHED;
939 spin_lock_init(&dentry->d_lock);
940 dentry->d_inode = NULL;
941 dentry->d_parent = NULL;
944 dentry->d_fsdata = NULL;
945 dentry->d_mounted = 0;
946 #ifdef CONFIG_PROFILING
947 dentry->d_cookie = NULL;
949 INIT_HLIST_NODE(&dentry->d_hash);
950 INIT_LIST_HEAD(&dentry->d_lru);
951 INIT_LIST_HEAD(&dentry->d_subdirs);
952 INIT_LIST_HEAD(&dentry->d_alias);
955 dentry->d_parent = dget(parent);
956 dentry->d_sb = parent->d_sb;
958 INIT_LIST_HEAD(&dentry->d_u.d_child);
961 spin_lock(&dcache_lock);
963 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
964 dentry_stat.nr_dentry++;
965 spin_unlock(&dcache_lock);
970 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
975 q.len = strlen(name);
976 q.hash = full_name_hash(q.name, q.len);
977 return d_alloc(parent, &q);
981 * d_instantiate - fill in inode information for a dentry
982 * @entry: dentry to complete
983 * @inode: inode to attach to this dentry
985 * Fill in inode information in the entry.
987 * This turns negative dentries into productive full members
990 * NOTE! This assumes that the inode count has been incremented
991 * (or otherwise set) by the caller to indicate that it is now
992 * in use by the dcache.
995 void d_instantiate(struct dentry *entry, struct inode * inode)
997 BUG_ON(!list_empty(&entry->d_alias));
998 spin_lock(&dcache_lock);
1000 list_add(&entry->d_alias, &inode->i_dentry);
1001 entry->d_inode = inode;
1002 fsnotify_d_instantiate(entry, inode);
1003 spin_unlock(&dcache_lock);
1004 security_d_instantiate(entry, inode);
1008 * d_instantiate_unique - instantiate a non-aliased dentry
1009 * @entry: dentry to instantiate
1010 * @inode: inode to attach to this dentry
1012 * Fill in inode information in the entry. On success, it returns NULL.
1013 * If an unhashed alias of "entry" already exists, then we return the
1014 * aliased dentry instead and drop one reference to inode.
1016 * Note that in order to avoid conflicts with rename() etc, the caller
1017 * had better be holding the parent directory semaphore.
1019 * This also assumes that the inode count has been incremented
1020 * (or otherwise set) by the caller to indicate that it is now
1021 * in use by the dcache.
1023 static struct dentry *__d_instantiate_unique(struct dentry *entry,
1024 struct inode *inode)
1026 struct dentry *alias;
1027 int len = entry->d_name.len;
1028 const char *name = entry->d_name.name;
1029 unsigned int hash = entry->d_name.hash;
1032 entry->d_inode = NULL;
1036 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1037 struct qstr *qstr = &alias->d_name;
1039 if (qstr->hash != hash)
1041 if (alias->d_parent != entry->d_parent)
1043 if (qstr->len != len)
1045 if (memcmp(qstr->name, name, len))
1051 list_add(&entry->d_alias, &inode->i_dentry);
1052 entry->d_inode = inode;
1053 fsnotify_d_instantiate(entry, inode);
1057 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1059 struct dentry *result;
1061 BUG_ON(!list_empty(&entry->d_alias));
1063 spin_lock(&dcache_lock);
1064 result = __d_instantiate_unique(entry, inode);
1065 spin_unlock(&dcache_lock);
1068 security_d_instantiate(entry, inode);
1072 BUG_ON(!d_unhashed(result));
1077 EXPORT_SYMBOL(d_instantiate_unique);
1080 * d_alloc_root - allocate root dentry
1081 * @root_inode: inode to allocate the root for
1083 * Allocate a root ("/") dentry for the inode given. The inode is
1084 * instantiated and returned. %NULL is returned if there is insufficient
1085 * memory or the inode passed is %NULL.
1088 struct dentry * d_alloc_root(struct inode * root_inode)
1090 struct dentry *res = NULL;
1093 static const struct qstr name = { .name = "/", .len = 1 };
1095 res = d_alloc(NULL, &name);
1097 res->d_sb = root_inode->i_sb;
1098 res->d_parent = res;
1099 d_instantiate(res, root_inode);
1105 static inline struct hlist_head *d_hash(struct dentry *parent,
1108 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1109 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1110 return dentry_hashtable + (hash & D_HASHMASK);
1114 * d_obtain_alias - find or allocate a dentry for a given inode
1115 * @inode: inode to allocate the dentry for
1117 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1118 * similar open by handle operations. The returned dentry may be anonymous,
1119 * or may have a full name (if the inode was already in the cache).
1121 * When called on a directory inode, we must ensure that the inode only ever
1122 * has one dentry. If a dentry is found, that is returned instead of
1123 * allocating a new one.
1125 * On successful return, the reference to the inode has been transferred
1126 * to the dentry. In case of an error the reference on the inode is released.
1127 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1128 * be passed in and will be the error will be propagate to the return value,
1129 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1131 struct dentry *d_obtain_alias(struct inode *inode)
1133 static const struct qstr anonstring = { .name = "" };
1138 return ERR_PTR(-ESTALE);
1140 return ERR_CAST(inode);
1142 res = d_find_alias(inode);
1146 tmp = d_alloc(NULL, &anonstring);
1148 res = ERR_PTR(-ENOMEM);
1151 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1153 spin_lock(&dcache_lock);
1154 res = __d_find_alias(inode, 0);
1156 spin_unlock(&dcache_lock);
1161 /* attach a disconnected dentry */
1162 spin_lock(&tmp->d_lock);
1163 tmp->d_sb = inode->i_sb;
1164 tmp->d_inode = inode;
1165 tmp->d_flags |= DCACHE_DISCONNECTED;
1166 tmp->d_flags &= ~DCACHE_UNHASHED;
1167 list_add(&tmp->d_alias, &inode->i_dentry);
1168 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1169 spin_unlock(&tmp->d_lock);
1171 spin_unlock(&dcache_lock);
1178 EXPORT_SYMBOL_GPL(d_obtain_alias);
1181 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1182 * @inode: the inode which may have a disconnected dentry
1183 * @dentry: a negative dentry which we want to point to the inode.
1185 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1186 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1187 * and return it, else simply d_add the inode to the dentry and return NULL.
1189 * This is needed in the lookup routine of any filesystem that is exportable
1190 * (via knfsd) so that we can build dcache paths to directories effectively.
1192 * If a dentry was found and moved, then it is returned. Otherwise NULL
1193 * is returned. This matches the expected return value of ->lookup.
1196 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1198 struct dentry *new = NULL;
1200 if (inode && S_ISDIR(inode->i_mode)) {
1201 spin_lock(&dcache_lock);
1202 new = __d_find_alias(inode, 1);
1204 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1205 fsnotify_d_instantiate(new, inode);
1206 spin_unlock(&dcache_lock);
1207 security_d_instantiate(new, inode);
1209 d_move(new, dentry);
1212 /* d_instantiate takes dcache_lock, so we do it by hand */
1213 list_add(&dentry->d_alias, &inode->i_dentry);
1214 dentry->d_inode = inode;
1215 fsnotify_d_instantiate(dentry, inode);
1216 spin_unlock(&dcache_lock);
1217 security_d_instantiate(dentry, inode);
1221 d_add(dentry, inode);
1226 * d_add_ci - lookup or allocate new dentry with case-exact name
1227 * @inode: the inode case-insensitive lookup has found
1228 * @dentry: the negative dentry that was passed to the parent's lookup func
1229 * @name: the case-exact name to be associated with the returned dentry
1231 * This is to avoid filling the dcache with case-insensitive names to the
1232 * same inode, only the actual correct case is stored in the dcache for
1233 * case-insensitive filesystems.
1235 * For a case-insensitive lookup match and if the the case-exact dentry
1236 * already exists in in the dcache, use it and return it.
1238 * If no entry exists with the exact case name, allocate new dentry with
1239 * the exact case, and return the spliced entry.
1241 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
1245 struct dentry *found;
1248 /* Does a dentry matching the name exist already? */
1249 found = d_hash_and_lookup(dentry->d_parent, name);
1250 /* If not, create it now and return */
1252 new = d_alloc(dentry->d_parent, name);
1257 found = d_splice_alias(inode, new);
1264 /* Matching dentry exists, check if it is negative. */
1265 if (found->d_inode) {
1266 if (unlikely(found->d_inode != inode)) {
1267 /* This can't happen because bad inodes are unhashed. */
1268 BUG_ON(!is_bad_inode(inode));
1269 BUG_ON(!is_bad_inode(found->d_inode));
1272 * Already have the inode and the dentry attached, decrement
1273 * the reference count to balance the iget() done
1274 * earlier on. We found the dentry using d_lookup() so it
1275 * cannot be disconnected and thus we do not need to worry
1276 * about any NFS/disconnectedness issues here.
1282 * Negative dentry: instantiate it unless the inode is a directory and
1283 * has a 'disconnected' dentry (i.e. IS_ROOT and DCACHE_DISCONNECTED),
1284 * in which case d_move() that in place of the found dentry.
1286 if (!S_ISDIR(inode->i_mode)) {
1287 /* Not a directory; everything is easy. */
1288 d_instantiate(found, inode);
1291 spin_lock(&dcache_lock);
1292 if (list_empty(&inode->i_dentry)) {
1294 * Directory without a 'disconnected' dentry; we need to do
1295 * d_instantiate() by hand because it takes dcache_lock which
1298 list_add(&found->d_alias, &inode->i_dentry);
1299 found->d_inode = inode;
1300 spin_unlock(&dcache_lock);
1301 security_d_instantiate(found, inode);
1305 * Directory with a 'disconnected' dentry; get a reference to the
1306 * 'disconnected' dentry.
1308 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1310 spin_unlock(&dcache_lock);
1311 /* Do security vodoo. */
1312 security_d_instantiate(found, inode);
1313 /* Move new in place of found. */
1315 /* Balance the iget() we did above. */
1317 /* Throw away found. */
1319 /* Use new as the actual dentry. */
1324 return ERR_PTR(error);
1328 * d_lookup - search for a dentry
1329 * @parent: parent dentry
1330 * @name: qstr of name we wish to find
1332 * Searches the children of the parent dentry for the name in question. If
1333 * the dentry is found its reference count is incremented and the dentry
1334 * is returned. The caller must use d_put to free the entry when it has
1335 * finished using it. %NULL is returned on failure.
1337 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1338 * Memory barriers are used while updating and doing lockless traversal.
1339 * To avoid races with d_move while rename is happening, d_lock is used.
1341 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1342 * and name pointer in one structure pointed by d_qstr.
1344 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1345 * lookup is going on.
1347 * The dentry unused LRU is not updated even if lookup finds the required dentry
1348 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1349 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1352 * d_lookup() is protected against the concurrent renames in some unrelated
1353 * directory using the seqlockt_t rename_lock.
1356 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1358 struct dentry * dentry = NULL;
1362 seq = read_seqbegin(&rename_lock);
1363 dentry = __d_lookup(parent, name);
1366 } while (read_seqretry(&rename_lock, seq));
1370 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1372 unsigned int len = name->len;
1373 unsigned int hash = name->hash;
1374 const unsigned char *str = name->name;
1375 struct hlist_head *head = d_hash(parent,hash);
1376 struct dentry *found = NULL;
1377 struct hlist_node *node;
1378 struct dentry *dentry;
1382 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1385 if (dentry->d_name.hash != hash)
1387 if (dentry->d_parent != parent)
1390 spin_lock(&dentry->d_lock);
1393 * Recheck the dentry after taking the lock - d_move may have
1394 * changed things. Don't bother checking the hash because we're
1395 * about to compare the whole name anyway.
1397 if (dentry->d_parent != parent)
1400 /* non-existing due to RCU? */
1401 if (d_unhashed(dentry))
1405 * It is safe to compare names since d_move() cannot
1406 * change the qstr (protected by d_lock).
1408 qstr = &dentry->d_name;
1409 if (parent->d_op && parent->d_op->d_compare) {
1410 if (parent->d_op->d_compare(parent, qstr, name))
1413 if (qstr->len != len)
1415 if (memcmp(qstr->name, str, len))
1419 atomic_inc(&dentry->d_count);
1421 spin_unlock(&dentry->d_lock);
1424 spin_unlock(&dentry->d_lock);
1432 * d_hash_and_lookup - hash the qstr then search for a dentry
1433 * @dir: Directory to search in
1434 * @name: qstr of name we wish to find
1436 * On hash failure or on lookup failure NULL is returned.
1438 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1440 struct dentry *dentry = NULL;
1443 * Check for a fs-specific hash function. Note that we must
1444 * calculate the standard hash first, as the d_op->d_hash()
1445 * routine may choose to leave the hash value unchanged.
1447 name->hash = full_name_hash(name->name, name->len);
1448 if (dir->d_op && dir->d_op->d_hash) {
1449 if (dir->d_op->d_hash(dir, name) < 0)
1452 dentry = d_lookup(dir, name);
1458 * d_validate - verify dentry provided from insecure source
1459 * @dentry: The dentry alleged to be valid child of @dparent
1460 * @dparent: The parent dentry (known to be valid)
1461 * @hash: Hash of the dentry
1462 * @len: Length of the name
1464 * An insecure source has sent us a dentry, here we verify it and dget() it.
1465 * This is used by ncpfs in its readdir implementation.
1466 * Zero is returned in the dentry is invalid.
1469 int d_validate(struct dentry *dentry, struct dentry *dparent)
1471 struct hlist_head *base;
1472 struct hlist_node *lhp;
1474 /* Check whether the ptr might be valid at all.. */
1475 if (!kmem_ptr_validate(dentry_cache, dentry))
1478 if (dentry->d_parent != dparent)
1481 spin_lock(&dcache_lock);
1482 base = d_hash(dparent, dentry->d_name.hash);
1483 hlist_for_each(lhp,base) {
1484 /* hlist_for_each_entry_rcu() not required for d_hash list
1485 * as it is parsed under dcache_lock
1487 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1488 __dget_locked(dentry);
1489 spin_unlock(&dcache_lock);
1493 spin_unlock(&dcache_lock);
1499 * When a file is deleted, we have two options:
1500 * - turn this dentry into a negative dentry
1501 * - unhash this dentry and free it.
1503 * Usually, we want to just turn this into
1504 * a negative dentry, but if anybody else is
1505 * currently using the dentry or the inode
1506 * we can't do that and we fall back on removing
1507 * it from the hash queues and waiting for
1508 * it to be deleted later when it has no users
1512 * d_delete - delete a dentry
1513 * @dentry: The dentry to delete
1515 * Turn the dentry into a negative dentry if possible, otherwise
1516 * remove it from the hash queues so it can be deleted later
1519 void d_delete(struct dentry * dentry)
1523 * Are we the only user?
1525 spin_lock(&dcache_lock);
1526 spin_lock(&dentry->d_lock);
1527 isdir = S_ISDIR(dentry->d_inode->i_mode);
1528 if (atomic_read(&dentry->d_count) == 1) {
1529 dentry_iput(dentry);
1530 fsnotify_nameremove(dentry, isdir);
1534 if (!d_unhashed(dentry))
1537 spin_unlock(&dentry->d_lock);
1538 spin_unlock(&dcache_lock);
1540 fsnotify_nameremove(dentry, isdir);
1543 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1546 entry->d_flags &= ~DCACHE_UNHASHED;
1547 hlist_add_head_rcu(&entry->d_hash, list);
1550 static void _d_rehash(struct dentry * entry)
1552 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1556 * d_rehash - add an entry back to the hash
1557 * @entry: dentry to add to the hash
1559 * Adds a dentry to the hash according to its name.
1562 void d_rehash(struct dentry * entry)
1564 spin_lock(&dcache_lock);
1565 spin_lock(&entry->d_lock);
1567 spin_unlock(&entry->d_lock);
1568 spin_unlock(&dcache_lock);
1571 #define do_switch(x,y) do { \
1572 __typeof__ (x) __tmp = x; \
1573 x = y; y = __tmp; } while (0)
1576 * When switching names, the actual string doesn't strictly have to
1577 * be preserved in the target - because we're dropping the target
1578 * anyway. As such, we can just do a simple memcpy() to copy over
1579 * the new name before we switch.
1581 * Note that we have to be a lot more careful about getting the hash
1582 * switched - we have to switch the hash value properly even if it
1583 * then no longer matches the actual (corrupted) string of the target.
1584 * The hash value has to match the hash queue that the dentry is on..
1586 static void switch_names(struct dentry *dentry, struct dentry *target)
1588 if (dname_external(target)) {
1589 if (dname_external(dentry)) {
1591 * Both external: swap the pointers
1593 do_switch(target->d_name.name, dentry->d_name.name);
1596 * dentry:internal, target:external. Steal target's
1597 * storage and make target internal.
1599 memcpy(target->d_iname, dentry->d_name.name,
1600 dentry->d_name.len + 1);
1601 dentry->d_name.name = target->d_name.name;
1602 target->d_name.name = target->d_iname;
1605 if (dname_external(dentry)) {
1607 * dentry:external, target:internal. Give dentry's
1608 * storage to target and make dentry internal
1610 memcpy(dentry->d_iname, target->d_name.name,
1611 target->d_name.len + 1);
1612 target->d_name.name = dentry->d_name.name;
1613 dentry->d_name.name = dentry->d_iname;
1616 * Both are internal. Just copy target to dentry
1618 memcpy(dentry->d_iname, target->d_name.name,
1619 target->d_name.len + 1);
1625 * We cannibalize "target" when moving dentry on top of it,
1626 * because it's going to be thrown away anyway. We could be more
1627 * polite about it, though.
1629 * This forceful removal will result in ugly /proc output if
1630 * somebody holds a file open that got deleted due to a rename.
1631 * We could be nicer about the deleted file, and let it show
1632 * up under the name it had before it was deleted rather than
1633 * under the original name of the file that was moved on top of it.
1637 * d_move_locked - move a dentry
1638 * @dentry: entry to move
1639 * @target: new dentry
1641 * Update the dcache to reflect the move of a file name. Negative
1642 * dcache entries should not be moved in this way.
1644 static void d_move_locked(struct dentry * dentry, struct dentry * target)
1646 struct hlist_head *list;
1648 if (!dentry->d_inode)
1649 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1651 write_seqlock(&rename_lock);
1653 * XXXX: do we really need to take target->d_lock?
1655 if (target < dentry) {
1656 spin_lock(&target->d_lock);
1657 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1659 spin_lock(&dentry->d_lock);
1660 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
1663 /* Move the dentry to the target hash queue, if on different bucket */
1664 if (d_unhashed(dentry))
1665 goto already_unhashed;
1667 hlist_del_rcu(&dentry->d_hash);
1670 list = d_hash(target->d_parent, target->d_name.hash);
1671 __d_rehash(dentry, list);
1673 /* Unhash the target: dput() will then get rid of it */
1676 list_del(&dentry->d_u.d_child);
1677 list_del(&target->d_u.d_child);
1679 /* Switch the names.. */
1680 switch_names(dentry, target);
1681 do_switch(dentry->d_name.len, target->d_name.len);
1682 do_switch(dentry->d_name.hash, target->d_name.hash);
1684 /* ... and switch the parents */
1685 if (IS_ROOT(dentry)) {
1686 dentry->d_parent = target->d_parent;
1687 target->d_parent = target;
1688 INIT_LIST_HEAD(&target->d_u.d_child);
1690 do_switch(dentry->d_parent, target->d_parent);
1692 /* And add them back to the (new) parent lists */
1693 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1696 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1697 spin_unlock(&target->d_lock);
1698 fsnotify_d_move(dentry);
1699 spin_unlock(&dentry->d_lock);
1700 write_sequnlock(&rename_lock);
1704 * d_move - move a dentry
1705 * @dentry: entry to move
1706 * @target: new dentry
1708 * Update the dcache to reflect the move of a file name. Negative
1709 * dcache entries should not be moved in this way.
1712 void d_move(struct dentry * dentry, struct dentry * target)
1714 spin_lock(&dcache_lock);
1715 d_move_locked(dentry, target);
1716 spin_unlock(&dcache_lock);
1720 * Helper that returns 1 if p1 is a parent of p2, else 0
1722 static int d_isparent(struct dentry *p1, struct dentry *p2)
1726 for (p = p2; p->d_parent != p; p = p->d_parent) {
1727 if (p->d_parent == p1)
1734 * This helper attempts to cope with remotely renamed directories
1736 * It assumes that the caller is already holding
1737 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1739 * Note: If ever the locking in lock_rename() changes, then please
1740 * remember to update this too...
1742 static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1743 __releases(dcache_lock)
1745 struct mutex *m1 = NULL, *m2 = NULL;
1748 /* If alias and dentry share a parent, then no extra locks required */
1749 if (alias->d_parent == dentry->d_parent)
1752 /* Check for loops */
1753 ret = ERR_PTR(-ELOOP);
1754 if (d_isparent(alias, dentry))
1757 /* See lock_rename() */
1758 ret = ERR_PTR(-EBUSY);
1759 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1761 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1762 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1764 m2 = &alias->d_parent->d_inode->i_mutex;
1766 d_move_locked(alias, dentry);
1769 spin_unlock(&dcache_lock);
1778 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1779 * named dentry in place of the dentry to be replaced.
1781 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1783 struct dentry *dparent, *aparent;
1785 switch_names(dentry, anon);
1786 do_switch(dentry->d_name.len, anon->d_name.len);
1787 do_switch(dentry->d_name.hash, anon->d_name.hash);
1789 dparent = dentry->d_parent;
1790 aparent = anon->d_parent;
1792 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1793 list_del(&dentry->d_u.d_child);
1794 if (!IS_ROOT(dentry))
1795 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1797 INIT_LIST_HEAD(&dentry->d_u.d_child);
1799 anon->d_parent = (dparent == dentry) ? anon : dparent;
1800 list_del(&anon->d_u.d_child);
1802 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1804 INIT_LIST_HEAD(&anon->d_u.d_child);
1806 anon->d_flags &= ~DCACHE_DISCONNECTED;
1810 * d_materialise_unique - introduce an inode into the tree
1811 * @dentry: candidate dentry
1812 * @inode: inode to bind to the dentry, to which aliases may be attached
1814 * Introduces an dentry into the tree, substituting an extant disconnected
1815 * root directory alias in its place if there is one
1817 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1819 struct dentry *actual;
1821 BUG_ON(!d_unhashed(dentry));
1823 spin_lock(&dcache_lock);
1827 dentry->d_inode = NULL;
1831 if (S_ISDIR(inode->i_mode)) {
1832 struct dentry *alias;
1834 /* Does an aliased dentry already exist? */
1835 alias = __d_find_alias(inode, 0);
1838 /* Is this an anonymous mountpoint that we could splice
1840 if (IS_ROOT(alias)) {
1841 spin_lock(&alias->d_lock);
1842 __d_materialise_dentry(dentry, alias);
1846 /* Nope, but we must(!) avoid directory aliasing */
1847 actual = __d_unalias(dentry, alias);
1854 /* Add a unique reference */
1855 actual = __d_instantiate_unique(dentry, inode);
1858 else if (unlikely(!d_unhashed(actual)))
1859 goto shouldnt_be_hashed;
1862 spin_lock(&actual->d_lock);
1865 spin_unlock(&actual->d_lock);
1866 spin_unlock(&dcache_lock);
1868 if (actual == dentry) {
1869 security_d_instantiate(dentry, inode);
1877 spin_unlock(&dcache_lock);
1881 static int prepend(char **buffer, int *buflen, const char *str, int namelen)
1885 return -ENAMETOOLONG;
1887 memcpy(*buffer, str, namelen);
1891 static int prepend_name(char **buffer, int *buflen, struct qstr *name)
1893 return prepend(buffer, buflen, name->name, name->len);
1897 * __d_path - return the path of a dentry
1898 * @path: the dentry/vfsmount to report
1899 * @root: root vfsmnt/dentry (may be modified by this function)
1900 * @buffer: buffer to return value in
1901 * @buflen: buffer length
1903 * Convert a dentry into an ASCII path name. If the entry has been deleted
1904 * the string " (deleted)" is appended. Note that this is ambiguous.
1906 * Returns the buffer or an error code if the path was too long.
1908 * "buflen" should be positive. Caller holds the dcache_lock.
1910 * If path is not reachable from the supplied root, then the value of
1911 * root is changed (without modifying refcounts).
1913 char *__d_path(const struct path *path, struct path *root,
1914 char *buffer, int buflen)
1916 struct dentry *dentry = path->dentry;
1917 struct vfsmount *vfsmnt = path->mnt;
1918 char *end = buffer + buflen;
1921 spin_lock(&vfsmount_lock);
1922 prepend(&end, &buflen, "\0", 1);
1923 if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
1924 (prepend(&end, &buflen, " (deleted)", 10) != 0))
1934 struct dentry * parent;
1936 if (dentry == root->dentry && vfsmnt == root->mnt)
1938 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1940 if (vfsmnt->mnt_parent == vfsmnt) {
1943 dentry = vfsmnt->mnt_mountpoint;
1944 vfsmnt = vfsmnt->mnt_parent;
1947 parent = dentry->d_parent;
1949 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
1950 (prepend(&end, &buflen, "/", 1) != 0))
1957 spin_unlock(&vfsmount_lock);
1961 retval += 1; /* hit the slash */
1962 if (prepend_name(&retval, &buflen, &dentry->d_name) != 0)
1965 root->dentry = dentry;
1969 retval = ERR_PTR(-ENAMETOOLONG);
1974 * d_path - return the path of a dentry
1975 * @path: path to report
1976 * @buf: buffer to return value in
1977 * @buflen: buffer length
1979 * Convert a dentry into an ASCII path name. If the entry has been deleted
1980 * the string " (deleted)" is appended. Note that this is ambiguous.
1982 * Returns the buffer or an error code if the path was too long.
1984 * "buflen" should be positive.
1986 char *d_path(const struct path *path, char *buf, int buflen)
1993 * We have various synthetic filesystems that never get mounted. On
1994 * these filesystems dentries are never used for lookup purposes, and
1995 * thus don't need to be hashed. They also don't need a name until a
1996 * user wants to identify the object in /proc/pid/fd/. The little hack
1997 * below allows us to generate a name for these objects on demand:
1999 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2000 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2002 read_lock(¤t->fs->lock);
2003 root = current->fs->root;
2005 read_unlock(¤t->fs->lock);
2006 spin_lock(&dcache_lock);
2008 res = __d_path(path, &tmp, buf, buflen);
2009 spin_unlock(&dcache_lock);
2015 * Helper function for dentry_operations.d_dname() members
2017 char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2018 const char *fmt, ...)
2024 va_start(args, fmt);
2025 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2028 if (sz > sizeof(temp) || sz > buflen)
2029 return ERR_PTR(-ENAMETOOLONG);
2031 buffer += buflen - sz;
2032 return memcpy(buffer, temp, sz);
2036 * Write full pathname from the root of the filesystem into the buffer.
2038 char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2040 char *end = buf + buflen;
2043 spin_lock(&dcache_lock);
2044 prepend(&end, &buflen, "\0", 1);
2045 if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
2046 (prepend(&end, &buflen, "//deleted", 9) != 0))
2054 while (!IS_ROOT(dentry)) {
2055 struct dentry *parent = dentry->d_parent;
2058 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
2059 (prepend(&end, &buflen, "/", 1) != 0))
2065 spin_unlock(&dcache_lock);
2068 spin_unlock(&dcache_lock);
2069 return ERR_PTR(-ENAMETOOLONG);
2073 * NOTE! The user-level library version returns a
2074 * character pointer. The kernel system call just
2075 * returns the length of the buffer filled (which
2076 * includes the ending '\0' character), or a negative
2077 * error value. So libc would do something like
2079 * char *getcwd(char * buf, size_t size)
2083 * retval = sys_getcwd(buf, size);
2090 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
2093 struct path pwd, root;
2094 char *page = (char *) __get_free_page(GFP_USER);
2099 read_lock(¤t->fs->lock);
2100 pwd = current->fs->pwd;
2102 root = current->fs->root;
2104 read_unlock(¤t->fs->lock);
2107 /* Has the current directory has been unlinked? */
2108 spin_lock(&dcache_lock);
2109 if (IS_ROOT(pwd.dentry) || !d_unhashed(pwd.dentry)) {
2111 struct path tmp = root;
2114 cwd = __d_path(&pwd, &tmp, page, PAGE_SIZE);
2115 spin_unlock(&dcache_lock);
2117 error = PTR_ERR(cwd);
2122 len = PAGE_SIZE + page - cwd;
2125 if (copy_to_user(buf, cwd, len))
2129 spin_unlock(&dcache_lock);
2134 free_page((unsigned long) page);
2139 * Test whether new_dentry is a subdirectory of old_dentry.
2141 * Trivially implemented using the dcache structure
2145 * is_subdir - is new dentry a subdirectory of old_dentry
2146 * @new_dentry: new dentry
2147 * @old_dentry: old dentry
2149 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2150 * Returns 0 otherwise.
2151 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2154 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
2157 struct dentry * saved = new_dentry;
2160 /* need rcu_readlock to protect against the d_parent trashing due to
2165 /* for restarting inner loop in case of seq retry */
2168 seq = read_seqbegin(&rename_lock);
2170 if (new_dentry != old_dentry) {
2171 struct dentry * parent = new_dentry->d_parent;
2172 if (parent == new_dentry)
2174 new_dentry = parent;
2180 } while (read_seqretry(&rename_lock, seq));
2186 void d_genocide(struct dentry *root)
2188 struct dentry *this_parent = root;
2189 struct list_head *next;
2191 spin_lock(&dcache_lock);
2193 next = this_parent->d_subdirs.next;
2195 while (next != &this_parent->d_subdirs) {
2196 struct list_head *tmp = next;
2197 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
2199 if (d_unhashed(dentry)||!dentry->d_inode)
2201 if (!list_empty(&dentry->d_subdirs)) {
2202 this_parent = dentry;
2205 atomic_dec(&dentry->d_count);
2207 if (this_parent != root) {
2208 next = this_parent->d_u.d_child.next;
2209 atomic_dec(&this_parent->d_count);
2210 this_parent = this_parent->d_parent;
2213 spin_unlock(&dcache_lock);
2217 * find_inode_number - check for dentry with name
2218 * @dir: directory to check
2219 * @name: Name to find.
2221 * Check whether a dentry already exists for the given name,
2222 * and return the inode number if it has an inode. Otherwise
2225 * This routine is used to post-process directory listings for
2226 * filesystems using synthetic inode numbers, and is necessary
2227 * to keep getcwd() working.
2230 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2232 struct dentry * dentry;
2235 dentry = d_hash_and_lookup(dir, name);
2237 if (dentry->d_inode)
2238 ino = dentry->d_inode->i_ino;
2244 static __initdata unsigned long dhash_entries;
2245 static int __init set_dhash_entries(char *str)
2249 dhash_entries = simple_strtoul(str, &str, 0);
2252 __setup("dhash_entries=", set_dhash_entries);
2254 static void __init dcache_init_early(void)
2258 /* If hashes are distributed across NUMA nodes, defer
2259 * hash allocation until vmalloc space is available.
2265 alloc_large_system_hash("Dentry cache",
2266 sizeof(struct hlist_head),
2274 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2275 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2278 static void __init dcache_init(void)
2283 * A constructor could be added for stable state like the lists,
2284 * but it is probably not worth it because of the cache nature
2287 dentry_cache = KMEM_CACHE(dentry,
2288 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
2290 register_shrinker(&dcache_shrinker);
2292 /* Hash may have been set up in dcache_init_early */
2297 alloc_large_system_hash("Dentry cache",
2298 sizeof(struct hlist_head),
2306 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2307 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2310 /* SLAB cache for __getname() consumers */
2311 struct kmem_cache *names_cachep __read_mostly;
2313 /* SLAB cache for file structures */
2314 struct kmem_cache *filp_cachep __read_mostly;
2316 EXPORT_SYMBOL(d_genocide);
2318 void __init vfs_caches_init_early(void)
2320 dcache_init_early();
2324 void __init vfs_caches_init(unsigned long mempages)
2326 unsigned long reserve;
2328 /* Base hash sizes on available memory, with a reserve equal to
2329 150% of current kernel size */
2331 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2332 mempages -= reserve;
2334 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2335 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2337 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
2338 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2342 files_init(mempages);
2348 EXPORT_SYMBOL(d_alloc);
2349 EXPORT_SYMBOL(d_alloc_root);
2350 EXPORT_SYMBOL(d_delete);
2351 EXPORT_SYMBOL(d_find_alias);
2352 EXPORT_SYMBOL(d_instantiate);
2353 EXPORT_SYMBOL(d_invalidate);
2354 EXPORT_SYMBOL(d_lookup);
2355 EXPORT_SYMBOL(d_move);
2356 EXPORT_SYMBOL_GPL(d_materialise_unique);
2357 EXPORT_SYMBOL(d_path);
2358 EXPORT_SYMBOL(d_prune_aliases);
2359 EXPORT_SYMBOL(d_rehash);
2360 EXPORT_SYMBOL(d_splice_alias);
2361 EXPORT_SYMBOL(d_add_ci);
2362 EXPORT_SYMBOL(d_validate);
2363 EXPORT_SYMBOL(dget_locked);
2364 EXPORT_SYMBOL(dput);
2365 EXPORT_SYMBOL(find_inode_number);
2366 EXPORT_SYMBOL(have_submounts);
2367 EXPORT_SYMBOL(names_cachep);
2368 EXPORT_SYMBOL(shrink_dcache_parent);
2369 EXPORT_SYMBOL(shrink_dcache_sb);