4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
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/string.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/smp_lock.h>
24 #include <linux/cache.h>
25 #include <linux/module.h>
27 #include <asm/uaccess.h>
29 #define DCACHE_PARANOIA 1
30 /* #define DCACHE_DEBUG 1 */
32 spinlock_t dcache_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
34 /* Right now the dcache depends on the kernel lock */
35 #define check_lock() if (!kernel_locked()) BUG()
37 static kmem_cache_t *dentry_cache;
40 * This is the single most critical data structure when it comes
41 * to the dcache: the hashtable for lookups. Somebody should try
42 * to make this good - I've just made it work.
44 * This hash-function tries to avoid losing too many bits of hash
45 * information, yet avoid using a prime hash-size or similar.
47 #define D_HASHBITS d_hash_shift
48 #define D_HASHMASK d_hash_mask
50 static unsigned int d_hash_mask;
51 static unsigned int d_hash_shift;
52 static struct list_head *dentry_hashtable;
53 static LIST_HEAD(dentry_unused);
55 /* Statistics gathering. */
56 struct dentry_stat_t dentry_stat = {0, 0, 45, 0,};
59 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
62 static inline void d_free(struct dentry *dentry)
64 if (dentry->d_op && dentry->d_op->d_release)
65 dentry->d_op->d_release(dentry);
66 if (dname_external(dentry))
67 kfree(dentry->d_name.name);
68 kmem_cache_free(dentry_cache, dentry);
72 * Release the dentry's inode, using the filesystem
73 * d_iput() operation if defined.
74 * Called with dcache_lock held, drops it.
76 static inline void dentry_iput(struct dentry * dentry)
78 struct inode *inode = dentry->d_inode;
80 dentry->d_inode = NULL;
81 list_del_init(&dentry->d_alias);
82 spin_unlock(&dcache_lock);
83 if (dentry->d_op && dentry->d_op->d_iput)
84 dentry->d_op->d_iput(dentry, inode);
88 spin_unlock(&dcache_lock);
94 * This is complicated by the fact that we do not want to put
95 * dentries that are no longer on any hash chain on the unused
96 * list: we'd much rather just get rid of them immediately.
98 * However, that implies that we have to traverse the dentry
99 * tree upwards to the parents which might _also_ now be
100 * scheduled for deletion (it may have been only waiting for
101 * its last child to go away).
103 * This tail recursion is done by hand as we don't want to depend
104 * on the compiler to always get this right (gcc generally doesn't).
105 * Real recursion would eat up our stack space.
109 * dput - release a dentry
110 * @dentry: dentry to release
112 * Release a dentry. This will drop the usage count and if appropriate
113 * call the dentry unlink method as well as removing it from the queues and
114 * releasing its resources. If the parent dentries were scheduled for release
115 * they too may now get deleted.
117 * no dcache lock, please.
120 void dput(struct dentry *dentry)
126 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
129 /* dput on a free dentry? */
130 if (!list_empty(&dentry->d_lru))
133 * AV: ->d_delete() is _NOT_ allowed to block now.
135 if (dentry->d_op && dentry->d_op->d_delete) {
136 if (dentry->d_op->d_delete(dentry))
139 /* Unreachable? Get rid of it */
140 if (list_empty(&dentry->d_hash))
142 list_add(&dentry->d_lru, &dentry_unused);
143 dentry_stat.nr_unused++;
144 spin_unlock(&dcache_lock);
148 list_del_init(&dentry->d_hash);
151 struct dentry *parent;
152 list_del(&dentry->d_child);
153 dentry_stat.nr_dentry--; /* For d_free, below */
154 /* drops the lock, at that point nobody can reach this dentry */
156 parent = dentry->d_parent;
158 if (dentry == parent)
166 * d_invalidate - invalidate a dentry
167 * @dentry: dentry to invalidate
169 * Try to invalidate the dentry if it turns out to be
170 * possible. If there are other dentries that can be
171 * reached through this one we can't delete it and we
172 * return -EBUSY. On success we return 0.
177 int d_invalidate(struct dentry * dentry)
180 * If it's already been dropped, return OK.
182 spin_lock(&dcache_lock);
183 if (list_empty(&dentry->d_hash)) {
184 spin_unlock(&dcache_lock);
188 * Check whether to do a partial shrink_dcache
189 * to get rid of unused child entries.
191 if (!list_empty(&dentry->d_subdirs)) {
192 spin_unlock(&dcache_lock);
193 shrink_dcache_parent(dentry);
194 spin_lock(&dcache_lock);
198 * Somebody else still using it?
200 * If it's a directory, we can't drop it
201 * for fear of somebody re-populating it
202 * with children (even though dropping it
203 * would make it unreachable from the root,
204 * we might still populate it if it was a
205 * working directory or similar).
207 if (atomic_read(&dentry->d_count) > 1) {
208 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
209 spin_unlock(&dcache_lock);
214 list_del_init(&dentry->d_hash);
215 spin_unlock(&dcache_lock);
219 /* This should be called _only_ with dcache_lock held */
221 static inline struct dentry * __dget_locked(struct dentry *dentry)
223 atomic_inc(&dentry->d_count);
224 if (!list_empty(&dentry->d_lru)) {
225 dentry_stat.nr_unused--;
226 list_del_init(&dentry->d_lru);
231 struct dentry * dget_locked(struct dentry *dentry)
233 return __dget_locked(dentry);
237 * d_find_alias - grab a hashed alias of inode
238 * @inode: inode in question
240 * If inode has a hashed alias - acquire the reference to alias and
241 * return it. Otherwise return NULL. Notice that if inode is a directory
242 * there can be only one alias and it can be unhashed only if it has
246 struct dentry * d_find_alias(struct inode *inode)
248 struct list_head *head, *next, *tmp;
249 struct dentry *alias;
251 spin_lock(&dcache_lock);
252 head = &inode->i_dentry;
253 next = inode->i_dentry.next;
254 while (next != head) {
257 alias = list_entry(tmp, struct dentry, d_alias);
258 if (!list_empty(&alias->d_hash)) {
259 __dget_locked(alias);
260 spin_unlock(&dcache_lock);
264 spin_unlock(&dcache_lock);
269 * Try to kill dentries associated with this inode.
270 * WARNING: you must own a reference to inode.
272 void d_prune_aliases(struct inode *inode)
274 struct list_head *tmp, *head = &inode->i_dentry;
276 spin_lock(&dcache_lock);
278 while ((tmp = tmp->next) != head) {
279 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
280 if (!atomic_read(&dentry->d_count)) {
281 __dget_locked(dentry);
282 spin_unlock(&dcache_lock);
288 spin_unlock(&dcache_lock);
292 * Throw away a dentry - free the inode, dput the parent.
293 * This requires that the LRU list has already been
295 * Called with dcache_lock, drops it and then regains.
297 static inline void prune_one_dentry(struct dentry * dentry)
299 struct dentry * parent;
301 list_del_init(&dentry->d_hash);
302 list_del(&dentry->d_child);
303 dentry_stat.nr_dentry--; /* For d_free, below */
305 parent = dentry->d_parent;
307 if (parent != dentry)
309 spin_lock(&dcache_lock);
313 * prune_dcache - shrink the dcache
314 * @count: number of entries to try and free
316 * Shrink the dcache. This is done when we need
317 * more memory, or simply when we need to unmount
318 * something (at which point we need to unuse
321 * This function may fail to free any resources if
322 * all the dentries are in use.
325 void prune_dcache(int count)
327 spin_lock(&dcache_lock);
329 struct dentry *dentry;
330 struct list_head *tmp;
332 tmp = dentry_unused.prev;
334 if (tmp == &dentry_unused)
337 dentry = list_entry(tmp, struct dentry, d_lru);
339 /* If the dentry was recently referenced, don't free it. */
340 if (dentry->d_vfs_flags & DCACHE_REFERENCED) {
341 dentry->d_vfs_flags &= ~DCACHE_REFERENCED;
342 list_add(&dentry->d_lru, &dentry_unused);
345 dentry_stat.nr_unused--;
347 /* Unused dentry with a count? */
348 if (atomic_read(&dentry->d_count))
351 prune_one_dentry(dentry);
355 spin_unlock(&dcache_lock);
359 * Shrink the dcache for the specified super block.
360 * This allows us to unmount a device without disturbing
361 * the dcache for the other devices.
363 * This implementation makes just two traversals of the
364 * unused list. On the first pass we move the selected
365 * dentries to the most recent end, and on the second
366 * pass we free them. The second pass must restart after
367 * each dput(), but since the target dentries are all at
368 * the end, it's really just a single traversal.
372 * shrink_dcache_sb - shrink dcache for a superblock
375 * Shrink the dcache for the specified super block. This
376 * is used to free the dcache before unmounting a file
380 void shrink_dcache_sb(struct super_block * sb)
382 struct list_head *tmp, *next;
383 struct dentry *dentry;
386 * Pass one ... move the dentries for the specified
387 * superblock to the most recent end of the unused list.
389 spin_lock(&dcache_lock);
390 next = dentry_unused.next;
391 while (next != &dentry_unused) {
394 dentry = list_entry(tmp, struct dentry, d_lru);
395 if (dentry->d_sb != sb)
398 list_add(tmp, &dentry_unused);
402 * Pass two ... free the dentries for this superblock.
405 next = dentry_unused.next;
406 while (next != &dentry_unused) {
409 dentry = list_entry(tmp, struct dentry, d_lru);
410 if (dentry->d_sb != sb)
412 if (atomic_read(&dentry->d_count))
414 dentry_stat.nr_unused--;
416 prune_one_dentry(dentry);
419 spin_unlock(&dcache_lock);
423 * Search for at least 1 mount point in the dentry's subdirs.
424 * We descend to the next level whenever the d_subdirs
425 * list is non-empty and continue searching.
429 * have_submounts - check for mounts over a dentry
430 * @parent: dentry to check.
432 * Return true if the parent or its subdirectories contain
436 int have_submounts(struct dentry *parent)
438 struct dentry *this_parent = parent;
439 struct list_head *next;
441 spin_lock(&dcache_lock);
442 if (d_mountpoint(parent))
445 next = this_parent->d_subdirs.next;
447 while (next != &this_parent->d_subdirs) {
448 struct list_head *tmp = next;
449 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
451 /* Have we found a mount point ? */
452 if (d_mountpoint(dentry))
454 if (!list_empty(&dentry->d_subdirs)) {
455 this_parent = dentry;
460 * All done at this level ... ascend and resume the search.
462 if (this_parent != parent) {
463 next = this_parent->d_child.next;
464 this_parent = this_parent->d_parent;
467 spin_unlock(&dcache_lock);
468 return 0; /* No mount points found in tree */
470 spin_unlock(&dcache_lock);
475 * Search the dentry child list for the specified parent,
476 * and move any unused dentries to the end of the unused
477 * list for prune_dcache(). We descend to the next level
478 * whenever the d_subdirs list is non-empty and continue
481 static int select_parent(struct dentry * parent)
483 struct dentry *this_parent = parent;
484 struct list_head *next;
487 spin_lock(&dcache_lock);
489 next = this_parent->d_subdirs.next;
491 while (next != &this_parent->d_subdirs) {
492 struct list_head *tmp = next;
493 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
495 if (!atomic_read(&dentry->d_count)) {
496 list_del(&dentry->d_lru);
497 list_add(&dentry->d_lru, dentry_unused.prev);
501 * Descend a level if the d_subdirs list is non-empty.
503 if (!list_empty(&dentry->d_subdirs)) {
504 this_parent = dentry;
506 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
507 dentry->d_parent->d_name.name, dentry->d_name.name, found);
513 * All done at this level ... ascend and resume the search.
515 if (this_parent != parent) {
516 next = this_parent->d_child.next;
517 this_parent = this_parent->d_parent;
519 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
520 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
524 spin_unlock(&dcache_lock);
529 * shrink_dcache_parent - prune dcache
530 * @parent: parent of entries to prune
532 * Prune the dcache to remove unused children of the parent dentry.
535 void shrink_dcache_parent(struct dentry * parent)
539 while ((found = select_parent(parent)) != 0)
544 * This is called from kswapd when we think we need some
545 * more memory, but aren't really sure how much. So we
546 * carefully try to free a _bit_ of our dcache, but not
550 * 0 - very urgent: shrink everything
552 * 6 - base-level: try to shrink a bit.
554 int shrink_dcache_memory(int priority, unsigned int gfp_mask)
559 * Nasty deadlock avoidance.
561 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
562 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->
563 * put_inode->ext2_discard_prealloc->ext2_free_blocks->lock_super->
566 * We should make sure we don't hold the superblock lock over
567 * block allocations, but for now:
569 if (!(gfp_mask & __GFP_FS))
572 count = dentry_stat.nr_unused / priority;
575 return kmem_cache_shrink(dentry_cache);
578 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
581 * d_alloc - allocate a dcache entry
582 * @parent: parent of entry to allocate
583 * @name: qstr of the name
585 * Allocates a dentry. It returns %NULL if there is insufficient memory
586 * available. On a success the dentry is returned. The name passed in is
587 * copied and the copy passed in may be reused after this call.
590 struct dentry * d_alloc(struct dentry * parent, const struct qstr *name)
593 struct dentry *dentry;
595 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
599 if (name->len > DNAME_INLINE_LEN-1) {
600 str = kmalloc(NAME_ALLOC_LEN(name->len), GFP_KERNEL);
602 kmem_cache_free(dentry_cache, dentry);
606 str = dentry->d_iname;
608 memcpy(str, name->name, name->len);
611 atomic_set(&dentry->d_count, 1);
612 dentry->d_vfs_flags = 0;
614 dentry->d_inode = NULL;
615 dentry->d_parent = NULL;
617 dentry->d_name.name = str;
618 dentry->d_name.len = name->len;
619 dentry->d_name.hash = name->hash;
621 dentry->d_fsdata = NULL;
622 dentry->d_mounted = 0;
623 INIT_LIST_HEAD(&dentry->d_hash);
624 INIT_LIST_HEAD(&dentry->d_lru);
625 INIT_LIST_HEAD(&dentry->d_subdirs);
626 INIT_LIST_HEAD(&dentry->d_alias);
628 dentry->d_parent = dget(parent);
629 dentry->d_sb = parent->d_sb;
631 INIT_LIST_HEAD(&dentry->d_child);
633 spin_lock(&dcache_lock);
635 list_add(&dentry->d_child, &parent->d_subdirs);
636 dentry_stat.nr_dentry++;
637 spin_unlock(&dcache_lock);
643 * d_instantiate - fill in inode information for a dentry
644 * @entry: dentry to complete
645 * @inode: inode to attach to this dentry
647 * Fill in inode information in the entry.
649 * This turns negative dentries into productive full members
652 * NOTE! This assumes that the inode count has been incremented
653 * (or otherwise set) by the caller to indicate that it is now
654 * in use by the dcache.
657 void d_instantiate(struct dentry *entry, struct inode * inode)
659 if (!list_empty(&entry->d_alias)) BUG();
660 spin_lock(&dcache_lock);
662 list_add(&entry->d_alias, &inode->i_dentry);
663 entry->d_inode = inode;
664 spin_unlock(&dcache_lock);
668 * d_alloc_root - allocate root dentry
669 * @root_inode: inode to allocate the root for
671 * Allocate a root ("/") dentry for the inode given. The inode is
672 * instantiated and returned. %NULL is returned if there is insufficient
673 * memory or the inode passed is %NULL.
676 struct dentry * d_alloc_root(struct inode * root_inode)
678 struct dentry *res = NULL;
681 res = d_alloc(NULL, &(const struct qstr) { "/", 1, 0 });
683 res->d_sb = root_inode->i_sb;
685 d_instantiate(res, root_inode);
691 static inline struct list_head * d_hash(struct dentry * parent, unsigned long hash)
693 hash += (unsigned long) parent / L1_CACHE_BYTES;
694 hash = hash ^ (hash >> D_HASHBITS);
695 return dentry_hashtable + (hash & D_HASHMASK);
699 * d_lookup - search for a dentry
700 * @parent: parent dentry
701 * @name: qstr of name we wish to find
703 * Searches the children of the parent dentry for the name in question. If
704 * the dentry is found its reference count is incremented and the dentry
705 * is returned. The caller must use d_put to free the entry when it has
706 * finished using it. %NULL is returned on failure.
709 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
711 unsigned int len = name->len;
712 unsigned int hash = name->hash;
713 const unsigned char *str = name->name;
714 struct list_head *head = d_hash(parent,hash);
715 struct list_head *tmp;
717 spin_lock(&dcache_lock);
720 struct dentry * dentry = list_entry(tmp, struct dentry, d_hash);
724 if (dentry->d_name.hash != hash)
726 if (dentry->d_parent != parent)
728 if (parent->d_op && parent->d_op->d_compare) {
729 if (parent->d_op->d_compare(parent, &dentry->d_name, name))
732 if (dentry->d_name.len != len)
734 if (memcmp(dentry->d_name.name, str, len))
737 __dget_locked(dentry);
738 dentry->d_vfs_flags |= DCACHE_REFERENCED;
739 spin_unlock(&dcache_lock);
742 spin_unlock(&dcache_lock);
747 * d_validate - verify dentry provided from insecure source
748 * @dentry: The dentry alleged to be valid child of @dparent
749 * @dparent: The parent dentry (known to be valid)
750 * @hash: Hash of the dentry
751 * @len: Length of the name
753 * An insecure source has sent us a dentry, here we verify it and dget() it.
754 * This is used by ncpfs in its readdir implementation.
755 * Zero is returned in the dentry is invalid.
758 int d_validate(struct dentry *dentry, struct dentry *dparent)
760 unsigned long dent_addr = (unsigned long) dentry;
761 unsigned long min_addr = PAGE_OFFSET;
762 unsigned long align_mask = 0x0F;
763 struct list_head *base, *lhp;
765 if (dent_addr < min_addr)
767 if (dent_addr > (unsigned long)high_memory - sizeof(struct dentry))
769 if (dent_addr & align_mask)
771 if ((!kern_addr_valid(dent_addr)) || (!kern_addr_valid(dent_addr -1 +
772 sizeof(struct dentry))))
775 if (dentry->d_parent != dparent)
778 spin_lock(&dcache_lock);
779 lhp = base = d_hash(dparent, dentry->d_name.hash);
780 while ((lhp = lhp->next) != base) {
781 if (dentry == list_entry(lhp, struct dentry, d_hash)) {
782 __dget_locked(dentry);
783 spin_unlock(&dcache_lock);
787 spin_unlock(&dcache_lock);
793 * When a file is deleted, we have two options:
794 * - turn this dentry into a negative dentry
795 * - unhash this dentry and free it.
797 * Usually, we want to just turn this into
798 * a negative dentry, but if anybody else is
799 * currently using the dentry or the inode
800 * we can't do that and we fall back on removing
801 * it from the hash queues and waiting for
802 * it to be deleted later when it has no users
806 * d_delete - delete a dentry
807 * @dentry: The dentry to delete
809 * Turn the dentry into a negative dentry if possible, otherwise
810 * remove it from the hash queues so it can be deleted later
813 void d_delete(struct dentry * dentry)
816 * Are we the only user?
818 spin_lock(&dcache_lock);
819 if (atomic_read(&dentry->d_count) == 1) {
823 spin_unlock(&dcache_lock);
826 * If not, just drop the dentry and let dput
833 * d_rehash - add an entry back to the hash
834 * @entry: dentry to add to the hash
836 * Adds a dentry to the hash according to its name.
839 void d_rehash(struct dentry * entry)
841 struct list_head *list = d_hash(entry->d_parent, entry->d_name.hash);
842 if (!list_empty(&entry->d_hash)) BUG();
843 spin_lock(&dcache_lock);
844 list_add(&entry->d_hash, list);
845 spin_unlock(&dcache_lock);
848 #define do_switch(x,y) do { \
849 __typeof__ (x) __tmp = x; \
850 x = y; y = __tmp; } while (0)
853 * When switching names, the actual string doesn't strictly have to
854 * be preserved in the target - because we're dropping the target
855 * anyway. As such, we can just do a simple memcpy() to copy over
856 * the new name before we switch.
858 * Note that we have to be a lot more careful about getting the hash
859 * switched - we have to switch the hash value properly even if it
860 * then no longer matches the actual (corrupted) string of the target.
861 * The hash value has to match the hash queue that the dentry is on..
863 static inline void switch_names(struct dentry * dentry, struct dentry * target)
865 const unsigned char *old_name, *new_name;
868 memcpy(dentry->d_iname, target->d_iname, DNAME_INLINE_LEN);
869 old_name = target->d_name.name;
870 new_name = dentry->d_name.name;
871 if (old_name == target->d_iname)
872 old_name = dentry->d_iname;
873 if (new_name == dentry->d_iname)
874 new_name = target->d_iname;
875 target->d_name.name = new_name;
876 dentry->d_name.name = old_name;
880 * We cannibalize "target" when moving dentry on top of it,
881 * because it's going to be thrown away anyway. We could be more
882 * polite about it, though.
884 * This forceful removal will result in ugly /proc output if
885 * somebody holds a file open that got deleted due to a rename.
886 * We could be nicer about the deleted file, and let it show
887 * up under the name it got deleted rather than the name that
890 * Careful with the hash switch. The hash switch depends on
891 * the fact that any list-entry can be a head of the list.
896 * d_move - move a dentry
897 * @dentry: entry to move
898 * @target: new dentry
900 * Update the dcache to reflect the move of a file name. Negative
901 * dcache entries should not be moved in this way.
904 void d_move(struct dentry * dentry, struct dentry * target)
908 if (!dentry->d_inode)
909 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
911 spin_lock(&dcache_lock);
912 /* Move the dentry to the target hash queue */
913 list_del(&dentry->d_hash);
914 list_add(&dentry->d_hash, &target->d_hash);
916 /* Unhash the target: dput() will then get rid of it */
917 list_del_init(&target->d_hash);
919 list_del(&dentry->d_child);
920 list_del(&target->d_child);
922 /* Switch the parents and the names.. */
923 switch_names(dentry, target);
924 do_switch(dentry->d_parent, target->d_parent);
925 do_switch(dentry->d_name.len, target->d_name.len);
926 do_switch(dentry->d_name.hash, target->d_name.hash);
928 /* And add them back to the (new) parent lists */
929 list_add(&target->d_child, &target->d_parent->d_subdirs);
930 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
931 spin_unlock(&dcache_lock);
935 * d_path - return the path of a dentry
936 * @dentry: dentry to report
937 * @vfsmnt: vfsmnt to which the dentry belongs
939 * @rootmnt: vfsmnt to which the root dentry belongs
940 * @buffer: buffer to return value in
941 * @buflen: buffer length
943 * Convert a dentry into an ASCII path name. If the entry has been deleted
944 * the string " (deleted)" is appended. Note that this is ambiguous. Returns
947 * "buflen" should be %PAGE_SIZE or more. Caller holds the dcache_lock.
949 char * __d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
950 struct dentry *root, struct vfsmount *rootmnt,
951 char *buffer, int buflen)
953 char * end = buffer+buflen;
959 if (!IS_ROOT(dentry) && list_empty(&dentry->d_hash)) {
962 memcpy(end, " (deleted)", 10);
970 struct dentry * parent;
972 if (dentry == root && vfsmnt == rootmnt)
974 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
976 if (vfsmnt->mnt_parent == vfsmnt)
978 dentry = vfsmnt->mnt_mountpoint;
979 vfsmnt = vfsmnt->mnt_parent;
982 parent = dentry->d_parent;
983 namelen = dentry->d_name.len;
984 buflen -= namelen + 1;
986 return ERR_PTR(-ENAMETOOLONG);
988 memcpy(end, dentry->d_name.name, namelen);
997 namelen = dentry->d_name.len;
1000 retval -= namelen-1; /* hit the slash */
1001 memcpy(retval, dentry->d_name.name, namelen);
1003 retval = ERR_PTR(-ENAMETOOLONG);
1008 * NOTE! The user-level library version returns a
1009 * character pointer. The kernel system call just
1010 * returns the length of the buffer filled (which
1011 * includes the ending '\0' character), or a negative
1012 * error value. So libc would do something like
1014 * char *getcwd(char * buf, size_t size)
1018 * retval = sys_getcwd(buf, size);
1025 asmlinkage long sys_getcwd(char *buf, unsigned long size)
1028 struct vfsmount *pwdmnt, *rootmnt;
1029 struct dentry *pwd, *root;
1030 char *page = (char *) __get_free_page(GFP_USER);
1035 read_lock(¤t->fs->lock);
1036 pwdmnt = mntget(current->fs->pwdmnt);
1037 pwd = dget(current->fs->pwd);
1038 rootmnt = mntget(current->fs->rootmnt);
1039 root = dget(current->fs->root);
1040 read_unlock(¤t->fs->lock);
1043 /* Has the current directory has been unlinked? */
1044 spin_lock(&dcache_lock);
1045 if (pwd->d_parent == pwd || !list_empty(&pwd->d_hash)) {
1049 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1050 spin_unlock(&dcache_lock);
1052 error = PTR_ERR(cwd);
1057 len = PAGE_SIZE + page - cwd;
1060 if (copy_to_user(buf, cwd, len))
1064 spin_unlock(&dcache_lock);
1071 free_page((unsigned long) page);
1076 * Test whether new_dentry is a subdirectory of old_dentry.
1078 * Trivially implemented using the dcache structure
1082 * is_subdir - is new dentry a subdirectory of old_dentry
1083 * @new_dentry: new dentry
1084 * @old_dentry: old dentry
1086 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1087 * Returns 0 otherwise.
1090 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1096 if (new_dentry != old_dentry) {
1097 struct dentry * parent = new_dentry->d_parent;
1098 if (parent == new_dentry)
1100 new_dentry = parent;
1109 void d_genocide(struct dentry *root)
1111 struct dentry *this_parent = root;
1112 struct list_head *next;
1114 spin_lock(&dcache_lock);
1116 next = this_parent->d_subdirs.next;
1118 while (next != &this_parent->d_subdirs) {
1119 struct list_head *tmp = next;
1120 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1122 if (d_unhashed(dentry)||!dentry->d_inode)
1124 if (!list_empty(&dentry->d_subdirs)) {
1125 this_parent = dentry;
1128 atomic_dec(&dentry->d_count);
1130 if (this_parent != root) {
1131 next = this_parent->d_child.next;
1132 atomic_dec(&this_parent->d_count);
1133 this_parent = this_parent->d_parent;
1136 spin_unlock(&dcache_lock);
1140 * find_inode_number - check for dentry with name
1141 * @dir: directory to check
1142 * @name: Name to find.
1144 * Check whether a dentry already exists for the given name,
1145 * and return the inode number if it has an inode. Otherwise
1148 * This routine is used to post-process directory listings for
1149 * filesystems using synthetic inode numbers, and is necessary
1150 * to keep getcwd() working.
1153 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1155 struct dentry * dentry;
1159 * Check for a fs-specific hash function. Note that we must
1160 * calculate the standard hash first, as the d_op->d_hash()
1161 * routine may choose to leave the hash value unchanged.
1163 name->hash = full_name_hash(name->name, name->len);
1164 if (dir->d_op && dir->d_op->d_hash)
1166 if (dir->d_op->d_hash(dir, name) != 0)
1170 dentry = d_lookup(dir, name);
1173 if (dentry->d_inode)
1174 ino = dentry->d_inode->i_ino;
1181 static void __init dcache_init(unsigned long mempages)
1183 struct list_head *d;
1184 unsigned long order;
1185 unsigned int nr_hash;
1189 * A constructor could be added for stable state like the lists,
1190 * but it is probably not worth it because of the cache nature
1192 * If fragmentation is too bad then the SLAB_HWCACHE_ALIGN
1193 * flag could be removed here, to hint to the allocator that
1194 * it should not try to get multiple page regions.
1196 dentry_cache = kmem_cache_create("dentry_cache",
1197 sizeof(struct dentry),
1202 panic("Cannot create dentry cache");
1205 mempages >>= (13 - PAGE_SHIFT);
1207 mempages *= sizeof(struct list_head);
1208 for (order = 0; ((1UL << order) << PAGE_SHIFT) < mempages; order++)
1214 nr_hash = (1UL << order) * PAGE_SIZE /
1215 sizeof(struct list_head);
1216 d_hash_mask = (nr_hash - 1);
1220 while ((tmp >>= 1UL) != 0UL)
1223 dentry_hashtable = (struct list_head *)
1224 __get_free_pages(GFP_ATOMIC, order);
1225 } while (dentry_hashtable == NULL && --order >= 0);
1227 printk(KERN_INFO "Dentry cache hash table entries: %d (order: %ld, %ld bytes)\n",
1228 nr_hash, order, (PAGE_SIZE << order));
1230 if (!dentry_hashtable)
1231 panic("Failed to allocate dcache hash table\n");
1233 d = dentry_hashtable;
1242 static void init_buffer_head(void * foo, kmem_cache_t * cachep, unsigned long flags)
1244 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1245 SLAB_CTOR_CONSTRUCTOR)
1247 struct buffer_head * bh = (struct buffer_head *) foo;
1249 memset(bh, 0, sizeof(*bh));
1250 init_waitqueue_head(&bh->b_wait);
1254 /* SLAB cache for __getname() consumers */
1255 kmem_cache_t *names_cachep;
1257 /* SLAB cache for file structures */
1258 kmem_cache_t *filp_cachep;
1260 /* SLAB cache for dquot structures */
1261 kmem_cache_t *dquot_cachep;
1263 /* SLAB cache for buffer_head structures */
1264 kmem_cache_t *bh_cachep;
1265 EXPORT_SYMBOL(bh_cachep);
1267 extern void bdev_cache_init(void);
1268 extern void cdev_cache_init(void);
1269 extern void iobuf_cache_init(void);
1271 void __init vfs_caches_init(unsigned long mempages)
1273 bh_cachep = kmem_cache_create("buffer_head",
1274 sizeof(struct buffer_head), 0,
1275 SLAB_HWCACHE_ALIGN, init_buffer_head, NULL);
1277 panic("Cannot create buffer head SLAB cache");
1279 names_cachep = kmem_cache_create("names_cache",
1281 SLAB_HWCACHE_ALIGN, NULL, NULL);
1283 panic("Cannot create names SLAB cache");
1285 filp_cachep = kmem_cache_create("filp",
1286 sizeof(struct file), 0,
1287 SLAB_HWCACHE_ALIGN, NULL, NULL);
1289 panic("Cannot create filp SLAB cache");
1291 #if defined (CONFIG_QUOTA)
1292 dquot_cachep = kmem_cache_create("dquot",
1293 sizeof(struct dquot), sizeof(unsigned long) * 4,
1294 SLAB_HWCACHE_ALIGN, NULL, NULL);
1296 panic("Cannot create dquot SLAB cache");
1299 dcache_init(mempages);
1300 inode_init(mempages);
1301 files_init(mempages);