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