blob: 385f5dbc4b0cc2ab36a76b2e747e9070930c384c [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
17#include <linux/config.h>
18#include <linux/syscalls.h>
19#include <linux/string.h>
20#include <linux/mm.h>
21#include <linux/fs.h>
John McCutchan7a91bf72005-08-08 13:52:16 -040022#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
24#include <linux/init.h>
25#include <linux/smp_lock.h>
26#include <linux/hash.h>
27#include <linux/cache.h>
28#include <linux/module.h>
29#include <linux/mount.h>
30#include <linux/file.h>
31#include <asm/uaccess.h>
32#include <linux/security.h>
33#include <linux/seqlock.h>
34#include <linux/swap.h>
35#include <linux/bootmem.h>
36
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);
Adrian Bunk75c96f82005-05-05 16:16:09 -070042static seqlock_t rename_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
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 *
294 * If the inode has a DCACHE_DISCONNECTED alias, then prefer
295 * any other hashed alias over that one unless @want_discon is set,
296 * in which case only return a DCACHE_DISCONNECTED alias.
297 */
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)) {
312 if (alias->d_flags & DCACHE_DISCONNECTED)
313 discon_alias = alias;
314 else if (!want_discon) {
315 __dget_locked(alias);
316 return alias;
317 }
318 }
319 }
320 if (discon_alias)
321 __dget_locked(discon_alias);
322 return discon_alias;
323}
324
325struct dentry * d_find_alias(struct inode *inode)
326{
David Howells214fda12006-03-25 03:06:36 -0800327 struct dentry *de = NULL;
328
329 if (!list_empty(&inode->i_dentry)) {
330 spin_lock(&dcache_lock);
331 de = __d_find_alias(inode, 0);
332 spin_unlock(&dcache_lock);
333 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 return de;
335}
336
337/*
338 * Try to kill dentries associated with this inode.
339 * WARNING: you must own a reference to inode.
340 */
341void d_prune_aliases(struct inode *inode)
342{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700343 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344restart:
345 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700346 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347 spin_lock(&dentry->d_lock);
348 if (!atomic_read(&dentry->d_count)) {
349 __dget_locked(dentry);
350 __d_drop(dentry);
351 spin_unlock(&dentry->d_lock);
352 spin_unlock(&dcache_lock);
353 dput(dentry);
354 goto restart;
355 }
356 spin_unlock(&dentry->d_lock);
357 }
358 spin_unlock(&dcache_lock);
359}
360
361/*
362 * Throw away a dentry - free the inode, dput the parent.
363 * This requires that the LRU list has already been
364 * removed.
365 * Called with dcache_lock, drops it and then regains.
366 */
367static inline void prune_one_dentry(struct dentry * dentry)
368{
369 struct dentry * parent;
370
371 __d_drop(dentry);
Eric Dumazet5160ee62006-01-08 01:03:32 -0800372 list_del(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700373 dentry_stat.nr_dentry--; /* For d_free, below */
374 dentry_iput(dentry);
375 parent = dentry->d_parent;
376 d_free(dentry);
377 if (parent != dentry)
378 dput(parent);
379 spin_lock(&dcache_lock);
380}
381
382/**
383 * prune_dcache - shrink the dcache
384 * @count: number of entries to try and free
NeilBrown0feae5c2006-06-22 14:47:28 -0700385 * @sb: if given, ignore dentries for other superblocks
386 * which are being unmounted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 *
388 * Shrink the dcache. This is done when we need
389 * more memory, or simply when we need to unmount
390 * something (at which point we need to unuse
391 * all dentries).
392 *
393 * This function may fail to free any resources if
394 * all the dentries are in use.
395 */
396
NeilBrown0feae5c2006-06-22 14:47:28 -0700397static void prune_dcache(int count, struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700398{
399 spin_lock(&dcache_lock);
400 for (; count ; count--) {
401 struct dentry *dentry;
402 struct list_head *tmp;
NeilBrown0feae5c2006-06-22 14:47:28 -0700403 struct rw_semaphore *s_umount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404
405 cond_resched_lock(&dcache_lock);
406
407 tmp = dentry_unused.prev;
NeilBrown0feae5c2006-06-22 14:47:28 -0700408 if (unlikely(sb)) {
409 /* Try to find a dentry for this sb, but don't try
410 * too hard, if they aren't near the tail they will
411 * be moved down again soon
412 */
413 int skip = count;
414 while (skip && tmp != &dentry_unused &&
415 list_entry(tmp, struct dentry, d_lru)->d_sb != sb) {
416 skip--;
417 tmp = tmp->prev;
418 }
419 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 if (tmp == &dentry_unused)
421 break;
422 list_del_init(tmp);
423 prefetch(dentry_unused.prev);
424 dentry_stat.nr_unused--;
425 dentry = list_entry(tmp, struct dentry, d_lru);
426
427 spin_lock(&dentry->d_lock);
428 /*
429 * We found an inuse dentry which was not removed from
430 * dentry_unused because of laziness during lookup. Do not free
431 * it - just keep it off the dentry_unused list.
432 */
433 if (atomic_read(&dentry->d_count)) {
434 spin_unlock(&dentry->d_lock);
435 continue;
436 }
437 /* If the dentry was recently referenced, don't free it. */
438 if (dentry->d_flags & DCACHE_REFERENCED) {
439 dentry->d_flags &= ~DCACHE_REFERENCED;
440 list_add(&dentry->d_lru, &dentry_unused);
441 dentry_stat.nr_unused++;
442 spin_unlock(&dentry->d_lock);
443 continue;
444 }
NeilBrown0feae5c2006-06-22 14:47:28 -0700445 /*
446 * If the dentry is not DCACHED_REFERENCED, it is time
447 * to remove it from the dcache, provided the super block is
448 * NULL (which means we are trying to reclaim memory)
449 * or this dentry belongs to the same super block that
450 * we want to shrink.
451 */
452 /*
453 * If this dentry is for "my" filesystem, then I can prune it
454 * without taking the s_umount lock (I already hold it).
455 */
456 if (sb && dentry->d_sb == sb) {
457 prune_one_dentry(dentry);
458 continue;
459 }
460 /*
461 * ...otherwise we need to be sure this filesystem isn't being
462 * unmounted, otherwise we could race with
463 * generic_shutdown_super(), and end up holding a reference to
464 * an inode while the filesystem is unmounted.
465 * So we try to get s_umount, and make sure s_root isn't NULL.
466 * (Take a local copy of s_umount to avoid a use-after-free of
467 * `dentry').
468 */
469 s_umount = &dentry->d_sb->s_umount;
470 if (down_read_trylock(s_umount)) {
471 if (dentry->d_sb->s_root != NULL) {
472 prune_one_dentry(dentry);
473 up_read(s_umount);
474 continue;
475 }
476 up_read(s_umount);
477 }
478 spin_unlock(&dentry->d_lock);
479 /* Cannot remove the first dentry, and it isn't appropriate
480 * to move it to the head of the list, so give up, and try
481 * later
482 */
483 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700484 }
485 spin_unlock(&dcache_lock);
486}
487
488/*
489 * Shrink the dcache for the specified super block.
490 * This allows us to unmount a device without disturbing
491 * the dcache for the other devices.
492 *
493 * This implementation makes just two traversals of the
494 * unused list. On the first pass we move the selected
495 * dentries to the most recent end, and on the second
496 * pass we free them. The second pass must restart after
497 * each dput(), but since the target dentries are all at
498 * the end, it's really just a single traversal.
499 */
500
501/**
502 * shrink_dcache_sb - shrink dcache for a superblock
503 * @sb: superblock
504 *
505 * Shrink the dcache for the specified super block. This
506 * is used to free the dcache before unmounting a file
507 * system
508 */
509
510void shrink_dcache_sb(struct super_block * sb)
511{
512 struct list_head *tmp, *next;
513 struct dentry *dentry;
514
515 /*
516 * Pass one ... move the dentries for the specified
517 * superblock to the most recent end of the unused list.
518 */
519 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700520 list_for_each_safe(tmp, next, &dentry_unused) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521 dentry = list_entry(tmp, struct dentry, d_lru);
522 if (dentry->d_sb != sb)
523 continue;
524 list_del(tmp);
525 list_add(tmp, &dentry_unused);
526 }
527
528 /*
529 * Pass two ... free the dentries for this superblock.
530 */
531repeat:
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700532 list_for_each_safe(tmp, next, &dentry_unused) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 dentry = list_entry(tmp, struct dentry, d_lru);
534 if (dentry->d_sb != sb)
535 continue;
536 dentry_stat.nr_unused--;
537 list_del_init(tmp);
538 spin_lock(&dentry->d_lock);
539 if (atomic_read(&dentry->d_count)) {
540 spin_unlock(&dentry->d_lock);
541 continue;
542 }
543 prune_one_dentry(dentry);
Kirill Korotaev2ab13462006-03-25 03:07:45 -0800544 cond_resched_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 goto repeat;
546 }
547 spin_unlock(&dcache_lock);
548}
549
550/*
551 * Search for at least 1 mount point in the dentry's subdirs.
552 * We descend to the next level whenever the d_subdirs
553 * list is non-empty and continue searching.
554 */
555
556/**
557 * have_submounts - check for mounts over a dentry
558 * @parent: dentry to check.
559 *
560 * Return true if the parent or its subdirectories contain
561 * a mount point
562 */
563
564int have_submounts(struct dentry *parent)
565{
566 struct dentry *this_parent = parent;
567 struct list_head *next;
568
569 spin_lock(&dcache_lock);
570 if (d_mountpoint(parent))
571 goto positive;
572repeat:
573 next = this_parent->d_subdirs.next;
574resume:
575 while (next != &this_parent->d_subdirs) {
576 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800577 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 next = tmp->next;
579 /* Have we found a mount point ? */
580 if (d_mountpoint(dentry))
581 goto positive;
582 if (!list_empty(&dentry->d_subdirs)) {
583 this_parent = dentry;
584 goto repeat;
585 }
586 }
587 /*
588 * All done at this level ... ascend and resume the search.
589 */
590 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800591 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592 this_parent = this_parent->d_parent;
593 goto resume;
594 }
595 spin_unlock(&dcache_lock);
596 return 0; /* No mount points found in tree */
597positive:
598 spin_unlock(&dcache_lock);
599 return 1;
600}
601
602/*
603 * Search the dentry child list for the specified parent,
604 * and move any unused dentries to the end of the unused
605 * list for prune_dcache(). We descend to the next level
606 * whenever the d_subdirs list is non-empty and continue
607 * searching.
608 *
609 * It returns zero iff there are no unused children,
610 * otherwise it returns the number of children moved to
611 * the end of the unused list. This may not be the total
612 * number of unused children, because select_parent can
613 * drop the lock and return early due to latency
614 * constraints.
615 */
616static int select_parent(struct dentry * parent)
617{
618 struct dentry *this_parent = parent;
619 struct list_head *next;
620 int found = 0;
621
622 spin_lock(&dcache_lock);
623repeat:
624 next = this_parent->d_subdirs.next;
625resume:
626 while (next != &this_parent->d_subdirs) {
627 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800628 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629 next = tmp->next;
630
631 if (!list_empty(&dentry->d_lru)) {
632 dentry_stat.nr_unused--;
633 list_del_init(&dentry->d_lru);
634 }
635 /*
636 * move only zero ref count dentries to the end
637 * of the unused list for prune_dcache
638 */
639 if (!atomic_read(&dentry->d_count)) {
640 list_add(&dentry->d_lru, dentry_unused.prev);
641 dentry_stat.nr_unused++;
642 found++;
643 }
644
645 /*
646 * We can return to the caller if we have found some (this
647 * ensures forward progress). We'll be coming back to find
648 * the rest.
649 */
650 if (found && need_resched())
651 goto out;
652
653 /*
654 * Descend a level if the d_subdirs list is non-empty.
655 */
656 if (!list_empty(&dentry->d_subdirs)) {
657 this_parent = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700658 goto repeat;
659 }
660 }
661 /*
662 * All done at this level ... ascend and resume the search.
663 */
664 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800665 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700666 this_parent = this_parent->d_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667 goto resume;
668 }
669out:
670 spin_unlock(&dcache_lock);
671 return found;
672}
673
674/**
675 * shrink_dcache_parent - prune dcache
676 * @parent: parent of entries to prune
677 *
678 * Prune the dcache to remove unused children of the parent dentry.
679 */
680
681void shrink_dcache_parent(struct dentry * parent)
682{
683 int found;
684
685 while ((found = select_parent(parent)) != 0)
NeilBrown0feae5c2006-06-22 14:47:28 -0700686 prune_dcache(found, parent->d_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687}
688
689/**
690 * shrink_dcache_anon - further prune the cache
691 * @head: head of d_hash list of dentries to prune
692 *
693 * Prune the dentries that are anonymous
694 *
Paul E. McKenney665a7582005-11-07 00:59:17 -0800695 * parsing d_hash list does not hlist_for_each_entry_rcu() as it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700696 * done under dcache_lock.
697 *
698 */
NeilBrown0feae5c2006-06-22 14:47:28 -0700699void shrink_dcache_anon(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700700{
701 struct hlist_node *lp;
NeilBrown0feae5c2006-06-22 14:47:28 -0700702 struct hlist_head *head = &sb->s_anon;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 int found;
704 do {
705 found = 0;
706 spin_lock(&dcache_lock);
707 hlist_for_each(lp, head) {
708 struct dentry *this = hlist_entry(lp, struct dentry, d_hash);
709 if (!list_empty(&this->d_lru)) {
710 dentry_stat.nr_unused--;
711 list_del_init(&this->d_lru);
712 }
713
714 /*
715 * move only zero ref count dentries to the end
716 * of the unused list for prune_dcache
717 */
718 if (!atomic_read(&this->d_count)) {
719 list_add_tail(&this->d_lru, &dentry_unused);
720 dentry_stat.nr_unused++;
721 found++;
722 }
723 }
724 spin_unlock(&dcache_lock);
NeilBrown0feae5c2006-06-22 14:47:28 -0700725 prune_dcache(found, sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 } while(found);
727}
728
729/*
730 * Scan `nr' dentries and return the number which remain.
731 *
732 * We need to avoid reentering the filesystem if the caller is performing a
733 * GFP_NOFS allocation attempt. One example deadlock is:
734 *
735 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
736 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
737 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
738 *
739 * In this case we return -1 to tell the caller that we baled.
740 */
Al Viro27496a82005-10-21 03:20:48 -0400741static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742{
743 if (nr) {
744 if (!(gfp_mask & __GFP_FS))
745 return -1;
NeilBrown0feae5c2006-06-22 14:47:28 -0700746 prune_dcache(nr, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 }
748 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
749}
750
751/**
752 * d_alloc - allocate a dcache entry
753 * @parent: parent of entry to allocate
754 * @name: qstr of the name
755 *
756 * Allocates a dentry. It returns %NULL if there is insufficient memory
757 * available. On a success the dentry is returned. The name passed in is
758 * copied and the copy passed in may be reused after this call.
759 */
760
761struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
762{
763 struct dentry *dentry;
764 char *dname;
765
766 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
767 if (!dentry)
768 return NULL;
769
770 if (name->len > DNAME_INLINE_LEN-1) {
771 dname = kmalloc(name->len + 1, GFP_KERNEL);
772 if (!dname) {
773 kmem_cache_free(dentry_cache, dentry);
774 return NULL;
775 }
776 } else {
777 dname = dentry->d_iname;
778 }
779 dentry->d_name.name = dname;
780
781 dentry->d_name.len = name->len;
782 dentry->d_name.hash = name->hash;
783 memcpy(dname, name->name, name->len);
784 dname[name->len] = 0;
785
786 atomic_set(&dentry->d_count, 1);
787 dentry->d_flags = DCACHE_UNHASHED;
788 spin_lock_init(&dentry->d_lock);
789 dentry->d_inode = NULL;
790 dentry->d_parent = NULL;
791 dentry->d_sb = NULL;
792 dentry->d_op = NULL;
793 dentry->d_fsdata = NULL;
794 dentry->d_mounted = 0;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800795#ifdef CONFIG_PROFILING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700796 dentry->d_cookie = NULL;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800797#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 INIT_HLIST_NODE(&dentry->d_hash);
799 INIT_LIST_HEAD(&dentry->d_lru);
800 INIT_LIST_HEAD(&dentry->d_subdirs);
801 INIT_LIST_HEAD(&dentry->d_alias);
802
803 if (parent) {
804 dentry->d_parent = dget(parent);
805 dentry->d_sb = parent->d_sb;
806 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800807 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700808 }
809
810 spin_lock(&dcache_lock);
811 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -0800812 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700813 dentry_stat.nr_dentry++;
814 spin_unlock(&dcache_lock);
815
816 return dentry;
817}
818
819struct dentry *d_alloc_name(struct dentry *parent, const char *name)
820{
821 struct qstr q;
822
823 q.name = name;
824 q.len = strlen(name);
825 q.hash = full_name_hash(q.name, q.len);
826 return d_alloc(parent, &q);
827}
828
829/**
830 * d_instantiate - fill in inode information for a dentry
831 * @entry: dentry to complete
832 * @inode: inode to attach to this dentry
833 *
834 * Fill in inode information in the entry.
835 *
836 * This turns negative dentries into productive full members
837 * of society.
838 *
839 * NOTE! This assumes that the inode count has been incremented
840 * (or otherwise set) by the caller to indicate that it is now
841 * in use by the dcache.
842 */
843
844void d_instantiate(struct dentry *entry, struct inode * inode)
845{
Eric Sesterhenn28133c72006-03-26 18:25:39 +0200846 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 spin_lock(&dcache_lock);
848 if (inode)
849 list_add(&entry->d_alias, &inode->i_dentry);
850 entry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800851 fsnotify_d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700852 spin_unlock(&dcache_lock);
853 security_d_instantiate(entry, inode);
854}
855
856/**
857 * d_instantiate_unique - instantiate a non-aliased dentry
858 * @entry: dentry to instantiate
859 * @inode: inode to attach to this dentry
860 *
861 * Fill in inode information in the entry. On success, it returns NULL.
862 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -0800863 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864 *
865 * Note that in order to avoid conflicts with rename() etc, the caller
866 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -0800867 *
868 * This also assumes that the inode count has been incremented
869 * (or otherwise set) by the caller to indicate that it is now
870 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700871 */
872struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
873{
874 struct dentry *alias;
875 int len = entry->d_name.len;
876 const char *name = entry->d_name.name;
877 unsigned int hash = entry->d_name.hash;
878
879 BUG_ON(!list_empty(&entry->d_alias));
880 spin_lock(&dcache_lock);
881 if (!inode)
882 goto do_negative;
883 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
884 struct qstr *qstr = &alias->d_name;
885
886 if (qstr->hash != hash)
887 continue;
888 if (alias->d_parent != entry->d_parent)
889 continue;
890 if (qstr->len != len)
891 continue;
892 if (memcmp(qstr->name, name, len))
893 continue;
894 dget_locked(alias);
895 spin_unlock(&dcache_lock);
896 BUG_ON(!d_unhashed(alias));
Oleg Drokine866cfa2006-01-09 20:52:51 -0800897 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700898 return alias;
899 }
900 list_add(&entry->d_alias, &inode->i_dentry);
901do_negative:
902 entry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800903 fsnotify_d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700904 spin_unlock(&dcache_lock);
905 security_d_instantiate(entry, inode);
906 return NULL;
907}
908EXPORT_SYMBOL(d_instantiate_unique);
909
910/**
911 * d_alloc_root - allocate root dentry
912 * @root_inode: inode to allocate the root for
913 *
914 * Allocate a root ("/") dentry for the inode given. The inode is
915 * instantiated and returned. %NULL is returned if there is insufficient
916 * memory or the inode passed is %NULL.
917 */
918
919struct dentry * d_alloc_root(struct inode * root_inode)
920{
921 struct dentry *res = NULL;
922
923 if (root_inode) {
924 static const struct qstr name = { .name = "/", .len = 1 };
925
926 res = d_alloc(NULL, &name);
927 if (res) {
928 res->d_sb = root_inode->i_sb;
929 res->d_parent = res;
930 d_instantiate(res, root_inode);
931 }
932 }
933 return res;
934}
935
936static inline struct hlist_head *d_hash(struct dentry *parent,
937 unsigned long hash)
938{
939 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
940 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
941 return dentry_hashtable + (hash & D_HASHMASK);
942}
943
944/**
945 * d_alloc_anon - allocate an anonymous dentry
946 * @inode: inode to allocate the dentry for
947 *
948 * This is similar to d_alloc_root. It is used by filesystems when
949 * creating a dentry for a given inode, often in the process of
950 * mapping a filehandle to a dentry. The returned dentry may be
951 * anonymous, or may have a full name (if the inode was already
952 * in the cache). The file system may need to make further
953 * efforts to connect this dentry into the dcache properly.
954 *
955 * When called on a directory inode, we must ensure that
956 * the inode only ever has one dentry. If a dentry is
957 * found, that is returned instead of allocating a new one.
958 *
959 * On successful return, the reference to the inode has been transferred
960 * to the dentry. If %NULL is returned (indicating kmalloc failure),
961 * the reference on the inode has not been released.
962 */
963
964struct dentry * d_alloc_anon(struct inode *inode)
965{
966 static const struct qstr anonstring = { .name = "" };
967 struct dentry *tmp;
968 struct dentry *res;
969
970 if ((res = d_find_alias(inode))) {
971 iput(inode);
972 return res;
973 }
974
975 tmp = d_alloc(NULL, &anonstring);
976 if (!tmp)
977 return NULL;
978
979 tmp->d_parent = tmp; /* make sure dput doesn't croak */
980
981 spin_lock(&dcache_lock);
982 res = __d_find_alias(inode, 0);
983 if (!res) {
984 /* attach a disconnected dentry */
985 res = tmp;
986 tmp = NULL;
987 spin_lock(&res->d_lock);
988 res->d_sb = inode->i_sb;
989 res->d_parent = res;
990 res->d_inode = inode;
991 res->d_flags |= DCACHE_DISCONNECTED;
992 res->d_flags &= ~DCACHE_UNHASHED;
993 list_add(&res->d_alias, &inode->i_dentry);
994 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
995 spin_unlock(&res->d_lock);
996
997 inode = NULL; /* don't drop reference */
998 }
999 spin_unlock(&dcache_lock);
1000
1001 if (inode)
1002 iput(inode);
1003 if (tmp)
1004 dput(tmp);
1005 return res;
1006}
1007
1008
1009/**
1010 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1011 * @inode: the inode which may have a disconnected dentry
1012 * @dentry: a negative dentry which we want to point to the inode.
1013 *
1014 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1015 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1016 * and return it, else simply d_add the inode to the dentry and return NULL.
1017 *
1018 * This is needed in the lookup routine of any filesystem that is exportable
1019 * (via knfsd) so that we can build dcache paths to directories effectively.
1020 *
1021 * If a dentry was found and moved, then it is returned. Otherwise NULL
1022 * is returned. This matches the expected return value of ->lookup.
1023 *
1024 */
1025struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1026{
1027 struct dentry *new = NULL;
1028
1029 if (inode) {
1030 spin_lock(&dcache_lock);
1031 new = __d_find_alias(inode, 1);
1032 if (new) {
1033 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Pigginc32ccd82006-03-25 03:07:09 -08001034 fsnotify_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 spin_unlock(&dcache_lock);
1036 security_d_instantiate(new, inode);
1037 d_rehash(dentry);
1038 d_move(new, dentry);
1039 iput(inode);
1040 } else {
1041 /* d_instantiate takes dcache_lock, so we do it by hand */
1042 list_add(&dentry->d_alias, &inode->i_dentry);
1043 dentry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -08001044 fsnotify_d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 spin_unlock(&dcache_lock);
1046 security_d_instantiate(dentry, inode);
1047 d_rehash(dentry);
1048 }
1049 } else
1050 d_add(dentry, inode);
1051 return new;
1052}
1053
1054
1055/**
1056 * d_lookup - search for a dentry
1057 * @parent: parent dentry
1058 * @name: qstr of name we wish to find
1059 *
1060 * Searches the children of the parent dentry for the name in question. If
1061 * the dentry is found its reference count is incremented and the dentry
1062 * is returned. The caller must use d_put to free the entry when it has
1063 * finished using it. %NULL is returned on failure.
1064 *
1065 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1066 * Memory barriers are used while updating and doing lockless traversal.
1067 * To avoid races with d_move while rename is happening, d_lock is used.
1068 *
1069 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1070 * and name pointer in one structure pointed by d_qstr.
1071 *
1072 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1073 * lookup is going on.
1074 *
1075 * dentry_unused list is not updated even if lookup finds the required dentry
1076 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1077 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1078 * acquisition.
1079 *
1080 * d_lookup() is protected against the concurrent renames in some unrelated
1081 * directory using the seqlockt_t rename_lock.
1082 */
1083
1084struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1085{
1086 struct dentry * dentry = NULL;
1087 unsigned long seq;
1088
1089 do {
1090 seq = read_seqbegin(&rename_lock);
1091 dentry = __d_lookup(parent, name);
1092 if (dentry)
1093 break;
1094 } while (read_seqretry(&rename_lock, seq));
1095 return dentry;
1096}
1097
1098struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1099{
1100 unsigned int len = name->len;
1101 unsigned int hash = name->hash;
1102 const unsigned char *str = name->name;
1103 struct hlist_head *head = d_hash(parent,hash);
1104 struct dentry *found = NULL;
1105 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001106 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107
1108 rcu_read_lock();
1109
Paul E. McKenney665a7582005-11-07 00:59:17 -08001110 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 struct qstr *qstr;
1112
Linus Torvalds1da177e2005-04-16 15:20:36 -07001113 if (dentry->d_name.hash != hash)
1114 continue;
1115 if (dentry->d_parent != parent)
1116 continue;
1117
1118 spin_lock(&dentry->d_lock);
1119
1120 /*
1121 * Recheck the dentry after taking the lock - d_move may have
1122 * changed things. Don't bother checking the hash because we're
1123 * about to compare the whole name anyway.
1124 */
1125 if (dentry->d_parent != parent)
1126 goto next;
1127
1128 /*
1129 * It is safe to compare names since d_move() cannot
1130 * change the qstr (protected by d_lock).
1131 */
1132 qstr = &dentry->d_name;
1133 if (parent->d_op && parent->d_op->d_compare) {
1134 if (parent->d_op->d_compare(parent, qstr, name))
1135 goto next;
1136 } else {
1137 if (qstr->len != len)
1138 goto next;
1139 if (memcmp(qstr->name, str, len))
1140 goto next;
1141 }
1142
1143 if (!d_unhashed(dentry)) {
1144 atomic_inc(&dentry->d_count);
1145 found = dentry;
1146 }
1147 spin_unlock(&dentry->d_lock);
1148 break;
1149next:
1150 spin_unlock(&dentry->d_lock);
1151 }
1152 rcu_read_unlock();
1153
1154 return found;
1155}
1156
1157/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001158 * d_hash_and_lookup - hash the qstr then search for a dentry
1159 * @dir: Directory to search in
1160 * @name: qstr of name we wish to find
1161 *
1162 * On hash failure or on lookup failure NULL is returned.
1163 */
1164struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1165{
1166 struct dentry *dentry = NULL;
1167
1168 /*
1169 * Check for a fs-specific hash function. Note that we must
1170 * calculate the standard hash first, as the d_op->d_hash()
1171 * routine may choose to leave the hash value unchanged.
1172 */
1173 name->hash = full_name_hash(name->name, name->len);
1174 if (dir->d_op && dir->d_op->d_hash) {
1175 if (dir->d_op->d_hash(dir, name) < 0)
1176 goto out;
1177 }
1178 dentry = d_lookup(dir, name);
1179out:
1180 return dentry;
1181}
1182
1183/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 * d_validate - verify dentry provided from insecure source
1185 * @dentry: The dentry alleged to be valid child of @dparent
1186 * @dparent: The parent dentry (known to be valid)
1187 * @hash: Hash of the dentry
1188 * @len: Length of the name
1189 *
1190 * An insecure source has sent us a dentry, here we verify it and dget() it.
1191 * This is used by ncpfs in its readdir implementation.
1192 * Zero is returned in the dentry is invalid.
1193 */
1194
1195int d_validate(struct dentry *dentry, struct dentry *dparent)
1196{
1197 struct hlist_head *base;
1198 struct hlist_node *lhp;
1199
1200 /* Check whether the ptr might be valid at all.. */
1201 if (!kmem_ptr_validate(dentry_cache, dentry))
1202 goto out;
1203
1204 if (dentry->d_parent != dparent)
1205 goto out;
1206
1207 spin_lock(&dcache_lock);
1208 base = d_hash(dparent, dentry->d_name.hash);
1209 hlist_for_each(lhp,base) {
Paul E. McKenney665a7582005-11-07 00:59:17 -08001210 /* hlist_for_each_entry_rcu() not required for d_hash list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001211 * as it is parsed under dcache_lock
1212 */
1213 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1214 __dget_locked(dentry);
1215 spin_unlock(&dcache_lock);
1216 return 1;
1217 }
1218 }
1219 spin_unlock(&dcache_lock);
1220out:
1221 return 0;
1222}
1223
1224/*
1225 * When a file is deleted, we have two options:
1226 * - turn this dentry into a negative dentry
1227 * - unhash this dentry and free it.
1228 *
1229 * Usually, we want to just turn this into
1230 * a negative dentry, but if anybody else is
1231 * currently using the dentry or the inode
1232 * we can't do that and we fall back on removing
1233 * it from the hash queues and waiting for
1234 * it to be deleted later when it has no users
1235 */
1236
1237/**
1238 * d_delete - delete a dentry
1239 * @dentry: The dentry to delete
1240 *
1241 * Turn the dentry into a negative dentry if possible, otherwise
1242 * remove it from the hash queues so it can be deleted later
1243 */
1244
1245void d_delete(struct dentry * dentry)
1246{
John McCutchan7a91bf72005-08-08 13:52:16 -04001247 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 /*
1249 * Are we the only user?
1250 */
1251 spin_lock(&dcache_lock);
1252 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001253 isdir = S_ISDIR(dentry->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254 if (atomic_read(&dentry->d_count) == 1) {
1255 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001256 fsnotify_nameremove(dentry, isdir);
Amy Griffis7a2bd3f2006-03-31 02:30:54 -08001257
1258 /* remove this and other inotify debug checks after 2.6.18 */
1259 dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260 return;
1261 }
1262
1263 if (!d_unhashed(dentry))
1264 __d_drop(dentry);
1265
1266 spin_unlock(&dentry->d_lock);
1267 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001268
1269 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001270}
1271
1272static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1273{
1274
1275 entry->d_flags &= ~DCACHE_UNHASHED;
1276 hlist_add_head_rcu(&entry->d_hash, list);
1277}
1278
1279/**
1280 * d_rehash - add an entry back to the hash
1281 * @entry: dentry to add to the hash
1282 *
1283 * Adds a dentry to the hash according to its name.
1284 */
1285
1286void d_rehash(struct dentry * entry)
1287{
1288 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1289
1290 spin_lock(&dcache_lock);
1291 spin_lock(&entry->d_lock);
1292 __d_rehash(entry, list);
1293 spin_unlock(&entry->d_lock);
1294 spin_unlock(&dcache_lock);
1295}
1296
1297#define do_switch(x,y) do { \
1298 __typeof__ (x) __tmp = x; \
1299 x = y; y = __tmp; } while (0)
1300
1301/*
1302 * When switching names, the actual string doesn't strictly have to
1303 * be preserved in the target - because we're dropping the target
1304 * anyway. As such, we can just do a simple memcpy() to copy over
1305 * the new name before we switch.
1306 *
1307 * Note that we have to be a lot more careful about getting the hash
1308 * switched - we have to switch the hash value properly even if it
1309 * then no longer matches the actual (corrupted) string of the target.
1310 * The hash value has to match the hash queue that the dentry is on..
1311 */
1312static void switch_names(struct dentry *dentry, struct dentry *target)
1313{
1314 if (dname_external(target)) {
1315 if (dname_external(dentry)) {
1316 /*
1317 * Both external: swap the pointers
1318 */
1319 do_switch(target->d_name.name, dentry->d_name.name);
1320 } else {
1321 /*
1322 * dentry:internal, target:external. Steal target's
1323 * storage and make target internal.
1324 */
1325 dentry->d_name.name = target->d_name.name;
1326 target->d_name.name = target->d_iname;
1327 }
1328 } else {
1329 if (dname_external(dentry)) {
1330 /*
1331 * dentry:external, target:internal. Give dentry's
1332 * storage to target and make dentry internal
1333 */
1334 memcpy(dentry->d_iname, target->d_name.name,
1335 target->d_name.len + 1);
1336 target->d_name.name = dentry->d_name.name;
1337 dentry->d_name.name = dentry->d_iname;
1338 } else {
1339 /*
1340 * Both are internal. Just copy target to dentry
1341 */
1342 memcpy(dentry->d_iname, target->d_name.name,
1343 target->d_name.len + 1);
1344 }
1345 }
1346}
1347
1348/*
1349 * We cannibalize "target" when moving dentry on top of it,
1350 * because it's going to be thrown away anyway. We could be more
1351 * polite about it, though.
1352 *
1353 * This forceful removal will result in ugly /proc output if
1354 * somebody holds a file open that got deleted due to a rename.
1355 * We could be nicer about the deleted file, and let it show
1356 * up under the name it got deleted rather than the name that
1357 * deleted it.
1358 */
1359
1360/**
1361 * d_move - move a dentry
1362 * @dentry: entry to move
1363 * @target: new dentry
1364 *
1365 * Update the dcache to reflect the move of a file name. Negative
1366 * dcache entries should not be moved in this way.
1367 */
1368
1369void d_move(struct dentry * dentry, struct dentry * target)
1370{
1371 struct hlist_head *list;
1372
1373 if (!dentry->d_inode)
1374 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1375
1376 spin_lock(&dcache_lock);
1377 write_seqlock(&rename_lock);
1378 /*
1379 * XXXX: do we really need to take target->d_lock?
1380 */
1381 if (target < dentry) {
1382 spin_lock(&target->d_lock);
1383 spin_lock(&dentry->d_lock);
1384 } else {
1385 spin_lock(&dentry->d_lock);
1386 spin_lock(&target->d_lock);
1387 }
1388
1389 /* Move the dentry to the target hash queue, if on different bucket */
1390 if (dentry->d_flags & DCACHE_UNHASHED)
1391 goto already_unhashed;
1392
1393 hlist_del_rcu(&dentry->d_hash);
1394
1395already_unhashed:
1396 list = d_hash(target->d_parent, target->d_name.hash);
1397 __d_rehash(dentry, list);
1398
1399 /* Unhash the target: dput() will then get rid of it */
1400 __d_drop(target);
1401
Eric Dumazet5160ee62006-01-08 01:03:32 -08001402 list_del(&dentry->d_u.d_child);
1403 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001404
1405 /* Switch the names.. */
1406 switch_names(dentry, target);
1407 do_switch(dentry->d_name.len, target->d_name.len);
1408 do_switch(dentry->d_name.hash, target->d_name.hash);
1409
1410 /* ... and switch the parents */
1411 if (IS_ROOT(dentry)) {
1412 dentry->d_parent = target->d_parent;
1413 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001414 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001415 } else {
1416 do_switch(dentry->d_parent, target->d_parent);
1417
1418 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001419 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001420 }
1421
Eric Dumazet5160ee62006-01-08 01:03:32 -08001422 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001424 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001425 spin_unlock(&dentry->d_lock);
1426 write_sequnlock(&rename_lock);
1427 spin_unlock(&dcache_lock);
1428}
1429
1430/**
1431 * d_path - return the path of a dentry
1432 * @dentry: dentry to report
1433 * @vfsmnt: vfsmnt to which the dentry belongs
1434 * @root: root dentry
1435 * @rootmnt: vfsmnt to which the root dentry belongs
1436 * @buffer: buffer to return value in
1437 * @buflen: buffer length
1438 *
1439 * Convert a dentry into an ASCII path name. If the entry has been deleted
1440 * the string " (deleted)" is appended. Note that this is ambiguous.
1441 *
1442 * Returns the buffer or an error code if the path was too long.
1443 *
1444 * "buflen" should be positive. Caller holds the dcache_lock.
1445 */
1446static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1447 struct dentry *root, struct vfsmount *rootmnt,
1448 char *buffer, int buflen)
1449{
1450 char * end = buffer+buflen;
1451 char * retval;
1452 int namelen;
1453
1454 *--end = '\0';
1455 buflen--;
1456 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1457 buflen -= 10;
1458 end -= 10;
1459 if (buflen < 0)
1460 goto Elong;
1461 memcpy(end, " (deleted)", 10);
1462 }
1463
1464 if (buflen < 1)
1465 goto Elong;
1466 /* Get '/' right */
1467 retval = end-1;
1468 *retval = '/';
1469
1470 for (;;) {
1471 struct dentry * parent;
1472
1473 if (dentry == root && vfsmnt == rootmnt)
1474 break;
1475 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1476 /* Global root? */
1477 spin_lock(&vfsmount_lock);
1478 if (vfsmnt->mnt_parent == vfsmnt) {
1479 spin_unlock(&vfsmount_lock);
1480 goto global_root;
1481 }
1482 dentry = vfsmnt->mnt_mountpoint;
1483 vfsmnt = vfsmnt->mnt_parent;
1484 spin_unlock(&vfsmount_lock);
1485 continue;
1486 }
1487 parent = dentry->d_parent;
1488 prefetch(parent);
1489 namelen = dentry->d_name.len;
1490 buflen -= namelen + 1;
1491 if (buflen < 0)
1492 goto Elong;
1493 end -= namelen;
1494 memcpy(end, dentry->d_name.name, namelen);
1495 *--end = '/';
1496 retval = end;
1497 dentry = parent;
1498 }
1499
1500 return retval;
1501
1502global_root:
1503 namelen = dentry->d_name.len;
1504 buflen -= namelen;
1505 if (buflen < 0)
1506 goto Elong;
1507 retval -= namelen-1; /* hit the slash */
1508 memcpy(retval, dentry->d_name.name, namelen);
1509 return retval;
1510Elong:
1511 return ERR_PTR(-ENAMETOOLONG);
1512}
1513
1514/* write full pathname into buffer and return start of pathname */
1515char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1516 char *buf, int buflen)
1517{
1518 char *res;
1519 struct vfsmount *rootmnt;
1520 struct dentry *root;
1521
1522 read_lock(&current->fs->lock);
1523 rootmnt = mntget(current->fs->rootmnt);
1524 root = dget(current->fs->root);
1525 read_unlock(&current->fs->lock);
1526 spin_lock(&dcache_lock);
1527 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1528 spin_unlock(&dcache_lock);
1529 dput(root);
1530 mntput(rootmnt);
1531 return res;
1532}
1533
1534/*
1535 * NOTE! The user-level library version returns a
1536 * character pointer. The kernel system call just
1537 * returns the length of the buffer filled (which
1538 * includes the ending '\0' character), or a negative
1539 * error value. So libc would do something like
1540 *
1541 * char *getcwd(char * buf, size_t size)
1542 * {
1543 * int retval;
1544 *
1545 * retval = sys_getcwd(buf, size);
1546 * if (retval >= 0)
1547 * return buf;
1548 * errno = -retval;
1549 * return NULL;
1550 * }
1551 */
1552asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1553{
1554 int error;
1555 struct vfsmount *pwdmnt, *rootmnt;
1556 struct dentry *pwd, *root;
1557 char *page = (char *) __get_free_page(GFP_USER);
1558
1559 if (!page)
1560 return -ENOMEM;
1561
1562 read_lock(&current->fs->lock);
1563 pwdmnt = mntget(current->fs->pwdmnt);
1564 pwd = dget(current->fs->pwd);
1565 rootmnt = mntget(current->fs->rootmnt);
1566 root = dget(current->fs->root);
1567 read_unlock(&current->fs->lock);
1568
1569 error = -ENOENT;
1570 /* Has the current directory has been unlinked? */
1571 spin_lock(&dcache_lock);
1572 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1573 unsigned long len;
1574 char * cwd;
1575
1576 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1577 spin_unlock(&dcache_lock);
1578
1579 error = PTR_ERR(cwd);
1580 if (IS_ERR(cwd))
1581 goto out;
1582
1583 error = -ERANGE;
1584 len = PAGE_SIZE + page - cwd;
1585 if (len <= size) {
1586 error = len;
1587 if (copy_to_user(buf, cwd, len))
1588 error = -EFAULT;
1589 }
1590 } else
1591 spin_unlock(&dcache_lock);
1592
1593out:
1594 dput(pwd);
1595 mntput(pwdmnt);
1596 dput(root);
1597 mntput(rootmnt);
1598 free_page((unsigned long) page);
1599 return error;
1600}
1601
1602/*
1603 * Test whether new_dentry is a subdirectory of old_dentry.
1604 *
1605 * Trivially implemented using the dcache structure
1606 */
1607
1608/**
1609 * is_subdir - is new dentry a subdirectory of old_dentry
1610 * @new_dentry: new dentry
1611 * @old_dentry: old dentry
1612 *
1613 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1614 * Returns 0 otherwise.
1615 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1616 */
1617
1618int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1619{
1620 int result;
1621 struct dentry * saved = new_dentry;
1622 unsigned long seq;
1623
1624 /* need rcu_readlock to protect against the d_parent trashing due to
1625 * d_move
1626 */
1627 rcu_read_lock();
1628 do {
1629 /* for restarting inner loop in case of seq retry */
1630 new_dentry = saved;
1631 result = 0;
1632 seq = read_seqbegin(&rename_lock);
1633 for (;;) {
1634 if (new_dentry != old_dentry) {
1635 struct dentry * parent = new_dentry->d_parent;
1636 if (parent == new_dentry)
1637 break;
1638 new_dentry = parent;
1639 continue;
1640 }
1641 result = 1;
1642 break;
1643 }
1644 } while (read_seqretry(&rename_lock, seq));
1645 rcu_read_unlock();
1646
1647 return result;
1648}
1649
1650void d_genocide(struct dentry *root)
1651{
1652 struct dentry *this_parent = root;
1653 struct list_head *next;
1654
1655 spin_lock(&dcache_lock);
1656repeat:
1657 next = this_parent->d_subdirs.next;
1658resume:
1659 while (next != &this_parent->d_subdirs) {
1660 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001661 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 next = tmp->next;
1663 if (d_unhashed(dentry)||!dentry->d_inode)
1664 continue;
1665 if (!list_empty(&dentry->d_subdirs)) {
1666 this_parent = dentry;
1667 goto repeat;
1668 }
1669 atomic_dec(&dentry->d_count);
1670 }
1671 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001672 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 atomic_dec(&this_parent->d_count);
1674 this_parent = this_parent->d_parent;
1675 goto resume;
1676 }
1677 spin_unlock(&dcache_lock);
1678}
1679
1680/**
1681 * find_inode_number - check for dentry with name
1682 * @dir: directory to check
1683 * @name: Name to find.
1684 *
1685 * Check whether a dentry already exists for the given name,
1686 * and return the inode number if it has an inode. Otherwise
1687 * 0 is returned.
1688 *
1689 * This routine is used to post-process directory listings for
1690 * filesystems using synthetic inode numbers, and is necessary
1691 * to keep getcwd() working.
1692 */
1693
1694ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1695{
1696 struct dentry * dentry;
1697 ino_t ino = 0;
1698
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001699 dentry = d_hash_and_lookup(dir, name);
1700 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 if (dentry->d_inode)
1702 ino = dentry->d_inode->i_ino;
1703 dput(dentry);
1704 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 return ino;
1706}
1707
1708static __initdata unsigned long dhash_entries;
1709static int __init set_dhash_entries(char *str)
1710{
1711 if (!str)
1712 return 0;
1713 dhash_entries = simple_strtoul(str, &str, 0);
1714 return 1;
1715}
1716__setup("dhash_entries=", set_dhash_entries);
1717
1718static void __init dcache_init_early(void)
1719{
1720 int loop;
1721
1722 /* If hashes are distributed across NUMA nodes, defer
1723 * hash allocation until vmalloc space is available.
1724 */
1725 if (hashdist)
1726 return;
1727
1728 dentry_hashtable =
1729 alloc_large_system_hash("Dentry cache",
1730 sizeof(struct hlist_head),
1731 dhash_entries,
1732 13,
1733 HASH_EARLY,
1734 &d_hash_shift,
1735 &d_hash_mask,
1736 0);
1737
1738 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1739 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1740}
1741
1742static void __init dcache_init(unsigned long mempages)
1743{
1744 int loop;
1745
1746 /*
1747 * A constructor could be added for stable state like the lists,
1748 * but it is probably not worth it because of the cache nature
1749 * of the dcache.
1750 */
1751 dentry_cache = kmem_cache_create("dentry_cache",
1752 sizeof(struct dentry),
1753 0,
Paul Jacksonb0196002006-03-24 03:16:09 -08001754 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1755 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756 NULL, NULL);
1757
1758 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1759
1760 /* Hash may have been set up in dcache_init_early */
1761 if (!hashdist)
1762 return;
1763
1764 dentry_hashtable =
1765 alloc_large_system_hash("Dentry cache",
1766 sizeof(struct hlist_head),
1767 dhash_entries,
1768 13,
1769 0,
1770 &d_hash_shift,
1771 &d_hash_mask,
1772 0);
1773
1774 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1775 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1776}
1777
1778/* SLAB cache for __getname() consumers */
Eric Dumazetfa3536c2006-03-26 01:37:24 -08001779kmem_cache_t *names_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001780
1781/* SLAB cache for file structures */
Eric Dumazetfa3536c2006-03-26 01:37:24 -08001782kmem_cache_t *filp_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783
1784EXPORT_SYMBOL(d_genocide);
1785
1786extern void bdev_cache_init(void);
1787extern void chrdev_init(void);
1788
1789void __init vfs_caches_init_early(void)
1790{
1791 dcache_init_early();
1792 inode_init_early();
1793}
1794
1795void __init vfs_caches_init(unsigned long mempages)
1796{
1797 unsigned long reserve;
1798
1799 /* Base hash sizes on available memory, with a reserve equal to
1800 150% of current kernel size */
1801
1802 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1803 mempages -= reserve;
1804
1805 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1806 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1807
1808 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
Dipankar Sarma529bf6b2006-03-07 21:55:35 -08001809 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810
1811 dcache_init(mempages);
1812 inode_init(mempages);
1813 files_init(mempages);
1814 mnt_init(mempages);
1815 bdev_cache_init();
1816 chrdev_init();
1817}
1818
1819EXPORT_SYMBOL(d_alloc);
1820EXPORT_SYMBOL(d_alloc_anon);
1821EXPORT_SYMBOL(d_alloc_root);
1822EXPORT_SYMBOL(d_delete);
1823EXPORT_SYMBOL(d_find_alias);
1824EXPORT_SYMBOL(d_instantiate);
1825EXPORT_SYMBOL(d_invalidate);
1826EXPORT_SYMBOL(d_lookup);
1827EXPORT_SYMBOL(d_move);
1828EXPORT_SYMBOL(d_path);
1829EXPORT_SYMBOL(d_prune_aliases);
1830EXPORT_SYMBOL(d_rehash);
1831EXPORT_SYMBOL(d_splice_alias);
1832EXPORT_SYMBOL(d_validate);
1833EXPORT_SYMBOL(dget_locked);
1834EXPORT_SYMBOL(dput);
1835EXPORT_SYMBOL(find_inode_number);
1836EXPORT_SYMBOL(have_submounts);
1837EXPORT_SYMBOL(names_cachep);
1838EXPORT_SYMBOL(shrink_dcache_parent);
1839EXPORT_SYMBOL(shrink_dcache_sb);