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