blob: 2135ab8bb103d1fabef4ef2f90b7bcafc50dbabd [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>
24#include <linux/smp_lock.h>
25#include <linux/hash.h>
26#include <linux/cache.h>
27#include <linux/module.h>
28#include <linux/mount.h>
29#include <linux/file.h>
30#include <asm/uaccess.h>
31#include <linux/security.h>
32#include <linux/seqlock.h>
33#include <linux/swap.h>
34#include <linux/bootmem.h>
David Howells07f3f052006-09-30 20:52:18 +020035#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070036
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Eric Dumazetfa3536c2006-03-26 01:37:24 -080038int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
40
41 __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
Ingo Molnare4d91912006-07-03 00:24:34 -070042static __cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070043
44EXPORT_SYMBOL(dcache_lock);
45
Christoph Lametere18b8902006-12-06 20:33:20 -080046static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070047
48#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
49
50/*
51 * This is the single most critical data structure when it comes
52 * to the dcache: the hashtable for lookups. Somebody should try
53 * to make this good - I've just made it work.
54 *
55 * This hash-function tries to avoid losing too many bits of hash
56 * information, yet avoid using a prime hash-size or similar.
57 */
58#define D_HASHBITS d_hash_shift
59#define D_HASHMASK d_hash_mask
60
Eric Dumazetfa3536c2006-03-26 01:37:24 -080061static unsigned int d_hash_mask __read_mostly;
62static unsigned int d_hash_shift __read_mostly;
63static struct hlist_head *dentry_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070064static LIST_HEAD(dentry_unused);
65
66/* Statistics gathering. */
67struct dentry_stat_t dentry_stat = {
68 .age_limit = 45,
69};
70
Eric Dumazetb3423412006-12-06 20:38:48 -080071static void __d_free(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -070072{
Linus Torvalds1da177e2005-04-16 15:20:36 -070073 if (dname_external(dentry))
74 kfree(dentry->d_name.name);
75 kmem_cache_free(dentry_cache, dentry);
76}
77
Eric Dumazetb3423412006-12-06 20:38:48 -080078static void d_callback(struct rcu_head *head)
79{
80 struct dentry * dentry = container_of(head, struct dentry, d_u.d_rcu);
81 __d_free(dentry);
82}
83
Linus Torvalds1da177e2005-04-16 15:20:36 -070084/*
85 * no dcache_lock, please. The caller must decrement dentry_stat.nr_dentry
86 * inside dcache_lock.
87 */
88static void d_free(struct dentry *dentry)
89{
90 if (dentry->d_op && dentry->d_op->d_release)
91 dentry->d_op->d_release(dentry);
Eric Dumazetb3423412006-12-06 20:38:48 -080092 /* if dentry was never inserted into hash, immediate free is OK */
93 if (dentry->d_hash.pprev == NULL)
94 __d_free(dentry);
95 else
96 call_rcu(&dentry->d_u.d_rcu, d_callback);
Linus Torvalds1da177e2005-04-16 15:20:36 -070097}
98
99/*
100 * Release the dentry's inode, using the filesystem
101 * d_iput() operation if defined.
102 * Called with dcache_lock and per dentry lock held, drops both.
103 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800104static void dentry_iput(struct dentry * dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105{
106 struct inode *inode = dentry->d_inode;
107 if (inode) {
108 dentry->d_inode = NULL;
109 list_del_init(&dentry->d_alias);
110 spin_unlock(&dentry->d_lock);
111 spin_unlock(&dcache_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700112 if (!inode->i_nlink)
113 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700114 if (dentry->d_op && dentry->d_op->d_iput)
115 dentry->d_op->d_iput(dentry, inode);
116 else
117 iput(inode);
118 } else {
119 spin_unlock(&dentry->d_lock);
120 spin_unlock(&dcache_lock);
121 }
122}
123
Miklos Szeredid52b9082007-05-08 00:23:46 -0700124/**
125 * d_kill - kill dentry and return parent
126 * @dentry: dentry to kill
127 *
128 * Called with dcache_lock and d_lock, releases both. The dentry must
129 * already be unhashed and removed from the LRU.
130 *
131 * If this is the root of the dentry tree, return NULL.
132 */
133static struct dentry *d_kill(struct dentry *dentry)
134{
135 struct dentry *parent;
136
137 list_del(&dentry->d_u.d_child);
138 dentry_stat.nr_dentry--; /* For d_free, below */
139 /*drops the locks, at that point nobody can reach this dentry */
140 dentry_iput(dentry);
141 parent = dentry->d_parent;
142 d_free(dentry);
143 return dentry == parent ? NULL : parent;
144}
145
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146/*
147 * This is dput
148 *
149 * This is complicated by the fact that we do not want to put
150 * dentries that are no longer on any hash chain on the unused
151 * list: we'd much rather just get rid of them immediately.
152 *
153 * However, that implies that we have to traverse the dentry
154 * tree upwards to the parents which might _also_ now be
155 * scheduled for deletion (it may have been only waiting for
156 * its last child to go away).
157 *
158 * This tail recursion is done by hand as we don't want to depend
159 * on the compiler to always get this right (gcc generally doesn't).
160 * Real recursion would eat up our stack space.
161 */
162
163/*
164 * dput - release a dentry
165 * @dentry: dentry to release
166 *
167 * Release a dentry. This will drop the usage count and if appropriate
168 * call the dentry unlink method as well as removing it from the queues and
169 * releasing its resources. If the parent dentries were scheduled for release
170 * they too may now get deleted.
171 *
172 * no dcache lock, please.
173 */
174
175void dput(struct dentry *dentry)
176{
177 if (!dentry)
178 return;
179
180repeat:
181 if (atomic_read(&dentry->d_count) == 1)
182 might_sleep();
183 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
184 return;
185
186 spin_lock(&dentry->d_lock);
187 if (atomic_read(&dentry->d_count)) {
188 spin_unlock(&dentry->d_lock);
189 spin_unlock(&dcache_lock);
190 return;
191 }
192
193 /*
194 * AV: ->d_delete() is _NOT_ allowed to block now.
195 */
196 if (dentry->d_op && dentry->d_op->d_delete) {
197 if (dentry->d_op->d_delete(dentry))
198 goto unhash_it;
199 }
200 /* Unreachable? Get rid of it */
201 if (d_unhashed(dentry))
202 goto kill_it;
203 if (list_empty(&dentry->d_lru)) {
204 dentry->d_flags |= DCACHE_REFERENCED;
205 list_add(&dentry->d_lru, &dentry_unused);
206 dentry_stat.nr_unused++;
207 }
208 spin_unlock(&dentry->d_lock);
209 spin_unlock(&dcache_lock);
210 return;
211
212unhash_it:
213 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700214kill_it:
215 /* If dentry was on d_lru list
216 * delete it from there
217 */
218 if (!list_empty(&dentry->d_lru)) {
219 list_del(&dentry->d_lru);
220 dentry_stat.nr_unused--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221 }
Miklos Szeredid52b9082007-05-08 00:23:46 -0700222 dentry = d_kill(dentry);
223 if (dentry)
224 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700225}
226
227/**
228 * d_invalidate - invalidate a dentry
229 * @dentry: dentry to invalidate
230 *
231 * Try to invalidate the dentry if it turns out to be
232 * possible. If there are other dentries that can be
233 * reached through this one we can't delete it and we
234 * return -EBUSY. On success we return 0.
235 *
236 * no dcache lock.
237 */
238
239int d_invalidate(struct dentry * dentry)
240{
241 /*
242 * If it's already been dropped, return OK.
243 */
244 spin_lock(&dcache_lock);
245 if (d_unhashed(dentry)) {
246 spin_unlock(&dcache_lock);
247 return 0;
248 }
249 /*
250 * Check whether to do a partial shrink_dcache
251 * to get rid of unused child entries.
252 */
253 if (!list_empty(&dentry->d_subdirs)) {
254 spin_unlock(&dcache_lock);
255 shrink_dcache_parent(dentry);
256 spin_lock(&dcache_lock);
257 }
258
259 /*
260 * Somebody else still using it?
261 *
262 * If it's a directory, we can't drop it
263 * for fear of somebody re-populating it
264 * with children (even though dropping it
265 * would make it unreachable from the root,
266 * we might still populate it if it was a
267 * working directory or similar).
268 */
269 spin_lock(&dentry->d_lock);
270 if (atomic_read(&dentry->d_count) > 1) {
271 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
272 spin_unlock(&dentry->d_lock);
273 spin_unlock(&dcache_lock);
274 return -EBUSY;
275 }
276 }
277
278 __d_drop(dentry);
279 spin_unlock(&dentry->d_lock);
280 spin_unlock(&dcache_lock);
281 return 0;
282}
283
284/* This should be called _only_ with dcache_lock held */
285
286static inline struct dentry * __dget_locked(struct dentry *dentry)
287{
288 atomic_inc(&dentry->d_count);
289 if (!list_empty(&dentry->d_lru)) {
290 dentry_stat.nr_unused--;
291 list_del_init(&dentry->d_lru);
292 }
293 return dentry;
294}
295
296struct dentry * dget_locked(struct dentry *dentry)
297{
298 return __dget_locked(dentry);
299}
300
301/**
302 * d_find_alias - grab a hashed alias of inode
303 * @inode: inode in question
304 * @want_discon: flag, used by d_splice_alias, to request
305 * that only a DISCONNECTED alias be returned.
306 *
307 * If inode has a hashed alias, or is a directory and has any alias,
308 * acquire the reference to alias and return it. Otherwise return NULL.
309 * Notice that if inode is a directory there can be only one alias and
310 * it can be unhashed only if it has no children, or if it is the root
311 * of a filesystem.
312 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700313 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700314 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700315 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 */
317
318static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
319{
320 struct list_head *head, *next, *tmp;
321 struct dentry *alias, *discon_alias=NULL;
322
323 head = &inode->i_dentry;
324 next = inode->i_dentry.next;
325 while (next != head) {
326 tmp = next;
327 next = tmp->next;
328 prefetch(next);
329 alias = list_entry(tmp, struct dentry, d_alias);
330 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700331 if (IS_ROOT(alias) &&
332 (alias->d_flags & DCACHE_DISCONNECTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 discon_alias = alias;
334 else if (!want_discon) {
335 __dget_locked(alias);
336 return alias;
337 }
338 }
339 }
340 if (discon_alias)
341 __dget_locked(discon_alias);
342 return discon_alias;
343}
344
345struct dentry * d_find_alias(struct inode *inode)
346{
David Howells214fda12006-03-25 03:06:36 -0800347 struct dentry *de = NULL;
348
349 if (!list_empty(&inode->i_dentry)) {
350 spin_lock(&dcache_lock);
351 de = __d_find_alias(inode, 0);
352 spin_unlock(&dcache_lock);
353 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 return de;
355}
356
357/*
358 * Try to kill dentries associated with this inode.
359 * WARNING: you must own a reference to inode.
360 */
361void d_prune_aliases(struct inode *inode)
362{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700363 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364restart:
365 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700366 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700367 spin_lock(&dentry->d_lock);
368 if (!atomic_read(&dentry->d_count)) {
369 __dget_locked(dentry);
370 __d_drop(dentry);
371 spin_unlock(&dentry->d_lock);
372 spin_unlock(&dcache_lock);
373 dput(dentry);
374 goto restart;
375 }
376 spin_unlock(&dentry->d_lock);
377 }
378 spin_unlock(&dcache_lock);
379}
380
381/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700382 * Throw away a dentry - free the inode, dput the parent. This requires that
383 * the LRU list has already been removed.
384 *
Miklos Szeredid52b9082007-05-08 00:23:46 -0700385 * If prune_parents is true, try to prune ancestors as well.
386 *
Linus Torvalds1da177e2005-04-16 15:20:36 -0700387 * Called with dcache_lock, drops it and then regains.
Andrew Mortond702ccb2006-06-22 14:47:31 -0700388 * Called with dentry->d_lock held, drops it.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700389 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700390static void prune_one_dentry(struct dentry * dentry, int prune_parents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700392 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700393 dentry = d_kill(dentry);
394 if (!prune_parents) {
395 dput(dentry);
396 spin_lock(&dcache_lock);
397 return;
398 }
399
400 /*
401 * Prune ancestors. Locking is simpler than in dput(),
402 * because dcache_lock needs to be taken anyway.
403 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700404 spin_lock(&dcache_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700405 while (dentry) {
406 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
407 return;
408
409 if (dentry->d_op && dentry->d_op->d_delete)
410 dentry->d_op->d_delete(dentry);
411 if (!list_empty(&dentry->d_lru)) {
412 list_del(&dentry->d_lru);
413 dentry_stat.nr_unused--;
414 }
415 __d_drop(dentry);
416 dentry = d_kill(dentry);
417 spin_lock(&dcache_lock);
418 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419}
420
421/**
422 * prune_dcache - shrink the dcache
423 * @count: number of entries to try and free
NeilBrown0feae5c2006-06-22 14:47:28 -0700424 * @sb: if given, ignore dentries for other superblocks
425 * which are being unmounted.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700426 * @prune_parents: if true, try to prune ancestors as well in one go
Linus Torvalds1da177e2005-04-16 15:20:36 -0700427 *
428 * Shrink the dcache. This is done when we need
429 * more memory, or simply when we need to unmount
430 * something (at which point we need to unuse
431 * all dentries).
432 *
433 * This function may fail to free any resources if
434 * all the dentries are in use.
435 */
436
Miklos Szeredid52b9082007-05-08 00:23:46 -0700437static void prune_dcache(int count, struct super_block *sb, int prune_parents)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438{
439 spin_lock(&dcache_lock);
440 for (; count ; count--) {
441 struct dentry *dentry;
442 struct list_head *tmp;
NeilBrown0feae5c2006-06-22 14:47:28 -0700443 struct rw_semaphore *s_umount;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444
445 cond_resched_lock(&dcache_lock);
446
447 tmp = dentry_unused.prev;
Hua Zhongf58a1eb2006-06-25 05:49:32 -0700448 if (sb) {
NeilBrown0feae5c2006-06-22 14:47:28 -0700449 /* Try to find a dentry for this sb, but don't try
450 * too hard, if they aren't near the tail they will
451 * be moved down again soon
452 */
453 int skip = count;
454 while (skip && tmp != &dentry_unused &&
455 list_entry(tmp, struct dentry, d_lru)->d_sb != sb) {
456 skip--;
457 tmp = tmp->prev;
458 }
459 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460 if (tmp == &dentry_unused)
461 break;
462 list_del_init(tmp);
463 prefetch(dentry_unused.prev);
464 dentry_stat.nr_unused--;
465 dentry = list_entry(tmp, struct dentry, d_lru);
466
467 spin_lock(&dentry->d_lock);
468 /*
469 * We found an inuse dentry which was not removed from
470 * dentry_unused because of laziness during lookup. Do not free
471 * it - just keep it off the dentry_unused list.
472 */
473 if (atomic_read(&dentry->d_count)) {
474 spin_unlock(&dentry->d_lock);
475 continue;
476 }
477 /* If the dentry was recently referenced, don't free it. */
478 if (dentry->d_flags & DCACHE_REFERENCED) {
479 dentry->d_flags &= ~DCACHE_REFERENCED;
480 list_add(&dentry->d_lru, &dentry_unused);
481 dentry_stat.nr_unused++;
482 spin_unlock(&dentry->d_lock);
483 continue;
484 }
NeilBrown0feae5c2006-06-22 14:47:28 -0700485 /*
486 * If the dentry is not DCACHED_REFERENCED, it is time
487 * to remove it from the dcache, provided the super block is
488 * NULL (which means we are trying to reclaim memory)
489 * or this dentry belongs to the same super block that
490 * we want to shrink.
491 */
492 /*
493 * If this dentry is for "my" filesystem, then I can prune it
494 * without taking the s_umount lock (I already hold it).
495 */
496 if (sb && dentry->d_sb == sb) {
Miklos Szeredid52b9082007-05-08 00:23:46 -0700497 prune_one_dentry(dentry, prune_parents);
NeilBrown0feae5c2006-06-22 14:47:28 -0700498 continue;
499 }
500 /*
501 * ...otherwise we need to be sure this filesystem isn't being
502 * unmounted, otherwise we could race with
503 * generic_shutdown_super(), and end up holding a reference to
504 * an inode while the filesystem is unmounted.
505 * So we try to get s_umount, and make sure s_root isn't NULL.
506 * (Take a local copy of s_umount to avoid a use-after-free of
507 * `dentry').
508 */
509 s_umount = &dentry->d_sb->s_umount;
510 if (down_read_trylock(s_umount)) {
511 if (dentry->d_sb->s_root != NULL) {
Miklos Szeredid52b9082007-05-08 00:23:46 -0700512 prune_one_dentry(dentry, prune_parents);
NeilBrown0feae5c2006-06-22 14:47:28 -0700513 up_read(s_umount);
514 continue;
515 }
516 up_read(s_umount);
517 }
518 spin_unlock(&dentry->d_lock);
Vasily Averin6eac3f92006-10-28 10:38:44 -0700519 /*
520 * Insert dentry at the head of the list as inserting at the
521 * tail leads to a cycle.
NeilBrown0feae5c2006-06-22 14:47:28 -0700522 */
Vasily Averin6eac3f92006-10-28 10:38:44 -0700523 list_add(&dentry->d_lru, &dentry_unused);
524 dentry_stat.nr_unused++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 }
526 spin_unlock(&dcache_lock);
527}
528
529/*
530 * Shrink the dcache for the specified super block.
531 * This allows us to unmount a device without disturbing
532 * the dcache for the other devices.
533 *
534 * This implementation makes just two traversals of the
535 * unused list. On the first pass we move the selected
536 * dentries to the most recent end, and on the second
537 * pass we free them. The second pass must restart after
538 * each dput(), but since the target dentries are all at
539 * the end, it's really just a single traversal.
540 */
541
542/**
543 * shrink_dcache_sb - shrink dcache for a superblock
544 * @sb: superblock
545 *
546 * Shrink the dcache for the specified super block. This
547 * is used to free the dcache before unmounting a file
548 * system
549 */
550
551void shrink_dcache_sb(struct super_block * sb)
552{
553 struct list_head *tmp, *next;
554 struct dentry *dentry;
555
556 /*
557 * Pass one ... move the dentries for the specified
558 * superblock to the most recent end of the unused list.
559 */
560 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700561 list_for_each_safe(tmp, next, &dentry_unused) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700562 dentry = list_entry(tmp, struct dentry, d_lru);
563 if (dentry->d_sb != sb)
564 continue;
Akinobu Mita1bfba4e2006-06-26 00:24:40 -0700565 list_move(tmp, &dentry_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700566 }
567
568 /*
569 * Pass two ... free the dentries for this superblock.
570 */
571repeat:
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700572 list_for_each_safe(tmp, next, &dentry_unused) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573 dentry = list_entry(tmp, struct dentry, d_lru);
574 if (dentry->d_sb != sb)
575 continue;
576 dentry_stat.nr_unused--;
577 list_del_init(tmp);
578 spin_lock(&dentry->d_lock);
579 if (atomic_read(&dentry->d_count)) {
580 spin_unlock(&dentry->d_lock);
581 continue;
582 }
Miklos Szeredid52b9082007-05-08 00:23:46 -0700583 prune_one_dentry(dentry, 1);
Kirill Korotaev2ab13462006-03-25 03:07:45 -0800584 cond_resched_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585 goto repeat;
586 }
587 spin_unlock(&dcache_lock);
588}
589
590/*
David Howellsc636ebd2006-10-11 01:22:19 -0700591 * destroy a single subtree of dentries for unmount
592 * - see the comments on shrink_dcache_for_umount() for a description of the
593 * locking
594 */
595static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
596{
597 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700598 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700599
600 BUG_ON(!IS_ROOT(dentry));
601
602 /* detach this root from the system */
603 spin_lock(&dcache_lock);
604 if (!list_empty(&dentry->d_lru)) {
605 dentry_stat.nr_unused--;
606 list_del_init(&dentry->d_lru);
607 }
608 __d_drop(dentry);
609 spin_unlock(&dcache_lock);
610
611 for (;;) {
612 /* descend to the first leaf in the current subtree */
613 while (!list_empty(&dentry->d_subdirs)) {
614 struct dentry *loop;
615
616 /* this is a branch with children - detach all of them
617 * from the system in one go */
618 spin_lock(&dcache_lock);
619 list_for_each_entry(loop, &dentry->d_subdirs,
620 d_u.d_child) {
621 if (!list_empty(&loop->d_lru)) {
622 dentry_stat.nr_unused--;
623 list_del_init(&loop->d_lru);
624 }
625
626 __d_drop(loop);
627 cond_resched_lock(&dcache_lock);
628 }
629 spin_unlock(&dcache_lock);
630
631 /* move to the first child */
632 dentry = list_entry(dentry->d_subdirs.next,
633 struct dentry, d_u.d_child);
634 }
635
636 /* consume the dentries from this leaf up through its parents
637 * until we find one with children or run out altogether */
638 do {
639 struct inode *inode;
640
641 if (atomic_read(&dentry->d_count) != 0) {
642 printk(KERN_ERR
643 "BUG: Dentry %p{i=%lx,n=%s}"
644 " still in use (%d)"
645 " [unmount of %s %s]\n",
646 dentry,
647 dentry->d_inode ?
648 dentry->d_inode->i_ino : 0UL,
649 dentry->d_name.name,
650 atomic_read(&dentry->d_count),
651 dentry->d_sb->s_type->name,
652 dentry->d_sb->s_id);
653 BUG();
654 }
655
656 parent = dentry->d_parent;
657 if (parent == dentry)
658 parent = NULL;
659 else
660 atomic_dec(&parent->d_count);
661
662 list_del(&dentry->d_u.d_child);
David Howellsf8713572006-10-28 10:38:46 -0700663 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700664
665 inode = dentry->d_inode;
666 if (inode) {
667 dentry->d_inode = NULL;
668 list_del_init(&dentry->d_alias);
669 if (dentry->d_op && dentry->d_op->d_iput)
670 dentry->d_op->d_iput(dentry, inode);
671 else
672 iput(inode);
673 }
674
675 d_free(dentry);
676
677 /* finished when we fall off the top of the tree,
678 * otherwise we ascend to the parent and move to the
679 * next sibling if there is one */
680 if (!parent)
David Howellsf8713572006-10-28 10:38:46 -0700681 goto out;
David Howellsc636ebd2006-10-11 01:22:19 -0700682
683 dentry = parent;
684
685 } while (list_empty(&dentry->d_subdirs));
686
687 dentry = list_entry(dentry->d_subdirs.next,
688 struct dentry, d_u.d_child);
689 }
David Howellsf8713572006-10-28 10:38:46 -0700690out:
691 /* several dentries were freed, need to correct nr_dentry */
692 spin_lock(&dcache_lock);
693 dentry_stat.nr_dentry -= detached;
694 spin_unlock(&dcache_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700695}
696
697/*
698 * destroy the dentries attached to a superblock on unmounting
699 * - we don't need to use dentry->d_lock, and only need dcache_lock when
700 * removing the dentry from the system lists and hashes because:
701 * - the superblock is detached from all mountings and open files, so the
702 * dentry trees will not be rearranged by the VFS
703 * - s_umount is write-locked, so the memory pressure shrinker will ignore
704 * any dentries belonging to this superblock that it comes across
705 * - the filesystem itself is no longer permitted to rearrange the dentries
706 * in this superblock
707 */
708void shrink_dcache_for_umount(struct super_block *sb)
709{
710 struct dentry *dentry;
711
712 if (down_read_trylock(&sb->s_umount))
713 BUG();
714
715 dentry = sb->s_root;
716 sb->s_root = NULL;
717 atomic_dec(&dentry->d_count);
718 shrink_dcache_for_umount_subtree(dentry);
719
720 while (!hlist_empty(&sb->s_anon)) {
721 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
722 shrink_dcache_for_umount_subtree(dentry);
723 }
724}
725
726/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700727 * Search for at least 1 mount point in the dentry's subdirs.
728 * We descend to the next level whenever the d_subdirs
729 * list is non-empty and continue searching.
730 */
731
732/**
733 * have_submounts - check for mounts over a dentry
734 * @parent: dentry to check.
735 *
736 * Return true if the parent or its subdirectories contain
737 * a mount point
738 */
739
740int have_submounts(struct dentry *parent)
741{
742 struct dentry *this_parent = parent;
743 struct list_head *next;
744
745 spin_lock(&dcache_lock);
746 if (d_mountpoint(parent))
747 goto positive;
748repeat:
749 next = this_parent->d_subdirs.next;
750resume:
751 while (next != &this_parent->d_subdirs) {
752 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800753 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700754 next = tmp->next;
755 /* Have we found a mount point ? */
756 if (d_mountpoint(dentry))
757 goto positive;
758 if (!list_empty(&dentry->d_subdirs)) {
759 this_parent = dentry;
760 goto repeat;
761 }
762 }
763 /*
764 * All done at this level ... ascend and resume the search.
765 */
766 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800767 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700768 this_parent = this_parent->d_parent;
769 goto resume;
770 }
771 spin_unlock(&dcache_lock);
772 return 0; /* No mount points found in tree */
773positive:
774 spin_unlock(&dcache_lock);
775 return 1;
776}
777
778/*
779 * Search the dentry child list for the specified parent,
780 * and move any unused dentries to the end of the unused
781 * list for prune_dcache(). We descend to the next level
782 * whenever the d_subdirs list is non-empty and continue
783 * searching.
784 *
785 * It returns zero iff there are no unused children,
786 * otherwise it returns the number of children moved to
787 * the end of the unused list. This may not be the total
788 * number of unused children, because select_parent can
789 * drop the lock and return early due to latency
790 * constraints.
791 */
792static int select_parent(struct dentry * parent)
793{
794 struct dentry *this_parent = parent;
795 struct list_head *next;
796 int found = 0;
797
798 spin_lock(&dcache_lock);
799repeat:
800 next = this_parent->d_subdirs.next;
801resume:
802 while (next != &this_parent->d_subdirs) {
803 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800804 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805 next = tmp->next;
806
807 if (!list_empty(&dentry->d_lru)) {
808 dentry_stat.nr_unused--;
809 list_del_init(&dentry->d_lru);
810 }
811 /*
812 * move only zero ref count dentries to the end
813 * of the unused list for prune_dcache
814 */
815 if (!atomic_read(&dentry->d_count)) {
Akinobu Mita8e130592006-06-26 00:24:37 -0700816 list_add_tail(&dentry->d_lru, &dentry_unused);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700817 dentry_stat.nr_unused++;
818 found++;
819 }
820
821 /*
822 * We can return to the caller if we have found some (this
823 * ensures forward progress). We'll be coming back to find
824 * the rest.
825 */
826 if (found && need_resched())
827 goto out;
828
829 /*
830 * Descend a level if the d_subdirs list is non-empty.
831 */
832 if (!list_empty(&dentry->d_subdirs)) {
833 this_parent = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700834 goto repeat;
835 }
836 }
837 /*
838 * All done at this level ... ascend and resume the search.
839 */
840 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800841 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842 this_parent = this_parent->d_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700843 goto resume;
844 }
845out:
846 spin_unlock(&dcache_lock);
847 return found;
848}
849
850/**
851 * shrink_dcache_parent - prune dcache
852 * @parent: parent of entries to prune
853 *
854 * Prune the dcache to remove unused children of the parent dentry.
855 */
856
857void shrink_dcache_parent(struct dentry * parent)
858{
859 int found;
860
861 while ((found = select_parent(parent)) != 0)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700862 prune_dcache(found, parent->d_sb, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700863}
864
Linus Torvalds1da177e2005-04-16 15:20:36 -0700865/*
866 * Scan `nr' dentries and return the number which remain.
867 *
868 * We need to avoid reentering the filesystem if the caller is performing a
869 * GFP_NOFS allocation attempt. One example deadlock is:
870 *
871 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
872 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
873 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
874 *
875 * In this case we return -1 to tell the caller that we baled.
876 */
Al Viro27496a82005-10-21 03:20:48 -0400877static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700878{
879 if (nr) {
880 if (!(gfp_mask & __GFP_FS))
881 return -1;
Andrew Morton24c32d72007-05-08 00:23:49 -0700882 prune_dcache(nr, NULL, 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700883 }
884 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
885}
886
887/**
888 * d_alloc - allocate a dcache entry
889 * @parent: parent of entry to allocate
890 * @name: qstr of the name
891 *
892 * Allocates a dentry. It returns %NULL if there is insufficient memory
893 * available. On a success the dentry is returned. The name passed in is
894 * copied and the copy passed in may be reused after this call.
895 */
896
897struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
898{
899 struct dentry *dentry;
900 char *dname;
901
902 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
903 if (!dentry)
904 return NULL;
905
906 if (name->len > DNAME_INLINE_LEN-1) {
907 dname = kmalloc(name->len + 1, GFP_KERNEL);
908 if (!dname) {
909 kmem_cache_free(dentry_cache, dentry);
910 return NULL;
911 }
912 } else {
913 dname = dentry->d_iname;
914 }
915 dentry->d_name.name = dname;
916
917 dentry->d_name.len = name->len;
918 dentry->d_name.hash = name->hash;
919 memcpy(dname, name->name, name->len);
920 dname[name->len] = 0;
921
922 atomic_set(&dentry->d_count, 1);
923 dentry->d_flags = DCACHE_UNHASHED;
924 spin_lock_init(&dentry->d_lock);
925 dentry->d_inode = NULL;
926 dentry->d_parent = NULL;
927 dentry->d_sb = NULL;
928 dentry->d_op = NULL;
929 dentry->d_fsdata = NULL;
930 dentry->d_mounted = 0;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800931#ifdef CONFIG_PROFILING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700932 dentry->d_cookie = NULL;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800933#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700934 INIT_HLIST_NODE(&dentry->d_hash);
935 INIT_LIST_HEAD(&dentry->d_lru);
936 INIT_LIST_HEAD(&dentry->d_subdirs);
937 INIT_LIST_HEAD(&dentry->d_alias);
938
939 if (parent) {
940 dentry->d_parent = dget(parent);
941 dentry->d_sb = parent->d_sb;
942 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800943 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700944 }
945
946 spin_lock(&dcache_lock);
947 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -0800948 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700949 dentry_stat.nr_dentry++;
950 spin_unlock(&dcache_lock);
951
952 return dentry;
953}
954
955struct dentry *d_alloc_name(struct dentry *parent, const char *name)
956{
957 struct qstr q;
958
959 q.name = name;
960 q.len = strlen(name);
961 q.hash = full_name_hash(q.name, q.len);
962 return d_alloc(parent, &q);
963}
964
965/**
966 * d_instantiate - fill in inode information for a dentry
967 * @entry: dentry to complete
968 * @inode: inode to attach to this dentry
969 *
970 * Fill in inode information in the entry.
971 *
972 * This turns negative dentries into productive full members
973 * of society.
974 *
975 * NOTE! This assumes that the inode count has been incremented
976 * (or otherwise set) by the caller to indicate that it is now
977 * in use by the dcache.
978 */
979
980void d_instantiate(struct dentry *entry, struct inode * inode)
981{
Eric Sesterhenn28133c72006-03-26 18:25:39 +0200982 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700983 spin_lock(&dcache_lock);
984 if (inode)
985 list_add(&entry->d_alias, &inode->i_dentry);
986 entry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -0800987 fsnotify_d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700988 spin_unlock(&dcache_lock);
989 security_d_instantiate(entry, inode);
990}
991
992/**
993 * d_instantiate_unique - instantiate a non-aliased dentry
994 * @entry: dentry to instantiate
995 * @inode: inode to attach to this dentry
996 *
997 * Fill in inode information in the entry. On success, it returns NULL.
998 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -0800999 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001000 *
1001 * Note that in order to avoid conflicts with rename() etc, the caller
1002 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001003 *
1004 * This also assumes that the inode count has been incremented
1005 * (or otherwise set) by the caller to indicate that it is now
1006 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001007 */
David Howells770bfad2006-08-22 20:06:07 -04001008static struct dentry *__d_instantiate_unique(struct dentry *entry,
1009 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010{
1011 struct dentry *alias;
1012 int len = entry->d_name.len;
1013 const char *name = entry->d_name.name;
1014 unsigned int hash = entry->d_name.hash;
1015
David Howells770bfad2006-08-22 20:06:07 -04001016 if (!inode) {
1017 entry->d_inode = NULL;
1018 return NULL;
1019 }
1020
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1022 struct qstr *qstr = &alias->d_name;
1023
1024 if (qstr->hash != hash)
1025 continue;
1026 if (alias->d_parent != entry->d_parent)
1027 continue;
1028 if (qstr->len != len)
1029 continue;
1030 if (memcmp(qstr->name, name, len))
1031 continue;
1032 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001033 return alias;
1034 }
David Howells770bfad2006-08-22 20:06:07 -04001035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 list_add(&entry->d_alias, &inode->i_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 entry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -08001038 fsnotify_d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 return NULL;
1040}
David Howells770bfad2006-08-22 20:06:07 -04001041
1042struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1043{
1044 struct dentry *result;
1045
1046 BUG_ON(!list_empty(&entry->d_alias));
1047
1048 spin_lock(&dcache_lock);
1049 result = __d_instantiate_unique(entry, inode);
1050 spin_unlock(&dcache_lock);
1051
1052 if (!result) {
1053 security_d_instantiate(entry, inode);
1054 return NULL;
1055 }
1056
1057 BUG_ON(!d_unhashed(result));
1058 iput(inode);
1059 return result;
1060}
1061
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062EXPORT_SYMBOL(d_instantiate_unique);
1063
1064/**
1065 * d_alloc_root - allocate root dentry
1066 * @root_inode: inode to allocate the root for
1067 *
1068 * Allocate a root ("/") dentry for the inode given. The inode is
1069 * instantiated and returned. %NULL is returned if there is insufficient
1070 * memory or the inode passed is %NULL.
1071 */
1072
1073struct dentry * d_alloc_root(struct inode * root_inode)
1074{
1075 struct dentry *res = NULL;
1076
1077 if (root_inode) {
1078 static const struct qstr name = { .name = "/", .len = 1 };
1079
1080 res = d_alloc(NULL, &name);
1081 if (res) {
1082 res->d_sb = root_inode->i_sb;
1083 res->d_parent = res;
1084 d_instantiate(res, root_inode);
1085 }
1086 }
1087 return res;
1088}
1089
1090static inline struct hlist_head *d_hash(struct dentry *parent,
1091 unsigned long hash)
1092{
1093 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1094 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1095 return dentry_hashtable + (hash & D_HASHMASK);
1096}
1097
1098/**
1099 * d_alloc_anon - allocate an anonymous dentry
1100 * @inode: inode to allocate the dentry for
1101 *
1102 * This is similar to d_alloc_root. It is used by filesystems when
1103 * creating a dentry for a given inode, often in the process of
1104 * mapping a filehandle to a dentry. The returned dentry may be
1105 * anonymous, or may have a full name (if the inode was already
1106 * in the cache). The file system may need to make further
1107 * efforts to connect this dentry into the dcache properly.
1108 *
1109 * When called on a directory inode, we must ensure that
1110 * the inode only ever has one dentry. If a dentry is
1111 * found, that is returned instead of allocating a new one.
1112 *
1113 * On successful return, the reference to the inode has been transferred
1114 * to the dentry. If %NULL is returned (indicating kmalloc failure),
1115 * the reference on the inode has not been released.
1116 */
1117
1118struct dentry * d_alloc_anon(struct inode *inode)
1119{
1120 static const struct qstr anonstring = { .name = "" };
1121 struct dentry *tmp;
1122 struct dentry *res;
1123
1124 if ((res = d_find_alias(inode))) {
1125 iput(inode);
1126 return res;
1127 }
1128
1129 tmp = d_alloc(NULL, &anonstring);
1130 if (!tmp)
1131 return NULL;
1132
1133 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1134
1135 spin_lock(&dcache_lock);
1136 res = __d_find_alias(inode, 0);
1137 if (!res) {
1138 /* attach a disconnected dentry */
1139 res = tmp;
1140 tmp = NULL;
1141 spin_lock(&res->d_lock);
1142 res->d_sb = inode->i_sb;
1143 res->d_parent = res;
1144 res->d_inode = inode;
1145 res->d_flags |= DCACHE_DISCONNECTED;
1146 res->d_flags &= ~DCACHE_UNHASHED;
1147 list_add(&res->d_alias, &inode->i_dentry);
1148 hlist_add_head(&res->d_hash, &inode->i_sb->s_anon);
1149 spin_unlock(&res->d_lock);
1150
1151 inode = NULL; /* don't drop reference */
1152 }
1153 spin_unlock(&dcache_lock);
1154
1155 if (inode)
1156 iput(inode);
1157 if (tmp)
1158 dput(tmp);
1159 return res;
1160}
1161
1162
1163/**
1164 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1165 * @inode: the inode which may have a disconnected dentry
1166 * @dentry: a negative dentry which we want to point to the inode.
1167 *
1168 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1169 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1170 * and return it, else simply d_add the inode to the dentry and return NULL.
1171 *
1172 * This is needed in the lookup routine of any filesystem that is exportable
1173 * (via knfsd) so that we can build dcache paths to directories effectively.
1174 *
1175 * If a dentry was found and moved, then it is returned. Otherwise NULL
1176 * is returned. This matches the expected return value of ->lookup.
1177 *
1178 */
1179struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1180{
1181 struct dentry *new = NULL;
1182
NeilBrown21c0d8f2006-10-04 02:16:16 -07001183 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001184 spin_lock(&dcache_lock);
1185 new = __d_find_alias(inode, 1);
1186 if (new) {
1187 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Pigginc32ccd82006-03-25 03:07:09 -08001188 fsnotify_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189 spin_unlock(&dcache_lock);
1190 security_d_instantiate(new, inode);
1191 d_rehash(dentry);
1192 d_move(new, dentry);
1193 iput(inode);
1194 } else {
1195 /* d_instantiate takes dcache_lock, so we do it by hand */
1196 list_add(&dentry->d_alias, &inode->i_dentry);
1197 dentry->d_inode = inode;
Nick Pigginc32ccd82006-03-25 03:07:09 -08001198 fsnotify_d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001199 spin_unlock(&dcache_lock);
1200 security_d_instantiate(dentry, inode);
1201 d_rehash(dentry);
1202 }
1203 } else
1204 d_add(dentry, inode);
1205 return new;
1206}
1207
1208
1209/**
1210 * d_lookup - search for a dentry
1211 * @parent: parent dentry
1212 * @name: qstr of name we wish to find
1213 *
1214 * Searches the children of the parent dentry for the name in question. If
1215 * the dentry is found its reference count is incremented and the dentry
1216 * is returned. The caller must use d_put to free the entry when it has
1217 * finished using it. %NULL is returned on failure.
1218 *
1219 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1220 * Memory barriers are used while updating and doing lockless traversal.
1221 * To avoid races with d_move while rename is happening, d_lock is used.
1222 *
1223 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1224 * and name pointer in one structure pointed by d_qstr.
1225 *
1226 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1227 * lookup is going on.
1228 *
1229 * dentry_unused list is not updated even if lookup finds the required dentry
1230 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1231 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1232 * acquisition.
1233 *
1234 * d_lookup() is protected against the concurrent renames in some unrelated
1235 * directory using the seqlockt_t rename_lock.
1236 */
1237
1238struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1239{
1240 struct dentry * dentry = NULL;
1241 unsigned long seq;
1242
1243 do {
1244 seq = read_seqbegin(&rename_lock);
1245 dentry = __d_lookup(parent, name);
1246 if (dentry)
1247 break;
1248 } while (read_seqretry(&rename_lock, seq));
1249 return dentry;
1250}
1251
1252struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1253{
1254 unsigned int len = name->len;
1255 unsigned int hash = name->hash;
1256 const unsigned char *str = name->name;
1257 struct hlist_head *head = d_hash(parent,hash);
1258 struct dentry *found = NULL;
1259 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001260 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001261
1262 rcu_read_lock();
1263
Paul E. McKenney665a7582005-11-07 00:59:17 -08001264 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 struct qstr *qstr;
1266
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 if (dentry->d_name.hash != hash)
1268 continue;
1269 if (dentry->d_parent != parent)
1270 continue;
1271
1272 spin_lock(&dentry->d_lock);
1273
1274 /*
1275 * Recheck the dentry after taking the lock - d_move may have
1276 * changed things. Don't bother checking the hash because we're
1277 * about to compare the whole name anyway.
1278 */
1279 if (dentry->d_parent != parent)
1280 goto next;
1281
1282 /*
1283 * It is safe to compare names since d_move() cannot
1284 * change the qstr (protected by d_lock).
1285 */
1286 qstr = &dentry->d_name;
1287 if (parent->d_op && parent->d_op->d_compare) {
1288 if (parent->d_op->d_compare(parent, qstr, name))
1289 goto next;
1290 } else {
1291 if (qstr->len != len)
1292 goto next;
1293 if (memcmp(qstr->name, str, len))
1294 goto next;
1295 }
1296
1297 if (!d_unhashed(dentry)) {
1298 atomic_inc(&dentry->d_count);
1299 found = dentry;
1300 }
1301 spin_unlock(&dentry->d_lock);
1302 break;
1303next:
1304 spin_unlock(&dentry->d_lock);
1305 }
1306 rcu_read_unlock();
1307
1308 return found;
1309}
1310
1311/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001312 * d_hash_and_lookup - hash the qstr then search for a dentry
1313 * @dir: Directory to search in
1314 * @name: qstr of name we wish to find
1315 *
1316 * On hash failure or on lookup failure NULL is returned.
1317 */
1318struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1319{
1320 struct dentry *dentry = NULL;
1321
1322 /*
1323 * Check for a fs-specific hash function. Note that we must
1324 * calculate the standard hash first, as the d_op->d_hash()
1325 * routine may choose to leave the hash value unchanged.
1326 */
1327 name->hash = full_name_hash(name->name, name->len);
1328 if (dir->d_op && dir->d_op->d_hash) {
1329 if (dir->d_op->d_hash(dir, name) < 0)
1330 goto out;
1331 }
1332 dentry = d_lookup(dir, name);
1333out:
1334 return dentry;
1335}
1336
1337/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 * d_validate - verify dentry provided from insecure source
1339 * @dentry: The dentry alleged to be valid child of @dparent
1340 * @dparent: The parent dentry (known to be valid)
1341 * @hash: Hash of the dentry
1342 * @len: Length of the name
1343 *
1344 * An insecure source has sent us a dentry, here we verify it and dget() it.
1345 * This is used by ncpfs in its readdir implementation.
1346 * Zero is returned in the dentry is invalid.
1347 */
1348
1349int d_validate(struct dentry *dentry, struct dentry *dparent)
1350{
1351 struct hlist_head *base;
1352 struct hlist_node *lhp;
1353
1354 /* Check whether the ptr might be valid at all.. */
1355 if (!kmem_ptr_validate(dentry_cache, dentry))
1356 goto out;
1357
1358 if (dentry->d_parent != dparent)
1359 goto out;
1360
1361 spin_lock(&dcache_lock);
1362 base = d_hash(dparent, dentry->d_name.hash);
1363 hlist_for_each(lhp,base) {
Paul E. McKenney665a7582005-11-07 00:59:17 -08001364 /* hlist_for_each_entry_rcu() not required for d_hash list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365 * as it is parsed under dcache_lock
1366 */
1367 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1368 __dget_locked(dentry);
1369 spin_unlock(&dcache_lock);
1370 return 1;
1371 }
1372 }
1373 spin_unlock(&dcache_lock);
1374out:
1375 return 0;
1376}
1377
1378/*
1379 * When a file is deleted, we have two options:
1380 * - turn this dentry into a negative dentry
1381 * - unhash this dentry and free it.
1382 *
1383 * Usually, we want to just turn this into
1384 * a negative dentry, but if anybody else is
1385 * currently using the dentry or the inode
1386 * we can't do that and we fall back on removing
1387 * it from the hash queues and waiting for
1388 * it to be deleted later when it has no users
1389 */
1390
1391/**
1392 * d_delete - delete a dentry
1393 * @dentry: The dentry to delete
1394 *
1395 * Turn the dentry into a negative dentry if possible, otherwise
1396 * remove it from the hash queues so it can be deleted later
1397 */
1398
1399void d_delete(struct dentry * dentry)
1400{
John McCutchan7a91bf72005-08-08 13:52:16 -04001401 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402 /*
1403 * Are we the only user?
1404 */
1405 spin_lock(&dcache_lock);
1406 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001407 isdir = S_ISDIR(dentry->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001408 if (atomic_read(&dentry->d_count) == 1) {
1409 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001410 fsnotify_nameremove(dentry, isdir);
Amy Griffis7a2bd3f2006-03-31 02:30:54 -08001411
1412 /* remove this and other inotify debug checks after 2.6.18 */
1413 dentry->d_flags &= ~DCACHE_INOTIFY_PARENT_WATCHED;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001414 return;
1415 }
1416
1417 if (!d_unhashed(dentry))
1418 __d_drop(dentry);
1419
1420 spin_unlock(&dentry->d_lock);
1421 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001422
1423 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424}
1425
1426static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1427{
1428
1429 entry->d_flags &= ~DCACHE_UNHASHED;
1430 hlist_add_head_rcu(&entry->d_hash, list);
1431}
1432
David Howells770bfad2006-08-22 20:06:07 -04001433static void _d_rehash(struct dentry * entry)
1434{
1435 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1436}
1437
Linus Torvalds1da177e2005-04-16 15:20:36 -07001438/**
1439 * d_rehash - add an entry back to the hash
1440 * @entry: dentry to add to the hash
1441 *
1442 * Adds a dentry to the hash according to its name.
1443 */
1444
1445void d_rehash(struct dentry * entry)
1446{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001447 spin_lock(&dcache_lock);
1448 spin_lock(&entry->d_lock);
David Howells770bfad2006-08-22 20:06:07 -04001449 _d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001450 spin_unlock(&entry->d_lock);
1451 spin_unlock(&dcache_lock);
1452}
1453
1454#define do_switch(x,y) do { \
1455 __typeof__ (x) __tmp = x; \
1456 x = y; y = __tmp; } while (0)
1457
1458/*
1459 * When switching names, the actual string doesn't strictly have to
1460 * be preserved in the target - because we're dropping the target
1461 * anyway. As such, we can just do a simple memcpy() to copy over
1462 * the new name before we switch.
1463 *
1464 * Note that we have to be a lot more careful about getting the hash
1465 * switched - we have to switch the hash value properly even if it
1466 * then no longer matches the actual (corrupted) string of the target.
1467 * The hash value has to match the hash queue that the dentry is on..
1468 */
1469static void switch_names(struct dentry *dentry, struct dentry *target)
1470{
1471 if (dname_external(target)) {
1472 if (dname_external(dentry)) {
1473 /*
1474 * Both external: swap the pointers
1475 */
1476 do_switch(target->d_name.name, dentry->d_name.name);
1477 } else {
1478 /*
1479 * dentry:internal, target:external. Steal target's
1480 * storage and make target internal.
1481 */
1482 dentry->d_name.name = target->d_name.name;
1483 target->d_name.name = target->d_iname;
1484 }
1485 } else {
1486 if (dname_external(dentry)) {
1487 /*
1488 * dentry:external, target:internal. Give dentry's
1489 * storage to target and make dentry internal
1490 */
1491 memcpy(dentry->d_iname, target->d_name.name,
1492 target->d_name.len + 1);
1493 target->d_name.name = dentry->d_name.name;
1494 dentry->d_name.name = dentry->d_iname;
1495 } else {
1496 /*
1497 * Both are internal. Just copy target to dentry
1498 */
1499 memcpy(dentry->d_iname, target->d_name.name,
1500 target->d_name.len + 1);
1501 }
1502 }
1503}
1504
1505/*
1506 * We cannibalize "target" when moving dentry on top of it,
1507 * because it's going to be thrown away anyway. We could be more
1508 * polite about it, though.
1509 *
1510 * This forceful removal will result in ugly /proc output if
1511 * somebody holds a file open that got deleted due to a rename.
1512 * We could be nicer about the deleted file, and let it show
1513 * up under the name it got deleted rather than the name that
1514 * deleted it.
1515 */
1516
Trond Myklebust9eaef272006-10-21 10:24:20 -07001517/*
1518 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001519 * @dentry: entry to move
1520 * @target: new dentry
1521 *
1522 * Update the dcache to reflect the move of a file name. Negative
1523 * dcache entries should not be moved in this way.
1524 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001525static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526{
1527 struct hlist_head *list;
1528
1529 if (!dentry->d_inode)
1530 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1531
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 write_seqlock(&rename_lock);
1533 /*
1534 * XXXX: do we really need to take target->d_lock?
1535 */
1536 if (target < dentry) {
1537 spin_lock(&target->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001538 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539 } else {
1540 spin_lock(&dentry->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001541 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001542 }
1543
1544 /* Move the dentry to the target hash queue, if on different bucket */
1545 if (dentry->d_flags & DCACHE_UNHASHED)
1546 goto already_unhashed;
1547
1548 hlist_del_rcu(&dentry->d_hash);
1549
1550already_unhashed:
1551 list = d_hash(target->d_parent, target->d_name.hash);
1552 __d_rehash(dentry, list);
1553
1554 /* Unhash the target: dput() will then get rid of it */
1555 __d_drop(target);
1556
Eric Dumazet5160ee62006-01-08 01:03:32 -08001557 list_del(&dentry->d_u.d_child);
1558 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001559
1560 /* Switch the names.. */
1561 switch_names(dentry, target);
1562 do_switch(dentry->d_name.len, target->d_name.len);
1563 do_switch(dentry->d_name.hash, target->d_name.hash);
1564
1565 /* ... and switch the parents */
1566 if (IS_ROOT(dentry)) {
1567 dentry->d_parent = target->d_parent;
1568 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001569 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001570 } else {
1571 do_switch(dentry->d_parent, target->d_parent);
1572
1573 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001574 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001575 }
1576
Eric Dumazet5160ee62006-01-08 01:03:32 -08001577 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001579 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001580 spin_unlock(&dentry->d_lock);
1581 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001582}
1583
1584/**
1585 * d_move - move a dentry
1586 * @dentry: entry to move
1587 * @target: new dentry
1588 *
1589 * Update the dcache to reflect the move of a file name. Negative
1590 * dcache entries should not be moved in this way.
1591 */
1592
1593void d_move(struct dentry * dentry, struct dentry * target)
1594{
1595 spin_lock(&dcache_lock);
1596 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 spin_unlock(&dcache_lock);
1598}
1599
David Howells770bfad2006-08-22 20:06:07 -04001600/*
Trond Myklebust9eaef272006-10-21 10:24:20 -07001601 * Helper that returns 1 if p1 is a parent of p2, else 0
1602 */
1603static int d_isparent(struct dentry *p1, struct dentry *p2)
1604{
1605 struct dentry *p;
1606
1607 for (p = p2; p->d_parent != p; p = p->d_parent) {
1608 if (p->d_parent == p1)
1609 return 1;
1610 }
1611 return 0;
1612}
1613
1614/*
1615 * This helper attempts to cope with remotely renamed directories
1616 *
1617 * It assumes that the caller is already holding
1618 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1619 *
1620 * Note: If ever the locking in lock_rename() changes, then please
1621 * remember to update this too...
1622 *
1623 * On return, dcache_lock will have been unlocked.
1624 */
1625static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
1626{
1627 struct mutex *m1 = NULL, *m2 = NULL;
1628 struct dentry *ret;
1629
1630 /* If alias and dentry share a parent, then no extra locks required */
1631 if (alias->d_parent == dentry->d_parent)
1632 goto out_unalias;
1633
1634 /* Check for loops */
1635 ret = ERR_PTR(-ELOOP);
1636 if (d_isparent(alias, dentry))
1637 goto out_err;
1638
1639 /* See lock_rename() */
1640 ret = ERR_PTR(-EBUSY);
1641 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1642 goto out_err;
1643 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1644 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1645 goto out_err;
1646 m2 = &alias->d_parent->d_inode->i_mutex;
1647out_unalias:
1648 d_move_locked(alias, dentry);
1649 ret = alias;
1650out_err:
1651 spin_unlock(&dcache_lock);
1652 if (m2)
1653 mutex_unlock(m2);
1654 if (m1)
1655 mutex_unlock(m1);
1656 return ret;
1657}
1658
1659/*
David Howells770bfad2006-08-22 20:06:07 -04001660 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1661 * named dentry in place of the dentry to be replaced.
1662 */
1663static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1664{
1665 struct dentry *dparent, *aparent;
1666
1667 switch_names(dentry, anon);
1668 do_switch(dentry->d_name.len, anon->d_name.len);
1669 do_switch(dentry->d_name.hash, anon->d_name.hash);
1670
1671 dparent = dentry->d_parent;
1672 aparent = anon->d_parent;
1673
1674 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1675 list_del(&dentry->d_u.d_child);
1676 if (!IS_ROOT(dentry))
1677 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1678 else
1679 INIT_LIST_HEAD(&dentry->d_u.d_child);
1680
1681 anon->d_parent = (dparent == dentry) ? anon : dparent;
1682 list_del(&anon->d_u.d_child);
1683 if (!IS_ROOT(anon))
1684 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1685 else
1686 INIT_LIST_HEAD(&anon->d_u.d_child);
1687
1688 anon->d_flags &= ~DCACHE_DISCONNECTED;
1689}
1690
1691/**
1692 * d_materialise_unique - introduce an inode into the tree
1693 * @dentry: candidate dentry
1694 * @inode: inode to bind to the dentry, to which aliases may be attached
1695 *
1696 * Introduces an dentry into the tree, substituting an extant disconnected
1697 * root directory alias in its place if there is one
1698 */
1699struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1700{
Trond Myklebust9eaef272006-10-21 10:24:20 -07001701 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04001702
1703 BUG_ON(!d_unhashed(dentry));
1704
1705 spin_lock(&dcache_lock);
1706
1707 if (!inode) {
1708 actual = dentry;
1709 dentry->d_inode = NULL;
1710 goto found_lock;
1711 }
1712
Trond Myklebust9eaef272006-10-21 10:24:20 -07001713 if (S_ISDIR(inode->i_mode)) {
1714 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04001715
Trond Myklebust9eaef272006-10-21 10:24:20 -07001716 /* Does an aliased dentry already exist? */
1717 alias = __d_find_alias(inode, 0);
1718 if (alias) {
1719 actual = alias;
1720 /* Is this an anonymous mountpoint that we could splice
1721 * into our tree? */
1722 if (IS_ROOT(alias)) {
1723 spin_lock(&alias->d_lock);
1724 __d_materialise_dentry(dentry, alias);
1725 __d_drop(alias);
1726 goto found;
1727 }
1728 /* Nope, but we must(!) avoid directory aliasing */
1729 actual = __d_unalias(dentry, alias);
1730 if (IS_ERR(actual))
1731 dput(alias);
1732 goto out_nolock;
1733 }
David Howells770bfad2006-08-22 20:06:07 -04001734 }
1735
1736 /* Add a unique reference */
1737 actual = __d_instantiate_unique(dentry, inode);
1738 if (!actual)
1739 actual = dentry;
1740 else if (unlikely(!d_unhashed(actual)))
1741 goto shouldnt_be_hashed;
1742
1743found_lock:
1744 spin_lock(&actual->d_lock);
1745found:
1746 _d_rehash(actual);
1747 spin_unlock(&actual->d_lock);
1748 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001749out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04001750 if (actual == dentry) {
1751 security_d_instantiate(dentry, inode);
1752 return NULL;
1753 }
1754
1755 iput(inode);
1756 return actual;
1757
David Howells770bfad2006-08-22 20:06:07 -04001758shouldnt_be_hashed:
1759 spin_unlock(&dcache_lock);
1760 BUG();
1761 goto shouldnt_be_hashed;
1762}
1763
Linus Torvalds1da177e2005-04-16 15:20:36 -07001764/**
1765 * d_path - return the path of a dentry
1766 * @dentry: dentry to report
1767 * @vfsmnt: vfsmnt to which the dentry belongs
1768 * @root: root dentry
1769 * @rootmnt: vfsmnt to which the root dentry belongs
1770 * @buffer: buffer to return value in
1771 * @buflen: buffer length
1772 *
Linus Torvalds552ce542007-02-13 12:08:18 -08001773 * Convert a dentry into an ASCII path name. If the entry has been deleted
1774 * the string " (deleted)" is appended. Note that this is ambiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001775 *
Linus Torvalds552ce542007-02-13 12:08:18 -08001776 * Returns the buffer or an error code if the path was too long.
1777 *
1778 * "buflen" should be positive. Caller holds the dcache_lock.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001779 */
Linus Torvalds552ce542007-02-13 12:08:18 -08001780static char * __d_path( struct dentry *dentry, struct vfsmount *vfsmnt,
1781 struct dentry *root, struct vfsmount *rootmnt,
1782 char *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783{
Linus Torvalds552ce542007-02-13 12:08:18 -08001784 char * end = buffer+buflen;
1785 char * retval;
1786 int namelen;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001787
Linus Torvalds552ce542007-02-13 12:08:18 -08001788 *--end = '\0';
1789 buflen--;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 if (!IS_ROOT(dentry) && d_unhashed(dentry)) {
Andreas Gruenbachereb3dfb02007-02-12 00:51:47 -08001791 buflen -= 10;
Linus Torvalds552ce542007-02-13 12:08:18 -08001792 end -= 10;
1793 if (buflen < 0)
1794 goto Elong;
1795 memcpy(end, " (deleted)", 10);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 }
Linus Torvalds552ce542007-02-13 12:08:18 -08001797
1798 if (buflen < 1)
1799 goto Elong;
1800 /* Get '/' right */
1801 retval = end-1;
1802 *retval = '/';
1803
1804 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 struct dentry * parent;
1806
Linus Torvalds552ce542007-02-13 12:08:18 -08001807 if (dentry == root && vfsmnt == rootmnt)
1808 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001809 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08001810 /* Global root? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 spin_lock(&vfsmount_lock);
1812 if (vfsmnt->mnt_parent == vfsmnt) {
1813 spin_unlock(&vfsmount_lock);
1814 goto global_root;
1815 }
1816 dentry = vfsmnt->mnt_mountpoint;
1817 vfsmnt = vfsmnt->mnt_parent;
1818 spin_unlock(&vfsmount_lock);
1819 continue;
1820 }
1821 parent = dentry->d_parent;
1822 prefetch(parent);
1823 namelen = dentry->d_name.len;
Andreas Gruenbachereb3dfb02007-02-12 00:51:47 -08001824 buflen -= namelen + 1;
Linus Torvalds552ce542007-02-13 12:08:18 -08001825 if (buflen < 0)
1826 goto Elong;
1827 end -= namelen;
1828 memcpy(end, dentry->d_name.name, namelen);
1829 *--end = '/';
1830 retval = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 dentry = parent;
1832 }
1833
Linus Torvalds552ce542007-02-13 12:08:18 -08001834 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835
1836global_root:
1837 namelen = dentry->d_name.len;
Linus Torvalds552ce542007-02-13 12:08:18 -08001838 buflen -= namelen;
1839 if (buflen < 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 goto Elong;
Linus Torvalds552ce542007-02-13 12:08:18 -08001841 retval -= namelen-1; /* hit the slash */
1842 memcpy(retval, dentry->d_name.name, namelen);
1843 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844Elong:
Linus Torvalds552ce542007-02-13 12:08:18 -08001845 return ERR_PTR(-ENAMETOOLONG);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001846}
1847
1848/* write full pathname into buffer and return start of pathname */
Linus Torvalds552ce542007-02-13 12:08:18 -08001849char * d_path(struct dentry *dentry, struct vfsmount *vfsmnt,
1850 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001851{
1852 char *res;
1853 struct vfsmount *rootmnt;
1854 struct dentry *root;
1855
Eric Dumazetc23fbb62007-05-08 00:26:18 -07001856 /*
1857 * We have various synthetic filesystems that never get mounted. On
1858 * these filesystems dentries are never used for lookup purposes, and
1859 * thus don't need to be hashed. They also don't need a name until a
1860 * user wants to identify the object in /proc/pid/fd/. The little hack
1861 * below allows us to generate a name for these objects on demand:
1862 */
1863 if (dentry->d_op && dentry->d_op->d_dname)
1864 return dentry->d_op->d_dname(dentry, buf, buflen);
1865
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866 read_lock(&current->fs->lock);
1867 rootmnt = mntget(current->fs->rootmnt);
1868 root = dget(current->fs->root);
1869 read_unlock(&current->fs->lock);
Linus Torvalds552ce542007-02-13 12:08:18 -08001870 spin_lock(&dcache_lock);
1871 res = __d_path(dentry, vfsmnt, root, rootmnt, buf, buflen);
1872 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001873 dput(root);
1874 mntput(rootmnt);
1875 return res;
1876}
1877
1878/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07001879 * Helper function for dentry_operations.d_dname() members
1880 */
1881char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
1882 const char *fmt, ...)
1883{
1884 va_list args;
1885 char temp[64];
1886 int sz;
1887
1888 va_start(args, fmt);
1889 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
1890 va_end(args);
1891
1892 if (sz > sizeof(temp) || sz > buflen)
1893 return ERR_PTR(-ENAMETOOLONG);
1894
1895 buffer += buflen - sz;
1896 return memcpy(buffer, temp, sz);
1897}
1898
1899/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 * NOTE! The user-level library version returns a
1901 * character pointer. The kernel system call just
1902 * returns the length of the buffer filled (which
1903 * includes the ending '\0' character), or a negative
1904 * error value. So libc would do something like
1905 *
1906 * char *getcwd(char * buf, size_t size)
1907 * {
1908 * int retval;
1909 *
1910 * retval = sys_getcwd(buf, size);
1911 * if (retval >= 0)
1912 * return buf;
1913 * errno = -retval;
1914 * return NULL;
1915 * }
1916 */
1917asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
1918{
Linus Torvalds552ce542007-02-13 12:08:18 -08001919 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001920 struct vfsmount *pwdmnt, *rootmnt;
1921 struct dentry *pwd, *root;
Linus Torvalds552ce542007-02-13 12:08:18 -08001922 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001923
1924 if (!page)
1925 return -ENOMEM;
1926
1927 read_lock(&current->fs->lock);
1928 pwdmnt = mntget(current->fs->pwdmnt);
1929 pwd = dget(current->fs->pwd);
1930 rootmnt = mntget(current->fs->rootmnt);
1931 root = dget(current->fs->root);
1932 read_unlock(&current->fs->lock);
1933
Linus Torvalds552ce542007-02-13 12:08:18 -08001934 error = -ENOENT;
1935 /* Has the current directory has been unlinked? */
1936 spin_lock(&dcache_lock);
1937 if (pwd->d_parent == pwd || !d_unhashed(pwd)) {
1938 unsigned long len;
1939 char * cwd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940
Linus Torvalds552ce542007-02-13 12:08:18 -08001941 cwd = __d_path(pwd, pwdmnt, root, rootmnt, page, PAGE_SIZE);
1942 spin_unlock(&dcache_lock);
1943
1944 error = PTR_ERR(cwd);
1945 if (IS_ERR(cwd))
1946 goto out;
1947
1948 error = -ERANGE;
1949 len = PAGE_SIZE + page - cwd;
1950 if (len <= size) {
1951 error = len;
1952 if (copy_to_user(buf, cwd, len))
1953 error = -EFAULT;
1954 }
1955 } else
1956 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001957
1958out:
1959 dput(pwd);
1960 mntput(pwdmnt);
1961 dput(root);
1962 mntput(rootmnt);
1963 free_page((unsigned long) page);
1964 return error;
1965}
1966
1967/*
1968 * Test whether new_dentry is a subdirectory of old_dentry.
1969 *
1970 * Trivially implemented using the dcache structure
1971 */
1972
1973/**
1974 * is_subdir - is new dentry a subdirectory of old_dentry
1975 * @new_dentry: new dentry
1976 * @old_dentry: old dentry
1977 *
1978 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
1979 * Returns 0 otherwise.
1980 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
1981 */
1982
1983int is_subdir(struct dentry * new_dentry, struct dentry * old_dentry)
1984{
1985 int result;
1986 struct dentry * saved = new_dentry;
1987 unsigned long seq;
1988
1989 /* need rcu_readlock to protect against the d_parent trashing due to
1990 * d_move
1991 */
1992 rcu_read_lock();
1993 do {
1994 /* for restarting inner loop in case of seq retry */
1995 new_dentry = saved;
1996 result = 0;
1997 seq = read_seqbegin(&rename_lock);
1998 for (;;) {
1999 if (new_dentry != old_dentry) {
2000 struct dentry * parent = new_dentry->d_parent;
2001 if (parent == new_dentry)
2002 break;
2003 new_dentry = parent;
2004 continue;
2005 }
2006 result = 1;
2007 break;
2008 }
2009 } while (read_seqretry(&rename_lock, seq));
2010 rcu_read_unlock();
2011
2012 return result;
2013}
2014
2015void d_genocide(struct dentry *root)
2016{
2017 struct dentry *this_parent = root;
2018 struct list_head *next;
2019
2020 spin_lock(&dcache_lock);
2021repeat:
2022 next = this_parent->d_subdirs.next;
2023resume:
2024 while (next != &this_parent->d_subdirs) {
2025 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002026 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 next = tmp->next;
2028 if (d_unhashed(dentry)||!dentry->d_inode)
2029 continue;
2030 if (!list_empty(&dentry->d_subdirs)) {
2031 this_parent = dentry;
2032 goto repeat;
2033 }
2034 atomic_dec(&dentry->d_count);
2035 }
2036 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08002037 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038 atomic_dec(&this_parent->d_count);
2039 this_parent = this_parent->d_parent;
2040 goto resume;
2041 }
2042 spin_unlock(&dcache_lock);
2043}
2044
2045/**
2046 * find_inode_number - check for dentry with name
2047 * @dir: directory to check
2048 * @name: Name to find.
2049 *
2050 * Check whether a dentry already exists for the given name,
2051 * and return the inode number if it has an inode. Otherwise
2052 * 0 is returned.
2053 *
2054 * This routine is used to post-process directory listings for
2055 * filesystems using synthetic inode numbers, and is necessary
2056 * to keep getcwd() working.
2057 */
2058
2059ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2060{
2061 struct dentry * dentry;
2062 ino_t ino = 0;
2063
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002064 dentry = d_hash_and_lookup(dir, name);
2065 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002066 if (dentry->d_inode)
2067 ino = dentry->d_inode->i_ino;
2068 dput(dentry);
2069 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 return ino;
2071}
2072
2073static __initdata unsigned long dhash_entries;
2074static int __init set_dhash_entries(char *str)
2075{
2076 if (!str)
2077 return 0;
2078 dhash_entries = simple_strtoul(str, &str, 0);
2079 return 1;
2080}
2081__setup("dhash_entries=", set_dhash_entries);
2082
2083static void __init dcache_init_early(void)
2084{
2085 int loop;
2086
2087 /* If hashes are distributed across NUMA nodes, defer
2088 * hash allocation until vmalloc space is available.
2089 */
2090 if (hashdist)
2091 return;
2092
2093 dentry_hashtable =
2094 alloc_large_system_hash("Dentry cache",
2095 sizeof(struct hlist_head),
2096 dhash_entries,
2097 13,
2098 HASH_EARLY,
2099 &d_hash_shift,
2100 &d_hash_mask,
2101 0);
2102
2103 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2104 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2105}
2106
2107static void __init dcache_init(unsigned long mempages)
2108{
2109 int loop;
2110
2111 /*
2112 * A constructor could be added for stable state like the lists,
2113 * but it is probably not worth it because of the cache nature
2114 * of the dcache.
2115 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002116 dentry_cache = KMEM_CACHE(dentry,
2117 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002118
2119 set_shrinker(DEFAULT_SEEKS, shrink_dcache_memory);
2120
2121 /* Hash may have been set up in dcache_init_early */
2122 if (!hashdist)
2123 return;
2124
2125 dentry_hashtable =
2126 alloc_large_system_hash("Dentry cache",
2127 sizeof(struct hlist_head),
2128 dhash_entries,
2129 13,
2130 0,
2131 &d_hash_shift,
2132 &d_hash_mask,
2133 0);
2134
2135 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2136 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2137}
2138
2139/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002140struct kmem_cache *names_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002141
2142/* SLAB cache for file structures */
Christoph Lametere18b8902006-12-06 20:33:20 -08002143struct kmem_cache *filp_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144
2145EXPORT_SYMBOL(d_genocide);
2146
Linus Torvalds1da177e2005-04-16 15:20:36 -07002147void __init vfs_caches_init_early(void)
2148{
2149 dcache_init_early();
2150 inode_init_early();
2151}
2152
2153void __init vfs_caches_init(unsigned long mempages)
2154{
2155 unsigned long reserve;
2156
2157 /* Base hash sizes on available memory, with a reserve equal to
2158 150% of current kernel size */
2159
2160 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2161 mempages -= reserve;
2162
2163 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
2164 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
2165
2166 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
Dipankar Sarma529bf6b2006-03-07 21:55:35 -08002167 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002168
2169 dcache_init(mempages);
2170 inode_init(mempages);
2171 files_init(mempages);
2172 mnt_init(mempages);
2173 bdev_cache_init();
2174 chrdev_init();
2175}
2176
2177EXPORT_SYMBOL(d_alloc);
2178EXPORT_SYMBOL(d_alloc_anon);
2179EXPORT_SYMBOL(d_alloc_root);
2180EXPORT_SYMBOL(d_delete);
2181EXPORT_SYMBOL(d_find_alias);
2182EXPORT_SYMBOL(d_instantiate);
2183EXPORT_SYMBOL(d_invalidate);
2184EXPORT_SYMBOL(d_lookup);
2185EXPORT_SYMBOL(d_move);
David Howells770bfad2006-08-22 20:06:07 -04002186EXPORT_SYMBOL_GPL(d_materialise_unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002187EXPORT_SYMBOL(d_path);
2188EXPORT_SYMBOL(d_prune_aliases);
2189EXPORT_SYMBOL(d_rehash);
2190EXPORT_SYMBOL(d_splice_alias);
2191EXPORT_SYMBOL(d_validate);
2192EXPORT_SYMBOL(dget_locked);
2193EXPORT_SYMBOL(dput);
2194EXPORT_SYMBOL(find_inode_number);
2195EXPORT_SYMBOL(have_submounts);
2196EXPORT_SYMBOL(names_cachep);
2197EXPORT_SYMBOL(shrink_dcache_parent);
2198EXPORT_SYMBOL(shrink_dcache_sb);