4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer
9 * Notes on the allocation strategy:
11 * The dcache is a master of the icache - whenever a dcache entry
12 * exists, the inode will always exist. "iput()" is done either when
13 * the dcache entry is deleted or garbage collected.
16 #include <linux/string.h>
19 #include <linux/malloc.h>
20 #include <linux/init.h>
22 #define DCACHE_PARANOIA 1
23 /* #define DCACHE_DEBUG 1 */
25 /* For managing the dcache */
26 extern unsigned long num_physpages
, page_cache_size
;
27 extern int inodes_stat
[];
28 #define nr_inodes (inodes_stat[0])
31 * This is the single most critical data structure when it comes
32 * to the dcache: the hashtable for lookups. Somebody should try
33 * to make this good - I've just made it work.
35 * This hash-function tries to avoid losing too many bits of hash
36 * information, yet avoid using a prime hash-size or similar.
39 #define D_HASHSIZE (1UL << D_HASHBITS)
40 #define D_HASHMASK (D_HASHSIZE-1)
42 static struct list_head dentry_hashtable
[D_HASHSIZE
];
43 static LIST_HEAD(dentry_unused
);
48 int age_limit
; /* age in seconds */
49 int want_pages
; /* pages requested by system */
51 } dentry_stat
= {0, 0, 45, 0,};
53 static inline void d_free(struct dentry
*dentry
)
55 if (dentry
->d_op
&& dentry
->d_op
->d_release
)
56 dentry
->d_op
->d_release(dentry
);
57 kfree(dentry
->d_name
.name
);
62 * Release the dentry's inode, using the fileystem
63 * d_iput() operation if defined.
65 static inline void dentry_iput(struct dentry
* dentry
)
67 struct inode
*inode
= dentry
->d_inode
;
69 dentry
->d_inode
= NULL
;
70 list_del(&dentry
->d_alias
);
71 INIT_LIST_HEAD(&dentry
->d_alias
);
72 if (dentry
->d_op
&& dentry
->d_op
->d_iput
)
73 dentry
->d_op
->d_iput(dentry
, inode
);
82 * This is complicated by the fact that we do not want to put
83 * dentries that are no longer on any hash chain on the unused
84 * list: we'd much rather just get rid of them immediately.
86 * However, that implies that we have to traverse the dentry
87 * tree upwards to the parents which might _also_ now be
88 * scheduled for deletion (it may have been only waiting for
89 * its last child to go away).
91 * This tail recursion is done by hand as we don't want to depend
92 * on the compiler to always get this right (gcc generally doesn't).
93 * Real recursion would eat up our stack space.
95 void dput(struct dentry
*dentry
)
103 count
= dentry
->d_count
- 1;
108 * Note that if d_op->d_delete blocks,
109 * the dentry could go back in use.
110 * Each fs will have to watch for this.
112 if (dentry
->d_op
&& dentry
->d_op
->d_delete
) {
113 dentry
->d_op
->d_delete(dentry
);
115 count
= dentry
->d_count
- 1;
120 if (!list_empty(&dentry
->d_lru
)) {
121 dentry_stat
.nr_unused
--;
122 list_del(&dentry
->d_lru
);
124 if (list_empty(&dentry
->d_hash
)) {
125 struct dentry
* parent
;
127 list_del(&dentry
->d_child
);
129 parent
= dentry
->d_parent
;
131 if (dentry
== parent
)
136 list_add(&dentry
->d_lru
, &dentry_unused
);
137 dentry_stat
.nr_unused
++;
139 * Update the timestamp
141 dentry
->d_reftime
= jiffies
;
145 dentry
->d_count
= count
;
149 printk(KERN_CRIT
"Negative d_count (%d) for %s/%s\n",
151 dentry
->d_parent
->d_name
.name
,
152 dentry
->d_name
.name
);
157 * Try to invalidate the dentry if it turns out to be
158 * possible. If there are other users of the dentry we
159 * can't invalidate it.
161 int d_invalidate(struct dentry
* dentry
)
163 /* Check whether to do a partial shrink_dcache */
164 if (!list_empty(&dentry
->d_subdirs
))
165 shrink_dcache_parent(dentry
);
167 if (dentry
->d_count
!= 1)
175 * Select less valuable dentries to be pruned when we need
176 * inodes or memory. The selected dentries are moved to the
177 * old end of the list where prune_dcache() can find them.
179 * Negative dentries are included in the selection so that
180 * they don't accumulate at the end of the list. The count
181 * returned is the total number of dentries selected, which
182 * may be much larger than the requested number of inodes.
184 int select_dcache(int inode_count
, int page_count
)
186 struct list_head
*next
, *tail
= &dentry_unused
;
187 int found
= 0, forward
= 0, young
= 8;
188 int depth
= dentry_stat
.nr_unused
>> 1;
189 unsigned long min_value
= 0, max_value
= 4;
195 while (next
!= &dentry_unused
&& depth
--) {
196 struct list_head
*tmp
= next
;
197 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_lru
);
198 struct inode
*inode
= dentry
->d_inode
;
199 unsigned long value
= 0;
204 if (dentry
->d_count
) {
205 dentry_stat
.nr_unused
--;
211 * Check the dentry's age to see whether to change direction.
214 int age
= (jiffies
- dentry
->d_reftime
) / HZ
;
215 if (age
< dentry_stat
.age_limit
) {
218 next
= dentry_unused
.next
;
220 * Update the limits -- we don't want
221 * files with too few or too many pages.
228 printk("select_dcache: %s/%s age=%d, scanning forward\n",
229 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, age
);
237 * Select dentries based on the page cache count ...
238 * should factor in number of uses as well. We take
239 * all negative dentries so that they don't accumulate.
240 * (We skip inodes that aren't immediately available.)
243 value
= inode
->i_nrpages
;
244 if (value
>= max_value
|| value
< min_value
)
246 if (inode
->i_state
|| inode
->i_count
> 1)
251 * Move the selected dentries behind the tail.
253 if (tmp
!= tail
->prev
) {
255 list_add(tmp
, tail
->prev
);
259 if (inode
&& --inode_count
<= 0)
261 if (page_count
&& (page_count
-= value
) <= 0)
268 * Throw away a dentry - free the inode, dput the parent.
269 * This requires that the LRU list has already been
272 static inline void prune_one_dentry(struct dentry
* dentry
)
274 struct dentry
* parent
;
276 list_del(&dentry
->d_hash
);
277 list_del(&dentry
->d_child
);
279 parent
= dentry
->d_parent
;
285 * Shrink the dcache. This is done when we need
286 * more memory, or simply when we need to unmount
287 * something (at which point we need to unuse
290 void prune_dcache(int count
)
293 struct dentry
*dentry
;
294 struct list_head
*tmp
= dentry_unused
.prev
;
296 if (tmp
== &dentry_unused
)
298 dentry_stat
.nr_unused
--;
301 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
302 if (!dentry
->d_count
) {
303 prune_one_dentry(dentry
);
311 * Shrink the dcache for the specified super block.
312 * This allows us to unmount a device without disturbing
313 * the dcache for the other devices.
315 * This implementation makes just two traversals of the
316 * unused list. On the first pass we move the selected
317 * dentries to the most recent end, and on the second
318 * pass we free them. The second pass must restart after
319 * each dput(), but since the target dentries are all at
320 * the end, it's really just a single traversal.
322 void shrink_dcache_sb(struct super_block
* sb
)
324 struct list_head
*tmp
, *next
;
325 struct dentry
*dentry
;
328 * Pass one ... move the dentries for the specified
329 * superblock to the most recent end of the unused list.
331 next
= dentry_unused
.next
;
332 while (next
!= &dentry_unused
) {
335 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
336 if (dentry
->d_sb
!= sb
)
339 list_add(tmp
, &dentry_unused
);
343 * Pass two ... free the dentries for this superblock.
346 next
= dentry_unused
.next
;
347 while (next
!= &dentry_unused
) {
350 dentry
= list_entry(tmp
, struct dentry
, d_lru
);
351 if (dentry
->d_sb
!= sb
)
355 dentry_stat
.nr_unused
--;
358 prune_one_dentry(dentry
);
364 * Search the dentry child list for the specified parent,
365 * and move any unused dentries to the end of the unused
366 * list for prune_dcache(). We descend to the next level
367 * whenever the d_subdirs list is non-empty and continue
370 static int select_parent(struct dentry
* parent
)
372 struct dentry
*this_parent
= parent
;
373 struct list_head
*next
;
377 next
= this_parent
->d_subdirs
.next
;
379 while (next
!= &this_parent
->d_subdirs
) {
380 struct list_head
*tmp
= next
;
381 struct dentry
*dentry
= list_entry(tmp
, struct dentry
, d_child
);
383 if (!dentry
->d_count
) {
384 list_del(&dentry
->d_lru
);
385 list_add(&dentry
->d_lru
, dentry_unused
.prev
);
389 * Descend a level if the d_subdirs list is non-empty.
391 if (!list_empty(&dentry
->d_subdirs
)) {
392 this_parent
= dentry
;
394 printk(KERN_DEBUG
"select_parent: descending to %s/%s, found=%d\n",
395 dentry
->d_parent
->d_name
.name
, dentry
->d_name
.name
, found
);
401 * All done at this level ... ascend and resume the search.
403 if (this_parent
!= parent
) {
404 next
= this_parent
->d_child
.next
;
405 this_parent
= this_parent
->d_parent
;
407 printk(KERN_DEBUG
"select_parent: ascending to %s/%s, found=%d\n",
408 this_parent
->d_parent
->d_name
.name
, this_parent
->d_name
.name
, found
);
416 * Prune the dcache to remove unused children of the parent dentry.
418 void shrink_dcache_parent(struct dentry
* parent
)
422 while ((found
= select_parent(parent
)) != 0)
427 * This is called from do_try_to_free_page() to indicate
428 * that we should reduce the dcache and inode cache memory.
430 void shrink_dcache_memory()
432 dentry_stat
.want_pages
++;
436 * This carries out the request received by the above routine.
438 void check_dcache_memory()
440 if (dentry_stat
.want_pages
) {
441 unsigned int count
, goal
= 0;
443 * Set the page goal. We don't necessarily need to trim
444 * the dcache just because the system needs memory ...
446 if (page_cache_size
> (num_physpages
>> 1))
447 goal
= (dentry_stat
.want_pages
* page_cache_size
)
449 dentry_stat
.want_pages
= 0;
453 count
= select_dcache(32, goal
);
455 printk(KERN_DEBUG
"check_dcache_memory: goal=%d, count=%d\n", goal
, count
);
459 free_inode_memory(count
);
465 #define NAME_ALLOC_LEN(len) ((len+16) & ~15)
467 struct dentry
* d_alloc(struct dentry
* parent
, const struct qstr
*name
)
470 struct dentry
*dentry
;
473 * Prune the dcache if there are too many unused dentries.
475 if (dentry_stat
.nr_unused
> 3*(nr_inodes
>> 1)) {
477 printk("d_alloc: %d unused, pruning dcache\n", dentry_stat
.nr_unused
);
480 free_inode_memory(8);
483 dentry
= kmalloc(sizeof(struct dentry
), GFP_KERNEL
);
487 str
= kmalloc(NAME_ALLOC_LEN(name
->len
), GFP_KERNEL
);
493 memcpy(str
, name
->name
, name
->len
);
498 dentry
->d_inode
= NULL
;
499 dentry
->d_parent
= NULL
;
502 dentry
->d_parent
= dget(parent
);
503 dentry
->d_sb
= parent
->d_sb
;
504 list_add(&dentry
->d_child
, &parent
->d_subdirs
);
506 INIT_LIST_HEAD(&dentry
->d_child
);
508 dentry
->d_mounts
= dentry
;
509 dentry
->d_covers
= dentry
;
510 INIT_LIST_HEAD(&dentry
->d_hash
);
511 INIT_LIST_HEAD(&dentry
->d_lru
);
512 INIT_LIST_HEAD(&dentry
->d_subdirs
);
513 INIT_LIST_HEAD(&dentry
->d_alias
);
515 dentry
->d_name
.name
= str
;
516 dentry
->d_name
.len
= name
->len
;
517 dentry
->d_name
.hash
= name
->hash
;
519 dentry
->d_fsdata
= NULL
;
524 * Fill in inode information in the entry.
526 * This turns negative dentries into productive full members
529 * NOTE! This assumes that the inode count has been incremented
530 * (or otherwise set) by the caller to indicate that it is now
531 * in use by the dcache..
533 void d_instantiate(struct dentry
*entry
, struct inode
* inode
)
536 list_add(&entry
->d_alias
, &inode
->i_dentry
);
537 entry
->d_inode
= inode
;
540 struct dentry
* d_alloc_root(struct inode
* root_inode
, struct dentry
*old_root
)
542 struct dentry
*res
= NULL
;
545 res
= d_alloc(NULL
, &(const struct qstr
) { "/", 1, 0 });
547 res
->d_sb
= root_inode
->i_sb
;
549 d_instantiate(res
, root_inode
);
555 static inline struct list_head
* d_hash(struct dentry
* parent
, unsigned long hash
)
557 hash
+= (unsigned long) parent
;
558 hash
= hash
^ (hash
>> D_HASHBITS
) ^ (hash
>> D_HASHBITS
*2);
559 return dentry_hashtable
+ (hash
& D_HASHMASK
);
562 struct dentry
* d_lookup(struct dentry
* parent
, struct qstr
* name
)
564 unsigned int len
= name
->len
;
565 unsigned int hash
= name
->hash
;
566 const unsigned char *str
= name
->name
;
567 struct list_head
*head
= d_hash(parent
,hash
);
568 struct list_head
*tmp
= head
->next
;
570 while (tmp
!= head
) {
571 struct dentry
* dentry
= list_entry(tmp
, struct dentry
, d_hash
);
574 if (dentry
->d_name
.hash
!= hash
)
576 if (dentry
->d_parent
!= parent
)
578 if (parent
->d_op
&& parent
->d_op
->d_compare
) {
579 if (parent
->d_op
->d_compare(parent
, &dentry
->d_name
, name
))
582 if (dentry
->d_name
.len
!= len
)
584 if (memcmp(dentry
->d_name
.name
, str
, len
))
593 * An insecure source has sent us a dentry, here we verify it.
595 * This is just to make knfsd able to have the dentry pointer
596 * in the NFS file handle.
598 * NOTE! Do _not_ dereference the pointers before we have
599 * validated them. We can test the pointer values, but we
600 * must not actually use them until we have found a valid
601 * copy of the pointer in kernel space..
603 int d_validate(struct dentry
*dentry
, struct dentry
*dparent
,
604 unsigned int hash
, unsigned int len
)
606 struct list_head
*base
, *lhp
;
609 if (dentry
!= dparent
) {
610 base
= d_hash(dparent
, hash
);
612 while ((lhp
= lhp
->next
) != base
) {
613 if (dentry
== list_entry(lhp
, struct dentry
, d_hash
))
618 * Special case: local mount points don't live in
619 * the hashes, so we search the super blocks.
621 struct super_block
*sb
= super_blocks
+ 0;
623 for (; sb
< super_blocks
+ NR_SUPER
; sb
++) {
626 if (sb
->s_root
== dentry
)
636 * When a file is deleted, we have two options:
637 * - turn this dentry into a negative dentry
638 * - unhash this dentry and free it.
640 * Usually, we want to just turn this into
641 * a negative dentry, but if anybody else is
642 * currently using the dentry or the inode
643 * we can't do that and we fall back on removing
644 * it from the hash queues and waiting for
645 * it to be deleted later when it has no users
647 void d_delete(struct dentry
* dentry
)
650 * Are we the only user?
652 if (dentry
->d_count
== 1) {
658 * If not, just drop the dentry and let dput
664 void d_add(struct dentry
* entry
, struct inode
* inode
)
666 struct dentry
* parent
= entry
->d_parent
;
668 list_add(&entry
->d_hash
, d_hash(parent
, entry
->d_name
.hash
));
669 d_instantiate(entry
, inode
);
672 #define switch(x,y) do { \
673 __typeof__ (x) __tmp = x; \
674 x = y; y = __tmp; } while (0)
677 * We cannibalize "target" when moving dentry on top of it,
678 * because it's going to be thrown away anyway. We could be more
679 * polite about it, though.
681 * This forceful removal will result in ugly /proc output if
682 * somebody holds a file open that got deleted due to a rename.
683 * We could be nicer about the deleted file, and let it show
684 * up under the name it got deleted rather than the name that
687 * Careful with the hash switch. The hash switch depends on
688 * the fact that any list-entry can be a head of the list.
691 void d_move(struct dentry
* dentry
, struct dentry
* target
)
693 if (!dentry
->d_inode
)
694 printk(KERN_WARNING
"VFS: moving negative dcache entry\n");
696 /* Move the dentry to the target hash queue */
697 list_del(&dentry
->d_hash
);
698 list_add(&dentry
->d_hash
, &target
->d_hash
);
700 /* Unhash the target: dput() will then get rid of it */
701 list_del(&target
->d_hash
);
702 INIT_LIST_HEAD(&target
->d_hash
);
704 list_del(&dentry
->d_child
);
705 list_del(&target
->d_child
);
707 /* Switch the parents and the names.. */
708 switch(dentry
->d_parent
, target
->d_parent
);
709 switch(dentry
->d_name
.name
, target
->d_name
.name
);
710 switch(dentry
->d_name
.len
, target
->d_name
.len
);
711 switch(dentry
->d_name
.hash
, target
->d_name
.hash
);
712 list_add(&target
->d_child
, &target
->d_parent
->d_subdirs
);
713 list_add(&dentry
->d_child
, &dentry
->d_parent
->d_subdirs
);
717 * "buflen" should be PAGE_SIZE or more.
719 char * d_path(struct dentry
*dentry
, char *buffer
, int buflen
)
721 char * end
= buffer
+buflen
;
723 struct dentry
* root
= current
->fs
->root
;
727 if (dentry
->d_parent
!= dentry
&& list_empty(&dentry
->d_hash
)) {
730 memcpy(end
, " (deleted)", 10);
738 struct dentry
* parent
;
743 dentry
= dentry
->d_covers
;
744 parent
= dentry
->d_parent
;
745 if (dentry
== parent
)
747 namelen
= dentry
->d_name
.len
;
748 buflen
-= namelen
+ 1;
752 memcpy(end
, dentry
->d_name
.name
, namelen
);
761 * Check whether a dentry already exists for the given name,
762 * and return the inode number if it has an inode.
764 * This routine is used to post-process directory listings for
765 * filesystems using synthetic inode numbers, and is necessary
766 * to keep getcwd() working.
768 ino_t
find_inode_number(struct dentry
*dir
, struct qstr
*name
)
770 struct dentry
* dentry
;
774 * Check for a fs-specific hash function. Note that we must
775 * calculate the standard hash first, as the d_op->d_hash()
776 * routine may choose to leave the hash value unchanged.
778 name
->hash
= full_name_hash(name
->name
, name
->len
);
779 if (dir
->d_op
&& dir
->d_op
->d_hash
)
781 if (dir
->d_op
->d_hash(dir
, name
) != 0)
785 dentry
= d_lookup(dir
, name
);
789 ino
= dentry
->d_inode
->i_ino
;
796 __initfunc(void dcache_init(void))
799 struct list_head
*d
= dentry_hashtable
;