Merge master.kernel.org:/pub/scm/linux/kernel/git/wim/linux-2.6-watchdog
[linux-2.6/linux-loongson.git] / fs / dcache.c
blob7376b61269fb711c230ec1d0161119ce0672fc36
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/config.h>
18 #include <linux/syscalls.h>
19 #include <linux/string.h>
20 #include <linux/mm.h>
21 #include <linux/fs.h>
22 #include <linux/fsnotify.h>
23 #include <linux/slab.h>
24 #include <linux/init.h>
25 #include <linux/smp_lock.h>
26 #include <linux/hash.h>
27 #include <linux/cache.h>
28 #include <linux/module.h>
29 #include <linux/mount.h>
30 #include <linux/file.h>
31 #include <asm/uaccess.h>
32 #include <linux/security.h>
33 #include <linux/seqlock.h>
34 #include <linux/swap.h>
35 #include <linux/bootmem.h>
37 /* #define DCACHE_DEBUG 1 */
39 int sysctl_vfs_cache_pressure = 100;
40 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
42 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
43 static seqlock_t rename_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
45 EXPORT_SYMBOL(dcache_lock);
47 static kmem_cache_t *dentry_cache;
49 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
52 * This is the single most critical data structure when it comes
53 * to the dcache: the hashtable for lookups. Somebody should try
54 * to make this good - I've just made it work.
56 * This hash-function tries to avoid losing too many bits of hash
57 * information, yet avoid using a prime hash-size or similar.
59 #define D_HASHBITS d_hash_shift
60 #define D_HASHMASK d_hash_mask
62 static unsigned int d_hash_mask;
63 static unsigned int d_hash_shift;
64 static struct hlist_head *dentry_hashtable;
65 static LIST_HEAD(dentry_unused);
67 /* Statistics gathering. */
68 struct dentry_stat_t dentry_stat = {
69 .age_limit = 45,
72 static void d_callback(struct rcu_head *head)
74 struct dentry * dentry = container_of(head, struct dentry, d_rcu);
76 if (dname_external(dentry))
77 kfree(dentry->d_name.name);
78 kmem_cache_free(dentry_cache, dentry);
82 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
83 * inside dcache_lock.
85 static void d_free(struct dentry *dentry)
87 if (dentry->d_op && dentry->d_op->d_release)
88 dentry->d_op->d_release(dentry);
89 call_rcu(&dentry->d_rcu, d_callback);
93 * Release the dentry's inode, using the filesystem
94 * d_iput() operation if defined.
95 * Called with dcache_lock and per dentry lock held, drops both.
97 static inline void dentry_iput(struct dentry * dentry)
99 struct inode *inode = dentry->d_inode;
100 if (inode) {
101 dentry->d_inode = NULL;
102 list_del_init(&dentry->d_alias);
103 spin_unlock(&dentry->d_lock);
104 spin_unlock(&dcache_lock);
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_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 a DCACHE_DISCONNECTED alias, then prefer
295 * any other hashed alias over that one unless @want_discon is set,
296 * in which case only return a 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 (alias->d_flags & DCACHE_DISCONNECTED)
313 discon_alias = alias;
314 else if (!want_discon) {
315 __dget_locked(alias);
316 return alias;
320 if (discon_alias)
321 __dget_locked(discon_alias);
322 return discon_alias;
325 struct dentry * d_find_alias(struct inode *inode)
327 struct dentry *de;
328 spin_lock(&dcache_lock);
329 de = __d_find_alias(inode, 0);
330 spin_unlock(&dcache_lock);
331 return de;
335 * Try to kill dentries associated with this inode.
336 * WARNING: you must own a reference to inode.
338 void d_prune_aliases(struct inode *inode)
340 struct dentry *dentry;
341 restart:
342 spin_lock(&dcache_lock);
343 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
344 spin_lock(&dentry->d_lock);
345 if (!atomic_read(&dentry->d_count)) {
346 __dget_locked(dentry);
347 __d_drop(dentry);
348 spin_unlock(&dentry->d_lock);
349 spin_unlock(&dcache_lock);
350 dput(dentry);
351 goto restart;
353 spin_unlock(&dentry->d_lock);
355 spin_unlock(&dcache_lock);
359 * Throw away a dentry - free the inode, dput the parent.
360 * This requires that the LRU list has already been
361 * removed.
362 * Called with dcache_lock, drops it and then regains.
364 static inline void prune_one_dentry(struct dentry * dentry)
366 struct dentry * parent;
368 __d_drop(dentry);
369 list_del(&dentry->d_child);
370 dentry_stat.nr_dentry--; /* For d_free, below */
371 dentry_iput(dentry);
372 parent = dentry->d_parent;
373 d_free(dentry);
374 if (parent != dentry)
375 dput(parent);
376 spin_lock(&dcache_lock);
380 * prune_dcache - shrink the dcache
381 * @count: number of entries to try and free
383 * Shrink the dcache. This is done when we need
384 * more memory, or simply when we need to unmount
385 * something (at which point we need to unuse
386 * all dentries).
388 * This function may fail to free any resources if
389 * all the dentries are in use.
392 static void prune_dcache(int count)
394 spin_lock(&dcache_lock);
395 for (; count ; count--) {
396 struct dentry *dentry;
397 struct list_head *tmp;
399 cond_resched_lock(&dcache_lock);
401 tmp = dentry_unused.prev;
402 if (tmp == &dentry_unused)
403 break;
404 list_del_init(tmp);
405 prefetch(dentry_unused.prev);
406 dentry_stat.nr_unused--;
407 dentry = list_entry(tmp, struct dentry, d_lru);
409 spin_lock(&dentry->d_lock);
411 * We found an inuse dentry which was not removed from
412 * dentry_unused because of laziness during lookup. Do not free
413 * it - just keep it off the dentry_unused list.
415 if (atomic_read(&dentry->d_count)) {
416 spin_unlock(&dentry->d_lock);
417 continue;
419 /* If the dentry was recently referenced, don't free it. */
420 if (dentry->d_flags & DCACHE_REFERENCED) {
421 dentry->d_flags &= ~DCACHE_REFERENCED;
422 list_add(&dentry->d_lru, &dentry_unused);
423 dentry_stat.nr_unused++;
424 spin_unlock(&dentry->d_lock);
425 continue;
427 prune_one_dentry(dentry);
429 spin_unlock(&dcache_lock);
433 * Shrink the dcache for the specified super block.
434 * This allows us to unmount a device without disturbing
435 * the dcache for the other devices.
437 * This implementation makes just two traversals of the
438 * unused list. On the first pass we move the selected
439 * dentries to the most recent end, and on the second
440 * pass we free them. The second pass must restart after
441 * each dput(), but since the target dentries are all at
442 * the end, it's really just a single traversal.
446 * shrink_dcache_sb - shrink dcache for a superblock
447 * @sb: superblock
449 * Shrink the dcache for the specified super block. This
450 * is used to free the dcache before unmounting a file
451 * system
454 void shrink_dcache_sb(struct super_block * sb)
456 struct list_head *tmp, *next;
457 struct dentry *dentry;
460 * Pass one ... move the dentries for the specified
461 * superblock to the most recent end of the unused list.
463 spin_lock(&dcache_lock);
464 list_for_each_safe(tmp, next, &dentry_unused) {
465 dentry = list_entry(tmp, struct dentry, d_lru);
466 if (dentry->d_sb != sb)
467 continue;
468 list_del(tmp);
469 list_add(tmp, &dentry_unused);
473 * Pass two ... free the dentries for this superblock.
475 repeat:
476 list_for_each_safe(tmp, next, &dentry_unused) {
477 dentry = list_entry(tmp, struct dentry, d_lru);
478 if (dentry->d_sb != sb)
479 continue;
480 dentry_stat.nr_unused--;
481 list_del_init(tmp);
482 spin_lock(&dentry->d_lock);
483 if (atomic_read(&dentry->d_count)) {
484 spin_unlock(&dentry->d_lock);
485 continue;
487 prune_one_dentry(dentry);
488 goto repeat;
490 spin_unlock(&dcache_lock);
494 * Search for at least 1 mount point in the dentry's subdirs.
495 * We descend to the next level whenever the d_subdirs
496 * list is non-empty and continue searching.
500 * have_submounts - check for mounts over a dentry
501 * @parent: dentry to check.
503 * Return true if the parent or its subdirectories contain
504 * a mount point
507 int have_submounts(struct dentry *parent)
509 struct dentry *this_parent = parent;
510 struct list_head *next;
512 spin_lock(&dcache_lock);
513 if (d_mountpoint(parent))
514 goto positive;
515 repeat:
516 next = this_parent->d_subdirs.next;
517 resume:
518 while (next != &this_parent->d_subdirs) {
519 struct list_head *tmp = next;
520 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
521 next = tmp->next;
522 /* Have we found a mount point ? */
523 if (d_mountpoint(dentry))
524 goto positive;
525 if (!list_empty(&dentry->d_subdirs)) {
526 this_parent = dentry;
527 goto repeat;
531 * All done at this level ... ascend and resume the search.
533 if (this_parent != parent) {
534 next = this_parent->d_child.next;
535 this_parent = this_parent->d_parent;
536 goto resume;
538 spin_unlock(&dcache_lock);
539 return 0; /* No mount points found in tree */
540 positive:
541 spin_unlock(&dcache_lock);
542 return 1;
546 * Search the dentry child list for the specified parent,
547 * and move any unused dentries to the end of the unused
548 * list for prune_dcache(). We descend to the next level
549 * whenever the d_subdirs list is non-empty and continue
550 * searching.
552 * It returns zero iff there are no unused children,
553 * otherwise it returns the number of children moved to
554 * the end of the unused list. This may not be the total
555 * number of unused children, because select_parent can
556 * drop the lock and return early due to latency
557 * constraints.
559 static int select_parent(struct dentry * parent)
561 struct dentry *this_parent = parent;
562 struct list_head *next;
563 int found = 0;
565 spin_lock(&dcache_lock);
566 repeat:
567 next = this_parent->d_subdirs.next;
568 resume:
569 while (next != &this_parent->d_subdirs) {
570 struct list_head *tmp = next;
571 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
572 next = tmp->next;
574 if (!list_empty(&dentry->d_lru)) {
575 dentry_stat.nr_unused--;
576 list_del_init(&dentry->d_lru);
579 * move only zero ref count dentries to the end
580 * of the unused list for prune_dcache
582 if (!atomic_read(&dentry->d_count)) {
583 list_add(&dentry->d_lru, dentry_unused.prev);
584 dentry_stat.nr_unused++;
585 found++;
589 * We can return to the caller if we have found some (this
590 * ensures forward progress). We'll be coming back to find
591 * the rest.
593 if (found && need_resched())
594 goto out;
597 * Descend a level if the d_subdirs list is non-empty.
599 if (!list_empty(&dentry->d_subdirs)) {
600 this_parent = dentry;
601 #ifdef DCACHE_DEBUG
602 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
603 dentry->d_parent->d_name.name, dentry->d_name.name, found);
604 #endif
605 goto repeat;
609 * All done at this level ... ascend and resume the search.
611 if (this_parent != parent) {
612 next = this_parent->d_child.next;
613 this_parent = this_parent->d_parent;
614 #ifdef DCACHE_DEBUG
615 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
616 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
617 #endif
618 goto resume;
620 out:
621 spin_unlock(&dcache_lock);
622 return found;
626 * shrink_dcache_parent - prune dcache
627 * @parent: parent of entries to prune
629 * Prune the dcache to remove unused children of the parent dentry.
632 void shrink_dcache_parent(struct dentry * parent)
634 int found;
636 while ((found = select_parent(parent)) != 0)
637 prune_dcache(found);
641 * shrink_dcache_anon - further prune the cache
642 * @head: head of d_hash list of dentries to prune
644 * Prune the dentries that are anonymous
646 * parsing d_hash list does not hlist_for_each_rcu() as it
647 * done under dcache_lock.
650 void shrink_dcache_anon(struct hlist_head *head)
652 struct hlist_node *lp;
653 int found;
654 do {
655 found = 0;
656 spin_lock(&dcache_lock);
657 hlist_for_each(lp, head) {
658 struct dentry *this = hlist_entry(lp, struct dentry, d_hash);
659 if (!list_empty(&this->d_lru)) {
660 dentry_stat.nr_unused--;
661 list_del_init(&this->d_lru);
665 * move only zero ref count dentries to the end
666 * of the unused list for prune_dcache
668 if (!atomic_read(&this->d_count)) {
669 list_add_tail(&this->d_lru, &dentry_unused);
670 dentry_stat.nr_unused++;
671 found++;
674 spin_unlock(&dcache_lock);
675 prune_dcache(found);
676 } while(found);
680 * Scan `nr' dentries and return the number which remain.
682 * We need to avoid reentering the filesystem if the caller is performing a
683 * GFP_NOFS allocation attempt. One example deadlock is:
685 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
686 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
687 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
689 * In this case we return -1 to tell the caller that we baled.
691 static int shrink_dcache_memory(int nr, unsigned int gfp_mask)
693 if (nr) {
694 if (!(gfp_mask & __GFP_FS))
695 return -1;
696 prune_dcache(nr);
698 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
702 * d_alloc - allocate a dcache entry
703 * @parent: parent of entry to allocate
704 * @name: qstr of the name
706 * Allocates a dentry. It returns %NULL if there is insufficient memory
707 * available. On a success the dentry is returned. The name passed in is
708 * copied and the copy passed in may be reused after this call.
711 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
713 struct dentry *dentry;
714 char *dname;
716 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
717 if (!dentry)
718 return NULL;
720 if (name->len > DNAME_INLINE_LEN-1) {
721 dname = kmalloc(name->len + 1, GFP_KERNEL);
722 if (!dname) {
723 kmem_cache_free(dentry_cache, dentry);
724 return NULL;
726 } else {
727 dname = dentry->d_iname;
729 dentry->d_name.name = dname;
731 dentry->d_name.len = name->len;
732 dentry->d_name.hash = name->hash;
733 memcpy(dname, name->name, name->len);
734 dname[name->len] = 0;
736 atomic_set(&dentry->d_count, 1);
737 dentry->d_flags = DCACHE_UNHASHED;
738 spin_lock_init(&dentry->d_lock);
739 dentry->d_inode = NULL;
740 dentry->d_parent = NULL;
741 dentry->d_sb = NULL;
742 dentry->d_op = NULL;
743 dentry->d_fsdata = NULL;
744 dentry->d_mounted = 0;
745 dentry->d_cookie = NULL;
746 INIT_HLIST_NODE(&dentry->d_hash);
747 INIT_LIST_HEAD(&dentry->d_lru);
748 INIT_LIST_HEAD(&dentry->d_subdirs);
749 INIT_LIST_HEAD(&dentry->d_alias);
751 if (parent) {
752 dentry->d_parent = dget(parent);
753 dentry->d_sb = parent->d_sb;
754 } else {
755 INIT_LIST_HEAD(&dentry->d_child);
758 spin_lock(&dcache_lock);
759 if (parent)
760 list_add(&dentry->d_child, &parent->d_subdirs);
761 dentry_stat.nr_dentry++;
762 spin_unlock(&dcache_lock);
764 return dentry;
767 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
769 struct qstr q;
771 q.name = name;
772 q.len = strlen(name);
773 q.hash = full_name_hash(q.name, q.len);
774 return d_alloc(parent, &q);
778 * d_instantiate - fill in inode information for a dentry
779 * @entry: dentry to complete
780 * @inode: inode to attach to this dentry
782 * Fill in inode information in the entry.
784 * This turns negative dentries into productive full members
785 * of society.
787 * NOTE! This assumes that the inode count has been incremented
788 * (or otherwise set) by the caller to indicate that it is now
789 * in use by the dcache.
792 void d_instantiate(struct dentry *entry, struct inode * inode)
794 if (!list_empty(&entry->d_alias)) BUG();
795 spin_lock(&dcache_lock);
796 if (inode)
797 list_add(&entry->d_alias, &inode->i_dentry);
798 entry->d_inode = inode;
799 spin_unlock(&dcache_lock);
800 security_d_instantiate(entry, inode);
804 * d_instantiate_unique - instantiate a non-aliased dentry
805 * @entry: dentry to instantiate
806 * @inode: inode to attach to this dentry
808 * Fill in inode information in the entry. On success, it returns NULL.
809 * If an unhashed alias of "entry" already exists, then we return the
810 * aliased dentry instead.
812 * Note that in order to avoid conflicts with rename() etc, the caller
813 * had better be holding the parent directory semaphore.
815 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
817 struct dentry *alias;
818 int len = entry->d_name.len;
819 const char *name = entry->d_name.name;
820 unsigned int hash = entry->d_name.hash;
822 BUG_ON(!list_empty(&entry->d_alias));
823 spin_lock(&dcache_lock);
824 if (!inode)
825 goto do_negative;
826 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
827 struct qstr *qstr = &alias->d_name;
829 if (qstr->hash != hash)
830 continue;
831 if (alias->d_parent != entry->d_parent)
832 continue;
833 if (qstr->len != len)
834 continue;
835 if (memcmp(qstr->name, name, len))
836 continue;
837 dget_locked(alias);
838 spin_unlock(&dcache_lock);
839 BUG_ON(!d_unhashed(alias));
840 return alias;
842 list_add(&entry->d_alias, &inode->i_dentry);
843 do_negative:
844 entry->d_inode = inode;
845 spin_unlock(&dcache_lock);
846 security_d_instantiate(entry, inode);
847 return NULL;
849 EXPORT_SYMBOL(d_instantiate_unique);
852 * d_alloc_root - allocate root dentry
853 * @root_inode: inode to allocate the root for
855 * Allocate a root ("/") dentry for the inode given. The inode is
856 * instantiated and returned. %NULL is returned if there is insufficient
857 * memory or the inode passed is %NULL.
860 struct dentry * d_alloc_root(struct inode * root_inode)
862 struct dentry *res = NULL;
864 if (root_inode) {
865 static const struct qstr name = { .name = "/", .len = 1 };
867 res = d_alloc(NULL, &name);
868 if (res) {
869 res->d_sb = root_inode->i_sb;
870 res->d_parent = res;
871 d_instantiate(res, root_inode);
874 return res;
877 static inline struct hlist_head *d_hash(struct dentry *parent,
878 unsigned long hash)
880 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
881 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
882 return dentry_hashtable + (hash & D_HASHMASK);
886 * d_alloc_anon - allocate an anonymous dentry
887 * @inode: inode to allocate the dentry for
889 * This is similar to d_alloc_root. It is used by filesystems when
890 * creating a dentry for a given inode, often in the process of
891 * mapping a filehandle to a dentry. The returned dentry may be
892 * anonymous, or may have a full name (if the inode was already
893 * in the cache). The file system may need to make further
894 * efforts to connect this dentry into the dcache properly.
896 * When called on a directory inode, we must ensure that
897 * the inode only ever has one dentry. If a dentry is
898 * found, that is returned instead of allocating a new one.
900 * On successful return, the reference to the inode has been transferred
901 * to the dentry. If %NULL is returned (indicating kmalloc failure),
902 * the reference on the inode has not been released.
905 struct dentry * d_alloc_anon(struct inode *inode)
907 static const struct qstr anonstring = { .name = "" };
908 struct dentry *tmp;
909 struct dentry *res;
911 if ((res = d_find_alias(inode))) {
912 iput(inode);
913 return res;
916 tmp = d_alloc(NULL, &anonstring);
917 if (!tmp)
918 return NULL;
920 tmp->d_parent = tmp; /* make sure dput doesn't croak */
922 spin_lock(&dcache_lock);
923 res = __d_find_alias(inode, 0);
924 if (!res) {
925 /* attach a disconnected dentry */
926 res = tmp;
927 tmp = NULL;
928 spin_lock(&res->d_lock);
929 res->d_sb = inode->i_sb;
930 res->d_parent = res;
931 res->d_inode = inode;
932 res->d_flags |= DCACHE_DISCONNECTED;
933 res->d_flags &= ~DCACHE_UNHASHED;
934 list_add(&res->d_alias, &inode->i_dentry);
935 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
936 spin_unlock(&res->d_lock);
938 inode = NULL; /* don't drop reference */
940 spin_unlock(&dcache_lock);
942 if (inode)
943 iput(inode);
944 if (tmp)
945 dput(tmp);
946 return res;
951 * d_splice_alias - splice a disconnected dentry into the tree if one exists
952 * @inode: the inode which may have a disconnected dentry
953 * @dentry: a negative dentry which we want to point to the inode.
955 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
956 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
957 * and return it, else simply d_add the inode to the dentry and return NULL.
959 * This is needed in the lookup routine of any filesystem that is exportable
960 * (via knfsd) so that we can build dcache paths to directories effectively.
962 * If a dentry was found and moved, then it is returned. Otherwise NULL
963 * is returned. This matches the expected return value of ->lookup.
966 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
968 struct dentry *new = NULL;
970 if (inode) {
971 spin_lock(&dcache_lock);
972 new = __d_find_alias(inode, 1);
973 if (new) {
974 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
975 spin_unlock(&dcache_lock);
976 security_d_instantiate(new, inode);
977 d_rehash(dentry);
978 d_move(new, dentry);
979 iput(inode);
980 } else {
981 /* d_instantiate takes dcache_lock, so we do it by hand */
982 list_add(&dentry->d_alias, &inode->i_dentry);
983 dentry->d_inode = inode;
984 spin_unlock(&dcache_lock);
985 security_d_instantiate(dentry, inode);
986 d_rehash(dentry);
988 } else
989 d_add(dentry, inode);
990 return new;
995 * d_lookup - search for a dentry
996 * @parent: parent dentry
997 * @name: qstr of name we wish to find
999 * Searches the children of the parent dentry for the name in question. If
1000 * the dentry is found its reference count is incremented and the dentry
1001 * is returned. The caller must use d_put to free the entry when it has
1002 * finished using it. %NULL is returned on failure.
1004 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1005 * Memory barriers are used while updating and doing lockless traversal.
1006 * To avoid races with d_move while rename is happening, d_lock is used.
1008 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1009 * and name pointer in one structure pointed by d_qstr.
1011 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1012 * lookup is going on.
1014 * dentry_unused list is not updated even if lookup finds the required dentry
1015 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1016 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1017 * acquisition.
1019 * d_lookup() is protected against the concurrent renames in some unrelated
1020 * directory using the seqlockt_t rename_lock.
1023 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1025 struct dentry * dentry = NULL;
1026 unsigned long seq;
1028 do {
1029 seq = read_seqbegin(&rename_lock);
1030 dentry = __d_lookup(parent, name);
1031 if (dentry)
1032 break;
1033 } while (read_seqretry(&rename_lock, seq));
1034 return dentry;
1037 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1039 unsigned int len = name->len;
1040 unsigned int hash = name->hash;
1041 const unsigned char *str = name->name;
1042 struct hlist_head *head = d_hash(parent,hash);
1043 struct dentry *found = NULL;
1044 struct hlist_node *node;
1046 rcu_read_lock();
1048 hlist_for_each_rcu(node, head) {
1049 struct dentry *dentry;
1050 struct qstr *qstr;
1052 dentry = hlist_entry(node, struct dentry, d_hash);
1054 if (dentry->d_name.hash != hash)
1055 continue;
1056 if (dentry->d_parent != parent)
1057 continue;
1059 spin_lock(&dentry->d_lock);
1062 * Recheck the dentry after taking the lock - d_move may have
1063 * changed things. Don't bother checking the hash because we're
1064 * about to compare the whole name anyway.
1066 if (dentry->d_parent != parent)
1067 goto next;
1070 * It is safe to compare names since d_move() cannot
1071 * change the qstr (protected by d_lock).
1073 qstr = &dentry->d_name;
1074 if (parent->d_op && parent->d_op->d_compare) {
1075 if (parent->d_op->d_compare(parent, qstr, name))
1076 goto next;
1077 } else {
1078 if (qstr->len != len)
1079 goto next;
1080 if (memcmp(qstr->name, str, len))
1081 goto next;
1084 if (!d_unhashed(dentry)) {
1085 atomic_inc(&dentry->d_count);
1086 found = dentry;
1088 spin_unlock(&dentry->d_lock);
1089 break;
1090 next:
1091 spin_unlock(&dentry->d_lock);
1093 rcu_read_unlock();
1095 return found;
1099 * d_validate - verify dentry provided from insecure source
1100 * @dentry: The dentry alleged to be valid child of @dparent
1101 * @dparent: The parent dentry (known to be valid)
1102 * @hash: Hash of the dentry
1103 * @len: Length of the name
1105 * An insecure source has sent us a dentry, here we verify it and dget() it.
1106 * This is used by ncpfs in its readdir implementation.
1107 * Zero is returned in the dentry is invalid.
1110 int d_validate(struct dentry *dentry, struct dentry *dparent)
1112 struct hlist_head *base;
1113 struct hlist_node *lhp;
1115 /* Check whether the ptr might be valid at all.. */
1116 if (!kmem_ptr_validate(dentry_cache, dentry))
1117 goto out;
1119 if (dentry->d_parent != dparent)
1120 goto out;
1122 spin_lock(&dcache_lock);
1123 base = d_hash(dparent, dentry->d_name.hash);
1124 hlist_for_each(lhp,base) {
1125 /* hlist_for_each_rcu() not required for d_hash list
1126 * as it is parsed under dcache_lock
1128 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1129 __dget_locked(dentry);
1130 spin_unlock(&dcache_lock);
1131 return 1;
1134 spin_unlock(&dcache_lock);
1135 out:
1136 return 0;
1140 * When a file is deleted, we have two options:
1141 * - turn this dentry into a negative dentry
1142 * - unhash this dentry and free it.
1144 * Usually, we want to just turn this into
1145 * a negative dentry, but if anybody else is
1146 * currently using the dentry or the inode
1147 * we can't do that and we fall back on removing
1148 * it from the hash queues and waiting for
1149 * it to be deleted later when it has no users
1153 * d_delete - delete a dentry
1154 * @dentry: The dentry to delete
1156 * Turn the dentry into a negative dentry if possible, otherwise
1157 * remove it from the hash queues so it can be deleted later
1160 void d_delete(struct dentry * dentry)
1162 int isdir = 0;
1164 * Are we the only user?
1166 spin_lock(&dcache_lock);
1167 spin_lock(&dentry->d_lock);
1168 isdir = S_ISDIR(dentry->d_inode->i_mode);
1169 if (atomic_read(&dentry->d_count) == 1) {
1170 dentry_iput(dentry);
1171 fsnotify_nameremove(dentry, isdir);
1172 return;
1175 if (!d_unhashed(dentry))
1176 __d_drop(dentry);
1178 spin_unlock(&dentry->d_lock);
1179 spin_unlock(&dcache_lock);
1181 fsnotify_nameremove(dentry, isdir);
1184 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1187 entry->d_flags &= ~DCACHE_UNHASHED;
1188 hlist_add_head_rcu(&entry->d_hash, list);
1192 * d_rehash - add an entry back to the hash
1193 * @entry: dentry to add to the hash
1195 * Adds a dentry to the hash according to its name.
1198 void d_rehash(struct dentry * entry)
1200 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1202 spin_lock(&dcache_lock);
1203 spin_lock(&entry->d_lock);
1204 __d_rehash(entry, list);
1205 spin_unlock(&entry->d_lock);
1206 spin_unlock(&dcache_lock);
1209 #define do_switch(x,y) do { \
1210 __typeof__ (x) __tmp = x; \
1211 x = y; y = __tmp; } while (0)
1214 * When switching names, the actual string doesn't strictly have to
1215 * be preserved in the target - because we're dropping the target
1216 * anyway. As such, we can just do a simple memcpy() to copy over
1217 * the new name before we switch.
1219 * Note that we have to be a lot more careful about getting the hash
1220 * switched - we have to switch the hash value properly even if it
1221 * then no longer matches the actual (corrupted) string of the target.
1222 * The hash value has to match the hash queue that the dentry is on..
1224 static void switch_names(struct dentry *dentry, struct dentry *target)
1226 if (dname_external(target)) {
1227 if (dname_external(dentry)) {
1229 * Both external: swap the pointers
1231 do_switch(target->d_name.name, dentry->d_name.name);
1232 } else {
1234 * dentry:internal, target:external. Steal target's
1235 * storage and make target internal.
1237 dentry->d_name.name = target->d_name.name;
1238 target->d_name.name = target->d_iname;
1240 } else {
1241 if (dname_external(dentry)) {
1243 * dentry:external, target:internal. Give dentry's
1244 * storage to target and make dentry internal
1246 memcpy(dentry->d_iname, target->d_name.name,
1247 target->d_name.len + 1);
1248 target->d_name.name = dentry->d_name.name;
1249 dentry->d_name.name = dentry->d_iname;
1250 } else {
1252 * Both are internal. Just copy target to dentry
1254 memcpy(dentry->d_iname, target->d_name.name,
1255 target->d_name.len + 1);
1261 * We cannibalize "target" when moving dentry on top of it,
1262 * because it's going to be thrown away anyway. We could be more
1263 * polite about it, though.
1265 * This forceful removal will result in ugly /proc output if
1266 * somebody holds a file open that got deleted due to a rename.
1267 * We could be nicer about the deleted file, and let it show
1268 * up under the name it got deleted rather than the name that
1269 * deleted it.
1273 * d_move - move a dentry
1274 * @dentry: entry to move
1275 * @target: new dentry
1277 * Update the dcache to reflect the move of a file name. Negative
1278 * dcache entries should not be moved in this way.
1281 void d_move(struct dentry * dentry, struct dentry * target)
1283 struct hlist_head *list;
1285 if (!dentry->d_inode)
1286 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1288 spin_lock(&dcache_lock);
1289 write_seqlock(&rename_lock);
1291 * XXXX: do we really need to take target->d_lock?
1293 if (target < dentry) {
1294 spin_lock(&target->d_lock);
1295 spin_lock(&dentry->d_lock);
1296 } else {
1297 spin_lock(&dentry->d_lock);
1298 spin_lock(&target->d_lock);
1301 /* Move the dentry to the target hash queue, if on different bucket */
1302 if (dentry->d_flags & DCACHE_UNHASHED)
1303 goto already_unhashed;
1305 hlist_del_rcu(&dentry->d_hash);
1307 already_unhashed:
1308 list = d_hash(target->d_parent, target->d_name.hash);
1309 __d_rehash(dentry, list);
1311 /* Unhash the target: dput() will then get rid of it */
1312 __d_drop(target);
1314 list_del(&dentry->d_child);
1315 list_del(&target->d_child);
1317 /* Switch the names.. */
1318 switch_names(dentry, target);
1319 do_switch(dentry->d_name.len, target->d_name.len);
1320 do_switch(dentry->d_name.hash, target->d_name.hash);
1322 /* ... and switch the parents */
1323 if (IS_ROOT(dentry)) {
1324 dentry->d_parent = target->d_parent;
1325 target->d_parent = target;
1326 INIT_LIST_HEAD(&target->d_child);
1327 } else {
1328 do_switch(dentry->d_parent, target->d_parent);
1330 /* And add them back to the (new) parent lists */
1331 list_add(&target->d_child, &target->d_parent->d_subdirs);
1334 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
1335 spin_unlock(&target->d_lock);
1336 spin_unlock(&dentry->d_lock);
1337 write_sequnlock(&rename_lock);
1338 spin_unlock(&dcache_lock);
1342 * d_path - return the path of a dentry
1343 * @dentry: dentry to report
1344 * @vfsmnt: vfsmnt to which the dentry belongs
1345 * @root: root dentry
1346 * @rootmnt: vfsmnt to which the root dentry belongs
1347 * @buffer: buffer to return value in
1348 * @buflen: buffer length
1350 * Convert a dentry into an ASCII path name. If the entry has been deleted
1351 * the string " (deleted)" is appended. Note that this is ambiguous.
1353 * Returns the buffer or an error code if the path was too long.
1355 * "buflen" should be positive. Caller holds the dcache_lock.
1357 static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1358 struct dentry *root, struct vfsmount *rootmnt,
1359 char *buffer, int buflen)
1361 char * end = buffer+buflen;
1362 char * retval;
1363 int namelen;
1365 *--end = '\0';
1366 buflen--;
1367 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1368 buflen -= 10;
1369 end -= 10;
1370 if (buflen < 0)
1371 goto Elong;
1372 memcpy(end, " (deleted)", 10);
1375 if (buflen < 1)
1376 goto Elong;
1377 /* Get '/' right */
1378 retval = end-1;
1379 *retval = '/';
1381 for (;;) {
1382 struct dentry * parent;
1384 if (dentry == root && vfsmnt == rootmnt)
1385 break;
1386 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1387 /* Global root? */
1388 spin_lock(&vfsmount_lock);
1389 if (vfsmnt->mnt_parent == vfsmnt) {
1390 spin_unlock(&vfsmount_lock);
1391 goto global_root;
1393 dentry = vfsmnt->mnt_mountpoint;
1394 vfsmnt = vfsmnt->mnt_parent;
1395 spin_unlock(&vfsmount_lock);
1396 continue;
1398 parent = dentry->d_parent;
1399 prefetch(parent);
1400 namelen = dentry->d_name.len;
1401 buflen -= namelen + 1;
1402 if (buflen < 0)
1403 goto Elong;
1404 end -= namelen;
1405 memcpy(end, dentry->d_name.name, namelen);
1406 *--end = '/';
1407 retval = end;
1408 dentry = parent;
1411 return retval;
1413 global_root:
1414 namelen = dentry->d_name.len;
1415 buflen -= namelen;
1416 if (buflen < 0)
1417 goto Elong;
1418 retval -= namelen-1; /* hit the slash */
1419 memcpy(retval, dentry->d_name.name, namelen);
1420 return retval;
1421 Elong:
1422 return ERR_PTR(-ENAMETOOLONG);
1425 /* write full pathname into buffer and return start of pathname */
1426 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1427 char *buf, int buflen)
1429 char *res;
1430 struct vfsmount *rootmnt;
1431 struct dentry *root;
1433 read_lock(&current->fs->lock);
1434 rootmnt = mntget(current->fs->rootmnt);
1435 root = dget(current->fs->root);
1436 read_unlock(&current->fs->lock);
1437 spin_lock(&dcache_lock);
1438 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1439 spin_unlock(&dcache_lock);
1440 dput(root);
1441 mntput(rootmnt);
1442 return res;
1446 * NOTE! The user-level library version returns a
1447 * character pointer. The kernel system call just
1448 * returns the length of the buffer filled (which
1449 * includes the ending '\0' character), or a negative
1450 * error value. So libc would do something like
1452 * char *getcwd(char * buf, size_t size)
1454 * int retval;
1456 * retval = sys_getcwd(buf, size);
1457 * if (retval >= 0)
1458 * return buf;
1459 * errno = -retval;
1460 * return NULL;
1463 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1465 int error;
1466 struct vfsmount *pwdmnt, *rootmnt;
1467 struct dentry *pwd, *root;
1468 char *page = (char *) __get_free_page(GFP_USER);
1470 if (!page)
1471 return -ENOMEM;
1473 read_lock(&current->fs->lock);
1474 pwdmnt = mntget(current->fs->pwdmnt);
1475 pwd = dget(current->fs->pwd);
1476 rootmnt = mntget(current->fs->rootmnt);
1477 root = dget(current->fs->root);
1478 read_unlock(&current->fs->lock);
1480 error = -ENOENT;
1481 /* Has the current directory has been unlinked? */
1482 spin_lock(&dcache_lock);
1483 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1484 unsigned long len;
1485 char * cwd;
1487 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1488 spin_unlock(&dcache_lock);
1490 error = PTR_ERR(cwd);
1491 if (IS_ERR(cwd))
1492 goto out;
1494 error = -ERANGE;
1495 len = PAGE_SIZE + page - cwd;
1496 if (len <= size) {
1497 error = len;
1498 if (copy_to_user(buf, cwd, len))
1499 error = -EFAULT;
1501 } else
1502 spin_unlock(&dcache_lock);
1504 out:
1505 dput(pwd);
1506 mntput(pwdmnt);
1507 dput(root);
1508 mntput(rootmnt);
1509 free_page((unsigned long) page);
1510 return error;
1514 * Test whether new_dentry is a subdirectory of old_dentry.
1516 * Trivially implemented using the dcache structure
1520 * is_subdir - is new dentry a subdirectory of old_dentry
1521 * @new_dentry: new dentry
1522 * @old_dentry: old dentry
1524 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1525 * Returns 0 otherwise.
1526 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1529 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1531 int result;
1532 struct dentry * saved = new_dentry;
1533 unsigned long seq;
1535 /* need rcu_readlock to protect against the d_parent trashing due to
1536 * d_move
1538 rcu_read_lock();
1539 do {
1540 /* for restarting inner loop in case of seq retry */
1541 new_dentry = saved;
1542 result = 0;
1543 seq = read_seqbegin(&rename_lock);
1544 for (;;) {
1545 if (new_dentry != old_dentry) {
1546 struct dentry * parent = new_dentry->d_parent;
1547 if (parent == new_dentry)
1548 break;
1549 new_dentry = parent;
1550 continue;
1552 result = 1;
1553 break;
1555 } while (read_seqretry(&rename_lock, seq));
1556 rcu_read_unlock();
1558 return result;
1561 void d_genocide(struct dentry *root)
1563 struct dentry *this_parent = root;
1564 struct list_head *next;
1566 spin_lock(&dcache_lock);
1567 repeat:
1568 next = this_parent->d_subdirs.next;
1569 resume:
1570 while (next != &this_parent->d_subdirs) {
1571 struct list_head *tmp = next;
1572 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1573 next = tmp->next;
1574 if (d_unhashed(dentry)||!dentry->d_inode)
1575 continue;
1576 if (!list_empty(&dentry->d_subdirs)) {
1577 this_parent = dentry;
1578 goto repeat;
1580 atomic_dec(&dentry->d_count);
1582 if (this_parent != root) {
1583 next = this_parent->d_child.next;
1584 atomic_dec(&this_parent->d_count);
1585 this_parent = this_parent->d_parent;
1586 goto resume;
1588 spin_unlock(&dcache_lock);
1592 * find_inode_number - check for dentry with name
1593 * @dir: directory to check
1594 * @name: Name to find.
1596 * Check whether a dentry already exists for the given name,
1597 * and return the inode number if it has an inode. Otherwise
1598 * 0 is returned.
1600 * This routine is used to post-process directory listings for
1601 * filesystems using synthetic inode numbers, and is necessary
1602 * to keep getcwd() working.
1605 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1607 struct dentry * dentry;
1608 ino_t ino = 0;
1611 * Check for a fs-specific hash function. Note that we must
1612 * calculate the standard hash first, as the d_op->d_hash()
1613 * routine may choose to leave the hash value unchanged.
1615 name->hash = full_name_hash(name->name, name->len);
1616 if (dir->d_op && dir->d_op->d_hash)
1618 if (dir->d_op->d_hash(dir, name) != 0)
1619 goto out;
1622 dentry = d_lookup(dir, name);
1623 if (dentry)
1625 if (dentry->d_inode)
1626 ino = dentry->d_inode->i_ino;
1627 dput(dentry);
1629 out:
1630 return ino;
1633 static __initdata unsigned long dhash_entries;
1634 static int __init set_dhash_entries(char *str)
1636 if (!str)
1637 return 0;
1638 dhash_entries = simple_strtoul(str, &str, 0);
1639 return 1;
1641 __setup("dhash_entries=", set_dhash_entries);
1643 static void __init dcache_init_early(void)
1645 int loop;
1647 /* If hashes are distributed across NUMA nodes, defer
1648 * hash allocation until vmalloc space is available.
1650 if (hashdist)
1651 return;
1653 dentry_hashtable =
1654 alloc_large_system_hash("Dentry cache",
1655 sizeof(struct hlist_head),
1656 dhash_entries,
1658 HASH_EARLY,
1659 &d_hash_shift,
1660 &d_hash_mask,
1663 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1664 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1667 static void __init dcache_init(unsigned long mempages)
1669 int loop;
1672 * A constructor could be added for stable state like the lists,
1673 * but it is probably not worth it because of the cache nature
1674 * of the dcache.
1676 dentry_cache = kmem_cache_create("dentry_cache",
1677 sizeof(struct dentry),
1679 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC,
1680 NULL, NULL);
1682 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1684 /* Hash may have been set up in dcache_init_early */
1685 if (!hashdist)
1686 return;
1688 dentry_hashtable =
1689 alloc_large_system_hash("Dentry cache",
1690 sizeof(struct hlist_head),
1691 dhash_entries,
1694 &d_hash_shift,
1695 &d_hash_mask,
1698 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1699 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1702 /* SLAB cache for __getname() consumers */
1703 kmem_cache_t *names_cachep;
1705 /* SLAB cache for file structures */
1706 kmem_cache_t *filp_cachep;
1708 EXPORT_SYMBOL(d_genocide);
1710 extern void bdev_cache_init(void);
1711 extern void chrdev_init(void);
1713 void __init vfs_caches_init_early(void)
1715 dcache_init_early();
1716 inode_init_early();
1719 void __init vfs_caches_init(unsigned long mempages)
1721 unsigned long reserve;
1723 /* Base hash sizes on available memory, with a reserve equal to
1724 150% of current kernel size */
1726 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1727 mempages -= reserve;
1729 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1730 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1732 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
1733 SLAB_HWCACHE_ALIGN|SLAB_PANIC, filp_ctor, filp_dtor);
1735 dcache_init(mempages);
1736 inode_init(mempages);
1737 files_init(mempages);
1738 mnt_init(mempages);
1739 bdev_cache_init();
1740 chrdev_init();
1743 EXPORT_SYMBOL(d_alloc);
1744 EXPORT_SYMBOL(d_alloc_anon);
1745 EXPORT_SYMBOL(d_alloc_root);
1746 EXPORT_SYMBOL(d_delete);
1747 EXPORT_SYMBOL(d_find_alias);
1748 EXPORT_SYMBOL(d_instantiate);
1749 EXPORT_SYMBOL(d_invalidate);
1750 EXPORT_SYMBOL(d_lookup);
1751 EXPORT_SYMBOL(d_move);
1752 EXPORT_SYMBOL(d_path);
1753 EXPORT_SYMBOL(d_prune_aliases);
1754 EXPORT_SYMBOL(d_rehash);
1755 EXPORT_SYMBOL(d_splice_alias);
1756 EXPORT_SYMBOL(d_validate);
1757 EXPORT_SYMBOL(dget_locked);
1758 EXPORT_SYMBOL(dput);
1759 EXPORT_SYMBOL(find_inode_number);
1760 EXPORT_SYMBOL(have_submounts);
1761 EXPORT_SYMBOL(names_cachep);
1762 EXPORT_SYMBOL(shrink_dcache_parent);
1763 EXPORT_SYMBOL(shrink_dcache_sb);