fs: scale inode alias list
[linux-flexiantxendom0-natty.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/syscalls.h>
18 #include <linux/string.h>
19 #include <linux/mm.h>
20 #include <linux/fs.h>
21 #include <linux/fsnotify.h>
22 #include <linux/slab.h>
23 #include <linux/init.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 #include <linux/fs_struct.h>
35 #include <linux/hardirq.h>
36 #include "internal.h"
37
38 /*
39  * Usage:
40  * dcache_inode_lock protects:
41  *   - i_dentry, d_alias, d_inode
42  * dcache_hash_lock protects:
43  *   - the dcache hash table, s_anon lists
44  * dcache_lru_lock protects:
45  *   - the dcache lru lists and counters
46  * d_lock protects:
47  *   - d_flags
48  *   - d_name
49  *   - d_lru
50  *   - d_count
51  *   - d_unhashed()
52  *   - d_parent and d_subdirs
53  *   - childrens' d_child and d_parent
54  *   - d_alias, d_inode
55  *
56  * Ordering:
57  * dcache_lock
58  *   dcache_inode_lock
59  *     dentry->d_lock
60  *       dcache_lru_lock
61  *       dcache_hash_lock
62  *
63  * If there is an ancestor relationship:
64  * dentry->d_parent->...->d_parent->d_lock
65  *   ...
66  *     dentry->d_parent->d_lock
67  *       dentry->d_lock
68  *
69  * If no ancestor relationship:
70  * if (dentry1 < dentry2)
71  *   dentry1->d_lock
72  *     dentry2->d_lock
73  */
74 int sysctl_vfs_cache_pressure __read_mostly = 100;
75 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
76
77 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_inode_lock);
78 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_hash_lock);
79 static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
80 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
81 __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
82
83 EXPORT_SYMBOL(dcache_inode_lock);
84 EXPORT_SYMBOL(dcache_lock);
85
86 static struct kmem_cache *dentry_cache __read_mostly;
87
88 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
89
90 /*
91  * This is the single most critical data structure when it comes
92  * to the dcache: the hashtable for lookups. Somebody should try
93  * to make this good - I've just made it work.
94  *
95  * This hash-function tries to avoid losing too many bits of hash
96  * information, yet avoid using a prime hash-size or similar.
97  */
98 #define D_HASHBITS     d_hash_shift
99 #define D_HASHMASK     d_hash_mask
100
101 static unsigned int d_hash_mask __read_mostly;
102 static unsigned int d_hash_shift __read_mostly;
103 static struct hlist_head *dentry_hashtable __read_mostly;
104
105 /* Statistics gathering. */
106 struct dentry_stat_t dentry_stat = {
107         .age_limit = 45,
108 };
109
110 static DEFINE_PER_CPU(unsigned int, nr_dentry);
111
112 #if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
113 static int get_nr_dentry(void)
114 {
115         int i;
116         int sum = 0;
117         for_each_possible_cpu(i)
118                 sum += per_cpu(nr_dentry, i);
119         return sum < 0 ? 0 : sum;
120 }
121
122 int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
123                    size_t *lenp, loff_t *ppos)
124 {
125         dentry_stat.nr_dentry = get_nr_dentry();
126         return proc_dointvec(table, write, buffer, lenp, ppos);
127 }
128 #endif
129
130 static void __d_free(struct rcu_head *head)
131 {
132         struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
133
134         WARN_ON(!list_empty(&dentry->d_alias));
135         if (dname_external(dentry))
136                 kfree(dentry->d_name.name);
137         kmem_cache_free(dentry_cache, dentry); 
138 }
139
140 /*
141  * no dcache_lock, please.
142  */
143 static void d_free(struct dentry *dentry)
144 {
145         BUG_ON(dentry->d_count);
146         this_cpu_dec(nr_dentry);
147         if (dentry->d_op && dentry->d_op->d_release)
148                 dentry->d_op->d_release(dentry);
149
150         /* if dentry was never inserted into hash, immediate free is OK */
151         if (hlist_unhashed(&dentry->d_hash))
152                 __d_free(&dentry->d_u.d_rcu);
153         else
154                 call_rcu(&dentry->d_u.d_rcu, __d_free);
155 }
156
157 /*
158  * Release the dentry's inode, using the filesystem
159  * d_iput() operation if defined.
160  */
161 static void dentry_iput(struct dentry * dentry)
162         __releases(dentry->d_lock)
163         __releases(dcache_inode_lock)
164         __releases(dcache_lock)
165 {
166         struct inode *inode = dentry->d_inode;
167         if (inode) {
168                 dentry->d_inode = NULL;
169                 list_del_init(&dentry->d_alias);
170                 spin_unlock(&dentry->d_lock);
171                 spin_unlock(&dcache_inode_lock);
172                 spin_unlock(&dcache_lock);
173                 if (!inode->i_nlink)
174                         fsnotify_inoderemove(inode);
175                 if (dentry->d_op && dentry->d_op->d_iput)
176                         dentry->d_op->d_iput(dentry, inode);
177                 else
178                         iput(inode);
179         } else {
180                 spin_unlock(&dentry->d_lock);
181                 spin_unlock(&dcache_inode_lock);
182                 spin_unlock(&dcache_lock);
183         }
184 }
185
186 /*
187  * dentry_lru_(add|del|move_tail) must be called with d_lock held.
188  */
189 static void dentry_lru_add(struct dentry *dentry)
190 {
191         if (list_empty(&dentry->d_lru)) {
192                 spin_lock(&dcache_lru_lock);
193                 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
194                 dentry->d_sb->s_nr_dentry_unused++;
195                 dentry_stat.nr_unused++;
196                 spin_unlock(&dcache_lru_lock);
197         }
198 }
199
200 static void __dentry_lru_del(struct dentry *dentry)
201 {
202         list_del_init(&dentry->d_lru);
203         dentry->d_sb->s_nr_dentry_unused--;
204         dentry_stat.nr_unused--;
205 }
206
207 static void dentry_lru_del(struct dentry *dentry)
208 {
209         if (!list_empty(&dentry->d_lru)) {
210                 spin_lock(&dcache_lru_lock);
211                 __dentry_lru_del(dentry);
212                 spin_unlock(&dcache_lru_lock);
213         }
214 }
215
216 static void dentry_lru_move_tail(struct dentry *dentry)
217 {
218         spin_lock(&dcache_lru_lock);
219         if (list_empty(&dentry->d_lru)) {
220                 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
221                 dentry->d_sb->s_nr_dentry_unused++;
222                 dentry_stat.nr_unused++;
223         } else {
224                 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
225         }
226         spin_unlock(&dcache_lru_lock);
227 }
228
229 /**
230  * d_kill - kill dentry and return parent
231  * @dentry: dentry to kill
232  *
233  * The dentry must already be unhashed and removed from the LRU.
234  *
235  * If this is the root of the dentry tree, return NULL.
236  *
237  * dcache_lock and d_lock and d_parent->d_lock must be held by caller, and
238  * are dropped by d_kill.
239  */
240 static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
241         __releases(dentry->d_lock)
242         __releases(parent->d_lock)
243         __releases(dcache_inode_lock)
244         __releases(dcache_lock)
245 {
246         list_del(&dentry->d_u.d_child);
247         if (parent)
248                 spin_unlock(&parent->d_lock);
249         dentry_iput(dentry);
250         /*
251          * dentry_iput drops the locks, at which point nobody (except
252          * transient RCU lookups) can reach this dentry.
253          */
254         d_free(dentry);
255         return parent;
256 }
257
258 /**
259  * d_drop - drop a dentry
260  * @dentry: dentry to drop
261  *
262  * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
263  * be found through a VFS lookup any more. Note that this is different from
264  * deleting the dentry - d_delete will try to mark the dentry negative if
265  * possible, giving a successful _negative_ lookup, while d_drop will
266  * just make the cache lookup fail.
267  *
268  * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
269  * reason (NFS timeouts or autofs deletes).
270  *
271  * __d_drop requires dentry->d_lock.
272  */
273 void __d_drop(struct dentry *dentry)
274 {
275         if (!(dentry->d_flags & DCACHE_UNHASHED)) {
276                 dentry->d_flags |= DCACHE_UNHASHED;
277                 spin_lock(&dcache_hash_lock);
278                 hlist_del_rcu(&dentry->d_hash);
279                 spin_unlock(&dcache_hash_lock);
280         }
281 }
282 EXPORT_SYMBOL(__d_drop);
283
284 void d_drop(struct dentry *dentry)
285 {
286         spin_lock(&dcache_lock);
287         spin_lock(&dentry->d_lock);
288         __d_drop(dentry);
289         spin_unlock(&dentry->d_lock);
290         spin_unlock(&dcache_lock);
291 }
292 EXPORT_SYMBOL(d_drop);
293
294 /* 
295  * This is dput
296  *
297  * This is complicated by the fact that we do not want to put
298  * dentries that are no longer on any hash chain on the unused
299  * list: we'd much rather just get rid of them immediately.
300  *
301  * However, that implies that we have to traverse the dentry
302  * tree upwards to the parents which might _also_ now be
303  * scheduled for deletion (it may have been only waiting for
304  * its last child to go away).
305  *
306  * This tail recursion is done by hand as we don't want to depend
307  * on the compiler to always get this right (gcc generally doesn't).
308  * Real recursion would eat up our stack space.
309  */
310
311 /*
312  * dput - release a dentry
313  * @dentry: dentry to release 
314  *
315  * Release a dentry. This will drop the usage count and if appropriate
316  * call the dentry unlink method as well as removing it from the queues and
317  * releasing its resources. If the parent dentries were scheduled for release
318  * they too may now get deleted.
319  *
320  * no dcache lock, please.
321  */
322
323 void dput(struct dentry *dentry)
324 {
325         struct dentry *parent;
326         if (!dentry)
327                 return;
328
329 repeat:
330         if (dentry->d_count == 1)
331                 might_sleep();
332         spin_lock(&dentry->d_lock);
333         if (IS_ROOT(dentry))
334                 parent = NULL;
335         else
336                 parent = dentry->d_parent;
337         if (dentry->d_count == 1) {
338                 if (!spin_trylock(&dcache_lock)) {
339                         /*
340                          * Something of a livelock possibility we could avoid
341                          * by taking dcache_lock and trying again, but we
342                          * want to reduce dcache_lock anyway so this will
343                          * get improved.
344                          */
345 drop1:
346                         spin_unlock(&dentry->d_lock);
347                         goto repeat;
348                 }
349                 if (!spin_trylock(&dcache_inode_lock)) {
350 drop2:
351                         spin_unlock(&dcache_lock);
352                         goto drop1;
353                 }
354                 if (parent && !spin_trylock(&parent->d_lock)) {
355                         spin_unlock(&dcache_inode_lock);
356                         goto drop2;
357                 }
358         }
359         dentry->d_count--;
360         if (dentry->d_count) {
361                 spin_unlock(&dentry->d_lock);
362                 if (parent)
363                         spin_unlock(&parent->d_lock);
364                 spin_unlock(&dcache_lock);
365                 return;
366         }
367
368         /*
369          * AV: ->d_delete() is _NOT_ allowed to block now.
370          */
371         if (dentry->d_op && dentry->d_op->d_delete) {
372                 if (dentry->d_op->d_delete(dentry))
373                         goto unhash_it;
374         }
375
376         /* Unreachable? Get rid of it */
377         if (d_unhashed(dentry))
378                 goto kill_it;
379
380         /* Otherwise leave it cached and ensure it's on the LRU */
381         dentry->d_flags |= DCACHE_REFERENCED;
382         dentry_lru_add(dentry);
383
384         spin_unlock(&dentry->d_lock);
385         if (parent)
386                 spin_unlock(&parent->d_lock);
387         spin_unlock(&dcache_inode_lock);
388         spin_unlock(&dcache_lock);
389         return;
390
391 unhash_it:
392         __d_drop(dentry);
393 kill_it:
394         /* if dentry was on the d_lru list delete it from there */
395         dentry_lru_del(dentry);
396         dentry = d_kill(dentry, parent);
397         if (dentry)
398                 goto repeat;
399 }
400 EXPORT_SYMBOL(dput);
401
402 /**
403  * d_invalidate - invalidate a dentry
404  * @dentry: dentry to invalidate
405  *
406  * Try to invalidate the dentry if it turns out to be
407  * possible. If there are other dentries that can be
408  * reached through this one we can't delete it and we
409  * return -EBUSY. On success we return 0.
410  *
411  * no dcache lock.
412  */
413  
414 int d_invalidate(struct dentry * dentry)
415 {
416         /*
417          * If it's already been dropped, return OK.
418          */
419         spin_lock(&dcache_lock);
420         spin_lock(&dentry->d_lock);
421         if (d_unhashed(dentry)) {
422                 spin_unlock(&dentry->d_lock);
423                 spin_unlock(&dcache_lock);
424                 return 0;
425         }
426         /*
427          * Check whether to do a partial shrink_dcache
428          * to get rid of unused child entries.
429          */
430         if (!list_empty(&dentry->d_subdirs)) {
431                 spin_unlock(&dentry->d_lock);
432                 spin_unlock(&dcache_lock);
433                 shrink_dcache_parent(dentry);
434                 spin_lock(&dcache_lock);
435                 spin_lock(&dentry->d_lock);
436         }
437
438         /*
439          * Somebody else still using it?
440          *
441          * If it's a directory, we can't drop it
442          * for fear of somebody re-populating it
443          * with children (even though dropping it
444          * would make it unreachable from the root,
445          * we might still populate it if it was a
446          * working directory or similar).
447          */
448         if (dentry->d_count > 1) {
449                 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
450                         spin_unlock(&dentry->d_lock);
451                         spin_unlock(&dcache_lock);
452                         return -EBUSY;
453                 }
454         }
455
456         __d_drop(dentry);
457         spin_unlock(&dentry->d_lock);
458         spin_unlock(&dcache_lock);
459         return 0;
460 }
461 EXPORT_SYMBOL(d_invalidate);
462
463 /* This must be called with dcache_lock and d_lock held */
464 static inline struct dentry * __dget_locked_dlock(struct dentry *dentry)
465 {
466         dentry->d_count++;
467         dentry_lru_del(dentry);
468         return dentry;
469 }
470
471 /* This should be called _only_ with dcache_lock held */
472 static inline struct dentry * __dget_locked(struct dentry *dentry)
473 {
474         spin_lock(&dentry->d_lock);
475         __dget_locked_dlock(dentry);
476         spin_unlock(&dentry->d_lock);
477         return dentry;
478 }
479
480 struct dentry * dget_locked_dlock(struct dentry *dentry)
481 {
482         return __dget_locked_dlock(dentry);
483 }
484
485 struct dentry * dget_locked(struct dentry *dentry)
486 {
487         return __dget_locked(dentry);
488 }
489 EXPORT_SYMBOL(dget_locked);
490
491 struct dentry *dget_parent(struct dentry *dentry)
492 {
493         struct dentry *ret;
494
495 repeat:
496         spin_lock(&dentry->d_lock);
497         ret = dentry->d_parent;
498         if (!ret)
499                 goto out;
500         if (dentry == ret) {
501                 ret->d_count++;
502                 goto out;
503         }
504         if (!spin_trylock(&ret->d_lock)) {
505                 spin_unlock(&dentry->d_lock);
506                 cpu_relax();
507                 goto repeat;
508         }
509         BUG_ON(!ret->d_count);
510         ret->d_count++;
511         spin_unlock(&ret->d_lock);
512 out:
513         spin_unlock(&dentry->d_lock);
514         return ret;
515 }
516 EXPORT_SYMBOL(dget_parent);
517
518 /**
519  * d_find_alias - grab a hashed alias of inode
520  * @inode: inode in question
521  * @want_discon:  flag, used by d_splice_alias, to request
522  *          that only a DISCONNECTED alias be returned.
523  *
524  * If inode has a hashed alias, or is a directory and has any alias,
525  * acquire the reference to alias and return it. Otherwise return NULL.
526  * Notice that if inode is a directory there can be only one alias and
527  * it can be unhashed only if it has no children, or if it is the root
528  * of a filesystem.
529  *
530  * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
531  * any other hashed alias over that one unless @want_discon is set,
532  * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
533  */
534 static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
535 {
536         struct dentry *alias, *discon_alias;
537
538 again:
539         discon_alias = NULL;
540         list_for_each_entry(alias, &inode->i_dentry, d_alias) {
541                 spin_lock(&alias->d_lock);
542                 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
543                         if (IS_ROOT(alias) &&
544                             (alias->d_flags & DCACHE_DISCONNECTED)) {
545                                 discon_alias = alias;
546                         } else if (!want_discon) {
547                                 __dget_locked_dlock(alias);
548                                 spin_unlock(&alias->d_lock);
549                                 return alias;
550                         }
551                 }
552                 spin_unlock(&alias->d_lock);
553         }
554         if (discon_alias) {
555                 alias = discon_alias;
556                 spin_lock(&alias->d_lock);
557                 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
558                         if (IS_ROOT(alias) &&
559                             (alias->d_flags & DCACHE_DISCONNECTED)) {
560                                 __dget_locked_dlock(alias);
561                                 spin_unlock(&alias->d_lock);
562                                 return alias;
563                         }
564                 }
565                 spin_unlock(&alias->d_lock);
566                 goto again;
567         }
568         return NULL;
569 }
570
571 struct dentry *d_find_alias(struct inode *inode)
572 {
573         struct dentry *de = NULL;
574
575         if (!list_empty(&inode->i_dentry)) {
576                 spin_lock(&dcache_lock);
577                 spin_lock(&dcache_inode_lock);
578                 de = __d_find_alias(inode, 0);
579                 spin_unlock(&dcache_inode_lock);
580                 spin_unlock(&dcache_lock);
581         }
582         return de;
583 }
584 EXPORT_SYMBOL(d_find_alias);
585
586 /*
587  *      Try to kill dentries associated with this inode.
588  * WARNING: you must own a reference to inode.
589  */
590 void d_prune_aliases(struct inode *inode)
591 {
592         struct dentry *dentry;
593 restart:
594         spin_lock(&dcache_lock);
595         spin_lock(&dcache_inode_lock);
596         list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
597                 spin_lock(&dentry->d_lock);
598                 if (!dentry->d_count) {
599                         __dget_locked_dlock(dentry);
600                         __d_drop(dentry);
601                         spin_unlock(&dentry->d_lock);
602                         spin_unlock(&dcache_inode_lock);
603                         spin_unlock(&dcache_lock);
604                         dput(dentry);
605                         goto restart;
606                 }
607                 spin_unlock(&dentry->d_lock);
608         }
609         spin_unlock(&dcache_inode_lock);
610         spin_unlock(&dcache_lock);
611 }
612 EXPORT_SYMBOL(d_prune_aliases);
613
614 /*
615  * Throw away a dentry - free the inode, dput the parent.  This requires that
616  * the LRU list has already been removed.
617  *
618  * Try to prune ancestors as well.  This is necessary to prevent
619  * quadratic behavior of shrink_dcache_parent(), but is also expected
620  * to be beneficial in reducing dentry cache fragmentation.
621  */
622 static void prune_one_dentry(struct dentry *dentry, struct dentry *parent)
623         __releases(dentry->d_lock)
624         __releases(parent->d_lock)
625         __releases(dcache_inode_lock)
626         __releases(dcache_lock)
627 {
628         __d_drop(dentry);
629         dentry = d_kill(dentry, parent);
630
631         /*
632          * Prune ancestors.  Locking is simpler than in dput(),
633          * because dcache_lock needs to be taken anyway.
634          */
635         while (dentry) {
636                 spin_lock(&dcache_lock);
637                 spin_lock(&dcache_inode_lock);
638 again:
639                 spin_lock(&dentry->d_lock);
640                 if (IS_ROOT(dentry))
641                         parent = NULL;
642                 else
643                         parent = dentry->d_parent;
644                 if (parent && !spin_trylock(&parent->d_lock)) {
645                         spin_unlock(&dentry->d_lock);
646                         goto again;
647                 }
648                 dentry->d_count--;
649                 if (dentry->d_count) {
650                         if (parent)
651                                 spin_unlock(&parent->d_lock);
652                         spin_unlock(&dentry->d_lock);
653                         spin_unlock(&dcache_inode_lock);
654                         spin_unlock(&dcache_lock);
655                         return;
656                 }
657
658                 dentry_lru_del(dentry);
659                 __d_drop(dentry);
660                 dentry = d_kill(dentry, parent);
661         }
662 }
663
664 static void shrink_dentry_list(struct list_head *list)
665 {
666         struct dentry *dentry;
667
668         while (!list_empty(list)) {
669                 struct dentry *parent;
670
671                 dentry = list_entry(list->prev, struct dentry, d_lru);
672
673                 if (!spin_trylock(&dentry->d_lock)) {
674 relock:
675                         spin_unlock(&dcache_lru_lock);
676                         cpu_relax();
677                         spin_lock(&dcache_lru_lock);
678                         continue;
679                 }
680
681                 /*
682                  * We found an inuse dentry which was not removed from
683                  * the LRU because of laziness during lookup.  Do not free
684                  * it - just keep it off the LRU list.
685                  */
686                 if (dentry->d_count) {
687                         __dentry_lru_del(dentry);
688                         spin_unlock(&dentry->d_lock);
689                         continue;
690                 }
691                 if (IS_ROOT(dentry))
692                         parent = NULL;
693                 else
694                         parent = dentry->d_parent;
695                 if (parent && !spin_trylock(&parent->d_lock)) {
696                         spin_unlock(&dentry->d_lock);
697                         goto relock;
698                 }
699                 __dentry_lru_del(dentry);
700                 spin_unlock(&dcache_lru_lock);
701
702                 prune_one_dentry(dentry, parent);
703                 /* dcache_lock, dcache_inode_lock and dentry->d_lock dropped */
704                 spin_lock(&dcache_lock);
705                 spin_lock(&dcache_inode_lock);
706                 spin_lock(&dcache_lru_lock);
707         }
708 }
709
710 /**
711  * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
712  * @sb:         superblock to shrink dentry LRU.
713  * @count:      number of entries to prune
714  * @flags:      flags to control the dentry processing
715  *
716  * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
717  */
718 static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
719 {
720         /* called from prune_dcache() and shrink_dcache_parent() */
721         struct dentry *dentry;
722         LIST_HEAD(referenced);
723         LIST_HEAD(tmp);
724         int cnt = *count;
725
726         spin_lock(&dcache_lock);
727         spin_lock(&dcache_inode_lock);
728 relock:
729         spin_lock(&dcache_lru_lock);
730         while (!list_empty(&sb->s_dentry_lru)) {
731                 dentry = list_entry(sb->s_dentry_lru.prev,
732                                 struct dentry, d_lru);
733                 BUG_ON(dentry->d_sb != sb);
734
735                 if (!spin_trylock(&dentry->d_lock)) {
736                         spin_unlock(&dcache_lru_lock);
737                         cpu_relax();
738                         goto relock;
739                 }
740
741                 /*
742                  * If we are honouring the DCACHE_REFERENCED flag and the
743                  * dentry has this flag set, don't free it.  Clear the flag
744                  * and put it back on the LRU.
745                  */
746                 if (flags & DCACHE_REFERENCED &&
747                                 dentry->d_flags & DCACHE_REFERENCED) {
748                         dentry->d_flags &= ~DCACHE_REFERENCED;
749                         list_move(&dentry->d_lru, &referenced);
750                         spin_unlock(&dentry->d_lock);
751                 } else {
752                         list_move_tail(&dentry->d_lru, &tmp);
753                         spin_unlock(&dentry->d_lock);
754                         if (!--cnt)
755                                 break;
756                 }
757                 /* XXX: re-add cond_resched_lock when dcache_lock goes away */
758         }
759
760         *count = cnt;
761         shrink_dentry_list(&tmp);
762
763         if (!list_empty(&referenced))
764                 list_splice(&referenced, &sb->s_dentry_lru);
765         spin_unlock(&dcache_lru_lock);
766         spin_unlock(&dcache_inode_lock);
767         spin_unlock(&dcache_lock);
768 }
769
770 /**
771  * prune_dcache - shrink the dcache
772  * @count: number of entries to try to free
773  *
774  * Shrink the dcache. This is done when we need more memory, or simply when we
775  * need to unmount something (at which point we need to unuse all dentries).
776  *
777  * This function may fail to free any resources if all the dentries are in use.
778  */
779 static void prune_dcache(int count)
780 {
781         struct super_block *sb, *p = NULL;
782         int w_count;
783         int unused = dentry_stat.nr_unused;
784         int prune_ratio;
785         int pruned;
786
787         if (unused == 0 || count == 0)
788                 return;
789         spin_lock(&dcache_lock);
790         if (count >= unused)
791                 prune_ratio = 1;
792         else
793                 prune_ratio = unused / count;
794         spin_lock(&sb_lock);
795         list_for_each_entry(sb, &super_blocks, s_list) {
796                 if (list_empty(&sb->s_instances))
797                         continue;
798                 if (sb->s_nr_dentry_unused == 0)
799                         continue;
800                 sb->s_count++;
801                 /* Now, we reclaim unused dentrins with fairness.
802                  * We reclaim them same percentage from each superblock.
803                  * We calculate number of dentries to scan on this sb
804                  * as follows, but the implementation is arranged to avoid
805                  * overflows:
806                  * number of dentries to scan on this sb =
807                  * count * (number of dentries on this sb /
808                  * number of dentries in the machine)
809                  */
810                 spin_unlock(&sb_lock);
811                 if (prune_ratio != 1)
812                         w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
813                 else
814                         w_count = sb->s_nr_dentry_unused;
815                 pruned = w_count;
816                 /*
817                  * We need to be sure this filesystem isn't being unmounted,
818                  * otherwise we could race with generic_shutdown_super(), and
819                  * end up holding a reference to an inode while the filesystem
820                  * is unmounted.  So we try to get s_umount, and make sure
821                  * s_root isn't NULL.
822                  */
823                 if (down_read_trylock(&sb->s_umount)) {
824                         if ((sb->s_root != NULL) &&
825                             (!list_empty(&sb->s_dentry_lru))) {
826                                 spin_unlock(&dcache_lock);
827                                 __shrink_dcache_sb(sb, &w_count,
828                                                 DCACHE_REFERENCED);
829                                 pruned -= w_count;
830                                 spin_lock(&dcache_lock);
831                         }
832                         up_read(&sb->s_umount);
833                 }
834                 spin_lock(&sb_lock);
835                 if (p)
836                         __put_super(p);
837                 count -= pruned;
838                 p = sb;
839                 /* more work left to do? */
840                 if (count <= 0)
841                         break;
842         }
843         if (p)
844                 __put_super(p);
845         spin_unlock(&sb_lock);
846         spin_unlock(&dcache_lock);
847 }
848
849 /**
850  * shrink_dcache_sb - shrink dcache for a superblock
851  * @sb: superblock
852  *
853  * Shrink the dcache for the specified super block. This is used to free
854  * the dcache before unmounting a file system.
855  */
856 void shrink_dcache_sb(struct super_block *sb)
857 {
858         LIST_HEAD(tmp);
859
860         spin_lock(&dcache_lock);
861         spin_lock(&dcache_inode_lock);
862         spin_lock(&dcache_lru_lock);
863         while (!list_empty(&sb->s_dentry_lru)) {
864                 list_splice_init(&sb->s_dentry_lru, &tmp);
865                 shrink_dentry_list(&tmp);
866         }
867         spin_unlock(&dcache_lru_lock);
868         spin_unlock(&dcache_inode_lock);
869         spin_unlock(&dcache_lock);
870 }
871 EXPORT_SYMBOL(shrink_dcache_sb);
872
873 /*
874  * destroy a single subtree of dentries for unmount
875  * - see the comments on shrink_dcache_for_umount() for a description of the
876  *   locking
877  */
878 static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
879 {
880         struct dentry *parent;
881         unsigned detached = 0;
882
883         BUG_ON(!IS_ROOT(dentry));
884
885         /* detach this root from the system */
886         spin_lock(&dcache_lock);
887         spin_lock(&dentry->d_lock);
888         dentry_lru_del(dentry);
889         __d_drop(dentry);
890         spin_unlock(&dentry->d_lock);
891         spin_unlock(&dcache_lock);
892
893         for (;;) {
894                 /* descend to the first leaf in the current subtree */
895                 while (!list_empty(&dentry->d_subdirs)) {
896                         struct dentry *loop;
897
898                         /* this is a branch with children - detach all of them
899                          * from the system in one go */
900                         spin_lock(&dcache_lock);
901                         spin_lock(&dentry->d_lock);
902                         list_for_each_entry(loop, &dentry->d_subdirs,
903                                             d_u.d_child) {
904                                 spin_lock_nested(&loop->d_lock,
905                                                 DENTRY_D_LOCK_NESTED);
906                                 dentry_lru_del(loop);
907                                 __d_drop(loop);
908                                 spin_unlock(&loop->d_lock);
909                         }
910                         spin_unlock(&dentry->d_lock);
911                         spin_unlock(&dcache_lock);
912
913                         /* move to the first child */
914                         dentry = list_entry(dentry->d_subdirs.next,
915                                             struct dentry, d_u.d_child);
916                 }
917
918                 /* consume the dentries from this leaf up through its parents
919                  * until we find one with children or run out altogether */
920                 do {
921                         struct inode *inode;
922
923                         if (dentry->d_count != 0) {
924                                 printk(KERN_ERR
925                                        "BUG: Dentry %p{i=%lx,n=%s}"
926                                        " still in use (%d)"
927                                        " [unmount of %s %s]\n",
928                                        dentry,
929                                        dentry->d_inode ?
930                                        dentry->d_inode->i_ino : 0UL,
931                                        dentry->d_name.name,
932                                        dentry->d_count,
933                                        dentry->d_sb->s_type->name,
934                                        dentry->d_sb->s_id);
935                                 BUG();
936                         }
937
938                         if (IS_ROOT(dentry)) {
939                                 parent = NULL;
940                                 list_del(&dentry->d_u.d_child);
941                         } else {
942                                 parent = dentry->d_parent;
943                                 spin_lock(&parent->d_lock);
944                                 parent->d_count--;
945                                 list_del(&dentry->d_u.d_child);
946                                 spin_unlock(&parent->d_lock);
947                         }
948
949                         detached++;
950
951                         inode = dentry->d_inode;
952                         if (inode) {
953                                 dentry->d_inode = NULL;
954                                 list_del_init(&dentry->d_alias);
955                                 if (dentry->d_op && dentry->d_op->d_iput)
956                                         dentry->d_op->d_iput(dentry, inode);
957                                 else
958                                         iput(inode);
959                         }
960
961                         d_free(dentry);
962
963                         /* finished when we fall off the top of the tree,
964                          * otherwise we ascend to the parent and move to the
965                          * next sibling if there is one */
966                         if (!parent)
967                                 return;
968                         dentry = parent;
969                 } while (list_empty(&dentry->d_subdirs));
970
971                 dentry = list_entry(dentry->d_subdirs.next,
972                                     struct dentry, d_u.d_child);
973         }
974 }
975
976 /*
977  * destroy the dentries attached to a superblock on unmounting
978  * - we don't need to use dentry->d_lock, and only need dcache_lock when
979  *   removing the dentry from the system lists and hashes because:
980  *   - the superblock is detached from all mountings and open files, so the
981  *     dentry trees will not be rearranged by the VFS
982  *   - s_umount is write-locked, so the memory pressure shrinker will ignore
983  *     any dentries belonging to this superblock that it comes across
984  *   - the filesystem itself is no longer permitted to rearrange the dentries
985  *     in this superblock
986  */
987 void shrink_dcache_for_umount(struct super_block *sb)
988 {
989         struct dentry *dentry;
990
991         if (down_read_trylock(&sb->s_umount))
992                 BUG();
993
994         dentry = sb->s_root;
995         sb->s_root = NULL;
996         spin_lock(&dentry->d_lock);
997         dentry->d_count--;
998         spin_unlock(&dentry->d_lock);
999         shrink_dcache_for_umount_subtree(dentry);
1000
1001         while (!hlist_empty(&sb->s_anon)) {
1002                 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
1003                 shrink_dcache_for_umount_subtree(dentry);
1004         }
1005 }
1006
1007 /*
1008  * Search for at least 1 mount point in the dentry's subdirs.
1009  * We descend to the next level whenever the d_subdirs
1010  * list is non-empty and continue searching.
1011  */
1012  
1013 /**
1014  * have_submounts - check for mounts over a dentry
1015  * @parent: dentry to check.
1016  *
1017  * Return true if the parent or its subdirectories contain
1018  * a mount point
1019  */
1020  
1021 int have_submounts(struct dentry *parent)
1022 {
1023         struct dentry *this_parent = parent;
1024         struct list_head *next;
1025
1026         spin_lock(&dcache_lock);
1027         if (d_mountpoint(parent))
1028                 goto positive;
1029         spin_lock(&this_parent->d_lock);
1030 repeat:
1031         next = this_parent->d_subdirs.next;
1032 resume:
1033         while (next != &this_parent->d_subdirs) {
1034                 struct list_head *tmp = next;
1035                 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1036                 next = tmp->next;
1037
1038                 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1039                 /* Have we found a mount point ? */
1040                 if (d_mountpoint(dentry)) {
1041                         spin_unlock(&dentry->d_lock);
1042                         spin_unlock(&this_parent->d_lock);
1043                         goto positive;
1044                 }
1045                 if (!list_empty(&dentry->d_subdirs)) {
1046                         spin_unlock(&this_parent->d_lock);
1047                         spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
1048                         this_parent = dentry;
1049                         spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1050                         goto repeat;
1051                 }
1052                 spin_unlock(&dentry->d_lock);
1053         }
1054         /*
1055          * All done at this level ... ascend and resume the search.
1056          */
1057         if (this_parent != parent) {
1058                 next = this_parent->d_u.d_child.next;
1059                 spin_unlock(&this_parent->d_lock);
1060                 this_parent = this_parent->d_parent;
1061                 spin_lock(&this_parent->d_lock);
1062                 goto resume;
1063         }
1064         spin_unlock(&this_parent->d_lock);
1065         spin_unlock(&dcache_lock);
1066         return 0; /* No mount points found in tree */
1067 positive:
1068         spin_unlock(&dcache_lock);
1069         return 1;
1070 }
1071 EXPORT_SYMBOL(have_submounts);
1072
1073 /*
1074  * Search the dentry child list for the specified parent,
1075  * and move any unused dentries to the end of the unused
1076  * list for prune_dcache(). We descend to the next level
1077  * whenever the d_subdirs list is non-empty and continue
1078  * searching.
1079  *
1080  * It returns zero iff there are no unused children,
1081  * otherwise  it returns the number of children moved to
1082  * the end of the unused list. This may not be the total
1083  * number of unused children, because select_parent can
1084  * drop the lock and return early due to latency
1085  * constraints.
1086  */
1087 static int select_parent(struct dentry * parent)
1088 {
1089         struct dentry *this_parent = parent;
1090         struct list_head *next;
1091         int found = 0;
1092
1093         spin_lock(&dcache_lock);
1094         spin_lock(&this_parent->d_lock);
1095 repeat:
1096         next = this_parent->d_subdirs.next;
1097 resume:
1098         while (next != &this_parent->d_subdirs) {
1099                 struct list_head *tmp = next;
1100                 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1101                 next = tmp->next;
1102                 BUG_ON(this_parent == dentry);
1103
1104                 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1105
1106                 /* 
1107                  * move only zero ref count dentries to the end 
1108                  * of the unused list for prune_dcache
1109                  */
1110                 if (!dentry->d_count) {
1111                         dentry_lru_move_tail(dentry);
1112                         found++;
1113                 } else {
1114                         dentry_lru_del(dentry);
1115                 }
1116
1117                 /*
1118                  * We can return to the caller if we have found some (this
1119                  * ensures forward progress). We'll be coming back to find
1120                  * the rest.
1121                  */
1122                 if (found && need_resched()) {
1123                         spin_unlock(&dentry->d_lock);
1124                         goto out;
1125                 }
1126
1127                 /*
1128                  * Descend a level if the d_subdirs list is non-empty.
1129                  */
1130                 if (!list_empty(&dentry->d_subdirs)) {
1131                         spin_unlock(&this_parent->d_lock);
1132                         spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
1133                         this_parent = dentry;
1134                         spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
1135                         goto repeat;
1136                 }
1137
1138                 spin_unlock(&dentry->d_lock);
1139         }
1140         /*
1141          * All done at this level ... ascend and resume the search.
1142          */
1143         if (this_parent != parent) {
1144                 struct dentry *tmp;
1145                 next = this_parent->d_u.d_child.next;
1146                 tmp = this_parent->d_parent;
1147                 spin_unlock(&this_parent->d_lock);
1148                 BUG_ON(tmp == this_parent);
1149                 this_parent = tmp;
1150                 spin_lock(&this_parent->d_lock);
1151                 goto resume;
1152         }
1153 out:
1154         spin_unlock(&this_parent->d_lock);
1155         spin_unlock(&dcache_lock);
1156         return found;
1157 }
1158
1159 /**
1160  * shrink_dcache_parent - prune dcache
1161  * @parent: parent of entries to prune
1162  *
1163  * Prune the dcache to remove unused children of the parent dentry.
1164  */
1165  
1166 void shrink_dcache_parent(struct dentry * parent)
1167 {
1168         struct super_block *sb = parent->d_sb;
1169         int found;
1170
1171         while ((found = select_parent(parent)) != 0)
1172                 __shrink_dcache_sb(sb, &found, 0);
1173 }
1174 EXPORT_SYMBOL(shrink_dcache_parent);
1175
1176 /*
1177  * Scan `nr' dentries and return the number which remain.
1178  *
1179  * We need to avoid reentering the filesystem if the caller is performing a
1180  * GFP_NOFS allocation attempt.  One example deadlock is:
1181  *
1182  * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1183  * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1184  * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1185  *
1186  * In this case we return -1 to tell the caller that we baled.
1187  */
1188 static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
1189 {
1190         if (nr) {
1191                 if (!(gfp_mask & __GFP_FS))
1192                         return -1;
1193                 prune_dcache(nr);
1194         }
1195
1196         return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
1197 }
1198
1199 static struct shrinker dcache_shrinker = {
1200         .shrink = shrink_dcache_memory,
1201         .seeks = DEFAULT_SEEKS,
1202 };
1203
1204 /**
1205  * d_alloc      -       allocate a dcache entry
1206  * @parent: parent of entry to allocate
1207  * @name: qstr of the name
1208  *
1209  * Allocates a dentry. It returns %NULL if there is insufficient memory
1210  * available. On a success the dentry is returned. The name passed in is
1211  * copied and the copy passed in may be reused after this call.
1212  */
1213  
1214 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1215 {
1216         struct dentry *dentry;
1217         char *dname;
1218
1219         dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
1220         if (!dentry)
1221                 return NULL;
1222
1223         if (name->len > DNAME_INLINE_LEN-1) {
1224                 dname = kmalloc(name->len + 1, GFP_KERNEL);
1225                 if (!dname) {
1226                         kmem_cache_free(dentry_cache, dentry); 
1227                         return NULL;
1228                 }
1229         } else  {
1230                 dname = dentry->d_iname;
1231         }       
1232         dentry->d_name.name = dname;
1233
1234         dentry->d_name.len = name->len;
1235         dentry->d_name.hash = name->hash;
1236         memcpy(dname, name->name, name->len);
1237         dname[name->len] = 0;
1238
1239         dentry->d_count = 1;
1240         dentry->d_flags = DCACHE_UNHASHED;
1241         spin_lock_init(&dentry->d_lock);
1242         dentry->d_inode = NULL;
1243         dentry->d_parent = NULL;
1244         dentry->d_sb = NULL;
1245         dentry->d_op = NULL;
1246         dentry->d_fsdata = NULL;
1247         dentry->d_mounted = 0;
1248         INIT_HLIST_NODE(&dentry->d_hash);
1249         INIT_LIST_HEAD(&dentry->d_lru);
1250         INIT_LIST_HEAD(&dentry->d_subdirs);
1251         INIT_LIST_HEAD(&dentry->d_alias);
1252         INIT_LIST_HEAD(&dentry->d_u.d_child);
1253
1254         if (parent) {
1255                 spin_lock(&dcache_lock);
1256                 spin_lock(&parent->d_lock);
1257                 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1258                 dentry->d_parent = dget_dlock(parent);
1259                 dentry->d_sb = parent->d_sb;
1260                 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
1261                 spin_unlock(&dentry->d_lock);
1262                 spin_unlock(&parent->d_lock);
1263                 spin_unlock(&dcache_lock);
1264         }
1265
1266         this_cpu_inc(nr_dentry);
1267
1268         return dentry;
1269 }
1270 EXPORT_SYMBOL(d_alloc);
1271
1272 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1273 {
1274         struct qstr q;
1275
1276         q.name = name;
1277         q.len = strlen(name);
1278         q.hash = full_name_hash(q.name, q.len);
1279         return d_alloc(parent, &q);
1280 }
1281 EXPORT_SYMBOL(d_alloc_name);
1282
1283 /* the caller must hold dcache_lock */
1284 static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1285 {
1286         spin_lock(&dentry->d_lock);
1287         if (inode)
1288                 list_add(&dentry->d_alias, &inode->i_dentry);
1289         dentry->d_inode = inode;
1290         spin_unlock(&dentry->d_lock);
1291         fsnotify_d_instantiate(dentry, inode);
1292 }
1293
1294 /**
1295  * d_instantiate - fill in inode information for a dentry
1296  * @entry: dentry to complete
1297  * @inode: inode to attach to this dentry
1298  *
1299  * Fill in inode information in the entry.
1300  *
1301  * This turns negative dentries into productive full members
1302  * of society.
1303  *
1304  * NOTE! This assumes that the inode count has been incremented
1305  * (or otherwise set) by the caller to indicate that it is now
1306  * in use by the dcache.
1307  */
1308  
1309 void d_instantiate(struct dentry *entry, struct inode * inode)
1310 {
1311         BUG_ON(!list_empty(&entry->d_alias));
1312         spin_lock(&dcache_lock);
1313         spin_lock(&dcache_inode_lock);
1314         __d_instantiate(entry, inode);
1315         spin_unlock(&dcache_inode_lock);
1316         spin_unlock(&dcache_lock);
1317         security_d_instantiate(entry, inode);
1318 }
1319 EXPORT_SYMBOL(d_instantiate);
1320
1321 /**
1322  * d_instantiate_unique - instantiate a non-aliased dentry
1323  * @entry: dentry to instantiate
1324  * @inode: inode to attach to this dentry
1325  *
1326  * Fill in inode information in the entry. On success, it returns NULL.
1327  * If an unhashed alias of "entry" already exists, then we return the
1328  * aliased dentry instead and drop one reference to inode.
1329  *
1330  * Note that in order to avoid conflicts with rename() etc, the caller
1331  * had better be holding the parent directory semaphore.
1332  *
1333  * This also assumes that the inode count has been incremented
1334  * (or otherwise set) by the caller to indicate that it is now
1335  * in use by the dcache.
1336  */
1337 static struct dentry *__d_instantiate_unique(struct dentry *entry,
1338                                              struct inode *inode)
1339 {
1340         struct dentry *alias;
1341         int len = entry->d_name.len;
1342         const char *name = entry->d_name.name;
1343         unsigned int hash = entry->d_name.hash;
1344
1345         if (!inode) {
1346                 __d_instantiate(entry, NULL);
1347                 return NULL;
1348         }
1349
1350         list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1351                 struct qstr *qstr = &alias->d_name;
1352
1353                 if (qstr->hash != hash)
1354                         continue;
1355                 if (alias->d_parent != entry->d_parent)
1356                         continue;
1357                 if (qstr->len != len)
1358                         continue;
1359                 if (memcmp(qstr->name, name, len))
1360                         continue;
1361                 dget_locked(alias);
1362                 return alias;
1363         }
1364
1365         __d_instantiate(entry, inode);
1366         return NULL;
1367 }
1368
1369 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1370 {
1371         struct dentry *result;
1372
1373         BUG_ON(!list_empty(&entry->d_alias));
1374
1375         spin_lock(&dcache_lock);
1376         spin_lock(&dcache_inode_lock);
1377         result = __d_instantiate_unique(entry, inode);
1378         spin_unlock(&dcache_inode_lock);
1379         spin_unlock(&dcache_lock);
1380
1381         if (!result) {
1382                 security_d_instantiate(entry, inode);
1383                 return NULL;
1384         }
1385
1386         BUG_ON(!d_unhashed(result));
1387         iput(inode);
1388         return result;
1389 }
1390
1391 EXPORT_SYMBOL(d_instantiate_unique);
1392
1393 /**
1394  * d_alloc_root - allocate root dentry
1395  * @root_inode: inode to allocate the root for
1396  *
1397  * Allocate a root ("/") dentry for the inode given. The inode is
1398  * instantiated and returned. %NULL is returned if there is insufficient
1399  * memory or the inode passed is %NULL.
1400  */
1401  
1402 struct dentry * d_alloc_root(struct inode * root_inode)
1403 {
1404         struct dentry *res = NULL;
1405
1406         if (root_inode) {
1407                 static const struct qstr name = { .name = "/", .len = 1 };
1408
1409                 res = d_alloc(NULL, &name);
1410                 if (res) {
1411                         res->d_sb = root_inode->i_sb;
1412                         res->d_parent = res;
1413                         d_instantiate(res, root_inode);
1414                 }
1415         }
1416         return res;
1417 }
1418 EXPORT_SYMBOL(d_alloc_root);
1419
1420 static inline struct hlist_head *d_hash(struct dentry *parent,
1421                                         unsigned long hash)
1422 {
1423         hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1424         hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1425         return dentry_hashtable + (hash & D_HASHMASK);
1426 }
1427
1428 /**
1429  * d_obtain_alias - find or allocate a dentry for a given inode
1430  * @inode: inode to allocate the dentry for
1431  *
1432  * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1433  * similar open by handle operations.  The returned dentry may be anonymous,
1434  * or may have a full name (if the inode was already in the cache).
1435  *
1436  * When called on a directory inode, we must ensure that the inode only ever
1437  * has one dentry.  If a dentry is found, that is returned instead of
1438  * allocating a new one.
1439  *
1440  * On successful return, the reference to the inode has been transferred
1441  * to the dentry.  In case of an error the reference on the inode is released.
1442  * To make it easier to use in export operations a %NULL or IS_ERR inode may
1443  * be passed in and will be the error will be propagate to the return value,
1444  * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
1445  */
1446 struct dentry *d_obtain_alias(struct inode *inode)
1447 {
1448         static const struct qstr anonstring = { .name = "" };
1449         struct dentry *tmp;
1450         struct dentry *res;
1451
1452         if (!inode)
1453                 return ERR_PTR(-ESTALE);
1454         if (IS_ERR(inode))
1455                 return ERR_CAST(inode);
1456
1457         res = d_find_alias(inode);
1458         if (res)
1459                 goto out_iput;
1460
1461         tmp = d_alloc(NULL, &anonstring);
1462         if (!tmp) {
1463                 res = ERR_PTR(-ENOMEM);
1464                 goto out_iput;
1465         }
1466         tmp->d_parent = tmp; /* make sure dput doesn't croak */
1467
1468         spin_lock(&dcache_lock);
1469         spin_lock(&dcache_inode_lock);
1470         res = __d_find_alias(inode, 0);
1471         if (res) {
1472                 spin_unlock(&dcache_inode_lock);
1473                 spin_unlock(&dcache_lock);
1474                 dput(tmp);
1475                 goto out_iput;
1476         }
1477
1478         /* attach a disconnected dentry */
1479         spin_lock(&tmp->d_lock);
1480         tmp->d_sb = inode->i_sb;
1481         tmp->d_inode = inode;
1482         tmp->d_flags |= DCACHE_DISCONNECTED;
1483         tmp->d_flags &= ~DCACHE_UNHASHED;
1484         list_add(&tmp->d_alias, &inode->i_dentry);
1485         spin_lock(&dcache_hash_lock);
1486         hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1487         spin_unlock(&dcache_hash_lock);
1488         spin_unlock(&tmp->d_lock);
1489         spin_unlock(&dcache_inode_lock);
1490
1491         spin_unlock(&dcache_lock);
1492         return tmp;
1493
1494  out_iput:
1495         iput(inode);
1496         return res;
1497 }
1498 EXPORT_SYMBOL(d_obtain_alias);
1499
1500 /**
1501  * d_splice_alias - splice a disconnected dentry into the tree if one exists
1502  * @inode:  the inode which may have a disconnected dentry
1503  * @dentry: a negative dentry which we want to point to the inode.
1504  *
1505  * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1506  * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1507  * and return it, else simply d_add the inode to the dentry and return NULL.
1508  *
1509  * This is needed in the lookup routine of any filesystem that is exportable
1510  * (via knfsd) so that we can build dcache paths to directories effectively.
1511  *
1512  * If a dentry was found and moved, then it is returned.  Otherwise NULL
1513  * is returned.  This matches the expected return value of ->lookup.
1514  *
1515  */
1516 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1517 {
1518         struct dentry *new = NULL;
1519
1520         if (inode && S_ISDIR(inode->i_mode)) {
1521                 spin_lock(&dcache_lock);
1522                 spin_lock(&dcache_inode_lock);
1523                 new = __d_find_alias(inode, 1);
1524                 if (new) {
1525                         BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1526                         spin_unlock(&dcache_inode_lock);
1527                         spin_unlock(&dcache_lock);
1528                         security_d_instantiate(new, inode);
1529                         d_move(new, dentry);
1530                         iput(inode);
1531                 } else {
1532                         /* already taking dcache_lock, so d_add() by hand */
1533                         __d_instantiate(dentry, inode);
1534                         spin_unlock(&dcache_inode_lock);
1535                         spin_unlock(&dcache_lock);
1536                         security_d_instantiate(dentry, inode);
1537                         d_rehash(dentry);
1538                 }
1539         } else
1540                 d_add(dentry, inode);
1541         return new;
1542 }
1543 EXPORT_SYMBOL(d_splice_alias);
1544
1545 /**
1546  * d_add_ci - lookup or allocate new dentry with case-exact name
1547  * @inode:  the inode case-insensitive lookup has found
1548  * @dentry: the negative dentry that was passed to the parent's lookup func
1549  * @name:   the case-exact name to be associated with the returned dentry
1550  *
1551  * This is to avoid filling the dcache with case-insensitive names to the
1552  * same inode, only the actual correct case is stored in the dcache for
1553  * case-insensitive filesystems.
1554  *
1555  * For a case-insensitive lookup match and if the the case-exact dentry
1556  * already exists in in the dcache, use it and return it.
1557  *
1558  * If no entry exists with the exact case name, allocate new dentry with
1559  * the exact case, and return the spliced entry.
1560  */
1561 struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
1562                         struct qstr *name)
1563 {
1564         int error;
1565         struct dentry *found;
1566         struct dentry *new;
1567
1568         /*
1569          * First check if a dentry matching the name already exists,
1570          * if not go ahead and create it now.
1571          */
1572         found = d_hash_and_lookup(dentry->d_parent, name);
1573         if (!found) {
1574                 new = d_alloc(dentry->d_parent, name);
1575                 if (!new) {
1576                         error = -ENOMEM;
1577                         goto err_out;
1578                 }
1579
1580                 found = d_splice_alias(inode, new);
1581                 if (found) {
1582                         dput(new);
1583                         return found;
1584                 }
1585                 return new;
1586         }
1587
1588         /*
1589          * If a matching dentry exists, and it's not negative use it.
1590          *
1591          * Decrement the reference count to balance the iget() done
1592          * earlier on.
1593          */
1594         if (found->d_inode) {
1595                 if (unlikely(found->d_inode != inode)) {
1596                         /* This can't happen because bad inodes are unhashed. */
1597                         BUG_ON(!is_bad_inode(inode));
1598                         BUG_ON(!is_bad_inode(found->d_inode));
1599                 }
1600                 iput(inode);
1601                 return found;
1602         }
1603
1604         /*
1605          * Negative dentry: instantiate it unless the inode is a directory and
1606          * already has a dentry.
1607          */
1608         spin_lock(&dcache_lock);
1609         spin_lock(&dcache_inode_lock);
1610         if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
1611                 __d_instantiate(found, inode);
1612                 spin_unlock(&dcache_inode_lock);
1613                 spin_unlock(&dcache_lock);
1614                 security_d_instantiate(found, inode);
1615                 return found;
1616         }
1617
1618         /*
1619          * In case a directory already has a (disconnected) entry grab a
1620          * reference to it, move it in place and use it.
1621          */
1622         new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1623         dget_locked(new);
1624         spin_unlock(&dcache_inode_lock);
1625         spin_unlock(&dcache_lock);
1626         security_d_instantiate(found, inode);
1627         d_move(new, found);
1628         iput(inode);
1629         dput(found);
1630         return new;
1631
1632 err_out:
1633         iput(inode);
1634         return ERR_PTR(error);
1635 }
1636 EXPORT_SYMBOL(d_add_ci);
1637
1638 /**
1639  * d_lookup - search for a dentry
1640  * @parent: parent dentry
1641  * @name: qstr of name we wish to find
1642  * Returns: dentry, or NULL
1643  *
1644  * d_lookup searches the children of the parent dentry for the name in
1645  * question. If the dentry is found its reference count is incremented and the
1646  * dentry is returned. The caller must use dput to free the entry when it has
1647  * finished using it. %NULL is returned if the dentry does not exist.
1648  */
1649 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1650 {
1651         struct dentry * dentry = NULL;
1652         unsigned long seq;
1653
1654         do {
1655                 seq = read_seqbegin(&rename_lock);
1656                 dentry = __d_lookup(parent, name);
1657                 if (dentry)
1658                         break;
1659         } while (read_seqretry(&rename_lock, seq));
1660         return dentry;
1661 }
1662 EXPORT_SYMBOL(d_lookup);
1663
1664 /*
1665  * __d_lookup - search for a dentry (racy)
1666  * @parent: parent dentry
1667  * @name: qstr of name we wish to find
1668  * Returns: dentry, or NULL
1669  *
1670  * __d_lookup is like d_lookup, however it may (rarely) return a
1671  * false-negative result due to unrelated rename activity.
1672  *
1673  * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1674  * however it must be used carefully, eg. with a following d_lookup in
1675  * the case of failure.
1676  *
1677  * __d_lookup callers must be commented.
1678  */
1679 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1680 {
1681         unsigned int len = name->len;
1682         unsigned int hash = name->hash;
1683         const unsigned char *str = name->name;
1684         struct hlist_head *head = d_hash(parent,hash);
1685         struct dentry *found = NULL;
1686         struct hlist_node *node;
1687         struct dentry *dentry;
1688
1689         /*
1690          * The hash list is protected using RCU.
1691          *
1692          * Take d_lock when comparing a candidate dentry, to avoid races
1693          * with d_move().
1694          *
1695          * It is possible that concurrent renames can mess up our list
1696          * walk here and result in missing our dentry, resulting in the
1697          * false-negative result. d_lookup() protects against concurrent
1698          * renames using rename_lock seqlock.
1699          *
1700          * See Documentation/vfs/dcache-locking.txt for more details.
1701          */
1702         rcu_read_lock();
1703         
1704         hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1705                 struct qstr *qstr;
1706
1707                 if (dentry->d_name.hash != hash)
1708                         continue;
1709                 if (dentry->d_parent != parent)
1710                         continue;
1711
1712                 spin_lock(&dentry->d_lock);
1713
1714                 /*
1715                  * Recheck the dentry after taking the lock - d_move may have
1716                  * changed things. Don't bother checking the hash because
1717                  * we're about to compare the whole name anyway.
1718                  */
1719                 if (dentry->d_parent != parent)
1720                         goto next;
1721
1722                 /* non-existing due to RCU? */
1723                 if (d_unhashed(dentry))
1724                         goto next;
1725
1726                 /*
1727                  * It is safe to compare names since d_move() cannot
1728                  * change the qstr (protected by d_lock).
1729                  */
1730                 qstr = &dentry->d_name;
1731                 if (parent->d_op && parent->d_op->d_compare) {
1732                         if (parent->d_op->d_compare(parent, parent->d_inode,
1733                                                 dentry, dentry->d_inode,
1734                                                 qstr->len, qstr->name, name))
1735                                 goto next;
1736                 } else {
1737                         if (qstr->len != len)
1738                                 goto next;
1739                         if (memcmp(qstr->name, str, len))
1740                                 goto next;
1741                 }
1742
1743                 dentry->d_count++;
1744                 found = dentry;
1745                 spin_unlock(&dentry->d_lock);
1746                 break;
1747 next:
1748                 spin_unlock(&dentry->d_lock);
1749         }
1750         rcu_read_unlock();
1751
1752         return found;
1753 }
1754
1755 /**
1756  * d_hash_and_lookup - hash the qstr then search for a dentry
1757  * @dir: Directory to search in
1758  * @name: qstr of name we wish to find
1759  *
1760  * On hash failure or on lookup failure NULL is returned.
1761  */
1762 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1763 {
1764         struct dentry *dentry = NULL;
1765
1766         /*
1767          * Check for a fs-specific hash function. Note that we must
1768          * calculate the standard hash first, as the d_op->d_hash()
1769          * routine may choose to leave the hash value unchanged.
1770          */
1771         name->hash = full_name_hash(name->name, name->len);
1772         if (dir->d_op && dir->d_op->d_hash) {
1773                 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
1774                         goto out;
1775         }
1776         dentry = d_lookup(dir, name);
1777 out:
1778         return dentry;
1779 }
1780
1781 /**
1782  * d_validate - verify dentry provided from insecure source (deprecated)
1783  * @dentry: The dentry alleged to be valid child of @dparent
1784  * @dparent: The parent dentry (known to be valid)
1785  *
1786  * An insecure source has sent us a dentry, here we verify it and dget() it.
1787  * This is used by ncpfs in its readdir implementation.
1788  * Zero is returned in the dentry is invalid.
1789  *
1790  * This function is slow for big directories, and deprecated, do not use it.
1791  */
1792 int d_validate(struct dentry *dentry, struct dentry *dparent)
1793 {
1794         struct dentry *child;
1795
1796         spin_lock(&dcache_lock);
1797         spin_lock(&dparent->d_lock);
1798         list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1799                 if (dentry == child) {
1800                         spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1801                         __dget_locked_dlock(dentry);
1802                         spin_unlock(&dentry->d_lock);
1803                         spin_unlock(&dparent->d_lock);
1804                         spin_unlock(&dcache_lock);
1805                         return 1;
1806                 }
1807         }
1808         spin_unlock(&dparent->d_lock);
1809         spin_unlock(&dcache_lock);
1810
1811         return 0;
1812 }
1813 EXPORT_SYMBOL(d_validate);
1814
1815 /*
1816  * When a file is deleted, we have two options:
1817  * - turn this dentry into a negative dentry
1818  * - unhash this dentry and free it.
1819  *
1820  * Usually, we want to just turn this into
1821  * a negative dentry, but if anybody else is
1822  * currently using the dentry or the inode
1823  * we can't do that and we fall back on removing
1824  * it from the hash queues and waiting for
1825  * it to be deleted later when it has no users
1826  */
1827  
1828 /**
1829  * d_delete - delete a dentry
1830  * @dentry: The dentry to delete
1831  *
1832  * Turn the dentry into a negative dentry if possible, otherwise
1833  * remove it from the hash queues so it can be deleted later
1834  */
1835  
1836 void d_delete(struct dentry * dentry)
1837 {
1838         int isdir = 0;
1839         /*
1840          * Are we the only user?
1841          */
1842         spin_lock(&dcache_lock);
1843         spin_lock(&dcache_inode_lock);
1844         spin_lock(&dentry->d_lock);
1845         isdir = S_ISDIR(dentry->d_inode->i_mode);
1846         if (dentry->d_count == 1) {
1847                 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
1848                 dentry_iput(dentry);
1849                 fsnotify_nameremove(dentry, isdir);
1850                 return;
1851         }
1852
1853         if (!d_unhashed(dentry))
1854                 __d_drop(dentry);
1855
1856         spin_unlock(&dentry->d_lock);
1857         spin_unlock(&dcache_inode_lock);
1858         spin_unlock(&dcache_lock);
1859
1860         fsnotify_nameremove(dentry, isdir);
1861 }
1862 EXPORT_SYMBOL(d_delete);
1863
1864 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1865 {
1866
1867         entry->d_flags &= ~DCACHE_UNHASHED;
1868         hlist_add_head_rcu(&entry->d_hash, list);
1869 }
1870
1871 static void _d_rehash(struct dentry * entry)
1872 {
1873         __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1874 }
1875
1876 /**
1877  * d_rehash     - add an entry back to the hash
1878  * @entry: dentry to add to the hash
1879  *
1880  * Adds a dentry to the hash according to its name.
1881  */
1882  
1883 void d_rehash(struct dentry * entry)
1884 {
1885         spin_lock(&dcache_lock);
1886         spin_lock(&entry->d_lock);
1887         spin_lock(&dcache_hash_lock);
1888         _d_rehash(entry);
1889         spin_unlock(&dcache_hash_lock);
1890         spin_unlock(&entry->d_lock);
1891         spin_unlock(&dcache_lock);
1892 }
1893 EXPORT_SYMBOL(d_rehash);
1894
1895 /**
1896  * dentry_update_name_case - update case insensitive dentry with a new name
1897  * @dentry: dentry to be updated
1898  * @name: new name
1899  *
1900  * Update a case insensitive dentry with new case of name.
1901  *
1902  * dentry must have been returned by d_lookup with name @name. Old and new
1903  * name lengths must match (ie. no d_compare which allows mismatched name
1904  * lengths).
1905  *
1906  * Parent inode i_mutex must be held over d_lookup and into this call (to
1907  * keep renames and concurrent inserts, and readdir(2) away).
1908  */
1909 void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
1910 {
1911         BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
1912         BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
1913
1914         spin_lock(&dcache_lock);
1915         spin_lock(&dentry->d_lock);
1916         memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
1917         spin_unlock(&dentry->d_lock);
1918         spin_unlock(&dcache_lock);
1919 }
1920 EXPORT_SYMBOL(dentry_update_name_case);
1921
1922 static void switch_names(struct dentry *dentry, struct dentry *target)
1923 {
1924         if (dname_external(target)) {
1925                 if (dname_external(dentry)) {
1926                         /*
1927                          * Both external: swap the pointers
1928                          */
1929                         swap(target->d_name.name, dentry->d_name.name);
1930                 } else {
1931                         /*
1932                          * dentry:internal, target:external.  Steal target's
1933                          * storage and make target internal.
1934                          */
1935                         memcpy(target->d_iname, dentry->d_name.name,
1936                                         dentry->d_name.len + 1);
1937                         dentry->d_name.name = target->d_name.name;
1938                         target->d_name.name = target->d_iname;
1939                 }
1940         } else {
1941                 if (dname_external(dentry)) {
1942                         /*
1943                          * dentry:external, target:internal.  Give dentry's
1944                          * storage to target and make dentry internal
1945                          */
1946                         memcpy(dentry->d_iname, target->d_name.name,
1947                                         target->d_name.len + 1);
1948                         target->d_name.name = dentry->d_name.name;
1949                         dentry->d_name.name = dentry->d_iname;
1950                 } else {
1951                         /*
1952                          * Both are internal.  Just copy target to dentry
1953                          */
1954                         memcpy(dentry->d_iname, target->d_name.name,
1955                                         target->d_name.len + 1);
1956                         dentry->d_name.len = target->d_name.len;
1957                         return;
1958                 }
1959         }
1960         swap(dentry->d_name.len, target->d_name.len);
1961 }
1962
1963 static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
1964 {
1965         /*
1966          * XXXX: do we really need to take target->d_lock?
1967          */
1968         if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
1969                 spin_lock(&target->d_parent->d_lock);
1970         else {
1971                 if (d_ancestor(dentry->d_parent, target->d_parent)) {
1972                         spin_lock(&dentry->d_parent->d_lock);
1973                         spin_lock_nested(&target->d_parent->d_lock,
1974                                                 DENTRY_D_LOCK_NESTED);
1975                 } else {
1976                         spin_lock(&target->d_parent->d_lock);
1977                         spin_lock_nested(&dentry->d_parent->d_lock,
1978                                                 DENTRY_D_LOCK_NESTED);
1979                 }
1980         }
1981         if (target < dentry) {
1982                 spin_lock_nested(&target->d_lock, 2);
1983                 spin_lock_nested(&dentry->d_lock, 3);
1984         } else {
1985                 spin_lock_nested(&dentry->d_lock, 2);
1986                 spin_lock_nested(&target->d_lock, 3);
1987         }
1988 }
1989
1990 static void dentry_unlock_parents_for_move(struct dentry *dentry,
1991                                         struct dentry *target)
1992 {
1993         if (target->d_parent != dentry->d_parent)
1994                 spin_unlock(&dentry->d_parent->d_lock);
1995         if (target->d_parent != target)
1996                 spin_unlock(&target->d_parent->d_lock);
1997 }
1998
1999 /*
2000  * When switching names, the actual string doesn't strictly have to
2001  * be preserved in the target - because we're dropping the target
2002  * anyway. As such, we can just do a simple memcpy() to copy over
2003  * the new name before we switch.
2004  *
2005  * Note that we have to be a lot more careful about getting the hash
2006  * switched - we have to switch the hash value properly even if it
2007  * then no longer matches the actual (corrupted) string of the target.
2008  * The hash value has to match the hash queue that the dentry is on..
2009  */
2010 /*
2011  * d_move_locked - move a dentry
2012  * @dentry: entry to move
2013  * @target: new dentry
2014  *
2015  * Update the dcache to reflect the move of a file name. Negative
2016  * dcache entries should not be moved in this way.
2017  */
2018 static void d_move_locked(struct dentry * dentry, struct dentry * target)
2019 {
2020         if (!dentry->d_inode)
2021                 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2022
2023         BUG_ON(d_ancestor(dentry, target));
2024         BUG_ON(d_ancestor(target, dentry));
2025
2026         write_seqlock(&rename_lock);
2027
2028         dentry_lock_for_move(dentry, target);
2029
2030         /* Move the dentry to the target hash queue, if on different bucket */
2031         spin_lock(&dcache_hash_lock);
2032         if (!d_unhashed(dentry))
2033                 hlist_del_rcu(&dentry->d_hash);
2034         __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
2035         spin_unlock(&dcache_hash_lock);
2036
2037         /* Unhash the target: dput() will then get rid of it */
2038         __d_drop(target);
2039
2040         list_del(&dentry->d_u.d_child);
2041         list_del(&target->d_u.d_child);
2042
2043         /* Switch the names.. */
2044         switch_names(dentry, target);
2045         swap(dentry->d_name.hash, target->d_name.hash);
2046
2047         /* ... and switch the parents */
2048         if (IS_ROOT(dentry)) {
2049                 dentry->d_parent = target->d_parent;
2050                 target->d_parent = target;
2051                 INIT_LIST_HEAD(&target->d_u.d_child);
2052         } else {
2053                 swap(dentry->d_parent, target->d_parent);
2054
2055                 /* And add them back to the (new) parent lists */
2056                 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
2057         }
2058
2059         list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2060
2061         dentry_unlock_parents_for_move(dentry, target);
2062         spin_unlock(&target->d_lock);
2063         fsnotify_d_move(dentry);
2064         spin_unlock(&dentry->d_lock);
2065         write_sequnlock(&rename_lock);
2066 }
2067
2068 /**
2069  * d_move - move a dentry
2070  * @dentry: entry to move
2071  * @target: new dentry
2072  *
2073  * Update the dcache to reflect the move of a file name. Negative
2074  * dcache entries should not be moved in this way.
2075  */
2076
2077 void d_move(struct dentry * dentry, struct dentry * target)
2078 {
2079         spin_lock(&dcache_lock);
2080         d_move_locked(dentry, target);
2081         spin_unlock(&dcache_lock);
2082 }
2083 EXPORT_SYMBOL(d_move);
2084
2085 /**
2086  * d_ancestor - search for an ancestor
2087  * @p1: ancestor dentry
2088  * @p2: child dentry
2089  *
2090  * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2091  * an ancestor of p2, else NULL.
2092  */
2093 struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
2094 {
2095         struct dentry *p;
2096
2097         for (p = p2; !IS_ROOT(p); p = p->d_parent) {
2098                 if (p->d_parent == p1)
2099                         return p;
2100         }
2101         return NULL;
2102 }
2103
2104 /*
2105  * This helper attempts to cope with remotely renamed directories
2106  *
2107  * It assumes that the caller is already holding
2108  * dentry->d_parent->d_inode->i_mutex and the dcache_lock
2109  *
2110  * Note: If ever the locking in lock_rename() changes, then please
2111  * remember to update this too...
2112  */
2113 static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
2114         __releases(dcache_lock)
2115         __releases(dcache_inode_lock)
2116 {
2117         struct mutex *m1 = NULL, *m2 = NULL;
2118         struct dentry *ret;
2119
2120         /* If alias and dentry share a parent, then no extra locks required */
2121         if (alias->d_parent == dentry->d_parent)
2122                 goto out_unalias;
2123
2124         /* Check for loops */
2125         ret = ERR_PTR(-ELOOP);
2126         if (d_ancestor(alias, dentry))
2127                 goto out_err;
2128
2129         /* See lock_rename() */
2130         ret = ERR_PTR(-EBUSY);
2131         if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2132                 goto out_err;
2133         m1 = &dentry->d_sb->s_vfs_rename_mutex;
2134         if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2135                 goto out_err;
2136         m2 = &alias->d_parent->d_inode->i_mutex;
2137 out_unalias:
2138         d_move_locked(alias, dentry);
2139         ret = alias;
2140 out_err:
2141         spin_unlock(&dcache_inode_lock);
2142         spin_unlock(&dcache_lock);
2143         if (m2)
2144                 mutex_unlock(m2);
2145         if (m1)
2146                 mutex_unlock(m1);
2147         return ret;
2148 }
2149
2150 /*
2151  * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2152  * named dentry in place of the dentry to be replaced.
2153  * returns with anon->d_lock held!
2154  */
2155 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2156 {
2157         struct dentry *dparent, *aparent;
2158
2159         dentry_lock_for_move(anon, dentry);
2160
2161         dparent = dentry->d_parent;
2162         aparent = anon->d_parent;
2163
2164         switch_names(dentry, anon);
2165         swap(dentry->d_name.hash, anon->d_name.hash);
2166
2167         dentry->d_parent = (aparent == anon) ? dentry : aparent;
2168         list_del(&dentry->d_u.d_child);
2169         if (!IS_ROOT(dentry))
2170                 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2171         else
2172                 INIT_LIST_HEAD(&dentry->d_u.d_child);
2173
2174         anon->d_parent = (dparent == dentry) ? anon : dparent;
2175         list_del(&anon->d_u.d_child);
2176         if (!IS_ROOT(anon))
2177                 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
2178         else
2179                 INIT_LIST_HEAD(&anon->d_u.d_child);
2180
2181         dentry_unlock_parents_for_move(anon, dentry);
2182         spin_unlock(&dentry->d_lock);
2183
2184         /* anon->d_lock still locked, returns locked */
2185         anon->d_flags &= ~DCACHE_DISCONNECTED;
2186 }
2187
2188 /**
2189  * d_materialise_unique - introduce an inode into the tree
2190  * @dentry: candidate dentry
2191  * @inode: inode to bind to the dentry, to which aliases may be attached
2192  *
2193  * Introduces an dentry into the tree, substituting an extant disconnected
2194  * root directory alias in its place if there is one
2195  */
2196 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2197 {
2198         struct dentry *actual;
2199
2200         BUG_ON(!d_unhashed(dentry));
2201
2202         spin_lock(&dcache_lock);
2203         spin_lock(&dcache_inode_lock);
2204
2205         if (!inode) {
2206                 actual = dentry;
2207                 __d_instantiate(dentry, NULL);
2208                 goto found_lock;
2209         }
2210
2211         if (S_ISDIR(inode->i_mode)) {
2212                 struct dentry *alias;
2213
2214                 /* Does an aliased dentry already exist? */
2215                 alias = __d_find_alias(inode, 0);
2216                 if (alias) {
2217                         actual = alias;
2218                         /* Is this an anonymous mountpoint that we could splice
2219                          * into our tree? */
2220                         if (IS_ROOT(alias)) {
2221                                 __d_materialise_dentry(dentry, alias);
2222                                 __d_drop(alias);
2223                                 goto found;
2224                         }
2225                         /* Nope, but we must(!) avoid directory aliasing */
2226                         actual = __d_unalias(dentry, alias);
2227                         if (IS_ERR(actual))
2228                                 dput(alias);
2229                         goto out_nolock;
2230                 }
2231         }
2232
2233         /* Add a unique reference */
2234         actual = __d_instantiate_unique(dentry, inode);
2235         if (!actual)
2236                 actual = dentry;
2237         else if (unlikely(!d_unhashed(actual)))
2238                 goto shouldnt_be_hashed;
2239
2240 found_lock:
2241         spin_lock(&actual->d_lock);
2242 found:
2243         spin_lock(&dcache_hash_lock);
2244         _d_rehash(actual);
2245         spin_unlock(&dcache_hash_lock);
2246         spin_unlock(&actual->d_lock);
2247         spin_unlock(&dcache_inode_lock);
2248         spin_unlock(&dcache_lock);
2249 out_nolock:
2250         if (actual == dentry) {
2251                 security_d_instantiate(dentry, inode);
2252                 return NULL;
2253         }
2254
2255         iput(inode);
2256         return actual;
2257
2258 shouldnt_be_hashed:
2259         spin_unlock(&dcache_inode_lock);
2260         spin_unlock(&dcache_lock);
2261         BUG();
2262 }
2263 EXPORT_SYMBOL_GPL(d_materialise_unique);
2264
2265 static int prepend(char **buffer, int *buflen, const char *str, int namelen)
2266 {
2267         *buflen -= namelen;
2268         if (*buflen < 0)
2269                 return -ENAMETOOLONG;
2270         *buffer -= namelen;
2271         memcpy(*buffer, str, namelen);
2272         return 0;
2273 }
2274
2275 static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2276 {
2277         return prepend(buffer, buflen, name->name, name->len);
2278 }
2279
2280 /**
2281  * Prepend path string to a buffer
2282  *
2283  * @path: the dentry/vfsmount to report
2284  * @root: root vfsmnt/dentry (may be modified by this function)
2285  * @buffer: pointer to the end of the buffer
2286  * @buflen: pointer to buffer length
2287  *
2288  * Caller holds the dcache_lock.
2289  *
2290  * If path is not reachable from the supplied root, then the value of
2291  * root is changed (without modifying refcounts).
2292  */
2293 static int prepend_path(const struct path *path, struct path *root,
2294                         char **buffer, int *buflen)
2295 {
2296         struct dentry *dentry = path->dentry;
2297         struct vfsmount *vfsmnt = path->mnt;
2298         bool slash = false;
2299         int error = 0;
2300
2301         br_read_lock(vfsmount_lock);
2302         while (dentry != root->dentry || vfsmnt != root->mnt) {
2303                 struct dentry * parent;
2304
2305                 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2306                         /* Global root? */
2307                         if (vfsmnt->mnt_parent == vfsmnt) {
2308                                 goto global_root;
2309                         }
2310                         dentry = vfsmnt->mnt_mountpoint;
2311                         vfsmnt = vfsmnt->mnt_parent;
2312                         continue;
2313                 }
2314                 parent = dentry->d_parent;
2315                 prefetch(parent);
2316                 error = prepend_name(buffer, buflen, &dentry->d_name);
2317                 if (!error)
2318                         error = prepend(buffer, buflen, "/", 1);
2319                 if (error)
2320                         break;
2321
2322                 slash = true;
2323                 dentry = parent;
2324         }
2325
2326 out:
2327         if (!error && !slash)
2328                 error = prepend(buffer, buflen, "/", 1);
2329
2330         br_read_unlock(vfsmount_lock);
2331         return error;
2332
2333 global_root:
2334         /*
2335          * Filesystems needing to implement special "root names"
2336          * should do so with ->d_dname()
2337          */
2338         if (IS_ROOT(dentry) &&
2339             (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2340                 WARN(1, "Root dentry has weird name <%.*s>\n",
2341                      (int) dentry->d_name.len, dentry->d_name.name);
2342         }
2343         root->mnt = vfsmnt;
2344         root->dentry = dentry;
2345         goto out;
2346 }
2347
2348 /**
2349  * __d_path - return the path of a dentry
2350  * @path: the dentry/vfsmount to report
2351  * @root: root vfsmnt/dentry (may be modified by this function)
2352  * @buf: buffer to return value in
2353  * @buflen: buffer length
2354  *
2355  * Convert a dentry into an ASCII path name.
2356  *
2357  * Returns a pointer into the buffer or an error code if the
2358  * path was too long.
2359  *
2360  * "buflen" should be positive.
2361  *
2362  * If path is not reachable from the supplied root, then the value of
2363  * root is changed (without modifying refcounts).
2364  */
2365 char *__d_path(const struct path *path, struct path *root,
2366                char *buf, int buflen)
2367 {
2368         char *res = buf + buflen;
2369         int error;
2370
2371         prepend(&res, &buflen, "\0", 1);
2372         spin_lock(&dcache_lock);
2373         error = prepend_path(path, root, &res, &buflen);
2374         spin_unlock(&dcache_lock);
2375
2376         if (error)
2377                 return ERR_PTR(error);
2378         return res;
2379 }
2380
2381 /*
2382  * same as __d_path but appends "(deleted)" for unlinked files.
2383  */
2384 static int path_with_deleted(const struct path *path, struct path *root,
2385                                  char **buf, int *buflen)
2386 {
2387         prepend(buf, buflen, "\0", 1);
2388         if (d_unlinked(path->dentry)) {
2389                 int error = prepend(buf, buflen, " (deleted)", 10);
2390                 if (error)
2391                         return error;
2392         }
2393
2394         return prepend_path(path, root, buf, buflen);
2395 }
2396
2397 static int prepend_unreachable(char **buffer, int *buflen)
2398 {
2399         return prepend(buffer, buflen, "(unreachable)", 13);
2400 }
2401
2402 /**
2403  * d_path - return the path of a dentry
2404  * @path: path to report
2405  * @buf: buffer to return value in
2406  * @buflen: buffer length
2407  *
2408  * Convert a dentry into an ASCII path name. If the entry has been deleted
2409  * the string " (deleted)" is appended. Note that this is ambiguous.
2410  *
2411  * Returns a pointer into the buffer or an error code if the path was
2412  * too long. Note: Callers should use the returned pointer, not the passed
2413  * in buffer, to use the name! The implementation often starts at an offset
2414  * into the buffer, and may leave 0 bytes at the start.
2415  *
2416  * "buflen" should be positive.
2417  */
2418 char *d_path(const struct path *path, char *buf, int buflen)
2419 {
2420         char *res = buf + buflen;
2421         struct path root;
2422         struct path tmp;
2423         int error;
2424
2425         /*
2426          * We have various synthetic filesystems that never get mounted.  On
2427          * these filesystems dentries are never used for lookup purposes, and
2428          * thus don't need to be hashed.  They also don't need a name until a
2429          * user wants to identify the object in /proc/pid/fd/.  The little hack
2430          * below allows us to generate a name for these objects on demand:
2431          */
2432         if (path->dentry->d_op && path->dentry->d_op->d_dname)
2433                 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2434
2435         get_fs_root(current->fs, &root);
2436         spin_lock(&dcache_lock);
2437         tmp = root;
2438         error = path_with_deleted(path, &tmp, &res, &buflen);
2439         if (error)
2440                 res = ERR_PTR(error);
2441         spin_unlock(&dcache_lock);
2442         path_put(&root);
2443         return res;
2444 }
2445 EXPORT_SYMBOL(d_path);
2446
2447 /**
2448  * d_path_with_unreachable - return the path of a dentry
2449  * @path: path to report
2450  * @buf: buffer to return value in
2451  * @buflen: buffer length
2452  *
2453  * The difference from d_path() is that this prepends "(unreachable)"
2454  * to paths which are unreachable from the current process' root.
2455  */
2456 char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2457 {
2458         char *res = buf + buflen;
2459         struct path root;
2460         struct path tmp;
2461         int error;
2462
2463         if (path->dentry->d_op && path->dentry->d_op->d_dname)
2464                 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2465
2466         get_fs_root(current->fs, &root);
2467         spin_lock(&dcache_lock);
2468         tmp = root;
2469         error = path_with_deleted(path, &tmp, &res, &buflen);
2470         if (!error && !path_equal(&tmp, &root))
2471                 error = prepend_unreachable(&res, &buflen);
2472         spin_unlock(&dcache_lock);
2473         path_put(&root);
2474         if (error)
2475                 res =  ERR_PTR(error);
2476
2477         return res;
2478 }
2479
2480 /*
2481  * Helper function for dentry_operations.d_dname() members
2482  */
2483 char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2484                         const char *fmt, ...)
2485 {
2486         va_list args;
2487         char temp[64];
2488         int sz;
2489
2490         va_start(args, fmt);
2491         sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2492         va_end(args);
2493
2494         if (sz > sizeof(temp) || sz > buflen)
2495                 return ERR_PTR(-ENAMETOOLONG);
2496
2497         buffer += buflen - sz;
2498         return memcpy(buffer, temp, sz);
2499 }
2500
2501 /*
2502  * Write full pathname from the root of the filesystem into the buffer.
2503  */
2504 static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
2505 {
2506         char *end = buf + buflen;
2507         char *retval;
2508
2509         prepend(&end, &buflen, "\0", 1);
2510         if (buflen < 1)
2511                 goto Elong;
2512         /* Get '/' right */
2513         retval = end-1;
2514         *retval = '/';
2515
2516         while (!IS_ROOT(dentry)) {
2517                 struct dentry *parent = dentry->d_parent;
2518
2519                 prefetch(parent);
2520                 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
2521                     (prepend(&end, &buflen, "/", 1) != 0))
2522                         goto Elong;
2523
2524                 retval = end;
2525                 dentry = parent;
2526         }
2527         return retval;
2528 Elong:
2529         return ERR_PTR(-ENAMETOOLONG);
2530 }
2531
2532 char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2533 {
2534         char *retval;
2535
2536         spin_lock(&dcache_lock);
2537         retval = __dentry_path(dentry, buf, buflen);
2538         spin_unlock(&dcache_lock);
2539
2540         return retval;
2541 }
2542 EXPORT_SYMBOL(dentry_path_raw);
2543
2544 char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2545 {
2546         char *p = NULL;
2547         char *retval;
2548
2549         spin_lock(&dcache_lock);
2550         if (d_unlinked(dentry)) {
2551                 p = buf + buflen;
2552                 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2553                         goto Elong;
2554                 buflen++;
2555         }
2556         retval = __dentry_path(dentry, buf, buflen);
2557         spin_unlock(&dcache_lock);
2558         if (!IS_ERR(retval) && p)
2559                 *p = '/';       /* restore '/' overriden with '\0' */
2560         return retval;
2561 Elong:
2562         spin_unlock(&dcache_lock);
2563         return ERR_PTR(-ENAMETOOLONG);
2564 }
2565
2566 /*
2567  * NOTE! The user-level library version returns a
2568  * character pointer. The kernel system call just
2569  * returns the length of the buffer filled (which
2570  * includes the ending '\0' character), or a negative
2571  * error value. So libc would do something like
2572  *
2573  *      char *getcwd(char * buf, size_t size)
2574  *      {
2575  *              int retval;
2576  *
2577  *              retval = sys_getcwd(buf, size);
2578  *              if (retval >= 0)
2579  *                      return buf;
2580  *              errno = -retval;
2581  *              return NULL;
2582  *      }
2583  */
2584 SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
2585 {
2586         int error;
2587         struct path pwd, root;
2588         char *page = (char *) __get_free_page(GFP_USER);
2589
2590         if (!page)
2591                 return -ENOMEM;
2592
2593         get_fs_root_and_pwd(current->fs, &root, &pwd);
2594
2595         error = -ENOENT;
2596         spin_lock(&dcache_lock);
2597         if (!d_unlinked(pwd.dentry)) {
2598                 unsigned long len;
2599                 struct path tmp = root;
2600                 char *cwd = page + PAGE_SIZE;
2601                 int buflen = PAGE_SIZE;
2602
2603                 prepend(&cwd, &buflen, "\0", 1);
2604                 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
2605                 spin_unlock(&dcache_lock);
2606
2607                 if (error)
2608                         goto out;
2609
2610                 /* Unreachable from current root */
2611                 if (!path_equal(&tmp, &root)) {
2612                         error = prepend_unreachable(&cwd, &buflen);
2613                         if (error)
2614                                 goto out;
2615                 }
2616
2617                 error = -ERANGE;
2618                 len = PAGE_SIZE + page - cwd;
2619                 if (len <= size) {
2620                         error = len;
2621                         if (copy_to_user(buf, cwd, len))
2622                                 error = -EFAULT;
2623                 }
2624         } else
2625                 spin_unlock(&dcache_lock);
2626
2627 out:
2628         path_put(&pwd);
2629         path_put(&root);
2630         free_page((unsigned long) page);
2631         return error;
2632 }
2633
2634 /*
2635  * Test whether new_dentry is a subdirectory of old_dentry.
2636  *
2637  * Trivially implemented using the dcache structure
2638  */
2639
2640 /**
2641  * is_subdir - is new dentry a subdirectory of old_dentry
2642  * @new_dentry: new dentry
2643  * @old_dentry: old dentry
2644  *
2645  * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2646  * Returns 0 otherwise.
2647  * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2648  */
2649   
2650 int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
2651 {
2652         int result;
2653         unsigned long seq;
2654
2655         if (new_dentry == old_dentry)
2656                 return 1;
2657
2658         /*
2659          * Need rcu_readlock to protect against the d_parent trashing
2660          * due to d_move
2661          */
2662         rcu_read_lock();
2663         do {
2664                 /* for restarting inner loop in case of seq retry */
2665                 seq = read_seqbegin(&rename_lock);
2666                 if (d_ancestor(old_dentry, new_dentry))
2667                         result = 1;
2668                 else
2669                         result = 0;
2670         } while (read_seqretry(&rename_lock, seq));
2671         rcu_read_unlock();
2672
2673         return result;
2674 }
2675
2676 int path_is_under(struct path *path1, struct path *path2)
2677 {
2678         struct vfsmount *mnt = path1->mnt;
2679         struct dentry *dentry = path1->dentry;
2680         int res;
2681
2682         br_read_lock(vfsmount_lock);
2683         if (mnt != path2->mnt) {
2684                 for (;;) {
2685                         if (mnt->mnt_parent == mnt) {
2686                                 br_read_unlock(vfsmount_lock);
2687                                 return 0;
2688                         }
2689                         if (mnt->mnt_parent == path2->mnt)
2690                                 break;
2691                         mnt = mnt->mnt_parent;
2692                 }
2693                 dentry = mnt->mnt_mountpoint;
2694         }
2695         res = is_subdir(dentry, path2->dentry);
2696         br_read_unlock(vfsmount_lock);
2697         return res;
2698 }
2699 EXPORT_SYMBOL(path_is_under);
2700
2701 void d_genocide(struct dentry *root)
2702 {
2703         struct dentry *this_parent = root;
2704         struct list_head *next;
2705
2706         spin_lock(&dcache_lock);
2707         spin_lock(&this_parent->d_lock);
2708 repeat:
2709         next = this_parent->d_subdirs.next;
2710 resume:
2711         while (next != &this_parent->d_subdirs) {
2712                 struct list_head *tmp = next;
2713                 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
2714                 next = tmp->next;
2715                 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2716                 if (d_unhashed(dentry) || !dentry->d_inode) {
2717                         spin_unlock(&dentry->d_lock);
2718                         continue;
2719                 }
2720                 if (!list_empty(&dentry->d_subdirs)) {
2721                         spin_unlock(&this_parent->d_lock);
2722                         spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
2723                         this_parent = dentry;
2724                         spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
2725                         goto repeat;
2726                 }
2727                 dentry->d_count--;
2728                 spin_unlock(&dentry->d_lock);
2729         }
2730         if (this_parent != root) {
2731                 next = this_parent->d_u.d_child.next;
2732                 this_parent->d_count--;
2733                 spin_unlock(&this_parent->d_lock);
2734                 this_parent = this_parent->d_parent;
2735                 spin_lock(&this_parent->d_lock);
2736                 goto resume;
2737         }
2738         spin_unlock(&this_parent->d_lock);
2739         spin_unlock(&dcache_lock);
2740 }
2741
2742 /**
2743  * find_inode_number - check for dentry with name
2744  * @dir: directory to check
2745  * @name: Name to find.
2746  *
2747  * Check whether a dentry already exists for the given name,
2748  * and return the inode number if it has an inode. Otherwise
2749  * 0 is returned.
2750  *
2751  * This routine is used to post-process directory listings for
2752  * filesystems using synthetic inode numbers, and is necessary
2753  * to keep getcwd() working.
2754  */
2755  
2756 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2757 {
2758         struct dentry * dentry;
2759         ino_t ino = 0;
2760
2761         dentry = d_hash_and_lookup(dir, name);
2762         if (dentry) {
2763                 if (dentry->d_inode)
2764                         ino = dentry->d_inode->i_ino;
2765                 dput(dentry);
2766         }
2767         return ino;
2768 }
2769 EXPORT_SYMBOL(find_inode_number);
2770
2771 static __initdata unsigned long dhash_entries;
2772 static int __init set_dhash_entries(char *str)
2773 {
2774         if (!str)
2775                 return 0;
2776         dhash_entries = simple_strtoul(str, &str, 0);
2777         return 1;
2778 }
2779 __setup("dhash_entries=", set_dhash_entries);
2780
2781 static void __init dcache_init_early(void)
2782 {
2783         int loop;
2784
2785         /* If hashes are distributed across NUMA nodes, defer
2786          * hash allocation until vmalloc space is available.
2787          */
2788         if (hashdist)
2789                 return;
2790
2791         dentry_hashtable =
2792                 alloc_large_system_hash("Dentry cache",
2793                                         sizeof(struct hlist_head),
2794                                         dhash_entries,
2795                                         13,
2796                                         HASH_EARLY,
2797                                         &d_hash_shift,
2798                                         &d_hash_mask,
2799                                         0);
2800
2801         for (loop = 0; loop < (1 << d_hash_shift); loop++)
2802                 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2803 }
2804
2805 static void __init dcache_init(void)
2806 {
2807         int loop;
2808
2809         /* 
2810          * A constructor could be added for stable state like the lists,
2811          * but it is probably not worth it because of the cache nature
2812          * of the dcache. 
2813          */
2814         dentry_cache = KMEM_CACHE(dentry,
2815                 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
2816         
2817         register_shrinker(&dcache_shrinker);
2818
2819         /* Hash may have been set up in dcache_init_early */
2820         if (!hashdist)
2821                 return;
2822
2823         dentry_hashtable =
2824                 alloc_large_system_hash("Dentry cache",
2825                                         sizeof(struct hlist_head),
2826                                         dhash_entries,
2827                                         13,
2828                                         0,
2829                                         &d_hash_shift,
2830                                         &d_hash_mask,
2831                                         0);
2832
2833         for (loop = 0; loop < (1 << d_hash_shift); loop++)
2834                 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2835 }
2836
2837 /* SLAB cache for __getname() consumers */
2838 struct kmem_cache *names_cachep __read_mostly;
2839 EXPORT_SYMBOL(names_cachep);
2840
2841 EXPORT_SYMBOL(d_genocide);
2842
2843 void __init vfs_caches_init_early(void)
2844 {
2845         dcache_init_early();
2846         inode_init_early();
2847 }
2848
2849 void __init vfs_caches_init(unsigned long mempages)
2850 {
2851         unsigned long reserve;
2852
2853         /* Base hash sizes on available memory, with a reserve equal to
2854            150% of current kernel size */
2855
2856         reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2857         mempages -= reserve;
2858
2859         names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2860                         SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
2861
2862         dcache_init();
2863         inode_init();
2864         files_init(mempages);
2865         mnt_init();
2866         bdev_cache_init();
2867         chrdev_init();
2868 }