fedora core 6 1.2949 + vserver 2.2.0
[linux-2.6.git] / fs / dcache.c
1 /*
2  * fs/dcache.c
3  *
4  * Complete reimplementation
5  * (C) 1997 Thomas Schoebel-Theuer,
6  * with heavy changes by Linus Torvalds
7  */
8
9 /*
10  * Notes on the allocation strategy:
11  *
12  * The dcache is a master of the icache - whenever a dcache entry
13  * exists, the inode will always exist. "iput()" is done either when
14  * the dcache entry is deleted or garbage collected.
15  */
16
17 #include <linux/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/smp_lock.h>
25 #include <linux/hash.h>
26 #include <linux/cache.h>
27 #include <linux/module.h>
28 #include <linux/mount.h>
29 #include <linux/file.h>
30 #include <asm/uaccess.h>
31 #include <linux/security.h>
32 #include <linux/seqlock.h>
33 #include <linux/swap.h>
34 #include <linux/bootmem.h>
35 #include <linux/vs_limit.h>
36 #include "internal.h"
37
38
39 int sysctl_vfs_cache_pressure __read_mostly = 100;
40 EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
41
42  __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
43 static __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
44
45 EXPORT_SYMBOL(dcache_lock);
46
47 static struct kmem_cache *dentry_cache __read_mostly;
48
49 #define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
50
51 /*
52  * This is the single most critical data structure when it comes
53  * to the dcache: the hashtable for lookups. Somebody should try
54  * to make this good - I've just made it work.
55  *
56  * This hash-function tries to avoid losing too many bits of hash
57  * information, yet avoid using a prime hash-size or similar.
58  */
59 #define D_HASHBITS     d_hash_shift
60 #define D_HASHMASK     d_hash_mask
61
62 static unsigned int d_hash_mask __read_mostly;
63 static unsigned int d_hash_shift __read_mostly;
64 static struct hlist_head *dentry_hashtable __read_mostly;
65 static LIST_HEAD(dentry_unused);
66
67 /* Statistics gathering. */
68 struct dentry_stat_t dentry_stat = {
69         .age_limit = 45,
70 };
71
72 static void __d_free(struct dentry *dentry)
73 {
74         if (dname_external(dentry))
75                 kfree(dentry->d_name.name);
76         kmem_cache_free(dentry_cache, dentry); 
77 }
78
79 static void d_callback(struct rcu_head *head)
80 {
81         struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
82         __d_free(dentry);
83 }
84
85 /*
86  * no dcache_lock, please.  The caller must decrement dentry_stat.nr_dentry
87  * inside dcache_lock.
88  */
89 static void d_free(struct dentry *dentry)
90 {
91         if (dentry->d_op && dentry->d_op->d_release)
92                 dentry->d_op->d_release(dentry);
93         /* if dentry was never inserted into hash, immediate free is OK */
94         if (dentry->d_hash.pprev == NULL)
95                 __d_free(dentry);
96         else
97                 call_rcu(&dentry->d_u.d_rcu, d_callback);
98 }
99
100 /*
101  * Release the dentry's inode, using the filesystem
102  * d_iput() operation if defined.
103  * Called with dcache_lock and per dentry lock held, drops both.
104  */
105 static void dentry_iput(struct dentry * dentry)
106 {
107         struct inode *inode = dentry->d_inode;
108         if (inode) {
109                 dentry->d_inode = NULL;
110                 list_del_init(&dentry->d_alias);
111                 spin_unlock(&dentry->d_lock);
112                 spin_unlock(&dcache_lock);
113                 if (!inode->i_nlink)
114                         fsnotify_inoderemove(inode);
115                 if (dentry->d_op && dentry->d_op->d_iput)
116                         dentry->d_op->d_iput(dentry, inode);
117                 else
118                         iput(inode);
119         } else {
120                 spin_unlock(&dentry->d_lock);
121                 spin_unlock(&dcache_lock);
122         }
123 }
124
125 /* 
126  * This is dput
127  *
128  * This is complicated by the fact that we do not want to put
129  * dentries that are no longer on any hash chain on the unused
130  * list: we'd much rather just get rid of them immediately.
131  *
132  * However, that implies that we have to traverse the dentry
133  * tree upwards to the parents which might _also_ now be
134  * scheduled for deletion (it may have been only waiting for
135  * its last child to go away).
136  *
137  * This tail recursion is done by hand as we don't want to depend
138  * on the compiler to always get this right (gcc generally doesn't).
139  * Real recursion would eat up our stack space.
140  */
141
142 /*
143  * dput - release a dentry
144  * @dentry: dentry to release 
145  *
146  * Release a dentry. This will drop the usage count and if appropriate
147  * call the dentry unlink method as well as removing it from the queues and
148  * releasing its resources. If the parent dentries were scheduled for release
149  * they too may now get deleted.
150  *
151  * no dcache lock, please.
152  */
153
154 void dput(struct dentry *dentry)
155 {
156         if (!dentry)
157                 return;
158
159         vx_dentry_dec(dentry);
160 repeat:
161         if (atomic_read(&dentry->d_count) == 1)
162                 might_sleep();
163         if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
164                 return;
165
166         spin_lock(&dentry->d_lock);
167         if (atomic_read(&dentry->d_count)) {
168                 spin_unlock(&dentry->d_lock);
169                 spin_unlock(&dcache_lock);
170                 return;
171         }
172
173         vx_dentry_dec(dentry);
174
175         /*
176          * AV: ->d_delete() is _NOT_ allowed to block now.
177          */
178         if (dentry->d_op && dentry->d_op->d_delete) {
179                 if (dentry->d_op->d_delete(dentry))
180                         goto unhash_it;
181         }
182         /* Unreachable? Get rid of it */
183         if (d_unhashed(dentry))
184                 goto kill_it;
185         if (list_empty(&dentry->d_lru)) {
186                 dentry->d_flags |= DCACHE_REFERENCED;
187                 list_add(&dentry->d_lru, &dentry_unused);
188                 dentry_stat.nr_unused++;
189         }
190         spin_unlock(&dentry->d_lock);
191         spin_unlock(&dcache_lock);
192         return;
193
194 unhash_it:
195         __d_drop(dentry);
196
197 kill_it: {
198                 struct dentry *parent;
199
200                 /* If dentry was on d_lru list
201                  * delete it from there
202                  */
203                 if (!list_empty(&dentry->d_lru)) {
204                         list_del(&dentry->d_lru);
205                         dentry_stat.nr_unused--;
206                 }
207                 list_del(&dentry->d_u.d_child);
208                 dentry_stat.nr_dentry--;        /* For d_free, below */
209                 /*drops the locks, at that point nobody can reach this dentry */
210                 dentry_iput(dentry);
211                 parent = dentry->d_parent;
212                 d_free(dentry);
213                 if (dentry == parent)
214                         return;
215                 dentry = parent;
216                 goto repeat;
217         }
218 }
219
220 /**
221  * d_invalidate - invalidate a dentry
222  * @dentry: dentry to invalidate
223  *
224  * Try to invalidate the dentry if it turns out to be
225  * possible. If there are other dentries that can be
226  * reached through this one we can't delete it and we
227  * return -EBUSY. On success we return 0.
228  *
229  * no dcache lock.
230  */
231  
232 int d_invalidate(struct dentry * dentry)
233 {
234         /*
235          * If it's already been dropped, return OK.
236          */
237         spin_lock(&dcache_lock);
238         if (d_unhashed(dentry)) {
239                 spin_unlock(&dcache_lock);
240                 return 0;
241         }
242         /*
243          * Check whether to do a partial shrink_dcache
244          * to get rid of unused child entries.
245          */
246         if (!list_empty(&dentry->d_subdirs)) {
247                 spin_unlock(&dcache_lock);
248                 shrink_dcache_parent(dentry);
249                 spin_lock(&dcache_lock);
250         }
251
252         /*
253          * Somebody else still using it?
254          *
255          * If it's a directory, we can't drop it
256          * for fear of somebody re-populating it
257          * with children (even though dropping it
258          * would make it unreachable from the root,
259          * we might still populate it if it was a
260          * working directory or similar).
261          */
262         spin_lock(&dentry->d_lock);
263         if (atomic_read(&dentry->d_count) > 1) {
264                 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
265                         spin_unlock(&dentry->d_lock);
266                         spin_unlock(&dcache_lock);
267                         return -EBUSY;
268                 }
269         }
270
271         __d_drop(dentry);
272         spin_unlock(&dentry->d_lock);
273         spin_unlock(&dcache_lock);
274         return 0;
275 }
276
277 /* This should be called _only_ with dcache_lock held */
278
279 static inline struct dentry * __dget_locked(struct dentry *dentry)
280 {
281         atomic_inc(&dentry->d_count);
282         if (!list_empty(&dentry->d_lru)) {
283                 dentry_stat.nr_unused--;
284                 list_del_init(&dentry->d_lru);
285                 vx_dentry_inc(dentry);
286         }
287         return dentry;
288 }
289
290 struct dentry * dget_locked(struct dentry *dentry)
291 {
292         return __dget_locked(dentry);
293 }
294
295 /**
296  * d_find_alias - grab a hashed alias of inode
297  * @inode: inode in question
298  * @want_discon:  flag, used by d_splice_alias, to request
299  *          that only a DISCONNECTED alias be returned.
300  *
301  * If inode has a hashed alias, or is a directory and has any alias,
302  * acquire the reference to alias and return it. Otherwise return NULL.
303  * Notice that if inode is a directory there can be only one alias and
304  * it can be unhashed only if it has no children, or if it is the root
305  * of a filesystem.
306  *
307  * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
308  * any other hashed alias over that one unless @want_discon is set,
309  * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
310  */
311
312 static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
313 {
314         struct list_head *head, *next, *tmp;
315         struct dentry *alias, *discon_alias=NULL;
316
317         head = &inode->i_dentry;
318         next = inode->i_dentry.next;
319         while (next != head) {
320                 tmp = next;
321                 next = tmp->next;
322                 prefetch(next);
323                 alias = list_entry(tmp, struct dentry, d_alias);
324                 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
325                         if (IS_ROOT(alias) &&
326                             (alias->d_flags & DCACHE_DISCONNECTED))
327                                 discon_alias = alias;
328                         else if (!want_discon) {
329                                 __dget_locked(alias);
330                                 return alias;
331                         }
332                 }
333         }
334         if (discon_alias)
335                 __dget_locked(discon_alias);
336         return discon_alias;
337 }
338
339 struct dentry * d_find_alias(struct inode *inode)
340 {
341         struct dentry *de = NULL;
342
343         if (!list_empty(&inode->i_dentry)) {
344                 spin_lock(&dcache_lock);
345                 de = __d_find_alias(inode, 0);
346                 spin_unlock(&dcache_lock);
347         }
348         return de;
349 }
350
351 /*
352  *      Try to kill dentries associated with this inode.
353  * WARNING: you must own a reference to inode.
354  */
355 void d_prune_aliases(struct inode *inode)
356 {
357         struct dentry *dentry;
358 restart:
359         spin_lock(&dcache_lock);
360         list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
361                 spin_lock(&dentry->d_lock);
362                 if (!atomic_read(&dentry->d_count)) {
363                         __dget_locked(dentry);
364                         __d_drop(dentry);
365                         spin_unlock(&dentry->d_lock);
366                         spin_unlock(&dcache_lock);
367                         dput(dentry);
368                         goto restart;
369                 }
370                 spin_unlock(&dentry->d_lock);
371         }
372         spin_unlock(&dcache_lock);
373 }
374
375 /*
376  * Throw away a dentry - free the inode, dput the parent.  This requires that
377  * the LRU list has already been removed.
378  *
379  * Called with dcache_lock, drops it and then regains.
380  * Called with dentry->d_lock held, drops it.
381  */
382 static void prune_one_dentry(struct dentry * dentry)
383 {
384         struct dentry * parent;
385
386         __d_drop(dentry);
387         list_del(&dentry->d_u.d_child);
388         dentry_stat.nr_dentry--;        /* For d_free, below */
389         dentry_iput(dentry);
390         parent = dentry->d_parent;
391         d_free(dentry);
392         if (parent != dentry)
393                 dput(parent);
394         spin_lock(&dcache_lock);
395 }
396
397 /**
398  * prune_dcache - shrink the dcache
399  * @count: number of entries to try and free
400  * @sb: if given, ignore dentries for other superblocks
401  *         which are being unmounted.
402  *
403  * Shrink the dcache. This is done when we need
404  * more memory, or simply when we need to unmount
405  * something (at which point we need to unuse
406  * all dentries).
407  *
408  * This function may fail to free any resources if
409  * all the dentries are in use.
410  */
411  
412 static void prune_dcache(int count, struct super_block *sb)
413 {
414         spin_lock(&dcache_lock);
415         for (; count ; count--) {
416                 struct dentry *dentry;
417                 struct list_head *tmp;
418                 struct rw_semaphore *s_umount;
419
420                 cond_resched_lock(&dcache_lock);
421
422                 tmp = dentry_unused.prev;
423                 if (sb) {
424                         /* Try to find a dentry for this sb, but don't try
425                          * too hard, if they aren't near the tail they will
426                          * be moved down again soon
427                          */
428                         int skip = count;
429                         while (skip && tmp != &dentry_unused &&
430                             list_entry(tmp, struct dentry, d_lru)->d_sb != sb) {
431                                 skip--;
432                                 tmp = tmp->prev;
433                         }
434                 }
435                 if (tmp == &dentry_unused)
436                         break;
437                 list_del_init(tmp);
438                 prefetch(dentry_unused.prev);
439                 dentry_stat.nr_unused--;
440                 dentry = list_entry(tmp, struct dentry, d_lru);
441
442                 spin_lock(&dentry->d_lock);
443                 /*
444                  * We found an inuse dentry which was not removed from
445                  * dentry_unused because of laziness during lookup.  Do not free
446                  * it - just keep it off the dentry_unused list.
447                  */
448                 if (atomic_read(&dentry->d_count)) {
449                         spin_unlock(&dentry->d_lock);
450                         continue;
451                 }
452                 /* If the dentry was recently referenced, don't free it. */
453                 if (dentry->d_flags & DCACHE_REFERENCED) {
454                         dentry->d_flags &= ~DCACHE_REFERENCED;
455                         list_add(&dentry->d_lru, &dentry_unused);
456                         dentry_stat.nr_unused++;
457                         spin_unlock(&dentry->d_lock);
458                         continue;
459                 }
460                 /*
461                  * If the dentry is not DCACHED_REFERENCED, it is time
462                  * to remove it from the dcache, provided the super block is
463                  * NULL (which means we are trying to reclaim memory)
464                  * or this dentry belongs to the same super block that
465                  * we want to shrink.
466                  */
467                 /*
468                  * If this dentry is for "my" filesystem, then I can prune it
469                  * without taking the s_umount lock (I already hold it).
470                  */
471                 if (sb && dentry->d_sb == sb) {
472                         prune_one_dentry(dentry);
473                         continue;
474                 }
475                 /*
476                  * ...otherwise we need to be sure this filesystem isn't being
477                  * unmounted, otherwise we could race with
478                  * generic_shutdown_super(), and end up holding a reference to
479                  * an inode while the filesystem is unmounted.
480                  * So we try to get s_umount, and make sure s_root isn't NULL.
481                  * (Take a local copy of s_umount to avoid a use-after-free of
482                  * `dentry').
483                  */
484                 s_umount = &dentry->d_sb->s_umount;
485                 if (down_read_trylock(s_umount)) {
486                         if (dentry->d_sb->s_root != NULL) {
487                                 prune_one_dentry(dentry);
488                                 up_read(s_umount);
489                                 continue;
490                         }
491                         up_read(s_umount);
492                 }
493                 spin_unlock(&dentry->d_lock);
494                 /*
495                  * Insert dentry at the head of the list as inserting at the
496                  * tail leads to a cycle.
497                  */
498                 list_add(&dentry->d_lru, &dentry_unused);
499                 dentry_stat.nr_unused++;
500         }
501         spin_unlock(&dcache_lock);
502 }
503
504 /*
505  * Shrink the dcache for the specified super block.
506  * This allows us to unmount a device without disturbing
507  * the dcache for the other devices.
508  *
509  * This implementation makes just two traversals of the
510  * unused list.  On the first pass we move the selected
511  * dentries to the most recent end, and on the second
512  * pass we free them.  The second pass must restart after
513  * each dput(), but since the target dentries are all at
514  * the end, it's really just a single traversal.
515  */
516
517 /**
518  * shrink_dcache_sb - shrink dcache for a superblock
519  * @sb: superblock
520  *
521  * Shrink the dcache for the specified super block. This
522  * is used to free the dcache before unmounting a file
523  * system
524  */
525
526 void shrink_dcache_sb(struct super_block * sb)
527 {
528         struct list_head *tmp, *next;
529         struct dentry *dentry;
530
531         /*
532          * Pass one ... move the dentries for the specified
533          * superblock to the most recent end of the unused list.
534          */
535         spin_lock(&dcache_lock);
536         list_for_each_safe(tmp, next, &dentry_unused) {
537                 dentry = list_entry(tmp, struct dentry, d_lru);
538                 if (dentry->d_sb != sb)
539                         continue;
540                 list_move(tmp, &dentry_unused);
541         }
542
543         /*
544          * Pass two ... free the dentries for this superblock.
545          */
546 repeat:
547         list_for_each_safe(tmp, next, &dentry_unused) {
548                 dentry = list_entry(tmp, struct dentry, d_lru);
549                 if (dentry->d_sb != sb)
550                         continue;
551                 dentry_stat.nr_unused--;
552                 list_del_init(tmp);
553                 spin_lock(&dentry->d_lock);
554                 if (atomic_read(&dentry->d_count)) {
555                         spin_unlock(&dentry->d_lock);
556                         continue;
557                 }
558                 prune_one_dentry(dentry);
559                 cond_resched_lock(&dcache_lock);
560                 goto repeat;
561         }
562         spin_unlock(&dcache_lock);
563 }
564
565 /*
566  * destroy a single subtree of dentries for unmount
567  * - see the comments on shrink_dcache_for_umount() for a description of the
568  *   locking
569  */
570 static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
571 {
572         struct dentry *parent;
573         unsigned detached = 0;
574
575         BUG_ON(!IS_ROOT(dentry));
576
577         /* detach this root from the system */
578         spin_lock(&dcache_lock);
579         if (!list_empty(&dentry->d_lru)) {
580                 dentry_stat.nr_unused--;
581                 list_del_init(&dentry->d_lru);
582         }
583         __d_drop(dentry);
584         spin_unlock(&dcache_lock);
585
586         for (;;) {
587                 /* descend to the first leaf in the current subtree */
588                 while (!list_empty(&dentry->d_subdirs)) {
589                         struct dentry *loop;
590
591                         /* this is a branch with children - detach all of them
592                          * from the system in one go */
593                         spin_lock(&dcache_lock);
594                         list_for_each_entry(loop, &dentry->d_subdirs,
595                                             d_u.d_child) {
596                                 if (!list_empty(&loop->d_lru)) {
597                                         dentry_stat.nr_unused--;
598                                         list_del_init(&loop->d_lru);
599                                 }
600
601                                 __d_drop(loop);
602                                 cond_resched_lock(&dcache_lock);
603                         }
604                         spin_unlock(&dcache_lock);
605
606                         /* move to the first child */
607                         dentry = list_entry(dentry->d_subdirs.next,
608                                             struct dentry, d_u.d_child);
609                 }
610
611                 /* consume the dentries from this leaf up through its parents
612                  * until we find one with children or run out altogether */
613                 do {
614                         struct inode *inode;
615
616                         if (atomic_read(&dentry->d_count) != 0) {
617                                 printk(KERN_ERR
618                                        "BUG: Dentry %p{i=%lx,n=%s}"
619                                        " still in use (%d)"
620                                        " [unmount of %s %s]\n",
621                                        dentry,
622                                        dentry->d_inode ?
623                                        dentry->d_inode->i_ino : 0UL,
624                                        dentry->d_name.name,
625                                        atomic_read(&dentry->d_count),
626                                        dentry->d_sb->s_type->name,
627                                        dentry->d_sb->s_id);
628                                 BUG();
629                         }
630
631                         parent = dentry->d_parent;
632                         if (parent == dentry)
633                                 parent = NULL;
634                         else
635                                 atomic_dec(&parent->d_count);
636
637                         list_del(&dentry->d_u.d_child);
638                         detached++;
639
640                         inode = dentry->d_inode;
641                         if (inode) {
642                                 dentry->d_inode = NULL;
643                                 list_del_init(&dentry->d_alias);
644                                 if (dentry->d_op && dentry->d_op->d_iput)
645                                         dentry->d_op->d_iput(dentry, inode);
646                                 else
647                                         iput(inode);
648                         }
649
650                         d_free(dentry);
651
652                         /* finished when we fall off the top of the tree,
653                          * otherwise we ascend to the parent and move to the
654                          * next sibling if there is one */
655                         if (!parent)
656                                 goto out;
657
658                         dentry = parent;
659
660                 } while (list_empty(&dentry->d_subdirs));
661
662                 dentry = list_entry(dentry->d_subdirs.next,
663                                     struct dentry, d_u.d_child);
664         }
665 out:
666         /* several dentries were freed, need to correct nr_dentry */
667         spin_lock(&dcache_lock);
668         dentry_stat.nr_dentry -= detached;
669         spin_unlock(&dcache_lock);
670 }
671
672 /*
673  * destroy the dentries attached to a superblock on unmounting
674  * - we don't need to use dentry->d_lock, and only need dcache_lock when
675  *   removing the dentry from the system lists and hashes because:
676  *   - the superblock is detached from all mountings and open files, so the
677  *     dentry trees will not be rearranged by the VFS
678  *   - s_umount is write-locked, so the memory pressure shrinker will ignore
679  *     any dentries belonging to this superblock that it comes across
680  *   - the filesystem itself is no longer permitted to rearrange the dentries
681  *     in this superblock
682  */
683 void shrink_dcache_for_umount(struct super_block *sb)
684 {
685         struct dentry *dentry;
686
687         if (down_read_trylock(&sb->s_umount))
688                 BUG();
689
690         dentry = sb->s_root;
691         sb->s_root = NULL;
692         atomic_dec(&dentry->d_count);
693         shrink_dcache_for_umount_subtree(dentry);
694
695         while (!hlist_empty(&sb->s_anon)) {
696                 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
697                 shrink_dcache_for_umount_subtree(dentry);
698         }
699 }
700
701 /*
702  * Search for at least 1 mount point in the dentry's subdirs.
703  * We descend to the next level whenever the d_subdirs
704  * list is non-empty and continue searching.
705  */
706  
707 /**
708  * have_submounts - check for mounts over a dentry
709  * @parent: dentry to check.
710  *
711  * Return true if the parent or its subdirectories contain
712  * a mount point
713  */
714  
715 int have_submounts(struct dentry *parent)
716 {
717         struct dentry *this_parent = parent;
718         struct list_head *next;
719
720         spin_lock(&dcache_lock);
721         if (d_mountpoint(parent))
722                 goto positive;
723 repeat:
724         next = this_parent->d_subdirs.next;
725 resume:
726         while (next != &this_parent->d_subdirs) {
727                 struct list_head *tmp = next;
728                 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
729                 next = tmp->next;
730                 /* Have we found a mount point ? */
731                 if (d_mountpoint(dentry))
732                         goto positive;
733                 if (!list_empty(&dentry->d_subdirs)) {
734                         this_parent = dentry;
735                         goto repeat;
736                 }
737         }
738         /*
739          * All done at this level ... ascend and resume the search.
740          */
741         if (this_parent != parent) {
742                 next = this_parent->d_u.d_child.next;
743                 this_parent = this_parent->d_parent;
744                 goto resume;
745         }
746         spin_unlock(&dcache_lock);
747         return 0; /* No mount points found in tree */
748 positive:
749         spin_unlock(&dcache_lock);
750         return 1;
751 }
752
753 /*
754  * Search the dentry child list for the specified parent,
755  * and move any unused dentries to the end of the unused
756  * list for prune_dcache(). We descend to the next level
757  * whenever the d_subdirs list is non-empty and continue
758  * searching.
759  *
760  * It returns zero iff there are no unused children,
761  * otherwise  it returns the number of children moved to
762  * the end of the unused list. This may not be the total
763  * number of unused children, because select_parent can
764  * drop the lock and return early due to latency
765  * constraints.
766  */
767 static int select_parent(struct dentry * parent)
768 {
769         struct dentry *this_parent = parent;
770         struct list_head *next;
771         int found = 0;
772
773         spin_lock(&dcache_lock);
774 repeat:
775         next = this_parent->d_subdirs.next;
776 resume:
777         while (next != &this_parent->d_subdirs) {
778                 struct list_head *tmp = next;
779                 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
780                 next = tmp->next;
781
782                 if (!list_empty(&dentry->d_lru)) {
783                         dentry_stat.nr_unused--;
784                         list_del_init(&dentry->d_lru);
785                 }
786                 /* 
787                  * move only zero ref count dentries to the end 
788                  * of the unused list for prune_dcache
789                  */
790                 if (!atomic_read(&dentry->d_count)) {
791                         list_add_tail(&dentry->d_lru, &dentry_unused);
792                         dentry_stat.nr_unused++;
793                         found++;
794                 }
795
796                 /*
797                  * We can return to the caller if we have found some (this
798                  * ensures forward progress). We'll be coming back to find
799                  * the rest.
800                  */
801                 if (found && need_resched())
802                         goto out;
803
804                 /*
805                  * Descend a level if the d_subdirs list is non-empty.
806                  */
807                 if (!list_empty(&dentry->d_subdirs)) {
808                         this_parent = dentry;
809                         goto repeat;
810                 }
811         }
812         /*
813          * All done at this level ... ascend and resume the search.
814          */
815         if (this_parent != parent) {
816                 next = this_parent->d_u.d_child.next;
817                 this_parent = this_parent->d_parent;
818                 goto resume;
819         }
820 out:
821         spin_unlock(&dcache_lock);
822         return found;
823 }
824
825 /**
826  * shrink_dcache_parent - prune dcache
827  * @parent: parent of entries to prune
828  *
829  * Prune the dcache to remove unused children of the parent dentry.
830  */
831  
832 void shrink_dcache_parent(struct dentry * parent)
833 {
834         int found;
835
836         while ((found = select_parent(parent)) != 0)
837                 prune_dcache(found, parent->d_sb);
838 }
839
840 /*
841  * Scan `nr' dentries and return the number which remain.
842  *
843  * We need to avoid reentering the filesystem if the caller is performing a
844  * GFP_NOFS allocation attempt.  One example deadlock is:
845  *
846  * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
847  * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
848  * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
849  *
850  * In this case we return -1 to tell the caller that we baled.
851  */
852 static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
853 {
854         if (nr) {
855                 if (!(gfp_mask & __GFP_FS))
856                         return -1;
857                 prune_dcache(nr, NULL);
858         }
859         return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
860 }
861
862 /**
863  * d_alloc      -       allocate a dcache entry
864  * @parent: parent of entry to allocate
865  * @name: qstr of the name
866  *
867  * Allocates a dentry. It returns %NULL if there is insufficient memory
868  * available. On a success the dentry is returned. The name passed in is
869  * copied and the copy passed in may be reused after this call.
870  */
871  
872 struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
873 {
874         struct dentry *dentry;
875         char *dname;
876
877         if (!vx_dentry_avail(1))
878                 return NULL;
879
880         dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL); 
881         if (!dentry)
882                 return NULL;
883
884         if (name->len > DNAME_INLINE_LEN-1) {
885                 dname = kmalloc(name->len + 1, GFP_KERNEL);
886                 if (!dname) {
887                         kmem_cache_free(dentry_cache, dentry); 
888                         return NULL;
889                 }
890         } else  {
891                 dname = dentry->d_iname;
892         }       
893         dentry->d_name.name = dname;
894
895         dentry->d_name.len = name->len;
896         dentry->d_name.hash = name->hash;
897         memcpy(dname, name->name, name->len);
898         dname[name->len] = 0;
899
900         atomic_set(&dentry->d_count, 1);
901         dentry->d_flags = DCACHE_UNHASHED;
902         spin_lock_init(&dentry->d_lock);
903         dentry->d_inode = NULL;
904         dentry->d_parent = NULL;
905         dentry->d_sb = NULL;
906         dentry->d_op = NULL;
907         dentry->d_fsdata = NULL;
908         dentry->d_mounted = 0;
909 #ifdef CONFIG_PROFILING
910         dentry->d_cookie = NULL;
911 #endif
912         INIT_HLIST_NODE(&dentry->d_hash);
913         INIT_LIST_HEAD(&dentry->d_lru);
914         INIT_LIST_HEAD(&dentry->d_subdirs);
915         INIT_LIST_HEAD(&dentry->d_alias);
916
917         if (parent) {
918                 dentry->d_parent = dget(parent);
919                 dentry->d_sb = parent->d_sb;
920         } else {
921                 INIT_LIST_HEAD(&dentry->d_u.d_child);
922         }
923
924         spin_lock(&dcache_lock);
925         if (parent)
926                 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
927         dentry_stat.nr_dentry++;
928         vx_dentry_inc(dentry);
929         spin_unlock(&dcache_lock);
930
931         return dentry;
932 }
933
934 struct dentry *d_alloc_name(struct dentry *parent, const char *name)
935 {
936         struct qstr q;
937
938         q.name = name;
939         q.len = strlen(name);
940         q.hash = full_name_hash(q.name, q.len);
941         return d_alloc(parent, &q);
942 }
943
944 /**
945  * d_instantiate - fill in inode information for a dentry
946  * @entry: dentry to complete
947  * @inode: inode to attach to this dentry
948  *
949  * Fill in inode information in the entry.
950  *
951  * This turns negative dentries into productive full members
952  * of society.
953  *
954  * NOTE! This assumes that the inode count has been incremented
955  * (or otherwise set) by the caller to indicate that it is now
956  * in use by the dcache.
957  */
958  
959 void d_instantiate(struct dentry *entry, struct inode * inode)
960 {
961         BUG_ON(!list_empty(&entry->d_alias));
962         spin_lock(&dcache_lock);
963         if (inode)
964                 list_add(&entry->d_alias, &inode->i_dentry);
965         entry->d_inode = inode;
966         fsnotify_d_instantiate(entry, inode);
967         spin_unlock(&dcache_lock);
968         security_d_instantiate(entry, inode);
969 }
970
971 /**
972  * d_instantiate_unique - instantiate a non-aliased dentry
973  * @entry: dentry to instantiate
974  * @inode: inode to attach to this dentry
975  *
976  * Fill in inode information in the entry. On success, it returns NULL.
977  * If an unhashed alias of "entry" already exists, then we return the
978  * aliased dentry instead and drop one reference to inode.
979  *
980  * Note that in order to avoid conflicts with rename() etc, the caller
981  * had better be holding the parent directory semaphore.
982  *
983  * This also assumes that the inode count has been incremented
984  * (or otherwise set) by the caller to indicate that it is now
985  * in use by the dcache.
986  */
987 static struct dentry *__d_instantiate_unique(struct dentry *entry,
988                                              struct inode *inode)
989 {
990         struct dentry *alias;
991         int len = entry->d_name.len;
992         const char *name = entry->d_name.name;
993         unsigned int hash = entry->d_name.hash;
994
995         if (!inode) {
996                 entry->d_inode = NULL;
997                 return NULL;
998         }
999
1000         list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1001                 struct qstr *qstr = &alias->d_name;
1002
1003                 if (qstr->hash != hash)
1004                         continue;
1005                 if (alias->d_parent != entry->d_parent)
1006                         continue;
1007                 if (qstr->len != len)
1008                         continue;
1009                 if (memcmp(qstr->name, name, len))
1010                         continue;
1011                 dget_locked(alias);
1012                 return alias;
1013         }
1014
1015         list_add(&entry->d_alias, &inode->i_dentry);
1016         entry->d_inode = inode;
1017         fsnotify_d_instantiate(entry, inode);
1018         return NULL;
1019 }
1020
1021 struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1022 {
1023         struct dentry *result;
1024
1025         BUG_ON(!list_empty(&entry->d_alias));
1026
1027         spin_lock(&dcache_lock);
1028         result = __d_instantiate_unique(entry, inode);
1029         spin_unlock(&dcache_lock);
1030
1031         if (!result) {
1032                 security_d_instantiate(entry, inode);
1033                 return NULL;
1034         }
1035
1036         BUG_ON(!d_unhashed(result));
1037         iput(inode);
1038         return result;
1039 }
1040
1041 EXPORT_SYMBOL(d_instantiate_unique);
1042
1043 /**
1044  * d_alloc_root - allocate root dentry
1045  * @root_inode: inode to allocate the root for
1046  *
1047  * Allocate a root ("/") dentry for the inode given. The inode is
1048  * instantiated and returned. %NULL is returned if there is insufficient
1049  * memory or the inode passed is %NULL.
1050  */
1051  
1052 struct dentry * d_alloc_root(struct inode * root_inode)
1053 {
1054         struct dentry *res = NULL;
1055
1056         if (root_inode) {
1057                 static const struct qstr name = { .name = "/", .len = 1 };
1058
1059                 res = d_alloc(NULL, &name);
1060                 if (res) {
1061                         res->d_sb = root_inode->i_sb;
1062                         res->d_parent = res;
1063                         d_instantiate(res, root_inode);
1064                 }
1065         }
1066         return res;
1067 }
1068
1069 static inline struct hlist_head *d_hash(struct dentry *parent,
1070                                         unsigned long hash)
1071 {
1072         hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1073         hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1074         return dentry_hashtable + (hash & D_HASHMASK);
1075 }
1076
1077 /**
1078  * d_alloc_anon - allocate an anonymous dentry
1079  * @inode: inode to allocate the dentry for
1080  *
1081  * This is similar to d_alloc_root.  It is used by filesystems when
1082  * creating a dentry for a given inode, often in the process of 
1083  * mapping a filehandle to a dentry.  The returned dentry may be
1084  * anonymous, or may have a full name (if the inode was already
1085  * in the cache).  The file system may need to make further
1086  * efforts to connect this dentry into the dcache properly.
1087  *
1088  * When called on a directory inode, we must ensure that
1089  * the inode only ever has one dentry.  If a dentry is
1090  * found, that is returned instead of allocating a new one.
1091  *
1092  * On successful return, the reference to the inode has been transferred
1093  * to the dentry.  If %NULL is returned (indicating kmalloc failure),
1094  * the reference on the inode has not been released.
1095  */
1096
1097 struct dentry * d_alloc_anon(struct inode *inode)
1098 {
1099         static const struct qstr anonstring = { .name = "" };
1100         struct dentry *tmp;
1101         struct dentry *res;
1102
1103         if ((res = d_find_alias(inode))) {
1104                 iput(inode);
1105                 return res;
1106         }
1107
1108         tmp = d_alloc(NULL, &anonstring);
1109         if (!tmp)
1110                 return NULL;
1111
1112         tmp->d_parent = tmp; /* make sure dput doesn't croak */
1113         
1114         spin_lock(&dcache_lock);
1115         res = __d_find_alias(inode, 0);
1116         if (!res) {
1117                 /* attach a disconnected dentry */
1118                 res = tmp;
1119                 tmp = NULL;
1120                 spin_lock(&res->d_lock);
1121                 res->d_sb = inode->i_sb;
1122                 res->d_parent = res;
1123                 res->d_inode = inode;
1124                 res->d_flags |= DCACHE_DISCONNECTED;
1125                 res->d_flags &= ~DCACHE_UNHASHED;
1126                 list_add(&res->d_alias, &inode->i_dentry);
1127                 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
1128                 spin_unlock(&res->d_lock);
1129
1130                 inode = NULL; /* don't drop reference */
1131         }
1132         spin_unlock(&dcache_lock);
1133
1134         if (inode)
1135                 iput(inode);
1136         if (tmp)
1137                 dput(tmp);
1138         return res;
1139 }
1140
1141
1142 /**
1143  * d_splice_alias - splice a disconnected dentry into the tree if one exists
1144  * @inode:  the inode which may have a disconnected dentry
1145  * @dentry: a negative dentry which we want to point to the inode.
1146  *
1147  * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1148  * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1149  * and return it, else simply d_add the inode to the dentry and return NULL.
1150  *
1151  * This is needed in the lookup routine of any filesystem that is exportable
1152  * (via knfsd) so that we can build dcache paths to directories effectively.
1153  *
1154  * If a dentry was found and moved, then it is returned.  Otherwise NULL
1155  * is returned.  This matches the expected return value of ->lookup.
1156  *
1157  */
1158 struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1159 {
1160         struct dentry *new = NULL;
1161
1162         if (inode && S_ISDIR(inode->i_mode)) {
1163                 spin_lock(&dcache_lock);
1164                 new = __d_find_alias(inode, 1);
1165                 if (new) {
1166                         BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1167                         fsnotify_d_instantiate(new, inode);
1168                         spin_unlock(&dcache_lock);
1169                         security_d_instantiate(new, inode);
1170                         d_rehash(dentry);
1171                         d_move(new, dentry);
1172                         iput(inode);
1173                 } else {
1174                         /* d_instantiate takes dcache_lock, so we do it by hand */
1175                         list_add(&dentry->d_alias, &inode->i_dentry);
1176                         dentry->d_inode = inode;
1177                         fsnotify_d_instantiate(dentry, inode);
1178                         spin_unlock(&dcache_lock);
1179                         security_d_instantiate(dentry, inode);
1180                         d_rehash(dentry);
1181                 }
1182         } else
1183                 d_add(dentry, inode);
1184         return new;
1185 }
1186
1187
1188 /**
1189  * d_lookup - search for a dentry
1190  * @parent: parent dentry
1191  * @name: qstr of name we wish to find
1192  *
1193  * Searches the children of the parent dentry for the name in question. If
1194  * the dentry is found its reference count is incremented and the dentry
1195  * is returned. The caller must use d_put to free the entry when it has
1196  * finished using it. %NULL is returned on failure.
1197  *
1198  * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1199  * Memory barriers are used while updating and doing lockless traversal. 
1200  * To avoid races with d_move while rename is happening, d_lock is used.
1201  *
1202  * Overflows in memcmp(), while d_move, are avoided by keeping the length
1203  * and name pointer in one structure pointed by d_qstr.
1204  *
1205  * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1206  * lookup is going on.
1207  *
1208  * dentry_unused list is not updated even if lookup finds the required dentry
1209  * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1210  * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1211  * acquisition.
1212  *
1213  * d_lookup() is protected against the concurrent renames in some unrelated
1214  * directory using the seqlockt_t rename_lock.
1215  */
1216
1217 struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1218 {
1219         struct dentry * dentry = NULL;
1220         unsigned long seq;
1221
1222         do {
1223                 seq = read_seqbegin(&rename_lock);
1224                 dentry = __d_lookup(parent, name);
1225                 if (dentry)
1226                         break;
1227         } while (read_seqretry(&rename_lock, seq));
1228         return dentry;
1229 }
1230
1231 struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1232 {
1233         unsigned int len = name->len;
1234         unsigned int hash = name->hash;
1235         const unsigned char *str = name->name;
1236         struct hlist_head *head = d_hash(parent,hash);
1237         struct dentry *found = NULL;
1238         struct hlist_node *node;
1239         struct dentry *dentry;
1240
1241         rcu_read_lock();
1242         
1243         hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1244                 struct qstr *qstr;
1245
1246                 if (dentry->d_name.hash != hash)
1247                         continue;
1248                 if (dentry->d_parent != parent)
1249                         continue;
1250
1251                 spin_lock(&dentry->d_lock);
1252
1253                 /*
1254                  * Recheck the dentry after taking the lock - d_move may have
1255                  * changed things.  Don't bother checking the hash because we're
1256                  * about to compare the whole name anyway.
1257                  */
1258                 if (dentry->d_parent != parent)
1259                         goto next;
1260
1261                 /*
1262                  * It is safe to compare names since d_move() cannot
1263                  * change the qstr (protected by d_lock).
1264                  */
1265                 qstr = &dentry->d_name;
1266                 if (parent->d_op && parent->d_op->d_compare) {
1267                         if (parent->d_op->d_compare(parent, qstr, name))
1268                                 goto next;
1269                 } else {
1270                         if (qstr->len != len)
1271                                 goto next;
1272                         if (memcmp(qstr->name, str, len))
1273                                 goto next;
1274                 }
1275
1276                 if (!d_unhashed(dentry)) {
1277                         atomic_inc(&dentry->d_count);
1278                         vx_dentry_inc(dentry);
1279                         found = dentry;
1280                 }
1281                 spin_unlock(&dentry->d_lock);
1282                 break;
1283 next:
1284                 spin_unlock(&dentry->d_lock);
1285         }
1286         rcu_read_unlock();
1287
1288         return found;
1289 }
1290
1291 /**
1292  * d_hash_and_lookup - hash the qstr then search for a dentry
1293  * @dir: Directory to search in
1294  * @name: qstr of name we wish to find
1295  *
1296  * On hash failure or on lookup failure NULL is returned.
1297  */
1298 struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1299 {
1300         struct dentry *dentry = NULL;
1301
1302         /*
1303          * Check for a fs-specific hash function. Note that we must
1304          * calculate the standard hash first, as the d_op->d_hash()
1305          * routine may choose to leave the hash value unchanged.
1306          */
1307         name->hash = full_name_hash(name->name, name->len);
1308         if (dir->d_op && dir->d_op->d_hash) {
1309                 if (dir->d_op->d_hash(dir, name) < 0)
1310                         goto out;
1311         }
1312         dentry = d_lookup(dir, name);
1313 out:
1314         return dentry;
1315 }
1316
1317 /**
1318  * d_validate - verify dentry provided from insecure source
1319  * @dentry: The dentry alleged to be valid child of @dparent
1320  * @dparent: The parent dentry (known to be valid)
1321  * @hash: Hash of the dentry
1322  * @len: Length of the name
1323  *
1324  * An insecure source has sent us a dentry, here we verify it and dget() it.
1325  * This is used by ncpfs in its readdir implementation.
1326  * Zero is returned in the dentry is invalid.
1327  */
1328  
1329 int d_validate(struct dentry *dentry, struct dentry *dparent)
1330 {
1331         struct hlist_head *base;
1332         struct hlist_node *lhp;
1333
1334         /* Check whether the ptr might be valid at all.. */
1335         if (!kmem_ptr_validate(dentry_cache, dentry))
1336                 goto out;
1337
1338         if (dentry->d_parent != dparent)
1339                 goto out;
1340
1341         spin_lock(&dcache_lock);
1342         base = d_hash(dparent, dentry->d_name.hash);
1343         hlist_for_each(lhp,base) { 
1344                 /* hlist_for_each_entry_rcu() not required for d_hash list
1345                  * as it is parsed under dcache_lock
1346                  */
1347                 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1348                         __dget_locked(dentry);
1349                         spin_unlock(&dcache_lock);
1350                         return 1;
1351                 }
1352         }
1353         spin_unlock(&dcache_lock);
1354 out:
1355         return 0;
1356 }
1357
1358 /*
1359  * When a file is deleted, we have two options:
1360  * - turn this dentry into a negative dentry
1361  * - unhash this dentry and free it.
1362  *
1363  * Usually, we want to just turn this into
1364  * a negative dentry, but if anybody else is
1365  * currently using the dentry or the inode
1366  * we can't do that and we fall back on removing
1367  * it from the hash queues and waiting for
1368  * it to be deleted later when it has no users
1369  */
1370  
1371 /**
1372  * d_delete - delete a dentry
1373  * @dentry: The dentry to delete
1374  *
1375  * Turn the dentry into a negative dentry if possible, otherwise
1376  * remove it from the hash queues so it can be deleted later
1377  */
1378  
1379 void d_delete(struct dentry * dentry)
1380 {
1381         int isdir = 0;
1382         /*
1383          * Are we the only user?
1384          */
1385         spin_lock(&dcache_lock);
1386         spin_lock(&dentry->d_lock);
1387         isdir = S_ISDIR(dentry->d_inode->i_mode);
1388         if (atomic_read(&dentry->d_count) == 1) {
1389                 dentry_iput(dentry);
1390                 fsnotify_nameremove(dentry, isdir);
1391
1392                 /* remove this and other inotify debug checks after 2.6.18 */
1393                 dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
1394                 return;
1395         }
1396
1397         if (!d_unhashed(dentry))
1398                 __d_drop(dentry);
1399
1400         spin_unlock(&dentry->d_lock);
1401         spin_unlock(&dcache_lock);
1402
1403         fsnotify_nameremove(dentry, isdir);
1404 }
1405
1406 static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1407 {
1408
1409         entry->d_flags &= ~DCACHE_UNHASHED;
1410         hlist_add_head_rcu(&entry->d_hash, list);
1411 }
1412
1413 static void _d_rehash(struct dentry * entry)
1414 {
1415         __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1416 }
1417
1418 /**
1419  * d_rehash     - add an entry back to the hash
1420  * @entry: dentry to add to the hash
1421  *
1422  * Adds a dentry to the hash according to its name.
1423  */
1424  
1425 void d_rehash(struct dentry * entry)
1426 {
1427         spin_lock(&dcache_lock);
1428         spin_lock(&entry->d_lock);
1429         _d_rehash(entry);
1430         spin_unlock(&entry->d_lock);
1431         spin_unlock(&dcache_lock);
1432 }
1433
1434 #define do_switch(x,y) do { \
1435         __typeof__ (x) __tmp = x; \
1436         x = y; y = __tmp; } while (0)
1437
1438 /*
1439  * When switching names, the actual string doesn't strictly have to
1440  * be preserved in the target - because we're dropping the target
1441  * anyway. As such, we can just do a simple memcpy() to copy over
1442  * the new name before we switch.
1443  *
1444  * Note that we have to be a lot more careful about getting the hash
1445  * switched - we have to switch the hash value properly even if it
1446  * then no longer matches the actual (corrupted) string of the target.
1447  * The hash value has to match the hash queue that the dentry is on..
1448  */
1449 static void switch_names(struct dentry *dentry, struct dentry *target)
1450 {
1451         if (dname_external(target)) {
1452                 if (dname_external(dentry)) {
1453                         /*
1454                          * Both external: swap the pointers
1455                          */
1456                         do_switch(target->d_name.name, dentry->d_name.name);
1457                 } else {
1458                         /*
1459                          * dentry:internal, target:external.  Steal target's
1460                          * storage and make target internal.
1461                          */
1462                         dentry->d_name.name = target->d_name.name;
1463                         target->d_name.name = target->d_iname;
1464                 }
1465         } else {
1466                 if (dname_external(dentry)) {
1467                         /*
1468                          * dentry:external, target:internal.  Give dentry's
1469                          * storage to target and make dentry internal
1470                          */
1471                         memcpy(dentry->d_iname, target->d_name.name,
1472                                         target->d_name.len + 1);
1473                         target->d_name.name = dentry->d_name.name;
1474                         dentry->d_name.name = dentry->d_iname;
1475                 } else {
1476                         /*
1477                          * Both are internal.  Just copy target to dentry
1478                          */
1479                         memcpy(dentry->d_iname, target->d_name.name,
1480                                         target->d_name.len + 1);
1481                 }
1482         }
1483 }
1484
1485 /*
1486  * We cannibalize "target" when moving dentry on top of it,
1487  * because it's going to be thrown away anyway. We could be more
1488  * polite about it, though.
1489  *
1490  * This forceful removal will result in ugly /proc output if
1491  * somebody holds a file open that got deleted due to a rename.
1492  * We could be nicer about the deleted file, and let it show
1493  * up under the name it got deleted rather than the name that
1494  * deleted it.
1495  */
1496  
1497 /*
1498  * d_move_locked - move a dentry
1499  * @dentry: entry to move
1500  * @target: new dentry
1501  *
1502  * Update the dcache to reflect the move of a file name. Negative
1503  * dcache entries should not be moved in this way.
1504  */
1505 static void d_move_locked(struct dentry * dentry, struct dentry * target)
1506 {
1507         struct hlist_head *list;
1508
1509         if (!dentry->d_inode)
1510                 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1511
1512         write_seqlock(&rename_lock);
1513         /*
1514          * XXXX: do we really need to take target->d_lock?
1515          */
1516         if (target < dentry) {
1517                 spin_lock(&target->d_lock);
1518                 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1519         } else {
1520                 spin_lock(&dentry->d_lock);
1521                 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
1522         }
1523
1524         /* Move the dentry to the target hash queue, if on different bucket */
1525         if (dentry->d_flags & DCACHE_UNHASHED)
1526                 goto already_unhashed;
1527
1528         hlist_del_rcu(&dentry->d_hash);
1529
1530 already_unhashed:
1531         list = d_hash(target->d_parent, target->d_name.hash);
1532         __d_rehash(dentry, list);
1533
1534         /* Unhash the target: dput() will then get rid of it */
1535         __d_drop(target);
1536
1537         list_del(&dentry->d_u.d_child);
1538         list_del(&target->d_u.d_child);
1539
1540         /* Switch the names.. */
1541         switch_names(dentry, target);
1542         do_switch(dentry->d_name.len, target->d_name.len);
1543         do_switch(dentry->d_name.hash, target->d_name.hash);
1544
1545         /* ... and switch the parents */
1546         if (IS_ROOT(dentry)) {
1547                 dentry->d_parent = target->d_parent;
1548                 target->d_parent = target;
1549                 INIT_LIST_HEAD(&target->d_u.d_child);
1550         } else {
1551                 do_switch(dentry->d_parent, target->d_parent);
1552
1553                 /* And add them back to the (new) parent lists */
1554                 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1555         }
1556
1557         list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1558         spin_unlock(&target->d_lock);
1559         fsnotify_d_move(dentry);
1560         spin_unlock(&dentry->d_lock);
1561         write_sequnlock(&rename_lock);
1562 }
1563
1564 /**
1565  * d_move - move a dentry
1566  * @dentry: entry to move
1567  * @target: new dentry
1568  *
1569  * Update the dcache to reflect the move of a file name. Negative
1570  * dcache entries should not be moved in this way.
1571  */
1572
1573 void d_move(struct dentry * dentry, struct dentry * target)
1574 {
1575         spin_lock(&dcache_lock);
1576         d_move_locked(dentry, target);
1577         spin_unlock(&dcache_lock);
1578 }
1579
1580 /*
1581  * Helper that returns 1 if p1 is a parent of p2, else 0
1582  */
1583 static int d_isparent(struct dentry *p1, struct dentry *p2)
1584 {
1585         struct dentry *p;
1586
1587         for (p = p2; p->d_parent != p; p = p->d_parent) {
1588                 if (p->d_parent == p1)
1589                         return 1;
1590         }
1591         return 0;
1592 }
1593
1594 /*
1595  * This helper attempts to cope with remotely renamed directories
1596  *
1597  * It assumes that the caller is already holding
1598  * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1599  *
1600  * Note: If ever the locking in lock_rename() changes, then please
1601  * remember to update this too...
1602  *
1603  * On return, dcache_lock will have been unlocked.
1604  */
1605 static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1606 {
1607         struct mutex *m1 = NULL, *m2 = NULL;
1608         struct dentry *ret;
1609
1610         /* If alias and dentry share a parent, then no extra locks required */
1611         if (alias->d_parent == dentry->d_parent)
1612                 goto out_unalias;
1613
1614         /* Check for loops */
1615         ret = ERR_PTR(-ELOOP);
1616         if (d_isparent(alias, dentry))
1617                 goto out_err;
1618
1619         /* See lock_rename() */
1620         ret = ERR_PTR(-EBUSY);
1621         if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1622                 goto out_err;
1623         m1 = &dentry->d_sb->s_vfs_rename_mutex;
1624         if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1625                 goto out_err;
1626         m2 = &alias->d_parent->d_inode->i_mutex;
1627 out_unalias:
1628         d_move_locked(alias, dentry);
1629         ret = alias;
1630 out_err:
1631         spin_unlock(&dcache_lock);
1632         if (m2)
1633                 mutex_unlock(m2);
1634         if (m1)
1635                 mutex_unlock(m1);
1636         return ret;
1637 }
1638
1639 /*
1640  * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1641  * named dentry in place of the dentry to be replaced.
1642  */
1643 static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1644 {
1645         struct dentry *dparent, *aparent;
1646
1647         switch_names(dentry, anon);
1648         do_switch(dentry->d_name.len, anon->d_name.len);
1649         do_switch(dentry->d_name.hash, anon->d_name.hash);
1650
1651         dparent = dentry->d_parent;
1652         aparent = anon->d_parent;
1653
1654         dentry->d_parent = (aparent == anon) ? dentry : aparent;
1655         list_del(&dentry->d_u.d_child);
1656         if (!IS_ROOT(dentry))
1657                 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1658         else
1659                 INIT_LIST_HEAD(&dentry->d_u.d_child);
1660
1661         anon->d_parent = (dparent == dentry) ? anon : dparent;
1662         list_del(&anon->d_u.d_child);
1663         if (!IS_ROOT(anon))
1664                 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1665         else
1666                 INIT_LIST_HEAD(&anon->d_u.d_child);
1667
1668         anon->d_flags &= ~DCACHE_DISCONNECTED;
1669 }
1670
1671 /**
1672  * d_materialise_unique - introduce an inode into the tree
1673  * @dentry: candidate dentry
1674  * @inode: inode to bind to the dentry, to which aliases may be attached
1675  *
1676  * Introduces an dentry into the tree, substituting an extant disconnected
1677  * root directory alias in its place if there is one
1678  */
1679 struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1680 {
1681         struct dentry *actual;
1682
1683         BUG_ON(!d_unhashed(dentry));
1684
1685         spin_lock(&dcache_lock);
1686
1687         if (!inode) {
1688                 actual = dentry;
1689                 dentry->d_inode = NULL;
1690                 goto found_lock;
1691         }
1692
1693         if (S_ISDIR(inode->i_mode)) {
1694                 struct dentry *alias;
1695
1696                 /* Does an aliased dentry already exist? */
1697                 alias = __d_find_alias(inode, 0);
1698                 if (alias) {
1699                         actual = alias;
1700                         /* Is this an anonymous mountpoint that we could splice
1701                          * into our tree? */
1702                         if (IS_ROOT(alias)) {
1703                                 spin_lock(&alias->d_lock);
1704                                 __d_materialise_dentry(dentry, alias);
1705                                 __d_drop(alias);
1706                                 goto found;
1707                         }
1708                         /* Nope, but we must(!) avoid directory aliasing */
1709                         actual = __d_unalias(dentry, alias);
1710                         if (IS_ERR(actual))
1711                                 dput(alias);
1712                         goto out_nolock;
1713                 }
1714         }
1715
1716         /* Add a unique reference */
1717         actual = __d_instantiate_unique(dentry, inode);
1718         if (!actual)
1719                 actual = dentry;
1720         else if (unlikely(!d_unhashed(actual)))
1721                 goto shouldnt_be_hashed;
1722
1723 found_lock:
1724         spin_lock(&actual->d_lock);
1725 found:
1726         _d_rehash(actual);
1727         spin_unlock(&actual->d_lock);
1728         spin_unlock(&dcache_lock);
1729 out_nolock:
1730         if (actual == dentry) {
1731                 security_d_instantiate(dentry, inode);
1732                 return NULL;
1733         }
1734
1735         iput(inode);
1736         return actual;
1737
1738 shouldnt_be_hashed:
1739         spin_unlock(&dcache_lock);
1740         BUG();
1741         goto shouldnt_be_hashed;
1742 }
1743
1744 /**
1745  * d_path - return the path of a dentry
1746  * @dentry: dentry to report
1747  * @vfsmnt: vfsmnt to which the dentry belongs
1748  * @root: root dentry
1749  * @rootmnt: vfsmnt to which the root dentry belongs
1750  * @buffer: buffer to return value in
1751  * @buflen: buffer length
1752  *
1753  * Convert a dentry into an ASCII path name. If the entry has been deleted
1754  * the string " (deleted)" is appended. Note that this is ambiguous.
1755  *
1756  * Returns the buffer or an error code if the path was too long.
1757  *
1758  * "buflen" should be positive. Caller holds the dcache_lock.
1759  */
1760 static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1761                         struct dentry *root, struct vfsmount *rootmnt,
1762                         char *buffer, int buflen)
1763 {
1764         char * end = buffer+buflen;
1765         char * retval;
1766         int namelen;
1767
1768         *--end = '\0';
1769         buflen--;
1770         if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1771                 buflen -= 10;
1772                 end -= 10;
1773                 if (buflen < 0)
1774                         goto Elong;
1775                 memcpy(end, " (deleted)", 10);
1776         }
1777
1778         if (buflen < 1)
1779                 goto Elong;
1780         /* Get '/' right */
1781         retval = end-1;
1782         *retval = '/';
1783
1784         for (;;) {
1785                 struct dentry * parent;
1786
1787                 if (dentry == root && vfsmnt == rootmnt)
1788                         break;
1789                 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1790                         /* Global root? */
1791                         spin_lock(&vfsmount_lock);
1792                         if (vfsmnt->mnt_parent == vfsmnt) {
1793                                 spin_unlock(&vfsmount_lock);
1794                                 goto global_root;
1795                         }
1796                         dentry = vfsmnt->mnt_mountpoint;
1797                         vfsmnt = vfsmnt->mnt_parent;
1798                         spin_unlock(&vfsmount_lock);
1799                         continue;
1800                 }
1801                 parent = dentry->d_parent;
1802                 prefetch(parent);
1803                 namelen = dentry->d_name.len;
1804                 buflen -= namelen + 1;
1805                 if (buflen < 0)
1806                         goto Elong;
1807                 end -= namelen;
1808                 memcpy(end, dentry->d_name.name, namelen);
1809                 *--end = '/';
1810                 retval = end;
1811                 dentry = parent;
1812         }
1813
1814         return retval;
1815
1816 global_root:
1817         namelen = dentry->d_name.len;
1818         buflen -= namelen;
1819         if (buflen < 0)
1820                 goto Elong;
1821         retval -= namelen-1;    /* hit the slash */
1822         memcpy(retval, dentry->d_name.name, namelen);
1823         return retval;
1824 Elong:
1825         return ERR_PTR(-ENAMETOOLONG);
1826 }
1827
1828 /* write full pathname into buffer and return start of pathname */
1829 char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1830                                 char *buf, int buflen)
1831 {
1832         char *res;
1833         struct vfsmount *rootmnt;
1834         struct dentry *root;
1835
1836         read_lock(&current->fs->lock);
1837         rootmnt = mntget(current->fs->rootmnt);
1838         root = dget(current->fs->root);
1839         read_unlock(&current->fs->lock);
1840         spin_lock(&dcache_lock);
1841         res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1842         spin_unlock(&dcache_lock);
1843         dput(root);
1844         mntput(rootmnt);
1845         return res;
1846 }
1847
1848 /*
1849  * NOTE! The user-level library version returns a
1850  * character pointer. The kernel system call just
1851  * returns the length of the buffer filled (which
1852  * includes the ending '\0' character), or a negative
1853  * error value. So libc would do something like
1854  *
1855  *      char *getcwd(char * buf, size_t size)
1856  *      {
1857  *              int retval;
1858  *
1859  *              retval = sys_getcwd(buf, size);
1860  *              if (retval >= 0)
1861  *                      return buf;
1862  *              errno = -retval;
1863  *              return NULL;
1864  *      }
1865  */
1866 asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1867 {
1868         int error;
1869         struct vfsmount *pwdmnt, *rootmnt;
1870         struct dentry *pwd, *root;
1871         char *page = (char *) __get_free_page(GFP_USER);
1872
1873         if (!page)
1874                 return -ENOMEM;
1875
1876         read_lock(&current->fs->lock);
1877         pwdmnt = mntget(current->fs->pwdmnt);
1878         pwd = dget(current->fs->pwd);
1879         rootmnt = mntget(current->fs->rootmnt);
1880         root = dget(current->fs->root);
1881         read_unlock(&current->fs->lock);
1882
1883         error = -ENOENT;
1884         /* Has the current directory has been unlinked? */
1885         spin_lock(&dcache_lock);
1886         if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1887                 unsigned long len;
1888                 char * cwd;
1889
1890                 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1891                 spin_unlock(&dcache_lock);
1892
1893                 error = PTR_ERR(cwd);
1894                 if (IS_ERR(cwd))
1895                         goto out;
1896
1897                 error = -ERANGE;
1898                 len = PAGE_SIZE + page - cwd;
1899                 if (len <= size) {
1900                         error = len;
1901                         if (copy_to_user(buf, cwd, len))
1902                                 error = -EFAULT;
1903                 }
1904         } else
1905                 spin_unlock(&dcache_lock);
1906
1907 out:
1908         dput(pwd);
1909         mntput(pwdmnt);
1910         dput(root);
1911         mntput(rootmnt);
1912         free_page((unsigned long) page);
1913         return error;
1914 }
1915
1916 /*
1917  * Test whether new_dentry is a subdirectory of old_dentry.
1918  *
1919  * Trivially implemented using the dcache structure
1920  */
1921
1922 /**
1923  * is_subdir - is new dentry a subdirectory of old_dentry
1924  * @new_dentry: new dentry
1925  * @old_dentry: old dentry
1926  *
1927  * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1928  * Returns 0 otherwise.
1929  * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1930  */
1931   
1932 int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1933 {
1934         int result;
1935         struct dentry * saved = new_dentry;
1936         unsigned long seq;
1937
1938         /* need rcu_readlock to protect against the d_parent trashing due to
1939          * d_move
1940          */
1941         rcu_read_lock();
1942         do {
1943                 /* for restarting inner loop in case of seq retry */
1944                 new_dentry = saved;
1945                 result = 0;
1946                 seq = read_seqbegin(&rename_lock);
1947                 for (;;) {
1948                         if (new_dentry != old_dentry) {
1949                                 struct dentry * parent = new_dentry->d_parent;
1950                                 if (parent == new_dentry)
1951                                         break;
1952                                 new_dentry = parent;
1953                                 continue;
1954                         }
1955                         result = 1;
1956                         break;
1957                 }
1958         } while (read_seqretry(&rename_lock, seq));
1959         rcu_read_unlock();
1960
1961         return result;
1962 }
1963
1964 void d_genocide(struct dentry *root)
1965 {
1966         struct dentry *this_parent = root;
1967         struct list_head *next;
1968
1969         spin_lock(&dcache_lock);
1970 repeat:
1971         next = this_parent->d_subdirs.next;
1972 resume:
1973         while (next != &this_parent->d_subdirs) {
1974                 struct list_head *tmp = next;
1975                 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1976                 next = tmp->next;
1977                 if (d_unhashed(dentry)||!dentry->d_inode)
1978                         continue;
1979                 if (!list_empty(&dentry->d_subdirs)) {
1980                         this_parent = dentry;
1981                         goto repeat;
1982                 }
1983                 atomic_dec(&dentry->d_count);
1984         }
1985         if (this_parent != root) {
1986                 next = this_parent->d_u.d_child.next;
1987                 atomic_dec(&this_parent->d_count);
1988                 this_parent = this_parent->d_parent;
1989                 goto resume;
1990         }
1991         spin_unlock(&dcache_lock);
1992 }
1993
1994 /**
1995  * find_inode_number - check for dentry with name
1996  * @dir: directory to check
1997  * @name: Name to find.
1998  *
1999  * Check whether a dentry already exists for the given name,
2000  * and return the inode number if it has an inode. Otherwise
2001  * 0 is returned.
2002  *
2003  * This routine is used to post-process directory listings for
2004  * filesystems using synthetic inode numbers, and is necessary
2005  * to keep getcwd() working.
2006  */
2007  
2008 ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2009 {
2010         struct dentry * dentry;
2011         ino_t ino = 0;
2012
2013         dentry = d_hash_and_lookup(dir, name);
2014         if (dentry) {
2015                 if (dentry->d_inode)
2016                         ino = dentry->d_inode->i_ino;
2017                 dput(dentry);
2018         }
2019         return ino;
2020 }
2021
2022 static __initdata unsigned long dhash_entries;
2023 static int __init set_dhash_entries(char *str)
2024 {
2025         if (!str)
2026                 return 0;
2027         dhash_entries = simple_strtoul(str, &str, 0);
2028         return 1;
2029 }
2030 __setup("dhash_entries=", set_dhash_entries);
2031
2032 static void __init dcache_init_early(void)
2033 {
2034         int loop;
2035
2036         /* If hashes are distributed across NUMA nodes, defer
2037          * hash allocation until vmalloc space is available.
2038          */
2039         if (hashdist)
2040                 return;
2041
2042         dentry_hashtable =
2043                 alloc_large_system_hash("Dentry cache",
2044                                         sizeof(struct hlist_head),
2045                                         dhash_entries,
2046                                         13,
2047                                         HASH_EARLY,
2048                                         &d_hash_shift,
2049                                         &d_hash_mask,
2050                                         0);
2051
2052         for (loop = 0; loop < (1 << d_hash_shift); loop++)
2053                 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2054 }
2055
2056 static void __init dcache_init(unsigned long mempages)
2057 {
2058         int loop;
2059
2060         /* 
2061          * A constructor could be added for stable state like the lists,
2062          * but it is probably not worth it because of the cache nature
2063          * of the dcache. 
2064          */
2065         dentry_cache = kmem_cache_create("dentry_cache",
2066                                          sizeof(struct dentry),
2067                                          0,
2068                                          (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
2069                                          SLAB_MEM_SPREAD),
2070                                          NULL, NULL);
2071         
2072         set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
2073
2074         /* Hash may have been set up in dcache_init_early */
2075         if (!hashdist)
2076                 return;
2077
2078         dentry_hashtable =
2079                 alloc_large_system_hash("Dentry cache",
2080                                         sizeof(struct hlist_head),
2081                                         dhash_entries,
2082                                         13,
2083                                         0,
2084                                         &d_hash_shift,
2085                                         &d_hash_mask,
2086                                         0);
2087
2088         for (loop = 0; loop < (1 << d_hash_shift); loop++)
2089                 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2090 }
2091
2092 /* SLAB cache for __getname() consumers */
2093 struct kmem_cache *names_cachep __read_mostly;
2094
2095 /* SLAB cache for file structures */
2096 struct kmem_cache *filp_cachep __read_mostly;
2097
2098 EXPORT_SYMBOL(d_genocide);
2099
2100 void __init vfs_caches_init_early(void)
2101 {
2102         dcache_init_early();
2103         inode_init_early();
2104 }
2105
2106 void __init vfs_caches_init(unsigned long mempages)
2107 {
2108         unsigned long reserve;
2109
2110         /* Base hash sizes on available memory, with a reserve equal to
2111            150% of current kernel size */
2112
2113         reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2114         mempages -= reserve;
2115
2116         names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2117                         SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
2118
2119         filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
2120                         SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
2121
2122         dcache_init(mempages);
2123         inode_init(mempages);
2124         files_init(mempages);
2125         mnt_init(mempages);
2126         bdev_cache_init();
2127         chrdev_init();
2128 }
2129
2130 EXPORT_SYMBOL(d_alloc);
2131 EXPORT_SYMBOL(d_alloc_anon);
2132 EXPORT_SYMBOL(d_alloc_root);
2133 EXPORT_SYMBOL(d_delete);
2134 EXPORT_SYMBOL(d_find_alias);
2135 EXPORT_SYMBOL(d_instantiate);
2136 EXPORT_SYMBOL(d_invalidate);
2137 EXPORT_SYMBOL(d_lookup);
2138 EXPORT_SYMBOL(d_move);
2139 EXPORT_SYMBOL_GPL(d_materialise_unique);
2140 EXPORT_SYMBOL(d_path);
2141 EXPORT_SYMBOL(d_prune_aliases);
2142 EXPORT_SYMBOL(d_rehash);
2143 EXPORT_SYMBOL(d_splice_alias);
2144 EXPORT_SYMBOL(d_validate);
2145 EXPORT_SYMBOL(dget_locked);
2146 EXPORT_SYMBOL(dput);
2147 EXPORT_SYMBOL(find_inode_number);
2148 EXPORT_SYMBOL(have_submounts);
2149 EXPORT_SYMBOL(names_cachep);
2150 EXPORT_SYMBOL(shrink_dcache_parent);
2151 EXPORT_SYMBOL(shrink_dcache_sb);