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