4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
10 * Notes on the allocation strategy:
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
17 #include <linux/syscalls.h>
18 #include <linux/string.h>
21 #include <linux/fsnotify.h>
22 #include <linux/slab.h>
23 #include <linux/init.h>
24 #include <linux/hash.h>
25 #include <linux/cache.h>
26 #include <linux/module.h>
27 #include <linux/mount.h>
28 #include <linux/file.h>
29 #include <asm/uaccess.h>
30 #include <linux/security.h>
31 #include <linux/seqlock.h>
32 #include <linux/swap.h>
33 #include <linux/bootmem.h>
37 int sysctl_vfs_cache_pressure __read_mostly
= 100;
38 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure
);
40 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dcache_lock
);
41 __cacheline_aligned_in_smp
DEFINE_SEQLOCK(rename_lock
);
43 EXPORT_SYMBOL(dcache_lock
);
45 static struct kmem_cache
*dentry_cache __read_mostly
;
47 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
50 * This is the single most critical data structure when it comes
51 * to the dcache: the hashtable for lookups. Somebody should try
52 * to make this good - I've just made it work.
54 * This hash-function tries to avoid losing too many bits of hash
55 * information, yet avoid using a prime hash-size or similar.
57 #define D_HASHBITS d_hash_shift
58 #define D_HASHMASK d_hash_mask
60 static unsigned int d_hash_mask __read_mostly
;
61 static unsigned int d_hash_shift __read_mostly
;
62 static struct hlist_head
*dentry_hashtable __read_mostly
;
63 static LIST_HEAD(dentry_unused
);
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
);
98 static void dentry_lru_remove(struct dentry
*dentry
)
100 if (!list_empty(&dentry
->d_lru
)) {
101 list_del_init(&dentry
->d_lru
);
102 dentry_stat
.nr_unused
--;
107 * Release the dentry's inode, using the filesystem
108 * d_iput() operation if defined.
109 * Called with dcache_lock and per dentry lock held, drops both.
111 static void dentry_iput(struct dentry
* dentry
)
113 struct inode
*inode
= dentry
->d_inode
;
115 dentry
->d_inode
= NULL
;
116 list_del_init(&dentry
->d_alias
);
117 spin_unlock(&dentry
->d_lock
);
118 spin_unlock(&dcache_lock
);
120 fsnotify_inoderemove(inode
);
121 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
122 dentry
->d_op
->d_iput(dentry
, inode
);
126 spin_unlock(&dentry
->d_lock
);
127 spin_unlock(&dcache_lock
);
132 * d_kill - kill dentry and return parent
133 * @dentry: dentry to kill
135 * Called with dcache_lock and d_lock, releases both. The dentry must
136 * already be unhashed and removed from the LRU.
138 * If this is the root of the dentry tree, return NULL.
140 static struct dentry
*d_kill(struct dentry
*dentry
)
142 struct dentry
*parent
;
144 list_del(&dentry
->d_u
.d_child
);
145 dentry_stat
.nr_dentry
--; /* For d_free, below */
146 /*drops the locks, at that point nobody can reach this dentry */
148 parent
= dentry
->d_parent
;
150 return dentry
== parent
? NULL
: parent
;
156 * This is complicated by the fact that we do not want to put
157 * dentries that are no longer on any hash chain on the unused
158 * list: we'd much rather just get rid of them immediately.
160 * However, that implies that we have to traverse the dentry
161 * tree upwards to the parents which might _also_ now be
162 * scheduled for deletion (it may have been only waiting for
163 * its last child to go away).
165 * This tail recursion is done by hand as we don't want to depend
166 * on the compiler to always get this right (gcc generally doesn't).
167 * Real recursion would eat up our stack space.
171 * dput - release a dentry
172 * @dentry: dentry to release
174 * Release a dentry. This will drop the usage count and if appropriate
175 * call the dentry unlink method as well as removing it from the queues and
176 * releasing its resources. If the parent dentries were scheduled for release
177 * they too may now get deleted.
179 * no dcache lock, please.
182 void dput(struct dentry
*dentry
)
188 if (atomic_read(&dentry
->d_count
) == 1)
190 if (!atomic_dec_and_lock(&dentry
->d_count
, &dcache_lock
))
193 spin_lock(&dentry
->d_lock
);
194 if (atomic_read(&dentry
->d_count
)) {
195 spin_unlock(&dentry
->d_lock
);
196 spin_unlock(&dcache_lock
);
201 * AV: ->d_delete() is _NOT_ allowed to block now.
203 if (dentry
->d_op
&& dentry
->d_op
->d_delete
) {
204 if (dentry
->d_op
->d_delete(dentry
))
207 /* Unreachable? Get rid of it */
208 if (d_unhashed(dentry
))
210 if (list_empty(&dentry
->d_lru
)) {
211 dentry
->d_flags
|= DCACHE_REFERENCED
;
212 list_add(&dentry
->d_lru
, &dentry_unused
);
213 dentry_stat
.nr_unused
++;
215 spin_unlock(&dentry
->d_lock
);
216 spin_unlock(&dcache_lock
);
222 dentry_lru_remove(dentry
);
223 dentry
= d_kill(dentry
);
229 * d_invalidate - invalidate a dentry
230 * @dentry: dentry to invalidate
232 * Try to invalidate the dentry if it turns out to be
233 * possible. If there are other dentries that can be
234 * reached through this one we can't delete it and we
235 * return -EBUSY. On success we return 0.
240 int d_invalidate(struct dentry
* dentry
)
243 * If it's already been dropped, return OK.
245 spin_lock(&dcache_lock
);
246 if (d_unhashed(dentry
)) {
247 spin_unlock(&dcache_lock
);
251 * Check whether to do a partial shrink_dcache
252 * to get rid of unused child entries.
254 if (!list_empty(&dentry
->d_subdirs
)) {
255 spin_unlock(&dcache_lock
);
256 shrink_dcache_parent(dentry
);
257 spin_lock(&dcache_lock
);
261 * Somebody else still using it?
263 * If it's a directory, we can't drop it
264 * for fear of somebody re-populating it
265 * with children (even though dropping it
266 * would make it unreachable from the root,
267 * we might still populate it if it was a
268 * working directory or similar).
270 spin_lock(&dentry
->d_lock
);
271 if (atomic_read(&dentry
->d_count
) > 1) {
272 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
)) {
273 spin_unlock(&dentry
->d_lock
);
274 spin_unlock(&dcache_lock
);
280 spin_unlock(&dentry
->d_lock
);
281 spin_unlock(&dcache_lock
);
285 /* This should be called _only_ with dcache_lock held */
287 static inline struct dentry
* __dget_locked(struct dentry
*dentry
)
289 atomic_inc(&dentry
->d_count
);
290 dentry_lru_remove(dentry
);
294 struct dentry
* dget_locked(struct dentry
*dentry
)
296 return __dget_locked(dentry
);
300 * d_find_alias - grab a hashed alias of inode
301 * @inode: inode in question
302 * @want_discon: flag, used by d_splice_alias, to request
303 * that only a DISCONNECTED alias be returned.
305 * If inode has a hashed alias, or is a directory and has any alias,
306 * acquire the reference to alias and return it. Otherwise return NULL.
307 * Notice that if inode is a directory there can be only one alias and
308 * it can be unhashed only if it has no children, or if it is the root
311 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
312 * any other hashed alias over that one unless @want_discon is set,
313 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
316 static struct dentry
* __d_find_alias(struct inode
*inode
, int want_discon
)
318 struct list_head
*head
, *next
, *tmp
;
319 struct dentry
*alias
, *discon_alias
=NULL
;
321 head
= &inode
->i_dentry
;
322 next
= inode
->i_dentry
.next
;
323 while (next
!= head
) {
327 alias
= list_entry(tmp
, struct dentry
, d_alias
);
328 if (S_ISDIR(inode
->i_mode
) || !d_unhashed(alias
)) {
329 if (IS_ROOT(alias
) &&
330 (alias
->d_flags
& DCACHE_DISCONNECTED
))
331 discon_alias
= alias
;
332 else if (!want_discon
) {
333 __dget_locked(alias
);
339 __dget_locked(discon_alias
);
343 struct dentry
* d_find_alias(struct inode
*inode
)
345 struct dentry
*de
= NULL
;
347 if (!list_empty(&inode
->i_dentry
)) {
348 spin_lock(&dcache_lock
);
349 de
= __d_find_alias(inode
, 0);
350 spin_unlock(&dcache_lock
);
356 * Try to kill dentries associated with this inode.
357 * WARNING: you must own a reference to inode.
359 void d_prune_aliases(struct inode
*inode
)
361 struct dentry
*dentry
;
363 spin_lock(&dcache_lock
);
364 list_for_each_entry(dentry
, &inode
->i_dentry
, d_alias
) {
365 spin_lock(&dentry
->d_lock
);
366 if (!atomic_read(&dentry
->d_count
)) {
367 __dget_locked(dentry
);
369 spin_unlock(&dentry
->d_lock
);
370 spin_unlock(&dcache_lock
);
374 spin_unlock(&dentry
->d_lock
);
376 spin_unlock(&dcache_lock
);
380 * Throw away a dentry - free the inode, dput the parent. This requires that
381 * the LRU list has already been removed.
383 * Try to prune ancestors as well. This is necessary to prevent
384 * quadratic behavior of shrink_dcache_parent(), but is also expected
385 * to be beneficial in reducing dentry cache fragmentation.
387 * Called with dcache_lock, drops it and then regains.
388 * Called with dentry->d_lock held, drops it.
390 static void prune_one_dentry(struct dentry
* dentry
)
393 dentry
= d_kill(dentry
);
396 * Prune ancestors. Locking is simpler than in dput(),
397 * because dcache_lock needs to be taken anyway.
399 spin_lock(&dcache_lock
);
401 if (!atomic_dec_and_lock(&dentry
->d_count
, &dentry
->d_lock
))
404 if (dentry
->d_op
&& dentry
->d_op
->d_delete
)
405 dentry
->d_op
->d_delete(dentry
);
406 dentry_lru_remove(dentry
);
408 dentry
= d_kill(dentry
);
409 spin_lock(&dcache_lock
);
414 * prune_dcache - shrink the dcache
415 * @count: number of entries to try and free
416 * @sb: if given, ignore dentries for other superblocks
417 * which are being unmounted.
419 * Shrink the dcache. This is done when we need
420 * more memory, or simply when we need to unmount
421 * something (at which point we need to unuse
424 * This function may fail to free any resources if
425 * all the dentries are in use.
428 static void prune_dcache(int count
, struct super_block
*sb
)
430 spin_lock(&dcache_lock
);
431 for (; count
; count
--) {
432 struct dentry
*dentry
;
433 struct list_head
*tmp
;
434 struct rw_semaphore
*s_umount
;
436 cond_resched_lock(&dcache_lock
);
438 tmp
= dentry_unused
.prev
;
440 /* Try to find a dentry for this sb, but don't try
441 * too hard, if they aren't near the tail they will
442 * be moved down again soon
445 while (skip
&& tmp
!= &dentry_unused
&&
446 list_entry(tmp
, struct dentry
, d_lru
)->d_sb
!= sb
) {
451 if (tmp
== &dentry_unused
)
454 prefetch(dentry_unused
.prev
);
455 dentry_stat
.nr_unused
--;
456 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
458 spin_lock(&dentry
->d_lock
);
460 * We found an inuse dentry which was not removed from
461 * dentry_unused because of laziness during lookup. Do not free
462 * it - just keep it off the dentry_unused list.
464 if (atomic_read(&dentry
->d_count
)) {
465 spin_unlock(&dentry
->d_lock
);
468 /* If the dentry was recently referenced, don't free it. */
469 if (dentry
->d_flags
& DCACHE_REFERENCED
) {
470 dentry
->d_flags
&= ~DCACHE_REFERENCED
;
471 list_add(&dentry
->d_lru
, &dentry_unused
);
472 dentry_stat
.nr_unused
++;
473 spin_unlock(&dentry
->d_lock
);
477 * If the dentry is not DCACHED_REFERENCED, it is time
478 * to remove it from the dcache, provided the super block is
479 * NULL (which means we are trying to reclaim memory)
480 * or this dentry belongs to the same super block that
484 * If this dentry is for "my" filesystem, then I can prune it
485 * without taking the s_umount lock (I already hold it).
487 if (sb
&& dentry
->d_sb
== sb
) {
488 prune_one_dentry(dentry
);
492 * ...otherwise we need to be sure this filesystem isn't being
493 * unmounted, otherwise we could race with
494 * generic_shutdown_super(), and end up holding a reference to
495 * an inode while the filesystem is unmounted.
496 * So we try to get s_umount, and make sure s_root isn't NULL.
497 * (Take a local copy of s_umount to avoid a use-after-free of
500 s_umount
= &dentry
->d_sb
->s_umount
;
501 if (down_read_trylock(s_umount
)) {
502 if (dentry
->d_sb
->s_root
!= NULL
) {
503 prune_one_dentry(dentry
);
509 spin_unlock(&dentry
->d_lock
);
511 * Insert dentry at the head of the list as inserting at the
512 * tail leads to a cycle.
514 list_add(&dentry
->d_lru
, &dentry_unused
);
515 dentry_stat
.nr_unused
++;
517 spin_unlock(&dcache_lock
);
521 * Shrink the dcache for the specified super block.
522 * This allows us to unmount a device without disturbing
523 * the dcache for the other devices.
525 * This implementation makes just two traversals of the
526 * unused list. On the first pass we move the selected
527 * dentries to the most recent end, and on the second
528 * pass we free them. The second pass must restart after
529 * each dput(), but since the target dentries are all at
530 * the end, it's really just a single traversal.
534 * shrink_dcache_sb - shrink dcache for a superblock
537 * Shrink the dcache for the specified super block. This
538 * is used to free the dcache before unmounting a file
542 void shrink_dcache_sb(struct super_block
* sb
)
544 struct list_head
*tmp
, *next
;
545 struct dentry
*dentry
;
548 * Pass one ... move the dentries for the specified
549 * superblock to the most recent end of the unused list.
551 spin_lock(&dcache_lock
);
552 list_for_each_prev_safe(tmp
, next
, &dentry_unused
) {
553 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
554 if (dentry
->d_sb
!= sb
)
556 list_move_tail(tmp
, &dentry_unused
);
560 * Pass two ... free the dentries for this superblock.
563 list_for_each_prev_safe(tmp
, next
, &dentry_unused
) {
564 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
565 if (dentry
->d_sb
!= sb
)
567 dentry_stat
.nr_unused
--;
569 spin_lock(&dentry
->d_lock
);
570 if (atomic_read(&dentry
->d_count
)) {
571 spin_unlock(&dentry
->d_lock
);
574 prune_one_dentry(dentry
);
575 cond_resched_lock(&dcache_lock
);
578 spin_unlock(&dcache_lock
);
582 * destroy a single subtree of dentries for unmount
583 * - see the comments on shrink_dcache_for_umount() for a description of the
586 static void shrink_dcache_for_umount_subtree(struct dentry
*dentry
)
588 struct dentry
*parent
;
589 unsigned detached
= 0;
591 BUG_ON(!IS_ROOT(dentry
));
593 /* detach this root from the system */
594 spin_lock(&dcache_lock
);
595 dentry_lru_remove(dentry
);
597 spin_unlock(&dcache_lock
);
600 /* descend to the first leaf in the current subtree */
601 while (!list_empty(&dentry
->d_subdirs
)) {
604 /* this is a branch with children - detach all of them
605 * from the system in one go */
606 spin_lock(&dcache_lock
);
607 list_for_each_entry(loop
, &dentry
->d_subdirs
,
609 dentry_lru_remove(loop
);
611 cond_resched_lock(&dcache_lock
);
613 spin_unlock(&dcache_lock
);
615 /* move to the first child */
616 dentry
= list_entry(dentry
->d_subdirs
.next
,
617 struct dentry
, d_u
.d_child
);
620 /* consume the dentries from this leaf up through its parents
621 * until we find one with children or run out altogether */
625 if (atomic_read(&dentry
->d_count
) != 0) {
627 "BUG: Dentry %p{i=%lx,n=%s}"
629 " [unmount of %s %s]\n",
632 dentry
->d_inode
->i_ino
: 0UL,
634 atomic_read(&dentry
->d_count
),
635 dentry
->d_sb
->s_type
->name
,
640 parent
= dentry
->d_parent
;
641 if (parent
== dentry
)
644 atomic_dec(&parent
->d_count
);
646 list_del(&dentry
->d_u
.d_child
);
649 inode
= dentry
->d_inode
;
651 dentry
->d_inode
= NULL
;
652 list_del_init(&dentry
->d_alias
);
653 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
654 dentry
->d_op
->d_iput(dentry
, inode
);
661 /* finished when we fall off the top of the tree,
662 * otherwise we ascend to the parent and move to the
663 * next sibling if there is one */
669 } while (list_empty(&dentry
->d_subdirs
));
671 dentry
= list_entry(dentry
->d_subdirs
.next
,
672 struct dentry
, d_u
.d_child
);
675 /* several dentries were freed, need to correct nr_dentry */
676 spin_lock(&dcache_lock
);
677 dentry_stat
.nr_dentry
-= detached
;
678 spin_unlock(&dcache_lock
);
682 * destroy the dentries attached to a superblock on unmounting
683 * - we don't need to use dentry->d_lock, and only need dcache_lock when
684 * removing the dentry from the system lists and hashes because:
685 * - the superblock is detached from all mountings and open files, so the
686 * dentry trees will not be rearranged by the VFS
687 * - s_umount is write-locked, so the memory pressure shrinker will ignore
688 * any dentries belonging to this superblock that it comes across
689 * - the filesystem itself is no longer permitted to rearrange the dentries
692 void shrink_dcache_for_umount(struct super_block
*sb
)
694 struct dentry
*dentry
;
696 if (down_read_trylock(&sb
->s_umount
))
701 atomic_dec(&dentry
->d_count
);
702 shrink_dcache_for_umount_subtree(dentry
);
704 while (!hlist_empty(&sb
->s_anon
)) {
705 dentry
= hlist_entry(sb
->s_anon
.first
, struct dentry
, d_hash
);
706 shrink_dcache_for_umount_subtree(dentry
);
711 * Search for at least 1 mount point in the dentry's subdirs.
712 * We descend to the next level whenever the d_subdirs
713 * list is non-empty and continue searching.
717 * have_submounts - check for mounts over a dentry
718 * @parent: dentry to check.
720 * Return true if the parent or its subdirectories contain
724 int have_submounts(struct dentry
*parent
)
726 struct dentry
*this_parent
= parent
;
727 struct list_head
*next
;
729 spin_lock(&dcache_lock
);
730 if (d_mountpoint(parent
))
733 next
= this_parent
->d_subdirs
.next
;
735 while (next
!= &this_parent
->d_subdirs
) {
736 struct list_head
*tmp
= next
;
737 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
739 /* Have we found a mount point ? */
740 if (d_mountpoint(dentry
))
742 if (!list_empty(&dentry
->d_subdirs
)) {
743 this_parent
= dentry
;
748 * All done at this level ... ascend and resume the search.
750 if (this_parent
!= parent
) {
751 next
= this_parent
->d_u
.d_child
.next
;
752 this_parent
= this_parent
->d_parent
;
755 spin_unlock(&dcache_lock
);
756 return 0; /* No mount points found in tree */
758 spin_unlock(&dcache_lock
);
763 * Search the dentry child list for the specified parent,
764 * and move any unused dentries to the end of the unused
765 * list for prune_dcache(). We descend to the next level
766 * whenever the d_subdirs list is non-empty and continue
769 * It returns zero iff there are no unused children,
770 * otherwise it returns the number of children moved to
771 * the end of the unused list. This may not be the total
772 * number of unused children, because select_parent can
773 * drop the lock and return early due to latency
776 static int select_parent(struct dentry
* parent
)
778 struct dentry
*this_parent
= parent
;
779 struct list_head
*next
;
782 spin_lock(&dcache_lock
);
784 next
= this_parent
->d_subdirs
.next
;
786 while (next
!= &this_parent
->d_subdirs
) {
787 struct list_head
*tmp
= next
;
788 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
791 dentry_lru_remove(dentry
);
793 * move only zero ref count dentries to the end
794 * of the unused list for prune_dcache
796 if (!atomic_read(&dentry
->d_count
)) {
797 list_add_tail(&dentry
->d_lru
, &dentry_unused
);
798 dentry_stat
.nr_unused
++;
803 * We can return to the caller if we have found some (this
804 * ensures forward progress). We'll be coming back to find
807 if (found
&& need_resched())
811 * Descend a level if the d_subdirs list is non-empty.
813 if (!list_empty(&dentry
->d_subdirs
)) {
814 this_parent
= dentry
;
819 * All done at this level ... ascend and resume the search.
821 if (this_parent
!= parent
) {
822 next
= this_parent
->d_u
.d_child
.next
;
823 this_parent
= this_parent
->d_parent
;
827 spin_unlock(&dcache_lock
);
832 * shrink_dcache_parent - prune dcache
833 * @parent: parent of entries to prune
835 * Prune the dcache to remove unused children of the parent dentry.
838 void shrink_dcache_parent(struct dentry
* parent
)
842 while ((found
= select_parent(parent
)) != 0)
843 prune_dcache(found
, parent
->d_sb
);
847 * Scan `nr' dentries and return the number which remain.
849 * We need to avoid reentering the filesystem if the caller is performing a
850 * GFP_NOFS allocation attempt. One example deadlock is:
852 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
853 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
854 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
856 * In this case we return -1 to tell the caller that we baled.
858 static int shrink_dcache_memory(int nr
, gfp_t gfp_mask
)
861 if (!(gfp_mask
& __GFP_FS
))
863 prune_dcache(nr
, NULL
);
865 return (dentry_stat
.nr_unused
/ 100) * sysctl_vfs_cache_pressure
;
868 static struct shrinker dcache_shrinker
= {
869 .shrink
= shrink_dcache_memory
,
870 .seeks
= DEFAULT_SEEKS
,
874 * d_alloc - allocate a dcache entry
875 * @parent: parent of entry to allocate
876 * @name: qstr of the name
878 * Allocates a dentry. It returns %NULL if there is insufficient memory
879 * available. On a success the dentry is returned. The name passed in is
880 * copied and the copy passed in may be reused after this call.
883 struct dentry
*d_alloc(struct dentry
* parent
, const struct qstr
*name
)
885 struct dentry
*dentry
;
888 dentry
= kmem_cache_alloc(dentry_cache
, GFP_KERNEL
);
892 if (name
->len
> DNAME_INLINE_LEN
-1) {
893 dname
= kmalloc(name
->len
+ 1, GFP_KERNEL
);
895 kmem_cache_free(dentry_cache
, dentry
);
899 dname
= dentry
->d_iname
;
901 dentry
->d_name
.name
= dname
;
903 dentry
->d_name
.len
= name
->len
;
904 dentry
->d_name
.hash
= name
->hash
;
905 memcpy(dname
, name
->name
, name
->len
);
906 dname
[name
->len
] = 0;
908 atomic_set(&dentry
->d_count
, 1);
909 dentry
->d_flags
= DCACHE_UNHASHED
;
910 spin_lock_init(&dentry
->d_lock
);
911 dentry
->d_inode
= NULL
;
912 dentry
->d_parent
= NULL
;
915 dentry
->d_fsdata
= NULL
;
916 dentry
->d_mounted
= 0;
917 #ifdef CONFIG_PROFILING
918 dentry
->d_cookie
= NULL
;
920 INIT_HLIST_NODE(&dentry
->d_hash
);
921 INIT_LIST_HEAD(&dentry
->d_lru
);
922 INIT_LIST_HEAD(&dentry
->d_subdirs
);
923 INIT_LIST_HEAD(&dentry
->d_alias
);
926 dentry
->d_parent
= dget(parent
);
927 dentry
->d_sb
= parent
->d_sb
;
929 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
932 spin_lock(&dcache_lock
);
934 list_add(&dentry
->d_u
.d_child
, &parent
->d_subdirs
);
935 dentry_stat
.nr_dentry
++;
936 spin_unlock(&dcache_lock
);
941 struct dentry
*d_alloc_name(struct dentry
*parent
, const char *name
)
946 q
.len
= strlen(name
);
947 q
.hash
= full_name_hash(q
.name
, q
.len
);
948 return d_alloc(parent
, &q
);
952 * d_instantiate - fill in inode information for a dentry
953 * @entry: dentry to complete
954 * @inode: inode to attach to this dentry
956 * Fill in inode information in the entry.
958 * This turns negative dentries into productive full members
961 * NOTE! This assumes that the inode count has been incremented
962 * (or otherwise set) by the caller to indicate that it is now
963 * in use by the dcache.
966 void d_instantiate(struct dentry
*entry
, struct inode
* inode
)
968 BUG_ON(!list_empty(&entry
->d_alias
));
969 spin_lock(&dcache_lock
);
971 list_add(&entry
->d_alias
, &inode
->i_dentry
);
972 entry
->d_inode
= inode
;
973 fsnotify_d_instantiate(entry
, inode
);
974 spin_unlock(&dcache_lock
);
975 security_d_instantiate(entry
, inode
);
979 * d_instantiate_unique - instantiate a non-aliased dentry
980 * @entry: dentry to instantiate
981 * @inode: inode to attach to this dentry
983 * Fill in inode information in the entry. On success, it returns NULL.
984 * If an unhashed alias of "entry" already exists, then we return the
985 * aliased dentry instead and drop one reference to inode.
987 * Note that in order to avoid conflicts with rename() etc, the caller
988 * had better be holding the parent directory semaphore.
990 * This also 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.
994 static struct dentry
*__d_instantiate_unique(struct dentry
*entry
,
997 struct dentry
*alias
;
998 int len
= entry
->d_name
.len
;
999 const char *name
= entry
->d_name
.name
;
1000 unsigned int hash
= entry
->d_name
.hash
;
1003 entry
->d_inode
= NULL
;
1007 list_for_each_entry(alias
, &inode
->i_dentry
, d_alias
) {
1008 struct qstr
*qstr
= &alias
->d_name
;
1010 if (qstr
->hash
!= hash
)
1012 if (alias
->d_parent
!= entry
->d_parent
)
1014 if (qstr
->len
!= len
)
1016 if (memcmp(qstr
->name
, name
, len
))
1022 list_add(&entry
->d_alias
, &inode
->i_dentry
);
1023 entry
->d_inode
= inode
;
1024 fsnotify_d_instantiate(entry
, inode
);
1028 struct dentry
*d_instantiate_unique(struct dentry
*entry
, struct inode
*inode
)
1030 struct dentry
*result
;
1032 BUG_ON(!list_empty(&entry
->d_alias
));
1034 spin_lock(&dcache_lock
);
1035 result
= __d_instantiate_unique(entry
, inode
);
1036 spin_unlock(&dcache_lock
);
1039 security_d_instantiate(entry
, inode
);
1043 BUG_ON(!d_unhashed(result
));
1048 EXPORT_SYMBOL(d_instantiate_unique
);
1051 * d_alloc_root - allocate root dentry
1052 * @root_inode: inode to allocate the root for
1054 * Allocate a root ("/") dentry for the inode given. The inode is
1055 * instantiated and returned. %NULL is returned if there is insufficient
1056 * memory or the inode passed is %NULL.
1059 struct dentry
* d_alloc_root(struct inode
* root_inode
)
1061 struct dentry
*res
= NULL
;
1064 static const struct qstr name
= { .name
= "/", .len
= 1 };
1066 res
= d_alloc(NULL
, &name
);
1068 res
->d_sb
= root_inode
->i_sb
;
1069 res
->d_parent
= res
;
1070 d_instantiate(res
, root_inode
);
1076 static inline struct hlist_head
*d_hash(struct dentry
*parent
,
1079 hash
+= ((unsigned long) parent
^ GOLDEN_RATIO_PRIME
) / L1_CACHE_BYTES
;
1080 hash
= hash
^ ((hash
^ GOLDEN_RATIO_PRIME
) >> D_HASHBITS
);
1081 return dentry_hashtable
+ (hash
& D_HASHMASK
);
1085 * d_alloc_anon - allocate an anonymous dentry
1086 * @inode: inode to allocate the dentry for
1088 * This is similar to d_alloc_root. It is used by filesystems when
1089 * creating a dentry for a given inode, often in the process of
1090 * mapping a filehandle to a dentry. The returned dentry may be
1091 * anonymous, or may have a full name (if the inode was already
1092 * in the cache). The file system may need to make further
1093 * efforts to connect this dentry into the dcache properly.
1095 * When called on a directory inode, we must ensure that
1096 * the inode only ever has one dentry. If a dentry is
1097 * found, that is returned instead of allocating a new one.
1099 * On successful return, the reference to the inode has been transferred
1100 * to the dentry. If %NULL is returned (indicating kmalloc failure),
1101 * the reference on the inode has not been released.
1104 struct dentry
* d_alloc_anon(struct inode
*inode
)
1106 static const struct qstr anonstring
= { .name
= "" };
1110 if ((res
= d_find_alias(inode
))) {
1115 tmp
= d_alloc(NULL
, &anonstring
);
1119 tmp
->d_parent
= tmp
; /* make sure dput doesn't croak */
1121 spin_lock(&dcache_lock
);
1122 res
= __d_find_alias(inode
, 0);
1124 /* attach a disconnected dentry */
1127 spin_lock(&res
->d_lock
);
1128 res
->d_sb
= inode
->i_sb
;
1129 res
->d_parent
= res
;
1130 res
->d_inode
= inode
;
1131 res
->d_flags
|= DCACHE_DISCONNECTED
;
1132 res
->d_flags
&= ~DCACHE_UNHASHED
;
1133 list_add(&res
->d_alias
, &inode
->i_dentry
);
1134 hlist_add_head(&res
->d_hash
, &inode
->i_sb
->s_anon
);
1135 spin_unlock(&res
->d_lock
);
1137 inode
= NULL
; /* don't drop reference */
1139 spin_unlock(&dcache_lock
);
1150 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1151 * @inode: the inode which may have a disconnected dentry
1152 * @dentry: a negative dentry which we want to point to the inode.
1154 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1155 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1156 * and return it, else simply d_add the inode to the dentry and return NULL.
1158 * This is needed in the lookup routine of any filesystem that is exportable
1159 * (via knfsd) so that we can build dcache paths to directories effectively.
1161 * If a dentry was found and moved, then it is returned. Otherwise NULL
1162 * is returned. This matches the expected return value of ->lookup.
1165 struct dentry
*d_splice_alias(struct inode
*inode
, struct dentry
*dentry
)
1167 struct dentry
*new = NULL
;
1169 if (inode
&& S_ISDIR(inode
->i_mode
)) {
1170 spin_lock(&dcache_lock
);
1171 new = __d_find_alias(inode
, 1);
1173 BUG_ON(!(new->d_flags
& DCACHE_DISCONNECTED
));
1174 fsnotify_d_instantiate(new, inode
);
1175 spin_unlock(&dcache_lock
);
1176 security_d_instantiate(new, inode
);
1178 d_move(new, dentry
);
1181 /* d_instantiate takes dcache_lock, so we do it by hand */
1182 list_add(&dentry
->d_alias
, &inode
->i_dentry
);
1183 dentry
->d_inode
= inode
;
1184 fsnotify_d_instantiate(dentry
, inode
);
1185 spin_unlock(&dcache_lock
);
1186 security_d_instantiate(dentry
, inode
);
1190 d_add(dentry
, inode
);
1196 * d_lookup - search for a dentry
1197 * @parent: parent dentry
1198 * @name: qstr of name we wish to find
1200 * Searches the children of the parent dentry for the name in question. If
1201 * the dentry is found its reference count is incremented and the dentry
1202 * is returned. The caller must use d_put to free the entry when it has
1203 * finished using it. %NULL is returned on failure.
1205 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1206 * Memory barriers are used while updating and doing lockless traversal.
1207 * To avoid races with d_move while rename is happening, d_lock is used.
1209 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1210 * and name pointer in one structure pointed by d_qstr.
1212 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1213 * lookup is going on.
1215 * dentry_unused list is not updated even if lookup finds the required dentry
1216 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1217 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1220 * d_lookup() is protected against the concurrent renames in some unrelated
1221 * directory using the seqlockt_t rename_lock.
1224 struct dentry
* d_lookup(struct dentry
* parent
, struct qstr
* name
)
1226 struct dentry
* dentry
= NULL
;
1230 seq
= read_seqbegin(&rename_lock
);
1231 dentry
= __d_lookup(parent
, name
);
1234 } while (read_seqretry(&rename_lock
, seq
));
1238 struct dentry
* __d_lookup(struct dentry
* parent
, struct qstr
* name
)
1240 unsigned int len
= name
->len
;
1241 unsigned int hash
= name
->hash
;
1242 const unsigned char *str
= name
->name
;
1243 struct hlist_head
*head
= d_hash(parent
,hash
);
1244 struct dentry
*found
= NULL
;
1245 struct hlist_node
*node
;
1246 struct dentry
*dentry
;
1250 hlist_for_each_entry_rcu(dentry
, node
, head
, d_hash
) {
1253 if (dentry
->d_name
.hash
!= hash
)
1255 if (dentry
->d_parent
!= parent
)
1258 spin_lock(&dentry
->d_lock
);
1261 * Recheck the dentry after taking the lock - d_move may have
1262 * changed things. Don't bother checking the hash because we're
1263 * about to compare the whole name anyway.
1265 if (dentry
->d_parent
!= parent
)
1269 * It is safe to compare names since d_move() cannot
1270 * change the qstr (protected by d_lock).
1272 qstr
= &dentry
->d_name
;
1273 if (parent
->d_op
&& parent
->d_op
->d_compare
) {
1274 if (parent
->d_op
->d_compare(parent
, qstr
, name
))
1277 if (qstr
->len
!= len
)
1279 if (memcmp(qstr
->name
, str
, len
))
1283 if (!d_unhashed(dentry
)) {
1284 atomic_inc(&dentry
->d_count
);
1287 spin_unlock(&dentry
->d_lock
);
1290 spin_unlock(&dentry
->d_lock
);
1298 * d_hash_and_lookup - hash the qstr then search for a dentry
1299 * @dir: Directory to search in
1300 * @name: qstr of name we wish to find
1302 * On hash failure or on lookup failure NULL is returned.
1304 struct dentry
*d_hash_and_lookup(struct dentry
*dir
, struct qstr
*name
)
1306 struct dentry
*dentry
= NULL
;
1309 * Check for a fs-specific hash function. Note that we must
1310 * calculate the standard hash first, as the d_op->d_hash()
1311 * routine may choose to leave the hash value unchanged.
1313 name
->hash
= full_name_hash(name
->name
, name
->len
);
1314 if (dir
->d_op
&& dir
->d_op
->d_hash
) {
1315 if (dir
->d_op
->d_hash(dir
, name
) < 0)
1318 dentry
= d_lookup(dir
, name
);
1324 * d_validate - verify dentry provided from insecure source
1325 * @dentry: The dentry alleged to be valid child of @dparent
1326 * @dparent: The parent dentry (known to be valid)
1327 * @hash: Hash of the dentry
1328 * @len: Length of the name
1330 * An insecure source has sent us a dentry, here we verify it and dget() it.
1331 * This is used by ncpfs in its readdir implementation.
1332 * Zero is returned in the dentry is invalid.
1335 int d_validate(struct dentry
*dentry
, struct dentry
*dparent
)
1337 struct hlist_head
*base
;
1338 struct hlist_node
*lhp
;
1340 /* Check whether the ptr might be valid at all.. */
1341 if (!kmem_ptr_validate(dentry_cache
, dentry
))
1344 if (dentry
->d_parent
!= dparent
)
1347 spin_lock(&dcache_lock
);
1348 base
= d_hash(dparent
, dentry
->d_name
.hash
);
1349 hlist_for_each(lhp
,base
) {
1350 /* hlist_for_each_entry_rcu() not required for d_hash list
1351 * as it is parsed under dcache_lock
1353 if (dentry
== hlist_entry(lhp
, struct dentry
, d_hash
)) {
1354 __dget_locked(dentry
);
1355 spin_unlock(&dcache_lock
);
1359 spin_unlock(&dcache_lock
);
1365 * When a file is deleted, we have two options:
1366 * - turn this dentry into a negative dentry
1367 * - unhash this dentry and free it.
1369 * Usually, we want to just turn this into
1370 * a negative dentry, but if anybody else is
1371 * currently using the dentry or the inode
1372 * we can't do that and we fall back on removing
1373 * it from the hash queues and waiting for
1374 * it to be deleted later when it has no users
1378 * d_delete - delete a dentry
1379 * @dentry: The dentry to delete
1381 * Turn the dentry into a negative dentry if possible, otherwise
1382 * remove it from the hash queues so it can be deleted later
1385 void d_delete(struct dentry
* dentry
)
1389 * Are we the only user?
1391 spin_lock(&dcache_lock
);
1392 spin_lock(&dentry
->d_lock
);
1393 isdir
= S_ISDIR(dentry
->d_inode
->i_mode
);
1394 if (atomic_read(&dentry
->d_count
) == 1) {
1395 dentry_iput(dentry
);
1396 fsnotify_nameremove(dentry
, isdir
);
1400 if (!d_unhashed(dentry
))
1403 spin_unlock(&dentry
->d_lock
);
1404 spin_unlock(&dcache_lock
);
1406 fsnotify_nameremove(dentry
, isdir
);
1409 static void __d_rehash(struct dentry
* entry
, struct hlist_head
*list
)
1412 entry
->d_flags
&= ~DCACHE_UNHASHED
;
1413 hlist_add_head_rcu(&entry
->d_hash
, list
);
1416 static void _d_rehash(struct dentry
* entry
)
1418 __d_rehash(entry
, d_hash(entry
->d_parent
, entry
->d_name
.hash
));
1422 * d_rehash - add an entry back to the hash
1423 * @entry: dentry to add to the hash
1425 * Adds a dentry to the hash according to its name.
1428 void d_rehash(struct dentry
* entry
)
1430 spin_lock(&dcache_lock
);
1431 spin_lock(&entry
->d_lock
);
1433 spin_unlock(&entry
->d_lock
);
1434 spin_unlock(&dcache_lock
);
1437 #define do_switch(x,y) do { \
1438 __typeof__ (x) __tmp = x; \
1439 x = y; y = __tmp; } while (0)
1442 * When switching names, the actual string doesn't strictly have to
1443 * be preserved in the target - because we're dropping the target
1444 * anyway. As such, we can just do a simple memcpy() to copy over
1445 * the new name before we switch.
1447 * Note that we have to be a lot more careful about getting the hash
1448 * switched - we have to switch the hash value properly even if it
1449 * then no longer matches the actual (corrupted) string of the target.
1450 * The hash value has to match the hash queue that the dentry is on..
1452 static void switch_names(struct dentry
*dentry
, struct dentry
*target
)
1454 if (dname_external(target
)) {
1455 if (dname_external(dentry
)) {
1457 * Both external: swap the pointers
1459 do_switch(target
->d_name
.name
, dentry
->d_name
.name
);
1462 * dentry:internal, target:external. Steal target's
1463 * storage and make target internal.
1465 memcpy(target
->d_iname
, dentry
->d_name
.name
,
1466 dentry
->d_name
.len
+ 1);
1467 dentry
->d_name
.name
= target
->d_name
.name
;
1468 target
->d_name
.name
= target
->d_iname
;
1471 if (dname_external(dentry
)) {
1473 * dentry:external, target:internal. Give dentry's
1474 * storage to target and make dentry internal
1476 memcpy(dentry
->d_iname
, target
->d_name
.name
,
1477 target
->d_name
.len
+ 1);
1478 target
->d_name
.name
= dentry
->d_name
.name
;
1479 dentry
->d_name
.name
= dentry
->d_iname
;
1482 * Both are internal. Just copy target to dentry
1484 memcpy(dentry
->d_iname
, target
->d_name
.name
,
1485 target
->d_name
.len
+ 1);
1491 * We cannibalize "target" when moving dentry on top of it,
1492 * because it's going to be thrown away anyway. We could be more
1493 * polite about it, though.
1495 * This forceful removal will result in ugly /proc output if
1496 * somebody holds a file open that got deleted due to a rename.
1497 * We could be nicer about the deleted file, and let it show
1498 * up under the name it had before it was deleted rather than
1499 * under the original name of the file that was moved on top of it.
1503 * d_move_locked - move a dentry
1504 * @dentry: entry to move
1505 * @target: new dentry
1507 * Update the dcache to reflect the move of a file name. Negative
1508 * dcache entries should not be moved in this way.
1510 static void d_move_locked(struct dentry
* dentry
, struct dentry
* target
)
1512 struct hlist_head
*list
;
1514 if (!dentry
->d_inode
)
1515 printk(KERN_WARNING
"VFS: moving negative dcache entry\n");
1517 write_seqlock(&rename_lock
);
1519 * XXXX: do we really need to take target->d_lock?
1521 if (target
< dentry
) {
1522 spin_lock(&target
->d_lock
);
1523 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1525 spin_lock(&dentry
->d_lock
);
1526 spin_lock_nested(&target
->d_lock
, DENTRY_D_LOCK_NESTED
);
1529 /* Move the dentry to the target hash queue, if on different bucket */
1530 if (d_unhashed(dentry
))
1531 goto already_unhashed
;
1533 hlist_del_rcu(&dentry
->d_hash
);
1536 list
= d_hash(target
->d_parent
, target
->d_name
.hash
);
1537 __d_rehash(dentry
, list
);
1539 /* Unhash the target: dput() will then get rid of it */
1542 list_del(&dentry
->d_u
.d_child
);
1543 list_del(&target
->d_u
.d_child
);
1545 /* Switch the names.. */
1546 switch_names(dentry
, target
);
1547 do_switch(dentry
->d_name
.len
, target
->d_name
.len
);
1548 do_switch(dentry
->d_name
.hash
, target
->d_name
.hash
);
1550 /* ... and switch the parents */
1551 if (IS_ROOT(dentry
)) {
1552 dentry
->d_parent
= target
->d_parent
;
1553 target
->d_parent
= target
;
1554 INIT_LIST_HEAD(&target
->d_u
.d_child
);
1556 do_switch(dentry
->d_parent
, target
->d_parent
);
1558 /* And add them back to the (new) parent lists */
1559 list_add(&target
->d_u
.d_child
, &target
->d_parent
->d_subdirs
);
1562 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
1563 spin_unlock(&target
->d_lock
);
1564 fsnotify_d_move(dentry
);
1565 spin_unlock(&dentry
->d_lock
);
1566 write_sequnlock(&rename_lock
);
1570 * d_move - move a dentry
1571 * @dentry: entry to move
1572 * @target: new dentry
1574 * Update the dcache to reflect the move of a file name. Negative
1575 * dcache entries should not be moved in this way.
1578 void d_move(struct dentry
* dentry
, struct dentry
* target
)
1580 spin_lock(&dcache_lock
);
1581 d_move_locked(dentry
, target
);
1582 spin_unlock(&dcache_lock
);
1586 * Helper that returns 1 if p1 is a parent of p2, else 0
1588 static int d_isparent(struct dentry
*p1
, struct dentry
*p2
)
1592 for (p
= p2
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1593 if (p
->d_parent
== p1
)
1600 * This helper attempts to cope with remotely renamed directories
1602 * It assumes that the caller is already holding
1603 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1605 * Note: If ever the locking in lock_rename() changes, then please
1606 * remember to update this too...
1608 * On return, dcache_lock will have been unlocked.
1610 static struct dentry
*__d_unalias(struct dentry
*dentry
, struct dentry
*alias
)
1612 struct mutex
*m1
= NULL
, *m2
= NULL
;
1615 /* If alias and dentry share a parent, then no extra locks required */
1616 if (alias
->d_parent
== dentry
->d_parent
)
1619 /* Check for loops */
1620 ret
= ERR_PTR(-ELOOP
);
1621 if (d_isparent(alias
, dentry
))
1624 /* See lock_rename() */
1625 ret
= ERR_PTR(-EBUSY
);
1626 if (!mutex_trylock(&dentry
->d_sb
->s_vfs_rename_mutex
))
1628 m1
= &dentry
->d_sb
->s_vfs_rename_mutex
;
1629 if (!mutex_trylock(&alias
->d_parent
->d_inode
->i_mutex
))
1631 m2
= &alias
->d_parent
->d_inode
->i_mutex
;
1633 d_move_locked(alias
, dentry
);
1636 spin_unlock(&dcache_lock
);
1645 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1646 * named dentry in place of the dentry to be replaced.
1648 static void __d_materialise_dentry(struct dentry
*dentry
, struct dentry
*anon
)
1650 struct dentry
*dparent
, *aparent
;
1652 switch_names(dentry
, anon
);
1653 do_switch(dentry
->d_name
.len
, anon
->d_name
.len
);
1654 do_switch(dentry
->d_name
.hash
, anon
->d_name
.hash
);
1656 dparent
= dentry
->d_parent
;
1657 aparent
= anon
->d_parent
;
1659 dentry
->d_parent
= (aparent
== anon
) ? dentry
: aparent
;
1660 list_del(&dentry
->d_u
.d_child
);
1661 if (!IS_ROOT(dentry
))
1662 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
1664 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
1666 anon
->d_parent
= (dparent
== dentry
) ? anon
: dparent
;
1667 list_del(&anon
->d_u
.d_child
);
1669 list_add(&anon
->d_u
.d_child
, &anon
->d_parent
->d_subdirs
);
1671 INIT_LIST_HEAD(&anon
->d_u
.d_child
);
1673 anon
->d_flags
&= ~DCACHE_DISCONNECTED
;
1677 * d_materialise_unique - introduce an inode into the tree
1678 * @dentry: candidate dentry
1679 * @inode: inode to bind to the dentry, to which aliases may be attached
1681 * Introduces an dentry into the tree, substituting an extant disconnected
1682 * root directory alias in its place if there is one
1684 struct dentry
*d_materialise_unique(struct dentry
*dentry
, struct inode
*inode
)
1686 struct dentry
*actual
;
1688 BUG_ON(!d_unhashed(dentry
));
1690 spin_lock(&dcache_lock
);
1694 dentry
->d_inode
= NULL
;
1698 if (S_ISDIR(inode
->i_mode
)) {
1699 struct dentry
*alias
;
1701 /* Does an aliased dentry already exist? */
1702 alias
= __d_find_alias(inode
, 0);
1705 /* Is this an anonymous mountpoint that we could splice
1707 if (IS_ROOT(alias
)) {
1708 spin_lock(&alias
->d_lock
);
1709 __d_materialise_dentry(dentry
, alias
);
1713 /* Nope, but we must(!) avoid directory aliasing */
1714 actual
= __d_unalias(dentry
, alias
);
1721 /* Add a unique reference */
1722 actual
= __d_instantiate_unique(dentry
, inode
);
1725 else if (unlikely(!d_unhashed(actual
)))
1726 goto shouldnt_be_hashed
;
1729 spin_lock(&actual
->d_lock
);
1732 spin_unlock(&actual
->d_lock
);
1733 spin_unlock(&dcache_lock
);
1735 if (actual
== dentry
) {
1736 security_d_instantiate(dentry
, inode
);
1744 spin_unlock(&dcache_lock
);
1746 goto shouldnt_be_hashed
;
1750 * d_path - return the path of a dentry
1751 * @dentry: dentry to report
1752 * @vfsmnt: vfsmnt to which the dentry belongs
1753 * @root: root dentry
1754 * @rootmnt: vfsmnt to which the root dentry belongs
1755 * @buffer: buffer to return value in
1756 * @buflen: buffer length
1758 * Convert a dentry into an ASCII path name. If the entry has been deleted
1759 * the string " (deleted)" is appended. Note that this is ambiguous.
1761 * Returns the buffer or an error code if the path was too long.
1763 * "buflen" should be positive. Caller holds the dcache_lock.
1765 static char *__d_path(struct dentry
*dentry
, struct vfsmount
*vfsmnt
,
1766 struct path
*root
, char *buffer
, int buflen
)
1768 char * end
= buffer
+buflen
;
1774 if (!IS_ROOT(dentry
) && d_unhashed(dentry
)) {
1779 memcpy(end
, " (deleted)", 10);
1789 struct dentry
* parent
;
1791 if (dentry
== root
->dentry
&& vfsmnt
== root
->mnt
)
1793 if (dentry
== vfsmnt
->mnt_root
|| IS_ROOT(dentry
)) {
1795 spin_lock(&vfsmount_lock
);
1796 if (vfsmnt
->mnt_parent
== vfsmnt
) {
1797 spin_unlock(&vfsmount_lock
);
1800 dentry
= vfsmnt
->mnt_mountpoint
;
1801 vfsmnt
= vfsmnt
->mnt_parent
;
1802 spin_unlock(&vfsmount_lock
);
1805 parent
= dentry
->d_parent
;
1807 namelen
= dentry
->d_name
.len
;
1808 buflen
-= namelen
+ 1;
1812 memcpy(end
, dentry
->d_name
.name
, namelen
);
1821 namelen
= dentry
->d_name
.len
;
1825 retval
-= namelen
-1; /* hit the slash */
1826 memcpy(retval
, dentry
->d_name
.name
, namelen
);
1829 return ERR_PTR(-ENAMETOOLONG
);
1833 * d_path - return the path of a dentry
1834 * @path: path to report
1835 * @buf: buffer to return value in
1836 * @buflen: buffer length
1838 * Convert a dentry into an ASCII path name. If the entry has been deleted
1839 * the string " (deleted)" is appended. Note that this is ambiguous.
1841 * Returns the buffer or an error code if the path was too long.
1843 * "buflen" should be positive. Caller holds the dcache_lock.
1845 char *d_path(struct path
*path
, char *buf
, int buflen
)
1851 * We have various synthetic filesystems that never get mounted. On
1852 * these filesystems dentries are never used for lookup purposes, and
1853 * thus don't need to be hashed. They also don't need a name until a
1854 * user wants to identify the object in /proc/pid/fd/. The little hack
1855 * below allows us to generate a name for these objects on demand:
1857 if (path
->dentry
->d_op
&& path
->dentry
->d_op
->d_dname
)
1858 return path
->dentry
->d_op
->d_dname(path
->dentry
, buf
, buflen
);
1860 read_lock(¤t
->fs
->lock
);
1861 root
= current
->fs
->root
;
1862 path_get(¤t
->fs
->root
);
1863 read_unlock(¤t
->fs
->lock
);
1864 spin_lock(&dcache_lock
);
1865 res
= __d_path(path
->dentry
, path
->mnt
, &root
, buf
, buflen
);
1866 spin_unlock(&dcache_lock
);
1872 * Helper function for dentry_operations.d_dname() members
1874 char *dynamic_dname(struct dentry
*dentry
, char *buffer
, int buflen
,
1875 const char *fmt
, ...)
1881 va_start(args
, fmt
);
1882 sz
= vsnprintf(temp
, sizeof(temp
), fmt
, args
) + 1;
1885 if (sz
> sizeof(temp
) || sz
> buflen
)
1886 return ERR_PTR(-ENAMETOOLONG
);
1888 buffer
+= buflen
- sz
;
1889 return memcpy(buffer
, temp
, sz
);
1893 * NOTE! The user-level library version returns a
1894 * character pointer. The kernel system call just
1895 * returns the length of the buffer filled (which
1896 * includes the ending '\0' character), or a negative
1897 * error value. So libc would do something like
1899 * char *getcwd(char * buf, size_t size)
1903 * retval = sys_getcwd(buf, size);
1910 asmlinkage
long sys_getcwd(char __user
*buf
, unsigned long size
)
1913 struct path pwd
, root
;
1914 char *page
= (char *) __get_free_page(GFP_USER
);
1919 read_lock(¤t
->fs
->lock
);
1920 pwd
= current
->fs
->pwd
;
1921 path_get(¤t
->fs
->pwd
);
1922 root
= current
->fs
->root
;
1923 path_get(¤t
->fs
->root
);
1924 read_unlock(¤t
->fs
->lock
);
1927 /* Has the current directory has been unlinked? */
1928 spin_lock(&dcache_lock
);
1929 if (pwd
.dentry
->d_parent
== pwd
.dentry
|| !d_unhashed(pwd
.dentry
)) {
1933 cwd
= __d_path(pwd
.dentry
, pwd
.mnt
, &root
, page
, PAGE_SIZE
);
1934 spin_unlock(&dcache_lock
);
1936 error
= PTR_ERR(cwd
);
1941 len
= PAGE_SIZE
+ page
- cwd
;
1944 if (copy_to_user(buf
, cwd
, len
))
1948 spin_unlock(&dcache_lock
);
1953 free_page((unsigned long) page
);
1958 * Test whether new_dentry is a subdirectory of old_dentry.
1960 * Trivially implemented using the dcache structure
1964 * is_subdir - is new dentry a subdirectory of old_dentry
1965 * @new_dentry: new dentry
1966 * @old_dentry: old dentry
1968 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1969 * Returns 0 otherwise.
1970 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1973 int is_subdir(struct dentry
* new_dentry
, struct dentry
* old_dentry
)
1976 struct dentry
* saved
= new_dentry
;
1979 /* need rcu_readlock to protect against the d_parent trashing due to
1984 /* for restarting inner loop in case of seq retry */
1987 seq
= read_seqbegin(&rename_lock
);
1989 if (new_dentry
!= old_dentry
) {
1990 struct dentry
* parent
= new_dentry
->d_parent
;
1991 if (parent
== new_dentry
)
1993 new_dentry
= parent
;
1999 } while (read_seqretry(&rename_lock
, seq
));
2005 void d_genocide(struct dentry
*root
)
2007 struct dentry
*this_parent
= root
;
2008 struct list_head
*next
;
2010 spin_lock(&dcache_lock
);
2012 next
= this_parent
->d_subdirs
.next
;
2014 while (next
!= &this_parent
->d_subdirs
) {
2015 struct list_head
*tmp
= next
;
2016 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
2018 if (d_unhashed(dentry
)||!dentry
->d_inode
)
2020 if (!list_empty(&dentry
->d_subdirs
)) {
2021 this_parent
= dentry
;
2024 atomic_dec(&dentry
->d_count
);
2026 if (this_parent
!= root
) {
2027 next
= this_parent
->d_u
.d_child
.next
;
2028 atomic_dec(&this_parent
->d_count
);
2029 this_parent
= this_parent
->d_parent
;
2032 spin_unlock(&dcache_lock
);
2036 * find_inode_number - check for dentry with name
2037 * @dir: directory to check
2038 * @name: Name to find.
2040 * Check whether a dentry already exists for the given name,
2041 * and return the inode number if it has an inode. Otherwise
2044 * This routine is used to post-process directory listings for
2045 * filesystems using synthetic inode numbers, and is necessary
2046 * to keep getcwd() working.
2049 ino_t
find_inode_number(struct dentry
*dir
, struct qstr
*name
)
2051 struct dentry
* dentry
;
2054 dentry
= d_hash_and_lookup(dir
, name
);
2056 if (dentry
->d_inode
)
2057 ino
= dentry
->d_inode
->i_ino
;
2063 static __initdata
unsigned long dhash_entries
;
2064 static int __init
set_dhash_entries(char *str
)
2068 dhash_entries
= simple_strtoul(str
, &str
, 0);
2071 __setup("dhash_entries=", set_dhash_entries
);
2073 static void __init
dcache_init_early(void)
2077 /* If hashes are distributed across NUMA nodes, defer
2078 * hash allocation until vmalloc space is available.
2084 alloc_large_system_hash("Dentry cache",
2085 sizeof(struct hlist_head
),
2093 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2094 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2097 static void __init
dcache_init(void)
2102 * A constructor could be added for stable state like the lists,
2103 * but it is probably not worth it because of the cache nature
2106 dentry_cache
= KMEM_CACHE(dentry
,
2107 SLAB_RECLAIM_ACCOUNT
|SLAB_PANIC
|SLAB_MEM_SPREAD
);
2109 register_shrinker(&dcache_shrinker
);
2111 /* Hash may have been set up in dcache_init_early */
2116 alloc_large_system_hash("Dentry cache",
2117 sizeof(struct hlist_head
),
2125 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2126 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2129 /* SLAB cache for __getname() consumers */
2130 struct kmem_cache
*names_cachep __read_mostly
;
2132 /* SLAB cache for file structures */
2133 struct kmem_cache
*filp_cachep __read_mostly
;
2135 EXPORT_SYMBOL(d_genocide
);
2137 void __init
vfs_caches_init_early(void)
2139 dcache_init_early();
2143 void __init
vfs_caches_init(unsigned long mempages
)
2145 unsigned long reserve
;
2147 /* Base hash sizes on available memory, with a reserve equal to
2148 150% of current kernel size */
2150 reserve
= min((mempages
- nr_free_pages()) * 3/2, mempages
- 1);
2151 mempages
-= reserve
;
2153 names_cachep
= kmem_cache_create("names_cache", PATH_MAX
, 0,
2154 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
2156 filp_cachep
= kmem_cache_create("filp", sizeof(struct file
), 0,
2157 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
2161 files_init(mempages
);
2167 EXPORT_SYMBOL(d_alloc
);
2168 EXPORT_SYMBOL(d_alloc_anon
);
2169 EXPORT_SYMBOL(d_alloc_root
);
2170 EXPORT_SYMBOL(d_delete
);
2171 EXPORT_SYMBOL(d_find_alias
);
2172 EXPORT_SYMBOL(d_instantiate
);
2173 EXPORT_SYMBOL(d_invalidate
);
2174 EXPORT_SYMBOL(d_lookup
);
2175 EXPORT_SYMBOL(d_move
);
2176 EXPORT_SYMBOL_GPL(d_materialise_unique
);
2177 EXPORT_SYMBOL(d_path
);
2178 EXPORT_SYMBOL(d_prune_aliases
);
2179 EXPORT_SYMBOL(d_rehash
);
2180 EXPORT_SYMBOL(d_splice_alias
);
2181 EXPORT_SYMBOL(d_validate
);
2182 EXPORT_SYMBOL(dget_locked
);
2183 EXPORT_SYMBOL(dput
);
2184 EXPORT_SYMBOL(find_inode_number
);
2185 EXPORT_SYMBOL(have_submounts
);
2186 EXPORT_SYMBOL(names_cachep
);
2187 EXPORT_SYMBOL(shrink_dcache_parent
);
2188 EXPORT_SYMBOL(shrink_dcache_sb
);