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/string.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
);
55 int age_limit
; /* age in seconds */
56 int want_pages
; /* pages requested by system */
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
;
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
);
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
)
124 count
= dentry
->d_count
- 1;
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
))
137 count
= dentry
->d_count
- 1;
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
);
151 parent
= dentry
->d_parent
;
153 if (dentry
== parent
)
158 list_add(&dentry
->d_lru
, &dentry_unused
);
159 dentry_stat
.nr_unused
++;
161 * Update the timestamp
163 dentry
->d_reftime
= jiffies
;
167 dentry
->d_count
= count
;
171 printk(KERN_CRIT
"Negative d_count (%d) for %s/%s\n",
173 dentry
->d_parent
->d_name
.name
,
174 dentry
->d_name
.name
);
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
)
193 * If it's already been dropped, return OK.
195 if (list_empty(&dentry
->d_hash
))
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
))
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
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
) {
244 alias
= list_entry(tmp
, struct dentry
, d_alias
);
245 if (!d_unhashed(alias
))
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
;
260 while ((tmp
= tmp
->next
) != head
) {
261 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_alias
);
262 if (!dentry
->d_count
) {
272 * Throw away a dentry - free the inode, dput the parent.
273 * This requires that the LRU list has already been
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
);
283 parent
= dentry
->d_parent
;
285 if (parent
!= dentry
)
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
298 * This function may fail to free any resources if
299 * all the dentries are in use.
302 void prune_dcache(int count
)
306 struct dentry
*dentry
;
307 struct list_head
*tmp
= dentry_unused
.prev
;
309 if (tmp
== &dentry_unused
)
311 dentry_stat
.nr_unused
--;
314 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
315 if (!dentry
->d_count
) {
316 prune_one_dentry(dentry
);
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
340 * Shrink the dcache for the specified super block. This
341 * is used to free the dcache before unmounting a file
345 void shrink_dcache_sb(struct super_block
* sb
)
347 struct list_head
*tmp
, *next
;
348 struct dentry
*dentry
;
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
) {
360 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
361 if (dentry
->d_sb
!= sb
)
364 list_add(tmp
, &dentry_unused
);
368 * Pass two ... free the dentries for this superblock.
371 next
= dentry_unused
.next
;
372 while (next
!= &dentry_unused
) {
375 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
376 if (dentry
->d_sb
!= sb
)
380 dentry_stat
.nr_unused
--;
383 prune_one_dentry(dentry
);
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
402 int have_submounts(struct dentry
*parent
)
404 struct dentry
*this_parent
= parent
;
405 struct list_head
*next
;
407 if (d_mountpoint(parent
))
410 next
= this_parent
->d_subdirs
.next
;
412 while (next
!= &this_parent
->d_subdirs
) {
413 struct list_head
*tmp
= next
;
414 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_child
);
416 /* Have we found a mount point ? */
417 if (d_mountpoint(dentry
))
419 if (!list_empty(&dentry
->d_subdirs
)) {
420 this_parent
= dentry
;
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
;
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
442 static int select_parent(struct dentry
* parent
)
444 struct dentry
*this_parent
= parent
;
445 struct list_head
*next
;
451 next
= this_parent
->d_subdirs
.next
;
453 while (next
!= &this_parent
->d_subdirs
) {
454 struct list_head
*tmp
= next
;
455 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_child
);
457 if (!dentry
->d_count
) {
458 list_del(&dentry
->d_lru
);
459 list_add(&dentry
->d_lru
, dentry_unused
.prev
);
463 * Descend a level if the d_subdirs list is non-empty.
465 if (!list_empty(&dentry
->d_subdirs
)) {
466 this_parent
= dentry
;
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
);
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
;
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
);
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
)
500 while ((found
= select_parent(parent
)) != 0)
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
511 * 0 - very urgent: shrink everything
513 * 6 - base-level: try to shrink a bit.
515 int shrink_dcache_memory(int priority
, unsigned int gfp_mask
)
520 count
= dentry_stat
.nr_unused
/ priority
;
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
);
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
)
548 struct dentry
*dentry
;
550 dentry
= kmem_cache_alloc(dentry_cache
, GFP_KERNEL
);
554 if (name
->len
> DNAME_INLINE_LEN
-1) {
555 str
= kmalloc(NAME_ALLOC_LEN(name
->len
), GFP_KERNEL
);
557 kmem_cache_free(dentry_cache
, dentry
);
561 str
= dentry
->d_iname
;
563 memcpy(str
, name
->name
, name
->len
);
568 dentry
->d_inode
= NULL
;
569 dentry
->d_parent
= NULL
;
572 dentry
->d_parent
= dget(parent
);
573 dentry
->d_sb
= parent
->d_sb
;
574 list_add(&dentry
->d_child
, &parent
->d_subdirs
);
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
;
588 dentry
->d_fsdata
= NULL
;
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
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
)
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
;
628 res
= d_alloc(NULL
, &(const struct qstr
) { "/", 1, 0 });
630 res
->d_sb
= root_inode
->i_sb
;
632 d_instantiate(res
, root_inode
);
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
;
667 struct dentry
* dentry
= list_entry(tmp
, struct dentry
, d_hash
);
671 if (dentry
->d_name
.hash
!= hash
)
673 if (dentry
->d_parent
!= parent
)
675 if (parent
->d_op
&& parent
->d_op
->d_compare
) {
676 if (parent
->d_op
->d_compare(parent
, &dentry
->d_name
, name
))
679 if (dentry
->d_name
.len
!= len
)
681 if (memcmp(dentry
->d_name
.name
, str
, len
))
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
;
714 if (dentry
!= dparent
) {
715 base
= d_hash(dparent
, hash
);
717 while ((lhp
= lhp
->next
) != base
) {
718 if (dentry
== list_entry(lhp
, struct dentry
, d_hash
))
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
)) {
732 if (sb
->s_root
== dentry
)
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
)
767 * Are we the only user?
769 if (dentry
->d_count
== 1) {
775 * If not, just drop the dentry and let dput
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
;
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
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.
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
)
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
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
;
902 if (!IS_ROOT(dentry
) && list_empty(&dentry
->d_hash
)) {
905 memcpy(end
, " (deleted)", 10);
913 struct dentry
* parent
;
915 if (dentry
== root
&& vfsmnt
== rootmnt
)
917 if (dentry
== vfsmnt
->mnt_root
|| IS_ROOT(dentry
)) {
919 if (vfsmnt
->mnt_parent
== vfsmnt
)
921 dentry
= vfsmnt
->mnt_mountpoint
;
922 vfsmnt
= vfsmnt
->mnt_parent
;
925 parent
= dentry
->d_parent
;
926 namelen
= dentry
->d_name
.len
;
927 buflen
-= namelen
+ 1;
931 memcpy(end
, dentry
->d_name
.name
, namelen
);
938 namelen
= dentry
->d_name
.len
;
942 memcpy(end
, dentry
->d_name
.name
, namelen
);
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)
958 * retval = sys_getcwd(buf, size);
965 asmlinkage
long sys_getcwd(char *buf
, unsigned long size
)
968 struct vfsmount
*pwdmnt
;
972 pwdmnt
= mntget(current
->fs
->pwdmnt
);
973 pwd
= dget(current
->fs
->pwd
);
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
);
984 cwd
= d_path(pwd
, current
->fs
->pwdmnt
, page
, PAGE_SIZE
);
987 len
= PAGE_SIZE
+ page
- cwd
;
990 if (copy_to_user(buf
, cwd
, len
))
993 free_page((unsigned long) page
);
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
)
1023 if (new_dentry
!= old_dentry
) {
1024 struct dentry
* parent
= new_dentry
->d_parent
;
1025 if (parent
== new_dentry
)
1027 new_dentry
= parent
;
1036 void d_genocide(struct dentry
*root
)
1038 struct dentry
*this_parent
= root
;
1039 struct list_head
*next
;
1042 next
= this_parent
->d_subdirs
.next
;
1044 while (next
!= &this_parent
->d_subdirs
) {
1045 struct list_head
*tmp
= next
;
1046 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_child
);
1048 if (d_unhashed(dentry
)||!dentry
->d_inode
)
1050 if (!list_empty(&dentry
->d_subdirs
)) {
1051 this_parent
= dentry
;
1056 if (this_parent
!= root
) {
1057 next
= this_parent
->d_child
.next
;
1058 this_parent
->d_count
--;
1059 this_parent
= this_parent
->d_parent
;
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
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
;
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)
1095 dentry
= d_lookup(dir
, name
);
1098 if (dentry
->d_inode
)
1099 ino
= dentry
->d_inode
->i_ino
;
1106 void __init
dcache_init(unsigned long mempages
)
1108 struct list_head
*d
;
1109 unsigned long order
;
1110 unsigned int nr_hash
;
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
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
),
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
++)
1137 nr_hash
= (1UL << order
) * PAGE_SIZE
/
1138 sizeof(struct list_head
);
1139 d_hash_mask
= (nr_hash
- 1);
1143 while ((tmp
>>= 1UL) != 0UL)
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
;