blob: 5a24cee6b76a2965b2c6f22c243cd78b38299ee1 [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>
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +020020#include <linux/fdtable.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include <linux/fs.h>
John McCutchan7a91bf72005-08-08 13:52:16 -040022#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070023#include <linux/slab.h>
24#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070025#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);
Al Viro74c3cbe2007-07-22 08:04:18 -040042__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 -070064
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 */
Akinobu Mitae8462ca2008-02-06 01:37:07 -080092 if (hlist_unhashed(&dentry->d_hash))
Eric Dumazetb3423412006-12-06 20:38:48 -080093 __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.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800102static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200103 __releases(dentry->d_lock)
104 __releases(dcache_lock)
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
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700124/*
125 * dentry_lru_(add|add_tail|del|del_init) must be called with dcache_lock held.
126 */
127static void dentry_lru_add(struct dentry *dentry)
128{
129 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
130 dentry->d_sb->s_nr_dentry_unused++;
131 dentry_stat.nr_unused++;
132}
133
134static void dentry_lru_add_tail(struct dentry *dentry)
135{
136 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
137 dentry->d_sb->s_nr_dentry_unused++;
138 dentry_stat.nr_unused++;
139}
140
141static void dentry_lru_del(struct dentry *dentry)
142{
143 if (!list_empty(&dentry->d_lru)) {
144 list_del(&dentry->d_lru);
145 dentry->d_sb->s_nr_dentry_unused--;
146 dentry_stat.nr_unused--;
147 }
148}
149
150static void dentry_lru_del_init(struct dentry *dentry)
151{
152 if (likely(!list_empty(&dentry->d_lru))) {
153 list_del_init(&dentry->d_lru);
154 dentry->d_sb->s_nr_dentry_unused--;
155 dentry_stat.nr_unused--;
156 }
157}
158
Miklos Szeredid52b9082007-05-08 00:23:46 -0700159/**
160 * d_kill - kill dentry and return parent
161 * @dentry: dentry to kill
162 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200163 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700164 *
165 * If this is the root of the dentry tree, return NULL.
166 */
167static struct dentry *d_kill(struct dentry *dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200168 __releases(dentry->d_lock)
169 __releases(dcache_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700170{
171 struct dentry *parent;
172
173 list_del(&dentry->d_u.d_child);
174 dentry_stat.nr_dentry--; /* For d_free, below */
175 /*drops the locks, at that point nobody can reach this dentry */
176 dentry_iput(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900177 if (IS_ROOT(dentry))
178 parent = NULL;
179 else
180 parent = dentry->d_parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700181 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900182 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700183}
184
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185/*
186 * This is dput
187 *
188 * This is complicated by the fact that we do not want to put
189 * dentries that are no longer on any hash chain on the unused
190 * list: we'd much rather just get rid of them immediately.
191 *
192 * However, that implies that we have to traverse the dentry
193 * tree upwards to the parents which might _also_ now be
194 * scheduled for deletion (it may have been only waiting for
195 * its last child to go away).
196 *
197 * This tail recursion is done by hand as we don't want to depend
198 * on the compiler to always get this right (gcc generally doesn't).
199 * Real recursion would eat up our stack space.
200 */
201
202/*
203 * dput - release a dentry
204 * @dentry: dentry to release
205 *
206 * Release a dentry. This will drop the usage count and if appropriate
207 * call the dentry unlink method as well as removing it from the queues and
208 * releasing its resources. If the parent dentries were scheduled for release
209 * they too may now get deleted.
210 *
211 * no dcache lock, please.
212 */
213
214void dput(struct dentry *dentry)
215{
216 if (!dentry)
217 return;
218
219repeat:
220 if (atomic_read(&dentry->d_count) == 1)
221 might_sleep();
222 if (!atomic_dec_and_lock(&dentry->d_count, &dcache_lock))
223 return;
224
225 spin_lock(&dentry->d_lock);
226 if (atomic_read(&dentry->d_count)) {
227 spin_unlock(&dentry->d_lock);
228 spin_unlock(&dcache_lock);
229 return;
230 }
231
232 /*
233 * AV: ->d_delete() is _NOT_ allowed to block now.
234 */
235 if (dentry->d_op && dentry->d_op->d_delete) {
236 if (dentry->d_op->d_delete(dentry))
237 goto unhash_it;
238 }
239 /* Unreachable? Get rid of it */
240 if (d_unhashed(dentry))
241 goto kill_it;
242 if (list_empty(&dentry->d_lru)) {
243 dentry->d_flags |= DCACHE_REFERENCED;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700244 dentry_lru_add(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700245 }
246 spin_unlock(&dentry->d_lock);
247 spin_unlock(&dcache_lock);
248 return;
249
250unhash_it:
251 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700252kill_it:
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700253 /* if dentry was on the d_lru list delete it from there */
254 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700255 dentry = d_kill(dentry);
256 if (dentry)
257 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700258}
259
260/**
261 * d_invalidate - invalidate a dentry
262 * @dentry: dentry to invalidate
263 *
264 * Try to invalidate the dentry if it turns out to be
265 * possible. If there are other dentries that can be
266 * reached through this one we can't delete it and we
267 * return -EBUSY. On success we return 0.
268 *
269 * no dcache lock.
270 */
271
272int d_invalidate(struct dentry * dentry)
273{
274 /*
275 * If it's already been dropped, return OK.
276 */
277 spin_lock(&dcache_lock);
278 if (d_unhashed(dentry)) {
279 spin_unlock(&dcache_lock);
280 return 0;
281 }
282 /*
283 * Check whether to do a partial shrink_dcache
284 * to get rid of unused child entries.
285 */
286 if (!list_empty(&dentry->d_subdirs)) {
287 spin_unlock(&dcache_lock);
288 shrink_dcache_parent(dentry);
289 spin_lock(&dcache_lock);
290 }
291
292 /*
293 * Somebody else still using it?
294 *
295 * If it's a directory, we can't drop it
296 * for fear of somebody re-populating it
297 * with children (even though dropping it
298 * would make it unreachable from the root,
299 * we might still populate it if it was a
300 * working directory or similar).
301 */
302 spin_lock(&dentry->d_lock);
303 if (atomic_read(&dentry->d_count) > 1) {
304 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
305 spin_unlock(&dentry->d_lock);
306 spin_unlock(&dcache_lock);
307 return -EBUSY;
308 }
309 }
310
311 __d_drop(dentry);
312 spin_unlock(&dentry->d_lock);
313 spin_unlock(&dcache_lock);
314 return 0;
315}
316
317/* This should be called _only_ with dcache_lock held */
318
319static inline struct dentry * __dget_locked(struct dentry *dentry)
320{
321 atomic_inc(&dentry->d_count);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700322 dentry_lru_del_init(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700323 return dentry;
324}
325
326struct dentry * dget_locked(struct dentry *dentry)
327{
328 return __dget_locked(dentry);
329}
330
331/**
332 * d_find_alias - grab a hashed alias of inode
333 * @inode: inode in question
334 * @want_discon: flag, used by d_splice_alias, to request
335 * that only a DISCONNECTED alias be returned.
336 *
337 * If inode has a hashed alias, or is a directory and has any alias,
338 * acquire the reference to alias and return it. Otherwise return NULL.
339 * Notice that if inode is a directory there can be only one alias and
340 * it can be unhashed only if it has no children, or if it is the root
341 * of a filesystem.
342 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700343 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700344 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700345 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
347
348static struct dentry * __d_find_alias(struct inode *inode, int want_discon)
349{
350 struct list_head *head, *next, *tmp;
351 struct dentry *alias, *discon_alias=NULL;
352
353 head = &inode->i_dentry;
354 next = inode->i_dentry.next;
355 while (next != head) {
356 tmp = next;
357 next = tmp->next;
358 prefetch(next);
359 alias = list_entry(tmp, struct dentry, d_alias);
360 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700361 if (IS_ROOT(alias) &&
362 (alias->d_flags & DCACHE_DISCONNECTED))
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 discon_alias = alias;
364 else if (!want_discon) {
365 __dget_locked(alias);
366 return alias;
367 }
368 }
369 }
370 if (discon_alias)
371 __dget_locked(discon_alias);
372 return discon_alias;
373}
374
375struct dentry * d_find_alias(struct inode *inode)
376{
David Howells214fda12006-03-25 03:06:36 -0800377 struct dentry *de = NULL;
378
379 if (!list_empty(&inode->i_dentry)) {
380 spin_lock(&dcache_lock);
381 de = __d_find_alias(inode, 0);
382 spin_unlock(&dcache_lock);
383 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 return de;
385}
386
387/*
388 * Try to kill dentries associated with this inode.
389 * WARNING: you must own a reference to inode.
390 */
391void d_prune_aliases(struct inode *inode)
392{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700393 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700394restart:
395 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700396 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700397 spin_lock(&dentry->d_lock);
398 if (!atomic_read(&dentry->d_count)) {
399 __dget_locked(dentry);
400 __d_drop(dentry);
401 spin_unlock(&dentry->d_lock);
402 spin_unlock(&dcache_lock);
403 dput(dentry);
404 goto restart;
405 }
406 spin_unlock(&dentry->d_lock);
407 }
408 spin_unlock(&dcache_lock);
409}
410
411/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700412 * Throw away a dentry - free the inode, dput the parent. This requires that
413 * the LRU list has already been removed.
414 *
Miklos Szeredi85864e12007-10-16 23:27:09 -0700415 * Try to prune ancestors as well. This is necessary to prevent
416 * quadratic behavior of shrink_dcache_parent(), but is also expected
417 * to be beneficial in reducing dentry cache fragmentation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700418 */
Miklos Szeredi85864e12007-10-16 23:27:09 -0700419static void prune_one_dentry(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200420 __releases(dentry->d_lock)
421 __releases(dcache_lock)
422 __acquires(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700424 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700425 dentry = d_kill(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700426
427 /*
428 * Prune ancestors. Locking is simpler than in dput(),
429 * because dcache_lock needs to be taken anyway.
430 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700431 spin_lock(&dcache_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700432 while (dentry) {
433 if (!atomic_dec_and_lock(&dentry->d_count, &dentry->d_lock))
434 return;
435
436 if (dentry->d_op && dentry->d_op->d_delete)
437 dentry->d_op->d_delete(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700438 dentry_lru_del_init(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700439 __d_drop(dentry);
440 dentry = d_kill(dentry);
441 spin_lock(&dcache_lock);
442 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700443}
444
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700445/*
446 * Shrink the dentry LRU on a given superblock.
447 * @sb : superblock to shrink dentry LRU.
448 * @count: If count is NULL, we prune all dentries on superblock.
449 * @flags: If flags is non-zero, we need to do special processing based on
450 * which flags are set. This means we don't need to maintain multiple
451 * similar copies of this loop.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700453static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700454{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700455 LIST_HEAD(referenced);
456 LIST_HEAD(tmp);
457 struct dentry *dentry;
458 int cnt = 0;
459
460 BUG_ON(!sb);
461 BUG_ON((flags & DCACHE_REFERENCED) && count == NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700463 if (count != NULL)
464 /* called from prune_dcache() and shrink_dcache_parent() */
465 cnt = *count;
466restart:
467 if (count == NULL)
468 list_splice_init(&sb->s_dentry_lru, &tmp);
469 else {
470 while (!list_empty(&sb->s_dentry_lru)) {
471 dentry = list_entry(sb->s_dentry_lru.prev,
472 struct dentry, d_lru);
473 BUG_ON(dentry->d_sb != sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700475 spin_lock(&dentry->d_lock);
476 /*
477 * If we are honouring the DCACHE_REFERENCED flag and
478 * the dentry has this flag set, don't free it. Clear
479 * the flag and put it back on the LRU.
NeilBrown0feae5c2006-06-22 14:47:28 -0700480 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700481 if ((flags & DCACHE_REFERENCED)
482 && (dentry->d_flags & DCACHE_REFERENCED)) {
483 dentry->d_flags &= ~DCACHE_REFERENCED;
484 list_move_tail(&dentry->d_lru, &referenced);
485 spin_unlock(&dentry->d_lock);
486 } else {
487 list_move_tail(&dentry->d_lru, &tmp);
488 spin_unlock(&dentry->d_lock);
489 cnt--;
490 if (!cnt)
491 break;
NeilBrown0feae5c2006-06-22 14:47:28 -0700492 }
Kentaro Makitaf3c6ba92008-07-25 19:44:40 -0700493 cond_resched_lock(&dcache_lock);
NeilBrown0feae5c2006-06-22 14:47:28 -0700494 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700495 }
496 while (!list_empty(&tmp)) {
497 dentry = list_entry(tmp.prev, struct dentry, d_lru);
498 dentry_lru_del_init(dentry);
499 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 /*
501 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700502 * the LRU because of laziness during lookup. Do not free
503 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700505 if (atomic_read(&dentry->d_count)) {
506 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507 continue;
508 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700509 prune_one_dentry(dentry);
510 /* dentry->d_lock was dropped in prune_one_dentry() */
511 cond_resched_lock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700513 if (count == NULL && !list_empty(&sb->s_dentry_lru))
514 goto restart;
515 if (count != NULL)
516 *count = cnt;
517 if (!list_empty(&referenced))
518 list_splice(&referenced, &sb->s_dentry_lru);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 spin_unlock(&dcache_lock);
520}
521
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700522/**
523 * prune_dcache - shrink the dcache
524 * @count: number of entries to try to free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700525 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700526 * Shrink the dcache. This is done when we need more memory, or simply when we
527 * need to unmount something (at which point we need to unuse all dentries).
528 *
529 * This function may fail to free any resources if all the dentries are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700530 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700531static void prune_dcache(int count)
532{
533 struct super_block *sb;
534 int w_count;
535 int unused = dentry_stat.nr_unused;
536 int prune_ratio;
537 int pruned;
538
539 if (unused == 0 || count == 0)
540 return;
541 spin_lock(&dcache_lock);
542restart:
543 if (count >= unused)
544 prune_ratio = 1;
545 else
546 prune_ratio = unused / count;
547 spin_lock(&sb_lock);
548 list_for_each_entry(sb, &super_blocks, s_list) {
549 if (sb->s_nr_dentry_unused == 0)
550 continue;
551 sb->s_count++;
552 /* Now, we reclaim unused dentrins with fairness.
553 * We reclaim them same percentage from each superblock.
554 * We calculate number of dentries to scan on this sb
555 * as follows, but the implementation is arranged to avoid
556 * overflows:
557 * number of dentries to scan on this sb =
558 * count * (number of dentries on this sb /
559 * number of dentries in the machine)
560 */
561 spin_unlock(&sb_lock);
562 if (prune_ratio != 1)
563 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
564 else
565 w_count = sb->s_nr_dentry_unused;
566 pruned = w_count;
567 /*
568 * We need to be sure this filesystem isn't being unmounted,
569 * otherwise we could race with generic_shutdown_super(), and
570 * end up holding a reference to an inode while the filesystem
571 * is unmounted. So we try to get s_umount, and make sure
572 * s_root isn't NULL.
573 */
574 if (down_read_trylock(&sb->s_umount)) {
575 if ((sb->s_root != NULL) &&
576 (!list_empty(&sb->s_dentry_lru))) {
577 spin_unlock(&dcache_lock);
578 __shrink_dcache_sb(sb, &w_count,
579 DCACHE_REFERENCED);
580 pruned -= w_count;
581 spin_lock(&dcache_lock);
582 }
583 up_read(&sb->s_umount);
584 }
585 spin_lock(&sb_lock);
586 count -= pruned;
587 /*
588 * restart only when sb is no longer on the list and
589 * we have more work to do.
590 */
591 if (__put_super_and_need_restart(sb) && count > 0) {
592 spin_unlock(&sb_lock);
593 goto restart;
594 }
595 }
596 spin_unlock(&sb_lock);
597 spin_unlock(&dcache_lock);
598}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599
600/**
601 * shrink_dcache_sb - shrink dcache for a superblock
602 * @sb: superblock
603 *
604 * Shrink the dcache for the specified super block. This
605 * is used to free the dcache before unmounting a file
606 * system
607 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700608void shrink_dcache_sb(struct super_block * sb)
609{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700610 __shrink_dcache_sb(sb, NULL, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700611}
612
613/*
David Howellsc636ebd2006-10-11 01:22:19 -0700614 * destroy a single subtree of dentries for unmount
615 * - see the comments on shrink_dcache_for_umount() for a description of the
616 * locking
617 */
618static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
619{
620 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700621 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700622
623 BUG_ON(!IS_ROOT(dentry));
624
625 /* detach this root from the system */
626 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700627 dentry_lru_del_init(dentry);
David Howellsc636ebd2006-10-11 01:22:19 -0700628 __d_drop(dentry);
629 spin_unlock(&dcache_lock);
630
631 for (;;) {
632 /* descend to the first leaf in the current subtree */
633 while (!list_empty(&dentry->d_subdirs)) {
634 struct dentry *loop;
635
636 /* this is a branch with children - detach all of them
637 * from the system in one go */
638 spin_lock(&dcache_lock);
639 list_for_each_entry(loop, &dentry->d_subdirs,
640 d_u.d_child) {
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700641 dentry_lru_del_init(loop);
David Howellsc636ebd2006-10-11 01:22:19 -0700642 __d_drop(loop);
643 cond_resched_lock(&dcache_lock);
644 }
645 spin_unlock(&dcache_lock);
646
647 /* move to the first child */
648 dentry = list_entry(dentry->d_subdirs.next,
649 struct dentry, d_u.d_child);
650 }
651
652 /* consume the dentries from this leaf up through its parents
653 * until we find one with children or run out altogether */
654 do {
655 struct inode *inode;
656
657 if (atomic_read(&dentry->d_count) != 0) {
658 printk(KERN_ERR
659 "BUG: Dentry %p{i=%lx,n=%s}"
660 " still in use (%d)"
661 " [unmount of %s %s]\n",
662 dentry,
663 dentry->d_inode ?
664 dentry->d_inode->i_ino : 0UL,
665 dentry->d_name.name,
666 atomic_read(&dentry->d_count),
667 dentry->d_sb->s_type->name,
668 dentry->d_sb->s_id);
669 BUG();
670 }
671
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900672 if (IS_ROOT(dentry))
David Howellsc636ebd2006-10-11 01:22:19 -0700673 parent = NULL;
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900674 else {
675 parent = dentry->d_parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700676 atomic_dec(&parent->d_count);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900677 }
David Howellsc636ebd2006-10-11 01:22:19 -0700678
679 list_del(&dentry->d_u.d_child);
David Howellsf8713572006-10-28 10:38:46 -0700680 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700681
682 inode = dentry->d_inode;
683 if (inode) {
684 dentry->d_inode = NULL;
685 list_del_init(&dentry->d_alias);
686 if (dentry->d_op && dentry->d_op->d_iput)
687 dentry->d_op->d_iput(dentry, inode);
688 else
689 iput(inode);
690 }
691
692 d_free(dentry);
693
694 /* finished when we fall off the top of the tree,
695 * otherwise we ascend to the parent and move to the
696 * next sibling if there is one */
697 if (!parent)
David Howellsf8713572006-10-28 10:38:46 -0700698 goto out;
David Howellsc636ebd2006-10-11 01:22:19 -0700699
700 dentry = parent;
701
702 } while (list_empty(&dentry->d_subdirs));
703
704 dentry = list_entry(dentry->d_subdirs.next,
705 struct dentry, d_u.d_child);
706 }
David Howellsf8713572006-10-28 10:38:46 -0700707out:
708 /* several dentries were freed, need to correct nr_dentry */
709 spin_lock(&dcache_lock);
710 dentry_stat.nr_dentry -= detached;
711 spin_unlock(&dcache_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700712}
713
714/*
715 * destroy the dentries attached to a superblock on unmounting
716 * - we don't need to use dentry->d_lock, and only need dcache_lock when
717 * removing the dentry from the system lists and hashes because:
718 * - the superblock is detached from all mountings and open files, so the
719 * dentry trees will not be rearranged by the VFS
720 * - s_umount is write-locked, so the memory pressure shrinker will ignore
721 * any dentries belonging to this superblock that it comes across
722 * - the filesystem itself is no longer permitted to rearrange the dentries
723 * in this superblock
724 */
725void shrink_dcache_for_umount(struct super_block *sb)
726{
727 struct dentry *dentry;
728
729 if (down_read_trylock(&sb->s_umount))
730 BUG();
731
732 dentry = sb->s_root;
733 sb->s_root = NULL;
734 atomic_dec(&dentry->d_count);
735 shrink_dcache_for_umount_subtree(dentry);
736
737 while (!hlist_empty(&sb->s_anon)) {
738 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
739 shrink_dcache_for_umount_subtree(dentry);
740 }
741}
742
743/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700744 * Search for at least 1 mount point in the dentry's subdirs.
745 * We descend to the next level whenever the d_subdirs
746 * list is non-empty and continue searching.
747 */
748
749/**
750 * have_submounts - check for mounts over a dentry
751 * @parent: dentry to check.
752 *
753 * Return true if the parent or its subdirectories contain
754 * a mount point
755 */
756
757int have_submounts(struct dentry *parent)
758{
759 struct dentry *this_parent = parent;
760 struct list_head *next;
761
762 spin_lock(&dcache_lock);
763 if (d_mountpoint(parent))
764 goto positive;
765repeat:
766 next = this_parent->d_subdirs.next;
767resume:
768 while (next != &this_parent->d_subdirs) {
769 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800770 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700771 next = tmp->next;
772 /* Have we found a mount point ? */
773 if (d_mountpoint(dentry))
774 goto positive;
775 if (!list_empty(&dentry->d_subdirs)) {
776 this_parent = dentry;
777 goto repeat;
778 }
779 }
780 /*
781 * All done at this level ... ascend and resume the search.
782 */
783 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800784 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785 this_parent = this_parent->d_parent;
786 goto resume;
787 }
788 spin_unlock(&dcache_lock);
789 return 0; /* No mount points found in tree */
790positive:
791 spin_unlock(&dcache_lock);
792 return 1;
793}
794
795/*
796 * Search the dentry child list for the specified parent,
797 * and move any unused dentries to the end of the unused
798 * list for prune_dcache(). We descend to the next level
799 * whenever the d_subdirs list is non-empty and continue
800 * searching.
801 *
802 * It returns zero iff there are no unused children,
803 * otherwise it returns the number of children moved to
804 * the end of the unused list. This may not be the total
805 * number of unused children, because select_parent can
806 * drop the lock and return early due to latency
807 * constraints.
808 */
809static int select_parent(struct dentry * parent)
810{
811 struct dentry *this_parent = parent;
812 struct list_head *next;
813 int found = 0;
814
815 spin_lock(&dcache_lock);
816repeat:
817 next = this_parent->d_subdirs.next;
818resume:
819 while (next != &this_parent->d_subdirs) {
820 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800821 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822 next = tmp->next;
823
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700824 dentry_lru_del_init(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700825 /*
826 * move only zero ref count dentries to the end
827 * of the unused list for prune_dcache
828 */
829 if (!atomic_read(&dentry->d_count)) {
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700830 dentry_lru_add_tail(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831 found++;
832 }
833
834 /*
835 * We can return to the caller if we have found some (this
836 * ensures forward progress). We'll be coming back to find
837 * the rest.
838 */
839 if (found && need_resched())
840 goto out;
841
842 /*
843 * Descend a level if the d_subdirs list is non-empty.
844 */
845 if (!list_empty(&dentry->d_subdirs)) {
846 this_parent = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700847 goto repeat;
848 }
849 }
850 /*
851 * All done at this level ... ascend and resume the search.
852 */
853 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800854 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 this_parent = this_parent->d_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700856 goto resume;
857 }
858out:
859 spin_unlock(&dcache_lock);
860 return found;
861}
862
863/**
864 * shrink_dcache_parent - prune dcache
865 * @parent: parent of entries to prune
866 *
867 * Prune the dcache to remove unused children of the parent dentry.
868 */
869
870void shrink_dcache_parent(struct dentry * parent)
871{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700872 struct super_block *sb = parent->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700873 int found;
874
875 while ((found = select_parent(parent)) != 0)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700876 __shrink_dcache_sb(sb, &found, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700877}
878
Linus Torvalds1da177e2005-04-16 15:20:36 -0700879/*
880 * Scan `nr' dentries and return the number which remain.
881 *
882 * We need to avoid reentering the filesystem if the caller is performing a
883 * GFP_NOFS allocation attempt. One example deadlock is:
884 *
885 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
886 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
887 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
888 *
889 * In this case we return -1 to tell the caller that we baled.
890 */
Al Viro27496a82005-10-21 03:20:48 -0400891static int shrink_dcache_memory(int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700892{
893 if (nr) {
894 if (!(gfp_mask & __GFP_FS))
895 return -1;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700896 prune_dcache(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700897 }
898 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
899}
900
Rusty Russell8e1f9362007-07-17 04:03:17 -0700901static struct shrinker dcache_shrinker = {
902 .shrink = shrink_dcache_memory,
903 .seeks = DEFAULT_SEEKS,
904};
905
Linus Torvalds1da177e2005-04-16 15:20:36 -0700906/**
907 * d_alloc - allocate a dcache entry
908 * @parent: parent of entry to allocate
909 * @name: qstr of the name
910 *
911 * Allocates a dentry. It returns %NULL if there is insufficient memory
912 * available. On a success the dentry is returned. The name passed in is
913 * copied and the copy passed in may be reused after this call.
914 */
915
916struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
917{
918 struct dentry *dentry;
919 char *dname;
920
Mel Gormane12ba742007-10-16 01:25:52 -0700921 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700922 if (!dentry)
923 return NULL;
924
925 if (name->len > DNAME_INLINE_LEN-1) {
926 dname = kmalloc(name->len + 1, GFP_KERNEL);
927 if (!dname) {
928 kmem_cache_free(dentry_cache, dentry);
929 return NULL;
930 }
931 } else {
932 dname = dentry->d_iname;
933 }
934 dentry->d_name.name = dname;
935
936 dentry->d_name.len = name->len;
937 dentry->d_name.hash = name->hash;
938 memcpy(dname, name->name, name->len);
939 dname[name->len] = 0;
940
941 atomic_set(&dentry->d_count, 1);
942 dentry->d_flags = DCACHE_UNHASHED;
943 spin_lock_init(&dentry->d_lock);
944 dentry->d_inode = NULL;
945 dentry->d_parent = NULL;
946 dentry->d_sb = NULL;
947 dentry->d_op = NULL;
948 dentry->d_fsdata = NULL;
949 dentry->d_mounted = 0;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800950#ifdef CONFIG_PROFILING
Linus Torvalds1da177e2005-04-16 15:20:36 -0700951 dentry->d_cookie = NULL;
Marcelo Tosatti47ba87e2006-02-03 03:04:06 -0800952#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700953 INIT_HLIST_NODE(&dentry->d_hash);
954 INIT_LIST_HEAD(&dentry->d_lru);
955 INIT_LIST_HEAD(&dentry->d_subdirs);
956 INIT_LIST_HEAD(&dentry->d_alias);
957
958 if (parent) {
959 dentry->d_parent = dget(parent);
960 dentry->d_sb = parent->d_sb;
961 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800962 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700963 }
964
965 spin_lock(&dcache_lock);
966 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -0800967 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700968 dentry_stat.nr_dentry++;
969 spin_unlock(&dcache_lock);
970
971 return dentry;
972}
973
974struct dentry *d_alloc_name(struct dentry *parent, const char *name)
975{
976 struct qstr q;
977
978 q.name = name;
979 q.len = strlen(name);
980 q.hash = full_name_hash(q.name, q.len);
981 return d_alloc(parent, &q);
982}
983
OGAWA Hirofumi360da902008-10-16 07:50:28 +0900984/* the caller must hold dcache_lock */
985static void __d_instantiate(struct dentry *dentry, struct inode *inode)
986{
987 if (inode)
988 list_add(&dentry->d_alias, &inode->i_dentry);
989 dentry->d_inode = inode;
990 fsnotify_d_instantiate(dentry, inode);
991}
992
Linus Torvalds1da177e2005-04-16 15:20:36 -0700993/**
994 * d_instantiate - fill in inode information for a dentry
995 * @entry: dentry to complete
996 * @inode: inode to attach to this dentry
997 *
998 * Fill in inode information in the entry.
999 *
1000 * This turns negative dentries into productive full members
1001 * of society.
1002 *
1003 * NOTE! This 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.
1006 */
1007
1008void d_instantiate(struct dentry *entry, struct inode * inode)
1009{
Eric Sesterhenn28133c72006-03-26 18:25:39 +02001010 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 spin_lock(&dcache_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001012 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001013 spin_unlock(&dcache_lock);
1014 security_d_instantiate(entry, inode);
1015}
1016
1017/**
1018 * d_instantiate_unique - instantiate a non-aliased dentry
1019 * @entry: dentry to instantiate
1020 * @inode: inode to attach to this dentry
1021 *
1022 * Fill in inode information in the entry. On success, it returns NULL.
1023 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001024 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 *
1026 * Note that in order to avoid conflicts with rename() etc, the caller
1027 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001028 *
1029 * This also assumes that the inode count has been incremented
1030 * (or otherwise set) by the caller to indicate that it is now
1031 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 */
David Howells770bfad2006-08-22 20:06:07 -04001033static struct dentry *__d_instantiate_unique(struct dentry *entry,
1034 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001035{
1036 struct dentry *alias;
1037 int len = entry->d_name.len;
1038 const char *name = entry->d_name.name;
1039 unsigned int hash = entry->d_name.hash;
1040
David Howells770bfad2006-08-22 20:06:07 -04001041 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001042 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001043 return NULL;
1044 }
1045
Linus Torvalds1da177e2005-04-16 15:20:36 -07001046 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1047 struct qstr *qstr = &alias->d_name;
1048
1049 if (qstr->hash != hash)
1050 continue;
1051 if (alias->d_parent != entry->d_parent)
1052 continue;
1053 if (qstr->len != len)
1054 continue;
1055 if (memcmp(qstr->name, name, len))
1056 continue;
1057 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 return alias;
1059 }
David Howells770bfad2006-08-22 20:06:07 -04001060
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001061 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 return NULL;
1063}
David Howells770bfad2006-08-22 20:06:07 -04001064
1065struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1066{
1067 struct dentry *result;
1068
1069 BUG_ON(!list_empty(&entry->d_alias));
1070
1071 spin_lock(&dcache_lock);
1072 result = __d_instantiate_unique(entry, inode);
1073 spin_unlock(&dcache_lock);
1074
1075 if (!result) {
1076 security_d_instantiate(entry, inode);
1077 return NULL;
1078 }
1079
1080 BUG_ON(!d_unhashed(result));
1081 iput(inode);
1082 return result;
1083}
1084
Linus Torvalds1da177e2005-04-16 15:20:36 -07001085EXPORT_SYMBOL(d_instantiate_unique);
1086
1087/**
1088 * d_alloc_root - allocate root dentry
1089 * @root_inode: inode to allocate the root for
1090 *
1091 * Allocate a root ("/") dentry for the inode given. The inode is
1092 * instantiated and returned. %NULL is returned if there is insufficient
1093 * memory or the inode passed is %NULL.
1094 */
1095
1096struct dentry * d_alloc_root(struct inode * root_inode)
1097{
1098 struct dentry *res = NULL;
1099
1100 if (root_inode) {
1101 static const struct qstr name = { .name = "/", .len = 1 };
1102
1103 res = d_alloc(NULL, &name);
1104 if (res) {
1105 res->d_sb = root_inode->i_sb;
1106 res->d_parent = res;
1107 d_instantiate(res, root_inode);
1108 }
1109 }
1110 return res;
1111}
1112
1113static inline struct hlist_head *d_hash(struct dentry *parent,
1114 unsigned long hash)
1115{
1116 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1117 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1118 return dentry_hashtable + (hash & D_HASHMASK);
1119}
1120
1121/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001122 * d_obtain_alias - find or allocate a dentry for a given inode
1123 * @inode: inode to allocate the dentry for
1124 *
1125 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1126 * similar open by handle operations. The returned dentry may be anonymous,
1127 * or may have a full name (if the inode was already in the cache).
1128 *
1129 * When called on a directory inode, we must ensure that the inode only ever
1130 * has one dentry. If a dentry is found, that is returned instead of
1131 * allocating a new one.
1132 *
1133 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001134 * to the dentry. In case of an error the reference on the inode is released.
1135 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1136 * be passed in and will be the error will be propagate to the return value,
1137 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001138 */
1139struct dentry *d_obtain_alias(struct inode *inode)
1140{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001141 static const struct qstr anonstring = { .name = "" };
1142 struct dentry *tmp;
1143 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001144
1145 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001146 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001147 if (IS_ERR(inode))
1148 return ERR_CAST(inode);
1149
Christoph Hellwig9308a612008-08-11 15:49:12 +02001150 res = d_find_alias(inode);
1151 if (res)
1152 goto out_iput;
1153
1154 tmp = d_alloc(NULL, &anonstring);
1155 if (!tmp) {
1156 res = ERR_PTR(-ENOMEM);
1157 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001158 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001159 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1160
1161 spin_lock(&dcache_lock);
1162 res = __d_find_alias(inode, 0);
1163 if (res) {
1164 spin_unlock(&dcache_lock);
1165 dput(tmp);
1166 goto out_iput;
1167 }
1168
1169 /* attach a disconnected dentry */
1170 spin_lock(&tmp->d_lock);
1171 tmp->d_sb = inode->i_sb;
1172 tmp->d_inode = inode;
1173 tmp->d_flags |= DCACHE_DISCONNECTED;
1174 tmp->d_flags &= ~DCACHE_UNHASHED;
1175 list_add(&tmp->d_alias, &inode->i_dentry);
1176 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
1177 spin_unlock(&tmp->d_lock);
1178
1179 spin_unlock(&dcache_lock);
1180 return tmp;
1181
1182 out_iput:
1183 iput(inode);
1184 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001185}
1186EXPORT_SYMBOL_GPL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
1188/**
1189 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1190 * @inode: the inode which may have a disconnected dentry
1191 * @dentry: a negative dentry which we want to point to the inode.
1192 *
1193 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1194 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1195 * and return it, else simply d_add the inode to the dentry and return NULL.
1196 *
1197 * This is needed in the lookup routine of any filesystem that is exportable
1198 * (via knfsd) so that we can build dcache paths to directories effectively.
1199 *
1200 * If a dentry was found and moved, then it is returned. Otherwise NULL
1201 * is returned. This matches the expected return value of ->lookup.
1202 *
1203 */
1204struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1205{
1206 struct dentry *new = NULL;
1207
NeilBrown21c0d8f2006-10-04 02:16:16 -07001208 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001209 spin_lock(&dcache_lock);
1210 new = __d_find_alias(inode, 1);
1211 if (new) {
1212 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Pigginc32ccd82006-03-25 03:07:09 -08001213 fsnotify_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 spin_unlock(&dcache_lock);
1215 security_d_instantiate(new, inode);
1216 d_rehash(dentry);
1217 d_move(new, dentry);
1218 iput(inode);
1219 } else {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001220 /* already taking dcache_lock, so d_add() by hand */
1221 __d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 spin_unlock(&dcache_lock);
1223 security_d_instantiate(dentry, inode);
1224 d_rehash(dentry);
1225 }
1226 } else
1227 d_add(dentry, inode);
1228 return new;
1229}
1230
Barry Naujok94035402008-05-21 16:50:46 +10001231/**
1232 * d_add_ci - lookup or allocate new dentry with case-exact name
1233 * @inode: the inode case-insensitive lookup has found
1234 * @dentry: the negative dentry that was passed to the parent's lookup func
1235 * @name: the case-exact name to be associated with the returned dentry
1236 *
1237 * This is to avoid filling the dcache with case-insensitive names to the
1238 * same inode, only the actual correct case is stored in the dcache for
1239 * case-insensitive filesystems.
1240 *
1241 * For a case-insensitive lookup match and if the the case-exact dentry
1242 * already exists in in the dcache, use it and return it.
1243 *
1244 * If no entry exists with the exact case name, allocate new dentry with
1245 * the exact case, and return the spliced entry.
1246 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001247struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001248 struct qstr *name)
1249{
1250 int error;
1251 struct dentry *found;
1252 struct dentry *new;
1253
1254 /* Does a dentry matching the name exist already? */
1255 found = d_hash_and_lookup(dentry->d_parent, name);
1256 /* If not, create it now and return */
1257 if (!found) {
1258 new = d_alloc(dentry->d_parent, name);
1259 if (!new) {
1260 error = -ENOMEM;
1261 goto err_out;
1262 }
1263 found = d_splice_alias(inode, new);
1264 if (found) {
1265 dput(new);
1266 return found;
1267 }
1268 return new;
1269 }
1270 /* Matching dentry exists, check if it is negative. */
1271 if (found->d_inode) {
1272 if (unlikely(found->d_inode != inode)) {
1273 /* This can't happen because bad inodes are unhashed. */
1274 BUG_ON(!is_bad_inode(inode));
1275 BUG_ON(!is_bad_inode(found->d_inode));
1276 }
1277 /*
1278 * Already have the inode and the dentry attached, decrement
1279 * the reference count to balance the iget() done
1280 * earlier on. We found the dentry using d_lookup() so it
1281 * cannot be disconnected and thus we do not need to worry
1282 * about any NFS/disconnectedness issues here.
1283 */
1284 iput(inode);
1285 return found;
1286 }
1287 /*
1288 * Negative dentry: instantiate it unless the inode is a directory and
1289 * has a 'disconnected' dentry (i.e. IS_ROOT and DCACHE_DISCONNECTED),
1290 * in which case d_move() that in place of the found dentry.
1291 */
1292 if (!S_ISDIR(inode->i_mode)) {
1293 /* Not a directory; everything is easy. */
1294 d_instantiate(found, inode);
1295 return found;
1296 }
1297 spin_lock(&dcache_lock);
1298 if (list_empty(&inode->i_dentry)) {
1299 /*
1300 * Directory without a 'disconnected' dentry; we need to do
1301 * d_instantiate() by hand because it takes dcache_lock which
1302 * we already hold.
1303 */
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001304 __d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001305 spin_unlock(&dcache_lock);
1306 security_d_instantiate(found, inode);
1307 return found;
1308 }
1309 /*
1310 * Directory with a 'disconnected' dentry; get a reference to the
1311 * 'disconnected' dentry.
1312 */
1313 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1314 dget_locked(new);
1315 spin_unlock(&dcache_lock);
1316 /* Do security vodoo. */
1317 security_d_instantiate(found, inode);
1318 /* Move new in place of found. */
1319 d_move(new, found);
1320 /* Balance the iget() we did above. */
1321 iput(inode);
1322 /* Throw away found. */
1323 dput(found);
1324 /* Use new as the actual dentry. */
1325 return new;
1326
1327err_out:
1328 iput(inode);
1329 return ERR_PTR(error);
1330}
Linus Torvalds1da177e2005-04-16 15:20:36 -07001331
1332/**
1333 * d_lookup - search for a dentry
1334 * @parent: parent dentry
1335 * @name: qstr of name we wish to find
1336 *
1337 * Searches the children of the parent dentry for the name in question. If
1338 * the dentry is found its reference count is incremented and the dentry
1339 * is returned. The caller must use d_put to free the entry when it has
1340 * finished using it. %NULL is returned on failure.
1341 *
1342 * __d_lookup is dcache_lock free. The hash list is protected using RCU.
1343 * Memory barriers are used while updating and doing lockless traversal.
1344 * To avoid races with d_move while rename is happening, d_lock is used.
1345 *
1346 * Overflows in memcmp(), while d_move, are avoided by keeping the length
1347 * and name pointer in one structure pointed by d_qstr.
1348 *
1349 * rcu_read_lock() and rcu_read_unlock() are used to disable preemption while
1350 * lookup is going on.
1351 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001352 * The dentry unused LRU is not updated even if lookup finds the required dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001353 * in there. It is updated in places such as prune_dcache, shrink_dcache_sb,
1354 * select_parent and __dget_locked. This laziness saves lookup from dcache_lock
1355 * acquisition.
1356 *
1357 * d_lookup() is protected against the concurrent renames in some unrelated
1358 * directory using the seqlockt_t rename_lock.
1359 */
1360
1361struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1362{
1363 struct dentry * dentry = NULL;
1364 unsigned long seq;
1365
1366 do {
1367 seq = read_seqbegin(&rename_lock);
1368 dentry = __d_lookup(parent, name);
1369 if (dentry)
1370 break;
1371 } while (read_seqretry(&rename_lock, seq));
1372 return dentry;
1373}
1374
1375struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1376{
1377 unsigned int len = name->len;
1378 unsigned int hash = name->hash;
1379 const unsigned char *str = name->name;
1380 struct hlist_head *head = d_hash(parent,hash);
1381 struct dentry *found = NULL;
1382 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001383 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384
1385 rcu_read_lock();
1386
Paul E. McKenney665a7582005-11-07 00:59:17 -08001387 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001388 struct qstr *qstr;
1389
Linus Torvalds1da177e2005-04-16 15:20:36 -07001390 if (dentry->d_name.hash != hash)
1391 continue;
1392 if (dentry->d_parent != parent)
1393 continue;
1394
1395 spin_lock(&dentry->d_lock);
1396
1397 /*
1398 * Recheck the dentry after taking the lock - d_move may have
1399 * changed things. Don't bother checking the hash because we're
1400 * about to compare the whole name anyway.
1401 */
1402 if (dentry->d_parent != parent)
1403 goto next;
1404
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001405 /* non-existing due to RCU? */
1406 if (d_unhashed(dentry))
1407 goto next;
1408
Linus Torvalds1da177e2005-04-16 15:20:36 -07001409 /*
1410 * It is safe to compare names since d_move() cannot
1411 * change the qstr (protected by d_lock).
1412 */
1413 qstr = &dentry->d_name;
1414 if (parent->d_op && parent->d_op->d_compare) {
1415 if (parent->d_op->d_compare(parent, qstr, name))
1416 goto next;
1417 } else {
1418 if (qstr->len != len)
1419 goto next;
1420 if (memcmp(qstr->name, str, len))
1421 goto next;
1422 }
1423
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001424 atomic_inc(&dentry->d_count);
1425 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001426 spin_unlock(&dentry->d_lock);
1427 break;
1428next:
1429 spin_unlock(&dentry->d_lock);
1430 }
1431 rcu_read_unlock();
1432
1433 return found;
1434}
1435
1436/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001437 * d_hash_and_lookup - hash the qstr then search for a dentry
1438 * @dir: Directory to search in
1439 * @name: qstr of name we wish to find
1440 *
1441 * On hash failure or on lookup failure NULL is returned.
1442 */
1443struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1444{
1445 struct dentry *dentry = NULL;
1446
1447 /*
1448 * Check for a fs-specific hash function. Note that we must
1449 * calculate the standard hash first, as the d_op->d_hash()
1450 * routine may choose to leave the hash value unchanged.
1451 */
1452 name->hash = full_name_hash(name->name, name->len);
1453 if (dir->d_op && dir->d_op->d_hash) {
1454 if (dir->d_op->d_hash(dir, name) < 0)
1455 goto out;
1456 }
1457 dentry = d_lookup(dir, name);
1458out:
1459 return dentry;
1460}
1461
1462/**
Linus Torvalds1da177e2005-04-16 15:20:36 -07001463 * d_validate - verify dentry provided from insecure source
1464 * @dentry: The dentry alleged to be valid child of @dparent
1465 * @dparent: The parent dentry (known to be valid)
1466 * @hash: Hash of the dentry
1467 * @len: Length of the name
1468 *
1469 * An insecure source has sent us a dentry, here we verify it and dget() it.
1470 * This is used by ncpfs in its readdir implementation.
1471 * Zero is returned in the dentry is invalid.
1472 */
1473
1474int d_validate(struct dentry *dentry, struct dentry *dparent)
1475{
1476 struct hlist_head *base;
1477 struct hlist_node *lhp;
1478
1479 /* Check whether the ptr might be valid at all.. */
1480 if (!kmem_ptr_validate(dentry_cache, dentry))
1481 goto out;
1482
1483 if (dentry->d_parent != dparent)
1484 goto out;
1485
1486 spin_lock(&dcache_lock);
1487 base = d_hash(dparent, dentry->d_name.hash);
1488 hlist_for_each(lhp,base) {
Paul E. McKenney665a7582005-11-07 00:59:17 -08001489 /* hlist_for_each_entry_rcu() not required for d_hash list
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 * as it is parsed under dcache_lock
1491 */
1492 if (dentry == hlist_entry(lhp, struct dentry, d_hash)) {
1493 __dget_locked(dentry);
1494 spin_unlock(&dcache_lock);
1495 return 1;
1496 }
1497 }
1498 spin_unlock(&dcache_lock);
1499out:
1500 return 0;
1501}
1502
1503/*
1504 * When a file is deleted, we have two options:
1505 * - turn this dentry into a negative dentry
1506 * - unhash this dentry and free it.
1507 *
1508 * Usually, we want to just turn this into
1509 * a negative dentry, but if anybody else is
1510 * currently using the dentry or the inode
1511 * we can't do that and we fall back on removing
1512 * it from the hash queues and waiting for
1513 * it to be deleted later when it has no users
1514 */
1515
1516/**
1517 * d_delete - delete a dentry
1518 * @dentry: The dentry to delete
1519 *
1520 * Turn the dentry into a negative dentry if possible, otherwise
1521 * remove it from the hash queues so it can be deleted later
1522 */
1523
1524void d_delete(struct dentry * dentry)
1525{
John McCutchan7a91bf72005-08-08 13:52:16 -04001526 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 /*
1528 * Are we the only user?
1529 */
1530 spin_lock(&dcache_lock);
1531 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001532 isdir = S_ISDIR(dentry->d_inode->i_mode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 if (atomic_read(&dentry->d_count) == 1) {
1534 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001535 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001536 return;
1537 }
1538
1539 if (!d_unhashed(dentry))
1540 __d_drop(dentry);
1541
1542 spin_unlock(&dentry->d_lock);
1543 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001544
1545 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001546}
1547
1548static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1549{
1550
1551 entry->d_flags &= ~DCACHE_UNHASHED;
1552 hlist_add_head_rcu(&entry->d_hash, list);
1553}
1554
David Howells770bfad2006-08-22 20:06:07 -04001555static void _d_rehash(struct dentry * entry)
1556{
1557 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1558}
1559
Linus Torvalds1da177e2005-04-16 15:20:36 -07001560/**
1561 * d_rehash - add an entry back to the hash
1562 * @entry: dentry to add to the hash
1563 *
1564 * Adds a dentry to the hash according to its name.
1565 */
1566
1567void d_rehash(struct dentry * entry)
1568{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569 spin_lock(&dcache_lock);
1570 spin_lock(&entry->d_lock);
David Howells770bfad2006-08-22 20:06:07 -04001571 _d_rehash(entry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001572 spin_unlock(&entry->d_lock);
1573 spin_unlock(&dcache_lock);
1574}
1575
1576#define do_switch(x,y) do { \
1577 __typeof__ (x) __tmp = x; \
1578 x = y; y = __tmp; } while (0)
1579
1580/*
1581 * When switching names, the actual string doesn't strictly have to
1582 * be preserved in the target - because we're dropping the target
1583 * anyway. As such, we can just do a simple memcpy() to copy over
1584 * the new name before we switch.
1585 *
1586 * Note that we have to be a lot more careful about getting the hash
1587 * switched - we have to switch the hash value properly even if it
1588 * then no longer matches the actual (corrupted) string of the target.
1589 * The hash value has to match the hash queue that the dentry is on..
1590 */
1591static void switch_names(struct dentry *dentry, struct dentry *target)
1592{
1593 if (dname_external(target)) {
1594 if (dname_external(dentry)) {
1595 /*
1596 * Both external: swap the pointers
1597 */
1598 do_switch(target->d_name.name, dentry->d_name.name);
1599 } else {
1600 /*
1601 * dentry:internal, target:external. Steal target's
1602 * storage and make target internal.
1603 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07001604 memcpy(target->d_iname, dentry->d_name.name,
1605 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606 dentry->d_name.name = target->d_name.name;
1607 target->d_name.name = target->d_iname;
1608 }
1609 } else {
1610 if (dname_external(dentry)) {
1611 /*
1612 * dentry:external, target:internal. Give dentry's
1613 * storage to target and make dentry internal
1614 */
1615 memcpy(dentry->d_iname, target->d_name.name,
1616 target->d_name.len + 1);
1617 target->d_name.name = dentry->d_name.name;
1618 dentry->d_name.name = dentry->d_iname;
1619 } else {
1620 /*
1621 * Both are internal. Just copy target to dentry
1622 */
1623 memcpy(dentry->d_iname, target->d_name.name,
1624 target->d_name.len + 1);
1625 }
1626 }
1627}
1628
1629/*
1630 * We cannibalize "target" when moving dentry on top of it,
1631 * because it's going to be thrown away anyway. We could be more
1632 * polite about it, though.
1633 *
1634 * This forceful removal will result in ugly /proc output if
1635 * somebody holds a file open that got deleted due to a rename.
1636 * We could be nicer about the deleted file, and let it show
J. Bruce Fieldsbc154b12007-10-16 23:29:42 -07001637 * up under the name it had before it was deleted rather than
1638 * under the original name of the file that was moved on top of it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001639 */
1640
Trond Myklebust9eaef272006-10-21 10:24:20 -07001641/*
1642 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 * @dentry: entry to move
1644 * @target: new dentry
1645 *
1646 * Update the dcache to reflect the move of a file name. Negative
1647 * dcache entries should not be moved in this way.
1648 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001649static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001650{
1651 struct hlist_head *list;
1652
1653 if (!dentry->d_inode)
1654 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1655
Linus Torvalds1da177e2005-04-16 15:20:36 -07001656 write_seqlock(&rename_lock);
1657 /*
1658 * XXXX: do we really need to take target->d_lock?
1659 */
1660 if (target < dentry) {
1661 spin_lock(&target->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001662 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663 } else {
1664 spin_lock(&dentry->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001665 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001666 }
1667
1668 /* Move the dentry to the target hash queue, if on different bucket */
Denis Chengf77e3492007-10-16 23:30:11 -07001669 if (d_unhashed(dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001670 goto already_unhashed;
1671
1672 hlist_del_rcu(&dentry->d_hash);
1673
1674already_unhashed:
1675 list = d_hash(target->d_parent, target->d_name.hash);
1676 __d_rehash(dentry, list);
1677
1678 /* Unhash the target: dput() will then get rid of it */
1679 __d_drop(target);
1680
Eric Dumazet5160ee62006-01-08 01:03:32 -08001681 list_del(&dentry->d_u.d_child);
1682 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683
1684 /* Switch the names.. */
1685 switch_names(dentry, target);
1686 do_switch(dentry->d_name.len, target->d_name.len);
1687 do_switch(dentry->d_name.hash, target->d_name.hash);
1688
1689 /* ... and switch the parents */
1690 if (IS_ROOT(dentry)) {
1691 dentry->d_parent = target->d_parent;
1692 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001693 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001694 } else {
1695 do_switch(dentry->d_parent, target->d_parent);
1696
1697 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001698 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699 }
1700
Eric Dumazet5160ee62006-01-08 01:03:32 -08001701 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001703 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001704 spin_unlock(&dentry->d_lock);
1705 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001706}
1707
1708/**
1709 * d_move - move a dentry
1710 * @dentry: entry to move
1711 * @target: new dentry
1712 *
1713 * Update the dcache to reflect the move of a file name. Negative
1714 * dcache entries should not be moved in this way.
1715 */
1716
1717void d_move(struct dentry * dentry, struct dentry * target)
1718{
1719 spin_lock(&dcache_lock);
1720 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001721 spin_unlock(&dcache_lock);
1722}
1723
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001724/**
1725 * d_ancestor - search for an ancestor
1726 * @p1: ancestor dentry
1727 * @p2: child dentry
1728 *
1729 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1730 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07001731 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001732struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001733{
1734 struct dentry *p;
1735
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09001736 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07001737 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001738 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001739 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001740 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001741}
1742
1743/*
1744 * This helper attempts to cope with remotely renamed directories
1745 *
1746 * It assumes that the caller is already holding
1747 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1748 *
1749 * Note: If ever the locking in lock_rename() changes, then please
1750 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07001751 */
1752static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02001753 __releases(dcache_lock)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001754{
1755 struct mutex *m1 = NULL, *m2 = NULL;
1756 struct dentry *ret;
1757
1758 /* If alias and dentry share a parent, then no extra locks required */
1759 if (alias->d_parent == dentry->d_parent)
1760 goto out_unalias;
1761
1762 /* Check for loops */
1763 ret = ERR_PTR(-ELOOP);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001764 if (d_ancestor(alias, dentry))
Trond Myklebust9eaef272006-10-21 10:24:20 -07001765 goto out_err;
1766
1767 /* See lock_rename() */
1768 ret = ERR_PTR(-EBUSY);
1769 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1770 goto out_err;
1771 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1772 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1773 goto out_err;
1774 m2 = &alias->d_parent->d_inode->i_mutex;
1775out_unalias:
1776 d_move_locked(alias, dentry);
1777 ret = alias;
1778out_err:
1779 spin_unlock(&dcache_lock);
1780 if (m2)
1781 mutex_unlock(m2);
1782 if (m1)
1783 mutex_unlock(m1);
1784 return ret;
1785}
1786
1787/*
David Howells770bfad2006-08-22 20:06:07 -04001788 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
1789 * named dentry in place of the dentry to be replaced.
1790 */
1791static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
1792{
1793 struct dentry *dparent, *aparent;
1794
1795 switch_names(dentry, anon);
1796 do_switch(dentry->d_name.len, anon->d_name.len);
1797 do_switch(dentry->d_name.hash, anon->d_name.hash);
1798
1799 dparent = dentry->d_parent;
1800 aparent = anon->d_parent;
1801
1802 dentry->d_parent = (aparent == anon) ? dentry : aparent;
1803 list_del(&dentry->d_u.d_child);
1804 if (!IS_ROOT(dentry))
1805 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
1806 else
1807 INIT_LIST_HEAD(&dentry->d_u.d_child);
1808
1809 anon->d_parent = (dparent == dentry) ? anon : dparent;
1810 list_del(&anon->d_u.d_child);
1811 if (!IS_ROOT(anon))
1812 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
1813 else
1814 INIT_LIST_HEAD(&anon->d_u.d_child);
1815
1816 anon->d_flags &= ~DCACHE_DISCONNECTED;
1817}
1818
1819/**
1820 * d_materialise_unique - introduce an inode into the tree
1821 * @dentry: candidate dentry
1822 * @inode: inode to bind to the dentry, to which aliases may be attached
1823 *
1824 * Introduces an dentry into the tree, substituting an extant disconnected
1825 * root directory alias in its place if there is one
1826 */
1827struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
1828{
Trond Myklebust9eaef272006-10-21 10:24:20 -07001829 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04001830
1831 BUG_ON(!d_unhashed(dentry));
1832
1833 spin_lock(&dcache_lock);
1834
1835 if (!inode) {
1836 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001837 __d_instantiate(dentry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001838 goto found_lock;
1839 }
1840
Trond Myklebust9eaef272006-10-21 10:24:20 -07001841 if (S_ISDIR(inode->i_mode)) {
1842 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04001843
Trond Myklebust9eaef272006-10-21 10:24:20 -07001844 /* Does an aliased dentry already exist? */
1845 alias = __d_find_alias(inode, 0);
1846 if (alias) {
1847 actual = alias;
1848 /* Is this an anonymous mountpoint that we could splice
1849 * into our tree? */
1850 if (IS_ROOT(alias)) {
1851 spin_lock(&alias->d_lock);
1852 __d_materialise_dentry(dentry, alias);
1853 __d_drop(alias);
1854 goto found;
1855 }
1856 /* Nope, but we must(!) avoid directory aliasing */
1857 actual = __d_unalias(dentry, alias);
1858 if (IS_ERR(actual))
1859 dput(alias);
1860 goto out_nolock;
1861 }
David Howells770bfad2006-08-22 20:06:07 -04001862 }
1863
1864 /* Add a unique reference */
1865 actual = __d_instantiate_unique(dentry, inode);
1866 if (!actual)
1867 actual = dentry;
1868 else if (unlikely(!d_unhashed(actual)))
1869 goto shouldnt_be_hashed;
1870
1871found_lock:
1872 spin_lock(&actual->d_lock);
1873found:
1874 _d_rehash(actual);
1875 spin_unlock(&actual->d_lock);
1876 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001877out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04001878 if (actual == dentry) {
1879 security_d_instantiate(dentry, inode);
1880 return NULL;
1881 }
1882
1883 iput(inode);
1884 return actual;
1885
David Howells770bfad2006-08-22 20:06:07 -04001886shouldnt_be_hashed:
1887 spin_unlock(&dcache_lock);
1888 BUG();
David Howells770bfad2006-08-22 20:06:07 -04001889}
1890
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001891static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01001892{
1893 *buflen -= namelen;
1894 if (*buflen < 0)
1895 return -ENAMETOOLONG;
1896 *buffer -= namelen;
1897 memcpy(*buffer, str, namelen);
1898 return 0;
1899}
1900
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001901static int prepend_name(char **buffer, int *buflen, struct qstr *name)
1902{
1903 return prepend(buffer, buflen, name->name, name->len);
1904}
1905
Linus Torvalds1da177e2005-04-16 15:20:36 -07001906/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02001907 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01001908 * @path: the dentry/vfsmount to report
1909 * @root: root vfsmnt/dentry (may be modified by this function)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001910 * @buffer: buffer to return value in
1911 * @buflen: buffer length
1912 *
Linus Torvalds552ce542007-02-13 12:08:18 -08001913 * Convert a dentry into an ASCII path name. If the entry has been deleted
1914 * the string " (deleted)" is appended. Note that this is ambiguous.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001915 *
Linus Torvalds552ce542007-02-13 12:08:18 -08001916 * Returns the buffer or an error code if the path was too long.
1917 *
1918 * "buflen" should be positive. Caller holds the dcache_lock.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01001919 *
1920 * If path is not reachable from the supplied root, then the value of
1921 * root is changed (without modifying refcounts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922 */
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01001923char *__d_path(const struct path *path, struct path *root,
1924 char *buffer, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001925{
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01001926 struct dentry *dentry = path->dentry;
1927 struct vfsmount *vfsmnt = path->mnt;
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001928 char *end = buffer + buflen;
1929 char *retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930
Andreas Gruenbacherbe285c72008-06-16 13:28:07 +02001931 spin_lock(&vfsmount_lock);
Ram Pai6092d042008-03-27 13:06:20 +01001932 prepend(&end, &buflen, "\0", 1);
1933 if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
1934 (prepend(&end, &buflen, " (deleted)", 10) != 0))
Linus Torvalds552ce542007-02-13 12:08:18 -08001935 goto Elong;
Linus Torvalds552ce542007-02-13 12:08:18 -08001936
1937 if (buflen < 1)
1938 goto Elong;
1939 /* Get '/' right */
1940 retval = end-1;
1941 *retval = '/';
1942
1943 for (;;) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944 struct dentry * parent;
1945
Jan Blunck329c97f2008-02-14 19:38:31 -08001946 if (dentry == root->dentry && vfsmnt == root->mnt)
Linus Torvalds552ce542007-02-13 12:08:18 -08001947 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001948 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08001949 /* Global root? */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001950 if (vfsmnt->mnt_parent == vfsmnt) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001951 goto global_root;
1952 }
1953 dentry = vfsmnt->mnt_mountpoint;
1954 vfsmnt = vfsmnt->mnt_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001955 continue;
1956 }
1957 parent = dentry->d_parent;
1958 prefetch(parent);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001959 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
Ram Pai6092d042008-03-27 13:06:20 +01001960 (prepend(&end, &buflen, "/", 1) != 0))
Linus Torvalds552ce542007-02-13 12:08:18 -08001961 goto Elong;
Linus Torvalds552ce542007-02-13 12:08:18 -08001962 retval = end;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 dentry = parent;
1964 }
1965
Andreas Gruenbacherbe285c72008-06-16 13:28:07 +02001966out:
1967 spin_unlock(&vfsmount_lock);
Linus Torvalds552ce542007-02-13 12:08:18 -08001968 return retval;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969
1970global_root:
Ram Pai6092d042008-03-27 13:06:20 +01001971 retval += 1; /* hit the slash */
Miklos Szeredicdd16d02008-06-23 18:11:53 +02001972 if (prepend_name(&retval, &buflen, &dentry->d_name) != 0)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001973 goto Elong;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01001974 root->mnt = vfsmnt;
1975 root->dentry = dentry;
Andreas Gruenbacherbe285c72008-06-16 13:28:07 +02001976 goto out;
1977
Linus Torvalds1da177e2005-04-16 15:20:36 -07001978Elong:
Andreas Gruenbacherbe285c72008-06-16 13:28:07 +02001979 retval = ERR_PTR(-ENAMETOOLONG);
1980 goto out;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981}
1982
Jan Bluncka03a8a702008-02-14 19:38:32 -08001983/**
1984 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08001985 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08001986 * @buf: buffer to return value in
1987 * @buflen: buffer length
1988 *
1989 * Convert a dentry into an ASCII path name. If the entry has been deleted
1990 * the string " (deleted)" is appended. Note that this is ambiguous.
1991 *
1992 * Returns the buffer or an error code if the path was too long.
1993 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02001994 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08001995 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07001996char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997{
1998 char *res;
Jan Blunck6ac08c32008-02-14 19:34:38 -08001999 struct path root;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002000 struct path tmp;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002002 /*
2003 * We have various synthetic filesystems that never get mounted. On
2004 * these filesystems dentries are never used for lookup purposes, and
2005 * thus don't need to be hashed. They also don't need a name until a
2006 * user wants to identify the object in /proc/pid/fd/. The little hack
2007 * below allows us to generate a name for these objects on demand:
2008 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002009 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2010 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002011
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 read_lock(&current->fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002013 root = current->fs->root;
Ram Pai6092d042008-03-27 13:06:20 +01002014 path_get(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002015 read_unlock(&current->fs->lock);
Linus Torvalds552ce542007-02-13 12:08:18 -08002016 spin_lock(&dcache_lock);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002017 tmp = root;
2018 res = __d_path(path, &tmp, buf, buflen);
Linus Torvalds552ce542007-02-13 12:08:18 -08002019 spin_unlock(&dcache_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002020 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002021 return res;
2022}
2023
2024/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002025 * Helper function for dentry_operations.d_dname() members
2026 */
2027char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2028 const char *fmt, ...)
2029{
2030 va_list args;
2031 char temp[64];
2032 int sz;
2033
2034 va_start(args, fmt);
2035 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2036 va_end(args);
2037
2038 if (sz > sizeof(temp) || sz > buflen)
2039 return ERR_PTR(-ENAMETOOLONG);
2040
2041 buffer += buflen - sz;
2042 return memcpy(buffer, temp, sz);
2043}
2044
2045/*
Ram Pai6092d042008-03-27 13:06:20 +01002046 * Write full pathname from the root of the filesystem into the buffer.
2047 */
2048char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2049{
2050 char *end = buf + buflen;
2051 char *retval;
2052
2053 spin_lock(&dcache_lock);
2054 prepend(&end, &buflen, "\0", 1);
2055 if (!IS_ROOT(dentry) && d_unhashed(dentry) &&
2056 (prepend(&end, &buflen, "//deleted", 9) != 0))
2057 goto Elong;
2058 if (buflen < 1)
2059 goto Elong;
2060 /* Get '/' right */
2061 retval = end-1;
2062 *retval = '/';
2063
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002064 while (!IS_ROOT(dentry)) {
2065 struct dentry *parent = dentry->d_parent;
Ram Pai6092d042008-03-27 13:06:20 +01002066
Ram Pai6092d042008-03-27 13:06:20 +01002067 prefetch(parent);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002068 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
Ram Pai6092d042008-03-27 13:06:20 +01002069 (prepend(&end, &buflen, "/", 1) != 0))
2070 goto Elong;
2071
2072 retval = end;
2073 dentry = parent;
2074 }
2075 spin_unlock(&dcache_lock);
2076 return retval;
2077Elong:
2078 spin_unlock(&dcache_lock);
2079 return ERR_PTR(-ENAMETOOLONG);
2080}
2081
2082/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002083 * NOTE! The user-level library version returns a
2084 * character pointer. The kernel system call just
2085 * returns the length of the buffer filled (which
2086 * includes the ending '\0' character), or a negative
2087 * error value. So libc would do something like
2088 *
2089 * char *getcwd(char * buf, size_t size)
2090 * {
2091 * int retval;
2092 *
2093 * retval = sys_getcwd(buf, size);
2094 * if (retval >= 0)
2095 * return buf;
2096 * errno = -retval;
2097 * return NULL;
2098 * }
2099 */
2100asmlinkage long sys_getcwd(char __user *buf, unsigned long size)
2101{
Linus Torvalds552ce542007-02-13 12:08:18 -08002102 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002103 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002104 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002105
2106 if (!page)
2107 return -ENOMEM;
2108
2109 read_lock(&current->fs->lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002110 pwd = current->fs->pwd;
Ram Pai6092d042008-03-27 13:06:20 +01002111 path_get(&pwd);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002112 root = current->fs->root;
Ram Pai6092d042008-03-27 13:06:20 +01002113 path_get(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 read_unlock(&current->fs->lock);
2115
Linus Torvalds552ce542007-02-13 12:08:18 -08002116 error = -ENOENT;
2117 /* Has the current directory has been unlinked? */
2118 spin_lock(&dcache_lock);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002119 if (IS_ROOT(pwd.dentry) || !d_unhashed(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002120 unsigned long len;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002121 struct path tmp = root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002122 char * cwd;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002123
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002124 cwd = __d_path(&pwd, &tmp, page, PAGE_SIZE);
Linus Torvalds552ce542007-02-13 12:08:18 -08002125 spin_unlock(&dcache_lock);
2126
2127 error = PTR_ERR(cwd);
2128 if (IS_ERR(cwd))
2129 goto out;
2130
2131 error = -ERANGE;
2132 len = PAGE_SIZE + page - cwd;
2133 if (len <= size) {
2134 error = len;
2135 if (copy_to_user(buf, cwd, len))
2136 error = -EFAULT;
2137 }
2138 } else
2139 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002140
2141out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002142 path_put(&pwd);
2143 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002144 free_page((unsigned long) page);
2145 return error;
2146}
2147
2148/*
2149 * Test whether new_dentry is a subdirectory of old_dentry.
2150 *
2151 * Trivially implemented using the dcache structure
2152 */
2153
2154/**
2155 * is_subdir - is new dentry a subdirectory of old_dentry
2156 * @new_dentry: new dentry
2157 * @old_dentry: old dentry
2158 *
2159 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2160 * Returns 0 otherwise.
2161 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2162 */
2163
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002164int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002165{
2166 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002167 unsigned long seq;
2168
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002169 /* FIXME: This is old behavior, needed? Please check callers. */
2170 if (new_dentry == old_dentry)
2171 return 1;
2172
2173 /*
2174 * Need rcu_readlock to protect against the d_parent trashing
2175 * due to d_move
Linus Torvalds1da177e2005-04-16 15:20:36 -07002176 */
2177 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002178 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002179 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002180 seq = read_seqbegin(&rename_lock);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002181 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002182 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002183 else
2184 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002185 } while (read_seqretry(&rename_lock, seq));
2186 rcu_read_unlock();
2187
2188 return result;
2189}
2190
2191void d_genocide(struct dentry *root)
2192{
2193 struct dentry *this_parent = root;
2194 struct list_head *next;
2195
2196 spin_lock(&dcache_lock);
2197repeat:
2198 next = this_parent->d_subdirs.next;
2199resume:
2200 while (next != &this_parent->d_subdirs) {
2201 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002202 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002203 next = tmp->next;
2204 if (d_unhashed(dentry)||!dentry->d_inode)
2205 continue;
2206 if (!list_empty(&dentry->d_subdirs)) {
2207 this_parent = dentry;
2208 goto repeat;
2209 }
2210 atomic_dec(&dentry->d_count);
2211 }
2212 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08002213 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002214 atomic_dec(&this_parent->d_count);
2215 this_parent = this_parent->d_parent;
2216 goto resume;
2217 }
2218 spin_unlock(&dcache_lock);
2219}
2220
2221/**
2222 * find_inode_number - check for dentry with name
2223 * @dir: directory to check
2224 * @name: Name to find.
2225 *
2226 * Check whether a dentry already exists for the given name,
2227 * and return the inode number if it has an inode. Otherwise
2228 * 0 is returned.
2229 *
2230 * This routine is used to post-process directory listings for
2231 * filesystems using synthetic inode numbers, and is necessary
2232 * to keep getcwd() working.
2233 */
2234
2235ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2236{
2237 struct dentry * dentry;
2238 ino_t ino = 0;
2239
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002240 dentry = d_hash_and_lookup(dir, name);
2241 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002242 if (dentry->d_inode)
2243 ino = dentry->d_inode->i_ino;
2244 dput(dentry);
2245 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002246 return ino;
2247}
2248
2249static __initdata unsigned long dhash_entries;
2250static int __init set_dhash_entries(char *str)
2251{
2252 if (!str)
2253 return 0;
2254 dhash_entries = simple_strtoul(str, &str, 0);
2255 return 1;
2256}
2257__setup("dhash_entries=", set_dhash_entries);
2258
2259static void __init dcache_init_early(void)
2260{
2261 int loop;
2262
2263 /* If hashes are distributed across NUMA nodes, defer
2264 * hash allocation until vmalloc space is available.
2265 */
2266 if (hashdist)
2267 return;
2268
2269 dentry_hashtable =
2270 alloc_large_system_hash("Dentry cache",
2271 sizeof(struct hlist_head),
2272 dhash_entries,
2273 13,
2274 HASH_EARLY,
2275 &d_hash_shift,
2276 &d_hash_mask,
2277 0);
2278
2279 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2280 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2281}
2282
Denis Cheng74bf17c2007-10-16 23:26:30 -07002283static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002284{
2285 int loop;
2286
2287 /*
2288 * A constructor could be added for stable state like the lists,
2289 * but it is probably not worth it because of the cache nature
2290 * of the dcache.
2291 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002292 dentry_cache = KMEM_CACHE(dentry,
2293 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002294
Rusty Russell8e1f9362007-07-17 04:03:17 -07002295 register_shrinker(&dcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002296
2297 /* Hash may have been set up in dcache_init_early */
2298 if (!hashdist)
2299 return;
2300
2301 dentry_hashtable =
2302 alloc_large_system_hash("Dentry cache",
2303 sizeof(struct hlist_head),
2304 dhash_entries,
2305 13,
2306 0,
2307 &d_hash_shift,
2308 &d_hash_mask,
2309 0);
2310
2311 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2312 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2313}
2314
2315/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002316struct kmem_cache *names_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
2318/* SLAB cache for file structures */
Christoph Lametere18b8902006-12-06 20:33:20 -08002319struct kmem_cache *filp_cachep __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
2321EXPORT_SYMBOL(d_genocide);
2322
Linus Torvalds1da177e2005-04-16 15:20:36 -07002323void __init vfs_caches_init_early(void)
2324{
2325 dcache_init_early();
2326 inode_init_early();
2327}
2328
2329void __init vfs_caches_init(unsigned long mempages)
2330{
2331 unsigned long reserve;
2332
2333 /* Base hash sizes on available memory, with a reserve equal to
2334 150% of current kernel size */
2335
2336 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2337 mempages -= reserve;
2338
2339 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002340 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002341
2342 filp_cachep = kmem_cache_create("filp", sizeof(struct file), 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002343 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002344
Denis Cheng74bf17c2007-10-16 23:26:30 -07002345 dcache_init();
2346 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002347 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07002348 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002349 bdev_cache_init();
2350 chrdev_init();
2351}
2352
2353EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002354EXPORT_SYMBOL(d_alloc_root);
2355EXPORT_SYMBOL(d_delete);
2356EXPORT_SYMBOL(d_find_alias);
2357EXPORT_SYMBOL(d_instantiate);
2358EXPORT_SYMBOL(d_invalidate);
2359EXPORT_SYMBOL(d_lookup);
2360EXPORT_SYMBOL(d_move);
David Howells770bfad2006-08-22 20:06:07 -04002361EXPORT_SYMBOL_GPL(d_materialise_unique);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002362EXPORT_SYMBOL(d_path);
2363EXPORT_SYMBOL(d_prune_aliases);
2364EXPORT_SYMBOL(d_rehash);
2365EXPORT_SYMBOL(d_splice_alias);
Barry Naujok94035402008-05-21 16:50:46 +10002366EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367EXPORT_SYMBOL(d_validate);
2368EXPORT_SYMBOL(dget_locked);
2369EXPORT_SYMBOL(dput);
2370EXPORT_SYMBOL(find_inode_number);
2371EXPORT_SYMBOL(have_submounts);
2372EXPORT_SYMBOL(names_cachep);
2373EXPORT_SYMBOL(shrink_dcache_parent);
2374EXPORT_SYMBOL(shrink_dcache_sb);