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