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