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/config.h>
18 #include <linux/string.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/smp_lock.h>
24 #include <linux/cache.h>
25 #include <linux/module.h>
26 #include <linux/mount.h>
27 #include <linux/file.h>
28 #include <asm/uaccess.h>
29 #include <linux/security.h>
30 #include <linux/seqlock.h>
32 #define DCACHE_PARANOIA 1
33 /* #define DCACHE_DEBUG 1 */
35 spinlock_t dcache_lock __cacheline_aligned_in_smp
= SPIN_LOCK_UNLOCKED
;
36 seqlock_t rename_lock __cacheline_aligned_in_smp
= SEQLOCK_UNLOCKED
;
38 static kmem_cache_t
*dentry_cache
;
41 * This is the single most critical data structure when it comes
42 * to the dcache: the hashtable for lookups. Somebody should try
43 * to make this good - I've just made it work.
45 * This hash-function tries to avoid losing too many bits of hash
46 * information, yet avoid using a prime hash-size or similar.
48 #define D_HASHBITS d_hash_shift
49 #define D_HASHMASK d_hash_mask
51 static unsigned int d_hash_mask
;
52 static unsigned int d_hash_shift
;
53 static struct hlist_head
*dentry_hashtable
;
54 static LIST_HEAD(dentry_unused
);
56 /* Statistics gathering. */
57 struct dentry_stat_t dentry_stat
= {
61 static void d_callback(void *arg
)
63 struct dentry
* dentry
= (struct dentry
*)arg
;
65 if (dname_external(dentry
)) {
66 kfree(dentry
->d_qstr
);
68 kmem_cache_free(dentry_cache
, dentry
);
72 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
75 static void d_free(struct dentry
*dentry
)
77 if (dentry
->d_op
&& dentry
->d_op
->d_release
)
78 dentry
->d_op
->d_release(dentry
);
79 call_rcu(&dentry
->d_rcu
, d_callback
, dentry
);
83 * Release the dentry's inode, using the filesystem
84 * d_iput() operation if defined.
85 * Called with dcache_lock held, drops it.
87 static inline void dentry_iput(struct dentry
* dentry
)
89 struct inode
*inode
= dentry
->d_inode
;
91 dentry
->d_inode
= NULL
;
92 list_del_init(&dentry
->d_alias
);
93 spin_unlock(&dcache_lock
);
94 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
95 dentry
->d_op
->d_iput(dentry
, inode
);
99 spin_unlock(&dcache_lock
);
105 * This is complicated by the fact that we do not want to put
106 * dentries that are no longer on any hash chain on the unused
107 * list: we'd much rather just get rid of them immediately.
109 * However, that implies that we have to traverse the dentry
110 * tree upwards to the parents which might _also_ now be
111 * scheduled for deletion (it may have been only waiting for
112 * its last child to go away).
114 * This tail recursion is done by hand as we don't want to depend
115 * on the compiler to always get this right (gcc generally doesn't).
116 * Real recursion would eat up our stack space.
120 * dput - release a dentry
121 * @dentry: dentry to release
123 * Release a dentry. This will drop the usage count and if appropriate
124 * call the dentry unlink method as well as removing it from the queues and
125 * releasing its resources. If the parent dentries were scheduled for release
126 * they too may now get deleted.
128 * no dcache lock, please.
131 void dput(struct dentry
*dentry
)
137 if (!atomic_dec_and_lock(&dentry
->d_count
, &dcache_lock
))
140 spin_lock(&dentry
->d_lock
);
141 if (atomic_read(&dentry
->d_count
)) {
142 spin_unlock(&dentry
->d_lock
);
143 spin_unlock(&dcache_lock
);
148 * AV: ->d_delete() is _NOT_ allowed to block now.
150 if (dentry
->d_op
&& dentry
->d_op
->d_delete
) {
151 if (dentry
->d_op
->d_delete(dentry
))
154 /* Unreachable? Get rid of it */
155 if (d_unhashed(dentry
))
157 if (list_empty(&dentry
->d_lru
)) {
158 dentry
->d_vfs_flags
|= DCACHE_REFERENCED
;
159 list_add(&dentry
->d_lru
, &dentry_unused
);
160 dentry_stat
.nr_unused
++;
162 spin_unlock(&dentry
->d_lock
);
163 spin_unlock(&dcache_lock
);
170 struct dentry
*parent
;
172 /* If dentry was on d_lru list
173 * delete it from there
175 if (!list_empty(&dentry
->d_lru
)) {
176 list_del(&dentry
->d_lru
);
177 dentry_stat
.nr_unused
--;
179 list_del(&dentry
->d_child
);
180 spin_unlock(&dentry
->d_lock
);
181 dentry_stat
.nr_dentry
--; /* For d_free, below */
182 /* drops the lock, at that point nobody can reach this dentry */
184 parent
= dentry
->d_parent
;
186 if (dentry
== parent
)
194 * d_invalidate - invalidate a dentry
195 * @dentry: dentry to invalidate
197 * Try to invalidate the dentry if it turns out to be
198 * possible. If there are other dentries that can be
199 * reached through this one we can't delete it and we
200 * return -EBUSY. On success we return 0.
205 int d_invalidate(struct dentry
* dentry
)
208 * If it's already been dropped, return OK.
210 spin_lock(&dcache_lock
);
211 if (d_unhashed(dentry
)) {
212 spin_unlock(&dcache_lock
);
216 * Check whether to do a partial shrink_dcache
217 * to get rid of unused child entries.
219 if (!list_empty(&dentry
->d_subdirs
)) {
220 spin_unlock(&dcache_lock
);
221 shrink_dcache_parent(dentry
);
222 spin_lock(&dcache_lock
);
226 * Somebody else still using it?
228 * If it's a directory, we can't drop it
229 * for fear of somebody re-populating it
230 * with children (even though dropping it
231 * would make it unreachable from the root,
232 * we might still populate it if it was a
233 * working directory or similar).
235 spin_lock(&dentry
->d_lock
);
236 if (atomic_read(&dentry
->d_count
) > 1) {
237 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
)) {
238 spin_unlock(&dentry
->d_lock
);
239 spin_unlock(&dcache_lock
);
245 spin_unlock(&dentry
->d_lock
);
246 spin_unlock(&dcache_lock
);
250 /* This should be called _only_ with dcache_lock held */
252 static inline struct dentry
* __dget_locked(struct dentry
*dentry
)
254 atomic_inc(&dentry
->d_count
);
255 if (atomic_read(&dentry
->d_count
) == 1) {
256 dentry_stat
.nr_unused
--;
257 list_del_init(&dentry
->d_lru
);
262 struct dentry
* dget_locked(struct dentry
*dentry
)
264 return __dget_locked(dentry
);
268 * d_find_alias - grab a hashed alias of inode
269 * @inode: inode in question
271 * If inode has a hashed alias - acquire the reference to alias and
272 * return it. Otherwise return NULL. Notice that if inode is a directory
273 * there can be only one alias and it can be unhashed only if it has
276 * If the inode has a DCACHE_DISCONNECTED alias, then prefer
277 * any other hashed alias over that one.
280 struct dentry
* d_find_alias(struct inode
*inode
)
282 struct list_head
*head
, *next
, *tmp
;
283 struct dentry
*alias
, *discon_alias
=NULL
;
285 spin_lock(&dcache_lock
);
286 head
= &inode
->i_dentry
;
287 next
= inode
->i_dentry
.next
;
288 while (next
!= head
) {
292 alias
= list_entry(tmp
, struct dentry
, d_alias
);
293 if (!d_unhashed(alias
)) {
294 if (alias
->d_flags
& DCACHE_DISCONNECTED
)
295 discon_alias
= alias
;
297 __dget_locked(alias
);
298 spin_unlock(&dcache_lock
);
304 __dget_locked(discon_alias
);
305 spin_unlock(&dcache_lock
);
310 * Try to kill dentries associated with this inode.
311 * WARNING: you must own a reference to inode.
313 void d_prune_aliases(struct inode
*inode
)
315 struct list_head
*tmp
, *head
= &inode
->i_dentry
;
317 spin_lock(&dcache_lock
);
319 while ((tmp
= tmp
->next
) != head
) {
320 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_alias
);
321 if (!atomic_read(&dentry
->d_count
)) {
322 __dget_locked(dentry
);
324 spin_unlock(&dcache_lock
);
329 spin_unlock(&dcache_lock
);
333 * Throw away a dentry - free the inode, dput the parent.
334 * This requires that the LRU list has already been
336 * Called with dcache_lock, drops it and then regains.
338 static inline void prune_one_dentry(struct dentry
* dentry
)
340 struct dentry
* parent
;
343 list_del(&dentry
->d_child
);
344 spin_unlock(&dentry
->d_lock
);
345 dentry_stat
.nr_dentry
--; /* For d_free, below */
347 parent
= dentry
->d_parent
;
349 if (parent
!= dentry
)
351 spin_lock(&dcache_lock
);
355 * prune_dcache - shrink the dcache
356 * @count: number of entries to try and free
358 * Shrink the dcache. This is done when we need
359 * more memory, or simply when we need to unmount
360 * something (at which point we need to unuse
363 * This function may fail to free any resources if
364 * all the dentries are in use.
367 static void prune_dcache(int count
)
369 spin_lock(&dcache_lock
);
370 for (; count
; count
--) {
371 struct dentry
*dentry
;
372 struct list_head
*tmp
;
374 tmp
= dentry_unused
.prev
;
375 if (tmp
== &dentry_unused
)
378 prefetch(dentry_unused
.prev
);
379 dentry_stat
.nr_unused
--;
380 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
382 spin_lock(&dentry
->d_lock
);
383 /* leave inuse dentries */
384 if (atomic_read(&dentry
->d_count
)) {
385 spin_unlock(&dentry
->d_lock
);
388 /* If the dentry was recently referenced, don't free it. */
389 if (dentry
->d_vfs_flags
& DCACHE_REFERENCED
) {
390 dentry
->d_vfs_flags
&= ~DCACHE_REFERENCED
;
391 list_add(&dentry
->d_lru
, &dentry_unused
);
392 dentry_stat
.nr_unused
++;
393 spin_unlock(&dentry
->d_lock
);
396 prune_one_dentry(dentry
);
398 spin_unlock(&dcache_lock
);
402 * Shrink the dcache for the specified super block.
403 * This allows us to unmount a device without disturbing
404 * the dcache for the other devices.
406 * This implementation makes just two traversals of the
407 * unused list. On the first pass we move the selected
408 * dentries to the most recent end, and on the second
409 * pass we free them. The second pass must restart after
410 * each dput(), but since the target dentries are all at
411 * the end, it's really just a single traversal.
415 * shrink_dcache_sb - shrink dcache for a superblock
418 * Shrink the dcache for the specified super block. This
419 * is used to free the dcache before unmounting a file
423 void shrink_dcache_sb(struct super_block
* sb
)
425 struct list_head
*tmp
, *next
;
426 struct dentry
*dentry
;
429 * Pass one ... move the dentries for the specified
430 * superblock to the most recent end of the unused list.
432 spin_lock(&dcache_lock
);
433 next
= dentry_unused
.next
;
434 while (next
!= &dentry_unused
) {
437 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
438 if (dentry
->d_sb
!= sb
)
441 list_add(tmp
, &dentry_unused
);
445 * Pass two ... free the dentries for this superblock.
448 next
= dentry_unused
.next
;
449 while (next
!= &dentry_unused
) {
452 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
453 if (dentry
->d_sb
!= sb
)
455 dentry_stat
.nr_unused
--;
457 spin_lock(&dentry
->d_lock
);
458 if (atomic_read(&dentry
->d_count
)) {
459 spin_unlock(&dentry
->d_lock
);
462 prune_one_dentry(dentry
);
465 spin_unlock(&dcache_lock
);
469 * Search for at least 1 mount point in the dentry's subdirs.
470 * We descend to the next level whenever the d_subdirs
471 * list is non-empty and continue searching.
475 * have_submounts - check for mounts over a dentry
476 * @parent: dentry to check.
478 * Return true if the parent or its subdirectories contain
482 int have_submounts(struct dentry
*parent
)
484 struct dentry
*this_parent
= parent
;
485 struct list_head
*next
;
487 spin_lock(&dcache_lock
);
488 if (d_mountpoint(parent
))
491 next
= this_parent
->d_subdirs
.next
;
493 while (next
!= &this_parent
->d_subdirs
) {
494 struct list_head
*tmp
= next
;
495 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_child
);
497 /* Have we found a mount point ? */
498 if (d_mountpoint(dentry
))
500 if (!list_empty(&dentry
->d_subdirs
)) {
501 this_parent
= dentry
;
506 * All done at this level ... ascend and resume the search.
508 if (this_parent
!= parent
) {
509 next
= this_parent
->d_child
.next
;
510 this_parent
= this_parent
->d_parent
;
513 spin_unlock(&dcache_lock
);
514 return 0; /* No mount points found in tree */
516 spin_unlock(&dcache_lock
);
521 * Search the dentry child list for the specified parent,
522 * and move any unused dentries to the end of the unused
523 * list for prune_dcache(). We descend to the next level
524 * whenever the d_subdirs list is non-empty and continue
527 static int select_parent(struct dentry
* parent
)
529 struct dentry
*this_parent
= parent
;
530 struct list_head
*next
;
533 spin_lock(&dcache_lock
);
535 next
= this_parent
->d_subdirs
.next
;
537 while (next
!= &this_parent
->d_subdirs
) {
538 struct list_head
*tmp
= next
;
539 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_child
);
542 if (!list_empty(&dentry
->d_lru
)) {
543 dentry_stat
.nr_unused
--;
544 list_del_init(&dentry
->d_lru
);
547 * move only zero ref count dentries to the end
548 * of the unused list for prune_dcache
550 if (!atomic_read(&dentry
->d_count
)) {
551 list_add(&dentry
->d_lru
, dentry_unused
.prev
);
552 dentry_stat
.nr_unused
++;
556 * Descend a level if the d_subdirs list is non-empty.
558 if (!list_empty(&dentry
->d_subdirs
)) {
559 this_parent
= dentry
;
561 printk(KERN_DEBUG
"select_parent: descending to %s/%s, found=%d\n",
562 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, found
);
568 * All done at this level ... ascend and resume the search.
570 if (this_parent
!= parent
) {
571 next
= this_parent
->d_child
.next
;
572 this_parent
= this_parent
->d_parent
;
574 printk(KERN_DEBUG
"select_parent: ascending to %s/%s, found=%d\n",
575 this_parent
->d_parent
->d_name
.name
, this_parent
->d_name
.name
, found
);
579 spin_unlock(&dcache_lock
);
584 * shrink_dcache_parent - prune dcache
585 * @parent: parent of entries to prune
587 * Prune the dcache to remove unused children of the parent dentry.
590 void shrink_dcache_parent(struct dentry
* parent
)
594 while ((found
= select_parent(parent
)) != 0)
599 * shrink_dcache_anon - further prune the cache
600 * @head: head of d_hash list of dentries to prune
602 * Prune the dentries that are anonymous
604 * parsing d_hash list does not read_barrier_depends() as it
605 * done under dcache_lock.
608 void shrink_dcache_anon(struct hlist_head
*head
)
610 struct hlist_node
*lp
;
614 spin_lock(&dcache_lock
);
615 hlist_for_each(lp
, head
) {
616 struct dentry
*this = hlist_entry(lp
, struct dentry
, d_hash
);
617 if (!list_empty(&this->d_lru
)) {
618 dentry_stat
.nr_unused
--;
619 list_del(&this->d_lru
);
623 * move only zero ref count dentries to the end
624 * of the unused list for prune_dcache
626 if (!atomic_read(&this->d_count
)) {
627 list_add_tail(&this->d_lru
, &dentry_unused
);
628 dentry_stat
.nr_unused
++;
632 spin_unlock(&dcache_lock
);
638 * This is called from kswapd when we think we need some more memory.
640 * We don't want the VM to steal _all_ unused dcache. Because that leads to
641 * the VM stealing all unused inodes, which shoots down recently-used
642 * pagecache. So what we do is to tell fibs to the VM about how many reapable
643 * objects there are in this cache. If the number of unused dentries is
644 * less than half of the total dentry count then return zero. The net effect
645 * is that the number of unused dentries will be, at a minimum, equal to the
646 * number of used ones.
648 * If unused_ratio is set to 5, the number of unused dentries will not fall
649 * below 5* the number of used ones.
651 static int shrink_dcache_memory(int nr
, unsigned int gfp_mask
)
655 const int unused_ratio
= 1;
659 * Nasty deadlock avoidance.
661 * ext2_new_block->getblk->GFP->shrink_dcache_memory->
662 * prune_dcache->prune_one_dentry->dput->dentry_iput->iput->
663 * inode->i_sb->s_op->put_inode->ext2_discard_prealloc->
664 * ext2_free_blocks->lock_super->DEADLOCK.
666 * We should make sure we don't hold the superblock lock over
667 * block allocations, but for now:
669 if (gfp_mask
& __GFP_FS
)
672 nr_unused
= dentry_stat
.nr_unused
;
673 nr_used
= dentry_stat
.nr_dentry
- nr_unused
;
674 if (nr_unused
< nr_used
* unused_ratio
)
676 return nr_unused
- nr_used
* unused_ratio
;
679 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
682 * d_alloc - allocate a dcache entry
683 * @parent: parent of entry to allocate
684 * @name: qstr of the name
686 * Allocates a dentry. It returns %NULL if there is insufficient memory
687 * available. On a success the dentry is returned. The name passed in is
688 * copied and the copy passed in may be reused after this call.
691 struct dentry
* d_alloc(struct dentry
* parent
, const struct qstr
*name
)
694 struct dentry
*dentry
;
697 dentry
= kmem_cache_alloc(dentry_cache
, GFP_KERNEL
);
701 if (name
->len
> DNAME_INLINE_LEN
-1) {
702 qstr
= kmalloc(sizeof(*qstr
) + NAME_ALLOC_LEN(name
->len
),
705 kmem_cache_free(dentry_cache
, dentry
);
708 qstr
->name
= qstr
->name_str
;
709 qstr
->len
= name
->len
;
710 qstr
->hash
= name
->hash
;
711 dentry
->d_qstr
= qstr
;
712 str
= qstr
->name_str
;
714 dentry
->d_qstr
= &dentry
->d_name
;
715 str
= dentry
->d_iname
;
718 memcpy(str
, name
->name
, name
->len
);
721 atomic_set(&dentry
->d_count
, 1);
722 dentry
->d_vfs_flags
= DCACHE_UNHASHED
;
723 dentry
->d_lock
= SPIN_LOCK_UNLOCKED
;
725 dentry
->d_inode
= NULL
;
726 dentry
->d_parent
= NULL
;
727 dentry
->d_move_count
= 0;
729 dentry
->d_name
.name
= str
;
730 dentry
->d_name
.len
= name
->len
;
731 dentry
->d_name
.hash
= name
->hash
;
733 dentry
->d_fsdata
= NULL
;
734 dentry
->d_mounted
= 0;
735 dentry
->d_cookie
= NULL
;
736 dentry
->d_bucket
= NULL
;
737 INIT_HLIST_NODE(&dentry
->d_hash
);
738 INIT_LIST_HEAD(&dentry
->d_lru
);
739 INIT_LIST_HEAD(&dentry
->d_subdirs
);
740 INIT_LIST_HEAD(&dentry
->d_alias
);
743 dentry
->d_parent
= dget(parent
);
744 dentry
->d_sb
= parent
->d_sb
;
746 INIT_LIST_HEAD(&dentry
->d_child
);
749 spin_lock(&dcache_lock
);
751 list_add(&dentry
->d_child
, &parent
->d_subdirs
);
752 dentry_stat
.nr_dentry
++;
753 spin_unlock(&dcache_lock
);
759 * d_instantiate - fill in inode information for a dentry
760 * @entry: dentry to complete
761 * @inode: inode to attach to this dentry
763 * Fill in inode information in the entry.
765 * This turns negative dentries into productive full members
768 * NOTE! This assumes that the inode count has been incremented
769 * (or otherwise set) by the caller to indicate that it is now
770 * in use by the dcache.
773 void d_instantiate(struct dentry
*entry
, struct inode
* inode
)
775 if (!list_empty(&entry
->d_alias
)) BUG();
776 spin_lock(&dcache_lock
);
778 list_add(&entry
->d_alias
, &inode
->i_dentry
);
779 entry
->d_inode
= inode
;
780 spin_unlock(&dcache_lock
);
781 security_d_instantiate(entry
, inode
);
785 * d_alloc_root - allocate root dentry
786 * @root_inode: inode to allocate the root for
788 * Allocate a root ("/") dentry for the inode given. The inode is
789 * instantiated and returned. %NULL is returned if there is insufficient
790 * memory or the inode passed is %NULL.
793 struct dentry
* d_alloc_root(struct inode
* root_inode
)
795 struct dentry
*res
= NULL
;
798 static const struct qstr name
= { .name
= "/", .len
= 1, .hash
= 0 };
799 res
= d_alloc(NULL
, &name
);
801 res
->d_sb
= root_inode
->i_sb
;
803 d_instantiate(res
, root_inode
);
809 static inline struct hlist_head
* d_hash(struct dentry
* parent
, unsigned long hash
)
811 hash
+= (unsigned long) parent
/ L1_CACHE_BYTES
;
812 hash
= hash
^ (hash
>> D_HASHBITS
);
813 return dentry_hashtable
+ (hash
& D_HASHMASK
);
817 * d_alloc_anon - allocate an anonymous dentry
818 * @inode: inode to allocate the dentry for
820 * This is similar to d_alloc_root. It is used by filesystems when
821 * creating a dentry for a given inode, often in the process of
822 * mapping a filehandle to a dentry. The returned dentry may be
823 * anonymous, or may have a full name (if the inode was already
824 * in the cache). The file system may need to make further
825 * efforts to connect this dentry into the dcache properly.
827 * When called on a directory inode, we must ensure that
828 * the inode only ever has one dentry. If a dentry is
829 * found, that is returned instead of allocating a new one.
831 * On successful return, the reference to the inode has been transferred
832 * to the dentry. If %NULL is returned (indicating kmalloc failure),
833 * the reference on the inode has not been released.
836 struct dentry
* d_alloc_anon(struct inode
*inode
)
838 static const struct qstr anonstring
= { "", 0, 0};
842 if ((res
= d_find_alias(inode
))) {
847 tmp
= d_alloc(NULL
, &anonstring
);
851 tmp
->d_parent
= tmp
; /* make sure dput doesn't croak */
853 spin_lock(&dcache_lock
);
854 if (S_ISDIR(inode
->i_mode
) && !list_empty(&inode
->i_dentry
)) {
855 /* A directory can only have one dentry.
856 * This (now) has one, so use it.
858 res
= list_entry(inode
->i_dentry
.next
, struct dentry
, d_alias
);
861 /* attach a disconnected dentry */
865 spin_lock(&res
->d_lock
);
866 res
->d_sb
= inode
->i_sb
;
868 res
->d_inode
= inode
;
869 res
->d_bucket
= d_hash(res
, res
->d_name
.hash
);
870 res
->d_flags
|= DCACHE_DISCONNECTED
;
871 res
->d_vfs_flags
&= ~DCACHE_UNHASHED
;
872 list_add(&res
->d_alias
, &inode
->i_dentry
);
873 hlist_add_head(&res
->d_hash
, &inode
->i_sb
->s_anon
);
874 spin_unlock(&res
->d_lock
);
876 inode
= NULL
; /* don't drop reference */
878 spin_unlock(&dcache_lock
);
889 * d_splice_alias - splice a disconnected dentry into the tree if one exists
890 * @inode: the inode which may have a disconnected dentry
891 * @dentry: a negative dentry which we want to point to the inode.
893 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
894 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
895 * and return it, else simply d_add the inode to the dentry and return NULL.
897 * This is (will be) needed in the lookup routine of any filesystem that is exportable
898 * (via knfsd) so that we can build dcache paths to directories effectively.
900 * If a dentry was found and moved, then it is returned. Otherwise NULL
901 * is returned. This matches the expected return value of ->lookup.
904 struct dentry
*d_splice_alias(struct inode
*inode
, struct dentry
*dentry
)
906 struct dentry
*new = NULL
;
908 if (inode
&& S_ISDIR(inode
->i_mode
)) {
909 spin_lock(&dcache_lock
);
910 if (!list_empty(&inode
->i_dentry
)) {
911 new = list_entry(inode
->i_dentry
.next
, struct dentry
, d_alias
);
913 spin_unlock(&dcache_lock
);
914 security_d_instantiate(dentry
, inode
);
919 /* d_instantiate takes dcache_lock, so we do it by hand */
920 list_add(&dentry
->d_alias
, &inode
->i_dentry
);
921 dentry
->d_inode
= inode
;
922 spin_unlock(&dcache_lock
);
923 security_d_instantiate(dentry
, inode
);
927 d_add(dentry
, inode
);
933 * d_lookup - search for a dentry
934 * @parent: parent dentry
935 * @name: qstr of name we wish to find
937 * Searches the children of the parent dentry for the name in question. If
938 * the dentry is found its reference count is incremented and the dentry
939 * is returned. The caller must use d_put to free the entry when it has
940 * finished using it. %NULL is returned on failure.
942 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
943 * Memory barriers are used while updating and doing lockless traversal.
944 * To avoid races with d_move while rename is happening, d_move_count is
947 * Overflows in memcmp(), while d_move, are avoided by keeping the length
948 * and name pointer in one structure pointed by d_qstr.
950 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
951 * lookup is going on.
953 * d_lru list is not updated, which can leave non-zero d_count dentries
954 * around in d_lru list.
956 * d_lookup() is protected against the concurrent renames in some unrelated
957 * directory using the seqlockt_t rename_lock.
960 struct dentry
* d_lookup(struct dentry
* parent
, struct qstr
* name
)
962 struct dentry
* dentry
= NULL
;
966 seq
= read_seqbegin(&rename_lock
);
967 dentry
= __d_lookup(parent
, name
);
970 } while (read_seqretry(&rename_lock
, seq
));
974 struct dentry
* __d_lookup(struct dentry
* parent
, struct qstr
* name
)
976 unsigned int len
= name
->len
;
977 unsigned int hash
= name
->hash
;
978 const unsigned char *str
= name
->name
;
979 struct hlist_head
*head
= d_hash(parent
,hash
);
980 struct dentry
*found
= NULL
;
981 struct hlist_node
*node
;
985 hlist_for_each (node
, head
) {
986 struct dentry
*dentry
;
987 unsigned long move_count
;
990 smp_read_barrier_depends();
991 dentry
= hlist_entry(node
, struct dentry
, d_hash
);
993 /* if lookup ends up in a different bucket
994 * due to concurrent rename, fail it
996 if (unlikely(dentry
->d_bucket
!= head
))
1000 * We must take a snapshot of d_move_count followed by
1001 * read memory barrier before any search key comparison
1003 move_count
= dentry
->d_move_count
;
1006 if (dentry
->d_name
.hash
!= hash
)
1008 if (dentry
->d_parent
!= parent
)
1011 qstr
= dentry
->d_qstr
;
1012 smp_read_barrier_depends();
1013 if (parent
->d_op
&& parent
->d_op
->d_compare
) {
1014 if (parent
->d_op
->d_compare(parent
, qstr
, name
))
1017 if (qstr
->len
!= len
)
1019 if (memcmp(qstr
->name
, str
, len
))
1022 spin_lock(&dentry
->d_lock
);
1024 * If dentry is moved, fail the lookup
1026 if (likely(move_count
== dentry
->d_move_count
)) {
1027 if (!d_unhashed(dentry
)) {
1028 atomic_inc(&dentry
->d_count
);
1032 spin_unlock(&dentry
->d_lock
);
1041 * d_validate - verify dentry provided from insecure source
1042 * @dentry: The dentry alleged to be valid child of @dparent
1043 * @dparent: The parent dentry (known to be valid)
1044 * @hash: Hash of the dentry
1045 * @len: Length of the name
1047 * An insecure source has sent us a dentry, here we verify it and dget() it.
1048 * This is used by ncpfs in its readdir implementation.
1049 * Zero is returned in the dentry is invalid.
1052 int d_validate(struct dentry
*dentry
, struct dentry
*dparent
)
1054 unsigned long dent_addr
= (unsigned long) dentry
;
1055 unsigned long min_addr
= PAGE_OFFSET
;
1056 unsigned long align_mask
= 0x0F;
1057 struct hlist_head
*base
;
1058 struct hlist_node
*lhp
;
1060 if (dent_addr
< min_addr
)
1062 if (dent_addr
> (unsigned long)high_memory
- sizeof(struct dentry
))
1064 if (dent_addr
& align_mask
)
1066 if ((!kern_addr_valid(dent_addr
)) || (!kern_addr_valid(dent_addr
-1 +
1067 sizeof(struct dentry
))))
1070 if (dentry
->d_parent
!= dparent
)
1073 spin_lock(&dcache_lock
);
1074 base
= d_hash(dparent
, dentry
->d_name
.hash
);
1075 hlist_for_each(lhp
,base
) {
1076 /* read_barrier_depends() not required for d_hash list
1077 * as it is parsed under dcache_lock
1079 if (dentry
== hlist_entry(lhp
, struct dentry
, d_hash
)) {
1080 __dget_locked(dentry
);
1081 spin_unlock(&dcache_lock
);
1085 spin_unlock(&dcache_lock
);
1091 * When a file is deleted, we have two options:
1092 * - turn this dentry into a negative dentry
1093 * - unhash this dentry and free it.
1095 * Usually, we want to just turn this into
1096 * a negative dentry, but if anybody else is
1097 * currently using the dentry or the inode
1098 * we can't do that and we fall back on removing
1099 * it from the hash queues and waiting for
1100 * it to be deleted later when it has no users
1104 * d_delete - delete a dentry
1105 * @dentry: The dentry to delete
1107 * Turn the dentry into a negative dentry if possible, otherwise
1108 * remove it from the hash queues so it can be deleted later
1111 void d_delete(struct dentry
* dentry
)
1114 * Are we the only user?
1116 spin_lock(&dcache_lock
);
1117 spin_lock(&dentry
->d_lock
);
1118 if (atomic_read(&dentry
->d_count
) == 1) {
1119 spin_unlock(&dentry
->d_lock
);
1120 dentry_iput(dentry
);
1124 if (!d_unhashed(dentry
))
1127 spin_unlock(&dentry
->d_lock
);
1128 spin_unlock(&dcache_lock
);
1132 * d_rehash - add an entry back to the hash
1133 * @entry: dentry to add to the hash
1135 * Adds a dentry to the hash according to its name.
1138 void d_rehash(struct dentry
* entry
)
1140 struct hlist_head
*list
= d_hash(entry
->d_parent
, entry
->d_name
.hash
);
1141 spin_lock(&dcache_lock
);
1142 entry
->d_vfs_flags
&= ~DCACHE_UNHASHED
;
1143 entry
->d_bucket
= list
;
1144 hlist_add_head_rcu(&entry
->d_hash
, list
);
1145 spin_unlock(&dcache_lock
);
1148 #define do_switch(x,y) do { \
1149 __typeof__ (x) __tmp = x; \
1150 x = y; y = __tmp; } while (0)
1153 * When switching names, the actual string doesn't strictly have to
1154 * be preserved in the target - because we're dropping the target
1155 * anyway. As such, we can just do a simple memcpy() to copy over
1156 * the new name before we switch.
1158 * Note that we have to be a lot more careful about getting the hash
1159 * switched - we have to switch the hash value properly even if it
1160 * then no longer matches the actual (corrupted) string of the target.
1161 * The hash value has to match the hash queue that the dentry is on..
1163 static inline void switch_names(struct dentry
* dentry
, struct dentry
* target
)
1165 const unsigned char *old_name
, *new_name
;
1166 struct qstr
*old_qstr
, *new_qstr
;
1168 memcpy(dentry
->d_iname
, target
->d_iname
, DNAME_INLINE_LEN
);
1169 old_qstr
= target
->d_qstr
;
1170 old_name
= target
->d_name
.name
;
1171 new_qstr
= dentry
->d_qstr
;
1172 new_name
= dentry
->d_name
.name
;
1173 if (old_name
== target
->d_iname
) {
1174 old_name
= dentry
->d_iname
;
1175 old_qstr
= &dentry
->d_name
;
1177 if (new_name
== dentry
->d_iname
) {
1178 new_name
= target
->d_iname
;
1179 new_qstr
= &target
->d_name
;
1181 target
->d_name
.name
= new_name
;
1182 dentry
->d_name
.name
= old_name
;
1183 target
->d_qstr
= new_qstr
;
1184 dentry
->d_qstr
= old_qstr
;
1188 * We cannibalize "target" when moving dentry on top of it,
1189 * because it's going to be thrown away anyway. We could be more
1190 * polite about it, though.
1192 * This forceful removal will result in ugly /proc output if
1193 * somebody holds a file open that got deleted due to a rename.
1194 * We could be nicer about the deleted file, and let it show
1195 * up under the name it got deleted rather than the name that
1200 * d_move - move a dentry
1201 * @dentry: entry to move
1202 * @target: new dentry
1204 * Update the dcache to reflect the move of a file name. Negative
1205 * dcache entries should not be moved in this way.
1208 void d_move(struct dentry
* dentry
, struct dentry
* target
)
1210 if (!dentry
->d_inode
)
1211 printk(KERN_WARNING
"VFS: moving negative dcache entry\n");
1213 spin_lock(&dcache_lock
);
1214 write_seqlock(&rename_lock
);
1216 * XXXX: do we really need to take target->d_lock?
1218 if (target
< dentry
) {
1219 spin_lock(&target
->d_lock
);
1220 spin_lock(&dentry
->d_lock
);
1222 spin_lock(&dentry
->d_lock
);
1223 spin_lock(&target
->d_lock
);
1226 /* Move the dentry to the target hash queue, if on different bucket */
1227 if (dentry
->d_vfs_flags
& DCACHE_UNHASHED
)
1228 goto already_unhashed
;
1229 if (dentry
->d_bucket
!= target
->d_bucket
) {
1230 hlist_del_rcu(&dentry
->d_hash
);
1232 dentry
->d_bucket
= target
->d_bucket
;
1233 hlist_add_head_rcu(&dentry
->d_hash
, target
->d_bucket
);
1234 dentry
->d_vfs_flags
&= ~DCACHE_UNHASHED
;
1237 /* Unhash the target: dput() will then get rid of it */
1240 list_del(&dentry
->d_child
);
1241 list_del(&target
->d_child
);
1243 /* Switch the names.. */
1244 switch_names(dentry
, target
);
1246 do_switch(dentry
->d_name
.len
, target
->d_name
.len
);
1247 do_switch(dentry
->d_name
.hash
, target
->d_name
.hash
);
1249 /* ... and switch the parents */
1250 if (IS_ROOT(dentry
)) {
1251 dentry
->d_parent
= target
->d_parent
;
1252 target
->d_parent
= target
;
1253 INIT_LIST_HEAD(&target
->d_child
);
1255 do_switch(dentry
->d_parent
, target
->d_parent
);
1257 /* And add them back to the (new) parent lists */
1258 list_add(&target
->d_child
, &target
->d_parent
->d_subdirs
);
1261 list_add(&dentry
->d_child
, &dentry
->d_parent
->d_subdirs
);
1262 dentry
->d_move_count
++;
1263 spin_unlock(&target
->d_lock
);
1264 spin_unlock(&dentry
->d_lock
);
1265 write_sequnlock(&rename_lock
);
1266 spin_unlock(&dcache_lock
);
1270 * d_path - return the path of a dentry
1271 * @dentry: dentry to report
1272 * @vfsmnt: vfsmnt to which the dentry belongs
1273 * @root: root dentry
1274 * @rootmnt: vfsmnt to which the root dentry belongs
1275 * @buffer: buffer to return value in
1276 * @buflen: buffer length
1278 * Convert a dentry into an ASCII path name. If the entry has been deleted
1279 * the string " (deleted)" is appended. Note that this is ambiguous.
1281 * Returns the buffer or an error code if the path was too long.
1283 * "buflen" should be positive. Caller holds the dcache_lock.
1285 static char * __d_path( struct dentry
*dentry
, struct vfsmount
*vfsmnt
,
1286 struct dentry
*root
, struct vfsmount
*rootmnt
,
1287 char *buffer
, int buflen
)
1289 char * end
= buffer
+buflen
;
1295 if (!IS_ROOT(dentry
) && d_unhashed(dentry
)) {
1300 memcpy(end
, " (deleted)", 10);
1310 struct dentry
* parent
;
1312 if (dentry
== root
&& vfsmnt
== rootmnt
)
1314 if (dentry
== vfsmnt
->mnt_root
|| IS_ROOT(dentry
)) {
1316 if (vfsmnt
->mnt_parent
== vfsmnt
)
1318 dentry
= vfsmnt
->mnt_mountpoint
;
1319 vfsmnt
= vfsmnt
->mnt_parent
;
1322 parent
= dentry
->d_parent
;
1324 namelen
= dentry
->d_name
.len
;
1325 buflen
-= namelen
+ 1;
1329 memcpy(end
, dentry
->d_name
.name
, namelen
);
1338 namelen
= dentry
->d_name
.len
;
1342 retval
-= namelen
-1; /* hit the slash */
1343 memcpy(retval
, dentry
->d_name
.name
, namelen
);
1346 return ERR_PTR(-ENAMETOOLONG
);
1349 /* write full pathname into buffer and return start of pathname */
1350 char * d_path(struct dentry
*dentry
, struct vfsmount
*vfsmnt
,
1351 char *buf
, int buflen
)
1354 struct vfsmount
*rootmnt
;
1355 struct dentry
*root
;
1356 read_lock(¤t
->fs
->lock
);
1357 rootmnt
= mntget(current
->fs
->rootmnt
);
1358 root
= dget(current
->fs
->root
);
1359 read_unlock(¤t
->fs
->lock
);
1360 spin_lock(&dcache_lock
);
1361 res
= __d_path(dentry
, vfsmnt
, root
, rootmnt
, buf
, buflen
);
1362 spin_unlock(&dcache_lock
);
1369 * NOTE! The user-level library version returns a
1370 * character pointer. The kernel system call just
1371 * returns the length of the buffer filled (which
1372 * includes the ending '\0' character), or a negative
1373 * error value. So libc would do something like
1375 * char *getcwd(char * buf, size_t size)
1379 * retval = sys_getcwd(buf, size);
1386 asmlinkage
long sys_getcwd(char __user
*buf
, unsigned long size
)
1389 struct vfsmount
*pwdmnt
, *rootmnt
;
1390 struct dentry
*pwd
, *root
;
1391 char *page
= (char *) __get_free_page(GFP_USER
);
1396 read_lock(¤t
->fs
->lock
);
1397 pwdmnt
= mntget(current
->fs
->pwdmnt
);
1398 pwd
= dget(current
->fs
->pwd
);
1399 rootmnt
= mntget(current
->fs
->rootmnt
);
1400 root
= dget(current
->fs
->root
);
1401 read_unlock(¤t
->fs
->lock
);
1404 /* Has the current directory has been unlinked? */
1405 spin_lock(&dcache_lock
);
1406 if (pwd
->d_parent
== pwd
|| !d_unhashed(pwd
)) {
1410 cwd
= __d_path(pwd
, pwdmnt
, root
, rootmnt
, page
, PAGE_SIZE
);
1411 spin_unlock(&dcache_lock
);
1413 error
= PTR_ERR(cwd
);
1418 len
= PAGE_SIZE
+ page
- cwd
;
1421 if (copy_to_user(buf
, cwd
, len
))
1425 spin_unlock(&dcache_lock
);
1432 free_page((unsigned long) page
);
1437 * Test whether new_dentry is a subdirectory of old_dentry.
1439 * Trivially implemented using the dcache structure
1443 * is_subdir - is new dentry a subdirectory of old_dentry
1444 * @new_dentry: new dentry
1445 * @old_dentry: old dentry
1447 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1448 * Returns 0 otherwise.
1451 int is_subdir(struct dentry
* new_dentry
, struct dentry
* old_dentry
)
1458 seq
= read_seqbegin(&rename_lock
);
1460 if (new_dentry
!= old_dentry
) {
1461 struct dentry
* parent
= new_dentry
->d_parent
;
1462 if (parent
== new_dentry
)
1464 new_dentry
= parent
;
1470 } while (read_seqretry(&rename_lock
, seq
));
1475 void d_genocide(struct dentry
*root
)
1477 struct dentry
*this_parent
= root
;
1478 struct list_head
*next
;
1480 spin_lock(&dcache_lock
);
1482 next
= this_parent
->d_subdirs
.next
;
1484 while (next
!= &this_parent
->d_subdirs
) {
1485 struct list_head
*tmp
= next
;
1486 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_child
);
1488 if (d_unhashed(dentry
)||!dentry
->d_inode
)
1490 if (!list_empty(&dentry
->d_subdirs
)) {
1491 this_parent
= dentry
;
1494 atomic_dec(&dentry
->d_count
);
1496 if (this_parent
!= root
) {
1497 next
= this_parent
->d_child
.next
;
1498 atomic_dec(&this_parent
->d_count
);
1499 this_parent
= this_parent
->d_parent
;
1502 spin_unlock(&dcache_lock
);
1506 * find_inode_number - check for dentry with name
1507 * @dir: directory to check
1508 * @name: Name to find.
1510 * Check whether a dentry already exists for the given name,
1511 * and return the inode number if it has an inode. Otherwise
1514 * This routine is used to post-process directory listings for
1515 * filesystems using synthetic inode numbers, and is necessary
1516 * to keep getcwd() working.
1519 ino_t
find_inode_number(struct dentry
*dir
, struct qstr
*name
)
1521 struct dentry
* dentry
;
1525 * Check for a fs-specific hash function. Note that we must
1526 * calculate the standard hash first, as the d_op->d_hash()
1527 * routine may choose to leave the hash value unchanged.
1529 name
->hash
= full_name_hash(name
->name
, name
->len
);
1530 if (dir
->d_op
&& dir
->d_op
->d_hash
)
1532 if (dir
->d_op
->d_hash(dir
, name
) != 0)
1536 dentry
= d_lookup(dir
, name
);
1539 if (dentry
->d_inode
)
1540 ino
= dentry
->d_inode
->i_ino
;
1547 static void __init
dcache_init(unsigned long mempages
)
1549 struct hlist_head
*d
;
1550 unsigned long order
;
1551 unsigned int nr_hash
;
1555 * A constructor could be added for stable state like the lists,
1556 * but it is probably not worth it because of the cache nature
1558 * If fragmentation is too bad then the SLAB_HWCACHE_ALIGN
1559 * flag could be removed here, to hint to the allocator that
1560 * it should not try to get multiple page regions.
1562 dentry_cache
= kmem_cache_create("dentry_cache",
1563 sizeof(struct dentry
),
1565 SLAB_HWCACHE_ALIGN
|SLAB_RECLAIM_ACCOUNT
,
1568 panic("Cannot create dentry cache");
1570 set_shrinker(DEFAULT_SEEKS
, shrink_dcache_memory
);
1573 mempages
>>= (13 - PAGE_SHIFT
);
1575 mempages
*= sizeof(struct hlist_head
);
1576 for (order
= 0; ((1UL << order
) << PAGE_SHIFT
) < mempages
; order
++)
1582 nr_hash
= (1UL << order
) * PAGE_SIZE
/
1583 sizeof(struct hlist_head
);
1584 d_hash_mask
= (nr_hash
- 1);
1588 while ((tmp
>>= 1UL) != 0UL)
1591 dentry_hashtable
= (struct hlist_head
*)
1592 __get_free_pages(GFP_ATOMIC
, order
);
1593 } while (dentry_hashtable
== NULL
&& --order
>= 0);
1595 printk(KERN_INFO
"Dentry cache hash table entries: %d (order: %ld, %ld bytes)\n",
1596 nr_hash
, order
, (PAGE_SIZE
<< order
));
1598 if (!dentry_hashtable
)
1599 panic("Failed to allocate dcache hash table\n");
1601 d
= dentry_hashtable
;
1610 /* SLAB cache for __getname() consumers */
1611 kmem_cache_t
*names_cachep
;
1613 /* SLAB cache for file structures */
1614 kmem_cache_t
*filp_cachep
;
1616 EXPORT_SYMBOL(d_genocide
);
1618 extern void bdev_cache_init(void);
1619 extern void chrdev_init(void);
1621 void __init
vfs_caches_init(unsigned long mempages
)
1623 names_cachep
= kmem_cache_create("names_cache",
1625 SLAB_HWCACHE_ALIGN
, NULL
, NULL
);
1627 panic("Cannot create names SLAB cache");
1629 filp_cachep
= kmem_cache_create("filp",
1630 sizeof(struct file
), 0,
1631 SLAB_HWCACHE_ALIGN
, filp_ctor
, filp_dtor
);
1633 panic("Cannot create filp SLAB cache");
1635 dcache_init(mempages
);
1636 inode_init(mempages
);
1637 files_init(mempages
);