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