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