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