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_inode_lock protects:
41 * - i_dentry, d_alias, d_inode
42 * dcache_hash_lock protects:
43 * - the dcache hash table, s_anon lists
44 * dcache_lru_lock protects:
45 * - the dcache lru lists and counters
52 * - d_parent and d_subdirs
53 * - childrens' d_child and d_parent
62 * If there is an ancestor relationship:
63 * dentry->d_parent->...->d_parent->d_lock
65 * dentry->d_parent->d_lock
68 * If no ancestor relationship:
69 * if (dentry1 < dentry2)
73 int sysctl_vfs_cache_pressure __read_mostly
= 100;
74 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure
);
76 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dcache_inode_lock
);
77 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dcache_hash_lock
);
78 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dcache_lru_lock
);
79 __cacheline_aligned_in_smp
DEFINE_SEQLOCK(rename_lock
);
81 EXPORT_SYMBOL(rename_lock
);
82 EXPORT_SYMBOL(dcache_inode_lock
);
84 static struct kmem_cache
*dentry_cache __read_mostly
;
86 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
89 * This is the single most critical data structure when it comes
90 * to the dcache: the hashtable for lookups. Somebody should try
91 * to make this good - I've just made it work.
93 * This hash-function tries to avoid losing too many bits of hash
94 * information, yet avoid using a prime hash-size or similar.
96 #define D_HASHBITS d_hash_shift
97 #define D_HASHMASK d_hash_mask
99 static unsigned int d_hash_mask __read_mostly
;
100 static unsigned int d_hash_shift __read_mostly
;
101 static struct hlist_head
*dentry_hashtable __read_mostly
;
103 /* Statistics gathering. */
104 struct dentry_stat_t dentry_stat
= {
108 static DEFINE_PER_CPU(unsigned int, nr_dentry
);
110 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
111 static int get_nr_dentry(void)
115 for_each_possible_cpu(i
)
116 sum
+= per_cpu(nr_dentry
, i
);
117 return sum
< 0 ? 0 : sum
;
120 int proc_nr_dentry(ctl_table
*table
, int write
, void __user
*buffer
,
121 size_t *lenp
, loff_t
*ppos
)
123 dentry_stat
.nr_dentry
= get_nr_dentry();
124 return proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
128 static void __d_free(struct rcu_head
*head
)
130 struct dentry
*dentry
= container_of(head
, struct dentry
, d_u
.d_rcu
);
132 WARN_ON(!list_empty(&dentry
->d_alias
));
133 if (dname_external(dentry
))
134 kfree(dentry
->d_name
.name
);
135 kmem_cache_free(dentry_cache
, dentry
);
141 static void d_free(struct dentry
*dentry
)
143 BUG_ON(dentry
->d_count
);
144 this_cpu_dec(nr_dentry
);
145 if (dentry
->d_op
&& dentry
->d_op
->d_release
)
146 dentry
->d_op
->d_release(dentry
);
148 /* if dentry was never inserted into hash, immediate free is OK */
149 if (hlist_unhashed(&dentry
->d_hash
))
150 __d_free(&dentry
->d_u
.d_rcu
);
152 call_rcu(&dentry
->d_u
.d_rcu
, __d_free
);
156 * dentry_rcuwalk_barrier - invalidate in-progress rcu-walk lookups
157 * After this call, in-progress rcu-walk path lookup will fail. This
158 * should be called after unhashing, and after changing d_inode (if
159 * the dentry has not already been unhashed).
161 static inline void dentry_rcuwalk_barrier(struct dentry
*dentry
)
163 assert_spin_locked(&dentry
->d_lock
);
164 /* Go through a barrier */
165 write_seqcount_barrier(&dentry
->d_seq
);
169 * Release the dentry's inode, using the filesystem
170 * d_iput() operation if defined. Dentry has no refcount
173 static void dentry_iput(struct dentry
* dentry
)
174 __releases(dentry
->d_lock
)
175 __releases(dcache_inode_lock
)
177 struct inode
*inode
= dentry
->d_inode
;
179 dentry
->d_inode
= NULL
;
180 list_del_init(&dentry
->d_alias
);
181 spin_unlock(&dentry
->d_lock
);
182 spin_unlock(&dcache_inode_lock
);
184 fsnotify_inoderemove(inode
);
185 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
186 dentry
->d_op
->d_iput(dentry
, inode
);
190 spin_unlock(&dentry
->d_lock
);
191 spin_unlock(&dcache_inode_lock
);
196 * Release the dentry's inode, using the filesystem
197 * d_iput() operation if defined. dentry remains in-use.
199 static void dentry_unlink_inode(struct dentry
* dentry
)
200 __releases(dentry
->d_lock
)
201 __releases(dcache_inode_lock
)
203 struct inode
*inode
= dentry
->d_inode
;
204 dentry
->d_inode
= NULL
;
205 list_del_init(&dentry
->d_alias
);
206 dentry_rcuwalk_barrier(dentry
);
207 spin_unlock(&dentry
->d_lock
);
208 spin_unlock(&dcache_inode_lock
);
210 fsnotify_inoderemove(inode
);
211 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
212 dentry
->d_op
->d_iput(dentry
, inode
);
218 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
220 static void dentry_lru_add(struct dentry
*dentry
)
222 if (list_empty(&dentry
->d_lru
)) {
223 spin_lock(&dcache_lru_lock
);
224 list_add(&dentry
->d_lru
, &dentry
->d_sb
->s_dentry_lru
);
225 dentry
->d_sb
->s_nr_dentry_unused
++;
226 dentry_stat
.nr_unused
++;
227 spin_unlock(&dcache_lru_lock
);
231 static void __dentry_lru_del(struct dentry
*dentry
)
233 list_del_init(&dentry
->d_lru
);
234 dentry
->d_sb
->s_nr_dentry_unused
--;
235 dentry_stat
.nr_unused
--;
238 static void dentry_lru_del(struct dentry
*dentry
)
240 if (!list_empty(&dentry
->d_lru
)) {
241 spin_lock(&dcache_lru_lock
);
242 __dentry_lru_del(dentry
);
243 spin_unlock(&dcache_lru_lock
);
247 static void dentry_lru_move_tail(struct dentry
*dentry
)
249 spin_lock(&dcache_lru_lock
);
250 if (list_empty(&dentry
->d_lru
)) {
251 list_add_tail(&dentry
->d_lru
, &dentry
->d_sb
->s_dentry_lru
);
252 dentry
->d_sb
->s_nr_dentry_unused
++;
253 dentry_stat
.nr_unused
++;
255 list_move_tail(&dentry
->d_lru
, &dentry
->d_sb
->s_dentry_lru
);
257 spin_unlock(&dcache_lru_lock
);
261 * d_kill - kill dentry and return parent
262 * @dentry: dentry to kill
264 * The dentry must already be unhashed and removed from the LRU.
266 * If this is the root of the dentry tree, return NULL.
268 * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by
271 static struct dentry
*d_kill(struct dentry
*dentry
, struct dentry
*parent
)
272 __releases(dentry
->d_lock
)
273 __releases(parent
->d_lock
)
274 __releases(dcache_inode_lock
)
276 dentry
->d_parent
= NULL
;
277 list_del(&dentry
->d_u
.d_child
);
279 spin_unlock(&parent
->d_lock
);
282 * dentry_iput drops the locks, at which point nobody (except
283 * transient RCU lookups) can reach this dentry.
290 * d_drop - drop a dentry
291 * @dentry: dentry to drop
293 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
294 * be found through a VFS lookup any more. Note that this is different from
295 * deleting the dentry - d_delete will try to mark the dentry negative if
296 * possible, giving a successful _negative_ lookup, while d_drop will
297 * just make the cache lookup fail.
299 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
300 * reason (NFS timeouts or autofs deletes).
302 * __d_drop requires dentry->d_lock.
304 void __d_drop(struct dentry
*dentry
)
306 if (!(dentry
->d_flags
& DCACHE_UNHASHED
)) {
307 dentry
->d_flags
|= DCACHE_UNHASHED
;
308 spin_lock(&dcache_hash_lock
);
309 hlist_del_rcu(&dentry
->d_hash
);
310 spin_unlock(&dcache_hash_lock
);
311 dentry_rcuwalk_barrier(dentry
);
314 EXPORT_SYMBOL(__d_drop
);
316 void d_drop(struct dentry
*dentry
)
318 spin_lock(&dentry
->d_lock
);
320 spin_unlock(&dentry
->d_lock
);
322 EXPORT_SYMBOL(d_drop
);
325 * Finish off a dentry we've decided to kill.
326 * dentry->d_lock must be held, returns with it unlocked.
327 * If ref is non-zero, then decrement the refcount too.
328 * Returns dentry requiring refcount drop, or NULL if we're done.
330 static inline struct dentry
*dentry_kill(struct dentry
*dentry
, int ref
)
331 __releases(dentry
->d_lock
)
333 struct dentry
*parent
;
335 if (!spin_trylock(&dcache_inode_lock
)) {
337 spin_unlock(&dentry
->d_lock
);
339 return dentry
; /* try again with same dentry */
344 parent
= dentry
->d_parent
;
345 if (parent
&& !spin_trylock(&parent
->d_lock
)) {
346 spin_unlock(&dcache_inode_lock
);
352 /* if dentry was on the d_lru list delete it from there */
353 dentry_lru_del(dentry
);
354 /* if it was on the hash then remove it */
356 return d_kill(dentry
, parent
);
362 * This is complicated by the fact that we do not want to put
363 * dentries that are no longer on any hash chain on the unused
364 * list: we'd much rather just get rid of them immediately.
366 * However, that implies that we have to traverse the dentry
367 * tree upwards to the parents which might _also_ now be
368 * scheduled for deletion (it may have been only waiting for
369 * its last child to go away).
371 * This tail recursion is done by hand as we don't want to depend
372 * on the compiler to always get this right (gcc generally doesn't).
373 * Real recursion would eat up our stack space.
377 * dput - release a dentry
378 * @dentry: dentry to release
380 * Release a dentry. This will drop the usage count and if appropriate
381 * call the dentry unlink method as well as removing it from the queues and
382 * releasing its resources. If the parent dentries were scheduled for release
383 * they too may now get deleted.
385 void dput(struct dentry
*dentry
)
391 if (dentry
->d_count
== 1)
393 spin_lock(&dentry
->d_lock
);
394 BUG_ON(!dentry
->d_count
);
395 if (dentry
->d_count
> 1) {
397 spin_unlock(&dentry
->d_lock
);
401 if (dentry
->d_op
&& dentry
->d_op
->d_delete
) {
402 if (dentry
->d_op
->d_delete(dentry
))
406 /* Unreachable? Get rid of it */
407 if (d_unhashed(dentry
))
410 /* Otherwise leave it cached and ensure it's on the LRU */
411 dentry
->d_flags
|= DCACHE_REFERENCED
;
412 dentry_lru_add(dentry
);
415 spin_unlock(&dentry
->d_lock
);
419 dentry
= dentry_kill(dentry
, 1);
426 * d_invalidate - invalidate a dentry
427 * @dentry: dentry to invalidate
429 * Try to invalidate the dentry if it turns out to be
430 * possible. If there are other dentries that can be
431 * reached through this one we can't delete it and we
432 * return -EBUSY. On success we return 0.
437 int d_invalidate(struct dentry
* dentry
)
440 * If it's already been dropped, return OK.
442 spin_lock(&dentry
->d_lock
);
443 if (d_unhashed(dentry
)) {
444 spin_unlock(&dentry
->d_lock
);
448 * Check whether to do a partial shrink_dcache
449 * to get rid of unused child entries.
451 if (!list_empty(&dentry
->d_subdirs
)) {
452 spin_unlock(&dentry
->d_lock
);
453 shrink_dcache_parent(dentry
);
454 spin_lock(&dentry
->d_lock
);
458 * Somebody else still using it?
460 * If it's a directory, we can't drop it
461 * for fear of somebody re-populating it
462 * with children (even though dropping it
463 * would make it unreachable from the root,
464 * we might still populate it if it was a
465 * working directory or similar).
467 if (dentry
->d_count
> 1) {
468 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
)) {
469 spin_unlock(&dentry
->d_lock
);
475 spin_unlock(&dentry
->d_lock
);
478 EXPORT_SYMBOL(d_invalidate
);
480 /* This must be called with d_lock held */
481 static inline void __dget_dlock(struct dentry
*dentry
)
486 static inline void __dget(struct dentry
*dentry
)
488 spin_lock(&dentry
->d_lock
);
489 __dget_dlock(dentry
);
490 spin_unlock(&dentry
->d_lock
);
493 struct dentry
*dget_parent(struct dentry
*dentry
)
499 * Don't need rcu_dereference because we re-check it was correct under
503 ret
= dentry
->d_parent
;
508 spin_lock(&ret
->d_lock
);
509 if (unlikely(ret
!= dentry
->d_parent
)) {
510 spin_unlock(&ret
->d_lock
);
515 BUG_ON(!ret
->d_count
);
517 spin_unlock(&ret
->d_lock
);
521 EXPORT_SYMBOL(dget_parent
);
524 * d_find_alias - grab a hashed alias of inode
525 * @inode: inode in question
526 * @want_discon: flag, used by d_splice_alias, to request
527 * that only a DISCONNECTED alias be returned.
529 * If inode has a hashed alias, or is a directory and has any alias,
530 * acquire the reference to alias and return it. Otherwise return NULL.
531 * Notice that if inode is a directory there can be only one alias and
532 * it can be unhashed only if it has no children, or if it is the root
535 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
536 * any other hashed alias over that one unless @want_discon is set,
537 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
539 static struct dentry
*__d_find_alias(struct inode
*inode
, int want_discon
)
541 struct dentry
*alias
, *discon_alias
;
545 list_for_each_entry(alias
, &inode
->i_dentry
, d_alias
) {
546 spin_lock(&alias
->d_lock
);
547 if (S_ISDIR(inode
->i_mode
) || !d_unhashed(alias
)) {
548 if (IS_ROOT(alias
) &&
549 (alias
->d_flags
& DCACHE_DISCONNECTED
)) {
550 discon_alias
= alias
;
551 } else if (!want_discon
) {
553 spin_unlock(&alias
->d_lock
);
557 spin_unlock(&alias
->d_lock
);
560 alias
= discon_alias
;
561 spin_lock(&alias
->d_lock
);
562 if (S_ISDIR(inode
->i_mode
) || !d_unhashed(alias
)) {
563 if (IS_ROOT(alias
) &&
564 (alias
->d_flags
& DCACHE_DISCONNECTED
)) {
566 spin_unlock(&alias
->d_lock
);
570 spin_unlock(&alias
->d_lock
);
576 struct dentry
*d_find_alias(struct inode
*inode
)
578 struct dentry
*de
= NULL
;
580 if (!list_empty(&inode
->i_dentry
)) {
581 spin_lock(&dcache_inode_lock
);
582 de
= __d_find_alias(inode
, 0);
583 spin_unlock(&dcache_inode_lock
);
587 EXPORT_SYMBOL(d_find_alias
);
590 * Try to kill dentries associated with this inode.
591 * WARNING: you must own a reference to inode.
593 void d_prune_aliases(struct inode
*inode
)
595 struct dentry
*dentry
;
597 spin_lock(&dcache_inode_lock
);
598 list_for_each_entry(dentry
, &inode
->i_dentry
, d_alias
) {
599 spin_lock(&dentry
->d_lock
);
600 if (!dentry
->d_count
) {
601 __dget_dlock(dentry
);
603 spin_unlock(&dentry
->d_lock
);
604 spin_unlock(&dcache_inode_lock
);
608 spin_unlock(&dentry
->d_lock
);
610 spin_unlock(&dcache_inode_lock
);
612 EXPORT_SYMBOL(d_prune_aliases
);
615 * Try to throw away a dentry - free the inode, dput the parent.
616 * Requires dentry->d_lock is held, and dentry->d_count == 0.
617 * Releases dentry->d_lock.
619 * This may fail if locks cannot be acquired no problem, just try again.
621 static void try_prune_one_dentry(struct dentry
*dentry
)
622 __releases(dentry
->d_lock
)
624 struct dentry
*parent
;
626 parent
= dentry_kill(dentry
, 0);
628 * If dentry_kill returns NULL, we have nothing more to do.
629 * if it returns the same dentry, trylocks failed. In either
630 * case, just loop again.
632 * Otherwise, we need to prune ancestors too. This is necessary
633 * to prevent quadratic behavior of shrink_dcache_parent(), but
634 * is also expected to be beneficial in reducing dentry cache
639 if (parent
== dentry
)
642 /* Prune ancestors. */
645 spin_lock(&dentry
->d_lock
);
646 if (dentry
->d_count
> 1) {
648 spin_unlock(&dentry
->d_lock
);
651 dentry
= dentry_kill(dentry
, 1);
655 static void shrink_dentry_list(struct list_head
*list
)
657 struct dentry
*dentry
;
661 dentry
= list_entry_rcu(list
->prev
, struct dentry
, d_lru
);
662 if (&dentry
->d_lru
== list
)
664 spin_lock(&dentry
->d_lock
);
665 if (dentry
!= list_entry(list
->prev
, struct dentry
, d_lru
)) {
666 spin_unlock(&dentry
->d_lock
);
671 * We found an inuse dentry which was not removed from
672 * the LRU because of laziness during lookup. Do not free
673 * it - just keep it off the LRU list.
675 if (dentry
->d_count
) {
676 dentry_lru_del(dentry
);
677 spin_unlock(&dentry
->d_lock
);
683 try_prune_one_dentry(dentry
);
691 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
692 * @sb: superblock to shrink dentry LRU.
693 * @count: number of entries to prune
694 * @flags: flags to control the dentry processing
696 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
698 static void __shrink_dcache_sb(struct super_block
*sb
, int *count
, int flags
)
700 /* called from prune_dcache() and shrink_dcache_parent() */
701 struct dentry
*dentry
;
702 LIST_HEAD(referenced
);
707 spin_lock(&dcache_lru_lock
);
708 while (!list_empty(&sb
->s_dentry_lru
)) {
709 dentry
= list_entry(sb
->s_dentry_lru
.prev
,
710 struct dentry
, d_lru
);
711 BUG_ON(dentry
->d_sb
!= sb
);
713 if (!spin_trylock(&dentry
->d_lock
)) {
714 spin_unlock(&dcache_lru_lock
);
720 * If we are honouring the DCACHE_REFERENCED flag and the
721 * dentry has this flag set, don't free it. Clear the flag
722 * and put it back on the LRU.
724 if (flags
& DCACHE_REFERENCED
&&
725 dentry
->d_flags
& DCACHE_REFERENCED
) {
726 dentry
->d_flags
&= ~DCACHE_REFERENCED
;
727 list_move(&dentry
->d_lru
, &referenced
);
728 spin_unlock(&dentry
->d_lock
);
730 list_move_tail(&dentry
->d_lru
, &tmp
);
731 spin_unlock(&dentry
->d_lock
);
735 cond_resched_lock(&dcache_lru_lock
);
737 if (!list_empty(&referenced
))
738 list_splice(&referenced
, &sb
->s_dentry_lru
);
739 spin_unlock(&dcache_lru_lock
);
741 shrink_dentry_list(&tmp
);
747 * prune_dcache - shrink the dcache
748 * @count: number of entries to try to free
750 * Shrink the dcache. This is done when we need more memory, or simply when we
751 * need to unmount something (at which point we need to unuse all dentries).
753 * This function may fail to free any resources if all the dentries are in use.
755 static void prune_dcache(int count
)
757 struct super_block
*sb
, *p
= NULL
;
759 int unused
= dentry_stat
.nr_unused
;
763 if (unused
== 0 || count
== 0)
768 prune_ratio
= unused
/ count
;
770 list_for_each_entry(sb
, &super_blocks
, s_list
) {
771 if (list_empty(&sb
->s_instances
))
773 if (sb
->s_nr_dentry_unused
== 0)
776 /* Now, we reclaim unused dentrins with fairness.
777 * We reclaim them same percentage from each superblock.
778 * We calculate number of dentries to scan on this sb
779 * as follows, but the implementation is arranged to avoid
781 * number of dentries to scan on this sb =
782 * count * (number of dentries on this sb /
783 * number of dentries in the machine)
785 spin_unlock(&sb_lock
);
786 if (prune_ratio
!= 1)
787 w_count
= (sb
->s_nr_dentry_unused
/ prune_ratio
) + 1;
789 w_count
= sb
->s_nr_dentry_unused
;
792 * We need to be sure this filesystem isn't being unmounted,
793 * otherwise we could race with generic_shutdown_super(), and
794 * end up holding a reference to an inode while the filesystem
795 * is unmounted. So we try to get s_umount, and make sure
798 if (down_read_trylock(&sb
->s_umount
)) {
799 if ((sb
->s_root
!= NULL
) &&
800 (!list_empty(&sb
->s_dentry_lru
))) {
801 __shrink_dcache_sb(sb
, &w_count
,
805 up_read(&sb
->s_umount
);
812 /* more work left to do? */
818 spin_unlock(&sb_lock
);
822 * shrink_dcache_sb - shrink dcache for a superblock
825 * Shrink the dcache for the specified super block. This is used to free
826 * the dcache before unmounting a file system.
828 void shrink_dcache_sb(struct super_block
*sb
)
832 spin_lock(&dcache_lru_lock
);
833 while (!list_empty(&sb
->s_dentry_lru
)) {
834 list_splice_init(&sb
->s_dentry_lru
, &tmp
);
835 spin_unlock(&dcache_lru_lock
);
836 shrink_dentry_list(&tmp
);
837 spin_lock(&dcache_lru_lock
);
839 spin_unlock(&dcache_lru_lock
);
841 EXPORT_SYMBOL(shrink_dcache_sb
);
844 * destroy a single subtree of dentries for unmount
845 * - see the comments on shrink_dcache_for_umount() for a description of the
848 static void shrink_dcache_for_umount_subtree(struct dentry
*dentry
)
850 struct dentry
*parent
;
851 unsigned detached
= 0;
853 BUG_ON(!IS_ROOT(dentry
));
855 /* detach this root from the system */
856 spin_lock(&dentry
->d_lock
);
857 dentry_lru_del(dentry
);
859 spin_unlock(&dentry
->d_lock
);
862 /* descend to the first leaf in the current subtree */
863 while (!list_empty(&dentry
->d_subdirs
)) {
866 /* this is a branch with children - detach all of them
867 * from the system in one go */
868 spin_lock(&dentry
->d_lock
);
869 list_for_each_entry(loop
, &dentry
->d_subdirs
,
871 spin_lock_nested(&loop
->d_lock
,
872 DENTRY_D_LOCK_NESTED
);
873 dentry_lru_del(loop
);
875 spin_unlock(&loop
->d_lock
);
877 spin_unlock(&dentry
->d_lock
);
879 /* move to the first child */
880 dentry
= list_entry(dentry
->d_subdirs
.next
,
881 struct dentry
, d_u
.d_child
);
884 /* consume the dentries from this leaf up through its parents
885 * until we find one with children or run out altogether */
889 if (dentry
->d_count
!= 0) {
891 "BUG: Dentry %p{i=%lx,n=%s}"
893 " [unmount of %s %s]\n",
896 dentry
->d_inode
->i_ino
: 0UL,
899 dentry
->d_sb
->s_type
->name
,
904 if (IS_ROOT(dentry
)) {
906 list_del(&dentry
->d_u
.d_child
);
908 parent
= dentry
->d_parent
;
909 spin_lock(&parent
->d_lock
);
911 list_del(&dentry
->d_u
.d_child
);
912 spin_unlock(&parent
->d_lock
);
917 inode
= dentry
->d_inode
;
919 dentry
->d_inode
= NULL
;
920 list_del_init(&dentry
->d_alias
);
921 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
922 dentry
->d_op
->d_iput(dentry
, inode
);
929 /* finished when we fall off the top of the tree,
930 * otherwise we ascend to the parent and move to the
931 * next sibling if there is one */
935 } while (list_empty(&dentry
->d_subdirs
));
937 dentry
= list_entry(dentry
->d_subdirs
.next
,
938 struct dentry
, d_u
.d_child
);
943 * destroy the dentries attached to a superblock on unmounting
944 * - we don't need to use dentry->d_lock because:
945 * - the superblock is detached from all mountings and open files, so the
946 * dentry trees will not be rearranged by the VFS
947 * - s_umount is write-locked, so the memory pressure shrinker will ignore
948 * any dentries belonging to this superblock that it comes across
949 * - the filesystem itself is no longer permitted to rearrange the dentries
952 void shrink_dcache_for_umount(struct super_block
*sb
)
954 struct dentry
*dentry
;
956 if (down_read_trylock(&sb
->s_umount
))
961 spin_lock(&dentry
->d_lock
);
963 spin_unlock(&dentry
->d_lock
);
964 shrink_dcache_for_umount_subtree(dentry
);
966 while (!hlist_empty(&sb
->s_anon
)) {
967 dentry
= hlist_entry(sb
->s_anon
.first
, struct dentry
, d_hash
);
968 shrink_dcache_for_umount_subtree(dentry
);
973 * Search for at least 1 mount point in the dentry's subdirs.
974 * We descend to the next level whenever the d_subdirs
975 * list is non-empty and continue searching.
979 * have_submounts - check for mounts over a dentry
980 * @parent: dentry to check.
982 * Return true if the parent or its subdirectories contain
985 int have_submounts(struct dentry
*parent
)
987 struct dentry
*this_parent
;
988 struct list_head
*next
;
992 seq
= read_seqbegin(&rename_lock
);
994 this_parent
= parent
;
996 if (d_mountpoint(parent
))
998 spin_lock(&this_parent
->d_lock
);
1000 next
= this_parent
->d_subdirs
.next
;
1002 while (next
!= &this_parent
->d_subdirs
) {
1003 struct list_head
*tmp
= next
;
1004 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
1007 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1008 /* Have we found a mount point ? */
1009 if (d_mountpoint(dentry
)) {
1010 spin_unlock(&dentry
->d_lock
);
1011 spin_unlock(&this_parent
->d_lock
);
1014 if (!list_empty(&dentry
->d_subdirs
)) {
1015 spin_unlock(&this_parent
->d_lock
);
1016 spin_release(&dentry
->d_lock
.dep_map
, 1, _RET_IP_
);
1017 this_parent
= dentry
;
1018 spin_acquire(&this_parent
->d_lock
.dep_map
, 0, 1, _RET_IP_
);
1021 spin_unlock(&dentry
->d_lock
);
1024 * All done at this level ... ascend and resume the search.
1026 if (this_parent
!= parent
) {
1028 struct dentry
*child
;
1030 tmp
= this_parent
->d_parent
;
1032 spin_unlock(&this_parent
->d_lock
);
1033 child
= this_parent
;
1035 spin_lock(&this_parent
->d_lock
);
1036 /* might go back up the wrong parent if we have had a rename
1038 if (this_parent
!= child
->d_parent
||
1039 (!locked
&& read_seqretry(&rename_lock
, seq
))) {
1040 spin_unlock(&this_parent
->d_lock
);
1045 next
= child
->d_u
.d_child
.next
;
1048 spin_unlock(&this_parent
->d_lock
);
1049 if (!locked
&& read_seqretry(&rename_lock
, seq
))
1052 write_sequnlock(&rename_lock
);
1053 return 0; /* No mount points found in tree */
1055 if (!locked
&& read_seqretry(&rename_lock
, seq
))
1058 write_sequnlock(&rename_lock
);
1063 write_seqlock(&rename_lock
);
1066 EXPORT_SYMBOL(have_submounts
);
1069 * Search the dentry child list for the specified parent,
1070 * and move any unused dentries to the end of the unused
1071 * list for prune_dcache(). We descend to the next level
1072 * whenever the d_subdirs list is non-empty and continue
1075 * It returns zero iff there are no unused children,
1076 * otherwise it returns the number of children moved to
1077 * the end of the unused list. This may not be the total
1078 * number of unused children, because select_parent can
1079 * drop the lock and return early due to latency
1082 static int select_parent(struct dentry
* parent
)
1084 struct dentry
*this_parent
;
1085 struct list_head
*next
;
1090 seq
= read_seqbegin(&rename_lock
);
1092 this_parent
= parent
;
1093 spin_lock(&this_parent
->d_lock
);
1095 next
= this_parent
->d_subdirs
.next
;
1097 while (next
!= &this_parent
->d_subdirs
) {
1098 struct list_head
*tmp
= next
;
1099 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
1102 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1105 * move only zero ref count dentries to the end
1106 * of the unused list for prune_dcache
1108 if (!dentry
->d_count
) {
1109 dentry_lru_move_tail(dentry
);
1112 dentry_lru_del(dentry
);
1116 * We can return to the caller if we have found some (this
1117 * ensures forward progress). We'll be coming back to find
1120 if (found
&& need_resched()) {
1121 spin_unlock(&dentry
->d_lock
);
1126 * Descend a level if the d_subdirs list is non-empty.
1128 if (!list_empty(&dentry
->d_subdirs
)) {
1129 spin_unlock(&this_parent
->d_lock
);
1130 spin_release(&dentry
->d_lock
.dep_map
, 1, _RET_IP_
);
1131 this_parent
= dentry
;
1132 spin_acquire(&this_parent
->d_lock
.dep_map
, 0, 1, _RET_IP_
);
1136 spin_unlock(&dentry
->d_lock
);
1139 * All done at this level ... ascend and resume the search.
1141 if (this_parent
!= parent
) {
1143 struct dentry
*child
;
1145 tmp
= this_parent
->d_parent
;
1147 spin_unlock(&this_parent
->d_lock
);
1148 child
= this_parent
;
1150 spin_lock(&this_parent
->d_lock
);
1151 /* might go back up the wrong parent if we have had a rename
1153 if (this_parent
!= child
->d_parent
||
1154 (!locked
&& read_seqretry(&rename_lock
, seq
))) {
1155 spin_unlock(&this_parent
->d_lock
);
1160 next
= child
->d_u
.d_child
.next
;
1164 spin_unlock(&this_parent
->d_lock
);
1165 if (!locked
&& read_seqretry(&rename_lock
, seq
))
1168 write_sequnlock(&rename_lock
);
1175 write_seqlock(&rename_lock
);
1180 * shrink_dcache_parent - prune dcache
1181 * @parent: parent of entries to prune
1183 * Prune the dcache to remove unused children of the parent dentry.
1186 void shrink_dcache_parent(struct dentry
* parent
)
1188 struct super_block
*sb
= parent
->d_sb
;
1191 while ((found
= select_parent(parent
)) != 0)
1192 __shrink_dcache_sb(sb
, &found
, 0);
1194 EXPORT_SYMBOL(shrink_dcache_parent
);
1197 * Scan `nr' dentries and return the number which remain.
1199 * We need to avoid reentering the filesystem if the caller is performing a
1200 * GFP_NOFS allocation attempt. One example deadlock is:
1202 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1203 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1204 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1206 * In this case we return -1 to tell the caller that we baled.
1208 static int shrink_dcache_memory(struct shrinker
*shrink
, int nr
, gfp_t gfp_mask
)
1211 if (!(gfp_mask
& __GFP_FS
))
1216 return (dentry_stat
.nr_unused
/ 100) * sysctl_vfs_cache_pressure
;
1219 static struct shrinker dcache_shrinker
= {
1220 .shrink
= shrink_dcache_memory
,
1221 .seeks
= DEFAULT_SEEKS
,
1225 * d_alloc - allocate a dcache entry
1226 * @parent: parent of entry to allocate
1227 * @name: qstr of the name
1229 * Allocates a dentry. It returns %NULL if there is insufficient memory
1230 * available. On a success the dentry is returned. The name passed in is
1231 * copied and the copy passed in may be reused after this call.
1234 struct dentry
*d_alloc(struct dentry
* parent
, const struct qstr
*name
)
1236 struct dentry
*dentry
;
1239 dentry
= kmem_cache_alloc(dentry_cache
, GFP_KERNEL
);
1243 if (name
->len
> DNAME_INLINE_LEN
-1) {
1244 dname
= kmalloc(name
->len
+ 1, GFP_KERNEL
);
1246 kmem_cache_free(dentry_cache
, dentry
);
1250 dname
= dentry
->d_iname
;
1252 dentry
->d_name
.name
= dname
;
1254 dentry
->d_name
.len
= name
->len
;
1255 dentry
->d_name
.hash
= name
->hash
;
1256 memcpy(dname
, name
->name
, name
->len
);
1257 dname
[name
->len
] = 0;
1259 dentry
->d_count
= 1;
1260 dentry
->d_flags
= DCACHE_UNHASHED
;
1261 spin_lock_init(&dentry
->d_lock
);
1262 seqcount_init(&dentry
->d_seq
);
1263 dentry
->d_inode
= NULL
;
1264 dentry
->d_parent
= NULL
;
1265 dentry
->d_sb
= NULL
;
1266 dentry
->d_op
= NULL
;
1267 dentry
->d_fsdata
= NULL
;
1268 INIT_HLIST_NODE(&dentry
->d_hash
);
1269 INIT_LIST_HEAD(&dentry
->d_lru
);
1270 INIT_LIST_HEAD(&dentry
->d_subdirs
);
1271 INIT_LIST_HEAD(&dentry
->d_alias
);
1272 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
1275 spin_lock(&parent
->d_lock
);
1277 * don't need child lock because it is not subject
1278 * to concurrency here
1280 __dget_dlock(parent
);
1281 dentry
->d_parent
= parent
;
1282 dentry
->d_sb
= parent
->d_sb
;
1283 list_add(&dentry
->d_u
.d_child
, &parent
->d_subdirs
);
1284 spin_unlock(&parent
->d_lock
);
1287 this_cpu_inc(nr_dentry
);
1291 EXPORT_SYMBOL(d_alloc
);
1293 struct dentry
*d_alloc_name(struct dentry
*parent
, const char *name
)
1298 q
.len
= strlen(name
);
1299 q
.hash
= full_name_hash(q
.name
, q
.len
);
1300 return d_alloc(parent
, &q
);
1302 EXPORT_SYMBOL(d_alloc_name
);
1304 static void __d_instantiate(struct dentry
*dentry
, struct inode
*inode
)
1306 spin_lock(&dentry
->d_lock
);
1308 list_add(&dentry
->d_alias
, &inode
->i_dentry
);
1309 dentry
->d_inode
= inode
;
1310 dentry_rcuwalk_barrier(dentry
);
1311 spin_unlock(&dentry
->d_lock
);
1312 fsnotify_d_instantiate(dentry
, inode
);
1316 * d_instantiate - fill in inode information for a dentry
1317 * @entry: dentry to complete
1318 * @inode: inode to attach to this dentry
1320 * Fill in inode information in the entry.
1322 * This turns negative dentries into productive full members
1325 * NOTE! This assumes that the inode count has been incremented
1326 * (or otherwise set) by the caller to indicate that it is now
1327 * in use by the dcache.
1330 void d_instantiate(struct dentry
*entry
, struct inode
* inode
)
1332 BUG_ON(!list_empty(&entry
->d_alias
));
1333 spin_lock(&dcache_inode_lock
);
1334 __d_instantiate(entry
, inode
);
1335 spin_unlock(&dcache_inode_lock
);
1336 security_d_instantiate(entry
, inode
);
1338 EXPORT_SYMBOL(d_instantiate
);
1341 * d_instantiate_unique - instantiate a non-aliased dentry
1342 * @entry: dentry to instantiate
1343 * @inode: inode to attach to this dentry
1345 * Fill in inode information in the entry. On success, it returns NULL.
1346 * If an unhashed alias of "entry" already exists, then we return the
1347 * aliased dentry instead and drop one reference to inode.
1349 * Note that in order to avoid conflicts with rename() etc, the caller
1350 * had better be holding the parent directory semaphore.
1352 * This also assumes that the inode count has been incremented
1353 * (or otherwise set) by the caller to indicate that it is now
1354 * in use by the dcache.
1356 static struct dentry
*__d_instantiate_unique(struct dentry
*entry
,
1357 struct inode
*inode
)
1359 struct dentry
*alias
;
1360 int len
= entry
->d_name
.len
;
1361 const char *name
= entry
->d_name
.name
;
1362 unsigned int hash
= entry
->d_name
.hash
;
1365 __d_instantiate(entry
, NULL
);
1369 list_for_each_entry(alias
, &inode
->i_dentry
, d_alias
) {
1370 struct qstr
*qstr
= &alias
->d_name
;
1373 * Don't need alias->d_lock here, because aliases with
1374 * d_parent == entry->d_parent are not subject to name or
1375 * parent changes, because the parent inode i_mutex is held.
1377 if (qstr
->hash
!= hash
)
1379 if (alias
->d_parent
!= entry
->d_parent
)
1381 if (qstr
->len
!= len
)
1383 if (memcmp(qstr
->name
, name
, len
))
1389 __d_instantiate(entry
, inode
);
1393 struct dentry
*d_instantiate_unique(struct dentry
*entry
, struct inode
*inode
)
1395 struct dentry
*result
;
1397 BUG_ON(!list_empty(&entry
->d_alias
));
1399 spin_lock(&dcache_inode_lock
);
1400 result
= __d_instantiate_unique(entry
, inode
);
1401 spin_unlock(&dcache_inode_lock
);
1404 security_d_instantiate(entry
, inode
);
1408 BUG_ON(!d_unhashed(result
));
1413 EXPORT_SYMBOL(d_instantiate_unique
);
1416 * d_alloc_root - allocate root dentry
1417 * @root_inode: inode to allocate the root for
1419 * Allocate a root ("/") dentry for the inode given. The inode is
1420 * instantiated and returned. %NULL is returned if there is insufficient
1421 * memory or the inode passed is %NULL.
1424 struct dentry
* d_alloc_root(struct inode
* root_inode
)
1426 struct dentry
*res
= NULL
;
1429 static const struct qstr name
= { .name
= "/", .len
= 1 };
1431 res
= d_alloc(NULL
, &name
);
1433 res
->d_sb
= root_inode
->i_sb
;
1434 res
->d_parent
= res
;
1435 d_instantiate(res
, root_inode
);
1440 EXPORT_SYMBOL(d_alloc_root
);
1442 static inline struct hlist_head
*d_hash(struct dentry
*parent
,
1445 hash
+= ((unsigned long) parent
^ GOLDEN_RATIO_PRIME
) / L1_CACHE_BYTES
;
1446 hash
= hash
^ ((hash
^ GOLDEN_RATIO_PRIME
) >> D_HASHBITS
);
1447 return dentry_hashtable
+ (hash
& D_HASHMASK
);
1451 * d_obtain_alias - find or allocate a dentry for a given inode
1452 * @inode: inode to allocate the dentry for
1454 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1455 * similar open by handle operations. The returned dentry may be anonymous,
1456 * or may have a full name (if the inode was already in the cache).
1458 * When called on a directory inode, we must ensure that the inode only ever
1459 * has one dentry. If a dentry is found, that is returned instead of
1460 * allocating a new one.
1462 * On successful return, the reference to the inode has been transferred
1463 * to the dentry. In case of an error the reference on the inode is released.
1464 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1465 * be passed in and will be the error will be propagate to the return value,
1466 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1468 struct dentry
*d_obtain_alias(struct inode
*inode
)
1470 static const struct qstr anonstring
= { .name
= "" };
1475 return ERR_PTR(-ESTALE
);
1477 return ERR_CAST(inode
);
1479 res
= d_find_alias(inode
);
1483 tmp
= d_alloc(NULL
, &anonstring
);
1485 res
= ERR_PTR(-ENOMEM
);
1488 tmp
->d_parent
= tmp
; /* make sure dput doesn't croak */
1491 spin_lock(&dcache_inode_lock
);
1492 res
= __d_find_alias(inode
, 0);
1494 spin_unlock(&dcache_inode_lock
);
1499 /* attach a disconnected dentry */
1500 spin_lock(&tmp
->d_lock
);
1501 tmp
->d_sb
= inode
->i_sb
;
1502 tmp
->d_inode
= inode
;
1503 tmp
->d_flags
|= DCACHE_DISCONNECTED
;
1504 tmp
->d_flags
&= ~DCACHE_UNHASHED
;
1505 list_add(&tmp
->d_alias
, &inode
->i_dentry
);
1506 spin_lock(&dcache_hash_lock
);
1507 hlist_add_head(&tmp
->d_hash
, &inode
->i_sb
->s_anon
);
1508 spin_unlock(&dcache_hash_lock
);
1509 spin_unlock(&tmp
->d_lock
);
1510 spin_unlock(&dcache_inode_lock
);
1518 EXPORT_SYMBOL(d_obtain_alias
);
1521 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1522 * @inode: the inode which may have a disconnected dentry
1523 * @dentry: a negative dentry which we want to point to the inode.
1525 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1526 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1527 * and return it, else simply d_add the inode to the dentry and return NULL.
1529 * This is needed in the lookup routine of any filesystem that is exportable
1530 * (via knfsd) so that we can build dcache paths to directories effectively.
1532 * If a dentry was found and moved, then it is returned. Otherwise NULL
1533 * is returned. This matches the expected return value of ->lookup.
1536 struct dentry
*d_splice_alias(struct inode
*inode
, struct dentry
*dentry
)
1538 struct dentry
*new = NULL
;
1540 if (inode
&& S_ISDIR(inode
->i_mode
)) {
1541 spin_lock(&dcache_inode_lock
);
1542 new = __d_find_alias(inode
, 1);
1544 BUG_ON(!(new->d_flags
& DCACHE_DISCONNECTED
));
1545 spin_unlock(&dcache_inode_lock
);
1546 security_d_instantiate(new, inode
);
1547 d_move(new, dentry
);
1550 /* already taking dcache_inode_lock, so d_add() by hand */
1551 __d_instantiate(dentry
, inode
);
1552 spin_unlock(&dcache_inode_lock
);
1553 security_d_instantiate(dentry
, inode
);
1557 d_add(dentry
, inode
);
1560 EXPORT_SYMBOL(d_splice_alias
);
1563 * d_add_ci - lookup or allocate new dentry with case-exact name
1564 * @inode: the inode case-insensitive lookup has found
1565 * @dentry: the negative dentry that was passed to the parent's lookup func
1566 * @name: the case-exact name to be associated with the returned dentry
1568 * This is to avoid filling the dcache with case-insensitive names to the
1569 * same inode, only the actual correct case is stored in the dcache for
1570 * case-insensitive filesystems.
1572 * For a case-insensitive lookup match and if the the case-exact dentry
1573 * already exists in in the dcache, use it and return it.
1575 * If no entry exists with the exact case name, allocate new dentry with
1576 * the exact case, and return the spliced entry.
1578 struct dentry
*d_add_ci(struct dentry
*dentry
, struct inode
*inode
,
1582 struct dentry
*found
;
1586 * First check if a dentry matching the name already exists,
1587 * if not go ahead and create it now.
1589 found
= d_hash_and_lookup(dentry
->d_parent
, name
);
1591 new = d_alloc(dentry
->d_parent
, name
);
1597 found
= d_splice_alias(inode
, new);
1606 * If a matching dentry exists, and it's not negative use it.
1608 * Decrement the reference count to balance the iget() done
1611 if (found
->d_inode
) {
1612 if (unlikely(found
->d_inode
!= inode
)) {
1613 /* This can't happen because bad inodes are unhashed. */
1614 BUG_ON(!is_bad_inode(inode
));
1615 BUG_ON(!is_bad_inode(found
->d_inode
));
1622 * Negative dentry: instantiate it unless the inode is a directory and
1623 * already has a dentry.
1625 spin_lock(&dcache_inode_lock
);
1626 if (!S_ISDIR(inode
->i_mode
) || list_empty(&inode
->i_dentry
)) {
1627 __d_instantiate(found
, inode
);
1628 spin_unlock(&dcache_inode_lock
);
1629 security_d_instantiate(found
, inode
);
1634 * In case a directory already has a (disconnected) entry grab a
1635 * reference to it, move it in place and use it.
1637 new = list_entry(inode
->i_dentry
.next
, struct dentry
, d_alias
);
1639 spin_unlock(&dcache_inode_lock
);
1640 security_d_instantiate(found
, inode
);
1648 return ERR_PTR(error
);
1650 EXPORT_SYMBOL(d_add_ci
);
1653 * __d_lookup_rcu - search for a dentry (racy, store-free)
1654 * @parent: parent dentry
1655 * @name: qstr of name we wish to find
1656 * @seq: returns d_seq value at the point where the dentry was found
1657 * @inode: returns dentry->d_inode when the inode was found valid.
1658 * Returns: dentry, or NULL
1660 * __d_lookup_rcu is the dcache lookup function for rcu-walk name
1661 * resolution (store-free path walking) design described in
1662 * Documentation/filesystems/path-lookup.txt.
1664 * This is not to be used outside core vfs.
1666 * __d_lookup_rcu must only be used in rcu-walk mode, ie. with vfsmount lock
1667 * held, and rcu_read_lock held. The returned dentry must not be stored into
1668 * without taking d_lock and checking d_seq sequence count against @seq
1671 * A refcount may be taken on the found dentry with the __d_rcu_to_refcount
1674 * Alternatively, __d_lookup_rcu may be called again to look up the child of
1675 * the returned dentry, so long as its parent's seqlock is checked after the
1676 * child is looked up. Thus, an interlocking stepping of sequence lock checks
1677 * is formed, giving integrity down the path walk.
1679 struct dentry
*__d_lookup_rcu(struct dentry
*parent
, struct qstr
*name
,
1680 unsigned *seq
, struct inode
**inode
)
1682 unsigned int len
= name
->len
;
1683 unsigned int hash
= name
->hash
;
1684 const unsigned char *str
= name
->name
;
1685 struct hlist_head
*head
= d_hash(parent
, hash
);
1686 struct hlist_node
*node
;
1687 struct dentry
*dentry
;
1690 * Note: There is significant duplication with __d_lookup_rcu which is
1691 * required to prevent single threaded performance regressions
1692 * especially on architectures where smp_rmb (in seqcounts) are costly.
1693 * Keep the two functions in sync.
1697 * The hash list is protected using RCU.
1699 * Carefully use d_seq when comparing a candidate dentry, to avoid
1700 * races with d_move().
1702 * It is possible that concurrent renames can mess up our list
1703 * walk here and result in missing our dentry, resulting in the
1704 * false-negative result. d_lookup() protects against concurrent
1705 * renames using rename_lock seqlock.
1707 * See Documentation/vfs/dcache-locking.txt for more details.
1709 hlist_for_each_entry_rcu(dentry
, node
, head
, d_hash
) {
1714 if (dentry
->d_name
.hash
!= hash
)
1718 *seq
= read_seqcount_begin(&dentry
->d_seq
);
1719 if (dentry
->d_parent
!= parent
)
1721 if (d_unhashed(dentry
))
1723 tlen
= dentry
->d_name
.len
;
1724 tname
= dentry
->d_name
.name
;
1725 i
= dentry
->d_inode
;
1727 * This seqcount check is required to ensure name and
1728 * len are loaded atomically, so as not to walk off the
1729 * edge of memory when walking. If we could load this
1730 * atomically some other way, we could drop this check.
1732 if (read_seqcount_retry(&dentry
->d_seq
, *seq
))
1734 if (parent
->d_op
&& parent
->d_op
->d_compare
) {
1735 if (parent
->d_op
->d_compare(parent
, *inode
,
1742 if (memcmp(tname
, str
, tlen
))
1746 * No extra seqcount check is required after the name
1747 * compare. The caller must perform a seqcount check in
1748 * order to do anything useful with the returned dentry
1758 * d_lookup - search for a dentry
1759 * @parent: parent dentry
1760 * @name: qstr of name we wish to find
1761 * Returns: dentry, or NULL
1763 * d_lookup searches the children of the parent dentry for the name in
1764 * question. If the dentry is found its reference count is incremented and the
1765 * dentry is returned. The caller must use dput to free the entry when it has
1766 * finished using it. %NULL is returned if the dentry does not exist.
1768 struct dentry
*d_lookup(struct dentry
*parent
, struct qstr
*name
)
1770 struct dentry
*dentry
;
1774 seq
= read_seqbegin(&rename_lock
);
1775 dentry
= __d_lookup(parent
, name
);
1778 } while (read_seqretry(&rename_lock
, seq
));
1781 EXPORT_SYMBOL(d_lookup
);
1784 * __d_lookup - search for a dentry (racy)
1785 * @parent: parent dentry
1786 * @name: qstr of name we wish to find
1787 * Returns: dentry, or NULL
1789 * __d_lookup is like d_lookup, however it may (rarely) return a
1790 * false-negative result due to unrelated rename activity.
1792 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1793 * however it must be used carefully, eg. with a following d_lookup in
1794 * the case of failure.
1796 * __d_lookup callers must be commented.
1798 struct dentry
*__d_lookup(struct dentry
*parent
, struct qstr
*name
)
1800 unsigned int len
= name
->len
;
1801 unsigned int hash
= name
->hash
;
1802 const unsigned char *str
= name
->name
;
1803 struct hlist_head
*head
= d_hash(parent
,hash
);
1804 struct hlist_node
*node
;
1805 struct dentry
*found
= NULL
;
1806 struct dentry
*dentry
;
1809 * Note: There is significant duplication with __d_lookup_rcu which is
1810 * required to prevent single threaded performance regressions
1811 * especially on architectures where smp_rmb (in seqcounts) are costly.
1812 * Keep the two functions in sync.
1816 * The hash list is protected using RCU.
1818 * Take d_lock when comparing a candidate dentry, to avoid races
1821 * It is possible that concurrent renames can mess up our list
1822 * walk here and result in missing our dentry, resulting in the
1823 * false-negative result. d_lookup() protects against concurrent
1824 * renames using rename_lock seqlock.
1826 * See Documentation/vfs/dcache-locking.txt for more details.
1830 hlist_for_each_entry_rcu(dentry
, node
, head
, d_hash
) {
1834 if (dentry
->d_name
.hash
!= hash
)
1837 spin_lock(&dentry
->d_lock
);
1838 if (dentry
->d_parent
!= parent
)
1840 if (d_unhashed(dentry
))
1844 * It is safe to compare names since d_move() cannot
1845 * change the qstr (protected by d_lock).
1847 tlen
= dentry
->d_name
.len
;
1848 tname
= dentry
->d_name
.name
;
1849 if (parent
->d_op
&& parent
->d_op
->d_compare
) {
1850 if (parent
->d_op
->d_compare(parent
, parent
->d_inode
,
1851 dentry
, dentry
->d_inode
,
1857 if (memcmp(tname
, str
, tlen
))
1863 spin_unlock(&dentry
->d_lock
);
1866 spin_unlock(&dentry
->d_lock
);
1874 * d_hash_and_lookup - hash the qstr then search for a dentry
1875 * @dir: Directory to search in
1876 * @name: qstr of name we wish to find
1878 * On hash failure or on lookup failure NULL is returned.
1880 struct dentry
*d_hash_and_lookup(struct dentry
*dir
, struct qstr
*name
)
1882 struct dentry
*dentry
= NULL
;
1885 * Check for a fs-specific hash function. Note that we must
1886 * calculate the standard hash first, as the d_op->d_hash()
1887 * routine may choose to leave the hash value unchanged.
1889 name
->hash
= full_name_hash(name
->name
, name
->len
);
1890 if (dir
->d_op
&& dir
->d_op
->d_hash
) {
1891 if (dir
->d_op
->d_hash(dir
, dir
->d_inode
, name
) < 0)
1894 dentry
= d_lookup(dir
, name
);
1900 * d_validate - verify dentry provided from insecure source (deprecated)
1901 * @dentry: The dentry alleged to be valid child of @dparent
1902 * @dparent: The parent dentry (known to be valid)
1904 * An insecure source has sent us a dentry, here we verify it and dget() it.
1905 * This is used by ncpfs in its readdir implementation.
1906 * Zero is returned in the dentry is invalid.
1908 * This function is slow for big directories, and deprecated, do not use it.
1910 int d_validate(struct dentry
*dentry
, struct dentry
*dparent
)
1912 struct dentry
*child
;
1914 spin_lock(&dparent
->d_lock
);
1915 list_for_each_entry(child
, &dparent
->d_subdirs
, d_u
.d_child
) {
1916 if (dentry
== child
) {
1917 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1918 __dget_dlock(dentry
);
1919 spin_unlock(&dentry
->d_lock
);
1920 spin_unlock(&dparent
->d_lock
);
1924 spin_unlock(&dparent
->d_lock
);
1928 EXPORT_SYMBOL(d_validate
);
1931 * When a file is deleted, we have two options:
1932 * - turn this dentry into a negative dentry
1933 * - unhash this dentry and free it.
1935 * Usually, we want to just turn this into
1936 * a negative dentry, but if anybody else is
1937 * currently using the dentry or the inode
1938 * we can't do that and we fall back on removing
1939 * it from the hash queues and waiting for
1940 * it to be deleted later when it has no users
1944 * d_delete - delete a dentry
1945 * @dentry: The dentry to delete
1947 * Turn the dentry into a negative dentry if possible, otherwise
1948 * remove it from the hash queues so it can be deleted later
1951 void d_delete(struct dentry
* dentry
)
1955 * Are we the only user?
1958 spin_lock(&dentry
->d_lock
);
1959 isdir
= S_ISDIR(dentry
->d_inode
->i_mode
);
1960 if (dentry
->d_count
== 1) {
1961 if (!spin_trylock(&dcache_inode_lock
)) {
1962 spin_unlock(&dentry
->d_lock
);
1966 dentry
->d_flags
&= ~DCACHE_CANT_MOUNT
;
1967 dentry_unlink_inode(dentry
);
1968 fsnotify_nameremove(dentry
, isdir
);
1972 if (!d_unhashed(dentry
))
1975 spin_unlock(&dentry
->d_lock
);
1977 fsnotify_nameremove(dentry
, isdir
);
1979 EXPORT_SYMBOL(d_delete
);
1981 static void __d_rehash(struct dentry
* entry
, struct hlist_head
*list
)
1984 entry
->d_flags
&= ~DCACHE_UNHASHED
;
1985 hlist_add_head_rcu(&entry
->d_hash
, list
);
1988 static void _d_rehash(struct dentry
* entry
)
1990 __d_rehash(entry
, d_hash(entry
->d_parent
, entry
->d_name
.hash
));
1994 * d_rehash - add an entry back to the hash
1995 * @entry: dentry to add to the hash
1997 * Adds a dentry to the hash according to its name.
2000 void d_rehash(struct dentry
* entry
)
2002 spin_lock(&entry
->d_lock
);
2003 spin_lock(&dcache_hash_lock
);
2005 spin_unlock(&dcache_hash_lock
);
2006 spin_unlock(&entry
->d_lock
);
2008 EXPORT_SYMBOL(d_rehash
);
2011 * dentry_update_name_case - update case insensitive dentry with a new name
2012 * @dentry: dentry to be updated
2015 * Update a case insensitive dentry with new case of name.
2017 * dentry must have been returned by d_lookup with name @name. Old and new
2018 * name lengths must match (ie. no d_compare which allows mismatched name
2021 * Parent inode i_mutex must be held over d_lookup and into this call (to
2022 * keep renames and concurrent inserts, and readdir(2) away).
2024 void dentry_update_name_case(struct dentry
*dentry
, struct qstr
*name
)
2026 BUG_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
2027 BUG_ON(dentry
->d_name
.len
!= name
->len
); /* d_lookup gives this */
2029 spin_lock(&dentry
->d_lock
);
2030 write_seqcount_begin(&dentry
->d_seq
);
2031 memcpy((unsigned char *)dentry
->d_name
.name
, name
->name
, name
->len
);
2032 write_seqcount_end(&dentry
->d_seq
);
2033 spin_unlock(&dentry
->d_lock
);
2035 EXPORT_SYMBOL(dentry_update_name_case
);
2037 static void switch_names(struct dentry
*dentry
, struct dentry
*target
)
2039 if (dname_external(target
)) {
2040 if (dname_external(dentry
)) {
2042 * Both external: swap the pointers
2044 swap(target
->d_name
.name
, dentry
->d_name
.name
);
2047 * dentry:internal, target:external. Steal target's
2048 * storage and make target internal.
2050 memcpy(target
->d_iname
, dentry
->d_name
.name
,
2051 dentry
->d_name
.len
+ 1);
2052 dentry
->d_name
.name
= target
->d_name
.name
;
2053 target
->d_name
.name
= target
->d_iname
;
2056 if (dname_external(dentry
)) {
2058 * dentry:external, target:internal. Give dentry's
2059 * storage to target and make dentry internal
2061 memcpy(dentry
->d_iname
, target
->d_name
.name
,
2062 target
->d_name
.len
+ 1);
2063 target
->d_name
.name
= dentry
->d_name
.name
;
2064 dentry
->d_name
.name
= dentry
->d_iname
;
2067 * Both are internal. Just copy target to dentry
2069 memcpy(dentry
->d_iname
, target
->d_name
.name
,
2070 target
->d_name
.len
+ 1);
2071 dentry
->d_name
.len
= target
->d_name
.len
;
2075 swap(dentry
->d_name
.len
, target
->d_name
.len
);
2078 static void dentry_lock_for_move(struct dentry
*dentry
, struct dentry
*target
)
2081 * XXXX: do we really need to take target->d_lock?
2083 if (IS_ROOT(dentry
) || dentry
->d_parent
== target
->d_parent
)
2084 spin_lock(&target
->d_parent
->d_lock
);
2086 if (d_ancestor(dentry
->d_parent
, target
->d_parent
)) {
2087 spin_lock(&dentry
->d_parent
->d_lock
);
2088 spin_lock_nested(&target
->d_parent
->d_lock
,
2089 DENTRY_D_LOCK_NESTED
);
2091 spin_lock(&target
->d_parent
->d_lock
);
2092 spin_lock_nested(&dentry
->d_parent
->d_lock
,
2093 DENTRY_D_LOCK_NESTED
);
2096 if (target
< dentry
) {
2097 spin_lock_nested(&target
->d_lock
, 2);
2098 spin_lock_nested(&dentry
->d_lock
, 3);
2100 spin_lock_nested(&dentry
->d_lock
, 2);
2101 spin_lock_nested(&target
->d_lock
, 3);
2105 static void dentry_unlock_parents_for_move(struct dentry
*dentry
,
2106 struct dentry
*target
)
2108 if (target
->d_parent
!= dentry
->d_parent
)
2109 spin_unlock(&dentry
->d_parent
->d_lock
);
2110 if (target
->d_parent
!= target
)
2111 spin_unlock(&target
->d_parent
->d_lock
);
2115 * When switching names, the actual string doesn't strictly have to
2116 * be preserved in the target - because we're dropping the target
2117 * anyway. As such, we can just do a simple memcpy() to copy over
2118 * the new name before we switch.
2120 * Note that we have to be a lot more careful about getting the hash
2121 * switched - we have to switch the hash value properly even if it
2122 * then no longer matches the actual (corrupted) string of the target.
2123 * The hash value has to match the hash queue that the dentry is on..
2126 * d_move - move a dentry
2127 * @dentry: entry to move
2128 * @target: new dentry
2130 * Update the dcache to reflect the move of a file name. Negative
2131 * dcache entries should not be moved in this way.
2133 void d_move(struct dentry
* dentry
, struct dentry
* target
)
2135 if (!dentry
->d_inode
)
2136 printk(KERN_WARNING
"VFS: moving negative dcache entry\n");
2138 BUG_ON(d_ancestor(dentry
, target
));
2139 BUG_ON(d_ancestor(target
, dentry
));
2141 write_seqlock(&rename_lock
);
2143 dentry_lock_for_move(dentry
, target
);
2145 write_seqcount_begin(&dentry
->d_seq
);
2146 write_seqcount_begin(&target
->d_seq
);
2148 /* Move the dentry to the target hash queue, if on different bucket */
2149 spin_lock(&dcache_hash_lock
);
2150 if (!d_unhashed(dentry
))
2151 hlist_del_rcu(&dentry
->d_hash
);
2152 __d_rehash(dentry
, d_hash(target
->d_parent
, target
->d_name
.hash
));
2153 spin_unlock(&dcache_hash_lock
);
2155 /* Unhash the target: dput() will then get rid of it */
2156 /* __d_drop does write_seqcount_barrier, but they're OK to nest. */
2159 list_del(&dentry
->d_u
.d_child
);
2160 list_del(&target
->d_u
.d_child
);
2162 /* Switch the names.. */
2163 switch_names(dentry
, target
);
2164 swap(dentry
->d_name
.hash
, target
->d_name
.hash
);
2166 /* ... and switch the parents */
2167 if (IS_ROOT(dentry
)) {
2168 dentry
->d_parent
= target
->d_parent
;
2169 target
->d_parent
= target
;
2170 INIT_LIST_HEAD(&target
->d_u
.d_child
);
2172 swap(dentry
->d_parent
, target
->d_parent
);
2174 /* And add them back to the (new) parent lists */
2175 list_add(&target
->d_u
.d_child
, &target
->d_parent
->d_subdirs
);
2178 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
2180 write_seqcount_end(&target
->d_seq
);
2181 write_seqcount_end(&dentry
->d_seq
);
2183 dentry_unlock_parents_for_move(dentry
, target
);
2184 spin_unlock(&target
->d_lock
);
2185 fsnotify_d_move(dentry
);
2186 spin_unlock(&dentry
->d_lock
);
2187 write_sequnlock(&rename_lock
);
2189 EXPORT_SYMBOL(d_move
);
2192 * d_ancestor - search for an ancestor
2193 * @p1: ancestor dentry
2196 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2197 * an ancestor of p2, else NULL.
2199 struct dentry
*d_ancestor(struct dentry
*p1
, struct dentry
*p2
)
2203 for (p
= p2
; !IS_ROOT(p
); p
= p
->d_parent
) {
2204 if (p
->d_parent
== p1
)
2211 * This helper attempts to cope with remotely renamed directories
2213 * It assumes that the caller is already holding
2214 * dentry->d_parent->d_inode->i_mutex and the dcache_inode_lock
2216 * Note: If ever the locking in lock_rename() changes, then please
2217 * remember to update this too...
2219 static struct dentry
*__d_unalias(struct dentry
*dentry
, struct dentry
*alias
)
2220 __releases(dcache_inode_lock
)
2222 struct mutex
*m1
= NULL
, *m2
= NULL
;
2225 /* If alias and dentry share a parent, then no extra locks required */
2226 if (alias
->d_parent
== dentry
->d_parent
)
2229 /* Check for loops */
2230 ret
= ERR_PTR(-ELOOP
);
2231 if (d_ancestor(alias
, dentry
))
2234 /* See lock_rename() */
2235 ret
= ERR_PTR(-EBUSY
);
2236 if (!mutex_trylock(&dentry
->d_sb
->s_vfs_rename_mutex
))
2238 m1
= &dentry
->d_sb
->s_vfs_rename_mutex
;
2239 if (!mutex_trylock(&alias
->d_parent
->d_inode
->i_mutex
))
2241 m2
= &alias
->d_parent
->d_inode
->i_mutex
;
2243 d_move(alias
, dentry
);
2246 spin_unlock(&dcache_inode_lock
);
2255 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2256 * named dentry in place of the dentry to be replaced.
2257 * returns with anon->d_lock held!
2259 static void __d_materialise_dentry(struct dentry
*dentry
, struct dentry
*anon
)
2261 struct dentry
*dparent
, *aparent
;
2263 dentry_lock_for_move(anon
, dentry
);
2265 write_seqcount_begin(&dentry
->d_seq
);
2266 write_seqcount_begin(&anon
->d_seq
);
2268 dparent
= dentry
->d_parent
;
2269 aparent
= anon
->d_parent
;
2271 switch_names(dentry
, anon
);
2272 swap(dentry
->d_name
.hash
, anon
->d_name
.hash
);
2274 dentry
->d_parent
= (aparent
== anon
) ? dentry
: aparent
;
2275 list_del(&dentry
->d_u
.d_child
);
2276 if (!IS_ROOT(dentry
))
2277 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
2279 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
2281 anon
->d_parent
= (dparent
== dentry
) ? anon
: dparent
;
2282 list_del(&anon
->d_u
.d_child
);
2284 list_add(&anon
->d_u
.d_child
, &anon
->d_parent
->d_subdirs
);
2286 INIT_LIST_HEAD(&anon
->d_u
.d_child
);
2288 write_seqcount_end(&dentry
->d_seq
);
2289 write_seqcount_end(&anon
->d_seq
);
2291 dentry_unlock_parents_for_move(anon
, dentry
);
2292 spin_unlock(&dentry
->d_lock
);
2294 /* anon->d_lock still locked, returns locked */
2295 anon
->d_flags
&= ~DCACHE_DISCONNECTED
;
2299 * d_materialise_unique - introduce an inode into the tree
2300 * @dentry: candidate dentry
2301 * @inode: inode to bind to the dentry, to which aliases may be attached
2303 * Introduces an dentry into the tree, substituting an extant disconnected
2304 * root directory alias in its place if there is one
2306 struct dentry
*d_materialise_unique(struct dentry
*dentry
, struct inode
*inode
)
2308 struct dentry
*actual
;
2310 BUG_ON(!d_unhashed(dentry
));
2314 __d_instantiate(dentry
, NULL
);
2319 spin_lock(&dcache_inode_lock
);
2321 if (S_ISDIR(inode
->i_mode
)) {
2322 struct dentry
*alias
;
2324 /* Does an aliased dentry already exist? */
2325 alias
= __d_find_alias(inode
, 0);
2328 /* Is this an anonymous mountpoint that we could splice
2330 if (IS_ROOT(alias
)) {
2331 __d_materialise_dentry(dentry
, alias
);
2335 /* Nope, but we must(!) avoid directory aliasing */
2336 actual
= __d_unalias(dentry
, alias
);
2343 /* Add a unique reference */
2344 actual
= __d_instantiate_unique(dentry
, inode
);
2348 BUG_ON(!d_unhashed(actual
));
2350 spin_lock(&actual
->d_lock
);
2352 spin_lock(&dcache_hash_lock
);
2354 spin_unlock(&dcache_hash_lock
);
2355 spin_unlock(&actual
->d_lock
);
2356 spin_unlock(&dcache_inode_lock
);
2358 if (actual
== dentry
) {
2359 security_d_instantiate(dentry
, inode
);
2366 EXPORT_SYMBOL_GPL(d_materialise_unique
);
2368 static int prepend(char **buffer
, int *buflen
, const char *str
, int namelen
)
2372 return -ENAMETOOLONG
;
2374 memcpy(*buffer
, str
, namelen
);
2378 static int prepend_name(char **buffer
, int *buflen
, struct qstr
*name
)
2380 return prepend(buffer
, buflen
, name
->name
, name
->len
);
2384 * Prepend path string to a buffer
2386 * @path: the dentry/vfsmount to report
2387 * @root: root vfsmnt/dentry (may be modified by this function)
2388 * @buffer: pointer to the end of the buffer
2389 * @buflen: pointer to buffer length
2391 * Caller holds the rename_lock.
2393 * If path is not reachable from the supplied root, then the value of
2394 * root is changed (without modifying refcounts).
2396 static int prepend_path(const struct path
*path
, struct path
*root
,
2397 char **buffer
, int *buflen
)
2399 struct dentry
*dentry
= path
->dentry
;
2400 struct vfsmount
*vfsmnt
= path
->mnt
;
2404 br_read_lock(vfsmount_lock
);
2405 while (dentry
!= root
->dentry
|| vfsmnt
!= root
->mnt
) {
2406 struct dentry
* parent
;
2408 if (dentry
== vfsmnt
->mnt_root
|| IS_ROOT(dentry
)) {
2410 if (vfsmnt
->mnt_parent
== vfsmnt
) {
2413 dentry
= vfsmnt
->mnt_mountpoint
;
2414 vfsmnt
= vfsmnt
->mnt_parent
;
2417 parent
= dentry
->d_parent
;
2419 spin_lock(&dentry
->d_lock
);
2420 error
= prepend_name(buffer
, buflen
, &dentry
->d_name
);
2421 spin_unlock(&dentry
->d_lock
);
2423 error
= prepend(buffer
, buflen
, "/", 1);
2432 if (!error
&& !slash
)
2433 error
= prepend(buffer
, buflen
, "/", 1);
2435 br_read_unlock(vfsmount_lock
);
2440 * Filesystems needing to implement special "root names"
2441 * should do so with ->d_dname()
2443 if (IS_ROOT(dentry
) &&
2444 (dentry
->d_name
.len
!= 1 || dentry
->d_name
.name
[0] != '/')) {
2445 WARN(1, "Root dentry has weird name <%.*s>\n",
2446 (int) dentry
->d_name
.len
, dentry
->d_name
.name
);
2449 root
->dentry
= dentry
;
2454 * __d_path - return the path of a dentry
2455 * @path: the dentry/vfsmount to report
2456 * @root: root vfsmnt/dentry (may be modified by this function)
2457 * @buf: buffer to return value in
2458 * @buflen: buffer length
2460 * Convert a dentry into an ASCII path name.
2462 * Returns a pointer into the buffer or an error code if the
2463 * path was too long.
2465 * "buflen" should be positive.
2467 * If path is not reachable from the supplied root, then the value of
2468 * root is changed (without modifying refcounts).
2470 char *__d_path(const struct path
*path
, struct path
*root
,
2471 char *buf
, int buflen
)
2473 char *res
= buf
+ buflen
;
2476 prepend(&res
, &buflen
, "\0", 1);
2477 write_seqlock(&rename_lock
);
2478 error
= prepend_path(path
, root
, &res
, &buflen
);
2479 write_sequnlock(&rename_lock
);
2482 return ERR_PTR(error
);
2487 * same as __d_path but appends "(deleted)" for unlinked files.
2489 static int path_with_deleted(const struct path
*path
, struct path
*root
,
2490 char **buf
, int *buflen
)
2492 prepend(buf
, buflen
, "\0", 1);
2493 if (d_unlinked(path
->dentry
)) {
2494 int error
= prepend(buf
, buflen
, " (deleted)", 10);
2499 return prepend_path(path
, root
, buf
, buflen
);
2502 static int prepend_unreachable(char **buffer
, int *buflen
)
2504 return prepend(buffer
, buflen
, "(unreachable)", 13);
2508 * d_path - return the path of a dentry
2509 * @path: path to report
2510 * @buf: buffer to return value in
2511 * @buflen: buffer length
2513 * Convert a dentry into an ASCII path name. If the entry has been deleted
2514 * the string " (deleted)" is appended. Note that this is ambiguous.
2516 * Returns a pointer into the buffer or an error code if the path was
2517 * too long. Note: Callers should use the returned pointer, not the passed
2518 * in buffer, to use the name! The implementation often starts at an offset
2519 * into the buffer, and may leave 0 bytes at the start.
2521 * "buflen" should be positive.
2523 char *d_path(const struct path
*path
, char *buf
, int buflen
)
2525 char *res
= buf
+ buflen
;
2531 * We have various synthetic filesystems that never get mounted. On
2532 * these filesystems dentries are never used for lookup purposes, and
2533 * thus don't need to be hashed. They also don't need a name until a
2534 * user wants to identify the object in /proc/pid/fd/. The little hack
2535 * below allows us to generate a name for these objects on demand:
2537 if (path
->dentry
->d_op
&& path
->dentry
->d_op
->d_dname
)
2538 return path
->dentry
->d_op
->d_dname(path
->dentry
, buf
, buflen
);
2540 get_fs_root(current
->fs
, &root
);
2541 write_seqlock(&rename_lock
);
2543 error
= path_with_deleted(path
, &tmp
, &res
, &buflen
);
2545 res
= ERR_PTR(error
);
2546 write_sequnlock(&rename_lock
);
2550 EXPORT_SYMBOL(d_path
);
2553 * d_path_with_unreachable - return the path of a dentry
2554 * @path: path to report
2555 * @buf: buffer to return value in
2556 * @buflen: buffer length
2558 * The difference from d_path() is that this prepends "(unreachable)"
2559 * to paths which are unreachable from the current process' root.
2561 char *d_path_with_unreachable(const struct path
*path
, char *buf
, int buflen
)
2563 char *res
= buf
+ buflen
;
2568 if (path
->dentry
->d_op
&& path
->dentry
->d_op
->d_dname
)
2569 return path
->dentry
->d_op
->d_dname(path
->dentry
, buf
, buflen
);
2571 get_fs_root(current
->fs
, &root
);
2572 write_seqlock(&rename_lock
);
2574 error
= path_with_deleted(path
, &tmp
, &res
, &buflen
);
2575 if (!error
&& !path_equal(&tmp
, &root
))
2576 error
= prepend_unreachable(&res
, &buflen
);
2577 write_sequnlock(&rename_lock
);
2580 res
= ERR_PTR(error
);
2586 * Helper function for dentry_operations.d_dname() members
2588 char *dynamic_dname(struct dentry
*dentry
, char *buffer
, int buflen
,
2589 const char *fmt
, ...)
2595 va_start(args
, fmt
);
2596 sz
= vsnprintf(temp
, sizeof(temp
), fmt
, args
) + 1;
2599 if (sz
> sizeof(temp
) || sz
> buflen
)
2600 return ERR_PTR(-ENAMETOOLONG
);
2602 buffer
+= buflen
- sz
;
2603 return memcpy(buffer
, temp
, sz
);
2607 * Write full pathname from the root of the filesystem into the buffer.
2609 static char *__dentry_path(struct dentry
*dentry
, char *buf
, int buflen
)
2611 char *end
= buf
+ buflen
;
2614 prepend(&end
, &buflen
, "\0", 1);
2621 while (!IS_ROOT(dentry
)) {
2622 struct dentry
*parent
= dentry
->d_parent
;
2626 spin_lock(&dentry
->d_lock
);
2627 error
= prepend_name(&end
, &buflen
, &dentry
->d_name
);
2628 spin_unlock(&dentry
->d_lock
);
2629 if (error
!= 0 || prepend(&end
, &buflen
, "/", 1) != 0)
2637 return ERR_PTR(-ENAMETOOLONG
);
2640 char *dentry_path_raw(struct dentry
*dentry
, char *buf
, int buflen
)
2644 write_seqlock(&rename_lock
);
2645 retval
= __dentry_path(dentry
, buf
, buflen
);
2646 write_sequnlock(&rename_lock
);
2650 EXPORT_SYMBOL(dentry_path_raw
);
2652 char *dentry_path(struct dentry
*dentry
, char *buf
, int buflen
)
2657 write_seqlock(&rename_lock
);
2658 if (d_unlinked(dentry
)) {
2660 if (prepend(&p
, &buflen
, "//deleted", 10) != 0)
2664 retval
= __dentry_path(dentry
, buf
, buflen
);
2665 write_sequnlock(&rename_lock
);
2666 if (!IS_ERR(retval
) && p
)
2667 *p
= '/'; /* restore '/' overriden with '\0' */
2670 return ERR_PTR(-ENAMETOOLONG
);
2674 * NOTE! The user-level library version returns a
2675 * character pointer. The kernel system call just
2676 * returns the length of the buffer filled (which
2677 * includes the ending '\0' character), or a negative
2678 * error value. So libc would do something like
2680 * char *getcwd(char * buf, size_t size)
2684 * retval = sys_getcwd(buf, size);
2691 SYSCALL_DEFINE2(getcwd
, char __user
*, buf
, unsigned long, size
)
2694 struct path pwd
, root
;
2695 char *page
= (char *) __get_free_page(GFP_USER
);
2700 get_fs_root_and_pwd(current
->fs
, &root
, &pwd
);
2703 write_seqlock(&rename_lock
);
2704 if (!d_unlinked(pwd
.dentry
)) {
2706 struct path tmp
= root
;
2707 char *cwd
= page
+ PAGE_SIZE
;
2708 int buflen
= PAGE_SIZE
;
2710 prepend(&cwd
, &buflen
, "\0", 1);
2711 error
= prepend_path(&pwd
, &tmp
, &cwd
, &buflen
);
2712 write_sequnlock(&rename_lock
);
2717 /* Unreachable from current root */
2718 if (!path_equal(&tmp
, &root
)) {
2719 error
= prepend_unreachable(&cwd
, &buflen
);
2725 len
= PAGE_SIZE
+ page
- cwd
;
2728 if (copy_to_user(buf
, cwd
, len
))
2732 write_sequnlock(&rename_lock
);
2738 free_page((unsigned long) page
);
2743 * Test whether new_dentry is a subdirectory of old_dentry.
2745 * Trivially implemented using the dcache structure
2749 * is_subdir - is new dentry a subdirectory of old_dentry
2750 * @new_dentry: new dentry
2751 * @old_dentry: old dentry
2753 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2754 * Returns 0 otherwise.
2755 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2758 int is_subdir(struct dentry
*new_dentry
, struct dentry
*old_dentry
)
2763 if (new_dentry
== old_dentry
)
2767 /* for restarting inner loop in case of seq retry */
2768 seq
= read_seqbegin(&rename_lock
);
2770 * Need rcu_readlock to protect against the d_parent trashing
2774 if (d_ancestor(old_dentry
, new_dentry
))
2779 } while (read_seqretry(&rename_lock
, seq
));
2784 int path_is_under(struct path
*path1
, struct path
*path2
)
2786 struct vfsmount
*mnt
= path1
->mnt
;
2787 struct dentry
*dentry
= path1
->dentry
;
2790 br_read_lock(vfsmount_lock
);
2791 if (mnt
!= path2
->mnt
) {
2793 if (mnt
->mnt_parent
== mnt
) {
2794 br_read_unlock(vfsmount_lock
);
2797 if (mnt
->mnt_parent
== path2
->mnt
)
2799 mnt
= mnt
->mnt_parent
;
2801 dentry
= mnt
->mnt_mountpoint
;
2803 res
= is_subdir(dentry
, path2
->dentry
);
2804 br_read_unlock(vfsmount_lock
);
2807 EXPORT_SYMBOL(path_is_under
);
2809 void d_genocide(struct dentry
*root
)
2811 struct dentry
*this_parent
;
2812 struct list_head
*next
;
2816 seq
= read_seqbegin(&rename_lock
);
2819 spin_lock(&this_parent
->d_lock
);
2821 next
= this_parent
->d_subdirs
.next
;
2823 while (next
!= &this_parent
->d_subdirs
) {
2824 struct list_head
*tmp
= next
;
2825 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
2828 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
2829 if (d_unhashed(dentry
) || !dentry
->d_inode
) {
2830 spin_unlock(&dentry
->d_lock
);
2833 if (!list_empty(&dentry
->d_subdirs
)) {
2834 spin_unlock(&this_parent
->d_lock
);
2835 spin_release(&dentry
->d_lock
.dep_map
, 1, _RET_IP_
);
2836 this_parent
= dentry
;
2837 spin_acquire(&this_parent
->d_lock
.dep_map
, 0, 1, _RET_IP_
);
2840 if (!(dentry
->d_flags
& DCACHE_GENOCIDE
)) {
2841 dentry
->d_flags
|= DCACHE_GENOCIDE
;
2844 spin_unlock(&dentry
->d_lock
);
2846 if (this_parent
!= root
) {
2848 struct dentry
*child
;
2850 tmp
= this_parent
->d_parent
;
2851 if (!(this_parent
->d_flags
& DCACHE_GENOCIDE
)) {
2852 this_parent
->d_flags
|= DCACHE_GENOCIDE
;
2853 this_parent
->d_count
--;
2856 spin_unlock(&this_parent
->d_lock
);
2857 child
= this_parent
;
2859 spin_lock(&this_parent
->d_lock
);
2860 /* might go back up the wrong parent if we have had a rename
2862 if (this_parent
!= child
->d_parent
||
2863 (!locked
&& read_seqretry(&rename_lock
, seq
))) {
2864 spin_unlock(&this_parent
->d_lock
);
2869 next
= child
->d_u
.d_child
.next
;
2872 spin_unlock(&this_parent
->d_lock
);
2873 if (!locked
&& read_seqretry(&rename_lock
, seq
))
2876 write_sequnlock(&rename_lock
);
2881 write_seqlock(&rename_lock
);
2886 * find_inode_number - check for dentry with name
2887 * @dir: directory to check
2888 * @name: Name to find.
2890 * Check whether a dentry already exists for the given name,
2891 * and return the inode number if it has an inode. Otherwise
2894 * This routine is used to post-process directory listings for
2895 * filesystems using synthetic inode numbers, and is necessary
2896 * to keep getcwd() working.
2899 ino_t
find_inode_number(struct dentry
*dir
, struct qstr
*name
)
2901 struct dentry
* dentry
;
2904 dentry
= d_hash_and_lookup(dir
, name
);
2906 if (dentry
->d_inode
)
2907 ino
= dentry
->d_inode
->i_ino
;
2912 EXPORT_SYMBOL(find_inode_number
);
2914 static __initdata
unsigned long dhash_entries
;
2915 static int __init
set_dhash_entries(char *str
)
2919 dhash_entries
= simple_strtoul(str
, &str
, 0);
2922 __setup("dhash_entries=", set_dhash_entries
);
2924 static void __init
dcache_init_early(void)
2928 /* If hashes are distributed across NUMA nodes, defer
2929 * hash allocation until vmalloc space is available.
2935 alloc_large_system_hash("Dentry cache",
2936 sizeof(struct hlist_head
),
2944 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2945 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2948 static void __init
dcache_init(void)
2953 * A constructor could be added for stable state like the lists,
2954 * but it is probably not worth it because of the cache nature
2957 dentry_cache
= KMEM_CACHE(dentry
,
2958 SLAB_RECLAIM_ACCOUNT
|SLAB_PANIC
|SLAB_MEM_SPREAD
);
2960 register_shrinker(&dcache_shrinker
);
2962 /* Hash may have been set up in dcache_init_early */
2967 alloc_large_system_hash("Dentry cache",
2968 sizeof(struct hlist_head
),
2976 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2977 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2980 /* SLAB cache for __getname() consumers */
2981 struct kmem_cache
*names_cachep __read_mostly
;
2982 EXPORT_SYMBOL(names_cachep
);
2984 EXPORT_SYMBOL(d_genocide
);
2986 void __init
vfs_caches_init_early(void)
2988 dcache_init_early();
2992 void __init
vfs_caches_init(unsigned long mempages
)
2994 unsigned long reserve
;
2996 /* Base hash sizes on available memory, with a reserve equal to
2997 150% of current kernel size */
2999 reserve
= min((mempages
- nr_free_pages()) * 3/2, mempages
- 1);
3000 mempages
-= reserve
;
3002 names_cachep
= kmem_cache_create("names_cache", PATH_MAX
, 0,
3003 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
3007 files_init(mempages
);