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
;
64 static LIST_HEAD(dentry_unused
);
66 /* Statistics gathering. */
67 struct dentry_stat_t dentry_stat
= {
71 static void __d_free(struct dentry
*dentry
)
73 if (dname_external(dentry
))
74 kfree(dentry
->d_name
.name
);
75 kmem_cache_free(dentry_cache
, dentry
);
78 static void d_callback(struct rcu_head
*head
)
80 struct dentry
* dentry
= container_of(head
, struct dentry
, d_u
.d_rcu
);
85 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
88 static void d_free(struct dentry
*dentry
)
90 if (dentry
->d_op
&& dentry
->d_op
->d_release
)
91 dentry
->d_op
->d_release(dentry
);
92 /* if dentry was never inserted into hash, immediate free is OK */
93 if (hlist_unhashed(&dentry
->d_hash
))
96 call_rcu(&dentry
->d_u
.d_rcu
, d_callback
);
99 static void dentry_lru_remove(struct dentry
*dentry
)
101 if (!list_empty(&dentry
->d_lru
)) {
102 list_del_init(&dentry
->d_lru
);
103 dentry_stat
.nr_unused
--;
108 * Release the dentry's inode, using the filesystem
109 * d_iput() operation if defined.
111 static void dentry_iput(struct dentry
* dentry
)
112 __releases(dentry
->d_lock
)
113 __releases(dcache_lock
)
115 struct inode
*inode
= dentry
->d_inode
;
117 dentry
->d_inode
= NULL
;
118 list_del_init(&dentry
->d_alias
);
119 spin_unlock(&dentry
->d_lock
);
120 spin_unlock(&dcache_lock
);
122 fsnotify_inoderemove(inode
);
123 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
124 dentry
->d_op
->d_iput(dentry
, inode
);
128 spin_unlock(&dentry
->d_lock
);
129 spin_unlock(&dcache_lock
);
134 * d_kill - kill dentry and return parent
135 * @dentry: dentry to kill
137 * The dentry must already be unhashed and removed from the LRU.
139 * If this is the root of the dentry tree, return NULL.
141 static struct dentry
*d_kill(struct dentry
*dentry
)
142 __releases(dentry
->d_lock
)
143 __releases(dcache_lock
)
145 struct dentry
*parent
;
147 list_del(&dentry
->d_u
.d_child
);
148 dentry_stat
.nr_dentry
--; /* For d_free, below */
149 /*drops the locks, at that point nobody can reach this dentry */
151 parent
= dentry
->d_parent
;
153 return dentry
== parent
? NULL
: parent
;
159 * This is complicated by the fact that we do not want to put
160 * dentries that are no longer on any hash chain on the unused
161 * list: we'd much rather just get rid of them immediately.
163 * However, that implies that we have to traverse the dentry
164 * tree upwards to the parents which might _also_ now be
165 * scheduled for deletion (it may have been only waiting for
166 * its last child to go away).
168 * This tail recursion is done by hand as we don't want to depend
169 * on the compiler to always get this right (gcc generally doesn't).
170 * Real recursion would eat up our stack space.
174 * dput - release a dentry
175 * @dentry: dentry to release
177 * Release a dentry. This will drop the usage count and if appropriate
178 * call the dentry unlink method as well as removing it from the queues and
179 * releasing its resources. If the parent dentries were scheduled for release
180 * they too may now get deleted.
182 * no dcache lock, please.
185 void dput(struct dentry
*dentry
)
191 if (atomic_read(&dentry
->d_count
) == 1)
193 if (!atomic_dec_and_lock(&dentry
->d_count
, &dcache_lock
))
196 spin_lock(&dentry
->d_lock
);
197 if (atomic_read(&dentry
->d_count
)) {
198 spin_unlock(&dentry
->d_lock
);
199 spin_unlock(&dcache_lock
);
204 * AV: ->d_delete() is _NOT_ allowed to block now.
206 if (dentry
->d_op
&& dentry
->d_op
->d_delete
) {
207 if (dentry
->d_op
->d_delete(dentry
))
210 /* Unreachable? Get rid of it */
211 if (d_unhashed(dentry
))
213 if (list_empty(&dentry
->d_lru
)) {
214 dentry
->d_flags
|= DCACHE_REFERENCED
;
215 list_add(&dentry
->d_lru
, &dentry_unused
);
216 dentry_stat
.nr_unused
++;
218 spin_unlock(&dentry
->d_lock
);
219 spin_unlock(&dcache_lock
);
225 dentry_lru_remove(dentry
);
226 dentry
= d_kill(dentry
);
232 * d_invalidate - invalidate a dentry
233 * @dentry: dentry to invalidate
235 * Try to invalidate the dentry if it turns out to be
236 * possible. If there are other dentries that can be
237 * reached through this one we can't delete it and we
238 * return -EBUSY. On success we return 0.
243 int d_invalidate(struct dentry
* dentry
)
246 * If it's already been dropped, return OK.
248 spin_lock(&dcache_lock
);
249 if (d_unhashed(dentry
)) {
250 spin_unlock(&dcache_lock
);
254 * Check whether to do a partial shrink_dcache
255 * to get rid of unused child entries.
257 if (!list_empty(&dentry
->d_subdirs
)) {
258 spin_unlock(&dcache_lock
);
259 shrink_dcache_parent(dentry
);
260 spin_lock(&dcache_lock
);
264 * Somebody else still using it?
266 * If it's a directory, we can't drop it
267 * for fear of somebody re-populating it
268 * with children (even though dropping it
269 * would make it unreachable from the root,
270 * we might still populate it if it was a
271 * working directory or similar).
273 spin_lock(&dentry
->d_lock
);
274 if (atomic_read(&dentry
->d_count
) > 1) {
275 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
)) {
276 spin_unlock(&dentry
->d_lock
);
277 spin_unlock(&dcache_lock
);
283 spin_unlock(&dentry
->d_lock
);
284 spin_unlock(&dcache_lock
);
288 /* This should be called _only_ with dcache_lock held */
290 static inline struct dentry
* __dget_locked(struct dentry
*dentry
)
292 atomic_inc(&dentry
->d_count
);
293 dentry_lru_remove(dentry
);
297 struct dentry
* dget_locked(struct dentry
*dentry
)
299 return __dget_locked(dentry
);
303 * d_find_alias - grab a hashed alias of inode
304 * @inode: inode in question
305 * @want_discon: flag, used by d_splice_alias, to request
306 * that only a DISCONNECTED alias be returned.
308 * If inode has a hashed alias, or is a directory and has any alias,
309 * acquire the reference to alias and return it. Otherwise return NULL.
310 * Notice that if inode is a directory there can be only one alias and
311 * it can be unhashed only if it has no children, or if it is the root
314 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
315 * any other hashed alias over that one unless @want_discon is set,
316 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
319 static struct dentry
* __d_find_alias(struct inode
*inode
, int want_discon
)
321 struct list_head
*head
, *next
, *tmp
;
322 struct dentry
*alias
, *discon_alias
=NULL
;
324 head
= &inode
->i_dentry
;
325 next
= inode
->i_dentry
.next
;
326 while (next
!= head
) {
330 alias
= list_entry(tmp
, struct dentry
, d_alias
);
331 if (S_ISDIR(inode
->i_mode
) || !d_unhashed(alias
)) {
332 if (IS_ROOT(alias
) &&
333 (alias
->d_flags
& DCACHE_DISCONNECTED
))
334 discon_alias
= alias
;
335 else if (!want_discon
) {
336 __dget_locked(alias
);
342 __dget_locked(discon_alias
);
346 struct dentry
* d_find_alias(struct inode
*inode
)
348 struct dentry
*de
= NULL
;
350 if (!list_empty(&inode
->i_dentry
)) {
351 spin_lock(&dcache_lock
);
352 de
= __d_find_alias(inode
, 0);
353 spin_unlock(&dcache_lock
);
359 * Try to kill dentries associated with this inode.
360 * WARNING: you must own a reference to inode.
362 void d_prune_aliases(struct inode
*inode
)
364 struct dentry
*dentry
;
366 spin_lock(&dcache_lock
);
367 list_for_each_entry(dentry
, &inode
->i_dentry
, d_alias
) {
368 spin_lock(&dentry
->d_lock
);
369 if (!atomic_read(&dentry
->d_count
)) {
370 __dget_locked(dentry
);
372 spin_unlock(&dentry
->d_lock
);
373 spin_unlock(&dcache_lock
);
377 spin_unlock(&dentry
->d_lock
);
379 spin_unlock(&dcache_lock
);
383 * Throw away a dentry - free the inode, dput the parent. This requires that
384 * the LRU list has already been removed.
386 * Try to prune ancestors as well. This is necessary to prevent
387 * quadratic behavior of shrink_dcache_parent(), but is also expected
388 * to be beneficial in reducing dentry cache fragmentation.
390 static void prune_one_dentry(struct dentry
* dentry
)
391 __releases(dentry
->d_lock
)
392 __releases(dcache_lock
)
393 __acquires(dcache_lock
)
396 dentry
= d_kill(dentry
);
399 * Prune ancestors. Locking is simpler than in dput(),
400 * because dcache_lock needs to be taken anyway.
402 spin_lock(&dcache_lock
);
404 if (!atomic_dec_and_lock(&dentry
->d_count
, &dentry
->d_lock
))
407 if (dentry
->d_op
&& dentry
->d_op
->d_delete
)
408 dentry
->d_op
->d_delete(dentry
);
409 dentry_lru_remove(dentry
);
411 dentry
= d_kill(dentry
);
412 spin_lock(&dcache_lock
);
417 * prune_dcache - shrink the dcache
418 * @count: number of entries to try and free
419 * @sb: if given, ignore dentries for other superblocks
420 * which are being unmounted.
422 * Shrink the dcache. This is done when we need
423 * more memory, or simply when we need to unmount
424 * something (at which point we need to unuse
427 * This function may fail to free any resources if
428 * all the dentries are in use.
431 static void prune_dcache(int count
, struct super_block
*sb
)
433 spin_lock(&dcache_lock
);
434 for (; count
; count
--) {
435 struct dentry
*dentry
;
436 struct list_head
*tmp
;
437 struct rw_semaphore
*s_umount
;
439 cond_resched_lock(&dcache_lock
);
441 tmp
= dentry_unused
.prev
;
443 /* Try to find a dentry for this sb, but don't try
444 * too hard, if they aren't near the tail they will
445 * be moved down again soon
448 while (skip
&& tmp
!= &dentry_unused
&&
449 list_entry(tmp
, struct dentry
, d_lru
)->d_sb
!= sb
) {
454 if (tmp
== &dentry_unused
)
457 prefetch(dentry_unused
.prev
);
458 dentry_stat
.nr_unused
--;
459 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
461 spin_lock(&dentry
->d_lock
);
463 * We found an inuse dentry which was not removed from
464 * dentry_unused because of laziness during lookup. Do not free
465 * it - just keep it off the dentry_unused list.
467 if (atomic_read(&dentry
->d_count
)) {
468 spin_unlock(&dentry
->d_lock
);
471 /* If the dentry was recently referenced, don't free it. */
472 if (dentry
->d_flags
& DCACHE_REFERENCED
) {
473 dentry
->d_flags
&= ~DCACHE_REFERENCED
;
474 list_add(&dentry
->d_lru
, &dentry_unused
);
475 dentry_stat
.nr_unused
++;
476 spin_unlock(&dentry
->d_lock
);
480 * If the dentry is not DCACHED_REFERENCED, it is time
481 * to remove it from the dcache, provided the super block is
482 * NULL (which means we are trying to reclaim memory)
483 * or this dentry belongs to the same super block that
487 * If this dentry is for "my" filesystem, then I can prune it
488 * without taking the s_umount lock (I already hold it).
490 if (sb
&& dentry
->d_sb
== sb
) {
491 prune_one_dentry(dentry
);
495 * ...otherwise we need to be sure this filesystem isn't being
496 * unmounted, otherwise we could race with
497 * generic_shutdown_super(), and end up holding a reference to
498 * an inode while the filesystem is unmounted.
499 * So we try to get s_umount, and make sure s_root isn't NULL.
500 * (Take a local copy of s_umount to avoid a use-after-free of
503 s_umount
= &dentry
->d_sb
->s_umount
;
504 if (down_read_trylock(s_umount
)) {
505 if (dentry
->d_sb
->s_root
!= NULL
) {
506 prune_one_dentry(dentry
);
512 spin_unlock(&dentry
->d_lock
);
514 * Insert dentry at the head of the list as inserting at the
515 * tail leads to a cycle.
517 list_add(&dentry
->d_lru
, &dentry_unused
);
518 dentry_stat
.nr_unused
++;
520 spin_unlock(&dcache_lock
);
524 * Shrink the dcache for the specified super block.
525 * This allows us to unmount a device without disturbing
526 * the dcache for the other devices.
528 * This implementation makes just two traversals of the
529 * unused list. On the first pass we move the selected
530 * dentries to the most recent end, and on the second
531 * pass we free them. The second pass must restart after
532 * each dput(), but since the target dentries are all at
533 * the end, it's really just a single traversal.
537 * shrink_dcache_sb - shrink dcache for a superblock
540 * Shrink the dcache for the specified super block. This
541 * is used to free the dcache before unmounting a file
545 void shrink_dcache_sb(struct super_block
* sb
)
547 struct list_head
*tmp
, *next
;
548 struct dentry
*dentry
;
551 * Pass one ... move the dentries for the specified
552 * superblock to the most recent end of the unused list.
554 spin_lock(&dcache_lock
);
555 list_for_each_prev_safe(tmp
, next
, &dentry_unused
) {
556 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
557 if (dentry
->d_sb
!= sb
)
559 list_move_tail(tmp
, &dentry_unused
);
563 * Pass two ... free the dentries for this superblock.
566 list_for_each_prev_safe(tmp
, next
, &dentry_unused
) {
567 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
568 if (dentry
->d_sb
!= sb
)
570 dentry_stat
.nr_unused
--;
572 spin_lock(&dentry
->d_lock
);
573 if (atomic_read(&dentry
->d_count
)) {
574 spin_unlock(&dentry
->d_lock
);
577 prune_one_dentry(dentry
);
578 cond_resched_lock(&dcache_lock
);
581 spin_unlock(&dcache_lock
);
585 * destroy a single subtree of dentries for unmount
586 * - see the comments on shrink_dcache_for_umount() for a description of the
589 static void shrink_dcache_for_umount_subtree(struct dentry
*dentry
)
591 struct dentry
*parent
;
592 unsigned detached
= 0;
594 BUG_ON(!IS_ROOT(dentry
));
596 /* detach this root from the system */
597 spin_lock(&dcache_lock
);
598 dentry_lru_remove(dentry
);
600 spin_unlock(&dcache_lock
);
603 /* descend to the first leaf in the current subtree */
604 while (!list_empty(&dentry
->d_subdirs
)) {
607 /* this is a branch with children - detach all of them
608 * from the system in one go */
609 spin_lock(&dcache_lock
);
610 list_for_each_entry(loop
, &dentry
->d_subdirs
,
612 dentry_lru_remove(loop
);
614 cond_resched_lock(&dcache_lock
);
616 spin_unlock(&dcache_lock
);
618 /* move to the first child */
619 dentry
= list_entry(dentry
->d_subdirs
.next
,
620 struct dentry
, d_u
.d_child
);
623 /* consume the dentries from this leaf up through its parents
624 * until we find one with children or run out altogether */
628 if (atomic_read(&dentry
->d_count
) != 0) {
630 "BUG: Dentry %p{i=%lx,n=%s}"
632 " [unmount of %s %s]\n",
635 dentry
->d_inode
->i_ino
: 0UL,
637 atomic_read(&dentry
->d_count
),
638 dentry
->d_sb
->s_type
->name
,
643 parent
= dentry
->d_parent
;
644 if (parent
== dentry
)
647 atomic_dec(&parent
->d_count
);
649 list_del(&dentry
->d_u
.d_child
);
652 inode
= dentry
->d_inode
;
654 dentry
->d_inode
= NULL
;
655 list_del_init(&dentry
->d_alias
);
656 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
657 dentry
->d_op
->d_iput(dentry
, inode
);
664 /* finished when we fall off the top of the tree,
665 * otherwise we ascend to the parent and move to the
666 * next sibling if there is one */
672 } while (list_empty(&dentry
->d_subdirs
));
674 dentry
= list_entry(dentry
->d_subdirs
.next
,
675 struct dentry
, d_u
.d_child
);
678 /* several dentries were freed, need to correct nr_dentry */
679 spin_lock(&dcache_lock
);
680 dentry_stat
.nr_dentry
-= detached
;
681 spin_unlock(&dcache_lock
);
685 * destroy the dentries attached to a superblock on unmounting
686 * - we don't need to use dentry->d_lock, and only need dcache_lock when
687 * removing the dentry from the system lists and hashes because:
688 * - the superblock is detached from all mountings and open files, so the
689 * dentry trees will not be rearranged by the VFS
690 * - s_umount is write-locked, so the memory pressure shrinker will ignore
691 * any dentries belonging to this superblock that it comes across
692 * - the filesystem itself is no longer permitted to rearrange the dentries
695 void shrink_dcache_for_umount(struct super_block
*sb
)
697 struct dentry
*dentry
;
699 if (down_read_trylock(&sb
->s_umount
))
704 atomic_dec(&dentry
->d_count
);
705 shrink_dcache_for_umount_subtree(dentry
);
707 while (!hlist_empty(&sb
->s_anon
)) {
708 dentry
= hlist_entry(sb
->s_anon
.first
, struct dentry
, d_hash
);
709 shrink_dcache_for_umount_subtree(dentry
);
714 * Search for at least 1 mount point in the dentry's subdirs.
715 * We descend to the next level whenever the d_subdirs
716 * list is non-empty and continue searching.
720 * have_submounts - check for mounts over a dentry
721 * @parent: dentry to check.
723 * Return true if the parent or its subdirectories contain
727 int have_submounts(struct dentry
*parent
)
729 struct dentry
*this_parent
= parent
;
730 struct list_head
*next
;
732 spin_lock(&dcache_lock
);
733 if (d_mountpoint(parent
))
736 next
= this_parent
->d_subdirs
.next
;
738 while (next
!= &this_parent
->d_subdirs
) {
739 struct list_head
*tmp
= next
;
740 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
742 /* Have we found a mount point ? */
743 if (d_mountpoint(dentry
))
745 if (!list_empty(&dentry
->d_subdirs
)) {
746 this_parent
= dentry
;
751 * All done at this level ... ascend and resume the search.
753 if (this_parent
!= parent
) {
754 next
= this_parent
->d_u
.d_child
.next
;
755 this_parent
= this_parent
->d_parent
;
758 spin_unlock(&dcache_lock
);
759 return 0; /* No mount points found in tree */
761 spin_unlock(&dcache_lock
);
766 * Search the dentry child list for the specified parent,
767 * and move any unused dentries to the end of the unused
768 * list for prune_dcache(). We descend to the next level
769 * whenever the d_subdirs list is non-empty and continue
772 * It returns zero iff there are no unused children,
773 * otherwise it returns the number of children moved to
774 * the end of the unused list. This may not be the total
775 * number of unused children, because select_parent can
776 * drop the lock and return early due to latency
779 static int select_parent(struct dentry
* parent
)
781 struct dentry
*this_parent
= parent
;
782 struct list_head
*next
;
785 spin_lock(&dcache_lock
);
787 next
= this_parent
->d_subdirs
.next
;
789 while (next
!= &this_parent
->d_subdirs
) {
790 struct list_head
*tmp
= next
;
791 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
794 dentry_lru_remove(dentry
);
796 * move only zero ref count dentries to the end
797 * of the unused list for prune_dcache
799 if (!atomic_read(&dentry
->d_count
)) {
800 list_add_tail(&dentry
->d_lru
, &dentry_unused
);
801 dentry_stat
.nr_unused
++;
806 * We can return to the caller if we have found some (this
807 * ensures forward progress). We'll be coming back to find
810 if (found
&& need_resched())
814 * Descend a level if the d_subdirs list is non-empty.
816 if (!list_empty(&dentry
->d_subdirs
)) {
817 this_parent
= dentry
;
822 * All done at this level ... ascend and resume the search.
824 if (this_parent
!= parent
) {
825 next
= this_parent
->d_u
.d_child
.next
;
826 this_parent
= this_parent
->d_parent
;
830 spin_unlock(&dcache_lock
);
835 * shrink_dcache_parent - prune dcache
836 * @parent: parent of entries to prune
838 * Prune the dcache to remove unused children of the parent dentry.
841 void shrink_dcache_parent(struct dentry
* parent
)
845 while ((found
= select_parent(parent
)) != 0)
846 prune_dcache(found
, parent
->d_sb
);
850 * Scan `nr' dentries and return the number which remain.
852 * We need to avoid reentering the filesystem if the caller is performing a
853 * GFP_NOFS allocation attempt. One example deadlock is:
855 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
856 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
857 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
859 * In this case we return -1 to tell the caller that we baled.
861 static int shrink_dcache_memory(int nr
, gfp_t gfp_mask
)
864 if (!(gfp_mask
& __GFP_FS
))
866 prune_dcache(nr
, NULL
);
868 return (dentry_stat
.nr_unused
/ 100) * sysctl_vfs_cache_pressure
;
871 static struct shrinker dcache_shrinker
= {
872 .shrink
= shrink_dcache_memory
,
873 .seeks
= DEFAULT_SEEKS
,
877 * d_alloc - allocate a dcache entry
878 * @parent: parent of entry to allocate
879 * @name: qstr of the name
881 * Allocates a dentry. It returns %NULL if there is insufficient memory
882 * available. On a success the dentry is returned. The name passed in is
883 * copied and the copy passed in may be reused after this call.
886 struct dentry
*d_alloc(struct dentry
* parent
, const struct qstr
*name
)
888 struct dentry
*dentry
;
891 dentry
= kmem_cache_alloc(dentry_cache
, GFP_KERNEL
);
895 if (name
->len
> DNAME_INLINE_LEN
-1) {
896 dname
= kmalloc(name
->len
+ 1, GFP_KERNEL
);
898 kmem_cache_free(dentry_cache
, dentry
);
902 dname
= dentry
->d_iname
;
904 dentry
->d_name
.name
= dname
;
906 dentry
->d_name
.len
= name
->len
;
907 dentry
->d_name
.hash
= name
->hash
;
908 memcpy(dname
, name
->name
, name
->len
);
909 dname
[name
->len
] = 0;
911 atomic_set(&dentry
->d_count
, 1);
912 dentry
->d_flags
= DCACHE_UNHASHED
;
913 spin_lock_init(&dentry
->d_lock
);
914 dentry
->d_inode
= NULL
;
915 dentry
->d_parent
= NULL
;
918 dentry
->d_fsdata
= NULL
;
919 dentry
->d_mounted
= 0;
920 #ifdef CONFIG_PROFILING
921 dentry
->d_cookie
= NULL
;
923 INIT_HLIST_NODE(&dentry
->d_hash
);
924 INIT_LIST_HEAD(&dentry
->d_lru
);
925 INIT_LIST_HEAD(&dentry
->d_subdirs
);
926 INIT_LIST_HEAD(&dentry
->d_alias
);
929 dentry
->d_parent
= dget(parent
);
930 dentry
->d_sb
= parent
->d_sb
;
932 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
935 spin_lock(&dcache_lock
);
937 list_add(&dentry
->d_u
.d_child
, &parent
->d_subdirs
);
938 dentry_stat
.nr_dentry
++;
939 spin_unlock(&dcache_lock
);
944 struct dentry
*d_alloc_name(struct dentry
*parent
, const char *name
)
949 q
.len
= strlen(name
);
950 q
.hash
= full_name_hash(q
.name
, q
.len
);
951 return d_alloc(parent
, &q
);
955 * d_instantiate - fill in inode information for a dentry
956 * @entry: dentry to complete
957 * @inode: inode to attach to this dentry
959 * Fill in inode information in the entry.
961 * This turns negative dentries into productive full members
964 * NOTE! This assumes that the inode count has been incremented
965 * (or otherwise set) by the caller to indicate that it is now
966 * in use by the dcache.
969 void d_instantiate(struct dentry
*entry
, struct inode
* inode
)
971 BUG_ON(!list_empty(&entry
->d_alias
));
972 spin_lock(&dcache_lock
);
974 list_add(&entry
->d_alias
, &inode
->i_dentry
);
975 entry
->d_inode
= inode
;
976 fsnotify_d_instantiate(entry
, inode
);
977 spin_unlock(&dcache_lock
);
978 security_d_instantiate(entry
, inode
);
982 * d_instantiate_unique - instantiate a non-aliased dentry
983 * @entry: dentry to instantiate
984 * @inode: inode to attach to this dentry
986 * Fill in inode information in the entry. On success, it returns NULL.
987 * If an unhashed alias of "entry" already exists, then we return the
988 * aliased dentry instead and drop one reference to inode.
990 * Note that in order to avoid conflicts with rename() etc, the caller
991 * had better be holding the parent directory semaphore.
993 * This also assumes that the inode count has been incremented
994 * (or otherwise set) by the caller to indicate that it is now
995 * in use by the dcache.
997 static struct dentry
*__d_instantiate_unique(struct dentry
*entry
,
1000 struct dentry
*alias
;
1001 int len
= entry
->d_name
.len
;
1002 const char *name
= entry
->d_name
.name
;
1003 unsigned int hash
= entry
->d_name
.hash
;
1006 entry
->d_inode
= NULL
;
1010 list_for_each_entry(alias
, &inode
->i_dentry
, d_alias
) {
1011 struct qstr
*qstr
= &alias
->d_name
;
1013 if (qstr
->hash
!= hash
)
1015 if (alias
->d_parent
!= entry
->d_parent
)
1017 if (qstr
->len
!= len
)
1019 if (memcmp(qstr
->name
, name
, len
))
1025 list_add(&entry
->d_alias
, &inode
->i_dentry
);
1026 entry
->d_inode
= inode
;
1027 fsnotify_d_instantiate(entry
, inode
);
1031 struct dentry
*d_instantiate_unique(struct dentry
*entry
, struct inode
*inode
)
1033 struct dentry
*result
;
1035 BUG_ON(!list_empty(&entry
->d_alias
));
1037 spin_lock(&dcache_lock
);
1038 result
= __d_instantiate_unique(entry
, inode
);
1039 spin_unlock(&dcache_lock
);
1042 security_d_instantiate(entry
, inode
);
1046 BUG_ON(!d_unhashed(result
));
1051 EXPORT_SYMBOL(d_instantiate_unique
);
1054 * d_alloc_root - allocate root dentry
1055 * @root_inode: inode to allocate the root for
1057 * Allocate a root ("/") dentry for the inode given. The inode is
1058 * instantiated and returned. %NULL is returned if there is insufficient
1059 * memory or the inode passed is %NULL.
1062 struct dentry
* d_alloc_root(struct inode
* root_inode
)
1064 struct dentry
*res
= NULL
;
1067 static const struct qstr name
= { .name
= "/", .len
= 1 };
1069 res
= d_alloc(NULL
, &name
);
1071 res
->d_sb
= root_inode
->i_sb
;
1072 res
->d_parent
= res
;
1073 d_instantiate(res
, root_inode
);
1079 static inline struct hlist_head
*d_hash(struct dentry
*parent
,
1082 hash
+= ((unsigned long) parent
^ GOLDEN_RATIO_PRIME
) / L1_CACHE_BYTES
;
1083 hash
= hash
^ ((hash
^ GOLDEN_RATIO_PRIME
) >> D_HASHBITS
);
1084 return dentry_hashtable
+ (hash
& D_HASHMASK
);
1088 * d_alloc_anon - allocate an anonymous dentry
1089 * @inode: inode to allocate the dentry for
1091 * This is similar to d_alloc_root. It is used by filesystems when
1092 * creating a dentry for a given inode, often in the process of
1093 * mapping a filehandle to a dentry. The returned dentry may be
1094 * anonymous, or may have a full name (if the inode was already
1095 * in the cache). The file system may need to make further
1096 * efforts to connect this dentry into the dcache properly.
1098 * When called on a directory inode, we must ensure that
1099 * the inode only ever has one dentry. If a dentry is
1100 * found, that is returned instead of allocating a new one.
1102 * On successful return, the reference to the inode has been transferred
1103 * to the dentry. If %NULL is returned (indicating kmalloc failure),
1104 * the reference on the inode has not been released.
1107 struct dentry
* d_alloc_anon(struct inode
*inode
)
1109 static const struct qstr anonstring
= { .name
= "" };
1113 if ((res
= d_find_alias(inode
))) {
1118 tmp
= d_alloc(NULL
, &anonstring
);
1122 tmp
->d_parent
= tmp
; /* make sure dput doesn't croak */
1124 spin_lock(&dcache_lock
);
1125 res
= __d_find_alias(inode
, 0);
1127 /* attach a disconnected dentry */
1130 spin_lock(&res
->d_lock
);
1131 res
->d_sb
= inode
->i_sb
;
1132 res
->d_parent
= res
;
1133 res
->d_inode
= inode
;
1134 res
->d_flags
|= DCACHE_DISCONNECTED
;
1135 res
->d_flags
&= ~DCACHE_UNHASHED
;
1136 list_add(&res
->d_alias
, &inode
->i_dentry
);
1137 hlist_add_head(&res
->d_hash
, &inode
->i_sb
->s_anon
);
1138 spin_unlock(&res
->d_lock
);
1140 inode
= NULL
; /* don't drop reference */
1142 spin_unlock(&dcache_lock
);
1153 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1154 * @inode: the inode which may have a disconnected dentry
1155 * @dentry: a negative dentry which we want to point to the inode.
1157 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1158 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1159 * and return it, else simply d_add the inode to the dentry and return NULL.
1161 * This is needed in the lookup routine of any filesystem that is exportable
1162 * (via knfsd) so that we can build dcache paths to directories effectively.
1164 * If a dentry was found and moved, then it is returned. Otherwise NULL
1165 * is returned. This matches the expected return value of ->lookup.
1168 struct dentry
*d_splice_alias(struct inode
*inode
, struct dentry
*dentry
)
1170 struct dentry
*new = NULL
;
1172 if (inode
&& S_ISDIR(inode
->i_mode
)) {
1173 spin_lock(&dcache_lock
);
1174 new = __d_find_alias(inode
, 1);
1176 BUG_ON(!(new->d_flags
& DCACHE_DISCONNECTED
));
1177 fsnotify_d_instantiate(new, inode
);
1178 spin_unlock(&dcache_lock
);
1179 security_d_instantiate(new, inode
);
1181 d_move(new, dentry
);
1184 /* d_instantiate takes dcache_lock, so we do it by hand */
1185 list_add(&dentry
->d_alias
, &inode
->i_dentry
);
1186 dentry
->d_inode
= inode
;
1187 fsnotify_d_instantiate(dentry
, inode
);
1188 spin_unlock(&dcache_lock
);
1189 security_d_instantiate(dentry
, inode
);
1193 d_add(dentry
, inode
);
1199 * d_lookup - search for a dentry
1200 * @parent: parent dentry
1201 * @name: qstr of name we wish to find
1203 * Searches the children of the parent dentry for the name in question. If
1204 * the dentry is found its reference count is incremented and the dentry
1205 * is returned. The caller must use d_put to free the entry when it has
1206 * finished using it. %NULL is returned on failure.
1208 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1209 * Memory barriers are used while updating and doing lockless traversal.
1210 * To avoid races with d_move while rename is happening, d_lock is used.
1212 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1213 * and name pointer in one structure pointed by d_qstr.
1215 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1216 * lookup is going on.
1218 * dentry_unused list is not updated even if lookup finds the required dentry
1219 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1220 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1223 * d_lookup() is protected against the concurrent renames in some unrelated
1224 * directory using the seqlockt_t rename_lock.
1227 struct dentry
* d_lookup(struct dentry
* parent
, struct qstr
* name
)
1229 struct dentry
* dentry
= NULL
;
1233 seq
= read_seqbegin(&rename_lock
);
1234 dentry
= __d_lookup(parent
, name
);
1237 } while (read_seqretry(&rename_lock
, seq
));
1241 struct dentry
* __d_lookup(struct dentry
* parent
, struct qstr
* name
)
1243 unsigned int len
= name
->len
;
1244 unsigned int hash
= name
->hash
;
1245 const unsigned char *str
= name
->name
;
1246 struct hlist_head
*head
= d_hash(parent
,hash
);
1247 struct dentry
*found
= NULL
;
1248 struct hlist_node
*node
;
1249 struct dentry
*dentry
;
1253 hlist_for_each_entry_rcu(dentry
, node
, head
, d_hash
) {
1256 if (dentry
->d_name
.hash
!= hash
)
1258 if (dentry
->d_parent
!= parent
)
1261 spin_lock(&dentry
->d_lock
);
1264 * Recheck the dentry after taking the lock - d_move may have
1265 * changed things. Don't bother checking the hash because we're
1266 * about to compare the whole name anyway.
1268 if (dentry
->d_parent
!= parent
)
1272 * It is safe to compare names since d_move() cannot
1273 * change the qstr (protected by d_lock).
1275 qstr
= &dentry
->d_name
;
1276 if (parent
->d_op
&& parent
->d_op
->d_compare
) {
1277 if (parent
->d_op
->d_compare(parent
, qstr
, name
))
1280 if (qstr
->len
!= len
)
1282 if (memcmp(qstr
->name
, str
, len
))
1286 if (!d_unhashed(dentry
)) {
1287 atomic_inc(&dentry
->d_count
);
1290 spin_unlock(&dentry
->d_lock
);
1293 spin_unlock(&dentry
->d_lock
);
1301 * d_hash_and_lookup - hash the qstr then search for a dentry
1302 * @dir: Directory to search in
1303 * @name: qstr of name we wish to find
1305 * On hash failure or on lookup failure NULL is returned.
1307 struct dentry
*d_hash_and_lookup(struct dentry
*dir
, struct qstr
*name
)
1309 struct dentry
*dentry
= NULL
;
1312 * Check for a fs-specific hash function. Note that we must
1313 * calculate the standard hash first, as the d_op->d_hash()
1314 * routine may choose to leave the hash value unchanged.
1316 name
->hash
= full_name_hash(name
->name
, name
->len
);
1317 if (dir
->d_op
&& dir
->d_op
->d_hash
) {
1318 if (dir
->d_op
->d_hash(dir
, name
) < 0)
1321 dentry
= d_lookup(dir
, name
);
1327 * d_validate - verify dentry provided from insecure source
1328 * @dentry: The dentry alleged to be valid child of @dparent
1329 * @dparent: The parent dentry (known to be valid)
1330 * @hash: Hash of the dentry
1331 * @len: Length of the name
1333 * An insecure source has sent us a dentry, here we verify it and dget() it.
1334 * This is used by ncpfs in its readdir implementation.
1335 * Zero is returned in the dentry is invalid.
1338 int d_validate(struct dentry
*dentry
, struct dentry
*dparent
)
1340 struct hlist_head
*base
;
1341 struct hlist_node
*lhp
;
1343 /* Check whether the ptr might be valid at all.. */
1344 if (!kmem_ptr_validate(dentry_cache
, dentry
))
1347 if (dentry
->d_parent
!= dparent
)
1350 spin_lock(&dcache_lock
);
1351 base
= d_hash(dparent
, dentry
->d_name
.hash
);
1352 hlist_for_each(lhp
,base
) {
1353 /* hlist_for_each_entry_rcu() not required for d_hash list
1354 * as it is parsed under dcache_lock
1356 if (dentry
== hlist_entry(lhp
, struct dentry
, d_hash
)) {
1357 __dget_locked(dentry
);
1358 spin_unlock(&dcache_lock
);
1362 spin_unlock(&dcache_lock
);
1368 * When a file is deleted, we have two options:
1369 * - turn this dentry into a negative dentry
1370 * - unhash this dentry and free it.
1372 * Usually, we want to just turn this into
1373 * a negative dentry, but if anybody else is
1374 * currently using the dentry or the inode
1375 * we can't do that and we fall back on removing
1376 * it from the hash queues and waiting for
1377 * it to be deleted later when it has no users
1381 * d_delete - delete a dentry
1382 * @dentry: The dentry to delete
1384 * Turn the dentry into a negative dentry if possible, otherwise
1385 * remove it from the hash queues so it can be deleted later
1388 void d_delete(struct dentry
* dentry
)
1392 * Are we the only user?
1394 spin_lock(&dcache_lock
);
1395 spin_lock(&dentry
->d_lock
);
1396 isdir
= S_ISDIR(dentry
->d_inode
->i_mode
);
1397 if (atomic_read(&dentry
->d_count
) == 1) {
1398 dentry_iput(dentry
);
1399 fsnotify_nameremove(dentry
, isdir
);
1403 if (!d_unhashed(dentry
))
1406 spin_unlock(&dentry
->d_lock
);
1407 spin_unlock(&dcache_lock
);
1409 fsnotify_nameremove(dentry
, isdir
);
1412 static void __d_rehash(struct dentry
* entry
, struct hlist_head
*list
)
1415 entry
->d_flags
&= ~DCACHE_UNHASHED
;
1416 hlist_add_head_rcu(&entry
->d_hash
, list
);
1419 static void _d_rehash(struct dentry
* entry
)
1421 __d_rehash(entry
, d_hash(entry
->d_parent
, entry
->d_name
.hash
));
1425 * d_rehash - add an entry back to the hash
1426 * @entry: dentry to add to the hash
1428 * Adds a dentry to the hash according to its name.
1431 void d_rehash(struct dentry
* entry
)
1433 spin_lock(&dcache_lock
);
1434 spin_lock(&entry
->d_lock
);
1436 spin_unlock(&entry
->d_lock
);
1437 spin_unlock(&dcache_lock
);
1440 #define do_switch(x,y) do { \
1441 __typeof__ (x) __tmp = x; \
1442 x = y; y = __tmp; } while (0)
1445 * When switching names, the actual string doesn't strictly have to
1446 * be preserved in the target - because we're dropping the target
1447 * anyway. As such, we can just do a simple memcpy() to copy over
1448 * the new name before we switch.
1450 * Note that we have to be a lot more careful about getting the hash
1451 * switched - we have to switch the hash value properly even if it
1452 * then no longer matches the actual (corrupted) string of the target.
1453 * The hash value has to match the hash queue that the dentry is on..
1455 static void switch_names(struct dentry
*dentry
, struct dentry
*target
)
1457 if (dname_external(target
)) {
1458 if (dname_external(dentry
)) {
1460 * Both external: swap the pointers
1462 do_switch(target
->d_name
.name
, dentry
->d_name
.name
);
1465 * dentry:internal, target:external. Steal target's
1466 * storage and make target internal.
1468 memcpy(target
->d_iname
, dentry
->d_name
.name
,
1469 dentry
->d_name
.len
+ 1);
1470 dentry
->d_name
.name
= target
->d_name
.name
;
1471 target
->d_name
.name
= target
->d_iname
;
1474 if (dname_external(dentry
)) {
1476 * dentry:external, target:internal. Give dentry's
1477 * storage to target and make dentry internal
1479 memcpy(dentry
->d_iname
, target
->d_name
.name
,
1480 target
->d_name
.len
+ 1);
1481 target
->d_name
.name
= dentry
->d_name
.name
;
1482 dentry
->d_name
.name
= dentry
->d_iname
;
1485 * Both are internal. Just copy target to dentry
1487 memcpy(dentry
->d_iname
, target
->d_name
.name
,
1488 target
->d_name
.len
+ 1);
1494 * We cannibalize "target" when moving dentry on top of it,
1495 * because it's going to be thrown away anyway. We could be more
1496 * polite about it, though.
1498 * This forceful removal will result in ugly /proc output if
1499 * somebody holds a file open that got deleted due to a rename.
1500 * We could be nicer about the deleted file, and let it show
1501 * up under the name it had before it was deleted rather than
1502 * under the original name of the file that was moved on top of it.
1506 * d_move_locked - move a dentry
1507 * @dentry: entry to move
1508 * @target: new dentry
1510 * Update the dcache to reflect the move of a file name. Negative
1511 * dcache entries should not be moved in this way.
1513 static void d_move_locked(struct dentry
* dentry
, struct dentry
* target
)
1515 struct hlist_head
*list
;
1517 if (!dentry
->d_inode
)
1518 printk(KERN_WARNING
"VFS: moving negative dcache entry\n");
1520 write_seqlock(&rename_lock
);
1522 * XXXX: do we really need to take target->d_lock?
1524 if (target
< dentry
) {
1525 spin_lock(&target
->d_lock
);
1526 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1528 spin_lock(&dentry
->d_lock
);
1529 spin_lock_nested(&target
->d_lock
, DENTRY_D_LOCK_NESTED
);
1532 /* Move the dentry to the target hash queue, if on different bucket */
1533 if (d_unhashed(dentry
))
1534 goto already_unhashed
;
1536 hlist_del_rcu(&dentry
->d_hash
);
1539 list
= d_hash(target
->d_parent
, target
->d_name
.hash
);
1540 __d_rehash(dentry
, list
);
1542 /* Unhash the target: dput() will then get rid of it */
1545 list_del(&dentry
->d_u
.d_child
);
1546 list_del(&target
->d_u
.d_child
);
1548 /* Switch the names.. */
1549 switch_names(dentry
, target
);
1550 do_switch(dentry
->d_name
.len
, target
->d_name
.len
);
1551 do_switch(dentry
->d_name
.hash
, target
->d_name
.hash
);
1553 /* ... and switch the parents */
1554 if (IS_ROOT(dentry
)) {
1555 dentry
->d_parent
= target
->d_parent
;
1556 target
->d_parent
= target
;
1557 INIT_LIST_HEAD(&target
->d_u
.d_child
);
1559 do_switch(dentry
->d_parent
, target
->d_parent
);
1561 /* And add them back to the (new) parent lists */
1562 list_add(&target
->d_u
.d_child
, &target
->d_parent
->d_subdirs
);
1565 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
1566 spin_unlock(&target
->d_lock
);
1567 fsnotify_d_move(dentry
);
1568 spin_unlock(&dentry
->d_lock
);
1569 write_sequnlock(&rename_lock
);
1573 * d_move - move a dentry
1574 * @dentry: entry to move
1575 * @target: new dentry
1577 * Update the dcache to reflect the move of a file name. Negative
1578 * dcache entries should not be moved in this way.
1581 void d_move(struct dentry
* dentry
, struct dentry
* target
)
1583 spin_lock(&dcache_lock
);
1584 d_move_locked(dentry
, target
);
1585 spin_unlock(&dcache_lock
);
1589 * Helper that returns 1 if p1 is a parent of p2, else 0
1591 static int d_isparent(struct dentry
*p1
, struct dentry
*p2
)
1595 for (p
= p2
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1596 if (p
->d_parent
== p1
)
1603 * This helper attempts to cope with remotely renamed directories
1605 * It assumes that the caller is already holding
1606 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1608 * Note: If ever the locking in lock_rename() changes, then please
1609 * remember to update this too...
1611 static struct dentry
*__d_unalias(struct dentry
*dentry
, struct dentry
*alias
)
1612 __releases(dcache_lock
)
1614 struct mutex
*m1
= NULL
, *m2
= NULL
;
1617 /* If alias and dentry share a parent, then no extra locks required */
1618 if (alias
->d_parent
== dentry
->d_parent
)
1621 /* Check for loops */
1622 ret
= ERR_PTR(-ELOOP
);
1623 if (d_isparent(alias
, dentry
))
1626 /* See lock_rename() */
1627 ret
= ERR_PTR(-EBUSY
);
1628 if (!mutex_trylock(&dentry
->d_sb
->s_vfs_rename_mutex
))
1630 m1
= &dentry
->d_sb
->s_vfs_rename_mutex
;
1631 if (!mutex_trylock(&alias
->d_parent
->d_inode
->i_mutex
))
1633 m2
= &alias
->d_parent
->d_inode
->i_mutex
;
1635 d_move_locked(alias
, dentry
);
1638 spin_unlock(&dcache_lock
);
1647 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1648 * named dentry in place of the dentry to be replaced.
1650 static void __d_materialise_dentry(struct dentry
*dentry
, struct dentry
*anon
)
1652 struct dentry
*dparent
, *aparent
;
1654 switch_names(dentry
, anon
);
1655 do_switch(dentry
->d_name
.len
, anon
->d_name
.len
);
1656 do_switch(dentry
->d_name
.hash
, anon
->d_name
.hash
);
1658 dparent
= dentry
->d_parent
;
1659 aparent
= anon
->d_parent
;
1661 dentry
->d_parent
= (aparent
== anon
) ? dentry
: aparent
;
1662 list_del(&dentry
->d_u
.d_child
);
1663 if (!IS_ROOT(dentry
))
1664 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
1666 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
1668 anon
->d_parent
= (dparent
== dentry
) ? anon
: dparent
;
1669 list_del(&anon
->d_u
.d_child
);
1671 list_add(&anon
->d_u
.d_child
, &anon
->d_parent
->d_subdirs
);
1673 INIT_LIST_HEAD(&anon
->d_u
.d_child
);
1675 anon
->d_flags
&= ~DCACHE_DISCONNECTED
;
1679 * d_materialise_unique - introduce an inode into the tree
1680 * @dentry: candidate dentry
1681 * @inode: inode to bind to the dentry, to which aliases may be attached
1683 * Introduces an dentry into the tree, substituting an extant disconnected
1684 * root directory alias in its place if there is one
1686 struct dentry
*d_materialise_unique(struct dentry
*dentry
, struct inode
*inode
)
1688 struct dentry
*actual
;
1690 BUG_ON(!d_unhashed(dentry
));
1692 spin_lock(&dcache_lock
);
1696 dentry
->d_inode
= NULL
;
1700 if (S_ISDIR(inode
->i_mode
)) {
1701 struct dentry
*alias
;
1703 /* Does an aliased dentry already exist? */
1704 alias
= __d_find_alias(inode
, 0);
1707 /* Is this an anonymous mountpoint that we could splice
1709 if (IS_ROOT(alias
)) {
1710 spin_lock(&alias
->d_lock
);
1711 __d_materialise_dentry(dentry
, alias
);
1715 /* Nope, but we must(!) avoid directory aliasing */
1716 actual
= __d_unalias(dentry
, alias
);
1723 /* Add a unique reference */
1724 actual
= __d_instantiate_unique(dentry
, inode
);
1727 else if (unlikely(!d_unhashed(actual
)))
1728 goto shouldnt_be_hashed
;
1731 spin_lock(&actual
->d_lock
);
1734 spin_unlock(&actual
->d_lock
);
1735 spin_unlock(&dcache_lock
);
1737 if (actual
== dentry
) {
1738 security_d_instantiate(dentry
, inode
);
1746 spin_unlock(&dcache_lock
);
1750 static int prepend(char **buffer
, int *buflen
, const char *str
, int namelen
)
1754 return -ENAMETOOLONG
;
1756 memcpy(*buffer
, str
, namelen
);
1760 static int prepend_name(char **buffer
, int *buflen
, struct qstr
*name
)
1762 return prepend(buffer
, buflen
, name
->name
, name
->len
);
1766 * __d_path - return the path of a dentry
1767 * @path: the dentry/vfsmount to report
1768 * @root: root vfsmnt/dentry (may be modified by this function)
1769 * @buffer: buffer to return value in
1770 * @buflen: buffer length
1772 * Convert a dentry into an ASCII path name. If the entry has been deleted
1773 * the string " (deleted)" is appended. Note that this is ambiguous.
1775 * Returns the buffer or an error code if the path was too long.
1777 * "buflen" should be positive. Caller holds the dcache_lock.
1779 * If path is not reachable from the supplied root, then the value of
1780 * root is changed (without modifying refcounts).
1782 char *__d_path(const struct path
*path
, struct path
*root
,
1783 char *buffer
, int buflen
)
1785 struct dentry
*dentry
= path
->dentry
;
1786 struct vfsmount
*vfsmnt
= path
->mnt
;
1787 char *end
= buffer
+ buflen
;
1790 spin_lock(&vfsmount_lock
);
1791 prepend(&end
, &buflen
, "\0", 1);
1792 if (!IS_ROOT(dentry
) && d_unhashed(dentry
) &&
1793 (prepend(&end
, &buflen
, " (deleted)", 10) != 0))
1803 struct dentry
* parent
;
1805 if (dentry
== root
->dentry
&& vfsmnt
== root
->mnt
)
1807 if (dentry
== vfsmnt
->mnt_root
|| IS_ROOT(dentry
)) {
1809 if (vfsmnt
->mnt_parent
== vfsmnt
) {
1812 dentry
= vfsmnt
->mnt_mountpoint
;
1813 vfsmnt
= vfsmnt
->mnt_parent
;
1816 parent
= dentry
->d_parent
;
1818 if ((prepend_name(&end
, &buflen
, &dentry
->d_name
) != 0) ||
1819 (prepend(&end
, &buflen
, "/", 1) != 0))
1826 spin_unlock(&vfsmount_lock
);
1830 retval
+= 1; /* hit the slash */
1831 if (prepend_name(&retval
, &buflen
, &dentry
->d_name
) != 0)
1834 root
->dentry
= dentry
;
1838 retval
= ERR_PTR(-ENAMETOOLONG
);
1843 * d_path - return the path of a dentry
1844 * @path: path to report
1845 * @buf: buffer to return value in
1846 * @buflen: buffer length
1848 * Convert a dentry into an ASCII path name. If the entry has been deleted
1849 * the string " (deleted)" is appended. Note that this is ambiguous.
1851 * Returns the buffer or an error code if the path was too long.
1853 * "buflen" should be positive.
1855 char *d_path(const struct path
*path
, char *buf
, int buflen
)
1862 * We have various synthetic filesystems that never get mounted. On
1863 * these filesystems dentries are never used for lookup purposes, and
1864 * thus don't need to be hashed. They also don't need a name until a
1865 * user wants to identify the object in /proc/pid/fd/. The little hack
1866 * below allows us to generate a name for these objects on demand:
1868 if (path
->dentry
->d_op
&& path
->dentry
->d_op
->d_dname
)
1869 return path
->dentry
->d_op
->d_dname(path
->dentry
, buf
, buflen
);
1871 read_lock(¤t
->fs
->lock
);
1872 root
= current
->fs
->root
;
1874 read_unlock(¤t
->fs
->lock
);
1875 spin_lock(&dcache_lock
);
1877 res
= __d_path(path
, &tmp
, buf
, buflen
);
1878 spin_unlock(&dcache_lock
);
1884 * Helper function for dentry_operations.d_dname() members
1886 char *dynamic_dname(struct dentry
*dentry
, char *buffer
, int buflen
,
1887 const char *fmt
, ...)
1893 va_start(args
, fmt
);
1894 sz
= vsnprintf(temp
, sizeof(temp
), fmt
, args
) + 1;
1897 if (sz
> sizeof(temp
) || sz
> buflen
)
1898 return ERR_PTR(-ENAMETOOLONG
);
1900 buffer
+= buflen
- sz
;
1901 return memcpy(buffer
, temp
, sz
);
1905 * Write full pathname from the root of the filesystem into the buffer.
1907 char *dentry_path(struct dentry
*dentry
, char *buf
, int buflen
)
1909 char *end
= buf
+ buflen
;
1912 spin_lock(&dcache_lock
);
1913 prepend(&end
, &buflen
, "\0", 1);
1914 if (!IS_ROOT(dentry
) && d_unhashed(dentry
) &&
1915 (prepend(&end
, &buflen
, "//deleted", 9) != 0))
1923 while (!IS_ROOT(dentry
)) {
1924 struct dentry
*parent
= dentry
->d_parent
;
1927 if ((prepend_name(&end
, &buflen
, &dentry
->d_name
) != 0) ||
1928 (prepend(&end
, &buflen
, "/", 1) != 0))
1934 spin_unlock(&dcache_lock
);
1937 spin_unlock(&dcache_lock
);
1938 return ERR_PTR(-ENAMETOOLONG
);
1942 * NOTE! The user-level library version returns a
1943 * character pointer. The kernel system call just
1944 * returns the length of the buffer filled (which
1945 * includes the ending '\0' character), or a negative
1946 * error value. So libc would do something like
1948 * char *getcwd(char * buf, size_t size)
1952 * retval = sys_getcwd(buf, size);
1959 asmlinkage
long sys_getcwd(char __user
*buf
, unsigned long size
)
1962 struct path pwd
, root
;
1963 char *page
= (char *) __get_free_page(GFP_USER
);
1968 read_lock(¤t
->fs
->lock
);
1969 pwd
= current
->fs
->pwd
;
1971 root
= current
->fs
->root
;
1973 read_unlock(¤t
->fs
->lock
);
1976 /* Has the current directory has been unlinked? */
1977 spin_lock(&dcache_lock
);
1978 if (IS_ROOT(pwd
.dentry
) || !d_unhashed(pwd
.dentry
)) {
1980 struct path tmp
= root
;
1983 cwd
= __d_path(&pwd
, &tmp
, page
, PAGE_SIZE
);
1984 spin_unlock(&dcache_lock
);
1986 error
= PTR_ERR(cwd
);
1991 len
= PAGE_SIZE
+ page
- cwd
;
1994 if (copy_to_user(buf
, cwd
, len
))
1998 spin_unlock(&dcache_lock
);
2003 free_page((unsigned long) page
);
2008 * Test whether new_dentry is a subdirectory of old_dentry.
2010 * Trivially implemented using the dcache structure
2014 * is_subdir - is new dentry a subdirectory of old_dentry
2015 * @new_dentry: new dentry
2016 * @old_dentry: old dentry
2018 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2019 * Returns 0 otherwise.
2020 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2023 int is_subdir(struct dentry
* new_dentry
, struct dentry
* old_dentry
)
2026 struct dentry
* saved
= new_dentry
;
2029 /* need rcu_readlock to protect against the d_parent trashing due to
2034 /* for restarting inner loop in case of seq retry */
2037 seq
= read_seqbegin(&rename_lock
);
2039 if (new_dentry
!= old_dentry
) {
2040 struct dentry
* parent
= new_dentry
->d_parent
;
2041 if (parent
== new_dentry
)
2043 new_dentry
= parent
;
2049 } while (read_seqretry(&rename_lock
, seq
));
2055 void d_genocide(struct dentry
*root
)
2057 struct dentry
*this_parent
= root
;
2058 struct list_head
*next
;
2060 spin_lock(&dcache_lock
);
2062 next
= this_parent
->d_subdirs
.next
;
2064 while (next
!= &this_parent
->d_subdirs
) {
2065 struct list_head
*tmp
= next
;
2066 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
2068 if (d_unhashed(dentry
)||!dentry
->d_inode
)
2070 if (!list_empty(&dentry
->d_subdirs
)) {
2071 this_parent
= dentry
;
2074 atomic_dec(&dentry
->d_count
);
2076 if (this_parent
!= root
) {
2077 next
= this_parent
->d_u
.d_child
.next
;
2078 atomic_dec(&this_parent
->d_count
);
2079 this_parent
= this_parent
->d_parent
;
2082 spin_unlock(&dcache_lock
);
2086 * find_inode_number - check for dentry with name
2087 * @dir: directory to check
2088 * @name: Name to find.
2090 * Check whether a dentry already exists for the given name,
2091 * and return the inode number if it has an inode. Otherwise
2094 * This routine is used to post-process directory listings for
2095 * filesystems using synthetic inode numbers, and is necessary
2096 * to keep getcwd() working.
2099 ino_t
find_inode_number(struct dentry
*dir
, struct qstr
*name
)
2101 struct dentry
* dentry
;
2104 dentry
= d_hash_and_lookup(dir
, name
);
2106 if (dentry
->d_inode
)
2107 ino
= dentry
->d_inode
->i_ino
;
2113 static __initdata
unsigned long dhash_entries
;
2114 static int __init
set_dhash_entries(char *str
)
2118 dhash_entries
= simple_strtoul(str
, &str
, 0);
2121 __setup("dhash_entries=", set_dhash_entries
);
2123 static void __init
dcache_init_early(void)
2127 /* If hashes are distributed across NUMA nodes, defer
2128 * hash allocation until vmalloc space is available.
2134 alloc_large_system_hash("Dentry cache",
2135 sizeof(struct hlist_head
),
2143 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2144 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2147 static void __init
dcache_init(void)
2152 * A constructor could be added for stable state like the lists,
2153 * but it is probably not worth it because of the cache nature
2156 dentry_cache
= KMEM_CACHE(dentry
,
2157 SLAB_RECLAIM_ACCOUNT
|SLAB_PANIC
|SLAB_MEM_SPREAD
);
2159 register_shrinker(&dcache_shrinker
);
2161 /* Hash may have been set up in dcache_init_early */
2166 alloc_large_system_hash("Dentry cache",
2167 sizeof(struct hlist_head
),
2175 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2176 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2179 /* SLAB cache for __getname() consumers */
2180 struct kmem_cache
*names_cachep __read_mostly
;
2182 /* SLAB cache for file structures */
2183 struct kmem_cache
*filp_cachep __read_mostly
;
2185 EXPORT_SYMBOL(d_genocide
);
2187 void __init
vfs_caches_init_early(void)
2189 dcache_init_early();
2193 void __init
vfs_caches_init(unsigned long mempages
)
2195 unsigned long reserve
;
2197 /* Base hash sizes on available memory, with a reserve equal to
2198 150% of current kernel size */
2200 reserve
= min((mempages
- nr_free_pages()) * 3/2, mempages
- 1);
2201 mempages
-= reserve
;
2203 names_cachep
= kmem_cache_create("names_cache", PATH_MAX
, 0,
2204 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
2206 filp_cachep
= kmem_cache_create("filp", sizeof(struct file
), 0,
2207 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
2211 files_init(mempages
);
2217 EXPORT_SYMBOL(d_alloc
);
2218 EXPORT_SYMBOL(d_alloc_anon
);
2219 EXPORT_SYMBOL(d_alloc_root
);
2220 EXPORT_SYMBOL(d_delete
);
2221 EXPORT_SYMBOL(d_find_alias
);
2222 EXPORT_SYMBOL(d_instantiate
);
2223 EXPORT_SYMBOL(d_invalidate
);
2224 EXPORT_SYMBOL(d_lookup
);
2225 EXPORT_SYMBOL(d_move
);
2226 EXPORT_SYMBOL_GPL(d_materialise_unique
);
2227 EXPORT_SYMBOL(d_path
);
2228 EXPORT_SYMBOL(d_prune_aliases
);
2229 EXPORT_SYMBOL(d_rehash
);
2230 EXPORT_SYMBOL(d_splice_alias
);
2231 EXPORT_SYMBOL(d_validate
);
2232 EXPORT_SYMBOL(dget_locked
);
2233 EXPORT_SYMBOL(dput
);
2234 EXPORT_SYMBOL(find_inode_number
);
2235 EXPORT_SYMBOL(have_submounts
);
2236 EXPORT_SYMBOL(names_cachep
);
2237 EXPORT_SYMBOL(shrink_dcache_parent
);
2238 EXPORT_SYMBOL(shrink_dcache_sb
);