blob: a1ff91eef10810c50583962ae671f07e14737bf6 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
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
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
John McCutchan7a91bf72005-08-08 13:52:16 -040021#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/slab.h>
23#include <linux/init.h>
24#include <linux/smp_lock.h>
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>
David Howells07f3f052006-09-30 20:52:18 +020035#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Eric Dumazetfa3536c2006-03-26 01:37:24 -080038int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
40
41 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
Ingo Molnare4d91912006-07-03 00:24:34 -070042static __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44EXPORT_SYMBOL(dcache_lock);
45
Eric Dumazetfa3536c2006-03-26 01:37:24 -080046static kmem_cache_t *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
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
Eric Dumazetfa3536c2006-03-26 01:37:24 -080061static unsigned int d_hash_mask __read_mostly;
62static unsigned int d_hash_shift __read_mostly;
63static struct hlist_head *dentry_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static LIST_HEAD(dentry_unused);
65
66/* Statistics gathering. */
67struct dentry_stat_t dentry_stat = {
68 .age_limit = 45,
69};
70
71static void d_callback(struct rcu_head *head)
72{
Eric Dumazet5160ee62006-01-08 01:03:32 -080073 struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070074
75 if (dname_external(dentry))
76 kfree(dentry->d_name.name);
77 kmem_cache_free(dentry_cache, dentry);
78}
79
80/*
81 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
82 * inside dcache_lock.
83 */
84static void d_free(struct dentry *dentry)
85{
86 if (dentry->d_op && dentry->d_op->d_release)
87 dentry->d_op->d_release(dentry);
Eric Dumazet5160ee62006-01-08 01:03:32 -080088 call_rcu(&dentry->d_u.d_rcu, d_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91/*
92 * Release the dentry's inode, using the filesystem
93 * d_iput() operation if defined.
94 * Called with dcache_lock and per dentry lock held, drops both.
95 */
Arjan van de Ven858119e2006-01-14 13:20:43 -080096static void dentry_iput(struct dentry * dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070097{
98 struct inode *inode = dentry->d_inode;
99 if (inode) {
100 dentry->d_inode = NULL;
101 list_del_init(&dentry->d_alias);
102 spin_unlock(&dentry->d_lock);
103 spin_unlock(&dcache_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700104 if (!inode->i_nlink)
105 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700106 if (dentry->d_op && dentry->d_op->d_iput)
107 dentry->d_op->d_iput(dentry, inode);
108 else
109 iput(inode);
110 } else {
111 spin_unlock(&dentry->d_lock);
112 spin_unlock(&dcache_lock);
113 }
114}
115
116/*
117 * This is dput
118 *
119 * This is complicated by the fact that we do not want to put
120 * dentries that are no longer on any hash chain on the unused
121 * list: we'd much rather just get rid of them immediately.
122 *
123 * However, that implies that we have to traverse the dentry
124 * tree upwards to the parents which might _also_ now be
125 * scheduled for deletion (it may have been only waiting for
126 * its last child to go away).
127 *
128 * This tail recursion is done by hand as we don't want to depend
129 * on the compiler to always get this right (gcc generally doesn't).
130 * Real recursion would eat up our stack space.
131 */
132
133/*
134 * dput - release a dentry
135 * @dentry: dentry to release
136 *
137 * Release a dentry. This will drop the usage count and if appropriate
138 * call the dentry unlink method as well as removing it from the queues and
139 * releasing its resources. If the parent dentries were scheduled for release
140 * they too may now get deleted.
141 *
142 * no dcache lock, please.
143 */
144
145void dput(struct dentry *dentry)
146{
147 if (!dentry)
148 return;
149
150repeat:
151 if (atomic_read(&dentry->d_count) == 1)
152 might_sleep();
153 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
154 return;
155
156 spin_lock(&dentry->d_lock);
157 if (atomic_read(&dentry->d_count)) {
158 spin_unlock(&dentry->d_lock);
159 spin_unlock(&dcache_lock);
160 return;
161 }
162
163 /*
164 * AV: ->d_delete() is _NOT_ allowed to block now.
165 */
166 if (dentry->d_op && dentry->d_op->d_delete) {
167 if (dentry->d_op->d_delete(dentry))
168 goto unhash_it;
169 }
170 /* Unreachable? Get rid of it */
171 if (d_unhashed(dentry))
172 goto kill_it;
173 if (list_empty(&dentry->d_lru)) {
174 dentry->d_flags |= DCACHE_REFERENCED;
175 list_add(&dentry->d_lru, &dentry_unused);
176 dentry_stat.nr_unused++;
177 }
178 spin_unlock(&dentry->d_lock);
179 spin_unlock(&dcache_lock);
180 return;
181
182unhash_it:
183 __d_drop(dentry);
184
185kill_it: {
186 struct dentry *parent;
187
188 /* If dentry was on d_lru list
189 * delete it from there
190 */
191 if (!list_empty(&dentry->d_lru)) {
192 list_del(&dentry->d_lru);
193 dentry_stat.nr_unused--;
194 }
Eric Dumazet5160ee62006-01-08 01:03:32 -0800195 list_del(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700196 dentry_stat.nr_dentry--; /* For d_free, below */
197 /*drops the locks, at that point nobody can reach this dentry */
198 dentry_iput(dentry);
199 parent = dentry->d_parent;
200 d_free(dentry);
201 if (dentry == parent)
202 return;
203 dentry = parent;
204 goto repeat;
205 }
206}
207
208/**
209 * d_invalidate - invalidate a dentry
210 * @dentry: dentry to invalidate
211 *
212 * Try to invalidate the dentry if it turns out to be
213 * possible. If there are other dentries that can be
214 * reached through this one we can't delete it and we
215 * return -EBUSY. On success we return 0.
216 *
217 * no dcache lock.
218 */
219
220int d_invalidate(struct dentry * dentry)
221{
222 /*
223 * If it's already been dropped, return OK.
224 */
225 spin_lock(&dcache_lock);
226 if (d_unhashed(dentry)) {
227 spin_unlock(&dcache_lock);
228 return 0;
229 }
230 /*
231 * Check whether to do a partial shrink_dcache
232 * to get rid of unused child entries.
233 */
234 if (!list_empty(&dentry->d_subdirs)) {
235 spin_unlock(&dcache_lock);
236 shrink_dcache_parent(dentry);
237 spin_lock(&dcache_lock);
238 }
239
240 /*
241 * Somebody else still using it?
242 *
243 * If it's a directory, we can't drop it
244 * for fear of somebody re-populating it
245 * with children (even though dropping it
246 * would make it unreachable from the root,
247 * we might still populate it if it was a
248 * working directory or similar).
249 */
250 spin_lock(&dentry->d_lock);
251 if (atomic_read(&dentry->d_count) > 1) {
252 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
253 spin_unlock(&dentry->d_lock);
254 spin_unlock(&dcache_lock);
255 return -EBUSY;
256 }
257 }
258
259 __d_drop(dentry);
260 spin_unlock(&dentry->d_lock);
261 spin_unlock(&dcache_lock);
262 return 0;
263}
264
265/* This should be called _only_ with dcache_lock held */
266
267static inline struct dentry * __dget_locked(struct dentry *dentry)
268{
269 atomic_inc(&dentry->d_count);
270 if (!list_empty(&dentry->d_lru)) {
271 dentry_stat.nr_unused--;
272 list_del_init(&dentry->d_lru);
273 }
274 return dentry;
275}
276
277struct dentry * dget_locked(struct dentry *dentry)
278{
279 return __dget_locked(dentry);
280}
281
282/**
283 * d_find_alias - grab a hashed alias of inode
284 * @inode: inode in question
285 * @want_discon: flag, used by d_splice_alias, to request
286 * that only a DISCONNECTED alias be returned.
287 *
288 * If inode has a hashed alias, or is a directory and has any alias,
289 * acquire the reference to alias and return it. Otherwise return NULL.
290 * Notice that if inode is a directory there can be only one alias and
291 * it can be unhashed only if it has no children, or if it is the root
292 * of a filesystem.
293 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700294 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700295 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700296 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700297 */
298
299static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
300{
301 struct list_head *head, *next, *tmp;
302 struct dentry *alias, *discon_alias=NULL;
303
304 head = &inode->i_dentry;
305 next = inode->i_dentry.next;
306 while (next != head) {
307 tmp = next;
308 next = tmp->next;
309 prefetch(next);
310 alias = list_entry(tmp, struct dentry, d_alias);
311 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700312 if (IS_ROOT(alias) &&
313 (alias->d_flags & DCACHE_DISCONNECTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 discon_alias = alias;
315 else if (!want_discon) {
316 __dget_locked(alias);
317 return alias;
318 }
319 }
320 }
321 if (discon_alias)
322 __dget_locked(discon_alias);
323 return discon_alias;
324}
325
326struct dentry * d_find_alias(struct inode *inode)
327{
David Howells214fda12006-03-25 03:06:36 -0800328 struct dentry *de = NULL;
329
330 if (!list_empty(&inode->i_dentry)) {
331 spin_lock(&dcache_lock);
332 de = __d_find_alias(inode, 0);
333 spin_unlock(&dcache_lock);
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return de;
336}
337
338/*
339 * Try to kill dentries associated with this inode.
340 * WARNING: you must own a reference to inode.
341 */
342void d_prune_aliases(struct inode *inode)
343{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700344 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345restart:
346 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700347 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 spin_lock(&dentry->d_lock);
349 if (!atomic_read(&dentry->d_count)) {
350 __dget_locked(dentry);
351 __d_drop(dentry);
352 spin_unlock(&dentry->d_lock);
353 spin_unlock(&dcache_lock);
354 dput(dentry);
355 goto restart;
356 }
357 spin_unlock(&dentry->d_lock);
358 }
359 spin_unlock(&dcache_lock);
360}
361
362/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700363 * Throw away a dentry - free the inode, dput the parent. This requires that
364 * the LRU list has already been removed.
365 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 * Called with dcache_lock, drops it and then regains.
Andrew Mortond702ccb2006-06-22 14:47:31 -0700367 * Called with dentry->d_lock held, drops it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 */
Andrew Mortond702ccb2006-06-22 14:47:31 -0700369static void prune_one_dentry(struct dentry * dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700370{
371 struct dentry * parent;
372
373 __d_drop(dentry);
Eric Dumazet5160ee62006-01-08 01:03:32 -0800374 list_del(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700375 dentry_stat.nr_dentry--; /* For d_free, below */
376 dentry_iput(dentry);
377 parent = dentry->d_parent;
378 d_free(dentry);
379 if (parent != dentry)
380 dput(parent);
381 spin_lock(&dcache_lock);
382}
383
384/**
385 * prune_dcache - shrink the dcache
386 * @count: number of entries to try and free
NeilBrown0feae5c2006-06-22 14:47:28 -0700387 * @sb: if given, ignore dentries for other superblocks
388 * which are being unmounted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 *
390 * Shrink the dcache. This is done when we need
391 * more memory, or simply when we need to unmount
392 * something (at which point we need to unuse
393 * all dentries).
394 *
395 * This function may fail to free any resources if
396 * all the dentries are in use.
397 */
398
NeilBrown0feae5c2006-06-22 14:47:28 -0700399static void prune_dcache(int count, struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700400{
401 spin_lock(&dcache_lock);
402 for (; count ; count--) {
403 struct dentry *dentry;
404 struct list_head *tmp;
NeilBrown0feae5c2006-06-22 14:47:28 -0700405 struct rw_semaphore *s_umount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406
407 cond_resched_lock(&dcache_lock);
408
409 tmp = dentry_unused.prev;
Hua Zhongf58a1eb2006-06-25 05:49:32 -0700410 if (sb) {
NeilBrown0feae5c2006-06-22 14:47:28 -0700411 /* Try to find a dentry for this sb, but don't try
412 * too hard, if they aren't near the tail they will
413 * be moved down again soon
414 */
415 int skip = count;
416 while (skip && tmp != &dentry_unused &&
417 list_entry(tmp, struct dentry, d_lru)->d_sb != sb) {
418 skip--;
419 tmp = tmp->prev;
420 }
421 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700422 if (tmp == &dentry_unused)
423 break;
424 list_del_init(tmp);
425 prefetch(dentry_unused.prev);
426 dentry_stat.nr_unused--;
427 dentry = list_entry(tmp, struct dentry, d_lru);
428
429 spin_lock(&dentry->d_lock);
430 /*
431 * We found an inuse dentry which was not removed from
432 * dentry_unused because of laziness during lookup. Do not free
433 * it - just keep it off the dentry_unused list.
434 */
435 if (atomic_read(&dentry->d_count)) {
436 spin_unlock(&dentry->d_lock);
437 continue;
438 }
439 /* If the dentry was recently referenced, don't free it. */
440 if (dentry->d_flags & DCACHE_REFERENCED) {
441 dentry->d_flags &= ~DCACHE_REFERENCED;
442 list_add(&dentry->d_lru, &dentry_unused);
443 dentry_stat.nr_unused++;
444 spin_unlock(&dentry->d_lock);
445 continue;
446 }
NeilBrown0feae5c2006-06-22 14:47:28 -0700447 /*
448 * If the dentry is not DCACHED_REFERENCED, it is time
449 * to remove it from the dcache, provided the super block is
450 * NULL (which means we are trying to reclaim memory)
451 * or this dentry belongs to the same super block that
452 * we want to shrink.
453 */
454 /*
455 * If this dentry is for "my" filesystem, then I can prune it
456 * without taking the s_umount lock (I already hold it).
457 */
458 if (sb && dentry->d_sb == sb) {
459 prune_one_dentry(dentry);
460 continue;
461 }
462 /*
463 * ...otherwise we need to be sure this filesystem isn't being
464 * unmounted, otherwise we could race with
465 * generic_shutdown_super(), and end up holding a reference to
466 * an inode while the filesystem is unmounted.
467 * So we try to get s_umount, and make sure s_root isn't NULL.
468 * (Take a local copy of s_umount to avoid a use-after-free of
469 * `dentry').
470 */
471 s_umount = &dentry->d_sb->s_umount;
472 if (down_read_trylock(s_umount)) {
473 if (dentry->d_sb->s_root != NULL) {
474 prune_one_dentry(dentry);
475 up_read(s_umount);
476 continue;
477 }
478 up_read(s_umount);
479 }
480 spin_unlock(&dentry->d_lock);
481 /* Cannot remove the first dentry, and it isn't appropriate
482 * to move it to the head of the list, so give up, and try
483 * later
484 */
485 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700486 }
487 spin_unlock(&dcache_lock);
488}
489
490/*
491 * Shrink the dcache for the specified super block.
492 * This allows us to unmount a device without disturbing
493 * the dcache for the other devices.
494 *
495 * This implementation makes just two traversals of the
496 * unused list. On the first pass we move the selected
497 * dentries to the most recent end, and on the second
498 * pass we free them. The second pass must restart after
499 * each dput(), but since the target dentries are all at
500 * the end, it's really just a single traversal.
501 */
502
503/**
504 * shrink_dcache_sb - shrink dcache for a superblock
505 * @sb: superblock
506 *
507 * Shrink the dcache for the specified super block. This
508 * is used to free the dcache before unmounting a file
509 * system
510 */
511
512void shrink_dcache_sb(struct super_block * sb)
513{
514 struct list_head *tmp, *next;
515 struct dentry *dentry;
516
517 /*
518 * Pass one ... move the dentries for the specified
519 * superblock to the most recent end of the unused list.
520 */
521 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700522 list_for_each_safe(tmp, next, &dentry_unused) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 dentry = list_entry(tmp, struct dentry, d_lru);
524 if (dentry->d_sb != sb)
525 continue;
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700526 list_move(tmp, &dentry_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700527 }
528
529 /*
530 * Pass two ... free the dentries for this superblock.
531 */
532repeat:
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700533 list_for_each_safe(tmp, next, &dentry_unused) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700534 dentry = list_entry(tmp, struct dentry, d_lru);
535 if (dentry->d_sb != sb)
536 continue;
537 dentry_stat.nr_unused--;
538 list_del_init(tmp);
539 spin_lock(&dentry->d_lock);
540 if (atomic_read(&dentry->d_count)) {
541 spin_unlock(&dentry->d_lock);
542 continue;
543 }
544 prune_one_dentry(dentry);
Kirill Korotaev2ab13462006-03-25 03:07:45 -0800545 cond_resched_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700546 goto repeat;
547 }
548 spin_unlock(&dcache_lock);
549}
550
551/*
David Howellsc636ebd2006-10-11 01:22:19 -0700552 * destroy a single subtree of dentries for unmount
553 * - see the comments on shrink_dcache_for_umount() for a description of the
554 * locking
555 */
556static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
557{
558 struct dentry *parent;
559
560 BUG_ON(!IS_ROOT(dentry));
561
562 /* detach this root from the system */
563 spin_lock(&dcache_lock);
564 if (!list_empty(&dentry->d_lru)) {
565 dentry_stat.nr_unused--;
566 list_del_init(&dentry->d_lru);
567 }
568 __d_drop(dentry);
569 spin_unlock(&dcache_lock);
570
571 for (;;) {
572 /* descend to the first leaf in the current subtree */
573 while (!list_empty(&dentry->d_subdirs)) {
574 struct dentry *loop;
575
576 /* this is a branch with children - detach all of them
577 * from the system in one go */
578 spin_lock(&dcache_lock);
579 list_for_each_entry(loop, &dentry->d_subdirs,
580 d_u.d_child) {
581 if (!list_empty(&loop->d_lru)) {
582 dentry_stat.nr_unused--;
583 list_del_init(&loop->d_lru);
584 }
585
586 __d_drop(loop);
587 cond_resched_lock(&dcache_lock);
588 }
589 spin_unlock(&dcache_lock);
590
591 /* move to the first child */
592 dentry = list_entry(dentry->d_subdirs.next,
593 struct dentry, d_u.d_child);
594 }
595
596 /* consume the dentries from this leaf up through its parents
597 * until we find one with children or run out altogether */
598 do {
599 struct inode *inode;
600
601 if (atomic_read(&dentry->d_count) != 0) {
602 printk(KERN_ERR
603 "BUG: Dentry %p{i=%lx,n=%s}"
604 " still in use (%d)"
605 " [unmount of %s %s]\n",
606 dentry,
607 dentry->d_inode ?
608 dentry->d_inode->i_ino : 0UL,
609 dentry->d_name.name,
610 atomic_read(&dentry->d_count),
611 dentry->d_sb->s_type->name,
612 dentry->d_sb->s_id);
613 BUG();
614 }
615
616 parent = dentry->d_parent;
617 if (parent == dentry)
618 parent = NULL;
619 else
620 atomic_dec(&parent->d_count);
621
622 list_del(&dentry->d_u.d_child);
623 dentry_stat.nr_dentry--; /* For d_free, below */
624
625 inode = dentry->d_inode;
626 if (inode) {
627 dentry->d_inode = NULL;
628 list_del_init(&dentry->d_alias);
629 if (dentry->d_op && dentry->d_op->d_iput)
630 dentry->d_op->d_iput(dentry, inode);
631 else
632 iput(inode);
633 }
634
635 d_free(dentry);
636
637 /* finished when we fall off the top of the tree,
638 * otherwise we ascend to the parent and move to the
639 * next sibling if there is one */
640 if (!parent)
641 return;
642
643 dentry = parent;
644
645 } while (list_empty(&dentry->d_subdirs));
646
647 dentry = list_entry(dentry->d_subdirs.next,
648 struct dentry, d_u.d_child);
649 }
650}
651
652/*
653 * destroy the dentries attached to a superblock on unmounting
654 * - we don't need to use dentry->d_lock, and only need dcache_lock when
655 * removing the dentry from the system lists and hashes because:
656 * - the superblock is detached from all mountings and open files, so the
657 * dentry trees will not be rearranged by the VFS
658 * - s_umount is write-locked, so the memory pressure shrinker will ignore
659 * any dentries belonging to this superblock that it comes across
660 * - the filesystem itself is no longer permitted to rearrange the dentries
661 * in this superblock
662 */
663void shrink_dcache_for_umount(struct super_block *sb)
664{
665 struct dentry *dentry;
666
667 if (down_read_trylock(&sb->s_umount))
668 BUG();
669
670 dentry = sb->s_root;
671 sb->s_root = NULL;
672 atomic_dec(&dentry->d_count);
673 shrink_dcache_for_umount_subtree(dentry);
674
675 while (!hlist_empty(&sb->s_anon)) {
676 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
677 shrink_dcache_for_umount_subtree(dentry);
678 }
679}
680
681/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 * Search for at least 1 mount point in the dentry's subdirs.
683 * We descend to the next level whenever the d_subdirs
684 * list is non-empty and continue searching.
685 */
686
687/**
688 * have_submounts - check for mounts over a dentry
689 * @parent: dentry to check.
690 *
691 * Return true if the parent or its subdirectories contain
692 * a mount point
693 */
694
695int have_submounts(struct dentry *parent)
696{
697 struct dentry *this_parent = parent;
698 struct list_head *next;
699
700 spin_lock(&dcache_lock);
701 if (d_mountpoint(parent))
702 goto positive;
703repeat:
704 next = this_parent->d_subdirs.next;
705resume:
706 while (next != &this_parent->d_subdirs) {
707 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800708 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 next = tmp->next;
710 /* Have we found a mount point ? */
711 if (d_mountpoint(dentry))
712 goto positive;
713 if (!list_empty(&dentry->d_subdirs)) {
714 this_parent = dentry;
715 goto repeat;
716 }
717 }
718 /*
719 * All done at this level ... ascend and resume the search.
720 */
721 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800722 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700723 this_parent = this_parent->d_parent;
724 goto resume;
725 }
726 spin_unlock(&dcache_lock);
727 return 0; /* No mount points found in tree */
728positive:
729 spin_unlock(&dcache_lock);
730 return 1;
731}
732
733/*
734 * Search the dentry child list for the specified parent,
735 * and move any unused dentries to the end of the unused
736 * list for prune_dcache(). We descend to the next level
737 * whenever the d_subdirs list is non-empty and continue
738 * searching.
739 *
740 * It returns zero iff there are no unused children,
741 * otherwise it returns the number of children moved to
742 * the end of the unused list. This may not be the total
743 * number of unused children, because select_parent can
744 * drop the lock and return early due to latency
745 * constraints.
746 */
747static int select_parent(struct dentry * parent)
748{
749 struct dentry *this_parent = parent;
750 struct list_head *next;
751 int found = 0;
752
753 spin_lock(&dcache_lock);
754repeat:
755 next = this_parent->d_subdirs.next;
756resume:
757 while (next != &this_parent->d_subdirs) {
758 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800759 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700760 next = tmp->next;
761
762 if (!list_empty(&dentry->d_lru)) {
763 dentry_stat.nr_unused--;
764 list_del_init(&dentry->d_lru);
765 }
766 /*
767 * move only zero ref count dentries to the end
768 * of the unused list for prune_dcache
769 */
770 if (!atomic_read(&dentry->d_count)) {
Akinobu Mita8e130592006-06-26 00:24:37 -0700771 list_add_tail(&dentry->d_lru, &dentry_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700772 dentry_stat.nr_unused++;
773 found++;
774 }
775
776 /*
777 * We can return to the caller if we have found some (this
778 * ensures forward progress). We'll be coming back to find
779 * the rest.
780 */
781 if (found && need_resched())
782 goto out;
783
784 /*
785 * Descend a level if the d_subdirs list is non-empty.
786 */
787 if (!list_empty(&dentry->d_subdirs)) {
788 this_parent = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 goto repeat;
790 }
791 }
792 /*
793 * All done at this level ... ascend and resume the search.
794 */
795 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800796 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700797 this_parent = this_parent->d_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 goto resume;
799 }
800out:
801 spin_unlock(&dcache_lock);
802 return found;
803}
804
805/**
806 * shrink_dcache_parent - prune dcache
807 * @parent: parent of entries to prune
808 *
809 * Prune the dcache to remove unused children of the parent dentry.
810 */
811
812void shrink_dcache_parent(struct dentry * parent)
813{
814 int found;
815
816 while ((found = select_parent(parent)) != 0)
NeilBrown0feae5c2006-06-22 14:47:28 -0700817 prune_dcache(found, parent->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700818}
819
Linus Torvalds1da177e2005-04-16 15:20:36 -0700820/*
821 * Scan `nr' dentries and return the number which remain.
822 *
823 * We need to avoid reentering the filesystem if the caller is performing a
824 * GFP_NOFS allocation attempt. One example deadlock is:
825 *
826 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
827 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
828 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
829 *
830 * In this case we return -1 to tell the caller that we baled.
831 */
Al Viro27496a82005-10-21 03:20:48 -0400832static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833{
834 if (nr) {
835 if (!(gfp_mask & __GFP_FS))
836 return -1;
NeilBrown0feae5c2006-06-22 14:47:28 -0700837 prune_dcache(nr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700838 }
839 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
840}
841
842/**
843 * d_alloc - allocate a dcache entry
844 * @parent: parent of entry to allocate
845 * @name: qstr of the name
846 *
847 * Allocates a dentry. It returns %NULL if there is insufficient memory
848 * available. On a success the dentry is returned. The name passed in is
849 * copied and the copy passed in may be reused after this call.
850 */
851
852struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
853{
854 struct dentry *dentry;
855 char *dname;
856
857 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
858 if (!dentry)
859 return NULL;
860
861 if (name->len > DNAME_INLINE_LEN-1) {
862 dname = kmalloc(name->len + 1, GFP_KERNEL);
863 if (!dname) {
864 kmem_cache_free(dentry_cache, dentry);
865 return NULL;
866 }
867 } else {
868 dname = dentry->d_iname;
869 }
870 dentry->d_name.name = dname;
871
872 dentry->d_name.len = name->len;
873 dentry->d_name.hash = name->hash;
874 memcpy(dname, name->name, name->len);
875 dname[name->len] = 0;
876
877 atomic_set(&dentry->d_count, 1);
878 dentry->d_flags = DCACHE_UNHASHED;
879 spin_lock_init(&dentry->d_lock);
880 dentry->d_inode = NULL;
881 dentry->d_parent = NULL;
882 dentry->d_sb = NULL;
883 dentry->d_op = NULL;
884 dentry->d_fsdata = NULL;
885 dentry->d_mounted = 0;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800886#ifdef CONFIG_PROFILING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700887 dentry->d_cookie = NULL;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800888#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700889 INIT_HLIST_NODE(&dentry->d_hash);
890 INIT_LIST_HEAD(&dentry->d_lru);
891 INIT_LIST_HEAD(&dentry->d_subdirs);
892 INIT_LIST_HEAD(&dentry->d_alias);
893
894 if (parent) {
895 dentry->d_parent = dget(parent);
896 dentry->d_sb = parent->d_sb;
897 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800898 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700899 }
900
901 spin_lock(&dcache_lock);
902 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -0800903 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 dentry_stat.nr_dentry++;
905 spin_unlock(&dcache_lock);
906
907 return dentry;
908}
909
910struct dentry *d_alloc_name(struct dentry *parent, const char *name)
911{
912 struct qstr q;
913
914 q.name = name;
915 q.len = strlen(name);
916 q.hash = full_name_hash(q.name, q.len);
917 return d_alloc(parent, &q);
918}
919
920/**
921 * d_instantiate - fill in inode information for a dentry
922 * @entry: dentry to complete
923 * @inode: inode to attach to this dentry
924 *
925 * Fill in inode information in the entry.
926 *
927 * This turns negative dentries into productive full members
928 * of society.
929 *
930 * NOTE! This assumes that the inode count has been incremented
931 * (or otherwise set) by the caller to indicate that it is now
932 * in use by the dcache.
933 */
934
935void d_instantiate(struct dentry *entry, struct inode * inode)
936{
Eric Sesterhenn28133c72006-03-26 18:25:39 +0200937 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700938 spin_lock(&dcache_lock);
939 if (inode)
940 list_add(&entry->d_alias, &inode->i_dentry);
941 entry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800942 fsnotify_d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 spin_unlock(&dcache_lock);
944 security_d_instantiate(entry, inode);
945}
946
947/**
948 * d_instantiate_unique - instantiate a non-aliased dentry
949 * @entry: dentry to instantiate
950 * @inode: inode to attach to this dentry
951 *
952 * Fill in inode information in the entry. On success, it returns NULL.
953 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -0800954 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700955 *
956 * Note that in order to avoid conflicts with rename() etc, the caller
957 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -0800958 *
959 * This also assumes that the inode count has been incremented
960 * (or otherwise set) by the caller to indicate that it is now
961 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700962 */
David Howells770bfad2006-08-22 20:06:07 -0400963static struct dentry *__d_instantiate_unique(struct dentry *entry,
964 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700965{
966 struct dentry *alias;
967 int len = entry->d_name.len;
968 const char *name = entry->d_name.name;
969 unsigned int hash = entry->d_name.hash;
970
David Howells770bfad2006-08-22 20:06:07 -0400971 if (!inode) {
972 entry->d_inode = NULL;
973 return NULL;
974 }
975
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
977 struct qstr *qstr = &alias->d_name;
978
979 if (qstr->hash != hash)
980 continue;
981 if (alias->d_parent != entry->d_parent)
982 continue;
983 if (qstr->len != len)
984 continue;
985 if (memcmp(qstr->name, name, len))
986 continue;
987 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 return alias;
989 }
David Howells770bfad2006-08-22 20:06:07 -0400990
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 list_add(&entry->d_alias, &inode->i_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700992 entry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800993 fsnotify_d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700994 return NULL;
995}
David Howells770bfad2006-08-22 20:06:07 -0400996
997struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
998{
999 struct dentry *result;
1000
1001 BUG_ON(!list_empty(&entry->d_alias));
1002
1003 spin_lock(&dcache_lock);
1004 result = __d_instantiate_unique(entry, inode);
1005 spin_unlock(&dcache_lock);
1006
1007 if (!result) {
1008 security_d_instantiate(entry, inode);
1009 return NULL;
1010 }
1011
1012 BUG_ON(!d_unhashed(result));
1013 iput(inode);
1014 return result;
1015}
1016
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017EXPORT_SYMBOL(d_instantiate_unique);
1018
1019/**
1020 * d_alloc_root - allocate root dentry
1021 * @root_inode: inode to allocate the root for
1022 *
1023 * Allocate a root ("/") dentry for the inode given. The inode is
1024 * instantiated and returned. %NULL is returned if there is insufficient
1025 * memory or the inode passed is %NULL.
1026 */
1027
1028struct dentry * d_alloc_root(struct inode * root_inode)
1029{
1030 struct dentry *res = NULL;
1031
1032 if (root_inode) {
1033 static const struct qstr name = { .name = "/", .len = 1 };
1034
1035 res = d_alloc(NULL, &name);
1036 if (res) {
1037 res->d_sb = root_inode->i_sb;
1038 res->d_parent = res;
1039 d_instantiate(res, root_inode);
1040 }
1041 }
1042 return res;
1043}
1044
1045static inline struct hlist_head *d_hash(struct dentry *parent,
1046 unsigned long hash)
1047{
1048 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1049 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1050 return dentry_hashtable + (hash & D_HASHMASK);
1051}
1052
1053/**
1054 * d_alloc_anon - allocate an anonymous dentry
1055 * @inode: inode to allocate the dentry for
1056 *
1057 * This is similar to d_alloc_root. It is used by filesystems when
1058 * creating a dentry for a given inode, often in the process of
1059 * mapping a filehandle to a dentry. The returned dentry may be
1060 * anonymous, or may have a full name (if the inode was already
1061 * in the cache). The file system may need to make further
1062 * efforts to connect this dentry into the dcache properly.
1063 *
1064 * When called on a directory inode, we must ensure that
1065 * the inode only ever has one dentry. If a dentry is
1066 * found, that is returned instead of allocating a new one.
1067 *
1068 * On successful return, the reference to the inode has been transferred
1069 * to the dentry. If %NULL is returned (indicating kmalloc failure),
1070 * the reference on the inode has not been released.
1071 */
1072
1073struct dentry * d_alloc_anon(struct inode *inode)
1074{
1075 static const struct qstr anonstring = { .name = "" };
1076 struct dentry *tmp;
1077 struct dentry *res;
1078
1079 if ((res = d_find_alias(inode))) {
1080 iput(inode);
1081 return res;
1082 }
1083
1084 tmp = d_alloc(NULL, &anonstring);
1085 if (!tmp)
1086 return NULL;
1087
1088 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1089
1090 spin_lock(&dcache_lock);
1091 res = __d_find_alias(inode, 0);
1092 if (!res) {
1093 /* attach a disconnected dentry */
1094 res = tmp;
1095 tmp = NULL;
1096 spin_lock(&res->d_lock);
1097 res->d_sb = inode->i_sb;
1098 res->d_parent = res;
1099 res->d_inode = inode;
1100 res->d_flags |= DCACHE_DISCONNECTED;
1101 res->d_flags &= ~DCACHE_UNHASHED;
1102 list_add(&res->d_alias, &inode->i_dentry);
1103 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
1104 spin_unlock(&res->d_lock);
1105
1106 inode = NULL; /* don't drop reference */
1107 }
1108 spin_unlock(&dcache_lock);
1109
1110 if (inode)
1111 iput(inode);
1112 if (tmp)
1113 dput(tmp);
1114 return res;
1115}
1116
1117
1118/**
1119 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1120 * @inode: the inode which may have a disconnected dentry
1121 * @dentry: a negative dentry which we want to point to the inode.
1122 *
1123 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1124 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1125 * and return it, else simply d_add the inode to the dentry and return NULL.
1126 *
1127 * This is needed in the lookup routine of any filesystem that is exportable
1128 * (via knfsd) so that we can build dcache paths to directories effectively.
1129 *
1130 * If a dentry was found and moved, then it is returned. Otherwise NULL
1131 * is returned. This matches the expected return value of ->lookup.
1132 *
1133 */
1134struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1135{
1136 struct dentry *new = NULL;
1137
NeilBrown21c0d8f2006-10-04 02:16:16 -07001138 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 spin_lock(&dcache_lock);
1140 new = __d_find_alias(inode, 1);
1141 if (new) {
1142 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Pigginc32ccd82006-03-25 03:07:09 -08001143 fsnotify_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001144 spin_unlock(&dcache_lock);
1145 security_d_instantiate(new, inode);
1146 d_rehash(dentry);
1147 d_move(new, dentry);
1148 iput(inode);
1149 } else {
1150 /* d_instantiate takes dcache_lock, so we do it by hand */
1151 list_add(&dentry->d_alias, &inode->i_dentry);
1152 dentry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -08001153 fsnotify_d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 spin_unlock(&dcache_lock);
1155 security_d_instantiate(dentry, inode);
1156 d_rehash(dentry);
1157 }
1158 } else
1159 d_add(dentry, inode);
1160 return new;
1161}
1162
1163
1164/**
1165 * d_lookup - search for a dentry
1166 * @parent: parent dentry
1167 * @name: qstr of name we wish to find
1168 *
1169 * Searches the children of the parent dentry for the name in question. If
1170 * the dentry is found its reference count is incremented and the dentry
1171 * is returned. The caller must use d_put to free the entry when it has
1172 * finished using it. %NULL is returned on failure.
1173 *
1174 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1175 * Memory barriers are used while updating and doing lockless traversal.
1176 * To avoid races with d_move while rename is happening, d_lock is used.
1177 *
1178 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1179 * and name pointer in one structure pointed by d_qstr.
1180 *
1181 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1182 * lookup is going on.
1183 *
1184 * dentry_unused list is not updated even if lookup finds the required dentry
1185 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1186 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1187 * acquisition.
1188 *
1189 * d_lookup() is protected against the concurrent renames in some unrelated
1190 * directory using the seqlockt_t rename_lock.
1191 */
1192
1193struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1194{
1195 struct dentry * dentry = NULL;
1196 unsigned long seq;
1197
1198 do {
1199 seq = read_seqbegin(&rename_lock);
1200 dentry = __d_lookup(parent, name);
1201 if (dentry)
1202 break;
1203 } while (read_seqretry(&rename_lock, seq));
1204 return dentry;
1205}
1206
1207struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1208{
1209 unsigned int len = name->len;
1210 unsigned int hash = name->hash;
1211 const unsigned char *str = name->name;
1212 struct hlist_head *head = d_hash(parent,hash);
1213 struct dentry *found = NULL;
1214 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001215 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216
1217 rcu_read_lock();
1218
Paul E. McKenney665a7582005-11-07 00:59:17 -08001219 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 struct qstr *qstr;
1221
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 if (dentry->d_name.hash != hash)
1223 continue;
1224 if (dentry->d_parent != parent)
1225 continue;
1226
1227 spin_lock(&dentry->d_lock);
1228
1229 /*
1230 * Recheck the dentry after taking the lock - d_move may have
1231 * changed things. Don't bother checking the hash because we're
1232 * about to compare the whole name anyway.
1233 */
1234 if (dentry->d_parent != parent)
1235 goto next;
1236
1237 /*
1238 * It is safe to compare names since d_move() cannot
1239 * change the qstr (protected by d_lock).
1240 */
1241 qstr = &dentry->d_name;
1242 if (parent->d_op && parent->d_op->d_compare) {
1243 if (parent->d_op->d_compare(parent, qstr, name))
1244 goto next;
1245 } else {
1246 if (qstr->len != len)
1247 goto next;
1248 if (memcmp(qstr->name, str, len))
1249 goto next;
1250 }
1251
1252 if (!d_unhashed(dentry)) {
1253 atomic_inc(&dentry->d_count);
1254 found = dentry;
1255 }
1256 spin_unlock(&dentry->d_lock);
1257 break;
1258next:
1259 spin_unlock(&dentry->d_lock);
1260 }
1261 rcu_read_unlock();
1262
1263 return found;
1264}
1265
1266/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001267 * d_hash_and_lookup - hash the qstr then search for a dentry
1268 * @dir: Directory to search in
1269 * @name: qstr of name we wish to find
1270 *
1271 * On hash failure or on lookup failure NULL is returned.
1272 */
1273struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1274{
1275 struct dentry *dentry = NULL;
1276
1277 /*
1278 * Check for a fs-specific hash function. Note that we must
1279 * calculate the standard hash first, as the d_op->d_hash()
1280 * routine may choose to leave the hash value unchanged.
1281 */
1282 name->hash = full_name_hash(name->name, name->len);
1283 if (dir->d_op && dir->d_op->d_hash) {
1284 if (dir->d_op->d_hash(dir, name) < 0)
1285 goto out;
1286 }
1287 dentry = d_lookup(dir, name);
1288out:
1289 return dentry;
1290}
1291
1292/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 * d_validate - verify dentry provided from insecure source
1294 * @dentry: The dentry alleged to be valid child of @dparent
1295 * @dparent: The parent dentry (known to be valid)
1296 * @hash: Hash of the dentry
1297 * @len: Length of the name
1298 *
1299 * An insecure source has sent us a dentry, here we verify it and dget() it.
1300 * This is used by ncpfs in its readdir implementation.
1301 * Zero is returned in the dentry is invalid.
1302 */
1303
1304int d_validate(struct dentry *dentry, struct dentry *dparent)
1305{
1306 struct hlist_head *base;
1307 struct hlist_node *lhp;
1308
1309 /* Check whether the ptr might be valid at all.. */
1310 if (!kmem_ptr_validate(dentry_cache, dentry))
1311 goto out;
1312
1313 if (dentry->d_parent != dparent)
1314 goto out;
1315
1316 spin_lock(&dcache_lock);
1317 base = d_hash(dparent, dentry->d_name.hash);
1318 hlist_for_each(lhp,base) {
Paul E. McKenney665a7582005-11-07 00:59:17 -08001319 /* hlist_for_each_entry_rcu() not required for d_hash list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320 * as it is parsed under dcache_lock
1321 */
1322 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1323 __dget_locked(dentry);
1324 spin_unlock(&dcache_lock);
1325 return 1;
1326 }
1327 }
1328 spin_unlock(&dcache_lock);
1329out:
1330 return 0;
1331}
1332
1333/*
1334 * When a file is deleted, we have two options:
1335 * - turn this dentry into a negative dentry
1336 * - unhash this dentry and free it.
1337 *
1338 * Usually, we want to just turn this into
1339 * a negative dentry, but if anybody else is
1340 * currently using the dentry or the inode
1341 * we can't do that and we fall back on removing
1342 * it from the hash queues and waiting for
1343 * it to be deleted later when it has no users
1344 */
1345
1346/**
1347 * d_delete - delete a dentry
1348 * @dentry: The dentry to delete
1349 *
1350 * Turn the dentry into a negative dentry if possible, otherwise
1351 * remove it from the hash queues so it can be deleted later
1352 */
1353
1354void d_delete(struct dentry * dentry)
1355{
John McCutchan7a91bf72005-08-08 13:52:16 -04001356 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 /*
1358 * Are we the only user?
1359 */
1360 spin_lock(&dcache_lock);
1361 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001362 isdir = S_ISDIR(dentry->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001363 if (atomic_read(&dentry->d_count) == 1) {
1364 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001365 fsnotify_nameremove(dentry, isdir);
Amy Griffis7a2bd3f2006-03-31 02:30:54 -08001366
1367 /* remove this and other inotify debug checks after 2.6.18 */
1368 dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 return;
1370 }
1371
1372 if (!d_unhashed(dentry))
1373 __d_drop(dentry);
1374
1375 spin_unlock(&dentry->d_lock);
1376 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001377
1378 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001379}
1380
1381static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1382{
1383
1384 entry->d_flags &= ~DCACHE_UNHASHED;
1385 hlist_add_head_rcu(&entry->d_hash, list);
1386}
1387
David Howells770bfad2006-08-22 20:06:07 -04001388static void _d_rehash(struct dentry * entry)
1389{
1390 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1391}
1392
Linus Torvalds1da177e2005-04-16 15:20:36 -07001393/**
1394 * d_rehash - add an entry back to the hash
1395 * @entry: dentry to add to the hash
1396 *
1397 * Adds a dentry to the hash according to its name.
1398 */
1399
1400void d_rehash(struct dentry * entry)
1401{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 spin_lock(&dcache_lock);
1403 spin_lock(&entry->d_lock);
David Howells770bfad2006-08-22 20:06:07 -04001404 _d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001405 spin_unlock(&entry->d_lock);
1406 spin_unlock(&dcache_lock);
1407}
1408
1409#define do_switch(x,y) do { \
1410 __typeof__ (x) __tmp = x; \
1411 x = y; y = __tmp; } while (0)
1412
1413/*
1414 * When switching names, the actual string doesn't strictly have to
1415 * be preserved in the target - because we're dropping the target
1416 * anyway. As such, we can just do a simple memcpy() to copy over
1417 * the new name before we switch.
1418 *
1419 * Note that we have to be a lot more careful about getting the hash
1420 * switched - we have to switch the hash value properly even if it
1421 * then no longer matches the actual (corrupted) string of the target.
1422 * The hash value has to match the hash queue that the dentry is on..
1423 */
1424static void switch_names(struct dentry *dentry, struct dentry *target)
1425{
1426 if (dname_external(target)) {
1427 if (dname_external(dentry)) {
1428 /*
1429 * Both external: swap the pointers
1430 */
1431 do_switch(target->d_name.name, dentry->d_name.name);
1432 } else {
1433 /*
1434 * dentry:internal, target:external. Steal target's
1435 * storage and make target internal.
1436 */
1437 dentry->d_name.name = target->d_name.name;
1438 target->d_name.name = target->d_iname;
1439 }
1440 } else {
1441 if (dname_external(dentry)) {
1442 /*
1443 * dentry:external, target:internal. Give dentry's
1444 * storage to target and make dentry internal
1445 */
1446 memcpy(dentry->d_iname, target->d_name.name,
1447 target->d_name.len + 1);
1448 target->d_name.name = dentry->d_name.name;
1449 dentry->d_name.name = dentry->d_iname;
1450 } else {
1451 /*
1452 * Both are internal. Just copy target to dentry
1453 */
1454 memcpy(dentry->d_iname, target->d_name.name,
1455 target->d_name.len + 1);
1456 }
1457 }
1458}
1459
1460/*
1461 * We cannibalize "target" when moving dentry on top of it,
1462 * because it's going to be thrown away anyway. We could be more
1463 * polite about it, though.
1464 *
1465 * This forceful removal will result in ugly /proc output if
1466 * somebody holds a file open that got deleted due to a rename.
1467 * We could be nicer about the deleted file, and let it show
1468 * up under the name it got deleted rather than the name that
1469 * deleted it.
1470 */
1471
Trond Myklebust9eaef272006-10-21 10:24:20 -07001472/*
1473 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001474 * @dentry: entry to move
1475 * @target: new dentry
1476 *
1477 * Update the dcache to reflect the move of a file name. Negative
1478 * dcache entries should not be moved in this way.
1479 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001480static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001481{
1482 struct hlist_head *list;
1483
1484 if (!dentry->d_inode)
1485 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1486
Linus Torvalds1da177e2005-04-16 15:20:36 -07001487 write_seqlock(&rename_lock);
1488 /*
1489 * XXXX: do we really need to take target->d_lock?
1490 */
1491 if (target < dentry) {
1492 spin_lock(&target->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001493 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001494 } else {
1495 spin_lock(&dentry->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001496 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001497 }
1498
1499 /* Move the dentry to the target hash queue, if on different bucket */
1500 if (dentry->d_flags & DCACHE_UNHASHED)
1501 goto already_unhashed;
1502
1503 hlist_del_rcu(&dentry->d_hash);
1504
1505already_unhashed:
1506 list = d_hash(target->d_parent, target->d_name.hash);
1507 __d_rehash(dentry, list);
1508
1509 /* Unhash the target: dput() will then get rid of it */
1510 __d_drop(target);
1511
Eric Dumazet5160ee62006-01-08 01:03:32 -08001512 list_del(&dentry->d_u.d_child);
1513 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514
1515 /* Switch the names.. */
1516 switch_names(dentry, target);
1517 do_switch(dentry->d_name.len, target->d_name.len);
1518 do_switch(dentry->d_name.hash, target->d_name.hash);
1519
1520 /* ... and switch the parents */
1521 if (IS_ROOT(dentry)) {
1522 dentry->d_parent = target->d_parent;
1523 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001524 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525 } else {
1526 do_switch(dentry->d_parent, target->d_parent);
1527
1528 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001529 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001530 }
1531
Eric Dumazet5160ee62006-01-08 01:03:32 -08001532 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001534 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 spin_unlock(&dentry->d_lock);
1536 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001537}
1538
1539/**
1540 * d_move - move a dentry
1541 * @dentry: entry to move
1542 * @target: new dentry
1543 *
1544 * Update the dcache to reflect the move of a file name. Negative
1545 * dcache entries should not be moved in this way.
1546 */
1547
1548void d_move(struct dentry * dentry, struct dentry * target)
1549{
1550 spin_lock(&dcache_lock);
1551 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001552 spin_unlock(&dcache_lock);
1553}
1554
David Howells770bfad2006-08-22 20:06:07 -04001555/*
Trond Myklebust9eaef272006-10-21 10:24:20 -07001556 * Helper that returns 1 if p1 is a parent of p2, else 0
1557 */
1558static int d_isparent(struct dentry *p1, struct dentry *p2)
1559{
1560 struct dentry *p;
1561
1562 for (p = p2; p->d_parent != p; p = p->d_parent) {
1563 if (p->d_parent == p1)
1564 return 1;
1565 }
1566 return 0;
1567}
1568
1569/*
1570 * This helper attempts to cope with remotely renamed directories
1571 *
1572 * It assumes that the caller is already holding
1573 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1574 *
1575 * Note: If ever the locking in lock_rename() changes, then please
1576 * remember to update this too...
1577 *
1578 * On return, dcache_lock will have been unlocked.
1579 */
1580static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1581{
1582 struct mutex *m1 = NULL, *m2 = NULL;
1583 struct dentry *ret;
1584
1585 /* If alias and dentry share a parent, then no extra locks required */
1586 if (alias->d_parent == dentry->d_parent)
1587 goto out_unalias;
1588
1589 /* Check for loops */
1590 ret = ERR_PTR(-ELOOP);
1591 if (d_isparent(alias, dentry))
1592 goto out_err;
1593
1594 /* See lock_rename() */
1595 ret = ERR_PTR(-EBUSY);
1596 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1597 goto out_err;
1598 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1599 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1600 goto out_err;
1601 m2 = &alias->d_parent->d_inode->i_mutex;
1602out_unalias:
1603 d_move_locked(alias, dentry);
1604 ret = alias;
1605out_err:
1606 spin_unlock(&dcache_lock);
1607 if (m2)
1608 mutex_unlock(m2);
1609 if (m1)
1610 mutex_unlock(m1);
1611 return ret;
1612}
1613
1614/*
David Howells770bfad2006-08-22 20:06:07 -04001615 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1616 * named dentry in place of the dentry to be replaced.
1617 */
1618static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1619{
1620 struct dentry *dparent, *aparent;
1621
1622 switch_names(dentry, anon);
1623 do_switch(dentry->d_name.len, anon->d_name.len);
1624 do_switch(dentry->d_name.hash, anon->d_name.hash);
1625
1626 dparent = dentry->d_parent;
1627 aparent = anon->d_parent;
1628
1629 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1630 list_del(&dentry->d_u.d_child);
1631 if (!IS_ROOT(dentry))
1632 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1633 else
1634 INIT_LIST_HEAD(&dentry->d_u.d_child);
1635
1636 anon->d_parent = (dparent == dentry) ? anon : dparent;
1637 list_del(&anon->d_u.d_child);
1638 if (!IS_ROOT(anon))
1639 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1640 else
1641 INIT_LIST_HEAD(&anon->d_u.d_child);
1642
1643 anon->d_flags &= ~DCACHE_DISCONNECTED;
1644}
1645
1646/**
1647 * d_materialise_unique - introduce an inode into the tree
1648 * @dentry: candidate dentry
1649 * @inode: inode to bind to the dentry, to which aliases may be attached
1650 *
1651 * Introduces an dentry into the tree, substituting an extant disconnected
1652 * root directory alias in its place if there is one
1653 */
1654struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1655{
Trond Myklebust9eaef272006-10-21 10:24:20 -07001656 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04001657
1658 BUG_ON(!d_unhashed(dentry));
1659
1660 spin_lock(&dcache_lock);
1661
1662 if (!inode) {
1663 actual = dentry;
1664 dentry->d_inode = NULL;
1665 goto found_lock;
1666 }
1667
Trond Myklebust9eaef272006-10-21 10:24:20 -07001668 if (S_ISDIR(inode->i_mode)) {
1669 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04001670
Trond Myklebust9eaef272006-10-21 10:24:20 -07001671 /* Does an aliased dentry already exist? */
1672 alias = __d_find_alias(inode, 0);
1673 if (alias) {
1674 actual = alias;
1675 /* Is this an anonymous mountpoint that we could splice
1676 * into our tree? */
1677 if (IS_ROOT(alias)) {
1678 spin_lock(&alias->d_lock);
1679 __d_materialise_dentry(dentry, alias);
1680 __d_drop(alias);
1681 goto found;
1682 }
1683 /* Nope, but we must(!) avoid directory aliasing */
1684 actual = __d_unalias(dentry, alias);
1685 if (IS_ERR(actual))
1686 dput(alias);
1687 goto out_nolock;
1688 }
David Howells770bfad2006-08-22 20:06:07 -04001689 }
1690
1691 /* Add a unique reference */
1692 actual = __d_instantiate_unique(dentry, inode);
1693 if (!actual)
1694 actual = dentry;
1695 else if (unlikely(!d_unhashed(actual)))
1696 goto shouldnt_be_hashed;
1697
1698found_lock:
1699 spin_lock(&actual->d_lock);
1700found:
1701 _d_rehash(actual);
1702 spin_unlock(&actual->d_lock);
1703 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001704out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04001705 if (actual == dentry) {
1706 security_d_instantiate(dentry, inode);
1707 return NULL;
1708 }
1709
1710 iput(inode);
1711 return actual;
1712
David Howells770bfad2006-08-22 20:06:07 -04001713shouldnt_be_hashed:
1714 spin_unlock(&dcache_lock);
1715 BUG();
1716 goto shouldnt_be_hashed;
1717}
1718
Linus Torvalds1da177e2005-04-16 15:20:36 -07001719/**
1720 * d_path - return the path of a dentry
1721 * @dentry: dentry to report
1722 * @vfsmnt: vfsmnt to which the dentry belongs
1723 * @root: root dentry
1724 * @rootmnt: vfsmnt to which the root dentry belongs
1725 * @buffer: buffer to return value in
1726 * @buflen: buffer length
1727 *
1728 * Convert a dentry into an ASCII path name. If the entry has been deleted
1729 * the string " (deleted)" is appended. Note that this is ambiguous.
1730 *
1731 * Returns the buffer or an error code if the path was too long.
1732 *
1733 * "buflen" should be positive. Caller holds the dcache_lock.
1734 */
1735static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1736 struct dentry *root, struct vfsmount *rootmnt,
1737 char *buffer, int buflen)
1738{
1739 char * end = buffer+buflen;
1740 char * retval;
1741 int namelen;
1742
1743 *--end = '\0';
1744 buflen--;
1745 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1746 buflen -= 10;
1747 end -= 10;
1748 if (buflen < 0)
1749 goto Elong;
1750 memcpy(end, " (deleted)", 10);
1751 }
1752
1753 if (buflen < 1)
1754 goto Elong;
1755 /* Get '/' right */
1756 retval = end-1;
1757 *retval = '/';
1758
1759 for (;;) {
1760 struct dentry * parent;
1761
1762 if (dentry == root && vfsmnt == rootmnt)
1763 break;
1764 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1765 /* Global root? */
1766 spin_lock(&vfsmount_lock);
1767 if (vfsmnt->mnt_parent == vfsmnt) {
1768 spin_unlock(&vfsmount_lock);
1769 goto global_root;
1770 }
1771 dentry = vfsmnt->mnt_mountpoint;
1772 vfsmnt = vfsmnt->mnt_parent;
1773 spin_unlock(&vfsmount_lock);
1774 continue;
1775 }
1776 parent = dentry->d_parent;
1777 prefetch(parent);
1778 namelen = dentry->d_name.len;
1779 buflen -= namelen + 1;
1780 if (buflen < 0)
1781 goto Elong;
1782 end -= namelen;
1783 memcpy(end, dentry->d_name.name, namelen);
1784 *--end = '/';
1785 retval = end;
1786 dentry = parent;
1787 }
1788
1789 return retval;
1790
1791global_root:
1792 namelen = dentry->d_name.len;
1793 buflen -= namelen;
1794 if (buflen < 0)
1795 goto Elong;
1796 retval -= namelen-1; /* hit the slash */
1797 memcpy(retval, dentry->d_name.name, namelen);
1798 return retval;
1799Elong:
1800 return ERR_PTR(-ENAMETOOLONG);
1801}
1802
1803/* write full pathname into buffer and return start of pathname */
1804char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1805 char *buf, int buflen)
1806{
1807 char *res;
1808 struct vfsmount *rootmnt;
1809 struct dentry *root;
1810
1811 read_lock(&current->fs->lock);
1812 rootmnt = mntget(current->fs->rootmnt);
1813 root = dget(current->fs->root);
1814 read_unlock(&current->fs->lock);
1815 spin_lock(&dcache_lock);
1816 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1817 spin_unlock(&dcache_lock);
1818 dput(root);
1819 mntput(rootmnt);
1820 return res;
1821}
1822
1823/*
1824 * NOTE! The user-level library version returns a
1825 * character pointer. The kernel system call just
1826 * returns the length of the buffer filled (which
1827 * includes the ending '\0' character), or a negative
1828 * error value. So libc would do something like
1829 *
1830 * char *getcwd(char * buf, size_t size)
1831 * {
1832 * int retval;
1833 *
1834 * retval = sys_getcwd(buf, size);
1835 * if (retval >= 0)
1836 * return buf;
1837 * errno = -retval;
1838 * return NULL;
1839 * }
1840 */
1841asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1842{
1843 int error;
1844 struct vfsmount *pwdmnt, *rootmnt;
1845 struct dentry *pwd, *root;
1846 char *page = (char *) __get_free_page(GFP_USER);
1847
1848 if (!page)
1849 return -ENOMEM;
1850
1851 read_lock(&current->fs->lock);
1852 pwdmnt = mntget(current->fs->pwdmnt);
1853 pwd = dget(current->fs->pwd);
1854 rootmnt = mntget(current->fs->rootmnt);
1855 root = dget(current->fs->root);
1856 read_unlock(&current->fs->lock);
1857
1858 error = -ENOENT;
1859 /* Has the current directory has been unlinked? */
1860 spin_lock(&dcache_lock);
1861 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1862 unsigned long len;
1863 char * cwd;
1864
1865 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1866 spin_unlock(&dcache_lock);
1867
1868 error = PTR_ERR(cwd);
1869 if (IS_ERR(cwd))
1870 goto out;
1871
1872 error = -ERANGE;
1873 len = PAGE_SIZE + page - cwd;
1874 if (len <= size) {
1875 error = len;
1876 if (copy_to_user(buf, cwd, len))
1877 error = -EFAULT;
1878 }
1879 } else
1880 spin_unlock(&dcache_lock);
1881
1882out:
1883 dput(pwd);
1884 mntput(pwdmnt);
1885 dput(root);
1886 mntput(rootmnt);
1887 free_page((unsigned long) page);
1888 return error;
1889}
1890
1891/*
1892 * Test whether new_dentry is a subdirectory of old_dentry.
1893 *
1894 * Trivially implemented using the dcache structure
1895 */
1896
1897/**
1898 * is_subdir - is new dentry a subdirectory of old_dentry
1899 * @new_dentry: new dentry
1900 * @old_dentry: old dentry
1901 *
1902 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1903 * Returns 0 otherwise.
1904 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1905 */
1906
1907int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1908{
1909 int result;
1910 struct dentry * saved = new_dentry;
1911 unsigned long seq;
1912
1913 /* need rcu_readlock to protect against the d_parent trashing due to
1914 * d_move
1915 */
1916 rcu_read_lock();
1917 do {
1918 /* for restarting inner loop in case of seq retry */
1919 new_dentry = saved;
1920 result = 0;
1921 seq = read_seqbegin(&rename_lock);
1922 for (;;) {
1923 if (new_dentry != old_dentry) {
1924 struct dentry * parent = new_dentry->d_parent;
1925 if (parent == new_dentry)
1926 break;
1927 new_dentry = parent;
1928 continue;
1929 }
1930 result = 1;
1931 break;
1932 }
1933 } while (read_seqretry(&rename_lock, seq));
1934 rcu_read_unlock();
1935
1936 return result;
1937}
1938
1939void d_genocide(struct dentry *root)
1940{
1941 struct dentry *this_parent = root;
1942 struct list_head *next;
1943
1944 spin_lock(&dcache_lock);
1945repeat:
1946 next = this_parent->d_subdirs.next;
1947resume:
1948 while (next != &this_parent->d_subdirs) {
1949 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001950 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 next = tmp->next;
1952 if (d_unhashed(dentry)||!dentry->d_inode)
1953 continue;
1954 if (!list_empty(&dentry->d_subdirs)) {
1955 this_parent = dentry;
1956 goto repeat;
1957 }
1958 atomic_dec(&dentry->d_count);
1959 }
1960 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001961 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001962 atomic_dec(&this_parent->d_count);
1963 this_parent = this_parent->d_parent;
1964 goto resume;
1965 }
1966 spin_unlock(&dcache_lock);
1967}
1968
1969/**
1970 * find_inode_number - check for dentry with name
1971 * @dir: directory to check
1972 * @name: Name to find.
1973 *
1974 * Check whether a dentry already exists for the given name,
1975 * and return the inode number if it has an inode. Otherwise
1976 * 0 is returned.
1977 *
1978 * This routine is used to post-process directory listings for
1979 * filesystems using synthetic inode numbers, and is necessary
1980 * to keep getcwd() working.
1981 */
1982
1983ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1984{
1985 struct dentry * dentry;
1986 ino_t ino = 0;
1987
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001988 dentry = d_hash_and_lookup(dir, name);
1989 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (dentry->d_inode)
1991 ino = dentry->d_inode->i_ino;
1992 dput(dentry);
1993 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001994 return ino;
1995}
1996
1997static __initdata unsigned long dhash_entries;
1998static int __init set_dhash_entries(char *str)
1999{
2000 if (!str)
2001 return 0;
2002 dhash_entries = simple_strtoul(str, &str, 0);
2003 return 1;
2004}
2005__setup("dhash_entries=", set_dhash_entries);
2006
2007static void __init dcache_init_early(void)
2008{
2009 int loop;
2010
2011 /* If hashes are distributed across NUMA nodes, defer
2012 * hash allocation until vmalloc space is available.
2013 */
2014 if (hashdist)
2015 return;
2016
2017 dentry_hashtable =
2018 alloc_large_system_hash("Dentry cache",
2019 sizeof(struct hlist_head),
2020 dhash_entries,
2021 13,
2022 HASH_EARLY,
2023 &d_hash_shift,
2024 &d_hash_mask,
2025 0);
2026
2027 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2028 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2029}
2030
2031static void __init dcache_init(unsigned long mempages)
2032{
2033 int loop;
2034
2035 /*
2036 * A constructor could be added for stable state like the lists,
2037 * but it is probably not worth it because of the cache nature
2038 * of the dcache.
2039 */
2040 dentry_cache = kmem_cache_create("dentry_cache",
2041 sizeof(struct dentry),
2042 0,
Paul Jacksonb0196002006-03-24 03:16:09 -08002043 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
2044 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -07002045 NULL, NULL);
2046
2047 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
2048
2049 /* Hash may have been set up in dcache_init_early */
2050 if (!hashdist)
2051 return;
2052
2053 dentry_hashtable =
2054 alloc_large_system_hash("Dentry cache",
2055 sizeof(struct hlist_head),
2056 dhash_entries,
2057 13,
2058 0,
2059 &d_hash_shift,
2060 &d_hash_mask,
2061 0);
2062
2063 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2064 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2065}
2066
2067/* SLAB cache for __getname() consumers */
Eric Dumazetfa3536c2006-03-26 01:37:24 -08002068kmem_cache_t *names_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069
2070/* SLAB cache for file structures */
Eric Dumazetfa3536c2006-03-26 01:37:24 -08002071kmem_cache_t *filp_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002072
2073EXPORT_SYMBOL(d_genocide);
2074
Linus Torvalds1da177e2005-04-16 15:20:36 -07002075void __init vfs_caches_init_early(void)
2076{
2077 dcache_init_early();
2078 inode_init_early();
2079}
2080
2081void __init vfs_caches_init(unsigned long mempages)
2082{
2083 unsigned long reserve;
2084
2085 /* Base hash sizes on available memory, with a reserve equal to
2086 150% of current kernel size */
2087
2088 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2089 mempages -= reserve;
2090
2091 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2092 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
2093
2094 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
Dipankar Sarma529bf6b2006-03-07 21:55:35 -08002095 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 dcache_init(mempages);
2098 inode_init(mempages);
2099 files_init(mempages);
2100 mnt_init(mempages);
2101 bdev_cache_init();
2102 chrdev_init();
2103}
2104
2105EXPORT_SYMBOL(d_alloc);
2106EXPORT_SYMBOL(d_alloc_anon);
2107EXPORT_SYMBOL(d_alloc_root);
2108EXPORT_SYMBOL(d_delete);
2109EXPORT_SYMBOL(d_find_alias);
2110EXPORT_SYMBOL(d_instantiate);
2111EXPORT_SYMBOL(d_invalidate);
2112EXPORT_SYMBOL(d_lookup);
2113EXPORT_SYMBOL(d_move);
David Howells770bfad2006-08-22 20:06:07 -04002114EXPORT_SYMBOL_GPL(d_materialise_unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002115EXPORT_SYMBOL(d_path);
2116EXPORT_SYMBOL(d_prune_aliases);
2117EXPORT_SYMBOL(d_rehash);
2118EXPORT_SYMBOL(d_splice_alias);
2119EXPORT_SYMBOL(d_validate);
2120EXPORT_SYMBOL(dget_locked);
2121EXPORT_SYMBOL(dput);
2122EXPORT_SYMBOL(find_inode_number);
2123EXPORT_SYMBOL(have_submounts);
2124EXPORT_SYMBOL(names_cachep);
2125EXPORT_SYMBOL(shrink_dcache_parent);
2126EXPORT_SYMBOL(shrink_dcache_sb);