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>
38 int sysctl_vfs_cache_pressure __read_mostly
= 100;
39 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure
);
41 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dcache_lock
);
42 __cacheline_aligned_in_smp
DEFINE_SEQLOCK(rename_lock
);
44 EXPORT_SYMBOL(dcache_lock
);
46 static struct kmem_cache
*dentry_cache __read_mostly
;
48 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
51 * This is the single most critical data structure when it comes
52 * to the dcache: the hashtable for lookups. Somebody should try
53 * to make this good - I've just made it work.
55 * This hash-function tries to avoid losing too many bits of hash
56 * information, yet avoid using a prime hash-size or similar.
58 #define D_HASHBITS d_hash_shift
59 #define D_HASHMASK d_hash_mask
61 static unsigned int d_hash_mask __read_mostly
;
62 static unsigned int d_hash_shift __read_mostly
;
63 static struct hlist_head
*dentry_hashtable __read_mostly
;
65 /* Statistics gathering. */
66 struct dentry_stat_t dentry_stat
= {
70 static struct percpu_counter nr_dentry __cacheline_aligned_in_smp
;
72 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
73 int proc_nr_dentry(ctl_table
*table
, int write
, void __user
*buffer
,
74 size_t *lenp
, loff_t
*ppos
)
76 dentry_stat
.nr_dentry
= percpu_counter_sum_positive(&nr_dentry
);
77 return proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
81 static void __d_free(struct rcu_head
*head
)
83 struct dentry
*dentry
= container_of(head
, struct dentry
, d_u
.d_rcu
);
85 WARN_ON(!list_empty(&dentry
->d_alias
));
86 if (dname_external(dentry
))
87 kfree(dentry
->d_name
.name
);
88 kmem_cache_free(dentry_cache
, dentry
);
92 * no dcache_lock, please.
94 static void d_free(struct dentry
*dentry
)
96 percpu_counter_dec(&nr_dentry
);
97 if (dentry
->d_op
&& dentry
->d_op
->d_release
)
98 dentry
->d_op
->d_release(dentry
);
100 /* if dentry was never inserted into hash, immediate free is OK */
101 if (hlist_unhashed(&dentry
->d_hash
))
102 __d_free(&dentry
->d_u
.d_rcu
);
104 call_rcu(&dentry
->d_u
.d_rcu
, __d_free
);
108 * Release the dentry's inode, using the filesystem
109 * d_iput() operation if defined.
111 static void dentry_iput(struct dentry
* dentry
)
112 __releases(dentry
->d_lock
)
113 __releases(dcache_lock
)
115 struct inode
*inode
= dentry
->d_inode
;
117 dentry
->d_inode
= NULL
;
118 list_del_init(&dentry
->d_alias
);
119 spin_unlock(&dentry
->d_lock
);
120 spin_unlock(&dcache_lock
);
122 fsnotify_inoderemove(inode
);
123 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
124 dentry
->d_op
->d_iput(dentry
, inode
);
128 spin_unlock(&dentry
->d_lock
);
129 spin_unlock(&dcache_lock
);
134 * dentry_lru_(add|del|move_tail) must be called with dcache_lock held.
136 static void dentry_lru_add(struct dentry
*dentry
)
138 if (list_empty(&dentry
->d_lru
)) {
139 list_add(&dentry
->d_lru
, &dentry
->d_sb
->s_dentry_lru
);
140 dentry
->d_sb
->s_nr_dentry_unused
++;
141 dentry_stat
.nr_unused
++;
145 static void dentry_lru_del(struct dentry
*dentry
)
147 if (!list_empty(&dentry
->d_lru
)) {
148 list_del_init(&dentry
->d_lru
);
149 dentry
->d_sb
->s_nr_dentry_unused
--;
150 dentry_stat
.nr_unused
--;
154 static void dentry_lru_move_tail(struct dentry
*dentry
)
156 if (list_empty(&dentry
->d_lru
)) {
157 list_add_tail(&dentry
->d_lru
, &dentry
->d_sb
->s_dentry_lru
);
158 dentry
->d_sb
->s_nr_dentry_unused
++;
159 dentry_stat
.nr_unused
++;
161 list_move_tail(&dentry
->d_lru
, &dentry
->d_sb
->s_dentry_lru
);
166 * d_kill - kill dentry and return parent
167 * @dentry: dentry to kill
169 * The dentry must already be unhashed and removed from the LRU.
171 * If this is the root of the dentry tree, return NULL.
173 static struct dentry
*d_kill(struct dentry
*dentry
)
174 __releases(dentry
->d_lock
)
175 __releases(dcache_lock
)
177 struct dentry
*parent
;
179 list_del(&dentry
->d_u
.d_child
);
180 /*drops the locks, at that point nobody can reach this dentry */
185 parent
= dentry
->d_parent
;
193 * This is complicated by the fact that we do not want to put
194 * dentries that are no longer on any hash chain on the unused
195 * list: we'd much rather just get rid of them immediately.
197 * However, that implies that we have to traverse the dentry
198 * tree upwards to the parents which might _also_ now be
199 * scheduled for deletion (it may have been only waiting for
200 * its last child to go away).
202 * This tail recursion is done by hand as we don't want to depend
203 * on the compiler to always get this right (gcc generally doesn't).
204 * Real recursion would eat up our stack space.
208 * dput - release a dentry
209 * @dentry: dentry to release
211 * Release a dentry. This will drop the usage count and if appropriate
212 * call the dentry unlink method as well as removing it from the queues and
213 * releasing its resources. If the parent dentries were scheduled for release
214 * they too may now get deleted.
216 * no dcache lock, please.
219 void dput(struct dentry
*dentry
)
225 if (atomic_read(&dentry
->d_count
) == 1)
227 if (!atomic_dec_and_lock(&dentry
->d_count
, &dcache_lock
))
230 spin_lock(&dentry
->d_lock
);
231 if (atomic_read(&dentry
->d_count
)) {
232 spin_unlock(&dentry
->d_lock
);
233 spin_unlock(&dcache_lock
);
238 * AV: ->d_delete() is _NOT_ allowed to block now.
240 if (dentry
->d_op
&& dentry
->d_op
->d_delete
) {
241 if (dentry
->d_op
->d_delete(dentry
))
245 /* Unreachable? Get rid of it */
246 if (d_unhashed(dentry
))
249 /* Otherwise leave it cached and ensure it's on the LRU */
250 dentry
->d_flags
|= DCACHE_REFERENCED
;
251 dentry_lru_add(dentry
);
253 spin_unlock(&dentry
->d_lock
);
254 spin_unlock(&dcache_lock
);
260 /* if dentry was on the d_lru list delete it from there */
261 dentry_lru_del(dentry
);
262 dentry
= d_kill(dentry
);
269 * d_invalidate - invalidate a dentry
270 * @dentry: dentry to invalidate
272 * Try to invalidate the dentry if it turns out to be
273 * possible. If there are other dentries that can be
274 * reached through this one we can't delete it and we
275 * return -EBUSY. On success we return 0.
280 int d_invalidate(struct dentry
* dentry
)
283 * If it's already been dropped, return OK.
285 spin_lock(&dcache_lock
);
286 if (d_unhashed(dentry
)) {
287 spin_unlock(&dcache_lock
);
291 * Check whether to do a partial shrink_dcache
292 * to get rid of unused child entries.
294 if (!list_empty(&dentry
->d_subdirs
)) {
295 spin_unlock(&dcache_lock
);
296 shrink_dcache_parent(dentry
);
297 spin_lock(&dcache_lock
);
301 * Somebody else still using it?
303 * If it's a directory, we can't drop it
304 * for fear of somebody re-populating it
305 * with children (even though dropping it
306 * would make it unreachable from the root,
307 * we might still populate it if it was a
308 * working directory or similar).
310 spin_lock(&dentry
->d_lock
);
311 if (atomic_read(&dentry
->d_count
) > 1) {
312 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
)) {
313 spin_unlock(&dentry
->d_lock
);
314 spin_unlock(&dcache_lock
);
320 spin_unlock(&dentry
->d_lock
);
321 spin_unlock(&dcache_lock
);
324 EXPORT_SYMBOL(d_invalidate
);
326 /* This should be called _only_ with dcache_lock held */
327 static inline struct dentry
* __dget_locked(struct dentry
*dentry
)
329 atomic_inc(&dentry
->d_count
);
330 dentry_lru_del(dentry
);
334 struct dentry
* dget_locked(struct dentry
*dentry
)
336 return __dget_locked(dentry
);
338 EXPORT_SYMBOL(dget_locked
);
341 * d_find_alias - grab a hashed alias of inode
342 * @inode: inode in question
343 * @want_discon: flag, used by d_splice_alias, to request
344 * that only a DISCONNECTED alias be returned.
346 * If inode has a hashed alias, or is a directory and has any alias,
347 * acquire the reference to alias and return it. Otherwise return NULL.
348 * Notice that if inode is a directory there can be only one alias and
349 * it can be unhashed only if it has no children, or if it is the root
352 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
353 * any other hashed alias over that one unless @want_discon is set,
354 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
357 static struct dentry
* __d_find_alias(struct inode
*inode
, int want_discon
)
359 struct list_head
*head
, *next
, *tmp
;
360 struct dentry
*alias
, *discon_alias
=NULL
;
362 head
= &inode
->i_dentry
;
363 next
= inode
->i_dentry
.next
;
364 while (next
!= head
) {
368 alias
= list_entry(tmp
, struct dentry
, d_alias
);
369 if (S_ISDIR(inode
->i_mode
) || !d_unhashed(alias
)) {
370 if (IS_ROOT(alias
) &&
371 (alias
->d_flags
& DCACHE_DISCONNECTED
))
372 discon_alias
= alias
;
373 else if (!want_discon
) {
374 __dget_locked(alias
);
380 __dget_locked(discon_alias
);
384 struct dentry
* d_find_alias(struct inode
*inode
)
386 struct dentry
*de
= NULL
;
388 if (!list_empty(&inode
->i_dentry
)) {
389 spin_lock(&dcache_lock
);
390 de
= __d_find_alias(inode
, 0);
391 spin_unlock(&dcache_lock
);
395 EXPORT_SYMBOL(d_find_alias
);
398 * Try to kill dentries associated with this inode.
399 * WARNING: you must own a reference to inode.
401 void d_prune_aliases(struct inode
*inode
)
403 struct dentry
*dentry
;
405 spin_lock(&dcache_lock
);
406 list_for_each_entry(dentry
, &inode
->i_dentry
, d_alias
) {
407 spin_lock(&dentry
->d_lock
);
408 if (!atomic_read(&dentry
->d_count
)) {
409 __dget_locked(dentry
);
411 spin_unlock(&dentry
->d_lock
);
412 spin_unlock(&dcache_lock
);
416 spin_unlock(&dentry
->d_lock
);
418 spin_unlock(&dcache_lock
);
420 EXPORT_SYMBOL(d_prune_aliases
);
423 * Throw away a dentry - free the inode, dput the parent. This requires that
424 * the LRU list has already been removed.
426 * Try to prune ancestors as well. This is necessary to prevent
427 * quadratic behavior of shrink_dcache_parent(), but is also expected
428 * to be beneficial in reducing dentry cache fragmentation.
430 static void prune_one_dentry(struct dentry
* dentry
)
431 __releases(dentry
->d_lock
)
432 __releases(dcache_lock
)
433 __acquires(dcache_lock
)
436 dentry
= d_kill(dentry
);
439 * Prune ancestors. Locking is simpler than in dput(),
440 * because dcache_lock needs to be taken anyway.
442 spin_lock(&dcache_lock
);
444 if (!atomic_dec_and_lock(&dentry
->d_count
, &dentry
->d_lock
))
447 if (dentry
->d_op
&& dentry
->d_op
->d_delete
)
448 dentry
->d_op
->d_delete(dentry
);
449 dentry_lru_del(dentry
);
451 dentry
= d_kill(dentry
);
452 spin_lock(&dcache_lock
);
456 static void shrink_dentry_list(struct list_head
*list
)
458 struct dentry
*dentry
;
460 while (!list_empty(list
)) {
461 dentry
= list_entry(list
->prev
, struct dentry
, d_lru
);
462 dentry_lru_del(dentry
);
465 * We found an inuse dentry which was not removed from
466 * the LRU because of laziness during lookup. Do not free
467 * it - just keep it off the LRU list.
469 spin_lock(&dentry
->d_lock
);
470 if (atomic_read(&dentry
->d_count
)) {
471 spin_unlock(&dentry
->d_lock
);
474 prune_one_dentry(dentry
);
475 /* dentry->d_lock was dropped in prune_one_dentry() */
476 cond_resched_lock(&dcache_lock
);
481 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
482 * @sb: superblock to shrink dentry LRU.
483 * @count: number of entries to prune
484 * @flags: flags to control the dentry processing
486 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
488 static void __shrink_dcache_sb(struct super_block
*sb
, int *count
, int flags
)
490 /* called from prune_dcache() and shrink_dcache_parent() */
491 struct dentry
*dentry
;
492 LIST_HEAD(referenced
);
496 spin_lock(&dcache_lock
);
497 while (!list_empty(&sb
->s_dentry_lru
)) {
498 dentry
= list_entry(sb
->s_dentry_lru
.prev
,
499 struct dentry
, d_lru
);
500 BUG_ON(dentry
->d_sb
!= sb
);
503 * If we are honouring the DCACHE_REFERENCED flag and the
504 * dentry has this flag set, don't free it. Clear the flag
505 * and put it back on the LRU.
507 if (flags
& DCACHE_REFERENCED
) {
508 spin_lock(&dentry
->d_lock
);
509 if (dentry
->d_flags
& DCACHE_REFERENCED
) {
510 dentry
->d_flags
&= ~DCACHE_REFERENCED
;
511 list_move(&dentry
->d_lru
, &referenced
);
512 spin_unlock(&dentry
->d_lock
);
513 cond_resched_lock(&dcache_lock
);
516 spin_unlock(&dentry
->d_lock
);
519 list_move_tail(&dentry
->d_lru
, &tmp
);
522 cond_resched_lock(&dcache_lock
);
526 shrink_dentry_list(&tmp
);
528 if (!list_empty(&referenced
))
529 list_splice(&referenced
, &sb
->s_dentry_lru
);
530 spin_unlock(&dcache_lock
);
535 * prune_dcache - shrink the dcache
536 * @count: number of entries to try to free
538 * Shrink the dcache. This is done when we need more memory, or simply when we
539 * need to unmount something (at which point we need to unuse all dentries).
541 * This function may fail to free any resources if all the dentries are in use.
543 static void prune_dcache(int count
)
545 struct super_block
*sb
, *p
= NULL
;
547 int unused
= dentry_stat
.nr_unused
;
551 if (unused
== 0 || count
== 0)
553 spin_lock(&dcache_lock
);
557 prune_ratio
= unused
/ count
;
559 list_for_each_entry(sb
, &super_blocks
, s_list
) {
560 if (list_empty(&sb
->s_instances
))
562 if (sb
->s_nr_dentry_unused
== 0)
565 /* Now, we reclaim unused dentrins with fairness.
566 * We reclaim them same percentage from each superblock.
567 * We calculate number of dentries to scan on this sb
568 * as follows, but the implementation is arranged to avoid
570 * number of dentries to scan on this sb =
571 * count * (number of dentries on this sb /
572 * number of dentries in the machine)
574 spin_unlock(&sb_lock
);
575 if (prune_ratio
!= 1)
576 w_count
= (sb
->s_nr_dentry_unused
/ prune_ratio
) + 1;
578 w_count
= sb
->s_nr_dentry_unused
;
581 * We need to be sure this filesystem isn't being unmounted,
582 * otherwise we could race with generic_shutdown_super(), and
583 * end up holding a reference to an inode while the filesystem
584 * is unmounted. So we try to get s_umount, and make sure
587 if (down_read_trylock(&sb
->s_umount
)) {
588 if ((sb
->s_root
!= NULL
) &&
589 (!list_empty(&sb
->s_dentry_lru
))) {
590 spin_unlock(&dcache_lock
);
591 __shrink_dcache_sb(sb
, &w_count
,
594 spin_lock(&dcache_lock
);
596 up_read(&sb
->s_umount
);
603 /* more work left to do? */
609 spin_unlock(&sb_lock
);
610 spin_unlock(&dcache_lock
);
614 * shrink_dcache_sb - shrink dcache for a superblock
617 * Shrink the dcache for the specified super block. This is used to free
618 * the dcache before unmounting a file system.
620 void shrink_dcache_sb(struct super_block
*sb
)
624 spin_lock(&dcache_lock
);
625 while (!list_empty(&sb
->s_dentry_lru
)) {
626 list_splice_init(&sb
->s_dentry_lru
, &tmp
);
627 shrink_dentry_list(&tmp
);
629 spin_unlock(&dcache_lock
);
631 EXPORT_SYMBOL(shrink_dcache_sb
);
634 * destroy a single subtree of dentries for unmount
635 * - see the comments on shrink_dcache_for_umount() for a description of the
638 static void shrink_dcache_for_umount_subtree(struct dentry
*dentry
)
640 struct dentry
*parent
;
641 unsigned detached
= 0;
643 BUG_ON(!IS_ROOT(dentry
));
645 /* detach this root from the system */
646 spin_lock(&dcache_lock
);
647 dentry_lru_del(dentry
);
649 spin_unlock(&dcache_lock
);
652 /* descend to the first leaf in the current subtree */
653 while (!list_empty(&dentry
->d_subdirs
)) {
656 /* this is a branch with children - detach all of them
657 * from the system in one go */
658 spin_lock(&dcache_lock
);
659 list_for_each_entry(loop
, &dentry
->d_subdirs
,
661 dentry_lru_del(loop
);
663 cond_resched_lock(&dcache_lock
);
665 spin_unlock(&dcache_lock
);
667 /* move to the first child */
668 dentry
= list_entry(dentry
->d_subdirs
.next
,
669 struct dentry
, d_u
.d_child
);
672 /* consume the dentries from this leaf up through its parents
673 * until we find one with children or run out altogether */
677 if (atomic_read(&dentry
->d_count
) != 0) {
679 "BUG: Dentry %p{i=%lx,n=%s}"
681 " [unmount of %s %s]\n",
684 dentry
->d_inode
->i_ino
: 0UL,
686 atomic_read(&dentry
->d_count
),
687 dentry
->d_sb
->s_type
->name
,
695 parent
= dentry
->d_parent
;
696 atomic_dec(&parent
->d_count
);
699 list_del(&dentry
->d_u
.d_child
);
702 inode
= dentry
->d_inode
;
704 dentry
->d_inode
= NULL
;
705 list_del_init(&dentry
->d_alias
);
706 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
707 dentry
->d_op
->d_iput(dentry
, inode
);
714 /* finished when we fall off the top of the tree,
715 * otherwise we ascend to the parent and move to the
716 * next sibling if there is one */
720 } while (list_empty(&dentry
->d_subdirs
));
722 dentry
= list_entry(dentry
->d_subdirs
.next
,
723 struct dentry
, d_u
.d_child
);
728 * destroy the dentries attached to a superblock on unmounting
729 * - we don't need to use dentry->d_lock, and only need dcache_lock when
730 * removing the dentry from the system lists and hashes because:
731 * - the superblock is detached from all mountings and open files, so the
732 * dentry trees will not be rearranged by the VFS
733 * - s_umount is write-locked, so the memory pressure shrinker will ignore
734 * any dentries belonging to this superblock that it comes across
735 * - the filesystem itself is no longer permitted to rearrange the dentries
738 void shrink_dcache_for_umount(struct super_block
*sb
)
740 struct dentry
*dentry
;
742 if (down_read_trylock(&sb
->s_umount
))
747 atomic_dec(&dentry
->d_count
);
748 shrink_dcache_for_umount_subtree(dentry
);
750 while (!hlist_empty(&sb
->s_anon
)) {
751 dentry
= hlist_entry(sb
->s_anon
.first
, struct dentry
, d_hash
);
752 shrink_dcache_for_umount_subtree(dentry
);
757 * Search for at least 1 mount point in the dentry's subdirs.
758 * We descend to the next level whenever the d_subdirs
759 * list is non-empty and continue searching.
763 * have_submounts - check for mounts over a dentry
764 * @parent: dentry to check.
766 * Return true if the parent or its subdirectories contain
770 int have_submounts(struct dentry
*parent
)
772 struct dentry
*this_parent
= parent
;
773 struct list_head
*next
;
775 spin_lock(&dcache_lock
);
776 if (d_mountpoint(parent
))
779 next
= this_parent
->d_subdirs
.next
;
781 while (next
!= &this_parent
->d_subdirs
) {
782 struct list_head
*tmp
= next
;
783 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
785 /* Have we found a mount point ? */
786 if (d_mountpoint(dentry
))
788 if (!list_empty(&dentry
->d_subdirs
)) {
789 this_parent
= dentry
;
794 * All done at this level ... ascend and resume the search.
796 if (this_parent
!= parent
) {
797 next
= this_parent
->d_u
.d_child
.next
;
798 this_parent
= this_parent
->d_parent
;
801 spin_unlock(&dcache_lock
);
802 return 0; /* No mount points found in tree */
804 spin_unlock(&dcache_lock
);
807 EXPORT_SYMBOL(have_submounts
);
810 * Search the dentry child list for the specified parent,
811 * and move any unused dentries to the end of the unused
812 * list for prune_dcache(). We descend to the next level
813 * whenever the d_subdirs list is non-empty and continue
816 * It returns zero iff there are no unused children,
817 * otherwise it returns the number of children moved to
818 * the end of the unused list. This may not be the total
819 * number of unused children, because select_parent can
820 * drop the lock and return early due to latency
823 static int select_parent(struct dentry
* parent
)
825 struct dentry
*this_parent
= parent
;
826 struct list_head
*next
;
829 spin_lock(&dcache_lock
);
831 next
= this_parent
->d_subdirs
.next
;
833 while (next
!= &this_parent
->d_subdirs
) {
834 struct list_head
*tmp
= next
;
835 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
839 * move only zero ref count dentries to the end
840 * of the unused list for prune_dcache
842 if (!atomic_read(&dentry
->d_count
)) {
843 dentry_lru_move_tail(dentry
);
846 dentry_lru_del(dentry
);
850 * We can return to the caller if we have found some (this
851 * ensures forward progress). We'll be coming back to find
854 if (found
&& need_resched())
858 * Descend a level if the d_subdirs list is non-empty.
860 if (!list_empty(&dentry
->d_subdirs
)) {
861 this_parent
= dentry
;
866 * All done at this level ... ascend and resume the search.
868 if (this_parent
!= parent
) {
869 next
= this_parent
->d_u
.d_child
.next
;
870 this_parent
= this_parent
->d_parent
;
874 spin_unlock(&dcache_lock
);
879 * shrink_dcache_parent - prune dcache
880 * @parent: parent of entries to prune
882 * Prune the dcache to remove unused children of the parent dentry.
885 void shrink_dcache_parent(struct dentry
* parent
)
887 struct super_block
*sb
= parent
->d_sb
;
890 while ((found
= select_parent(parent
)) != 0)
891 __shrink_dcache_sb(sb
, &found
, 0);
893 EXPORT_SYMBOL(shrink_dcache_parent
);
896 * Scan `nr' dentries and return the number which remain.
898 * We need to avoid reentering the filesystem if the caller is performing a
899 * GFP_NOFS allocation attempt. One example deadlock is:
901 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
902 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
903 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
905 * In this case we return -1 to tell the caller that we baled.
907 static int shrink_dcache_memory(struct shrinker
*shrink
, int nr
, gfp_t gfp_mask
)
910 if (!(gfp_mask
& __GFP_FS
))
915 return (dentry_stat
.nr_unused
/ 100) * sysctl_vfs_cache_pressure
;
918 static struct shrinker dcache_shrinker
= {
919 .shrink
= shrink_dcache_memory
,
920 .seeks
= DEFAULT_SEEKS
,
924 * d_alloc - allocate a dcache entry
925 * @parent: parent of entry to allocate
926 * @name: qstr of the name
928 * Allocates a dentry. It returns %NULL if there is insufficient memory
929 * available. On a success the dentry is returned. The name passed in is
930 * copied and the copy passed in may be reused after this call.
933 struct dentry
*d_alloc(struct dentry
* parent
, const struct qstr
*name
)
935 struct dentry
*dentry
;
938 dentry
= kmem_cache_alloc(dentry_cache
, GFP_KERNEL
);
942 if (name
->len
> DNAME_INLINE_LEN
-1) {
943 dname
= kmalloc(name
->len
+ 1, GFP_KERNEL
);
945 kmem_cache_free(dentry_cache
, dentry
);
949 dname
= dentry
->d_iname
;
951 dentry
->d_name
.name
= dname
;
953 dentry
->d_name
.len
= name
->len
;
954 dentry
->d_name
.hash
= name
->hash
;
955 memcpy(dname
, name
->name
, name
->len
);
956 dname
[name
->len
] = 0;
958 atomic_set(&dentry
->d_count
, 1);
959 dentry
->d_flags
= DCACHE_UNHASHED
;
960 spin_lock_init(&dentry
->d_lock
);
961 dentry
->d_inode
= NULL
;
962 dentry
->d_parent
= NULL
;
965 dentry
->d_fsdata
= NULL
;
966 dentry
->d_mounted
= 0;
967 INIT_HLIST_NODE(&dentry
->d_hash
);
968 INIT_LIST_HEAD(&dentry
->d_lru
);
969 INIT_LIST_HEAD(&dentry
->d_subdirs
);
970 INIT_LIST_HEAD(&dentry
->d_alias
);
973 dentry
->d_parent
= dget(parent
);
974 dentry
->d_sb
= parent
->d_sb
;
976 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
979 spin_lock(&dcache_lock
);
981 list_add(&dentry
->d_u
.d_child
, &parent
->d_subdirs
);
982 spin_unlock(&dcache_lock
);
984 percpu_counter_inc(&nr_dentry
);
988 EXPORT_SYMBOL(d_alloc
);
990 struct dentry
*d_alloc_name(struct dentry
*parent
, const char *name
)
995 q
.len
= strlen(name
);
996 q
.hash
= full_name_hash(q
.name
, q
.len
);
997 return d_alloc(parent
, &q
);
999 EXPORT_SYMBOL(d_alloc_name
);
1001 /* the caller must hold dcache_lock */
1002 static void __d_instantiate(struct dentry
*dentry
, struct inode
*inode
)
1005 list_add(&dentry
->d_alias
, &inode
->i_dentry
);
1006 dentry
->d_inode
= inode
;
1007 fsnotify_d_instantiate(dentry
, inode
);
1011 * d_instantiate - fill in inode information for a dentry
1012 * @entry: dentry to complete
1013 * @inode: inode to attach to this dentry
1015 * Fill in inode information in the entry.
1017 * This turns negative dentries into productive full members
1020 * NOTE! This assumes that the inode count has been incremented
1021 * (or otherwise set) by the caller to indicate that it is now
1022 * in use by the dcache.
1025 void d_instantiate(struct dentry
*entry
, struct inode
* inode
)
1027 BUG_ON(!list_empty(&entry
->d_alias
));
1028 spin_lock(&dcache_lock
);
1029 __d_instantiate(entry
, inode
);
1030 spin_unlock(&dcache_lock
);
1031 security_d_instantiate(entry
, inode
);
1033 EXPORT_SYMBOL(d_instantiate
);
1036 * d_instantiate_unique - instantiate a non-aliased dentry
1037 * @entry: dentry to instantiate
1038 * @inode: inode to attach to this dentry
1040 * Fill in inode information in the entry. On success, it returns NULL.
1041 * If an unhashed alias of "entry" already exists, then we return the
1042 * aliased dentry instead and drop one reference to inode.
1044 * Note that in order to avoid conflicts with rename() etc, the caller
1045 * had better be holding the parent directory semaphore.
1047 * This also assumes that the inode count has been incremented
1048 * (or otherwise set) by the caller to indicate that it is now
1049 * in use by the dcache.
1051 static struct dentry
*__d_instantiate_unique(struct dentry
*entry
,
1052 struct inode
*inode
)
1054 struct dentry
*alias
;
1055 int len
= entry
->d_name
.len
;
1056 const char *name
= entry
->d_name
.name
;
1057 unsigned int hash
= entry
->d_name
.hash
;
1060 __d_instantiate(entry
, NULL
);
1064 list_for_each_entry(alias
, &inode
->i_dentry
, d_alias
) {
1065 struct qstr
*qstr
= &alias
->d_name
;
1067 if (qstr
->hash
!= hash
)
1069 if (alias
->d_parent
!= entry
->d_parent
)
1071 if (qstr
->len
!= len
)
1073 if (memcmp(qstr
->name
, name
, len
))
1079 __d_instantiate(entry
, inode
);
1083 struct dentry
*d_instantiate_unique(struct dentry
*entry
, struct inode
*inode
)
1085 struct dentry
*result
;
1087 BUG_ON(!list_empty(&entry
->d_alias
));
1089 spin_lock(&dcache_lock
);
1090 result
= __d_instantiate_unique(entry
, inode
);
1091 spin_unlock(&dcache_lock
);
1094 security_d_instantiate(entry
, inode
);
1098 BUG_ON(!d_unhashed(result
));
1103 EXPORT_SYMBOL(d_instantiate_unique
);
1106 * d_alloc_root - allocate root dentry
1107 * @root_inode: inode to allocate the root for
1109 * Allocate a root ("/") dentry for the inode given. The inode is
1110 * instantiated and returned. %NULL is returned if there is insufficient
1111 * memory or the inode passed is %NULL.
1114 struct dentry
* d_alloc_root(struct inode
* root_inode
)
1116 struct dentry
*res
= NULL
;
1119 static const struct qstr name
= { .name
= "/", .len
= 1 };
1121 res
= d_alloc(NULL
, &name
);
1123 res
->d_sb
= root_inode
->i_sb
;
1124 res
->d_parent
= res
;
1125 d_instantiate(res
, root_inode
);
1130 EXPORT_SYMBOL(d_alloc_root
);
1132 static inline struct hlist_head
*d_hash(struct dentry
*parent
,
1135 hash
+= ((unsigned long) parent
^ GOLDEN_RATIO_PRIME
) / L1_CACHE_BYTES
;
1136 hash
= hash
^ ((hash
^ GOLDEN_RATIO_PRIME
) >> D_HASHBITS
);
1137 return dentry_hashtable
+ (hash
& D_HASHMASK
);
1141 * d_obtain_alias - find or allocate a dentry for a given inode
1142 * @inode: inode to allocate the dentry for
1144 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1145 * similar open by handle operations. The returned dentry may be anonymous,
1146 * or may have a full name (if the inode was already in the cache).
1148 * When called on a directory inode, we must ensure that the inode only ever
1149 * has one dentry. If a dentry is found, that is returned instead of
1150 * allocating a new one.
1152 * On successful return, the reference to the inode has been transferred
1153 * to the dentry. In case of an error the reference on the inode is released.
1154 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1155 * be passed in and will be the error will be propagate to the return value,
1156 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1158 struct dentry
*d_obtain_alias(struct inode
*inode
)
1160 static const struct qstr anonstring
= { .name
= "" };
1165 return ERR_PTR(-ESTALE
);
1167 return ERR_CAST(inode
);
1169 res
= d_find_alias(inode
);
1173 tmp
= d_alloc(NULL
, &anonstring
);
1175 res
= ERR_PTR(-ENOMEM
);
1178 tmp
->d_parent
= tmp
; /* make sure dput doesn't croak */
1180 spin_lock(&dcache_lock
);
1181 res
= __d_find_alias(inode
, 0);
1183 spin_unlock(&dcache_lock
);
1188 /* attach a disconnected dentry */
1189 spin_lock(&tmp
->d_lock
);
1190 tmp
->d_sb
= inode
->i_sb
;
1191 tmp
->d_inode
= inode
;
1192 tmp
->d_flags
|= DCACHE_DISCONNECTED
;
1193 tmp
->d_flags
&= ~DCACHE_UNHASHED
;
1194 list_add(&tmp
->d_alias
, &inode
->i_dentry
);
1195 hlist_add_head(&tmp
->d_hash
, &inode
->i_sb
->s_anon
);
1196 spin_unlock(&tmp
->d_lock
);
1198 spin_unlock(&dcache_lock
);
1205 EXPORT_SYMBOL(d_obtain_alias
);
1208 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1209 * @inode: the inode which may have a disconnected dentry
1210 * @dentry: a negative dentry which we want to point to the inode.
1212 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1213 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1214 * and return it, else simply d_add the inode to the dentry and return NULL.
1216 * This is needed in the lookup routine of any filesystem that is exportable
1217 * (via knfsd) so that we can build dcache paths to directories effectively.
1219 * If a dentry was found and moved, then it is returned. Otherwise NULL
1220 * is returned. This matches the expected return value of ->lookup.
1223 struct dentry
*d_splice_alias(struct inode
*inode
, struct dentry
*dentry
)
1225 struct dentry
*new = NULL
;
1227 if (inode
&& S_ISDIR(inode
->i_mode
)) {
1228 spin_lock(&dcache_lock
);
1229 new = __d_find_alias(inode
, 1);
1231 BUG_ON(!(new->d_flags
& DCACHE_DISCONNECTED
));
1232 spin_unlock(&dcache_lock
);
1233 security_d_instantiate(new, inode
);
1234 d_move(new, dentry
);
1237 /* already taking dcache_lock, so d_add() by hand */
1238 __d_instantiate(dentry
, inode
);
1239 spin_unlock(&dcache_lock
);
1240 security_d_instantiate(dentry
, inode
);
1244 d_add(dentry
, inode
);
1247 EXPORT_SYMBOL(d_splice_alias
);
1250 * d_add_ci - lookup or allocate new dentry with case-exact name
1251 * @inode: the inode case-insensitive lookup has found
1252 * @dentry: the negative dentry that was passed to the parent's lookup func
1253 * @name: the case-exact name to be associated with the returned dentry
1255 * This is to avoid filling the dcache with case-insensitive names to the
1256 * same inode, only the actual correct case is stored in the dcache for
1257 * case-insensitive filesystems.
1259 * For a case-insensitive lookup match and if the the case-exact dentry
1260 * already exists in in the dcache, use it and return it.
1262 * If no entry exists with the exact case name, allocate new dentry with
1263 * the exact case, and return the spliced entry.
1265 struct dentry
*d_add_ci(struct dentry
*dentry
, struct inode
*inode
,
1269 struct dentry
*found
;
1273 * First check if a dentry matching the name already exists,
1274 * if not go ahead and create it now.
1276 found
= d_hash_and_lookup(dentry
->d_parent
, name
);
1278 new = d_alloc(dentry
->d_parent
, name
);
1284 found
= d_splice_alias(inode
, new);
1293 * If a matching dentry exists, and it's not negative use it.
1295 * Decrement the reference count to balance the iget() done
1298 if (found
->d_inode
) {
1299 if (unlikely(found
->d_inode
!= inode
)) {
1300 /* This can't happen because bad inodes are unhashed. */
1301 BUG_ON(!is_bad_inode(inode
));
1302 BUG_ON(!is_bad_inode(found
->d_inode
));
1309 * Negative dentry: instantiate it unless the inode is a directory and
1310 * already has a dentry.
1312 spin_lock(&dcache_lock
);
1313 if (!S_ISDIR(inode
->i_mode
) || list_empty(&inode
->i_dentry
)) {
1314 __d_instantiate(found
, inode
);
1315 spin_unlock(&dcache_lock
);
1316 security_d_instantiate(found
, inode
);
1321 * In case a directory already has a (disconnected) entry grab a
1322 * reference to it, move it in place and use it.
1324 new = list_entry(inode
->i_dentry
.next
, struct dentry
, d_alias
);
1326 spin_unlock(&dcache_lock
);
1327 security_d_instantiate(found
, inode
);
1335 return ERR_PTR(error
);
1337 EXPORT_SYMBOL(d_add_ci
);
1340 * d_lookup - search for a dentry
1341 * @parent: parent dentry
1342 * @name: qstr of name we wish to find
1343 * Returns: dentry, or NULL
1345 * d_lookup searches the children of the parent dentry for the name in
1346 * question. If the dentry is found its reference count is incremented and the
1347 * dentry is returned. The caller must use dput to free the entry when it has
1348 * finished using it. %NULL is returned if the dentry does not exist.
1350 struct dentry
* d_lookup(struct dentry
* parent
, struct qstr
* name
)
1352 struct dentry
* dentry
= NULL
;
1356 seq
= read_seqbegin(&rename_lock
);
1357 dentry
= __d_lookup(parent
, name
);
1360 } while (read_seqretry(&rename_lock
, seq
));
1363 EXPORT_SYMBOL(d_lookup
);
1366 * __d_lookup - search for a dentry (racy)
1367 * @parent: parent dentry
1368 * @name: qstr of name we wish to find
1369 * Returns: dentry, or NULL
1371 * __d_lookup is like d_lookup, however it may (rarely) return a
1372 * false-negative result due to unrelated rename activity.
1374 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1375 * however it must be used carefully, eg. with a following d_lookup in
1376 * the case of failure.
1378 * __d_lookup callers must be commented.
1380 struct dentry
* __d_lookup(struct dentry
* parent
, struct qstr
* name
)
1382 unsigned int len
= name
->len
;
1383 unsigned int hash
= name
->hash
;
1384 const unsigned char *str
= name
->name
;
1385 struct hlist_head
*head
= d_hash(parent
,hash
);
1386 struct dentry
*found
= NULL
;
1387 struct hlist_node
*node
;
1388 struct dentry
*dentry
;
1391 * The hash list is protected using RCU.
1393 * Take d_lock when comparing a candidate dentry, to avoid races
1396 * It is possible that concurrent renames can mess up our list
1397 * walk here and result in missing our dentry, resulting in the
1398 * false-negative result. d_lookup() protects against concurrent
1399 * renames using rename_lock seqlock.
1401 * See Documentation/vfs/dcache-locking.txt for more details.
1405 hlist_for_each_entry_rcu(dentry
, node
, head
, d_hash
) {
1408 if (dentry
->d_name
.hash
!= hash
)
1410 if (dentry
->d_parent
!= parent
)
1413 spin_lock(&dentry
->d_lock
);
1416 * Recheck the dentry after taking the lock - d_move may have
1417 * changed things. Don't bother checking the hash because
1418 * we're about to compare the whole name anyway.
1420 if (dentry
->d_parent
!= parent
)
1423 /* non-existing due to RCU? */
1424 if (d_unhashed(dentry
))
1428 * It is safe to compare names since d_move() cannot
1429 * change the qstr (protected by d_lock).
1431 qstr
= &dentry
->d_name
;
1432 if (parent
->d_op
&& parent
->d_op
->d_compare
) {
1433 if (parent
->d_op
->d_compare(parent
, qstr
, name
))
1436 if (qstr
->len
!= len
)
1438 if (memcmp(qstr
->name
, str
, len
))
1442 atomic_inc(&dentry
->d_count
);
1444 spin_unlock(&dentry
->d_lock
);
1447 spin_unlock(&dentry
->d_lock
);
1455 * d_hash_and_lookup - hash the qstr then search for a dentry
1456 * @dir: Directory to search in
1457 * @name: qstr of name we wish to find
1459 * On hash failure or on lookup failure NULL is returned.
1461 struct dentry
*d_hash_and_lookup(struct dentry
*dir
, struct qstr
*name
)
1463 struct dentry
*dentry
= NULL
;
1466 * Check for a fs-specific hash function. Note that we must
1467 * calculate the standard hash first, as the d_op->d_hash()
1468 * routine may choose to leave the hash value unchanged.
1470 name
->hash
= full_name_hash(name
->name
, name
->len
);
1471 if (dir
->d_op
&& dir
->d_op
->d_hash
) {
1472 if (dir
->d_op
->d_hash(dir
, name
) < 0)
1475 dentry
= d_lookup(dir
, name
);
1481 * d_validate - verify dentry provided from insecure source (deprecated)
1482 * @dentry: The dentry alleged to be valid child of @dparent
1483 * @dparent: The parent dentry (known to be valid)
1485 * An insecure source has sent us a dentry, here we verify it and dget() it.
1486 * This is used by ncpfs in its readdir implementation.
1487 * Zero is returned in the dentry is invalid.
1489 * This function is slow for big directories, and deprecated, do not use it.
1491 int d_validate(struct dentry
*dentry
, struct dentry
*dparent
)
1493 struct dentry
*child
;
1495 spin_lock(&dcache_lock
);
1496 list_for_each_entry(child
, &dparent
->d_subdirs
, d_u
.d_child
) {
1497 if (dentry
== child
) {
1498 __dget_locked(dentry
);
1499 spin_unlock(&dcache_lock
);
1503 spin_unlock(&dcache_lock
);
1507 EXPORT_SYMBOL(d_validate
);
1510 * When a file is deleted, we have two options:
1511 * - turn this dentry into a negative dentry
1512 * - unhash this dentry and free it.
1514 * Usually, we want to just turn this into
1515 * a negative dentry, but if anybody else is
1516 * currently using the dentry or the inode
1517 * we can't do that and we fall back on removing
1518 * it from the hash queues and waiting for
1519 * it to be deleted later when it has no users
1523 * d_delete - delete a dentry
1524 * @dentry: The dentry to delete
1526 * Turn the dentry into a negative dentry if possible, otherwise
1527 * remove it from the hash queues so it can be deleted later
1530 void d_delete(struct dentry
* dentry
)
1534 * Are we the only user?
1536 spin_lock(&dcache_lock
);
1537 spin_lock(&dentry
->d_lock
);
1538 isdir
= S_ISDIR(dentry
->d_inode
->i_mode
);
1539 if (atomic_read(&dentry
->d_count
) == 1) {
1540 dentry
->d_flags
&= ~DCACHE_CANT_MOUNT
;
1541 dentry_iput(dentry
);
1542 fsnotify_nameremove(dentry
, isdir
);
1546 if (!d_unhashed(dentry
))
1549 spin_unlock(&dentry
->d_lock
);
1550 spin_unlock(&dcache_lock
);
1552 fsnotify_nameremove(dentry
, isdir
);
1554 EXPORT_SYMBOL(d_delete
);
1556 static void __d_rehash(struct dentry
* entry
, struct hlist_head
*list
)
1559 entry
->d_flags
&= ~DCACHE_UNHASHED
;
1560 hlist_add_head_rcu(&entry
->d_hash
, list
);
1563 static void _d_rehash(struct dentry
* entry
)
1565 __d_rehash(entry
, d_hash(entry
->d_parent
, entry
->d_name
.hash
));
1569 * d_rehash - add an entry back to the hash
1570 * @entry: dentry to add to the hash
1572 * Adds a dentry to the hash according to its name.
1575 void d_rehash(struct dentry
* entry
)
1577 spin_lock(&dcache_lock
);
1578 spin_lock(&entry
->d_lock
);
1580 spin_unlock(&entry
->d_lock
);
1581 spin_unlock(&dcache_lock
);
1583 EXPORT_SYMBOL(d_rehash
);
1586 * When switching names, the actual string doesn't strictly have to
1587 * be preserved in the target - because we're dropping the target
1588 * anyway. As such, we can just do a simple memcpy() to copy over
1589 * the new name before we switch.
1591 * Note that we have to be a lot more careful about getting the hash
1592 * switched - we have to switch the hash value properly even if it
1593 * then no longer matches the actual (corrupted) string of the target.
1594 * The hash value has to match the hash queue that the dentry is on..
1596 static void switch_names(struct dentry
*dentry
, struct dentry
*target
)
1598 if (dname_external(target
)) {
1599 if (dname_external(dentry
)) {
1601 * Both external: swap the pointers
1603 swap(target
->d_name
.name
, dentry
->d_name
.name
);
1606 * dentry:internal, target:external. Steal target's
1607 * storage and make target internal.
1609 memcpy(target
->d_iname
, dentry
->d_name
.name
,
1610 dentry
->d_name
.len
+ 1);
1611 dentry
->d_name
.name
= target
->d_name
.name
;
1612 target
->d_name
.name
= target
->d_iname
;
1615 if (dname_external(dentry
)) {
1617 * dentry:external, target:internal. Give dentry's
1618 * storage to target and make dentry internal
1620 memcpy(dentry
->d_iname
, target
->d_name
.name
,
1621 target
->d_name
.len
+ 1);
1622 target
->d_name
.name
= dentry
->d_name
.name
;
1623 dentry
->d_name
.name
= dentry
->d_iname
;
1626 * Both are internal. Just copy target to dentry
1628 memcpy(dentry
->d_iname
, target
->d_name
.name
,
1629 target
->d_name
.len
+ 1);
1630 dentry
->d_name
.len
= target
->d_name
.len
;
1634 swap(dentry
->d_name
.len
, target
->d_name
.len
);
1638 * We cannibalize "target" when moving dentry on top of it,
1639 * because it's going to be thrown away anyway. We could be more
1640 * polite about it, though.
1642 * This forceful removal will result in ugly /proc output if
1643 * somebody holds a file open that got deleted due to a rename.
1644 * We could be nicer about the deleted file, and let it show
1645 * up under the name it had before it was deleted rather than
1646 * under the original name of the file that was moved on top of it.
1650 * d_move_locked - move a dentry
1651 * @dentry: entry to move
1652 * @target: new dentry
1654 * Update the dcache to reflect the move of a file name. Negative
1655 * dcache entries should not be moved in this way.
1657 static void d_move_locked(struct dentry
* dentry
, struct dentry
* target
)
1659 struct hlist_head
*list
;
1661 if (!dentry
->d_inode
)
1662 printk(KERN_WARNING
"VFS: moving negative dcache entry\n");
1664 write_seqlock(&rename_lock
);
1666 * XXXX: do we really need to take target->d_lock?
1668 if (target
< dentry
) {
1669 spin_lock(&target
->d_lock
);
1670 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1672 spin_lock(&dentry
->d_lock
);
1673 spin_lock_nested(&target
->d_lock
, DENTRY_D_LOCK_NESTED
);
1676 /* Move the dentry to the target hash queue, if on different bucket */
1677 if (d_unhashed(dentry
))
1678 goto already_unhashed
;
1680 hlist_del_rcu(&dentry
->d_hash
);
1683 list
= d_hash(target
->d_parent
, target
->d_name
.hash
);
1684 __d_rehash(dentry
, list
);
1686 /* Unhash the target: dput() will then get rid of it */
1689 list_del(&dentry
->d_u
.d_child
);
1690 list_del(&target
->d_u
.d_child
);
1692 /* Switch the names.. */
1693 switch_names(dentry
, target
);
1694 swap(dentry
->d_name
.hash
, target
->d_name
.hash
);
1696 /* ... and switch the parents */
1697 if (IS_ROOT(dentry
)) {
1698 dentry
->d_parent
= target
->d_parent
;
1699 target
->d_parent
= target
;
1700 INIT_LIST_HEAD(&target
->d_u
.d_child
);
1702 swap(dentry
->d_parent
, target
->d_parent
);
1704 /* And add them back to the (new) parent lists */
1705 list_add(&target
->d_u
.d_child
, &target
->d_parent
->d_subdirs
);
1708 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
1709 spin_unlock(&target
->d_lock
);
1710 fsnotify_d_move(dentry
);
1711 spin_unlock(&dentry
->d_lock
);
1712 write_sequnlock(&rename_lock
);
1716 * d_move - move a dentry
1717 * @dentry: entry to move
1718 * @target: new dentry
1720 * Update the dcache to reflect the move of a file name. Negative
1721 * dcache entries should not be moved in this way.
1724 void d_move(struct dentry
* dentry
, struct dentry
* target
)
1726 spin_lock(&dcache_lock
);
1727 d_move_locked(dentry
, target
);
1728 spin_unlock(&dcache_lock
);
1730 EXPORT_SYMBOL(d_move
);
1733 * d_ancestor - search for an ancestor
1734 * @p1: ancestor dentry
1737 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1738 * an ancestor of p2, else NULL.
1740 struct dentry
*d_ancestor(struct dentry
*p1
, struct dentry
*p2
)
1744 for (p
= p2
; !IS_ROOT(p
); p
= p
->d_parent
) {
1745 if (p
->d_parent
== p1
)
1752 * This helper attempts to cope with remotely renamed directories
1754 * It assumes that the caller is already holding
1755 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1757 * Note: If ever the locking in lock_rename() changes, then please
1758 * remember to update this too...
1760 static struct dentry
*__d_unalias(struct dentry
*dentry
, struct dentry
*alias
)
1761 __releases(dcache_lock
)
1763 struct mutex
*m1
= NULL
, *m2
= NULL
;
1766 /* If alias and dentry share a parent, then no extra locks required */
1767 if (alias
->d_parent
== dentry
->d_parent
)
1770 /* Check for loops */
1771 ret
= ERR_PTR(-ELOOP
);
1772 if (d_ancestor(alias
, dentry
))
1775 /* See lock_rename() */
1776 ret
= ERR_PTR(-EBUSY
);
1777 if (!mutex_trylock(&dentry
->d_sb
->s_vfs_rename_mutex
))
1779 m1
= &dentry
->d_sb
->s_vfs_rename_mutex
;
1780 if (!mutex_trylock(&alias
->d_parent
->d_inode
->i_mutex
))
1782 m2
= &alias
->d_parent
->d_inode
->i_mutex
;
1784 d_move_locked(alias
, dentry
);
1787 spin_unlock(&dcache_lock
);
1796 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1797 * named dentry in place of the dentry to be replaced.
1799 static void __d_materialise_dentry(struct dentry
*dentry
, struct dentry
*anon
)
1801 struct dentry
*dparent
, *aparent
;
1803 switch_names(dentry
, anon
);
1804 swap(dentry
->d_name
.hash
, anon
->d_name
.hash
);
1806 dparent
= dentry
->d_parent
;
1807 aparent
= anon
->d_parent
;
1809 dentry
->d_parent
= (aparent
== anon
) ? dentry
: aparent
;
1810 list_del(&dentry
->d_u
.d_child
);
1811 if (!IS_ROOT(dentry
))
1812 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
1814 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
1816 anon
->d_parent
= (dparent
== dentry
) ? anon
: dparent
;
1817 list_del(&anon
->d_u
.d_child
);
1819 list_add(&anon
->d_u
.d_child
, &anon
->d_parent
->d_subdirs
);
1821 INIT_LIST_HEAD(&anon
->d_u
.d_child
);
1823 anon
->d_flags
&= ~DCACHE_DISCONNECTED
;
1827 * d_materialise_unique - introduce an inode into the tree
1828 * @dentry: candidate dentry
1829 * @inode: inode to bind to the dentry, to which aliases may be attached
1831 * Introduces an dentry into the tree, substituting an extant disconnected
1832 * root directory alias in its place if there is one
1834 struct dentry
*d_materialise_unique(struct dentry
*dentry
, struct inode
*inode
)
1836 struct dentry
*actual
;
1838 BUG_ON(!d_unhashed(dentry
));
1840 spin_lock(&dcache_lock
);
1844 __d_instantiate(dentry
, NULL
);
1848 if (S_ISDIR(inode
->i_mode
)) {
1849 struct dentry
*alias
;
1851 /* Does an aliased dentry already exist? */
1852 alias
= __d_find_alias(inode
, 0);
1855 /* Is this an anonymous mountpoint that we could splice
1857 if (IS_ROOT(alias
)) {
1858 spin_lock(&alias
->d_lock
);
1859 __d_materialise_dentry(dentry
, alias
);
1863 /* Nope, but we must(!) avoid directory aliasing */
1864 actual
= __d_unalias(dentry
, alias
);
1871 /* Add a unique reference */
1872 actual
= __d_instantiate_unique(dentry
, inode
);
1875 else if (unlikely(!d_unhashed(actual
)))
1876 goto shouldnt_be_hashed
;
1879 spin_lock(&actual
->d_lock
);
1882 spin_unlock(&actual
->d_lock
);
1883 spin_unlock(&dcache_lock
);
1885 if (actual
== dentry
) {
1886 security_d_instantiate(dentry
, inode
);
1894 spin_unlock(&dcache_lock
);
1897 EXPORT_SYMBOL_GPL(d_materialise_unique
);
1899 static int prepend(char **buffer
, int *buflen
, const char *str
, int namelen
)
1903 return -ENAMETOOLONG
;
1905 memcpy(*buffer
, str
, namelen
);
1909 static int prepend_name(char **buffer
, int *buflen
, struct qstr
*name
)
1911 return prepend(buffer
, buflen
, name
->name
, name
->len
);
1915 * Prepend path string to a buffer
1917 * @path: the dentry/vfsmount to report
1918 * @root: root vfsmnt/dentry (may be modified by this function)
1919 * @buffer: pointer to the end of the buffer
1920 * @buflen: pointer to buffer length
1922 * Caller holds the dcache_lock.
1924 * If path is not reachable from the supplied root, then the value of
1925 * root is changed (without modifying refcounts).
1927 static int prepend_path(const struct path
*path
, struct path
*root
,
1928 char **buffer
, int *buflen
)
1930 struct dentry
*dentry
= path
->dentry
;
1931 struct vfsmount
*vfsmnt
= path
->mnt
;
1935 br_read_lock(vfsmount_lock
);
1936 while (dentry
!= root
->dentry
|| vfsmnt
!= root
->mnt
) {
1937 struct dentry
* parent
;
1939 if (dentry
== vfsmnt
->mnt_root
|| IS_ROOT(dentry
)) {
1941 if (vfsmnt
->mnt_parent
== vfsmnt
) {
1944 dentry
= vfsmnt
->mnt_mountpoint
;
1945 vfsmnt
= vfsmnt
->mnt_parent
;
1948 parent
= dentry
->d_parent
;
1950 error
= prepend_name(buffer
, buflen
, &dentry
->d_name
);
1952 error
= prepend(buffer
, buflen
, "/", 1);
1961 if (!error
&& !slash
)
1962 error
= prepend(buffer
, buflen
, "/", 1);
1964 br_read_unlock(vfsmount_lock
);
1969 * Filesystems needing to implement special "root names"
1970 * should do so with ->d_dname()
1972 if (IS_ROOT(dentry
) &&
1973 (dentry
->d_name
.len
!= 1 || dentry
->d_name
.name
[0] != '/')) {
1974 WARN(1, "Root dentry has weird name <%.*s>\n",
1975 (int) dentry
->d_name
.len
, dentry
->d_name
.name
);
1978 root
->dentry
= dentry
;
1983 * __d_path - return the path of a dentry
1984 * @path: the dentry/vfsmount to report
1985 * @root: root vfsmnt/dentry (may be modified by this function)
1986 * @buf: buffer to return value in
1987 * @buflen: buffer length
1989 * Convert a dentry into an ASCII path name.
1991 * Returns a pointer into the buffer or an error code if the
1992 * path was too long.
1994 * "buflen" should be positive.
1996 * If path is not reachable from the supplied root, then the value of
1997 * root is changed (without modifying refcounts).
1999 char *__d_path(const struct path
*path
, struct path
*root
,
2000 char *buf
, int buflen
)
2002 char *res
= buf
+ buflen
;
2005 prepend(&res
, &buflen
, "\0", 1);
2006 spin_lock(&dcache_lock
);
2007 error
= prepend_path(path
, root
, &res
, &buflen
);
2008 spin_unlock(&dcache_lock
);
2011 return ERR_PTR(error
);
2016 * same as __d_path but appends "(deleted)" for unlinked files.
2018 static int path_with_deleted(const struct path
*path
, struct path
*root
,
2019 char **buf
, int *buflen
)
2021 prepend(buf
, buflen
, "\0", 1);
2022 if (d_unlinked(path
->dentry
)) {
2023 int error
= prepend(buf
, buflen
, " (deleted)", 10);
2028 return prepend_path(path
, root
, buf
, buflen
);
2031 static int prepend_unreachable(char **buffer
, int *buflen
)
2033 return prepend(buffer
, buflen
, "(unreachable)", 13);
2037 * d_path - return the path of a dentry
2038 * @path: path to report
2039 * @buf: buffer to return value in
2040 * @buflen: buffer length
2042 * Convert a dentry into an ASCII path name. If the entry has been deleted
2043 * the string " (deleted)" is appended. Note that this is ambiguous.
2045 * Returns a pointer into the buffer or an error code if the path was
2046 * too long. Note: Callers should use the returned pointer, not the passed
2047 * in buffer, to use the name! The implementation often starts at an offset
2048 * into the buffer, and may leave 0 bytes at the start.
2050 * "buflen" should be positive.
2052 char *d_path(const struct path
*path
, char *buf
, int buflen
)
2054 char *res
= buf
+ buflen
;
2060 * We have various synthetic filesystems that never get mounted. On
2061 * these filesystems dentries are never used for lookup purposes, and
2062 * thus don't need to be hashed. They also don't need a name until a
2063 * user wants to identify the object in /proc/pid/fd/. The little hack
2064 * below allows us to generate a name for these objects on demand:
2066 if (path
->dentry
->d_op
&& path
->dentry
->d_op
->d_dname
)
2067 return path
->dentry
->d_op
->d_dname(path
->dentry
, buf
, buflen
);
2069 get_fs_root(current
->fs
, &root
);
2070 spin_lock(&dcache_lock
);
2072 error
= path_with_deleted(path
, &tmp
, &res
, &buflen
);
2074 res
= ERR_PTR(error
);
2075 spin_unlock(&dcache_lock
);
2079 EXPORT_SYMBOL(d_path
);
2082 * d_path_with_unreachable - return the path of a dentry
2083 * @path: path to report
2084 * @buf: buffer to return value in
2085 * @buflen: buffer length
2087 * The difference from d_path() is that this prepends "(unreachable)"
2088 * to paths which are unreachable from the current process' root.
2090 char *d_path_with_unreachable(const struct path
*path
, char *buf
, int buflen
)
2092 char *res
= buf
+ buflen
;
2097 if (path
->dentry
->d_op
&& path
->dentry
->d_op
->d_dname
)
2098 return path
->dentry
->d_op
->d_dname(path
->dentry
, buf
, buflen
);
2100 get_fs_root(current
->fs
, &root
);
2101 spin_lock(&dcache_lock
);
2103 error
= path_with_deleted(path
, &tmp
, &res
, &buflen
);
2104 if (!error
&& !path_equal(&tmp
, &root
))
2105 error
= prepend_unreachable(&res
, &buflen
);
2106 spin_unlock(&dcache_lock
);
2109 res
= ERR_PTR(error
);
2115 * Helper function for dentry_operations.d_dname() members
2117 char *dynamic_dname(struct dentry
*dentry
, char *buffer
, int buflen
,
2118 const char *fmt
, ...)
2124 va_start(args
, fmt
);
2125 sz
= vsnprintf(temp
, sizeof(temp
), fmt
, args
) + 1;
2128 if (sz
> sizeof(temp
) || sz
> buflen
)
2129 return ERR_PTR(-ENAMETOOLONG
);
2131 buffer
+= buflen
- sz
;
2132 return memcpy(buffer
, temp
, sz
);
2136 * Write full pathname from the root of the filesystem into the buffer.
2138 char *__dentry_path(struct dentry
*dentry
, char *buf
, int buflen
)
2140 char *end
= buf
+ buflen
;
2143 prepend(&end
, &buflen
, "\0", 1);
2150 while (!IS_ROOT(dentry
)) {
2151 struct dentry
*parent
= dentry
->d_parent
;
2154 if ((prepend_name(&end
, &buflen
, &dentry
->d_name
) != 0) ||
2155 (prepend(&end
, &buflen
, "/", 1) != 0))
2163 return ERR_PTR(-ENAMETOOLONG
);
2165 EXPORT_SYMBOL(__dentry_path
);
2167 char *dentry_path(struct dentry
*dentry
, char *buf
, int buflen
)
2172 spin_lock(&dcache_lock
);
2173 if (d_unlinked(dentry
)) {
2175 if (prepend(&p
, &buflen
, "//deleted", 10) != 0)
2179 retval
= __dentry_path(dentry
, buf
, buflen
);
2180 spin_unlock(&dcache_lock
);
2181 if (!IS_ERR(retval
) && p
)
2182 *p
= '/'; /* restore '/' overriden with '\0' */
2185 spin_unlock(&dcache_lock
);
2186 return ERR_PTR(-ENAMETOOLONG
);
2190 * NOTE! The user-level library version returns a
2191 * character pointer. The kernel system call just
2192 * returns the length of the buffer filled (which
2193 * includes the ending '\0' character), or a negative
2194 * error value. So libc would do something like
2196 * char *getcwd(char * buf, size_t size)
2200 * retval = sys_getcwd(buf, size);
2207 SYSCALL_DEFINE2(getcwd
, char __user
*, buf
, unsigned long, size
)
2210 struct path pwd
, root
;
2211 char *page
= (char *) __get_free_page(GFP_USER
);
2216 get_fs_root_and_pwd(current
->fs
, &root
, &pwd
);
2219 spin_lock(&dcache_lock
);
2220 if (!d_unlinked(pwd
.dentry
)) {
2222 struct path tmp
= root
;
2223 char *cwd
= page
+ PAGE_SIZE
;
2224 int buflen
= PAGE_SIZE
;
2226 prepend(&cwd
, &buflen
, "\0", 1);
2227 error
= prepend_path(&pwd
, &tmp
, &cwd
, &buflen
);
2228 spin_unlock(&dcache_lock
);
2233 /* Unreachable from current root */
2234 if (!path_equal(&tmp
, &root
)) {
2235 error
= prepend_unreachable(&cwd
, &buflen
);
2241 len
= PAGE_SIZE
+ page
- cwd
;
2244 if (copy_to_user(buf
, cwd
, len
))
2248 spin_unlock(&dcache_lock
);
2253 free_page((unsigned long) page
);
2258 * Test whether new_dentry is a subdirectory of old_dentry.
2260 * Trivially implemented using the dcache structure
2264 * is_subdir - is new dentry a subdirectory of old_dentry
2265 * @new_dentry: new dentry
2266 * @old_dentry: old dentry
2268 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2269 * Returns 0 otherwise.
2270 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2273 int is_subdir(struct dentry
*new_dentry
, struct dentry
*old_dentry
)
2278 if (new_dentry
== old_dentry
)
2282 * Need rcu_readlock to protect against the d_parent trashing
2287 /* for restarting inner loop in case of seq retry */
2288 seq
= read_seqbegin(&rename_lock
);
2289 if (d_ancestor(old_dentry
, new_dentry
))
2293 } while (read_seqretry(&rename_lock
, seq
));
2299 int path_is_under(struct path
*path1
, struct path
*path2
)
2301 struct vfsmount
*mnt
= path1
->mnt
;
2302 struct dentry
*dentry
= path1
->dentry
;
2305 br_read_lock(vfsmount_lock
);
2306 if (mnt
!= path2
->mnt
) {
2308 if (mnt
->mnt_parent
== mnt
) {
2309 br_read_unlock(vfsmount_lock
);
2312 if (mnt
->mnt_parent
== path2
->mnt
)
2314 mnt
= mnt
->mnt_parent
;
2316 dentry
= mnt
->mnt_mountpoint
;
2318 res
= is_subdir(dentry
, path2
->dentry
);
2319 br_read_unlock(vfsmount_lock
);
2322 EXPORT_SYMBOL(path_is_under
);
2324 void d_genocide(struct dentry
*root
)
2326 struct dentry
*this_parent
= root
;
2327 struct list_head
*next
;
2329 spin_lock(&dcache_lock
);
2331 next
= this_parent
->d_subdirs
.next
;
2333 while (next
!= &this_parent
->d_subdirs
) {
2334 struct list_head
*tmp
= next
;
2335 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
2337 if (d_unhashed(dentry
)||!dentry
->d_inode
)
2339 if (!list_empty(&dentry
->d_subdirs
)) {
2340 this_parent
= dentry
;
2343 atomic_dec(&dentry
->d_count
);
2345 if (this_parent
!= root
) {
2346 next
= this_parent
->d_u
.d_child
.next
;
2347 atomic_dec(&this_parent
->d_count
);
2348 this_parent
= this_parent
->d_parent
;
2351 spin_unlock(&dcache_lock
);
2355 * find_inode_number - check for dentry with name
2356 * @dir: directory to check
2357 * @name: Name to find.
2359 * Check whether a dentry already exists for the given name,
2360 * and return the inode number if it has an inode. Otherwise
2363 * This routine is used to post-process directory listings for
2364 * filesystems using synthetic inode numbers, and is necessary
2365 * to keep getcwd() working.
2368 ino_t
find_inode_number(struct dentry
*dir
, struct qstr
*name
)
2370 struct dentry
* dentry
;
2373 dentry
= d_hash_and_lookup(dir
, name
);
2375 if (dentry
->d_inode
)
2376 ino
= dentry
->d_inode
->i_ino
;
2381 EXPORT_SYMBOL(find_inode_number
);
2383 static __initdata
unsigned long dhash_entries
;
2384 static int __init
set_dhash_entries(char *str
)
2388 dhash_entries
= simple_strtoul(str
, &str
, 0);
2391 __setup("dhash_entries=", set_dhash_entries
);
2393 static void __init
dcache_init_early(void)
2397 /* If hashes are distributed across NUMA nodes, defer
2398 * hash allocation until vmalloc space is available.
2404 alloc_large_system_hash("Dentry cache",
2405 sizeof(struct hlist_head
),
2413 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2414 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2417 static void __init
dcache_init(void)
2421 percpu_counter_init(&nr_dentry
, 0);
2424 * A constructor could be added for stable state like the lists,
2425 * but it is probably not worth it because of the cache nature
2428 dentry_cache
= KMEM_CACHE(dentry
,
2429 SLAB_RECLAIM_ACCOUNT
|SLAB_PANIC
|SLAB_MEM_SPREAD
);
2431 register_shrinker(&dcache_shrinker
);
2433 /* Hash may have been set up in dcache_init_early */
2438 alloc_large_system_hash("Dentry cache",
2439 sizeof(struct hlist_head
),
2447 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2448 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2451 /* SLAB cache for __getname() consumers */
2452 struct kmem_cache
*names_cachep __read_mostly
;
2453 EXPORT_SYMBOL(names_cachep
);
2455 EXPORT_SYMBOL(d_genocide
);
2457 void __init
vfs_caches_init_early(void)
2459 dcache_init_early();
2463 void __init
vfs_caches_init(unsigned long mempages
)
2465 unsigned long reserve
;
2467 /* Base hash sizes on available memory, with a reserve equal to
2468 150% of current kernel size */
2470 reserve
= min((mempages
- nr_free_pages()) * 3/2, mempages
- 1);
2471 mempages
-= reserve
;
2473 names_cachep
= kmem_cache_create("names_cache", PATH_MAX
, 0,
2474 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
2478 files_init(mempages
);