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