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