patch-2_6_7-vs1_9_1_12
[linux-2.6.git] / fs / dcache.c
1 /*
2  * fs/dcache.c
3  *
4  * Complete reimplementation
5  * (C) 1997 Thomas Schoebel-Theuer,
6  * with heavy changes by Linus Torvalds
7  */
8
9 /*
10  * Notes on the allocation strategy:
11  *
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.
15  */
16
17 #include <linux/config.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/fs.h>
21 #include <linux/slab.h>
22 #include <linux/init.h>
23 #include <linux/smp_lock.h>
24 #include <linux/hash.h>
25 #include <linux/cache.h>
26 #include <linux/module.h>
27 #include <linux/mount.h>
28 #include <linux/file.h>
29 #include <asm/uaccess.h>
30 #include <linux/security.h>
31 #include <linux/seqlock.h>
32 #include <linux/swap.h>
33
34 #define DCACHE_PARANOIA 1
35 /* #define DCACHE_DEBUG 1 */
36
37 spinlock_t dcache_lock __cacheline_aligned_in_smp = SPIN_LOCK_UNLOCKED;
38 seqlock_t rename_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
39
40 EXPORT_SYMBOL(dcache_lock);
41
42 static kmem_cache_t *dentry_cache; 
43
44 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
45
46 /*
47  * This is the single most critical data structure when it comes
48  * to the dcache: the hashtable for lookups. Somebody should try
49  * to make this good - I've just made it work.
50  *
51  * This hash-function tries to avoid losing too many bits of hash
52  * information, yet avoid using a prime hash-size or similar.
53  */
54 #define D_HASHBITS     d_hash_shift
55 #define D_HASHMASK     d_hash_mask
56
57 static unsigned int d_hash_mask;
58 static unsigned int d_hash_shift;
59 static struct hlist_head *dentry_hashtable;
60 static LIST_HEAD(dentry_unused);
61
62 /* Statistics gathering. */
63 struct dentry_stat_t dentry_stat = {
64         .age_limit = 45,
65 };
66
67 static void d_callback(void *arg)
68 {
69         struct dentry * dentry = (struct dentry *)arg;
70
71         if (dname_external(dentry))
72                 kfree(dentry->d_name.name);
73         kmem_cache_free(dentry_cache, dentry); 
74 }
75
76 /*
77  * no dcache_lock, please.  The caller must decrement dentry_stat.nr_dentry
78  * inside dcache_lock.
79  */
80 static void d_free(struct dentry *dentry)
81 {
82         if (dentry->d_op && dentry->d_op->d_release)
83                 dentry->d_op->d_release(dentry);
84         call_rcu(&dentry->d_rcu, d_callback, dentry);
85 }
86
87 /*
88  * Release the dentry's inode, using the filesystem
89  * d_iput() operation if defined.
90  * Called with dcache_lock and per dentry lock held, drops both.
91  */
92 static inline void dentry_iput(struct dentry * dentry)
93 {
94         struct inode *inode = dentry->d_inode;
95         if (inode) {
96                 dentry->d_inode = NULL;
97                 list_del_init(&dentry->d_alias);
98                 spin_unlock(&dentry->d_lock);
99                 spin_unlock(&dcache_lock);
100                 if (dentry->d_op && dentry->d_op->d_iput)
101                         dentry->d_op->d_iput(dentry, inode);
102                 else
103                         iput(inode);
104         } else {
105                 spin_unlock(&dentry->d_lock);
106                 spin_unlock(&dcache_lock);
107         }
108 }
109
110 /* 
111  * This is dput
112  *
113  * This is complicated by the fact that we do not want to put
114  * dentries that are no longer on any hash chain on the unused
115  * list: we'd much rather just get rid of them immediately.
116  *
117  * However, that implies that we have to traverse the dentry
118  * tree upwards to the parents which might _also_ now be
119  * scheduled for deletion (it may have been only waiting for
120  * its last child to go away).
121  *
122  * This tail recursion is done by hand as we don't want to depend
123  * on the compiler to always get this right (gcc generally doesn't).
124  * Real recursion would eat up our stack space.
125  */
126
127 /*
128  * dput - release a dentry
129  * @dentry: dentry to release 
130  *
131  * Release a dentry. This will drop the usage count and if appropriate
132  * call the dentry unlink method as well as removing it from the queues and
133  * releasing its resources. If the parent dentries were scheduled for release
134  * they too may now get deleted.
135  *
136  * no dcache lock, please.
137  */
138
139 void dput(struct dentry *dentry)
140 {
141         if (!dentry)
142                 return;
143
144 repeat:
145         if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
146                 return;
147
148         spin_lock(&dentry->d_lock);
149         if (atomic_read(&dentry->d_count)) {
150                 spin_unlock(&dentry->d_lock);
151                 spin_unlock(&dcache_lock);
152                 return;
153         }
154                         
155         /*
156          * AV: ->d_delete() is _NOT_ allowed to block now.
157          */
158         if (dentry->d_op && dentry->d_op->d_delete) {
159                 if (dentry->d_op->d_delete(dentry))
160                         goto unhash_it;
161         }
162         /* Unreachable? Get rid of it */
163         if (d_unhashed(dentry))
164                 goto kill_it;
165         if (list_empty(&dentry->d_lru)) {
166                 dentry->d_flags |= DCACHE_REFERENCED;
167                 list_add(&dentry->d_lru, &dentry_unused);
168                 dentry_stat.nr_unused++;
169         }
170         spin_unlock(&dentry->d_lock);
171         spin_unlock(&dcache_lock);
172         return;
173
174 unhash_it:
175         __d_drop(dentry);
176
177 kill_it: {
178                 struct dentry *parent;
179
180                 /* If dentry was on d_lru list
181                  * delete it from there
182                  */
183                 if (!list_empty(&dentry->d_lru)) {
184                         list_del(&dentry->d_lru);
185                         dentry_stat.nr_unused--;
186                 }
187                 list_del(&dentry->d_child);
188                 dentry_stat.nr_dentry--;        /* For d_free, below */
189                 /*drops the locks, at that point nobody can reach this dentry */
190                 dentry_iput(dentry);
191                 parent = dentry->d_parent;
192                 d_free(dentry);
193                 if (dentry == parent)
194                         return;
195                 dentry = parent;
196                 goto repeat;
197         }
198 }
199
200 /**
201  * d_invalidate - invalidate a dentry
202  * @dentry: dentry to invalidate
203  *
204  * Try to invalidate the dentry if it turns out to be
205  * possible. If there are other dentries that can be
206  * reached through this one we can't delete it and we
207  * return -EBUSY. On success we return 0.
208  *
209  * no dcache lock.
210  */
211  
212 int d_invalidate(struct dentry * dentry)
213 {
214         /*
215          * If it's already been dropped, return OK.
216          */
217         spin_lock(&dcache_lock);
218         if (d_unhashed(dentry)) {
219                 spin_unlock(&dcache_lock);
220                 return 0;
221         }
222         /*
223          * Check whether to do a partial shrink_dcache
224          * to get rid of unused child entries.
225          */
226         if (!list_empty(&dentry->d_subdirs)) {
227                 spin_unlock(&dcache_lock);
228                 shrink_dcache_parent(dentry);
229                 spin_lock(&dcache_lock);
230         }
231
232         /*
233          * Somebody else still using it?
234          *
235          * If it's a directory, we can't drop it
236          * for fear of somebody re-populating it
237          * with children (even though dropping it
238          * would make it unreachable from the root,
239          * we might still populate it if it was a
240          * working directory or similar).
241          */
242         spin_lock(&dentry->d_lock);
243         if (atomic_read(&dentry->d_count) > 1) {
244                 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
245                         spin_unlock(&dentry->d_lock);
246                         spin_unlock(&dcache_lock);
247                         return -EBUSY;
248                 }
249         }
250
251         __d_drop(dentry);
252         spin_unlock(&dentry->d_lock);
253         spin_unlock(&dcache_lock);
254         return 0;
255 }
256
257 /* This should be called _only_ with dcache_lock held */
258
259 static inline struct dentry * __dget_locked(struct dentry *dentry)
260 {
261         atomic_inc(&dentry->d_count);
262         if (!list_empty(&dentry->d_lru)) {
263                 dentry_stat.nr_unused--;
264                 list_del_init(&dentry->d_lru);
265         }
266         return dentry;
267 }
268
269 struct dentry * dget_locked(struct dentry *dentry)
270 {
271         return __dget_locked(dentry);
272 }
273
274 /**
275  * d_find_alias - grab a hashed alias of inode
276  * @inode: inode in question
277  *
278  * If inode has a hashed alias - acquire the reference to alias and
279  * return it. Otherwise return NULL. Notice that if inode is a directory
280  * there can be only one alias and it can be unhashed only if it has
281  * no children.
282  *
283  * If the inode has a DCACHE_DISCONNECTED alias, then prefer
284  * any other hashed alias over that one.
285  */
286
287 struct dentry * d_find_alias(struct inode *inode)
288 {
289         struct list_head *head, *next, *tmp;
290         struct dentry *alias, *discon_alias=NULL;
291
292         spin_lock(&dcache_lock);
293         head = &inode->i_dentry;
294         next = inode->i_dentry.next;
295         while (next != head) {
296                 tmp = next;
297                 next = tmp->next;
298                 prefetch(next);
299                 alias = list_entry(tmp, struct dentry, d_alias);
300                 if (!d_unhashed(alias)) {
301                         if (alias->d_flags & DCACHE_DISCONNECTED)
302                                 discon_alias = alias;
303                         else {
304                                 __dget_locked(alias);
305                                 spin_unlock(&dcache_lock);
306                                 return alias;
307                         }
308                 }
309         }
310         if (discon_alias)
311                 __dget_locked(discon_alias);
312         spin_unlock(&dcache_lock);
313         return discon_alias;
314 }
315
316 /*
317  *      Try to kill dentries associated with this inode.
318  * WARNING: you must own a reference to inode.
319  */
320 void d_prune_aliases(struct inode *inode)
321 {
322         struct list_head *tmp, *head = &inode->i_dentry;
323 restart:
324         spin_lock(&dcache_lock);
325         tmp = head;
326         while ((tmp = tmp->next) != head) {
327                 struct dentry *dentry = list_entry(tmp, struct dentry, d_alias);
328                 if (!atomic_read(&dentry->d_count)) {
329                         __dget_locked(dentry);
330                         __d_drop(dentry);
331                         spin_unlock(&dcache_lock);
332                         dput(dentry);
333                         goto restart;
334                 }
335         }
336         spin_unlock(&dcache_lock);
337 }
338
339 /*
340  * Throw away a dentry - free the inode, dput the parent.
341  * This requires that the LRU list has already been
342  * removed.
343  * Called with dcache_lock, drops it and then regains.
344  */
345 static inline void prune_one_dentry(struct dentry * dentry)
346 {
347         struct dentry * parent;
348
349         __d_drop(dentry);
350         list_del(&dentry->d_child);
351         dentry_stat.nr_dentry--;        /* For d_free, below */
352         dentry_iput(dentry);
353         parent = dentry->d_parent;
354         d_free(dentry);
355         if (parent != dentry)
356                 dput(parent);
357         spin_lock(&dcache_lock);
358 }
359
360 /**
361  * prune_dcache - shrink the dcache
362  * @count: number of entries to try and free
363  *
364  * Shrink the dcache. This is done when we need
365  * more memory, or simply when we need to unmount
366  * something (at which point we need to unuse
367  * all dentries).
368  *
369  * This function may fail to free any resources if
370  * all the dentries are in use.
371  */
372  
373 static void prune_dcache(int count)
374 {
375         spin_lock(&dcache_lock);
376         for (; count ; count--) {
377                 struct dentry *dentry;
378                 struct list_head *tmp;
379
380                 tmp = dentry_unused.prev;
381                 if (tmp == &dentry_unused)
382                         break;
383                 list_del_init(tmp);
384                 prefetch(dentry_unused.prev);
385                 dentry_stat.nr_unused--;
386                 dentry = list_entry(tmp, struct dentry, d_lru);
387
388                 spin_lock(&dentry->d_lock);
389                 /*
390                  * We found an inuse dentry which was not removed from
391                  * dentry_unused because of laziness during lookup.  Do not free
392                  * it - just keep it off the dentry_unused list.
393                  */
394                 if (atomic_read(&dentry->d_count)) {
395                         spin_unlock(&dentry->d_lock);
396                         continue;
397                 }
398                 /* If the dentry was recently referenced, don't free it. */
399                 if (dentry->d_flags & DCACHE_REFERENCED) {
400                         dentry->d_flags &= ~DCACHE_REFERENCED;
401                         list_add(&dentry->d_lru, &dentry_unused);
402                         dentry_stat.nr_unused++;
403                         spin_unlock(&dentry->d_lock);
404                         continue;
405                 }
406                 prune_one_dentry(dentry);
407         }
408         spin_unlock(&dcache_lock);
409 }
410
411 /*
412  * Shrink the dcache for the specified super block.
413  * This allows us to unmount a device without disturbing
414  * the dcache for the other devices.
415  *
416  * This implementation makes just two traversals of the
417  * unused list.  On the first pass we move the selected
418  * dentries to the most recent end, and on the second
419  * pass we free them.  The second pass must restart after
420  * each dput(), but since the target dentries are all at
421  * the end, it's really just a single traversal.
422  */
423
424 /**
425  * shrink_dcache_sb - shrink dcache for a superblock
426  * @sb: superblock
427  *
428  * Shrink the dcache for the specified super block. This
429  * is used to free the dcache before unmounting a file
430  * system
431  */
432
433 void shrink_dcache_sb(struct super_block * sb)
434 {
435         struct list_head *tmp, *next;
436         struct dentry *dentry;
437
438         /*
439          * Pass one ... move the dentries for the specified
440          * superblock to the most recent end of the unused list.
441          */
442         spin_lock(&dcache_lock);
443         next = dentry_unused.next;
444         while (next != &dentry_unused) {
445                 tmp = next;
446                 next = tmp->next;
447                 dentry = list_entry(tmp, struct dentry, d_lru);
448                 if (dentry->d_sb != sb)
449                         continue;
450                 list_del(tmp);
451                 list_add(tmp, &dentry_unused);
452         }
453
454         /*
455          * Pass two ... free the dentries for this superblock.
456          */
457 repeat:
458         next = dentry_unused.next;
459         while (next != &dentry_unused) {
460                 tmp = next;
461                 next = tmp->next;
462                 dentry = list_entry(tmp, struct dentry, d_lru);
463                 if (dentry->d_sb != sb)
464                         continue;
465                 dentry_stat.nr_unused--;
466                 list_del_init(tmp);
467                 spin_lock(&dentry->d_lock);
468                 if (atomic_read(&dentry->d_count)) {
469                         spin_unlock(&dentry->d_lock);
470                         continue;
471                 }
472                 prune_one_dentry(dentry);
473                 goto repeat;
474         }
475         spin_unlock(&dcache_lock);
476 }
477
478 /*
479  * Search for at least 1 mount point in the dentry's subdirs.
480  * We descend to the next level whenever the d_subdirs
481  * list is non-empty and continue searching.
482  */
483  
484 /**
485  * have_submounts - check for mounts over a dentry
486  * @parent: dentry to check.
487  *
488  * Return true if the parent or its subdirectories contain
489  * a mount point
490  */
491  
492 int have_submounts(struct dentry *parent)
493 {
494         struct dentry *this_parent = parent;
495         struct list_head *next;
496
497         spin_lock(&dcache_lock);
498         if (d_mountpoint(parent))
499                 goto positive;
500 repeat:
501         next = this_parent->d_subdirs.next;
502 resume:
503         while (next != &this_parent->d_subdirs) {
504                 struct list_head *tmp = next;
505                 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
506                 next = tmp->next;
507                 /* Have we found a mount point ? */
508                 if (d_mountpoint(dentry))
509                         goto positive;
510                 if (!list_empty(&dentry->d_subdirs)) {
511                         this_parent = dentry;
512                         goto repeat;
513                 }
514         }
515         /*
516          * All done at this level ... ascend and resume the search.
517          */
518         if (this_parent != parent) {
519                 next = this_parent->d_child.next; 
520                 this_parent = this_parent->d_parent;
521                 goto resume;
522         }
523         spin_unlock(&dcache_lock);
524         return 0; /* No mount points found in tree */
525 positive:
526         spin_unlock(&dcache_lock);
527         return 1;
528 }
529
530 /*
531  * Search the dentry child list for the specified parent,
532  * and move any unused dentries to the end of the unused
533  * list for prune_dcache(). We descend to the next level
534  * whenever the d_subdirs list is non-empty and continue
535  * searching.
536  */
537 static int select_parent(struct dentry * parent)
538 {
539         struct dentry *this_parent = parent;
540         struct list_head *next;
541         int found = 0;
542
543         spin_lock(&dcache_lock);
544 repeat:
545         next = this_parent->d_subdirs.next;
546 resume:
547         while (next != &this_parent->d_subdirs) {
548                 struct list_head *tmp = next;
549                 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
550                 next = tmp->next;
551
552                 if (!list_empty(&dentry->d_lru)) {
553                         dentry_stat.nr_unused--;
554                         list_del_init(&dentry->d_lru);
555                 }
556                 /* 
557                  * move only zero ref count dentries to the end 
558                  * of the unused list for prune_dcache
559                  */
560                 if (!atomic_read(&dentry->d_count)) {
561                         list_add(&dentry->d_lru, dentry_unused.prev);
562                         dentry_stat.nr_unused++;
563                         found++;
564                 }
565                 /*
566                  * Descend a level if the d_subdirs list is non-empty.
567                  */
568                 if (!list_empty(&dentry->d_subdirs)) {
569                         this_parent = dentry;
570 #ifdef DCACHE_DEBUG
571 printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
572 dentry->d_parent->d_name.name, dentry->d_name.name, found);
573 #endif
574                         goto repeat;
575                 }
576         }
577         /*
578          * All done at this level ... ascend and resume the search.
579          */
580         if (this_parent != parent) {
581                 next = this_parent->d_child.next; 
582                 this_parent = this_parent->d_parent;
583 #ifdef DCACHE_DEBUG
584 printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
585 this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
586 #endif
587                 goto resume;
588         }
589         spin_unlock(&dcache_lock);
590         return found;
591 }
592
593 /**
594  * shrink_dcache_parent - prune dcache
595  * @parent: parent of entries to prune
596  *
597  * Prune the dcache to remove unused children of the parent dentry.
598  */
599  
600 void shrink_dcache_parent(struct dentry * parent)
601 {
602         int found;
603
604         while ((found = select_parent(parent)) != 0)
605                 prune_dcache(found);
606 }
607
608 /**
609  * shrink_dcache_anon - further prune the cache
610  * @head: head of d_hash list of dentries to prune
611  *
612  * Prune the dentries that are anonymous
613  *
614  * parsing d_hash list does not read_barrier_depends() as it
615  * done under dcache_lock.
616  *
617  */
618 void shrink_dcache_anon(struct hlist_head *head)
619 {
620         struct hlist_node *lp;
621         int found;
622         do {
623                 found = 0;
624                 spin_lock(&dcache_lock);
625                 hlist_for_each(lp, head) {
626                         struct dentry *this = hlist_entry(lp, struct dentry, d_hash);
627                         if (!list_empty(&this->d_lru)) {
628                                 dentry_stat.nr_unused--;
629                                 list_del(&this->d_lru);
630                         }
631
632                         /* 
633                          * move only zero ref count dentries to the end 
634                          * of the unused list for prune_dcache
635                          */
636                         if (!atomic_read(&this->d_count)) {
637                                 list_add_tail(&this->d_lru, &dentry_unused);
638                                 dentry_stat.nr_unused++;
639                                 found++;
640                         }
641                 }
642                 spin_unlock(&dcache_lock);
643                 prune_dcache(found);
644         } while(found);
645 }
646
647 /*
648  * Scan `nr' dentries and return the number which remain.
649  *
650  * We need to avoid reentering the filesystem if the caller is performing a
651  * GFP_NOFS allocation attempt.  One example deadlock is:
652  *
653  * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
654  * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
655  * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
656  *
657  * In this case we return -1 to tell the caller that we baled.
658  */
659 static int shrink_dcache_memory(int nr, unsigned int gfp_mask)
660 {
661         if (nr) {
662                 if (!(gfp_mask & __GFP_FS))
663                         return -1;
664                 prune_dcache(nr);
665         }
666         return dentry_stat.nr_unused;
667 }
668
669 /**
670  * d_alloc      -       allocate a dcache entry
671  * @parent: parent of entry to allocate
672  * @name: qstr of the name
673  *
674  * Allocates a dentry. It returns %NULL if there is insufficient memory
675  * available. On a success the dentry is returned. The name passed in is
676  * copied and the copy passed in may be reused after this call.
677  */
678  
679 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
680 {
681         struct dentry *dentry;
682         char *dname;
683
684         dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL); 
685         if (!dentry)
686                 return NULL;
687
688         if (name->len > DNAME_INLINE_LEN-1) {
689                 dname = kmalloc(name->len + 1, GFP_KERNEL);
690                 if (!dname) {
691                         kmem_cache_free(dentry_cache, dentry); 
692                         return NULL;
693                 }
694         } else  {
695                 dname = dentry->d_iname;
696         }       
697         dentry->d_name.name = dname;
698
699         dentry->d_name.len = name->len;
700         dentry->d_name.hash = name->hash;
701         memcpy(dname, name->name, name->len);
702         dname[name->len] = 0;
703
704         atomic_set(&dentry->d_count, 1);
705         dentry->d_flags = DCACHE_UNHASHED;
706         dentry->d_lock = SPIN_LOCK_UNLOCKED;
707         dentry->d_inode = NULL;
708         dentry->d_parent = NULL;
709         dentry->d_sb = NULL;
710         dentry->d_op = NULL;
711         dentry->d_fsdata = NULL;
712         dentry->d_mounted = 0;
713         dentry->d_cookie = NULL;
714         dentry->d_bucket = NULL;
715         INIT_HLIST_NODE(&dentry->d_hash);
716         INIT_LIST_HEAD(&dentry->d_lru);
717         INIT_LIST_HEAD(&dentry->d_subdirs);
718         INIT_LIST_HEAD(&dentry->d_alias);
719
720         if (parent) {
721                 dentry->d_parent = dget(parent);
722                 dentry->d_sb = parent->d_sb;
723         } else {
724                 INIT_LIST_HEAD(&dentry->d_child);
725         }
726
727         spin_lock(&dcache_lock);
728         if (parent)
729                 list_add(&dentry->d_child, &parent->d_subdirs);
730         dentry_stat.nr_dentry++;
731         spin_unlock(&dcache_lock);
732
733         return dentry;
734 }
735
736 /**
737  * d_instantiate - fill in inode information for a dentry
738  * @entry: dentry to complete
739  * @inode: inode to attach to this dentry
740  *
741  * Fill in inode information in the entry.
742  *
743  * This turns negative dentries into productive full members
744  * of society.
745  *
746  * NOTE! This assumes that the inode count has been incremented
747  * (or otherwise set) by the caller to indicate that it is now
748  * in use by the dcache.
749  */
750  
751 void d_instantiate(struct dentry *entry, struct inode * inode)
752 {
753         if (!list_empty(&entry->d_alias)) BUG();
754         spin_lock(&dcache_lock);
755         if (inode)
756                 list_add(&entry->d_alias, &inode->i_dentry);
757         entry->d_inode = inode;
758         spin_unlock(&dcache_lock);
759         security_d_instantiate(entry, inode);
760 }
761
762 /**
763  * d_alloc_root - allocate root dentry
764  * @root_inode: inode to allocate the root for
765  *
766  * Allocate a root ("/") dentry for the inode given. The inode is
767  * instantiated and returned. %NULL is returned if there is insufficient
768  * memory or the inode passed is %NULL.
769  */
770  
771 struct dentry * d_alloc_root(struct inode * root_inode)
772 {
773         struct dentry *res = NULL;
774
775         if (root_inode) {
776                 static const struct qstr name = { .name = "/", .len = 1 };
777
778                 res = d_alloc(NULL, &name);
779                 if (res) {
780                         res->d_sb = root_inode->i_sb;
781                         res->d_parent = res;
782                         d_instantiate(res, root_inode);
783                 }
784         }
785         return res;
786 }
787
788 static inline struct hlist_head *d_hash(struct dentry *parent,
789                                         unsigned long hash)
790 {
791         hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
792         hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
793         return dentry_hashtable + (hash & D_HASHMASK);
794 }
795
796 /**
797  * d_alloc_anon - allocate an anonymous dentry
798  * @inode: inode to allocate the dentry for
799  *
800  * This is similar to d_alloc_root.  It is used by filesystems when
801  * creating a dentry for a given inode, often in the process of 
802  * mapping a filehandle to a dentry.  The returned dentry may be
803  * anonymous, or may have a full name (if the inode was already
804  * in the cache).  The file system may need to make further
805  * efforts to connect this dentry into the dcache properly.
806  *
807  * When called on a directory inode, we must ensure that
808  * the inode only ever has one dentry.  If a dentry is
809  * found, that is returned instead of allocating a new one.
810  *
811  * On successful return, the reference to the inode has been transferred
812  * to the dentry.  If %NULL is returned (indicating kmalloc failure),
813  * the reference on the inode has not been released.
814  */
815
816 struct dentry * d_alloc_anon(struct inode *inode)
817 {
818         static const struct qstr anonstring = { .name = "" };
819         struct dentry *tmp;
820         struct dentry *res;
821
822         if ((res = d_find_alias(inode))) {
823                 iput(inode);
824                 return res;
825         }
826
827         tmp = d_alloc(NULL, &anonstring);
828         if (!tmp)
829                 return NULL;
830
831         tmp->d_parent = tmp; /* make sure dput doesn't croak */
832         
833         spin_lock(&dcache_lock);
834         if (S_ISDIR(inode->i_mode) && !list_empty(&inode->i_dentry)) {
835                 /* A directory can only have one dentry.
836                  * This (now) has one, so use it.
837                  */
838                 res = list_entry(inode->i_dentry.next, struct dentry, d_alias);
839                 __dget_locked(res);
840         } else {
841                 /* attach a disconnected dentry */
842                 res = tmp;
843                 tmp = NULL;
844                 if (res) {
845                         spin_lock(&res->d_lock);
846                         res->d_sb = inode->i_sb;
847                         res->d_parent = res;
848                         res->d_inode = inode;
849
850                         /*
851                          * Set d_bucket to an "impossible" bucket address so
852                          * that d_move() doesn't get a false positive
853                          */
854                         res->d_bucket = NULL;
855                         res->d_flags |= DCACHE_DISCONNECTED;
856                         res->d_flags &= ~DCACHE_UNHASHED;
857                         list_add(&res->d_alias, &inode->i_dentry);
858                         hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
859                         spin_unlock(&res->d_lock);
860                 }
861                 inode = NULL; /* don't drop reference */
862         }
863         spin_unlock(&dcache_lock);
864
865         if (inode)
866                 iput(inode);
867         if (tmp)
868                 dput(tmp);
869         return res;
870 }
871
872
873 /**
874  * d_splice_alias - splice a disconnected dentry into the tree if one exists
875  * @inode:  the inode which may have a disconnected dentry
876  * @dentry: a negative dentry which we want to point to the inode.
877  *
878  * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
879  * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
880  * and return it, else simply d_add the inode to the dentry and return NULL.
881  *
882  * This is (will be) needed in the lookup routine of any filesystem that is exportable
883  * (via knfsd) so that we can build dcache paths to directories effectively.
884  *
885  * If a dentry was found and moved, then it is returned.  Otherwise NULL
886  * is returned.  This matches the expected return value of ->lookup.
887  *
888  */
889 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
890 {
891         struct dentry *new = NULL;
892
893         if (inode && S_ISDIR(inode->i_mode)) {
894                 spin_lock(&dcache_lock);
895                 if (!list_empty(&inode->i_dentry)) {
896                         new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
897                         __dget_locked(new);
898                         spin_unlock(&dcache_lock);
899                         security_d_instantiate(new, inode);
900                         d_rehash(dentry);
901                         d_move(new, dentry);
902                         iput(inode);
903                 } else {
904                         /* d_instantiate takes dcache_lock, so we do it by hand */
905                         list_add(&dentry->d_alias, &inode->i_dentry);
906                         dentry->d_inode = inode;
907                         spin_unlock(&dcache_lock);
908                         security_d_instantiate(dentry, inode);
909                         d_rehash(dentry);
910                 }
911         } else
912                 d_add(dentry, inode);
913         return new;
914 }
915
916
917 /**
918  * d_lookup - search for a dentry
919  * @parent: parent dentry
920  * @name: qstr of name we wish to find
921  *
922  * Searches the children of the parent dentry for the name in question. If
923  * the dentry is found its reference count is incremented and the dentry
924  * is returned. The caller must use d_put to free the entry when it has
925  * finished using it. %NULL is returned on failure.
926  *
927  * __d_lookup is dcache_lock free. The hash list is protected using RCU.
928  * Memory barriers are used while updating and doing lockless traversal. 
929  * To avoid races with d_move while rename is happening, d_lock is used.
930  *
931  * Overflows in memcmp(), while d_move, are avoided by keeping the length
932  * and name pointer in one structure pointed by d_qstr.
933  *
934  * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
935  * lookup is going on.
936  *
937  * dentry_unused list is not updated even if lookup finds the required dentry
938  * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
939  * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
940  * acquisition.
941  *
942  * d_lookup() is protected against the concurrent renames in some unrelated
943  * directory using the seqlockt_t rename_lock.
944  */
945
946 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
947 {
948         struct dentry * dentry = NULL;
949         unsigned long seq;
950
951         do {
952                 seq = read_seqbegin(&rename_lock);
953                 dentry = __d_lookup(parent, name);
954                 if (dentry)
955                         break;
956         } while (read_seqretry(&rename_lock, seq));
957         return dentry;
958 }
959
960 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
961 {
962         unsigned int len = name->len;
963         unsigned int hash = name->hash;
964         const unsigned char *str = name->name;
965         struct hlist_head *head = d_hash(parent,hash);
966         struct dentry *found = NULL;
967         struct hlist_node *node;
968
969         rcu_read_lock();
970         
971         hlist_for_each (node, head) { 
972                 struct dentry *dentry; 
973                 struct qstr *qstr;
974
975                 smp_read_barrier_depends();
976                 dentry = hlist_entry(node, struct dentry, d_hash);
977
978                 smp_rmb();
979
980                 if (dentry->d_name.hash != hash)
981                         continue;
982                 if (dentry->d_parent != parent)
983                         continue;
984
985                 spin_lock(&dentry->d_lock);
986
987                 /*
988                  * If lookup ends up in a different bucket due to concurrent
989                  * rename, fail it
990                  */
991                 if (unlikely(dentry->d_bucket != head))
992                         goto terminate;
993
994                 /*
995                  * Recheck the dentry after taking the lock - d_move may have
996                  * changed things.  Don't bother checking the hash because we're
997                  * about to compare the whole name anyway.
998                  */
999                 if (dentry->d_parent != parent)
1000                         goto next;
1001
1002                 qstr = &dentry->d_name;
1003                 smp_read_barrier_depends();
1004                 if (parent->d_op && parent->d_op->d_compare) {
1005                         if (parent->d_op->d_compare(parent, qstr, name))
1006                                 goto next;
1007                 } else {
1008                         if (qstr->len != len)
1009                                 goto next;
1010                         if (memcmp(qstr->name, str, len))
1011                                 goto next;
1012                 }
1013
1014                 if (!d_unhashed(dentry)) {
1015                         atomic_inc(&dentry->d_count);
1016                         found = dentry;
1017                 }
1018 terminate:
1019                 spin_unlock(&dentry->d_lock);
1020                 break;
1021 next:
1022                 spin_unlock(&dentry->d_lock);
1023         }
1024         rcu_read_unlock();
1025
1026         return found;
1027 }
1028
1029 /**
1030  * d_validate - verify dentry provided from insecure source
1031  * @dentry: The dentry alleged to be valid child of @dparent
1032  * @dparent: The parent dentry (known to be valid)
1033  * @hash: Hash of the dentry
1034  * @len: Length of the name
1035  *
1036  * An insecure source has sent us a dentry, here we verify it and dget() it.
1037  * This is used by ncpfs in its readdir implementation.
1038  * Zero is returned in the dentry is invalid.
1039  */
1040  
1041 int d_validate(struct dentry *dentry, struct dentry *dparent)
1042 {
1043         struct hlist_head *base;
1044         struct hlist_node *lhp;
1045
1046         /* Check whether the ptr might be valid at all.. */
1047         if (!kmem_ptr_validate(dentry_cache, dentry))
1048                 goto out;
1049
1050         if (dentry->d_parent != dparent)
1051                 goto out;
1052
1053         spin_lock(&dcache_lock);
1054         base = d_hash(dparent, dentry->d_name.hash);
1055         hlist_for_each(lhp,base) { 
1056                 /* read_barrier_depends() not required for d_hash list
1057                  * as it is parsed under dcache_lock
1058                  */
1059                 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1060                         __dget_locked(dentry);
1061                         spin_unlock(&dcache_lock);
1062                         return 1;
1063                 }
1064         }
1065         spin_unlock(&dcache_lock);
1066 out:
1067         return 0;
1068 }
1069
1070 /*
1071  * When a file is deleted, we have two options:
1072  * - turn this dentry into a negative dentry
1073  * - unhash this dentry and free it.
1074  *
1075  * Usually, we want to just turn this into
1076  * a negative dentry, but if anybody else is
1077  * currently using the dentry or the inode
1078  * we can't do that and we fall back on removing
1079  * it from the hash queues and waiting for
1080  * it to be deleted later when it has no users
1081  */
1082  
1083 /**
1084  * d_delete - delete a dentry
1085  * @dentry: The dentry to delete
1086  *
1087  * Turn the dentry into a negative dentry if possible, otherwise
1088  * remove it from the hash queues so it can be deleted later
1089  */
1090  
1091 void d_delete(struct dentry * dentry)
1092 {
1093         /*
1094          * Are we the only user?
1095          */
1096         spin_lock(&dcache_lock);
1097         spin_lock(&dentry->d_lock);
1098         if (atomic_read(&dentry->d_count) == 1) {
1099                 dentry_iput(dentry);
1100                 return;
1101         }
1102
1103         if (!d_unhashed(dentry))
1104                 __d_drop(dentry);
1105
1106         spin_unlock(&dentry->d_lock);
1107         spin_unlock(&dcache_lock);
1108 }
1109
1110 /**
1111  * d_rehash     - add an entry back to the hash
1112  * @entry: dentry to add to the hash
1113  *
1114  * Adds a dentry to the hash according to its name.
1115  */
1116  
1117 void d_rehash(struct dentry * entry)
1118 {
1119         struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1120
1121         spin_lock(&dcache_lock);
1122         spin_lock(&entry->d_lock);
1123         entry->d_flags &= ~DCACHE_UNHASHED;
1124         spin_unlock(&entry->d_lock);
1125         entry->d_bucket = list;
1126         hlist_add_head_rcu(&entry->d_hash, list);
1127         spin_unlock(&dcache_lock);
1128 }
1129
1130 #define do_switch(x,y) do { \
1131         __typeof__ (x) __tmp = x; \
1132         x = y; y = __tmp; } while (0)
1133
1134 /*
1135  * When switching names, the actual string doesn't strictly have to
1136  * be preserved in the target - because we're dropping the target
1137  * anyway. As such, we can just do a simple memcpy() to copy over
1138  * the new name before we switch.
1139  *
1140  * Note that we have to be a lot more careful about getting the hash
1141  * switched - we have to switch the hash value properly even if it
1142  * then no longer matches the actual (corrupted) string of the target.
1143  * The hash value has to match the hash queue that the dentry is on..
1144  */
1145 static void switch_names(struct dentry *dentry, struct dentry *target)
1146 {
1147         if (dname_external(target)) {
1148                 if (dname_external(dentry)) {
1149                         /*
1150                          * Both external: swap the pointers
1151                          */
1152                         do_switch(target->d_name.name, dentry->d_name.name);
1153                 } else {
1154                         /*
1155                          * dentry:internal, target:external.  Steal target's
1156                          * storage and make target internal.
1157                          */
1158                         dentry->d_name.name = target->d_name.name;
1159                         target->d_name.name = target->d_iname;
1160                 }
1161         } else {
1162                 if (dname_external(dentry)) {
1163                         /*
1164                          * dentry:external, target:internal.  Give dentry's
1165                          * storage to target and make dentry internal
1166                          */
1167                         memcpy(dentry->d_iname, target->d_name.name,
1168                                         target->d_name.len + 1);
1169                         target->d_name.name = dentry->d_name.name;
1170                         dentry->d_name.name = dentry->d_iname;
1171                 } else {
1172                         /*
1173                          * Both are internal.  Just copy target to dentry
1174                          */
1175                         memcpy(dentry->d_iname, target->d_name.name,
1176                                         target->d_name.len + 1);
1177                 }
1178         }
1179 }
1180
1181 /*
1182  * We cannibalize "target" when moving dentry on top of it,
1183  * because it's going to be thrown away anyway. We could be more
1184  * polite about it, though.
1185  *
1186  * This forceful removal will result in ugly /proc output if
1187  * somebody holds a file open that got deleted due to a rename.
1188  * We could be nicer about the deleted file, and let it show
1189  * up under the name it got deleted rather than the name that
1190  * deleted it.
1191  */
1192  
1193 /**
1194  * d_move - move a dentry
1195  * @dentry: entry to move
1196  * @target: new dentry
1197  *
1198  * Update the dcache to reflect the move of a file name. Negative
1199  * dcache entries should not be moved in this way.
1200  */
1201
1202 void d_move(struct dentry * dentry, struct dentry * target)
1203 {
1204         if (!dentry->d_inode)
1205                 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1206
1207         spin_lock(&dcache_lock);
1208         write_seqlock(&rename_lock);
1209         /*
1210          * XXXX: do we really need to take target->d_lock?
1211          */
1212         if (target < dentry) {
1213                 spin_lock(&target->d_lock);
1214                 spin_lock(&dentry->d_lock);
1215         } else {
1216                 spin_lock(&dentry->d_lock);
1217                 spin_lock(&target->d_lock);
1218         }
1219
1220         /* Move the dentry to the target hash queue, if on different bucket */
1221         if (dentry->d_flags & DCACHE_UNHASHED)
1222                 goto already_unhashed;
1223         if (dentry->d_bucket != target->d_bucket) {
1224                 hlist_del_rcu(&dentry->d_hash);
1225 already_unhashed:
1226                 dentry->d_bucket = target->d_bucket;
1227                 hlist_add_head_rcu(&dentry->d_hash, target->d_bucket);
1228                 dentry->d_flags &= ~DCACHE_UNHASHED;
1229         }
1230
1231         /* Unhash the target: dput() will then get rid of it */
1232         __d_drop(target);
1233
1234         list_del(&dentry->d_child);
1235         list_del(&target->d_child);
1236
1237         /* Switch the names.. */
1238         switch_names(dentry, target);
1239         smp_wmb();
1240         do_switch(dentry->d_name.len, target->d_name.len);
1241         do_switch(dentry->d_name.hash, target->d_name.hash);
1242
1243         /* ... and switch the parents */
1244         if (IS_ROOT(dentry)) {
1245                 dentry->d_parent = target->d_parent;
1246                 target->d_parent = target;
1247                 INIT_LIST_HEAD(&target->d_child);
1248         } else {
1249                 do_switch(dentry->d_parent, target->d_parent);
1250
1251                 /* And add them back to the (new) parent lists */
1252                 list_add(&target->d_child, &target->d_parent->d_subdirs);
1253         }
1254
1255         list_add(&dentry->d_child, &dentry->d_parent->d_subdirs);
1256         spin_unlock(&target->d_lock);
1257         spin_unlock(&dentry->d_lock);
1258         write_sequnlock(&rename_lock);
1259         spin_unlock(&dcache_lock);
1260 }
1261
1262 /**
1263  * d_path - return the path of a dentry
1264  * @dentry: dentry to report
1265  * @vfsmnt: vfsmnt to which the dentry belongs
1266  * @root: root dentry
1267  * @rootmnt: vfsmnt to which the root dentry belongs
1268  * @buffer: buffer to return value in
1269  * @buflen: buffer length
1270  *
1271  * Convert a dentry into an ASCII path name. If the entry has been deleted
1272  * the string " (deleted)" is appended. Note that this is ambiguous.
1273  *
1274  * Returns the buffer or an error code if the path was too long.
1275  *
1276  * "buflen" should be positive. Caller holds the dcache_lock.
1277  */
1278 static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1279                         struct dentry *root, struct vfsmount *rootmnt,
1280                         char *buffer, int buflen)
1281 {
1282         char * end = buffer+buflen;
1283         char * retval;
1284         int namelen;
1285
1286         *--end = '\0';
1287         buflen--;
1288         if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1289                 buflen -= 10;
1290                 end -= 10;
1291                 if (buflen < 0)
1292                         goto Elong;
1293                 memcpy(end, " (deleted)", 10);
1294         }
1295
1296         if (buflen < 1)
1297                 goto Elong;
1298         /* Get '/' right */
1299         retval = end-1;
1300         *retval = '/';
1301
1302         for (;;) {
1303                 struct dentry * parent;
1304
1305                 if (dentry == root && vfsmnt == rootmnt)
1306                         break;
1307                 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1308                         /* Global root? */
1309                         spin_lock(&vfsmount_lock);
1310                         if (vfsmnt->mnt_parent == vfsmnt) {
1311                                 spin_unlock(&vfsmount_lock);
1312                                 goto global_root;
1313                         }
1314                         dentry = vfsmnt->mnt_mountpoint;
1315                         vfsmnt = vfsmnt->mnt_parent;
1316                         spin_unlock(&vfsmount_lock);
1317                         continue;
1318                 }
1319                 parent = dentry->d_parent;
1320                 prefetch(parent);
1321                 namelen = dentry->d_name.len;
1322                 buflen -= namelen + 1;
1323                 if (buflen < 0)
1324                         goto Elong;
1325                 end -= namelen;
1326                 memcpy(end, dentry->d_name.name, namelen);
1327                 *--end = '/';
1328                 retval = end;
1329                 dentry = parent;
1330         }
1331
1332         return retval;
1333
1334 global_root:
1335         namelen = dentry->d_name.len;
1336         buflen -= namelen;
1337         if (buflen < 0)
1338                 goto Elong;
1339         retval -= namelen-1;    /* hit the slash */
1340         memcpy(retval, dentry->d_name.name, namelen);
1341         return retval;
1342 Elong:
1343         return ERR_PTR(-ENAMETOOLONG);
1344 }
1345
1346 /* write full pathname into buffer and return start of pathname */
1347 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1348                                 char *buf, int buflen)
1349 {
1350         char *res;
1351         struct vfsmount *rootmnt;
1352         struct dentry *root;
1353
1354         read_lock(&current->fs->lock);
1355         rootmnt = mntget(current->fs->rootmnt);
1356         root = dget(current->fs->root);
1357         read_unlock(&current->fs->lock);
1358         spin_lock(&dcache_lock);
1359         res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1360         spin_unlock(&dcache_lock);
1361         dput(root);
1362         mntput(rootmnt);
1363         return res;
1364 }
1365
1366 /*
1367  * NOTE! The user-level library version returns a
1368  * character pointer. The kernel system call just
1369  * returns the length of the buffer filled (which
1370  * includes the ending '\0' character), or a negative
1371  * error value. So libc would do something like
1372  *
1373  *      char *getcwd(char * buf, size_t size)
1374  *      {
1375  *              int retval;
1376  *
1377  *              retval = sys_getcwd(buf, size);
1378  *              if (retval >= 0)
1379  *                      return buf;
1380  *              errno = -retval;
1381  *              return NULL;
1382  *      }
1383  */
1384 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1385 {
1386         int error;
1387         struct vfsmount *pwdmnt, *rootmnt;
1388         struct dentry *pwd, *root;
1389         char *page = (char *) __get_free_page(GFP_USER);
1390
1391         if (!page)
1392                 return -ENOMEM;
1393
1394         read_lock(&current->fs->lock);
1395         pwdmnt = mntget(current->fs->pwdmnt);
1396         pwd = dget(current->fs->pwd);
1397         rootmnt = mntget(current->fs->rootmnt);
1398         root = dget(current->fs->root);
1399         read_unlock(&current->fs->lock);
1400
1401         error = -ENOENT;
1402         /* Has the current directory has been unlinked? */
1403         spin_lock(&dcache_lock);
1404         if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1405                 unsigned long len;
1406                 char * cwd;
1407
1408                 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1409                 spin_unlock(&dcache_lock);
1410
1411                 error = PTR_ERR(cwd);
1412                 if (IS_ERR(cwd))
1413                         goto out;
1414
1415                 error = -ERANGE;
1416                 len = PAGE_SIZE + page - cwd;
1417                 if (len <= size) {
1418                         error = len;
1419                         if (copy_to_user(buf, cwd, len))
1420                                 error = -EFAULT;
1421                 }
1422         } else
1423                 spin_unlock(&dcache_lock);
1424
1425 out:
1426         dput(pwd);
1427         mntput(pwdmnt);
1428         dput(root);
1429         mntput(rootmnt);
1430         free_page((unsigned long) page);
1431         return error;
1432 }
1433
1434 /*
1435  * Test whether new_dentry is a subdirectory of old_dentry.
1436  *
1437  * Trivially implemented using the dcache structure
1438  */
1439
1440 /**
1441  * is_subdir - is new dentry a subdirectory of old_dentry
1442  * @new_dentry: new dentry
1443  * @old_dentry: old dentry
1444  *
1445  * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1446  * Returns 0 otherwise.
1447  * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1448  */
1449   
1450 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1451 {
1452         int result;
1453         struct dentry * saved = new_dentry;
1454         unsigned long seq;
1455
1456         result = 0;
1457         /* need rcu_readlock to protect against the d_parent trashing due to
1458          * d_move
1459          */
1460         rcu_read_lock();
1461         do {
1462                 /* for restarting inner loop in case of seq retry */
1463                 new_dentry = saved;
1464                 seq = read_seqbegin(&rename_lock);
1465                 for (;;) {
1466                         if (new_dentry != old_dentry) {
1467                                 struct dentry * parent = new_dentry->d_parent;
1468                                 if (parent == new_dentry)
1469                                         break;
1470                                 new_dentry = parent;
1471                                 continue;
1472                         }
1473                         result = 1;
1474                         break;
1475                 }
1476         } while (read_seqretry(&rename_lock, seq));
1477         rcu_read_unlock();
1478
1479         return result;
1480 }
1481
1482 void d_genocide(struct dentry *root)
1483 {
1484         struct dentry *this_parent = root;
1485         struct list_head *next;
1486
1487         spin_lock(&dcache_lock);
1488 repeat:
1489         next = this_parent->d_subdirs.next;
1490 resume:
1491         while (next != &this_parent->d_subdirs) {
1492                 struct list_head *tmp = next;
1493                 struct dentry *dentry = list_entry(tmp, struct dentry, d_child);
1494                 next = tmp->next;
1495                 if (d_unhashed(dentry)||!dentry->d_inode)
1496                         continue;
1497                 if (!list_empty(&dentry->d_subdirs)) {
1498                         this_parent = dentry;
1499                         goto repeat;
1500                 }
1501                 atomic_dec(&dentry->d_count);
1502         }
1503         if (this_parent != root) {
1504                 next = this_parent->d_child.next; 
1505                 atomic_dec(&this_parent->d_count);
1506                 this_parent = this_parent->d_parent;
1507                 goto resume;
1508         }
1509         spin_unlock(&dcache_lock);
1510 }
1511
1512 /**
1513  * find_inode_number - check for dentry with name
1514  * @dir: directory to check
1515  * @name: Name to find.
1516  *
1517  * Check whether a dentry already exists for the given name,
1518  * and return the inode number if it has an inode. Otherwise
1519  * 0 is returned.
1520  *
1521  * This routine is used to post-process directory listings for
1522  * filesystems using synthetic inode numbers, and is necessary
1523  * to keep getcwd() working.
1524  */
1525  
1526 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1527 {
1528         struct dentry * dentry;
1529         ino_t ino = 0;
1530
1531         /*
1532          * Check for a fs-specific hash function. Note that we must
1533          * calculate the standard hash first, as the d_op->d_hash()
1534          * routine may choose to leave the hash value unchanged.
1535          */
1536         name->hash = full_name_hash(name->name, name->len);
1537         if (dir->d_op && dir->d_op->d_hash)
1538         {
1539                 if (dir->d_op->d_hash(dir, name) != 0)
1540                         goto out;
1541         }
1542
1543         dentry = d_lookup(dir, name);
1544         if (dentry)
1545         {
1546                 if (dentry->d_inode)
1547                         ino = dentry->d_inode->i_ino;
1548                 dput(dentry);
1549         }
1550 out:
1551         return ino;
1552 }
1553
1554 static __initdata unsigned long dhash_entries;
1555 static int __init set_dhash_entries(char *str)
1556 {
1557         if (!str)
1558                 return 0;
1559         dhash_entries = simple_strtoul(str, &str, 0);
1560         return 1;
1561 }
1562 __setup("dhash_entries=", set_dhash_entries);
1563
1564 static void __init dcache_init(unsigned long mempages)
1565 {
1566         struct hlist_head *d;
1567         unsigned long order;
1568         unsigned int nr_hash;
1569         int i;
1570
1571         /* 
1572          * A constructor could be added for stable state like the lists,
1573          * but it is probably not worth it because of the cache nature
1574          * of the dcache. 
1575          */
1576         dentry_cache = kmem_cache_create("dentry_cache",
1577                                          sizeof(struct dentry),
1578                                          0,
1579                                          SLAB_RECLAIM_ACCOUNT|SLAB_PANIC,
1580                                          NULL, NULL);
1581         
1582         set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1583
1584         if (!dhash_entries)
1585                 dhash_entries = PAGE_SHIFT < 13 ?
1586                                 mempages >> (13 - PAGE_SHIFT) :
1587                                 mempages << (PAGE_SHIFT - 13);
1588
1589         dhash_entries *= sizeof(struct hlist_head);
1590         for (order = 0; ((1UL << order) << PAGE_SHIFT) < dhash_entries; order++)
1591                 ;
1592
1593         do {
1594                 unsigned long tmp;
1595
1596                 nr_hash = (1UL << order) * PAGE_SIZE /
1597                         sizeof(struct hlist_head);
1598                 d_hash_mask = (nr_hash - 1);
1599
1600                 tmp = nr_hash;
1601                 d_hash_shift = 0;
1602                 while ((tmp >>= 1UL) != 0UL)
1603                         d_hash_shift++;
1604
1605                 dentry_hashtable = (struct hlist_head *)
1606                         __get_free_pages(GFP_ATOMIC, order);
1607         } while (dentry_hashtable == NULL && --order >= 0);
1608
1609         printk(KERN_INFO "Dentry cache hash table entries: %d (order: %ld, %ld bytes)\n",
1610                         nr_hash, order, (PAGE_SIZE << order));
1611
1612         if (!dentry_hashtable)
1613                 panic("Failed to allocate dcache hash table\n");
1614
1615         d = dentry_hashtable;
1616         i = nr_hash;
1617         do {
1618                 INIT_HLIST_HEAD(d);
1619                 d++;
1620                 i--;
1621         } while (i);
1622 }
1623
1624 /* SLAB cache for __getname() consumers */
1625 kmem_cache_t *names_cachep;
1626
1627 /* SLAB cache for file structures */
1628 kmem_cache_t *filp_cachep;
1629
1630 EXPORT_SYMBOL(d_genocide);
1631
1632 extern void bdev_cache_init(void);
1633 extern void chrdev_init(void);
1634
1635 void __init vfs_caches_init(unsigned long mempages)
1636 {
1637         unsigned long reserve;
1638
1639         /* Base hash sizes on available memory, with a reserve equal to
1640            150% of current kernel size */
1641
1642         reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1643         mempages -= reserve;
1644
1645         names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1646                         SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1647
1648         filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
1649                         SLAB_HWCACHE_ALIGN|SLAB_PANIC, filp_ctor, filp_dtor);
1650
1651         dcache_init(mempages);
1652         inode_init(mempages);
1653         files_init(mempages);
1654         mnt_init(mempages);
1655         bdev_cache_init();
1656         chrdev_init();
1657 }
1658
1659 EXPORT_SYMBOL(d_alloc);
1660 EXPORT_SYMBOL(d_alloc_anon);
1661 EXPORT_SYMBOL(d_alloc_root);
1662 EXPORT_SYMBOL(d_delete);
1663 EXPORT_SYMBOL(d_find_alias);
1664 EXPORT_SYMBOL(d_instantiate);
1665 EXPORT_SYMBOL(d_invalidate);
1666 EXPORT_SYMBOL(d_lookup);
1667 EXPORT_SYMBOL(d_move);
1668 EXPORT_SYMBOL(d_path);
1669 EXPORT_SYMBOL(d_prune_aliases);
1670 EXPORT_SYMBOL(d_rehash);
1671 EXPORT_SYMBOL(d_splice_alias);
1672 EXPORT_SYMBOL(d_validate);
1673 EXPORT_SYMBOL(dget_locked);
1674 EXPORT_SYMBOL(dput);
1675 EXPORT_SYMBOL(find_inode_number);
1676 EXPORT_SYMBOL(have_submounts);
1677 EXPORT_SYMBOL(is_subdir);
1678 EXPORT_SYMBOL(names_cachep);
1679 EXPORT_SYMBOL(shrink_dcache_anon);
1680 EXPORT_SYMBOL(shrink_dcache_parent);
1681 EXPORT_SYMBOL(shrink_dcache_sb);