added 2.6.29.6 aldebaran kernel
[nao-ulib.git] / kernel / 2.6.29.6-aldebaran-rt / fs / dcache.c
blob78a7ff1272f82f73cc6cdeed45d6e7172896dd4d
1 /*
2 * fs/dcache.c
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
7 */
9 /*
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>
19 #include <linux/mm.h>
20 #include <linux/fdtable.h>
21 #include <linux/fs.h>
22 #include <linux/fsnotify.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/hash.h>
26 #include <linux/cache.h>
27 #include <linux/module.h>
28 #include <linux/mount.h>
29 #include <linux/file.h>
30 #include <asm/uaccess.h>
31 #include <linux/security.h>
32 #include <linux/seqlock.h>
33 #include <linux/swap.h>
34 #include <linux/bootmem.h>
35 #include "internal.h"
37 int sysctl_vfs_cache_pressure __read_mostly = 100;
38 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
40 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
41 __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
43 EXPORT_SYMBOL(dcache_lock);
45 static struct kmem_cache *dentry_cache __read_mostly;
47 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
50 * This is the single most critical data structure when it comes
51 * to the dcache: the hashtable for lookups. Somebody should try
52 * to make this good - I've just made it work.
54 * This hash-function tries to avoid losing too many bits of hash
55 * information, yet avoid using a prime hash-size or similar.
57 #define D_HASHBITS d_hash_shift
58 #define D_HASHMASK d_hash_mask
60 static unsigned int d_hash_mask __read_mostly;
61 static unsigned int d_hash_shift __read_mostly;
62 static struct hlist_head *dentry_hashtable __read_mostly;
64 /* Statistics gathering. */
65 struct dentry_stat_t dentry_stat = {
66 .age_limit = 45,
69 static void __d_free(struct dentry *dentry)
71 WARN_ON(!list_empty(&dentry->d_alias));
72 if (dname_external(dentry))
73 kfree(dentry->d_name.name);
74 kmem_cache_free(dentry_cache, dentry);
77 static void d_callback(struct rcu_head *head)
79 struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
80 __d_free(dentry);
84 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
85 * inside dcache_lock.
87 static void d_free(struct dentry *dentry)
89 if (dentry->d_op && dentry->d_op->d_release)
90 dentry->d_op->d_release(dentry);
91 /* if dentry was never inserted into hash, immediate free is OK */
92 if (hlist_unhashed(&dentry->d_hash))
93 __d_free(dentry);
94 else
95 call_rcu(&dentry->d_u.d_rcu, d_callback);
99 * Release the dentry's inode, using the filesystem
100 * d_iput() operation if defined.
102 static void dentry_iput(struct dentry * dentry)
103 __releases(dentry->d_lock)
104 __releases(dcache_lock)
106 struct inode *inode = dentry->d_inode;
107 if (inode) {
108 dentry->d_inode = NULL;
109 list_del_init(&dentry->d_alias);
110 spin_unlock(&dentry->d_lock);
111 spin_unlock(&dcache_lock);
112 if (!inode->i_nlink)
113 fsnotify_inoderemove(inode);
114 if (dentry->d_op && dentry->d_op->d_iput)
115 dentry->d_op->d_iput(dentry, inode);
116 else
117 iput(inode);
118 } else {
119 spin_unlock(&dentry->d_lock);
120 spin_unlock(&dcache_lock);
125 * dentry_lru_(add|add_tail|del|del_init) must be called with dcache_lock held.
127 static void dentry_lru_add(struct dentry *dentry)
129 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
130 dentry->d_sb->s_nr_dentry_unused++;
131 dentry_stat.nr_unused++;
134 static void dentry_lru_add_tail(struct dentry *dentry)
136 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
137 dentry->d_sb->s_nr_dentry_unused++;
138 dentry_stat.nr_unused++;
141 static void dentry_lru_del(struct dentry *dentry)
143 if (!list_empty(&dentry->d_lru)) {
144 list_del(&dentry->d_lru);
145 dentry->d_sb->s_nr_dentry_unused--;
146 dentry_stat.nr_unused--;
150 static void dentry_lru_del_init(struct dentry *dentry)
152 if (likely(!list_empty(&dentry->d_lru))) {
153 list_del_init(&dentry->d_lru);
154 dentry->d_sb->s_nr_dentry_unused--;
155 dentry_stat.nr_unused--;
160 * d_kill - kill dentry and return parent
161 * @dentry: dentry to kill
163 * The dentry must already be unhashed and removed from the LRU.
165 * If this is the root of the dentry tree, return NULL.
167 static struct dentry *d_kill(struct dentry *dentry)
168 __releases(dentry->d_lock)
169 __releases(dcache_lock)
171 struct dentry *parent;
173 list_del(&dentry->d_u.d_child);
174 dentry_stat.nr_dentry--; /* For d_free, below */
175 /*drops the locks, at that point nobody can reach this dentry */
176 dentry_iput(dentry);
177 if (IS_ROOT(dentry))
178 parent = NULL;
179 else
180 parent = dentry->d_parent;
181 d_free(dentry);
182 return parent;
186 * This is dput
188 * This is complicated by the fact that we do not want to put
189 * dentries that are no longer on any hash chain on the unused
190 * list: we'd much rather just get rid of them immediately.
192 * However, that implies that we have to traverse the dentry
193 * tree upwards to the parents which might _also_ now be
194 * scheduled for deletion (it may have been only waiting for
195 * its last child to go away).
197 * This tail recursion is done by hand as we don't want to depend
198 * on the compiler to always get this right (gcc generally doesn't).
199 * Real recursion would eat up our stack space.
203 * dput - release a dentry
204 * @dentry: dentry to release
206 * Release a dentry. This will drop the usage count and if appropriate
207 * call the dentry unlink method as well as removing it from the queues and
208 * releasing its resources. If the parent dentries were scheduled for release
209 * they too may now get deleted.
211 * no dcache lock, please.
214 void dput(struct dentry *dentry)
216 if (!dentry)
217 return;
219 repeat:
220 if (atomic_read(&dentry->d_count) == 1)
221 might_sleep();
222 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
223 return;
225 spin_lock(&dentry->d_lock);
226 if (atomic_read(&dentry->d_count)) {
227 spin_unlock(&dentry->d_lock);
228 spin_unlock(&dcache_lock);
229 return;
233 * AV: ->d_delete() is _NOT_ allowed to block now.
235 if (dentry->d_op && dentry->d_op->d_delete) {
236 if (dentry->d_op->d_delete(dentry))
237 goto unhash_it;
239 /* Unreachable? Get rid of it */
240 if (d_unhashed(dentry))
241 goto kill_it;
242 if (list_empty(&dentry->d_lru)) {
243 dentry->d_flags |= DCACHE_REFERENCED;
244 dentry_lru_add(dentry);
246 spin_unlock(&dentry->d_lock);
247 spin_unlock(&dcache_lock);
248 return;
250 unhash_it:
251 __d_drop(dentry);
252 kill_it:
253 /* if dentry was on the d_lru list delete it from there */
254 dentry_lru_del(dentry);
255 dentry = d_kill(dentry);
256 if (dentry)
257 goto repeat;
261 * d_invalidate - invalidate a dentry
262 * @dentry: dentry to invalidate
264 * Try to invalidate the dentry if it turns out to be
265 * possible. If there are other dentries that can be
266 * reached through this one we can't delete it and we
267 * return -EBUSY. On success we return 0.
269 * no dcache lock.
272 int d_invalidate(struct dentry * dentry)
275 * If it's already been dropped, return OK.
277 spin_lock(&dcache_lock);
278 if (d_unhashed(dentry)) {
279 spin_unlock(&dcache_lock);
280 return 0;
283 * Check whether to do a partial shrink_dcache
284 * to get rid of unused child entries.
286 if (!list_empty(&dentry->d_subdirs)) {
287 spin_unlock(&dcache_lock);
288 shrink_dcache_parent(dentry);
289 spin_lock(&dcache_lock);
293 * Somebody else still using it?
295 * If it's a directory, we can't drop it
296 * for fear of somebody re-populating it
297 * with children (even though dropping it
298 * would make it unreachable from the root,
299 * we might still populate it if it was a
300 * working directory or similar).
302 spin_lock(&dentry->d_lock);
303 if (atomic_read(&dentry->d_count) > 1) {
304 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
305 spin_unlock(&dentry->d_lock);
306 spin_unlock(&dcache_lock);
307 return -EBUSY;
311 __d_drop(dentry);
312 spin_unlock(&dentry->d_lock);
313 spin_unlock(&dcache_lock);
314 return 0;
317 /* This should be called _only_ with dcache_lock held */
319 static inline struct dentry * __dget_locked(struct dentry *dentry)
321 atomic_inc(&dentry->d_count);
322 dentry_lru_del_init(dentry);
323 return dentry;
326 struct dentry * dget_locked(struct dentry *dentry)
328 return __dget_locked(dentry);
332 * d_find_alias - grab a hashed alias of inode
333 * @inode: inode in question
334 * @want_discon: flag, used by d_splice_alias, to request
335 * that only a DISCONNECTED alias be returned.
337 * If inode has a hashed alias, or is a directory and has any alias,
338 * acquire the reference to alias and return it. Otherwise return NULL.
339 * Notice that if inode is a directory there can be only one alias and
340 * it can be unhashed only if it has no children, or if it is the root
341 * of a filesystem.
343 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
344 * any other hashed alias over that one unless @want_discon is set,
345 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
348 static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
350 struct list_head *head, *next, *tmp;
351 struct dentry *alias, *discon_alias=NULL;
353 head = &inode->i_dentry;
354 next = inode->i_dentry.next;
355 while (next != head) {
356 tmp = next;
357 next = tmp->next;
358 prefetch(next);
359 alias = list_entry(tmp, struct dentry, d_alias);
360 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
361 if (IS_ROOT(alias) &&
362 (alias->d_flags & DCACHE_DISCONNECTED))
363 discon_alias = alias;
364 else if (!want_discon) {
365 __dget_locked(alias);
366 return alias;
370 if (discon_alias)
371 __dget_locked(discon_alias);
372 return discon_alias;
375 struct dentry * d_find_alias(struct inode *inode)
377 struct dentry *de = NULL;
379 if (!list_empty(&inode->i_dentry)) {
380 spin_lock(&dcache_lock);
381 de = __d_find_alias(inode, 0);
382 spin_unlock(&dcache_lock);
384 return de;
388 * Try to kill dentries associated with this inode.
389 * WARNING: you must own a reference to inode.
391 void d_prune_aliases(struct inode *inode)
393 struct dentry *dentry;
394 restart:
395 spin_lock(&dcache_lock);
396 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
397 spin_lock(&dentry->d_lock);
398 if (!atomic_read(&dentry->d_count)) {
399 __dget_locked(dentry);
400 __d_drop(dentry);
401 spin_unlock(&dentry->d_lock);
402 spin_unlock(&dcache_lock);
403 dput(dentry);
404 goto restart;
406 spin_unlock(&dentry->d_lock);
408 spin_unlock(&dcache_lock);
412 * Throw away a dentry - free the inode, dput the parent. This requires that
413 * the LRU list has already been removed.
415 * Try to prune ancestors as well. This is necessary to prevent
416 * quadratic behavior of shrink_dcache_parent(), but is also expected
417 * to be beneficial in reducing dentry cache fragmentation.
419 static void prune_one_dentry(struct dentry * dentry)
420 __releases(dentry->d_lock)
421 __releases(dcache_lock)
422 __acquires(dcache_lock)
424 __d_drop(dentry);
425 dentry = d_kill(dentry);
428 * Prune ancestors. Locking is simpler than in dput(),
429 * because dcache_lock needs to be taken anyway.
431 spin_lock(&dcache_lock);
432 while (dentry) {
433 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
434 return;
436 if (dentry->d_op && dentry->d_op->d_delete)
437 dentry->d_op->d_delete(dentry);
438 dentry_lru_del_init(dentry);
439 __d_drop(dentry);
440 dentry = d_kill(dentry);
441 spin_lock(&dcache_lock);
446 * Shrink the dentry LRU on a given superblock.
447 * @sb : superblock to shrink dentry LRU.
448 * @count: If count is NULL, we prune all dentries on superblock.
449 * @flags: If flags is non-zero, we need to do special processing based on
450 * which flags are set. This means we don't need to maintain multiple
451 * similar copies of this loop.
453 static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
455 LIST_HEAD(referenced);
456 LIST_HEAD(tmp);
457 struct dentry *dentry;
458 int cnt = 0;
460 BUG_ON(!sb);
461 BUG_ON((flags & DCACHE_REFERENCED) && count == NULL);
462 spin_lock(&dcache_lock);
463 if (count != NULL)
464 /* called from prune_dcache() and shrink_dcache_parent() */
465 cnt = *count;
466 restart:
467 if (count == NULL)
468 list_splice_init(&sb->s_dentry_lru, &tmp);
469 else {
470 while (!list_empty(&sb->s_dentry_lru)) {
471 dentry = list_entry(sb->s_dentry_lru.prev,
472 struct dentry, d_lru);
473 BUG_ON(dentry->d_sb != sb);
475 spin_lock(&dentry->d_lock);
477 * If we are honouring the DCACHE_REFERENCED flag and
478 * the dentry has this flag set, don't free it. Clear
479 * the flag and put it back on the LRU.
481 if ((flags & DCACHE_REFERENCED)
482 && (dentry->d_flags & DCACHE_REFERENCED)) {
483 dentry->d_flags &= ~DCACHE_REFERENCED;
484 list_move_tail(&dentry->d_lru, &referenced);
485 spin_unlock(&dentry->d_lock);
486 } else {
487 list_move_tail(&dentry->d_lru, &tmp);
488 spin_unlock(&dentry->d_lock);
489 cnt--;
490 if (!cnt)
491 break;
493 cond_resched_lock(&dcache_lock);
496 while (!list_empty(&tmp)) {
497 dentry = list_entry(tmp.prev, struct dentry, d_lru);
498 dentry_lru_del_init(dentry);
499 spin_lock(&dentry->d_lock);
501 * We found an inuse dentry which was not removed from
502 * the LRU because of laziness during lookup. Do not free
503 * it - just keep it off the LRU list.
505 if (atomic_read(&dentry->d_count)) {
506 spin_unlock(&dentry->d_lock);
507 continue;
509 prune_one_dentry(dentry);
510 /* dentry->d_lock was dropped in prune_one_dentry() */
511 cond_resched_lock(&dcache_lock);
513 if (count == NULL && !list_empty(&sb->s_dentry_lru))
514 goto restart;
515 if (count != NULL)
516 *count = cnt;
517 if (!list_empty(&referenced))
518 list_splice(&referenced, &sb->s_dentry_lru);
519 spin_unlock(&dcache_lock);
523 * prune_dcache - shrink the dcache
524 * @count: number of entries to try to free
526 * Shrink the dcache. This is done when we need more memory, or simply when we
527 * need to unmount something (at which point we need to unuse all dentries).
529 * This function may fail to free any resources if all the dentries are in use.
531 static void prune_dcache(int count)
533 struct super_block *sb;
534 int w_count;
535 int unused = dentry_stat.nr_unused;
536 int prune_ratio;
537 int pruned;
539 if (unused == 0 || count == 0)
540 return;
541 spin_lock(&dcache_lock);
542 restart:
543 if (count >= unused)
544 prune_ratio = 1;
545 else
546 prune_ratio = unused / count;
547 spin_lock(&sb_lock);
548 list_for_each_entry(sb, &super_blocks, s_list) {
549 if (sb->s_nr_dentry_unused == 0)
550 continue;
551 sb->s_count++;
552 /* Now, we reclaim unused dentrins with fairness.
553 * We reclaim them same percentage from each superblock.
554 * We calculate number of dentries to scan on this sb
555 * as follows, but the implementation is arranged to avoid
556 * overflows:
557 * number of dentries to scan on this sb =
558 * count * (number of dentries on this sb /
559 * number of dentries in the machine)
561 spin_unlock(&sb_lock);
562 if (prune_ratio != 1)
563 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
564 else
565 w_count = sb->s_nr_dentry_unused;
566 pruned = w_count;
568 * We need to be sure this filesystem isn't being unmounted,
569 * otherwise we could race with generic_shutdown_super(), and
570 * end up holding a reference to an inode while the filesystem
571 * is unmounted. So we try to get s_umount, and make sure
572 * s_root isn't NULL.
574 if (down_read_trylock(&sb->s_umount)) {
575 if ((sb->s_root != NULL) &&
576 (!list_empty(&sb->s_dentry_lru))) {
577 spin_unlock(&dcache_lock);
578 __shrink_dcache_sb(sb, &w_count,
579 DCACHE_REFERENCED);
580 pruned -= w_count;
581 spin_lock(&dcache_lock);
583 up_read(&sb->s_umount);
585 spin_lock(&sb_lock);
586 count -= pruned;
588 * restart only when sb is no longer on the list and
589 * we have more work to do.
591 if (__put_super_and_need_restart(sb) && count > 0) {
592 spin_unlock(&sb_lock);
593 goto restart;
596 spin_unlock(&sb_lock);
597 spin_unlock(&dcache_lock);
601 * shrink_dcache_sb - shrink dcache for a superblock
602 * @sb: superblock
604 * Shrink the dcache for the specified super block. This
605 * is used to free the dcache before unmounting a file
606 * system
608 void shrink_dcache_sb(struct super_block * sb)
610 __shrink_dcache_sb(sb, NULL, 0);
614 * destroy a single subtree of dentries for unmount
615 * - see the comments on shrink_dcache_for_umount() for a description of the
616 * locking
618 static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
620 struct dentry *parent;
621 unsigned detached = 0;
623 BUG_ON(!IS_ROOT(dentry));
625 /* detach this root from the system */
626 spin_lock(&dcache_lock);
627 dentry_lru_del_init(dentry);
628 __d_drop(dentry);
629 spin_unlock(&dcache_lock);
631 for (;;) {
632 /* descend to the first leaf in the current subtree */
633 while (!list_empty(&dentry->d_subdirs)) {
634 struct dentry *loop;
636 /* this is a branch with children - detach all of them
637 * from the system in one go */
638 spin_lock(&dcache_lock);
639 list_for_each_entry(loop, &dentry->d_subdirs,
640 d_u.d_child) {
641 dentry_lru_del_init(loop);
642 __d_drop(loop);
643 cond_resched_lock(&dcache_lock);
645 spin_unlock(&dcache_lock);
647 /* move to the first child */
648 dentry = list_entry(dentry->d_subdirs.next,
649 struct dentry, d_u.d_child);
652 /* consume the dentries from this leaf up through its parents
653 * until we find one with children or run out altogether */
654 do {
655 struct inode *inode;
657 if (atomic_read(&dentry->d_count) != 0) {
658 printk(KERN_ERR
659 "BUG: Dentry %p{i=%lx,n=%s}"
660 " still in use (%d)"
661 " [unmount of %s %s]\n",
662 dentry,
663 dentry->d_inode ?
664 dentry->d_inode->i_ino : 0UL,
665 dentry->d_name.name,
666 atomic_read(&dentry->d_count),
667 dentry->d_sb->s_type->name,
668 dentry->d_sb->s_id);
669 BUG();
672 if (IS_ROOT(dentry))
673 parent = NULL;
674 else {
675 parent = dentry->d_parent;
676 atomic_dec(&parent->d_count);
679 list_del(&dentry->d_u.d_child);
680 detached++;
682 inode = dentry->d_inode;
683 if (inode) {
684 dentry->d_inode = NULL;
685 list_del_init(&dentry->d_alias);
686 if (dentry->d_op && dentry->d_op->d_iput)
687 dentry->d_op->d_iput(dentry, inode);
688 else
689 iput(inode);
692 d_free(dentry);
694 /* finished when we fall off the top of the tree,
695 * otherwise we ascend to the parent and move to the
696 * next sibling if there is one */
697 if (!parent)
698 goto out;
700 dentry = parent;
702 } while (list_empty(&dentry->d_subdirs));
704 dentry = list_entry(dentry->d_subdirs.next,
705 struct dentry, d_u.d_child);
707 out:
708 /* several dentries were freed, need to correct nr_dentry */
709 spin_lock(&dcache_lock);
710 dentry_stat.nr_dentry -= detached;
711 spin_unlock(&dcache_lock);
715 * destroy the dentries attached to a superblock on unmounting
716 * - we don't need to use dentry->d_lock, and only need dcache_lock when
717 * removing the dentry from the system lists and hashes because:
718 * - the superblock is detached from all mountings and open files, so the
719 * dentry trees will not be rearranged by the VFS
720 * - s_umount is write-locked, so the memory pressure shrinker will ignore
721 * any dentries belonging to this superblock that it comes across
722 * - the filesystem itself is no longer permitted to rearrange the dentries
723 * in this superblock
725 void shrink_dcache_for_umount(struct super_block *sb)
727 struct dentry *dentry;
729 // -rt: this might succeed there ...
730 // if (down_read_trylock(&sb->s_umount))
731 // BUG();
733 dentry = sb->s_root;
734 sb->s_root = NULL;
735 atomic_dec(&dentry->d_count);
736 shrink_dcache_for_umount_subtree(dentry);
738 while (!hlist_empty(&sb->s_anon)) {
739 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
740 shrink_dcache_for_umount_subtree(dentry);
745 * Search for at least 1 mount point in the dentry's subdirs.
746 * We descend to the next level whenever the d_subdirs
747 * list is non-empty and continue searching.
751 * have_submounts - check for mounts over a dentry
752 * @parent: dentry to check.
754 * Return true if the parent or its subdirectories contain
755 * a mount point
758 int have_submounts(struct dentry *parent)
760 struct dentry *this_parent = parent;
761 struct list_head *next;
763 spin_lock(&dcache_lock);
764 if (d_mountpoint(parent))
765 goto positive;
766 repeat:
767 next = this_parent->d_subdirs.next;
768 resume:
769 while (next != &this_parent->d_subdirs) {
770 struct list_head *tmp = next;
771 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
772 next = tmp->next;
773 /* Have we found a mount point ? */
774 if (d_mountpoint(dentry))
775 goto positive;
776 if (!list_empty(&dentry->d_subdirs)) {
777 this_parent = dentry;
778 goto repeat;
782 * All done at this level ... ascend and resume the search.
784 if (this_parent != parent) {
785 next = this_parent->d_u.d_child.next;
786 this_parent = this_parent->d_parent;
787 goto resume;
789 spin_unlock(&dcache_lock);
790 return 0; /* No mount points found in tree */
791 positive:
792 spin_unlock(&dcache_lock);
793 return 1;
797 * Search the dentry child list for the specified parent,
798 * and move any unused dentries to the end of the unused
799 * list for prune_dcache(). We descend to the next level
800 * whenever the d_subdirs list is non-empty and continue
801 * searching.
803 * It returns zero iff there are no unused children,
804 * otherwise it returns the number of children moved to
805 * the end of the unused list. This may not be the total
806 * number of unused children, because select_parent can
807 * drop the lock and return early due to latency
808 * constraints.
810 static int select_parent(struct dentry * parent)
812 struct dentry *this_parent = parent;
813 struct list_head *next;
814 int found = 0;
816 spin_lock(&dcache_lock);
817 repeat:
818 next = this_parent->d_subdirs.next;
819 resume:
820 while (next != &this_parent->d_subdirs) {
821 struct list_head *tmp = next;
822 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
823 next = tmp->next;
825 dentry_lru_del_init(dentry);
827 * move only zero ref count dentries to the end
828 * of the unused list for prune_dcache
830 if (!atomic_read(&dentry->d_count)) {
831 dentry_lru_add_tail(dentry);
832 found++;
836 * We can return to the caller if we have found some (this
837 * ensures forward progress). We'll be coming back to find
838 * the rest.
840 if (found && need_resched())
841 goto out;
844 * Descend a level if the d_subdirs list is non-empty.
846 if (!list_empty(&dentry->d_subdirs)) {
847 this_parent = dentry;
848 goto repeat;
852 * All done at this level ... ascend and resume the search.
854 if (this_parent != parent) {
855 next = this_parent->d_u.d_child.next;
856 this_parent = this_parent->d_parent;
857 goto resume;
859 out:
860 spin_unlock(&dcache_lock);
861 return found;
865 * shrink_dcache_parent - prune dcache
866 * @parent: parent of entries to prune
868 * Prune the dcache to remove unused children of the parent dentry.
871 void shrink_dcache_parent(struct dentry * parent)
873 struct super_block *sb = parent->d_sb;
874 int found;
876 while ((found = select_parent(parent)) != 0)
877 __shrink_dcache_sb(sb, &found, 0);
881 * Scan `nr' dentries and return the number which remain.
883 * We need to avoid reentering the filesystem if the caller is performing a
884 * GFP_NOFS allocation attempt. One example deadlock is:
886 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
887 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
888 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
890 * In this case we return -1 to tell the caller that we baled.
892 static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
894 if (nr) {
895 if (!(gfp_mask & __GFP_FS))
896 return -1;
897 prune_dcache(nr);
899 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
902 static struct shrinker dcache_shrinker = {
903 .shrink = shrink_dcache_memory,
904 .seeks = DEFAULT_SEEKS,
908 * d_alloc - allocate a dcache entry
909 * @parent: parent of entry to allocate
910 * @name: qstr of the name
912 * Allocates a dentry. It returns %NULL if there is insufficient memory
913 * available. On a success the dentry is returned. The name passed in is
914 * copied and the copy passed in may be reused after this call.
917 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
919 struct dentry *dentry;
920 char *dname;
922 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
923 if (!dentry)
924 return NULL;
926 if (name->len > DNAME_INLINE_LEN-1) {
927 dname = kmalloc(name->len + 1, GFP_KERNEL);
928 if (!dname) {
929 kmem_cache_free(dentry_cache, dentry);
930 return NULL;
932 } else {
933 dname = dentry->d_iname;
935 dentry->d_name.name = dname;
937 dentry->d_name.len = name->len;
938 dentry->d_name.hash = name->hash;
939 memcpy(dname, name->name, name->len);
940 dname[name->len] = 0;
942 atomic_set(&dentry->d_count, 1);
943 dentry->d_flags = DCACHE_UNHASHED;
944 spin_lock_init(&dentry->d_lock);
945 dentry->d_inode = NULL;
946 dentry->d_parent = NULL;
947 dentry->d_sb = NULL;
948 dentry->d_op = NULL;
949 dentry->d_fsdata = NULL;
950 dentry->d_mounted = 0;
951 INIT_HLIST_NODE(&dentry->d_hash);
952 INIT_LIST_HEAD(&dentry->d_lru);
953 INIT_LIST_HEAD(&dentry->d_subdirs);
954 INIT_LIST_HEAD(&dentry->d_alias);
956 if (parent) {
957 dentry->d_parent = dget(parent);
958 dentry->d_sb = parent->d_sb;
959 } else {
960 INIT_LIST_HEAD(&dentry->d_u.d_child);
963 spin_lock(&dcache_lock);
964 if (parent)
965 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
966 dentry_stat.nr_dentry++;
967 spin_unlock(&dcache_lock);
969 return dentry;
972 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
974 struct qstr q;
976 q.name = name;
977 q.len = strlen(name);
978 q.hash = full_name_hash(q.name, q.len);
979 return d_alloc(parent, &q);
982 /* the caller must hold dcache_lock */
983 static void __d_instantiate(struct dentry *dentry, struct inode *inode)
985 if (inode)
986 list_add(&dentry->d_alias, &inode->i_dentry);
987 dentry->d_inode = inode;
988 fsnotify_d_instantiate(dentry, inode);
992 * d_instantiate - fill in inode information for a dentry
993 * @entry: dentry to complete
994 * @inode: inode to attach to this dentry
996 * Fill in inode information in the entry.
998 * This turns negative dentries into productive full members
999 * of society.
1001 * NOTE! This assumes that the inode count has been incremented
1002 * (or otherwise set) by the caller to indicate that it is now
1003 * in use by the dcache.
1006 void d_instantiate(struct dentry *entry, struct inode * inode)
1008 BUG_ON(!list_empty(&entry->d_alias));
1009 spin_lock(&dcache_lock);
1010 __d_instantiate(entry, inode);
1011 spin_unlock(&dcache_lock);
1012 security_d_instantiate(entry, inode);
1016 * d_instantiate_unique - instantiate a non-aliased dentry
1017 * @entry: dentry to instantiate
1018 * @inode: inode to attach to this dentry
1020 * Fill in inode information in the entry. On success, it returns NULL.
1021 * If an unhashed alias of "entry" already exists, then we return the
1022 * aliased dentry instead and drop one reference to inode.
1024 * Note that in order to avoid conflicts with rename() etc, the caller
1025 * had better be holding the parent directory semaphore.
1027 * This also assumes that the inode count has been incremented
1028 * (or otherwise set) by the caller to indicate that it is now
1029 * in use by the dcache.
1031 static struct dentry *__d_instantiate_unique(struct dentry *entry,
1032 struct inode *inode)
1034 struct dentry *alias;
1035 int len = entry->d_name.len;
1036 const char *name = entry->d_name.name;
1037 unsigned int hash = entry->d_name.hash;
1039 if (!inode) {
1040 __d_instantiate(entry, NULL);
1041 return NULL;
1044 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1045 struct qstr *qstr = &alias->d_name;
1047 if (qstr->hash != hash)
1048 continue;
1049 if (alias->d_parent != entry->d_parent)
1050 continue;
1051 if (qstr->len != len)
1052 continue;
1053 if (memcmp(qstr->name, name, len))
1054 continue;
1055 dget_locked(alias);
1056 return alias;
1059 __d_instantiate(entry, inode);
1060 return NULL;
1063 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1065 struct dentry *result;
1067 BUG_ON(!list_empty(&entry->d_alias));
1069 spin_lock(&dcache_lock);
1070 result = __d_instantiate_unique(entry, inode);
1071 spin_unlock(&dcache_lock);
1073 if (!result) {
1074 security_d_instantiate(entry, inode);
1075 return NULL;
1078 BUG_ON(!d_unhashed(result));
1079 iput(inode);
1080 return result;
1083 EXPORT_SYMBOL(d_instantiate_unique);
1086 * d_alloc_root - allocate root dentry
1087 * @root_inode: inode to allocate the root for
1089 * Allocate a root ("/") dentry for the inode given. The inode is
1090 * instantiated and returned. %NULL is returned if there is insufficient
1091 * memory or the inode passed is %NULL.
1094 struct dentry * d_alloc_root(struct inode * root_inode)
1096 struct dentry *res = NULL;
1098 if (root_inode) {
1099 static const struct qstr name = { .name = "/", .len = 1 };
1101 res = d_alloc(NULL, &name);
1102 if (res) {
1103 res->d_sb = root_inode->i_sb;
1104 res->d_parent = res;
1105 d_instantiate(res, root_inode);
1108 return res;
1111 static inline struct hlist_head *d_hash(struct dentry *parent,
1112 unsigned long hash)
1114 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1115 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1116 return dentry_hashtable + (hash & D_HASHMASK);
1120 * d_obtain_alias - find or allocate a dentry for a given inode
1121 * @inode: inode to allocate the dentry for
1123 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1124 * similar open by handle operations. The returned dentry may be anonymous,
1125 * or may have a full name (if the inode was already in the cache).
1127 * When called on a directory inode, we must ensure that the inode only ever
1128 * has one dentry. If a dentry is found, that is returned instead of
1129 * allocating a new one.
1131 * On successful return, the reference to the inode has been transferred
1132 * to the dentry. In case of an error the reference on the inode is released.
1133 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1134 * be passed in and will be the error will be propagate to the return value,
1135 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1137 struct dentry *d_obtain_alias(struct inode *inode)
1139 static const struct qstr anonstring = { .name = "" };
1140 struct dentry *tmp;
1141 struct dentry *res;
1143 if (!inode)
1144 return ERR_PTR(-ESTALE);
1145 if (IS_ERR(inode))
1146 return ERR_CAST(inode);
1148 res = d_find_alias(inode);
1149 if (res)
1150 goto out_iput;
1152 tmp = d_alloc(NULL, &anonstring);
1153 if (!tmp) {
1154 res = ERR_PTR(-ENOMEM);
1155 goto out_iput;
1157 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1159 spin_lock(&dcache_lock);
1160 res = __d_find_alias(inode, 0);
1161 if (res) {
1162 spin_unlock(&dcache_lock);
1163 dput(tmp);
1164 goto out_iput;
1167 /* attach a disconnected dentry */
1168 spin_lock(&tmp->d_lock);
1169 tmp->d_sb = inode->i_sb;
1170 tmp->d_inode = inode;
1171 tmp->d_flags |= DCACHE_DISCONNECTED;
1172 tmp->d_flags &= ~DCACHE_UNHASHED;
1173 list_add(&tmp->d_alias, &inode->i_dentry);
1174 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1175 spin_unlock(&tmp->d_lock);
1177 spin_unlock(&dcache_lock);
1178 return tmp;
1180 out_iput:
1181 iput(inode);
1182 return res;
1184 EXPORT_SYMBOL(d_obtain_alias);
1187 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1188 * @inode: the inode which may have a disconnected dentry
1189 * @dentry: a negative dentry which we want to point to the inode.
1191 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1192 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1193 * and return it, else simply d_add the inode to the dentry and return NULL.
1195 * This is needed in the lookup routine of any filesystem that is exportable
1196 * (via knfsd) so that we can build dcache paths to directories effectively.
1198 * If a dentry was found and moved, then it is returned. Otherwise NULL
1199 * is returned. This matches the expected return value of ->lookup.
1202 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1204 struct dentry *new = NULL;
1206 if (inode && S_ISDIR(inode->i_mode)) {
1207 spin_lock(&dcache_lock);
1208 new = __d_find_alias(inode, 1);
1209 if (new) {
1210 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1211 spin_unlock(&dcache_lock);
1212 security_d_instantiate(new, inode);
1213 d_rehash(dentry);
1214 d_move(new, dentry);
1215 iput(inode);
1216 } else {
1217 /* already taking dcache_lock, so d_add() by hand */
1218 __d_instantiate(dentry, inode);
1219 spin_unlock(&dcache_lock);
1220 security_d_instantiate(dentry, inode);
1221 d_rehash(dentry);
1223 } else
1224 d_add(dentry, inode);
1225 return new;
1229 * d_add_ci - lookup or allocate new dentry with case-exact name
1230 * @inode: the inode case-insensitive lookup has found
1231 * @dentry: the negative dentry that was passed to the parent's lookup func
1232 * @name: the case-exact name to be associated with the returned dentry
1234 * This is to avoid filling the dcache with case-insensitive names to the
1235 * same inode, only the actual correct case is stored in the dcache for
1236 * case-insensitive filesystems.
1238 * For a case-insensitive lookup match and if the the case-exact dentry
1239 * already exists in in the dcache, use it and return it.
1241 * If no entry exists with the exact case name, allocate new dentry with
1242 * the exact case, and return the spliced entry.
1244 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
1245 struct qstr *name)
1247 int error;
1248 struct dentry *found;
1249 struct dentry *new;
1251 /* Does a dentry matching the name exist already? */
1252 found = d_hash_and_lookup(dentry->d_parent, name);
1253 /* If not, create it now and return */
1254 if (!found) {
1255 new = d_alloc(dentry->d_parent, name);
1256 if (!new) {
1257 error = -ENOMEM;
1258 goto err_out;
1260 found = d_splice_alias(inode, new);
1261 if (found) {
1262 dput(new);
1263 return found;
1265 return new;
1267 /* Matching dentry exists, check if it is negative. */
1268 if (found->d_inode) {
1269 if (unlikely(found->d_inode != inode)) {
1270 /* This can't happen because bad inodes are unhashed. */
1271 BUG_ON(!is_bad_inode(inode));
1272 BUG_ON(!is_bad_inode(found->d_inode));
1275 * Already have the inode and the dentry attached, decrement
1276 * the reference count to balance the iget() done
1277 * earlier on. We found the dentry using d_lookup() so it
1278 * cannot be disconnected and thus we do not need to worry
1279 * about any NFS/disconnectedness issues here.
1281 iput(inode);
1282 return found;
1285 * Negative dentry: instantiate it unless the inode is a directory and
1286 * has a 'disconnected' dentry (i.e. IS_ROOT and DCACHE_DISCONNECTED),
1287 * in which case d_move() that in place of the found dentry.
1289 if (!S_ISDIR(inode->i_mode)) {
1290 /* Not a directory; everything is easy. */
1291 d_instantiate(found, inode);
1292 return found;
1294 spin_lock(&dcache_lock);
1295 if (list_empty(&inode->i_dentry)) {
1297 * Directory without a 'disconnected' dentry; we need to do
1298 * d_instantiate() by hand because it takes dcache_lock which
1299 * we already hold.
1301 __d_instantiate(found, inode);
1302 spin_unlock(&dcache_lock);
1303 security_d_instantiate(found, inode);
1304 return found;
1307 * Directory with a 'disconnected' dentry; get a reference to the
1308 * 'disconnected' dentry.
1310 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1311 dget_locked(new);
1312 spin_unlock(&dcache_lock);
1313 /* Do security vodoo. */
1314 security_d_instantiate(found, inode);
1315 /* Move new in place of found. */
1316 d_move(new, found);
1317 /* Balance the iget() we did above. */
1318 iput(inode);
1319 /* Throw away found. */
1320 dput(found);
1321 /* Use new as the actual dentry. */
1322 return new;
1324 err_out:
1325 iput(inode);
1326 return ERR_PTR(error);
1330 * d_lookup - search for a dentry
1331 * @parent: parent dentry
1332 * @name: qstr of name we wish to find
1334 * Searches the children of the parent dentry for the name in question. If
1335 * the dentry is found its reference count is incremented and the dentry
1336 * is returned. The caller must use dput to free the entry when it has
1337 * finished using it. %NULL is returned on failure.
1339 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1340 * Memory barriers are used while updating and doing lockless traversal.
1341 * To avoid races with d_move while rename is happening, d_lock is used.
1343 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1344 * and name pointer in one structure pointed by d_qstr.
1346 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1347 * lookup is going on.
1349 * The dentry unused LRU is not updated even if lookup finds the required dentry
1350 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1351 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1352 * acquisition.
1354 * d_lookup() is protected against the concurrent renames in some unrelated
1355 * directory using the seqlockt_t rename_lock.
1358 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1360 struct dentry * dentry = NULL;
1361 unsigned long seq;
1363 do {
1364 seq = read_seqbegin(&rename_lock);
1365 dentry = __d_lookup(parent, name);
1366 if (dentry)
1367 break;
1368 } while (read_seqretry(&rename_lock, seq));
1369 return dentry;
1372 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1374 unsigned int len = name->len;
1375 unsigned int hash = name->hash;
1376 const unsigned char *str = name->name;
1377 struct hlist_head *head = d_hash(parent,hash);
1378 struct dentry *found = NULL;
1379 struct hlist_node *node;
1380 struct dentry *dentry;
1382 rcu_read_lock();
1384 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1385 struct qstr *qstr;
1387 if (dentry->d_name.hash != hash)
1388 continue;
1389 if (dentry->d_parent != parent)
1390 continue;
1392 spin_lock(&dentry->d_lock);
1395 * Recheck the dentry after taking the lock - d_move may have
1396 * changed things. Don't bother checking the hash because we're
1397 * about to compare the whole name anyway.
1399 if (dentry->d_parent != parent)
1400 goto next;
1402 /* non-existing due to RCU? */
1403 if (d_unhashed(dentry))
1404 goto next;
1407 * It is safe to compare names since d_move() cannot
1408 * change the qstr (protected by d_lock).
1410 qstr = &dentry->d_name;
1411 if (parent->d_op && parent->d_op->d_compare) {
1412 if (parent->d_op->d_compare(parent, qstr, name))
1413 goto next;
1414 } else {
1415 if (qstr->len != len)
1416 goto next;
1417 if (memcmp(qstr->name, str, len))
1418 goto next;
1421 atomic_inc(&dentry->d_count);
1422 found = dentry;
1423 spin_unlock(&dentry->d_lock);
1424 break;
1425 next:
1426 spin_unlock(&dentry->d_lock);
1428 rcu_read_unlock();
1430 return found;
1434 * d_hash_and_lookup - hash the qstr then search for a dentry
1435 * @dir: Directory to search in
1436 * @name: qstr of name we wish to find
1438 * On hash failure or on lookup failure NULL is returned.
1440 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1442 struct dentry *dentry = NULL;
1445 * Check for a fs-specific hash function. Note that we must
1446 * calculate the standard hash first, as the d_op->d_hash()
1447 * routine may choose to leave the hash value unchanged.
1449 name->hash = full_name_hash(name->name, name->len);
1450 if (dir->d_op && dir->d_op->d_hash) {
1451 if (dir->d_op->d_hash(dir, name) < 0)
1452 goto out;
1454 dentry = d_lookup(dir, name);
1455 out:
1456 return dentry;
1460 * d_validate - verify dentry provided from insecure source
1461 * @dentry: The dentry alleged to be valid child of @dparent
1462 * @dparent: The parent dentry (known to be valid)
1464 * An insecure source has sent us a dentry, here we verify it and dget() it.
1465 * This is used by ncpfs in its readdir implementation.
1466 * Zero is returned in the dentry is invalid.
1469 int d_validate(struct dentry *dentry, struct dentry *dparent)
1471 struct hlist_head *base;
1472 struct hlist_node *lhp;
1474 /* Check whether the ptr might be valid at all.. */
1475 if (!kmem_ptr_validate(dentry_cache, dentry))
1476 goto out;
1478 if (dentry->d_parent != dparent)
1479 goto out;
1481 spin_lock(&dcache_lock);
1482 base = d_hash(dparent, dentry->d_name.hash);
1483 hlist_for_each(lhp,base) {
1484 /* hlist_for_each_entry_rcu() not required for d_hash list
1485 * as it is parsed under dcache_lock
1487 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1488 __dget_locked(dentry);
1489 spin_unlock(&dcache_lock);
1490 return 1;
1493 spin_unlock(&dcache_lock);
1494 out:
1495 return 0;
1499 * When a file is deleted, we have two options:
1500 * - turn this dentry into a negative dentry
1501 * - unhash this dentry and free it.
1503 * Usually, we want to just turn this into
1504 * a negative dentry, but if anybody else is
1505 * currently using the dentry or the inode
1506 * we can't do that and we fall back on removing
1507 * it from the hash queues and waiting for
1508 * it to be deleted later when it has no users
1512 * d_delete - delete a dentry
1513 * @dentry: The dentry to delete
1515 * Turn the dentry into a negative dentry if possible, otherwise
1516 * remove it from the hash queues so it can be deleted later
1519 void d_delete(struct dentry * dentry)
1521 int isdir = 0;
1523 * Are we the only user?
1525 spin_lock(&dcache_lock);
1526 spin_lock(&dentry->d_lock);
1527 isdir = S_ISDIR(dentry->d_inode->i_mode);
1528 if (atomic_read(&dentry->d_count) == 1) {
1529 dentry_iput(dentry);
1530 fsnotify_nameremove(dentry, isdir);
1531 return;
1534 if (!d_unhashed(dentry))
1535 __d_drop(dentry);
1537 spin_unlock(&dentry->d_lock);
1538 spin_unlock(&dcache_lock);
1540 fsnotify_nameremove(dentry, isdir);
1543 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1546 entry->d_flags &= ~DCACHE_UNHASHED;
1547 hlist_add_head_rcu(&entry->d_hash, list);
1550 static void _d_rehash(struct dentry * entry)
1552 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1556 * d_rehash - add an entry back to the hash
1557 * @entry: dentry to add to the hash
1559 * Adds a dentry to the hash according to its name.
1562 void d_rehash(struct dentry * entry)
1564 spin_lock(&dcache_lock);
1565 spin_lock(&entry->d_lock);
1566 _d_rehash(entry);
1567 spin_unlock(&entry->d_lock);
1568 spin_unlock(&dcache_lock);
1572 * When switching names, the actual string doesn't strictly have to
1573 * be preserved in the target - because we're dropping the target
1574 * anyway. As such, we can just do a simple memcpy() to copy over
1575 * the new name before we switch.
1577 * Note that we have to be a lot more careful about getting the hash
1578 * switched - we have to switch the hash value properly even if it
1579 * then no longer matches the actual (corrupted) string of the target.
1580 * The hash value has to match the hash queue that the dentry is on..
1582 static void switch_names(struct dentry *dentry, struct dentry *target)
1584 if (dname_external(target)) {
1585 if (dname_external(dentry)) {
1587 * Both external: swap the pointers
1589 swap(target->d_name.name, dentry->d_name.name);
1590 } else {
1592 * dentry:internal, target:external. Steal target's
1593 * storage and make target internal.
1595 memcpy(target->d_iname, dentry->d_name.name,
1596 dentry->d_name.len + 1);
1597 dentry->d_name.name = target->d_name.name;
1598 target->d_name.name = target->d_iname;
1600 } else {
1601 if (dname_external(dentry)) {
1603 * dentry:external, target:internal. Give dentry's
1604 * storage to target and make dentry internal
1606 memcpy(dentry->d_iname, target->d_name.name,
1607 target->d_name.len + 1);
1608 target->d_name.name = dentry->d_name.name;
1609 dentry->d_name.name = dentry->d_iname;
1610 } else {
1612 * Both are internal. Just copy target to dentry
1614 memcpy(dentry->d_iname, target->d_name.name,
1615 target->d_name.len + 1);
1616 dentry->d_name.len = target->d_name.len;
1617 return;
1620 swap(dentry->d_name.len, target->d_name.len);
1624 * We cannibalize "target" when moving dentry on top of it,
1625 * because it's going to be thrown away anyway. We could be more
1626 * polite about it, though.
1628 * This forceful removal will result in ugly /proc output if
1629 * somebody holds a file open that got deleted due to a rename.
1630 * We could be nicer about the deleted file, and let it show
1631 * up under the name it had before it was deleted rather than
1632 * under the original name of the file that was moved on top of it.
1636 * d_move_locked - move a dentry
1637 * @dentry: entry to move
1638 * @target: new dentry
1640 * Update the dcache to reflect the move of a file name. Negative
1641 * dcache entries should not be moved in this way.
1643 static void d_move_locked(struct dentry * dentry, struct dentry * target)
1645 struct hlist_head *list;
1647 if (!dentry->d_inode)
1648 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1650 write_seqlock(&rename_lock);
1652 * XXXX: do we really need to take target->d_lock?
1654 if (target < dentry) {
1655 spin_lock(&target->d_lock);
1656 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1657 } else {
1658 spin_lock(&dentry->d_lock);
1659 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
1662 /* Move the dentry to the target hash queue, if on different bucket */
1663 if (d_unhashed(dentry))
1664 goto already_unhashed;
1666 hlist_del_rcu(&dentry->d_hash);
1668 already_unhashed:
1669 list = d_hash(target->d_parent, target->d_name.hash);
1670 __d_rehash(dentry, list);
1672 /* Unhash the target: dput() will then get rid of it */
1673 __d_drop(target);
1675 list_del(&dentry->d_u.d_child);
1676 list_del(&target->d_u.d_child);
1678 /* Switch the names.. */
1679 switch_names(dentry, target);
1680 swap(dentry->d_name.hash, target->d_name.hash);
1682 /* ... and switch the parents */
1683 if (IS_ROOT(dentry)) {
1684 dentry->d_parent = target->d_parent;
1685 target->d_parent = target;
1686 INIT_LIST_HEAD(&target->d_u.d_child);
1687 } else {
1688 swap(dentry->d_parent, target->d_parent);
1690 /* And add them back to the (new) parent lists */
1691 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1694 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1695 spin_unlock(&target->d_lock);
1696 fsnotify_d_move(dentry);
1697 spin_unlock(&dentry->d_lock);
1698 write_sequnlock(&rename_lock);
1702 * d_move - move a dentry
1703 * @dentry: entry to move
1704 * @target: new dentry
1706 * Update the dcache to reflect the move of a file name. Negative
1707 * dcache entries should not be moved in this way.
1710 void d_move(struct dentry * dentry, struct dentry * target)
1712 spin_lock(&dcache_lock);
1713 d_move_locked(dentry, target);
1714 spin_unlock(&dcache_lock);
1718 * d_ancestor - search for an ancestor
1719 * @p1: ancestor dentry
1720 * @p2: child dentry
1722 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1723 * an ancestor of p2, else NULL.
1725 struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
1727 struct dentry *p;
1729 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
1730 if (p->d_parent == p1)
1731 return p;
1733 return NULL;
1737 * This helper attempts to cope with remotely renamed directories
1739 * It assumes that the caller is already holding
1740 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1742 * Note: If ever the locking in lock_rename() changes, then please
1743 * remember to update this too...
1745 static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1746 __releases(dcache_lock)
1748 struct mutex *m1 = NULL, *m2 = NULL;
1749 struct dentry *ret;
1751 /* If alias and dentry share a parent, then no extra locks required */
1752 if (alias->d_parent == dentry->d_parent)
1753 goto out_unalias;
1755 /* Check for loops */
1756 ret = ERR_PTR(-ELOOP);
1757 if (d_ancestor(alias, dentry))
1758 goto out_err;
1760 /* See lock_rename() */
1761 ret = ERR_PTR(-EBUSY);
1762 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1763 goto out_err;
1764 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1765 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1766 goto out_err;
1767 m2 = &alias->d_parent->d_inode->i_mutex;
1768 out_unalias:
1769 d_move_locked(alias, dentry);
1770 ret = alias;
1771 out_err:
1772 spin_unlock(&dcache_lock);
1773 if (m2)
1774 mutex_unlock(m2);
1775 if (m1)
1776 mutex_unlock(m1);
1777 return ret;
1781 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1782 * named dentry in place of the dentry to be replaced.
1784 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1786 struct dentry *dparent, *aparent;
1788 switch_names(dentry, anon);
1789 swap(dentry->d_name.hash, anon->d_name.hash);
1791 dparent = dentry->d_parent;
1792 aparent = anon->d_parent;
1794 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1795 list_del(&dentry->d_u.d_child);
1796 if (!IS_ROOT(dentry))
1797 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1798 else
1799 INIT_LIST_HEAD(&dentry->d_u.d_child);
1801 anon->d_parent = (dparent == dentry) ? anon : dparent;
1802 list_del(&anon->d_u.d_child);
1803 if (!IS_ROOT(anon))
1804 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1805 else
1806 INIT_LIST_HEAD(&anon->d_u.d_child);
1808 anon->d_flags &= ~DCACHE_DISCONNECTED;
1812 * d_materialise_unique - introduce an inode into the tree
1813 * @dentry: candidate dentry
1814 * @inode: inode to bind to the dentry, to which aliases may be attached
1816 * Introduces an dentry into the tree, substituting an extant disconnected
1817 * root directory alias in its place if there is one
1819 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1821 struct dentry *actual;
1823 BUG_ON(!d_unhashed(dentry));
1825 spin_lock(&dcache_lock);
1827 if (!inode) {
1828 actual = dentry;
1829 __d_instantiate(dentry, NULL);
1830 goto found_lock;
1833 if (S_ISDIR(inode->i_mode)) {
1834 struct dentry *alias;
1836 /* Does an aliased dentry already exist? */
1837 alias = __d_find_alias(inode, 0);
1838 if (alias) {
1839 actual = alias;
1840 /* Is this an anonymous mountpoint that we could splice
1841 * into our tree? */
1842 if (IS_ROOT(alias)) {
1843 spin_lock(&alias->d_lock);
1844 __d_materialise_dentry(dentry, alias);
1845 __d_drop(alias);
1846 goto found;
1848 /* Nope, but we must(!) avoid directory aliasing */
1849 actual = __d_unalias(dentry, alias);
1850 if (IS_ERR(actual))
1851 dput(alias);
1852 goto out_nolock;
1856 /* Add a unique reference */
1857 actual = __d_instantiate_unique(dentry, inode);
1858 if (!actual)
1859 actual = dentry;
1860 else if (unlikely(!d_unhashed(actual)))
1861 goto shouldnt_be_hashed;
1863 found_lock:
1864 spin_lock(&actual->d_lock);
1865 found:
1866 _d_rehash(actual);
1867 spin_unlock(&actual->d_lock);
1868 spin_unlock(&dcache_lock);
1869 out_nolock:
1870 if (actual == dentry) {
1871 security_d_instantiate(dentry, inode);
1872 return NULL;
1875 iput(inode);
1876 return actual;
1878 shouldnt_be_hashed:
1879 spin_unlock(&dcache_lock);
1880 BUG();
1882 return NULL;
1885 static int prepend(char **buffer, int *buflen, const char *str, int namelen)
1887 *buflen -= namelen;
1888 if (*buflen < 0)
1889 return -ENAMETOOLONG;
1890 *buffer -= namelen;
1891 memcpy(*buffer, str, namelen);
1892 return 0;
1895 static int prepend_name(char **buffer, int *buflen, struct qstr *name)
1897 return prepend(buffer, buflen, name->name, name->len);
1901 * __d_path - return the path of a dentry
1902 * @path: the dentry/vfsmount to report
1903 * @root: root vfsmnt/dentry (may be modified by this function)
1904 * @buffer: buffer to return value in
1905 * @buflen: buffer length
1907 * Convert a dentry into an ASCII path name. If the entry has been deleted
1908 * the string " (deleted)" is appended. Note that this is ambiguous.
1910 * Returns a pointer into the buffer or an error code if the
1911 * path was too long.
1913 * "buflen" should be positive. Caller holds the dcache_lock.
1915 * If path is not reachable from the supplied root, then the value of
1916 * root is changed (without modifying refcounts).
1918 char *__d_path(const struct path *path, struct path *root,
1919 char *buffer, int buflen)
1921 struct dentry *dentry = path->dentry;
1922 struct vfsmount *vfsmnt = path->mnt;
1923 char *end = buffer + buflen;
1924 char *retval;
1926 spin_lock(&vfsmount_lock);
1927 prepend(&end, &buflen, "\0", 1);
1928 if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
1929 (prepend(&end, &buflen, " (deleted)", 10) != 0))
1930 goto Elong;
1932 if (buflen < 1)
1933 goto Elong;
1934 /* Get '/' right */
1935 retval = end-1;
1936 *retval = '/';
1938 for (;;) {
1939 struct dentry * parent;
1941 if (dentry == root->dentry && vfsmnt == root->mnt)
1942 break;
1943 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1944 /* Global root? */
1945 if (vfsmnt->mnt_parent == vfsmnt) {
1946 goto global_root;
1948 dentry = vfsmnt->mnt_mountpoint;
1949 vfsmnt = vfsmnt->mnt_parent;
1950 continue;
1952 parent = dentry->d_parent;
1953 prefetch(parent);
1954 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
1955 (prepend(&end, &buflen, "/", 1) != 0))
1956 goto Elong;
1957 retval = end;
1958 dentry = parent;
1961 out:
1962 spin_unlock(&vfsmount_lock);
1963 return retval;
1965 global_root:
1966 retval += 1; /* hit the slash */
1967 if (prepend_name(&retval, &buflen, &dentry->d_name) != 0)
1968 goto Elong;
1969 root->mnt = vfsmnt;
1970 root->dentry = dentry;
1971 goto out;
1973 Elong:
1974 retval = ERR_PTR(-ENAMETOOLONG);
1975 goto out;
1979 * d_path - return the path of a dentry
1980 * @path: path to report
1981 * @buf: buffer to return value in
1982 * @buflen: buffer length
1984 * Convert a dentry into an ASCII path name. If the entry has been deleted
1985 * the string " (deleted)" is appended. Note that this is ambiguous.
1987 * Returns a pointer into the buffer or an error code if the path was
1988 * too long. Note: Callers should use the returned pointer, not the passed
1989 * in buffer, to use the name! The implementation often starts at an offset
1990 * into the buffer, and may leave 0 bytes at the start.
1992 * "buflen" should be positive.
1994 char *d_path(const struct path *path, char *buf, int buflen)
1996 char *res;
1997 struct path root;
1998 struct path tmp;
2001 * We have various synthetic filesystems that never get mounted. On
2002 * these filesystems dentries are never used for lookup purposes, and
2003 * thus don't need to be hashed. They also don't need a name until a
2004 * user wants to identify the object in /proc/pid/fd/. The little hack
2005 * below allows us to generate a name for these objects on demand:
2007 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2008 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2010 read_lock(&current->fs->lock);
2011 root = current->fs->root;
2012 path_get(&root);
2013 read_unlock(&current->fs->lock);
2014 spin_lock(&dcache_lock);
2015 tmp = root;
2016 res = __d_path(path, &tmp, buf, buflen);
2017 spin_unlock(&dcache_lock);
2018 path_put(&root);
2019 return res;
2023 * Helper function for dentry_operations.d_dname() members
2025 char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2026 const char *fmt, ...)
2028 va_list args;
2029 char temp[64];
2030 int sz;
2032 va_start(args, fmt);
2033 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2034 va_end(args);
2036 if (sz > sizeof(temp) || sz > buflen)
2037 return ERR_PTR(-ENAMETOOLONG);
2039 buffer += buflen - sz;
2040 return memcpy(buffer, temp, sz);
2044 * Write full pathname from the root of the filesystem into the buffer.
2046 char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2048 char *end = buf + buflen;
2049 char *retval;
2051 spin_lock(&dcache_lock);
2052 prepend(&end, &buflen, "\0", 1);
2053 if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
2054 (prepend(&end, &buflen, "//deleted", 9) != 0))
2055 goto Elong;
2056 if (buflen < 1)
2057 goto Elong;
2058 /* Get '/' right */
2059 retval = end-1;
2060 *retval = '/';
2062 while (!IS_ROOT(dentry)) {
2063 struct dentry *parent = dentry->d_parent;
2065 prefetch(parent);
2066 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
2067 (prepend(&end, &buflen, "/", 1) != 0))
2068 goto Elong;
2070 retval = end;
2071 dentry = parent;
2073 spin_unlock(&dcache_lock);
2074 return retval;
2075 Elong:
2076 spin_unlock(&dcache_lock);
2077 return ERR_PTR(-ENAMETOOLONG);
2081 * NOTE! The user-level library version returns a
2082 * character pointer. The kernel system call just
2083 * returns the length of the buffer filled (which
2084 * includes the ending '\0' character), or a negative
2085 * error value. So libc would do something like
2087 * char *getcwd(char * buf, size_t size)
2089 * int retval;
2091 * retval = sys_getcwd(buf, size);
2092 * if (retval >= 0)
2093 * return buf;
2094 * errno = -retval;
2095 * return NULL;
2098 SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
2100 int error;
2101 struct path pwd, root;
2102 char *page = (char *) __get_free_page(GFP_USER);
2104 if (!page)
2105 return -ENOMEM;
2107 read_lock(&current->fs->lock);
2108 pwd = current->fs->pwd;
2109 path_get(&pwd);
2110 root = current->fs->root;
2111 path_get(&root);
2112 read_unlock(&current->fs->lock);
2114 error = -ENOENT;
2115 /* Has the current directory has been unlinked? */
2116 spin_lock(&dcache_lock);
2117 if (IS_ROOT(pwd.dentry) || !d_unhashed(pwd.dentry)) {
2118 unsigned long len;
2119 struct path tmp = root;
2120 char * cwd;
2122 cwd = __d_path(&pwd, &tmp, page, PAGE_SIZE);
2123 spin_unlock(&dcache_lock);
2125 error = PTR_ERR(cwd);
2126 if (IS_ERR(cwd))
2127 goto out;
2129 error = -ERANGE;
2130 len = PAGE_SIZE + page - cwd;
2131 if (len <= size) {
2132 error = len;
2133 if (copy_to_user(buf, cwd, len))
2134 error = -EFAULT;
2136 } else
2137 spin_unlock(&dcache_lock);
2139 out:
2140 path_put(&pwd);
2141 path_put(&root);
2142 free_page((unsigned long) page);
2143 return error;
2147 * Test whether new_dentry is a subdirectory of old_dentry.
2149 * Trivially implemented using the dcache structure
2153 * is_subdir - is new dentry a subdirectory of old_dentry
2154 * @new_dentry: new dentry
2155 * @old_dentry: old dentry
2157 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2158 * Returns 0 otherwise.
2159 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2162 int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
2164 int result;
2165 unsigned long seq;
2167 /* FIXME: This is old behavior, needed? Please check callers. */
2168 if (new_dentry == old_dentry)
2169 return 1;
2172 * Need rcu_readlock to protect against the d_parent trashing
2173 * due to d_move
2175 rcu_read_lock();
2176 do {
2177 /* for restarting inner loop in case of seq retry */
2178 seq = read_seqbegin(&rename_lock);
2179 if (d_ancestor(old_dentry, new_dentry))
2180 result = 1;
2181 else
2182 result = 0;
2183 } while (read_seqretry(&rename_lock, seq));
2184 rcu_read_unlock();
2186 return result;
2189 void d_genocide(struct dentry *root)
2191 struct dentry *this_parent = root;
2192 struct list_head *next;
2194 spin_lock(&dcache_lock);
2195 repeat:
2196 next = this_parent->d_subdirs.next;
2197 resume:
2198 while (next != &this_parent->d_subdirs) {
2199 struct list_head *tmp = next;
2200 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
2201 next = tmp->next;
2202 if (d_unhashed(dentry)||!dentry->d_inode)
2203 continue;
2204 if (!list_empty(&dentry->d_subdirs)) {
2205 this_parent = dentry;
2206 goto repeat;
2208 atomic_dec(&dentry->d_count);
2210 if (this_parent != root) {
2211 next = this_parent->d_u.d_child.next;
2212 atomic_dec(&this_parent->d_count);
2213 this_parent = this_parent->d_parent;
2214 goto resume;
2216 spin_unlock(&dcache_lock);
2220 * find_inode_number - check for dentry with name
2221 * @dir: directory to check
2222 * @name: Name to find.
2224 * Check whether a dentry already exists for the given name,
2225 * and return the inode number if it has an inode. Otherwise
2226 * 0 is returned.
2228 * This routine is used to post-process directory listings for
2229 * filesystems using synthetic inode numbers, and is necessary
2230 * to keep getcwd() working.
2233 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2235 struct dentry * dentry;
2236 ino_t ino = 0;
2238 dentry = d_hash_and_lookup(dir, name);
2239 if (dentry) {
2240 if (dentry->d_inode)
2241 ino = dentry->d_inode->i_ino;
2242 dput(dentry);
2244 return ino;
2247 static __initdata unsigned long dhash_entries;
2248 static int __init set_dhash_entries(char *str)
2250 if (!str)
2251 return 0;
2252 dhash_entries = simple_strtoul(str, &str, 0);
2253 return 1;
2255 __setup("dhash_entries=", set_dhash_entries);
2257 static void __init dcache_init_early(void)
2259 int loop;
2261 /* If hashes are distributed across NUMA nodes, defer
2262 * hash allocation until vmalloc space is available.
2264 if (hashdist)
2265 return;
2267 dentry_hashtable =
2268 alloc_large_system_hash("Dentry cache",
2269 sizeof(struct hlist_head),
2270 dhash_entries,
2272 HASH_EARLY,
2273 &d_hash_shift,
2274 &d_hash_mask,
2277 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2278 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2281 static void __init dcache_init(void)
2283 int loop;
2286 * A constructor could be added for stable state like the lists,
2287 * but it is probably not worth it because of the cache nature
2288 * of the dcache.
2290 dentry_cache = KMEM_CACHE(dentry,
2291 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
2293 register_shrinker(&dcache_shrinker);
2295 /* Hash may have been set up in dcache_init_early */
2296 if (!hashdist)
2297 return;
2299 dentry_hashtable =
2300 alloc_large_system_hash("Dentry cache",
2301 sizeof(struct hlist_head),
2302 dhash_entries,
2305 &d_hash_shift,
2306 &d_hash_mask,
2309 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2310 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2313 /* SLAB cache for __getname() consumers */
2314 struct kmem_cache *names_cachep __read_mostly;
2316 EXPORT_SYMBOL(d_genocide);
2318 void __init vfs_caches_init_early(void)
2320 dcache_init_early();
2321 inode_init_early();
2324 void __init vfs_caches_init(unsigned long mempages)
2326 unsigned long reserve;
2328 /* Base hash sizes on available memory, with a reserve equal to
2329 150% of current kernel size */
2331 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2332 mempages -= reserve;
2334 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2335 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2337 dcache_init();
2338 inode_init();
2339 files_init(mempages);
2340 mnt_init();
2341 bdev_cache_init();
2342 chrdev_init();
2345 EXPORT_SYMBOL(d_alloc);
2346 EXPORT_SYMBOL(d_alloc_root);
2347 EXPORT_SYMBOL(d_delete);
2348 EXPORT_SYMBOL(d_find_alias);
2349 EXPORT_SYMBOL(d_instantiate);
2350 EXPORT_SYMBOL(d_invalidate);
2351 EXPORT_SYMBOL(d_lookup);
2352 EXPORT_SYMBOL(d_move);
2353 EXPORT_SYMBOL_GPL(d_materialise_unique);
2354 EXPORT_SYMBOL(d_path);
2355 EXPORT_SYMBOL(d_prune_aliases);
2356 EXPORT_SYMBOL(d_rehash);
2357 EXPORT_SYMBOL(d_splice_alias);
2358 EXPORT_SYMBOL(d_add_ci);
2359 EXPORT_SYMBOL(d_validate);
2360 EXPORT_SYMBOL(dget_locked);
2361 EXPORT_SYMBOL(dput);
2362 EXPORT_SYMBOL(find_inode_number);
2363 EXPORT_SYMBOL(have_submounts);
2364 EXPORT_SYMBOL(names_cachep);
2365 EXPORT_SYMBOL(shrink_dcache_parent);
2366 EXPORT_SYMBOL(shrink_dcache_sb);