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