blob: 0e73aa0a0e8b66a1bf213ed53ad579bbabe7b2f3 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/dcache.c
3 *
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
7 */
8
9/*
10 * Notes on the allocation strategy:
11 *
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
John McCutchan7a91bf72005-08-08 13:52:16 -040021#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/slab.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/hash.h>
25#include <linux/cache.h>
26#include <linux/module.h>
27#include <linux/mount.h>
28#include <linux/file.h>
29#include <asm/uaccess.h>
30#include <linux/security.h>
31#include <linux/seqlock.h>
32#include <linux/swap.h>
33#include <linux/bootmem.h>
David Howells07f3f052006-09-30 20:52:18 +020034#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070035
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Eric Dumazetfa3536c2006-03-26 01:37:24 -080037int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
39
40 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
Ingo Molnare4d91912006-07-03 00:24:34 -070041static __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070042
43EXPORT_SYMBOL(dcache_lock);
44
Christoph Lametere18b8902006-12-06 20:33:20 -080045static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070046
47#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
48
49/*
50 * This is the single most critical data structure when it comes
51 * to the dcache: the hashtable for lookups. Somebody should try
52 * to make this good - I've just made it work.
53 *
54 * This hash-function tries to avoid losing too many bits of hash
55 * information, yet avoid using a prime hash-size or similar.
56 */
57#define D_HASHBITS d_hash_shift
58#define D_HASHMASK d_hash_mask
59
Eric Dumazetfa3536c2006-03-26 01:37:24 -080060static unsigned int d_hash_mask __read_mostly;
61static unsigned int d_hash_shift __read_mostly;
62static struct hlist_head *dentry_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070063static LIST_HEAD(dentry_unused);
64
65/* Statistics gathering. */
66struct dentry_stat_t dentry_stat = {
67 .age_limit = 45,
68};
69
Eric Dumazetb3423412006-12-06 20:38:48 -080070static void __d_free(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070071{
Linus Torvalds1da177e2005-04-16 15:20:36 -070072 if (dname_external(dentry))
73 kfree(dentry->d_name.name);
74 kmem_cache_free(dentry_cache, dentry);
75}
76
Eric Dumazetb3423412006-12-06 20:38:48 -080077static void d_callback(struct rcu_head *head)
78{
79 struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
80 __d_free(dentry);
81}
82
Linus Torvalds1da177e2005-04-16 15:20:36 -070083/*
84 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
85 * inside dcache_lock.
86 */
87static void d_free(struct dentry *dentry)
88{
89 if (dentry->d_op && dentry->d_op->d_release)
90 dentry->d_op->d_release(dentry);
Eric Dumazetb3423412006-12-06 20:38:48 -080091 /* if dentry was never inserted into hash, immediate free is OK */
92 if (dentry->d_hash.pprev == NULL)
93 __d_free(dentry);
94 else
95 call_rcu(&dentry->d_u.d_rcu, d_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -070096}
97
98/*
99 * Release the dentry's inode, using the filesystem
100 * d_iput() operation if defined.
101 * Called with dcache_lock and per dentry lock held, drops both.
102 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800103static void dentry_iput(struct dentry * dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104{
105 struct inode *inode = dentry->d_inode;
106 if (inode) {
107 dentry->d_inode = NULL;
108 list_del_init(&dentry->d_alias);
109 spin_unlock(&dentry->d_lock);
110 spin_unlock(&dcache_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700111 if (!inode->i_nlink)
112 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700113 if (dentry->d_op && dentry->d_op->d_iput)
114 dentry->d_op->d_iput(dentry, inode);
115 else
116 iput(inode);
117 } else {
118 spin_unlock(&dentry->d_lock);
119 spin_unlock(&dcache_lock);
120 }
121}
122
Miklos Szeredid52b9082007-05-08 00:23:46 -0700123/**
124 * d_kill - kill dentry and return parent
125 * @dentry: dentry to kill
126 *
127 * Called with dcache_lock and d_lock, releases both. The dentry must
128 * already be unhashed and removed from the LRU.
129 *
130 * If this is the root of the dentry tree, return NULL.
131 */
132static struct dentry *d_kill(struct dentry *dentry)
133{
134 struct dentry *parent;
135
136 list_del(&dentry->d_u.d_child);
137 dentry_stat.nr_dentry--; /* For d_free, below */
138 /*drops the locks, at that point nobody can reach this dentry */
139 dentry_iput(dentry);
140 parent = dentry->d_parent;
141 d_free(dentry);
142 return dentry == parent ? NULL : parent;
143}
144
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145/*
146 * This is dput
147 *
148 * This is complicated by the fact that we do not want to put
149 * dentries that are no longer on any hash chain on the unused
150 * list: we'd much rather just get rid of them immediately.
151 *
152 * However, that implies that we have to traverse the dentry
153 * tree upwards to the parents which might _also_ now be
154 * scheduled for deletion (it may have been only waiting for
155 * its last child to go away).
156 *
157 * This tail recursion is done by hand as we don't want to depend
158 * on the compiler to always get this right (gcc generally doesn't).
159 * Real recursion would eat up our stack space.
160 */
161
162/*
163 * dput - release a dentry
164 * @dentry: dentry to release
165 *
166 * Release a dentry. This will drop the usage count and if appropriate
167 * call the dentry unlink method as well as removing it from the queues and
168 * releasing its resources. If the parent dentries were scheduled for release
169 * they too may now get deleted.
170 *
171 * no dcache lock, please.
172 */
173
174void dput(struct dentry *dentry)
175{
176 if (!dentry)
177 return;
178
179repeat:
180 if (atomic_read(&dentry->d_count) == 1)
181 might_sleep();
182 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
183 return;
184
185 spin_lock(&dentry->d_lock);
186 if (atomic_read(&dentry->d_count)) {
187 spin_unlock(&dentry->d_lock);
188 spin_unlock(&dcache_lock);
189 return;
190 }
191
192 /*
193 * AV: ->d_delete() is _NOT_ allowed to block now.
194 */
195 if (dentry->d_op && dentry->d_op->d_delete) {
196 if (dentry->d_op->d_delete(dentry))
197 goto unhash_it;
198 }
199 /* Unreachable? Get rid of it */
200 if (d_unhashed(dentry))
201 goto kill_it;
202 if (list_empty(&dentry->d_lru)) {
203 dentry->d_flags |= DCACHE_REFERENCED;
204 list_add(&dentry->d_lru, &dentry_unused);
205 dentry_stat.nr_unused++;
206 }
207 spin_unlock(&dentry->d_lock);
208 spin_unlock(&dcache_lock);
209 return;
210
211unhash_it:
212 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700213kill_it:
214 /* If dentry was on d_lru list
215 * delete it from there
216 */
217 if (!list_empty(&dentry->d_lru)) {
218 list_del(&dentry->d_lru);
219 dentry_stat.nr_unused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220 }
Miklos Szeredid52b9082007-05-08 00:23:46 -0700221 dentry = d_kill(dentry);
222 if (dentry)
223 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700224}
225
226/**
227 * d_invalidate - invalidate a dentry
228 * @dentry: dentry to invalidate
229 *
230 * Try to invalidate the dentry if it turns out to be
231 * possible. If there are other dentries that can be
232 * reached through this one we can't delete it and we
233 * return -EBUSY. On success we return 0.
234 *
235 * no dcache lock.
236 */
237
238int d_invalidate(struct dentry * dentry)
239{
240 /*
241 * If it's already been dropped, return OK.
242 */
243 spin_lock(&dcache_lock);
244 if (d_unhashed(dentry)) {
245 spin_unlock(&dcache_lock);
246 return 0;
247 }
248 /*
249 * Check whether to do a partial shrink_dcache
250 * to get rid of unused child entries.
251 */
252 if (!list_empty(&dentry->d_subdirs)) {
253 spin_unlock(&dcache_lock);
254 shrink_dcache_parent(dentry);
255 spin_lock(&dcache_lock);
256 }
257
258 /*
259 * Somebody else still using it?
260 *
261 * If it's a directory, we can't drop it
262 * for fear of somebody re-populating it
263 * with children (even though dropping it
264 * would make it unreachable from the root,
265 * we might still populate it if it was a
266 * working directory or similar).
267 */
268 spin_lock(&dentry->d_lock);
269 if (atomic_read(&dentry->d_count) > 1) {
270 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
271 spin_unlock(&dentry->d_lock);
272 spin_unlock(&dcache_lock);
273 return -EBUSY;
274 }
275 }
276
277 __d_drop(dentry);
278 spin_unlock(&dentry->d_lock);
279 spin_unlock(&dcache_lock);
280 return 0;
281}
282
283/* This should be called _only_ with dcache_lock held */
284
285static inline struct dentry * __dget_locked(struct dentry *dentry)
286{
287 atomic_inc(&dentry->d_count);
288 if (!list_empty(&dentry->d_lru)) {
289 dentry_stat.nr_unused--;
290 list_del_init(&dentry->d_lru);
291 }
292 return dentry;
293}
294
295struct dentry * dget_locked(struct dentry *dentry)
296{
297 return __dget_locked(dentry);
298}
299
300/**
301 * d_find_alias - grab a hashed alias of inode
302 * @inode: inode in question
303 * @want_discon: flag, used by d_splice_alias, to request
304 * that only a DISCONNECTED alias be returned.
305 *
306 * If inode has a hashed alias, or is a directory and has any alias,
307 * acquire the reference to alias and return it. Otherwise return NULL.
308 * Notice that if inode is a directory there can be only one alias and
309 * it can be unhashed only if it has no children, or if it is the root
310 * of a filesystem.
311 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700312 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700313 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700314 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700315 */
316
317static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
318{
319 struct list_head *head, *next, *tmp;
320 struct dentry *alias, *discon_alias=NULL;
321
322 head = &inode->i_dentry;
323 next = inode->i_dentry.next;
324 while (next != head) {
325 tmp = next;
326 next = tmp->next;
327 prefetch(next);
328 alias = list_entry(tmp, struct dentry, d_alias);
329 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700330 if (IS_ROOT(alias) &&
331 (alias->d_flags & DCACHE_DISCONNECTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 discon_alias = alias;
333 else if (!want_discon) {
334 __dget_locked(alias);
335 return alias;
336 }
337 }
338 }
339 if (discon_alias)
340 __dget_locked(discon_alias);
341 return discon_alias;
342}
343
344struct dentry * d_find_alias(struct inode *inode)
345{
David Howells214fda12006-03-25 03:06:36 -0800346 struct dentry *de = NULL;
347
348 if (!list_empty(&inode->i_dentry)) {
349 spin_lock(&dcache_lock);
350 de = __d_find_alias(inode, 0);
351 spin_unlock(&dcache_lock);
352 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700353 return de;
354}
355
356/*
357 * Try to kill dentries associated with this inode.
358 * WARNING: you must own a reference to inode.
359 */
360void d_prune_aliases(struct inode *inode)
361{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700362 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363restart:
364 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700365 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 spin_lock(&dentry->d_lock);
367 if (!atomic_read(&dentry->d_count)) {
368 __dget_locked(dentry);
369 __d_drop(dentry);
370 spin_unlock(&dentry->d_lock);
371 spin_unlock(&dcache_lock);
372 dput(dentry);
373 goto restart;
374 }
375 spin_unlock(&dentry->d_lock);
376 }
377 spin_unlock(&dcache_lock);
378}
379
380/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700381 * Throw away a dentry - free the inode, dput the parent. This requires that
382 * the LRU list has already been removed.
383 *
Miklos Szeredid52b9082007-05-08 00:23:46 -0700384 * If prune_parents is true, try to prune ancestors as well.
385 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 * Called with dcache_lock, drops it and then regains.
Andrew Mortond702ccb2006-06-22 14:47:31 -0700387 * Called with dentry->d_lock held, drops it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700389static void prune_one_dentry(struct dentry * dentry, int prune_parents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700392 dentry = d_kill(dentry);
393 if (!prune_parents) {
394 dput(dentry);
395 spin_lock(&dcache_lock);
396 return;
397 }
398
399 /*
400 * Prune ancestors. Locking is simpler than in dput(),
401 * because dcache_lock needs to be taken anyway.
402 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403 spin_lock(&dcache_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700404 while (dentry) {
405 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
406 return;
407
408 if (dentry->d_op && dentry->d_op->d_delete)
409 dentry->d_op->d_delete(dentry);
410 if (!list_empty(&dentry->d_lru)) {
411 list_del(&dentry->d_lru);
412 dentry_stat.nr_unused--;
413 }
414 __d_drop(dentry);
415 dentry = d_kill(dentry);
416 spin_lock(&dcache_lock);
417 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418}
419
420/**
421 * prune_dcache - shrink the dcache
422 * @count: number of entries to try and free
NeilBrown0feae5c2006-06-22 14:47:28 -0700423 * @sb: if given, ignore dentries for other superblocks
424 * which are being unmounted.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700425 * @prune_parents: if true, try to prune ancestors as well in one go
Linus Torvalds1da177e2005-04-16 15:20:36 -0700426 *
427 * Shrink the dcache. This is done when we need
428 * more memory, or simply when we need to unmount
429 * something (at which point we need to unuse
430 * all dentries).
431 *
432 * This function may fail to free any resources if
433 * all the dentries are in use.
434 */
435
Miklos Szeredid52b9082007-05-08 00:23:46 -0700436static void prune_dcache(int count, struct super_block *sb, int prune_parents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700437{
438 spin_lock(&dcache_lock);
439 for (; count ; count--) {
440 struct dentry *dentry;
441 struct list_head *tmp;
NeilBrown0feae5c2006-06-22 14:47:28 -0700442 struct rw_semaphore *s_umount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443
444 cond_resched_lock(&dcache_lock);
445
446 tmp = dentry_unused.prev;
Hua Zhongf58a1eb2006-06-25 05:49:32 -0700447 if (sb) {
NeilBrown0feae5c2006-06-22 14:47:28 -0700448 /* Try to find a dentry for this sb, but don't try
449 * too hard, if they aren't near the tail they will
450 * be moved down again soon
451 */
452 int skip = count;
453 while (skip && tmp != &dentry_unused &&
454 list_entry(tmp, struct dentry, d_lru)->d_sb != sb) {
455 skip--;
456 tmp = tmp->prev;
457 }
458 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700459 if (tmp == &dentry_unused)
460 break;
461 list_del_init(tmp);
462 prefetch(dentry_unused.prev);
463 dentry_stat.nr_unused--;
464 dentry = list_entry(tmp, struct dentry, d_lru);
465
466 spin_lock(&dentry->d_lock);
467 /*
468 * We found an inuse dentry which was not removed from
469 * dentry_unused because of laziness during lookup. Do not free
470 * it - just keep it off the dentry_unused list.
471 */
472 if (atomic_read(&dentry->d_count)) {
473 spin_unlock(&dentry->d_lock);
474 continue;
475 }
476 /* If the dentry was recently referenced, don't free it. */
477 if (dentry->d_flags & DCACHE_REFERENCED) {
478 dentry->d_flags &= ~DCACHE_REFERENCED;
479 list_add(&dentry->d_lru, &dentry_unused);
480 dentry_stat.nr_unused++;
481 spin_unlock(&dentry->d_lock);
482 continue;
483 }
NeilBrown0feae5c2006-06-22 14:47:28 -0700484 /*
485 * If the dentry is not DCACHED_REFERENCED, it is time
486 * to remove it from the dcache, provided the super block is
487 * NULL (which means we are trying to reclaim memory)
488 * or this dentry belongs to the same super block that
489 * we want to shrink.
490 */
491 /*
492 * If this dentry is for "my" filesystem, then I can prune it
493 * without taking the s_umount lock (I already hold it).
494 */
495 if (sb && dentry->d_sb == sb) {
Miklos Szeredid52b9082007-05-08 00:23:46 -0700496 prune_one_dentry(dentry, prune_parents);
NeilBrown0feae5c2006-06-22 14:47:28 -0700497 continue;
498 }
499 /*
500 * ...otherwise we need to be sure this filesystem isn't being
501 * unmounted, otherwise we could race with
502 * generic_shutdown_super(), and end up holding a reference to
503 * an inode while the filesystem is unmounted.
504 * So we try to get s_umount, and make sure s_root isn't NULL.
505 * (Take a local copy of s_umount to avoid a use-after-free of
506 * `dentry').
507 */
508 s_umount = &dentry->d_sb->s_umount;
509 if (down_read_trylock(s_umount)) {
510 if (dentry->d_sb->s_root != NULL) {
Miklos Szeredid52b9082007-05-08 00:23:46 -0700511 prune_one_dentry(dentry, prune_parents);
NeilBrown0feae5c2006-06-22 14:47:28 -0700512 up_read(s_umount);
513 continue;
514 }
515 up_read(s_umount);
516 }
517 spin_unlock(&dentry->d_lock);
Vasily Averin6eac3f92006-10-28 10:38:44 -0700518 /*
519 * Insert dentry at the head of the list as inserting at the
520 * tail leads to a cycle.
NeilBrown0feae5c2006-06-22 14:47:28 -0700521 */
Vasily Averin6eac3f92006-10-28 10:38:44 -0700522 list_add(&dentry->d_lru, &dentry_unused);
523 dentry_stat.nr_unused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700524 }
525 spin_unlock(&dcache_lock);
526}
527
528/*
529 * Shrink the dcache for the specified super block.
530 * This allows us to unmount a device without disturbing
531 * the dcache for the other devices.
532 *
533 * This implementation makes just two traversals of the
534 * unused list. On the first pass we move the selected
535 * dentries to the most recent end, and on the second
536 * pass we free them. The second pass must restart after
537 * each dput(), but since the target dentries are all at
538 * the end, it's really just a single traversal.
539 */
540
541/**
542 * shrink_dcache_sb - shrink dcache for a superblock
543 * @sb: superblock
544 *
545 * Shrink the dcache for the specified super block. This
546 * is used to free the dcache before unmounting a file
547 * system
548 */
549
550void shrink_dcache_sb(struct super_block * sb)
551{
552 struct list_head *tmp, *next;
553 struct dentry *dentry;
554
555 /*
556 * Pass one ... move the dentries for the specified
557 * superblock to the most recent end of the unused list.
558 */
559 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700560 list_for_each_safe(tmp, next, &dentry_unused) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 dentry = list_entry(tmp, struct dentry, d_lru);
562 if (dentry->d_sb != sb)
563 continue;
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700564 list_move(tmp, &dentry_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700565 }
566
567 /*
568 * Pass two ... free the dentries for this superblock.
569 */
570repeat:
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700571 list_for_each_safe(tmp, next, &dentry_unused) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572 dentry = list_entry(tmp, struct dentry, d_lru);
573 if (dentry->d_sb != sb)
574 continue;
575 dentry_stat.nr_unused--;
576 list_del_init(tmp);
577 spin_lock(&dentry->d_lock);
578 if (atomic_read(&dentry->d_count)) {
579 spin_unlock(&dentry->d_lock);
580 continue;
581 }
Miklos Szeredid52b9082007-05-08 00:23:46 -0700582 prune_one_dentry(dentry, 1);
Kirill Korotaev2ab13462006-03-25 03:07:45 -0800583 cond_resched_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 goto repeat;
585 }
586 spin_unlock(&dcache_lock);
587}
588
589/*
David Howellsc636ebd2006-10-11 01:22:19 -0700590 * destroy a single subtree of dentries for unmount
591 * - see the comments on shrink_dcache_for_umount() for a description of the
592 * locking
593 */
594static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
595{
596 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700597 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700598
599 BUG_ON(!IS_ROOT(dentry));
600
601 /* detach this root from the system */
602 spin_lock(&dcache_lock);
603 if (!list_empty(&dentry->d_lru)) {
604 dentry_stat.nr_unused--;
605 list_del_init(&dentry->d_lru);
606 }
607 __d_drop(dentry);
608 spin_unlock(&dcache_lock);
609
610 for (;;) {
611 /* descend to the first leaf in the current subtree */
612 while (!list_empty(&dentry->d_subdirs)) {
613 struct dentry *loop;
614
615 /* this is a branch with children - detach all of them
616 * from the system in one go */
617 spin_lock(&dcache_lock);
618 list_for_each_entry(loop, &dentry->d_subdirs,
619 d_u.d_child) {
620 if (!list_empty(&loop->d_lru)) {
621 dentry_stat.nr_unused--;
622 list_del_init(&loop->d_lru);
623 }
624
625 __d_drop(loop);
626 cond_resched_lock(&dcache_lock);
627 }
628 spin_unlock(&dcache_lock);
629
630 /* move to the first child */
631 dentry = list_entry(dentry->d_subdirs.next,
632 struct dentry, d_u.d_child);
633 }
634
635 /* consume the dentries from this leaf up through its parents
636 * until we find one with children or run out altogether */
637 do {
638 struct inode *inode;
639
640 if (atomic_read(&dentry->d_count) != 0) {
641 printk(KERN_ERR
642 "BUG: Dentry %p{i=%lx,n=%s}"
643 " still in use (%d)"
644 " [unmount of %s %s]\n",
645 dentry,
646 dentry->d_inode ?
647 dentry->d_inode->i_ino : 0UL,
648 dentry->d_name.name,
649 atomic_read(&dentry->d_count),
650 dentry->d_sb->s_type->name,
651 dentry->d_sb->s_id);
652 BUG();
653 }
654
655 parent = dentry->d_parent;
656 if (parent == dentry)
657 parent = NULL;
658 else
659 atomic_dec(&parent->d_count);
660
661 list_del(&dentry->d_u.d_child);
David Howellsf8713572006-10-28 10:38:46 -0700662 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700663
664 inode = dentry->d_inode;
665 if (inode) {
666 dentry->d_inode = NULL;
667 list_del_init(&dentry->d_alias);
668 if (dentry->d_op && dentry->d_op->d_iput)
669 dentry->d_op->d_iput(dentry, inode);
670 else
671 iput(inode);
672 }
673
674 d_free(dentry);
675
676 /* finished when we fall off the top of the tree,
677 * otherwise we ascend to the parent and move to the
678 * next sibling if there is one */
679 if (!parent)
David Howellsf8713572006-10-28 10:38:46 -0700680 goto out;
David Howellsc636ebd2006-10-11 01:22:19 -0700681
682 dentry = parent;
683
684 } while (list_empty(&dentry->d_subdirs));
685
686 dentry = list_entry(dentry->d_subdirs.next,
687 struct dentry, d_u.d_child);
688 }
David Howellsf8713572006-10-28 10:38:46 -0700689out:
690 /* several dentries were freed, need to correct nr_dentry */
691 spin_lock(&dcache_lock);
692 dentry_stat.nr_dentry -= detached;
693 spin_unlock(&dcache_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700694}
695
696/*
697 * destroy the dentries attached to a superblock on unmounting
698 * - we don't need to use dentry->d_lock, and only need dcache_lock when
699 * removing the dentry from the system lists and hashes because:
700 * - the superblock is detached from all mountings and open files, so the
701 * dentry trees will not be rearranged by the VFS
702 * - s_umount is write-locked, so the memory pressure shrinker will ignore
703 * any dentries belonging to this superblock that it comes across
704 * - the filesystem itself is no longer permitted to rearrange the dentries
705 * in this superblock
706 */
707void shrink_dcache_for_umount(struct super_block *sb)
708{
709 struct dentry *dentry;
710
711 if (down_read_trylock(&sb->s_umount))
712 BUG();
713
714 dentry = sb->s_root;
715 sb->s_root = NULL;
716 atomic_dec(&dentry->d_count);
717 shrink_dcache_for_umount_subtree(dentry);
718
719 while (!hlist_empty(&sb->s_anon)) {
720 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
721 shrink_dcache_for_umount_subtree(dentry);
722 }
723}
724
725/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700726 * Search for at least 1 mount point in the dentry's subdirs.
727 * We descend to the next level whenever the d_subdirs
728 * list is non-empty and continue searching.
729 */
730
731/**
732 * have_submounts - check for mounts over a dentry
733 * @parent: dentry to check.
734 *
735 * Return true if the parent or its subdirectories contain
736 * a mount point
737 */
738
739int have_submounts(struct dentry *parent)
740{
741 struct dentry *this_parent = parent;
742 struct list_head *next;
743
744 spin_lock(&dcache_lock);
745 if (d_mountpoint(parent))
746 goto positive;
747repeat:
748 next = this_parent->d_subdirs.next;
749resume:
750 while (next != &this_parent->d_subdirs) {
751 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800752 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700753 next = tmp->next;
754 /* Have we found a mount point ? */
755 if (d_mountpoint(dentry))
756 goto positive;
757 if (!list_empty(&dentry->d_subdirs)) {
758 this_parent = dentry;
759 goto repeat;
760 }
761 }
762 /*
763 * All done at this level ... ascend and resume the search.
764 */
765 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800766 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 this_parent = this_parent->d_parent;
768 goto resume;
769 }
770 spin_unlock(&dcache_lock);
771 return 0; /* No mount points found in tree */
772positive:
773 spin_unlock(&dcache_lock);
774 return 1;
775}
776
777/*
778 * Search the dentry child list for the specified parent,
779 * and move any unused dentries to the end of the unused
780 * list for prune_dcache(). We descend to the next level
781 * whenever the d_subdirs list is non-empty and continue
782 * searching.
783 *
784 * It returns zero iff there are no unused children,
785 * otherwise it returns the number of children moved to
786 * the end of the unused list. This may not be the total
787 * number of unused children, because select_parent can
788 * drop the lock and return early due to latency
789 * constraints.
790 */
791static int select_parent(struct dentry * parent)
792{
793 struct dentry *this_parent = parent;
794 struct list_head *next;
795 int found = 0;
796
797 spin_lock(&dcache_lock);
798repeat:
799 next = this_parent->d_subdirs.next;
800resume:
801 while (next != &this_parent->d_subdirs) {
802 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800803 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804 next = tmp->next;
805
806 if (!list_empty(&dentry->d_lru)) {
807 dentry_stat.nr_unused--;
808 list_del_init(&dentry->d_lru);
809 }
810 /*
811 * move only zero ref count dentries to the end
812 * of the unused list for prune_dcache
813 */
814 if (!atomic_read(&dentry->d_count)) {
Akinobu Mita8e130592006-06-26 00:24:37 -0700815 list_add_tail(&dentry->d_lru, &dentry_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700816 dentry_stat.nr_unused++;
817 found++;
818 }
819
820 /*
821 * We can return to the caller if we have found some (this
822 * ensures forward progress). We'll be coming back to find
823 * the rest.
824 */
825 if (found && need_resched())
826 goto out;
827
828 /*
829 * Descend a level if the d_subdirs list is non-empty.
830 */
831 if (!list_empty(&dentry->d_subdirs)) {
832 this_parent = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700833 goto repeat;
834 }
835 }
836 /*
837 * All done at this level ... ascend and resume the search.
838 */
839 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800840 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700841 this_parent = this_parent->d_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 goto resume;
843 }
844out:
845 spin_unlock(&dcache_lock);
846 return found;
847}
848
849/**
850 * shrink_dcache_parent - prune dcache
851 * @parent: parent of entries to prune
852 *
853 * Prune the dcache to remove unused children of the parent dentry.
854 */
855
856void shrink_dcache_parent(struct dentry * parent)
857{
858 int found;
859
860 while ((found = select_parent(parent)) != 0)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700861 prune_dcache(found, parent->d_sb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700862}
863
Linus Torvalds1da177e2005-04-16 15:20:36 -0700864/*
865 * Scan `nr' dentries and return the number which remain.
866 *
867 * We need to avoid reentering the filesystem if the caller is performing a
868 * GFP_NOFS allocation attempt. One example deadlock is:
869 *
870 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
871 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
872 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
873 *
874 * In this case we return -1 to tell the caller that we baled.
875 */
Al Viro27496a82005-10-21 03:20:48 -0400876static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877{
878 if (nr) {
879 if (!(gfp_mask & __GFP_FS))
880 return -1;
Andrew Morton24c32d72007-05-08 00:23:49 -0700881 prune_dcache(nr, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700882 }
883 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
884}
885
886/**
887 * d_alloc - allocate a dcache entry
888 * @parent: parent of entry to allocate
889 * @name: qstr of the name
890 *
891 * Allocates a dentry. It returns %NULL if there is insufficient memory
892 * available. On a success the dentry is returned. The name passed in is
893 * copied and the copy passed in may be reused after this call.
894 */
895
896struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
897{
898 struct dentry *dentry;
899 char *dname;
900
901 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
902 if (!dentry)
903 return NULL;
904
905 if (name->len > DNAME_INLINE_LEN-1) {
906 dname = kmalloc(name->len + 1, GFP_KERNEL);
907 if (!dname) {
908 kmem_cache_free(dentry_cache, dentry);
909 return NULL;
910 }
911 } else {
912 dname = dentry->d_iname;
913 }
914 dentry->d_name.name = dname;
915
916 dentry->d_name.len = name->len;
917 dentry->d_name.hash = name->hash;
918 memcpy(dname, name->name, name->len);
919 dname[name->len] = 0;
920
921 atomic_set(&dentry->d_count, 1);
922 dentry->d_flags = DCACHE_UNHASHED;
923 spin_lock_init(&dentry->d_lock);
924 dentry->d_inode = NULL;
925 dentry->d_parent = NULL;
926 dentry->d_sb = NULL;
927 dentry->d_op = NULL;
928 dentry->d_fsdata = NULL;
929 dentry->d_mounted = 0;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800930#ifdef CONFIG_PROFILING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700931 dentry->d_cookie = NULL;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800932#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700933 INIT_HLIST_NODE(&dentry->d_hash);
934 INIT_LIST_HEAD(&dentry->d_lru);
935 INIT_LIST_HEAD(&dentry->d_subdirs);
936 INIT_LIST_HEAD(&dentry->d_alias);
937
938 if (parent) {
939 dentry->d_parent = dget(parent);
940 dentry->d_sb = parent->d_sb;
941 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800942 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700943 }
944
945 spin_lock(&dcache_lock);
946 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -0800947 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700948 dentry_stat.nr_dentry++;
949 spin_unlock(&dcache_lock);
950
951 return dentry;
952}
953
954struct dentry *d_alloc_name(struct dentry *parent, const char *name)
955{
956 struct qstr q;
957
958 q.name = name;
959 q.len = strlen(name);
960 q.hash = full_name_hash(q.name, q.len);
961 return d_alloc(parent, &q);
962}
963
964/**
965 * d_instantiate - fill in inode information for a dentry
966 * @entry: dentry to complete
967 * @inode: inode to attach to this dentry
968 *
969 * Fill in inode information in the entry.
970 *
971 * This turns negative dentries into productive full members
972 * of society.
973 *
974 * NOTE! This assumes that the inode count has been incremented
975 * (or otherwise set) by the caller to indicate that it is now
976 * in use by the dcache.
977 */
978
979void d_instantiate(struct dentry *entry, struct inode * inode)
980{
Eric Sesterhenn28133c72006-03-26 18:25:39 +0200981 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700982 spin_lock(&dcache_lock);
983 if (inode)
984 list_add(&entry->d_alias, &inode->i_dentry);
985 entry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800986 fsnotify_d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700987 spin_unlock(&dcache_lock);
988 security_d_instantiate(entry, inode);
989}
990
991/**
992 * d_instantiate_unique - instantiate a non-aliased dentry
993 * @entry: dentry to instantiate
994 * @inode: inode to attach to this dentry
995 *
996 * Fill in inode information in the entry. On success, it returns NULL.
997 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -0800998 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700999 *
1000 * Note that in order to avoid conflicts with rename() etc, the caller
1001 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001002 *
1003 * This also assumes that the inode count has been incremented
1004 * (or otherwise set) by the caller to indicate that it is now
1005 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001006 */
David Howells770bfad2006-08-22 20:06:07 -04001007static struct dentry *__d_instantiate_unique(struct dentry *entry,
1008 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001009{
1010 struct dentry *alias;
1011 int len = entry->d_name.len;
1012 const char *name = entry->d_name.name;
1013 unsigned int hash = entry->d_name.hash;
1014
David Howells770bfad2006-08-22 20:06:07 -04001015 if (!inode) {
1016 entry->d_inode = NULL;
1017 return NULL;
1018 }
1019
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1021 struct qstr *qstr = &alias->d_name;
1022
1023 if (qstr->hash != hash)
1024 continue;
1025 if (alias->d_parent != entry->d_parent)
1026 continue;
1027 if (qstr->len != len)
1028 continue;
1029 if (memcmp(qstr->name, name, len))
1030 continue;
1031 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 return alias;
1033 }
David Howells770bfad2006-08-22 20:06:07 -04001034
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035 list_add(&entry->d_alias, &inode->i_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 entry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -08001037 fsnotify_d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001038 return NULL;
1039}
David Howells770bfad2006-08-22 20:06:07 -04001040
1041struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1042{
1043 struct dentry *result;
1044
1045 BUG_ON(!list_empty(&entry->d_alias));
1046
1047 spin_lock(&dcache_lock);
1048 result = __d_instantiate_unique(entry, inode);
1049 spin_unlock(&dcache_lock);
1050
1051 if (!result) {
1052 security_d_instantiate(entry, inode);
1053 return NULL;
1054 }
1055
1056 BUG_ON(!d_unhashed(result));
1057 iput(inode);
1058 return result;
1059}
1060
Linus Torvalds1da177e2005-04-16 15:20:36 -07001061EXPORT_SYMBOL(d_instantiate_unique);
1062
1063/**
1064 * d_alloc_root - allocate root dentry
1065 * @root_inode: inode to allocate the root for
1066 *
1067 * Allocate a root ("/") dentry for the inode given. The inode is
1068 * instantiated and returned. %NULL is returned if there is insufficient
1069 * memory or the inode passed is %NULL.
1070 */
1071
1072struct dentry * d_alloc_root(struct inode * root_inode)
1073{
1074 struct dentry *res = NULL;
1075
1076 if (root_inode) {
1077 static const struct qstr name = { .name = "/", .len = 1 };
1078
1079 res = d_alloc(NULL, &name);
1080 if (res) {
1081 res->d_sb = root_inode->i_sb;
1082 res->d_parent = res;
1083 d_instantiate(res, root_inode);
1084 }
1085 }
1086 return res;
1087}
1088
1089static inline struct hlist_head *d_hash(struct dentry *parent,
1090 unsigned long hash)
1091{
1092 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1093 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1094 return dentry_hashtable + (hash & D_HASHMASK);
1095}
1096
1097/**
1098 * d_alloc_anon - allocate an anonymous dentry
1099 * @inode: inode to allocate the dentry for
1100 *
1101 * This is similar to d_alloc_root. It is used by filesystems when
1102 * creating a dentry for a given inode, often in the process of
1103 * mapping a filehandle to a dentry. The returned dentry may be
1104 * anonymous, or may have a full name (if the inode was already
1105 * in the cache). The file system may need to make further
1106 * efforts to connect this dentry into the dcache properly.
1107 *
1108 * When called on a directory inode, we must ensure that
1109 * the inode only ever has one dentry. If a dentry is
1110 * found, that is returned instead of allocating a new one.
1111 *
1112 * On successful return, the reference to the inode has been transferred
1113 * to the dentry. If %NULL is returned (indicating kmalloc failure),
1114 * the reference on the inode has not been released.
1115 */
1116
1117struct dentry * d_alloc_anon(struct inode *inode)
1118{
1119 static const struct qstr anonstring = { .name = "" };
1120 struct dentry *tmp;
1121 struct dentry *res;
1122
1123 if ((res = d_find_alias(inode))) {
1124 iput(inode);
1125 return res;
1126 }
1127
1128 tmp = d_alloc(NULL, &anonstring);
1129 if (!tmp)
1130 return NULL;
1131
1132 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1133
1134 spin_lock(&dcache_lock);
1135 res = __d_find_alias(inode, 0);
1136 if (!res) {
1137 /* attach a disconnected dentry */
1138 res = tmp;
1139 tmp = NULL;
1140 spin_lock(&res->d_lock);
1141 res->d_sb = inode->i_sb;
1142 res->d_parent = res;
1143 res->d_inode = inode;
1144 res->d_flags |= DCACHE_DISCONNECTED;
1145 res->d_flags &= ~DCACHE_UNHASHED;
1146 list_add(&res->d_alias, &inode->i_dentry);
1147 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
1148 spin_unlock(&res->d_lock);
1149
1150 inode = NULL; /* don't drop reference */
1151 }
1152 spin_unlock(&dcache_lock);
1153
1154 if (inode)
1155 iput(inode);
1156 if (tmp)
1157 dput(tmp);
1158 return res;
1159}
1160
1161
1162/**
1163 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1164 * @inode: the inode which may have a disconnected dentry
1165 * @dentry: a negative dentry which we want to point to the inode.
1166 *
1167 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1168 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1169 * and return it, else simply d_add the inode to the dentry and return NULL.
1170 *
1171 * This is needed in the lookup routine of any filesystem that is exportable
1172 * (via knfsd) so that we can build dcache paths to directories effectively.
1173 *
1174 * If a dentry was found and moved, then it is returned. Otherwise NULL
1175 * is returned. This matches the expected return value of ->lookup.
1176 *
1177 */
1178struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1179{
1180 struct dentry *new = NULL;
1181
NeilBrown21c0d8f2006-10-04 02:16:16 -07001182 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001183 spin_lock(&dcache_lock);
1184 new = __d_find_alias(inode, 1);
1185 if (new) {
1186 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Pigginc32ccd82006-03-25 03:07:09 -08001187 fsnotify_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001188 spin_unlock(&dcache_lock);
1189 security_d_instantiate(new, inode);
1190 d_rehash(dentry);
1191 d_move(new, dentry);
1192 iput(inode);
1193 } else {
1194 /* d_instantiate takes dcache_lock, so we do it by hand */
1195 list_add(&dentry->d_alias, &inode->i_dentry);
1196 dentry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -08001197 fsnotify_d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 spin_unlock(&dcache_lock);
1199 security_d_instantiate(dentry, inode);
1200 d_rehash(dentry);
1201 }
1202 } else
1203 d_add(dentry, inode);
1204 return new;
1205}
1206
1207
1208/**
1209 * d_lookup - search for a dentry
1210 * @parent: parent dentry
1211 * @name: qstr of name we wish to find
1212 *
1213 * Searches the children of the parent dentry for the name in question. If
1214 * the dentry is found its reference count is incremented and the dentry
1215 * is returned. The caller must use d_put to free the entry when it has
1216 * finished using it. %NULL is returned on failure.
1217 *
1218 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1219 * Memory barriers are used while updating and doing lockless traversal.
1220 * To avoid races with d_move while rename is happening, d_lock is used.
1221 *
1222 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1223 * and name pointer in one structure pointed by d_qstr.
1224 *
1225 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1226 * lookup is going on.
1227 *
1228 * dentry_unused list is not updated even if lookup finds the required dentry
1229 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1230 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1231 * acquisition.
1232 *
1233 * d_lookup() is protected against the concurrent renames in some unrelated
1234 * directory using the seqlockt_t rename_lock.
1235 */
1236
1237struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1238{
1239 struct dentry * dentry = NULL;
1240 unsigned long seq;
1241
1242 do {
1243 seq = read_seqbegin(&rename_lock);
1244 dentry = __d_lookup(parent, name);
1245 if (dentry)
1246 break;
1247 } while (read_seqretry(&rename_lock, seq));
1248 return dentry;
1249}
1250
1251struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1252{
1253 unsigned int len = name->len;
1254 unsigned int hash = name->hash;
1255 const unsigned char *str = name->name;
1256 struct hlist_head *head = d_hash(parent,hash);
1257 struct dentry *found = NULL;
1258 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001259 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001260
1261 rcu_read_lock();
1262
Paul E. McKenney665a7582005-11-07 00:59:17 -08001263 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264 struct qstr *qstr;
1265
Linus Torvalds1da177e2005-04-16 15:20:36 -07001266 if (dentry->d_name.hash != hash)
1267 continue;
1268 if (dentry->d_parent != parent)
1269 continue;
1270
1271 spin_lock(&dentry->d_lock);
1272
1273 /*
1274 * Recheck the dentry after taking the lock - d_move may have
1275 * changed things. Don't bother checking the hash because we're
1276 * about to compare the whole name anyway.
1277 */
1278 if (dentry->d_parent != parent)
1279 goto next;
1280
1281 /*
1282 * It is safe to compare names since d_move() cannot
1283 * change the qstr (protected by d_lock).
1284 */
1285 qstr = &dentry->d_name;
1286 if (parent->d_op && parent->d_op->d_compare) {
1287 if (parent->d_op->d_compare(parent, qstr, name))
1288 goto next;
1289 } else {
1290 if (qstr->len != len)
1291 goto next;
1292 if (memcmp(qstr->name, str, len))
1293 goto next;
1294 }
1295
1296 if (!d_unhashed(dentry)) {
1297 atomic_inc(&dentry->d_count);
1298 found = dentry;
1299 }
1300 spin_unlock(&dentry->d_lock);
1301 break;
1302next:
1303 spin_unlock(&dentry->d_lock);
1304 }
1305 rcu_read_unlock();
1306
1307 return found;
1308}
1309
1310/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001311 * d_hash_and_lookup - hash the qstr then search for a dentry
1312 * @dir: Directory to search in
1313 * @name: qstr of name we wish to find
1314 *
1315 * On hash failure or on lookup failure NULL is returned.
1316 */
1317struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1318{
1319 struct dentry *dentry = NULL;
1320
1321 /*
1322 * Check for a fs-specific hash function. Note that we must
1323 * calculate the standard hash first, as the d_op->d_hash()
1324 * routine may choose to leave the hash value unchanged.
1325 */
1326 name->hash = full_name_hash(name->name, name->len);
1327 if (dir->d_op && dir->d_op->d_hash) {
1328 if (dir->d_op->d_hash(dir, name) < 0)
1329 goto out;
1330 }
1331 dentry = d_lookup(dir, name);
1332out:
1333 return dentry;
1334}
1335
1336/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001337 * d_validate - verify dentry provided from insecure source
1338 * @dentry: The dentry alleged to be valid child of @dparent
1339 * @dparent: The parent dentry (known to be valid)
1340 * @hash: Hash of the dentry
1341 * @len: Length of the name
1342 *
1343 * An insecure source has sent us a dentry, here we verify it and dget() it.
1344 * This is used by ncpfs in its readdir implementation.
1345 * Zero is returned in the dentry is invalid.
1346 */
1347
1348int d_validate(struct dentry *dentry, struct dentry *dparent)
1349{
1350 struct hlist_head *base;
1351 struct hlist_node *lhp;
1352
1353 /* Check whether the ptr might be valid at all.. */
1354 if (!kmem_ptr_validate(dentry_cache, dentry))
1355 goto out;
1356
1357 if (dentry->d_parent != dparent)
1358 goto out;
1359
1360 spin_lock(&dcache_lock);
1361 base = d_hash(dparent, dentry->d_name.hash);
1362 hlist_for_each(lhp,base) {
Paul E. McKenney665a7582005-11-07 00:59:17 -08001363 /* hlist_for_each_entry_rcu() not required for d_hash list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001364 * as it is parsed under dcache_lock
1365 */
1366 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1367 __dget_locked(dentry);
1368 spin_unlock(&dcache_lock);
1369 return 1;
1370 }
1371 }
1372 spin_unlock(&dcache_lock);
1373out:
1374 return 0;
1375}
1376
1377/*
1378 * When a file is deleted, we have two options:
1379 * - turn this dentry into a negative dentry
1380 * - unhash this dentry and free it.
1381 *
1382 * Usually, we want to just turn this into
1383 * a negative dentry, but if anybody else is
1384 * currently using the dentry or the inode
1385 * we can't do that and we fall back on removing
1386 * it from the hash queues and waiting for
1387 * it to be deleted later when it has no users
1388 */
1389
1390/**
1391 * d_delete - delete a dentry
1392 * @dentry: The dentry to delete
1393 *
1394 * Turn the dentry into a negative dentry if possible, otherwise
1395 * remove it from the hash queues so it can be deleted later
1396 */
1397
1398void d_delete(struct dentry * dentry)
1399{
John McCutchan7a91bf72005-08-08 13:52:16 -04001400 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001401 /*
1402 * Are we the only user?
1403 */
1404 spin_lock(&dcache_lock);
1405 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001406 isdir = S_ISDIR(dentry->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001407 if (atomic_read(&dentry->d_count) == 1) {
1408 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001409 fsnotify_nameremove(dentry, isdir);
Amy Griffis7a2bd3f2006-03-31 02:30:54 -08001410
1411 /* remove this and other inotify debug checks after 2.6.18 */
1412 dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001413 return;
1414 }
1415
1416 if (!d_unhashed(dentry))
1417 __d_drop(dentry);
1418
1419 spin_unlock(&dentry->d_lock);
1420 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001421
1422 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423}
1424
1425static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1426{
1427
1428 entry->d_flags &= ~DCACHE_UNHASHED;
1429 hlist_add_head_rcu(&entry->d_hash, list);
1430}
1431
David Howells770bfad2006-08-22 20:06:07 -04001432static void _d_rehash(struct dentry * entry)
1433{
1434 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1435}
1436
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437/**
1438 * d_rehash - add an entry back to the hash
1439 * @entry: dentry to add to the hash
1440 *
1441 * Adds a dentry to the hash according to its name.
1442 */
1443
1444void d_rehash(struct dentry * entry)
1445{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001446 spin_lock(&dcache_lock);
1447 spin_lock(&entry->d_lock);
David Howells770bfad2006-08-22 20:06:07 -04001448 _d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001449 spin_unlock(&entry->d_lock);
1450 spin_unlock(&dcache_lock);
1451}
1452
1453#define do_switch(x,y) do { \
1454 __typeof__ (x) __tmp = x; \
1455 x = y; y = __tmp; } while (0)
1456
1457/*
1458 * When switching names, the actual string doesn't strictly have to
1459 * be preserved in the target - because we're dropping the target
1460 * anyway. As such, we can just do a simple memcpy() to copy over
1461 * the new name before we switch.
1462 *
1463 * Note that we have to be a lot more careful about getting the hash
1464 * switched - we have to switch the hash value properly even if it
1465 * then no longer matches the actual (corrupted) string of the target.
1466 * The hash value has to match the hash queue that the dentry is on..
1467 */
1468static void switch_names(struct dentry *dentry, struct dentry *target)
1469{
1470 if (dname_external(target)) {
1471 if (dname_external(dentry)) {
1472 /*
1473 * Both external: swap the pointers
1474 */
1475 do_switch(target->d_name.name, dentry->d_name.name);
1476 } else {
1477 /*
1478 * dentry:internal, target:external. Steal target's
1479 * storage and make target internal.
1480 */
1481 dentry->d_name.name = target->d_name.name;
1482 target->d_name.name = target->d_iname;
1483 }
1484 } else {
1485 if (dname_external(dentry)) {
1486 /*
1487 * dentry:external, target:internal. Give dentry's
1488 * storage to target and make dentry internal
1489 */
1490 memcpy(dentry->d_iname, target->d_name.name,
1491 target->d_name.len + 1);
1492 target->d_name.name = dentry->d_name.name;
1493 dentry->d_name.name = dentry->d_iname;
1494 } else {
1495 /*
1496 * Both are internal. Just copy target to dentry
1497 */
1498 memcpy(dentry->d_iname, target->d_name.name,
1499 target->d_name.len + 1);
1500 }
1501 }
1502}
1503
1504/*
1505 * We cannibalize "target" when moving dentry on top of it,
1506 * because it's going to be thrown away anyway. We could be more
1507 * polite about it, though.
1508 *
1509 * This forceful removal will result in ugly /proc output if
1510 * somebody holds a file open that got deleted due to a rename.
1511 * We could be nicer about the deleted file, and let it show
1512 * up under the name it got deleted rather than the name that
1513 * deleted it.
1514 */
1515
Trond Myklebust9eaef272006-10-21 10:24:20 -07001516/*
1517 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001518 * @dentry: entry to move
1519 * @target: new dentry
1520 *
1521 * Update the dcache to reflect the move of a file name. Negative
1522 * dcache entries should not be moved in this way.
1523 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001524static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001525{
1526 struct hlist_head *list;
1527
1528 if (!dentry->d_inode)
1529 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1530
Linus Torvalds1da177e2005-04-16 15:20:36 -07001531 write_seqlock(&rename_lock);
1532 /*
1533 * XXXX: do we really need to take target->d_lock?
1534 */
1535 if (target < dentry) {
1536 spin_lock(&target->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001537 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 } else {
1539 spin_lock(&dentry->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001540 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001541 }
1542
1543 /* Move the dentry to the target hash queue, if on different bucket */
1544 if (dentry->d_flags & DCACHE_UNHASHED)
1545 goto already_unhashed;
1546
1547 hlist_del_rcu(&dentry->d_hash);
1548
1549already_unhashed:
1550 list = d_hash(target->d_parent, target->d_name.hash);
1551 __d_rehash(dentry, list);
1552
1553 /* Unhash the target: dput() will then get rid of it */
1554 __d_drop(target);
1555
Eric Dumazet5160ee62006-01-08 01:03:32 -08001556 list_del(&dentry->d_u.d_child);
1557 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001558
1559 /* Switch the names.. */
1560 switch_names(dentry, target);
1561 do_switch(dentry->d_name.len, target->d_name.len);
1562 do_switch(dentry->d_name.hash, target->d_name.hash);
1563
1564 /* ... and switch the parents */
1565 if (IS_ROOT(dentry)) {
1566 dentry->d_parent = target->d_parent;
1567 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001568 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 } else {
1570 do_switch(dentry->d_parent, target->d_parent);
1571
1572 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001573 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001574 }
1575
Eric Dumazet5160ee62006-01-08 01:03:32 -08001576 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001578 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 spin_unlock(&dentry->d_lock);
1580 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001581}
1582
1583/**
1584 * d_move - move a dentry
1585 * @dentry: entry to move
1586 * @target: new dentry
1587 *
1588 * Update the dcache to reflect the move of a file name. Negative
1589 * dcache entries should not be moved in this way.
1590 */
1591
1592void d_move(struct dentry * dentry, struct dentry * target)
1593{
1594 spin_lock(&dcache_lock);
1595 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001596 spin_unlock(&dcache_lock);
1597}
1598
David Howells770bfad2006-08-22 20:06:07 -04001599/*
Trond Myklebust9eaef272006-10-21 10:24:20 -07001600 * Helper that returns 1 if p1 is a parent of p2, else 0
1601 */
1602static int d_isparent(struct dentry *p1, struct dentry *p2)
1603{
1604 struct dentry *p;
1605
1606 for (p = p2; p->d_parent != p; p = p->d_parent) {
1607 if (p->d_parent == p1)
1608 return 1;
1609 }
1610 return 0;
1611}
1612
1613/*
1614 * This helper attempts to cope with remotely renamed directories
1615 *
1616 * It assumes that the caller is already holding
1617 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1618 *
1619 * Note: If ever the locking in lock_rename() changes, then please
1620 * remember to update this too...
1621 *
1622 * On return, dcache_lock will have been unlocked.
1623 */
1624static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1625{
1626 struct mutex *m1 = NULL, *m2 = NULL;
1627 struct dentry *ret;
1628
1629 /* If alias and dentry share a parent, then no extra locks required */
1630 if (alias->d_parent == dentry->d_parent)
1631 goto out_unalias;
1632
1633 /* Check for loops */
1634 ret = ERR_PTR(-ELOOP);
1635 if (d_isparent(alias, dentry))
1636 goto out_err;
1637
1638 /* See lock_rename() */
1639 ret = ERR_PTR(-EBUSY);
1640 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1641 goto out_err;
1642 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1643 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1644 goto out_err;
1645 m2 = &alias->d_parent->d_inode->i_mutex;
1646out_unalias:
1647 d_move_locked(alias, dentry);
1648 ret = alias;
1649out_err:
1650 spin_unlock(&dcache_lock);
1651 if (m2)
1652 mutex_unlock(m2);
1653 if (m1)
1654 mutex_unlock(m1);
1655 return ret;
1656}
1657
1658/*
David Howells770bfad2006-08-22 20:06:07 -04001659 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1660 * named dentry in place of the dentry to be replaced.
1661 */
1662static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1663{
1664 struct dentry *dparent, *aparent;
1665
1666 switch_names(dentry, anon);
1667 do_switch(dentry->d_name.len, anon->d_name.len);
1668 do_switch(dentry->d_name.hash, anon->d_name.hash);
1669
1670 dparent = dentry->d_parent;
1671 aparent = anon->d_parent;
1672
1673 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1674 list_del(&dentry->d_u.d_child);
1675 if (!IS_ROOT(dentry))
1676 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1677 else
1678 INIT_LIST_HEAD(&dentry->d_u.d_child);
1679
1680 anon->d_parent = (dparent == dentry) ? anon : dparent;
1681 list_del(&anon->d_u.d_child);
1682 if (!IS_ROOT(anon))
1683 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1684 else
1685 INIT_LIST_HEAD(&anon->d_u.d_child);
1686
1687 anon->d_flags &= ~DCACHE_DISCONNECTED;
1688}
1689
1690/**
1691 * d_materialise_unique - introduce an inode into the tree
1692 * @dentry: candidate dentry
1693 * @inode: inode to bind to the dentry, to which aliases may be attached
1694 *
1695 * Introduces an dentry into the tree, substituting an extant disconnected
1696 * root directory alias in its place if there is one
1697 */
1698struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1699{
Trond Myklebust9eaef272006-10-21 10:24:20 -07001700 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04001701
1702 BUG_ON(!d_unhashed(dentry));
1703
1704 spin_lock(&dcache_lock);
1705
1706 if (!inode) {
1707 actual = dentry;
1708 dentry->d_inode = NULL;
1709 goto found_lock;
1710 }
1711
Trond Myklebust9eaef272006-10-21 10:24:20 -07001712 if (S_ISDIR(inode->i_mode)) {
1713 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04001714
Trond Myklebust9eaef272006-10-21 10:24:20 -07001715 /* Does an aliased dentry already exist? */
1716 alias = __d_find_alias(inode, 0);
1717 if (alias) {
1718 actual = alias;
1719 /* Is this an anonymous mountpoint that we could splice
1720 * into our tree? */
1721 if (IS_ROOT(alias)) {
1722 spin_lock(&alias->d_lock);
1723 __d_materialise_dentry(dentry, alias);
1724 __d_drop(alias);
1725 goto found;
1726 }
1727 /* Nope, but we must(!) avoid directory aliasing */
1728 actual = __d_unalias(dentry, alias);
1729 if (IS_ERR(actual))
1730 dput(alias);
1731 goto out_nolock;
1732 }
David Howells770bfad2006-08-22 20:06:07 -04001733 }
1734
1735 /* Add a unique reference */
1736 actual = __d_instantiate_unique(dentry, inode);
1737 if (!actual)
1738 actual = dentry;
1739 else if (unlikely(!d_unhashed(actual)))
1740 goto shouldnt_be_hashed;
1741
1742found_lock:
1743 spin_lock(&actual->d_lock);
1744found:
1745 _d_rehash(actual);
1746 spin_unlock(&actual->d_lock);
1747 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001748out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04001749 if (actual == dentry) {
1750 security_d_instantiate(dentry, inode);
1751 return NULL;
1752 }
1753
1754 iput(inode);
1755 return actual;
1756
David Howells770bfad2006-08-22 20:06:07 -04001757shouldnt_be_hashed:
1758 spin_unlock(&dcache_lock);
1759 BUG();
1760 goto shouldnt_be_hashed;
1761}
1762
Linus Torvalds1da177e2005-04-16 15:20:36 -07001763/**
1764 * d_path - return the path of a dentry
1765 * @dentry: dentry to report
1766 * @vfsmnt: vfsmnt to which the dentry belongs
1767 * @root: root dentry
1768 * @rootmnt: vfsmnt to which the root dentry belongs
1769 * @buffer: buffer to return value in
1770 * @buflen: buffer length
1771 *
Linus Torvalds552ce542007-02-13 12:08:18 -08001772 * Convert a dentry into an ASCII path name. If the entry has been deleted
1773 * the string " (deleted)" is appended. Note that this is ambiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001774 *
Linus Torvalds552ce542007-02-13 12:08:18 -08001775 * Returns the buffer or an error code if the path was too long.
1776 *
1777 * "buflen" should be positive. Caller holds the dcache_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 */
Linus Torvalds552ce542007-02-13 12:08:18 -08001779static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1780 struct dentry *root, struct vfsmount *rootmnt,
1781 char *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001782{
Linus Torvalds552ce542007-02-13 12:08:18 -08001783 char * end = buffer+buflen;
1784 char * retval;
1785 int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
Linus Torvalds552ce542007-02-13 12:08:18 -08001787 *--end = '\0';
1788 buflen--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001789 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
Andreas Gruenbachereb3dfb02007-02-12 00:51:47 -08001790 buflen -= 10;
Linus Torvalds552ce542007-02-13 12:08:18 -08001791 end -= 10;
1792 if (buflen < 0)
1793 goto Elong;
1794 memcpy(end, " (deleted)", 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 }
Linus Torvalds552ce542007-02-13 12:08:18 -08001796
1797 if (buflen < 1)
1798 goto Elong;
1799 /* Get '/' right */
1800 retval = end-1;
1801 *retval = '/';
1802
1803 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 struct dentry * parent;
1805
Linus Torvalds552ce542007-02-13 12:08:18 -08001806 if (dentry == root && vfsmnt == rootmnt)
1807 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001808 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08001809 /* Global root? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 spin_lock(&vfsmount_lock);
1811 if (vfsmnt->mnt_parent == vfsmnt) {
1812 spin_unlock(&vfsmount_lock);
1813 goto global_root;
1814 }
1815 dentry = vfsmnt->mnt_mountpoint;
1816 vfsmnt = vfsmnt->mnt_parent;
1817 spin_unlock(&vfsmount_lock);
1818 continue;
1819 }
1820 parent = dentry->d_parent;
1821 prefetch(parent);
1822 namelen = dentry->d_name.len;
Andreas Gruenbachereb3dfb02007-02-12 00:51:47 -08001823 buflen -= namelen + 1;
Linus Torvalds552ce542007-02-13 12:08:18 -08001824 if (buflen < 0)
1825 goto Elong;
1826 end -= namelen;
1827 memcpy(end, dentry->d_name.name, namelen);
1828 *--end = '/';
1829 retval = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001830 dentry = parent;
1831 }
1832
Linus Torvalds552ce542007-02-13 12:08:18 -08001833 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001834
1835global_root:
1836 namelen = dentry->d_name.len;
Linus Torvalds552ce542007-02-13 12:08:18 -08001837 buflen -= namelen;
1838 if (buflen < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 goto Elong;
Linus Torvalds552ce542007-02-13 12:08:18 -08001840 retval -= namelen-1; /* hit the slash */
1841 memcpy(retval, dentry->d_name.name, namelen);
1842 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843Elong:
Linus Torvalds552ce542007-02-13 12:08:18 -08001844 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845}
1846
1847/* write full pathname into buffer and return start of pathname */
Linus Torvalds552ce542007-02-13 12:08:18 -08001848char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1849 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850{
1851 char *res;
1852 struct vfsmount *rootmnt;
1853 struct dentry *root;
1854
Eric Dumazetc23fbb62007-05-08 00:26:18 -07001855 /*
1856 * We have various synthetic filesystems that never get mounted. On
1857 * these filesystems dentries are never used for lookup purposes, and
1858 * thus don't need to be hashed. They also don't need a name until a
1859 * user wants to identify the object in /proc/pid/fd/. The little hack
1860 * below allows us to generate a name for these objects on demand:
1861 */
1862 if (dentry->d_op && dentry->d_op->d_dname)
1863 return dentry->d_op->d_dname(dentry, buf, buflen);
1864
Linus Torvalds1da177e2005-04-16 15:20:36 -07001865 read_lock(&current->fs->lock);
1866 rootmnt = mntget(current->fs->rootmnt);
1867 root = dget(current->fs->root);
1868 read_unlock(&current->fs->lock);
Linus Torvalds552ce542007-02-13 12:08:18 -08001869 spin_lock(&dcache_lock);
1870 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1871 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001872 dput(root);
1873 mntput(rootmnt);
1874 return res;
1875}
1876
1877/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07001878 * Helper function for dentry_operations.d_dname() members
1879 */
1880char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
1881 const char *fmt, ...)
1882{
1883 va_list args;
1884 char temp[64];
1885 int sz;
1886
1887 va_start(args, fmt);
1888 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
1889 va_end(args);
1890
1891 if (sz > sizeof(temp) || sz > buflen)
1892 return ERR_PTR(-ENAMETOOLONG);
1893
1894 buffer += buflen - sz;
1895 return memcpy(buffer, temp, sz);
1896}
1897
1898/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899 * NOTE! The user-level library version returns a
1900 * character pointer. The kernel system call just
1901 * returns the length of the buffer filled (which
1902 * includes the ending '\0' character), or a negative
1903 * error value. So libc would do something like
1904 *
1905 * char *getcwd(char * buf, size_t size)
1906 * {
1907 * int retval;
1908 *
1909 * retval = sys_getcwd(buf, size);
1910 * if (retval >= 0)
1911 * return buf;
1912 * errno = -retval;
1913 * return NULL;
1914 * }
1915 */
1916asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1917{
Linus Torvalds552ce542007-02-13 12:08:18 -08001918 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 struct vfsmount *pwdmnt, *rootmnt;
1920 struct dentry *pwd, *root;
Linus Torvalds552ce542007-02-13 12:08:18 -08001921 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922
1923 if (!page)
1924 return -ENOMEM;
1925
1926 read_lock(&current->fs->lock);
1927 pwdmnt = mntget(current->fs->pwdmnt);
1928 pwd = dget(current->fs->pwd);
1929 rootmnt = mntget(current->fs->rootmnt);
1930 root = dget(current->fs->root);
1931 read_unlock(&current->fs->lock);
1932
Linus Torvalds552ce542007-02-13 12:08:18 -08001933 error = -ENOENT;
1934 /* Has the current directory has been unlinked? */
1935 spin_lock(&dcache_lock);
1936 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1937 unsigned long len;
1938 char * cwd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001939
Linus Torvalds552ce542007-02-13 12:08:18 -08001940 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1941 spin_unlock(&dcache_lock);
1942
1943 error = PTR_ERR(cwd);
1944 if (IS_ERR(cwd))
1945 goto out;
1946
1947 error = -ERANGE;
1948 len = PAGE_SIZE + page - cwd;
1949 if (len <= size) {
1950 error = len;
1951 if (copy_to_user(buf, cwd, len))
1952 error = -EFAULT;
1953 }
1954 } else
1955 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001956
1957out:
1958 dput(pwd);
1959 mntput(pwdmnt);
1960 dput(root);
1961 mntput(rootmnt);
1962 free_page((unsigned long) page);
1963 return error;
1964}
1965
1966/*
1967 * Test whether new_dentry is a subdirectory of old_dentry.
1968 *
1969 * Trivially implemented using the dcache structure
1970 */
1971
1972/**
1973 * is_subdir - is new dentry a subdirectory of old_dentry
1974 * @new_dentry: new dentry
1975 * @old_dentry: old dentry
1976 *
1977 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1978 * Returns 0 otherwise.
1979 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1980 */
1981
1982int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1983{
1984 int result;
1985 struct dentry * saved = new_dentry;
1986 unsigned long seq;
1987
1988 /* need rcu_readlock to protect against the d_parent trashing due to
1989 * d_move
1990 */
1991 rcu_read_lock();
1992 do {
1993 /* for restarting inner loop in case of seq retry */
1994 new_dentry = saved;
1995 result = 0;
1996 seq = read_seqbegin(&rename_lock);
1997 for (;;) {
1998 if (new_dentry != old_dentry) {
1999 struct dentry * parent = new_dentry->d_parent;
2000 if (parent == new_dentry)
2001 break;
2002 new_dentry = parent;
2003 continue;
2004 }
2005 result = 1;
2006 break;
2007 }
2008 } while (read_seqretry(&rename_lock, seq));
2009 rcu_read_unlock();
2010
2011 return result;
2012}
2013
2014void d_genocide(struct dentry *root)
2015{
2016 struct dentry *this_parent = root;
2017 struct list_head *next;
2018
2019 spin_lock(&dcache_lock);
2020repeat:
2021 next = this_parent->d_subdirs.next;
2022resume:
2023 while (next != &this_parent->d_subdirs) {
2024 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002025 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 next = tmp->next;
2027 if (d_unhashed(dentry)||!dentry->d_inode)
2028 continue;
2029 if (!list_empty(&dentry->d_subdirs)) {
2030 this_parent = dentry;
2031 goto repeat;
2032 }
2033 atomic_dec(&dentry->d_count);
2034 }
2035 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08002036 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002037 atomic_dec(&this_parent->d_count);
2038 this_parent = this_parent->d_parent;
2039 goto resume;
2040 }
2041 spin_unlock(&dcache_lock);
2042}
2043
2044/**
2045 * find_inode_number - check for dentry with name
2046 * @dir: directory to check
2047 * @name: Name to find.
2048 *
2049 * Check whether a dentry already exists for the given name,
2050 * and return the inode number if it has an inode. Otherwise
2051 * 0 is returned.
2052 *
2053 * This routine is used to post-process directory listings for
2054 * filesystems using synthetic inode numbers, and is necessary
2055 * to keep getcwd() working.
2056 */
2057
2058ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2059{
2060 struct dentry * dentry;
2061 ino_t ino = 0;
2062
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002063 dentry = d_hash_and_lookup(dir, name);
2064 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002065 if (dentry->d_inode)
2066 ino = dentry->d_inode->i_ino;
2067 dput(dentry);
2068 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 return ino;
2070}
2071
2072static __initdata unsigned long dhash_entries;
2073static int __init set_dhash_entries(char *str)
2074{
2075 if (!str)
2076 return 0;
2077 dhash_entries = simple_strtoul(str, &str, 0);
2078 return 1;
2079}
2080__setup("dhash_entries=", set_dhash_entries);
2081
2082static void __init dcache_init_early(void)
2083{
2084 int loop;
2085
2086 /* If hashes are distributed across NUMA nodes, defer
2087 * hash allocation until vmalloc space is available.
2088 */
2089 if (hashdist)
2090 return;
2091
2092 dentry_hashtable =
2093 alloc_large_system_hash("Dentry cache",
2094 sizeof(struct hlist_head),
2095 dhash_entries,
2096 13,
2097 HASH_EARLY,
2098 &d_hash_shift,
2099 &d_hash_mask,
2100 0);
2101
2102 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2103 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2104}
2105
2106static void __init dcache_init(unsigned long mempages)
2107{
2108 int loop;
2109
2110 /*
2111 * A constructor could be added for stable state like the lists,
2112 * but it is probably not worth it because of the cache nature
2113 * of the dcache.
2114 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002115 dentry_cache = KMEM_CACHE(dentry,
2116 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002117
2118 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
2119
2120 /* Hash may have been set up in dcache_init_early */
2121 if (!hashdist)
2122 return;
2123
2124 dentry_hashtable =
2125 alloc_large_system_hash("Dentry cache",
2126 sizeof(struct hlist_head),
2127 dhash_entries,
2128 13,
2129 0,
2130 &d_hash_shift,
2131 &d_hash_mask,
2132 0);
2133
2134 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2135 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2136}
2137
2138/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002139struct kmem_cache *names_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141/* SLAB cache for file structures */
Christoph Lametere18b8902006-12-06 20:33:20 -08002142struct kmem_cache *filp_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002143
2144EXPORT_SYMBOL(d_genocide);
2145
Linus Torvalds1da177e2005-04-16 15:20:36 -07002146void __init vfs_caches_init_early(void)
2147{
2148 dcache_init_early();
2149 inode_init_early();
2150}
2151
2152void __init vfs_caches_init(unsigned long mempages)
2153{
2154 unsigned long reserve;
2155
2156 /* Base hash sizes on available memory, with a reserve equal to
2157 150% of current kernel size */
2158
2159 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2160 mempages -= reserve;
2161
2162 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2163 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
2164
2165 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
Dipankar Sarma529bf6b2006-03-07 21:55:35 -08002166 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167
2168 dcache_init(mempages);
2169 inode_init(mempages);
2170 files_init(mempages);
2171 mnt_init(mempages);
2172 bdev_cache_init();
2173 chrdev_init();
2174}
2175
2176EXPORT_SYMBOL(d_alloc);
2177EXPORT_SYMBOL(d_alloc_anon);
2178EXPORT_SYMBOL(d_alloc_root);
2179EXPORT_SYMBOL(d_delete);
2180EXPORT_SYMBOL(d_find_alias);
2181EXPORT_SYMBOL(d_instantiate);
2182EXPORT_SYMBOL(d_invalidate);
2183EXPORT_SYMBOL(d_lookup);
2184EXPORT_SYMBOL(d_move);
David Howells770bfad2006-08-22 20:06:07 -04002185EXPORT_SYMBOL_GPL(d_materialise_unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002186EXPORT_SYMBOL(d_path);
2187EXPORT_SYMBOL(d_prune_aliases);
2188EXPORT_SYMBOL(d_rehash);
2189EXPORT_SYMBOL(d_splice_alias);
2190EXPORT_SYMBOL(d_validate);
2191EXPORT_SYMBOL(dget_locked);
2192EXPORT_SYMBOL(dput);
2193EXPORT_SYMBOL(find_inode_number);
2194EXPORT_SYMBOL(have_submounts);
2195EXPORT_SYMBOL(names_cachep);
2196EXPORT_SYMBOL(shrink_dcache_parent);
2197EXPORT_SYMBOL(shrink_dcache_sb);