[PATCH] Move cond_resched() after iput() in sync_sb_inodes()
[GitHub/mt8127/android_kernel_alcatel_ttab.git] / fs / dcache.c
CommitLineData
1da177e4
LT
1/*
2 * fs/dcache.c
3 *
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
7 */
8
9/*
10 * Notes on the allocation strategy:
11 *
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
15 */
16
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>
7a91bf7f 22#include <linux/fsnotify.h>
1da177e4
LT
23#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);
75c96f85 43static seqlock_t rename_lock __cacheline_aligned_in_smp = SEQLOCK_UNLOCKED;
1da177e4
LT
44
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{
5160ee6f 74 struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
1da177e4
LT
75
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);
5160ee6f 89 call_rcu(&dentry->d_u.d_rcu, d_callback);
1da177e4
LT
90}
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 */
858119e1 97static void dentry_iput(struct dentry * dentry)
1da177e4
LT
98{
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);
f805fbda
LT
105 if (!inode->i_nlink)
106 fsnotify_inoderemove(inode);
1da177e4
LT
107 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 }
5160ee6f 196 list_del(&dentry->d_u.d_child);
1da177e4
LT
197 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{
214fda1f
DH
328 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 }
1da177e4
LT
335 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{
0cdca3f9 344 struct dentry *dentry;
1da177e4
LT
345restart:
346 spin_lock(&dcache_lock);
0cdca3f9 347 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
1da177e4
LT
348 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);
5160ee6f 373 list_del(&dentry->d_u.d_child);
1da177e4
LT
374 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);
0cdca3f9 468 list_for_each_safe(tmp, next, &dentry_unused) {
1da177e4
LT
469 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:
0cdca3f9 480 list_for_each_safe(tmp, next, &dentry_unused) {
1da177e4
LT
481 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);
492 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;
5160ee6f 524 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1da177e4
LT
525 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) {
5160ee6f 538 next = this_parent->d_u.d_child.next;
1da177e4
LT
539 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;
5160ee6f 575 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1da177e4
LT
576 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;
605#ifdef DCACHE_DEBUG
606printk(KERN_DEBUG "select_parent: descending to %s/%s, found=%d\n",
607dentry->d_parent->d_name.name, dentry->d_name.name, found);
608#endif
609 goto repeat;
610 }
611 }
612 /*
613 * All done at this level ... ascend and resume the search.
614 */
615 if (this_parent != parent) {
5160ee6f 616 next = this_parent->d_u.d_child.next;
1da177e4
LT
617 this_parent = this_parent->d_parent;
618#ifdef DCACHE_DEBUG
619printk(KERN_DEBUG "select_parent: ascending to %s/%s, found=%d\n",
620this_parent->d_parent->d_name.name, this_parent->d_name.name, found);
621#endif
622 goto resume;
623 }
624out:
625 spin_unlock(&dcache_lock);
626 return found;
627}
628
629/**
630 * shrink_dcache_parent - prune dcache
631 * @parent: parent of entries to prune
632 *
633 * Prune the dcache to remove unused children of the parent dentry.
634 */
635
636void shrink_dcache_parent(struct dentry * parent)
637{
638 int found;
639
640 while ((found = select_parent(parent)) != 0)
641 prune_dcache(found);
642}
643
644/**
645 * shrink_dcache_anon - further prune the cache
646 * @head: head of d_hash list of dentries to prune
647 *
648 * Prune the dentries that are anonymous
649 *
665a7583 650 * parsing d_hash list does not hlist_for_each_entry_rcu() as it
1da177e4
LT
651 * done under dcache_lock.
652 *
653 */
654void shrink_dcache_anon(struct hlist_head *head)
655{
656 struct hlist_node *lp;
657 int found;
658 do {
659 found = 0;
660 spin_lock(&dcache_lock);
661 hlist_for_each(lp, head) {
662 struct dentry *this = hlist_entry(lp, struct dentry, d_hash);
663 if (!list_empty(&this->d_lru)) {
664 dentry_stat.nr_unused--;
665 list_del_init(&this->d_lru);
666 }
667
668 /*
669 * move only zero ref count dentries to the end
670 * of the unused list for prune_dcache
671 */
672 if (!atomic_read(&this->d_count)) {
673 list_add_tail(&this->d_lru, &dentry_unused);
674 dentry_stat.nr_unused++;
675 found++;
676 }
677 }
678 spin_unlock(&dcache_lock);
679 prune_dcache(found);
680 } while(found);
681}
682
683/*
684 * Scan `nr' dentries and return the number which remain.
685 *
686 * We need to avoid reentering the filesystem if the caller is performing a
687 * GFP_NOFS allocation attempt. One example deadlock is:
688 *
689 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
690 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
691 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
692 *
693 * In this case we return -1 to tell the caller that we baled.
694 */
27496a8c 695static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
1da177e4
LT
696{
697 if (nr) {
698 if (!(gfp_mask & __GFP_FS))
699 return -1;
700 prune_dcache(nr);
701 }
702 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
703}
704
705/**
706 * d_alloc - allocate a dcache entry
707 * @parent: parent of entry to allocate
708 * @name: qstr of the name
709 *
710 * Allocates a dentry. It returns %NULL if there is insufficient memory
711 * available. On a success the dentry is returned. The name passed in is
712 * copied and the copy passed in may be reused after this call.
713 */
714
715struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
716{
717 struct dentry *dentry;
718 char *dname;
719
720 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
721 if (!dentry)
722 return NULL;
723
724 if (name->len > DNAME_INLINE_LEN-1) {
725 dname = kmalloc(name->len + 1, GFP_KERNEL);
726 if (!dname) {
727 kmem_cache_free(dentry_cache, dentry);
728 return NULL;
729 }
730 } else {
731 dname = dentry->d_iname;
732 }
733 dentry->d_name.name = dname;
734
735 dentry->d_name.len = name->len;
736 dentry->d_name.hash = name->hash;
737 memcpy(dname, name->name, name->len);
738 dname[name->len] = 0;
739
740 atomic_set(&dentry->d_count, 1);
741 dentry->d_flags = DCACHE_UNHASHED;
742 spin_lock_init(&dentry->d_lock);
743 dentry->d_inode = NULL;
744 dentry->d_parent = NULL;
745 dentry->d_sb = NULL;
746 dentry->d_op = NULL;
747 dentry->d_fsdata = NULL;
748 dentry->d_mounted = 0;
47ba87e0 749#ifdef CONFIG_PROFILING
1da177e4 750 dentry->d_cookie = NULL;
47ba87e0 751#endif
1da177e4
LT
752 INIT_HLIST_NODE(&dentry->d_hash);
753 INIT_LIST_HEAD(&dentry->d_lru);
754 INIT_LIST_HEAD(&dentry->d_subdirs);
755 INIT_LIST_HEAD(&dentry->d_alias);
756
757 if (parent) {
758 dentry->d_parent = dget(parent);
759 dentry->d_sb = parent->d_sb;
760 } else {
5160ee6f 761 INIT_LIST_HEAD(&dentry->d_u.d_child);
1da177e4
LT
762 }
763
764 spin_lock(&dcache_lock);
765 if (parent)
5160ee6f 766 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
1da177e4
LT
767 dentry_stat.nr_dentry++;
768 spin_unlock(&dcache_lock);
769
770 return dentry;
771}
772
773struct dentry *d_alloc_name(struct dentry *parent, const char *name)
774{
775 struct qstr q;
776
777 q.name = name;
778 q.len = strlen(name);
779 q.hash = full_name_hash(q.name, q.len);
780 return d_alloc(parent, &q);
781}
782
783/**
784 * d_instantiate - fill in inode information for a dentry
785 * @entry: dentry to complete
786 * @inode: inode to attach to this dentry
787 *
788 * Fill in inode information in the entry.
789 *
790 * This turns negative dentries into productive full members
791 * of society.
792 *
793 * NOTE! This assumes that the inode count has been incremented
794 * (or otherwise set) by the caller to indicate that it is now
795 * in use by the dcache.
796 */
797
798void d_instantiate(struct dentry *entry, struct inode * inode)
799{
800 if (!list_empty(&entry->d_alias)) BUG();
801 spin_lock(&dcache_lock);
802 if (inode)
803 list_add(&entry->d_alias, &inode->i_dentry);
804 entry->d_inode = inode;
c32ccd87 805 fsnotify_d_instantiate(entry, inode);
1da177e4
LT
806 spin_unlock(&dcache_lock);
807 security_d_instantiate(entry, inode);
808}
809
810/**
811 * d_instantiate_unique - instantiate a non-aliased dentry
812 * @entry: dentry to instantiate
813 * @inode: inode to attach to this dentry
814 *
815 * Fill in inode information in the entry. On success, it returns NULL.
816 * If an unhashed alias of "entry" already exists, then we return the
e866cfa9 817 * aliased dentry instead and drop one reference to inode.
1da177e4
LT
818 *
819 * Note that in order to avoid conflicts with rename() etc, the caller
820 * had better be holding the parent directory semaphore.
e866cfa9
OD
821 *
822 * This also assumes that the inode count has been incremented
823 * (or otherwise set) by the caller to indicate that it is now
824 * in use by the dcache.
1da177e4
LT
825 */
826struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
827{
828 struct dentry *alias;
829 int len = entry->d_name.len;
830 const char *name = entry->d_name.name;
831 unsigned int hash = entry->d_name.hash;
832
833 BUG_ON(!list_empty(&entry->d_alias));
834 spin_lock(&dcache_lock);
835 if (!inode)
836 goto do_negative;
837 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
838 struct qstr *qstr = &alias->d_name;
839
840 if (qstr->hash != hash)
841 continue;
842 if (alias->d_parent != entry->d_parent)
843 continue;
844 if (qstr->len != len)
845 continue;
846 if (memcmp(qstr->name, name, len))
847 continue;
848 dget_locked(alias);
849 spin_unlock(&dcache_lock);
850 BUG_ON(!d_unhashed(alias));
e866cfa9 851 iput(inode);
1da177e4
LT
852 return alias;
853 }
854 list_add(&entry->d_alias, &inode->i_dentry);
855do_negative:
856 entry->d_inode = inode;
c32ccd87 857 fsnotify_d_instantiate(entry, inode);
1da177e4
LT
858 spin_unlock(&dcache_lock);
859 security_d_instantiate(entry, inode);
860 return NULL;
861}
862EXPORT_SYMBOL(d_instantiate_unique);
863
864/**
865 * d_alloc_root - allocate root dentry
866 * @root_inode: inode to allocate the root for
867 *
868 * Allocate a root ("/") dentry for the inode given. The inode is
869 * instantiated and returned. %NULL is returned if there is insufficient
870 * memory or the inode passed is %NULL.
871 */
872
873struct dentry * d_alloc_root(struct inode * root_inode)
874{
875 struct dentry *res = NULL;
876
877 if (root_inode) {
878 static const struct qstr name = { .name = "/", .len = 1 };
879
880 res = d_alloc(NULL, &name);
881 if (res) {
882 res->d_sb = root_inode->i_sb;
883 res->d_parent = res;
884 d_instantiate(res, root_inode);
885 }
886 }
887 return res;
888}
889
890static inline struct hlist_head *d_hash(struct dentry *parent,
891 unsigned long hash)
892{
893 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
894 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
895 return dentry_hashtable + (hash & D_HASHMASK);
896}
897
898/**
899 * d_alloc_anon - allocate an anonymous dentry
900 * @inode: inode to allocate the dentry for
901 *
902 * This is similar to d_alloc_root. It is used by filesystems when
903 * creating a dentry for a given inode, often in the process of
904 * mapping a filehandle to a dentry. The returned dentry may be
905 * anonymous, or may have a full name (if the inode was already
906 * in the cache). The file system may need to make further
907 * efforts to connect this dentry into the dcache properly.
908 *
909 * When called on a directory inode, we must ensure that
910 * the inode only ever has one dentry. If a dentry is
911 * found, that is returned instead of allocating a new one.
912 *
913 * On successful return, the reference to the inode has been transferred
914 * to the dentry. If %NULL is returned (indicating kmalloc failure),
915 * the reference on the inode has not been released.
916 */
917
918struct dentry * d_alloc_anon(struct inode *inode)
919{
920 static const struct qstr anonstring = { .name = "" };
921 struct dentry *tmp;
922 struct dentry *res;
923
924 if ((res = d_find_alias(inode))) {
925 iput(inode);
926 return res;
927 }
928
929 tmp = d_alloc(NULL, &anonstring);
930 if (!tmp)
931 return NULL;
932
933 tmp->d_parent = tmp; /* make sure dput doesn't croak */
934
935 spin_lock(&dcache_lock);
936 res = __d_find_alias(inode, 0);
937 if (!res) {
938 /* attach a disconnected dentry */
939 res = tmp;
940 tmp = NULL;
941 spin_lock(&res->d_lock);
942 res->d_sb = inode->i_sb;
943 res->d_parent = res;
944 res->d_inode = inode;
945 res->d_flags |= DCACHE_DISCONNECTED;
946 res->d_flags &= ~DCACHE_UNHASHED;
947 list_add(&res->d_alias, &inode->i_dentry);
948 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
949 spin_unlock(&res->d_lock);
950
951 inode = NULL; /* don't drop reference */
952 }
953 spin_unlock(&dcache_lock);
954
955 if (inode)
956 iput(inode);
957 if (tmp)
958 dput(tmp);
959 return res;
960}
961
962
963/**
964 * d_splice_alias - splice a disconnected dentry into the tree if one exists
965 * @inode: the inode which may have a disconnected dentry
966 * @dentry: a negative dentry which we want to point to the inode.
967 *
968 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
969 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
970 * and return it, else simply d_add the inode to the dentry and return NULL.
971 *
972 * This is needed in the lookup routine of any filesystem that is exportable
973 * (via knfsd) so that we can build dcache paths to directories effectively.
974 *
975 * If a dentry was found and moved, then it is returned. Otherwise NULL
976 * is returned. This matches the expected return value of ->lookup.
977 *
978 */
979struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
980{
981 struct dentry *new = NULL;
982
983 if (inode) {
984 spin_lock(&dcache_lock);
985 new = __d_find_alias(inode, 1);
986 if (new) {
987 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
c32ccd87 988 fsnotify_d_instantiate(new, inode);
1da177e4
LT
989 spin_unlock(&dcache_lock);
990 security_d_instantiate(new, inode);
991 d_rehash(dentry);
992 d_move(new, dentry);
993 iput(inode);
994 } else {
995 /* d_instantiate takes dcache_lock, so we do it by hand */
996 list_add(&dentry->d_alias, &inode->i_dentry);
997 dentry->d_inode = inode;
c32ccd87 998 fsnotify_d_instantiate(dentry, inode);
1da177e4
LT
999 spin_unlock(&dcache_lock);
1000 security_d_instantiate(dentry, inode);
1001 d_rehash(dentry);
1002 }
1003 } else
1004 d_add(dentry, inode);
1005 return new;
1006}
1007
1008
1009/**
1010 * d_lookup - search for a dentry
1011 * @parent: parent dentry
1012 * @name: qstr of name we wish to find
1013 *
1014 * Searches the children of the parent dentry for the name in question. If
1015 * the dentry is found its reference count is incremented and the dentry
1016 * is returned. The caller must use d_put to free the entry when it has
1017 * finished using it. %NULL is returned on failure.
1018 *
1019 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1020 * Memory barriers are used while updating and doing lockless traversal.
1021 * To avoid races with d_move while rename is happening, d_lock is used.
1022 *
1023 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1024 * and name pointer in one structure pointed by d_qstr.
1025 *
1026 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1027 * lookup is going on.
1028 *
1029 * dentry_unused list is not updated even if lookup finds the required dentry
1030 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1031 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1032 * acquisition.
1033 *
1034 * d_lookup() is protected against the concurrent renames in some unrelated
1035 * directory using the seqlockt_t rename_lock.
1036 */
1037
1038struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1039{
1040 struct dentry * dentry = NULL;
1041 unsigned long seq;
1042
1043 do {
1044 seq = read_seqbegin(&rename_lock);
1045 dentry = __d_lookup(parent, name);
1046 if (dentry)
1047 break;
1048 } while (read_seqretry(&rename_lock, seq));
1049 return dentry;
1050}
1051
1052struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1053{
1054 unsigned int len = name->len;
1055 unsigned int hash = name->hash;
1056 const unsigned char *str = name->name;
1057 struct hlist_head *head = d_hash(parent,hash);
1058 struct dentry *found = NULL;
1059 struct hlist_node *node;
665a7583 1060 struct dentry *dentry;
1da177e4
LT
1061
1062 rcu_read_lock();
1063
665a7583 1064 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
1da177e4
LT
1065 struct qstr *qstr;
1066
1da177e4
LT
1067 if (dentry->d_name.hash != hash)
1068 continue;
1069 if (dentry->d_parent != parent)
1070 continue;
1071
1072 spin_lock(&dentry->d_lock);
1073
1074 /*
1075 * Recheck the dentry after taking the lock - d_move may have
1076 * changed things. Don't bother checking the hash because we're
1077 * about to compare the whole name anyway.
1078 */
1079 if (dentry->d_parent != parent)
1080 goto next;
1081
1082 /*
1083 * It is safe to compare names since d_move() cannot
1084 * change the qstr (protected by d_lock).
1085 */
1086 qstr = &dentry->d_name;
1087 if (parent->d_op && parent->d_op->d_compare) {
1088 if (parent->d_op->d_compare(parent, qstr, name))
1089 goto next;
1090 } else {
1091 if (qstr->len != len)
1092 goto next;
1093 if (memcmp(qstr->name, str, len))
1094 goto next;
1095 }
1096
1097 if (!d_unhashed(dentry)) {
1098 atomic_inc(&dentry->d_count);
1099 found = dentry;
1100 }
1101 spin_unlock(&dentry->d_lock);
1102 break;
1103next:
1104 spin_unlock(&dentry->d_lock);
1105 }
1106 rcu_read_unlock();
1107
1108 return found;
1109}
1110
1111/**
1112 * d_validate - verify dentry provided from insecure source
1113 * @dentry: The dentry alleged to be valid child of @dparent
1114 * @dparent: The parent dentry (known to be valid)
1115 * @hash: Hash of the dentry
1116 * @len: Length of the name
1117 *
1118 * An insecure source has sent us a dentry, here we verify it and dget() it.
1119 * This is used by ncpfs in its readdir implementation.
1120 * Zero is returned in the dentry is invalid.
1121 */
1122
1123int d_validate(struct dentry *dentry, struct dentry *dparent)
1124{
1125 struct hlist_head *base;
1126 struct hlist_node *lhp;
1127
1128 /* Check whether the ptr might be valid at all.. */
1129 if (!kmem_ptr_validate(dentry_cache, dentry))
1130 goto out;
1131
1132 if (dentry->d_parent != dparent)
1133 goto out;
1134
1135 spin_lock(&dcache_lock);
1136 base = d_hash(dparent, dentry->d_name.hash);
1137 hlist_for_each(lhp,base) {
665a7583 1138 /* hlist_for_each_entry_rcu() not required for d_hash list
1da177e4
LT
1139 * as it is parsed under dcache_lock
1140 */
1141 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1142 __dget_locked(dentry);
1143 spin_unlock(&dcache_lock);
1144 return 1;
1145 }
1146 }
1147 spin_unlock(&dcache_lock);
1148out:
1149 return 0;
1150}
1151
1152/*
1153 * When a file is deleted, we have two options:
1154 * - turn this dentry into a negative dentry
1155 * - unhash this dentry and free it.
1156 *
1157 * Usually, we want to just turn this into
1158 * a negative dentry, but if anybody else is
1159 * currently using the dentry or the inode
1160 * we can't do that and we fall back on removing
1161 * it from the hash queues and waiting for
1162 * it to be deleted later when it has no users
1163 */
1164
1165/**
1166 * d_delete - delete a dentry
1167 * @dentry: The dentry to delete
1168 *
1169 * Turn the dentry into a negative dentry if possible, otherwise
1170 * remove it from the hash queues so it can be deleted later
1171 */
1172
1173void d_delete(struct dentry * dentry)
1174{
7a91bf7f 1175 int isdir = 0;
1da177e4
LT
1176 /*
1177 * Are we the only user?
1178 */
1179 spin_lock(&dcache_lock);
1180 spin_lock(&dentry->d_lock);
7a91bf7f 1181 isdir = S_ISDIR(dentry->d_inode->i_mode);
1da177e4 1182 if (atomic_read(&dentry->d_count) == 1) {
c32ccd87
NP
1183 /* remove this and other inotify debug checks after 2.6.18 */
1184 dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
1185
1da177e4 1186 dentry_iput(dentry);
7a91bf7f 1187 fsnotify_nameremove(dentry, isdir);
1da177e4
LT
1188 return;
1189 }
1190
1191 if (!d_unhashed(dentry))
1192 __d_drop(dentry);
1193
1194 spin_unlock(&dentry->d_lock);
1195 spin_unlock(&dcache_lock);
7a91bf7f
JM
1196
1197 fsnotify_nameremove(dentry, isdir);
1da177e4
LT
1198}
1199
1200static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1201{
1202
1203 entry->d_flags &= ~DCACHE_UNHASHED;
1204 hlist_add_head_rcu(&entry->d_hash, list);
1205}
1206
1207/**
1208 * d_rehash - add an entry back to the hash
1209 * @entry: dentry to add to the hash
1210 *
1211 * Adds a dentry to the hash according to its name.
1212 */
1213
1214void d_rehash(struct dentry * entry)
1215{
1216 struct hlist_head *list = d_hash(entry->d_parent, entry->d_name.hash);
1217
1218 spin_lock(&dcache_lock);
1219 spin_lock(&entry->d_lock);
1220 __d_rehash(entry, list);
1221 spin_unlock(&entry->d_lock);
1222 spin_unlock(&dcache_lock);
1223}
1224
1225#define do_switch(x,y) do { \
1226 __typeof__ (x) __tmp = x; \
1227 x = y; y = __tmp; } while (0)
1228
1229/*
1230 * When switching names, the actual string doesn't strictly have to
1231 * be preserved in the target - because we're dropping the target
1232 * anyway. As such, we can just do a simple memcpy() to copy over
1233 * the new name before we switch.
1234 *
1235 * Note that we have to be a lot more careful about getting the hash
1236 * switched - we have to switch the hash value properly even if it
1237 * then no longer matches the actual (corrupted) string of the target.
1238 * The hash value has to match the hash queue that the dentry is on..
1239 */
1240static void switch_names(struct dentry *dentry, struct dentry *target)
1241{
1242 if (dname_external(target)) {
1243 if (dname_external(dentry)) {
1244 /*
1245 * Both external: swap the pointers
1246 */
1247 do_switch(target->d_name.name, dentry->d_name.name);
1248 } else {
1249 /*
1250 * dentry:internal, target:external. Steal target's
1251 * storage and make target internal.
1252 */
1253 dentry->d_name.name = target->d_name.name;
1254 target->d_name.name = target->d_iname;
1255 }
1256 } else {
1257 if (dname_external(dentry)) {
1258 /*
1259 * dentry:external, target:internal. Give dentry's
1260 * storage to target and make dentry internal
1261 */
1262 memcpy(dentry->d_iname, target->d_name.name,
1263 target->d_name.len + 1);
1264 target->d_name.name = dentry->d_name.name;
1265 dentry->d_name.name = dentry->d_iname;
1266 } else {
1267 /*
1268 * Both are internal. Just copy target to dentry
1269 */
1270 memcpy(dentry->d_iname, target->d_name.name,
1271 target->d_name.len + 1);
1272 }
1273 }
1274}
1275
1276/*
1277 * We cannibalize "target" when moving dentry on top of it,
1278 * because it's going to be thrown away anyway. We could be more
1279 * polite about it, though.
1280 *
1281 * This forceful removal will result in ugly /proc output if
1282 * somebody holds a file open that got deleted due to a rename.
1283 * We could be nicer about the deleted file, and let it show
1284 * up under the name it got deleted rather than the name that
1285 * deleted it.
1286 */
1287
1288/**
1289 * d_move - move a dentry
1290 * @dentry: entry to move
1291 * @target: new dentry
1292 *
1293 * Update the dcache to reflect the move of a file name. Negative
1294 * dcache entries should not be moved in this way.
1295 */
1296
1297void d_move(struct dentry * dentry, struct dentry * target)
1298{
1299 struct hlist_head *list;
1300
1301 if (!dentry->d_inode)
1302 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1303
1304 spin_lock(&dcache_lock);
1305 write_seqlock(&rename_lock);
1306 /*
1307 * XXXX: do we really need to take target->d_lock?
1308 */
1309 if (target < dentry) {
1310 spin_lock(&target->d_lock);
1311 spin_lock(&dentry->d_lock);
1312 } else {
1313 spin_lock(&dentry->d_lock);
1314 spin_lock(&target->d_lock);
1315 }
1316
1317 /* Move the dentry to the target hash queue, if on different bucket */
1318 if (dentry->d_flags & DCACHE_UNHASHED)
1319 goto already_unhashed;
1320
1321 hlist_del_rcu(&dentry->d_hash);
1322
1323already_unhashed:
1324 list = d_hash(target->d_parent, target->d_name.hash);
1325 __d_rehash(dentry, list);
1326
1327 /* Unhash the target: dput() will then get rid of it */
1328 __d_drop(target);
1329
5160ee6f
ED
1330 list_del(&dentry->d_u.d_child);
1331 list_del(&target->d_u.d_child);
1da177e4
LT
1332
1333 /* Switch the names.. */
1334 switch_names(dentry, target);
1335 do_switch(dentry->d_name.len, target->d_name.len);
1336 do_switch(dentry->d_name.hash, target->d_name.hash);
1337
1338 /* ... and switch the parents */
1339 if (IS_ROOT(dentry)) {
1340 dentry->d_parent = target->d_parent;
1341 target->d_parent = target;
5160ee6f 1342 INIT_LIST_HEAD(&target->d_u.d_child);
1da177e4
LT
1343 } else {
1344 do_switch(dentry->d_parent, target->d_parent);
1345
1346 /* And add them back to the (new) parent lists */
5160ee6f 1347 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
1da177e4
LT
1348 }
1349
5160ee6f 1350 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1da177e4 1351 spin_unlock(&target->d_lock);
c32ccd87 1352 fsnotify_d_move(dentry);
1da177e4
LT
1353 spin_unlock(&dentry->d_lock);
1354 write_sequnlock(&rename_lock);
1355 spin_unlock(&dcache_lock);
1356}
1357
1358/**
1359 * d_path - return the path of a dentry
1360 * @dentry: dentry to report
1361 * @vfsmnt: vfsmnt to which the dentry belongs
1362 * @root: root dentry
1363 * @rootmnt: vfsmnt to which the root dentry belongs
1364 * @buffer: buffer to return value in
1365 * @buflen: buffer length
1366 *
1367 * Convert a dentry into an ASCII path name. If the entry has been deleted
1368 * the string " (deleted)" is appended. Note that this is ambiguous.
1369 *
1370 * Returns the buffer or an error code if the path was too long.
1371 *
1372 * "buflen" should be positive. Caller holds the dcache_lock.
1373 */
1374static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1375 struct dentry *root, struct vfsmount *rootmnt,
1376 char *buffer, int buflen)
1377{
1378 char * end = buffer+buflen;
1379 char * retval;
1380 int namelen;
1381
1382 *--end = '\0';
1383 buflen--;
1384 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
1385 buflen -= 10;
1386 end -= 10;
1387 if (buflen < 0)
1388 goto Elong;
1389 memcpy(end, " (deleted)", 10);
1390 }
1391
1392 if (buflen < 1)
1393 goto Elong;
1394 /* Get '/' right */
1395 retval = end-1;
1396 *retval = '/';
1397
1398 for (;;) {
1399 struct dentry * parent;
1400
1401 if (dentry == root && vfsmnt == rootmnt)
1402 break;
1403 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
1404 /* Global root? */
1405 spin_lock(&vfsmount_lock);
1406 if (vfsmnt->mnt_parent == vfsmnt) {
1407 spin_unlock(&vfsmount_lock);
1408 goto global_root;
1409 }
1410 dentry = vfsmnt->mnt_mountpoint;
1411 vfsmnt = vfsmnt->mnt_parent;
1412 spin_unlock(&vfsmount_lock);
1413 continue;
1414 }
1415 parent = dentry->d_parent;
1416 prefetch(parent);
1417 namelen = dentry->d_name.len;
1418 buflen -= namelen + 1;
1419 if (buflen < 0)
1420 goto Elong;
1421 end -= namelen;
1422 memcpy(end, dentry->d_name.name, namelen);
1423 *--end = '/';
1424 retval = end;
1425 dentry = parent;
1426 }
1427
1428 return retval;
1429
1430global_root:
1431 namelen = dentry->d_name.len;
1432 buflen -= namelen;
1433 if (buflen < 0)
1434 goto Elong;
1435 retval -= namelen-1; /* hit the slash */
1436 memcpy(retval, dentry->d_name.name, namelen);
1437 return retval;
1438Elong:
1439 return ERR_PTR(-ENAMETOOLONG);
1440}
1441
1442/* write full pathname into buffer and return start of pathname */
1443char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1444 char *buf, int buflen)
1445{
1446 char *res;
1447 struct vfsmount *rootmnt;
1448 struct dentry *root;
1449
1450 read_lock(&current->fs->lock);
1451 rootmnt = mntget(current->fs->rootmnt);
1452 root = dget(current->fs->root);
1453 read_unlock(&current->fs->lock);
1454 spin_lock(&dcache_lock);
1455 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1456 spin_unlock(&dcache_lock);
1457 dput(root);
1458 mntput(rootmnt);
1459 return res;
1460}
1461
1462/*
1463 * NOTE! The user-level library version returns a
1464 * character pointer. The kernel system call just
1465 * returns the length of the buffer filled (which
1466 * includes the ending '\0' character), or a negative
1467 * error value. So libc would do something like
1468 *
1469 * char *getcwd(char * buf, size_t size)
1470 * {
1471 * int retval;
1472 *
1473 * retval = sys_getcwd(buf, size);
1474 * if (retval >= 0)
1475 * return buf;
1476 * errno = -retval;
1477 * return NULL;
1478 * }
1479 */
1480asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1481{
1482 int error;
1483 struct vfsmount *pwdmnt, *rootmnt;
1484 struct dentry *pwd, *root;
1485 char *page = (char *) __get_free_page(GFP_USER);
1486
1487 if (!page)
1488 return -ENOMEM;
1489
1490 read_lock(&current->fs->lock);
1491 pwdmnt = mntget(current->fs->pwdmnt);
1492 pwd = dget(current->fs->pwd);
1493 rootmnt = mntget(current->fs->rootmnt);
1494 root = dget(current->fs->root);
1495 read_unlock(&current->fs->lock);
1496
1497 error = -ENOENT;
1498 /* Has the current directory has been unlinked? */
1499 spin_lock(&dcache_lock);
1500 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1501 unsigned long len;
1502 char * cwd;
1503
1504 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1505 spin_unlock(&dcache_lock);
1506
1507 error = PTR_ERR(cwd);
1508 if (IS_ERR(cwd))
1509 goto out;
1510
1511 error = -ERANGE;
1512 len = PAGE_SIZE + page - cwd;
1513 if (len <= size) {
1514 error = len;
1515 if (copy_to_user(buf, cwd, len))
1516 error = -EFAULT;
1517 }
1518 } else
1519 spin_unlock(&dcache_lock);
1520
1521out:
1522 dput(pwd);
1523 mntput(pwdmnt);
1524 dput(root);
1525 mntput(rootmnt);
1526 free_page((unsigned long) page);
1527 return error;
1528}
1529
1530/*
1531 * Test whether new_dentry is a subdirectory of old_dentry.
1532 *
1533 * Trivially implemented using the dcache structure
1534 */
1535
1536/**
1537 * is_subdir - is new dentry a subdirectory of old_dentry
1538 * @new_dentry: new dentry
1539 * @old_dentry: old dentry
1540 *
1541 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1542 * Returns 0 otherwise.
1543 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1544 */
1545
1546int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1547{
1548 int result;
1549 struct dentry * saved = new_dentry;
1550 unsigned long seq;
1551
1552 /* need rcu_readlock to protect against the d_parent trashing due to
1553 * d_move
1554 */
1555 rcu_read_lock();
1556 do {
1557 /* for restarting inner loop in case of seq retry */
1558 new_dentry = saved;
1559 result = 0;
1560 seq = read_seqbegin(&rename_lock);
1561 for (;;) {
1562 if (new_dentry != old_dentry) {
1563 struct dentry * parent = new_dentry->d_parent;
1564 if (parent == new_dentry)
1565 break;
1566 new_dentry = parent;
1567 continue;
1568 }
1569 result = 1;
1570 break;
1571 }
1572 } while (read_seqretry(&rename_lock, seq));
1573 rcu_read_unlock();
1574
1575 return result;
1576}
1577
1578void d_genocide(struct dentry *root)
1579{
1580 struct dentry *this_parent = root;
1581 struct list_head *next;
1582
1583 spin_lock(&dcache_lock);
1584repeat:
1585 next = this_parent->d_subdirs.next;
1586resume:
1587 while (next != &this_parent->d_subdirs) {
1588 struct list_head *tmp = next;
5160ee6f 1589 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
1da177e4
LT
1590 next = tmp->next;
1591 if (d_unhashed(dentry)||!dentry->d_inode)
1592 continue;
1593 if (!list_empty(&dentry->d_subdirs)) {
1594 this_parent = dentry;
1595 goto repeat;
1596 }
1597 atomic_dec(&dentry->d_count);
1598 }
1599 if (this_parent != root) {
5160ee6f 1600 next = this_parent->d_u.d_child.next;
1da177e4
LT
1601 atomic_dec(&this_parent->d_count);
1602 this_parent = this_parent->d_parent;
1603 goto resume;
1604 }
1605 spin_unlock(&dcache_lock);
1606}
1607
1608/**
1609 * find_inode_number - check for dentry with name
1610 * @dir: directory to check
1611 * @name: Name to find.
1612 *
1613 * Check whether a dentry already exists for the given name,
1614 * and return the inode number if it has an inode. Otherwise
1615 * 0 is returned.
1616 *
1617 * This routine is used to post-process directory listings for
1618 * filesystems using synthetic inode numbers, and is necessary
1619 * to keep getcwd() working.
1620 */
1621
1622ino_t find_inode_number(struct dentry *dir, struct qstr *name)
1623{
1624 struct dentry * dentry;
1625 ino_t ino = 0;
1626
1627 /*
1628 * Check for a fs-specific hash function. Note that we must
1629 * calculate the standard hash first, as the d_op->d_hash()
1630 * routine may choose to leave the hash value unchanged.
1631 */
1632 name->hash = full_name_hash(name->name, name->len);
1633 if (dir->d_op && dir->d_op->d_hash)
1634 {
1635 if (dir->d_op->d_hash(dir, name) != 0)
1636 goto out;
1637 }
1638
1639 dentry = d_lookup(dir, name);
1640 if (dentry)
1641 {
1642 if (dentry->d_inode)
1643 ino = dentry->d_inode->i_ino;
1644 dput(dentry);
1645 }
1646out:
1647 return ino;
1648}
1649
1650static __initdata unsigned long dhash_entries;
1651static int __init set_dhash_entries(char *str)
1652{
1653 if (!str)
1654 return 0;
1655 dhash_entries = simple_strtoul(str, &str, 0);
1656 return 1;
1657}
1658__setup("dhash_entries=", set_dhash_entries);
1659
1660static void __init dcache_init_early(void)
1661{
1662 int loop;
1663
1664 /* If hashes are distributed across NUMA nodes, defer
1665 * hash allocation until vmalloc space is available.
1666 */
1667 if (hashdist)
1668 return;
1669
1670 dentry_hashtable =
1671 alloc_large_system_hash("Dentry cache",
1672 sizeof(struct hlist_head),
1673 dhash_entries,
1674 13,
1675 HASH_EARLY,
1676 &d_hash_shift,
1677 &d_hash_mask,
1678 0);
1679
1680 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1681 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1682}
1683
1684static void __init dcache_init(unsigned long mempages)
1685{
1686 int loop;
1687
1688 /*
1689 * A constructor could be added for stable state like the lists,
1690 * but it is probably not worth it because of the cache nature
1691 * of the dcache.
1692 */
1693 dentry_cache = kmem_cache_create("dentry_cache",
1694 sizeof(struct dentry),
1695 0,
b0196009
PJ
1696 (SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|
1697 SLAB_MEM_SPREAD),
1da177e4
LT
1698 NULL, NULL);
1699
1700 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
1701
1702 /* Hash may have been set up in dcache_init_early */
1703 if (!hashdist)
1704 return;
1705
1706 dentry_hashtable =
1707 alloc_large_system_hash("Dentry cache",
1708 sizeof(struct hlist_head),
1709 dhash_entries,
1710 13,
1711 0,
1712 &d_hash_shift,
1713 &d_hash_mask,
1714 0);
1715
1716 for (loop = 0; loop < (1 << d_hash_shift); loop++)
1717 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
1718}
1719
1720/* SLAB cache for __getname() consumers */
1721kmem_cache_t *names_cachep;
1722
1723/* SLAB cache for file structures */
1724kmem_cache_t *filp_cachep;
1725
1726EXPORT_SYMBOL(d_genocide);
1727
1728extern void bdev_cache_init(void);
1729extern void chrdev_init(void);
1730
1731void __init vfs_caches_init_early(void)
1732{
1733 dcache_init_early();
1734 inode_init_early();
1735}
1736
1737void __init vfs_caches_init(unsigned long mempages)
1738{
1739 unsigned long reserve;
1740
1741 /* Base hash sizes on available memory, with a reserve equal to
1742 150% of current kernel size */
1743
1744 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
1745 mempages -= reserve;
1746
1747 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
1748 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1749
1750 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
529bf6be 1751 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
1da177e4
LT
1752
1753 dcache_init(mempages);
1754 inode_init(mempages);
1755 files_init(mempages);
1756 mnt_init(mempages);
1757 bdev_cache_init();
1758 chrdev_init();
1759}
1760
1761EXPORT_SYMBOL(d_alloc);
1762EXPORT_SYMBOL(d_alloc_anon);
1763EXPORT_SYMBOL(d_alloc_root);
1764EXPORT_SYMBOL(d_delete);
1765EXPORT_SYMBOL(d_find_alias);
1766EXPORT_SYMBOL(d_instantiate);
1767EXPORT_SYMBOL(d_invalidate);
1768EXPORT_SYMBOL(d_lookup);
1769EXPORT_SYMBOL(d_move);
1770EXPORT_SYMBOL(d_path);
1771EXPORT_SYMBOL(d_prune_aliases);
1772EXPORT_SYMBOL(d_rehash);
1773EXPORT_SYMBOL(d_splice_alias);
1774EXPORT_SYMBOL(d_validate);
1775EXPORT_SYMBOL(dget_locked);
1776EXPORT_SYMBOL(dput);
1777EXPORT_SYMBOL(find_inode_number);
1778EXPORT_SYMBOL(have_submounts);
1779EXPORT_SYMBOL(names_cachep);
1780EXPORT_SYMBOL(shrink_dcache_parent);
1781EXPORT_SYMBOL(shrink_dcache_sb);