blob: 940d188e5d14a90730fe170b59eaa426012ef546 [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
385 *
386 * Shrink the dcache. This is done when we need
387 * more memory, or simply when we need to unmount
388 * something (at which point we need to unuse
389 * all dentries).
390 *
391 * This function may fail to free any resources if
392 * all the dentries are in use.
393 */
394
395static void prune_dcache(int count)
396{
397 spin_lock(&dcache_lock);
398 for (; count ; count--) {
399 struct dentry *dentry;
400 struct list_head *tmp;
401
402 cond_resched_lock(&dcache_lock);
403
404 tmp = dentry_unused.prev;
405 if (tmp == &dentry_unused)
406 break;
407 list_del_init(tmp);
408 prefetch(dentry_unused.prev);
409 dentry_stat.nr_unused--;
410 dentry = list_entry(tmp, struct dentry, d_lru);
411
412 spin_lock(&dentry->d_lock);
413 /*
414 * We found an inuse dentry which was not removed from
415 * dentry_unused because of laziness during lookup. Do not free
416 * it - just keep it off the dentry_unused list.
417 */
418 if (atomic_read(&dentry->d_count)) {
419 spin_unlock(&dentry->d_lock);
420 continue;
421 }
422 /* If the dentry was recently referenced, don't free it. */
423 if (dentry->d_flags & DCACHE_REFERENCED) {
424 dentry->d_flags &= ~DCACHE_REFERENCED;
425 list_add(&dentry->d_lru, &dentry_unused);
426 dentry_stat.nr_unused++;
427 spin_unlock(&dentry->d_lock);
428 continue;
429 }
430 prune_one_dentry(dentry);
431 }
432 spin_unlock(&dcache_lock);
433}
434
435/*
436 * Shrink the dcache for the specified super block.
437 * This allows us to unmount a device without disturbing
438 * the dcache for the other devices.
439 *
440 * This implementation makes just two traversals of the
441 * unused list. On the first pass we move the selected
442 * dentries to the most recent end, and on the second
443 * pass we free them. The second pass must restart after
444 * each dput(), but since the target dentries are all at
445 * the end, it's really just a single traversal.
446 */
447
448/**
449 * shrink_dcache_sb - shrink dcache for a superblock
450 * @sb: superblock
451 *
452 * Shrink the dcache for the specified super block. This
453 * is used to free the dcache before unmounting a file
454 * system
455 */
456
457void shrink_dcache_sb(struct super_block * sb)
458{
459 struct list_head *tmp, *next;
460 struct dentry *dentry;
461
462 /*
463 * Pass one ... move the dentries for the specified
464 * superblock to the most recent end of the unused list.
465 */
466 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700467 list_for_each_safe(tmp, next, &dentry_unused) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 dentry = list_entry(tmp, struct dentry, d_lru);
469 if (dentry->d_sb != sb)
470 continue;
471 list_del(tmp);
472 list_add(tmp, &dentry_unused);
473 }
474
475 /*
476 * Pass two ... free the dentries for this superblock.
477 */
478repeat:
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700479 list_for_each_safe(tmp, next, &dentry_unused) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700480 dentry = list_entry(tmp, struct dentry, d_lru);
481 if (dentry->d_sb != sb)
482 continue;
483 dentry_stat.nr_unused--;
484 list_del_init(tmp);
485 spin_lock(&dentry->d_lock);
486 if (atomic_read(&dentry->d_count)) {
487 spin_unlock(&dentry->d_lock);
488 continue;
489 }
490 prune_one_dentry(dentry);
Kirill Korotaev2ab13462006-03-25 03:07:45 -0800491 cond_resched_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492 goto repeat;
493 }
494 spin_unlock(&dcache_lock);
495}
496
497/*
498 * Search for at least 1 mount point in the dentry's subdirs.
499 * We descend to the next level whenever the d_subdirs
500 * list is non-empty and continue searching.
501 */
502
503/**
504 * have_submounts - check for mounts over a dentry
505 * @parent: dentry to check.
506 *
507 * Return true if the parent or its subdirectories contain
508 * a mount point
509 */
510
511int have_submounts(struct dentry *parent)
512{
513 struct dentry *this_parent = parent;
514 struct list_head *next;
515
516 spin_lock(&dcache_lock);
517 if (d_mountpoint(parent))
518 goto positive;
519repeat:
520 next = this_parent->d_subdirs.next;
521resume:
522 while (next != &this_parent->d_subdirs) {
523 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800524 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 next = tmp->next;
526 /* Have we found a mount point ? */
527 if (d_mountpoint(dentry))
528 goto positive;
529 if (!list_empty(&dentry->d_subdirs)) {
530 this_parent = dentry;
531 goto repeat;
532 }
533 }
534 /*
535 * All done at this level ... ascend and resume the search.
536 */
537 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800538 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539 this_parent = this_parent->d_parent;
540 goto resume;
541 }
542 spin_unlock(&dcache_lock);
543 return 0; /* No mount points found in tree */
544positive:
545 spin_unlock(&dcache_lock);
546 return 1;
547}
548
549/*
550 * Search the dentry child list for the specified parent,
551 * and move any unused dentries to the end of the unused
552 * list for prune_dcache(). We descend to the next level
553 * whenever the d_subdirs list is non-empty and continue
554 * searching.
555 *
556 * It returns zero iff there are no unused children,
557 * otherwise it returns the number of children moved to
558 * the end of the unused list. This may not be the total
559 * number of unused children, because select_parent can
560 * drop the lock and return early due to latency
561 * constraints.
562 */
563static int select_parent(struct dentry * parent)
564{
565 struct dentry *this_parent = parent;
566 struct list_head *next;
567 int found = 0;
568
569 spin_lock(&dcache_lock);
570repeat:
571 next = this_parent->d_subdirs.next;
572resume:
573 while (next != &this_parent->d_subdirs) {
574 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800575 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700576 next = tmp->next;
577
578 if (!list_empty(&dentry->d_lru)) {
579 dentry_stat.nr_unused--;
580 list_del_init(&dentry->d_lru);
581 }
582 /*
583 * move only zero ref count dentries to the end
584 * of the unused list for prune_dcache
585 */
586 if (!atomic_read(&dentry->d_count)) {
587 list_add(&dentry->d_lru, dentry_unused.prev);
588 dentry_stat.nr_unused++;
589 found++;
590 }
591
592 /*
593 * We can return to the caller if we have found some (this
594 * ensures forward progress). We'll be coming back to find
595 * the rest.
596 */
597 if (found && need_resched())
598 goto out;
599
600 /*
601 * Descend a level if the d_subdirs list is non-empty.
602 */
603 if (!list_empty(&dentry->d_subdirs)) {
604 this_parent = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 goto repeat;
606 }
607 }
608 /*
609 * All done at this level ... ascend and resume the search.
610 */
611 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800612 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613 this_parent = this_parent->d_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700614 goto resume;
615 }
616out:
617 spin_unlock(&dcache_lock);
618 return found;
619}
620
621/**
622 * shrink_dcache_parent - prune dcache
623 * @parent: parent of entries to prune
624 *
625 * Prune the dcache to remove unused children of the parent dentry.
626 */
627
628void shrink_dcache_parent(struct dentry * parent)
629{
630 int found;
631
632 while ((found = select_parent(parent)) != 0)
633 prune_dcache(found);
634}
635
636/**
637 * shrink_dcache_anon - further prune the cache
638 * @head: head of d_hash list of dentries to prune
639 *
640 * Prune the dentries that are anonymous
641 *
Paul E. McKenney665a7582005-11-07 00:59:17 -0800642 * parsing d_hash list does not hlist_for_each_entry_rcu() as it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700643 * done under dcache_lock.
644 *
645 */
646void shrink_dcache_anon(struct hlist_head *head)
647{
648 struct hlist_node *lp;
649 int found;
650 do {
651 found = 0;
652 spin_lock(&dcache_lock);
653 hlist_for_each(lp, head) {
654 struct dentry *this = hlist_entry(lp, struct dentry, d_hash);
655 if (!list_empty(&this->d_lru)) {
656 dentry_stat.nr_unused--;
657 list_del_init(&this->d_lru);
658 }
659
660 /*
661 * move only zero ref count dentries to the end
662 * of the unused list for prune_dcache
663 */
664 if (!atomic_read(&this->d_count)) {
665 list_add_tail(&this->d_lru, &dentry_unused);
666 dentry_stat.nr_unused++;
667 found++;
668 }
669 }
670 spin_unlock(&dcache_lock);
671 prune_dcache(found);
672 } while(found);
673}
674
675/*
676 * Scan `nr' dentries and return the number which remain.
677 *
678 * We need to avoid reentering the filesystem if the caller is performing a
679 * GFP_NOFS allocation attempt. One example deadlock is:
680 *
681 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
682 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
683 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
684 *
685 * In this case we return -1 to tell the caller that we baled.
686 */
Al Viro27496a82005-10-21 03:20:48 -0400687static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700688{
689 if (nr) {
690 if (!(gfp_mask & __GFP_FS))
691 return -1;
692 prune_dcache(nr);
693 }
694 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
695}
696
697/**
698 * d_alloc - allocate a dcache entry
699 * @parent: parent of entry to allocate
700 * @name: qstr of the name
701 *
702 * Allocates a dentry. It returns %NULL if there is insufficient memory
703 * available. On a success the dentry is returned. The name passed in is
704 * copied and the copy passed in may be reused after this call.
705 */
706
707struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
708{
709 struct dentry *dentry;
710 char *dname;
711
712 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
713 if (!dentry)
714 return NULL;
715
716 if (name->len > DNAME_INLINE_LEN-1) {
717 dname = kmalloc(name->len + 1, GFP_KERNEL);
718 if (!dname) {
719 kmem_cache_free(dentry_cache, dentry);
720 return NULL;
721 }
722 } else {
723 dname = dentry->d_iname;
724 }
725 dentry->d_name.name = dname;
726
727 dentry->d_name.len = name->len;
728 dentry->d_name.hash = name->hash;
729 memcpy(dname, name->name, name->len);
730 dname[name->len] = 0;
731
732 atomic_set(&dentry->d_count, 1);
733 dentry->d_flags = DCACHE_UNHASHED;
734 spin_lock_init(&dentry->d_lock);
735 dentry->d_inode = NULL;
736 dentry->d_parent = NULL;
737 dentry->d_sb = NULL;
738 dentry->d_op = NULL;
739 dentry->d_fsdata = NULL;
740 dentry->d_mounted = 0;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800741#ifdef CONFIG_PROFILING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742 dentry->d_cookie = NULL;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800743#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 INIT_HLIST_NODE(&dentry->d_hash);
745 INIT_LIST_HEAD(&dentry->d_lru);
746 INIT_LIST_HEAD(&dentry->d_subdirs);
747 INIT_LIST_HEAD(&dentry->d_alias);
748
749 if (parent) {
750 dentry->d_parent = dget(parent);
751 dentry->d_sb = parent->d_sb;
752 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800753 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 }
755
756 spin_lock(&dcache_lock);
757 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -0800758 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700759 dentry_stat.nr_dentry++;
760 spin_unlock(&dcache_lock);
761
762 return dentry;
763}
764
765struct dentry *d_alloc_name(struct dentry *parent, const char *name)
766{
767 struct qstr q;
768
769 q.name = name;
770 q.len = strlen(name);
771 q.hash = full_name_hash(q.name, q.len);
772 return d_alloc(parent, &q);
773}
774
775/**
776 * d_instantiate - fill in inode information for a dentry
777 * @entry: dentry to complete
778 * @inode: inode to attach to this dentry
779 *
780 * Fill in inode information in the entry.
781 *
782 * This turns negative dentries into productive full members
783 * of society.
784 *
785 * NOTE! This assumes that the inode count has been incremented
786 * (or otherwise set) by the caller to indicate that it is now
787 * in use by the dcache.
788 */
789
790void d_instantiate(struct dentry *entry, struct inode * inode)
791{
Eric Sesterhenn28133c72006-03-26 18:25:39 +0200792 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700793 spin_lock(&dcache_lock);
794 if (inode)
795 list_add(&entry->d_alias, &inode->i_dentry);
796 entry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800797 fsnotify_d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700798 spin_unlock(&dcache_lock);
799 security_d_instantiate(entry, inode);
800}
801
802/**
803 * d_instantiate_unique - instantiate a non-aliased dentry
804 * @entry: dentry to instantiate
805 * @inode: inode to attach to this dentry
806 *
807 * Fill in inode information in the entry. On success, it returns NULL.
808 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -0800809 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700810 *
811 * Note that in order to avoid conflicts with rename() etc, the caller
812 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -0800813 *
814 * This also assumes that the inode count has been incremented
815 * (or otherwise set) by the caller to indicate that it is now
816 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 */
818struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
819{
820 struct dentry *alias;
821 int len = entry->d_name.len;
822 const char *name = entry->d_name.name;
823 unsigned int hash = entry->d_name.hash;
824
825 BUG_ON(!list_empty(&entry->d_alias));
826 spin_lock(&dcache_lock);
827 if (!inode)
828 goto do_negative;
829 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
830 struct qstr *qstr = &alias->d_name;
831
832 if (qstr->hash != hash)
833 continue;
834 if (alias->d_parent != entry->d_parent)
835 continue;
836 if (qstr->len != len)
837 continue;
838 if (memcmp(qstr->name, name, len))
839 continue;
840 dget_locked(alias);
841 spin_unlock(&dcache_lock);
842 BUG_ON(!d_unhashed(alias));
Oleg Drokine866cfa2006-01-09 20:52:51 -0800843 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844 return alias;
845 }
846 list_add(&entry->d_alias, &inode->i_dentry);
847do_negative:
848 entry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800849 fsnotify_d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850 spin_unlock(&dcache_lock);
851 security_d_instantiate(entry, inode);
852 return NULL;
853}
854EXPORT_SYMBOL(d_instantiate_unique);
855
856/**
857 * d_alloc_root - allocate root dentry
858 * @root_inode: inode to allocate the root for
859 *
860 * Allocate a root ("/") dentry for the inode given. The inode is
861 * instantiated and returned. %NULL is returned if there is insufficient
862 * memory or the inode passed is %NULL.
863 */
864
865struct dentry * d_alloc_root(struct inode * root_inode)
866{
867 struct dentry *res = NULL;
868
869 if (root_inode) {
870 static const struct qstr name = { .name = "/", .len = 1 };
871
872 res = d_alloc(NULL, &name);
873 if (res) {
874 res->d_sb = root_inode->i_sb;
875 res->d_parent = res;
876 d_instantiate(res, root_inode);
877 }
878 }
879 return res;
880}
881
882static inline struct hlist_head *d_hash(struct dentry *parent,
883 unsigned long hash)
884{
885 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
886 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
887 return dentry_hashtable + (hash & D_HASHMASK);
888}
889
890/**
891 * d_alloc_anon - allocate an anonymous dentry
892 * @inode: inode to allocate the dentry for
893 *
894 * This is similar to d_alloc_root. It is used by filesystems when
895 * creating a dentry for a given inode, often in the process of
896 * mapping a filehandle to a dentry. The returned dentry may be
897 * anonymous, or may have a full name (if the inode was already
898 * in the cache). The file system may need to make further
899 * efforts to connect this dentry into the dcache properly.
900 *
901 * When called on a directory inode, we must ensure that
902 * the inode only ever has one dentry. If a dentry is
903 * found, that is returned instead of allocating a new one.
904 *
905 * On successful return, the reference to the inode has been transferred
906 * to the dentry. If %NULL is returned (indicating kmalloc failure),
907 * the reference on the inode has not been released.
908 */
909
910struct dentry * d_alloc_anon(struct inode *inode)
911{
912 static const struct qstr anonstring = { .name = "" };
913 struct dentry *tmp;
914 struct dentry *res;
915
916 if ((res = d_find_alias(inode))) {
917 iput(inode);
918 return res;
919 }
920
921 tmp = d_alloc(NULL, &anonstring);
922 if (!tmp)
923 return NULL;
924
925 tmp->d_parent = tmp; /* make sure dput doesn't croak */
926
927 spin_lock(&dcache_lock);
928 res = __d_find_alias(inode, 0);
929 if (!res) {
930 /* attach a disconnected dentry */
931 res = tmp;
932 tmp = NULL;
933 spin_lock(&res->d_lock);
934 res->d_sb = inode->i_sb;
935 res->d_parent = res;
936 res->d_inode = inode;
937 res->d_flags |= DCACHE_DISCONNECTED;
938 res->d_flags &= ~DCACHE_UNHASHED;
939 list_add(&res->d_alias, &inode->i_dentry);
940 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
941 spin_unlock(&res->d_lock);
942
943 inode = NULL; /* don't drop reference */
944 }
945 spin_unlock(&dcache_lock);
946
947 if (inode)
948 iput(inode);
949 if (tmp)
950 dput(tmp);
951 return res;
952}
953
954
955/**
956 * d_splice_alias - splice a disconnected dentry into the tree if one exists
957 * @inode: the inode which may have a disconnected dentry
958 * @dentry: a negative dentry which we want to point to the inode.
959 *
960 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
961 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
962 * and return it, else simply d_add the inode to the dentry and return NULL.
963 *
964 * This is needed in the lookup routine of any filesystem that is exportable
965 * (via knfsd) so that we can build dcache paths to directories effectively.
966 *
967 * If a dentry was found and moved, then it is returned. Otherwise NULL
968 * is returned. This matches the expected return value of ->lookup.
969 *
970 */
971struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
972{
973 struct dentry *new = NULL;
974
975 if (inode) {
976 spin_lock(&dcache_lock);
977 new = __d_find_alias(inode, 1);
978 if (new) {
979 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Pigginc32ccd82006-03-25 03:07:09 -0800980 fsnotify_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 spin_unlock(&dcache_lock);
982 security_d_instantiate(new, inode);
983 d_rehash(dentry);
984 d_move(new, dentry);
985 iput(inode);
986 } else {
987 /* d_instantiate takes dcache_lock, so we do it by hand */
988 list_add(&dentry->d_alias, &inode->i_dentry);
989 dentry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800990 fsnotify_d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991 spin_unlock(&dcache_lock);
992 security_d_instantiate(dentry, inode);
993 d_rehash(dentry);
994 }
995 } else
996 d_add(dentry, inode);
997 return new;
998}
999
1000
1001/**
1002 * d_lookup - search for a dentry
1003 * @parent: parent dentry
1004 * @name: qstr of name we wish to find
1005 *
1006 * Searches the children of the parent dentry for the name in question. If
1007 * the dentry is found its reference count is incremented and the dentry
1008 * is returned. The caller must use d_put to free the entry when it has
1009 * finished using it. %NULL is returned on failure.
1010 *
1011 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1012 * Memory barriers are used while updating and doing lockless traversal.
1013 * To avoid races with d_move while rename is happening, d_lock is used.
1014 *
1015 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1016 * and name pointer in one structure pointed by d_qstr.
1017 *
1018 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1019 * lookup is going on.
1020 *
1021 * dentry_unused list is not updated even if lookup finds the required dentry
1022 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1023 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1024 * acquisition.
1025 *
1026 * d_lookup() is protected against the concurrent renames in some unrelated
1027 * directory using the seqlockt_t rename_lock.
1028 */
1029
1030struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1031{
1032 struct dentry * dentry = NULL;
1033 unsigned long seq;
1034
1035 do {
1036 seq = read_seqbegin(&rename_lock);
1037 dentry = __d_lookup(parent, name);
1038 if (dentry)
1039 break;
1040 } while (read_seqretry(&rename_lock, seq));
1041 return dentry;
1042}
1043
1044struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1045{
1046 unsigned int len = name->len;
1047 unsigned int hash = name->hash;
1048 const unsigned char *str = name->name;
1049 struct hlist_head *head = d_hash(parent,hash);
1050 struct dentry *found = NULL;
1051 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001052 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053
1054 rcu_read_lock();
1055
Paul E. McKenney665a7582005-11-07 00:59:17 -08001056 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 struct qstr *qstr;
1058
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 if (dentry->d_name.hash != hash)
1060 continue;
1061 if (dentry->d_parent != parent)
1062 continue;
1063
1064 spin_lock(&dentry->d_lock);
1065
1066 /*
1067 * Recheck the dentry after taking the lock - d_move may have
1068 * changed things. Don't bother checking the hash because we're
1069 * about to compare the whole name anyway.
1070 */
1071 if (dentry->d_parent != parent)
1072 goto next;
1073
1074 /*
1075 * It is safe to compare names since d_move() cannot
1076 * change the qstr (protected by d_lock).
1077 */
1078 qstr = &dentry->d_name;
1079 if (parent->d_op && parent->d_op->d_compare) {
1080 if (parent->d_op->d_compare(parent, qstr, name))
1081 goto next;
1082 } else {
1083 if (qstr->len != len)
1084 goto next;
1085 if (memcmp(qstr->name, str, len))
1086 goto next;
1087 }
1088
1089 if (!d_unhashed(dentry)) {
1090 atomic_inc(&dentry->d_count);
1091 found = dentry;
1092 }
1093 spin_unlock(&dentry->d_lock);
1094 break;
1095next:
1096 spin_unlock(&dentry->d_lock);
1097 }
1098 rcu_read_unlock();
1099
1100 return found;
1101}
1102
1103/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001104 * d_hash_and_lookup - hash the qstr then search for a dentry
1105 * @dir: Directory to search in
1106 * @name: qstr of name we wish to find
1107 *
1108 * On hash failure or on lookup failure NULL is returned.
1109 */
1110struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1111{
1112 struct dentry *dentry = NULL;
1113
1114 /*
1115 * Check for a fs-specific hash function. Note that we must
1116 * calculate the standard hash first, as the d_op->d_hash()
1117 * routine may choose to leave the hash value unchanged.
1118 */
1119 name->hash = full_name_hash(name->name, name->len);
1120 if (dir->d_op && dir->d_op->d_hash) {
1121 if (dir->d_op->d_hash(dir, name) < 0)
1122 goto out;
1123 }
1124 dentry = d_lookup(dir, name);
1125out:
1126 return dentry;
1127}
1128
1129/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001130 * d_validate - verify dentry provided from insecure source
1131 * @dentry: The dentry alleged to be valid child of @dparent
1132 * @dparent: The parent dentry (known to be valid)
1133 * @hash: Hash of the dentry
1134 * @len: Length of the name
1135 *
1136 * An insecure source has sent us a dentry, here we verify it and dget() it.
1137 * This is used by ncpfs in its readdir implementation.
1138 * Zero is returned in the dentry is invalid.
1139 */
1140
1141int d_validate(struct dentry *dentry, struct dentry *dparent)
1142{
1143 struct hlist_head *base;
1144 struct hlist_node *lhp;
1145
1146 /* Check whether the ptr might be valid at all.. */
1147 if (!kmem_ptr_validate(dentry_cache, dentry))
1148 goto out;
1149
1150 if (dentry->d_parent != dparent)
1151 goto out;
1152
1153 spin_lock(&dcache_lock);
1154 base = d_hash(dparent, dentry->d_name.hash);
1155 hlist_for_each(lhp,base) {
Paul E. McKenney665a7582005-11-07 00:59:17 -08001156 /* hlist_for_each_entry_rcu() not required for d_hash list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157 * as it is parsed under dcache_lock
1158 */
1159 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1160 __dget_locked(dentry);
1161 spin_unlock(&dcache_lock);
1162 return 1;
1163 }
1164 }
1165 spin_unlock(&dcache_lock);
1166out:
1167 return 0;
1168}
1169
1170/*
1171 * When a file is deleted, we have two options:
1172 * - turn this dentry into a negative dentry
1173 * - unhash this dentry and free it.
1174 *
1175 * Usually, we want to just turn this into
1176 * a negative dentry, but if anybody else is
1177 * currently using the dentry or the inode
1178 * we can't do that and we fall back on removing
1179 * it from the hash queues and waiting for
1180 * it to be deleted later when it has no users
1181 */
1182
1183/**
1184 * d_delete - delete a dentry
1185 * @dentry: The dentry to delete
1186 *
1187 * Turn the dentry into a negative dentry if possible, otherwise
1188 * remove it from the hash queues so it can be deleted later
1189 */
1190
1191void d_delete(struct dentry * dentry)
1192{
John McCutchan7a91bf72005-08-08 13:52:16 -04001193 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 /*
1195 * Are we the only user?
1196 */
1197 spin_lock(&dcache_lock);
1198 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001199 isdir = S_ISDIR(dentry->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001200 if (atomic_read(&dentry->d_count) == 1) {
1201 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001202 fsnotify_nameremove(dentry, isdir);
Amy Griffis7a2bd3f2006-03-31 02:30:54 -08001203
1204 /* remove this and other inotify debug checks after 2.6.18 */
1205 dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001206 return;
1207 }
1208
1209 if (!d_unhashed(dentry))
1210 __d_drop(dentry);
1211
1212 spin_unlock(&dentry->d_lock);
1213 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001214
1215 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001216}
1217
1218static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1219{
1220
1221 entry->d_flags &= ~DCACHE_UNHASHED;
1222 hlist_add_head_rcu(&entry->d_hash, list);
1223}
1224
1225/**
1226 * d_rehash - add an entry back to the hash
1227 * @entry: dentry to add to the hash
1228 *
1229 * Adds a dentry to the hash according to its name.
1230 */
1231
1232void d_rehash(struct dentry * entry)
1233{
1234 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1235
1236 spin_lock(&dcache_lock);
1237 spin_lock(&entry->d_lock);
1238 __d_rehash(entry, list);
1239 spin_unlock(&entry->d_lock);
1240 spin_unlock(&dcache_lock);
1241}
1242
1243#define do_switch(x,y) do { \
1244 __typeof__ (x) __tmp = x; \
1245 x = y; y = __tmp; } while (0)
1246
1247/*
1248 * When switching names, the actual string doesn't strictly have to
1249 * be preserved in the target - because we're dropping the target
1250 * anyway. As such, we can just do a simple memcpy() to copy over
1251 * the new name before we switch.
1252 *
1253 * Note that we have to be a lot more careful about getting the hash
1254 * switched - we have to switch the hash value properly even if it
1255 * then no longer matches the actual (corrupted) string of the target.
1256 * The hash value has to match the hash queue that the dentry is on..
1257 */
1258static void switch_names(struct dentry *dentry, struct dentry *target)
1259{
1260 if (dname_external(target)) {
1261 if (dname_external(dentry)) {
1262 /*
1263 * Both external: swap the pointers
1264 */
1265 do_switch(target->d_name.name, dentry->d_name.name);
1266 } else {
1267 /*
1268 * dentry:internal, target:external. Steal target's
1269 * storage and make target internal.
1270 */
1271 dentry->d_name.name = target->d_name.name;
1272 target->d_name.name = target->d_iname;
1273 }
1274 } else {
1275 if (dname_external(dentry)) {
1276 /*
1277 * dentry:external, target:internal. Give dentry's
1278 * storage to target and make dentry internal
1279 */
1280 memcpy(dentry->d_iname, target->d_name.name,
1281 target->d_name.len + 1);
1282 target->d_name.name = dentry->d_name.name;
1283 dentry->d_name.name = dentry->d_iname;
1284 } else {
1285 /*
1286 * Both are internal. Just copy target to dentry
1287 */
1288 memcpy(dentry->d_iname, target->d_name.name,
1289 target->d_name.len + 1);
1290 }
1291 }
1292}
1293
1294/*
1295 * We cannibalize "target" when moving dentry on top of it,
1296 * because it's going to be thrown away anyway. We could be more
1297 * polite about it, though.
1298 *
1299 * This forceful removal will result in ugly /proc output if
1300 * somebody holds a file open that got deleted due to a rename.
1301 * We could be nicer about the deleted file, and let it show
1302 * up under the name it got deleted rather than the name that
1303 * deleted it.
1304 */
1305
1306/**
1307 * d_move - move a dentry
1308 * @dentry: entry to move
1309 * @target: new dentry
1310 *
1311 * Update the dcache to reflect the move of a file name. Negative
1312 * dcache entries should not be moved in this way.
1313 */
1314
1315void d_move(struct dentry * dentry, struct dentry * target)
1316{
1317 struct hlist_head *list;
1318
1319 if (!dentry->d_inode)
1320 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1321
1322 spin_lock(&dcache_lock);
1323 write_seqlock(&rename_lock);
1324 /*
1325 * XXXX: do we really need to take target->d_lock?
1326 */
1327 if (target < dentry) {
1328 spin_lock(&target->d_lock);
1329 spin_lock(&dentry->d_lock);
1330 } else {
1331 spin_lock(&dentry->d_lock);
1332 spin_lock(&target->d_lock);
1333 }
1334
1335 /* Move the dentry to the target hash queue, if on different bucket */
1336 if (dentry->d_flags & DCACHE_UNHASHED)
1337 goto already_unhashed;
1338
1339 hlist_del_rcu(&dentry->d_hash);
1340
1341already_unhashed:
1342 list = d_hash(target->d_parent, target->d_name.hash);
1343 __d_rehash(dentry, list);
1344
1345 /* Unhash the target: dput() will then get rid of it */
1346 __d_drop(target);
1347
Eric Dumazet5160ee62006-01-08 01:03:32 -08001348 list_del(&dentry->d_u.d_child);
1349 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350
1351 /* Switch the names.. */
1352 switch_names(dentry, target);
1353 do_switch(dentry->d_name.len, target->d_name.len);
1354 do_switch(dentry->d_name.hash, target->d_name.hash);
1355
1356 /* ... and switch the parents */
1357 if (IS_ROOT(dentry)) {
1358 dentry->d_parent = target->d_parent;
1359 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001360 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 } else {
1362 do_switch(dentry->d_parent, target->d_parent);
1363
1364 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001365 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 }
1367
Eric Dumazet5160ee62006-01-08 01:03:32 -08001368 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001369 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001370 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 spin_unlock(&dentry->d_lock);
1372 write_sequnlock(&rename_lock);
1373 spin_unlock(&dcache_lock);
1374}
1375
1376/**
1377 * d_path - return the path of a dentry
1378 * @dentry: dentry to report
1379 * @vfsmnt: vfsmnt to which the dentry belongs
1380 * @root: root dentry
1381 * @rootmnt: vfsmnt to which the root dentry belongs
1382 * @buffer: buffer to return value in
1383 * @buflen: buffer length
1384 *
1385 * Convert a dentry into an ASCII path name. If the entry has been deleted
1386 * the string " (deleted)" is appended. Note that this is ambiguous.
1387 *
1388 * Returns the buffer or an error code if the path was too long.
1389 *
1390 * "buflen" should be positive. Caller holds the dcache_lock.
1391 */
1392static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1393 struct dentry *root, struct vfsmount *rootmnt,
1394 char *buffer, int buflen)
1395{
1396 char * end = buffer+buflen;
1397 char * retval;
1398 int namelen;
1399
1400 *--end = '\0';
1401 buflen--;
1402 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1403 buflen -= 10;
1404 end -= 10;
1405 if (buflen < 0)
1406 goto Elong;
1407 memcpy(end, " (deleted)", 10);
1408 }
1409
1410 if (buflen < 1)
1411 goto Elong;
1412 /* Get '/' right */
1413 retval = end-1;
1414 *retval = '/';
1415
1416 for (;;) {
1417 struct dentry * parent;
1418
1419 if (dentry == root && vfsmnt == rootmnt)
1420 break;
1421 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1422 /* Global root? */
1423 spin_lock(&vfsmount_lock);
1424 if (vfsmnt->mnt_parent == vfsmnt) {
1425 spin_unlock(&vfsmount_lock);
1426 goto global_root;
1427 }
1428 dentry = vfsmnt->mnt_mountpoint;
1429 vfsmnt = vfsmnt->mnt_parent;
1430 spin_unlock(&vfsmount_lock);
1431 continue;
1432 }
1433 parent = dentry->d_parent;
1434 prefetch(parent);
1435 namelen = dentry->d_name.len;
1436 buflen -= namelen + 1;
1437 if (buflen < 0)
1438 goto Elong;
1439 end -= namelen;
1440 memcpy(end, dentry->d_name.name, namelen);
1441 *--end = '/';
1442 retval = end;
1443 dentry = parent;
1444 }
1445
1446 return retval;
1447
1448global_root:
1449 namelen = dentry->d_name.len;
1450 buflen -= namelen;
1451 if (buflen < 0)
1452 goto Elong;
1453 retval -= namelen-1; /* hit the slash */
1454 memcpy(retval, dentry->d_name.name, namelen);
1455 return retval;
1456Elong:
1457 return ERR_PTR(-ENAMETOOLONG);
1458}
1459
1460/* write full pathname into buffer and return start of pathname */
1461char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1462 char *buf, int buflen)
1463{
1464 char *res;
1465 struct vfsmount *rootmnt;
1466 struct dentry *root;
1467
1468 read_lock(&current->fs->lock);
1469 rootmnt = mntget(current->fs->rootmnt);
1470 root = dget(current->fs->root);
1471 read_unlock(&current->fs->lock);
1472 spin_lock(&dcache_lock);
1473 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1474 spin_unlock(&dcache_lock);
1475 dput(root);
1476 mntput(rootmnt);
1477 return res;
1478}
1479
1480/*
1481 * NOTE! The user-level library version returns a
1482 * character pointer. The kernel system call just
1483 * returns the length of the buffer filled (which
1484 * includes the ending '\0' character), or a negative
1485 * error value. So libc would do something like
1486 *
1487 * char *getcwd(char * buf, size_t size)
1488 * {
1489 * int retval;
1490 *
1491 * retval = sys_getcwd(buf, size);
1492 * if (retval >= 0)
1493 * return buf;
1494 * errno = -retval;
1495 * return NULL;
1496 * }
1497 */
1498asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1499{
1500 int error;
1501 struct vfsmount *pwdmnt, *rootmnt;
1502 struct dentry *pwd, *root;
1503 char *page = (char *) __get_free_page(GFP_USER);
1504
1505 if (!page)
1506 return -ENOMEM;
1507
1508 read_lock(&current->fs->lock);
1509 pwdmnt = mntget(current->fs->pwdmnt);
1510 pwd = dget(current->fs->pwd);
1511 rootmnt = mntget(current->fs->rootmnt);
1512 root = dget(current->fs->root);
1513 read_unlock(&current->fs->lock);
1514
1515 error = -ENOENT;
1516 /* Has the current directory has been unlinked? */
1517 spin_lock(&dcache_lock);
1518 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1519 unsigned long len;
1520 char * cwd;
1521
1522 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1523 spin_unlock(&dcache_lock);
1524
1525 error = PTR_ERR(cwd);
1526 if (IS_ERR(cwd))
1527 goto out;
1528
1529 error = -ERANGE;
1530 len = PAGE_SIZE + page - cwd;
1531 if (len <= size) {
1532 error = len;
1533 if (copy_to_user(buf, cwd, len))
1534 error = -EFAULT;
1535 }
1536 } else
1537 spin_unlock(&dcache_lock);
1538
1539out:
1540 dput(pwd);
1541 mntput(pwdmnt);
1542 dput(root);
1543 mntput(rootmnt);
1544 free_page((unsigned long) page);
1545 return error;
1546}
1547
1548/*
1549 * Test whether new_dentry is a subdirectory of old_dentry.
1550 *
1551 * Trivially implemented using the dcache structure
1552 */
1553
1554/**
1555 * is_subdir - is new dentry a subdirectory of old_dentry
1556 * @new_dentry: new dentry
1557 * @old_dentry: old dentry
1558 *
1559 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1560 * Returns 0 otherwise.
1561 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1562 */
1563
1564int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1565{
1566 int result;
1567 struct dentry * saved = new_dentry;
1568 unsigned long seq;
1569
1570 /* need rcu_readlock to protect against the d_parent trashing due to
1571 * d_move
1572 */
1573 rcu_read_lock();
1574 do {
1575 /* for restarting inner loop in case of seq retry */
1576 new_dentry = saved;
1577 result = 0;
1578 seq = read_seqbegin(&rename_lock);
1579 for (;;) {
1580 if (new_dentry != old_dentry) {
1581 struct dentry * parent = new_dentry->d_parent;
1582 if (parent == new_dentry)
1583 break;
1584 new_dentry = parent;
1585 continue;
1586 }
1587 result = 1;
1588 break;
1589 }
1590 } while (read_seqretry(&rename_lock, seq));
1591 rcu_read_unlock();
1592
1593 return result;
1594}
1595
1596void d_genocide(struct dentry *root)
1597{
1598 struct dentry *this_parent = root;
1599 struct list_head *next;
1600
1601 spin_lock(&dcache_lock);
1602repeat:
1603 next = this_parent->d_subdirs.next;
1604resume:
1605 while (next != &this_parent->d_subdirs) {
1606 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001607 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 next = tmp->next;
1609 if (d_unhashed(dentry)||!dentry->d_inode)
1610 continue;
1611 if (!list_empty(&dentry->d_subdirs)) {
1612 this_parent = dentry;
1613 goto repeat;
1614 }
1615 atomic_dec(&dentry->d_count);
1616 }
1617 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001618 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001619 atomic_dec(&this_parent->d_count);
1620 this_parent = this_parent->d_parent;
1621 goto resume;
1622 }
1623 spin_unlock(&dcache_lock);
1624}
1625
1626/**
1627 * find_inode_number - check for dentry with name
1628 * @dir: directory to check
1629 * @name: Name to find.
1630 *
1631 * Check whether a dentry already exists for the given name,
1632 * and return the inode number if it has an inode. Otherwise
1633 * 0 is returned.
1634 *
1635 * This routine is used to post-process directory listings for
1636 * filesystems using synthetic inode numbers, and is necessary
1637 * to keep getcwd() working.
1638 */
1639
1640ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1641{
1642 struct dentry * dentry;
1643 ino_t ino = 0;
1644
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001645 dentry = d_hash_and_lookup(dir, name);
1646 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001647 if (dentry->d_inode)
1648 ino = dentry->d_inode->i_ino;
1649 dput(dentry);
1650 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001651 return ino;
1652}
1653
1654static __initdata unsigned long dhash_entries;
1655static int __init set_dhash_entries(char *str)
1656{
1657 if (!str)
1658 return 0;
1659 dhash_entries = simple_strtoul(str, &str, 0);
1660 return 1;
1661}
1662__setup("dhash_entries=", set_dhash_entries);
1663
1664static void __init dcache_init_early(void)
1665{
1666 int loop;
1667
1668 /* If hashes are distributed across NUMA nodes, defer
1669 * hash allocation until vmalloc space is available.
1670 */
1671 if (hashdist)
1672 return;
1673
1674 dentry_hashtable =
1675 alloc_large_system_hash("Dentry cache",
1676 sizeof(struct hlist_head),
1677 dhash_entries,
1678 13,
1679 HASH_EARLY,
1680 &d_hash_shift,
1681 &d_hash_mask,
1682 0);
1683
1684 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1685 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1686}
1687
1688static void __init dcache_init(unsigned long mempages)
1689{
1690 int loop;
1691
1692 /*
1693 * A constructor could be added for stable state like the lists,
1694 * but it is probably not worth it because of the cache nature
1695 * of the dcache.
1696 */
1697 dentry_cache = kmem_cache_create("dentry_cache",
1698 sizeof(struct dentry),
1699 0,
Paul Jacksonb0196002006-03-24 03:16:09 -08001700 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1701 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 NULL, NULL);
1703
1704 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1705
1706 /* Hash may have been set up in dcache_init_early */
1707 if (!hashdist)
1708 return;
1709
1710 dentry_hashtable =
1711 alloc_large_system_hash("Dentry cache",
1712 sizeof(struct hlist_head),
1713 dhash_entries,
1714 13,
1715 0,
1716 &d_hash_shift,
1717 &d_hash_mask,
1718 0);
1719
1720 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1721 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1722}
1723
1724/* SLAB cache for __getname() consumers */
Eric Dumazetfa3536c2006-03-26 01:37:24 -08001725kmem_cache_t *names_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726
1727/* SLAB cache for file structures */
Eric Dumazetfa3536c2006-03-26 01:37:24 -08001728kmem_cache_t *filp_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729
1730EXPORT_SYMBOL(d_genocide);
1731
1732extern void bdev_cache_init(void);
1733extern void chrdev_init(void);
1734
1735void __init vfs_caches_init_early(void)
1736{
1737 dcache_init_early();
1738 inode_init_early();
1739}
1740
1741void __init vfs_caches_init(unsigned long mempages)
1742{
1743 unsigned long reserve;
1744
1745 /* Base hash sizes on available memory, with a reserve equal to
1746 150% of current kernel size */
1747
1748 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1749 mempages -= reserve;
1750
1751 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1752 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1753
1754 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
Dipankar Sarma529bf6b2006-03-07 21:55:35 -08001755 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001756
1757 dcache_init(mempages);
1758 inode_init(mempages);
1759 files_init(mempages);
1760 mnt_init(mempages);
1761 bdev_cache_init();
1762 chrdev_init();
1763}
1764
1765EXPORT_SYMBOL(d_alloc);
1766EXPORT_SYMBOL(d_alloc_anon);
1767EXPORT_SYMBOL(d_alloc_root);
1768EXPORT_SYMBOL(d_delete);
1769EXPORT_SYMBOL(d_find_alias);
1770EXPORT_SYMBOL(d_instantiate);
1771EXPORT_SYMBOL(d_invalidate);
1772EXPORT_SYMBOL(d_lookup);
1773EXPORT_SYMBOL(d_move);
1774EXPORT_SYMBOL(d_path);
1775EXPORT_SYMBOL(d_prune_aliases);
1776EXPORT_SYMBOL(d_rehash);
1777EXPORT_SYMBOL(d_splice_alias);
1778EXPORT_SYMBOL(d_validate);
1779EXPORT_SYMBOL(dget_locked);
1780EXPORT_SYMBOL(dput);
1781EXPORT_SYMBOL(find_inode_number);
1782EXPORT_SYMBOL(have_submounts);
1783EXPORT_SYMBOL(names_cachep);
1784EXPORT_SYMBOL(shrink_dcache_parent);
1785EXPORT_SYMBOL(shrink_dcache_sb);