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>
34 #include <linux/fs_struct.h>
35 #include <linux/hardirq.h>
40 * dcache_hash_lock protects:
41 * - the dcache hash table, s_anon lists
42 * dcache_lru_lock protects:
43 * - the dcache lru lists and counters
56 * if (dentry1 < dentry2)
60 int sysctl_vfs_cache_pressure __read_mostly
= 100;
61 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure
);
63 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dcache_hash_lock
);
64 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dcache_lru_lock
);
65 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dcache_lock
);
66 __cacheline_aligned_in_smp
DEFINE_SEQLOCK(rename_lock
);
68 EXPORT_SYMBOL(dcache_lock
);
70 static struct kmem_cache
*dentry_cache __read_mostly
;
72 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
75 * This is the single most critical data structure when it comes
76 * to the dcache: the hashtable for lookups. Somebody should try
77 * to make this good - I've just made it work.
79 * This hash-function tries to avoid losing too many bits of hash
80 * information, yet avoid using a prime hash-size or similar.
82 #define D_HASHBITS d_hash_shift
83 #define D_HASHMASK d_hash_mask
85 static unsigned int d_hash_mask __read_mostly
;
86 static unsigned int d_hash_shift __read_mostly
;
87 static struct hlist_head
*dentry_hashtable __read_mostly
;
89 /* Statistics gathering. */
90 struct dentry_stat_t dentry_stat
= {
94 static DEFINE_PER_CPU(unsigned int, nr_dentry
);
96 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
97 static int get_nr_dentry(void)
101 for_each_possible_cpu(i
)
102 sum
+= per_cpu(nr_dentry
, i
);
103 return sum
< 0 ? 0 : sum
;
106 int proc_nr_dentry(ctl_table
*table
, int write
, void __user
*buffer
,
107 size_t *lenp
, loff_t
*ppos
)
109 dentry_stat
.nr_dentry
= get_nr_dentry();
110 return proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
114 static void __d_free(struct rcu_head
*head
)
116 struct dentry
*dentry
= container_of(head
, struct dentry
, d_u
.d_rcu
);
118 WARN_ON(!list_empty(&dentry
->d_alias
));
119 if (dname_external(dentry
))
120 kfree(dentry
->d_name
.name
);
121 kmem_cache_free(dentry_cache
, dentry
);
125 * no dcache_lock, please.
127 static void d_free(struct dentry
*dentry
)
129 BUG_ON(dentry
->d_count
);
130 this_cpu_dec(nr_dentry
);
131 if (dentry
->d_op
&& dentry
->d_op
->d_release
)
132 dentry
->d_op
->d_release(dentry
);
134 /* if dentry was never inserted into hash, immediate free is OK */
135 if (hlist_unhashed(&dentry
->d_hash
))
136 __d_free(&dentry
->d_u
.d_rcu
);
138 call_rcu(&dentry
->d_u
.d_rcu
, __d_free
);
142 * Release the dentry's inode, using the filesystem
143 * d_iput() operation if defined.
145 static void dentry_iput(struct dentry
* dentry
)
146 __releases(dentry
->d_lock
)
147 __releases(dcache_lock
)
149 struct inode
*inode
= dentry
->d_inode
;
151 dentry
->d_inode
= NULL
;
152 list_del_init(&dentry
->d_alias
);
153 spin_unlock(&dentry
->d_lock
);
154 spin_unlock(&dcache_lock
);
156 fsnotify_inoderemove(inode
);
157 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
158 dentry
->d_op
->d_iput(dentry
, inode
);
162 spin_unlock(&dentry
->d_lock
);
163 spin_unlock(&dcache_lock
);
168 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
170 static void dentry_lru_add(struct dentry
*dentry
)
172 if (list_empty(&dentry
->d_lru
)) {
173 spin_lock(&dcache_lru_lock
);
174 list_add(&dentry
->d_lru
, &dentry
->d_sb
->s_dentry_lru
);
175 dentry
->d_sb
->s_nr_dentry_unused
++;
176 dentry_stat
.nr_unused
++;
177 spin_unlock(&dcache_lru_lock
);
181 static void __dentry_lru_del(struct dentry
*dentry
)
183 list_del_init(&dentry
->d_lru
);
184 dentry
->d_sb
->s_nr_dentry_unused
--;
185 dentry_stat
.nr_unused
--;
188 static void dentry_lru_del(struct dentry
*dentry
)
190 if (!list_empty(&dentry
->d_lru
)) {
191 spin_lock(&dcache_lru_lock
);
192 __dentry_lru_del(dentry
);
193 spin_unlock(&dcache_lru_lock
);
197 static void dentry_lru_move_tail(struct dentry
*dentry
)
199 spin_lock(&dcache_lru_lock
);
200 if (list_empty(&dentry
->d_lru
)) {
201 list_add_tail(&dentry
->d_lru
, &dentry
->d_sb
->s_dentry_lru
);
202 dentry
->d_sb
->s_nr_dentry_unused
++;
203 dentry_stat
.nr_unused
++;
205 list_move_tail(&dentry
->d_lru
, &dentry
->d_sb
->s_dentry_lru
);
207 spin_unlock(&dcache_lru_lock
);
211 * d_kill - kill dentry and return parent
212 * @dentry: dentry to kill
214 * The dentry must already be unhashed and removed from the LRU.
216 * If this is the root of the dentry tree, return NULL.
218 * dcache_lock and d_lock must be held by caller, are dropped by d_kill.
220 static struct dentry
*d_kill(struct dentry
*dentry
)
221 __releases(dentry
->d_lock
)
222 __releases(dcache_lock
)
224 struct dentry
*parent
;
226 list_del(&dentry
->d_u
.d_child
);
229 * dentry_iput drops the locks, at which point nobody (except
230 * transient RCU lookups) can reach this dentry.
235 parent
= dentry
->d_parent
;
241 * d_drop - drop a dentry
242 * @dentry: dentry to drop
244 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
245 * be found through a VFS lookup any more. Note that this is different from
246 * deleting the dentry - d_delete will try to mark the dentry negative if
247 * possible, giving a successful _negative_ lookup, while d_drop will
248 * just make the cache lookup fail.
250 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
251 * reason (NFS timeouts or autofs deletes).
253 * __d_drop requires dentry->d_lock.
255 void __d_drop(struct dentry
*dentry
)
257 if (!(dentry
->d_flags
& DCACHE_UNHASHED
)) {
258 dentry
->d_flags
|= DCACHE_UNHASHED
;
259 spin_lock(&dcache_hash_lock
);
260 hlist_del_rcu(&dentry
->d_hash
);
261 spin_unlock(&dcache_hash_lock
);
264 EXPORT_SYMBOL(__d_drop
);
266 void d_drop(struct dentry
*dentry
)
268 spin_lock(&dcache_lock
);
269 spin_lock(&dentry
->d_lock
);
271 spin_unlock(&dentry
->d_lock
);
272 spin_unlock(&dcache_lock
);
274 EXPORT_SYMBOL(d_drop
);
279 * This is complicated by the fact that we do not want to put
280 * dentries that are no longer on any hash chain on the unused
281 * list: we'd much rather just get rid of them immediately.
283 * However, that implies that we have to traverse the dentry
284 * tree upwards to the parents which might _also_ now be
285 * scheduled for deletion (it may have been only waiting for
286 * its last child to go away).
288 * This tail recursion is done by hand as we don't want to depend
289 * on the compiler to always get this right (gcc generally doesn't).
290 * Real recursion would eat up our stack space.
294 * dput - release a dentry
295 * @dentry: dentry to release
297 * Release a dentry. This will drop the usage count and if appropriate
298 * call the dentry unlink method as well as removing it from the queues and
299 * releasing its resources. If the parent dentries were scheduled for release
300 * they too may now get deleted.
302 * no dcache lock, please.
305 void dput(struct dentry
*dentry
)
311 if (dentry
->d_count
== 1)
313 spin_lock(&dentry
->d_lock
);
314 if (dentry
->d_count
== 1) {
315 if (!spin_trylock(&dcache_lock
)) {
317 * Something of a livelock possibility we could avoid
318 * by taking dcache_lock and trying again, but we
319 * want to reduce dcache_lock anyway so this will
322 spin_unlock(&dentry
->d_lock
);
327 if (dentry
->d_count
) {
328 spin_unlock(&dentry
->d_lock
);
329 spin_unlock(&dcache_lock
);
334 * AV: ->d_delete() is _NOT_ allowed to block now.
336 if (dentry
->d_op
&& dentry
->d_op
->d_delete
) {
337 if (dentry
->d_op
->d_delete(dentry
))
341 /* Unreachable? Get rid of it */
342 if (d_unhashed(dentry
))
345 /* Otherwise leave it cached and ensure it's on the LRU */
346 dentry
->d_flags
|= DCACHE_REFERENCED
;
347 dentry_lru_add(dentry
);
349 spin_unlock(&dentry
->d_lock
);
350 spin_unlock(&dcache_lock
);
356 /* if dentry was on the d_lru list delete it from there */
357 dentry_lru_del(dentry
);
358 dentry
= d_kill(dentry
);
365 * d_invalidate - invalidate a dentry
366 * @dentry: dentry to invalidate
368 * Try to invalidate the dentry if it turns out to be
369 * possible. If there are other dentries that can be
370 * reached through this one we can't delete it and we
371 * return -EBUSY. On success we return 0.
376 int d_invalidate(struct dentry
* dentry
)
379 * If it's already been dropped, return OK.
381 spin_lock(&dcache_lock
);
382 if (d_unhashed(dentry
)) {
383 spin_unlock(&dcache_lock
);
387 * Check whether to do a partial shrink_dcache
388 * to get rid of unused child entries.
390 if (!list_empty(&dentry
->d_subdirs
)) {
391 spin_unlock(&dcache_lock
);
392 shrink_dcache_parent(dentry
);
393 spin_lock(&dcache_lock
);
397 * Somebody else still using it?
399 * If it's a directory, we can't drop it
400 * for fear of somebody re-populating it
401 * with children (even though dropping it
402 * would make it unreachable from the root,
403 * we might still populate it if it was a
404 * working directory or similar).
406 spin_lock(&dentry
->d_lock
);
407 if (dentry
->d_count
> 1) {
408 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
)) {
409 spin_unlock(&dentry
->d_lock
);
410 spin_unlock(&dcache_lock
);
416 spin_unlock(&dentry
->d_lock
);
417 spin_unlock(&dcache_lock
);
420 EXPORT_SYMBOL(d_invalidate
);
422 /* This must be called with dcache_lock and d_lock held */
423 static inline struct dentry
* __dget_locked_dlock(struct dentry
*dentry
)
426 dentry_lru_del(dentry
);
430 /* This should be called _only_ with dcache_lock held */
431 static inline struct dentry
* __dget_locked(struct dentry
*dentry
)
433 spin_lock(&dentry
->d_lock
);
434 __dget_locked_dlock(dentry
);
435 spin_unlock(&dentry
->d_lock
);
439 struct dentry
* dget_locked_dlock(struct dentry
*dentry
)
441 return __dget_locked_dlock(dentry
);
444 struct dentry
* dget_locked(struct dentry
*dentry
)
446 return __dget_locked(dentry
);
448 EXPORT_SYMBOL(dget_locked
);
450 struct dentry
*dget_parent(struct dentry
*dentry
)
455 spin_lock(&dentry
->d_lock
);
456 ret
= dentry
->d_parent
;
463 if (!spin_trylock(&ret
->d_lock
)) {
464 spin_unlock(&dentry
->d_lock
);
468 BUG_ON(!ret
->d_count
);
470 spin_unlock(&ret
->d_lock
);
472 spin_unlock(&dentry
->d_lock
);
475 EXPORT_SYMBOL(dget_parent
);
478 * d_find_alias - grab a hashed alias of inode
479 * @inode: inode in question
480 * @want_discon: flag, used by d_splice_alias, to request
481 * that only a DISCONNECTED alias be returned.
483 * If inode has a hashed alias, or is a directory and has any alias,
484 * acquire the reference to alias and return it. Otherwise return NULL.
485 * Notice that if inode is a directory there can be only one alias and
486 * it can be unhashed only if it has no children, or if it is the root
489 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
490 * any other hashed alias over that one unless @want_discon is set,
491 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
494 static struct dentry
* __d_find_alias(struct inode
*inode
, int want_discon
)
496 struct list_head
*head
, *next
, *tmp
;
497 struct dentry
*alias
, *discon_alias
=NULL
;
499 head
= &inode
->i_dentry
;
500 next
= inode
->i_dentry
.next
;
501 while (next
!= head
) {
505 alias
= list_entry(tmp
, struct dentry
, d_alias
);
506 if (S_ISDIR(inode
->i_mode
) || !d_unhashed(alias
)) {
507 if (IS_ROOT(alias
) &&
508 (alias
->d_flags
& DCACHE_DISCONNECTED
))
509 discon_alias
= alias
;
510 else if (!want_discon
) {
511 __dget_locked(alias
);
517 __dget_locked(discon_alias
);
521 struct dentry
* d_find_alias(struct inode
*inode
)
523 struct dentry
*de
= NULL
;
525 if (!list_empty(&inode
->i_dentry
)) {
526 spin_lock(&dcache_lock
);
527 de
= __d_find_alias(inode
, 0);
528 spin_unlock(&dcache_lock
);
532 EXPORT_SYMBOL(d_find_alias
);
535 * Try to kill dentries associated with this inode.
536 * WARNING: you must own a reference to inode.
538 void d_prune_aliases(struct inode
*inode
)
540 struct dentry
*dentry
;
542 spin_lock(&dcache_lock
);
543 list_for_each_entry(dentry
, &inode
->i_dentry
, d_alias
) {
544 spin_lock(&dentry
->d_lock
);
545 if (!dentry
->d_count
) {
546 __dget_locked_dlock(dentry
);
548 spin_unlock(&dentry
->d_lock
);
549 spin_unlock(&dcache_lock
);
553 spin_unlock(&dentry
->d_lock
);
555 spin_unlock(&dcache_lock
);
557 EXPORT_SYMBOL(d_prune_aliases
);
560 * Throw away a dentry - free the inode, dput the parent. This requires that
561 * the LRU list has already been removed.
563 * Try to prune ancestors as well. This is necessary to prevent
564 * quadratic behavior of shrink_dcache_parent(), but is also expected
565 * to be beneficial in reducing dentry cache fragmentation.
567 static void prune_one_dentry(struct dentry
* dentry
)
568 __releases(dentry
->d_lock
)
569 __releases(dcache_lock
)
572 dentry
= d_kill(dentry
);
575 * Prune ancestors. Locking is simpler than in dput(),
576 * because dcache_lock needs to be taken anyway.
579 spin_lock(&dcache_lock
);
580 spin_lock(&dentry
->d_lock
);
582 if (dentry
->d_count
) {
583 spin_unlock(&dentry
->d_lock
);
584 spin_unlock(&dcache_lock
);
588 dentry_lru_del(dentry
);
590 dentry
= d_kill(dentry
);
594 static void shrink_dentry_list(struct list_head
*list
)
596 struct dentry
*dentry
;
598 while (!list_empty(list
)) {
599 dentry
= list_entry(list
->prev
, struct dentry
, d_lru
);
601 if (!spin_trylock(&dentry
->d_lock
)) {
602 spin_unlock(&dcache_lru_lock
);
604 spin_lock(&dcache_lru_lock
);
608 __dentry_lru_del(dentry
);
611 * We found an inuse dentry which was not removed from
612 * the LRU because of laziness during lookup. Do not free
613 * it - just keep it off the LRU list.
615 if (dentry
->d_count
) {
616 spin_unlock(&dentry
->d_lock
);
619 spin_unlock(&dcache_lru_lock
);
621 prune_one_dentry(dentry
);
622 /* dcache_lock and dentry->d_lock dropped */
623 spin_lock(&dcache_lock
);
624 spin_lock(&dcache_lru_lock
);
629 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
630 * @sb: superblock to shrink dentry LRU.
631 * @count: number of entries to prune
632 * @flags: flags to control the dentry processing
634 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
636 static void __shrink_dcache_sb(struct super_block
*sb
, int *count
, int flags
)
638 /* called from prune_dcache() and shrink_dcache_parent() */
639 struct dentry
*dentry
;
640 LIST_HEAD(referenced
);
644 spin_lock(&dcache_lock
);
646 spin_lock(&dcache_lru_lock
);
647 while (!list_empty(&sb
->s_dentry_lru
)) {
648 dentry
= list_entry(sb
->s_dentry_lru
.prev
,
649 struct dentry
, d_lru
);
650 BUG_ON(dentry
->d_sb
!= sb
);
652 if (!spin_trylock(&dentry
->d_lock
)) {
653 spin_unlock(&dcache_lru_lock
);
659 * If we are honouring the DCACHE_REFERENCED flag and the
660 * dentry has this flag set, don't free it. Clear the flag
661 * and put it back on the LRU.
663 if (flags
& DCACHE_REFERENCED
&&
664 dentry
->d_flags
& DCACHE_REFERENCED
) {
665 dentry
->d_flags
&= ~DCACHE_REFERENCED
;
666 list_move(&dentry
->d_lru
, &referenced
);
667 spin_unlock(&dentry
->d_lock
);
669 list_move_tail(&dentry
->d_lru
, &tmp
);
670 spin_unlock(&dentry
->d_lock
);
674 /* XXX: re-add cond_resched_lock when dcache_lock goes away */
678 shrink_dentry_list(&tmp
);
680 if (!list_empty(&referenced
))
681 list_splice(&referenced
, &sb
->s_dentry_lru
);
682 spin_unlock(&dcache_lru_lock
);
683 spin_unlock(&dcache_lock
);
688 * prune_dcache - shrink the dcache
689 * @count: number of entries to try to free
691 * Shrink the dcache. This is done when we need more memory, or simply when we
692 * need to unmount something (at which point we need to unuse all dentries).
694 * This function may fail to free any resources if all the dentries are in use.
696 static void prune_dcache(int count
)
698 struct super_block
*sb
, *p
= NULL
;
700 int unused
= dentry_stat
.nr_unused
;
704 if (unused
== 0 || count
== 0)
706 spin_lock(&dcache_lock
);
710 prune_ratio
= unused
/ count
;
712 list_for_each_entry(sb
, &super_blocks
, s_list
) {
713 if (list_empty(&sb
->s_instances
))
715 if (sb
->s_nr_dentry_unused
== 0)
718 /* Now, we reclaim unused dentrins with fairness.
719 * We reclaim them same percentage from each superblock.
720 * We calculate number of dentries to scan on this sb
721 * as follows, but the implementation is arranged to avoid
723 * number of dentries to scan on this sb =
724 * count * (number of dentries on this sb /
725 * number of dentries in the machine)
727 spin_unlock(&sb_lock
);
728 if (prune_ratio
!= 1)
729 w_count
= (sb
->s_nr_dentry_unused
/ prune_ratio
) + 1;
731 w_count
= sb
->s_nr_dentry_unused
;
734 * We need to be sure this filesystem isn't being unmounted,
735 * otherwise we could race with generic_shutdown_super(), and
736 * end up holding a reference to an inode while the filesystem
737 * is unmounted. So we try to get s_umount, and make sure
740 if (down_read_trylock(&sb
->s_umount
)) {
741 if ((sb
->s_root
!= NULL
) &&
742 (!list_empty(&sb
->s_dentry_lru
))) {
743 spin_unlock(&dcache_lock
);
744 __shrink_dcache_sb(sb
, &w_count
,
747 spin_lock(&dcache_lock
);
749 up_read(&sb
->s_umount
);
756 /* more work left to do? */
762 spin_unlock(&sb_lock
);
763 spin_unlock(&dcache_lock
);
767 * shrink_dcache_sb - shrink dcache for a superblock
770 * Shrink the dcache for the specified super block. This is used to free
771 * the dcache before unmounting a file system.
773 void shrink_dcache_sb(struct super_block
*sb
)
777 spin_lock(&dcache_lock
);
778 spin_lock(&dcache_lru_lock
);
779 while (!list_empty(&sb
->s_dentry_lru
)) {
780 list_splice_init(&sb
->s_dentry_lru
, &tmp
);
781 shrink_dentry_list(&tmp
);
783 spin_unlock(&dcache_lru_lock
);
784 spin_unlock(&dcache_lock
);
786 EXPORT_SYMBOL(shrink_dcache_sb
);
789 * destroy a single subtree of dentries for unmount
790 * - see the comments on shrink_dcache_for_umount() for a description of the
793 static void shrink_dcache_for_umount_subtree(struct dentry
*dentry
)
795 struct dentry
*parent
;
796 unsigned detached
= 0;
798 BUG_ON(!IS_ROOT(dentry
));
800 /* detach this root from the system */
801 spin_lock(&dcache_lock
);
802 spin_lock(&dentry
->d_lock
);
803 dentry_lru_del(dentry
);
804 spin_unlock(&dentry
->d_lock
);
806 spin_unlock(&dcache_lock
);
809 /* descend to the first leaf in the current subtree */
810 while (!list_empty(&dentry
->d_subdirs
)) {
813 /* this is a branch with children - detach all of them
814 * from the system in one go */
815 spin_lock(&dcache_lock
);
816 list_for_each_entry(loop
, &dentry
->d_subdirs
,
818 spin_lock(&loop
->d_lock
);
819 dentry_lru_del(loop
);
820 spin_unlock(&loop
->d_lock
);
822 cond_resched_lock(&dcache_lock
);
824 spin_unlock(&dcache_lock
);
826 /* move to the first child */
827 dentry
= list_entry(dentry
->d_subdirs
.next
,
828 struct dentry
, d_u
.d_child
);
831 /* consume the dentries from this leaf up through its parents
832 * until we find one with children or run out altogether */
836 if (dentry
->d_count
!= 0) {
838 "BUG: Dentry %p{i=%lx,n=%s}"
840 " [unmount of %s %s]\n",
843 dentry
->d_inode
->i_ino
: 0UL,
846 dentry
->d_sb
->s_type
->name
,
854 parent
= dentry
->d_parent
;
855 spin_lock(&parent
->d_lock
);
857 spin_unlock(&parent
->d_lock
);
860 list_del(&dentry
->d_u
.d_child
);
863 inode
= dentry
->d_inode
;
865 dentry
->d_inode
= NULL
;
866 list_del_init(&dentry
->d_alias
);
867 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
868 dentry
->d_op
->d_iput(dentry
, inode
);
875 /* finished when we fall off the top of the tree,
876 * otherwise we ascend to the parent and move to the
877 * next sibling if there is one */
881 } while (list_empty(&dentry
->d_subdirs
));
883 dentry
= list_entry(dentry
->d_subdirs
.next
,
884 struct dentry
, d_u
.d_child
);
889 * destroy the dentries attached to a superblock on unmounting
890 * - we don't need to use dentry->d_lock, and only need dcache_lock when
891 * removing the dentry from the system lists and hashes because:
892 * - the superblock is detached from all mountings and open files, so the
893 * dentry trees will not be rearranged by the VFS
894 * - s_umount is write-locked, so the memory pressure shrinker will ignore
895 * any dentries belonging to this superblock that it comes across
896 * - the filesystem itself is no longer permitted to rearrange the dentries
899 void shrink_dcache_for_umount(struct super_block
*sb
)
901 struct dentry
*dentry
;
903 if (down_read_trylock(&sb
->s_umount
))
908 spin_lock(&dentry
->d_lock
);
910 spin_unlock(&dentry
->d_lock
);
911 shrink_dcache_for_umount_subtree(dentry
);
913 while (!hlist_empty(&sb
->s_anon
)) {
914 dentry
= hlist_entry(sb
->s_anon
.first
, struct dentry
, d_hash
);
915 shrink_dcache_for_umount_subtree(dentry
);
920 * Search for at least 1 mount point in the dentry's subdirs.
921 * We descend to the next level whenever the d_subdirs
922 * list is non-empty and continue searching.
926 * have_submounts - check for mounts over a dentry
927 * @parent: dentry to check.
929 * Return true if the parent or its subdirectories contain
933 int have_submounts(struct dentry
*parent
)
935 struct dentry
*this_parent
= parent
;
936 struct list_head
*next
;
938 spin_lock(&dcache_lock
);
939 if (d_mountpoint(parent
))
942 next
= this_parent
->d_subdirs
.next
;
944 while (next
!= &this_parent
->d_subdirs
) {
945 struct list_head
*tmp
= next
;
946 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
948 /* Have we found a mount point ? */
949 if (d_mountpoint(dentry
))
951 if (!list_empty(&dentry
->d_subdirs
)) {
952 this_parent
= dentry
;
957 * All done at this level ... ascend and resume the search.
959 if (this_parent
!= parent
) {
960 next
= this_parent
->d_u
.d_child
.next
;
961 this_parent
= this_parent
->d_parent
;
964 spin_unlock(&dcache_lock
);
965 return 0; /* No mount points found in tree */
967 spin_unlock(&dcache_lock
);
970 EXPORT_SYMBOL(have_submounts
);
973 * Search the dentry child list for the specified parent,
974 * and move any unused dentries to the end of the unused
975 * list for prune_dcache(). We descend to the next level
976 * whenever the d_subdirs list is non-empty and continue
979 * It returns zero iff there are no unused children,
980 * otherwise it returns the number of children moved to
981 * the end of the unused list. This may not be the total
982 * number of unused children, because select_parent can
983 * drop the lock and return early due to latency
986 static int select_parent(struct dentry
* parent
)
988 struct dentry
*this_parent
= parent
;
989 struct list_head
*next
;
992 spin_lock(&dcache_lock
);
994 next
= this_parent
->d_subdirs
.next
;
996 while (next
!= &this_parent
->d_subdirs
) {
997 struct list_head
*tmp
= next
;
998 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
1001 spin_lock(&dentry
->d_lock
);
1004 * move only zero ref count dentries to the end
1005 * of the unused list for prune_dcache
1007 if (!dentry
->d_count
) {
1008 dentry_lru_move_tail(dentry
);
1011 dentry_lru_del(dentry
);
1014 spin_unlock(&dentry
->d_lock
);
1017 * We can return to the caller if we have found some (this
1018 * ensures forward progress). We'll be coming back to find
1021 if (found
&& need_resched())
1025 * Descend a level if the d_subdirs list is non-empty.
1027 if (!list_empty(&dentry
->d_subdirs
)) {
1028 this_parent
= dentry
;
1033 * All done at this level ... ascend and resume the search.
1035 if (this_parent
!= parent
) {
1036 next
= this_parent
->d_u
.d_child
.next
;
1037 this_parent
= this_parent
->d_parent
;
1041 spin_unlock(&dcache_lock
);
1046 * shrink_dcache_parent - prune dcache
1047 * @parent: parent of entries to prune
1049 * Prune the dcache to remove unused children of the parent dentry.
1052 void shrink_dcache_parent(struct dentry
* parent
)
1054 struct super_block
*sb
= parent
->d_sb
;
1057 while ((found
= select_parent(parent
)) != 0)
1058 __shrink_dcache_sb(sb
, &found
, 0);
1060 EXPORT_SYMBOL(shrink_dcache_parent
);
1063 * Scan `nr' dentries and return the number which remain.
1065 * We need to avoid reentering the filesystem if the caller is performing a
1066 * GFP_NOFS allocation attempt. One example deadlock is:
1068 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1069 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1070 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1072 * In this case we return -1 to tell the caller that we baled.
1074 static int shrink_dcache_memory(struct shrinker
*shrink
, int nr
, gfp_t gfp_mask
)
1077 if (!(gfp_mask
& __GFP_FS
))
1082 return (dentry_stat
.nr_unused
/ 100) * sysctl_vfs_cache_pressure
;
1085 static struct shrinker dcache_shrinker
= {
1086 .shrink
= shrink_dcache_memory
,
1087 .seeks
= DEFAULT_SEEKS
,
1091 * d_alloc - allocate a dcache entry
1092 * @parent: parent of entry to allocate
1093 * @name: qstr of the name
1095 * Allocates a dentry. It returns %NULL if there is insufficient memory
1096 * available. On a success the dentry is returned. The name passed in is
1097 * copied and the copy passed in may be reused after this call.
1100 struct dentry
*d_alloc(struct dentry
* parent
, const struct qstr
*name
)
1102 struct dentry
*dentry
;
1105 dentry
= kmem_cache_alloc(dentry_cache
, GFP_KERNEL
);
1109 if (name
->len
> DNAME_INLINE_LEN
-1) {
1110 dname
= kmalloc(name
->len
+ 1, GFP_KERNEL
);
1112 kmem_cache_free(dentry_cache
, dentry
);
1116 dname
= dentry
->d_iname
;
1118 dentry
->d_name
.name
= dname
;
1120 dentry
->d_name
.len
= name
->len
;
1121 dentry
->d_name
.hash
= name
->hash
;
1122 memcpy(dname
, name
->name
, name
->len
);
1123 dname
[name
->len
] = 0;
1125 dentry
->d_count
= 1;
1126 dentry
->d_flags
= DCACHE_UNHASHED
;
1127 spin_lock_init(&dentry
->d_lock
);
1128 dentry
->d_inode
= NULL
;
1129 dentry
->d_parent
= NULL
;
1130 dentry
->d_sb
= NULL
;
1131 dentry
->d_op
= NULL
;
1132 dentry
->d_fsdata
= NULL
;
1133 dentry
->d_mounted
= 0;
1134 INIT_HLIST_NODE(&dentry
->d_hash
);
1135 INIT_LIST_HEAD(&dentry
->d_lru
);
1136 INIT_LIST_HEAD(&dentry
->d_subdirs
);
1137 INIT_LIST_HEAD(&dentry
->d_alias
);
1140 dentry
->d_parent
= dget(parent
);
1141 dentry
->d_sb
= parent
->d_sb
;
1143 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
1146 spin_lock(&dcache_lock
);
1148 list_add(&dentry
->d_u
.d_child
, &parent
->d_subdirs
);
1149 spin_unlock(&dcache_lock
);
1151 this_cpu_inc(nr_dentry
);
1155 EXPORT_SYMBOL(d_alloc
);
1157 struct dentry
*d_alloc_name(struct dentry
*parent
, const char *name
)
1162 q
.len
= strlen(name
);
1163 q
.hash
= full_name_hash(q
.name
, q
.len
);
1164 return d_alloc(parent
, &q
);
1166 EXPORT_SYMBOL(d_alloc_name
);
1168 /* the caller must hold dcache_lock */
1169 static void __d_instantiate(struct dentry
*dentry
, struct inode
*inode
)
1172 list_add(&dentry
->d_alias
, &inode
->i_dentry
);
1173 dentry
->d_inode
= inode
;
1174 fsnotify_d_instantiate(dentry
, inode
);
1178 * d_instantiate - fill in inode information for a dentry
1179 * @entry: dentry to complete
1180 * @inode: inode to attach to this dentry
1182 * Fill in inode information in the entry.
1184 * This turns negative dentries into productive full members
1187 * NOTE! This assumes that the inode count has been incremented
1188 * (or otherwise set) by the caller to indicate that it is now
1189 * in use by the dcache.
1192 void d_instantiate(struct dentry
*entry
, struct inode
* inode
)
1194 BUG_ON(!list_empty(&entry
->d_alias
));
1195 spin_lock(&dcache_lock
);
1196 __d_instantiate(entry
, inode
);
1197 spin_unlock(&dcache_lock
);
1198 security_d_instantiate(entry
, inode
);
1200 EXPORT_SYMBOL(d_instantiate
);
1203 * d_instantiate_unique - instantiate a non-aliased dentry
1204 * @entry: dentry to instantiate
1205 * @inode: inode to attach to this dentry
1207 * Fill in inode information in the entry. On success, it returns NULL.
1208 * If an unhashed alias of "entry" already exists, then we return the
1209 * aliased dentry instead and drop one reference to inode.
1211 * Note that in order to avoid conflicts with rename() etc, the caller
1212 * had better be holding the parent directory semaphore.
1214 * This also assumes that the inode count has been incremented
1215 * (or otherwise set) by the caller to indicate that it is now
1216 * in use by the dcache.
1218 static struct dentry
*__d_instantiate_unique(struct dentry
*entry
,
1219 struct inode
*inode
)
1221 struct dentry
*alias
;
1222 int len
= entry
->d_name
.len
;
1223 const char *name
= entry
->d_name
.name
;
1224 unsigned int hash
= entry
->d_name
.hash
;
1227 __d_instantiate(entry
, NULL
);
1231 list_for_each_entry(alias
, &inode
->i_dentry
, d_alias
) {
1232 struct qstr
*qstr
= &alias
->d_name
;
1234 if (qstr
->hash
!= hash
)
1236 if (alias
->d_parent
!= entry
->d_parent
)
1238 if (qstr
->len
!= len
)
1240 if (memcmp(qstr
->name
, name
, len
))
1246 __d_instantiate(entry
, inode
);
1250 struct dentry
*d_instantiate_unique(struct dentry
*entry
, struct inode
*inode
)
1252 struct dentry
*result
;
1254 BUG_ON(!list_empty(&entry
->d_alias
));
1256 spin_lock(&dcache_lock
);
1257 result
= __d_instantiate_unique(entry
, inode
);
1258 spin_unlock(&dcache_lock
);
1261 security_d_instantiate(entry
, inode
);
1265 BUG_ON(!d_unhashed(result
));
1270 EXPORT_SYMBOL(d_instantiate_unique
);
1273 * d_alloc_root - allocate root dentry
1274 * @root_inode: inode to allocate the root for
1276 * Allocate a root ("/") dentry for the inode given. The inode is
1277 * instantiated and returned. %NULL is returned if there is insufficient
1278 * memory or the inode passed is %NULL.
1281 struct dentry
* d_alloc_root(struct inode
* root_inode
)
1283 struct dentry
*res
= NULL
;
1286 static const struct qstr name
= { .name
= "/", .len
= 1 };
1288 res
= d_alloc(NULL
, &name
);
1290 res
->d_sb
= root_inode
->i_sb
;
1291 res
->d_parent
= res
;
1292 d_instantiate(res
, root_inode
);
1297 EXPORT_SYMBOL(d_alloc_root
);
1299 static inline struct hlist_head
*d_hash(struct dentry
*parent
,
1302 hash
+= ((unsigned long) parent
^ GOLDEN_RATIO_PRIME
) / L1_CACHE_BYTES
;
1303 hash
= hash
^ ((hash
^ GOLDEN_RATIO_PRIME
) >> D_HASHBITS
);
1304 return dentry_hashtable
+ (hash
& D_HASHMASK
);
1308 * d_obtain_alias - find or allocate a dentry for a given inode
1309 * @inode: inode to allocate the dentry for
1311 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1312 * similar open by handle operations. The returned dentry may be anonymous,
1313 * or may have a full name (if the inode was already in the cache).
1315 * When called on a directory inode, we must ensure that the inode only ever
1316 * has one dentry. If a dentry is found, that is returned instead of
1317 * allocating a new one.
1319 * On successful return, the reference to the inode has been transferred
1320 * to the dentry. In case of an error the reference on the inode is released.
1321 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1322 * be passed in and will be the error will be propagate to the return value,
1323 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1325 struct dentry
*d_obtain_alias(struct inode
*inode
)
1327 static const struct qstr anonstring
= { .name
= "" };
1332 return ERR_PTR(-ESTALE
);
1334 return ERR_CAST(inode
);
1336 res
= d_find_alias(inode
);
1340 tmp
= d_alloc(NULL
, &anonstring
);
1342 res
= ERR_PTR(-ENOMEM
);
1345 tmp
->d_parent
= tmp
; /* make sure dput doesn't croak */
1347 spin_lock(&dcache_lock
);
1348 res
= __d_find_alias(inode
, 0);
1350 spin_unlock(&dcache_lock
);
1355 /* attach a disconnected dentry */
1356 spin_lock(&tmp
->d_lock
);
1357 tmp
->d_sb
= inode
->i_sb
;
1358 tmp
->d_inode
= inode
;
1359 tmp
->d_flags
|= DCACHE_DISCONNECTED
;
1360 tmp
->d_flags
&= ~DCACHE_UNHASHED
;
1361 list_add(&tmp
->d_alias
, &inode
->i_dentry
);
1362 spin_lock(&dcache_hash_lock
);
1363 hlist_add_head(&tmp
->d_hash
, &inode
->i_sb
->s_anon
);
1364 spin_unlock(&dcache_hash_lock
);
1365 spin_unlock(&tmp
->d_lock
);
1367 spin_unlock(&dcache_lock
);
1374 EXPORT_SYMBOL(d_obtain_alias
);
1377 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1378 * @inode: the inode which may have a disconnected dentry
1379 * @dentry: a negative dentry which we want to point to the inode.
1381 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1382 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1383 * and return it, else simply d_add the inode to the dentry and return NULL.
1385 * This is needed in the lookup routine of any filesystem that is exportable
1386 * (via knfsd) so that we can build dcache paths to directories effectively.
1388 * If a dentry was found and moved, then it is returned. Otherwise NULL
1389 * is returned. This matches the expected return value of ->lookup.
1392 struct dentry
*d_splice_alias(struct inode
*inode
, struct dentry
*dentry
)
1394 struct dentry
*new = NULL
;
1396 if (inode
&& S_ISDIR(inode
->i_mode
)) {
1397 spin_lock(&dcache_lock
);
1398 new = __d_find_alias(inode
, 1);
1400 BUG_ON(!(new->d_flags
& DCACHE_DISCONNECTED
));
1401 spin_unlock(&dcache_lock
);
1402 security_d_instantiate(new, inode
);
1403 d_move(new, dentry
);
1406 /* already taking dcache_lock, so d_add() by hand */
1407 __d_instantiate(dentry
, inode
);
1408 spin_unlock(&dcache_lock
);
1409 security_d_instantiate(dentry
, inode
);
1413 d_add(dentry
, inode
);
1416 EXPORT_SYMBOL(d_splice_alias
);
1419 * d_add_ci - lookup or allocate new dentry with case-exact name
1420 * @inode: the inode case-insensitive lookup has found
1421 * @dentry: the negative dentry that was passed to the parent's lookup func
1422 * @name: the case-exact name to be associated with the returned dentry
1424 * This is to avoid filling the dcache with case-insensitive names to the
1425 * same inode, only the actual correct case is stored in the dcache for
1426 * case-insensitive filesystems.
1428 * For a case-insensitive lookup match and if the the case-exact dentry
1429 * already exists in in the dcache, use it and return it.
1431 * If no entry exists with the exact case name, allocate new dentry with
1432 * the exact case, and return the spliced entry.
1434 struct dentry
*d_add_ci(struct dentry
*dentry
, struct inode
*inode
,
1438 struct dentry
*found
;
1442 * First check if a dentry matching the name already exists,
1443 * if not go ahead and create it now.
1445 found
= d_hash_and_lookup(dentry
->d_parent
, name
);
1447 new = d_alloc(dentry
->d_parent
, name
);
1453 found
= d_splice_alias(inode
, new);
1462 * If a matching dentry exists, and it's not negative use it.
1464 * Decrement the reference count to balance the iget() done
1467 if (found
->d_inode
) {
1468 if (unlikely(found
->d_inode
!= inode
)) {
1469 /* This can't happen because bad inodes are unhashed. */
1470 BUG_ON(!is_bad_inode(inode
));
1471 BUG_ON(!is_bad_inode(found
->d_inode
));
1478 * Negative dentry: instantiate it unless the inode is a directory and
1479 * already has a dentry.
1481 spin_lock(&dcache_lock
);
1482 if (!S_ISDIR(inode
->i_mode
) || list_empty(&inode
->i_dentry
)) {
1483 __d_instantiate(found
, inode
);
1484 spin_unlock(&dcache_lock
);
1485 security_d_instantiate(found
, inode
);
1490 * In case a directory already has a (disconnected) entry grab a
1491 * reference to it, move it in place and use it.
1493 new = list_entry(inode
->i_dentry
.next
, struct dentry
, d_alias
);
1495 spin_unlock(&dcache_lock
);
1496 security_d_instantiate(found
, inode
);
1504 return ERR_PTR(error
);
1506 EXPORT_SYMBOL(d_add_ci
);
1509 * d_lookup - search for a dentry
1510 * @parent: parent dentry
1511 * @name: qstr of name we wish to find
1512 * Returns: dentry, or NULL
1514 * d_lookup searches the children of the parent dentry for the name in
1515 * question. If the dentry is found its reference count is incremented and the
1516 * dentry is returned. The caller must use dput to free the entry when it has
1517 * finished using it. %NULL is returned if the dentry does not exist.
1519 struct dentry
* d_lookup(struct dentry
* parent
, struct qstr
* name
)
1521 struct dentry
* dentry
= NULL
;
1525 seq
= read_seqbegin(&rename_lock
);
1526 dentry
= __d_lookup(parent
, name
);
1529 } while (read_seqretry(&rename_lock
, seq
));
1532 EXPORT_SYMBOL(d_lookup
);
1535 * __d_lookup - search for a dentry (racy)
1536 * @parent: parent dentry
1537 * @name: qstr of name we wish to find
1538 * Returns: dentry, or NULL
1540 * __d_lookup is like d_lookup, however it may (rarely) return a
1541 * false-negative result due to unrelated rename activity.
1543 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1544 * however it must be used carefully, eg. with a following d_lookup in
1545 * the case of failure.
1547 * __d_lookup callers must be commented.
1549 struct dentry
* __d_lookup(struct dentry
* parent
, struct qstr
* name
)
1551 unsigned int len
= name
->len
;
1552 unsigned int hash
= name
->hash
;
1553 const unsigned char *str
= name
->name
;
1554 struct hlist_head
*head
= d_hash(parent
,hash
);
1555 struct dentry
*found
= NULL
;
1556 struct hlist_node
*node
;
1557 struct dentry
*dentry
;
1560 * The hash list is protected using RCU.
1562 * Take d_lock when comparing a candidate dentry, to avoid races
1565 * It is possible that concurrent renames can mess up our list
1566 * walk here and result in missing our dentry, resulting in the
1567 * false-negative result. d_lookup() protects against concurrent
1568 * renames using rename_lock seqlock.
1570 * See Documentation/vfs/dcache-locking.txt for more details.
1574 hlist_for_each_entry_rcu(dentry
, node
, head
, d_hash
) {
1577 if (dentry
->d_name
.hash
!= hash
)
1579 if (dentry
->d_parent
!= parent
)
1582 spin_lock(&dentry
->d_lock
);
1585 * Recheck the dentry after taking the lock - d_move may have
1586 * changed things. Don't bother checking the hash because
1587 * we're about to compare the whole name anyway.
1589 if (dentry
->d_parent
!= parent
)
1592 /* non-existing due to RCU? */
1593 if (d_unhashed(dentry
))
1597 * It is safe to compare names since d_move() cannot
1598 * change the qstr (protected by d_lock).
1600 qstr
= &dentry
->d_name
;
1601 if (parent
->d_op
&& parent
->d_op
->d_compare
) {
1602 if (parent
->d_op
->d_compare(parent
, parent
->d_inode
,
1603 dentry
, dentry
->d_inode
,
1604 qstr
->len
, qstr
->name
, name
))
1607 if (qstr
->len
!= len
)
1609 if (memcmp(qstr
->name
, str
, len
))
1615 spin_unlock(&dentry
->d_lock
);
1618 spin_unlock(&dentry
->d_lock
);
1626 * d_hash_and_lookup - hash the qstr then search for a dentry
1627 * @dir: Directory to search in
1628 * @name: qstr of name we wish to find
1630 * On hash failure or on lookup failure NULL is returned.
1632 struct dentry
*d_hash_and_lookup(struct dentry
*dir
, struct qstr
*name
)
1634 struct dentry
*dentry
= NULL
;
1637 * Check for a fs-specific hash function. Note that we must
1638 * calculate the standard hash first, as the d_op->d_hash()
1639 * routine may choose to leave the hash value unchanged.
1641 name
->hash
= full_name_hash(name
->name
, name
->len
);
1642 if (dir
->d_op
&& dir
->d_op
->d_hash
) {
1643 if (dir
->d_op
->d_hash(dir
, dir
->d_inode
, name
) < 0)
1646 dentry
= d_lookup(dir
, name
);
1652 * d_validate - verify dentry provided from insecure source (deprecated)
1653 * @dentry: The dentry alleged to be valid child of @dparent
1654 * @dparent: The parent dentry (known to be valid)
1656 * An insecure source has sent us a dentry, here we verify it and dget() it.
1657 * This is used by ncpfs in its readdir implementation.
1658 * Zero is returned in the dentry is invalid.
1660 * This function is slow for big directories, and deprecated, do not use it.
1662 int d_validate(struct dentry
*dentry
, struct dentry
*dparent
)
1664 struct dentry
*child
;
1666 spin_lock(&dcache_lock
);
1667 list_for_each_entry(child
, &dparent
->d_subdirs
, d_u
.d_child
) {
1668 if (dentry
== child
) {
1669 __dget_locked(dentry
);
1670 spin_unlock(&dcache_lock
);
1674 spin_unlock(&dcache_lock
);
1678 EXPORT_SYMBOL(d_validate
);
1681 * When a file is deleted, we have two options:
1682 * - turn this dentry into a negative dentry
1683 * - unhash this dentry and free it.
1685 * Usually, we want to just turn this into
1686 * a negative dentry, but if anybody else is
1687 * currently using the dentry or the inode
1688 * we can't do that and we fall back on removing
1689 * it from the hash queues and waiting for
1690 * it to be deleted later when it has no users
1694 * d_delete - delete a dentry
1695 * @dentry: The dentry to delete
1697 * Turn the dentry into a negative dentry if possible, otherwise
1698 * remove it from the hash queues so it can be deleted later
1701 void d_delete(struct dentry
* dentry
)
1705 * Are we the only user?
1707 spin_lock(&dcache_lock
);
1708 spin_lock(&dentry
->d_lock
);
1709 isdir
= S_ISDIR(dentry
->d_inode
->i_mode
);
1710 if (dentry
->d_count
== 1) {
1711 dentry
->d_flags
&= ~DCACHE_CANT_MOUNT
;
1712 dentry_iput(dentry
);
1713 fsnotify_nameremove(dentry
, isdir
);
1717 if (!d_unhashed(dentry
))
1720 spin_unlock(&dentry
->d_lock
);
1721 spin_unlock(&dcache_lock
);
1723 fsnotify_nameremove(dentry
, isdir
);
1725 EXPORT_SYMBOL(d_delete
);
1727 static void __d_rehash(struct dentry
* entry
, struct hlist_head
*list
)
1730 entry
->d_flags
&= ~DCACHE_UNHASHED
;
1731 hlist_add_head_rcu(&entry
->d_hash
, list
);
1734 static void _d_rehash(struct dentry
* entry
)
1736 __d_rehash(entry
, d_hash(entry
->d_parent
, entry
->d_name
.hash
));
1740 * d_rehash - add an entry back to the hash
1741 * @entry: dentry to add to the hash
1743 * Adds a dentry to the hash according to its name.
1746 void d_rehash(struct dentry
* entry
)
1748 spin_lock(&dcache_lock
);
1749 spin_lock(&entry
->d_lock
);
1750 spin_lock(&dcache_hash_lock
);
1752 spin_unlock(&dcache_hash_lock
);
1753 spin_unlock(&entry
->d_lock
);
1754 spin_unlock(&dcache_lock
);
1756 EXPORT_SYMBOL(d_rehash
);
1759 * dentry_update_name_case - update case insensitive dentry with a new name
1760 * @dentry: dentry to be updated
1763 * Update a case insensitive dentry with new case of name.
1765 * dentry must have been returned by d_lookup with name @name. Old and new
1766 * name lengths must match (ie. no d_compare which allows mismatched name
1769 * Parent inode i_mutex must be held over d_lookup and into this call (to
1770 * keep renames and concurrent inserts, and readdir(2) away).
1772 void dentry_update_name_case(struct dentry
*dentry
, struct qstr
*name
)
1774 BUG_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
1775 BUG_ON(dentry
->d_name
.len
!= name
->len
); /* d_lookup gives this */
1777 spin_lock(&dcache_lock
);
1778 spin_lock(&dentry
->d_lock
);
1779 memcpy((unsigned char *)dentry
->d_name
.name
, name
->name
, name
->len
);
1780 spin_unlock(&dentry
->d_lock
);
1781 spin_unlock(&dcache_lock
);
1783 EXPORT_SYMBOL(dentry_update_name_case
);
1786 * When switching names, the actual string doesn't strictly have to
1787 * be preserved in the target - because we're dropping the target
1788 * anyway. As such, we can just do a simple memcpy() to copy over
1789 * the new name before we switch.
1791 * Note that we have to be a lot more careful about getting the hash
1792 * switched - we have to switch the hash value properly even if it
1793 * then no longer matches the actual (corrupted) string of the target.
1794 * The hash value has to match the hash queue that the dentry is on..
1796 static void switch_names(struct dentry
*dentry
, struct dentry
*target
)
1798 if (dname_external(target
)) {
1799 if (dname_external(dentry
)) {
1801 * Both external: swap the pointers
1803 swap(target
->d_name
.name
, dentry
->d_name
.name
);
1806 * dentry:internal, target:external. Steal target's
1807 * storage and make target internal.
1809 memcpy(target
->d_iname
, dentry
->d_name
.name
,
1810 dentry
->d_name
.len
+ 1);
1811 dentry
->d_name
.name
= target
->d_name
.name
;
1812 target
->d_name
.name
= target
->d_iname
;
1815 if (dname_external(dentry
)) {
1817 * dentry:external, target:internal. Give dentry's
1818 * storage to target and make dentry internal
1820 memcpy(dentry
->d_iname
, target
->d_name
.name
,
1821 target
->d_name
.len
+ 1);
1822 target
->d_name
.name
= dentry
->d_name
.name
;
1823 dentry
->d_name
.name
= dentry
->d_iname
;
1826 * Both are internal. Just copy target to dentry
1828 memcpy(dentry
->d_iname
, target
->d_name
.name
,
1829 target
->d_name
.len
+ 1);
1830 dentry
->d_name
.len
= target
->d_name
.len
;
1834 swap(dentry
->d_name
.len
, target
->d_name
.len
);
1838 * We cannibalize "target" when moving dentry on top of it,
1839 * because it's going to be thrown away anyway. We could be more
1840 * polite about it, though.
1842 * This forceful removal will result in ugly /proc output if
1843 * somebody holds a file open that got deleted due to a rename.
1844 * We could be nicer about the deleted file, and let it show
1845 * up under the name it had before it was deleted rather than
1846 * under the original name of the file that was moved on top of it.
1850 * d_move_locked - move a dentry
1851 * @dentry: entry to move
1852 * @target: new dentry
1854 * Update the dcache to reflect the move of a file name. Negative
1855 * dcache entries should not be moved in this way.
1857 static void d_move_locked(struct dentry
* dentry
, struct dentry
* target
)
1859 if (!dentry
->d_inode
)
1860 printk(KERN_WARNING
"VFS: moving negative dcache entry\n");
1862 write_seqlock(&rename_lock
);
1864 * XXXX: do we really need to take target->d_lock?
1866 if (target
< dentry
) {
1867 spin_lock(&target
->d_lock
);
1868 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1870 spin_lock(&dentry
->d_lock
);
1871 spin_lock_nested(&target
->d_lock
, DENTRY_D_LOCK_NESTED
);
1874 /* Move the dentry to the target hash queue, if on different bucket */
1875 spin_lock(&dcache_hash_lock
);
1876 if (!d_unhashed(dentry
))
1877 hlist_del_rcu(&dentry
->d_hash
);
1878 __d_rehash(dentry
, d_hash(target
->d_parent
, target
->d_name
.hash
));
1879 spin_unlock(&dcache_hash_lock
);
1881 /* Unhash the target: dput() will then get rid of it */
1884 list_del(&dentry
->d_u
.d_child
);
1885 list_del(&target
->d_u
.d_child
);
1887 /* Switch the names.. */
1888 switch_names(dentry
, target
);
1889 swap(dentry
->d_name
.hash
, target
->d_name
.hash
);
1891 /* ... and switch the parents */
1892 if (IS_ROOT(dentry
)) {
1893 dentry
->d_parent
= target
->d_parent
;
1894 target
->d_parent
= target
;
1895 INIT_LIST_HEAD(&target
->d_u
.d_child
);
1897 swap(dentry
->d_parent
, target
->d_parent
);
1899 /* And add them back to the (new) parent lists */
1900 list_add(&target
->d_u
.d_child
, &target
->d_parent
->d_subdirs
);
1903 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
1904 spin_unlock(&target
->d_lock
);
1905 fsnotify_d_move(dentry
);
1906 spin_unlock(&dentry
->d_lock
);
1907 write_sequnlock(&rename_lock
);
1911 * d_move - move a dentry
1912 * @dentry: entry to move
1913 * @target: new dentry
1915 * Update the dcache to reflect the move of a file name. Negative
1916 * dcache entries should not be moved in this way.
1919 void d_move(struct dentry
* dentry
, struct dentry
* target
)
1921 spin_lock(&dcache_lock
);
1922 d_move_locked(dentry
, target
);
1923 spin_unlock(&dcache_lock
);
1925 EXPORT_SYMBOL(d_move
);
1928 * d_ancestor - search for an ancestor
1929 * @p1: ancestor dentry
1932 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1933 * an ancestor of p2, else NULL.
1935 struct dentry
*d_ancestor(struct dentry
*p1
, struct dentry
*p2
)
1939 for (p
= p2
; !IS_ROOT(p
); p
= p
->d_parent
) {
1940 if (p
->d_parent
== p1
)
1947 * This helper attempts to cope with remotely renamed directories
1949 * It assumes that the caller is already holding
1950 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1952 * Note: If ever the locking in lock_rename() changes, then please
1953 * remember to update this too...
1955 static struct dentry
*__d_unalias(struct dentry
*dentry
, struct dentry
*alias
)
1956 __releases(dcache_lock
)
1958 struct mutex
*m1
= NULL
, *m2
= NULL
;
1961 /* If alias and dentry share a parent, then no extra locks required */
1962 if (alias
->d_parent
== dentry
->d_parent
)
1965 /* Check for loops */
1966 ret
= ERR_PTR(-ELOOP
);
1967 if (d_ancestor(alias
, dentry
))
1970 /* See lock_rename() */
1971 ret
= ERR_PTR(-EBUSY
);
1972 if (!mutex_trylock(&dentry
->d_sb
->s_vfs_rename_mutex
))
1974 m1
= &dentry
->d_sb
->s_vfs_rename_mutex
;
1975 if (!mutex_trylock(&alias
->d_parent
->d_inode
->i_mutex
))
1977 m2
= &alias
->d_parent
->d_inode
->i_mutex
;
1979 d_move_locked(alias
, dentry
);
1982 spin_unlock(&dcache_lock
);
1991 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1992 * named dentry in place of the dentry to be replaced.
1994 static void __d_materialise_dentry(struct dentry
*dentry
, struct dentry
*anon
)
1996 struct dentry
*dparent
, *aparent
;
1998 switch_names(dentry
, anon
);
1999 swap(dentry
->d_name
.hash
, anon
->d_name
.hash
);
2001 dparent
= dentry
->d_parent
;
2002 aparent
= anon
->d_parent
;
2004 dentry
->d_parent
= (aparent
== anon
) ? dentry
: aparent
;
2005 list_del(&dentry
->d_u
.d_child
);
2006 if (!IS_ROOT(dentry
))
2007 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
2009 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
2011 anon
->d_parent
= (dparent
== dentry
) ? anon
: dparent
;
2012 list_del(&anon
->d_u
.d_child
);
2014 list_add(&anon
->d_u
.d_child
, &anon
->d_parent
->d_subdirs
);
2016 INIT_LIST_HEAD(&anon
->d_u
.d_child
);
2018 anon
->d_flags
&= ~DCACHE_DISCONNECTED
;
2022 * d_materialise_unique - introduce an inode into the tree
2023 * @dentry: candidate dentry
2024 * @inode: inode to bind to the dentry, to which aliases may be attached
2026 * Introduces an dentry into the tree, substituting an extant disconnected
2027 * root directory alias in its place if there is one
2029 struct dentry
*d_materialise_unique(struct dentry
*dentry
, struct inode
*inode
)
2031 struct dentry
*actual
;
2033 BUG_ON(!d_unhashed(dentry
));
2035 spin_lock(&dcache_lock
);
2039 __d_instantiate(dentry
, NULL
);
2043 if (S_ISDIR(inode
->i_mode
)) {
2044 struct dentry
*alias
;
2046 /* Does an aliased dentry already exist? */
2047 alias
= __d_find_alias(inode
, 0);
2050 /* Is this an anonymous mountpoint that we could splice
2052 if (IS_ROOT(alias
)) {
2053 spin_lock(&alias
->d_lock
);
2054 __d_materialise_dentry(dentry
, alias
);
2058 /* Nope, but we must(!) avoid directory aliasing */
2059 actual
= __d_unalias(dentry
, alias
);
2066 /* Add a unique reference */
2067 actual
= __d_instantiate_unique(dentry
, inode
);
2070 else if (unlikely(!d_unhashed(actual
)))
2071 goto shouldnt_be_hashed
;
2074 spin_lock(&actual
->d_lock
);
2076 spin_lock(&dcache_hash_lock
);
2078 spin_unlock(&dcache_hash_lock
);
2079 spin_unlock(&actual
->d_lock
);
2080 spin_unlock(&dcache_lock
);
2082 if (actual
== dentry
) {
2083 security_d_instantiate(dentry
, inode
);
2091 spin_unlock(&dcache_lock
);
2094 EXPORT_SYMBOL_GPL(d_materialise_unique
);
2096 static int prepend(char **buffer
, int *buflen
, const char *str
, int namelen
)
2100 return -ENAMETOOLONG
;
2102 memcpy(*buffer
, str
, namelen
);
2106 static int prepend_name(char **buffer
, int *buflen
, struct qstr
*name
)
2108 return prepend(buffer
, buflen
, name
->name
, name
->len
);
2112 * Prepend path string to a buffer
2114 * @path: the dentry/vfsmount to report
2115 * @root: root vfsmnt/dentry (may be modified by this function)
2116 * @buffer: pointer to the end of the buffer
2117 * @buflen: pointer to buffer length
2119 * Caller holds the dcache_lock.
2121 * If path is not reachable from the supplied root, then the value of
2122 * root is changed (without modifying refcounts).
2124 static int prepend_path(const struct path
*path
, struct path
*root
,
2125 char **buffer
, int *buflen
)
2127 struct dentry
*dentry
= path
->dentry
;
2128 struct vfsmount
*vfsmnt
= path
->mnt
;
2132 br_read_lock(vfsmount_lock
);
2133 while (dentry
!= root
->dentry
|| vfsmnt
!= root
->mnt
) {
2134 struct dentry
* parent
;
2136 if (dentry
== vfsmnt
->mnt_root
|| IS_ROOT(dentry
)) {
2138 if (vfsmnt
->mnt_parent
== vfsmnt
) {
2141 dentry
= vfsmnt
->mnt_mountpoint
;
2142 vfsmnt
= vfsmnt
->mnt_parent
;
2145 parent
= dentry
->d_parent
;
2147 error
= prepend_name(buffer
, buflen
, &dentry
->d_name
);
2149 error
= prepend(buffer
, buflen
, "/", 1);
2158 if (!error
&& !slash
)
2159 error
= prepend(buffer
, buflen
, "/", 1);
2161 br_read_unlock(vfsmount_lock
);
2166 * Filesystems needing to implement special "root names"
2167 * should do so with ->d_dname()
2169 if (IS_ROOT(dentry
) &&
2170 (dentry
->d_name
.len
!= 1 || dentry
->d_name
.name
[0] != '/')) {
2171 WARN(1, "Root dentry has weird name <%.*s>\n",
2172 (int) dentry
->d_name
.len
, dentry
->d_name
.name
);
2175 root
->dentry
= dentry
;
2180 * __d_path - return the path of a dentry
2181 * @path: the dentry/vfsmount to report
2182 * @root: root vfsmnt/dentry (may be modified by this function)
2183 * @buf: buffer to return value in
2184 * @buflen: buffer length
2186 * Convert a dentry into an ASCII path name.
2188 * Returns a pointer into the buffer or an error code if the
2189 * path was too long.
2191 * "buflen" should be positive.
2193 * If path is not reachable from the supplied root, then the value of
2194 * root is changed (without modifying refcounts).
2196 char *__d_path(const struct path
*path
, struct path
*root
,
2197 char *buf
, int buflen
)
2199 char *res
= buf
+ buflen
;
2202 prepend(&res
, &buflen
, "\0", 1);
2203 spin_lock(&dcache_lock
);
2204 error
= prepend_path(path
, root
, &res
, &buflen
);
2205 spin_unlock(&dcache_lock
);
2208 return ERR_PTR(error
);
2213 * same as __d_path but appends "(deleted)" for unlinked files.
2215 static int path_with_deleted(const struct path
*path
, struct path
*root
,
2216 char **buf
, int *buflen
)
2218 prepend(buf
, buflen
, "\0", 1);
2219 if (d_unlinked(path
->dentry
)) {
2220 int error
= prepend(buf
, buflen
, " (deleted)", 10);
2225 return prepend_path(path
, root
, buf
, buflen
);
2228 static int prepend_unreachable(char **buffer
, int *buflen
)
2230 return prepend(buffer
, buflen
, "(unreachable)", 13);
2234 * d_path - return the path of a dentry
2235 * @path: path to report
2236 * @buf: buffer to return value in
2237 * @buflen: buffer length
2239 * Convert a dentry into an ASCII path name. If the entry has been deleted
2240 * the string " (deleted)" is appended. Note that this is ambiguous.
2242 * Returns a pointer into the buffer or an error code if the path was
2243 * too long. Note: Callers should use the returned pointer, not the passed
2244 * in buffer, to use the name! The implementation often starts at an offset
2245 * into the buffer, and may leave 0 bytes at the start.
2247 * "buflen" should be positive.
2249 char *d_path(const struct path
*path
, char *buf
, int buflen
)
2251 char *res
= buf
+ buflen
;
2257 * We have various synthetic filesystems that never get mounted. On
2258 * these filesystems dentries are never used for lookup purposes, and
2259 * thus don't need to be hashed. They also don't need a name until a
2260 * user wants to identify the object in /proc/pid/fd/. The little hack
2261 * below allows us to generate a name for these objects on demand:
2263 if (path
->dentry
->d_op
&& path
->dentry
->d_op
->d_dname
)
2264 return path
->dentry
->d_op
->d_dname(path
->dentry
, buf
, buflen
);
2266 get_fs_root(current
->fs
, &root
);
2267 spin_lock(&dcache_lock
);
2269 error
= path_with_deleted(path
, &tmp
, &res
, &buflen
);
2271 res
= ERR_PTR(error
);
2272 spin_unlock(&dcache_lock
);
2276 EXPORT_SYMBOL(d_path
);
2279 * d_path_with_unreachable - return the path of a dentry
2280 * @path: path to report
2281 * @buf: buffer to return value in
2282 * @buflen: buffer length
2284 * The difference from d_path() is that this prepends "(unreachable)"
2285 * to paths which are unreachable from the current process' root.
2287 char *d_path_with_unreachable(const struct path
*path
, char *buf
, int buflen
)
2289 char *res
= buf
+ buflen
;
2294 if (path
->dentry
->d_op
&& path
->dentry
->d_op
->d_dname
)
2295 return path
->dentry
->d_op
->d_dname(path
->dentry
, buf
, buflen
);
2297 get_fs_root(current
->fs
, &root
);
2298 spin_lock(&dcache_lock
);
2300 error
= path_with_deleted(path
, &tmp
, &res
, &buflen
);
2301 if (!error
&& !path_equal(&tmp
, &root
))
2302 error
= prepend_unreachable(&res
, &buflen
);
2303 spin_unlock(&dcache_lock
);
2306 res
= ERR_PTR(error
);
2312 * Helper function for dentry_operations.d_dname() members
2314 char *dynamic_dname(struct dentry
*dentry
, char *buffer
, int buflen
,
2315 const char *fmt
, ...)
2321 va_start(args
, fmt
);
2322 sz
= vsnprintf(temp
, sizeof(temp
), fmt
, args
) + 1;
2325 if (sz
> sizeof(temp
) || sz
> buflen
)
2326 return ERR_PTR(-ENAMETOOLONG
);
2328 buffer
+= buflen
- sz
;
2329 return memcpy(buffer
, temp
, sz
);
2333 * Write full pathname from the root of the filesystem into the buffer.
2335 static char *__dentry_path(struct dentry
*dentry
, char *buf
, int buflen
)
2337 char *end
= buf
+ buflen
;
2340 prepend(&end
, &buflen
, "\0", 1);
2347 while (!IS_ROOT(dentry
)) {
2348 struct dentry
*parent
= dentry
->d_parent
;
2351 if ((prepend_name(&end
, &buflen
, &dentry
->d_name
) != 0) ||
2352 (prepend(&end
, &buflen
, "/", 1) != 0))
2360 return ERR_PTR(-ENAMETOOLONG
);
2363 char *dentry_path_raw(struct dentry
*dentry
, char *buf
, int buflen
)
2367 spin_lock(&dcache_lock
);
2368 retval
= __dentry_path(dentry
, buf
, buflen
);
2369 spin_unlock(&dcache_lock
);
2373 EXPORT_SYMBOL(dentry_path_raw
);
2375 char *dentry_path(struct dentry
*dentry
, char *buf
, int buflen
)
2380 spin_lock(&dcache_lock
);
2381 if (d_unlinked(dentry
)) {
2383 if (prepend(&p
, &buflen
, "//deleted", 10) != 0)
2387 retval
= __dentry_path(dentry
, buf
, buflen
);
2388 spin_unlock(&dcache_lock
);
2389 if (!IS_ERR(retval
) && p
)
2390 *p
= '/'; /* restore '/' overriden with '\0' */
2393 spin_unlock(&dcache_lock
);
2394 return ERR_PTR(-ENAMETOOLONG
);
2398 * NOTE! The user-level library version returns a
2399 * character pointer. The kernel system call just
2400 * returns the length of the buffer filled (which
2401 * includes the ending '\0' character), or a negative
2402 * error value. So libc would do something like
2404 * char *getcwd(char * buf, size_t size)
2408 * retval = sys_getcwd(buf, size);
2415 SYSCALL_DEFINE2(getcwd
, char __user
*, buf
, unsigned long, size
)
2418 struct path pwd
, root
;
2419 char *page
= (char *) __get_free_page(GFP_USER
);
2424 get_fs_root_and_pwd(current
->fs
, &root
, &pwd
);
2427 spin_lock(&dcache_lock
);
2428 if (!d_unlinked(pwd
.dentry
)) {
2430 struct path tmp
= root
;
2431 char *cwd
= page
+ PAGE_SIZE
;
2432 int buflen
= PAGE_SIZE
;
2434 prepend(&cwd
, &buflen
, "\0", 1);
2435 error
= prepend_path(&pwd
, &tmp
, &cwd
, &buflen
);
2436 spin_unlock(&dcache_lock
);
2441 /* Unreachable from current root */
2442 if (!path_equal(&tmp
, &root
)) {
2443 error
= prepend_unreachable(&cwd
, &buflen
);
2449 len
= PAGE_SIZE
+ page
- cwd
;
2452 if (copy_to_user(buf
, cwd
, len
))
2456 spin_unlock(&dcache_lock
);
2461 free_page((unsigned long) page
);
2466 * Test whether new_dentry is a subdirectory of old_dentry.
2468 * Trivially implemented using the dcache structure
2472 * is_subdir - is new dentry a subdirectory of old_dentry
2473 * @new_dentry: new dentry
2474 * @old_dentry: old dentry
2476 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2477 * Returns 0 otherwise.
2478 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2481 int is_subdir(struct dentry
*new_dentry
, struct dentry
*old_dentry
)
2486 if (new_dentry
== old_dentry
)
2490 * Need rcu_readlock to protect against the d_parent trashing
2495 /* for restarting inner loop in case of seq retry */
2496 seq
= read_seqbegin(&rename_lock
);
2497 if (d_ancestor(old_dentry
, new_dentry
))
2501 } while (read_seqretry(&rename_lock
, seq
));
2507 int path_is_under(struct path
*path1
, struct path
*path2
)
2509 struct vfsmount
*mnt
= path1
->mnt
;
2510 struct dentry
*dentry
= path1
->dentry
;
2513 br_read_lock(vfsmount_lock
);
2514 if (mnt
!= path2
->mnt
) {
2516 if (mnt
->mnt_parent
== mnt
) {
2517 br_read_unlock(vfsmount_lock
);
2520 if (mnt
->mnt_parent
== path2
->mnt
)
2522 mnt
= mnt
->mnt_parent
;
2524 dentry
= mnt
->mnt_mountpoint
;
2526 res
= is_subdir(dentry
, path2
->dentry
);
2527 br_read_unlock(vfsmount_lock
);
2530 EXPORT_SYMBOL(path_is_under
);
2532 void d_genocide(struct dentry
*root
)
2534 struct dentry
*this_parent
= root
;
2535 struct list_head
*next
;
2537 spin_lock(&dcache_lock
);
2539 next
= this_parent
->d_subdirs
.next
;
2541 while (next
!= &this_parent
->d_subdirs
) {
2542 struct list_head
*tmp
= next
;
2543 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
2545 if (d_unhashed(dentry
)||!dentry
->d_inode
)
2547 if (!list_empty(&dentry
->d_subdirs
)) {
2548 this_parent
= dentry
;
2551 spin_lock(&dentry
->d_lock
);
2553 spin_unlock(&dentry
->d_lock
);
2555 if (this_parent
!= root
) {
2556 next
= this_parent
->d_u
.d_child
.next
;
2557 spin_lock(&this_parent
->d_lock
);
2558 this_parent
->d_count
--;
2559 spin_unlock(&this_parent
->d_lock
);
2560 this_parent
= this_parent
->d_parent
;
2563 spin_unlock(&dcache_lock
);
2567 * find_inode_number - check for dentry with name
2568 * @dir: directory to check
2569 * @name: Name to find.
2571 * Check whether a dentry already exists for the given name,
2572 * and return the inode number if it has an inode. Otherwise
2575 * This routine is used to post-process directory listings for
2576 * filesystems using synthetic inode numbers, and is necessary
2577 * to keep getcwd() working.
2580 ino_t
find_inode_number(struct dentry
*dir
, struct qstr
*name
)
2582 struct dentry
* dentry
;
2585 dentry
= d_hash_and_lookup(dir
, name
);
2587 if (dentry
->d_inode
)
2588 ino
= dentry
->d_inode
->i_ino
;
2593 EXPORT_SYMBOL(find_inode_number
);
2595 static __initdata
unsigned long dhash_entries
;
2596 static int __init
set_dhash_entries(char *str
)
2600 dhash_entries
= simple_strtoul(str
, &str
, 0);
2603 __setup("dhash_entries=", set_dhash_entries
);
2605 static void __init
dcache_init_early(void)
2609 /* If hashes are distributed across NUMA nodes, defer
2610 * hash allocation until vmalloc space is available.
2616 alloc_large_system_hash("Dentry cache",
2617 sizeof(struct hlist_head
),
2625 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2626 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2629 static void __init
dcache_init(void)
2634 * A constructor could be added for stable state like the lists,
2635 * but it is probably not worth it because of the cache nature
2638 dentry_cache
= KMEM_CACHE(dentry
,
2639 SLAB_RECLAIM_ACCOUNT
|SLAB_PANIC
|SLAB_MEM_SPREAD
);
2641 register_shrinker(&dcache_shrinker
);
2643 /* Hash may have been set up in dcache_init_early */
2648 alloc_large_system_hash("Dentry cache",
2649 sizeof(struct hlist_head
),
2657 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2658 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2661 /* SLAB cache for __getname() consumers */
2662 struct kmem_cache
*names_cachep __read_mostly
;
2663 EXPORT_SYMBOL(names_cachep
);
2665 EXPORT_SYMBOL(d_genocide
);
2667 void __init
vfs_caches_init_early(void)
2669 dcache_init_early();
2673 void __init
vfs_caches_init(unsigned long mempages
)
2675 unsigned long reserve
;
2677 /* Base hash sizes on available memory, with a reserve equal to
2678 150% of current kernel size */
2680 reserve
= min((mempages
- nr_free_pages()) * 3/2, mempages
- 1);
2681 mempages
-= reserve
;
2683 names_cachep
= kmem_cache_create("names_cache", PATH_MAX
, 0,
2684 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
2688 files_init(mempages
);