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