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