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