blob: 939584648504e6552be95e13a52b46e0d27592ea [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
37/* #define DCACHE_DEBUG 1 */
38
39int sysctl_vfs_cache_pressure = 100;
40EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
41
42 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
Adrian Bunk75c96f82005-05-05 16:16:09 -070043static seqlock_t rename_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
Linus Torvalds1da177e2005-04-16 15:20:36 -070044
45EXPORT_SYMBOL(dcache_lock);
46
47static kmem_cache_t *dentry_cache;
48
49#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
50
51/*
52 * This is the single most critical data structure when it comes
53 * to the dcache: the hashtable for lookups. Somebody should try
54 * to make this good - I've just made it work.
55 *
56 * This hash-function tries to avoid losing too many bits of hash
57 * information, yet avoid using a prime hash-size or similar.
58 */
59#define D_HASHBITS d_hash_shift
60#define D_HASHMASK d_hash_mask
61
62static unsigned int d_hash_mask;
63static unsigned int d_hash_shift;
64static struct hlist_head *dentry_hashtable;
65static LIST_HEAD(dentry_unused);
66
67/* Statistics gathering. */
68struct dentry_stat_t dentry_stat = {
69 .age_limit = 45,
70};
71
72static void d_callback(struct rcu_head *head)
73{
Eric Dumazet5160ee62006-01-08 01:03:32 -080074 struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76 if (dname_external(dentry))
77 kfree(dentry->d_name.name);
78 kmem_cache_free(dentry_cache, dentry);
79}
80
81/*
82 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
83 * inside dcache_lock.
84 */
85static void d_free(struct dentry *dentry)
86{
87 if (dentry->d_op && dentry->d_op->d_release)
88 dentry->d_op->d_release(dentry);
Eric Dumazet5160ee62006-01-08 01:03:32 -080089 call_rcu(&dentry->d_u.d_rcu, d_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -070090}
91
92/*
93 * Release the dentry's inode, using the filesystem
94 * d_iput() operation if defined.
95 * Called with dcache_lock and per dentry lock held, drops both.
96 */
Arjan van de Ven858119e2006-01-14 13:20:43 -080097static void dentry_iput(struct dentry * dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070098{
99 struct inode *inode = dentry->d_inode;
100 if (inode) {
101 dentry->d_inode = NULL;
102 list_del_init(&dentry->d_alias);
103 spin_unlock(&dentry->d_lock);
104 spin_unlock(&dcache_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700105 if (!inode->i_nlink)
106 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700107 if (dentry->d_op && dentry->d_op->d_iput)
108 dentry->d_op->d_iput(dentry, inode);
109 else
110 iput(inode);
111 } else {
112 spin_unlock(&dentry->d_lock);
113 spin_unlock(&dcache_lock);
114 }
115}
116
117/*
118 * This is dput
119 *
120 * This is complicated by the fact that we do not want to put
121 * dentries that are no longer on any hash chain on the unused
122 * list: we'd much rather just get rid of them immediately.
123 *
124 * However, that implies that we have to traverse the dentry
125 * tree upwards to the parents which might _also_ now be
126 * scheduled for deletion (it may have been only waiting for
127 * its last child to go away).
128 *
129 * This tail recursion is done by hand as we don't want to depend
130 * on the compiler to always get this right (gcc generally doesn't).
131 * Real recursion would eat up our stack space.
132 */
133
134/*
135 * dput - release a dentry
136 * @dentry: dentry to release
137 *
138 * Release a dentry. This will drop the usage count and if appropriate
139 * call the dentry unlink method as well as removing it from the queues and
140 * releasing its resources. If the parent dentries were scheduled for release
141 * they too may now get deleted.
142 *
143 * no dcache lock, please.
144 */
145
146void dput(struct dentry *dentry)
147{
148 if (!dentry)
149 return;
150
151repeat:
152 if (atomic_read(&dentry->d_count) == 1)
153 might_sleep();
154 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
155 return;
156
157 spin_lock(&dentry->d_lock);
158 if (atomic_read(&dentry->d_count)) {
159 spin_unlock(&dentry->d_lock);
160 spin_unlock(&dcache_lock);
161 return;
162 }
163
164 /*
165 * AV: ->d_delete() is _NOT_ allowed to block now.
166 */
167 if (dentry->d_op && dentry->d_op->d_delete) {
168 if (dentry->d_op->d_delete(dentry))
169 goto unhash_it;
170 }
171 /* Unreachable? Get rid of it */
172 if (d_unhashed(dentry))
173 goto kill_it;
174 if (list_empty(&dentry->d_lru)) {
175 dentry->d_flags |= DCACHE_REFERENCED;
176 list_add(&dentry->d_lru, &dentry_unused);
177 dentry_stat.nr_unused++;
178 }
179 spin_unlock(&dentry->d_lock);
180 spin_unlock(&dcache_lock);
181 return;
182
183unhash_it:
184 __d_drop(dentry);
185
186kill_it: {
187 struct dentry *parent;
188
189 /* If dentry was on d_lru list
190 * delete it from there
191 */
192 if (!list_empty(&dentry->d_lru)) {
193 list_del(&dentry->d_lru);
194 dentry_stat.nr_unused--;
195 }
Eric Dumazet5160ee62006-01-08 01:03:32 -0800196 list_del(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700197 dentry_stat.nr_dentry--; /* For d_free, below */
198 /*drops the locks, at that point nobody can reach this dentry */
199 dentry_iput(dentry);
200 parent = dentry->d_parent;
201 d_free(dentry);
202 if (dentry == parent)
203 return;
204 dentry = parent;
205 goto repeat;
206 }
207}
208
209/**
210 * d_invalidate - invalidate a dentry
211 * @dentry: dentry to invalidate
212 *
213 * Try to invalidate the dentry if it turns out to be
214 * possible. If there are other dentries that can be
215 * reached through this one we can't delete it and we
216 * return -EBUSY. On success we return 0.
217 *
218 * no dcache lock.
219 */
220
221int d_invalidate(struct dentry * dentry)
222{
223 /*
224 * If it's already been dropped, return OK.
225 */
226 spin_lock(&dcache_lock);
227 if (d_unhashed(dentry)) {
228 spin_unlock(&dcache_lock);
229 return 0;
230 }
231 /*
232 * Check whether to do a partial shrink_dcache
233 * to get rid of unused child entries.
234 */
235 if (!list_empty(&dentry->d_subdirs)) {
236 spin_unlock(&dcache_lock);
237 shrink_dcache_parent(dentry);
238 spin_lock(&dcache_lock);
239 }
240
241 /*
242 * Somebody else still using it?
243 *
244 * If it's a directory, we can't drop it
245 * for fear of somebody re-populating it
246 * with children (even though dropping it
247 * would make it unreachable from the root,
248 * we might still populate it if it was a
249 * working directory or similar).
250 */
251 spin_lock(&dentry->d_lock);
252 if (atomic_read(&dentry->d_count) > 1) {
253 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
254 spin_unlock(&dentry->d_lock);
255 spin_unlock(&dcache_lock);
256 return -EBUSY;
257 }
258 }
259
260 __d_drop(dentry);
261 spin_unlock(&dentry->d_lock);
262 spin_unlock(&dcache_lock);
263 return 0;
264}
265
266/* This should be called _only_ with dcache_lock held */
267
268static inline struct dentry * __dget_locked(struct dentry *dentry)
269{
270 atomic_inc(&dentry->d_count);
271 if (!list_empty(&dentry->d_lru)) {
272 dentry_stat.nr_unused--;
273 list_del_init(&dentry->d_lru);
274 }
275 return dentry;
276}
277
278struct dentry * dget_locked(struct dentry *dentry)
279{
280 return __dget_locked(dentry);
281}
282
283/**
284 * d_find_alias - grab a hashed alias of inode
285 * @inode: inode in question
286 * @want_discon: flag, used by d_splice_alias, to request
287 * that only a DISCONNECTED alias be returned.
288 *
289 * If inode has a hashed alias, or is a directory and has any alias,
290 * acquire the reference to alias and return it. Otherwise return NULL.
291 * Notice that if inode is a directory there can be only one alias and
292 * it can be unhashed only if it has no children, or if it is the root
293 * of a filesystem.
294 *
295 * If the inode has a DCACHE_DISCONNECTED alias, then prefer
296 * any other hashed alias over that one unless @want_discon is set,
297 * in which case only return a DCACHE_DISCONNECTED alias.
298 */
299
300static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
301{
302 struct list_head *head, *next, *tmp;
303 struct dentry *alias, *discon_alias=NULL;
304
305 head = &inode->i_dentry;
306 next = inode->i_dentry.next;
307 while (next != head) {
308 tmp = next;
309 next = tmp->next;
310 prefetch(next);
311 alias = list_entry(tmp, struct dentry, d_alias);
312 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
313 if (alias->d_flags & DCACHE_DISCONNECTED)
314 discon_alias = alias;
315 else if (!want_discon) {
316 __dget_locked(alias);
317 return alias;
318 }
319 }
320 }
321 if (discon_alias)
322 __dget_locked(discon_alias);
323 return discon_alias;
324}
325
326struct dentry * d_find_alias(struct inode *inode)
327{
David Howells214fda12006-03-25 03:06:36 -0800328 struct dentry *de = NULL;
329
330 if (!list_empty(&inode->i_dentry)) {
331 spin_lock(&dcache_lock);
332 de = __d_find_alias(inode, 0);
333 spin_unlock(&dcache_lock);
334 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700335 return de;
336}
337
338/*
339 * Try to kill dentries associated with this inode.
340 * WARNING: you must own a reference to inode.
341 */
342void d_prune_aliases(struct inode *inode)
343{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700344 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700345restart:
346 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700347 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700348 spin_lock(&dentry->d_lock);
349 if (!atomic_read(&dentry->d_count)) {
350 __dget_locked(dentry);
351 __d_drop(dentry);
352 spin_unlock(&dentry->d_lock);
353 spin_unlock(&dcache_lock);
354 dput(dentry);
355 goto restart;
356 }
357 spin_unlock(&dentry->d_lock);
358 }
359 spin_unlock(&dcache_lock);
360}
361
362/*
363 * Throw away a dentry - free the inode, dput the parent.
364 * This requires that the LRU list has already been
365 * removed.
366 * Called with dcache_lock, drops it and then regains.
367 */
368static inline void prune_one_dentry(struct dentry * dentry)
369{
370 struct dentry * parent;
371
372 __d_drop(dentry);
Eric Dumazet5160ee62006-01-08 01:03:32 -0800373 list_del(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700374 dentry_stat.nr_dentry--; /* For d_free, below */
375 dentry_iput(dentry);
376 parent = dentry->d_parent;
377 d_free(dentry);
378 if (parent != dentry)
379 dput(parent);
380 spin_lock(&dcache_lock);
381}
382
383/**
384 * prune_dcache - shrink the dcache
385 * @count: number of entries to try and free
386 *
387 * Shrink the dcache. This is done when we need
388 * more memory, or simply when we need to unmount
389 * something (at which point we need to unuse
390 * all dentries).
391 *
392 * This function may fail to free any resources if
393 * all the dentries are in use.
394 */
395
396static void prune_dcache(int count)
397{
398 spin_lock(&dcache_lock);
399 for (; count ; count--) {
400 struct dentry *dentry;
401 struct list_head *tmp;
402
403 cond_resched_lock(&dcache_lock);
404
405 tmp = dentry_unused.prev;
406 if (tmp == &dentry_unused)
407 break;
408 list_del_init(tmp);
409 prefetch(dentry_unused.prev);
410 dentry_stat.nr_unused--;
411 dentry = list_entry(tmp, struct dentry, d_lru);
412
413 spin_lock(&dentry->d_lock);
414 /*
415 * We found an inuse dentry which was not removed from
416 * dentry_unused because of laziness during lookup. Do not free
417 * it - just keep it off the dentry_unused list.
418 */
419 if (atomic_read(&dentry->d_count)) {
420 spin_unlock(&dentry->d_lock);
421 continue;
422 }
423 /* If the dentry was recently referenced, don't free it. */
424 if (dentry->d_flags & DCACHE_REFERENCED) {
425 dentry->d_flags &= ~DCACHE_REFERENCED;
426 list_add(&dentry->d_lru, &dentry_unused);
427 dentry_stat.nr_unused++;
428 spin_unlock(&dentry->d_lock);
429 continue;
430 }
431 prune_one_dentry(dentry);
432 }
433 spin_unlock(&dcache_lock);
434}
435
436/*
437 * Shrink the dcache for the specified super block.
438 * This allows us to unmount a device without disturbing
439 * the dcache for the other devices.
440 *
441 * This implementation makes just two traversals of the
442 * unused list. On the first pass we move the selected
443 * dentries to the most recent end, and on the second
444 * pass we free them. The second pass must restart after
445 * each dput(), but since the target dentries are all at
446 * the end, it's really just a single traversal.
447 */
448
449/**
450 * shrink_dcache_sb - shrink dcache for a superblock
451 * @sb: superblock
452 *
453 * Shrink the dcache for the specified super block. This
454 * is used to free the dcache before unmounting a file
455 * system
456 */
457
458void shrink_dcache_sb(struct super_block * sb)
459{
460 struct list_head *tmp, *next;
461 struct dentry *dentry;
462
463 /*
464 * Pass one ... move the dentries for the specified
465 * superblock to the most recent end of the unused list.
466 */
467 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700468 list_for_each_safe(tmp, next, &dentry_unused) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469 dentry = list_entry(tmp, struct dentry, d_lru);
470 if (dentry->d_sb != sb)
471 continue;
472 list_del(tmp);
473 list_add(tmp, &dentry_unused);
474 }
475
476 /*
477 * Pass two ... free the dentries for this superblock.
478 */
479repeat:
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700480 list_for_each_safe(tmp, next, &dentry_unused) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700481 dentry = list_entry(tmp, struct dentry, d_lru);
482 if (dentry->d_sb != sb)
483 continue;
484 dentry_stat.nr_unused--;
485 list_del_init(tmp);
486 spin_lock(&dentry->d_lock);
487 if (atomic_read(&dentry->d_count)) {
488 spin_unlock(&dentry->d_lock);
489 continue;
490 }
491 prune_one_dentry(dentry);
Kirill Korotaev2ab13462006-03-25 03:07:45 -0800492 cond_resched_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700493 goto repeat;
494 }
495 spin_unlock(&dcache_lock);
496}
497
498/*
499 * Search for at least 1 mount point in the dentry's subdirs.
500 * We descend to the next level whenever the d_subdirs
501 * list is non-empty and continue searching.
502 */
503
504/**
505 * have_submounts - check for mounts over a dentry
506 * @parent: dentry to check.
507 *
508 * Return true if the parent or its subdirectories contain
509 * a mount point
510 */
511
512int have_submounts(struct dentry *parent)
513{
514 struct dentry *this_parent = parent;
515 struct list_head *next;
516
517 spin_lock(&dcache_lock);
518 if (d_mountpoint(parent))
519 goto positive;
520repeat:
521 next = this_parent->d_subdirs.next;
522resume:
523 while (next != &this_parent->d_subdirs) {
524 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800525 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 next = tmp->next;
527 /* Have we found a mount point ? */
528 if (d_mountpoint(dentry))
529 goto positive;
530 if (!list_empty(&dentry->d_subdirs)) {
531 this_parent = dentry;
532 goto repeat;
533 }
534 }
535 /*
536 * All done at this level ... ascend and resume the search.
537 */
538 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800539 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700540 this_parent = this_parent->d_parent;
541 goto resume;
542 }
543 spin_unlock(&dcache_lock);
544 return 0; /* No mount points found in tree */
545positive:
546 spin_unlock(&dcache_lock);
547 return 1;
548}
549
550/*
551 * Search the dentry child list for the specified parent,
552 * and move any unused dentries to the end of the unused
553 * list for prune_dcache(). We descend to the next level
554 * whenever the d_subdirs list is non-empty and continue
555 * searching.
556 *
557 * It returns zero iff there are no unused children,
558 * otherwise it returns the number of children moved to
559 * the end of the unused list. This may not be the total
560 * number of unused children, because select_parent can
561 * drop the lock and return early due to latency
562 * constraints.
563 */
564static int select_parent(struct dentry * parent)
565{
566 struct dentry *this_parent = parent;
567 struct list_head *next;
568 int found = 0;
569
570 spin_lock(&dcache_lock);
571repeat:
572 next = this_parent->d_subdirs.next;
573resume:
574 while (next != &this_parent->d_subdirs) {
575 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800576 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700577 next = tmp->next;
578
579 if (!list_empty(&dentry->d_lru)) {
580 dentry_stat.nr_unused--;
581 list_del_init(&dentry->d_lru);
582 }
583 /*
584 * move only zero ref count dentries to the end
585 * of the unused list for prune_dcache
586 */
587 if (!atomic_read(&dentry->d_count)) {
588 list_add(&dentry->d_lru, dentry_unused.prev);
589 dentry_stat.nr_unused++;
590 found++;
591 }
592
593 /*
594 * We can return to the caller if we have found some (this
595 * ensures forward progress). We'll be coming back to find
596 * the rest.
597 */
598 if (found && need_resched())
599 goto out;
600
601 /*
602 * Descend a level if the d_subdirs list is non-empty.
603 */
604 if (!list_empty(&dentry->d_subdirs)) {
605 this_parent = dentry;
606#ifdef DCACHE_DEBUG
607printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
608dentry->d_parent->d_name.name, dentry->d_name.name, found);
609#endif
610 goto repeat;
611 }
612 }
613 /*
614 * All done at this level ... ascend and resume the search.
615 */
616 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800617 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618 this_parent = this_parent->d_parent;
619#ifdef DCACHE_DEBUG
620printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
621this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
622#endif
623 goto resume;
624 }
625out:
626 spin_unlock(&dcache_lock);
627 return found;
628}
629
630/**
631 * shrink_dcache_parent - prune dcache
632 * @parent: parent of entries to prune
633 *
634 * Prune the dcache to remove unused children of the parent dentry.
635 */
636
637void shrink_dcache_parent(struct dentry * parent)
638{
639 int found;
640
641 while ((found = select_parent(parent)) != 0)
642 prune_dcache(found);
643}
644
645/**
646 * shrink_dcache_anon - further prune the cache
647 * @head: head of d_hash list of dentries to prune
648 *
649 * Prune the dentries that are anonymous
650 *
Paul E. McKenney665a7582005-11-07 00:59:17 -0800651 * parsing d_hash list does not hlist_for_each_entry_rcu() as it
Linus Torvalds1da177e2005-04-16 15:20:36 -0700652 * done under dcache_lock.
653 *
654 */
655void shrink_dcache_anon(struct hlist_head *head)
656{
657 struct hlist_node *lp;
658 int found;
659 do {
660 found = 0;
661 spin_lock(&dcache_lock);
662 hlist_for_each(lp, head) {
663 struct dentry *this = hlist_entry(lp, struct dentry, d_hash);
664 if (!list_empty(&this->d_lru)) {
665 dentry_stat.nr_unused--;
666 list_del_init(&this->d_lru);
667 }
668
669 /*
670 * move only zero ref count dentries to the end
671 * of the unused list for prune_dcache
672 */
673 if (!atomic_read(&this->d_count)) {
674 list_add_tail(&this->d_lru, &dentry_unused);
675 dentry_stat.nr_unused++;
676 found++;
677 }
678 }
679 spin_unlock(&dcache_lock);
680 prune_dcache(found);
681 } while(found);
682}
683
684/*
685 * Scan `nr' dentries and return the number which remain.
686 *
687 * We need to avoid reentering the filesystem if the caller is performing a
688 * GFP_NOFS allocation attempt. One example deadlock is:
689 *
690 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
691 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
692 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
693 *
694 * In this case we return -1 to tell the caller that we baled.
695 */
Al Viro27496a82005-10-21 03:20:48 -0400696static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700697{
698 if (nr) {
699 if (!(gfp_mask & __GFP_FS))
700 return -1;
701 prune_dcache(nr);
702 }
703 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
704}
705
706/**
707 * d_alloc - allocate a dcache entry
708 * @parent: parent of entry to allocate
709 * @name: qstr of the name
710 *
711 * Allocates a dentry. It returns %NULL if there is insufficient memory
712 * available. On a success the dentry is returned. The name passed in is
713 * copied and the copy passed in may be reused after this call.
714 */
715
716struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
717{
718 struct dentry *dentry;
719 char *dname;
720
721 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
722 if (!dentry)
723 return NULL;
724
725 if (name->len > DNAME_INLINE_LEN-1) {
726 dname = kmalloc(name->len + 1, GFP_KERNEL);
727 if (!dname) {
728 kmem_cache_free(dentry_cache, dentry);
729 return NULL;
730 }
731 } else {
732 dname = dentry->d_iname;
733 }
734 dentry->d_name.name = dname;
735
736 dentry->d_name.len = name->len;
737 dentry->d_name.hash = name->hash;
738 memcpy(dname, name->name, name->len);
739 dname[name->len] = 0;
740
741 atomic_set(&dentry->d_count, 1);
742 dentry->d_flags = DCACHE_UNHASHED;
743 spin_lock_init(&dentry->d_lock);
744 dentry->d_inode = NULL;
745 dentry->d_parent = NULL;
746 dentry->d_sb = NULL;
747 dentry->d_op = NULL;
748 dentry->d_fsdata = NULL;
749 dentry->d_mounted = 0;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800750#ifdef CONFIG_PROFILING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700751 dentry->d_cookie = NULL;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800752#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 INIT_HLIST_NODE(&dentry->d_hash);
754 INIT_LIST_HEAD(&dentry->d_lru);
755 INIT_LIST_HEAD(&dentry->d_subdirs);
756 INIT_LIST_HEAD(&dentry->d_alias);
757
758 if (parent) {
759 dentry->d_parent = dget(parent);
760 dentry->d_sb = parent->d_sb;
761 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800762 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700763 }
764
765 spin_lock(&dcache_lock);
766 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -0800767 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 dentry_stat.nr_dentry++;
769 spin_unlock(&dcache_lock);
770
771 return dentry;
772}
773
774struct dentry *d_alloc_name(struct dentry *parent, const char *name)
775{
776 struct qstr q;
777
778 q.name = name;
779 q.len = strlen(name);
780 q.hash = full_name_hash(q.name, q.len);
781 return d_alloc(parent, &q);
782}
783
784/**
785 * d_instantiate - fill in inode information for a dentry
786 * @entry: dentry to complete
787 * @inode: inode to attach to this dentry
788 *
789 * Fill in inode information in the entry.
790 *
791 * This turns negative dentries into productive full members
792 * of society.
793 *
794 * NOTE! This assumes that the inode count has been incremented
795 * (or otherwise set) by the caller to indicate that it is now
796 * in use by the dcache.
797 */
798
799void d_instantiate(struct dentry *entry, struct inode * inode)
800{
801 if (!list_empty(&entry->d_alias)) BUG();
802 spin_lock(&dcache_lock);
803 if (inode)
804 list_add(&entry->d_alias, &inode->i_dentry);
805 entry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800806 fsnotify_d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807 spin_unlock(&dcache_lock);
808 security_d_instantiate(entry, inode);
809}
810
811/**
812 * d_instantiate_unique - instantiate a non-aliased dentry
813 * @entry: dentry to instantiate
814 * @inode: inode to attach to this dentry
815 *
816 * Fill in inode information in the entry. On success, it returns NULL.
817 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -0800818 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700819 *
820 * Note that in order to avoid conflicts with rename() etc, the caller
821 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -0800822 *
823 * This also assumes that the inode count has been incremented
824 * (or otherwise set) by the caller to indicate that it is now
825 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700826 */
827struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
828{
829 struct dentry *alias;
830 int len = entry->d_name.len;
831 const char *name = entry->d_name.name;
832 unsigned int hash = entry->d_name.hash;
833
834 BUG_ON(!list_empty(&entry->d_alias));
835 spin_lock(&dcache_lock);
836 if (!inode)
837 goto do_negative;
838 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
839 struct qstr *qstr = &alias->d_name;
840
841 if (qstr->hash != hash)
842 continue;
843 if (alias->d_parent != entry->d_parent)
844 continue;
845 if (qstr->len != len)
846 continue;
847 if (memcmp(qstr->name, name, len))
848 continue;
849 dget_locked(alias);
850 spin_unlock(&dcache_lock);
851 BUG_ON(!d_unhashed(alias));
Oleg Drokine866cfa2006-01-09 20:52:51 -0800852 iput(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700853 return alias;
854 }
855 list_add(&entry->d_alias, &inode->i_dentry);
856do_negative:
857 entry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800858 fsnotify_d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859 spin_unlock(&dcache_lock);
860 security_d_instantiate(entry, inode);
861 return NULL;
862}
863EXPORT_SYMBOL(d_instantiate_unique);
864
865/**
866 * d_alloc_root - allocate root dentry
867 * @root_inode: inode to allocate the root for
868 *
869 * Allocate a root ("/") dentry for the inode given. The inode is
870 * instantiated and returned. %NULL is returned if there is insufficient
871 * memory or the inode passed is %NULL.
872 */
873
874struct dentry * d_alloc_root(struct inode * root_inode)
875{
876 struct dentry *res = NULL;
877
878 if (root_inode) {
879 static const struct qstr name = { .name = "/", .len = 1 };
880
881 res = d_alloc(NULL, &name);
882 if (res) {
883 res->d_sb = root_inode->i_sb;
884 res->d_parent = res;
885 d_instantiate(res, root_inode);
886 }
887 }
888 return res;
889}
890
891static inline struct hlist_head *d_hash(struct dentry *parent,
892 unsigned long hash)
893{
894 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
895 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
896 return dentry_hashtable + (hash & D_HASHMASK);
897}
898
899/**
900 * d_alloc_anon - allocate an anonymous dentry
901 * @inode: inode to allocate the dentry for
902 *
903 * This is similar to d_alloc_root. It is used by filesystems when
904 * creating a dentry for a given inode, often in the process of
905 * mapping a filehandle to a dentry. The returned dentry may be
906 * anonymous, or may have a full name (if the inode was already
907 * in the cache). The file system may need to make further
908 * efforts to connect this dentry into the dcache properly.
909 *
910 * When called on a directory inode, we must ensure that
911 * the inode only ever has one dentry. If a dentry is
912 * found, that is returned instead of allocating a new one.
913 *
914 * On successful return, the reference to the inode has been transferred
915 * to the dentry. If %NULL is returned (indicating kmalloc failure),
916 * the reference on the inode has not been released.
917 */
918
919struct dentry * d_alloc_anon(struct inode *inode)
920{
921 static const struct qstr anonstring = { .name = "" };
922 struct dentry *tmp;
923 struct dentry *res;
924
925 if ((res = d_find_alias(inode))) {
926 iput(inode);
927 return res;
928 }
929
930 tmp = d_alloc(NULL, &anonstring);
931 if (!tmp)
932 return NULL;
933
934 tmp->d_parent = tmp; /* make sure dput doesn't croak */
935
936 spin_lock(&dcache_lock);
937 res = __d_find_alias(inode, 0);
938 if (!res) {
939 /* attach a disconnected dentry */
940 res = tmp;
941 tmp = NULL;
942 spin_lock(&res->d_lock);
943 res->d_sb = inode->i_sb;
944 res->d_parent = res;
945 res->d_inode = inode;
946 res->d_flags |= DCACHE_DISCONNECTED;
947 res->d_flags &= ~DCACHE_UNHASHED;
948 list_add(&res->d_alias, &inode->i_dentry);
949 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
950 spin_unlock(&res->d_lock);
951
952 inode = NULL; /* don't drop reference */
953 }
954 spin_unlock(&dcache_lock);
955
956 if (inode)
957 iput(inode);
958 if (tmp)
959 dput(tmp);
960 return res;
961}
962
963
964/**
965 * d_splice_alias - splice a disconnected dentry into the tree if one exists
966 * @inode: the inode which may have a disconnected dentry
967 * @dentry: a negative dentry which we want to point to the inode.
968 *
969 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
970 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
971 * and return it, else simply d_add the inode to the dentry and return NULL.
972 *
973 * This is needed in the lookup routine of any filesystem that is exportable
974 * (via knfsd) so that we can build dcache paths to directories effectively.
975 *
976 * If a dentry was found and moved, then it is returned. Otherwise NULL
977 * is returned. This matches the expected return value of ->lookup.
978 *
979 */
980struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
981{
982 struct dentry *new = NULL;
983
984 if (inode) {
985 spin_lock(&dcache_lock);
986 new = __d_find_alias(inode, 1);
987 if (new) {
988 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Pigginc32ccd82006-03-25 03:07:09 -0800989 fsnotify_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700990 spin_unlock(&dcache_lock);
991 security_d_instantiate(new, inode);
992 d_rehash(dentry);
993 d_move(new, dentry);
994 iput(inode);
995 } else {
996 /* d_instantiate takes dcache_lock, so we do it by hand */
997 list_add(&dentry->d_alias, &inode->i_dentry);
998 dentry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800999 fsnotify_d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 spin_unlock(&dcache_lock);
1001 security_d_instantiate(dentry, inode);
1002 d_rehash(dentry);
1003 }
1004 } else
1005 d_add(dentry, inode);
1006 return new;
1007}
1008
1009
1010/**
1011 * d_lookup - search for a dentry
1012 * @parent: parent dentry
1013 * @name: qstr of name we wish to find
1014 *
1015 * Searches the children of the parent dentry for the name in question. If
1016 * the dentry is found its reference count is incremented and the dentry
1017 * is returned. The caller must use d_put to free the entry when it has
1018 * finished using it. %NULL is returned on failure.
1019 *
1020 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1021 * Memory barriers are used while updating and doing lockless traversal.
1022 * To avoid races with d_move while rename is happening, d_lock is used.
1023 *
1024 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1025 * and name pointer in one structure pointed by d_qstr.
1026 *
1027 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1028 * lookup is going on.
1029 *
1030 * dentry_unused list is not updated even if lookup finds the required dentry
1031 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1032 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1033 * acquisition.
1034 *
1035 * d_lookup() is protected against the concurrent renames in some unrelated
1036 * directory using the seqlockt_t rename_lock.
1037 */
1038
1039struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1040{
1041 struct dentry * dentry = NULL;
1042 unsigned long seq;
1043
1044 do {
1045 seq = read_seqbegin(&rename_lock);
1046 dentry = __d_lookup(parent, name);
1047 if (dentry)
1048 break;
1049 } while (read_seqretry(&rename_lock, seq));
1050 return dentry;
1051}
1052
1053struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1054{
1055 unsigned int len = name->len;
1056 unsigned int hash = name->hash;
1057 const unsigned char *str = name->name;
1058 struct hlist_head *head = d_hash(parent,hash);
1059 struct dentry *found = NULL;
1060 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001061 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062
1063 rcu_read_lock();
1064
Paul E. McKenney665a7582005-11-07 00:59:17 -08001065 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 struct qstr *qstr;
1067
Linus Torvalds1da177e2005-04-16 15:20:36 -07001068 if (dentry->d_name.hash != hash)
1069 continue;
1070 if (dentry->d_parent != parent)
1071 continue;
1072
1073 spin_lock(&dentry->d_lock);
1074
1075 /*
1076 * Recheck the dentry after taking the lock - d_move may have
1077 * changed things. Don't bother checking the hash because we're
1078 * about to compare the whole name anyway.
1079 */
1080 if (dentry->d_parent != parent)
1081 goto next;
1082
1083 /*
1084 * It is safe to compare names since d_move() cannot
1085 * change the qstr (protected by d_lock).
1086 */
1087 qstr = &dentry->d_name;
1088 if (parent->d_op && parent->d_op->d_compare) {
1089 if (parent->d_op->d_compare(parent, qstr, name))
1090 goto next;
1091 } else {
1092 if (qstr->len != len)
1093 goto next;
1094 if (memcmp(qstr->name, str, len))
1095 goto next;
1096 }
1097
1098 if (!d_unhashed(dentry)) {
1099 atomic_inc(&dentry->d_count);
1100 found = dentry;
1101 }
1102 spin_unlock(&dentry->d_lock);
1103 break;
1104next:
1105 spin_unlock(&dentry->d_lock);
1106 }
1107 rcu_read_unlock();
1108
1109 return found;
1110}
1111
1112/**
1113 * d_validate - verify dentry provided from insecure source
1114 * @dentry: The dentry alleged to be valid child of @dparent
1115 * @dparent: The parent dentry (known to be valid)
1116 * @hash: Hash of the dentry
1117 * @len: Length of the name
1118 *
1119 * An insecure source has sent us a dentry, here we verify it and dget() it.
1120 * This is used by ncpfs in its readdir implementation.
1121 * Zero is returned in the dentry is invalid.
1122 */
1123
1124int d_validate(struct dentry *dentry, struct dentry *dparent)
1125{
1126 struct hlist_head *base;
1127 struct hlist_node *lhp;
1128
1129 /* Check whether the ptr might be valid at all.. */
1130 if (!kmem_ptr_validate(dentry_cache, dentry))
1131 goto out;
1132
1133 if (dentry->d_parent != dparent)
1134 goto out;
1135
1136 spin_lock(&dcache_lock);
1137 base = d_hash(dparent, dentry->d_name.hash);
1138 hlist_for_each(lhp,base) {
Paul E. McKenney665a7582005-11-07 00:59:17 -08001139 /* hlist_for_each_entry_rcu() not required for d_hash list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001140 * as it is parsed under dcache_lock
1141 */
1142 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1143 __dget_locked(dentry);
1144 spin_unlock(&dcache_lock);
1145 return 1;
1146 }
1147 }
1148 spin_unlock(&dcache_lock);
1149out:
1150 return 0;
1151}
1152
1153/*
1154 * When a file is deleted, we have two options:
1155 * - turn this dentry into a negative dentry
1156 * - unhash this dentry and free it.
1157 *
1158 * Usually, we want to just turn this into
1159 * a negative dentry, but if anybody else is
1160 * currently using the dentry or the inode
1161 * we can't do that and we fall back on removing
1162 * it from the hash queues and waiting for
1163 * it to be deleted later when it has no users
1164 */
1165
1166/**
1167 * d_delete - delete a dentry
1168 * @dentry: The dentry to delete
1169 *
1170 * Turn the dentry into a negative dentry if possible, otherwise
1171 * remove it from the hash queues so it can be deleted later
1172 */
1173
1174void d_delete(struct dentry * dentry)
1175{
John McCutchan7a91bf72005-08-08 13:52:16 -04001176 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001177 /*
1178 * Are we the only user?
1179 */
1180 spin_lock(&dcache_lock);
1181 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001182 isdir = S_ISDIR(dentry->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 if (atomic_read(&dentry->d_count) == 1) {
Nick Pigginc32ccd82006-03-25 03:07:09 -08001184 /* remove this and other inotify debug checks after 2.6.18 */
1185 dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
1186
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001188 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 return;
1190 }
1191
1192 if (!d_unhashed(dentry))
1193 __d_drop(dentry);
1194
1195 spin_unlock(&dentry->d_lock);
1196 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001197
1198 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199}
1200
1201static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1202{
1203
1204 entry->d_flags &= ~DCACHE_UNHASHED;
1205 hlist_add_head_rcu(&entry->d_hash, list);
1206}
1207
1208/**
1209 * d_rehash - add an entry back to the hash
1210 * @entry: dentry to add to the hash
1211 *
1212 * Adds a dentry to the hash according to its name.
1213 */
1214
1215void d_rehash(struct dentry * entry)
1216{
1217 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1218
1219 spin_lock(&dcache_lock);
1220 spin_lock(&entry->d_lock);
1221 __d_rehash(entry, list);
1222 spin_unlock(&entry->d_lock);
1223 spin_unlock(&dcache_lock);
1224}
1225
1226#define do_switch(x,y) do { \
1227 __typeof__ (x) __tmp = x; \
1228 x = y; y = __tmp; } while (0)
1229
1230/*
1231 * When switching names, the actual string doesn't strictly have to
1232 * be preserved in the target - because we're dropping the target
1233 * anyway. As such, we can just do a simple memcpy() to copy over
1234 * the new name before we switch.
1235 *
1236 * Note that we have to be a lot more careful about getting the hash
1237 * switched - we have to switch the hash value properly even if it
1238 * then no longer matches the actual (corrupted) string of the target.
1239 * The hash value has to match the hash queue that the dentry is on..
1240 */
1241static void switch_names(struct dentry *dentry, struct dentry *target)
1242{
1243 if (dname_external(target)) {
1244 if (dname_external(dentry)) {
1245 /*
1246 * Both external: swap the pointers
1247 */
1248 do_switch(target->d_name.name, dentry->d_name.name);
1249 } else {
1250 /*
1251 * dentry:internal, target:external. Steal target's
1252 * storage and make target internal.
1253 */
1254 dentry->d_name.name = target->d_name.name;
1255 target->d_name.name = target->d_iname;
1256 }
1257 } else {
1258 if (dname_external(dentry)) {
1259 /*
1260 * dentry:external, target:internal. Give dentry's
1261 * storage to target and make dentry internal
1262 */
1263 memcpy(dentry->d_iname, target->d_name.name,
1264 target->d_name.len + 1);
1265 target->d_name.name = dentry->d_name.name;
1266 dentry->d_name.name = dentry->d_iname;
1267 } else {
1268 /*
1269 * Both are internal. Just copy target to dentry
1270 */
1271 memcpy(dentry->d_iname, target->d_name.name,
1272 target->d_name.len + 1);
1273 }
1274 }
1275}
1276
1277/*
1278 * We cannibalize "target" when moving dentry on top of it,
1279 * because it's going to be thrown away anyway. We could be more
1280 * polite about it, though.
1281 *
1282 * This forceful removal will result in ugly /proc output if
1283 * somebody holds a file open that got deleted due to a rename.
1284 * We could be nicer about the deleted file, and let it show
1285 * up under the name it got deleted rather than the name that
1286 * deleted it.
1287 */
1288
1289/**
1290 * d_move - move a dentry
1291 * @dentry: entry to move
1292 * @target: new dentry
1293 *
1294 * Update the dcache to reflect the move of a file name. Negative
1295 * dcache entries should not be moved in this way.
1296 */
1297
1298void d_move(struct dentry * dentry, struct dentry * target)
1299{
1300 struct hlist_head *list;
1301
1302 if (!dentry->d_inode)
1303 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1304
1305 spin_lock(&dcache_lock);
1306 write_seqlock(&rename_lock);
1307 /*
1308 * XXXX: do we really need to take target->d_lock?
1309 */
1310 if (target < dentry) {
1311 spin_lock(&target->d_lock);
1312 spin_lock(&dentry->d_lock);
1313 } else {
1314 spin_lock(&dentry->d_lock);
1315 spin_lock(&target->d_lock);
1316 }
1317
1318 /* Move the dentry to the target hash queue, if on different bucket */
1319 if (dentry->d_flags & DCACHE_UNHASHED)
1320 goto already_unhashed;
1321
1322 hlist_del_rcu(&dentry->d_hash);
1323
1324already_unhashed:
1325 list = d_hash(target->d_parent, target->d_name.hash);
1326 __d_rehash(dentry, list);
1327
1328 /* Unhash the target: dput() will then get rid of it */
1329 __d_drop(target);
1330
Eric Dumazet5160ee62006-01-08 01:03:32 -08001331 list_del(&dentry->d_u.d_child);
1332 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001333
1334 /* Switch the names.. */
1335 switch_names(dentry, target);
1336 do_switch(dentry->d_name.len, target->d_name.len);
1337 do_switch(dentry->d_name.hash, target->d_name.hash);
1338
1339 /* ... and switch the parents */
1340 if (IS_ROOT(dentry)) {
1341 dentry->d_parent = target->d_parent;
1342 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001343 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001344 } else {
1345 do_switch(dentry->d_parent, target->d_parent);
1346
1347 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001348 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001349 }
1350
Eric Dumazet5160ee62006-01-08 01:03:32 -08001351 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001352 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001353 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001354 spin_unlock(&dentry->d_lock);
1355 write_sequnlock(&rename_lock);
1356 spin_unlock(&dcache_lock);
1357}
1358
1359/**
1360 * d_path - return the path of a dentry
1361 * @dentry: dentry to report
1362 * @vfsmnt: vfsmnt to which the dentry belongs
1363 * @root: root dentry
1364 * @rootmnt: vfsmnt to which the root dentry belongs
1365 * @buffer: buffer to return value in
1366 * @buflen: buffer length
1367 *
1368 * Convert a dentry into an ASCII path name. If the entry has been deleted
1369 * the string " (deleted)" is appended. Note that this is ambiguous.
1370 *
1371 * Returns the buffer or an error code if the path was too long.
1372 *
1373 * "buflen" should be positive. Caller holds the dcache_lock.
1374 */
1375static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1376 struct dentry *root, struct vfsmount *rootmnt,
1377 char *buffer, int buflen)
1378{
1379 char * end = buffer+buflen;
1380 char * retval;
1381 int namelen;
1382
1383 *--end = '\0';
1384 buflen--;
1385 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1386 buflen -= 10;
1387 end -= 10;
1388 if (buflen < 0)
1389 goto Elong;
1390 memcpy(end, " (deleted)", 10);
1391 }
1392
1393 if (buflen < 1)
1394 goto Elong;
1395 /* Get '/' right */
1396 retval = end-1;
1397 *retval = '/';
1398
1399 for (;;) {
1400 struct dentry * parent;
1401
1402 if (dentry == root && vfsmnt == rootmnt)
1403 break;
1404 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1405 /* Global root? */
1406 spin_lock(&vfsmount_lock);
1407 if (vfsmnt->mnt_parent == vfsmnt) {
1408 spin_unlock(&vfsmount_lock);
1409 goto global_root;
1410 }
1411 dentry = vfsmnt->mnt_mountpoint;
1412 vfsmnt = vfsmnt->mnt_parent;
1413 spin_unlock(&vfsmount_lock);
1414 continue;
1415 }
1416 parent = dentry->d_parent;
1417 prefetch(parent);
1418 namelen = dentry->d_name.len;
1419 buflen -= namelen + 1;
1420 if (buflen < 0)
1421 goto Elong;
1422 end -= namelen;
1423 memcpy(end, dentry->d_name.name, namelen);
1424 *--end = '/';
1425 retval = end;
1426 dentry = parent;
1427 }
1428
1429 return retval;
1430
1431global_root:
1432 namelen = dentry->d_name.len;
1433 buflen -= namelen;
1434 if (buflen < 0)
1435 goto Elong;
1436 retval -= namelen-1; /* hit the slash */
1437 memcpy(retval, dentry->d_name.name, namelen);
1438 return retval;
1439Elong:
1440 return ERR_PTR(-ENAMETOOLONG);
1441}
1442
1443/* write full pathname into buffer and return start of pathname */
1444char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1445 char *buf, int buflen)
1446{
1447 char *res;
1448 struct vfsmount *rootmnt;
1449 struct dentry *root;
1450
1451 read_lock(&current->fs->lock);
1452 rootmnt = mntget(current->fs->rootmnt);
1453 root = dget(current->fs->root);
1454 read_unlock(&current->fs->lock);
1455 spin_lock(&dcache_lock);
1456 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1457 spin_unlock(&dcache_lock);
1458 dput(root);
1459 mntput(rootmnt);
1460 return res;
1461}
1462
1463/*
1464 * NOTE! The user-level library version returns a
1465 * character pointer. The kernel system call just
1466 * returns the length of the buffer filled (which
1467 * includes the ending '\0' character), or a negative
1468 * error value. So libc would do something like
1469 *
1470 * char *getcwd(char * buf, size_t size)
1471 * {
1472 * int retval;
1473 *
1474 * retval = sys_getcwd(buf, size);
1475 * if (retval >= 0)
1476 * return buf;
1477 * errno = -retval;
1478 * return NULL;
1479 * }
1480 */
1481asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1482{
1483 int error;
1484 struct vfsmount *pwdmnt, *rootmnt;
1485 struct dentry *pwd, *root;
1486 char *page = (char *) __get_free_page(GFP_USER);
1487
1488 if (!page)
1489 return -ENOMEM;
1490
1491 read_lock(&current->fs->lock);
1492 pwdmnt = mntget(current->fs->pwdmnt);
1493 pwd = dget(current->fs->pwd);
1494 rootmnt = mntget(current->fs->rootmnt);
1495 root = dget(current->fs->root);
1496 read_unlock(&current->fs->lock);
1497
1498 error = -ENOENT;
1499 /* Has the current directory has been unlinked? */
1500 spin_lock(&dcache_lock);
1501 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1502 unsigned long len;
1503 char * cwd;
1504
1505 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1506 spin_unlock(&dcache_lock);
1507
1508 error = PTR_ERR(cwd);
1509 if (IS_ERR(cwd))
1510 goto out;
1511
1512 error = -ERANGE;
1513 len = PAGE_SIZE + page - cwd;
1514 if (len <= size) {
1515 error = len;
1516 if (copy_to_user(buf, cwd, len))
1517 error = -EFAULT;
1518 }
1519 } else
1520 spin_unlock(&dcache_lock);
1521
1522out:
1523 dput(pwd);
1524 mntput(pwdmnt);
1525 dput(root);
1526 mntput(rootmnt);
1527 free_page((unsigned long) page);
1528 return error;
1529}
1530
1531/*
1532 * Test whether new_dentry is a subdirectory of old_dentry.
1533 *
1534 * Trivially implemented using the dcache structure
1535 */
1536
1537/**
1538 * is_subdir - is new dentry a subdirectory of old_dentry
1539 * @new_dentry: new dentry
1540 * @old_dentry: old dentry
1541 *
1542 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1543 * Returns 0 otherwise.
1544 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1545 */
1546
1547int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1548{
1549 int result;
1550 struct dentry * saved = new_dentry;
1551 unsigned long seq;
1552
1553 /* need rcu_readlock to protect against the d_parent trashing due to
1554 * d_move
1555 */
1556 rcu_read_lock();
1557 do {
1558 /* for restarting inner loop in case of seq retry */
1559 new_dentry = saved;
1560 result = 0;
1561 seq = read_seqbegin(&rename_lock);
1562 for (;;) {
1563 if (new_dentry != old_dentry) {
1564 struct dentry * parent = new_dentry->d_parent;
1565 if (parent == new_dentry)
1566 break;
1567 new_dentry = parent;
1568 continue;
1569 }
1570 result = 1;
1571 break;
1572 }
1573 } while (read_seqretry(&rename_lock, seq));
1574 rcu_read_unlock();
1575
1576 return result;
1577}
1578
1579void d_genocide(struct dentry *root)
1580{
1581 struct dentry *this_parent = root;
1582 struct list_head *next;
1583
1584 spin_lock(&dcache_lock);
1585repeat:
1586 next = this_parent->d_subdirs.next;
1587resume:
1588 while (next != &this_parent->d_subdirs) {
1589 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001590 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001591 next = tmp->next;
1592 if (d_unhashed(dentry)||!dentry->d_inode)
1593 continue;
1594 if (!list_empty(&dentry->d_subdirs)) {
1595 this_parent = dentry;
1596 goto repeat;
1597 }
1598 atomic_dec(&dentry->d_count);
1599 }
1600 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001601 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001602 atomic_dec(&this_parent->d_count);
1603 this_parent = this_parent->d_parent;
1604 goto resume;
1605 }
1606 spin_unlock(&dcache_lock);
1607}
1608
1609/**
1610 * find_inode_number - check for dentry with name
1611 * @dir: directory to check
1612 * @name: Name to find.
1613 *
1614 * Check whether a dentry already exists for the given name,
1615 * and return the inode number if it has an inode. Otherwise
1616 * 0 is returned.
1617 *
1618 * This routine is used to post-process directory listings for
1619 * filesystems using synthetic inode numbers, and is necessary
1620 * to keep getcwd() working.
1621 */
1622
1623ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1624{
1625 struct dentry * dentry;
1626 ino_t ino = 0;
1627
1628 /*
1629 * Check for a fs-specific hash function. Note that we must
1630 * calculate the standard hash first, as the d_op->d_hash()
1631 * routine may choose to leave the hash value unchanged.
1632 */
1633 name->hash = full_name_hash(name->name, name->len);
1634 if (dir->d_op && dir->d_op->d_hash)
1635 {
1636 if (dir->d_op->d_hash(dir, name) != 0)
1637 goto out;
1638 }
1639
1640 dentry = d_lookup(dir, name);
1641 if (dentry)
1642 {
1643 if (dentry->d_inode)
1644 ino = dentry->d_inode->i_ino;
1645 dput(dentry);
1646 }
1647out:
1648 return ino;
1649}
1650
1651static __initdata unsigned long dhash_entries;
1652static int __init set_dhash_entries(char *str)
1653{
1654 if (!str)
1655 return 0;
1656 dhash_entries = simple_strtoul(str, &str, 0);
1657 return 1;
1658}
1659__setup("dhash_entries=", set_dhash_entries);
1660
1661static void __init dcache_init_early(void)
1662{
1663 int loop;
1664
1665 /* If hashes are distributed across NUMA nodes, defer
1666 * hash allocation until vmalloc space is available.
1667 */
1668 if (hashdist)
1669 return;
1670
1671 dentry_hashtable =
1672 alloc_large_system_hash("Dentry cache",
1673 sizeof(struct hlist_head),
1674 dhash_entries,
1675 13,
1676 HASH_EARLY,
1677 &d_hash_shift,
1678 &d_hash_mask,
1679 0);
1680
1681 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1682 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1683}
1684
1685static void __init dcache_init(unsigned long mempages)
1686{
1687 int loop;
1688
1689 /*
1690 * A constructor could be added for stable state like the lists,
1691 * but it is probably not worth it because of the cache nature
1692 * of the dcache.
1693 */
1694 dentry_cache = kmem_cache_create("dentry_cache",
1695 sizeof(struct dentry),
1696 0,
Paul Jacksonb0196002006-03-24 03:16:09 -08001697 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1698 SLAB_MEM_SPREAD),
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 NULL, NULL);
1700
1701 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1702
1703 /* Hash may have been set up in dcache_init_early */
1704 if (!hashdist)
1705 return;
1706
1707 dentry_hashtable =
1708 alloc_large_system_hash("Dentry cache",
1709 sizeof(struct hlist_head),
1710 dhash_entries,
1711 13,
1712 0,
1713 &d_hash_shift,
1714 &d_hash_mask,
1715 0);
1716
1717 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1718 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1719}
1720
1721/* SLAB cache for __getname() consumers */
1722kmem_cache_t *names_cachep;
1723
1724/* SLAB cache for file structures */
1725kmem_cache_t *filp_cachep;
1726
1727EXPORT_SYMBOL(d_genocide);
1728
1729extern void bdev_cache_init(void);
1730extern void chrdev_init(void);
1731
1732void __init vfs_caches_init_early(void)
1733{
1734 dcache_init_early();
1735 inode_init_early();
1736}
1737
1738void __init vfs_caches_init(unsigned long mempages)
1739{
1740 unsigned long reserve;
1741
1742 /* Base hash sizes on available memory, with a reserve equal to
1743 150% of current kernel size */
1744
1745 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1746 mempages -= reserve;
1747
1748 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1749 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1750
1751 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
Dipankar Sarma529bf6b2006-03-07 21:55:35 -08001752 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001753
1754 dcache_init(mempages);
1755 inode_init(mempages);
1756 files_init(mempages);
1757 mnt_init(mempages);
1758 bdev_cache_init();
1759 chrdev_init();
1760}
1761
1762EXPORT_SYMBOL(d_alloc);
1763EXPORT_SYMBOL(d_alloc_anon);
1764EXPORT_SYMBOL(d_alloc_root);
1765EXPORT_SYMBOL(d_delete);
1766EXPORT_SYMBOL(d_find_alias);
1767EXPORT_SYMBOL(d_instantiate);
1768EXPORT_SYMBOL(d_invalidate);
1769EXPORT_SYMBOL(d_lookup);
1770EXPORT_SYMBOL(d_move);
1771EXPORT_SYMBOL(d_path);
1772EXPORT_SYMBOL(d_prune_aliases);
1773EXPORT_SYMBOL(d_rehash);
1774EXPORT_SYMBOL(d_splice_alias);
1775EXPORT_SYMBOL(d_validate);
1776EXPORT_SYMBOL(dget_locked);
1777EXPORT_SYMBOL(dput);
1778EXPORT_SYMBOL(find_inode_number);
1779EXPORT_SYMBOL(have_submounts);
1780EXPORT_SYMBOL(names_cachep);
1781EXPORT_SYMBOL(shrink_dcache_parent);
1782EXPORT_SYMBOL(shrink_dcache_sb);