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