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