Remove references to CONFIG_PROFILE. Kernel profiling is no longer a
[linux-2.6/linux-mips.git] / fs / dcache.c
blobad897ff3864448e81c47e60d6cc992ff9321df5d
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/string.h>
18 #include <linux/mm.h>
19 #include <linux/fs.h>
20 #include <linux/malloc.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/smp_lock.h>
24 #include <linux/cache.h>
26 #include <asm/uaccess.h>
28 #define DCACHE_PARANOIA 1
29 /* #define DCACHE_DEBUG 1 */
31 /* Right now the dcache depends on the kernel lock */
32 #define check_lock() if (!kernel_locked()) BUG()
34 kmem_cache_t *dentry_cache;
37 * This is the single most critical data structure when it comes
38 * to the dcache: the hashtable for lookups. Somebody should try
39 * to make this good - I've just made it work.
41 * This hash-function tries to avoid losing too many bits of hash
42 * information, yet avoid using a prime hash-size or similar.
44 #define D_HASHBITS d_hash_shift
45 #define D_HASHMASK d_hash_mask
47 static unsigned int d_hash_mask;
48 static unsigned int d_hash_shift;
49 static struct list_head *dentry_hashtable;
50 static LIST_HEAD(dentry_unused);
52 struct {
53 int nr_dentry;
54 int nr_unused;
55 int age_limit; /* age in seconds */
56 int want_pages; /* pages requested by system */
57 int dummy[2];
58 } dentry_stat = {0, 0, 45, 0,};
60 static inline void d_free(struct dentry *dentry)
62 if (dentry->d_op && dentry->d_op->d_release)
63 dentry->d_op->d_release(dentry);
64 if (dname_external(dentry))
65 kfree(dentry->d_name.name);
66 kmem_cache_free(dentry_cache, dentry);
70 * Release the dentry's inode, using the fileystem
71 * d_iput() operation if defined.
73 static inline void dentry_iput(struct dentry * dentry)
75 struct inode *inode = dentry->d_inode;
76 if (inode) {
77 dentry->d_inode = NULL;
78 list_del(&dentry->d_alias);
79 INIT_LIST_HEAD(&dentry->d_alias);
80 if (dentry->d_op && dentry->d_op->d_iput)
81 dentry->d_op->d_iput(dentry, inode);
82 else
83 iput(inode);
87 /*
88 * This is dput
90 * This is complicated by the fact that we do not want to put
91 * dentries that are no longer on any hash chain on the unused
92 * list: we'd much rather just get rid of them immediately.
94 * However, that implies that we have to traverse the dentry
95 * tree upwards to the parents which might _also_ now be
96 * scheduled for deletion (it may have been only waiting for
97 * its last child to go away).
99 * This tail recursion is done by hand as we don't want to depend
100 * on the compiler to always get this right (gcc generally doesn't).
101 * Real recursion would eat up our stack space.
105 * dput - release a dentry
106 * @dentry: dentry to release
108 * Release a dentry. This will drop the usage count and if appropriate
109 * call the dentry unlink method as well as removing it from the queues and
110 * releasing its resources. If the parent dentries were scheduled for release
111 * they too may now get deleted.
114 void dput(struct dentry *dentry)
116 int count;
118 check_lock();
120 if (!dentry)
121 return;
123 repeat:
124 count = dentry->d_count - 1;
125 if (count != 0)
126 goto out;
129 * Note that if d_op->d_delete blocks,
130 * the dentry could go back in use.
131 * Each fs will have to watch for this.
133 if (dentry->d_op && dentry->d_op->d_delete) {
134 if (dentry->d_op->d_delete(dentry))
135 d_drop(dentry);
137 count = dentry->d_count - 1;
138 if (count != 0)
139 goto out;
142 if (!list_empty(&dentry->d_lru)) {
143 dentry_stat.nr_unused--;
144 list_del(&dentry->d_lru);
146 if (list_empty(&dentry->d_hash)) {
147 struct dentry * parent;
149 list_del(&dentry->d_child);
150 dentry_iput(dentry);
151 parent = dentry->d_parent;
152 d_free(dentry);
153 if (dentry == parent)
154 return;
155 dentry = parent;
156 goto repeat;
158 list_add(&dentry->d_lru, &dentry_unused);
159 dentry_stat.nr_unused++;
161 * Update the timestamp
163 dentry->d_reftime = jiffies;
165 out:
166 if (count >= 0) {
167 dentry->d_count = count;
168 return;
171 printk(KERN_CRIT "Negative d_count (%d) for %s/%s\n",
172 count,
173 dentry->d_parent->d_name.name,
174 dentry->d_name.name);
175 BUG();
179 * d_invalidate - invalidate a dentry
180 * @dentry: dentry to invalidate
182 * Try to invalidate the dentry if it turns out to be
183 * possible. If there are other dentries that can be
184 * reached through this one we can't delete it and we
185 * return -EBUSY. On success we return 0.
188 int d_invalidate(struct dentry * dentry)
190 check_lock();
193 * If it's already been dropped, return OK.
195 if (list_empty(&dentry->d_hash))
196 return 0;
198 * Check whether to do a partial shrink_dcache
199 * to get rid of unused child entries.
201 if (!list_empty(&dentry->d_subdirs)) {
202 shrink_dcache_parent(dentry);
206 * Somebody else still using it?
208 * If it's a directory, we can't drop it
209 * for fear of somebody re-populating it
210 * with children (even though dropping it
211 * would make it unreachable from the root,
212 * we might still populate it if it was a
213 * working directory or similar).
215 if (dentry->d_count > 1) {
216 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode))
217 return -EBUSY;
220 d_drop(dentry);
221 return 0;
225 * d_find_alias - grab a hashed alias of inode
226 * @inode: inode in question
228 * If inode has a hashed alias - acquire the reference to alias and
229 * return it. Otherwise return NULL. Notice that if inode is a directory
230 * there can be only one alias and it can be unhashed only if it has
231 * no children.
234 struct dentry * d_find_alias(struct inode *inode)
236 struct list_head *head, *next, *tmp;
237 struct dentry *alias;
239 head = &inode->i_dentry;
240 next = inode->i_dentry.next;
241 while (next != head) {
242 tmp = next;
243 next = tmp->next;
244 alias = list_entry(tmp, struct dentry, d_alias);
245 if (!d_unhashed(alias))
246 return dget(alias);
248 return NULL;
252 * Try to kill dentries associated with this inode.
253 * WARNING: you must own a reference to inode.
255 void d_prune_aliases(struct inode *inode)
257 struct list_head *tmp, *head = &inode->i_dentry;
258 restart:
259 tmp = head;
260 while ((tmp = tmp->next) != head) {
261 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
262 if (!dentry->d_count) {
263 dget(dentry);
264 d_drop(dentry);
265 dput(dentry);
266 goto restart;
272 * Throw away a dentry - free the inode, dput the parent.
273 * This requires that the LRU list has already been
274 * removed.
276 static inline void prune_one_dentry(struct dentry * dentry)
278 struct dentry * parent;
280 list_del(&dentry->d_hash);
281 list_del(&dentry->d_child);
282 dentry_iput(dentry);
283 parent = dentry->d_parent;
284 d_free(dentry);
285 if (parent != dentry)
286 dput(parent);
290 * prune_dcache - shrink the dcache
291 * @count: number of entries to try and free
293 * Shrink the dcache. This is done when we need
294 * more memory, or simply when we need to unmount
295 * something (at which point we need to unuse
296 * all dentries).
298 * This function may fail to free any resources if
299 * all the dentries are in use.
302 void prune_dcache(int count)
304 check_lock();
305 for (;;) {
306 struct dentry *dentry;
307 struct list_head *tmp = dentry_unused.prev;
309 if (tmp == &dentry_unused)
310 break;
311 dentry_stat.nr_unused--;
312 list_del(tmp);
313 INIT_LIST_HEAD(tmp);
314 dentry = list_entry(tmp, struct dentry, d_lru);
315 if (!dentry->d_count) {
316 prune_one_dentry(dentry);
317 if (!--count)
318 break;
324 * Shrink the dcache for the specified super block.
325 * This allows us to unmount a device without disturbing
326 * the dcache for the other devices.
328 * This implementation makes just two traversals of the
329 * unused list. On the first pass we move the selected
330 * dentries to the most recent end, and on the second
331 * pass we free them. The second pass must restart after
332 * each dput(), but since the target dentries are all at
333 * the end, it's really just a single traversal.
337 * shrink_dcache_sb - shrink dcache for a superblock
338 * @sb: superblock
340 * Shrink the dcache for the specified super block. This
341 * is used to free the dcache before unmounting a file
342 * system
345 void shrink_dcache_sb(struct super_block * sb)
347 struct list_head *tmp, *next;
348 struct dentry *dentry;
350 check_lock();
353 * Pass one ... move the dentries for the specified
354 * superblock to the most recent end of the unused list.
356 next = dentry_unused.next;
357 while (next != &dentry_unused) {
358 tmp = next;
359 next = tmp->next;
360 dentry = list_entry(tmp, struct dentry, d_lru);
361 if (dentry->d_sb != sb)
362 continue;
363 list_del(tmp);
364 list_add(tmp, &dentry_unused);
368 * Pass two ... free the dentries for this superblock.
370 repeat:
371 next = dentry_unused.next;
372 while (next != &dentry_unused) {
373 tmp = next;
374 next = tmp->next;
375 dentry = list_entry(tmp, struct dentry, d_lru);
376 if (dentry->d_sb != sb)
377 continue;
378 if (dentry->d_count)
379 continue;
380 dentry_stat.nr_unused--;
381 list_del(tmp);
382 INIT_LIST_HEAD(tmp);
383 prune_one_dentry(dentry);
384 goto repeat;
389 * Search for at least 1 mount point in the dentry's subdirs.
390 * We descend to the next level whenever the d_subdirs
391 * list is non-empty and continue searching.
395 * have_submounts - check for mounts over a dentry
396 * @parent: dentry to check.
398 * Return true if the parent or its subdirectories contain
399 * a mount point
402 int have_submounts(struct dentry *parent)
404 struct dentry *this_parent = parent;
405 struct list_head *next;
407 if (d_mountpoint(parent))
408 return 1;
409 repeat:
410 next = this_parent->d_subdirs.next;
411 resume:
412 while (next != &this_parent->d_subdirs) {
413 struct list_head *tmp = next;
414 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
415 next = tmp->next;
416 /* Have we found a mount point ? */
417 if (d_mountpoint(dentry))
418 return 1;
419 if (!list_empty(&dentry->d_subdirs)) {
420 this_parent = dentry;
421 goto repeat;
425 * All done at this level ... ascend and resume the search.
427 if (this_parent != parent) {
428 next = this_parent->d_child.next;
429 this_parent = this_parent->d_parent;
430 goto resume;
432 return 0; /* No mount points found in tree */
436 * Search the dentry child list for the specified parent,
437 * and move any unused dentries to the end of the unused
438 * list for prune_dcache(). We descend to the next level
439 * whenever the d_subdirs list is non-empty and continue
440 * searching.
442 static int select_parent(struct dentry * parent)
444 struct dentry *this_parent = parent;
445 struct list_head *next;
446 int found = 0;
448 check_lock();
450 repeat:
451 next = this_parent->d_subdirs.next;
452 resume:
453 while (next != &this_parent->d_subdirs) {
454 struct list_head *tmp = next;
455 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
456 next = tmp->next;
457 if (!dentry->d_count) {
458 list_del(&dentry->d_lru);
459 list_add(&dentry->d_lru, dentry_unused.prev);
460 found++;
463 * Descend a level if the d_subdirs list is non-empty.
465 if (!list_empty(&dentry->d_subdirs)) {
466 this_parent = dentry;
467 #ifdef DCACHE_DEBUG
468 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
469 dentry->d_parent->d_name.name, dentry->d_name.name, found);
470 #endif
471 goto repeat;
475 * All done at this level ... ascend and resume the search.
477 if (this_parent != parent) {
478 next = this_parent->d_child.next;
479 this_parent = this_parent->d_parent;
480 #ifdef DCACHE_DEBUG
481 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
482 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
483 #endif
484 goto resume;
486 return found;
490 * shrink_dcache_parent - prune dcache
491 * @parent: parent of entries to prune
493 * Prune the dcache to remove unused children of the parent dentry.
496 void shrink_dcache_parent(struct dentry * parent)
498 int found;
500 while ((found = select_parent(parent)) != 0)
501 prune_dcache(found);
505 * This is called from kswapd when we think we need some
506 * more memory, but aren't really sure how much. So we
507 * carefully try to free a _bit_ of our dcache, but not
508 * too much.
510 * Priority:
511 * 0 - very urgent: shrink everything
512 * ...
513 * 6 - base-level: try to shrink a bit.
515 int shrink_dcache_memory(int priority, unsigned int gfp_mask)
517 int count = 0;
518 lock_kernel();
519 if (priority)
520 count = dentry_stat.nr_unused / priority;
521 prune_dcache(count);
522 unlock_kernel();
523 /* FIXME: kmem_cache_shrink here should tell us
524 the number of pages freed, and it should
525 work in a __GFP_DMA/__GFP_HIGHMEM behaviour
526 to free only the interesting pages in
527 function of the needs of the current allocation. */
528 kmem_cache_shrink(dentry_cache);
530 return 0;
533 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
536 * d_alloc - allocate a dcache entry
537 * @parent: parent of entry to allocate
538 * @name: qstr of the name
540 * Allocates a dentry. It returns %NULL if there is insufficient memory
541 * available. On a success the dentry is returned. The name passed in is
542 * copied and the copy passed in may be reused after this call.
545 struct dentry * d_alloc(struct dentry * parent, const struct qstr *name)
547 char * str;
548 struct dentry *dentry;
550 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
551 if (!dentry)
552 return NULL;
554 if (name->len > DNAME_INLINE_LEN-1) {
555 str = kmalloc(NAME_ALLOC_LEN(name->len), GFP_KERNEL);
556 if (!str) {
557 kmem_cache_free(dentry_cache, dentry);
558 return NULL;
560 } else
561 str = dentry->d_iname;
563 memcpy(str, name->name, name->len);
564 str[name->len] = 0;
566 dentry->d_count = 1;
567 dentry->d_flags = 0;
568 dentry->d_inode = NULL;
569 dentry->d_parent = NULL;
570 dentry->d_sb = NULL;
571 if (parent) {
572 dentry->d_parent = dget(parent);
573 dentry->d_sb = parent->d_sb;
574 list_add(&dentry->d_child, &parent->d_subdirs);
575 } else
576 INIT_LIST_HEAD(&dentry->d_child);
578 INIT_LIST_HEAD(&dentry->d_vfsmnt);
579 INIT_LIST_HEAD(&dentry->d_hash);
580 INIT_LIST_HEAD(&dentry->d_lru);
581 INIT_LIST_HEAD(&dentry->d_subdirs);
582 INIT_LIST_HEAD(&dentry->d_alias);
584 dentry->d_name.name = str;
585 dentry->d_name.len = name->len;
586 dentry->d_name.hash = name->hash;
587 dentry->d_op = NULL;
588 dentry->d_fsdata = NULL;
589 return dentry;
593 * d_instantiate - fill in inode information for a dentry
594 * @entry: dentry to complete
595 * @inode: inode to attach to this dentry
597 * Fill in inode information in the entry.
599 * This turns negative dentries into productive full members
600 * of society.
602 * NOTE! This assumes that the inode count has been incremented
603 * (or otherwise set) by the caller to indicate that it is now
604 * in use by the dcache.
607 void d_instantiate(struct dentry *entry, struct inode * inode)
609 if (inode)
610 list_add(&entry->d_alias, &inode->i_dentry);
611 entry->d_inode = inode;
615 * d_alloc_root - allocate root dentry
616 * @root_inode: inode to allocate the root for
618 * Allocate a root ("/") dentry for the inode given. The inode is
619 * instantiated and returned. %NULL is returned if there is insufficient
620 * memory or the inode passed is %NULL.
623 struct dentry * d_alloc_root(struct inode * root_inode)
625 struct dentry *res = NULL;
627 if (root_inode) {
628 res = d_alloc(NULL, &(const struct qstr) { "/", 1, 0 });
629 if (res) {
630 res->d_sb = root_inode->i_sb;
631 res->d_parent = res;
632 d_instantiate(res, root_inode);
635 return res;
638 static inline struct list_head * d_hash(struct dentry * parent, unsigned long hash)
640 hash += (unsigned long) parent / L1_CACHE_BYTES;
641 hash = hash ^ (hash >> D_HASHBITS) ^ (hash >> D_HASHBITS*2);
642 return dentry_hashtable + (hash & D_HASHMASK);
646 * d_lookup - search for a dentry
647 * @parent: parent dentry
648 * @name: qstr of name we wish to find
650 * Searches the children of the parent dentry for the name in question. If
651 * the dentry is found its reference count is incremented and the dentry
652 * is returned. The caller must use d_put to free the entry when it has
653 * finished using it. %NULL is returned on failure.
656 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
658 unsigned int len = name->len;
659 unsigned int hash = name->hash;
660 const unsigned char *str = name->name;
661 struct list_head *head = d_hash(parent,hash);
662 struct list_head *tmp = head->next;
664 check_lock();
666 for (;;) {
667 struct dentry * dentry = list_entry(tmp, struct dentry, d_hash);
668 if (tmp == head)
669 break;
670 tmp = tmp->next;
671 if (dentry->d_name.hash != hash)
672 continue;
673 if (dentry->d_parent != parent)
674 continue;
675 if (parent->d_op && parent->d_op->d_compare) {
676 if (parent->d_op->d_compare(parent, &dentry->d_name, name))
677 continue;
678 } else {
679 if (dentry->d_name.len != len)
680 continue;
681 if (memcmp(dentry->d_name.name, str, len))
682 continue;
684 return dget(dentry);
686 return NULL;
690 * d_validate - verify dentry provided from insecure source
691 * @dentry: The dentry alleged to be valid
692 * @dparent: The parent dentry
693 * @hash: Hash of the dentry
694 * @len: Length of the name
696 * An insecure source has sent us a dentry, here we verify it.
697 * This is used by ncpfs in its readdir implementation.
698 * Zero is returned in the dentry is invalid.
700 * NOTE: This function does _not_ dereference the pointers before we have
701 * validated them. We can test the pointer values, but we
702 * must not actually use them until we have found a valid
703 * copy of the pointer in kernel space..
706 int d_validate(struct dentry *dentry, struct dentry *dparent,
707 unsigned int hash, unsigned int len)
709 struct list_head *base, *lhp;
710 int valid = 1;
712 check_lock();
714 if (dentry != dparent) {
715 base = d_hash(dparent, hash);
716 lhp = base;
717 while ((lhp = lhp->next) != base) {
718 if (dentry == list_entry(lhp, struct dentry, d_hash))
719 goto out;
721 } else {
723 * Special case: local mount points don't live in
724 * the hashes, so we search the super blocks.
726 struct super_block *sb = sb_entry(super_blocks.next);
728 for (; sb != sb_entry(&super_blocks);
729 sb = sb_entry(sb->s_list.next)) {
730 if (!sb->s_dev)
731 continue;
732 if (sb->s_root == dentry)
733 goto out;
736 valid = 0;
737 out:
738 return valid;
742 * When a file is deleted, we have two options:
743 * - turn this dentry into a negative dentry
744 * - unhash this dentry and free it.
746 * Usually, we want to just turn this into
747 * a negative dentry, but if anybody else is
748 * currently using the dentry or the inode
749 * we can't do that and we fall back on removing
750 * it from the hash queues and waiting for
751 * it to be deleted later when it has no users
755 * d_delete - delete a dentry
756 * @dentry: The dentry to delete
758 * Turn the dentry into a negative dentry if possible, otherwise
759 * remove it from the hash queues so it can be deleted later
762 void d_delete(struct dentry * dentry)
764 check_lock();
767 * Are we the only user?
769 if (dentry->d_count == 1) {
770 dentry_iput(dentry);
771 return;
775 * If not, just drop the dentry and let dput
776 * pick up the tab..
778 d_drop(dentry);
782 * d_rehash - add an entry back to the hash
783 * @entry: dentry to add to the hash
785 * Adds a dentry to the hash according to its name.
788 void d_rehash(struct dentry * entry)
790 struct dentry * parent = entry->d_parent;
792 list_add(&entry->d_hash, d_hash(parent, entry->d_name.hash));
795 #define do_switch(x,y) do { \
796 __typeof__ (x) __tmp = x; \
797 x = y; y = __tmp; } while (0)
800 * When switching names, the actual string doesn't strictly have to
801 * be preserved in the target - because we're dropping the target
802 * anyway. As such, we can just do a simple memcpy() to copy over
803 * the new name before we switch.
805 * Note that we have to be a lot more careful about getting the hash
806 * switched - we have to switch the hash value properly even if it
807 * then no longer matches the actual (corrupted) string of the target.
808 * The hash value has to match the hash queue that the dentry is on..
810 static inline void switch_names(struct dentry * dentry, struct dentry * target)
812 const unsigned char *old_name, *new_name;
814 check_lock();
815 memcpy(dentry->d_iname, target->d_iname, DNAME_INLINE_LEN);
816 old_name = target->d_name.name;
817 new_name = dentry->d_name.name;
818 if (old_name == target->d_iname)
819 old_name = dentry->d_iname;
820 if (new_name == dentry->d_iname)
821 new_name = target->d_iname;
822 target->d_name.name = new_name;
823 dentry->d_name.name = old_name;
827 * We cannibalize "target" when moving dentry on top of it,
828 * because it's going to be thrown away anyway. We could be more
829 * polite about it, though.
831 * This forceful removal will result in ugly /proc output if
832 * somebody holds a file open that got deleted due to a rename.
833 * We could be nicer about the deleted file, and let it show
834 * up under the name it got deleted rather than the name that
835 * deleted it.
837 * Careful with the hash switch. The hash switch depends on
838 * the fact that any list-entry can be a head of the list.
839 * Think about it.
843 * d_move - move a dentry
844 * @dentry: entry to move
845 * @target: new dentry
847 * Update the dcache to reflect the move of a file name. Negative
848 * dcache entries should not be moved in this way.
851 void d_move(struct dentry * dentry, struct dentry * target)
853 check_lock();
855 if (!dentry->d_inode)
856 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
858 /* Move the dentry to the target hash queue */
859 list_del(&dentry->d_hash);
860 list_add(&dentry->d_hash, &target->d_hash);
862 /* Unhash the target: dput() will then get rid of it */
863 list_del(&target->d_hash);
864 INIT_LIST_HEAD(&target->d_hash);
866 list_del(&dentry->d_child);
867 list_del(&target->d_child);
869 /* Switch the parents and the names.. */
870 switch_names(dentry, target);
871 do_switch(dentry->d_parent, target->d_parent);
872 do_switch(dentry->d_name.len, target->d_name.len);
873 do_switch(dentry->d_name.hash, target->d_name.hash);
875 /* And add them back to the (new) parent lists */
876 list_add(&target->d_child, &target->d_parent->d_subdirs);
877 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
881 * d_path - return the path of a dentry
882 * @dentry: dentry to report
883 * @buffer: buffer to return value in
884 * @buflen: buffer length
886 * Convert a dentry into an ASCII path name. If the entry has been deleted
887 * the string " (deleted)" is appended. Note that this is ambiguous. Returns
888 * the buffer.
890 * "buflen" should be %PAGE_SIZE or more.
892 char * __d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
893 struct dentry *root, struct vfsmount *rootmnt,
894 char *buffer, int buflen)
896 char * end = buffer+buflen;
897 char * retval;
898 int namelen;
900 *--end = '\0';
901 buflen--;
902 if (!IS_ROOT(dentry) && list_empty(&dentry->d_hash)) {
903 buflen -= 10;
904 end -= 10;
905 memcpy(end, " (deleted)", 10);
908 /* Get '/' right */
909 retval = end-1;
910 *retval = '/';
912 for (;;) {
913 struct dentry * parent;
915 if (dentry == root && vfsmnt == rootmnt)
916 break;
917 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
918 /* Global root? */
919 if (vfsmnt->mnt_parent == vfsmnt)
920 goto global_root;
921 dentry = vfsmnt->mnt_mountpoint;
922 vfsmnt = vfsmnt->mnt_parent;
923 continue;
925 parent = dentry->d_parent;
926 namelen = dentry->d_name.len;
927 buflen -= namelen + 1;
928 if (buflen < 0)
929 break;
930 end -= namelen;
931 memcpy(end, dentry->d_name.name, namelen);
932 *--end = '/';
933 retval = end;
934 dentry = parent;
936 return retval;
937 global_root:
938 namelen = dentry->d_name.len;
939 buflen -= namelen;
940 if (buflen >= 0) {
941 end -= namelen;
942 memcpy(end, dentry->d_name.name, namelen);
944 return end;
948 * NOTE! The user-level library version returns a
949 * character pointer. The kernel system call just
950 * returns the length of the buffer filled (which
951 * includes the ending '\0' character), or a negative
952 * error value. So libc would do something like
954 * char *getcwd(char * buf, size_t size)
956 * int retval;
958 * retval = sys_getcwd(buf, size);
959 * if (retval >= 0)
960 * return buf;
961 * errno = -retval;
962 * return NULL;
965 asmlinkage long sys_getcwd(char *buf, unsigned long size)
967 int error;
968 struct vfsmount *pwdmnt;
969 struct dentry *pwd;
971 lock_kernel();
972 pwdmnt = mntget(current->fs->pwdmnt);
973 pwd = dget(current->fs->pwd);
975 error = -ENOENT;
976 /* Has the current directory has been unlinked? */
977 if (pwd->d_parent == pwd || !list_empty(&pwd->d_hash)) {
978 char *page = (char *) __get_free_page(GFP_USER);
979 error = -ENOMEM;
980 if (page) {
981 unsigned long len;
982 char * cwd;
984 cwd = d_path(pwd, current->fs->pwdmnt, page, PAGE_SIZE);
986 error = -ERANGE;
987 len = PAGE_SIZE + page - cwd;
988 if (len <= size) {
989 error = len;
990 if (copy_to_user(buf, cwd, len))
991 error = -EFAULT;
993 free_page((unsigned long) page);
996 dput(pwd);
997 mntput(pwdmnt);
998 unlock_kernel();
999 return error;
1003 * Test whether new_dentry is a subdirectory of old_dentry.
1005 * Trivially implemented using the dcache structure
1009 * is_subdir - is new dentry a subdirectory of old_dentry
1010 * @new_dentry: new dentry
1011 * @old_dentry: old dentry
1013 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1014 * Returns 0 otherwise.
1017 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1019 int result;
1021 result = 0;
1022 for (;;) {
1023 if (new_dentry != old_dentry) {
1024 struct dentry * parent = new_dentry->d_parent;
1025 if (parent == new_dentry)
1026 break;
1027 new_dentry = parent;
1028 continue;
1030 result = 1;
1031 break;
1033 return result;
1036 void d_genocide(struct dentry *root)
1038 struct dentry *this_parent = root;
1039 struct list_head *next;
1041 repeat:
1042 next = this_parent->d_subdirs.next;
1043 resume:
1044 while (next != &this_parent->d_subdirs) {
1045 struct list_head *tmp = next;
1046 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1047 next = tmp->next;
1048 if (d_unhashed(dentry)||!dentry->d_inode)
1049 continue;
1050 if (!list_empty(&dentry->d_subdirs)) {
1051 this_parent = dentry;
1052 goto repeat;
1054 dentry->d_count--;
1056 if (this_parent != root) {
1057 next = this_parent->d_child.next;
1058 this_parent->d_count--;
1059 this_parent = this_parent->d_parent;
1060 goto resume;
1065 * find_inode_number - check for dentry with name
1066 * @dir: directory to check
1067 * @name: Name to find.
1069 * Check whether a dentry already exists for the given name,
1070 * and return the inode number if it has an inode. Otherwise
1071 * 0 is returned.
1073 * This routine is used to post-process directory listings for
1074 * filesystems using synthetic inode numbers, and is necessary
1075 * to keep getcwd() working.
1078 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1080 struct dentry * dentry;
1081 ino_t ino = 0;
1084 * Check for a fs-specific hash function. Note that we must
1085 * calculate the standard hash first, as the d_op->d_hash()
1086 * routine may choose to leave the hash value unchanged.
1088 name->hash = full_name_hash(name->name, name->len);
1089 if (dir->d_op && dir->d_op->d_hash)
1091 if (dir->d_op->d_hash(dir, name) != 0)
1092 goto out;
1095 dentry = d_lookup(dir, name);
1096 if (dentry)
1098 if (dentry->d_inode)
1099 ino = dentry->d_inode->i_ino;
1100 dput(dentry);
1102 out:
1103 return ino;
1106 void __init dcache_init(unsigned long mempages)
1108 struct list_head *d;
1109 unsigned long order;
1110 unsigned int nr_hash;
1111 int i;
1114 * A constructor could be added for stable state like the lists,
1115 * but it is probably not worth it because of the cache nature
1116 * of the dcache.
1117 * If fragmentation is too bad then the SLAB_HWCACHE_ALIGN
1118 * flag could be removed here, to hint to the allocator that
1119 * it should not try to get multiple page regions.
1121 dentry_cache = kmem_cache_create("dentry_cache",
1122 sizeof(struct dentry),
1124 SLAB_HWCACHE_ALIGN,
1125 NULL, NULL);
1126 if (!dentry_cache)
1127 panic("Cannot create dentry cache");
1129 mempages >>= (13 - PAGE_SHIFT);
1130 mempages *= sizeof(struct list_head);
1131 for (order = 0; ((1UL << order) << PAGE_SHIFT) < mempages; order++)
1134 do {
1135 unsigned long tmp;
1137 nr_hash = (1UL << order) * PAGE_SIZE /
1138 sizeof(struct list_head);
1139 d_hash_mask = (nr_hash - 1);
1141 tmp = nr_hash;
1142 d_hash_shift = 0;
1143 while ((tmp >>= 1UL) != 0UL)
1144 d_hash_shift++;
1146 dentry_hashtable = (struct list_head *)
1147 __get_free_pages(GFP_ATOMIC, order);
1148 } while (dentry_hashtable == NULL && --order >= 0);
1150 printk("Dentry-cache hash table entries: %d (order: %ld, %ld bytes)\n",
1151 nr_hash, order, (PAGE_SIZE << order));
1153 if (!dentry_hashtable)
1154 panic("Failed to allocate dcache hash table\n");
1156 d = dentry_hashtable;
1157 i = nr_hash;
1158 do {
1159 INIT_LIST_HEAD(d);
1160 d++;
1161 i--;
1162 } while (i);