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