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
63 * If there is an ancestor relationship:
64 * dentry->d_parent->...->d_parent->d_lock
66 * dentry->d_parent->d_lock
69 * If no ancestor relationship:
70 * if (dentry1 < dentry2)
74 int sysctl_vfs_cache_pressure __read_mostly
= 100;
75 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure
);
77 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dcache_inode_lock
);
78 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dcache_hash_lock
);
79 static __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dcache_lru_lock
);
80 __cacheline_aligned_in_smp
DEFINE_SPINLOCK(dcache_lock
);
81 __cacheline_aligned_in_smp
DEFINE_SEQLOCK(rename_lock
);
83 EXPORT_SYMBOL(rename_lock
);
84 EXPORT_SYMBOL(dcache_inode_lock
);
85 EXPORT_SYMBOL(dcache_lock
);
87 static struct kmem_cache
*dentry_cache __read_mostly
;
89 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
92 * This is the single most critical data structure when it comes
93 * to the dcache: the hashtable for lookups. Somebody should try
94 * to make this good - I've just made it work.
96 * This hash-function tries to avoid losing too many bits of hash
97 * information, yet avoid using a prime hash-size or similar.
99 #define D_HASHBITS d_hash_shift
100 #define D_HASHMASK d_hash_mask
102 static unsigned int d_hash_mask __read_mostly
;
103 static unsigned int d_hash_shift __read_mostly
;
104 static struct hlist_head
*dentry_hashtable __read_mostly
;
106 /* Statistics gathering. */
107 struct dentry_stat_t dentry_stat
= {
111 static DEFINE_PER_CPU(unsigned int, nr_dentry
);
113 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
114 static int get_nr_dentry(void)
118 for_each_possible_cpu(i
)
119 sum
+= per_cpu(nr_dentry
, i
);
120 return sum
< 0 ? 0 : sum
;
123 int proc_nr_dentry(ctl_table
*table
, int write
, void __user
*buffer
,
124 size_t *lenp
, loff_t
*ppos
)
126 dentry_stat
.nr_dentry
= get_nr_dentry();
127 return proc_dointvec(table
, write
, buffer
, lenp
, ppos
);
131 static void __d_free(struct rcu_head
*head
)
133 struct dentry
*dentry
= container_of(head
, struct dentry
, d_u
.d_rcu
);
135 WARN_ON(!list_empty(&dentry
->d_alias
));
136 if (dname_external(dentry
))
137 kfree(dentry
->d_name
.name
);
138 kmem_cache_free(dentry_cache
, dentry
);
142 * no dcache_lock, please.
144 static void d_free(struct dentry
*dentry
)
146 BUG_ON(dentry
->d_count
);
147 this_cpu_dec(nr_dentry
);
148 if (dentry
->d_op
&& dentry
->d_op
->d_release
)
149 dentry
->d_op
->d_release(dentry
);
151 /* if dentry was never inserted into hash, immediate free is OK */
152 if (hlist_unhashed(&dentry
->d_hash
))
153 __d_free(&dentry
->d_u
.d_rcu
);
155 call_rcu(&dentry
->d_u
.d_rcu
, __d_free
);
159 * Release the dentry's inode, using the filesystem
160 * d_iput() operation if defined.
162 static void dentry_iput(struct dentry
* dentry
)
163 __releases(dentry
->d_lock
)
164 __releases(dcache_inode_lock
)
165 __releases(dcache_lock
)
167 struct inode
*inode
= dentry
->d_inode
;
169 dentry
->d_inode
= NULL
;
170 list_del_init(&dentry
->d_alias
);
171 spin_unlock(&dentry
->d_lock
);
172 spin_unlock(&dcache_inode_lock
);
173 spin_unlock(&dcache_lock
);
175 fsnotify_inoderemove(inode
);
176 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
177 dentry
->d_op
->d_iput(dentry
, inode
);
181 spin_unlock(&dentry
->d_lock
);
182 spin_unlock(&dcache_inode_lock
);
183 spin_unlock(&dcache_lock
);
188 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
190 static void dentry_lru_add(struct dentry
*dentry
)
192 if (list_empty(&dentry
->d_lru
)) {
193 spin_lock(&dcache_lru_lock
);
194 list_add(&dentry
->d_lru
, &dentry
->d_sb
->s_dentry_lru
);
195 dentry
->d_sb
->s_nr_dentry_unused
++;
196 dentry_stat
.nr_unused
++;
197 spin_unlock(&dcache_lru_lock
);
201 static void __dentry_lru_del(struct dentry
*dentry
)
203 list_del_init(&dentry
->d_lru
);
204 dentry
->d_sb
->s_nr_dentry_unused
--;
205 dentry_stat
.nr_unused
--;
208 static void dentry_lru_del(struct dentry
*dentry
)
210 if (!list_empty(&dentry
->d_lru
)) {
211 spin_lock(&dcache_lru_lock
);
212 __dentry_lru_del(dentry
);
213 spin_unlock(&dcache_lru_lock
);
217 static void dentry_lru_move_tail(struct dentry
*dentry
)
219 spin_lock(&dcache_lru_lock
);
220 if (list_empty(&dentry
->d_lru
)) {
221 list_add_tail(&dentry
->d_lru
, &dentry
->d_sb
->s_dentry_lru
);
222 dentry
->d_sb
->s_nr_dentry_unused
++;
223 dentry_stat
.nr_unused
++;
225 list_move_tail(&dentry
->d_lru
, &dentry
->d_sb
->s_dentry_lru
);
227 spin_unlock(&dcache_lru_lock
);
231 * d_kill - kill dentry and return parent
232 * @dentry: dentry to kill
234 * The dentry must already be unhashed and removed from the LRU.
236 * If this is the root of the dentry tree, return NULL.
238 * dcache_lock and d_lock and d_parent->d_lock must be held by caller, and
239 * are dropped by d_kill.
241 static struct dentry
*d_kill(struct dentry
*dentry
, struct dentry
*parent
)
242 __releases(dentry
->d_lock
)
243 __releases(parent
->d_lock
)
244 __releases(dcache_inode_lock
)
245 __releases(dcache_lock
)
247 dentry
->d_parent
= NULL
;
248 list_del(&dentry
->d_u
.d_child
);
250 spin_unlock(&parent
->d_lock
);
253 * dentry_iput drops the locks, at which point nobody (except
254 * transient RCU lookups) can reach this dentry.
261 * d_drop - drop a dentry
262 * @dentry: dentry to drop
264 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
265 * be found through a VFS lookup any more. Note that this is different from
266 * deleting the dentry - d_delete will try to mark the dentry negative if
267 * possible, giving a successful _negative_ lookup, while d_drop will
268 * just make the cache lookup fail.
270 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
271 * reason (NFS timeouts or autofs deletes).
273 * __d_drop requires dentry->d_lock.
275 void __d_drop(struct dentry
*dentry
)
277 if (!(dentry
->d_flags
& DCACHE_UNHASHED
)) {
278 dentry
->d_flags
|= DCACHE_UNHASHED
;
279 spin_lock(&dcache_hash_lock
);
280 hlist_del_rcu(&dentry
->d_hash
);
281 spin_unlock(&dcache_hash_lock
);
284 EXPORT_SYMBOL(__d_drop
);
286 void d_drop(struct dentry
*dentry
)
288 spin_lock(&dcache_lock
);
289 spin_lock(&dentry
->d_lock
);
291 spin_unlock(&dentry
->d_lock
);
292 spin_unlock(&dcache_lock
);
294 EXPORT_SYMBOL(d_drop
);
299 * This is complicated by the fact that we do not want to put
300 * dentries that are no longer on any hash chain on the unused
301 * list: we'd much rather just get rid of them immediately.
303 * However, that implies that we have to traverse the dentry
304 * tree upwards to the parents which might _also_ now be
305 * scheduled for deletion (it may have been only waiting for
306 * its last child to go away).
308 * This tail recursion is done by hand as we don't want to depend
309 * on the compiler to always get this right (gcc generally doesn't).
310 * Real recursion would eat up our stack space.
314 * dput - release a dentry
315 * @dentry: dentry to release
317 * Release a dentry. This will drop the usage count and if appropriate
318 * call the dentry unlink method as well as removing it from the queues and
319 * releasing its resources. If the parent dentries were scheduled for release
320 * they too may now get deleted.
322 * no dcache lock, please.
325 void dput(struct dentry
*dentry
)
327 struct dentry
*parent
;
332 if (dentry
->d_count
== 1)
334 spin_lock(&dentry
->d_lock
);
338 parent
= dentry
->d_parent
;
339 if (dentry
->d_count
== 1) {
340 if (!spin_trylock(&dcache_lock
)) {
342 * Something of a livelock possibility we could avoid
343 * by taking dcache_lock and trying again, but we
344 * want to reduce dcache_lock anyway so this will
348 spin_unlock(&dentry
->d_lock
);
351 if (!spin_trylock(&dcache_inode_lock
)) {
353 spin_unlock(&dcache_lock
);
356 if (parent
&& !spin_trylock(&parent
->d_lock
)) {
357 spin_unlock(&dcache_inode_lock
);
362 if (dentry
->d_count
) {
363 spin_unlock(&dentry
->d_lock
);
365 spin_unlock(&parent
->d_lock
);
366 spin_unlock(&dcache_lock
);
371 * AV: ->d_delete() is _NOT_ allowed to block now.
373 if (dentry
->d_op
&& dentry
->d_op
->d_delete
) {
374 if (dentry
->d_op
->d_delete(dentry
))
378 /* Unreachable? Get rid of it */
379 if (d_unhashed(dentry
))
382 /* Otherwise leave it cached and ensure it's on the LRU */
383 dentry
->d_flags
|= DCACHE_REFERENCED
;
384 dentry_lru_add(dentry
);
386 spin_unlock(&dentry
->d_lock
);
388 spin_unlock(&parent
->d_lock
);
389 spin_unlock(&dcache_inode_lock
);
390 spin_unlock(&dcache_lock
);
396 /* if dentry was on the d_lru list delete it from there */
397 dentry_lru_del(dentry
);
398 dentry
= d_kill(dentry
, parent
);
405 * d_invalidate - invalidate a dentry
406 * @dentry: dentry to invalidate
408 * Try to invalidate the dentry if it turns out to be
409 * possible. If there are other dentries that can be
410 * reached through this one we can't delete it and we
411 * return -EBUSY. On success we return 0.
416 int d_invalidate(struct dentry
* dentry
)
419 * If it's already been dropped, return OK.
421 spin_lock(&dcache_lock
);
422 spin_lock(&dentry
->d_lock
);
423 if (d_unhashed(dentry
)) {
424 spin_unlock(&dentry
->d_lock
);
425 spin_unlock(&dcache_lock
);
429 * Check whether to do a partial shrink_dcache
430 * to get rid of unused child entries.
432 if (!list_empty(&dentry
->d_subdirs
)) {
433 spin_unlock(&dentry
->d_lock
);
434 spin_unlock(&dcache_lock
);
435 shrink_dcache_parent(dentry
);
436 spin_lock(&dcache_lock
);
437 spin_lock(&dentry
->d_lock
);
441 * Somebody else still using it?
443 * If it's a directory, we can't drop it
444 * for fear of somebody re-populating it
445 * with children (even though dropping it
446 * would make it unreachable from the root,
447 * we might still populate it if it was a
448 * working directory or similar).
450 if (dentry
->d_count
> 1) {
451 if (dentry
->d_inode
&& S_ISDIR(dentry
->d_inode
->i_mode
)) {
452 spin_unlock(&dentry
->d_lock
);
453 spin_unlock(&dcache_lock
);
459 spin_unlock(&dentry
->d_lock
);
460 spin_unlock(&dcache_lock
);
463 EXPORT_SYMBOL(d_invalidate
);
465 /* This must be called with dcache_lock and d_lock held */
466 static inline struct dentry
* __dget_locked_dlock(struct dentry
*dentry
)
469 dentry_lru_del(dentry
);
473 /* This should be called _only_ with dcache_lock held */
474 static inline struct dentry
* __dget_locked(struct dentry
*dentry
)
476 spin_lock(&dentry
->d_lock
);
477 __dget_locked_dlock(dentry
);
478 spin_unlock(&dentry
->d_lock
);
482 struct dentry
* dget_locked_dlock(struct dentry
*dentry
)
484 return __dget_locked_dlock(dentry
);
487 struct dentry
* dget_locked(struct dentry
*dentry
)
489 return __dget_locked(dentry
);
491 EXPORT_SYMBOL(dget_locked
);
493 struct dentry
*dget_parent(struct dentry
*dentry
)
498 spin_lock(&dentry
->d_lock
);
499 ret
= dentry
->d_parent
;
506 if (!spin_trylock(&ret
->d_lock
)) {
507 spin_unlock(&dentry
->d_lock
);
511 BUG_ON(!ret
->d_count
);
513 spin_unlock(&ret
->d_lock
);
515 spin_unlock(&dentry
->d_lock
);
518 EXPORT_SYMBOL(dget_parent
);
521 * d_find_alias - grab a hashed alias of inode
522 * @inode: inode in question
523 * @want_discon: flag, used by d_splice_alias, to request
524 * that only a DISCONNECTED alias be returned.
526 * If inode has a hashed alias, or is a directory and has any alias,
527 * acquire the reference to alias and return it. Otherwise return NULL.
528 * Notice that if inode is a directory there can be only one alias and
529 * it can be unhashed only if it has no children, or if it is the root
532 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
533 * any other hashed alias over that one unless @want_discon is set,
534 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
536 static struct dentry
*__d_find_alias(struct inode
*inode
, int want_discon
)
538 struct dentry
*alias
, *discon_alias
;
542 list_for_each_entry(alias
, &inode
->i_dentry
, d_alias
) {
543 spin_lock(&alias
->d_lock
);
544 if (S_ISDIR(inode
->i_mode
) || !d_unhashed(alias
)) {
545 if (IS_ROOT(alias
) &&
546 (alias
->d_flags
& DCACHE_DISCONNECTED
)) {
547 discon_alias
= alias
;
548 } else if (!want_discon
) {
549 __dget_locked_dlock(alias
);
550 spin_unlock(&alias
->d_lock
);
554 spin_unlock(&alias
->d_lock
);
557 alias
= discon_alias
;
558 spin_lock(&alias
->d_lock
);
559 if (S_ISDIR(inode
->i_mode
) || !d_unhashed(alias
)) {
560 if (IS_ROOT(alias
) &&
561 (alias
->d_flags
& DCACHE_DISCONNECTED
)) {
562 __dget_locked_dlock(alias
);
563 spin_unlock(&alias
->d_lock
);
567 spin_unlock(&alias
->d_lock
);
573 struct dentry
*d_find_alias(struct inode
*inode
)
575 struct dentry
*de
= NULL
;
577 if (!list_empty(&inode
->i_dentry
)) {
578 spin_lock(&dcache_lock
);
579 spin_lock(&dcache_inode_lock
);
580 de
= __d_find_alias(inode
, 0);
581 spin_unlock(&dcache_inode_lock
);
582 spin_unlock(&dcache_lock
);
586 EXPORT_SYMBOL(d_find_alias
);
589 * Try to kill dentries associated with this inode.
590 * WARNING: you must own a reference to inode.
592 void d_prune_aliases(struct inode
*inode
)
594 struct dentry
*dentry
;
596 spin_lock(&dcache_lock
);
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_locked_dlock(dentry
);
603 spin_unlock(&dentry
->d_lock
);
604 spin_unlock(&dcache_inode_lock
);
605 spin_unlock(&dcache_lock
);
609 spin_unlock(&dentry
->d_lock
);
611 spin_unlock(&dcache_inode_lock
);
612 spin_unlock(&dcache_lock
);
614 EXPORT_SYMBOL(d_prune_aliases
);
617 * Throw away a dentry - free the inode, dput the parent. This requires that
618 * the LRU list has already been removed.
620 * Try to prune ancestors as well. This is necessary to prevent
621 * quadratic behavior of shrink_dcache_parent(), but is also expected
622 * to be beneficial in reducing dentry cache fragmentation.
624 static void prune_one_dentry(struct dentry
*dentry
, struct dentry
*parent
)
625 __releases(dentry
->d_lock
)
626 __releases(parent
->d_lock
)
627 __releases(dcache_inode_lock
)
628 __releases(dcache_lock
)
631 dentry
= d_kill(dentry
, parent
);
634 * Prune ancestors. Locking is simpler than in dput(),
635 * because dcache_lock needs to be taken anyway.
638 spin_lock(&dcache_lock
);
639 spin_lock(&dcache_inode_lock
);
641 spin_lock(&dentry
->d_lock
);
645 parent
= dentry
->d_parent
;
646 if (parent
&& !spin_trylock(&parent
->d_lock
)) {
647 spin_unlock(&dentry
->d_lock
);
651 if (dentry
->d_count
) {
653 spin_unlock(&parent
->d_lock
);
654 spin_unlock(&dentry
->d_lock
);
655 spin_unlock(&dcache_inode_lock
);
656 spin_unlock(&dcache_lock
);
660 dentry_lru_del(dentry
);
662 dentry
= d_kill(dentry
, parent
);
666 static void shrink_dentry_list(struct list_head
*list
)
668 struct dentry
*dentry
;
670 while (!list_empty(list
)) {
671 struct dentry
*parent
;
673 dentry
= list_entry(list
->prev
, struct dentry
, d_lru
);
675 if (!spin_trylock(&dentry
->d_lock
)) {
677 spin_unlock(&dcache_lru_lock
);
679 spin_lock(&dcache_lru_lock
);
684 * We found an inuse dentry which was not removed from
685 * the LRU because of laziness during lookup. Do not free
686 * it - just keep it off the LRU list.
688 if (dentry
->d_count
) {
689 __dentry_lru_del(dentry
);
690 spin_unlock(&dentry
->d_lock
);
696 parent
= dentry
->d_parent
;
697 if (parent
&& !spin_trylock(&parent
->d_lock
)) {
698 spin_unlock(&dentry
->d_lock
);
701 __dentry_lru_del(dentry
);
702 spin_unlock(&dcache_lru_lock
);
704 prune_one_dentry(dentry
, parent
);
705 /* dcache_lock, dcache_inode_lock and dentry->d_lock dropped */
706 spin_lock(&dcache_lock
);
707 spin_lock(&dcache_inode_lock
);
708 spin_lock(&dcache_lru_lock
);
713 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
714 * @sb: superblock to shrink dentry LRU.
715 * @count: number of entries to prune
716 * @flags: flags to control the dentry processing
718 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
720 static void __shrink_dcache_sb(struct super_block
*sb
, int *count
, int flags
)
722 /* called from prune_dcache() and shrink_dcache_parent() */
723 struct dentry
*dentry
;
724 LIST_HEAD(referenced
);
728 spin_lock(&dcache_lock
);
729 spin_lock(&dcache_inode_lock
);
731 spin_lock(&dcache_lru_lock
);
732 while (!list_empty(&sb
->s_dentry_lru
)) {
733 dentry
= list_entry(sb
->s_dentry_lru
.prev
,
734 struct dentry
, d_lru
);
735 BUG_ON(dentry
->d_sb
!= sb
);
737 if (!spin_trylock(&dentry
->d_lock
)) {
738 spin_unlock(&dcache_lru_lock
);
744 * If we are honouring the DCACHE_REFERENCED flag and the
745 * dentry has this flag set, don't free it. Clear the flag
746 * and put it back on the LRU.
748 if (flags
& DCACHE_REFERENCED
&&
749 dentry
->d_flags
& DCACHE_REFERENCED
) {
750 dentry
->d_flags
&= ~DCACHE_REFERENCED
;
751 list_move(&dentry
->d_lru
, &referenced
);
752 spin_unlock(&dentry
->d_lock
);
754 list_move_tail(&dentry
->d_lru
, &tmp
);
755 spin_unlock(&dentry
->d_lock
);
759 /* XXX: re-add cond_resched_lock when dcache_lock goes away */
763 shrink_dentry_list(&tmp
);
765 if (!list_empty(&referenced
))
766 list_splice(&referenced
, &sb
->s_dentry_lru
);
767 spin_unlock(&dcache_lru_lock
);
768 spin_unlock(&dcache_inode_lock
);
769 spin_unlock(&dcache_lock
);
773 * prune_dcache - shrink the dcache
774 * @count: number of entries to try to free
776 * Shrink the dcache. This is done when we need more memory, or simply when we
777 * need to unmount something (at which point we need to unuse all dentries).
779 * This function may fail to free any resources if all the dentries are in use.
781 static void prune_dcache(int count
)
783 struct super_block
*sb
, *p
= NULL
;
785 int unused
= dentry_stat
.nr_unused
;
789 if (unused
== 0 || count
== 0)
791 spin_lock(&dcache_lock
);
795 prune_ratio
= unused
/ count
;
797 list_for_each_entry(sb
, &super_blocks
, s_list
) {
798 if (list_empty(&sb
->s_instances
))
800 if (sb
->s_nr_dentry_unused
== 0)
803 /* Now, we reclaim unused dentrins with fairness.
804 * We reclaim them same percentage from each superblock.
805 * We calculate number of dentries to scan on this sb
806 * as follows, but the implementation is arranged to avoid
808 * number of dentries to scan on this sb =
809 * count * (number of dentries on this sb /
810 * number of dentries in the machine)
812 spin_unlock(&sb_lock
);
813 if (prune_ratio
!= 1)
814 w_count
= (sb
->s_nr_dentry_unused
/ prune_ratio
) + 1;
816 w_count
= sb
->s_nr_dentry_unused
;
819 * We need to be sure this filesystem isn't being unmounted,
820 * otherwise we could race with generic_shutdown_super(), and
821 * end up holding a reference to an inode while the filesystem
822 * is unmounted. So we try to get s_umount, and make sure
825 if (down_read_trylock(&sb
->s_umount
)) {
826 if ((sb
->s_root
!= NULL
) &&
827 (!list_empty(&sb
->s_dentry_lru
))) {
828 spin_unlock(&dcache_lock
);
829 __shrink_dcache_sb(sb
, &w_count
,
832 spin_lock(&dcache_lock
);
834 up_read(&sb
->s_umount
);
841 /* more work left to do? */
847 spin_unlock(&sb_lock
);
848 spin_unlock(&dcache_lock
);
852 * shrink_dcache_sb - shrink dcache for a superblock
855 * Shrink the dcache for the specified super block. This is used to free
856 * the dcache before unmounting a file system.
858 void shrink_dcache_sb(struct super_block
*sb
)
862 spin_lock(&dcache_lock
);
863 spin_lock(&dcache_inode_lock
);
864 spin_lock(&dcache_lru_lock
);
865 while (!list_empty(&sb
->s_dentry_lru
)) {
866 list_splice_init(&sb
->s_dentry_lru
, &tmp
);
867 shrink_dentry_list(&tmp
);
869 spin_unlock(&dcache_lru_lock
);
870 spin_unlock(&dcache_inode_lock
);
871 spin_unlock(&dcache_lock
);
873 EXPORT_SYMBOL(shrink_dcache_sb
);
876 * destroy a single subtree of dentries for unmount
877 * - see the comments on shrink_dcache_for_umount() for a description of the
880 static void shrink_dcache_for_umount_subtree(struct dentry
*dentry
)
882 struct dentry
*parent
;
883 unsigned detached
= 0;
885 BUG_ON(!IS_ROOT(dentry
));
887 /* detach this root from the system */
888 spin_lock(&dcache_lock
);
889 spin_lock(&dentry
->d_lock
);
890 dentry_lru_del(dentry
);
892 spin_unlock(&dentry
->d_lock
);
893 spin_unlock(&dcache_lock
);
896 /* descend to the first leaf in the current subtree */
897 while (!list_empty(&dentry
->d_subdirs
)) {
900 /* this is a branch with children - detach all of them
901 * from the system in one go */
902 spin_lock(&dcache_lock
);
903 spin_lock(&dentry
->d_lock
);
904 list_for_each_entry(loop
, &dentry
->d_subdirs
,
906 spin_lock_nested(&loop
->d_lock
,
907 DENTRY_D_LOCK_NESTED
);
908 dentry_lru_del(loop
);
910 spin_unlock(&loop
->d_lock
);
912 spin_unlock(&dentry
->d_lock
);
913 spin_unlock(&dcache_lock
);
915 /* move to the first child */
916 dentry
= list_entry(dentry
->d_subdirs
.next
,
917 struct dentry
, d_u
.d_child
);
920 /* consume the dentries from this leaf up through its parents
921 * until we find one with children or run out altogether */
925 if (dentry
->d_count
!= 0) {
927 "BUG: Dentry %p{i=%lx,n=%s}"
929 " [unmount of %s %s]\n",
932 dentry
->d_inode
->i_ino
: 0UL,
935 dentry
->d_sb
->s_type
->name
,
940 if (IS_ROOT(dentry
)) {
942 list_del(&dentry
->d_u
.d_child
);
944 parent
= dentry
->d_parent
;
945 spin_lock(&parent
->d_lock
);
947 list_del(&dentry
->d_u
.d_child
);
948 spin_unlock(&parent
->d_lock
);
953 inode
= dentry
->d_inode
;
955 dentry
->d_inode
= NULL
;
956 list_del_init(&dentry
->d_alias
);
957 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
958 dentry
->d_op
->d_iput(dentry
, inode
);
965 /* finished when we fall off the top of the tree,
966 * otherwise we ascend to the parent and move to the
967 * next sibling if there is one */
971 } while (list_empty(&dentry
->d_subdirs
));
973 dentry
= list_entry(dentry
->d_subdirs
.next
,
974 struct dentry
, d_u
.d_child
);
979 * destroy the dentries attached to a superblock on unmounting
980 * - we don't need to use dentry->d_lock, and only need dcache_lock when
981 * removing the dentry from the system lists and hashes because:
982 * - the superblock is detached from all mountings and open files, so the
983 * dentry trees will not be rearranged by the VFS
984 * - s_umount is write-locked, so the memory pressure shrinker will ignore
985 * any dentries belonging to this superblock that it comes across
986 * - the filesystem itself is no longer permitted to rearrange the dentries
989 void shrink_dcache_for_umount(struct super_block
*sb
)
991 struct dentry
*dentry
;
993 if (down_read_trylock(&sb
->s_umount
))
998 spin_lock(&dentry
->d_lock
);
1000 spin_unlock(&dentry
->d_lock
);
1001 shrink_dcache_for_umount_subtree(dentry
);
1003 while (!hlist_empty(&sb
->s_anon
)) {
1004 dentry
= hlist_entry(sb
->s_anon
.first
, struct dentry
, d_hash
);
1005 shrink_dcache_for_umount_subtree(dentry
);
1010 * Search for at least 1 mount point in the dentry's subdirs.
1011 * We descend to the next level whenever the d_subdirs
1012 * list is non-empty and continue searching.
1016 * have_submounts - check for mounts over a dentry
1017 * @parent: dentry to check.
1019 * Return true if the parent or its subdirectories contain
1022 int have_submounts(struct dentry
*parent
)
1024 struct dentry
*this_parent
;
1025 struct list_head
*next
;
1029 this_parent
= parent
;
1030 seq
= read_seqbegin(&rename_lock
);
1032 spin_lock(&dcache_lock
);
1033 if (d_mountpoint(parent
))
1035 spin_lock(&this_parent
->d_lock
);
1037 next
= this_parent
->d_subdirs
.next
;
1039 while (next
!= &this_parent
->d_subdirs
) {
1040 struct list_head
*tmp
= next
;
1041 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
1044 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1045 /* Have we found a mount point ? */
1046 if (d_mountpoint(dentry
)) {
1047 spin_unlock(&dentry
->d_lock
);
1048 spin_unlock(&this_parent
->d_lock
);
1051 if (!list_empty(&dentry
->d_subdirs
)) {
1052 spin_unlock(&this_parent
->d_lock
);
1053 spin_release(&dentry
->d_lock
.dep_map
, 1, _RET_IP_
);
1054 this_parent
= dentry
;
1055 spin_acquire(&this_parent
->d_lock
.dep_map
, 0, 1, _RET_IP_
);
1058 spin_unlock(&dentry
->d_lock
);
1061 * All done at this level ... ascend and resume the search.
1063 if (this_parent
!= parent
) {
1065 struct dentry
*child
;
1067 tmp
= this_parent
->d_parent
;
1069 spin_unlock(&this_parent
->d_lock
);
1070 child
= this_parent
;
1072 spin_lock(&this_parent
->d_lock
);
1073 /* might go back up the wrong parent if we have had a rename
1075 if (this_parent
!= child
->d_parent
||
1076 read_seqretry(&rename_lock
, seq
)) {
1077 spin_unlock(&this_parent
->d_lock
);
1078 spin_unlock(&dcache_lock
);
1083 next
= child
->d_u
.d_child
.next
;
1086 spin_unlock(&this_parent
->d_lock
);
1087 spin_unlock(&dcache_lock
);
1088 if (read_seqretry(&rename_lock
, seq
))
1090 return 0; /* No mount points found in tree */
1092 spin_unlock(&dcache_lock
);
1093 if (read_seqretry(&rename_lock
, seq
))
1097 EXPORT_SYMBOL(have_submounts
);
1100 * Search the dentry child list for the specified parent,
1101 * and move any unused dentries to the end of the unused
1102 * list for prune_dcache(). We descend to the next level
1103 * whenever the d_subdirs list is non-empty and continue
1106 * It returns zero iff there are no unused children,
1107 * otherwise it returns the number of children moved to
1108 * the end of the unused list. This may not be the total
1109 * number of unused children, because select_parent can
1110 * drop the lock and return early due to latency
1113 static int select_parent(struct dentry
* parent
)
1115 struct dentry
*this_parent
;
1116 struct list_head
*next
;
1121 this_parent
= parent
;
1122 seq
= read_seqbegin(&rename_lock
);
1124 spin_lock(&dcache_lock
);
1125 spin_lock(&this_parent
->d_lock
);
1127 next
= this_parent
->d_subdirs
.next
;
1129 while (next
!= &this_parent
->d_subdirs
) {
1130 struct list_head
*tmp
= next
;
1131 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
1134 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1137 * move only zero ref count dentries to the end
1138 * of the unused list for prune_dcache
1140 if (!dentry
->d_count
) {
1141 dentry_lru_move_tail(dentry
);
1144 dentry_lru_del(dentry
);
1148 * We can return to the caller if we have found some (this
1149 * ensures forward progress). We'll be coming back to find
1152 if (found
&& need_resched()) {
1153 spin_unlock(&dentry
->d_lock
);
1158 * Descend a level if the d_subdirs list is non-empty.
1160 if (!list_empty(&dentry
->d_subdirs
)) {
1161 spin_unlock(&this_parent
->d_lock
);
1162 spin_release(&dentry
->d_lock
.dep_map
, 1, _RET_IP_
);
1163 this_parent
= dentry
;
1164 spin_acquire(&this_parent
->d_lock
.dep_map
, 0, 1, _RET_IP_
);
1168 spin_unlock(&dentry
->d_lock
);
1171 * All done at this level ... ascend and resume the search.
1173 if (this_parent
!= parent
) {
1175 struct dentry
*child
;
1177 tmp
= this_parent
->d_parent
;
1179 spin_unlock(&this_parent
->d_lock
);
1180 child
= this_parent
;
1182 spin_lock(&this_parent
->d_lock
);
1183 /* might go back up the wrong parent if we have had a rename
1185 if (this_parent
!= child
->d_parent
||
1186 read_seqretry(&rename_lock
, seq
)) {
1187 spin_unlock(&this_parent
->d_lock
);
1188 spin_unlock(&dcache_lock
);
1193 next
= child
->d_u
.d_child
.next
;
1197 spin_unlock(&this_parent
->d_lock
);
1198 spin_unlock(&dcache_lock
);
1199 if (read_seqretry(&rename_lock
, seq
))
1205 * shrink_dcache_parent - prune dcache
1206 * @parent: parent of entries to prune
1208 * Prune the dcache to remove unused children of the parent dentry.
1211 void shrink_dcache_parent(struct dentry
* parent
)
1213 struct super_block
*sb
= parent
->d_sb
;
1216 while ((found
= select_parent(parent
)) != 0)
1217 __shrink_dcache_sb(sb
, &found
, 0);
1219 EXPORT_SYMBOL(shrink_dcache_parent
);
1222 * Scan `nr' dentries and return the number which remain.
1224 * We need to avoid reentering the filesystem if the caller is performing a
1225 * GFP_NOFS allocation attempt. One example deadlock is:
1227 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1228 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1229 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1231 * In this case we return -1 to tell the caller that we baled.
1233 static int shrink_dcache_memory(struct shrinker
*shrink
, int nr
, gfp_t gfp_mask
)
1236 if (!(gfp_mask
& __GFP_FS
))
1241 return (dentry_stat
.nr_unused
/ 100) * sysctl_vfs_cache_pressure
;
1244 static struct shrinker dcache_shrinker
= {
1245 .shrink
= shrink_dcache_memory
,
1246 .seeks
= DEFAULT_SEEKS
,
1250 * d_alloc - allocate a dcache entry
1251 * @parent: parent of entry to allocate
1252 * @name: qstr of the name
1254 * Allocates a dentry. It returns %NULL if there is insufficient memory
1255 * available. On a success the dentry is returned. The name passed in is
1256 * copied and the copy passed in may be reused after this call.
1259 struct dentry
*d_alloc(struct dentry
* parent
, const struct qstr
*name
)
1261 struct dentry
*dentry
;
1264 dentry
= kmem_cache_alloc(dentry_cache
, GFP_KERNEL
);
1268 if (name
->len
> DNAME_INLINE_LEN
-1) {
1269 dname
= kmalloc(name
->len
+ 1, GFP_KERNEL
);
1271 kmem_cache_free(dentry_cache
, dentry
);
1275 dname
= dentry
->d_iname
;
1277 dentry
->d_name
.name
= dname
;
1279 dentry
->d_name
.len
= name
->len
;
1280 dentry
->d_name
.hash
= name
->hash
;
1281 memcpy(dname
, name
->name
, name
->len
);
1282 dname
[name
->len
] = 0;
1284 dentry
->d_count
= 1;
1285 dentry
->d_flags
= DCACHE_UNHASHED
;
1286 spin_lock_init(&dentry
->d_lock
);
1287 dentry
->d_inode
= NULL
;
1288 dentry
->d_parent
= NULL
;
1289 dentry
->d_sb
= NULL
;
1290 dentry
->d_op
= NULL
;
1291 dentry
->d_fsdata
= NULL
;
1292 dentry
->d_mounted
= 0;
1293 INIT_HLIST_NODE(&dentry
->d_hash
);
1294 INIT_LIST_HEAD(&dentry
->d_lru
);
1295 INIT_LIST_HEAD(&dentry
->d_subdirs
);
1296 INIT_LIST_HEAD(&dentry
->d_alias
);
1297 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
1300 spin_lock(&dcache_lock
);
1301 spin_lock(&parent
->d_lock
);
1302 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1303 dentry
->d_parent
= dget_dlock(parent
);
1304 dentry
->d_sb
= parent
->d_sb
;
1305 list_add(&dentry
->d_u
.d_child
, &parent
->d_subdirs
);
1306 spin_unlock(&dentry
->d_lock
);
1307 spin_unlock(&parent
->d_lock
);
1308 spin_unlock(&dcache_lock
);
1311 this_cpu_inc(nr_dentry
);
1315 EXPORT_SYMBOL(d_alloc
);
1317 struct dentry
*d_alloc_name(struct dentry
*parent
, const char *name
)
1322 q
.len
= strlen(name
);
1323 q
.hash
= full_name_hash(q
.name
, q
.len
);
1324 return d_alloc(parent
, &q
);
1326 EXPORT_SYMBOL(d_alloc_name
);
1328 /* the caller must hold dcache_lock */
1329 static void __d_instantiate(struct dentry
*dentry
, struct inode
*inode
)
1331 spin_lock(&dentry
->d_lock
);
1333 list_add(&dentry
->d_alias
, &inode
->i_dentry
);
1334 dentry
->d_inode
= inode
;
1335 spin_unlock(&dentry
->d_lock
);
1336 fsnotify_d_instantiate(dentry
, inode
);
1340 * d_instantiate - fill in inode information for a dentry
1341 * @entry: dentry to complete
1342 * @inode: inode to attach to this dentry
1344 * Fill in inode information in the entry.
1346 * This turns negative dentries into productive full members
1349 * NOTE! This assumes that the inode count has been incremented
1350 * (or otherwise set) by the caller to indicate that it is now
1351 * in use by the dcache.
1354 void d_instantiate(struct dentry
*entry
, struct inode
* inode
)
1356 BUG_ON(!list_empty(&entry
->d_alias
));
1357 spin_lock(&dcache_lock
);
1358 spin_lock(&dcache_inode_lock
);
1359 __d_instantiate(entry
, inode
);
1360 spin_unlock(&dcache_inode_lock
);
1361 spin_unlock(&dcache_lock
);
1362 security_d_instantiate(entry
, inode
);
1364 EXPORT_SYMBOL(d_instantiate
);
1367 * d_instantiate_unique - instantiate a non-aliased dentry
1368 * @entry: dentry to instantiate
1369 * @inode: inode to attach to this dentry
1371 * Fill in inode information in the entry. On success, it returns NULL.
1372 * If an unhashed alias of "entry" already exists, then we return the
1373 * aliased dentry instead and drop one reference to inode.
1375 * Note that in order to avoid conflicts with rename() etc, the caller
1376 * had better be holding the parent directory semaphore.
1378 * This also assumes that the inode count has been incremented
1379 * (or otherwise set) by the caller to indicate that it is now
1380 * in use by the dcache.
1382 static struct dentry
*__d_instantiate_unique(struct dentry
*entry
,
1383 struct inode
*inode
)
1385 struct dentry
*alias
;
1386 int len
= entry
->d_name
.len
;
1387 const char *name
= entry
->d_name
.name
;
1388 unsigned int hash
= entry
->d_name
.hash
;
1391 __d_instantiate(entry
, NULL
);
1395 list_for_each_entry(alias
, &inode
->i_dentry
, d_alias
) {
1396 struct qstr
*qstr
= &alias
->d_name
;
1399 * Don't need alias->d_lock here, because aliases with
1400 * d_parent == entry->d_parent are not subject to name or
1401 * parent changes, because the parent inode i_mutex is held.
1403 if (qstr
->hash
!= hash
)
1405 if (alias
->d_parent
!= entry
->d_parent
)
1407 if (qstr
->len
!= len
)
1409 if (memcmp(qstr
->name
, name
, len
))
1415 __d_instantiate(entry
, inode
);
1419 struct dentry
*d_instantiate_unique(struct dentry
*entry
, struct inode
*inode
)
1421 struct dentry
*result
;
1423 BUG_ON(!list_empty(&entry
->d_alias
));
1425 spin_lock(&dcache_lock
);
1426 spin_lock(&dcache_inode_lock
);
1427 result
= __d_instantiate_unique(entry
, inode
);
1428 spin_unlock(&dcache_inode_lock
);
1429 spin_unlock(&dcache_lock
);
1432 security_d_instantiate(entry
, inode
);
1436 BUG_ON(!d_unhashed(result
));
1441 EXPORT_SYMBOL(d_instantiate_unique
);
1444 * d_alloc_root - allocate root dentry
1445 * @root_inode: inode to allocate the root for
1447 * Allocate a root ("/") dentry for the inode given. The inode is
1448 * instantiated and returned. %NULL is returned if there is insufficient
1449 * memory or the inode passed is %NULL.
1452 struct dentry
* d_alloc_root(struct inode
* root_inode
)
1454 struct dentry
*res
= NULL
;
1457 static const struct qstr name
= { .name
= "/", .len
= 1 };
1459 res
= d_alloc(NULL
, &name
);
1461 res
->d_sb
= root_inode
->i_sb
;
1462 res
->d_parent
= res
;
1463 d_instantiate(res
, root_inode
);
1468 EXPORT_SYMBOL(d_alloc_root
);
1470 static inline struct hlist_head
*d_hash(struct dentry
*parent
,
1473 hash
+= ((unsigned long) parent
^ GOLDEN_RATIO_PRIME
) / L1_CACHE_BYTES
;
1474 hash
= hash
^ ((hash
^ GOLDEN_RATIO_PRIME
) >> D_HASHBITS
);
1475 return dentry_hashtable
+ (hash
& D_HASHMASK
);
1479 * d_obtain_alias - find or allocate a dentry for a given inode
1480 * @inode: inode to allocate the dentry for
1482 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1483 * similar open by handle operations. The returned dentry may be anonymous,
1484 * or may have a full name (if the inode was already in the cache).
1486 * When called on a directory inode, we must ensure that the inode only ever
1487 * has one dentry. If a dentry is found, that is returned instead of
1488 * allocating a new one.
1490 * On successful return, the reference to the inode has been transferred
1491 * to the dentry. In case of an error the reference on the inode is released.
1492 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1493 * be passed in and will be the error will be propagate to the return value,
1494 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1496 struct dentry
*d_obtain_alias(struct inode
*inode
)
1498 static const struct qstr anonstring
= { .name
= "" };
1503 return ERR_PTR(-ESTALE
);
1505 return ERR_CAST(inode
);
1507 res
= d_find_alias(inode
);
1511 tmp
= d_alloc(NULL
, &anonstring
);
1513 res
= ERR_PTR(-ENOMEM
);
1516 tmp
->d_parent
= tmp
; /* make sure dput doesn't croak */
1518 spin_lock(&dcache_lock
);
1519 spin_lock(&dcache_inode_lock
);
1520 res
= __d_find_alias(inode
, 0);
1522 spin_unlock(&dcache_inode_lock
);
1523 spin_unlock(&dcache_lock
);
1528 /* attach a disconnected dentry */
1529 spin_lock(&tmp
->d_lock
);
1530 tmp
->d_sb
= inode
->i_sb
;
1531 tmp
->d_inode
= inode
;
1532 tmp
->d_flags
|= DCACHE_DISCONNECTED
;
1533 tmp
->d_flags
&= ~DCACHE_UNHASHED
;
1534 list_add(&tmp
->d_alias
, &inode
->i_dentry
);
1535 spin_lock(&dcache_hash_lock
);
1536 hlist_add_head(&tmp
->d_hash
, &inode
->i_sb
->s_anon
);
1537 spin_unlock(&dcache_hash_lock
);
1538 spin_unlock(&tmp
->d_lock
);
1539 spin_unlock(&dcache_inode_lock
);
1541 spin_unlock(&dcache_lock
);
1548 EXPORT_SYMBOL(d_obtain_alias
);
1551 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1552 * @inode: the inode which may have a disconnected dentry
1553 * @dentry: a negative dentry which we want to point to the inode.
1555 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1556 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1557 * and return it, else simply d_add the inode to the dentry and return NULL.
1559 * This is needed in the lookup routine of any filesystem that is exportable
1560 * (via knfsd) so that we can build dcache paths to directories effectively.
1562 * If a dentry was found and moved, then it is returned. Otherwise NULL
1563 * is returned. This matches the expected return value of ->lookup.
1566 struct dentry
*d_splice_alias(struct inode
*inode
, struct dentry
*dentry
)
1568 struct dentry
*new = NULL
;
1570 if (inode
&& S_ISDIR(inode
->i_mode
)) {
1571 spin_lock(&dcache_lock
);
1572 spin_lock(&dcache_inode_lock
);
1573 new = __d_find_alias(inode
, 1);
1575 BUG_ON(!(new->d_flags
& DCACHE_DISCONNECTED
));
1576 spin_unlock(&dcache_inode_lock
);
1577 spin_unlock(&dcache_lock
);
1578 security_d_instantiate(new, inode
);
1579 d_move(new, dentry
);
1582 /* already taking dcache_lock, so d_add() by hand */
1583 __d_instantiate(dentry
, inode
);
1584 spin_unlock(&dcache_inode_lock
);
1585 spin_unlock(&dcache_lock
);
1586 security_d_instantiate(dentry
, inode
);
1590 d_add(dentry
, inode
);
1593 EXPORT_SYMBOL(d_splice_alias
);
1596 * d_add_ci - lookup or allocate new dentry with case-exact name
1597 * @inode: the inode case-insensitive lookup has found
1598 * @dentry: the negative dentry that was passed to the parent's lookup func
1599 * @name: the case-exact name to be associated with the returned dentry
1601 * This is to avoid filling the dcache with case-insensitive names to the
1602 * same inode, only the actual correct case is stored in the dcache for
1603 * case-insensitive filesystems.
1605 * For a case-insensitive lookup match and if the the case-exact dentry
1606 * already exists in in the dcache, use it and return it.
1608 * If no entry exists with the exact case name, allocate new dentry with
1609 * the exact case, and return the spliced entry.
1611 struct dentry
*d_add_ci(struct dentry
*dentry
, struct inode
*inode
,
1615 struct dentry
*found
;
1619 * First check if a dentry matching the name already exists,
1620 * if not go ahead and create it now.
1622 found
= d_hash_and_lookup(dentry
->d_parent
, name
);
1624 new = d_alloc(dentry
->d_parent
, name
);
1630 found
= d_splice_alias(inode
, new);
1639 * If a matching dentry exists, and it's not negative use it.
1641 * Decrement the reference count to balance the iget() done
1644 if (found
->d_inode
) {
1645 if (unlikely(found
->d_inode
!= inode
)) {
1646 /* This can't happen because bad inodes are unhashed. */
1647 BUG_ON(!is_bad_inode(inode
));
1648 BUG_ON(!is_bad_inode(found
->d_inode
));
1655 * Negative dentry: instantiate it unless the inode is a directory and
1656 * already has a dentry.
1658 spin_lock(&dcache_lock
);
1659 spin_lock(&dcache_inode_lock
);
1660 if (!S_ISDIR(inode
->i_mode
) || list_empty(&inode
->i_dentry
)) {
1661 __d_instantiate(found
, inode
);
1662 spin_unlock(&dcache_inode_lock
);
1663 spin_unlock(&dcache_lock
);
1664 security_d_instantiate(found
, inode
);
1669 * In case a directory already has a (disconnected) entry grab a
1670 * reference to it, move it in place and use it.
1672 new = list_entry(inode
->i_dentry
.next
, struct dentry
, d_alias
);
1674 spin_unlock(&dcache_inode_lock
);
1675 spin_unlock(&dcache_lock
);
1676 security_d_instantiate(found
, inode
);
1684 return ERR_PTR(error
);
1686 EXPORT_SYMBOL(d_add_ci
);
1689 * d_lookup - search for a dentry
1690 * @parent: parent dentry
1691 * @name: qstr of name we wish to find
1692 * Returns: dentry, or NULL
1694 * d_lookup searches the children of the parent dentry for the name in
1695 * question. If the dentry is found its reference count is incremented and the
1696 * dentry is returned. The caller must use dput to free the entry when it has
1697 * finished using it. %NULL is returned if the dentry does not exist.
1699 struct dentry
* d_lookup(struct dentry
* parent
, struct qstr
* name
)
1701 struct dentry
* dentry
= NULL
;
1705 seq
= read_seqbegin(&rename_lock
);
1706 dentry
= __d_lookup(parent
, name
);
1709 } while (read_seqretry(&rename_lock
, seq
));
1712 EXPORT_SYMBOL(d_lookup
);
1715 * __d_lookup - search for a dentry (racy)
1716 * @parent: parent dentry
1717 * @name: qstr of name we wish to find
1718 * Returns: dentry, or NULL
1720 * __d_lookup is like d_lookup, however it may (rarely) return a
1721 * false-negative result due to unrelated rename activity.
1723 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1724 * however it must be used carefully, eg. with a following d_lookup in
1725 * the case of failure.
1727 * __d_lookup callers must be commented.
1729 struct dentry
* __d_lookup(struct dentry
* parent
, struct qstr
* name
)
1731 unsigned int len
= name
->len
;
1732 unsigned int hash
= name
->hash
;
1733 const unsigned char *str
= name
->name
;
1734 struct hlist_head
*head
= d_hash(parent
,hash
);
1735 struct dentry
*found
= NULL
;
1736 struct hlist_node
*node
;
1737 struct dentry
*dentry
;
1740 * The hash list is protected using RCU.
1742 * Take d_lock when comparing a candidate dentry, to avoid races
1745 * It is possible that concurrent renames can mess up our list
1746 * walk here and result in missing our dentry, resulting in the
1747 * false-negative result. d_lookup() protects against concurrent
1748 * renames using rename_lock seqlock.
1750 * See Documentation/vfs/dcache-locking.txt for more details.
1754 hlist_for_each_entry_rcu(dentry
, node
, head
, d_hash
) {
1757 if (dentry
->d_name
.hash
!= hash
)
1759 if (dentry
->d_parent
!= parent
)
1762 spin_lock(&dentry
->d_lock
);
1765 * Recheck the dentry after taking the lock - d_move may have
1766 * changed things. Don't bother checking the hash because
1767 * we're about to compare the whole name anyway.
1769 if (dentry
->d_parent
!= parent
)
1772 /* non-existing due to RCU? */
1773 if (d_unhashed(dentry
))
1777 * It is safe to compare names since d_move() cannot
1778 * change the qstr (protected by d_lock).
1780 qstr
= &dentry
->d_name
;
1781 if (parent
->d_op
&& parent
->d_op
->d_compare
) {
1782 if (parent
->d_op
->d_compare(parent
, parent
->d_inode
,
1783 dentry
, dentry
->d_inode
,
1784 qstr
->len
, qstr
->name
, name
))
1787 if (qstr
->len
!= len
)
1789 if (memcmp(qstr
->name
, str
, len
))
1795 spin_unlock(&dentry
->d_lock
);
1798 spin_unlock(&dentry
->d_lock
);
1806 * d_hash_and_lookup - hash the qstr then search for a dentry
1807 * @dir: Directory to search in
1808 * @name: qstr of name we wish to find
1810 * On hash failure or on lookup failure NULL is returned.
1812 struct dentry
*d_hash_and_lookup(struct dentry
*dir
, struct qstr
*name
)
1814 struct dentry
*dentry
= NULL
;
1817 * Check for a fs-specific hash function. Note that we must
1818 * calculate the standard hash first, as the d_op->d_hash()
1819 * routine may choose to leave the hash value unchanged.
1821 name
->hash
= full_name_hash(name
->name
, name
->len
);
1822 if (dir
->d_op
&& dir
->d_op
->d_hash
) {
1823 if (dir
->d_op
->d_hash(dir
, dir
->d_inode
, name
) < 0)
1826 dentry
= d_lookup(dir
, name
);
1832 * d_validate - verify dentry provided from insecure source (deprecated)
1833 * @dentry: The dentry alleged to be valid child of @dparent
1834 * @dparent: The parent dentry (known to be valid)
1836 * An insecure source has sent us a dentry, here we verify it and dget() it.
1837 * This is used by ncpfs in its readdir implementation.
1838 * Zero is returned in the dentry is invalid.
1840 * This function is slow for big directories, and deprecated, do not use it.
1842 int d_validate(struct dentry
*dentry
, struct dentry
*dparent
)
1844 struct dentry
*child
;
1846 spin_lock(&dcache_lock
);
1847 spin_lock(&dparent
->d_lock
);
1848 list_for_each_entry(child
, &dparent
->d_subdirs
, d_u
.d_child
) {
1849 if (dentry
== child
) {
1850 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
1851 __dget_locked_dlock(dentry
);
1852 spin_unlock(&dentry
->d_lock
);
1853 spin_unlock(&dparent
->d_lock
);
1854 spin_unlock(&dcache_lock
);
1858 spin_unlock(&dparent
->d_lock
);
1859 spin_unlock(&dcache_lock
);
1863 EXPORT_SYMBOL(d_validate
);
1866 * When a file is deleted, we have two options:
1867 * - turn this dentry into a negative dentry
1868 * - unhash this dentry and free it.
1870 * Usually, we want to just turn this into
1871 * a negative dentry, but if anybody else is
1872 * currently using the dentry or the inode
1873 * we can't do that and we fall back on removing
1874 * it from the hash queues and waiting for
1875 * it to be deleted later when it has no users
1879 * d_delete - delete a dentry
1880 * @dentry: The dentry to delete
1882 * Turn the dentry into a negative dentry if possible, otherwise
1883 * remove it from the hash queues so it can be deleted later
1886 void d_delete(struct dentry
* dentry
)
1890 * Are we the only user?
1892 spin_lock(&dcache_lock
);
1893 spin_lock(&dcache_inode_lock
);
1894 spin_lock(&dentry
->d_lock
);
1895 isdir
= S_ISDIR(dentry
->d_inode
->i_mode
);
1896 if (dentry
->d_count
== 1) {
1897 dentry
->d_flags
&= ~DCACHE_CANT_MOUNT
;
1898 dentry_iput(dentry
);
1899 fsnotify_nameremove(dentry
, isdir
);
1903 if (!d_unhashed(dentry
))
1906 spin_unlock(&dentry
->d_lock
);
1907 spin_unlock(&dcache_inode_lock
);
1908 spin_unlock(&dcache_lock
);
1910 fsnotify_nameremove(dentry
, isdir
);
1912 EXPORT_SYMBOL(d_delete
);
1914 static void __d_rehash(struct dentry
* entry
, struct hlist_head
*list
)
1917 entry
->d_flags
&= ~DCACHE_UNHASHED
;
1918 hlist_add_head_rcu(&entry
->d_hash
, list
);
1921 static void _d_rehash(struct dentry
* entry
)
1923 __d_rehash(entry
, d_hash(entry
->d_parent
, entry
->d_name
.hash
));
1927 * d_rehash - add an entry back to the hash
1928 * @entry: dentry to add to the hash
1930 * Adds a dentry to the hash according to its name.
1933 void d_rehash(struct dentry
* entry
)
1935 spin_lock(&dcache_lock
);
1936 spin_lock(&entry
->d_lock
);
1937 spin_lock(&dcache_hash_lock
);
1939 spin_unlock(&dcache_hash_lock
);
1940 spin_unlock(&entry
->d_lock
);
1941 spin_unlock(&dcache_lock
);
1943 EXPORT_SYMBOL(d_rehash
);
1946 * dentry_update_name_case - update case insensitive dentry with a new name
1947 * @dentry: dentry to be updated
1950 * Update a case insensitive dentry with new case of name.
1952 * dentry must have been returned by d_lookup with name @name. Old and new
1953 * name lengths must match (ie. no d_compare which allows mismatched name
1956 * Parent inode i_mutex must be held over d_lookup and into this call (to
1957 * keep renames and concurrent inserts, and readdir(2) away).
1959 void dentry_update_name_case(struct dentry
*dentry
, struct qstr
*name
)
1961 BUG_ON(!mutex_is_locked(&dentry
->d_inode
->i_mutex
));
1962 BUG_ON(dentry
->d_name
.len
!= name
->len
); /* d_lookup gives this */
1964 spin_lock(&dcache_lock
);
1965 spin_lock(&dentry
->d_lock
);
1966 memcpy((unsigned char *)dentry
->d_name
.name
, name
->name
, name
->len
);
1967 spin_unlock(&dentry
->d_lock
);
1968 spin_unlock(&dcache_lock
);
1970 EXPORT_SYMBOL(dentry_update_name_case
);
1972 static void switch_names(struct dentry
*dentry
, struct dentry
*target
)
1974 if (dname_external(target
)) {
1975 if (dname_external(dentry
)) {
1977 * Both external: swap the pointers
1979 swap(target
->d_name
.name
, dentry
->d_name
.name
);
1982 * dentry:internal, target:external. Steal target's
1983 * storage and make target internal.
1985 memcpy(target
->d_iname
, dentry
->d_name
.name
,
1986 dentry
->d_name
.len
+ 1);
1987 dentry
->d_name
.name
= target
->d_name
.name
;
1988 target
->d_name
.name
= target
->d_iname
;
1991 if (dname_external(dentry
)) {
1993 * dentry:external, target:internal. Give dentry's
1994 * storage to target and make dentry internal
1996 memcpy(dentry
->d_iname
, target
->d_name
.name
,
1997 target
->d_name
.len
+ 1);
1998 target
->d_name
.name
= dentry
->d_name
.name
;
1999 dentry
->d_name
.name
= dentry
->d_iname
;
2002 * Both are internal. Just copy target to dentry
2004 memcpy(dentry
->d_iname
, target
->d_name
.name
,
2005 target
->d_name
.len
+ 1);
2006 dentry
->d_name
.len
= target
->d_name
.len
;
2010 swap(dentry
->d_name
.len
, target
->d_name
.len
);
2013 static void dentry_lock_for_move(struct dentry
*dentry
, struct dentry
*target
)
2016 * XXXX: do we really need to take target->d_lock?
2018 if (IS_ROOT(dentry
) || dentry
->d_parent
== target
->d_parent
)
2019 spin_lock(&target
->d_parent
->d_lock
);
2021 if (d_ancestor(dentry
->d_parent
, target
->d_parent
)) {
2022 spin_lock(&dentry
->d_parent
->d_lock
);
2023 spin_lock_nested(&target
->d_parent
->d_lock
,
2024 DENTRY_D_LOCK_NESTED
);
2026 spin_lock(&target
->d_parent
->d_lock
);
2027 spin_lock_nested(&dentry
->d_parent
->d_lock
,
2028 DENTRY_D_LOCK_NESTED
);
2031 if (target
< dentry
) {
2032 spin_lock_nested(&target
->d_lock
, 2);
2033 spin_lock_nested(&dentry
->d_lock
, 3);
2035 spin_lock_nested(&dentry
->d_lock
, 2);
2036 spin_lock_nested(&target
->d_lock
, 3);
2040 static void dentry_unlock_parents_for_move(struct dentry
*dentry
,
2041 struct dentry
*target
)
2043 if (target
->d_parent
!= dentry
->d_parent
)
2044 spin_unlock(&dentry
->d_parent
->d_lock
);
2045 if (target
->d_parent
!= target
)
2046 spin_unlock(&target
->d_parent
->d_lock
);
2050 * When switching names, the actual string doesn't strictly have to
2051 * be preserved in the target - because we're dropping the target
2052 * anyway. As such, we can just do a simple memcpy() to copy over
2053 * the new name before we switch.
2055 * Note that we have to be a lot more careful about getting the hash
2056 * switched - we have to switch the hash value properly even if it
2057 * then no longer matches the actual (corrupted) string of the target.
2058 * The hash value has to match the hash queue that the dentry is on..
2061 * d_move_locked - move a dentry
2062 * @dentry: entry to move
2063 * @target: new dentry
2065 * Update the dcache to reflect the move of a file name. Negative
2066 * dcache entries should not be moved in this way.
2068 static void d_move_locked(struct dentry
* dentry
, struct dentry
* target
)
2070 if (!dentry
->d_inode
)
2071 printk(KERN_WARNING
"VFS: moving negative dcache entry\n");
2073 BUG_ON(d_ancestor(dentry
, target
));
2074 BUG_ON(d_ancestor(target
, dentry
));
2076 write_seqlock(&rename_lock
);
2078 dentry_lock_for_move(dentry
, target
);
2080 /* Move the dentry to the target hash queue, if on different bucket */
2081 spin_lock(&dcache_hash_lock
);
2082 if (!d_unhashed(dentry
))
2083 hlist_del_rcu(&dentry
->d_hash
);
2084 __d_rehash(dentry
, d_hash(target
->d_parent
, target
->d_name
.hash
));
2085 spin_unlock(&dcache_hash_lock
);
2087 /* Unhash the target: dput() will then get rid of it */
2090 list_del(&dentry
->d_u
.d_child
);
2091 list_del(&target
->d_u
.d_child
);
2093 /* Switch the names.. */
2094 switch_names(dentry
, target
);
2095 swap(dentry
->d_name
.hash
, target
->d_name
.hash
);
2097 /* ... and switch the parents */
2098 if (IS_ROOT(dentry
)) {
2099 dentry
->d_parent
= target
->d_parent
;
2100 target
->d_parent
= target
;
2101 INIT_LIST_HEAD(&target
->d_u
.d_child
);
2103 swap(dentry
->d_parent
, target
->d_parent
);
2105 /* And add them back to the (new) parent lists */
2106 list_add(&target
->d_u
.d_child
, &target
->d_parent
->d_subdirs
);
2109 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
2111 dentry_unlock_parents_for_move(dentry
, target
);
2112 spin_unlock(&target
->d_lock
);
2113 fsnotify_d_move(dentry
);
2114 spin_unlock(&dentry
->d_lock
);
2115 write_sequnlock(&rename_lock
);
2119 * d_move - move a dentry
2120 * @dentry: entry to move
2121 * @target: new dentry
2123 * Update the dcache to reflect the move of a file name. Negative
2124 * dcache entries should not be moved in this way.
2127 void d_move(struct dentry
* dentry
, struct dentry
* target
)
2129 spin_lock(&dcache_lock
);
2130 d_move_locked(dentry
, target
);
2131 spin_unlock(&dcache_lock
);
2133 EXPORT_SYMBOL(d_move
);
2136 * d_ancestor - search for an ancestor
2137 * @p1: ancestor dentry
2140 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2141 * an ancestor of p2, else NULL.
2143 struct dentry
*d_ancestor(struct dentry
*p1
, struct dentry
*p2
)
2147 for (p
= p2
; !IS_ROOT(p
); p
= p
->d_parent
) {
2148 if (p
->d_parent
== p1
)
2155 * This helper attempts to cope with remotely renamed directories
2157 * It assumes that the caller is already holding
2158 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
2160 * Note: If ever the locking in lock_rename() changes, then please
2161 * remember to update this too...
2163 static struct dentry
*__d_unalias(struct dentry
*dentry
, struct dentry
*alias
)
2164 __releases(dcache_lock
)
2165 __releases(dcache_inode_lock
)
2167 struct mutex
*m1
= NULL
, *m2
= NULL
;
2170 /* If alias and dentry share a parent, then no extra locks required */
2171 if (alias
->d_parent
== dentry
->d_parent
)
2174 /* Check for loops */
2175 ret
= ERR_PTR(-ELOOP
);
2176 if (d_ancestor(alias
, dentry
))
2179 /* See lock_rename() */
2180 ret
= ERR_PTR(-EBUSY
);
2181 if (!mutex_trylock(&dentry
->d_sb
->s_vfs_rename_mutex
))
2183 m1
= &dentry
->d_sb
->s_vfs_rename_mutex
;
2184 if (!mutex_trylock(&alias
->d_parent
->d_inode
->i_mutex
))
2186 m2
= &alias
->d_parent
->d_inode
->i_mutex
;
2188 d_move_locked(alias
, dentry
);
2191 spin_unlock(&dcache_inode_lock
);
2192 spin_unlock(&dcache_lock
);
2201 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2202 * named dentry in place of the dentry to be replaced.
2203 * returns with anon->d_lock held!
2205 static void __d_materialise_dentry(struct dentry
*dentry
, struct dentry
*anon
)
2207 struct dentry
*dparent
, *aparent
;
2209 dentry_lock_for_move(anon
, dentry
);
2211 dparent
= dentry
->d_parent
;
2212 aparent
= anon
->d_parent
;
2214 switch_names(dentry
, anon
);
2215 swap(dentry
->d_name
.hash
, anon
->d_name
.hash
);
2217 dentry
->d_parent
= (aparent
== anon
) ? dentry
: aparent
;
2218 list_del(&dentry
->d_u
.d_child
);
2219 if (!IS_ROOT(dentry
))
2220 list_add(&dentry
->d_u
.d_child
, &dentry
->d_parent
->d_subdirs
);
2222 INIT_LIST_HEAD(&dentry
->d_u
.d_child
);
2224 anon
->d_parent
= (dparent
== dentry
) ? anon
: dparent
;
2225 list_del(&anon
->d_u
.d_child
);
2227 list_add(&anon
->d_u
.d_child
, &anon
->d_parent
->d_subdirs
);
2229 INIT_LIST_HEAD(&anon
->d_u
.d_child
);
2231 dentry_unlock_parents_for_move(anon
, dentry
);
2232 spin_unlock(&dentry
->d_lock
);
2234 /* anon->d_lock still locked, returns locked */
2235 anon
->d_flags
&= ~DCACHE_DISCONNECTED
;
2239 * d_materialise_unique - introduce an inode into the tree
2240 * @dentry: candidate dentry
2241 * @inode: inode to bind to the dentry, to which aliases may be attached
2243 * Introduces an dentry into the tree, substituting an extant disconnected
2244 * root directory alias in its place if there is one
2246 struct dentry
*d_materialise_unique(struct dentry
*dentry
, struct inode
*inode
)
2248 struct dentry
*actual
;
2250 BUG_ON(!d_unhashed(dentry
));
2252 spin_lock(&dcache_lock
);
2253 spin_lock(&dcache_inode_lock
);
2257 __d_instantiate(dentry
, NULL
);
2261 if (S_ISDIR(inode
->i_mode
)) {
2262 struct dentry
*alias
;
2264 /* Does an aliased dentry already exist? */
2265 alias
= __d_find_alias(inode
, 0);
2268 /* Is this an anonymous mountpoint that we could splice
2270 if (IS_ROOT(alias
)) {
2271 __d_materialise_dentry(dentry
, alias
);
2275 /* Nope, but we must(!) avoid directory aliasing */
2276 actual
= __d_unalias(dentry
, alias
);
2283 /* Add a unique reference */
2284 actual
= __d_instantiate_unique(dentry
, inode
);
2287 else if (unlikely(!d_unhashed(actual
)))
2288 goto shouldnt_be_hashed
;
2291 spin_lock(&actual
->d_lock
);
2293 spin_lock(&dcache_hash_lock
);
2295 spin_unlock(&dcache_hash_lock
);
2296 spin_unlock(&actual
->d_lock
);
2297 spin_unlock(&dcache_inode_lock
);
2298 spin_unlock(&dcache_lock
);
2300 if (actual
== dentry
) {
2301 security_d_instantiate(dentry
, inode
);
2309 spin_unlock(&dcache_inode_lock
);
2310 spin_unlock(&dcache_lock
);
2313 EXPORT_SYMBOL_GPL(d_materialise_unique
);
2315 static int prepend(char **buffer
, int *buflen
, const char *str
, int namelen
)
2319 return -ENAMETOOLONG
;
2321 memcpy(*buffer
, str
, namelen
);
2325 static int prepend_name(char **buffer
, int *buflen
, struct qstr
*name
)
2327 return prepend(buffer
, buflen
, name
->name
, name
->len
);
2331 * Prepend path string to a buffer
2333 * @path: the dentry/vfsmount to report
2334 * @root: root vfsmnt/dentry (may be modified by this function)
2335 * @buffer: pointer to the end of the buffer
2336 * @buflen: pointer to buffer length
2338 * Caller holds the rename_lock.
2340 * If path is not reachable from the supplied root, then the value of
2341 * root is changed (without modifying refcounts).
2343 static int prepend_path(const struct path
*path
, struct path
*root
,
2344 char **buffer
, int *buflen
)
2346 struct dentry
*dentry
= path
->dentry
;
2347 struct vfsmount
*vfsmnt
= path
->mnt
;
2351 br_read_lock(vfsmount_lock
);
2352 while (dentry
!= root
->dentry
|| vfsmnt
!= root
->mnt
) {
2353 struct dentry
* parent
;
2355 if (dentry
== vfsmnt
->mnt_root
|| IS_ROOT(dentry
)) {
2357 if (vfsmnt
->mnt_parent
== vfsmnt
) {
2360 dentry
= vfsmnt
->mnt_mountpoint
;
2361 vfsmnt
= vfsmnt
->mnt_parent
;
2364 parent
= dentry
->d_parent
;
2366 spin_lock(&dentry
->d_lock
);
2367 error
= prepend_name(buffer
, buflen
, &dentry
->d_name
);
2368 spin_unlock(&dentry
->d_lock
);
2370 error
= prepend(buffer
, buflen
, "/", 1);
2379 if (!error
&& !slash
)
2380 error
= prepend(buffer
, buflen
, "/", 1);
2382 br_read_unlock(vfsmount_lock
);
2387 * Filesystems needing to implement special "root names"
2388 * should do so with ->d_dname()
2390 if (IS_ROOT(dentry
) &&
2391 (dentry
->d_name
.len
!= 1 || dentry
->d_name
.name
[0] != '/')) {
2392 WARN(1, "Root dentry has weird name <%.*s>\n",
2393 (int) dentry
->d_name
.len
, dentry
->d_name
.name
);
2396 root
->dentry
= dentry
;
2401 * __d_path - return the path of a dentry
2402 * @path: the dentry/vfsmount to report
2403 * @root: root vfsmnt/dentry (may be modified by this function)
2404 * @buf: buffer to return value in
2405 * @buflen: buffer length
2407 * Convert a dentry into an ASCII path name.
2409 * Returns a pointer into the buffer or an error code if the
2410 * path was too long.
2412 * "buflen" should be positive.
2414 * If path is not reachable from the supplied root, then the value of
2415 * root is changed (without modifying refcounts).
2417 char *__d_path(const struct path
*path
, struct path
*root
,
2418 char *buf
, int buflen
)
2420 char *res
= buf
+ buflen
;
2423 prepend(&res
, &buflen
, "\0", 1);
2424 spin_lock(&dcache_lock
);
2425 write_seqlock(&rename_lock
);
2426 error
= prepend_path(path
, root
, &res
, &buflen
);
2427 write_sequnlock(&rename_lock
);
2428 spin_unlock(&dcache_lock
);
2431 return ERR_PTR(error
);
2436 * same as __d_path but appends "(deleted)" for unlinked files.
2438 static int path_with_deleted(const struct path
*path
, struct path
*root
,
2439 char **buf
, int *buflen
)
2441 prepend(buf
, buflen
, "\0", 1);
2442 if (d_unlinked(path
->dentry
)) {
2443 int error
= prepend(buf
, buflen
, " (deleted)", 10);
2448 return prepend_path(path
, root
, buf
, buflen
);
2451 static int prepend_unreachable(char **buffer
, int *buflen
)
2453 return prepend(buffer
, buflen
, "(unreachable)", 13);
2457 * d_path - return the path of a dentry
2458 * @path: path to report
2459 * @buf: buffer to return value in
2460 * @buflen: buffer length
2462 * Convert a dentry into an ASCII path name. If the entry has been deleted
2463 * the string " (deleted)" is appended. Note that this is ambiguous.
2465 * Returns a pointer into the buffer or an error code if the path was
2466 * too long. Note: Callers should use the returned pointer, not the passed
2467 * in buffer, to use the name! The implementation often starts at an offset
2468 * into the buffer, and may leave 0 bytes at the start.
2470 * "buflen" should be positive.
2472 char *d_path(const struct path
*path
, char *buf
, int buflen
)
2474 char *res
= buf
+ buflen
;
2480 * We have various synthetic filesystems that never get mounted. On
2481 * these filesystems dentries are never used for lookup purposes, and
2482 * thus don't need to be hashed. They also don't need a name until a
2483 * user wants to identify the object in /proc/pid/fd/. The little hack
2484 * below allows us to generate a name for these objects on demand:
2486 if (path
->dentry
->d_op
&& path
->dentry
->d_op
->d_dname
)
2487 return path
->dentry
->d_op
->d_dname(path
->dentry
, buf
, buflen
);
2489 get_fs_root(current
->fs
, &root
);
2490 spin_lock(&dcache_lock
);
2491 write_seqlock(&rename_lock
);
2493 error
= path_with_deleted(path
, &tmp
, &res
, &buflen
);
2495 res
= ERR_PTR(error
);
2496 write_sequnlock(&rename_lock
);
2497 spin_unlock(&dcache_lock
);
2501 EXPORT_SYMBOL(d_path
);
2504 * d_path_with_unreachable - return the path of a dentry
2505 * @path: path to report
2506 * @buf: buffer to return value in
2507 * @buflen: buffer length
2509 * The difference from d_path() is that this prepends "(unreachable)"
2510 * to paths which are unreachable from the current process' root.
2512 char *d_path_with_unreachable(const struct path
*path
, char *buf
, int buflen
)
2514 char *res
= buf
+ buflen
;
2519 if (path
->dentry
->d_op
&& path
->dentry
->d_op
->d_dname
)
2520 return path
->dentry
->d_op
->d_dname(path
->dentry
, buf
, buflen
);
2522 get_fs_root(current
->fs
, &root
);
2523 spin_lock(&dcache_lock
);
2524 write_seqlock(&rename_lock
);
2526 error
= path_with_deleted(path
, &tmp
, &res
, &buflen
);
2527 if (!error
&& !path_equal(&tmp
, &root
))
2528 error
= prepend_unreachable(&res
, &buflen
);
2529 write_sequnlock(&rename_lock
);
2530 spin_unlock(&dcache_lock
);
2533 res
= ERR_PTR(error
);
2539 * Helper function for dentry_operations.d_dname() members
2541 char *dynamic_dname(struct dentry
*dentry
, char *buffer
, int buflen
,
2542 const char *fmt
, ...)
2548 va_start(args
, fmt
);
2549 sz
= vsnprintf(temp
, sizeof(temp
), fmt
, args
) + 1;
2552 if (sz
> sizeof(temp
) || sz
> buflen
)
2553 return ERR_PTR(-ENAMETOOLONG
);
2555 buffer
+= buflen
- sz
;
2556 return memcpy(buffer
, temp
, sz
);
2560 * Write full pathname from the root of the filesystem into the buffer.
2562 static char *__dentry_path(struct dentry
*dentry
, char *buf
, int buflen
)
2564 char *end
= buf
+ buflen
;
2567 prepend(&end
, &buflen
, "\0", 1);
2574 while (!IS_ROOT(dentry
)) {
2575 struct dentry
*parent
= dentry
->d_parent
;
2579 spin_lock(&dentry
->d_lock
);
2580 error
= prepend_name(&end
, &buflen
, &dentry
->d_name
);
2581 spin_unlock(&dentry
->d_lock
);
2582 if (error
!= 0 || prepend(&end
, &buflen
, "/", 1) != 0)
2590 return ERR_PTR(-ENAMETOOLONG
);
2593 char *dentry_path_raw(struct dentry
*dentry
, char *buf
, int buflen
)
2597 spin_lock(&dcache_lock
);
2598 write_seqlock(&rename_lock
);
2599 retval
= __dentry_path(dentry
, buf
, buflen
);
2600 write_sequnlock(&rename_lock
);
2601 spin_unlock(&dcache_lock
);
2605 EXPORT_SYMBOL(dentry_path_raw
);
2607 char *dentry_path(struct dentry
*dentry
, char *buf
, int buflen
)
2612 spin_lock(&dcache_lock
);
2613 write_seqlock(&rename_lock
);
2614 if (d_unlinked(dentry
)) {
2616 if (prepend(&p
, &buflen
, "//deleted", 10) != 0)
2620 retval
= __dentry_path(dentry
, buf
, buflen
);
2621 write_sequnlock(&rename_lock
);
2622 spin_unlock(&dcache_lock
);
2623 if (!IS_ERR(retval
) && p
)
2624 *p
= '/'; /* restore '/' overriden with '\0' */
2627 spin_unlock(&dcache_lock
);
2628 return ERR_PTR(-ENAMETOOLONG
);
2632 * NOTE! The user-level library version returns a
2633 * character pointer. The kernel system call just
2634 * returns the length of the buffer filled (which
2635 * includes the ending '\0' character), or a negative
2636 * error value. So libc would do something like
2638 * char *getcwd(char * buf, size_t size)
2642 * retval = sys_getcwd(buf, size);
2649 SYSCALL_DEFINE2(getcwd
, char __user
*, buf
, unsigned long, size
)
2652 struct path pwd
, root
;
2653 char *page
= (char *) __get_free_page(GFP_USER
);
2658 get_fs_root_and_pwd(current
->fs
, &root
, &pwd
);
2661 spin_lock(&dcache_lock
);
2662 write_seqlock(&rename_lock
);
2663 if (!d_unlinked(pwd
.dentry
)) {
2665 struct path tmp
= root
;
2666 char *cwd
= page
+ PAGE_SIZE
;
2667 int buflen
= PAGE_SIZE
;
2669 prepend(&cwd
, &buflen
, "\0", 1);
2670 error
= prepend_path(&pwd
, &tmp
, &cwd
, &buflen
);
2671 write_sequnlock(&rename_lock
);
2672 spin_unlock(&dcache_lock
);
2677 /* Unreachable from current root */
2678 if (!path_equal(&tmp
, &root
)) {
2679 error
= prepend_unreachable(&cwd
, &buflen
);
2685 len
= PAGE_SIZE
+ page
- cwd
;
2688 if (copy_to_user(buf
, cwd
, len
))
2692 write_sequnlock(&rename_lock
);
2693 spin_unlock(&dcache_lock
);
2699 free_page((unsigned long) page
);
2704 * Test whether new_dentry is a subdirectory of old_dentry.
2706 * Trivially implemented using the dcache structure
2710 * is_subdir - is new dentry a subdirectory of old_dentry
2711 * @new_dentry: new dentry
2712 * @old_dentry: old dentry
2714 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2715 * Returns 0 otherwise.
2716 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2719 int is_subdir(struct dentry
*new_dentry
, struct dentry
*old_dentry
)
2724 if (new_dentry
== old_dentry
)
2728 /* for restarting inner loop in case of seq retry */
2729 seq
= read_seqbegin(&rename_lock
);
2731 * Need rcu_readlock to protect against the d_parent trashing
2735 if (d_ancestor(old_dentry
, new_dentry
))
2740 } while (read_seqretry(&rename_lock
, seq
));
2745 int path_is_under(struct path
*path1
, struct path
*path2
)
2747 struct vfsmount
*mnt
= path1
->mnt
;
2748 struct dentry
*dentry
= path1
->dentry
;
2751 br_read_lock(vfsmount_lock
);
2752 if (mnt
!= path2
->mnt
) {
2754 if (mnt
->mnt_parent
== mnt
) {
2755 br_read_unlock(vfsmount_lock
);
2758 if (mnt
->mnt_parent
== path2
->mnt
)
2760 mnt
= mnt
->mnt_parent
;
2762 dentry
= mnt
->mnt_mountpoint
;
2764 res
= is_subdir(dentry
, path2
->dentry
);
2765 br_read_unlock(vfsmount_lock
);
2768 EXPORT_SYMBOL(path_is_under
);
2770 void d_genocide(struct dentry
*root
)
2772 struct dentry
*this_parent
;
2773 struct list_head
*next
;
2778 seq
= read_seqbegin(&rename_lock
);
2779 spin_lock(&dcache_lock
);
2780 spin_lock(&this_parent
->d_lock
);
2782 next
= this_parent
->d_subdirs
.next
;
2784 while (next
!= &this_parent
->d_subdirs
) {
2785 struct list_head
*tmp
= next
;
2786 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_u
.d_child
);
2789 spin_lock_nested(&dentry
->d_lock
, DENTRY_D_LOCK_NESTED
);
2790 if (d_unhashed(dentry
) || !dentry
->d_inode
) {
2791 spin_unlock(&dentry
->d_lock
);
2794 if (!list_empty(&dentry
->d_subdirs
)) {
2795 spin_unlock(&this_parent
->d_lock
);
2796 spin_release(&dentry
->d_lock
.dep_map
, 1, _RET_IP_
);
2797 this_parent
= dentry
;
2798 spin_acquire(&this_parent
->d_lock
.dep_map
, 0, 1, _RET_IP_
);
2801 if (!(dentry
->d_flags
& DCACHE_GENOCIDE
)) {
2802 dentry
->d_flags
|= DCACHE_GENOCIDE
;
2805 spin_unlock(&dentry
->d_lock
);
2807 if (this_parent
!= root
) {
2809 struct dentry
*child
;
2811 tmp
= this_parent
->d_parent
;
2812 if (!(this_parent
->d_flags
& DCACHE_GENOCIDE
)) {
2813 this_parent
->d_flags
|= DCACHE_GENOCIDE
;
2814 this_parent
->d_count
--;
2817 spin_unlock(&this_parent
->d_lock
);
2818 child
= this_parent
;
2820 spin_lock(&this_parent
->d_lock
);
2821 /* might go back up the wrong parent if we have had a rename
2823 if (this_parent
!= child
->d_parent
||
2824 read_seqretry(&rename_lock
, seq
)) {
2825 spin_unlock(&this_parent
->d_lock
);
2826 spin_unlock(&dcache_lock
);
2831 next
= child
->d_u
.d_child
.next
;
2834 spin_unlock(&this_parent
->d_lock
);
2835 spin_unlock(&dcache_lock
);
2836 if (read_seqretry(&rename_lock
, seq
))
2841 * find_inode_number - check for dentry with name
2842 * @dir: directory to check
2843 * @name: Name to find.
2845 * Check whether a dentry already exists for the given name,
2846 * and return the inode number if it has an inode. Otherwise
2849 * This routine is used to post-process directory listings for
2850 * filesystems using synthetic inode numbers, and is necessary
2851 * to keep getcwd() working.
2854 ino_t
find_inode_number(struct dentry
*dir
, struct qstr
*name
)
2856 struct dentry
* dentry
;
2859 dentry
= d_hash_and_lookup(dir
, name
);
2861 if (dentry
->d_inode
)
2862 ino
= dentry
->d_inode
->i_ino
;
2867 EXPORT_SYMBOL(find_inode_number
);
2869 static __initdata
unsigned long dhash_entries
;
2870 static int __init
set_dhash_entries(char *str
)
2874 dhash_entries
= simple_strtoul(str
, &str
, 0);
2877 __setup("dhash_entries=", set_dhash_entries
);
2879 static void __init
dcache_init_early(void)
2883 /* If hashes are distributed across NUMA nodes, defer
2884 * hash allocation until vmalloc space is available.
2890 alloc_large_system_hash("Dentry cache",
2891 sizeof(struct hlist_head
),
2899 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2900 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2903 static void __init
dcache_init(void)
2908 * A constructor could be added for stable state like the lists,
2909 * but it is probably not worth it because of the cache nature
2912 dentry_cache
= KMEM_CACHE(dentry
,
2913 SLAB_RECLAIM_ACCOUNT
|SLAB_PANIC
|SLAB_MEM_SPREAD
);
2915 register_shrinker(&dcache_shrinker
);
2917 /* Hash may have been set up in dcache_init_early */
2922 alloc_large_system_hash("Dentry cache",
2923 sizeof(struct hlist_head
),
2931 for (loop
= 0; loop
< (1 << d_hash_shift
); loop
++)
2932 INIT_HLIST_HEAD(&dentry_hashtable
[loop
]);
2935 /* SLAB cache for __getname() consumers */
2936 struct kmem_cache
*names_cachep __read_mostly
;
2937 EXPORT_SYMBOL(names_cachep
);
2939 EXPORT_SYMBOL(d_genocide
);
2941 void __init
vfs_caches_init_early(void)
2943 dcache_init_early();
2947 void __init
vfs_caches_init(unsigned long mempages
)
2949 unsigned long reserve
;
2951 /* Base hash sizes on available memory, with a reserve equal to
2952 150% of current kernel size */
2954 reserve
= min((mempages
- nr_free_pages()) * 3/2, mempages
- 1);
2955 mempages
-= reserve
;
2957 names_cachep
= kmem_cache_create("names_cache", PATH_MAX
, 0,
2958 SLAB_HWCACHE_ALIGN
|SLAB_PANIC
, NULL
);
2962 files_init(mempages
);