PRCM: 34XX: Fix wrong shift value used in dpll4_m4x2_ck enable bit
[linux-ginger.git] / fs / dcache.c
blob3ee588d5f585b54d7aa50358648521b64423b3f8
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/fs.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 "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;
63 static LIST_HEAD(dentry_unused);
65 /* Statistics gathering. */
66 struct dentry_stat_t dentry_stat = {
67 .age_limit = 45,
70 static void __d_free(struct dentry *dentry)
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);
98 static void dentry_lru_remove(struct dentry *dentry)
100 if (!list_empty(&dentry->d_lru)) {
101 list_del_init(&dentry->d_lru);
102 dentry_stat.nr_unused--;
107 * Release the dentry's inode, using the filesystem
108 * d_iput() operation if defined.
109 * Called with dcache_lock and per dentry lock held, drops both.
111 static void dentry_iput(struct dentry * dentry)
113 struct inode *inode = dentry->d_inode;
114 if (inode) {
115 dentry->d_inode = NULL;
116 list_del_init(&dentry->d_alias);
117 spin_unlock(&dentry->d_lock);
118 spin_unlock(&dcache_lock);
119 if (!inode->i_nlink)
120 fsnotify_inoderemove(inode);
121 if (dentry->d_op && dentry->d_op->d_iput)
122 dentry->d_op->d_iput(dentry, inode);
123 else
124 iput(inode);
125 } else {
126 spin_unlock(&dentry->d_lock);
127 spin_unlock(&dcache_lock);
132 * d_kill - kill dentry and return parent
133 * @dentry: dentry to kill
135 * Called with dcache_lock and d_lock, releases both. The dentry must
136 * already be unhashed and removed from the LRU.
138 * If this is the root of the dentry tree, return NULL.
140 static struct dentry *d_kill(struct dentry *dentry)
142 struct dentry *parent;
144 list_del(&dentry->d_u.d_child);
145 dentry_stat.nr_dentry--; /* For d_free, below */
146 /*drops the locks, at that point nobody can reach this dentry */
147 dentry_iput(dentry);
148 parent = dentry->d_parent;
149 d_free(dentry);
150 return dentry == parent ? NULL : parent;
154 * This is dput
156 * This is complicated by the fact that we do not want to put
157 * dentries that are no longer on any hash chain on the unused
158 * list: we'd much rather just get rid of them immediately.
160 * However, that implies that we have to traverse the dentry
161 * tree upwards to the parents which might _also_ now be
162 * scheduled for deletion (it may have been only waiting for
163 * its last child to go away).
165 * This tail recursion is done by hand as we don't want to depend
166 * on the compiler to always get this right (gcc generally doesn't).
167 * Real recursion would eat up our stack space.
171 * dput - release a dentry
172 * @dentry: dentry to release
174 * Release a dentry. This will drop the usage count and if appropriate
175 * call the dentry unlink method as well as removing it from the queues and
176 * releasing its resources. If the parent dentries were scheduled for release
177 * they too may now get deleted.
179 * no dcache lock, please.
182 void dput(struct dentry *dentry)
184 if (!dentry)
185 return;
187 repeat:
188 if (atomic_read(&dentry->d_count) == 1)
189 might_sleep();
190 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
191 return;
193 spin_lock(&dentry->d_lock);
194 if (atomic_read(&dentry->d_count)) {
195 spin_unlock(&dentry->d_lock);
196 spin_unlock(&dcache_lock);
197 return;
201 * AV: ->d_delete() is _NOT_ allowed to block now.
203 if (dentry->d_op && dentry->d_op->d_delete) {
204 if (dentry->d_op->d_delete(dentry))
205 goto unhash_it;
207 /* Unreachable? Get rid of it */
208 if (d_unhashed(dentry))
209 goto kill_it;
210 if (list_empty(&dentry->d_lru)) {
211 dentry->d_flags |= DCACHE_REFERENCED;
212 list_add(&dentry->d_lru, &dentry_unused);
213 dentry_stat.nr_unused++;
215 spin_unlock(&dentry->d_lock);
216 spin_unlock(&dcache_lock);
217 return;
219 unhash_it:
220 __d_drop(dentry);
221 kill_it:
222 dentry_lru_remove(dentry);
223 dentry = d_kill(dentry);
224 if (dentry)
225 goto repeat;
229 * d_invalidate - invalidate a dentry
230 * @dentry: dentry to invalidate
232 * Try to invalidate the dentry if it turns out to be
233 * possible. If there are other dentries that can be
234 * reached through this one we can't delete it and we
235 * return -EBUSY. On success we return 0.
237 * no dcache lock.
240 int d_invalidate(struct dentry * dentry)
243 * If it's already been dropped, return OK.
245 spin_lock(&dcache_lock);
246 if (d_unhashed(dentry)) {
247 spin_unlock(&dcache_lock);
248 return 0;
251 * Check whether to do a partial shrink_dcache
252 * to get rid of unused child entries.
254 if (!list_empty(&dentry->d_subdirs)) {
255 spin_unlock(&dcache_lock);
256 shrink_dcache_parent(dentry);
257 spin_lock(&dcache_lock);
261 * Somebody else still using it?
263 * If it's a directory, we can't drop it
264 * for fear of somebody re-populating it
265 * with children (even though dropping it
266 * would make it unreachable from the root,
267 * we might still populate it if it was a
268 * working directory or similar).
270 spin_lock(&dentry->d_lock);
271 if (atomic_read(&dentry->d_count) > 1) {
272 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
273 spin_unlock(&dentry->d_lock);
274 spin_unlock(&dcache_lock);
275 return -EBUSY;
279 __d_drop(dentry);
280 spin_unlock(&dentry->d_lock);
281 spin_unlock(&dcache_lock);
282 return 0;
285 /* This should be called _only_ with dcache_lock held */
287 static inline struct dentry * __dget_locked(struct dentry *dentry)
289 atomic_inc(&dentry->d_count);
290 dentry_lru_remove(dentry);
291 return dentry;
294 struct dentry * dget_locked(struct dentry *dentry)
296 return __dget_locked(dentry);
300 * d_find_alias - grab a hashed alias of inode
301 * @inode: inode in question
302 * @want_discon: flag, used by d_splice_alias, to request
303 * that only a DISCONNECTED alias be returned.
305 * If inode has a hashed alias, or is a directory and has any alias,
306 * acquire the reference to alias and return it. Otherwise return NULL.
307 * Notice that if inode is a directory there can be only one alias and
308 * it can be unhashed only if it has no children, or if it is the root
309 * of a filesystem.
311 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
312 * any other hashed alias over that one unless @want_discon is set,
313 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
316 static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
318 struct list_head *head, *next, *tmp;
319 struct dentry *alias, *discon_alias=NULL;
321 head = &inode->i_dentry;
322 next = inode->i_dentry.next;
323 while (next != head) {
324 tmp = next;
325 next = tmp->next;
326 prefetch(next);
327 alias = list_entry(tmp, struct dentry, d_alias);
328 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
329 if (IS_ROOT(alias) &&
330 (alias->d_flags & DCACHE_DISCONNECTED))
331 discon_alias = alias;
332 else if (!want_discon) {
333 __dget_locked(alias);
334 return alias;
338 if (discon_alias)
339 __dget_locked(discon_alias);
340 return discon_alias;
343 struct dentry * d_find_alias(struct inode *inode)
345 struct dentry *de = NULL;
347 if (!list_empty(&inode->i_dentry)) {
348 spin_lock(&dcache_lock);
349 de = __d_find_alias(inode, 0);
350 spin_unlock(&dcache_lock);
352 return de;
356 * Try to kill dentries associated with this inode.
357 * WARNING: you must own a reference to inode.
359 void d_prune_aliases(struct inode *inode)
361 struct dentry *dentry;
362 restart:
363 spin_lock(&dcache_lock);
364 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
365 spin_lock(&dentry->d_lock);
366 if (!atomic_read(&dentry->d_count)) {
367 __dget_locked(dentry);
368 __d_drop(dentry);
369 spin_unlock(&dentry->d_lock);
370 spin_unlock(&dcache_lock);
371 dput(dentry);
372 goto restart;
374 spin_unlock(&dentry->d_lock);
376 spin_unlock(&dcache_lock);
380 * Throw away a dentry - free the inode, dput the parent. This requires that
381 * the LRU list has already been removed.
383 * Try to prune ancestors as well. This is necessary to prevent
384 * quadratic behavior of shrink_dcache_parent(), but is also expected
385 * to be beneficial in reducing dentry cache fragmentation.
387 * Called with dcache_lock, drops it and then regains.
388 * Called with dentry->d_lock held, drops it.
390 static void prune_one_dentry(struct dentry * dentry)
392 __d_drop(dentry);
393 dentry = d_kill(dentry);
396 * Prune ancestors. Locking is simpler than in dput(),
397 * because dcache_lock needs to be taken anyway.
399 spin_lock(&dcache_lock);
400 while (dentry) {
401 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
402 return;
404 if (dentry->d_op && dentry->d_op->d_delete)
405 dentry->d_op->d_delete(dentry);
406 dentry_lru_remove(dentry);
407 __d_drop(dentry);
408 dentry = d_kill(dentry);
409 spin_lock(&dcache_lock);
414 * prune_dcache - shrink the dcache
415 * @count: number of entries to try and free
416 * @sb: if given, ignore dentries for other superblocks
417 * which are being unmounted.
419 * Shrink the dcache. This is done when we need
420 * more memory, or simply when we need to unmount
421 * something (at which point we need to unuse
422 * all dentries).
424 * This function may fail to free any resources if
425 * all the dentries are in use.
428 static void prune_dcache(int count, struct super_block *sb)
430 spin_lock(&dcache_lock);
431 for (; count ; count--) {
432 struct dentry *dentry;
433 struct list_head *tmp;
434 struct rw_semaphore *s_umount;
436 cond_resched_lock(&dcache_lock);
438 tmp = dentry_unused.prev;
439 if (sb) {
440 /* Try to find a dentry for this sb, but don't try
441 * too hard, if they aren't near the tail they will
442 * be moved down again soon
444 int skip = count;
445 while (skip && tmp != &dentry_unused &&
446 list_entry(tmp, struct dentry, d_lru)->d_sb != sb) {
447 skip--;
448 tmp = tmp->prev;
451 if (tmp == &dentry_unused)
452 break;
453 list_del_init(tmp);
454 prefetch(dentry_unused.prev);
455 dentry_stat.nr_unused--;
456 dentry = list_entry(tmp, struct dentry, d_lru);
458 spin_lock(&dentry->d_lock);
460 * We found an inuse dentry which was not removed from
461 * dentry_unused because of laziness during lookup. Do not free
462 * it - just keep it off the dentry_unused list.
464 if (atomic_read(&dentry->d_count)) {
465 spin_unlock(&dentry->d_lock);
466 continue;
468 /* If the dentry was recently referenced, don't free it. */
469 if (dentry->d_flags & DCACHE_REFERENCED) {
470 dentry->d_flags &= ~DCACHE_REFERENCED;
471 list_add(&dentry->d_lru, &dentry_unused);
472 dentry_stat.nr_unused++;
473 spin_unlock(&dentry->d_lock);
474 continue;
477 * If the dentry is not DCACHED_REFERENCED, it is time
478 * to remove it from the dcache, provided the super block is
479 * NULL (which means we are trying to reclaim memory)
480 * or this dentry belongs to the same super block that
481 * we want to shrink.
484 * If this dentry is for "my" filesystem, then I can prune it
485 * without taking the s_umount lock (I already hold it).
487 if (sb && dentry->d_sb == sb) {
488 prune_one_dentry(dentry);
489 continue;
492 * ...otherwise we need to be sure this filesystem isn't being
493 * unmounted, otherwise we could race with
494 * generic_shutdown_super(), and end up holding a reference to
495 * an inode while the filesystem is unmounted.
496 * So we try to get s_umount, and make sure s_root isn't NULL.
497 * (Take a local copy of s_umount to avoid a use-after-free of
498 * `dentry').
500 s_umount = &dentry->d_sb->s_umount;
501 if (down_read_trylock(s_umount)) {
502 if (dentry->d_sb->s_root != NULL) {
503 prune_one_dentry(dentry);
504 up_read(s_umount);
505 continue;
507 up_read(s_umount);
509 spin_unlock(&dentry->d_lock);
511 * Insert dentry at the head of the list as inserting at the
512 * tail leads to a cycle.
514 list_add(&dentry->d_lru, &dentry_unused);
515 dentry_stat.nr_unused++;
517 spin_unlock(&dcache_lock);
521 * Shrink the dcache for the specified super block.
522 * This allows us to unmount a device without disturbing
523 * the dcache for the other devices.
525 * This implementation makes just two traversals of the
526 * unused list. On the first pass we move the selected
527 * dentries to the most recent end, and on the second
528 * pass we free them. The second pass must restart after
529 * each dput(), but since the target dentries are all at
530 * the end, it's really just a single traversal.
534 * shrink_dcache_sb - shrink dcache for a superblock
535 * @sb: superblock
537 * Shrink the dcache for the specified super block. This
538 * is used to free the dcache before unmounting a file
539 * system
542 void shrink_dcache_sb(struct super_block * sb)
544 struct list_head *tmp, *next;
545 struct dentry *dentry;
548 * Pass one ... move the dentries for the specified
549 * superblock to the most recent end of the unused list.
551 spin_lock(&dcache_lock);
552 list_for_each_prev_safe(tmp, next, &dentry_unused) {
553 dentry = list_entry(tmp, struct dentry, d_lru);
554 if (dentry->d_sb != sb)
555 continue;
556 list_move_tail(tmp, &dentry_unused);
560 * Pass two ... free the dentries for this superblock.
562 repeat:
563 list_for_each_prev_safe(tmp, next, &dentry_unused) {
564 dentry = list_entry(tmp, struct dentry, d_lru);
565 if (dentry->d_sb != sb)
566 continue;
567 dentry_stat.nr_unused--;
568 list_del_init(tmp);
569 spin_lock(&dentry->d_lock);
570 if (atomic_read(&dentry->d_count)) {
571 spin_unlock(&dentry->d_lock);
572 continue;
574 prune_one_dentry(dentry);
575 cond_resched_lock(&dcache_lock);
576 goto repeat;
578 spin_unlock(&dcache_lock);
582 * destroy a single subtree of dentries for unmount
583 * - see the comments on shrink_dcache_for_umount() for a description of the
584 * locking
586 static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
588 struct dentry *parent;
589 unsigned detached = 0;
591 BUG_ON(!IS_ROOT(dentry));
593 /* detach this root from the system */
594 spin_lock(&dcache_lock);
595 dentry_lru_remove(dentry);
596 __d_drop(dentry);
597 spin_unlock(&dcache_lock);
599 for (;;) {
600 /* descend to the first leaf in the current subtree */
601 while (!list_empty(&dentry->d_subdirs)) {
602 struct dentry *loop;
604 /* this is a branch with children - detach all of them
605 * from the system in one go */
606 spin_lock(&dcache_lock);
607 list_for_each_entry(loop, &dentry->d_subdirs,
608 d_u.d_child) {
609 dentry_lru_remove(loop);
610 __d_drop(loop);
611 cond_resched_lock(&dcache_lock);
613 spin_unlock(&dcache_lock);
615 /* move to the first child */
616 dentry = list_entry(dentry->d_subdirs.next,
617 struct dentry, d_u.d_child);
620 /* consume the dentries from this leaf up through its parents
621 * until we find one with children or run out altogether */
622 do {
623 struct inode *inode;
625 if (atomic_read(&dentry->d_count) != 0) {
626 printk(KERN_ERR
627 "BUG: Dentry %p{i=%lx,n=%s}"
628 " still in use (%d)"
629 " [unmount of %s %s]\n",
630 dentry,
631 dentry->d_inode ?
632 dentry->d_inode->i_ino : 0UL,
633 dentry->d_name.name,
634 atomic_read(&dentry->d_count),
635 dentry->d_sb->s_type->name,
636 dentry->d_sb->s_id);
637 BUG();
640 parent = dentry->d_parent;
641 if (parent == dentry)
642 parent = NULL;
643 else
644 atomic_dec(&parent->d_count);
646 list_del(&dentry->d_u.d_child);
647 detached++;
649 inode = dentry->d_inode;
650 if (inode) {
651 dentry->d_inode = NULL;
652 list_del_init(&dentry->d_alias);
653 if (dentry->d_op && dentry->d_op->d_iput)
654 dentry->d_op->d_iput(dentry, inode);
655 else
656 iput(inode);
659 d_free(dentry);
661 /* finished when we fall off the top of the tree,
662 * otherwise we ascend to the parent and move to the
663 * next sibling if there is one */
664 if (!parent)
665 goto out;
667 dentry = parent;
669 } while (list_empty(&dentry->d_subdirs));
671 dentry = list_entry(dentry->d_subdirs.next,
672 struct dentry, d_u.d_child);
674 out:
675 /* several dentries were freed, need to correct nr_dentry */
676 spin_lock(&dcache_lock);
677 dentry_stat.nr_dentry -= detached;
678 spin_unlock(&dcache_lock);
682 * destroy the dentries attached to a superblock on unmounting
683 * - we don't need to use dentry->d_lock, and only need dcache_lock when
684 * removing the dentry from the system lists and hashes because:
685 * - the superblock is detached from all mountings and open files, so the
686 * dentry trees will not be rearranged by the VFS
687 * - s_umount is write-locked, so the memory pressure shrinker will ignore
688 * any dentries belonging to this superblock that it comes across
689 * - the filesystem itself is no longer permitted to rearrange the dentries
690 * in this superblock
692 void shrink_dcache_for_umount(struct super_block *sb)
694 struct dentry *dentry;
696 if (down_read_trylock(&sb->s_umount))
697 BUG();
699 dentry = sb->s_root;
700 sb->s_root = NULL;
701 atomic_dec(&dentry->d_count);
702 shrink_dcache_for_umount_subtree(dentry);
704 while (!hlist_empty(&sb->s_anon)) {
705 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
706 shrink_dcache_for_umount_subtree(dentry);
711 * Search for at least 1 mount point in the dentry's subdirs.
712 * We descend to the next level whenever the d_subdirs
713 * list is non-empty and continue searching.
717 * have_submounts - check for mounts over a dentry
718 * @parent: dentry to check.
720 * Return true if the parent or its subdirectories contain
721 * a mount point
724 int have_submounts(struct dentry *parent)
726 struct dentry *this_parent = parent;
727 struct list_head *next;
729 spin_lock(&dcache_lock);
730 if (d_mountpoint(parent))
731 goto positive;
732 repeat:
733 next = this_parent->d_subdirs.next;
734 resume:
735 while (next != &this_parent->d_subdirs) {
736 struct list_head *tmp = next;
737 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
738 next = tmp->next;
739 /* Have we found a mount point ? */
740 if (d_mountpoint(dentry))
741 goto positive;
742 if (!list_empty(&dentry->d_subdirs)) {
743 this_parent = dentry;
744 goto repeat;
748 * All done at this level ... ascend and resume the search.
750 if (this_parent != parent) {
751 next = this_parent->d_u.d_child.next;
752 this_parent = this_parent->d_parent;
753 goto resume;
755 spin_unlock(&dcache_lock);
756 return 0; /* No mount points found in tree */
757 positive:
758 spin_unlock(&dcache_lock);
759 return 1;
763 * Search the dentry child list for the specified parent,
764 * and move any unused dentries to the end of the unused
765 * list for prune_dcache(). We descend to the next level
766 * whenever the d_subdirs list is non-empty and continue
767 * searching.
769 * It returns zero iff there are no unused children,
770 * otherwise it returns the number of children moved to
771 * the end of the unused list. This may not be the total
772 * number of unused children, because select_parent can
773 * drop the lock and return early due to latency
774 * constraints.
776 static int select_parent(struct dentry * parent)
778 struct dentry *this_parent = parent;
779 struct list_head *next;
780 int found = 0;
782 spin_lock(&dcache_lock);
783 repeat:
784 next = this_parent->d_subdirs.next;
785 resume:
786 while (next != &this_parent->d_subdirs) {
787 struct list_head *tmp = next;
788 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
789 next = tmp->next;
791 dentry_lru_remove(dentry);
793 * move only zero ref count dentries to the end
794 * of the unused list for prune_dcache
796 if (!atomic_read(&dentry->d_count)) {
797 list_add_tail(&dentry->d_lru, &dentry_unused);
798 dentry_stat.nr_unused++;
799 found++;
803 * We can return to the caller if we have found some (this
804 * ensures forward progress). We'll be coming back to find
805 * the rest.
807 if (found && need_resched())
808 goto out;
811 * Descend a level if the d_subdirs list is non-empty.
813 if (!list_empty(&dentry->d_subdirs)) {
814 this_parent = dentry;
815 goto repeat;
819 * All done at this level ... ascend and resume the search.
821 if (this_parent != parent) {
822 next = this_parent->d_u.d_child.next;
823 this_parent = this_parent->d_parent;
824 goto resume;
826 out:
827 spin_unlock(&dcache_lock);
828 return found;
832 * shrink_dcache_parent - prune dcache
833 * @parent: parent of entries to prune
835 * Prune the dcache to remove unused children of the parent dentry.
838 void shrink_dcache_parent(struct dentry * parent)
840 int found;
842 while ((found = select_parent(parent)) != 0)
843 prune_dcache(found, parent->d_sb);
847 * Scan `nr' dentries and return the number which remain.
849 * We need to avoid reentering the filesystem if the caller is performing a
850 * GFP_NOFS allocation attempt. One example deadlock is:
852 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
853 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
854 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
856 * In this case we return -1 to tell the caller that we baled.
858 static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
860 if (nr) {
861 if (!(gfp_mask & __GFP_FS))
862 return -1;
863 prune_dcache(nr, NULL);
865 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
868 static struct shrinker dcache_shrinker = {
869 .shrink = shrink_dcache_memory,
870 .seeks = DEFAULT_SEEKS,
874 * d_alloc - allocate a dcache entry
875 * @parent: parent of entry to allocate
876 * @name: qstr of the name
878 * Allocates a dentry. It returns %NULL if there is insufficient memory
879 * available. On a success the dentry is returned. The name passed in is
880 * copied and the copy passed in may be reused after this call.
883 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
885 struct dentry *dentry;
886 char *dname;
888 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
889 if (!dentry)
890 return NULL;
892 if (name->len > DNAME_INLINE_LEN-1) {
893 dname = kmalloc(name->len + 1, GFP_KERNEL);
894 if (!dname) {
895 kmem_cache_free(dentry_cache, dentry);
896 return NULL;
898 } else {
899 dname = dentry->d_iname;
901 dentry->d_name.name = dname;
903 dentry->d_name.len = name->len;
904 dentry->d_name.hash = name->hash;
905 memcpy(dname, name->name, name->len);
906 dname[name->len] = 0;
908 atomic_set(&dentry->d_count, 1);
909 dentry->d_flags = DCACHE_UNHASHED;
910 spin_lock_init(&dentry->d_lock);
911 dentry->d_inode = NULL;
912 dentry->d_parent = NULL;
913 dentry->d_sb = NULL;
914 dentry->d_op = NULL;
915 dentry->d_fsdata = NULL;
916 dentry->d_mounted = 0;
917 #ifdef CONFIG_PROFILING
918 dentry->d_cookie = NULL;
919 #endif
920 INIT_HLIST_NODE(&dentry->d_hash);
921 INIT_LIST_HEAD(&dentry->d_lru);
922 INIT_LIST_HEAD(&dentry->d_subdirs);
923 INIT_LIST_HEAD(&dentry->d_alias);
925 if (parent) {
926 dentry->d_parent = dget(parent);
927 dentry->d_sb = parent->d_sb;
928 } else {
929 INIT_LIST_HEAD(&dentry->d_u.d_child);
932 spin_lock(&dcache_lock);
933 if (parent)
934 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
935 dentry_stat.nr_dentry++;
936 spin_unlock(&dcache_lock);
938 return dentry;
941 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
943 struct qstr q;
945 q.name = name;
946 q.len = strlen(name);
947 q.hash = full_name_hash(q.name, q.len);
948 return d_alloc(parent, &q);
952 * d_instantiate - fill in inode information for a dentry
953 * @entry: dentry to complete
954 * @inode: inode to attach to this dentry
956 * Fill in inode information in the entry.
958 * This turns negative dentries into productive full members
959 * of society.
961 * NOTE! This assumes that the inode count has been incremented
962 * (or otherwise set) by the caller to indicate that it is now
963 * in use by the dcache.
966 void d_instantiate(struct dentry *entry, struct inode * inode)
968 BUG_ON(!list_empty(&entry->d_alias));
969 spin_lock(&dcache_lock);
970 if (inode)
971 list_add(&entry->d_alias, &inode->i_dentry);
972 entry->d_inode = inode;
973 fsnotify_d_instantiate(entry, inode);
974 spin_unlock(&dcache_lock);
975 security_d_instantiate(entry, inode);
979 * d_instantiate_unique - instantiate a non-aliased dentry
980 * @entry: dentry to instantiate
981 * @inode: inode to attach to this dentry
983 * Fill in inode information in the entry. On success, it returns NULL.
984 * If an unhashed alias of "entry" already exists, then we return the
985 * aliased dentry instead and drop one reference to inode.
987 * Note that in order to avoid conflicts with rename() etc, the caller
988 * had better be holding the parent directory semaphore.
990 * This also assumes that the inode count has been incremented
991 * (or otherwise set) by the caller to indicate that it is now
992 * in use by the dcache.
994 static struct dentry *__d_instantiate_unique(struct dentry *entry,
995 struct inode *inode)
997 struct dentry *alias;
998 int len = entry->d_name.len;
999 const char *name = entry->d_name.name;
1000 unsigned int hash = entry->d_name.hash;
1002 if (!inode) {
1003 entry->d_inode = NULL;
1004 return NULL;
1007 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1008 struct qstr *qstr = &alias->d_name;
1010 if (qstr->hash != hash)
1011 continue;
1012 if (alias->d_parent != entry->d_parent)
1013 continue;
1014 if (qstr->len != len)
1015 continue;
1016 if (memcmp(qstr->name, name, len))
1017 continue;
1018 dget_locked(alias);
1019 return alias;
1022 list_add(&entry->d_alias, &inode->i_dentry);
1023 entry->d_inode = inode;
1024 fsnotify_d_instantiate(entry, inode);
1025 return NULL;
1028 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1030 struct dentry *result;
1032 BUG_ON(!list_empty(&entry->d_alias));
1034 spin_lock(&dcache_lock);
1035 result = __d_instantiate_unique(entry, inode);
1036 spin_unlock(&dcache_lock);
1038 if (!result) {
1039 security_d_instantiate(entry, inode);
1040 return NULL;
1043 BUG_ON(!d_unhashed(result));
1044 iput(inode);
1045 return result;
1048 EXPORT_SYMBOL(d_instantiate_unique);
1051 * d_alloc_root - allocate root dentry
1052 * @root_inode: inode to allocate the root for
1054 * Allocate a root ("/") dentry for the inode given. The inode is
1055 * instantiated and returned. %NULL is returned if there is insufficient
1056 * memory or the inode passed is %NULL.
1059 struct dentry * d_alloc_root(struct inode * root_inode)
1061 struct dentry *res = NULL;
1063 if (root_inode) {
1064 static const struct qstr name = { .name = "/", .len = 1 };
1066 res = d_alloc(NULL, &name);
1067 if (res) {
1068 res->d_sb = root_inode->i_sb;
1069 res->d_parent = res;
1070 d_instantiate(res, root_inode);
1073 return res;
1076 static inline struct hlist_head *d_hash(struct dentry *parent,
1077 unsigned long hash)
1079 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1080 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1081 return dentry_hashtable + (hash & D_HASHMASK);
1085 * d_alloc_anon - allocate an anonymous dentry
1086 * @inode: inode to allocate the dentry for
1088 * This is similar to d_alloc_root. It is used by filesystems when
1089 * creating a dentry for a given inode, often in the process of
1090 * mapping a filehandle to a dentry. The returned dentry may be
1091 * anonymous, or may have a full name (if the inode was already
1092 * in the cache). The file system may need to make further
1093 * efforts to connect this dentry into the dcache properly.
1095 * When called on a directory inode, we must ensure that
1096 * the inode only ever has one dentry. If a dentry is
1097 * found, that is returned instead of allocating a new one.
1099 * On successful return, the reference to the inode has been transferred
1100 * to the dentry. If %NULL is returned (indicating kmalloc failure),
1101 * the reference on the inode has not been released.
1104 struct dentry * d_alloc_anon(struct inode *inode)
1106 static const struct qstr anonstring = { .name = "" };
1107 struct dentry *tmp;
1108 struct dentry *res;
1110 if ((res = d_find_alias(inode))) {
1111 iput(inode);
1112 return res;
1115 tmp = d_alloc(NULL, &anonstring);
1116 if (!tmp)
1117 return NULL;
1119 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1121 spin_lock(&dcache_lock);
1122 res = __d_find_alias(inode, 0);
1123 if (!res) {
1124 /* attach a disconnected dentry */
1125 res = tmp;
1126 tmp = NULL;
1127 spin_lock(&res->d_lock);
1128 res->d_sb = inode->i_sb;
1129 res->d_parent = res;
1130 res->d_inode = inode;
1131 res->d_flags |= DCACHE_DISCONNECTED;
1132 res->d_flags &= ~DCACHE_UNHASHED;
1133 list_add(&res->d_alias, &inode->i_dentry);
1134 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
1135 spin_unlock(&res->d_lock);
1137 inode = NULL; /* don't drop reference */
1139 spin_unlock(&dcache_lock);
1141 if (inode)
1142 iput(inode);
1143 if (tmp)
1144 dput(tmp);
1145 return res;
1150 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1151 * @inode: the inode which may have a disconnected dentry
1152 * @dentry: a negative dentry which we want to point to the inode.
1154 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1155 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1156 * and return it, else simply d_add the inode to the dentry and return NULL.
1158 * This is needed in the lookup routine of any filesystem that is exportable
1159 * (via knfsd) so that we can build dcache paths to directories effectively.
1161 * If a dentry was found and moved, then it is returned. Otherwise NULL
1162 * is returned. This matches the expected return value of ->lookup.
1165 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1167 struct dentry *new = NULL;
1169 if (inode && S_ISDIR(inode->i_mode)) {
1170 spin_lock(&dcache_lock);
1171 new = __d_find_alias(inode, 1);
1172 if (new) {
1173 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1174 fsnotify_d_instantiate(new, inode);
1175 spin_unlock(&dcache_lock);
1176 security_d_instantiate(new, inode);
1177 d_rehash(dentry);
1178 d_move(new, dentry);
1179 iput(inode);
1180 } else {
1181 /* d_instantiate takes dcache_lock, so we do it by hand */
1182 list_add(&dentry->d_alias, &inode->i_dentry);
1183 dentry->d_inode = inode;
1184 fsnotify_d_instantiate(dentry, inode);
1185 spin_unlock(&dcache_lock);
1186 security_d_instantiate(dentry, inode);
1187 d_rehash(dentry);
1189 } else
1190 d_add(dentry, inode);
1191 return new;
1196 * d_lookup - search for a dentry
1197 * @parent: parent dentry
1198 * @name: qstr of name we wish to find
1200 * Searches the children of the parent dentry for the name in question. If
1201 * the dentry is found its reference count is incremented and the dentry
1202 * is returned. The caller must use d_put to free the entry when it has
1203 * finished using it. %NULL is returned on failure.
1205 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1206 * Memory barriers are used while updating and doing lockless traversal.
1207 * To avoid races with d_move while rename is happening, d_lock is used.
1209 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1210 * and name pointer in one structure pointed by d_qstr.
1212 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1213 * lookup is going on.
1215 * dentry_unused list is not updated even if lookup finds the required dentry
1216 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1217 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1218 * acquisition.
1220 * d_lookup() is protected against the concurrent renames in some unrelated
1221 * directory using the seqlockt_t rename_lock.
1224 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1226 struct dentry * dentry = NULL;
1227 unsigned long seq;
1229 do {
1230 seq = read_seqbegin(&rename_lock);
1231 dentry = __d_lookup(parent, name);
1232 if (dentry)
1233 break;
1234 } while (read_seqretry(&rename_lock, seq));
1235 return dentry;
1238 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1240 unsigned int len = name->len;
1241 unsigned int hash = name->hash;
1242 const unsigned char *str = name->name;
1243 struct hlist_head *head = d_hash(parent,hash);
1244 struct dentry *found = NULL;
1245 struct hlist_node *node;
1246 struct dentry *dentry;
1248 rcu_read_lock();
1250 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1251 struct qstr *qstr;
1253 if (dentry->d_name.hash != hash)
1254 continue;
1255 if (dentry->d_parent != parent)
1256 continue;
1258 spin_lock(&dentry->d_lock);
1261 * Recheck the dentry after taking the lock - d_move may have
1262 * changed things. Don't bother checking the hash because we're
1263 * about to compare the whole name anyway.
1265 if (dentry->d_parent != parent)
1266 goto next;
1269 * It is safe to compare names since d_move() cannot
1270 * change the qstr (protected by d_lock).
1272 qstr = &dentry->d_name;
1273 if (parent->d_op && parent->d_op->d_compare) {
1274 if (parent->d_op->d_compare(parent, qstr, name))
1275 goto next;
1276 } else {
1277 if (qstr->len != len)
1278 goto next;
1279 if (memcmp(qstr->name, str, len))
1280 goto next;
1283 if (!d_unhashed(dentry)) {
1284 atomic_inc(&dentry->d_count);
1285 found = dentry;
1287 spin_unlock(&dentry->d_lock);
1288 break;
1289 next:
1290 spin_unlock(&dentry->d_lock);
1292 rcu_read_unlock();
1294 return found;
1298 * d_hash_and_lookup - hash the qstr then search for a dentry
1299 * @dir: Directory to search in
1300 * @name: qstr of name we wish to find
1302 * On hash failure or on lookup failure NULL is returned.
1304 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1306 struct dentry *dentry = NULL;
1309 * Check for a fs-specific hash function. Note that we must
1310 * calculate the standard hash first, as the d_op->d_hash()
1311 * routine may choose to leave the hash value unchanged.
1313 name->hash = full_name_hash(name->name, name->len);
1314 if (dir->d_op && dir->d_op->d_hash) {
1315 if (dir->d_op->d_hash(dir, name) < 0)
1316 goto out;
1318 dentry = d_lookup(dir, name);
1319 out:
1320 return dentry;
1324 * d_validate - verify dentry provided from insecure source
1325 * @dentry: The dentry alleged to be valid child of @dparent
1326 * @dparent: The parent dentry (known to be valid)
1327 * @hash: Hash of the dentry
1328 * @len: Length of the name
1330 * An insecure source has sent us a dentry, here we verify it and dget() it.
1331 * This is used by ncpfs in its readdir implementation.
1332 * Zero is returned in the dentry is invalid.
1335 int d_validate(struct dentry *dentry, struct dentry *dparent)
1337 struct hlist_head *base;
1338 struct hlist_node *lhp;
1340 /* Check whether the ptr might be valid at all.. */
1341 if (!kmem_ptr_validate(dentry_cache, dentry))
1342 goto out;
1344 if (dentry->d_parent != dparent)
1345 goto out;
1347 spin_lock(&dcache_lock);
1348 base = d_hash(dparent, dentry->d_name.hash);
1349 hlist_for_each(lhp,base) {
1350 /* hlist_for_each_entry_rcu() not required for d_hash list
1351 * as it is parsed under dcache_lock
1353 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1354 __dget_locked(dentry);
1355 spin_unlock(&dcache_lock);
1356 return 1;
1359 spin_unlock(&dcache_lock);
1360 out:
1361 return 0;
1365 * When a file is deleted, we have two options:
1366 * - turn this dentry into a negative dentry
1367 * - unhash this dentry and free it.
1369 * Usually, we want to just turn this into
1370 * a negative dentry, but if anybody else is
1371 * currently using the dentry or the inode
1372 * we can't do that and we fall back on removing
1373 * it from the hash queues and waiting for
1374 * it to be deleted later when it has no users
1378 * d_delete - delete a dentry
1379 * @dentry: The dentry to delete
1381 * Turn the dentry into a negative dentry if possible, otherwise
1382 * remove it from the hash queues so it can be deleted later
1385 void d_delete(struct dentry * dentry)
1387 int isdir = 0;
1389 * Are we the only user?
1391 spin_lock(&dcache_lock);
1392 spin_lock(&dentry->d_lock);
1393 isdir = S_ISDIR(dentry->d_inode->i_mode);
1394 if (atomic_read(&dentry->d_count) == 1) {
1395 dentry_iput(dentry);
1396 fsnotify_nameremove(dentry, isdir);
1397 return;
1400 if (!d_unhashed(dentry))
1401 __d_drop(dentry);
1403 spin_unlock(&dentry->d_lock);
1404 spin_unlock(&dcache_lock);
1406 fsnotify_nameremove(dentry, isdir);
1409 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1412 entry->d_flags &= ~DCACHE_UNHASHED;
1413 hlist_add_head_rcu(&entry->d_hash, list);
1416 static void _d_rehash(struct dentry * entry)
1418 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1422 * d_rehash - add an entry back to the hash
1423 * @entry: dentry to add to the hash
1425 * Adds a dentry to the hash according to its name.
1428 void d_rehash(struct dentry * entry)
1430 spin_lock(&dcache_lock);
1431 spin_lock(&entry->d_lock);
1432 _d_rehash(entry);
1433 spin_unlock(&entry->d_lock);
1434 spin_unlock(&dcache_lock);
1437 #define do_switch(x,y) do { \
1438 __typeof__ (x) __tmp = x; \
1439 x = y; y = __tmp; } while (0)
1442 * When switching names, the actual string doesn't strictly have to
1443 * be preserved in the target - because we're dropping the target
1444 * anyway. As such, we can just do a simple memcpy() to copy over
1445 * the new name before we switch.
1447 * Note that we have to be a lot more careful about getting the hash
1448 * switched - we have to switch the hash value properly even if it
1449 * then no longer matches the actual (corrupted) string of the target.
1450 * The hash value has to match the hash queue that the dentry is on..
1452 static void switch_names(struct dentry *dentry, struct dentry *target)
1454 if (dname_external(target)) {
1455 if (dname_external(dentry)) {
1457 * Both external: swap the pointers
1459 do_switch(target->d_name.name, dentry->d_name.name);
1460 } else {
1462 * dentry:internal, target:external. Steal target's
1463 * storage and make target internal.
1465 memcpy(target->d_iname, dentry->d_name.name,
1466 dentry->d_name.len + 1);
1467 dentry->d_name.name = target->d_name.name;
1468 target->d_name.name = target->d_iname;
1470 } else {
1471 if (dname_external(dentry)) {
1473 * dentry:external, target:internal. Give dentry's
1474 * storage to target and make dentry internal
1476 memcpy(dentry->d_iname, target->d_name.name,
1477 target->d_name.len + 1);
1478 target->d_name.name = dentry->d_name.name;
1479 dentry->d_name.name = dentry->d_iname;
1480 } else {
1482 * Both are internal. Just copy target to dentry
1484 memcpy(dentry->d_iname, target->d_name.name,
1485 target->d_name.len + 1);
1491 * We cannibalize "target" when moving dentry on top of it,
1492 * because it's going to be thrown away anyway. We could be more
1493 * polite about it, though.
1495 * This forceful removal will result in ugly /proc output if
1496 * somebody holds a file open that got deleted due to a rename.
1497 * We could be nicer about the deleted file, and let it show
1498 * up under the name it had before it was deleted rather than
1499 * under the original name of the file that was moved on top of it.
1503 * d_move_locked - move a dentry
1504 * @dentry: entry to move
1505 * @target: new dentry
1507 * Update the dcache to reflect the move of a file name. Negative
1508 * dcache entries should not be moved in this way.
1510 static void d_move_locked(struct dentry * dentry, struct dentry * target)
1512 struct hlist_head *list;
1514 if (!dentry->d_inode)
1515 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1517 write_seqlock(&rename_lock);
1519 * XXXX: do we really need to take target->d_lock?
1521 if (target < dentry) {
1522 spin_lock(&target->d_lock);
1523 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1524 } else {
1525 spin_lock(&dentry->d_lock);
1526 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
1529 /* Move the dentry to the target hash queue, if on different bucket */
1530 if (d_unhashed(dentry))
1531 goto already_unhashed;
1533 hlist_del_rcu(&dentry->d_hash);
1535 already_unhashed:
1536 list = d_hash(target->d_parent, target->d_name.hash);
1537 __d_rehash(dentry, list);
1539 /* Unhash the target: dput() will then get rid of it */
1540 __d_drop(target);
1542 list_del(&dentry->d_u.d_child);
1543 list_del(&target->d_u.d_child);
1545 /* Switch the names.. */
1546 switch_names(dentry, target);
1547 do_switch(dentry->d_name.len, target->d_name.len);
1548 do_switch(dentry->d_name.hash, target->d_name.hash);
1550 /* ... and switch the parents */
1551 if (IS_ROOT(dentry)) {
1552 dentry->d_parent = target->d_parent;
1553 target->d_parent = target;
1554 INIT_LIST_HEAD(&target->d_u.d_child);
1555 } else {
1556 do_switch(dentry->d_parent, target->d_parent);
1558 /* And add them back to the (new) parent lists */
1559 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1562 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1563 spin_unlock(&target->d_lock);
1564 fsnotify_d_move(dentry);
1565 spin_unlock(&dentry->d_lock);
1566 write_sequnlock(&rename_lock);
1570 * d_move - move a dentry
1571 * @dentry: entry to move
1572 * @target: new dentry
1574 * Update the dcache to reflect the move of a file name. Negative
1575 * dcache entries should not be moved in this way.
1578 void d_move(struct dentry * dentry, struct dentry * target)
1580 spin_lock(&dcache_lock);
1581 d_move_locked(dentry, target);
1582 spin_unlock(&dcache_lock);
1586 * Helper that returns 1 if p1 is a parent of p2, else 0
1588 static int d_isparent(struct dentry *p1, struct dentry *p2)
1590 struct dentry *p;
1592 for (p = p2; p->d_parent != p; p = p->d_parent) {
1593 if (p->d_parent == p1)
1594 return 1;
1596 return 0;
1600 * This helper attempts to cope with remotely renamed directories
1602 * It assumes that the caller is already holding
1603 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1605 * Note: If ever the locking in lock_rename() changes, then please
1606 * remember to update this too...
1608 * On return, dcache_lock will have been unlocked.
1610 static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1612 struct mutex *m1 = NULL, *m2 = NULL;
1613 struct dentry *ret;
1615 /* If alias and dentry share a parent, then no extra locks required */
1616 if (alias->d_parent == dentry->d_parent)
1617 goto out_unalias;
1619 /* Check for loops */
1620 ret = ERR_PTR(-ELOOP);
1621 if (d_isparent(alias, dentry))
1622 goto out_err;
1624 /* See lock_rename() */
1625 ret = ERR_PTR(-EBUSY);
1626 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1627 goto out_err;
1628 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1629 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1630 goto out_err;
1631 m2 = &alias->d_parent->d_inode->i_mutex;
1632 out_unalias:
1633 d_move_locked(alias, dentry);
1634 ret = alias;
1635 out_err:
1636 spin_unlock(&dcache_lock);
1637 if (m2)
1638 mutex_unlock(m2);
1639 if (m1)
1640 mutex_unlock(m1);
1641 return ret;
1645 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1646 * named dentry in place of the dentry to be replaced.
1648 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1650 struct dentry *dparent, *aparent;
1652 switch_names(dentry, anon);
1653 do_switch(dentry->d_name.len, anon->d_name.len);
1654 do_switch(dentry->d_name.hash, anon->d_name.hash);
1656 dparent = dentry->d_parent;
1657 aparent = anon->d_parent;
1659 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1660 list_del(&dentry->d_u.d_child);
1661 if (!IS_ROOT(dentry))
1662 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1663 else
1664 INIT_LIST_HEAD(&dentry->d_u.d_child);
1666 anon->d_parent = (dparent == dentry) ? anon : dparent;
1667 list_del(&anon->d_u.d_child);
1668 if (!IS_ROOT(anon))
1669 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1670 else
1671 INIT_LIST_HEAD(&anon->d_u.d_child);
1673 anon->d_flags &= ~DCACHE_DISCONNECTED;
1677 * d_materialise_unique - introduce an inode into the tree
1678 * @dentry: candidate dentry
1679 * @inode: inode to bind to the dentry, to which aliases may be attached
1681 * Introduces an dentry into the tree, substituting an extant disconnected
1682 * root directory alias in its place if there is one
1684 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1686 struct dentry *actual;
1688 BUG_ON(!d_unhashed(dentry));
1690 spin_lock(&dcache_lock);
1692 if (!inode) {
1693 actual = dentry;
1694 dentry->d_inode = NULL;
1695 goto found_lock;
1698 if (S_ISDIR(inode->i_mode)) {
1699 struct dentry *alias;
1701 /* Does an aliased dentry already exist? */
1702 alias = __d_find_alias(inode, 0);
1703 if (alias) {
1704 actual = alias;
1705 /* Is this an anonymous mountpoint that we could splice
1706 * into our tree? */
1707 if (IS_ROOT(alias)) {
1708 spin_lock(&alias->d_lock);
1709 __d_materialise_dentry(dentry, alias);
1710 __d_drop(alias);
1711 goto found;
1713 /* Nope, but we must(!) avoid directory aliasing */
1714 actual = __d_unalias(dentry, alias);
1715 if (IS_ERR(actual))
1716 dput(alias);
1717 goto out_nolock;
1721 /* Add a unique reference */
1722 actual = __d_instantiate_unique(dentry, inode);
1723 if (!actual)
1724 actual = dentry;
1725 else if (unlikely(!d_unhashed(actual)))
1726 goto shouldnt_be_hashed;
1728 found_lock:
1729 spin_lock(&actual->d_lock);
1730 found:
1731 _d_rehash(actual);
1732 spin_unlock(&actual->d_lock);
1733 spin_unlock(&dcache_lock);
1734 out_nolock:
1735 if (actual == dentry) {
1736 security_d_instantiate(dentry, inode);
1737 return NULL;
1740 iput(inode);
1741 return actual;
1743 shouldnt_be_hashed:
1744 spin_unlock(&dcache_lock);
1745 BUG();
1746 goto shouldnt_be_hashed;
1749 static int prepend(char **buffer, int *buflen, const char *str,
1750 int namelen)
1752 *buflen -= namelen;
1753 if (*buflen < 0)
1754 return -ENAMETOOLONG;
1755 *buffer -= namelen;
1756 memcpy(*buffer, str, namelen);
1757 return 0;
1761 * d_path - return the path of a dentry
1762 * @path: the dentry/vfsmount to report
1763 * @root: root vfsmnt/dentry (may be modified by this function)
1764 * @buffer: buffer to return value in
1765 * @buflen: buffer length
1767 * Convert a dentry into an ASCII path name. If the entry has been deleted
1768 * the string " (deleted)" is appended. Note that this is ambiguous.
1770 * Returns the buffer or an error code if the path was too long.
1772 * "buflen" should be positive. Caller holds the dcache_lock.
1774 * If path is not reachable from the supplied root, then the value of
1775 * root is changed (without modifying refcounts).
1777 char *__d_path(const struct path *path, struct path *root,
1778 char *buffer, int buflen)
1780 struct dentry *dentry = path->dentry;
1781 struct vfsmount *vfsmnt = path->mnt;
1782 char * end = buffer+buflen;
1783 char * retval;
1785 prepend(&end, &buflen, "\0", 1);
1786 if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
1787 (prepend(&end, &buflen, " (deleted)", 10) != 0))
1788 goto Elong;
1790 if (buflen < 1)
1791 goto Elong;
1792 /* Get '/' right */
1793 retval = end-1;
1794 *retval = '/';
1796 for (;;) {
1797 struct dentry * parent;
1799 if (dentry == root->dentry && vfsmnt == root->mnt)
1800 break;
1801 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1802 /* Global root? */
1803 spin_lock(&vfsmount_lock);
1804 if (vfsmnt->mnt_parent == vfsmnt) {
1805 spin_unlock(&vfsmount_lock);
1806 goto global_root;
1808 dentry = vfsmnt->mnt_mountpoint;
1809 vfsmnt = vfsmnt->mnt_parent;
1810 spin_unlock(&vfsmount_lock);
1811 continue;
1813 parent = dentry->d_parent;
1814 prefetch(parent);
1815 if ((prepend(&end, &buflen, dentry->d_name.name,
1816 dentry->d_name.len) != 0) ||
1817 (prepend(&end, &buflen, "/", 1) != 0))
1818 goto Elong;
1819 retval = end;
1820 dentry = parent;
1823 return retval;
1825 global_root:
1826 retval += 1; /* hit the slash */
1827 if (prepend(&retval, &buflen, dentry->d_name.name,
1828 dentry->d_name.len) != 0)
1829 goto Elong;
1830 root->mnt = vfsmnt;
1831 root->dentry = dentry;
1832 return retval;
1833 Elong:
1834 return ERR_PTR(-ENAMETOOLONG);
1838 * d_path - return the path of a dentry
1839 * @path: path to report
1840 * @buf: buffer to return value in
1841 * @buflen: buffer length
1843 * Convert a dentry into an ASCII path name. If the entry has been deleted
1844 * the string " (deleted)" is appended. Note that this is ambiguous.
1846 * Returns the buffer or an error code if the path was too long.
1848 * "buflen" should be positive. Caller holds the dcache_lock.
1850 char *d_path(struct path *path, char *buf, int buflen)
1852 char *res;
1853 struct path root;
1854 struct path tmp;
1857 * We have various synthetic filesystems that never get mounted. On
1858 * these filesystems dentries are never used for lookup purposes, and
1859 * thus don't need to be hashed. They also don't need a name until a
1860 * user wants to identify the object in /proc/pid/fd/. The little hack
1861 * below allows us to generate a name for these objects on demand:
1863 if (path->dentry->d_op && path->dentry->d_op->d_dname)
1864 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
1866 read_lock(&current->fs->lock);
1867 root = current->fs->root;
1868 path_get(&root);
1869 read_unlock(&current->fs->lock);
1870 spin_lock(&dcache_lock);
1871 tmp = root;
1872 res = __d_path(path, &tmp, buf, buflen);
1873 spin_unlock(&dcache_lock);
1874 path_put(&root);
1875 return res;
1879 * Helper function for dentry_operations.d_dname() members
1881 char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
1882 const char *fmt, ...)
1884 va_list args;
1885 char temp[64];
1886 int sz;
1888 va_start(args, fmt);
1889 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
1890 va_end(args);
1892 if (sz > sizeof(temp) || sz > buflen)
1893 return ERR_PTR(-ENAMETOOLONG);
1895 buffer += buflen - sz;
1896 return memcpy(buffer, temp, sz);
1900 * Write full pathname from the root of the filesystem into the buffer.
1902 char *dentry_path(struct dentry *dentry, char *buf, int buflen)
1904 char *end = buf + buflen;
1905 char *retval;
1907 spin_lock(&dcache_lock);
1908 prepend(&end, &buflen, "\0", 1);
1909 if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
1910 (prepend(&end, &buflen, "//deleted", 9) != 0))
1911 goto Elong;
1912 if (buflen < 1)
1913 goto Elong;
1914 /* Get '/' right */
1915 retval = end-1;
1916 *retval = '/';
1918 for (;;) {
1919 struct dentry *parent;
1920 if (IS_ROOT(dentry))
1921 break;
1923 parent = dentry->d_parent;
1924 prefetch(parent);
1926 if ((prepend(&end, &buflen, dentry->d_name.name,
1927 dentry->d_name.len) != 0) ||
1928 (prepend(&end, &buflen, "/", 1) != 0))
1929 goto Elong;
1931 retval = end;
1932 dentry = parent;
1934 spin_unlock(&dcache_lock);
1935 return retval;
1936 Elong:
1937 spin_unlock(&dcache_lock);
1938 return ERR_PTR(-ENAMETOOLONG);
1942 * NOTE! The user-level library version returns a
1943 * character pointer. The kernel system call just
1944 * returns the length of the buffer filled (which
1945 * includes the ending '\0' character), or a negative
1946 * error value. So libc would do something like
1948 * char *getcwd(char * buf, size_t size)
1950 * int retval;
1952 * retval = sys_getcwd(buf, size);
1953 * if (retval >= 0)
1954 * return buf;
1955 * errno = -retval;
1956 * return NULL;
1959 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1961 int error;
1962 struct path pwd, root;
1963 char *page = (char *) __get_free_page(GFP_USER);
1965 if (!page)
1966 return -ENOMEM;
1968 read_lock(&current->fs->lock);
1969 pwd = current->fs->pwd;
1970 path_get(&pwd);
1971 root = current->fs->root;
1972 path_get(&root);
1973 read_unlock(&current->fs->lock);
1975 error = -ENOENT;
1976 /* Has the current directory has been unlinked? */
1977 spin_lock(&dcache_lock);
1978 if (pwd.dentry->d_parent == pwd.dentry || !d_unhashed(pwd.dentry)) {
1979 unsigned long len;
1980 struct path tmp = root;
1981 char * cwd;
1983 cwd = __d_path(&pwd, &tmp, page, PAGE_SIZE);
1984 spin_unlock(&dcache_lock);
1986 error = PTR_ERR(cwd);
1987 if (IS_ERR(cwd))
1988 goto out;
1990 error = -ERANGE;
1991 len = PAGE_SIZE + page - cwd;
1992 if (len <= size) {
1993 error = len;
1994 if (copy_to_user(buf, cwd, len))
1995 error = -EFAULT;
1997 } else
1998 spin_unlock(&dcache_lock);
2000 out:
2001 path_put(&pwd);
2002 path_put(&root);
2003 free_page((unsigned long) page);
2004 return error;
2008 * Test whether new_dentry is a subdirectory of old_dentry.
2010 * Trivially implemented using the dcache structure
2014 * is_subdir - is new dentry a subdirectory of old_dentry
2015 * @new_dentry: new dentry
2016 * @old_dentry: old dentry
2018 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2019 * Returns 0 otherwise.
2020 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2023 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
2025 int result;
2026 struct dentry * saved = new_dentry;
2027 unsigned long seq;
2029 /* need rcu_readlock to protect against the d_parent trashing due to
2030 * d_move
2032 rcu_read_lock();
2033 do {
2034 /* for restarting inner loop in case of seq retry */
2035 new_dentry = saved;
2036 result = 0;
2037 seq = read_seqbegin(&rename_lock);
2038 for (;;) {
2039 if (new_dentry != old_dentry) {
2040 struct dentry * parent = new_dentry->d_parent;
2041 if (parent == new_dentry)
2042 break;
2043 new_dentry = parent;
2044 continue;
2046 result = 1;
2047 break;
2049 } while (read_seqretry(&rename_lock, seq));
2050 rcu_read_unlock();
2052 return result;
2055 void d_genocide(struct dentry *root)
2057 struct dentry *this_parent = root;
2058 struct list_head *next;
2060 spin_lock(&dcache_lock);
2061 repeat:
2062 next = this_parent->d_subdirs.next;
2063 resume:
2064 while (next != &this_parent->d_subdirs) {
2065 struct list_head *tmp = next;
2066 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
2067 next = tmp->next;
2068 if (d_unhashed(dentry)||!dentry->d_inode)
2069 continue;
2070 if (!list_empty(&dentry->d_subdirs)) {
2071 this_parent = dentry;
2072 goto repeat;
2074 atomic_dec(&dentry->d_count);
2076 if (this_parent != root) {
2077 next = this_parent->d_u.d_child.next;
2078 atomic_dec(&this_parent->d_count);
2079 this_parent = this_parent->d_parent;
2080 goto resume;
2082 spin_unlock(&dcache_lock);
2086 * find_inode_number - check for dentry with name
2087 * @dir: directory to check
2088 * @name: Name to find.
2090 * Check whether a dentry already exists for the given name,
2091 * and return the inode number if it has an inode. Otherwise
2092 * 0 is returned.
2094 * This routine is used to post-process directory listings for
2095 * filesystems using synthetic inode numbers, and is necessary
2096 * to keep getcwd() working.
2099 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2101 struct dentry * dentry;
2102 ino_t ino = 0;
2104 dentry = d_hash_and_lookup(dir, name);
2105 if (dentry) {
2106 if (dentry->d_inode)
2107 ino = dentry->d_inode->i_ino;
2108 dput(dentry);
2110 return ino;
2113 static __initdata unsigned long dhash_entries;
2114 static int __init set_dhash_entries(char *str)
2116 if (!str)
2117 return 0;
2118 dhash_entries = simple_strtoul(str, &str, 0);
2119 return 1;
2121 __setup("dhash_entries=", set_dhash_entries);
2123 static void __init dcache_init_early(void)
2125 int loop;
2127 /* If hashes are distributed across NUMA nodes, defer
2128 * hash allocation until vmalloc space is available.
2130 if (hashdist)
2131 return;
2133 dentry_hashtable =
2134 alloc_large_system_hash("Dentry cache",
2135 sizeof(struct hlist_head),
2136 dhash_entries,
2138 HASH_EARLY,
2139 &d_hash_shift,
2140 &d_hash_mask,
2143 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2144 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2147 static void __init dcache_init(void)
2149 int loop;
2152 * A constructor could be added for stable state like the lists,
2153 * but it is probably not worth it because of the cache nature
2154 * of the dcache.
2156 dentry_cache = KMEM_CACHE(dentry,
2157 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
2159 register_shrinker(&dcache_shrinker);
2161 /* Hash may have been set up in dcache_init_early */
2162 if (!hashdist)
2163 return;
2165 dentry_hashtable =
2166 alloc_large_system_hash("Dentry cache",
2167 sizeof(struct hlist_head),
2168 dhash_entries,
2171 &d_hash_shift,
2172 &d_hash_mask,
2175 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2176 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2179 /* SLAB cache for __getname() consumers */
2180 struct kmem_cache *names_cachep __read_mostly;
2182 /* SLAB cache for file structures */
2183 struct kmem_cache *filp_cachep __read_mostly;
2185 EXPORT_SYMBOL(d_genocide);
2187 void __init vfs_caches_init_early(void)
2189 dcache_init_early();
2190 inode_init_early();
2193 void __init vfs_caches_init(unsigned long mempages)
2195 unsigned long reserve;
2197 /* Base hash sizes on available memory, with a reserve equal to
2198 150% of current kernel size */
2200 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2201 mempages -= reserve;
2203 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2204 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2206 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
2207 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2209 dcache_init();
2210 inode_init();
2211 files_init(mempages);
2212 mnt_init();
2213 bdev_cache_init();
2214 chrdev_init();
2217 EXPORT_SYMBOL(d_alloc);
2218 EXPORT_SYMBOL(d_alloc_anon);
2219 EXPORT_SYMBOL(d_alloc_root);
2220 EXPORT_SYMBOL(d_delete);
2221 EXPORT_SYMBOL(d_find_alias);
2222 EXPORT_SYMBOL(d_instantiate);
2223 EXPORT_SYMBOL(d_invalidate);
2224 EXPORT_SYMBOL(d_lookup);
2225 EXPORT_SYMBOL(d_move);
2226 EXPORT_SYMBOL_GPL(d_materialise_unique);
2227 EXPORT_SYMBOL(d_path);
2228 EXPORT_SYMBOL(d_prune_aliases);
2229 EXPORT_SYMBOL(d_rehash);
2230 EXPORT_SYMBOL(d_splice_alias);
2231 EXPORT_SYMBOL(d_validate);
2232 EXPORT_SYMBOL(dget_locked);
2233 EXPORT_SYMBOL(dput);
2234 EXPORT_SYMBOL(find_inode_number);
2235 EXPORT_SYMBOL(have_submounts);
2236 EXPORT_SYMBOL(names_cachep);
2237 EXPORT_SYMBOL(shrink_dcache_parent);
2238 EXPORT_SYMBOL(shrink_dcache_sb);