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/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>
35 /* #define DCACHE_DEBUG 1 */
37 int sysctl_vfs_cache_pressure
= 100;
39 spinlock_t dcache_lock __cacheline_aligned_in_smp
= SPIN_LOCK_UNLOCKED
;
40 seqlock_t rename_lock __cacheline_aligned_in_smp
= SEQLOCK_UNLOCKED
;
42 EXPORT_SYMBOL(dcache_lock
);
44 static kmem_cache_t
*dentry_cache
;
46 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
49 * This is the single most critical data structure when it comes
50 * to the dcache: the hashtable for lookups. Somebody should try
51 * to make this good - I've just made it work.
53 * This hash-function tries to avoid losing too many bits of hash
54 * information, yet avoid using a prime hash-size or similar.
56 #define D_HASHBITS d_hash_shift
57 #define D_HASHMASK d_hash_mask
59 static unsigned int d_hash_mask
;
60 static unsigned int d_hash_shift
;
61 static struct hlist_head
*dentry_hashtable
;
62 static LIST_HEAD(dentry_unused
);
64 /* Statistics gathering. */
65 struct dentry_stat_t dentry_stat
= {
69 static void d_callback(struct rcu_head
*head
)
71 struct dentry
* dentry
= container_of(head
, struct dentry
, d_rcu
);
73 if (dname_external(dentry
))
74 kfree(dentry
->d_name
.name
);
75 kmem_cache_free(dentry_cache
, dentry
);
79 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
82 static void d_free(struct dentry
*dentry
)
84 if (dentry
->d_op
&& dentry
->d_op
->d_release
)
85 dentry
->d_op
->d_release(dentry
);
86 call_rcu(&dentry
->d_rcu
, d_callback
);
90 * Release the dentry's inode, using the filesystem
91 * d_iput() operation if defined.
92 * Called with dcache_lock and per dentry lock held, drops both.
94 static inline void dentry_iput(struct dentry
* dentry
)
96 struct inode
*inode
= dentry
->d_inode
;
98 dentry
->d_inode
= NULL
;
99 list_del_init(&dentry
->d_alias
);
100 spin_unlock(&dentry
->d_lock
);
101 spin_unlock(&dcache_lock
);
102 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
103 dentry
->d_op
->d_iput(dentry
, inode
);
107 spin_unlock(&dentry
->d_lock
);
108 spin_unlock(&dcache_lock
);
115 * This is complicated by the fact that we do not want to put
116 * dentries that are no longer on any hash chain on the unused
117 * list: we'd much rather just get rid of them immediately.
119 * However, that implies that we have to traverse the dentry
120 * tree upwards to the parents which might _also_ now be
121 * scheduled for deletion (it may have been only waiting for
122 * its last child to go away).
124 * This tail recursion is done by hand as we don't want to depend
125 * on the compiler to always get this right (gcc generally doesn't).
126 * Real recursion would eat up our stack space.
130 * dput - release a dentry
131 * @dentry: dentry to release
133 * Release a dentry. This will drop the usage count and if appropriate
134 * call the dentry unlink method as well as removing it from the queues and
135 * releasing its resources. If the parent dentries were scheduled for release
136 * they too may now get deleted.
138 * no dcache lock, please.
141 void dput(struct dentry
*dentry
)
147 if (atomic_read(&dentry
->d_count
) == 1)
149 if (!atomic_dec_and_lock(&dentry
->d_count
, &dcache_lock
))
152 spin_lock(&dentry
->d_lock
);
153 if (atomic_read(&dentry
->d_count
)) {
154 spin_unlock(&dentry
->d_lock
);
155 spin_unlock(&dcache_lock
);
160 * AV: ->d_delete() is _NOT_ allowed to block now.
162 if (dentry
->d_op
&& dentry
->d_op
->d_delete
) {
163 if (dentry
->d_op
->d_delete(dentry
))
166 /* Unreachable? Get rid of it */
167 if (d_unhashed(dentry
))
169 if (list_empty(&dentry
->d_lru
)) {
170 dentry
->d_flags
|= DCACHE_REFERENCED
;
171 list_add(&dentry
->d_lru
, &dentry_unused
);
172 dentry_stat
.nr_unused
++;
174 spin_unlock(&dentry
->d_lock
);
175 spin_unlock(&dcache_lock
);
182 struct dentry
*parent
;
184 /* If dentry was on d_lru list
185 * delete it from there
187 if (!list_empty(&dentry
->d_lru
)) {
188 list_del(&dentry
->d_lru
);
189 dentry_stat
.nr_unused
--;
191 list_del(&dentry
->d_child
);
192 dentry_stat
.nr_dentry
--; /* For d_free, below */
193 /*drops the locks, at that point nobody can reach this dentry */
195 parent
= dentry
->d_parent
;
197 if (dentry
== parent
)
205 * d_invalidate - invalidate a dentry
206 * @dentry: dentry to invalidate
208 * Try to invalidate the dentry if it turns out to be
209 * possible. If there are other dentries that can be
210 * reached through this one we can't delete it and we
211 * return -EBUSY. On success we return 0.
216 int d_invalidate(struct dentry
* dentry
)
219 * If it's already been dropped, return OK.
221 spin_lock(&dcache_lock
);
222 if (d_unhashed(dentry
)) {
223 spin_unlock(&dcache_lock
);
227 * Check whether to do a partial shrink_dcache
228 * to get rid of unused child entries.
230 if (!list_empty(&dentry
->d_subdirs
)) {
231 spin_unlock(&dcache_lock
);
232 shrink_dcache_parent(dentry
);
233 spin_lock(&dcache_lock
);
237 * Somebody else still using it?
239 * If it's a directory, we can't drop it
240 * for fear of somebody re-populating it
241 * with children (even though dropping it
242 * would make it unreachable from the root,
243 * we might still populate it if it was a
244 * working directory or similar).
246 spin_lock(&dentry
->d_lock
);
247 if (atomic_read(&dentry
->d_count
) > 1) {
248 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
)) {
249 spin_unlock(&dentry
->d_lock
);
250 spin_unlock(&dcache_lock
);
256 spin_unlock(&dentry
->d_lock
);
257 spin_unlock(&dcache_lock
);
261 /* This should be called _only_ with dcache_lock held */
263 static inline struct dentry
* __dget_locked(struct dentry
*dentry
)
265 atomic_inc(&dentry
->d_count
);
266 if (!list_empty(&dentry
->d_lru
)) {
267 dentry_stat
.nr_unused
--;
268 list_del_init(&dentry
->d_lru
);
273 struct dentry
* dget_locked(struct dentry
*dentry
)
275 return __dget_locked(dentry
);
279 * d_find_alias - grab a hashed alias of inode
280 * @inode: inode in question
282 * If inode has a hashed alias - acquire the reference to alias and
283 * return it. Otherwise return NULL. Notice that if inode is a directory
284 * there can be only one alias and it can be unhashed only if it has
287 * If the inode has a DCACHE_DISCONNECTED alias, then prefer
288 * any other hashed alias over that one.
291 static struct dentry
* __d_find_alias(struct inode
*inode
, int want_discon
)
293 struct list_head
*head
, *next
, *tmp
;
294 struct dentry
*alias
, *discon_alias
=NULL
;
296 head
= &inode
->i_dentry
;
297 next
= inode
->i_dentry
.next
;
298 while (next
!= head
) {
302 alias
= list_entry(tmp
, struct dentry
, d_alias
);
303 if (!d_unhashed(alias
)) {
304 if (alias
->d_flags
& DCACHE_DISCONNECTED
)
305 discon_alias
= alias
;
306 else if (!want_discon
) {
307 __dget_locked(alias
);
313 __dget_locked(discon_alias
);
317 struct dentry
* d_find_alias(struct inode
*inode
)
320 spin_lock(&dcache_lock
);
321 de
= __d_find_alias(inode
, 0);
322 spin_unlock(&dcache_lock
);
327 * Try to kill dentries associated with this inode.
328 * WARNING: you must own a reference to inode.
330 void d_prune_aliases(struct inode
*inode
)
332 struct list_head
*tmp
, *head
= &inode
->i_dentry
;
334 spin_lock(&dcache_lock
);
336 while ((tmp
= tmp
->next
) != head
) {
337 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_alias
);
338 if (!atomic_read(&dentry
->d_count
)) {
339 __dget_locked(dentry
);
341 spin_unlock(&dcache_lock
);
346 spin_unlock(&dcache_lock
);
350 * Throw away a dentry - free the inode, dput the parent.
351 * This requires that the LRU list has already been
353 * Called with dcache_lock, drops it and then regains.
355 static inline void prune_one_dentry(struct dentry
* dentry
)
357 struct dentry
* parent
;
360 list_del(&dentry
->d_child
);
361 dentry_stat
.nr_dentry
--; /* For d_free, below */
363 parent
= dentry
->d_parent
;
365 if (parent
!= dentry
)
367 spin_lock(&dcache_lock
);
371 * prune_dcache - shrink the dcache
372 * @count: number of entries to try and free
374 * Shrink the dcache. This is done when we need
375 * more memory, or simply when we need to unmount
376 * something (at which point we need to unuse
379 * This function may fail to free any resources if
380 * all the dentries are in use.
383 static void prune_dcache(int count
)
385 spin_lock(&dcache_lock
);
386 for (; count
; count
--) {
387 struct dentry
*dentry
;
388 struct list_head
*tmp
;
390 tmp
= dentry_unused
.prev
;
391 if (tmp
== &dentry_unused
)
394 prefetch(dentry_unused
.prev
);
395 dentry_stat
.nr_unused
--;
396 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
398 spin_lock(&dentry
->d_lock
);
400 * We found an inuse dentry which was not removed from
401 * dentry_unused because of laziness during lookup. Do not free
402 * it - just keep it off the dentry_unused list.
404 if (atomic_read(&dentry
->d_count
)) {
405 spin_unlock(&dentry
->d_lock
);
408 /* If the dentry was recently referenced, don't free it. */
409 if (dentry
->d_flags
& DCACHE_REFERENCED
) {
410 dentry
->d_flags
&= ~DCACHE_REFERENCED
;
411 list_add(&dentry
->d_lru
, &dentry_unused
);
412 dentry_stat
.nr_unused
++;
413 spin_unlock(&dentry
->d_lock
);
416 prune_one_dentry(dentry
);
418 spin_unlock(&dcache_lock
);
422 * Shrink the dcache for the specified super block.
423 * This allows us to unmount a device without disturbing
424 * the dcache for the other devices.
426 * This implementation makes just two traversals of the
427 * unused list. On the first pass we move the selected
428 * dentries to the most recent end, and on the second
429 * pass we free them. The second pass must restart after
430 * each dput(), but since the target dentries are all at
431 * the end, it's really just a single traversal.
435 * shrink_dcache_sb - shrink dcache for a superblock
438 * Shrink the dcache for the specified super block. This
439 * is used to free the dcache before unmounting a file
443 void shrink_dcache_sb(struct super_block
* sb
)
445 struct list_head
*tmp
, *next
;
446 struct dentry
*dentry
;
449 * Pass one ... move the dentries for the specified
450 * superblock to the most recent end of the unused list.
452 spin_lock(&dcache_lock
);
453 next
= dentry_unused
.next
;
454 while (next
!= &dentry_unused
) {
457 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
458 if (dentry
->d_sb
!= sb
)
461 list_add(tmp
, &dentry_unused
);
465 * Pass two ... free the dentries for this superblock.
468 next
= dentry_unused
.next
;
469 while (next
!= &dentry_unused
) {
472 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
473 if (dentry
->d_sb
!= sb
)
475 dentry_stat
.nr_unused
--;
477 spin_lock(&dentry
->d_lock
);
478 if (atomic_read(&dentry
->d_count
)) {
479 spin_unlock(&dentry
->d_lock
);
482 prune_one_dentry(dentry
);
485 spin_unlock(&dcache_lock
);
489 * Search for at least 1 mount point in the dentry's subdirs.
490 * We descend to the next level whenever the d_subdirs
491 * list is non-empty and continue searching.
495 * have_submounts - check for mounts over a dentry
496 * @parent: dentry to check.
498 * Return true if the parent or its subdirectories contain
502 int have_submounts(struct dentry
*parent
)
504 struct dentry
*this_parent
= parent
;
505 struct list_head
*next
;
507 spin_lock(&dcache_lock
);
508 if (d_mountpoint(parent
))
511 next
= this_parent
->d_subdirs
.next
;
513 while (next
!= &this_parent
->d_subdirs
) {
514 struct list_head
*tmp
= next
;
515 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_child
);
517 /* Have we found a mount point ? */
518 if (d_mountpoint(dentry
))
520 if (!list_empty(&dentry
->d_subdirs
)) {
521 this_parent
= dentry
;
526 * All done at this level ... ascend and resume the search.
528 if (this_parent
!= parent
) {
529 next
= this_parent
->d_child
.next
;
530 this_parent
= this_parent
->d_parent
;
533 spin_unlock(&dcache_lock
);
534 return 0; /* No mount points found in tree */
536 spin_unlock(&dcache_lock
);
541 * Search the dentry child list for the specified parent,
542 * and move any unused dentries to the end of the unused
543 * list for prune_dcache(). We descend to the next level
544 * whenever the d_subdirs list is non-empty and continue
547 static int select_parent(struct dentry
* parent
)
549 struct dentry
*this_parent
= parent
;
550 struct list_head
*next
;
553 spin_lock(&dcache_lock
);
555 next
= this_parent
->d_subdirs
.next
;
557 while (next
!= &this_parent
->d_subdirs
) {
558 struct list_head
*tmp
= next
;
559 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_child
);
562 if (!list_empty(&dentry
->d_lru
)) {
563 dentry_stat
.nr_unused
--;
564 list_del_init(&dentry
->d_lru
);
567 * move only zero ref count dentries to the end
568 * of the unused list for prune_dcache
570 if (!atomic_read(&dentry
->d_count
)) {
571 list_add(&dentry
->d_lru
, dentry_unused
.prev
);
572 dentry_stat
.nr_unused
++;
576 * Descend a level if the d_subdirs list is non-empty.
578 if (!list_empty(&dentry
->d_subdirs
)) {
579 this_parent
= dentry
;
581 printk(KERN_DEBUG
"select_parent: descending to %s/%s, found=%d\n",
582 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, found
);
588 * All done at this level ... ascend and resume the search.
590 if (this_parent
!= parent
) {
591 next
= this_parent
->d_child
.next
;
592 this_parent
= this_parent
->d_parent
;
594 printk(KERN_DEBUG
"select_parent: ascending to %s/%s, found=%d\n",
595 this_parent
->d_parent
->d_name
.name
, this_parent
->d_name
.name
, found
);
599 spin_unlock(&dcache_lock
);
604 * shrink_dcache_parent - prune dcache
605 * @parent: parent of entries to prune
607 * Prune the dcache to remove unused children of the parent dentry.
610 void shrink_dcache_parent(struct dentry
* parent
)
614 while ((found
= select_parent(parent
)) != 0)
619 * shrink_dcache_anon - further prune the cache
620 * @head: head of d_hash list of dentries to prune
622 * Prune the dentries that are anonymous
624 * parsing d_hash list does not hlist_for_each_rcu() as it
625 * done under dcache_lock.
628 void shrink_dcache_anon(struct hlist_head
*head
)
630 struct hlist_node
*lp
;
634 spin_lock(&dcache_lock
);
635 hlist_for_each(lp
, head
) {
636 struct dentry
*this = hlist_entry(lp
, struct dentry
, d_hash
);
637 if (!list_empty(&this->d_lru
)) {
638 dentry_stat
.nr_unused
--;
639 list_del_init(&this->d_lru
);
643 * move only zero ref count dentries to the end
644 * of the unused list for prune_dcache
646 if (!atomic_read(&this->d_count
)) {
647 list_add_tail(&this->d_lru
, &dentry_unused
);
648 dentry_stat
.nr_unused
++;
652 spin_unlock(&dcache_lock
);
658 * Scan `nr' dentries and return the number which remain.
660 * We need to avoid reentering the filesystem if the caller is performing a
661 * GFP_NOFS allocation attempt. One example deadlock is:
663 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
664 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
665 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
667 * In this case we return -1 to tell the caller that we baled.
669 static int shrink_dcache_memory(int nr
, unsigned int gfp_mask
)
672 if (!(gfp_mask
& __GFP_FS
))
676 return (dentry_stat
.nr_unused
/ 100) * sysctl_vfs_cache_pressure
;
680 * d_alloc - allocate a dcache entry
681 * @parent: parent of entry to allocate
682 * @name: qstr of the name
684 * Allocates a dentry. It returns %NULL if there is insufficient memory
685 * available. On a success the dentry is returned. The name passed in is
686 * copied and the copy passed in may be reused after this call.
689 struct dentry
*d_alloc(struct dentry
* parent
, const struct qstr
*name
)
691 struct dentry
*dentry
;
694 dentry
= kmem_cache_alloc(dentry_cache
, GFP_KERNEL
);
698 if (name
->len
> DNAME_INLINE_LEN
-1) {
699 dname
= kmalloc(name
->len
+ 1, GFP_KERNEL
);
701 kmem_cache_free(dentry_cache
, dentry
);
705 dname
= dentry
->d_iname
;
707 dentry
->d_name
.name
= dname
;
709 dentry
->d_name
.len
= name
->len
;
710 dentry
->d_name
.hash
= name
->hash
;
711 memcpy(dname
, name
->name
, name
->len
);
712 dname
[name
->len
] = 0;
714 atomic_set(&dentry
->d_count
, 1);
715 dentry
->d_flags
= DCACHE_UNHASHED
;
716 dentry
->d_lock
= SPIN_LOCK_UNLOCKED
;
717 dentry
->d_inode
= NULL
;
718 dentry
->d_parent
= NULL
;
721 dentry
->d_fsdata
= NULL
;
722 dentry
->d_mounted
= 0;
723 dentry
->d_cookie
= NULL
;
724 dentry
->d_bucket
= NULL
;
725 INIT_HLIST_NODE(&dentry
->d_hash
);
726 INIT_LIST_HEAD(&dentry
->d_lru
);
727 INIT_LIST_HEAD(&dentry
->d_subdirs
);
728 INIT_LIST_HEAD(&dentry
->d_alias
);
731 dentry
->d_parent
= dget(parent
);
732 dentry
->d_sb
= parent
->d_sb
;
734 INIT_LIST_HEAD(&dentry
->d_child
);
737 spin_lock(&dcache_lock
);
739 list_add(&dentry
->d_child
, &parent
->d_subdirs
);
740 dentry_stat
.nr_dentry
++;
741 spin_unlock(&dcache_lock
);
747 * d_instantiate - fill in inode information for a dentry
748 * @entry: dentry to complete
749 * @inode: inode to attach to this dentry
751 * Fill in inode information in the entry.
753 * This turns negative dentries into productive full members
756 * NOTE! This assumes that the inode count has been incremented
757 * (or otherwise set) by the caller to indicate that it is now
758 * in use by the dcache.
761 void d_instantiate(struct dentry
*entry
, struct inode
* inode
)
763 if (!list_empty(&entry
->d_alias
)) BUG();
764 spin_lock(&dcache_lock
);
766 list_add(&entry
->d_alias
, &inode
->i_dentry
);
767 entry
->d_inode
= inode
;
768 spin_unlock(&dcache_lock
);
769 security_d_instantiate(entry
, inode
);
773 * d_alloc_root - allocate root dentry
774 * @root_inode: inode to allocate the root for
776 * Allocate a root ("/") dentry for the inode given. The inode is
777 * instantiated and returned. %NULL is returned if there is insufficient
778 * memory or the inode passed is %NULL.
781 struct dentry
* d_alloc_root(struct inode
* root_inode
)
783 struct dentry
*res
= NULL
;
786 static const struct qstr name
= { .name
= "/", .len
= 1 };
788 res
= d_alloc(NULL
, &name
);
790 res
->d_sb
= root_inode
->i_sb
;
792 d_instantiate(res
, root_inode
);
798 static inline struct hlist_head
*d_hash(struct dentry
*parent
,
801 hash
+= ((unsigned long) parent
^ GOLDEN_RATIO_PRIME
) / L1_CACHE_BYTES
;
802 hash
= hash
^ ((hash
^ GOLDEN_RATIO_PRIME
) >> D_HASHBITS
);
803 return dentry_hashtable
+ (hash
& D_HASHMASK
);
807 * d_alloc_anon - allocate an anonymous dentry
808 * @inode: inode to allocate the dentry for
810 * This is similar to d_alloc_root. It is used by filesystems when
811 * creating a dentry for a given inode, often in the process of
812 * mapping a filehandle to a dentry. The returned dentry may be
813 * anonymous, or may have a full name (if the inode was already
814 * in the cache). The file system may need to make further
815 * efforts to connect this dentry into the dcache properly.
817 * When called on a directory inode, we must ensure that
818 * the inode only ever has one dentry. If a dentry is
819 * found, that is returned instead of allocating a new one.
821 * On successful return, the reference to the inode has been transferred
822 * to the dentry. If %NULL is returned (indicating kmalloc failure),
823 * the reference on the inode has not been released.
826 struct dentry
* d_alloc_anon(struct inode
*inode
)
828 static const struct qstr anonstring
= { .name
= "" };
832 if ((res
= d_find_alias(inode
))) {
837 tmp
= d_alloc(NULL
, &anonstring
);
841 tmp
->d_parent
= tmp
; /* make sure dput doesn't croak */
843 spin_lock(&dcache_lock
);
844 res
= __d_find_alias(inode
, 0);
846 /* attach a disconnected dentry */
849 spin_lock(&res
->d_lock
);
850 res
->d_sb
= inode
->i_sb
;
852 res
->d_inode
= inode
;
855 * Set d_bucket to an "impossible" bucket address so
856 * that d_move() doesn't get a false positive
858 res
->d_bucket
= NULL
;
859 res
->d_flags
|= DCACHE_DISCONNECTED
;
860 res
->d_flags
&= ~DCACHE_UNHASHED
;
861 list_add(&res
->d_alias
, &inode
->i_dentry
);
862 hlist_add_head(&res
->d_hash
, &inode
->i_sb
->s_anon
);
863 spin_unlock(&res
->d_lock
);
865 inode
= NULL
; /* don't drop reference */
867 spin_unlock(&dcache_lock
);
878 * d_splice_alias - splice a disconnected dentry into the tree if one exists
879 * @inode: the inode which may have a disconnected dentry
880 * @dentry: a negative dentry which we want to point to the inode.
882 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
883 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
884 * and return it, else simply d_add the inode to the dentry and return NULL.
886 * This is needed in the lookup routine of any filesystem that is exportable
887 * (via knfsd) so that we can build dcache paths to directories effectively.
889 * If a dentry was found and moved, then it is returned. Otherwise NULL
890 * is returned. This matches the expected return value of ->lookup.
893 struct dentry
*d_splice_alias(struct inode
*inode
, struct dentry
*dentry
)
895 struct dentry
*new = NULL
;
898 spin_lock(&dcache_lock
);
899 new = __d_find_alias(inode
, 1);
901 BUG_ON(!(new->d_flags
& DCACHE_DISCONNECTED
));
902 spin_unlock(&dcache_lock
);
903 security_d_instantiate(new, inode
);
908 /* d_instantiate takes dcache_lock, so we do it by hand */
909 list_add(&dentry
->d_alias
, &inode
->i_dentry
);
910 dentry
->d_inode
= inode
;
911 spin_unlock(&dcache_lock
);
912 security_d_instantiate(dentry
, inode
);
916 d_add(dentry
, inode
);
922 * d_lookup - search for a dentry
923 * @parent: parent dentry
924 * @name: qstr of name we wish to find
926 * Searches the children of the parent dentry for the name in question. If
927 * the dentry is found its reference count is incremented and the dentry
928 * is returned. The caller must use d_put to free the entry when it has
929 * finished using it. %NULL is returned on failure.
931 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
932 * Memory barriers are used while updating and doing lockless traversal.
933 * To avoid races with d_move while rename is happening, d_lock is used.
935 * Overflows in memcmp(), while d_move, are avoided by keeping the length
936 * and name pointer in one structure pointed by d_qstr.
938 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
939 * lookup is going on.
941 * dentry_unused list is not updated even if lookup finds the required dentry
942 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
943 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
946 * d_lookup() is protected against the concurrent renames in some unrelated
947 * directory using the seqlockt_t rename_lock.
950 struct dentry
* d_lookup(struct dentry
* parent
, struct qstr
* name
)
952 struct dentry
* dentry
= NULL
;
956 seq
= read_seqbegin(&rename_lock
);
957 dentry
= __d_lookup(parent
, name
);
960 } while (read_seqretry(&rename_lock
, seq
));
964 struct dentry
* __d_lookup(struct dentry
* parent
, struct qstr
* name
)
966 unsigned int len
= name
->len
;
967 unsigned int hash
= name
->hash
;
968 const unsigned char *str
= name
->name
;
969 struct hlist_head
*head
= d_hash(parent
,hash
);
970 struct dentry
*found
= NULL
;
971 struct hlist_node
*node
;
975 hlist_for_each_rcu(node
, head
) {
976 struct dentry
*dentry
;
979 dentry
= hlist_entry(node
, struct dentry
, d_hash
);
983 if (dentry
->d_name
.hash
!= hash
)
985 if (dentry
->d_parent
!= parent
)
988 spin_lock(&dentry
->d_lock
);
991 * If lookup ends up in a different bucket due to concurrent
994 if (unlikely(dentry
->d_bucket
!= head
))
998 * Recheck the dentry after taking the lock - d_move may have
999 * changed things. Don't bother checking the hash because we're
1000 * about to compare the whole name anyway.
1002 if (dentry
->d_parent
!= parent
)
1005 qstr
= rcu_dereference(&dentry
->d_name
);
1006 if (parent
->d_op
&& parent
->d_op
->d_compare
) {
1007 if (parent
->d_op
->d_compare(parent
, qstr
, name
))
1010 if (qstr
->len
!= len
)
1012 if (memcmp(qstr
->name
, str
, len
))
1016 if (!d_unhashed(dentry
)) {
1017 atomic_inc(&dentry
->d_count
);
1021 spin_unlock(&dentry
->d_lock
);
1024 spin_unlock(&dentry
->d_lock
);
1032 * d_validate - verify dentry provided from insecure source
1033 * @dentry: The dentry alleged to be valid child of @dparent
1034 * @dparent: The parent dentry (known to be valid)
1035 * @hash: Hash of the dentry
1036 * @len: Length of the name
1038 * An insecure source has sent us a dentry, here we verify it and dget() it.
1039 * This is used by ncpfs in its readdir implementation.
1040 * Zero is returned in the dentry is invalid.
1043 int d_validate(struct dentry
*dentry
, struct dentry
*dparent
)
1045 struct hlist_head
*base
;
1046 struct hlist_node
*lhp
;
1048 /* Check whether the ptr might be valid at all.. */
1049 if (!kmem_ptr_validate(dentry_cache
, dentry
))
1052 if (dentry
->d_parent
!= dparent
)
1055 spin_lock(&dcache_lock
);
1056 base
= d_hash(dparent
, dentry
->d_name
.hash
);
1057 hlist_for_each(lhp
,base
) {
1058 /* hlist_for_each_rcu() not required for d_hash list
1059 * as it is parsed under dcache_lock
1061 if (dentry
== hlist_entry(lhp
, struct dentry
, d_hash
)) {
1062 __dget_locked(dentry
);
1063 spin_unlock(&dcache_lock
);
1067 spin_unlock(&dcache_lock
);
1073 * When a file is deleted, we have two options:
1074 * - turn this dentry into a negative dentry
1075 * - unhash this dentry and free it.
1077 * Usually, we want to just turn this into
1078 * a negative dentry, but if anybody else is
1079 * currently using the dentry or the inode
1080 * we can't do that and we fall back on removing
1081 * it from the hash queues and waiting for
1082 * it to be deleted later when it has no users
1086 * d_delete - delete a dentry
1087 * @dentry: The dentry to delete
1089 * Turn the dentry into a negative dentry if possible, otherwise
1090 * remove it from the hash queues so it can be deleted later
1093 void d_delete(struct dentry
* dentry
)
1096 * Are we the only user?
1098 spin_lock(&dcache_lock
);
1099 spin_lock(&dentry
->d_lock
);
1100 if (atomic_read(&dentry
->d_count
) == 1) {
1101 dentry_iput(dentry
);
1105 if (!d_unhashed(dentry
))
1108 spin_unlock(&dentry
->d_lock
);
1109 spin_unlock(&dcache_lock
);
1113 * d_rehash - add an entry back to the hash
1114 * @entry: dentry to add to the hash
1116 * Adds a dentry to the hash according to its name.
1119 void d_rehash(struct dentry
* entry
)
1121 struct hlist_head
*list
= d_hash(entry
->d_parent
, entry
->d_name
.hash
);
1123 spin_lock(&dcache_lock
);
1124 spin_lock(&entry
->d_lock
);
1125 entry
->d_flags
&= ~DCACHE_UNHASHED
;
1126 spin_unlock(&entry
->d_lock
);
1127 entry
->d_bucket
= list
;
1128 hlist_add_head_rcu(&entry
->d_hash
, list
);
1129 spin_unlock(&dcache_lock
);
1132 #define do_switch(x,y) do { \
1133 __typeof__ (x) __tmp = x; \
1134 x = y; y = __tmp; } while (0)
1137 * When switching names, the actual string doesn't strictly have to
1138 * be preserved in the target - because we're dropping the target
1139 * anyway. As such, we can just do a simple memcpy() to copy over
1140 * the new name before we switch.
1142 * Note that we have to be a lot more careful about getting the hash
1143 * switched - we have to switch the hash value properly even if it
1144 * then no longer matches the actual (corrupted) string of the target.
1145 * The hash value has to match the hash queue that the dentry is on..
1147 static void switch_names(struct dentry
*dentry
, struct dentry
*target
)
1149 if (dname_external(target
)) {
1150 if (dname_external(dentry
)) {
1152 * Both external: swap the pointers
1154 do_switch(target
->d_name
.name
, dentry
->d_name
.name
);
1157 * dentry:internal, target:external. Steal target's
1158 * storage and make target internal.
1160 dentry
->d_name
.name
= target
->d_name
.name
;
1161 target
->d_name
.name
= target
->d_iname
;
1164 if (dname_external(dentry
)) {
1166 * dentry:external, target:internal. Give dentry's
1167 * storage to target and make dentry internal
1169 memcpy(dentry
->d_iname
, target
->d_name
.name
,
1170 target
->d_name
.len
+ 1);
1171 target
->d_name
.name
= dentry
->d_name
.name
;
1172 dentry
->d_name
.name
= dentry
->d_iname
;
1175 * Both are internal. Just copy target to dentry
1177 memcpy(dentry
->d_iname
, target
->d_name
.name
,
1178 target
->d_name
.len
+ 1);
1184 * We cannibalize "target" when moving dentry on top of it,
1185 * because it's going to be thrown away anyway. We could be more
1186 * polite about it, though.
1188 * This forceful removal will result in ugly /proc output if
1189 * somebody holds a file open that got deleted due to a rename.
1190 * We could be nicer about the deleted file, and let it show
1191 * up under the name it got deleted rather than the name that
1196 * d_move - move a dentry
1197 * @dentry: entry to move
1198 * @target: new dentry
1200 * Update the dcache to reflect the move of a file name. Negative
1201 * dcache entries should not be moved in this way.
1204 void d_move(struct dentry
* dentry
, struct dentry
* target
)
1206 if (!dentry
->d_inode
)
1207 printk(KERN_WARNING
"VFS: moving negative dcache entry\n");
1209 spin_lock(&dcache_lock
);
1210 write_seqlock(&rename_lock
);
1212 * XXXX: do we really need to take target->d_lock?
1214 if (target
< dentry
) {
1215 spin_lock(&target
->d_lock
);
1216 spin_lock(&dentry
->d_lock
);
1218 spin_lock(&dentry
->d_lock
);
1219 spin_lock(&target
->d_lock
);
1222 /* Move the dentry to the target hash queue, if on different bucket */
1223 if (dentry
->d_flags
& DCACHE_UNHASHED
)
1224 goto already_unhashed
;
1225 if (dentry
->d_bucket
!= target
->d_bucket
) {
1226 hlist_del_rcu(&dentry
->d_hash
);
1228 dentry
->d_bucket
= target
->d_bucket
;
1229 hlist_add_head_rcu(&dentry
->d_hash
, target
->d_bucket
);
1230 dentry
->d_flags
&= ~DCACHE_UNHASHED
;
1233 /* Unhash the target: dput() will then get rid of it */
1236 list_del(&dentry
->d_child
);
1237 list_del(&target
->d_child
);
1239 /* Switch the names.. */
1240 switch_names(dentry
, target
);
1242 do_switch(dentry
->d_name
.len
, target
->d_name
.len
);
1243 do_switch(dentry
->d_name
.hash
, target
->d_name
.hash
);
1245 /* ... and switch the parents */
1246 if (IS_ROOT(dentry
)) {
1247 dentry
->d_parent
= target
->d_parent
;
1248 target
->d_parent
= target
;
1249 INIT_LIST_HEAD(&target
->d_child
);
1251 do_switch(dentry
->d_parent
, target
->d_parent
);
1253 /* And add them back to the (new) parent lists */
1254 list_add(&target
->d_child
, &target
->d_parent
->d_subdirs
);
1257 list_add(&dentry
->d_child
, &dentry
->d_parent
->d_subdirs
);
1258 spin_unlock(&target
->d_lock
);
1259 spin_unlock(&dentry
->d_lock
);
1260 write_sequnlock(&rename_lock
);
1261 spin_unlock(&dcache_lock
);
1265 * d_path - return the path of a dentry
1266 * @dentry: dentry to report
1267 * @vfsmnt: vfsmnt to which the dentry belongs
1268 * @root: root dentry
1269 * @rootmnt: vfsmnt to which the root dentry belongs
1270 * @buffer: buffer to return value in
1271 * @buflen: buffer length
1273 * Convert a dentry into an ASCII path name. If the entry has been deleted
1274 * the string " (deleted)" is appended. Note that this is ambiguous.
1276 * Returns the buffer or an error code if the path was too long.
1278 * "buflen" should be positive. Caller holds the dcache_lock.
1280 static char * __d_path( struct dentry
*dentry
, struct vfsmount
*vfsmnt
,
1281 struct dentry
*root
, struct vfsmount
*rootmnt
,
1282 char *buffer
, int buflen
)
1284 char * end
= buffer
+buflen
;
1290 if (!IS_ROOT(dentry
) && d_unhashed(dentry
)) {
1295 memcpy(end
, " (deleted)", 10);
1305 struct dentry
* parent
;
1307 if (dentry
== root
&& vfsmnt
== rootmnt
)
1309 if (dentry
== vfsmnt
->mnt_root
|| IS_ROOT(dentry
)) {
1311 spin_lock(&vfsmount_lock
);
1312 if (vfsmnt
->mnt_parent
== vfsmnt
) {
1313 spin_unlock(&vfsmount_lock
);
1316 dentry
= vfsmnt
->mnt_mountpoint
;
1317 vfsmnt
= vfsmnt
->mnt_parent
;
1318 spin_unlock(&vfsmount_lock
);
1321 parent
= dentry
->d_parent
;
1323 namelen
= dentry
->d_name
.len
;
1324 buflen
-= namelen
+ 1;
1328 memcpy(end
, dentry
->d_name
.name
, namelen
);
1337 namelen
= dentry
->d_name
.len
;
1341 retval
-= namelen
-1; /* hit the slash */
1342 memcpy(retval
, dentry
->d_name
.name
, namelen
);
1345 return ERR_PTR(-ENAMETOOLONG
);
1348 /* write full pathname into buffer and return start of pathname */
1349 char * d_path(struct dentry
*dentry
, struct vfsmount
*vfsmnt
,
1350 char *buf
, int buflen
)
1353 struct vfsmount
*rootmnt
;
1354 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.
1449 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1452 int is_subdir(struct dentry
* new_dentry
, struct dentry
* old_dentry
)
1455 struct dentry
* saved
= new_dentry
;
1459 /* need rcu_readlock to protect against the d_parent trashing due to
1464 /* for restarting inner loop in case of seq retry */
1466 seq
= read_seqbegin(&rename_lock
);
1468 if (new_dentry
!= old_dentry
) {
1469 struct dentry
* parent
= new_dentry
->d_parent
;
1470 if (parent
== new_dentry
)
1472 new_dentry
= parent
;
1478 } while (read_seqretry(&rename_lock
, seq
));
1484 void d_genocide(struct dentry
*root
)
1486 struct dentry
*this_parent
= root
;
1487 struct list_head
*next
;
1489 spin_lock(&dcache_lock
);
1491 next
= this_parent
->d_subdirs
.next
;
1493 while (next
!= &this_parent
->d_subdirs
) {
1494 struct list_head
*tmp
= next
;
1495 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_child
);
1497 if (d_unhashed(dentry
)||!dentry
->d_inode
)
1499 if (!list_empty(&dentry
->d_subdirs
)) {
1500 this_parent
= dentry
;
1503 atomic_dec(&dentry
->d_count
);
1505 if (this_parent
!= root
) {
1506 next
= this_parent
->d_child
.next
;
1507 atomic_dec(&this_parent
->d_count
);
1508 this_parent
= this_parent
->d_parent
;
1511 spin_unlock(&dcache_lock
);
1515 * find_inode_number - check for dentry with name
1516 * @dir: directory to check
1517 * @name: Name to find.
1519 * Check whether a dentry already exists for the given name,
1520 * and return the inode number if it has an inode. Otherwise
1523 * This routine is used to post-process directory listings for
1524 * filesystems using synthetic inode numbers, and is necessary
1525 * to keep getcwd() working.
1528 ino_t
find_inode_number(struct dentry
*dir
, struct qstr
*name
)
1530 struct dentry
* dentry
;
1534 * Check for a fs-specific hash function. Note that we must
1535 * calculate the standard hash first, as the d_op->d_hash()
1536 * routine may choose to leave the hash value unchanged.
1538 name
->hash
= full_name_hash(name
->name
, name
->len
);
1539 if (dir
->d_op
&& dir
->d_op
->d_hash
)
1541 if (dir
->d_op
->d_hash(dir
, name
) != 0)
1545 dentry
= d_lookup(dir
, name
);
1548 if (dentry
->d_inode
)
1549 ino
= dentry
->d_inode
->i_ino
;
1556 static __initdata
unsigned long dhash_entries
;
1557 static int __init
set_dhash_entries(char *str
)
1561 dhash_entries
= simple_strtoul(str
, &str
, 0);
1564 __setup("dhash_entries=", set_dhash_entries
);
1566 static void __init
dcache_init_early(void)
1571 alloc_large_system_hash("Dentry cache",
1572 sizeof(struct hlist_head
),
1579 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
1580 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
1583 static void __init
dcache_init(unsigned long mempages
)
1586 * A constructor could be added for stable state like the lists,
1587 * but it is probably not worth it because of the cache nature
1590 dentry_cache
= kmem_cache_create("dentry_cache",
1591 sizeof(struct dentry
),
1593 SLAB_RECLAIM_ACCOUNT
|SLAB_PANIC
,
1596 set_shrinker(DEFAULT_SEEKS
, shrink_dcache_memory
);
1599 /* SLAB cache for __getname() consumers */
1600 kmem_cache_t
*names_cachep
;
1602 /* SLAB cache for file structures */
1603 kmem_cache_t
*filp_cachep
;
1605 EXPORT_SYMBOL(d_genocide
);
1607 extern void bdev_cache_init(void);
1608 extern void chrdev_init(void);
1610 void __init
vfs_caches_init_early(void)
1612 dcache_init_early();
1616 void __init
vfs_caches_init(unsigned long mempages
)
1618 unsigned long reserve
;
1620 /* Base hash sizes on available memory, with a reserve equal to
1621 150% of current kernel size */
1623 reserve
= min((mempages
- nr_free_pages()) * 3/2, mempages
- 1);
1624 mempages
-= reserve
;
1626 names_cachep
= kmem_cache_create("names_cache", PATH_MAX
, 0,
1627 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
, NULL
);
1629 filp_cachep
= kmem_cache_create("filp", sizeof(struct file
), 0,
1630 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, filp_ctor
, filp_dtor
);
1632 dcache_init(mempages
);
1633 inode_init(mempages
);
1634 files_init(mempages
);
1640 EXPORT_SYMBOL(d_alloc
);
1641 EXPORT_SYMBOL(d_alloc_anon
);
1642 EXPORT_SYMBOL(d_alloc_root
);
1643 EXPORT_SYMBOL(d_delete
);
1644 EXPORT_SYMBOL(d_find_alias
);
1645 EXPORT_SYMBOL(d_instantiate
);
1646 EXPORT_SYMBOL(d_invalidate
);
1647 EXPORT_SYMBOL(d_lookup
);
1648 EXPORT_SYMBOL(d_move
);
1649 EXPORT_SYMBOL(d_path
);
1650 EXPORT_SYMBOL(d_prune_aliases
);
1651 EXPORT_SYMBOL(d_rehash
);
1652 EXPORT_SYMBOL(d_splice_alias
);
1653 EXPORT_SYMBOL(d_validate
);
1654 EXPORT_SYMBOL(dget_locked
);
1655 EXPORT_SYMBOL(dput
);
1656 EXPORT_SYMBOL(find_inode_number
);
1657 EXPORT_SYMBOL(have_submounts
);
1658 EXPORT_SYMBOL(is_subdir
);
1659 EXPORT_SYMBOL(names_cachep
);
1660 EXPORT_SYMBOL(shrink_dcache_anon
);
1661 EXPORT_SYMBOL(shrink_dcache_parent
);
1662 EXPORT_SYMBOL(shrink_dcache_sb
);