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 static __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 (dentry
->d_hash
.pprev
== NULL
)
95 call_rcu(&dentry
->d_u
.d_rcu
, d_callback
);
99 * Release the dentry's inode, using the filesystem
100 * d_iput() operation if defined.
101 * Called with dcache_lock and per dentry lock held, drops both.
103 static void dentry_iput(struct dentry
* dentry
)
105 struct inode
*inode
= dentry
->d_inode
;
107 dentry
->d_inode
= NULL
;
108 list_del_init(&dentry
->d_alias
);
109 spin_unlock(&dentry
->d_lock
);
110 spin_unlock(&dcache_lock
);
112 fsnotify_inoderemove(inode
);
113 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
114 dentry
->d_op
->d_iput(dentry
, inode
);
118 spin_unlock(&dentry
->d_lock
);
119 spin_unlock(&dcache_lock
);
124 * d_kill - kill dentry and return parent
125 * @dentry: dentry to kill
127 * Called with dcache_lock and d_lock, releases both. The dentry must
128 * already be unhashed and removed from the LRU.
130 * If this is the root of the dentry tree, return NULL.
132 static struct dentry
*d_kill(struct dentry
*dentry
)
134 struct dentry
*parent
;
136 list_del(&dentry
->d_u
.d_child
);
137 dentry_stat
.nr_dentry
--; /* For d_free, below */
138 /*drops the locks, at that point nobody can reach this dentry */
140 parent
= dentry
->d_parent
;
142 return dentry
== parent
? NULL
: parent
;
148 * This is complicated by the fact that we do not want to put
149 * dentries that are no longer on any hash chain on the unused
150 * list: we'd much rather just get rid of them immediately.
152 * However, that implies that we have to traverse the dentry
153 * tree upwards to the parents which might _also_ now be
154 * scheduled for deletion (it may have been only waiting for
155 * its last child to go away).
157 * This tail recursion is done by hand as we don't want to depend
158 * on the compiler to always get this right (gcc generally doesn't).
159 * Real recursion would eat up our stack space.
163 * dput - release a dentry
164 * @dentry: dentry to release
166 * Release a dentry. This will drop the usage count and if appropriate
167 * call the dentry unlink method as well as removing it from the queues and
168 * releasing its resources. If the parent dentries were scheduled for release
169 * they too may now get deleted.
171 * no dcache lock, please.
174 void dput(struct dentry
*dentry
)
180 if (atomic_read(&dentry
->d_count
) == 1)
182 if (!atomic_dec_and_lock(&dentry
->d_count
, &dcache_lock
))
185 spin_lock(&dentry
->d_lock
);
186 if (atomic_read(&dentry
->d_count
)) {
187 spin_unlock(&dentry
->d_lock
);
188 spin_unlock(&dcache_lock
);
193 * AV: ->d_delete() is _NOT_ allowed to block now.
195 if (dentry
->d_op
&& dentry
->d_op
->d_delete
) {
196 if (dentry
->d_op
->d_delete(dentry
))
199 /* Unreachable? Get rid of it */
200 if (d_unhashed(dentry
))
202 if (list_empty(&dentry
->d_lru
)) {
203 dentry
->d_flags
|= DCACHE_REFERENCED
;
204 list_add(&dentry
->d_lru
, &dentry_unused
);
205 dentry_stat
.nr_unused
++;
207 spin_unlock(&dentry
->d_lock
);
208 spin_unlock(&dcache_lock
);
214 /* If dentry was on d_lru list
215 * delete it from there
217 if (!list_empty(&dentry
->d_lru
)) {
218 list_del(&dentry
->d_lru
);
219 dentry_stat
.nr_unused
--;
221 dentry
= d_kill(dentry
);
227 * d_invalidate - invalidate a dentry
228 * @dentry: dentry to invalidate
230 * Try to invalidate the dentry if it turns out to be
231 * possible. If there are other dentries that can be
232 * reached through this one we can't delete it and we
233 * return -EBUSY. On success we return 0.
238 int d_invalidate(struct dentry
* dentry
)
241 * If it's already been dropped, return OK.
243 spin_lock(&dcache_lock
);
244 if (d_unhashed(dentry
)) {
245 spin_unlock(&dcache_lock
);
249 * Check whether to do a partial shrink_dcache
250 * to get rid of unused child entries.
252 if (!list_empty(&dentry
->d_subdirs
)) {
253 spin_unlock(&dcache_lock
);
254 shrink_dcache_parent(dentry
);
255 spin_lock(&dcache_lock
);
259 * Somebody else still using it?
261 * If it's a directory, we can't drop it
262 * for fear of somebody re-populating it
263 * with children (even though dropping it
264 * would make it unreachable from the root,
265 * we might still populate it if it was a
266 * working directory or similar).
268 spin_lock(&dentry
->d_lock
);
269 if (atomic_read(&dentry
->d_count
) > 1) {
270 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
)) {
271 spin_unlock(&dentry
->d_lock
);
272 spin_unlock(&dcache_lock
);
278 spin_unlock(&dentry
->d_lock
);
279 spin_unlock(&dcache_lock
);
283 /* This should be called _only_ with dcache_lock held */
285 static inline struct dentry
* __dget_locked(struct dentry
*dentry
)
287 atomic_inc(&dentry
->d_count
);
288 if (!list_empty(&dentry
->d_lru
)) {
289 dentry_stat
.nr_unused
--;
290 list_del_init(&dentry
->d_lru
);
295 struct dentry
* dget_locked(struct dentry
*dentry
)
297 return __dget_locked(dentry
);
301 * d_find_alias - grab a hashed alias of inode
302 * @inode: inode in question
303 * @want_discon: flag, used by d_splice_alias, to request
304 * that only a DISCONNECTED alias be returned.
306 * If inode has a hashed alias, or is a directory and has any alias,
307 * acquire the reference to alias and return it. Otherwise return NULL.
308 * Notice that if inode is a directory there can be only one alias and
309 * it can be unhashed only if it has no children, or if it is the root
312 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
313 * any other hashed alias over that one unless @want_discon is set,
314 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
317 static struct dentry
* __d_find_alias(struct inode
*inode
, int want_discon
)
319 struct list_head
*head
, *next
, *tmp
;
320 struct dentry
*alias
, *discon_alias
=NULL
;
322 head
= &inode
->i_dentry
;
323 next
= inode
->i_dentry
.next
;
324 while (next
!= head
) {
328 alias
= list_entry(tmp
, struct dentry
, d_alias
);
329 if (S_ISDIR(inode
->i_mode
) || !d_unhashed(alias
)) {
330 if (IS_ROOT(alias
) &&
331 (alias
->d_flags
& DCACHE_DISCONNECTED
))
332 discon_alias
= alias
;
333 else if (!want_discon
) {
334 __dget_locked(alias
);
340 __dget_locked(discon_alias
);
344 struct dentry
* d_find_alias(struct inode
*inode
)
346 struct dentry
*de
= NULL
;
348 if (!list_empty(&inode
->i_dentry
)) {
349 spin_lock(&dcache_lock
);
350 de
= __d_find_alias(inode
, 0);
351 spin_unlock(&dcache_lock
);
357 * Try to kill dentries associated with this inode.
358 * WARNING: you must own a reference to inode.
360 void d_prune_aliases(struct inode
*inode
)
362 struct dentry
*dentry
;
364 spin_lock(&dcache_lock
);
365 list_for_each_entry(dentry
, &inode
->i_dentry
, d_alias
) {
366 spin_lock(&dentry
->d_lock
);
367 if (!atomic_read(&dentry
->d_count
)) {
368 __dget_locked(dentry
);
370 spin_unlock(&dentry
->d_lock
);
371 spin_unlock(&dcache_lock
);
375 spin_unlock(&dentry
->d_lock
);
377 spin_unlock(&dcache_lock
);
381 * Throw away a dentry - free the inode, dput the parent. This requires that
382 * the LRU list has already been removed.
384 * If prune_parents is true, try to prune ancestors as well.
386 * Called with dcache_lock, drops it and then regains.
387 * Called with dentry->d_lock held, drops it.
389 static void prune_one_dentry(struct dentry
* dentry
, int prune_parents
)
392 dentry
= d_kill(dentry
);
393 if (!prune_parents
) {
395 spin_lock(&dcache_lock
);
400 * Prune ancestors. Locking is simpler than in dput(),
401 * because dcache_lock needs to be taken anyway.
403 spin_lock(&dcache_lock
);
405 if (!atomic_dec_and_lock(&dentry
->d_count
, &dentry
->d_lock
))
408 if (dentry
->d_op
&& dentry
->d_op
->d_delete
)
409 dentry
->d_op
->d_delete(dentry
);
410 if (!list_empty(&dentry
->d_lru
)) {
411 list_del(&dentry
->d_lru
);
412 dentry_stat
.nr_unused
--;
415 dentry
= d_kill(dentry
);
416 spin_lock(&dcache_lock
);
421 * prune_dcache - shrink the dcache
422 * @count: number of entries to try and free
423 * @sb: if given, ignore dentries for other superblocks
424 * which are being unmounted.
425 * @prune_parents: if true, try to prune ancestors as well in one go
427 * Shrink the dcache. This is done when we need
428 * more memory, or simply when we need to unmount
429 * something (at which point we need to unuse
432 * This function may fail to free any resources if
433 * all the dentries are in use.
436 static void prune_dcache(int count
, struct super_block
*sb
, int prune_parents
)
438 spin_lock(&dcache_lock
);
439 for (; count
; count
--) {
440 struct dentry
*dentry
;
441 struct list_head
*tmp
;
442 struct rw_semaphore
*s_umount
;
444 cond_resched_lock(&dcache_lock
);
446 tmp
= dentry_unused
.prev
;
448 /* Try to find a dentry for this sb, but don't try
449 * too hard, if they aren't near the tail they will
450 * be moved down again soon
453 while (skip
&& tmp
!= &dentry_unused
&&
454 list_entry(tmp
, struct dentry
, d_lru
)->d_sb
!= sb
) {
459 if (tmp
== &dentry_unused
)
462 prefetch(dentry_unused
.prev
);
463 dentry_stat
.nr_unused
--;
464 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
466 spin_lock(&dentry
->d_lock
);
468 * We found an inuse dentry which was not removed from
469 * dentry_unused because of laziness during lookup. Do not free
470 * it - just keep it off the dentry_unused list.
472 if (atomic_read(&dentry
->d_count
)) {
473 spin_unlock(&dentry
->d_lock
);
476 /* If the dentry was recently referenced, don't free it. */
477 if (dentry
->d_flags
& DCACHE_REFERENCED
) {
478 dentry
->d_flags
&= ~DCACHE_REFERENCED
;
479 list_add(&dentry
->d_lru
, &dentry_unused
);
480 dentry_stat
.nr_unused
++;
481 spin_unlock(&dentry
->d_lock
);
485 * If the dentry is not DCACHED_REFERENCED, it is time
486 * to remove it from the dcache, provided the super block is
487 * NULL (which means we are trying to reclaim memory)
488 * or this dentry belongs to the same super block that
492 * If this dentry is for "my" filesystem, then I can prune it
493 * without taking the s_umount lock (I already hold it).
495 if (sb
&& dentry
->d_sb
== sb
) {
496 prune_one_dentry(dentry
, prune_parents
);
500 * ...otherwise we need to be sure this filesystem isn't being
501 * unmounted, otherwise we could race with
502 * generic_shutdown_super(), and end up holding a reference to
503 * an inode while the filesystem is unmounted.
504 * So we try to get s_umount, and make sure s_root isn't NULL.
505 * (Take a local copy of s_umount to avoid a use-after-free of
508 s_umount
= &dentry
->d_sb
->s_umount
;
509 if (down_read_trylock(s_umount
)) {
510 if (dentry
->d_sb
->s_root
!= NULL
) {
511 prune_one_dentry(dentry
, prune_parents
);
517 spin_unlock(&dentry
->d_lock
);
519 * Insert dentry at the head of the list as inserting at the
520 * tail leads to a cycle.
522 list_add(&dentry
->d_lru
, &dentry_unused
);
523 dentry_stat
.nr_unused
++;
525 spin_unlock(&dcache_lock
);
529 * Shrink the dcache for the specified super block.
530 * This allows us to unmount a device without disturbing
531 * the dcache for the other devices.
533 * This implementation makes just two traversals of the
534 * unused list. On the first pass we move the selected
535 * dentries to the most recent end, and on the second
536 * pass we free them. The second pass must restart after
537 * each dput(), but since the target dentries are all at
538 * the end, it's really just a single traversal.
542 * shrink_dcache_sb - shrink dcache for a superblock
545 * Shrink the dcache for the specified super block. This
546 * is used to free the dcache before unmounting a file
550 void shrink_dcache_sb(struct super_block
* sb
)
552 struct list_head
*tmp
, *next
;
553 struct dentry
*dentry
;
556 * Pass one ... move the dentries for the specified
557 * superblock to the most recent end of the unused list.
559 spin_lock(&dcache_lock
);
560 list_for_each_safe(tmp
, next
, &dentry_unused
) {
561 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
562 if (dentry
->d_sb
!= sb
)
564 list_move(tmp
, &dentry_unused
);
568 * Pass two ... free the dentries for this superblock.
571 list_for_each_safe(tmp
, next
, &dentry_unused
) {
572 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
573 if (dentry
->d_sb
!= sb
)
575 dentry_stat
.nr_unused
--;
577 spin_lock(&dentry
->d_lock
);
578 if (atomic_read(&dentry
->d_count
)) {
579 spin_unlock(&dentry
->d_lock
);
582 prune_one_dentry(dentry
, 1);
583 cond_resched_lock(&dcache_lock
);
586 spin_unlock(&dcache_lock
);
590 * destroy a single subtree of dentries for unmount
591 * - see the comments on shrink_dcache_for_umount() for a description of the
594 static void shrink_dcache_for_umount_subtree(struct dentry
*dentry
)
596 struct dentry
*parent
;
597 unsigned detached
= 0;
599 BUG_ON(!IS_ROOT(dentry
));
601 /* detach this root from the system */
602 spin_lock(&dcache_lock
);
603 if (!list_empty(&dentry
->d_lru
)) {
604 dentry_stat
.nr_unused
--;
605 list_del_init(&dentry
->d_lru
);
608 spin_unlock(&dcache_lock
);
611 /* descend to the first leaf in the current subtree */
612 while (!list_empty(&dentry
->d_subdirs
)) {
615 /* this is a branch with children - detach all of them
616 * from the system in one go */
617 spin_lock(&dcache_lock
);
618 list_for_each_entry(loop
, &dentry
->d_subdirs
,
620 if (!list_empty(&loop
->d_lru
)) {
621 dentry_stat
.nr_unused
--;
622 list_del_init(&loop
->d_lru
);
626 cond_resched_lock(&dcache_lock
);
628 spin_unlock(&dcache_lock
);
630 /* move to the first child */
631 dentry
= list_entry(dentry
->d_subdirs
.next
,
632 struct dentry
, d_u
.d_child
);
635 /* consume the dentries from this leaf up through its parents
636 * until we find one with children or run out altogether */
640 if (atomic_read(&dentry
->d_count
) != 0) {
642 "BUG: Dentry %p{i=%lx,n=%s}"
644 " [unmount of %s %s]\n",
647 dentry
->d_inode
->i_ino
: 0UL,
649 atomic_read(&dentry
->d_count
),
650 dentry
->d_sb
->s_type
->name
,
655 parent
= dentry
->d_parent
;
656 if (parent
== dentry
)
659 atomic_dec(&parent
->d_count
);
661 list_del(&dentry
->d_u
.d_child
);
664 inode
= dentry
->d_inode
;
666 dentry
->d_inode
= NULL
;
667 list_del_init(&dentry
->d_alias
);
668 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
669 dentry
->d_op
->d_iput(dentry
, inode
);
676 /* finished when we fall off the top of the tree,
677 * otherwise we ascend to the parent and move to the
678 * next sibling if there is one */
684 } while (list_empty(&dentry
->d_subdirs
));
686 dentry
= list_entry(dentry
->d_subdirs
.next
,
687 struct dentry
, d_u
.d_child
);
690 /* several dentries were freed, need to correct nr_dentry */
691 spin_lock(&dcache_lock
);
692 dentry_stat
.nr_dentry
-= detached
;
693 spin_unlock(&dcache_lock
);
697 * destroy the dentries attached to a superblock on unmounting
698 * - we don't need to use dentry->d_lock, and only need dcache_lock when
699 * removing the dentry from the system lists and hashes because:
700 * - the superblock is detached from all mountings and open files, so the
701 * dentry trees will not be rearranged by the VFS
702 * - s_umount is write-locked, so the memory pressure shrinker will ignore
703 * any dentries belonging to this superblock that it comes across
704 * - the filesystem itself is no longer permitted to rearrange the dentries
707 void shrink_dcache_for_umount(struct super_block
*sb
)
709 struct dentry
*dentry
;
711 if (down_read_trylock(&sb
->s_umount
))
716 atomic_dec(&dentry
->d_count
);
717 shrink_dcache_for_umount_subtree(dentry
);
719 while (!hlist_empty(&sb
->s_anon
)) {
720 dentry
= hlist_entry(sb
->s_anon
.first
, struct dentry
, d_hash
);
721 shrink_dcache_for_umount_subtree(dentry
);
726 * Search for at least 1 mount point in the dentry's subdirs.
727 * We descend to the next level whenever the d_subdirs
728 * list is non-empty and continue searching.
732 * have_submounts - check for mounts over a dentry
733 * @parent: dentry to check.
735 * Return true if the parent or its subdirectories contain
739 int have_submounts(struct dentry
*parent
)
741 struct dentry
*this_parent
= parent
;
742 struct list_head
*next
;
744 spin_lock(&dcache_lock
);
745 if (d_mountpoint(parent
))
748 next
= this_parent
->d_subdirs
.next
;
750 while (next
!= &this_parent
->d_subdirs
) {
751 struct list_head
*tmp
= next
;
752 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
754 /* Have we found a mount point ? */
755 if (d_mountpoint(dentry
))
757 if (!list_empty(&dentry
->d_subdirs
)) {
758 this_parent
= dentry
;
763 * All done at this level ... ascend and resume the search.
765 if (this_parent
!= parent
) {
766 next
= this_parent
->d_u
.d_child
.next
;
767 this_parent
= this_parent
->d_parent
;
770 spin_unlock(&dcache_lock
);
771 return 0; /* No mount points found in tree */
773 spin_unlock(&dcache_lock
);
778 * Search the dentry child list for the specified parent,
779 * and move any unused dentries to the end of the unused
780 * list for prune_dcache(). We descend to the next level
781 * whenever the d_subdirs list is non-empty and continue
784 * It returns zero iff there are no unused children,
785 * otherwise it returns the number of children moved to
786 * the end of the unused list. This may not be the total
787 * number of unused children, because select_parent can
788 * drop the lock and return early due to latency
791 static int select_parent(struct dentry
* parent
)
793 struct dentry
*this_parent
= parent
;
794 struct list_head
*next
;
797 spin_lock(&dcache_lock
);
799 next
= this_parent
->d_subdirs
.next
;
801 while (next
!= &this_parent
->d_subdirs
) {
802 struct list_head
*tmp
= next
;
803 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
806 if (!list_empty(&dentry
->d_lru
)) {
807 dentry_stat
.nr_unused
--;
808 list_del_init(&dentry
->d_lru
);
811 * move only zero ref count dentries to the end
812 * of the unused list for prune_dcache
814 if (!atomic_read(&dentry
->d_count
)) {
815 list_add_tail(&dentry
->d_lru
, &dentry_unused
);
816 dentry_stat
.nr_unused
++;
821 * We can return to the caller if we have found some (this
822 * ensures forward progress). We'll be coming back to find
825 if (found
&& need_resched())
829 * Descend a level if the d_subdirs list is non-empty.
831 if (!list_empty(&dentry
->d_subdirs
)) {
832 this_parent
= dentry
;
837 * All done at this level ... ascend and resume the search.
839 if (this_parent
!= parent
) {
840 next
= this_parent
->d_u
.d_child
.next
;
841 this_parent
= this_parent
->d_parent
;
845 spin_unlock(&dcache_lock
);
850 * shrink_dcache_parent - prune dcache
851 * @parent: parent of entries to prune
853 * Prune the dcache to remove unused children of the parent dentry.
856 void shrink_dcache_parent(struct dentry
* parent
)
860 while ((found
= select_parent(parent
)) != 0)
861 prune_dcache(found
, parent
->d_sb
, 1);
865 * Scan `nr' dentries and return the number which remain.
867 * We need to avoid reentering the filesystem if the caller is performing a
868 * GFP_NOFS allocation attempt. One example deadlock is:
870 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
871 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
872 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
874 * In this case we return -1 to tell the caller that we baled.
876 static int shrink_dcache_memory(int nr
, gfp_t gfp_mask
)
879 if (!(gfp_mask
& __GFP_FS
))
881 prune_dcache(nr
, NULL
, 1);
883 return (dentry_stat
.nr_unused
/ 100) * sysctl_vfs_cache_pressure
;
886 static struct shrinker dcache_shrinker
= {
887 .shrink
= shrink_dcache_memory
,
888 .seeks
= DEFAULT_SEEKS
,
892 * d_alloc - allocate a dcache entry
893 * @parent: parent of entry to allocate
894 * @name: qstr of the name
896 * Allocates a dentry. It returns %NULL if there is insufficient memory
897 * available. On a success the dentry is returned. The name passed in is
898 * copied and the copy passed in may be reused after this call.
901 struct dentry
*d_alloc(struct dentry
* parent
, const struct qstr
*name
)
903 struct dentry
*dentry
;
906 dentry
= kmem_cache_alloc(dentry_cache
, GFP_KERNEL
);
910 if (name
->len
> DNAME_INLINE_LEN
-1) {
911 dname
= kmalloc(name
->len
+ 1, GFP_KERNEL
);
913 kmem_cache_free(dentry_cache
, dentry
);
917 dname
= dentry
->d_iname
;
919 dentry
->d_name
.name
= dname
;
921 dentry
->d_name
.len
= name
->len
;
922 dentry
->d_name
.hash
= name
->hash
;
923 memcpy(dname
, name
->name
, name
->len
);
924 dname
[name
->len
] = 0;
926 atomic_set(&dentry
->d_count
, 1);
927 dentry
->d_flags
= DCACHE_UNHASHED
;
928 spin_lock_init(&dentry
->d_lock
);
929 dentry
->d_inode
= NULL
;
930 dentry
->d_parent
= NULL
;
933 dentry
->d_fsdata
= NULL
;
934 dentry
->d_mounted
= 0;
935 #ifdef CONFIG_PROFILING
936 dentry
->d_cookie
= NULL
;
938 INIT_HLIST_NODE(&dentry
->d_hash
);
939 INIT_LIST_HEAD(&dentry
->d_lru
);
940 INIT_LIST_HEAD(&dentry
->d_subdirs
);
941 INIT_LIST_HEAD(&dentry
->d_alias
);
944 dentry
->d_parent
= dget(parent
);
945 dentry
->d_sb
= parent
->d_sb
;
947 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
950 spin_lock(&dcache_lock
);
952 list_add(&dentry
->d_u
.d_child
, &parent
->d_subdirs
);
953 dentry_stat
.nr_dentry
++;
954 spin_unlock(&dcache_lock
);
959 struct dentry
*d_alloc_name(struct dentry
*parent
, const char *name
)
964 q
.len
= strlen(name
);
965 q
.hash
= full_name_hash(q
.name
, q
.len
);
966 return d_alloc(parent
, &q
);
970 * d_instantiate - fill in inode information for a dentry
971 * @entry: dentry to complete
972 * @inode: inode to attach to this dentry
974 * Fill in inode information in the entry.
976 * This turns negative dentries into productive full members
979 * NOTE! This assumes that the inode count has been incremented
980 * (or otherwise set) by the caller to indicate that it is now
981 * in use by the dcache.
984 void d_instantiate(struct dentry
*entry
, struct inode
* inode
)
986 BUG_ON(!list_empty(&entry
->d_alias
));
987 spin_lock(&dcache_lock
);
989 list_add(&entry
->d_alias
, &inode
->i_dentry
);
990 entry
->d_inode
= inode
;
991 fsnotify_d_instantiate(entry
, inode
);
992 spin_unlock(&dcache_lock
);
993 security_d_instantiate(entry
, inode
);
997 * d_instantiate_unique - instantiate a non-aliased dentry
998 * @entry: dentry to instantiate
999 * @inode: inode to attach to this dentry
1001 * Fill in inode information in the entry. On success, it returns NULL.
1002 * If an unhashed alias of "entry" already exists, then we return the
1003 * aliased dentry instead and drop one reference to inode.
1005 * Note that in order to avoid conflicts with rename() etc, the caller
1006 * had better be holding the parent directory semaphore.
1008 * This also assumes that the inode count has been incremented
1009 * (or otherwise set) by the caller to indicate that it is now
1010 * in use by the dcache.
1012 static struct dentry
*__d_instantiate_unique(struct dentry
*entry
,
1013 struct inode
*inode
)
1015 struct dentry
*alias
;
1016 int len
= entry
->d_name
.len
;
1017 const char *name
= entry
->d_name
.name
;
1018 unsigned int hash
= entry
->d_name
.hash
;
1021 entry
->d_inode
= NULL
;
1025 list_for_each_entry(alias
, &inode
->i_dentry
, d_alias
) {
1026 struct qstr
*qstr
= &alias
->d_name
;
1028 if (qstr
->hash
!= hash
)
1030 if (alias
->d_parent
!= entry
->d_parent
)
1032 if (qstr
->len
!= len
)
1034 if (memcmp(qstr
->name
, name
, len
))
1040 list_add(&entry
->d_alias
, &inode
->i_dentry
);
1041 entry
->d_inode
= inode
;
1042 fsnotify_d_instantiate(entry
, inode
);
1046 struct dentry
*d_instantiate_unique(struct dentry
*entry
, struct inode
*inode
)
1048 struct dentry
*result
;
1050 BUG_ON(!list_empty(&entry
->d_alias
));
1052 spin_lock(&dcache_lock
);
1053 result
= __d_instantiate_unique(entry
, inode
);
1054 spin_unlock(&dcache_lock
);
1057 security_d_instantiate(entry
, inode
);
1061 BUG_ON(!d_unhashed(result
));
1066 EXPORT_SYMBOL(d_instantiate_unique
);
1069 * d_alloc_root - allocate root dentry
1070 * @root_inode: inode to allocate the root for
1072 * Allocate a root ("/") dentry for the inode given. The inode is
1073 * instantiated and returned. %NULL is returned if there is insufficient
1074 * memory or the inode passed is %NULL.
1077 struct dentry
* d_alloc_root(struct inode
* root_inode
)
1079 struct dentry
*res
= NULL
;
1082 static const struct qstr name
= { .name
= "/", .len
= 1 };
1084 res
= d_alloc(NULL
, &name
);
1086 res
->d_sb
= root_inode
->i_sb
;
1087 res
->d_parent
= res
;
1088 d_instantiate(res
, root_inode
);
1094 static inline struct hlist_head
*d_hash(struct dentry
*parent
,
1097 hash
+= ((unsigned long) parent
^ GOLDEN_RATIO_PRIME
) / L1_CACHE_BYTES
;
1098 hash
= hash
^ ((hash
^ GOLDEN_RATIO_PRIME
) >> D_HASHBITS
);
1099 return dentry_hashtable
+ (hash
& D_HASHMASK
);
1103 * d_alloc_anon - allocate an anonymous dentry
1104 * @inode: inode to allocate the dentry for
1106 * This is similar to d_alloc_root. It is used by filesystems when
1107 * creating a dentry for a given inode, often in the process of
1108 * mapping a filehandle to a dentry. The returned dentry may be
1109 * anonymous, or may have a full name (if the inode was already
1110 * in the cache). The file system may need to make further
1111 * efforts to connect this dentry into the dcache properly.
1113 * When called on a directory inode, we must ensure that
1114 * the inode only ever has one dentry. If a dentry is
1115 * found, that is returned instead of allocating a new one.
1117 * On successful return, the reference to the inode has been transferred
1118 * to the dentry. If %NULL is returned (indicating kmalloc failure),
1119 * the reference on the inode has not been released.
1122 struct dentry
* d_alloc_anon(struct inode
*inode
)
1124 static const struct qstr anonstring
= { .name
= "" };
1128 if ((res
= d_find_alias(inode
))) {
1133 tmp
= d_alloc(NULL
, &anonstring
);
1137 tmp
->d_parent
= tmp
; /* make sure dput doesn't croak */
1139 spin_lock(&dcache_lock
);
1140 res
= __d_find_alias(inode
, 0);
1142 /* attach a disconnected dentry */
1145 spin_lock(&res
->d_lock
);
1146 res
->d_sb
= inode
->i_sb
;
1147 res
->d_parent
= res
;
1148 res
->d_inode
= inode
;
1149 res
->d_flags
|= DCACHE_DISCONNECTED
;
1150 res
->d_flags
&= ~DCACHE_UNHASHED
;
1151 list_add(&res
->d_alias
, &inode
->i_dentry
);
1152 hlist_add_head(&res
->d_hash
, &inode
->i_sb
->s_anon
);
1153 spin_unlock(&res
->d_lock
);
1155 inode
= NULL
; /* don't drop reference */
1157 spin_unlock(&dcache_lock
);
1168 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1169 * @inode: the inode which may have a disconnected dentry
1170 * @dentry: a negative dentry which we want to point to the inode.
1172 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1173 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1174 * and return it, else simply d_add the inode to the dentry and return NULL.
1176 * This is needed in the lookup routine of any filesystem that is exportable
1177 * (via knfsd) so that we can build dcache paths to directories effectively.
1179 * If a dentry was found and moved, then it is returned. Otherwise NULL
1180 * is returned. This matches the expected return value of ->lookup.
1183 struct dentry
*d_splice_alias(struct inode
*inode
, struct dentry
*dentry
)
1185 struct dentry
*new = NULL
;
1187 if (inode
&& S_ISDIR(inode
->i_mode
)) {
1188 spin_lock(&dcache_lock
);
1189 new = __d_find_alias(inode
, 1);
1191 BUG_ON(!(new->d_flags
& DCACHE_DISCONNECTED
));
1192 fsnotify_d_instantiate(new, inode
);
1193 spin_unlock(&dcache_lock
);
1194 security_d_instantiate(new, inode
);
1196 d_move(new, dentry
);
1199 /* d_instantiate takes dcache_lock, so we do it by hand */
1200 list_add(&dentry
->d_alias
, &inode
->i_dentry
);
1201 dentry
->d_inode
= inode
;
1202 fsnotify_d_instantiate(dentry
, inode
);
1203 spin_unlock(&dcache_lock
);
1204 security_d_instantiate(dentry
, inode
);
1208 d_add(dentry
, inode
);
1214 * d_lookup - search for a dentry
1215 * @parent: parent dentry
1216 * @name: qstr of name we wish to find
1218 * Searches the children of the parent dentry for the name in question. If
1219 * the dentry is found its reference count is incremented and the dentry
1220 * is returned. The caller must use d_put to free the entry when it has
1221 * finished using it. %NULL is returned on failure.
1223 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1224 * Memory barriers are used while updating and doing lockless traversal.
1225 * To avoid races with d_move while rename is happening, d_lock is used.
1227 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1228 * and name pointer in one structure pointed by d_qstr.
1230 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1231 * lookup is going on.
1233 * dentry_unused list is not updated even if lookup finds the required dentry
1234 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1235 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1238 * d_lookup() is protected against the concurrent renames in some unrelated
1239 * directory using the seqlockt_t rename_lock.
1242 struct dentry
* d_lookup(struct dentry
* parent
, struct qstr
* name
)
1244 struct dentry
* dentry
= NULL
;
1248 seq
= read_seqbegin(&rename_lock
);
1249 dentry
= __d_lookup(parent
, name
);
1252 } while (read_seqretry(&rename_lock
, seq
));
1256 struct dentry
* __d_lookup(struct dentry
* parent
, struct qstr
* name
)
1258 unsigned int len
= name
->len
;
1259 unsigned int hash
= name
->hash
;
1260 const unsigned char *str
= name
->name
;
1261 struct hlist_head
*head
= d_hash(parent
,hash
);
1262 struct dentry
*found
= NULL
;
1263 struct hlist_node
*node
;
1264 struct dentry
*dentry
;
1268 hlist_for_each_entry_rcu(dentry
, node
, head
, d_hash
) {
1271 if (dentry
->d_name
.hash
!= hash
)
1273 if (dentry
->d_parent
!= parent
)
1276 spin_lock(&dentry
->d_lock
);
1279 * Recheck the dentry after taking the lock - d_move may have
1280 * changed things. Don't bother checking the hash because we're
1281 * about to compare the whole name anyway.
1283 if (dentry
->d_parent
!= parent
)
1287 * It is safe to compare names since d_move() cannot
1288 * change the qstr (protected by d_lock).
1290 qstr
= &dentry
->d_name
;
1291 if (parent
->d_op
&& parent
->d_op
->d_compare
) {
1292 if (parent
->d_op
->d_compare(parent
, qstr
, name
))
1295 if (qstr
->len
!= len
)
1297 if (memcmp(qstr
->name
, str
, len
))
1301 if (!d_unhashed(dentry
)) {
1302 atomic_inc(&dentry
->d_count
);
1305 spin_unlock(&dentry
->d_lock
);
1308 spin_unlock(&dentry
->d_lock
);
1316 * d_hash_and_lookup - hash the qstr then search for a dentry
1317 * @dir: Directory to search in
1318 * @name: qstr of name we wish to find
1320 * On hash failure or on lookup failure NULL is returned.
1322 struct dentry
*d_hash_and_lookup(struct dentry
*dir
, struct qstr
*name
)
1324 struct dentry
*dentry
= NULL
;
1327 * Check for a fs-specific hash function. Note that we must
1328 * calculate the standard hash first, as the d_op->d_hash()
1329 * routine may choose to leave the hash value unchanged.
1331 name
->hash
= full_name_hash(name
->name
, name
->len
);
1332 if (dir
->d_op
&& dir
->d_op
->d_hash
) {
1333 if (dir
->d_op
->d_hash(dir
, name
) < 0)
1336 dentry
= d_lookup(dir
, name
);
1342 * d_validate - verify dentry provided from insecure source
1343 * @dentry: The dentry alleged to be valid child of @dparent
1344 * @dparent: The parent dentry (known to be valid)
1345 * @hash: Hash of the dentry
1346 * @len: Length of the name
1348 * An insecure source has sent us a dentry, here we verify it and dget() it.
1349 * This is used by ncpfs in its readdir implementation.
1350 * Zero is returned in the dentry is invalid.
1353 int d_validate(struct dentry
*dentry
, struct dentry
*dparent
)
1355 struct hlist_head
*base
;
1356 struct hlist_node
*lhp
;
1358 /* Check whether the ptr might be valid at all.. */
1359 if (!kmem_ptr_validate(dentry_cache
, dentry
))
1362 if (dentry
->d_parent
!= dparent
)
1365 spin_lock(&dcache_lock
);
1366 base
= d_hash(dparent
, dentry
->d_name
.hash
);
1367 hlist_for_each(lhp
,base
) {
1368 /* hlist_for_each_entry_rcu() not required for d_hash list
1369 * as it is parsed under dcache_lock
1371 if (dentry
== hlist_entry(lhp
, struct dentry
, d_hash
)) {
1372 __dget_locked(dentry
);
1373 spin_unlock(&dcache_lock
);
1377 spin_unlock(&dcache_lock
);
1383 * When a file is deleted, we have two options:
1384 * - turn this dentry into a negative dentry
1385 * - unhash this dentry and free it.
1387 * Usually, we want to just turn this into
1388 * a negative dentry, but if anybody else is
1389 * currently using the dentry or the inode
1390 * we can't do that and we fall back on removing
1391 * it from the hash queues and waiting for
1392 * it to be deleted later when it has no users
1396 * d_delete - delete a dentry
1397 * @dentry: The dentry to delete
1399 * Turn the dentry into a negative dentry if possible, otherwise
1400 * remove it from the hash queues so it can be deleted later
1403 void d_delete(struct dentry
* dentry
)
1407 * Are we the only user?
1409 spin_lock(&dcache_lock
);
1410 spin_lock(&dentry
->d_lock
);
1411 isdir
= S_ISDIR(dentry
->d_inode
->i_mode
);
1412 if (atomic_read(&dentry
->d_count
) == 1) {
1413 dentry_iput(dentry
);
1414 fsnotify_nameremove(dentry
, isdir
);
1416 /* remove this and other inotify debug checks after 2.6.18 */
1417 dentry
->d_flags
&= ~DCACHE_INOTIFY_PARENT_WATCHED
;
1421 if (!d_unhashed(dentry
))
1424 spin_unlock(&dentry
->d_lock
);
1425 spin_unlock(&dcache_lock
);
1427 fsnotify_nameremove(dentry
, isdir
);
1430 static void __d_rehash(struct dentry
* entry
, struct hlist_head
*list
)
1433 entry
->d_flags
&= ~DCACHE_UNHASHED
;
1434 hlist_add_head_rcu(&entry
->d_hash
, list
);
1437 static void _d_rehash(struct dentry
* entry
)
1439 __d_rehash(entry
, d_hash(entry
->d_parent
, entry
->d_name
.hash
));
1443 * d_rehash - add an entry back to the hash
1444 * @entry: dentry to add to the hash
1446 * Adds a dentry to the hash according to its name.
1449 void d_rehash(struct dentry
* entry
)
1451 spin_lock(&dcache_lock
);
1452 spin_lock(&entry
->d_lock
);
1454 spin_unlock(&entry
->d_lock
);
1455 spin_unlock(&dcache_lock
);
1458 #define do_switch(x,y) do { \
1459 __typeof__ (x) __tmp = x; \
1460 x = y; y = __tmp; } while (0)
1463 * When switching names, the actual string doesn't strictly have to
1464 * be preserved in the target - because we're dropping the target
1465 * anyway. As such, we can just do a simple memcpy() to copy over
1466 * the new name before we switch.
1468 * Note that we have to be a lot more careful about getting the hash
1469 * switched - we have to switch the hash value properly even if it
1470 * then no longer matches the actual (corrupted) string of the target.
1471 * The hash value has to match the hash queue that the dentry is on..
1473 static void switch_names(struct dentry
*dentry
, struct dentry
*target
)
1475 if (dname_external(target
)) {
1476 if (dname_external(dentry
)) {
1478 * Both external: swap the pointers
1480 do_switch(target
->d_name
.name
, dentry
->d_name
.name
);
1483 * dentry:internal, target:external. Steal target's
1484 * storage and make target internal.
1486 dentry
->d_name
.name
= target
->d_name
.name
;
1487 target
->d_name
.name
= target
->d_iname
;
1490 if (dname_external(dentry
)) {
1492 * dentry:external, target:internal. Give dentry's
1493 * storage to target and make dentry internal
1495 memcpy(dentry
->d_iname
, target
->d_name
.name
,
1496 target
->d_name
.len
+ 1);
1497 target
->d_name
.name
= dentry
->d_name
.name
;
1498 dentry
->d_name
.name
= dentry
->d_iname
;
1501 * Both are internal. Just copy target to dentry
1503 memcpy(dentry
->d_iname
, target
->d_name
.name
,
1504 target
->d_name
.len
+ 1);
1510 * We cannibalize "target" when moving dentry on top of it,
1511 * because it's going to be thrown away anyway. We could be more
1512 * polite about it, though.
1514 * This forceful removal will result in ugly /proc output if
1515 * somebody holds a file open that got deleted due to a rename.
1516 * We could be nicer about the deleted file, and let it show
1517 * up under the name it got deleted rather than the name that
1522 * d_move_locked - move a dentry
1523 * @dentry: entry to move
1524 * @target: new dentry
1526 * Update the dcache to reflect the move of a file name. Negative
1527 * dcache entries should not be moved in this way.
1529 static void d_move_locked(struct dentry
* dentry
, struct dentry
* target
)
1531 struct hlist_head
*list
;
1533 if (!dentry
->d_inode
)
1534 printk(KERN_WARNING
"VFS: moving negative dcache entry\n");
1536 write_seqlock(&rename_lock
);
1538 * XXXX: do we really need to take target->d_lock?
1540 if (target
< dentry
) {
1541 spin_lock(&target
->d_lock
);
1542 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1544 spin_lock(&dentry
->d_lock
);
1545 spin_lock_nested(&target
->d_lock
, DENTRY_D_LOCK_NESTED
);
1548 /* Move the dentry to the target hash queue, if on different bucket */
1549 if (dentry
->d_flags
& DCACHE_UNHASHED
)
1550 goto already_unhashed
;
1552 hlist_del_rcu(&dentry
->d_hash
);
1555 list
= d_hash(target
->d_parent
, target
->d_name
.hash
);
1556 __d_rehash(dentry
, list
);
1558 /* Unhash the target: dput() will then get rid of it */
1561 list_del(&dentry
->d_u
.d_child
);
1562 list_del(&target
->d_u
.d_child
);
1564 /* Switch the names.. */
1565 switch_names(dentry
, target
);
1566 do_switch(dentry
->d_name
.len
, target
->d_name
.len
);
1567 do_switch(dentry
->d_name
.hash
, target
->d_name
.hash
);
1569 /* ... and switch the parents */
1570 if (IS_ROOT(dentry
)) {
1571 dentry
->d_parent
= target
->d_parent
;
1572 target
->d_parent
= target
;
1573 INIT_LIST_HEAD(&target
->d_u
.d_child
);
1575 do_switch(dentry
->d_parent
, target
->d_parent
);
1577 /* And add them back to the (new) parent lists */
1578 list_add(&target
->d_u
.d_child
, &target
->d_parent
->d_subdirs
);
1581 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
1582 spin_unlock(&target
->d_lock
);
1583 fsnotify_d_move(dentry
);
1584 spin_unlock(&dentry
->d_lock
);
1585 write_sequnlock(&rename_lock
);
1589 * d_move - move a dentry
1590 * @dentry: entry to move
1591 * @target: new dentry
1593 * Update the dcache to reflect the move of a file name. Negative
1594 * dcache entries should not be moved in this way.
1597 void d_move(struct dentry
* dentry
, struct dentry
* target
)
1599 spin_lock(&dcache_lock
);
1600 d_move_locked(dentry
, target
);
1601 spin_unlock(&dcache_lock
);
1605 * Helper that returns 1 if p1 is a parent of p2, else 0
1607 static int d_isparent(struct dentry
*p1
, struct dentry
*p2
)
1611 for (p
= p2
; p
->d_parent
!= p
; p
= p
->d_parent
) {
1612 if (p
->d_parent
== p1
)
1619 * This helper attempts to cope with remotely renamed directories
1621 * It assumes that the caller is already holding
1622 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1624 * Note: If ever the locking in lock_rename() changes, then please
1625 * remember to update this too...
1627 * On return, dcache_lock will have been unlocked.
1629 static struct dentry
*__d_unalias(struct dentry
*dentry
, struct dentry
*alias
)
1631 struct mutex
*m1
= NULL
, *m2
= NULL
;
1634 /* If alias and dentry share a parent, then no extra locks required */
1635 if (alias
->d_parent
== dentry
->d_parent
)
1638 /* Check for loops */
1639 ret
= ERR_PTR(-ELOOP
);
1640 if (d_isparent(alias
, dentry
))
1643 /* See lock_rename() */
1644 ret
= ERR_PTR(-EBUSY
);
1645 if (!mutex_trylock(&dentry
->d_sb
->s_vfs_rename_mutex
))
1647 m1
= &dentry
->d_sb
->s_vfs_rename_mutex
;
1648 if (!mutex_trylock(&alias
->d_parent
->d_inode
->i_mutex
))
1650 m2
= &alias
->d_parent
->d_inode
->i_mutex
;
1652 d_move_locked(alias
, dentry
);
1655 spin_unlock(&dcache_lock
);
1664 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1665 * named dentry in place of the dentry to be replaced.
1667 static void __d_materialise_dentry(struct dentry
*dentry
, struct dentry
*anon
)
1669 struct dentry
*dparent
, *aparent
;
1671 switch_names(dentry
, anon
);
1672 do_switch(dentry
->d_name
.len
, anon
->d_name
.len
);
1673 do_switch(dentry
->d_name
.hash
, anon
->d_name
.hash
);
1675 dparent
= dentry
->d_parent
;
1676 aparent
= anon
->d_parent
;
1678 dentry
->d_parent
= (aparent
== anon
) ? dentry
: aparent
;
1679 list_del(&dentry
->d_u
.d_child
);
1680 if (!IS_ROOT(dentry
))
1681 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
1683 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
1685 anon
->d_parent
= (dparent
== dentry
) ? anon
: dparent
;
1686 list_del(&anon
->d_u
.d_child
);
1688 list_add(&anon
->d_u
.d_child
, &anon
->d_parent
->d_subdirs
);
1690 INIT_LIST_HEAD(&anon
->d_u
.d_child
);
1692 anon
->d_flags
&= ~DCACHE_DISCONNECTED
;
1696 * d_materialise_unique - introduce an inode into the tree
1697 * @dentry: candidate dentry
1698 * @inode: inode to bind to the dentry, to which aliases may be attached
1700 * Introduces an dentry into the tree, substituting an extant disconnected
1701 * root directory alias in its place if there is one
1703 struct dentry
*d_materialise_unique(struct dentry
*dentry
, struct inode
*inode
)
1705 struct dentry
*actual
;
1707 BUG_ON(!d_unhashed(dentry
));
1709 spin_lock(&dcache_lock
);
1713 dentry
->d_inode
= NULL
;
1717 if (S_ISDIR(inode
->i_mode
)) {
1718 struct dentry
*alias
;
1720 /* Does an aliased dentry already exist? */
1721 alias
= __d_find_alias(inode
, 0);
1724 /* Is this an anonymous mountpoint that we could splice
1726 if (IS_ROOT(alias
)) {
1727 spin_lock(&alias
->d_lock
);
1728 __d_materialise_dentry(dentry
, alias
);
1732 /* Nope, but we must(!) avoid directory aliasing */
1733 actual
= __d_unalias(dentry
, alias
);
1740 /* Add a unique reference */
1741 actual
= __d_instantiate_unique(dentry
, inode
);
1744 else if (unlikely(!d_unhashed(actual
)))
1745 goto shouldnt_be_hashed
;
1748 spin_lock(&actual
->d_lock
);
1751 spin_unlock(&actual
->d_lock
);
1752 spin_unlock(&dcache_lock
);
1754 if (actual
== dentry
) {
1755 security_d_instantiate(dentry
, inode
);
1763 spin_unlock(&dcache_lock
);
1765 goto shouldnt_be_hashed
;
1769 * d_path - return the path of a dentry
1770 * @dentry: dentry to report
1771 * @vfsmnt: vfsmnt to which the dentry belongs
1772 * @root: root dentry
1773 * @rootmnt: vfsmnt to which the root dentry belongs
1774 * @buffer: buffer to return value in
1775 * @buflen: buffer length
1777 * Convert a dentry into an ASCII path name. If the entry has been deleted
1778 * the string " (deleted)" is appended. Note that this is ambiguous.
1780 * Returns the buffer or an error code if the path was too long.
1782 * "buflen" should be positive. Caller holds the dcache_lock.
1784 static char * __d_path( struct dentry
*dentry
, struct vfsmount
*vfsmnt
,
1785 struct dentry
*root
, struct vfsmount
*rootmnt
,
1786 char *buffer
, int buflen
)
1788 char * end
= buffer
+buflen
;
1794 if (!IS_ROOT(dentry
) && d_unhashed(dentry
)) {
1799 memcpy(end
, " (deleted)", 10);
1809 struct dentry
* parent
;
1811 if (dentry
== root
&& vfsmnt
== rootmnt
)
1813 if (dentry
== vfsmnt
->mnt_root
|| IS_ROOT(dentry
)) {
1815 spin_lock(&vfsmount_lock
);
1816 if (vfsmnt
->mnt_parent
== vfsmnt
) {
1817 spin_unlock(&vfsmount_lock
);
1820 dentry
= vfsmnt
->mnt_mountpoint
;
1821 vfsmnt
= vfsmnt
->mnt_parent
;
1822 spin_unlock(&vfsmount_lock
);
1825 parent
= dentry
->d_parent
;
1827 namelen
= dentry
->d_name
.len
;
1828 buflen
-= namelen
+ 1;
1832 memcpy(end
, dentry
->d_name
.name
, namelen
);
1841 namelen
= dentry
->d_name
.len
;
1845 retval
-= namelen
-1; /* hit the slash */
1846 memcpy(retval
, dentry
->d_name
.name
, namelen
);
1849 return ERR_PTR(-ENAMETOOLONG
);
1852 /* write full pathname into buffer and return start of pathname */
1853 char * d_path(struct dentry
*dentry
, struct vfsmount
*vfsmnt
,
1854 char *buf
, int buflen
)
1857 struct vfsmount
*rootmnt
;
1858 struct dentry
*root
;
1861 * We have various synthetic filesystems that never get mounted. On
1862 * these filesystems dentries are never used for lookup purposes, and
1863 * thus don't need to be hashed. They also don't need a name until a
1864 * user wants to identify the object in /proc/pid/fd/. The little hack
1865 * below allows us to generate a name for these objects on demand:
1867 if (dentry
->d_op
&& dentry
->d_op
->d_dname
)
1868 return dentry
->d_op
->d_dname(dentry
, buf
, buflen
);
1870 read_lock(¤t
->fs
->lock
);
1871 rootmnt
= mntget(current
->fs
->rootmnt
);
1872 root
= dget(current
->fs
->root
);
1873 read_unlock(¤t
->fs
->lock
);
1874 spin_lock(&dcache_lock
);
1875 res
= __d_path(dentry
, vfsmnt
, root
, rootmnt
, buf
, buflen
);
1876 spin_unlock(&dcache_lock
);
1883 * Helper function for dentry_operations.d_dname() members
1885 char *dynamic_dname(struct dentry
*dentry
, char *buffer
, int buflen
,
1886 const char *fmt
, ...)
1892 va_start(args
, fmt
);
1893 sz
= vsnprintf(temp
, sizeof(temp
), fmt
, args
) + 1;
1896 if (sz
> sizeof(temp
) || sz
> buflen
)
1897 return ERR_PTR(-ENAMETOOLONG
);
1899 buffer
+= buflen
- sz
;
1900 return memcpy(buffer
, temp
, sz
);
1904 * NOTE! The user-level library version returns a
1905 * character pointer. The kernel system call just
1906 * returns the length of the buffer filled (which
1907 * includes the ending '\0' character), or a negative
1908 * error value. So libc would do something like
1910 * char *getcwd(char * buf, size_t size)
1914 * retval = sys_getcwd(buf, size);
1921 asmlinkage
long sys_getcwd(char __user
*buf
, unsigned long size
)
1924 struct vfsmount
*pwdmnt
, *rootmnt
;
1925 struct dentry
*pwd
, *root
;
1926 char *page
= (char *) __get_free_page(GFP_USER
);
1931 read_lock(¤t
->fs
->lock
);
1932 pwdmnt
= mntget(current
->fs
->pwdmnt
);
1933 pwd
= dget(current
->fs
->pwd
);
1934 rootmnt
= mntget(current
->fs
->rootmnt
);
1935 root
= dget(current
->fs
->root
);
1936 read_unlock(¤t
->fs
->lock
);
1939 /* Has the current directory has been unlinked? */
1940 spin_lock(&dcache_lock
);
1941 if (pwd
->d_parent
== pwd
|| !d_unhashed(pwd
)) {
1945 cwd
= __d_path(pwd
, pwdmnt
, root
, rootmnt
, page
, PAGE_SIZE
);
1946 spin_unlock(&dcache_lock
);
1948 error
= PTR_ERR(cwd
);
1953 len
= PAGE_SIZE
+ page
- cwd
;
1956 if (copy_to_user(buf
, cwd
, len
))
1960 spin_unlock(&dcache_lock
);
1967 free_page((unsigned long) page
);
1972 * Test whether new_dentry is a subdirectory of old_dentry.
1974 * Trivially implemented using the dcache structure
1978 * is_subdir - is new dentry a subdirectory of old_dentry
1979 * @new_dentry: new dentry
1980 * @old_dentry: old dentry
1982 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1983 * Returns 0 otherwise.
1984 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1987 int is_subdir(struct dentry
* new_dentry
, struct dentry
* old_dentry
)
1990 struct dentry
* saved
= new_dentry
;
1993 /* need rcu_readlock to protect against the d_parent trashing due to
1998 /* for restarting inner loop in case of seq retry */
2001 seq
= read_seqbegin(&rename_lock
);
2003 if (new_dentry
!= old_dentry
) {
2004 struct dentry
* parent
= new_dentry
->d_parent
;
2005 if (parent
== new_dentry
)
2007 new_dentry
= parent
;
2013 } while (read_seqretry(&rename_lock
, seq
));
2019 void d_genocide(struct dentry
*root
)
2021 struct dentry
*this_parent
= root
;
2022 struct list_head
*next
;
2024 spin_lock(&dcache_lock
);
2026 next
= this_parent
->d_subdirs
.next
;
2028 while (next
!= &this_parent
->d_subdirs
) {
2029 struct list_head
*tmp
= next
;
2030 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
2032 if (d_unhashed(dentry
)||!dentry
->d_inode
)
2034 if (!list_empty(&dentry
->d_subdirs
)) {
2035 this_parent
= dentry
;
2038 atomic_dec(&dentry
->d_count
);
2040 if (this_parent
!= root
) {
2041 next
= this_parent
->d_u
.d_child
.next
;
2042 atomic_dec(&this_parent
->d_count
);
2043 this_parent
= this_parent
->d_parent
;
2046 spin_unlock(&dcache_lock
);
2050 * find_inode_number - check for dentry with name
2051 * @dir: directory to check
2052 * @name: Name to find.
2054 * Check whether a dentry already exists for the given name,
2055 * and return the inode number if it has an inode. Otherwise
2058 * This routine is used to post-process directory listings for
2059 * filesystems using synthetic inode numbers, and is necessary
2060 * to keep getcwd() working.
2063 ino_t
find_inode_number(struct dentry
*dir
, struct qstr
*name
)
2065 struct dentry
* dentry
;
2068 dentry
= d_hash_and_lookup(dir
, name
);
2070 if (dentry
->d_inode
)
2071 ino
= dentry
->d_inode
->i_ino
;
2077 static __initdata
unsigned long dhash_entries
;
2078 static int __init
set_dhash_entries(char *str
)
2082 dhash_entries
= simple_strtoul(str
, &str
, 0);
2085 __setup("dhash_entries=", set_dhash_entries
);
2087 static void __init
dcache_init_early(void)
2091 /* If hashes are distributed across NUMA nodes, defer
2092 * hash allocation until vmalloc space is available.
2098 alloc_large_system_hash("Dentry cache",
2099 sizeof(struct hlist_head
),
2107 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2108 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2111 static void __init
dcache_init(void)
2116 * A constructor could be added for stable state like the lists,
2117 * but it is probably not worth it because of the cache nature
2120 dentry_cache
= KMEM_CACHE(dentry
,
2121 SLAB_RECLAIM_ACCOUNT
|SLAB_PANIC
|SLAB_MEM_SPREAD
);
2123 register_shrinker(&dcache_shrinker
);
2125 /* Hash may have been set up in dcache_init_early */
2130 alloc_large_system_hash("Dentry cache",
2131 sizeof(struct hlist_head
),
2139 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2140 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2143 /* SLAB cache for __getname() consumers */
2144 struct kmem_cache
*names_cachep __read_mostly
;
2146 /* SLAB cache for file structures */
2147 struct kmem_cache
*filp_cachep __read_mostly
;
2149 EXPORT_SYMBOL(d_genocide
);
2151 void __init
vfs_caches_init_early(void)
2153 dcache_init_early();
2157 void __init
vfs_caches_init(unsigned long mempages
)
2159 unsigned long reserve
;
2161 /* Base hash sizes on available memory, with a reserve equal to
2162 150% of current kernel size */
2164 reserve
= min((mempages
- nr_free_pages()) * 3/2, mempages
- 1);
2165 mempages
-= reserve
;
2167 names_cachep
= kmem_cache_create("names_cache", PATH_MAX
, 0,
2168 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
2170 filp_cachep
= kmem_cache_create("filp", sizeof(struct file
), 0,
2171 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
2175 files_init(mempages
);
2181 EXPORT_SYMBOL(d_alloc
);
2182 EXPORT_SYMBOL(d_alloc_anon
);
2183 EXPORT_SYMBOL(d_alloc_root
);
2184 EXPORT_SYMBOL(d_delete
);
2185 EXPORT_SYMBOL(d_find_alias
);
2186 EXPORT_SYMBOL(d_instantiate
);
2187 EXPORT_SYMBOL(d_invalidate
);
2188 EXPORT_SYMBOL(d_lookup
);
2189 EXPORT_SYMBOL(d_move
);
2190 EXPORT_SYMBOL_GPL(d_materialise_unique
);
2191 EXPORT_SYMBOL(d_path
);
2192 EXPORT_SYMBOL(d_prune_aliases
);
2193 EXPORT_SYMBOL(d_rehash
);
2194 EXPORT_SYMBOL(d_splice_alias
);
2195 EXPORT_SYMBOL(d_validate
);
2196 EXPORT_SYMBOL(dget_locked
);
2197 EXPORT_SYMBOL(dput
);
2198 EXPORT_SYMBOL(find_inode_number
);
2199 EXPORT_SYMBOL(have_submounts
);
2200 EXPORT_SYMBOL(names_cachep
);
2201 EXPORT_SYMBOL(shrink_dcache_parent
);
2202 EXPORT_SYMBOL(shrink_dcache_sb
);