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,};
58 /* no dcache_lock, please */
59 static inline void d_free(struct dentry *dentry)
61 if (dentry->d_op && dentry->d_op->d_release)
62 dentry->d_op->d_release(dentry);
63 if (dname_external(dentry))
64 kfree(dentry->d_name.name);
65 kmem_cache_free(dentry_cache, dentry);
66 dentry_stat.nr_dentry--;
70 * Release the dentry's inode, using the filesystem
71 * d_iput() operation if defined.
72 * Called with dcache_lock held, drops it.
74 static inline void dentry_iput(struct dentry * dentry)
76 struct inode *inode = dentry->d_inode;
78 dentry->d_inode = NULL;
79 list_del_init(&dentry->d_alias);
80 spin_unlock(&dcache_lock);
81 if (dentry->d_op && dentry->d_op->d_iput)
82 dentry->d_op->d_iput(dentry, inode);
86 spin_unlock(&dcache_lock);
92 * This is complicated by the fact that we do not want to put
93 * dentries that are no longer on any hash chain on the unused
94 * list: we'd much rather just get rid of them immediately.
96 * However, that implies that we have to traverse the dentry
97 * tree upwards to the parents which might _also_ now be
98 * scheduled for deletion (it may have been only waiting for
99 * its last child to go away).
101 * This tail recursion is done by hand as we don't want to depend
102 * on the compiler to always get this right (gcc generally doesn't).
103 * Real recursion would eat up our stack space.
107 * dput - release a dentry
108 * @dentry: dentry to release
110 * Release a dentry. This will drop the usage count and if appropriate
111 * call the dentry unlink method as well as removing it from the queues and
112 * releasing its resources. If the parent dentries were scheduled for release
113 * they too may now get deleted.
115 * no dcache lock, please.
118 void dput(struct dentry *dentry)
124 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
127 /* dput on a free dentry? */
128 if (!list_empty(&dentry->d_lru))
131 * AV: ->d_delete() is _NOT_ allowed to block now.
133 if (dentry->d_op && dentry->d_op->d_delete) {
134 if (dentry->d_op->d_delete(dentry))
137 /* Unreachable? Get rid of it */
138 if (list_empty(&dentry->d_hash))
140 list_add(&dentry->d_lru, &dentry_unused);
141 dentry_stat.nr_unused++;
142 spin_unlock(&dcache_lock);
146 list_del_init(&dentry->d_hash);
149 struct dentry *parent;
150 list_del(&dentry->d_child);
151 /* drops the lock, at that point nobody can reach this dentry */
153 parent = dentry->d_parent;
155 if (dentry == parent)
163 * d_invalidate - invalidate a dentry
164 * @dentry: dentry to invalidate
166 * Try to invalidate the dentry if it turns out to be
167 * possible. If there are other dentries that can be
168 * reached through this one we can't delete it and we
169 * return -EBUSY. On success we return 0.
174 int d_invalidate(struct dentry * dentry)
177 * If it's already been dropped, return OK.
179 spin_lock(&dcache_lock);
180 if (list_empty(&dentry->d_hash)) {
181 spin_unlock(&dcache_lock);
185 * Check whether to do a partial shrink_dcache
186 * to get rid of unused child entries.
188 if (!list_empty(&dentry->d_subdirs)) {
189 spin_unlock(&dcache_lock);
190 shrink_dcache_parent(dentry);
191 spin_lock(&dcache_lock);
195 * Somebody else still using it?
197 * If it's a directory, we can't drop it
198 * for fear of somebody re-populating it
199 * with children (even though dropping it
200 * would make it unreachable from the root,
201 * we might still populate it if it was a
202 * working directory or similar).
204 if (atomic_read(&dentry->d_count) > 1) {
205 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
206 spin_unlock(&dcache_lock);
211 list_del_init(&dentry->d_hash);
212 spin_unlock(&dcache_lock);
216 /* This should be called _only_ with dcache_lock held */
218 static inline struct dentry * __dget_locked(struct dentry *dentry)
220 atomic_inc(&dentry->d_count);
221 if (atomic_read(&dentry->d_count) == 1) {
222 dentry_stat.nr_unused--;
223 list_del_init(&dentry->d_lru);
228 struct dentry * dget_locked(struct dentry *dentry)
230 return __dget_locked(dentry);
234 * d_find_alias - grab a hashed alias of inode
235 * @inode: inode in question
237 * If inode has a hashed alias - acquire the reference to alias and
238 * return it. Otherwise return NULL. Notice that if inode is a directory
239 * there can be only one alias and it can be unhashed only if it has
243 struct dentry * d_find_alias(struct inode *inode)
245 struct list_head *head, *next, *tmp;
246 struct dentry *alias;
248 spin_lock(&dcache_lock);
249 head = &inode->i_dentry;
250 next = inode->i_dentry.next;
251 while (next != head) {
254 alias = list_entry(tmp, struct dentry, d_alias);
255 if (!list_empty(&alias->d_hash)) {
256 __dget_locked(alias);
257 spin_unlock(&dcache_lock);
261 spin_unlock(&dcache_lock);
266 * Try to kill dentries associated with this inode.
267 * WARNING: you must own a reference to inode.
269 void d_prune_aliases(struct inode *inode)
271 struct list_head *tmp, *head = &inode->i_dentry;
273 spin_lock(&dcache_lock);
275 while ((tmp = tmp->next) != head) {
276 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
277 if (!atomic_read(&dentry->d_count)) {
278 __dget_locked(dentry);
279 spin_unlock(&dcache_lock);
285 spin_unlock(&dcache_lock);
289 * Throw away a dentry - free the inode, dput the parent.
290 * This requires that the LRU list has already been
292 * Called with dcache_lock, drops it and then regains.
294 static inline void prune_one_dentry(struct dentry * dentry)
296 struct dentry * parent;
298 list_del_init(&dentry->d_hash);
299 list_del(&dentry->d_child);
301 parent = dentry->d_parent;
303 if (parent != dentry)
305 spin_lock(&dcache_lock);
309 * prune_dcache - shrink the dcache
310 * @count: number of entries to try and free
312 * Shrink the dcache. This is done when we need
313 * more memory, or simply when we need to unmount
314 * something (at which point we need to unuse
317 * This function may fail to free any resources if
318 * all the dentries are in use.
321 void prune_dcache(int count)
323 spin_lock(&dcache_lock);
325 struct dentry *dentry;
326 struct list_head *tmp;
328 tmp = dentry_unused.prev;
330 if (tmp == &dentry_unused)
333 dentry = list_entry(tmp, struct dentry, d_lru);
335 /* If the dentry was recently referenced, don't free it. */
336 if (dentry->d_vfs_flags & DCACHE_REFERENCED) {
337 dentry->d_vfs_flags &= ~DCACHE_REFERENCED;
338 list_add(&dentry->d_lru, &dentry_unused);
341 dentry_stat.nr_unused--;
343 /* Unused dentry with a count? */
344 if (atomic_read(&dentry->d_count))
347 prune_one_dentry(dentry);
351 spin_unlock(&dcache_lock);
355 * Shrink the dcache for the specified super block.
356 * This allows us to unmount a device without disturbing
357 * the dcache for the other devices.
359 * This implementation makes just two traversals of the
360 * unused list. On the first pass we move the selected
361 * dentries to the most recent end, and on the second
362 * pass we free them. The second pass must restart after
363 * each dput(), but since the target dentries are all at
364 * the end, it's really just a single traversal.
368 * shrink_dcache_sb - shrink dcache for a superblock
371 * Shrink the dcache for the specified super block. This
372 * is used to free the dcache before unmounting a file
376 void shrink_dcache_sb(struct super_block * sb)
378 struct list_head *tmp, *next;
379 struct dentry *dentry;
382 * Pass one ... move the dentries for the specified
383 * superblock to the most recent end of the unused list.
385 spin_lock(&dcache_lock);
386 next = dentry_unused.next;
387 while (next != &dentry_unused) {
390 dentry = list_entry(tmp, struct dentry, d_lru);
391 if (dentry->d_sb != sb)
394 list_add(tmp, &dentry_unused);
398 * Pass two ... free the dentries for this superblock.
401 next = dentry_unused.next;
402 while (next != &dentry_unused) {
405 dentry = list_entry(tmp, struct dentry, d_lru);
406 if (dentry->d_sb != sb)
408 if (atomic_read(&dentry->d_count))
410 dentry_stat.nr_unused--;
412 prune_one_dentry(dentry);
415 spin_unlock(&dcache_lock);
419 * Search for at least 1 mount point in the dentry's subdirs.
420 * We descend to the next level whenever the d_subdirs
421 * list is non-empty and continue searching.
425 * have_submounts - check for mounts over a dentry
426 * @parent: dentry to check.
428 * Return true if the parent or its subdirectories contain
432 int have_submounts(struct dentry *parent)
434 struct dentry *this_parent = parent;
435 struct list_head *next;
437 spin_lock(&dcache_lock);
438 if (d_mountpoint(parent))
441 next = this_parent->d_subdirs.next;
443 while (next != &this_parent->d_subdirs) {
444 struct list_head *tmp = next;
445 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
447 /* Have we found a mount point ? */
448 if (d_mountpoint(dentry))
450 if (!list_empty(&dentry->d_subdirs)) {
451 this_parent = dentry;
456 * All done at this level ... ascend and resume the search.
458 if (this_parent != parent) {
459 next = this_parent->d_child.next;
460 this_parent = this_parent->d_parent;
463 spin_unlock(&dcache_lock);
464 return 0; /* No mount points found in tree */
466 spin_unlock(&dcache_lock);
471 * Search the dentry child list for the specified parent,
472 * and move any unused dentries to the end of the unused
473 * list for prune_dcache(). We descend to the next level
474 * whenever the d_subdirs list is non-empty and continue
477 static int select_parent(struct dentry * parent)
479 struct dentry *this_parent = parent;
480 struct list_head *next;
483 spin_lock(&dcache_lock);
485 next = this_parent->d_subdirs.next;
487 while (next != &this_parent->d_subdirs) {
488 struct list_head *tmp = next;
489 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
491 if (!atomic_read(&dentry->d_count)) {
492 list_del(&dentry->d_lru);
493 list_add(&dentry->d_lru, dentry_unused.prev);
497 * Descend a level if the d_subdirs list is non-empty.
499 if (!list_empty(&dentry->d_subdirs)) {
500 this_parent = dentry;
502 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
503 dentry->d_parent->d_name.name, dentry->d_name.name, found);
509 * All done at this level ... ascend and resume the search.
511 if (this_parent != parent) {
512 next = this_parent->d_child.next;
513 this_parent = this_parent->d_parent;
515 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
516 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
520 spin_unlock(&dcache_lock);
525 * shrink_dcache_parent - prune dcache
526 * @parent: parent of entries to prune
528 * Prune the dcache to remove unused children of the parent dentry.
531 void shrink_dcache_parent(struct dentry * parent)
535 while ((found = select_parent(parent)) != 0)
540 * This is called from kswapd when we think we need some
541 * more memory, but aren't really sure how much. So we
542 * carefully try to free a _bit_ of our dcache, but not
546 * 0 - very urgent: shrink everything
548 * 6 - base-level: try to shrink a bit.
550 int shrink_dcache_memory(int priority, unsigned int gfp_mask)
555 * Nasty deadlock avoidance.
557 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
558 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->
559 * put_inode->ext2_discard_prealloc->ext2_free_blocks->lock_super->
562 * We should make sure we don't hold the superblock lock over
563 * block allocations, but for now:
565 if (!(gfp_mask & __GFP_FS))
568 count = dentry_stat.nr_unused / priority;
571 return kmem_cache_shrink(dentry_cache);
574 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
577 * d_alloc - allocate a dcache entry
578 * @parent: parent of entry to allocate
579 * @name: qstr of the name
581 * Allocates a dentry. It returns %NULL if there is insufficient memory
582 * available. On a success the dentry is returned. The name passed in is
583 * copied and the copy passed in may be reused after this call.
586 struct dentry * d_alloc(struct dentry * parent, const struct qstr *name)
589 struct dentry *dentry;
591 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
595 if (name->len > DNAME_INLINE_LEN-1) {
596 str = kmalloc(NAME_ALLOC_LEN(name->len), GFP_KERNEL);
598 kmem_cache_free(dentry_cache, dentry);
602 str = dentry->d_iname;
604 memcpy(str, name->name, name->len);
607 atomic_set(&dentry->d_count, 1);
608 dentry->d_vfs_flags = 0;
610 dentry->d_inode = NULL;
611 dentry->d_parent = NULL;
613 dentry->d_name.name = str;
614 dentry->d_name.len = name->len;
615 dentry->d_name.hash = name->hash;
617 dentry->d_fsdata = NULL;
618 dentry->d_mounted = 0;
619 INIT_LIST_HEAD(&dentry->d_hash);
620 INIT_LIST_HEAD(&dentry->d_lru);
621 INIT_LIST_HEAD(&dentry->d_subdirs);
622 INIT_LIST_HEAD(&dentry->d_alias);
624 dentry->d_parent = dget(parent);
625 dentry->d_sb = parent->d_sb;
626 spin_lock(&dcache_lock);
627 list_add(&dentry->d_child, &parent->d_subdirs);
628 spin_unlock(&dcache_lock);
630 INIT_LIST_HEAD(&dentry->d_child);
632 dentry_stat.nr_dentry++;
637 * d_instantiate - fill in inode information for a dentry
638 * @entry: dentry to complete
639 * @inode: inode to attach to this dentry
641 * Fill in inode information in the entry.
643 * This turns negative dentries into productive full members
646 * NOTE! This assumes that the inode count has been incremented
647 * (or otherwise set) by the caller to indicate that it is now
648 * in use by the dcache.
651 void d_instantiate(struct dentry *entry, struct inode * inode)
653 if (!list_empty(&entry->d_alias)) BUG();
654 spin_lock(&dcache_lock);
656 list_add(&entry->d_alias, &inode->i_dentry);
657 entry->d_inode = inode;
658 spin_unlock(&dcache_lock);
662 * d_alloc_root - allocate root dentry
663 * @root_inode: inode to allocate the root for
665 * Allocate a root ("/") dentry for the inode given. The inode is
666 * instantiated and returned. %NULL is returned if there is insufficient
667 * memory or the inode passed is %NULL.
670 struct dentry * d_alloc_root(struct inode * root_inode)
672 struct dentry *res = NULL;
675 res = d_alloc(NULL, &(const struct qstr) { "/", 1, 0 });
677 res->d_sb = root_inode->i_sb;
679 d_instantiate(res, root_inode);
685 static inline struct list_head * d_hash(struct dentry * parent, unsigned long hash)
687 hash += (unsigned long) parent / L1_CACHE_BYTES;
688 hash = hash ^ (hash >> D_HASHBITS);
689 return dentry_hashtable + (hash & D_HASHMASK);
693 * d_lookup - search for a dentry
694 * @parent: parent dentry
695 * @name: qstr of name we wish to find
697 * Searches the children of the parent dentry for the name in question. If
698 * the dentry is found its reference count is incremented and the dentry
699 * is returned. The caller must use d_put to free the entry when it has
700 * finished using it. %NULL is returned on failure.
703 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
705 unsigned int len = name->len;
706 unsigned int hash = name->hash;
707 const unsigned char *str = name->name;
708 struct list_head *head = d_hash(parent,hash);
709 struct list_head *tmp;
711 spin_lock(&dcache_lock);
714 struct dentry * dentry = list_entry(tmp, struct dentry, d_hash);
718 if (dentry->d_name.hash != hash)
720 if (dentry->d_parent != parent)
722 if (parent->d_op && parent->d_op->d_compare) {
723 if (parent->d_op->d_compare(parent, &dentry->d_name, name))
726 if (dentry->d_name.len != len)
728 if (memcmp(dentry->d_name.name, str, len))
731 __dget_locked(dentry);
732 dentry->d_vfs_flags |= DCACHE_REFERENCED;
733 spin_unlock(&dcache_lock);
736 spin_unlock(&dcache_lock);
741 * d_validate - verify dentry provided from insecure source
742 * @dentry: The dentry alleged to be valid child of @dparent
743 * @dparent: The parent dentry (known to be valid)
744 * @hash: Hash of the dentry
745 * @len: Length of the name
747 * An insecure source has sent us a dentry, here we verify it and dget() it.
748 * This is used by ncpfs in its readdir implementation.
749 * Zero is returned in the dentry is invalid.
752 int d_validate(struct dentry *dentry, struct dentry *dparent)
754 unsigned long dent_addr = (unsigned long) dentry;
755 unsigned long min_addr = PAGE_OFFSET;
756 unsigned long align_mask = 0x0F;
757 struct list_head *base, *lhp;
759 if (dent_addr < min_addr)
761 if (dent_addr > (unsigned long)high_memory - sizeof(struct dentry))
763 if (dent_addr & align_mask)
765 if ((!kern_addr_valid(dent_addr)) || (!kern_addr_valid(dent_addr -1 +
766 sizeof(struct dentry))))
769 if (dentry->d_parent != dparent)
772 spin_lock(&dcache_lock);
773 lhp = base = d_hash(dparent, dentry->d_name.hash);
774 while ((lhp = lhp->next) != base) {
775 if (dentry == list_entry(lhp, struct dentry, d_hash)) {
776 __dget_locked(dentry);
777 spin_unlock(&dcache_lock);
781 spin_unlock(&dcache_lock);
787 * When a file is deleted, we have two options:
788 * - turn this dentry into a negative dentry
789 * - unhash this dentry and free it.
791 * Usually, we want to just turn this into
792 * a negative dentry, but if anybody else is
793 * currently using the dentry or the inode
794 * we can't do that and we fall back on removing
795 * it from the hash queues and waiting for
796 * it to be deleted later when it has no users
800 * d_delete - delete a dentry
801 * @dentry: The dentry to delete
803 * Turn the dentry into a negative dentry if possible, otherwise
804 * remove it from the hash queues so it can be deleted later
807 void d_delete(struct dentry * dentry)
810 * Are we the only user?
812 spin_lock(&dcache_lock);
813 if (atomic_read(&dentry->d_count) == 1) {
817 spin_unlock(&dcache_lock);
820 * If not, just drop the dentry and let dput
827 * d_rehash - add an entry back to the hash
828 * @entry: dentry to add to the hash
830 * Adds a dentry to the hash according to its name.
833 void d_rehash(struct dentry * entry)
835 struct list_head *list = d_hash(entry->d_parent, entry->d_name.hash);
836 if (!list_empty(&entry->d_hash)) BUG();
837 spin_lock(&dcache_lock);
838 list_add(&entry->d_hash, list);
839 spin_unlock(&dcache_lock);
842 #define do_switch(x,y) do { \
843 __typeof__ (x) __tmp = x; \
844 x = y; y = __tmp; } while (0)
847 * When switching names, the actual string doesn't strictly have to
848 * be preserved in the target - because we're dropping the target
849 * anyway. As such, we can just do a simple memcpy() to copy over
850 * the new name before we switch.
852 * Note that we have to be a lot more careful about getting the hash
853 * switched - we have to switch the hash value properly even if it
854 * then no longer matches the actual (corrupted) string of the target.
855 * The hash value has to match the hash queue that the dentry is on..
857 static inline void switch_names(struct dentry * dentry, struct dentry * target)
859 const unsigned char *old_name, *new_name;
862 memcpy(dentry->d_iname, target->d_iname, DNAME_INLINE_LEN);
863 old_name = target->d_name.name;
864 new_name = dentry->d_name.name;
865 if (old_name == target->d_iname)
866 old_name = dentry->d_iname;
867 if (new_name == dentry->d_iname)
868 new_name = target->d_iname;
869 target->d_name.name = new_name;
870 dentry->d_name.name = old_name;
874 * We cannibalize "target" when moving dentry on top of it,
875 * because it's going to be thrown away anyway. We could be more
876 * polite about it, though.
878 * This forceful removal will result in ugly /proc output if
879 * somebody holds a file open that got deleted due to a rename.
880 * We could be nicer about the deleted file, and let it show
881 * up under the name it got deleted rather than the name that
884 * Careful with the hash switch. The hash switch depends on
885 * the fact that any list-entry can be a head of the list.
890 * d_move - move a dentry
891 * @dentry: entry to move
892 * @target: new dentry
894 * Update the dcache to reflect the move of a file name. Negative
895 * dcache entries should not be moved in this way.
898 void d_move(struct dentry * dentry, struct dentry * target)
902 if (!dentry->d_inode)
903 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
905 spin_lock(&dcache_lock);
906 /* Move the dentry to the target hash queue */
907 list_del(&dentry->d_hash);
908 list_add(&dentry->d_hash, &target->d_hash);
910 /* Unhash the target: dput() will then get rid of it */
911 list_del_init(&target->d_hash);
913 list_del(&dentry->d_child);
914 list_del(&target->d_child);
916 /* Switch the parents and the names.. */
917 switch_names(dentry, target);
918 do_switch(dentry->d_parent, target->d_parent);
919 do_switch(dentry->d_name.len, target->d_name.len);
920 do_switch(dentry->d_name.hash, target->d_name.hash);
922 /* And add them back to the (new) parent lists */
923 list_add(&target->d_child, &target->d_parent->d_subdirs);
924 list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
925 spin_unlock(&dcache_lock);
929 * d_path - return the path of a dentry
930 * @dentry: dentry to report
931 * @vfsmnt: vfsmnt to which the dentry belongs
933 * @rootmnt: vfsmnt to which the root dentry belongs
934 * @buffer: buffer to return value in
935 * @buflen: buffer length
937 * Convert a dentry into an ASCII path name. If the entry has been deleted
938 * the string " (deleted)" is appended. Note that this is ambiguous. Returns
941 * "buflen" should be %PAGE_SIZE or more. Caller holds the dcache_lock.
943 char * __d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
944 struct dentry *root, struct vfsmount *rootmnt,
945 char *buffer, int buflen)
947 char * end = buffer+buflen;
953 if (!IS_ROOT(dentry) && list_empty(&dentry->d_hash)) {
956 memcpy(end, " (deleted)", 10);
964 struct dentry * parent;
966 if (dentry == root && vfsmnt == rootmnt)
968 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
970 if (vfsmnt->mnt_parent == vfsmnt)
972 dentry = vfsmnt->mnt_mountpoint;
973 vfsmnt = vfsmnt->mnt_parent;
976 parent = dentry->d_parent;
977 namelen = dentry->d_name.len;
978 buflen -= namelen + 1;
980 return ERR_PTR(-ENAMETOOLONG);
982 memcpy(end, dentry->d_name.name, namelen);
991 namelen = dentry->d_name.len;
994 retval -= namelen-1; /* hit the slash */
995 memcpy(retval, dentry->d_name.name, namelen);
997 retval = ERR_PTR(-ENAMETOOLONG);
1002 * NOTE! The user-level library version returns a
1003 * character pointer. The kernel system call just
1004 * returns the length of the buffer filled (which
1005 * includes the ending '\0' character), or a negative
1006 * error value. So libc would do something like
1008 * char *getcwd(char * buf, size_t size)
1012 * retval = sys_getcwd(buf, size);
1019 asmlinkage long sys_getcwd(char *buf, unsigned long size)
1022 struct vfsmount *pwdmnt, *rootmnt;
1023 struct dentry *pwd, *root;
1024 char *page = (char *) __get_free_page(GFP_USER);
1029 read_lock(¤t->fs->lock);
1030 pwdmnt = mntget(current->fs->pwdmnt);
1031 pwd = dget(current->fs->pwd);
1032 rootmnt = mntget(current->fs->rootmnt);
1033 root = dget(current->fs->root);
1034 read_unlock(¤t->fs->lock);
1037 /* Has the current directory has been unlinked? */
1038 spin_lock(&dcache_lock);
1039 if (pwd->d_parent == pwd || !list_empty(&pwd->d_hash)) {
1043 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1044 spin_unlock(&dcache_lock);
1046 error = PTR_ERR(cwd);
1051 len = PAGE_SIZE + page - cwd;
1054 if (copy_to_user(buf, cwd, len))
1058 spin_unlock(&dcache_lock);
1065 free_page((unsigned long) page);
1070 * Test whether new_dentry is a subdirectory of old_dentry.
1072 * Trivially implemented using the dcache structure
1076 * is_subdir - is new dentry a subdirectory of old_dentry
1077 * @new_dentry: new dentry
1078 * @old_dentry: old dentry
1080 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1081 * Returns 0 otherwise.
1084 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1090 if (new_dentry != old_dentry) {
1091 struct dentry * parent = new_dentry->d_parent;
1092 if (parent == new_dentry)
1094 new_dentry = parent;
1103 void d_genocide(struct dentry *root)
1105 struct dentry *this_parent = root;
1106 struct list_head *next;
1108 spin_lock(&dcache_lock);
1110 next = this_parent->d_subdirs.next;
1112 while (next != &this_parent->d_subdirs) {
1113 struct list_head *tmp = next;
1114 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1116 if (d_unhashed(dentry)||!dentry->d_inode)
1118 if (!list_empty(&dentry->d_subdirs)) {
1119 this_parent = dentry;
1122 atomic_dec(&dentry->d_count);
1124 if (this_parent != root) {
1125 next = this_parent->d_child.next;
1126 atomic_dec(&this_parent->d_count);
1127 this_parent = this_parent->d_parent;
1130 spin_unlock(&dcache_lock);
1134 * find_inode_number - check for dentry with name
1135 * @dir: directory to check
1136 * @name: Name to find.
1138 * Check whether a dentry already exists for the given name,
1139 * and return the inode number if it has an inode. Otherwise
1142 * This routine is used to post-process directory listings for
1143 * filesystems using synthetic inode numbers, and is necessary
1144 * to keep getcwd() working.
1147 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1149 struct dentry * dentry;
1153 * Check for a fs-specific hash function. Note that we must
1154 * calculate the standard hash first, as the d_op->d_hash()
1155 * routine may choose to leave the hash value unchanged.
1157 name->hash = full_name_hash(name->name, name->len);
1158 if (dir->d_op && dir->d_op->d_hash)
1160 if (dir->d_op->d_hash(dir, name) != 0)
1164 dentry = d_lookup(dir, name);
1167 if (dentry->d_inode)
1168 ino = dentry->d_inode->i_ino;
1175 static void __init dcache_init(unsigned long mempages)
1177 struct list_head *d;
1178 unsigned long order;
1179 unsigned int nr_hash;
1183 * A constructor could be added for stable state like the lists,
1184 * but it is probably not worth it because of the cache nature
1186 * If fragmentation is too bad then the SLAB_HWCACHE_ALIGN
1187 * flag could be removed here, to hint to the allocator that
1188 * it should not try to get multiple page regions.
1190 dentry_cache = kmem_cache_create("dentry_cache",
1191 sizeof(struct dentry),
1196 panic("Cannot create dentry cache");
1199 mempages >>= (13 - PAGE_SHIFT);
1201 mempages *= sizeof(struct list_head);
1202 for (order = 0; ((1UL << order) << PAGE_SHIFT) < mempages; order++)
1208 nr_hash = (1UL << order) * PAGE_SIZE /
1209 sizeof(struct list_head);
1210 d_hash_mask = (nr_hash - 1);
1214 while ((tmp >>= 1UL) != 0UL)
1217 dentry_hashtable = (struct list_head *)
1218 __get_free_pages(GFP_ATOMIC, order);
1219 } while (dentry_hashtable == NULL && --order >= 0);
1221 printk(KERN_INFO "Dentry cache hash table entries: %d (order: %ld, %ld bytes)\n",
1222 nr_hash, order, (PAGE_SIZE << order));
1224 if (!dentry_hashtable)
1225 panic("Failed to allocate dcache hash table\n");
1227 d = dentry_hashtable;
1236 static void init_buffer_head(void * foo, kmem_cache_t * cachep, unsigned long flags)
1238 if ((flags & (SLAB_CTOR_VERIFY|SLAB_CTOR_CONSTRUCTOR)) ==
1239 SLAB_CTOR_CONSTRUCTOR)
1241 struct buffer_head * bh = (struct buffer_head *) foo;
1243 memset(bh, 0, sizeof(*bh));
1244 init_waitqueue_head(&bh->b_wait);
1248 /* SLAB cache for __getname() consumers */
1249 kmem_cache_t *names_cachep;
1251 /* SLAB cache for file structures */
1252 kmem_cache_t *filp_cachep;
1254 /* SLAB cache for dquot structures */
1255 kmem_cache_t *dquot_cachep;
1257 /* SLAB cache for buffer_head structures */
1258 kmem_cache_t *bh_cachep;
1259 EXPORT_SYMBOL(bh_cachep);
1261 extern void bdev_cache_init(void);
1262 extern void cdev_cache_init(void);
1263 extern void iobuf_cache_init(void);
1265 void __init vfs_caches_init(unsigned long mempages)
1267 bh_cachep = kmem_cache_create("buffer_head",
1268 sizeof(struct buffer_head), 0,
1269 SLAB_HWCACHE_ALIGN, init_buffer_head, NULL);
1271 panic("Cannot create buffer head SLAB cache");
1273 names_cachep = kmem_cache_create("names_cache",
1275 SLAB_HWCACHE_ALIGN, NULL, NULL);
1277 panic("Cannot create names SLAB cache");
1279 filp_cachep = kmem_cache_create("filp",
1280 sizeof(struct file), 0,
1281 SLAB_HWCACHE_ALIGN, NULL, NULL);
1283 panic("Cannot create filp SLAB cache");
1285 #if defined (CONFIG_QUOTA)
1286 dquot_cachep = kmem_cache_create("dquot",
1287 sizeof(struct dquot), sizeof(unsigned long) * 4,
1288 SLAB_HWCACHE_ALIGN, NULL, NULL);
1290 panic("Cannot create dquot SLAB cache");
1293 dcache_init(mempages);
1294 inode_init(mempages);
1295 files_init(mempages);