Import 2.1.116pre2
[davej-history.git] / fs / dcache.c
blob979f7b903aee90332066c504e3259f9e129c4764
1 /*
2 * fs/dcache.c
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer
6 */
8 /*
9 * Notes on the allocation strategy:
11 * The dcache is a master of the icache - whenever a dcache entry
12 * exists, the inode will always exist. "iput()" is done either when
13 * the dcache entry is deleted or garbage collected.
16 #include <linux/string.h>
17 #include <linux/mm.h>
18 #include <linux/fs.h>
19 #include <linux/malloc.h>
20 #include <linux/slab.h>
21 #include <linux/init.h>
23 #include <asm/uaccess.h>
25 #define DCACHE_PARANOIA 1
26 /* #define DCACHE_DEBUG 1 */
28 /* For managing the dcache */
29 extern unsigned long num_physpages, page_cache_size;
30 extern int inodes_stat[];
31 #define nr_inodes (inodes_stat[0])
33 kmem_cache_t *dentry_cache;
36 * This is the single most critical data structure when it comes
37 * to the dcache: the hashtable for lookups. Somebody should try
38 * to make this good - I've just made it work.
40 * This hash-function tries to avoid losing too many bits of hash
41 * information, yet avoid using a prime hash-size or similar.
43 #define D_HASHBITS 10
44 #define D_HASHSIZE (1UL << D_HASHBITS)
45 #define D_HASHMASK (D_HASHSIZE-1)
47 static struct list_head dentry_hashtable[D_HASHSIZE];
48 static LIST_HEAD(dentry_unused);
50 struct {
51 int nr_dentry;
52 int nr_unused;
53 int age_limit; /* age in seconds */
54 int want_pages; /* pages requested by system */
55 int dummy[2];
56 } dentry_stat = {0, 0, 45, 0,};
58 static inline void d_free(struct dentry *dentry)
60 if (dentry->d_op && dentry->d_op->d_release)
61 dentry->d_op->d_release(dentry);
62 if (dname_external(dentry))
63 kfree(dentry->d_name.name);
64 kmem_cache_free(dentry_cache, dentry);
68 * Release the dentry's inode, using the fileystem
69 * d_iput() operation if defined.
71 static inline void dentry_iput(struct dentry * dentry)
73 struct inode *inode = dentry->d_inode;
74 if (inode) {
75 dentry->d_inode = NULL;
76 list_del(&dentry->d_alias);
77 INIT_LIST_HEAD(&dentry->d_alias);
78 if (dentry->d_op && dentry->d_op->d_iput)
79 dentry->d_op->d_iput(dentry, inode);
80 else
81 iput(inode);
86 * dput()
88 * This is complicated by the fact that we do not want to put
89 * dentries that are no longer on any hash chain on the unused
90 * list: we'd much rather just get rid of them immediately.
92 * However, that implies that we have to traverse the dentry
93 * tree upwards to the parents which might _also_ now be
94 * scheduled for deletion (it may have been only waiting for
95 * its last child to go away).
97 * This tail recursion is done by hand as we don't want to depend
98 * on the compiler to always get this right (gcc generally doesn't).
99 * Real recursion would eat up our stack space.
101 void dput(struct dentry *dentry)
103 int count;
105 if (!dentry)
106 return;
108 repeat:
109 count = dentry->d_count - 1;
110 if (count != 0)
111 goto out;
114 * Note that if d_op->d_delete blocks,
115 * the dentry could go back in use.
116 * Each fs will have to watch for this.
118 if (dentry->d_op && dentry->d_op->d_delete) {
119 dentry->d_op->d_delete(dentry);
121 count = dentry->d_count - 1;
122 if (count != 0)
123 goto out;
126 if (!list_empty(&dentry->d_lru)) {
127 dentry_stat.nr_unused--;
128 list_del(&dentry->d_lru);
130 if (list_empty(&dentry->d_hash)) {
131 struct dentry * parent;
133 list_del(&dentry->d_child);
134 dentry_iput(dentry);
135 parent = dentry->d_parent;
136 d_free(dentry);
137 if (dentry == parent)
138 return;
139 dentry = parent;
140 goto repeat;
142 list_add(&dentry->d_lru, &dentry_unused);
143 dentry_stat.nr_unused++;
145 * Update the timestamp
147 dentry->d_reftime = jiffies;
149 out:
150 if (count >= 0) {
151 dentry->d_count = count;
152 return;
155 printk(KERN_CRIT "Negative d_count (%d) for %s/%s\n",
156 count,
157 dentry->d_parent->d_name.name,
158 dentry->d_name.name);
159 *(int *)0 = 0;
163 * Try to invalidate the dentry if it turns out to be
164 * possible. If there are other users of the dentry we
165 * can't invalidate it.
167 int d_invalidate(struct dentry * dentry)
169 /* Check whether to do a partial shrink_dcache */
170 if (!list_empty(&dentry->d_subdirs))
171 shrink_dcache_parent(dentry);
173 if (dentry->d_count != 1)
174 return -EBUSY;
176 d_drop(dentry);
177 return 0;
181 * Select less valuable dentries to be pruned when we need
182 * inodes or memory. The selected dentries are moved to the
183 * old end of the list where prune_dcache() can find them.
185 * Negative dentries are included in the selection so that
186 * they don't accumulate at the end of the list. The count
187 * returned is the total number of dentries selected, which
188 * may be much larger than the requested number of inodes.
190 int select_dcache(int inode_count, int page_count)
192 struct list_head *next, *tail = &dentry_unused;
193 int found = 0, forward = 0, young = 8;
194 int depth = dentry_stat.nr_unused >> 1;
195 unsigned long min_value = 0, max_value = 4;
197 if (page_count)
198 max_value = -1;
200 next = tail->prev;
201 while (next != &dentry_unused && depth--) {
202 struct list_head *tmp = next;
203 struct dentry *dentry = list_entry(tmp, struct dentry, d_lru);
204 struct inode *inode = dentry->d_inode;
205 unsigned long value = 0;
207 next = tmp->prev;
208 if (forward)
209 next = tmp->next;
210 if (dentry->d_count) {
211 dentry_stat.nr_unused--;
212 list_del(tmp);
213 INIT_LIST_HEAD(tmp);
214 continue;
217 * Check the dentry's age to see whether to change direction.
219 if (!forward) {
220 int age = (jiffies - dentry->d_reftime) / HZ;
221 if (age < dentry_stat.age_limit) {
222 if (!--young) {
223 forward = 1;
224 next = dentry_unused.next;
226 * Update the limits -- we don't want
227 * files with too few or too many pages.
229 if (page_count) {
230 min_value = 3;
231 max_value = 15;
233 #ifdef DCACHE_DEBUG
234 printk("select_dcache: %s/%s age=%d, scanning forward\n",
235 dentry->d_parent->d_name.name, dentry->d_name.name, age);
236 #endif
238 continue;
243 * Select dentries based on the page cache count ...
244 * should factor in number of uses as well. We take
245 * all negative dentries so that they don't accumulate.
246 * (We skip inodes that aren't immediately available.)
248 if (inode) {
249 value = inode->i_nrpages;
250 if (value >= max_value || value < min_value)
251 continue;
252 if (inode->i_state || inode->i_count > 1)
253 continue;
257 * Move the selected dentries behind the tail.
259 if (tmp != tail->prev) {
260 list_del(tmp);
261 list_add(tmp, tail->prev);
263 tail = tmp;
264 found++;
265 if (inode && --inode_count <= 0)
266 break;
267 if (page_count && (page_count -= value) <= 0)
268 break;
270 return found;
274 * Throw away a dentry - free the inode, dput the parent.
275 * This requires that the LRU list has already been
276 * removed.
278 static inline void prune_one_dentry(struct dentry * dentry)
280 struct dentry * parent;
282 list_del(&dentry->d_hash);
283 list_del(&dentry->d_child);
284 dentry_iput(dentry);
285 parent = dentry->d_parent;
286 d_free(dentry);
287 dput(parent);
291 * Shrink the dcache. This is done when we need
292 * more memory, or simply when we need to unmount
293 * something (at which point we need to unuse
294 * all dentries).
296 void prune_dcache(int count)
298 for (;;) {
299 struct dentry *dentry;
300 struct list_head *tmp = dentry_unused.prev;
302 if (tmp == &dentry_unused)
303 break;
304 dentry_stat.nr_unused--;
305 list_del(tmp);
306 INIT_LIST_HEAD(tmp);
307 dentry = list_entry(tmp, struct dentry, d_lru);
308 if (!dentry->d_count) {
309 prune_one_dentry(dentry);
310 if (!--count)
311 break;
317 * Shrink the dcache for the specified super block.
318 * This allows us to unmount a device without disturbing
319 * the dcache for the other devices.
321 * This implementation makes just two traversals of the
322 * unused list. On the first pass we move the selected
323 * dentries to the most recent end, and on the second
324 * pass we free them. The second pass must restart after
325 * each dput(), but since the target dentries are all at
326 * the end, it's really just a single traversal.
328 void shrink_dcache_sb(struct super_block * sb)
330 struct list_head *tmp, *next;
331 struct dentry *dentry;
334 * Pass one ... move the dentries for the specified
335 * superblock to the most recent end of the unused list.
337 next = dentry_unused.next;
338 while (next != &dentry_unused) {
339 tmp = next;
340 next = tmp->next;
341 dentry = list_entry(tmp, struct dentry, d_lru);
342 if (dentry->d_sb != sb)
343 continue;
344 list_del(tmp);
345 list_add(tmp, &dentry_unused);
349 * Pass two ... free the dentries for this superblock.
351 repeat:
352 next = dentry_unused.next;
353 while (next != &dentry_unused) {
354 tmp = next;
355 next = tmp->next;
356 dentry = list_entry(tmp, struct dentry, d_lru);
357 if (dentry->d_sb != sb)
358 continue;
359 if (dentry->d_count)
360 continue;
361 dentry_stat.nr_unused--;
362 list_del(tmp);
363 INIT_LIST_HEAD(tmp);
364 prune_one_dentry(dentry);
365 goto repeat;
370 * Check whether a root dentry would be in use if all of its
371 * child dentries were freed. This allows a non-destructive
372 * test for unmounting a device.
374 int is_root_busy(struct dentry *root)
376 struct dentry *this_parent = root;
377 struct list_head *next;
378 int count = root->d_count;
380 repeat:
381 next = this_parent->d_subdirs.next;
382 resume:
383 while (next != &this_parent->d_subdirs) {
384 struct list_head *tmp = next;
385 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
386 next = tmp->next;
387 /* Decrement count for unused children */
388 count += (dentry->d_count - 1);
389 if (!list_empty(&dentry->d_subdirs)) {
390 this_parent = dentry;
391 goto repeat;
393 /* root is busy if any leaf is busy */
394 if (dentry->d_count)
395 return 1;
398 * All done at this level ... ascend and resume the search.
400 if (this_parent != root) {
401 next = this_parent->d_child.next;
402 this_parent = this_parent->d_parent;
403 goto resume;
405 return (count == 1); /* one remaining use count? */
409 * Search the dentry child list for the specified parent,
410 * and move any unused dentries to the end of the unused
411 * list for prune_dcache(). We descend to the next level
412 * whenever the d_subdirs list is non-empty and continue
413 * searching.
415 static int select_parent(struct dentry * parent)
417 struct dentry *this_parent = parent;
418 struct list_head *next;
419 int found = 0;
421 repeat:
422 next = this_parent->d_subdirs.next;
423 resume:
424 while (next != &this_parent->d_subdirs) {
425 struct list_head *tmp = next;
426 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
427 next = tmp->next;
428 if (!dentry->d_count) {
429 list_del(&dentry->d_lru);
430 list_add(&dentry->d_lru, dentry_unused.prev);
431 found++;
434 * Descend a level if the d_subdirs list is non-empty.
436 if (!list_empty(&dentry->d_subdirs)) {
437 this_parent = dentry;
438 #ifdef DCACHE_DEBUG
439 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
440 dentry->d_parent->d_name.name, dentry->d_name.name, found);
441 #endif
442 goto repeat;
446 * All done at this level ... ascend and resume the search.
448 if (this_parent != parent) {
449 next = this_parent->d_child.next;
450 this_parent = this_parent->d_parent;
451 #ifdef DCACHE_DEBUG
452 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
453 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
454 #endif
455 goto resume;
457 return found;
461 * Prune the dcache to remove unused children of the parent dentry.
463 void shrink_dcache_parent(struct dentry * parent)
465 int found;
467 while ((found = select_parent(parent)) != 0)
468 prune_dcache(found);
472 * This is called from kswapd when we think we need some
473 * more memory, but aren't really sure how much. So we
474 * carefully try to free a _bit_ of our dcache, but not
475 * too much.
477 * Priority:
478 * 0 - very urgent: schrink everything
479 * ...
480 * 6 - base-level: try to shrink a bit.
482 void shrink_dcache_memory(int priority, unsigned int gfp_mask)
484 prune_dcache(0);
487 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
489 struct dentry * d_alloc(struct dentry * parent, const struct qstr *name)
491 char * str;
492 struct dentry *dentry;
495 * Prune the dcache if there are too many unused dentries.
497 if (dentry_stat.nr_unused > 3*(nr_inodes >> 1)) {
498 #ifdef DCACHE_DEBUG
499 printk("d_alloc: %d unused, pruning dcache\n", dentry_stat.nr_unused);
500 #endif
501 prune_dcache(8);
502 free_inode_memory(8);
505 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
506 if (!dentry)
507 return NULL;
509 if (name->len > DNAME_INLINE_LEN-1) {
510 str = kmalloc(NAME_ALLOC_LEN(name->len), GFP_KERNEL);
511 if (!str) {
512 kmem_cache_free(dentry_cache, dentry);
513 return NULL;
515 } else
516 str = dentry->d_iname;
518 memcpy(str, name->name, name->len);
519 str[name->len] = 0;
521 dentry->d_count = 1;
522 dentry->d_flags = 0;
523 dentry->d_inode = NULL;
524 dentry->d_parent = NULL;
525 dentry->d_sb = NULL;
526 if (parent) {
527 dentry->d_parent = dget(parent);
528 dentry->d_sb = parent->d_sb;
529 list_add(&dentry->d_child, &parent->d_subdirs);
530 } else
531 INIT_LIST_HEAD(&dentry->d_child);
533 dentry->d_mounts = dentry;
534 dentry->d_covers = dentry;
535 INIT_LIST_HEAD(&dentry->d_hash);
536 INIT_LIST_HEAD(&dentry->d_lru);
537 INIT_LIST_HEAD(&dentry->d_subdirs);
538 INIT_LIST_HEAD(&dentry->d_alias);
540 dentry->d_name.name = str;
541 dentry->d_name.len = name->len;
542 dentry->d_name.hash = name->hash;
543 dentry->d_op = NULL;
544 dentry->d_fsdata = NULL;
545 return dentry;
549 * Fill in inode information in the entry.
551 * This turns negative dentries into productive full members
552 * of society.
554 * NOTE! This assumes that the inode count has been incremented
555 * (or otherwise set) by the caller to indicate that it is now
556 * in use by the dcache..
558 void d_instantiate(struct dentry *entry, struct inode * inode)
560 if (inode)
561 list_add(&entry->d_alias, &inode->i_dentry);
562 entry->d_inode = inode;
565 struct dentry * d_alloc_root(struct inode * root_inode, struct dentry *old_root)
567 struct dentry *res = NULL;
569 if (root_inode) {
570 res = d_alloc(NULL, &(const struct qstr) { "/", 1, 0 });
571 if (res) {
572 res->d_sb = root_inode->i_sb;
573 res->d_parent = res;
574 d_instantiate(res, root_inode);
577 return res;
580 static inline struct list_head * d_hash(struct dentry * parent, unsigned long hash)
582 hash += (unsigned long) parent;
583 hash = hash ^ (hash >> D_HASHBITS) ^ (hash >> D_HASHBITS*2);
584 return dentry_hashtable + (hash & D_HASHMASK);
587 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
589 unsigned int len = name->len;
590 unsigned int hash = name->hash;
591 const unsigned char *str = name->name;
592 struct list_head *head = d_hash(parent,hash);
593 struct list_head *tmp = head->next;
595 while (tmp != head) {
596 struct dentry * dentry = list_entry(tmp, struct dentry, d_hash);
598 tmp = tmp->next;
599 if (dentry->d_name.hash != hash)
600 continue;
601 if (dentry->d_parent != parent)
602 continue;
603 if (parent->d_op && parent->d_op->d_compare) {
604 if (parent->d_op->d_compare(parent, &dentry->d_name, name))
605 continue;
606 } else {
607 if (dentry->d_name.len != len)
608 continue;
609 if (memcmp(dentry->d_name.name, str, len))
610 continue;
612 return dget(dentry);
614 return NULL;
618 * An insecure source has sent us a dentry, here we verify it.
620 * This is just to make knfsd able to have the dentry pointer
621 * in the NFS file handle.
623 * NOTE! Do _not_ dereference the pointers before we have
624 * validated them. We can test the pointer values, but we
625 * must not actually use them until we have found a valid
626 * copy of the pointer in kernel space..
628 int d_validate(struct dentry *dentry, struct dentry *dparent,
629 unsigned int hash, unsigned int len)
631 struct list_head *base, *lhp;
632 int valid = 1;
634 if (dentry != dparent) {
635 base = d_hash(dparent, hash);
636 lhp = base;
637 while ((lhp = lhp->next) != base) {
638 if (dentry == list_entry(lhp, struct dentry, d_hash))
639 goto out;
641 } else {
643 * Special case: local mount points don't live in
644 * the hashes, so we search the super blocks.
646 struct super_block *sb = super_blocks + 0;
648 for (; sb < super_blocks + NR_SUPER; sb++) {
649 if (!sb->s_dev)
650 continue;
651 if (sb->s_root == dentry)
652 goto out;
655 valid = 0;
656 out:
657 return valid;
661 * When a file is deleted, we have two options:
662 * - turn this dentry into a negative dentry
663 * - unhash this dentry and free it.
665 * Usually, we want to just turn this into
666 * a negative dentry, but if anybody else is
667 * currently using the dentry or the inode
668 * we can't do that and we fall back on removing
669 * it from the hash queues and waiting for
670 * it to be deleted later when it has no users
672 void d_delete(struct dentry * dentry)
675 * Are we the only user?
677 if (dentry->d_count == 1) {
678 dentry_iput(dentry);
679 return;
683 * If not, just drop the dentry and let dput
684 * pick up the tab..
686 d_drop(dentry);
689 void d_add(struct dentry * entry, struct inode * inode)
691 struct dentry * parent = entry->d_parent;
693 list_add(&entry->d_hash, d_hash(parent, entry->d_name.hash));
694 d_instantiate(entry, inode);
697 #define do_switch(x,y) do { \
698 __typeof__ (x) __tmp = x; \
699 x = y; y = __tmp; } while (0)
702 * When switching names, the actual string doesn't strictly have to
703 * be preserved in the target - because we're dropping the target
704 * anyway. As such, we can just do a simple memcpy() to copy over
705 * the new name before we switch.
707 * Note that we have to be a lot more careful about getting the hash
708 * switched - we have to switch the hash value properly even if it
709 * then no longer matches the actual (corrupted) string of the target.
710 * The has value has to match the hash queue that the dentry is on..
712 static inline void switch_names(struct dentry * dentry, struct dentry * target)
714 const unsigned char *old_name, *new_name;
716 memcpy(dentry->d_iname, target->d_iname, DNAME_INLINE_LEN);
717 old_name = target->d_name.name;
718 new_name = dentry->d_name.name;
719 if (old_name == target->d_iname)
720 old_name = dentry->d_iname;
721 if (new_name == dentry->d_iname)
722 new_name = target->d_iname;
723 target->d_name.name = new_name;
724 dentry->d_name.name = old_name;
728 * We cannibalize "target" when moving dentry on top of it,
729 * because it's going to be thrown away anyway. We could be more
730 * polite about it, though.
732 * This forceful removal will result in ugly /proc output if
733 * somebody holds a file open that got deleted due to a rename.
734 * We could be nicer about the deleted file, and let it show
735 * up under the name it got deleted rather than the name that
736 * deleted it.
738 * Careful with the hash switch. The hash switch depends on
739 * the fact that any list-entry can be a head of the list.
740 * Think about it.
742 void d_move(struct dentry * dentry, struct dentry * target)
744 if (!dentry->d_inode)
745 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
747 /* Move the dentry to the target hash queue */
748 list_del(&dentry->d_hash);
749 list_add(&dentry->d_hash, &target->d_hash);
751 /* Unhash the target: dput() will then get rid of it */
752 list_del(&target->d_hash);
753 INIT_LIST_HEAD(&target->d_hash);
755 list_del(&dentry->d_child);
756 list_del(&target->d_child);
758 /* Switch the parents and the names.. */
759 switch_names(dentry, target);
760 do_switch(dentry->d_parent, target->d_parent);
761 do_switch(dentry->d_name.len, target->d_name.len);
762 do_switch(dentry->d_name.hash, target->d_name.hash);
764 /* And add them back to the (new) parent lists */
765 list_add(&target->d_child, &target->d_parent->d_subdirs);
766 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
770 * "buflen" should be PAGE_SIZE or more.
772 char * d_path(struct dentry *dentry, char *buffer, int buflen)
774 char * end = buffer+buflen;
775 char * retval;
776 struct dentry * root = current->fs->root;
778 *--end = '\0';
779 buflen--;
780 if (dentry->d_parent != dentry && list_empty(&dentry->d_hash)) {
781 buflen -= 10;
782 end -= 10;
783 memcpy(end, " (deleted)", 10);
786 /* Get '/' right */
787 retval = end-1;
788 *retval = '/';
790 for (;;) {
791 struct dentry * parent;
792 int namelen;
794 if (dentry == root)
795 break;
796 dentry = dentry->d_covers;
797 parent = dentry->d_parent;
798 if (dentry == parent)
799 break;
800 namelen = dentry->d_name.len;
801 buflen -= namelen + 1;
802 if (buflen < 0)
803 break;
804 end -= namelen;
805 memcpy(end, dentry->d_name.name, namelen);
806 *--end = '/';
807 retval = end;
808 dentry = parent;
810 return retval;
814 * NOTE! The user-level library version returns a
815 * character pointer. The kernel system call just
816 * returns the length of the buffer filled (which
817 * includes the ending '\0' character), or a negative
818 * error value. So libc would do something like
820 * char *getcwd(char * buf, size_t size)
822 * int retval;
824 * retval = sys_getcwd(buf, size);
825 * if (retval >= 0)
826 * return buf;
827 * errno = -retval;
828 * return NULL;
831 asmlinkage int sys_getcwd(char *buf, unsigned long size)
833 int error;
834 struct dentry *pwd = current->fs->pwd;
836 error = -ENOENT;
837 /* Has the current directory has been unlinked? */
838 if (pwd->d_parent == pwd || !list_empty(&pwd->d_hash)) {
839 char *page = (char *) __get_free_page(GFP_USER);
840 error = -ENOMEM;
841 if (page) {
842 unsigned long len;
843 char * cwd = d_path(pwd, page, PAGE_SIZE);
845 error = -ERANGE;
846 len = PAGE_SIZE + page - cwd;
847 if (len <= size) {
848 error = len;
849 if (copy_to_user(buf, cwd, len))
850 error = -EFAULT;
852 free_page((unsigned long) page);
855 return error;
859 * Test whether new_dentry is a subdirectory of old_dentry.
861 * Trivially implemented using the dcache structure
863 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
865 int result;
867 result = 0;
868 for (;;) {
869 if (new_dentry != old_dentry) {
870 struct dentry * parent = new_dentry->d_parent;
871 if (parent == new_dentry)
872 break;
873 new_dentry = parent;
874 continue;
876 result = 1;
877 break;
879 return result;
883 * Check whether a dentry already exists for the given name,
884 * and return the inode number if it has an inode.
886 * This routine is used to post-process directory listings for
887 * filesystems using synthetic inode numbers, and is necessary
888 * to keep getcwd() working.
890 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
892 struct dentry * dentry;
893 ino_t ino = 0;
896 * Check for a fs-specific hash function. Note that we must
897 * calculate the standard hash first, as the d_op->d_hash()
898 * routine may choose to leave the hash value unchanged.
900 name->hash = full_name_hash(name->name, name->len);
901 if (dir->d_op && dir->d_op->d_hash)
903 if (dir->d_op->d_hash(dir, name) != 0)
904 goto out;
907 dentry = d_lookup(dir, name);
908 if (dentry)
910 if (dentry->d_inode)
911 ino = dentry->d_inode->i_ino;
912 dput(dentry);
914 out:
915 return ino;
918 __initfunc(void dcache_init(void))
920 int i;
921 struct list_head *d = dentry_hashtable;
924 * A constructor could be added for stable state like the lists,
925 * but it is probably not worth it because of the cache nature
926 * of the dcache.
927 * If fragmentation is too bad then the SLAB_HWCACHE_ALIGN
928 * flag could be removed here, to hint to the allocator that
929 * it should not try to get multiple page regions.
931 dentry_cache = kmem_cache_create("dentry_cache",
932 sizeof(struct dentry),
934 SLAB_HWCACHE_ALIGN,
935 NULL, NULL);
936 if (!dentry_cache)
937 panic("Cannot create dentry cache");
939 i = D_HASHSIZE;
940 do {
941 INIT_LIST_HEAD(d);
942 d++;
943 i--;
944 } while (i);