Driver core: convert fb code to use struct device
[linux-2.6/linux-acpi-2.6/ibm-acpi-2.6.git] / fs / dcache.c
blobfd4a428998efe1152c01129defec0691ac87a465
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/smp_lock.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"
38 int sysctl_vfs_cache_pressure __read_mostly = 100;
39 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
41 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
42 static __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
44 EXPORT_SYMBOL(dcache_lock);
46 static kmem_cache_t *dentry_cache __read_mostly;
48 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
51 * This is the single most critical data structure when it comes
52 * to the dcache: the hashtable for lookups. Somebody should try
53 * to make this good - I've just made it work.
55 * This hash-function tries to avoid losing too many bits of hash
56 * information, yet avoid using a prime hash-size or similar.
58 #define D_HASHBITS d_hash_shift
59 #define D_HASHMASK d_hash_mask
61 static unsigned int d_hash_mask __read_mostly;
62 static unsigned int d_hash_shift __read_mostly;
63 static struct hlist_head *dentry_hashtable __read_mostly;
64 static LIST_HEAD(dentry_unused);
66 /* Statistics gathering. */
67 struct dentry_stat_t dentry_stat = {
68 .age_limit = 45,
71 static void d_callback(struct rcu_head *head)
73 struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
75 if (dname_external(dentry))
76 kfree(dentry->d_name.name);
77 kmem_cache_free(dentry_cache, dentry);
81 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
82 * inside dcache_lock.
84 static void d_free(struct dentry *dentry)
86 if (dentry->d_op && dentry->d_op->d_release)
87 dentry->d_op->d_release(dentry);
88 call_rcu(&dentry->d_u.d_rcu, d_callback);
92 * Release the dentry's inode, using the filesystem
93 * d_iput() operation if defined.
94 * Called with dcache_lock and per dentry lock held, drops both.
96 static void dentry_iput(struct dentry * dentry)
98 struct inode *inode = dentry->d_inode;
99 if (inode) {
100 dentry->d_inode = NULL;
101 list_del_init(&dentry->d_alias);
102 spin_unlock(&dentry->d_lock);
103 spin_unlock(&dcache_lock);
104 if (!inode->i_nlink)
105 fsnotify_inoderemove(inode);
106 if (dentry->d_op && dentry->d_op->d_iput)
107 dentry->d_op->d_iput(dentry, inode);
108 else
109 iput(inode);
110 } else {
111 spin_unlock(&dentry->d_lock);
112 spin_unlock(&dcache_lock);
117 * This is dput
119 * This is complicated by the fact that we do not want to put
120 * dentries that are no longer on any hash chain on the unused
121 * list: we'd much rather just get rid of them immediately.
123 * However, that implies that we have to traverse the dentry
124 * tree upwards to the parents which might _also_ now be
125 * scheduled for deletion (it may have been only waiting for
126 * its last child to go away).
128 * This tail recursion is done by hand as we don't want to depend
129 * on the compiler to always get this right (gcc generally doesn't).
130 * Real recursion would eat up our stack space.
134 * dput - release a dentry
135 * @dentry: dentry to release
137 * Release a dentry. This will drop the usage count and if appropriate
138 * call the dentry unlink method as well as removing it from the queues and
139 * releasing its resources. If the parent dentries were scheduled for release
140 * they too may now get deleted.
142 * no dcache lock, please.
145 void dput(struct dentry *dentry)
147 if (!dentry)
148 return;
150 repeat:
151 if (atomic_read(&dentry->d_count) == 1)
152 might_sleep();
153 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
154 return;
156 spin_lock(&dentry->d_lock);
157 if (atomic_read(&dentry->d_count)) {
158 spin_unlock(&dentry->d_lock);
159 spin_unlock(&dcache_lock);
160 return;
164 * AV: ->d_delete() is _NOT_ allowed to block now.
166 if (dentry->d_op && dentry->d_op->d_delete) {
167 if (dentry->d_op->d_delete(dentry))
168 goto unhash_it;
170 /* Unreachable? Get rid of it */
171 if (d_unhashed(dentry))
172 goto kill_it;
173 if (list_empty(&dentry->d_lru)) {
174 dentry->d_flags |= DCACHE_REFERENCED;
175 list_add(&dentry->d_lru, &dentry_unused);
176 dentry_stat.nr_unused++;
178 spin_unlock(&dentry->d_lock);
179 spin_unlock(&dcache_lock);
180 return;
182 unhash_it:
183 __d_drop(dentry);
185 kill_it: {
186 struct dentry *parent;
188 /* If dentry was on d_lru list
189 * delete it from there
191 if (!list_empty(&dentry->d_lru)) {
192 list_del(&dentry->d_lru);
193 dentry_stat.nr_unused--;
195 list_del(&dentry->d_u.d_child);
196 dentry_stat.nr_dentry--; /* For d_free, below */
197 /*drops the locks, at that point nobody can reach this dentry */
198 dentry_iput(dentry);
199 parent = dentry->d_parent;
200 d_free(dentry);
201 if (dentry == parent)
202 return;
203 dentry = parent;
204 goto repeat;
209 * d_invalidate - invalidate a dentry
210 * @dentry: dentry to invalidate
212 * Try to invalidate the dentry if it turns out to be
213 * possible. If there are other dentries that can be
214 * reached through this one we can't delete it and we
215 * return -EBUSY. On success we return 0.
217 * no dcache lock.
220 int d_invalidate(struct dentry * dentry)
223 * If it's already been dropped, return OK.
225 spin_lock(&dcache_lock);
226 if (d_unhashed(dentry)) {
227 spin_unlock(&dcache_lock);
228 return 0;
231 * Check whether to do a partial shrink_dcache
232 * to get rid of unused child entries.
234 if (!list_empty(&dentry->d_subdirs)) {
235 spin_unlock(&dcache_lock);
236 shrink_dcache_parent(dentry);
237 spin_lock(&dcache_lock);
241 * Somebody else still using it?
243 * If it's a directory, we can't drop it
244 * for fear of somebody re-populating it
245 * with children (even though dropping it
246 * would make it unreachable from the root,
247 * we might still populate it if it was a
248 * working directory or similar).
250 spin_lock(&dentry->d_lock);
251 if (atomic_read(&dentry->d_count) > 1) {
252 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
253 spin_unlock(&dentry->d_lock);
254 spin_unlock(&dcache_lock);
255 return -EBUSY;
259 __d_drop(dentry);
260 spin_unlock(&dentry->d_lock);
261 spin_unlock(&dcache_lock);
262 return 0;
265 /* This should be called _only_ with dcache_lock held */
267 static inline struct dentry * __dget_locked(struct dentry *dentry)
269 atomic_inc(&dentry->d_count);
270 if (!list_empty(&dentry->d_lru)) {
271 dentry_stat.nr_unused--;
272 list_del_init(&dentry->d_lru);
274 return dentry;
277 struct dentry * dget_locked(struct dentry *dentry)
279 return __dget_locked(dentry);
283 * d_find_alias - grab a hashed alias of inode
284 * @inode: inode in question
285 * @want_discon: flag, used by d_splice_alias, to request
286 * that only a DISCONNECTED alias be returned.
288 * If inode has a hashed alias, or is a directory and has any alias,
289 * acquire the reference to alias and return it. Otherwise return NULL.
290 * Notice that if inode is a directory there can be only one alias and
291 * it can be unhashed only if it has no children, or if it is the root
292 * of a filesystem.
294 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
295 * any other hashed alias over that one unless @want_discon is set,
296 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
299 static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
301 struct list_head *head, *next, *tmp;
302 struct dentry *alias, *discon_alias=NULL;
304 head = &inode->i_dentry;
305 next = inode->i_dentry.next;
306 while (next != head) {
307 tmp = next;
308 next = tmp->next;
309 prefetch(next);
310 alias = list_entry(tmp, struct dentry, d_alias);
311 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
312 if (IS_ROOT(alias) &&
313 (alias->d_flags & DCACHE_DISCONNECTED))
314 discon_alias = alias;
315 else if (!want_discon) {
316 __dget_locked(alias);
317 return alias;
321 if (discon_alias)
322 __dget_locked(discon_alias);
323 return discon_alias;
326 struct dentry * d_find_alias(struct inode *inode)
328 struct dentry *de = NULL;
330 if (!list_empty(&inode->i_dentry)) {
331 spin_lock(&dcache_lock);
332 de = __d_find_alias(inode, 0);
333 spin_unlock(&dcache_lock);
335 return de;
339 * Try to kill dentries associated with this inode.
340 * WARNING: you must own a reference to inode.
342 void d_prune_aliases(struct inode *inode)
344 struct dentry *dentry;
345 restart:
346 spin_lock(&dcache_lock);
347 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
348 spin_lock(&dentry->d_lock);
349 if (!atomic_read(&dentry->d_count)) {
350 __dget_locked(dentry);
351 __d_drop(dentry);
352 spin_unlock(&dentry->d_lock);
353 spin_unlock(&dcache_lock);
354 dput(dentry);
355 goto restart;
357 spin_unlock(&dentry->d_lock);
359 spin_unlock(&dcache_lock);
363 * Throw away a dentry - free the inode, dput the parent. This requires that
364 * the LRU list has already been removed.
366 * Called with dcache_lock, drops it and then regains.
367 * Called with dentry->d_lock held, drops it.
369 static void prune_one_dentry(struct dentry * dentry)
371 struct dentry * parent;
373 __d_drop(dentry);
374 list_del(&dentry->d_u.d_child);
375 dentry_stat.nr_dentry--; /* For d_free, below */
376 dentry_iput(dentry);
377 parent = dentry->d_parent;
378 d_free(dentry);
379 if (parent != dentry)
380 dput(parent);
381 spin_lock(&dcache_lock);
385 * prune_dcache - shrink the dcache
386 * @count: number of entries to try and free
387 * @sb: if given, ignore dentries for other superblocks
388 * which are being unmounted.
390 * Shrink the dcache. This is done when we need
391 * more memory, or simply when we need to unmount
392 * something (at which point we need to unuse
393 * all dentries).
395 * This function may fail to free any resources if
396 * all the dentries are in use.
399 static void prune_dcache(int count, struct super_block *sb)
401 spin_lock(&dcache_lock);
402 for (; count ; count--) {
403 struct dentry *dentry;
404 struct list_head *tmp;
405 struct rw_semaphore *s_umount;
407 cond_resched_lock(&dcache_lock);
409 tmp = dentry_unused.prev;
410 if (sb) {
411 /* Try to find a dentry for this sb, but don't try
412 * too hard, if they aren't near the tail they will
413 * be moved down again soon
415 int skip = count;
416 while (skip && tmp != &dentry_unused &&
417 list_entry(tmp, struct dentry, d_lru)->d_sb != sb) {
418 skip--;
419 tmp = tmp->prev;
422 if (tmp == &dentry_unused)
423 break;
424 list_del_init(tmp);
425 prefetch(dentry_unused.prev);
426 dentry_stat.nr_unused--;
427 dentry = list_entry(tmp, struct dentry, d_lru);
429 spin_lock(&dentry->d_lock);
431 * We found an inuse dentry which was not removed from
432 * dentry_unused because of laziness during lookup. Do not free
433 * it - just keep it off the dentry_unused list.
435 if (atomic_read(&dentry->d_count)) {
436 spin_unlock(&dentry->d_lock);
437 continue;
439 /* If the dentry was recently referenced, don't free it. */
440 if (dentry->d_flags & DCACHE_REFERENCED) {
441 dentry->d_flags &= ~DCACHE_REFERENCED;
442 list_add(&dentry->d_lru, &dentry_unused);
443 dentry_stat.nr_unused++;
444 spin_unlock(&dentry->d_lock);
445 continue;
448 * If the dentry is not DCACHED_REFERENCED, it is time
449 * to remove it from the dcache, provided the super block is
450 * NULL (which means we are trying to reclaim memory)
451 * or this dentry belongs to the same super block that
452 * we want to shrink.
455 * If this dentry is for "my" filesystem, then I can prune it
456 * without taking the s_umount lock (I already hold it).
458 if (sb && dentry->d_sb == sb) {
459 prune_one_dentry(dentry);
460 continue;
463 * ...otherwise we need to be sure this filesystem isn't being
464 * unmounted, otherwise we could race with
465 * generic_shutdown_super(), and end up holding a reference to
466 * an inode while the filesystem is unmounted.
467 * So we try to get s_umount, and make sure s_root isn't NULL.
468 * (Take a local copy of s_umount to avoid a use-after-free of
469 * `dentry').
471 s_umount = &dentry->d_sb->s_umount;
472 if (down_read_trylock(s_umount)) {
473 if (dentry->d_sb->s_root != NULL) {
474 prune_one_dentry(dentry);
475 up_read(s_umount);
476 continue;
478 up_read(s_umount);
480 spin_unlock(&dentry->d_lock);
482 * Insert dentry at the head of the list as inserting at the
483 * tail leads to a cycle.
485 list_add(&dentry->d_lru, &dentry_unused);
486 dentry_stat.nr_unused++;
488 spin_unlock(&dcache_lock);
492 * Shrink the dcache for the specified super block.
493 * This allows us to unmount a device without disturbing
494 * the dcache for the other devices.
496 * This implementation makes just two traversals of the
497 * unused list. On the first pass we move the selected
498 * dentries to the most recent end, and on the second
499 * pass we free them. The second pass must restart after
500 * each dput(), but since the target dentries are all at
501 * the end, it's really just a single traversal.
505 * shrink_dcache_sb - shrink dcache for a superblock
506 * @sb: superblock
508 * Shrink the dcache for the specified super block. This
509 * is used to free the dcache before unmounting a file
510 * system
513 void shrink_dcache_sb(struct super_block * sb)
515 struct list_head *tmp, *next;
516 struct dentry *dentry;
519 * Pass one ... move the dentries for the specified
520 * superblock to the most recent end of the unused list.
522 spin_lock(&dcache_lock);
523 list_for_each_safe(tmp, next, &dentry_unused) {
524 dentry = list_entry(tmp, struct dentry, d_lru);
525 if (dentry->d_sb != sb)
526 continue;
527 list_move(tmp, &dentry_unused);
531 * Pass two ... free the dentries for this superblock.
533 repeat:
534 list_for_each_safe(tmp, next, &dentry_unused) {
535 dentry = list_entry(tmp, struct dentry, d_lru);
536 if (dentry->d_sb != sb)
537 continue;
538 dentry_stat.nr_unused--;
539 list_del_init(tmp);
540 spin_lock(&dentry->d_lock);
541 if (atomic_read(&dentry->d_count)) {
542 spin_unlock(&dentry->d_lock);
543 continue;
545 prune_one_dentry(dentry);
546 cond_resched_lock(&dcache_lock);
547 goto repeat;
549 spin_unlock(&dcache_lock);
553 * destroy a single subtree of dentries for unmount
554 * - see the comments on shrink_dcache_for_umount() for a description of the
555 * locking
557 static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
559 struct dentry *parent;
560 unsigned detached = 0;
562 BUG_ON(!IS_ROOT(dentry));
564 /* detach this root from the system */
565 spin_lock(&dcache_lock);
566 if (!list_empty(&dentry->d_lru)) {
567 dentry_stat.nr_unused--;
568 list_del_init(&dentry->d_lru);
570 __d_drop(dentry);
571 spin_unlock(&dcache_lock);
573 for (;;) {
574 /* descend to the first leaf in the current subtree */
575 while (!list_empty(&dentry->d_subdirs)) {
576 struct dentry *loop;
578 /* this is a branch with children - detach all of them
579 * from the system in one go */
580 spin_lock(&dcache_lock);
581 list_for_each_entry(loop, &dentry->d_subdirs,
582 d_u.d_child) {
583 if (!list_empty(&loop->d_lru)) {
584 dentry_stat.nr_unused--;
585 list_del_init(&loop->d_lru);
588 __d_drop(loop);
589 cond_resched_lock(&dcache_lock);
591 spin_unlock(&dcache_lock);
593 /* move to the first child */
594 dentry = list_entry(dentry->d_subdirs.next,
595 struct dentry, d_u.d_child);
598 /* consume the dentries from this leaf up through its parents
599 * until we find one with children or run out altogether */
600 do {
601 struct inode *inode;
603 if (atomic_read(&dentry->d_count) != 0) {
604 printk(KERN_ERR
605 "BUG: Dentry %p{i=%lx,n=%s}"
606 " still in use (%d)"
607 " [unmount of %s %s]\n",
608 dentry,
609 dentry->d_inode ?
610 dentry->d_inode->i_ino : 0UL,
611 dentry->d_name.name,
612 atomic_read(&dentry->d_count),
613 dentry->d_sb->s_type->name,
614 dentry->d_sb->s_id);
615 BUG();
618 parent = dentry->d_parent;
619 if (parent == dentry)
620 parent = NULL;
621 else
622 atomic_dec(&parent->d_count);
624 list_del(&dentry->d_u.d_child);
625 detached++;
627 inode = dentry->d_inode;
628 if (inode) {
629 dentry->d_inode = NULL;
630 list_del_init(&dentry->d_alias);
631 if (dentry->d_op && dentry->d_op->d_iput)
632 dentry->d_op->d_iput(dentry, inode);
633 else
634 iput(inode);
637 d_free(dentry);
639 /* finished when we fall off the top of the tree,
640 * otherwise we ascend to the parent and move to the
641 * next sibling if there is one */
642 if (!parent)
643 goto out;
645 dentry = parent;
647 } while (list_empty(&dentry->d_subdirs));
649 dentry = list_entry(dentry->d_subdirs.next,
650 struct dentry, d_u.d_child);
652 out:
653 /* several dentries were freed, need to correct nr_dentry */
654 spin_lock(&dcache_lock);
655 dentry_stat.nr_dentry -= detached;
656 spin_unlock(&dcache_lock);
660 * destroy the dentries attached to a superblock on unmounting
661 * - we don't need to use dentry->d_lock, and only need dcache_lock when
662 * removing the dentry from the system lists and hashes because:
663 * - the superblock is detached from all mountings and open files, so the
664 * dentry trees will not be rearranged by the VFS
665 * - s_umount is write-locked, so the memory pressure shrinker will ignore
666 * any dentries belonging to this superblock that it comes across
667 * - the filesystem itself is no longer permitted to rearrange the dentries
668 * in this superblock
670 void shrink_dcache_for_umount(struct super_block *sb)
672 struct dentry *dentry;
674 if (down_read_trylock(&sb->s_umount))
675 BUG();
677 dentry = sb->s_root;
678 sb->s_root = NULL;
679 atomic_dec(&dentry->d_count);
680 shrink_dcache_for_umount_subtree(dentry);
682 while (!hlist_empty(&sb->s_anon)) {
683 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
684 shrink_dcache_for_umount_subtree(dentry);
689 * Search for at least 1 mount point in the dentry's subdirs.
690 * We descend to the next level whenever the d_subdirs
691 * list is non-empty and continue searching.
695 * have_submounts - check for mounts over a dentry
696 * @parent: dentry to check.
698 * Return true if the parent or its subdirectories contain
699 * a mount point
702 int have_submounts(struct dentry *parent)
704 struct dentry *this_parent = parent;
705 struct list_head *next;
707 spin_lock(&dcache_lock);
708 if (d_mountpoint(parent))
709 goto positive;
710 repeat:
711 next = this_parent->d_subdirs.next;
712 resume:
713 while (next != &this_parent->d_subdirs) {
714 struct list_head *tmp = next;
715 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
716 next = tmp->next;
717 /* Have we found a mount point ? */
718 if (d_mountpoint(dentry))
719 goto positive;
720 if (!list_empty(&dentry->d_subdirs)) {
721 this_parent = dentry;
722 goto repeat;
726 * All done at this level ... ascend and resume the search.
728 if (this_parent != parent) {
729 next = this_parent->d_u.d_child.next;
730 this_parent = this_parent->d_parent;
731 goto resume;
733 spin_unlock(&dcache_lock);
734 return 0; /* No mount points found in tree */
735 positive:
736 spin_unlock(&dcache_lock);
737 return 1;
741 * Search the dentry child list for the specified parent,
742 * and move any unused dentries to the end of the unused
743 * list for prune_dcache(). We descend to the next level
744 * whenever the d_subdirs list is non-empty and continue
745 * searching.
747 * It returns zero iff there are no unused children,
748 * otherwise it returns the number of children moved to
749 * the end of the unused list. This may not be the total
750 * number of unused children, because select_parent can
751 * drop the lock and return early due to latency
752 * constraints.
754 static int select_parent(struct dentry * parent)
756 struct dentry *this_parent = parent;
757 struct list_head *next;
758 int found = 0;
760 spin_lock(&dcache_lock);
761 repeat:
762 next = this_parent->d_subdirs.next;
763 resume:
764 while (next != &this_parent->d_subdirs) {
765 struct list_head *tmp = next;
766 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
767 next = tmp->next;
769 if (!list_empty(&dentry->d_lru)) {
770 dentry_stat.nr_unused--;
771 list_del_init(&dentry->d_lru);
774 * move only zero ref count dentries to the end
775 * of the unused list for prune_dcache
777 if (!atomic_read(&dentry->d_count)) {
778 list_add_tail(&dentry->d_lru, &dentry_unused);
779 dentry_stat.nr_unused++;
780 found++;
784 * We can return to the caller if we have found some (this
785 * ensures forward progress). We'll be coming back to find
786 * the rest.
788 if (found && need_resched())
789 goto out;
792 * Descend a level if the d_subdirs list is non-empty.
794 if (!list_empty(&dentry->d_subdirs)) {
795 this_parent = dentry;
796 goto repeat;
800 * All done at this level ... ascend and resume the search.
802 if (this_parent != parent) {
803 next = this_parent->d_u.d_child.next;
804 this_parent = this_parent->d_parent;
805 goto resume;
807 out:
808 spin_unlock(&dcache_lock);
809 return found;
813 * shrink_dcache_parent - prune dcache
814 * @parent: parent of entries to prune
816 * Prune the dcache to remove unused children of the parent dentry.
819 void shrink_dcache_parent(struct dentry * parent)
821 int found;
823 while ((found = select_parent(parent)) != 0)
824 prune_dcache(found, parent->d_sb);
828 * Scan `nr' dentries and return the number which remain.
830 * We need to avoid reentering the filesystem if the caller is performing a
831 * GFP_NOFS allocation attempt. One example deadlock is:
833 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
834 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
835 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
837 * In this case we return -1 to tell the caller that we baled.
839 static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
841 if (nr) {
842 if (!(gfp_mask & __GFP_FS))
843 return -1;
844 prune_dcache(nr, NULL);
846 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
850 * d_alloc - allocate a dcache entry
851 * @parent: parent of entry to allocate
852 * @name: qstr of the name
854 * Allocates a dentry. It returns %NULL if there is insufficient memory
855 * available. On a success the dentry is returned. The name passed in is
856 * copied and the copy passed in may be reused after this call.
859 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
861 struct dentry *dentry;
862 char *dname;
864 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
865 if (!dentry)
866 return NULL;
868 if (name->len > DNAME_INLINE_LEN-1) {
869 dname = kmalloc(name->len + 1, GFP_KERNEL);
870 if (!dname) {
871 kmem_cache_free(dentry_cache, dentry);
872 return NULL;
874 } else {
875 dname = dentry->d_iname;
877 dentry->d_name.name = dname;
879 dentry->d_name.len = name->len;
880 dentry->d_name.hash = name->hash;
881 memcpy(dname, name->name, name->len);
882 dname[name->len] = 0;
884 atomic_set(&dentry->d_count, 1);
885 dentry->d_flags = DCACHE_UNHASHED;
886 spin_lock_init(&dentry->d_lock);
887 dentry->d_inode = NULL;
888 dentry->d_parent = NULL;
889 dentry->d_sb = NULL;
890 dentry->d_op = NULL;
891 dentry->d_fsdata = NULL;
892 dentry->d_mounted = 0;
893 #ifdef CONFIG_PROFILING
894 dentry->d_cookie = NULL;
895 #endif
896 INIT_HLIST_NODE(&dentry->d_hash);
897 INIT_LIST_HEAD(&dentry->d_lru);
898 INIT_LIST_HEAD(&dentry->d_subdirs);
899 INIT_LIST_HEAD(&dentry->d_alias);
901 if (parent) {
902 dentry->d_parent = dget(parent);
903 dentry->d_sb = parent->d_sb;
904 } else {
905 INIT_LIST_HEAD(&dentry->d_u.d_child);
908 spin_lock(&dcache_lock);
909 if (parent)
910 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
911 dentry_stat.nr_dentry++;
912 spin_unlock(&dcache_lock);
914 return dentry;
917 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
919 struct qstr q;
921 q.name = name;
922 q.len = strlen(name);
923 q.hash = full_name_hash(q.name, q.len);
924 return d_alloc(parent, &q);
928 * d_instantiate - fill in inode information for a dentry
929 * @entry: dentry to complete
930 * @inode: inode to attach to this dentry
932 * Fill in inode information in the entry.
934 * This turns negative dentries into productive full members
935 * of society.
937 * NOTE! This assumes that the inode count has been incremented
938 * (or otherwise set) by the caller to indicate that it is now
939 * in use by the dcache.
942 void d_instantiate(struct dentry *entry, struct inode * inode)
944 BUG_ON(!list_empty(&entry->d_alias));
945 spin_lock(&dcache_lock);
946 if (inode)
947 list_add(&entry->d_alias, &inode->i_dentry);
948 entry->d_inode = inode;
949 fsnotify_d_instantiate(entry, inode);
950 spin_unlock(&dcache_lock);
951 security_d_instantiate(entry, inode);
955 * d_instantiate_unique - instantiate a non-aliased dentry
956 * @entry: dentry to instantiate
957 * @inode: inode to attach to this dentry
959 * Fill in inode information in the entry. On success, it returns NULL.
960 * If an unhashed alias of "entry" already exists, then we return the
961 * aliased dentry instead and drop one reference to inode.
963 * Note that in order to avoid conflicts with rename() etc, the caller
964 * had better be holding the parent directory semaphore.
966 * This also assumes that the inode count has been incremented
967 * (or otherwise set) by the caller to indicate that it is now
968 * in use by the dcache.
970 static struct dentry *__d_instantiate_unique(struct dentry *entry,
971 struct inode *inode)
973 struct dentry *alias;
974 int len = entry->d_name.len;
975 const char *name = entry->d_name.name;
976 unsigned int hash = entry->d_name.hash;
978 if (!inode) {
979 entry->d_inode = NULL;
980 return NULL;
983 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
984 struct qstr *qstr = &alias->d_name;
986 if (qstr->hash != hash)
987 continue;
988 if (alias->d_parent != entry->d_parent)
989 continue;
990 if (qstr->len != len)
991 continue;
992 if (memcmp(qstr->name, name, len))
993 continue;
994 dget_locked(alias);
995 return alias;
998 list_add(&entry->d_alias, &inode->i_dentry);
999 entry->d_inode = inode;
1000 fsnotify_d_instantiate(entry, inode);
1001 return NULL;
1004 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1006 struct dentry *result;
1008 BUG_ON(!list_empty(&entry->d_alias));
1010 spin_lock(&dcache_lock);
1011 result = __d_instantiate_unique(entry, inode);
1012 spin_unlock(&dcache_lock);
1014 if (!result) {
1015 security_d_instantiate(entry, inode);
1016 return NULL;
1019 BUG_ON(!d_unhashed(result));
1020 iput(inode);
1021 return result;
1024 EXPORT_SYMBOL(d_instantiate_unique);
1027 * d_alloc_root - allocate root dentry
1028 * @root_inode: inode to allocate the root for
1030 * Allocate a root ("/") dentry for the inode given. The inode is
1031 * instantiated and returned. %NULL is returned if there is insufficient
1032 * memory or the inode passed is %NULL.
1035 struct dentry * d_alloc_root(struct inode * root_inode)
1037 struct dentry *res = NULL;
1039 if (root_inode) {
1040 static const struct qstr name = { .name = "/", .len = 1 };
1042 res = d_alloc(NULL, &name);
1043 if (res) {
1044 res->d_sb = root_inode->i_sb;
1045 res->d_parent = res;
1046 d_instantiate(res, root_inode);
1049 return res;
1052 static inline struct hlist_head *d_hash(struct dentry *parent,
1053 unsigned long hash)
1055 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1056 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1057 return dentry_hashtable + (hash & D_HASHMASK);
1061 * d_alloc_anon - allocate an anonymous dentry
1062 * @inode: inode to allocate the dentry for
1064 * This is similar to d_alloc_root. It is used by filesystems when
1065 * creating a dentry for a given inode, often in the process of
1066 * mapping a filehandle to a dentry. The returned dentry may be
1067 * anonymous, or may have a full name (if the inode was already
1068 * in the cache). The file system may need to make further
1069 * efforts to connect this dentry into the dcache properly.
1071 * When called on a directory inode, we must ensure that
1072 * the inode only ever has one dentry. If a dentry is
1073 * found, that is returned instead of allocating a new one.
1075 * On successful return, the reference to the inode has been transferred
1076 * to the dentry. If %NULL is returned (indicating kmalloc failure),
1077 * the reference on the inode has not been released.
1080 struct dentry * d_alloc_anon(struct inode *inode)
1082 static const struct qstr anonstring = { .name = "" };
1083 struct dentry *tmp;
1084 struct dentry *res;
1086 if ((res = d_find_alias(inode))) {
1087 iput(inode);
1088 return res;
1091 tmp = d_alloc(NULL, &anonstring);
1092 if (!tmp)
1093 return NULL;
1095 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1097 spin_lock(&dcache_lock);
1098 res = __d_find_alias(inode, 0);
1099 if (!res) {
1100 /* attach a disconnected dentry */
1101 res = tmp;
1102 tmp = NULL;
1103 spin_lock(&res->d_lock);
1104 res->d_sb = inode->i_sb;
1105 res->d_parent = res;
1106 res->d_inode = inode;
1107 res->d_flags |= DCACHE_DISCONNECTED;
1108 res->d_flags &= ~DCACHE_UNHASHED;
1109 list_add(&res->d_alias, &inode->i_dentry);
1110 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
1111 spin_unlock(&res->d_lock);
1113 inode = NULL; /* don't drop reference */
1115 spin_unlock(&dcache_lock);
1117 if (inode)
1118 iput(inode);
1119 if (tmp)
1120 dput(tmp);
1121 return res;
1126 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1127 * @inode: the inode which may have a disconnected dentry
1128 * @dentry: a negative dentry which we want to point to the inode.
1130 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1131 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1132 * and return it, else simply d_add the inode to the dentry and return NULL.
1134 * This is needed in the lookup routine of any filesystem that is exportable
1135 * (via knfsd) so that we can build dcache paths to directories effectively.
1137 * If a dentry was found and moved, then it is returned. Otherwise NULL
1138 * is returned. This matches the expected return value of ->lookup.
1141 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1143 struct dentry *new = NULL;
1145 if (inode && S_ISDIR(inode->i_mode)) {
1146 spin_lock(&dcache_lock);
1147 new = __d_find_alias(inode, 1);
1148 if (new) {
1149 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1150 fsnotify_d_instantiate(new, inode);
1151 spin_unlock(&dcache_lock);
1152 security_d_instantiate(new, inode);
1153 d_rehash(dentry);
1154 d_move(new, dentry);
1155 iput(inode);
1156 } else {
1157 /* d_instantiate takes dcache_lock, so we do it by hand */
1158 list_add(&dentry->d_alias, &inode->i_dentry);
1159 dentry->d_inode = inode;
1160 fsnotify_d_instantiate(dentry, inode);
1161 spin_unlock(&dcache_lock);
1162 security_d_instantiate(dentry, inode);
1163 d_rehash(dentry);
1165 } else
1166 d_add(dentry, inode);
1167 return new;
1172 * d_lookup - search for a dentry
1173 * @parent: parent dentry
1174 * @name: qstr of name we wish to find
1176 * Searches the children of the parent dentry for the name in question. If
1177 * the dentry is found its reference count is incremented and the dentry
1178 * is returned. The caller must use d_put to free the entry when it has
1179 * finished using it. %NULL is returned on failure.
1181 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1182 * Memory barriers are used while updating and doing lockless traversal.
1183 * To avoid races with d_move while rename is happening, d_lock is used.
1185 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1186 * and name pointer in one structure pointed by d_qstr.
1188 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1189 * lookup is going on.
1191 * dentry_unused list is not updated even if lookup finds the required dentry
1192 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1193 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1194 * acquisition.
1196 * d_lookup() is protected against the concurrent renames in some unrelated
1197 * directory using the seqlockt_t rename_lock.
1200 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1202 struct dentry * dentry = NULL;
1203 unsigned long seq;
1205 do {
1206 seq = read_seqbegin(&rename_lock);
1207 dentry = __d_lookup(parent, name);
1208 if (dentry)
1209 break;
1210 } while (read_seqretry(&rename_lock, seq));
1211 return dentry;
1214 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1216 unsigned int len = name->len;
1217 unsigned int hash = name->hash;
1218 const unsigned char *str = name->name;
1219 struct hlist_head *head = d_hash(parent,hash);
1220 struct dentry *found = NULL;
1221 struct hlist_node *node;
1222 struct dentry *dentry;
1224 rcu_read_lock();
1226 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1227 struct qstr *qstr;
1229 if (dentry->d_name.hash != hash)
1230 continue;
1231 if (dentry->d_parent != parent)
1232 continue;
1234 spin_lock(&dentry->d_lock);
1237 * Recheck the dentry after taking the lock - d_move may have
1238 * changed things. Don't bother checking the hash because we're
1239 * about to compare the whole name anyway.
1241 if (dentry->d_parent != parent)
1242 goto next;
1245 * It is safe to compare names since d_move() cannot
1246 * change the qstr (protected by d_lock).
1248 qstr = &dentry->d_name;
1249 if (parent->d_op && parent->d_op->d_compare) {
1250 if (parent->d_op->d_compare(parent, qstr, name))
1251 goto next;
1252 } else {
1253 if (qstr->len != len)
1254 goto next;
1255 if (memcmp(qstr->name, str, len))
1256 goto next;
1259 if (!d_unhashed(dentry)) {
1260 atomic_inc(&dentry->d_count);
1261 found = dentry;
1263 spin_unlock(&dentry->d_lock);
1264 break;
1265 next:
1266 spin_unlock(&dentry->d_lock);
1268 rcu_read_unlock();
1270 return found;
1274 * d_hash_and_lookup - hash the qstr then search for a dentry
1275 * @dir: Directory to search in
1276 * @name: qstr of name we wish to find
1278 * On hash failure or on lookup failure NULL is returned.
1280 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1282 struct dentry *dentry = NULL;
1285 * Check for a fs-specific hash function. Note that we must
1286 * calculate the standard hash first, as the d_op->d_hash()
1287 * routine may choose to leave the hash value unchanged.
1289 name->hash = full_name_hash(name->name, name->len);
1290 if (dir->d_op && dir->d_op->d_hash) {
1291 if (dir->d_op->d_hash(dir, name) < 0)
1292 goto out;
1294 dentry = d_lookup(dir, name);
1295 out:
1296 return dentry;
1300 * d_validate - verify dentry provided from insecure source
1301 * @dentry: The dentry alleged to be valid child of @dparent
1302 * @dparent: The parent dentry (known to be valid)
1303 * @hash: Hash of the dentry
1304 * @len: Length of the name
1306 * An insecure source has sent us a dentry, here we verify it and dget() it.
1307 * This is used by ncpfs in its readdir implementation.
1308 * Zero is returned in the dentry is invalid.
1311 int d_validate(struct dentry *dentry, struct dentry *dparent)
1313 struct hlist_head *base;
1314 struct hlist_node *lhp;
1316 /* Check whether the ptr might be valid at all.. */
1317 if (!kmem_ptr_validate(dentry_cache, dentry))
1318 goto out;
1320 if (dentry->d_parent != dparent)
1321 goto out;
1323 spin_lock(&dcache_lock);
1324 base = d_hash(dparent, dentry->d_name.hash);
1325 hlist_for_each(lhp,base) {
1326 /* hlist_for_each_entry_rcu() not required for d_hash list
1327 * as it is parsed under dcache_lock
1329 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1330 __dget_locked(dentry);
1331 spin_unlock(&dcache_lock);
1332 return 1;
1335 spin_unlock(&dcache_lock);
1336 out:
1337 return 0;
1341 * When a file is deleted, we have two options:
1342 * - turn this dentry into a negative dentry
1343 * - unhash this dentry and free it.
1345 * Usually, we want to just turn this into
1346 * a negative dentry, but if anybody else is
1347 * currently using the dentry or the inode
1348 * we can't do that and we fall back on removing
1349 * it from the hash queues and waiting for
1350 * it to be deleted later when it has no users
1354 * d_delete - delete a dentry
1355 * @dentry: The dentry to delete
1357 * Turn the dentry into a negative dentry if possible, otherwise
1358 * remove it from the hash queues so it can be deleted later
1361 void d_delete(struct dentry * dentry)
1363 int isdir = 0;
1365 * Are we the only user?
1367 spin_lock(&dcache_lock);
1368 spin_lock(&dentry->d_lock);
1369 isdir = S_ISDIR(dentry->d_inode->i_mode);
1370 if (atomic_read(&dentry->d_count) == 1) {
1371 dentry_iput(dentry);
1372 fsnotify_nameremove(dentry, isdir);
1374 /* remove this and other inotify debug checks after 2.6.18 */
1375 dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
1376 return;
1379 if (!d_unhashed(dentry))
1380 __d_drop(dentry);
1382 spin_unlock(&dentry->d_lock);
1383 spin_unlock(&dcache_lock);
1385 fsnotify_nameremove(dentry, isdir);
1388 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1391 entry->d_flags &= ~DCACHE_UNHASHED;
1392 hlist_add_head_rcu(&entry->d_hash, list);
1395 static void _d_rehash(struct dentry * entry)
1397 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1401 * d_rehash - add an entry back to the hash
1402 * @entry: dentry to add to the hash
1404 * Adds a dentry to the hash according to its name.
1407 void d_rehash(struct dentry * entry)
1409 spin_lock(&dcache_lock);
1410 spin_lock(&entry->d_lock);
1411 _d_rehash(entry);
1412 spin_unlock(&entry->d_lock);
1413 spin_unlock(&dcache_lock);
1416 #define do_switch(x,y) do { \
1417 __typeof__ (x) __tmp = x; \
1418 x = y; y = __tmp; } while (0)
1421 * When switching names, the actual string doesn't strictly have to
1422 * be preserved in the target - because we're dropping the target
1423 * anyway. As such, we can just do a simple memcpy() to copy over
1424 * the new name before we switch.
1426 * Note that we have to be a lot more careful about getting the hash
1427 * switched - we have to switch the hash value properly even if it
1428 * then no longer matches the actual (corrupted) string of the target.
1429 * The hash value has to match the hash queue that the dentry is on..
1431 static void switch_names(struct dentry *dentry, struct dentry *target)
1433 if (dname_external(target)) {
1434 if (dname_external(dentry)) {
1436 * Both external: swap the pointers
1438 do_switch(target->d_name.name, dentry->d_name.name);
1439 } else {
1441 * dentry:internal, target:external. Steal target's
1442 * storage and make target internal.
1444 dentry->d_name.name = target->d_name.name;
1445 target->d_name.name = target->d_iname;
1447 } else {
1448 if (dname_external(dentry)) {
1450 * dentry:external, target:internal. Give dentry's
1451 * storage to target and make dentry internal
1453 memcpy(dentry->d_iname, target->d_name.name,
1454 target->d_name.len + 1);
1455 target->d_name.name = dentry->d_name.name;
1456 dentry->d_name.name = dentry->d_iname;
1457 } else {
1459 * Both are internal. Just copy target to dentry
1461 memcpy(dentry->d_iname, target->d_name.name,
1462 target->d_name.len + 1);
1468 * We cannibalize "target" when moving dentry on top of it,
1469 * because it's going to be thrown away anyway. We could be more
1470 * polite about it, though.
1472 * This forceful removal will result in ugly /proc output if
1473 * somebody holds a file open that got deleted due to a rename.
1474 * We could be nicer about the deleted file, and let it show
1475 * up under the name it got deleted rather than the name that
1476 * deleted it.
1480 * d_move_locked - move a dentry
1481 * @dentry: entry to move
1482 * @target: new dentry
1484 * Update the dcache to reflect the move of a file name. Negative
1485 * dcache entries should not be moved in this way.
1487 static void d_move_locked(struct dentry * dentry, struct dentry * target)
1489 struct hlist_head *list;
1491 if (!dentry->d_inode)
1492 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1494 write_seqlock(&rename_lock);
1496 * XXXX: do we really need to take target->d_lock?
1498 if (target < dentry) {
1499 spin_lock(&target->d_lock);
1500 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1501 } else {
1502 spin_lock(&dentry->d_lock);
1503 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
1506 /* Move the dentry to the target hash queue, if on different bucket */
1507 if (dentry->d_flags & DCACHE_UNHASHED)
1508 goto already_unhashed;
1510 hlist_del_rcu(&dentry->d_hash);
1512 already_unhashed:
1513 list = d_hash(target->d_parent, target->d_name.hash);
1514 __d_rehash(dentry, list);
1516 /* Unhash the target: dput() will then get rid of it */
1517 __d_drop(target);
1519 list_del(&dentry->d_u.d_child);
1520 list_del(&target->d_u.d_child);
1522 /* Switch the names.. */
1523 switch_names(dentry, target);
1524 do_switch(dentry->d_name.len, target->d_name.len);
1525 do_switch(dentry->d_name.hash, target->d_name.hash);
1527 /* ... and switch the parents */
1528 if (IS_ROOT(dentry)) {
1529 dentry->d_parent = target->d_parent;
1530 target->d_parent = target;
1531 INIT_LIST_HEAD(&target->d_u.d_child);
1532 } else {
1533 do_switch(dentry->d_parent, target->d_parent);
1535 /* And add them back to the (new) parent lists */
1536 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1539 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1540 spin_unlock(&target->d_lock);
1541 fsnotify_d_move(dentry);
1542 spin_unlock(&dentry->d_lock);
1543 write_sequnlock(&rename_lock);
1547 * d_move - move a dentry
1548 * @dentry: entry to move
1549 * @target: new dentry
1551 * Update the dcache to reflect the move of a file name. Negative
1552 * dcache entries should not be moved in this way.
1555 void d_move(struct dentry * dentry, struct dentry * target)
1557 spin_lock(&dcache_lock);
1558 d_move_locked(dentry, target);
1559 spin_unlock(&dcache_lock);
1563 * Helper that returns 1 if p1 is a parent of p2, else 0
1565 static int d_isparent(struct dentry *p1, struct dentry *p2)
1567 struct dentry *p;
1569 for (p = p2; p->d_parent != p; p = p->d_parent) {
1570 if (p->d_parent == p1)
1571 return 1;
1573 return 0;
1577 * This helper attempts to cope with remotely renamed directories
1579 * It assumes that the caller is already holding
1580 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1582 * Note: If ever the locking in lock_rename() changes, then please
1583 * remember to update this too...
1585 * On return, dcache_lock will have been unlocked.
1587 static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1589 struct mutex *m1 = NULL, *m2 = NULL;
1590 struct dentry *ret;
1592 /* If alias and dentry share a parent, then no extra locks required */
1593 if (alias->d_parent == dentry->d_parent)
1594 goto out_unalias;
1596 /* Check for loops */
1597 ret = ERR_PTR(-ELOOP);
1598 if (d_isparent(alias, dentry))
1599 goto out_err;
1601 /* See lock_rename() */
1602 ret = ERR_PTR(-EBUSY);
1603 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1604 goto out_err;
1605 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1606 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1607 goto out_err;
1608 m2 = &alias->d_parent->d_inode->i_mutex;
1609 out_unalias:
1610 d_move_locked(alias, dentry);
1611 ret = alias;
1612 out_err:
1613 spin_unlock(&dcache_lock);
1614 if (m2)
1615 mutex_unlock(m2);
1616 if (m1)
1617 mutex_unlock(m1);
1618 return ret;
1622 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1623 * named dentry in place of the dentry to be replaced.
1625 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1627 struct dentry *dparent, *aparent;
1629 switch_names(dentry, anon);
1630 do_switch(dentry->d_name.len, anon->d_name.len);
1631 do_switch(dentry->d_name.hash, anon->d_name.hash);
1633 dparent = dentry->d_parent;
1634 aparent = anon->d_parent;
1636 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1637 list_del(&dentry->d_u.d_child);
1638 if (!IS_ROOT(dentry))
1639 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1640 else
1641 INIT_LIST_HEAD(&dentry->d_u.d_child);
1643 anon->d_parent = (dparent == dentry) ? anon : dparent;
1644 list_del(&anon->d_u.d_child);
1645 if (!IS_ROOT(anon))
1646 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1647 else
1648 INIT_LIST_HEAD(&anon->d_u.d_child);
1650 anon->d_flags &= ~DCACHE_DISCONNECTED;
1654 * d_materialise_unique - introduce an inode into the tree
1655 * @dentry: candidate dentry
1656 * @inode: inode to bind to the dentry, to which aliases may be attached
1658 * Introduces an dentry into the tree, substituting an extant disconnected
1659 * root directory alias in its place if there is one
1661 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1663 struct dentry *actual;
1665 BUG_ON(!d_unhashed(dentry));
1667 spin_lock(&dcache_lock);
1669 if (!inode) {
1670 actual = dentry;
1671 dentry->d_inode = NULL;
1672 goto found_lock;
1675 if (S_ISDIR(inode->i_mode)) {
1676 struct dentry *alias;
1678 /* Does an aliased dentry already exist? */
1679 alias = __d_find_alias(inode, 0);
1680 if (alias) {
1681 actual = alias;
1682 /* Is this an anonymous mountpoint that we could splice
1683 * into our tree? */
1684 if (IS_ROOT(alias)) {
1685 spin_lock(&alias->d_lock);
1686 __d_materialise_dentry(dentry, alias);
1687 __d_drop(alias);
1688 goto found;
1690 /* Nope, but we must(!) avoid directory aliasing */
1691 actual = __d_unalias(dentry, alias);
1692 if (IS_ERR(actual))
1693 dput(alias);
1694 goto out_nolock;
1698 /* Add a unique reference */
1699 actual = __d_instantiate_unique(dentry, inode);
1700 if (!actual)
1701 actual = dentry;
1702 else if (unlikely(!d_unhashed(actual)))
1703 goto shouldnt_be_hashed;
1705 found_lock:
1706 spin_lock(&actual->d_lock);
1707 found:
1708 _d_rehash(actual);
1709 spin_unlock(&actual->d_lock);
1710 spin_unlock(&dcache_lock);
1711 out_nolock:
1712 if (actual == dentry) {
1713 security_d_instantiate(dentry, inode);
1714 return NULL;
1717 iput(inode);
1718 return actual;
1720 shouldnt_be_hashed:
1721 spin_unlock(&dcache_lock);
1722 BUG();
1723 goto shouldnt_be_hashed;
1727 * d_path - return the path of a dentry
1728 * @dentry: dentry to report
1729 * @vfsmnt: vfsmnt to which the dentry belongs
1730 * @root: root dentry
1731 * @rootmnt: vfsmnt to which the root dentry belongs
1732 * @buffer: buffer to return value in
1733 * @buflen: buffer length
1735 * Convert a dentry into an ASCII path name. If the entry has been deleted
1736 * the string " (deleted)" is appended. Note that this is ambiguous.
1738 * Returns the buffer or an error code if the path was too long.
1740 * "buflen" should be positive. Caller holds the dcache_lock.
1742 static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1743 struct dentry *root, struct vfsmount *rootmnt,
1744 char *buffer, int buflen)
1746 char * end = buffer+buflen;
1747 char * retval;
1748 int namelen;
1750 *--end = '\0';
1751 buflen--;
1752 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1753 buflen -= 10;
1754 end -= 10;
1755 if (buflen < 0)
1756 goto Elong;
1757 memcpy(end, " (deleted)", 10);
1760 if (buflen < 1)
1761 goto Elong;
1762 /* Get '/' right */
1763 retval = end-1;
1764 *retval = '/';
1766 for (;;) {
1767 struct dentry * parent;
1769 if (dentry == root && vfsmnt == rootmnt)
1770 break;
1771 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1772 /* Global root? */
1773 spin_lock(&vfsmount_lock);
1774 if (vfsmnt->mnt_parent == vfsmnt) {
1775 spin_unlock(&vfsmount_lock);
1776 goto global_root;
1778 dentry = vfsmnt->mnt_mountpoint;
1779 vfsmnt = vfsmnt->mnt_parent;
1780 spin_unlock(&vfsmount_lock);
1781 continue;
1783 parent = dentry->d_parent;
1784 prefetch(parent);
1785 namelen = dentry->d_name.len;
1786 buflen -= namelen + 1;
1787 if (buflen < 0)
1788 goto Elong;
1789 end -= namelen;
1790 memcpy(end, dentry->d_name.name, namelen);
1791 *--end = '/';
1792 retval = end;
1793 dentry = parent;
1796 return retval;
1798 global_root:
1799 namelen = dentry->d_name.len;
1800 buflen -= namelen;
1801 if (buflen < 0)
1802 goto Elong;
1803 retval -= namelen-1; /* hit the slash */
1804 memcpy(retval, dentry->d_name.name, namelen);
1805 return retval;
1806 Elong:
1807 return ERR_PTR(-ENAMETOOLONG);
1810 /* write full pathname into buffer and return start of pathname */
1811 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1812 char *buf, int buflen)
1814 char *res;
1815 struct vfsmount *rootmnt;
1816 struct dentry *root;
1818 read_lock(&current->fs->lock);
1819 rootmnt = mntget(current->fs->rootmnt);
1820 root = dget(current->fs->root);
1821 read_unlock(&current->fs->lock);
1822 spin_lock(&dcache_lock);
1823 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1824 spin_unlock(&dcache_lock);
1825 dput(root);
1826 mntput(rootmnt);
1827 return res;
1831 * NOTE! The user-level library version returns a
1832 * character pointer. The kernel system call just
1833 * returns the length of the buffer filled (which
1834 * includes the ending '\0' character), or a negative
1835 * error value. So libc would do something like
1837 * char *getcwd(char * buf, size_t size)
1839 * int retval;
1841 * retval = sys_getcwd(buf, size);
1842 * if (retval >= 0)
1843 * return buf;
1844 * errno = -retval;
1845 * return NULL;
1848 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1850 int error;
1851 struct vfsmount *pwdmnt, *rootmnt;
1852 struct dentry *pwd, *root;
1853 char *page = (char *) __get_free_page(GFP_USER);
1855 if (!page)
1856 return -ENOMEM;
1858 read_lock(&current->fs->lock);
1859 pwdmnt = mntget(current->fs->pwdmnt);
1860 pwd = dget(current->fs->pwd);
1861 rootmnt = mntget(current->fs->rootmnt);
1862 root = dget(current->fs->root);
1863 read_unlock(&current->fs->lock);
1865 error = -ENOENT;
1866 /* Has the current directory has been unlinked? */
1867 spin_lock(&dcache_lock);
1868 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1869 unsigned long len;
1870 char * cwd;
1872 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1873 spin_unlock(&dcache_lock);
1875 error = PTR_ERR(cwd);
1876 if (IS_ERR(cwd))
1877 goto out;
1879 error = -ERANGE;
1880 len = PAGE_SIZE + page - cwd;
1881 if (len <= size) {
1882 error = len;
1883 if (copy_to_user(buf, cwd, len))
1884 error = -EFAULT;
1886 } else
1887 spin_unlock(&dcache_lock);
1889 out:
1890 dput(pwd);
1891 mntput(pwdmnt);
1892 dput(root);
1893 mntput(rootmnt);
1894 free_page((unsigned long) page);
1895 return error;
1899 * Test whether new_dentry is a subdirectory of old_dentry.
1901 * Trivially implemented using the dcache structure
1905 * is_subdir - is new dentry a subdirectory of old_dentry
1906 * @new_dentry: new dentry
1907 * @old_dentry: old dentry
1909 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1910 * Returns 0 otherwise.
1911 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1914 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1916 int result;
1917 struct dentry * saved = new_dentry;
1918 unsigned long seq;
1920 /* need rcu_readlock to protect against the d_parent trashing due to
1921 * d_move
1923 rcu_read_lock();
1924 do {
1925 /* for restarting inner loop in case of seq retry */
1926 new_dentry = saved;
1927 result = 0;
1928 seq = read_seqbegin(&rename_lock);
1929 for (;;) {
1930 if (new_dentry != old_dentry) {
1931 struct dentry * parent = new_dentry->d_parent;
1932 if (parent == new_dentry)
1933 break;
1934 new_dentry = parent;
1935 continue;
1937 result = 1;
1938 break;
1940 } while (read_seqretry(&rename_lock, seq));
1941 rcu_read_unlock();
1943 return result;
1946 void d_genocide(struct dentry *root)
1948 struct dentry *this_parent = root;
1949 struct list_head *next;
1951 spin_lock(&dcache_lock);
1952 repeat:
1953 next = this_parent->d_subdirs.next;
1954 resume:
1955 while (next != &this_parent->d_subdirs) {
1956 struct list_head *tmp = next;
1957 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1958 next = tmp->next;
1959 if (d_unhashed(dentry)||!dentry->d_inode)
1960 continue;
1961 if (!list_empty(&dentry->d_subdirs)) {
1962 this_parent = dentry;
1963 goto repeat;
1965 atomic_dec(&dentry->d_count);
1967 if (this_parent != root) {
1968 next = this_parent->d_u.d_child.next;
1969 atomic_dec(&this_parent->d_count);
1970 this_parent = this_parent->d_parent;
1971 goto resume;
1973 spin_unlock(&dcache_lock);
1977 * find_inode_number - check for dentry with name
1978 * @dir: directory to check
1979 * @name: Name to find.
1981 * Check whether a dentry already exists for the given name,
1982 * and return the inode number if it has an inode. Otherwise
1983 * 0 is returned.
1985 * This routine is used to post-process directory listings for
1986 * filesystems using synthetic inode numbers, and is necessary
1987 * to keep getcwd() working.
1990 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1992 struct dentry * dentry;
1993 ino_t ino = 0;
1995 dentry = d_hash_and_lookup(dir, name);
1996 if (dentry) {
1997 if (dentry->d_inode)
1998 ino = dentry->d_inode->i_ino;
1999 dput(dentry);
2001 return ino;
2004 static __initdata unsigned long dhash_entries;
2005 static int __init set_dhash_entries(char *str)
2007 if (!str)
2008 return 0;
2009 dhash_entries = simple_strtoul(str, &str, 0);
2010 return 1;
2012 __setup("dhash_entries=", set_dhash_entries);
2014 static void __init dcache_init_early(void)
2016 int loop;
2018 /* If hashes are distributed across NUMA nodes, defer
2019 * hash allocation until vmalloc space is available.
2021 if (hashdist)
2022 return;
2024 dentry_hashtable =
2025 alloc_large_system_hash("Dentry cache",
2026 sizeof(struct hlist_head),
2027 dhash_entries,
2029 HASH_EARLY,
2030 &d_hash_shift,
2031 &d_hash_mask,
2034 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2035 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2038 static void __init dcache_init(unsigned long mempages)
2040 int loop;
2043 * A constructor could be added for stable state like the lists,
2044 * but it is probably not worth it because of the cache nature
2045 * of the dcache.
2047 dentry_cache = kmem_cache_create("dentry_cache",
2048 sizeof(struct dentry),
2050 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
2051 SLAB_MEM_SPREAD),
2052 NULL, NULL);
2054 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
2056 /* Hash may have been set up in dcache_init_early */
2057 if (!hashdist)
2058 return;
2060 dentry_hashtable =
2061 alloc_large_system_hash("Dentry cache",
2062 sizeof(struct hlist_head),
2063 dhash_entries,
2066 &d_hash_shift,
2067 &d_hash_mask,
2070 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2071 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2074 /* SLAB cache for __getname() consumers */
2075 kmem_cache_t *names_cachep __read_mostly;
2077 /* SLAB cache for file structures */
2078 kmem_cache_t *filp_cachep __read_mostly;
2080 EXPORT_SYMBOL(d_genocide);
2082 void __init vfs_caches_init_early(void)
2084 dcache_init_early();
2085 inode_init_early();
2088 void __init vfs_caches_init(unsigned long mempages)
2090 unsigned long reserve;
2092 /* Base hash sizes on available memory, with a reserve equal to
2093 150% of current kernel size */
2095 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2096 mempages -= reserve;
2098 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2099 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
2101 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
2102 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
2104 dcache_init(mempages);
2105 inode_init(mempages);
2106 files_init(mempages);
2107 mnt_init(mempages);
2108 bdev_cache_init();
2109 chrdev_init();
2112 EXPORT_SYMBOL(d_alloc);
2113 EXPORT_SYMBOL(d_alloc_anon);
2114 EXPORT_SYMBOL(d_alloc_root);
2115 EXPORT_SYMBOL(d_delete);
2116 EXPORT_SYMBOL(d_find_alias);
2117 EXPORT_SYMBOL(d_instantiate);
2118 EXPORT_SYMBOL(d_invalidate);
2119 EXPORT_SYMBOL(d_lookup);
2120 EXPORT_SYMBOL(d_move);
2121 EXPORT_SYMBOL_GPL(d_materialise_unique);
2122 EXPORT_SYMBOL(d_path);
2123 EXPORT_SYMBOL(d_prune_aliases);
2124 EXPORT_SYMBOL(d_rehash);
2125 EXPORT_SYMBOL(d_splice_alias);
2126 EXPORT_SYMBOL(d_validate);
2127 EXPORT_SYMBOL(dget_locked);
2128 EXPORT_SYMBOL(dput);
2129 EXPORT_SYMBOL(find_inode_number);
2130 EXPORT_SYMBOL(have_submounts);
2131 EXPORT_SYMBOL(names_cachep);
2132 EXPORT_SYMBOL(shrink_dcache_parent);
2133 EXPORT_SYMBOL(shrink_dcache_sb);