blob: a661247a20d5c095cd137d83c5d13092aae1a4f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/dcache.c
3 *
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
7 */
8
9/*
10 * Notes on the allocation strategy:
11 *
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
John McCutchan7a91bf72005-08-08 13:52:16 -040021#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/slab.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/hash.h>
25#include <linux/cache.h>
26#include <linux/module.h>
27#include <linux/mount.h>
28#include <linux/file.h>
29#include <asm/uaccess.h>
30#include <linux/security.h>
31#include <linux/seqlock.h>
32#include <linux/swap.h>
33#include <linux/bootmem.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Frederic Weisbecker613afbf2009-07-16 15:44:29 +020035#include <linux/hardirq.h>
David Howells07f3f052006-09-30 20:52:18 +020036#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Nick Piggin789680d2011-01-07 17:49:30 +110038/*
39 * Usage:
Nick Piggin23044502011-01-07 17:49:31 +110040 * dcache_hash_lock protects:
41 * - the dcache hash table, s_anon lists
42 * dcache_lru_lock protects:
43 * - the dcache lru lists and counters
44 * d_lock protects:
45 * - d_flags
46 * - d_name
47 * - d_lru
Nick Pigginb7ab39f2011-01-07 17:49:32 +110048 * - d_count
Nick Pigginda502952011-01-07 17:49:33 +110049 * - d_unhashed()
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110050 * - d_parent and d_subdirs
51 * - childrens' d_child and d_parent
Nick Piggin789680d2011-01-07 17:49:30 +110052 *
53 * Ordering:
54 * dcache_lock
55 * dentry->d_lock
Nick Piggin23044502011-01-07 17:49:31 +110056 * dcache_lru_lock
Nick Piggin789680d2011-01-07 17:49:30 +110057 * dcache_hash_lock
58 *
Nick Pigginda502952011-01-07 17:49:33 +110059 * If there is an ancestor relationship:
60 * dentry->d_parent->...->d_parent->d_lock
61 * ...
62 * dentry->d_parent->d_lock
63 * dentry->d_lock
64 *
65 * If no ancestor relationship:
Nick Piggin789680d2011-01-07 17:49:30 +110066 * if (dentry1 < dentry2)
67 * dentry1->d_lock
68 * dentry2->d_lock
69 */
Eric Dumazetfa3536c2006-03-26 01:37:24 -080070int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070071EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
72
Nick Piggin789680d2011-01-07 17:49:30 +110073static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_hash_lock);
Nick Piggin23044502011-01-07 17:49:31 +110074static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
Nick Piggin789680d2011-01-07 17:49:30 +110075__cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -040076__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070077
78EXPORT_SYMBOL(dcache_lock);
79
Christoph Lametere18b8902006-12-06 20:33:20 -080080static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070081
82#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
83
84/*
85 * This is the single most critical data structure when it comes
86 * to the dcache: the hashtable for lookups. Somebody should try
87 * to make this good - I've just made it work.
88 *
89 * This hash-function tries to avoid losing too many bits of hash
90 * information, yet avoid using a prime hash-size or similar.
91 */
92#define D_HASHBITS d_hash_shift
93#define D_HASHMASK d_hash_mask
94
Eric Dumazetfa3536c2006-03-26 01:37:24 -080095static unsigned int d_hash_mask __read_mostly;
96static unsigned int d_hash_shift __read_mostly;
97static struct hlist_head *dentry_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070098
99/* Statistics gathering. */
100struct dentry_stat_t dentry_stat = {
101 .age_limit = 45,
102};
103
Nick Piggin3e880fb2011-01-07 17:49:19 +1100104static DEFINE_PER_CPU(unsigned int, nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400105
106#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Nick Piggin3e880fb2011-01-07 17:49:19 +1100107static int get_nr_dentry(void)
108{
109 int i;
110 int sum = 0;
111 for_each_possible_cpu(i)
112 sum += per_cpu(nr_dentry, i);
113 return sum < 0 ? 0 : sum;
114}
115
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400116int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
117 size_t *lenp, loff_t *ppos)
118{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100119 dentry_stat.nr_dentry = get_nr_dentry();
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400120 return proc_dointvec(table, write, buffer, lenp, ppos);
121}
122#endif
123
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400124static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700125{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400126 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
127
Arjan van de Venfd217f42008-10-21 06:47:33 -0700128 WARN_ON(!list_empty(&dentry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129 if (dname_external(dentry))
130 kfree(dentry->d_name.name);
131 kmem_cache_free(dentry_cache, dentry);
132}
133
134/*
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400135 * no dcache_lock, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 */
137static void d_free(struct dentry *dentry)
138{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100139 BUG_ON(dentry->d_count);
Nick Piggin3e880fb2011-01-07 17:49:19 +1100140 this_cpu_dec(nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 if (dentry->d_op && dentry->d_op->d_release)
142 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400143
Eric Dumazetb3423412006-12-06 20:38:48 -0800144 /* if dentry was never inserted into hash, immediate free is OK */
Akinobu Mitae8462ca2008-02-06 01:37:07 -0800145 if (hlist_unhashed(&dentry->d_hash))
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400146 __d_free(&dentry->d_u.d_rcu);
Eric Dumazetb3423412006-12-06 20:38:48 -0800147 else
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400148 call_rcu(&dentry->d_u.d_rcu, __d_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149}
150
151/*
152 * Release the dentry's inode, using the filesystem
153 * d_iput() operation if defined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800155static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200156 __releases(dentry->d_lock)
157 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158{
159 struct inode *inode = dentry->d_inode;
160 if (inode) {
161 dentry->d_inode = NULL;
162 list_del_init(&dentry->d_alias);
163 spin_unlock(&dentry->d_lock);
164 spin_unlock(&dcache_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700165 if (!inode->i_nlink)
166 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700167 if (dentry->d_op && dentry->d_op->d_iput)
168 dentry->d_op->d_iput(dentry, inode);
169 else
170 iput(inode);
171 } else {
172 spin_unlock(&dentry->d_lock);
173 spin_unlock(&dcache_lock);
174 }
175}
176
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700177/*
Nick Piggin23044502011-01-07 17:49:31 +1100178 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700179 */
180static void dentry_lru_add(struct dentry *dentry)
181{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400182 if (list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100183 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400184 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
185 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100186 dentry_stat.nr_unused++;
Nick Piggin23044502011-01-07 17:49:31 +1100187 spin_unlock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400188 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700189}
190
Nick Piggin23044502011-01-07 17:49:31 +1100191static void __dentry_lru_del(struct dentry *dentry)
192{
193 list_del_init(&dentry->d_lru);
194 dentry->d_sb->s_nr_dentry_unused--;
195 dentry_stat.nr_unused--;
196}
197
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700198static void dentry_lru_del(struct dentry *dentry)
199{
200 if (!list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100201 spin_lock(&dcache_lru_lock);
202 __dentry_lru_del(dentry);
203 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700204 }
205}
206
Christoph Hellwiga4633352010-10-10 05:36:26 -0400207static void dentry_lru_move_tail(struct dentry *dentry)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700208{
Nick Piggin23044502011-01-07 17:49:31 +1100209 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400210 if (list_empty(&dentry->d_lru)) {
211 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
212 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100213 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400214 } else {
215 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700216 }
Nick Piggin23044502011-01-07 17:49:31 +1100217 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700218}
219
Miklos Szeredid52b9082007-05-08 00:23:46 -0700220/**
221 * d_kill - kill dentry and return parent
222 * @dentry: dentry to kill
223 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200224 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700225 *
226 * If this is the root of the dentry tree, return NULL.
Nick Piggin23044502011-01-07 17:49:31 +1100227 *
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100228 * dcache_lock and d_lock and d_parent->d_lock must be held by caller, and
229 * are dropped by d_kill.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700230 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100231static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200232 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100233 __releases(parent->d_lock)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200234 __releases(dcache_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700235{
Miklos Szeredid52b9082007-05-08 00:23:46 -0700236 list_del(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100237 if (parent)
238 spin_unlock(&parent->d_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700239 dentry_iput(dentry);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100240 /*
241 * dentry_iput drops the locks, at which point nobody (except
242 * transient RCU lookups) can reach this dentry.
243 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700244 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900245 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700246}
247
Nick Piggin789680d2011-01-07 17:49:30 +1100248/**
249 * d_drop - drop a dentry
250 * @dentry: dentry to drop
251 *
252 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
253 * be found through a VFS lookup any more. Note that this is different from
254 * deleting the dentry - d_delete will try to mark the dentry negative if
255 * possible, giving a successful _negative_ lookup, while d_drop will
256 * just make the cache lookup fail.
257 *
258 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
259 * reason (NFS timeouts or autofs deletes).
260 *
261 * __d_drop requires dentry->d_lock.
262 */
263void __d_drop(struct dentry *dentry)
264{
265 if (!(dentry->d_flags & DCACHE_UNHASHED)) {
266 dentry->d_flags |= DCACHE_UNHASHED;
267 spin_lock(&dcache_hash_lock);
268 hlist_del_rcu(&dentry->d_hash);
269 spin_unlock(&dcache_hash_lock);
270 }
271}
272EXPORT_SYMBOL(__d_drop);
273
274void d_drop(struct dentry *dentry)
275{
276 spin_lock(&dcache_lock);
277 spin_lock(&dentry->d_lock);
278 __d_drop(dentry);
279 spin_unlock(&dentry->d_lock);
280 spin_unlock(&dcache_lock);
281}
282EXPORT_SYMBOL(d_drop);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/*
285 * This is dput
286 *
287 * This is complicated by the fact that we do not want to put
288 * dentries that are no longer on any hash chain on the unused
289 * list: we'd much rather just get rid of them immediately.
290 *
291 * However, that implies that we have to traverse the dentry
292 * tree upwards to the parents which might _also_ now be
293 * scheduled for deletion (it may have been only waiting for
294 * its last child to go away).
295 *
296 * This tail recursion is done by hand as we don't want to depend
297 * on the compiler to always get this right (gcc generally doesn't).
298 * Real recursion would eat up our stack space.
299 */
300
301/*
302 * dput - release a dentry
303 * @dentry: dentry to release
304 *
305 * Release a dentry. This will drop the usage count and if appropriate
306 * call the dentry unlink method as well as removing it from the queues and
307 * releasing its resources. If the parent dentries were scheduled for release
308 * they too may now get deleted.
309 *
310 * no dcache lock, please.
311 */
312
313void dput(struct dentry *dentry)
314{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100315 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700316 if (!dentry)
317 return;
318
319repeat:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100320 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700322 spin_lock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100323 if (IS_ROOT(dentry))
324 parent = NULL;
325 else
326 parent = dentry->d_parent;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100327 if (dentry->d_count == 1) {
328 if (!spin_trylock(&dcache_lock)) {
329 /*
330 * Something of a livelock possibility we could avoid
331 * by taking dcache_lock and trying again, but we
332 * want to reduce dcache_lock anyway so this will
333 * get improved.
334 */
335 spin_unlock(&dentry->d_lock);
336 goto repeat;
337 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100338 if (parent && !spin_trylock(&parent->d_lock)) {
339 spin_unlock(&dentry->d_lock);
340 spin_unlock(&dcache_lock);
341 goto repeat;
342 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100343 }
344 dentry->d_count--;
345 if (dentry->d_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 spin_unlock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100347 if (parent)
348 spin_unlock(&parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 spin_unlock(&dcache_lock);
350 return;
351 }
352
353 /*
354 * AV: ->d_delete() is _NOT_ allowed to block now.
355 */
356 if (dentry->d_op && dentry->d_op->d_delete) {
357 if (dentry->d_op->d_delete(dentry))
358 goto unhash_it;
359 }
Nick Piggin265ac902010-10-10 05:36:24 -0400360
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 /* Unreachable? Get rid of it */
362 if (d_unhashed(dentry))
363 goto kill_it;
Nick Piggin265ac902010-10-10 05:36:24 -0400364
365 /* Otherwise leave it cached and ensure it's on the LRU */
366 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400367 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400368
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369 spin_unlock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100370 if (parent)
371 spin_unlock(&parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700372 spin_unlock(&dcache_lock);
373 return;
374
375unhash_it:
376 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700377kill_it:
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700378 /* if dentry was on the d_lru list delete it from there */
379 dentry_lru_del(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100380 dentry = d_kill(dentry, parent);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700381 if (dentry)
382 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700383}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700384EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700385
386/**
387 * d_invalidate - invalidate a dentry
388 * @dentry: dentry to invalidate
389 *
390 * Try to invalidate the dentry if it turns out to be
391 * possible. If there are other dentries that can be
392 * reached through this one we can't delete it and we
393 * return -EBUSY. On success we return 0.
394 *
395 * no dcache lock.
396 */
397
398int d_invalidate(struct dentry * dentry)
399{
400 /*
401 * If it's already been dropped, return OK.
402 */
403 spin_lock(&dcache_lock);
Nick Pigginda502952011-01-07 17:49:33 +1100404 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (d_unhashed(dentry)) {
Nick Pigginda502952011-01-07 17:49:33 +1100406 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 spin_unlock(&dcache_lock);
408 return 0;
409 }
410 /*
411 * Check whether to do a partial shrink_dcache
412 * to get rid of unused child entries.
413 */
414 if (!list_empty(&dentry->d_subdirs)) {
Nick Pigginda502952011-01-07 17:49:33 +1100415 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700416 spin_unlock(&dcache_lock);
417 shrink_dcache_parent(dentry);
418 spin_lock(&dcache_lock);
Nick Pigginda502952011-01-07 17:49:33 +1100419 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700420 }
421
422 /*
423 * Somebody else still using it?
424 *
425 * If it's a directory, we can't drop it
426 * for fear of somebody re-populating it
427 * with children (even though dropping it
428 * would make it unreachable from the root,
429 * we might still populate it if it was a
430 * working directory or similar).
431 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100432 if (dentry->d_count > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700433 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
434 spin_unlock(&dentry->d_lock);
435 spin_unlock(&dcache_lock);
436 return -EBUSY;
437 }
438 }
439
440 __d_drop(dentry);
441 spin_unlock(&dentry->d_lock);
442 spin_unlock(&dcache_lock);
443 return 0;
444}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700445EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100447/* This must be called with dcache_lock and d_lock held */
Nick Piggin23044502011-01-07 17:49:31 +1100448static inline struct dentry * __dget_locked_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100450 dentry->d_count++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400451 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700452 return dentry;
453}
454
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100455/* This should be called _only_ with dcache_lock held */
Nick Piggin23044502011-01-07 17:49:31 +1100456static inline struct dentry * __dget_locked(struct dentry *dentry)
457{
Nick Piggin23044502011-01-07 17:49:31 +1100458 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100459 __dget_locked_dlock(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100460 spin_unlock(&dentry->d_lock);
461 return dentry;
462}
463
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100464struct dentry * dget_locked_dlock(struct dentry *dentry)
465{
466 return __dget_locked_dlock(dentry);
467}
468
Linus Torvalds1da177e2005-04-16 15:20:36 -0700469struct dentry * dget_locked(struct dentry *dentry)
470{
471 return __dget_locked(dentry);
472}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700473EXPORT_SYMBOL(dget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700474
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100475struct dentry *dget_parent(struct dentry *dentry)
476{
477 struct dentry *ret;
478
479repeat:
480 spin_lock(&dentry->d_lock);
481 ret = dentry->d_parent;
482 if (!ret)
483 goto out;
484 if (dentry == ret) {
485 ret->d_count++;
486 goto out;
487 }
488 if (!spin_trylock(&ret->d_lock)) {
489 spin_unlock(&dentry->d_lock);
490 cpu_relax();
491 goto repeat;
492 }
493 BUG_ON(!ret->d_count);
494 ret->d_count++;
495 spin_unlock(&ret->d_lock);
496out:
497 spin_unlock(&dentry->d_lock);
498 return ret;
499}
500EXPORT_SYMBOL(dget_parent);
501
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502/**
503 * d_find_alias - grab a hashed alias of inode
504 * @inode: inode in question
505 * @want_discon: flag, used by d_splice_alias, to request
506 * that only a DISCONNECTED alias be returned.
507 *
508 * If inode has a hashed alias, or is a directory and has any alias,
509 * acquire the reference to alias and return it. Otherwise return NULL.
510 * Notice that if inode is a directory there can be only one alias and
511 * it can be unhashed only if it has no children, or if it is the root
512 * of a filesystem.
513 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700514 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700516 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700517 */
Nick Pigginda502952011-01-07 17:49:33 +1100518static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519{
Nick Pigginda502952011-01-07 17:49:33 +1100520 struct dentry *alias, *discon_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700521
Nick Pigginda502952011-01-07 17:49:33 +1100522again:
523 discon_alias = NULL;
524 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
525 spin_lock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700526 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700527 if (IS_ROOT(alias) &&
Nick Pigginda502952011-01-07 17:49:33 +1100528 (alias->d_flags & DCACHE_DISCONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700529 discon_alias = alias;
Nick Pigginda502952011-01-07 17:49:33 +1100530 } else if (!want_discon) {
531 __dget_locked_dlock(alias);
532 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 return alias;
534 }
535 }
Nick Pigginda502952011-01-07 17:49:33 +1100536 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537 }
Nick Pigginda502952011-01-07 17:49:33 +1100538 if (discon_alias) {
539 alias = discon_alias;
540 spin_lock(&alias->d_lock);
541 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
542 if (IS_ROOT(alias) &&
543 (alias->d_flags & DCACHE_DISCONNECTED)) {
544 __dget_locked_dlock(alias);
545 spin_unlock(&alias->d_lock);
546 return alias;
547 }
548 }
549 spin_unlock(&alias->d_lock);
550 goto again;
551 }
552 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553}
554
Nick Pigginda502952011-01-07 17:49:33 +1100555struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700556{
David Howells214fda12006-03-25 03:06:36 -0800557 struct dentry *de = NULL;
558
559 if (!list_empty(&inode->i_dentry)) {
560 spin_lock(&dcache_lock);
561 de = __d_find_alias(inode, 0);
562 spin_unlock(&dcache_lock);
563 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 return de;
565}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700566EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567
568/*
569 * Try to kill dentries associated with this inode.
570 * WARNING: you must own a reference to inode.
571 */
572void d_prune_aliases(struct inode *inode)
573{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700574 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575restart:
576 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700577 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100579 if (!dentry->d_count) {
Nick Piggin23044502011-01-07 17:49:31 +1100580 __dget_locked_dlock(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700581 __d_drop(dentry);
582 spin_unlock(&dentry->d_lock);
583 spin_unlock(&dcache_lock);
584 dput(dentry);
585 goto restart;
586 }
587 spin_unlock(&dentry->d_lock);
588 }
589 spin_unlock(&dcache_lock);
590}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700591EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700592
593/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700594 * Throw away a dentry - free the inode, dput the parent. This requires that
595 * the LRU list has already been removed.
596 *
Miklos Szeredi85864e12007-10-16 23:27:09 -0700597 * Try to prune ancestors as well. This is necessary to prevent
598 * quadratic behavior of shrink_dcache_parent(), but is also expected
599 * to be beneficial in reducing dentry cache fragmentation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100601static void prune_one_dentry(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200602 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100603 __releases(parent->d_lock)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200604 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700606 __d_drop(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100607 dentry = d_kill(dentry, parent);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700608
609 /*
610 * Prune ancestors. Locking is simpler than in dput(),
611 * because dcache_lock needs to be taken anyway.
612 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700613 while (dentry) {
Nick Piggin23044502011-01-07 17:49:31 +1100614 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100615again:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100616 spin_lock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100617 if (IS_ROOT(dentry))
618 parent = NULL;
619 else
620 parent = dentry->d_parent;
621 if (parent && !spin_trylock(&parent->d_lock)) {
622 spin_unlock(&dentry->d_lock);
623 goto again;
624 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100625 dentry->d_count--;
626 if (dentry->d_count) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100627 if (parent)
628 spin_unlock(&parent->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100629 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100630 spin_unlock(&dcache_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700631 return;
Nick Piggin23044502011-01-07 17:49:31 +1100632 }
Miklos Szeredid52b9082007-05-08 00:23:46 -0700633
Christoph Hellwiga4633352010-10-10 05:36:26 -0400634 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700635 __d_drop(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100636 dentry = d_kill(dentry, parent);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700637 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700638}
639
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400640static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700641{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700642 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700643
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400644 while (!list_empty(list)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100645 struct dentry *parent;
646
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400647 dentry = list_entry(list->prev, struct dentry, d_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100648
649 if (!spin_trylock(&dentry->d_lock)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100650relock:
Nick Piggin23044502011-01-07 17:49:31 +1100651 spin_unlock(&dcache_lru_lock);
652 cpu_relax();
653 spin_lock(&dcache_lru_lock);
654 continue;
655 }
656
Linus Torvalds1da177e2005-04-16 15:20:36 -0700657 /*
658 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700659 * the LRU because of laziness during lookup. Do not free
660 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700661 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100662 if (dentry->d_count) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100663 __dentry_lru_del(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700664 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665 continue;
666 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100667 if (IS_ROOT(dentry))
668 parent = NULL;
669 else
670 parent = dentry->d_parent;
671 if (parent && !spin_trylock(&parent->d_lock)) {
672 spin_unlock(&dentry->d_lock);
673 goto relock;
674 }
675 __dentry_lru_del(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100676 spin_unlock(&dcache_lru_lock);
677
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100678 prune_one_dentry(dentry, parent);
Nick Piggin23044502011-01-07 17:49:31 +1100679 /* dcache_lock and dentry->d_lock dropped */
680 spin_lock(&dcache_lock);
681 spin_lock(&dcache_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700682 }
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400683}
684
685/**
686 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
687 * @sb: superblock to shrink dentry LRU.
688 * @count: number of entries to prune
689 * @flags: flags to control the dentry processing
690 *
691 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
692 */
693static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
694{
695 /* called from prune_dcache() and shrink_dcache_parent() */
696 struct dentry *dentry;
697 LIST_HEAD(referenced);
698 LIST_HEAD(tmp);
699 int cnt = *count;
700
701 spin_lock(&dcache_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100702relock:
703 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400704 while (!list_empty(&sb->s_dentry_lru)) {
705 dentry = list_entry(sb->s_dentry_lru.prev,
706 struct dentry, d_lru);
707 BUG_ON(dentry->d_sb != sb);
708
Nick Piggin23044502011-01-07 17:49:31 +1100709 if (!spin_trylock(&dentry->d_lock)) {
710 spin_unlock(&dcache_lru_lock);
711 cpu_relax();
712 goto relock;
713 }
714
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400715 /*
716 * If we are honouring the DCACHE_REFERENCED flag and the
717 * dentry has this flag set, don't free it. Clear the flag
718 * and put it back on the LRU.
719 */
Nick Piggin23044502011-01-07 17:49:31 +1100720 if (flags & DCACHE_REFERENCED &&
721 dentry->d_flags & DCACHE_REFERENCED) {
722 dentry->d_flags &= ~DCACHE_REFERENCED;
723 list_move(&dentry->d_lru, &referenced);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400724 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100725 } else {
726 list_move_tail(&dentry->d_lru, &tmp);
727 spin_unlock(&dentry->d_lock);
728 if (!--cnt)
729 break;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400730 }
Nick Piggin23044502011-01-07 17:49:31 +1100731 /* XXX: re-add cond_resched_lock when dcache_lock goes away */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400732 }
733
734 *count = cnt;
735 shrink_dentry_list(&tmp);
736
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700737 if (!list_empty(&referenced))
738 list_splice(&referenced, &sb->s_dentry_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100739 spin_unlock(&dcache_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700740 spin_unlock(&dcache_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400741
Linus Torvalds1da177e2005-04-16 15:20:36 -0700742}
743
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700744/**
745 * prune_dcache - shrink the dcache
746 * @count: number of entries to try to free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700747 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700748 * Shrink the dcache. This is done when we need more memory, or simply when we
749 * need to unmount something (at which point we need to unuse all dentries).
750 *
751 * This function may fail to free any resources if all the dentries are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700752 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700753static void prune_dcache(int count)
754{
Al Virodca33252010-07-25 02:31:46 +0400755 struct super_block *sb, *p = NULL;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700756 int w_count;
Nick Piggin86c87492011-01-07 17:49:18 +1100757 int unused = dentry_stat.nr_unused;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700758 int prune_ratio;
759 int pruned;
760
761 if (unused == 0 || count == 0)
762 return;
763 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700764 if (count >= unused)
765 prune_ratio = 1;
766 else
767 prune_ratio = unused / count;
768 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400769 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400770 if (list_empty(&sb->s_instances))
771 continue;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700772 if (sb->s_nr_dentry_unused == 0)
773 continue;
774 sb->s_count++;
775 /* Now, we reclaim unused dentrins with fairness.
776 * We reclaim them same percentage from each superblock.
777 * We calculate number of dentries to scan on this sb
778 * as follows, but the implementation is arranged to avoid
779 * overflows:
780 * number of dentries to scan on this sb =
781 * count * (number of dentries on this sb /
782 * number of dentries in the machine)
783 */
784 spin_unlock(&sb_lock);
785 if (prune_ratio != 1)
786 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
787 else
788 w_count = sb->s_nr_dentry_unused;
789 pruned = w_count;
790 /*
791 * We need to be sure this filesystem isn't being unmounted,
792 * otherwise we could race with generic_shutdown_super(), and
793 * end up holding a reference to an inode while the filesystem
794 * is unmounted. So we try to get s_umount, and make sure
795 * s_root isn't NULL.
796 */
797 if (down_read_trylock(&sb->s_umount)) {
798 if ((sb->s_root != NULL) &&
799 (!list_empty(&sb->s_dentry_lru))) {
800 spin_unlock(&dcache_lock);
801 __shrink_dcache_sb(sb, &w_count,
802 DCACHE_REFERENCED);
803 pruned -= w_count;
804 spin_lock(&dcache_lock);
805 }
806 up_read(&sb->s_umount);
807 }
808 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400809 if (p)
810 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700811 count -= pruned;
Al Virodca33252010-07-25 02:31:46 +0400812 p = sb;
Al Viro79893c12010-03-22 20:27:55 -0400813 /* more work left to do? */
814 if (count <= 0)
815 break;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700816 }
Al Virodca33252010-07-25 02:31:46 +0400817 if (p)
818 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700819 spin_unlock(&sb_lock);
820 spin_unlock(&dcache_lock);
821}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700822
823/**
824 * shrink_dcache_sb - shrink dcache for a superblock
825 * @sb: superblock
826 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400827 * Shrink the dcache for the specified super block. This is used to free
828 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700829 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400830void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700831{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400832 LIST_HEAD(tmp);
833
834 spin_lock(&dcache_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100835 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400836 while (!list_empty(&sb->s_dentry_lru)) {
837 list_splice_init(&sb->s_dentry_lru, &tmp);
838 shrink_dentry_list(&tmp);
839 }
Nick Piggin23044502011-01-07 17:49:31 +1100840 spin_unlock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400841 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700842}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700843EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700844
845/*
David Howellsc636ebd2006-10-11 01:22:19 -0700846 * destroy a single subtree of dentries for unmount
847 * - see the comments on shrink_dcache_for_umount() for a description of the
848 * locking
849 */
850static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
851{
852 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700853 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700854
855 BUG_ON(!IS_ROOT(dentry));
856
857 /* detach this root from the system */
858 spin_lock(&dcache_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100859 spin_lock(&dentry->d_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400860 dentry_lru_del(dentry);
David Howellsc636ebd2006-10-11 01:22:19 -0700861 __d_drop(dentry);
Nick Pigginda502952011-01-07 17:49:33 +1100862 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700863 spin_unlock(&dcache_lock);
864
865 for (;;) {
866 /* descend to the first leaf in the current subtree */
867 while (!list_empty(&dentry->d_subdirs)) {
868 struct dentry *loop;
869
870 /* this is a branch with children - detach all of them
871 * from the system in one go */
872 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100873 spin_lock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700874 list_for_each_entry(loop, &dentry->d_subdirs,
875 d_u.d_child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100876 spin_lock_nested(&loop->d_lock,
877 DENTRY_D_LOCK_NESTED);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400878 dentry_lru_del(loop);
David Howellsc636ebd2006-10-11 01:22:19 -0700879 __d_drop(loop);
Nick Pigginda502952011-01-07 17:49:33 +1100880 spin_unlock(&loop->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700881 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100882 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700883 spin_unlock(&dcache_lock);
884
885 /* move to the first child */
886 dentry = list_entry(dentry->d_subdirs.next,
887 struct dentry, d_u.d_child);
888 }
889
890 /* consume the dentries from this leaf up through its parents
891 * until we find one with children or run out altogether */
892 do {
893 struct inode *inode;
894
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100895 if (dentry->d_count != 0) {
David Howellsc636ebd2006-10-11 01:22:19 -0700896 printk(KERN_ERR
897 "BUG: Dentry %p{i=%lx,n=%s}"
898 " still in use (%d)"
899 " [unmount of %s %s]\n",
900 dentry,
901 dentry->d_inode ?
902 dentry->d_inode->i_ino : 0UL,
903 dentry->d_name.name,
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100904 dentry->d_count,
David Howellsc636ebd2006-10-11 01:22:19 -0700905 dentry->d_sb->s_type->name,
906 dentry->d_sb->s_id);
907 BUG();
908 }
909
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100910 if (IS_ROOT(dentry)) {
David Howellsc636ebd2006-10-11 01:22:19 -0700911 parent = NULL;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100912 list_del(&dentry->d_u.d_child);
913 } else {
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900914 parent = dentry->d_parent;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100915 spin_lock(&parent->d_lock);
916 parent->d_count--;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100917 list_del(&dentry->d_u.d_child);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100918 spin_unlock(&parent->d_lock);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900919 }
David Howellsc636ebd2006-10-11 01:22:19 -0700920
David Howellsf8713572006-10-28 10:38:46 -0700921 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700922
923 inode = dentry->d_inode;
924 if (inode) {
925 dentry->d_inode = NULL;
926 list_del_init(&dentry->d_alias);
927 if (dentry->d_op && dentry->d_op->d_iput)
928 dentry->d_op->d_iput(dentry, inode);
929 else
930 iput(inode);
931 }
932
933 d_free(dentry);
934
935 /* finished when we fall off the top of the tree,
936 * otherwise we ascend to the parent and move to the
937 * next sibling if there is one */
938 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400939 return;
David Howellsc636ebd2006-10-11 01:22:19 -0700940 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700941 } while (list_empty(&dentry->d_subdirs));
942
943 dentry = list_entry(dentry->d_subdirs.next,
944 struct dentry, d_u.d_child);
945 }
946}
947
948/*
949 * destroy the dentries attached to a superblock on unmounting
950 * - we don't need to use dentry->d_lock, and only need dcache_lock when
951 * removing the dentry from the system lists and hashes because:
952 * - the superblock is detached from all mountings and open files, so the
953 * dentry trees will not be rearranged by the VFS
954 * - s_umount is write-locked, so the memory pressure shrinker will ignore
955 * any dentries belonging to this superblock that it comes across
956 * - the filesystem itself is no longer permitted to rearrange the dentries
957 * in this superblock
958 */
959void shrink_dcache_for_umount(struct super_block *sb)
960{
961 struct dentry *dentry;
962
963 if (down_read_trylock(&sb->s_umount))
964 BUG();
965
966 dentry = sb->s_root;
967 sb->s_root = NULL;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100968 spin_lock(&dentry->d_lock);
969 dentry->d_count--;
970 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700971 shrink_dcache_for_umount_subtree(dentry);
972
973 while (!hlist_empty(&sb->s_anon)) {
974 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
975 shrink_dcache_for_umount_subtree(dentry);
976 }
977}
978
979/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700980 * Search for at least 1 mount point in the dentry's subdirs.
981 * We descend to the next level whenever the d_subdirs
982 * list is non-empty and continue searching.
983 */
984
985/**
986 * have_submounts - check for mounts over a dentry
987 * @parent: dentry to check.
988 *
989 * Return true if the parent or its subdirectories contain
990 * a mount point
991 */
992
993int have_submounts(struct dentry *parent)
994{
995 struct dentry *this_parent = parent;
996 struct list_head *next;
997
998 spin_lock(&dcache_lock);
999 if (d_mountpoint(parent))
1000 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001001 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001002repeat:
1003 next = this_parent->d_subdirs.next;
1004resume:
1005 while (next != &this_parent->d_subdirs) {
1006 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001007 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001009
1010 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001011 /* Have we found a mount point ? */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001012 if (d_mountpoint(dentry)) {
1013 spin_unlock(&dentry->d_lock);
1014 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001016 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001017 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001018 spin_unlock(&this_parent->d_lock);
1019 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001020 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001021 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022 goto repeat;
1023 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001024 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 }
1026 /*
1027 * All done at this level ... ascend and resume the search.
1028 */
1029 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001030 next = this_parent->d_u.d_child.next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001031 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 this_parent = this_parent->d_parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001033 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001034 goto resume;
1035 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001036 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001037 spin_unlock(&dcache_lock);
1038 return 0; /* No mount points found in tree */
1039positive:
1040 spin_unlock(&dcache_lock);
1041 return 1;
1042}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001043EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001044
1045/*
1046 * Search the dentry child list for the specified parent,
1047 * and move any unused dentries to the end of the unused
1048 * list for prune_dcache(). We descend to the next level
1049 * whenever the d_subdirs list is non-empty and continue
1050 * searching.
1051 *
1052 * It returns zero iff there are no unused children,
1053 * otherwise it returns the number of children moved to
1054 * the end of the unused list. This may not be the total
1055 * number of unused children, because select_parent can
1056 * drop the lock and return early due to latency
1057 * constraints.
1058 */
1059static int select_parent(struct dentry * parent)
1060{
1061 struct dentry *this_parent = parent;
1062 struct list_head *next;
1063 int found = 0;
1064
1065 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001066 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001067repeat:
1068 next = this_parent->d_subdirs.next;
1069resume:
1070 while (next != &this_parent->d_subdirs) {
1071 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001072 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001073 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001074 BUG_ON(this_parent == dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001076 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Nick Piggin23044502011-01-07 17:49:31 +11001077
Linus Torvalds1da177e2005-04-16 15:20:36 -07001078 /*
1079 * move only zero ref count dentries to the end
1080 * of the unused list for prune_dcache
1081 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001082 if (!dentry->d_count) {
Christoph Hellwiga4633352010-10-10 05:36:26 -04001083 dentry_lru_move_tail(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 found++;
Christoph Hellwiga4633352010-10-10 05:36:26 -04001085 } else {
1086 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 }
1088
1089 /*
1090 * We can return to the caller if we have found some (this
1091 * ensures forward progress). We'll be coming back to find
1092 * the rest.
1093 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001094 if (found && need_resched()) {
1095 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001096 goto out;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001097 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099 /*
1100 * Descend a level if the d_subdirs list is non-empty.
1101 */
1102 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001103 spin_unlock(&this_parent->d_lock);
1104 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001105 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001106 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001107 goto repeat;
1108 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001109
1110 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001111 }
1112 /*
1113 * All done at this level ... ascend and resume the search.
1114 */
1115 if (this_parent != parent) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001116 struct dentry *tmp;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001117 next = this_parent->d_u.d_child.next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001118 tmp = this_parent->d_parent;
1119 spin_unlock(&this_parent->d_lock);
1120 BUG_ON(tmp == this_parent);
1121 this_parent = tmp;
1122 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 goto resume;
1124 }
1125out:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001126 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001127 spin_unlock(&dcache_lock);
1128 return found;
1129}
1130
1131/**
1132 * shrink_dcache_parent - prune dcache
1133 * @parent: parent of entries to prune
1134 *
1135 * Prune the dcache to remove unused children of the parent dentry.
1136 */
1137
1138void shrink_dcache_parent(struct dentry * parent)
1139{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001140 struct super_block *sb = parent->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001141 int found;
1142
1143 while ((found = select_parent(parent)) != 0)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001144 __shrink_dcache_sb(sb, &found, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001146EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001147
Linus Torvalds1da177e2005-04-16 15:20:36 -07001148/*
1149 * Scan `nr' dentries and return the number which remain.
1150 *
1151 * We need to avoid reentering the filesystem if the caller is performing a
1152 * GFP_NOFS allocation attempt. One example deadlock is:
1153 *
1154 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1155 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1156 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1157 *
1158 * In this case we return -1 to tell the caller that we baled.
1159 */
Dave Chinner7f8275d2010-07-19 14:56:17 +10001160static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001161{
1162 if (nr) {
1163 if (!(gfp_mask & __GFP_FS))
1164 return -1;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001165 prune_dcache(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001166 }
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001167
Nick Piggin86c87492011-01-07 17:49:18 +11001168 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169}
1170
Rusty Russell8e1f9362007-07-17 04:03:17 -07001171static struct shrinker dcache_shrinker = {
1172 .shrink = shrink_dcache_memory,
1173 .seeks = DEFAULT_SEEKS,
1174};
1175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176/**
1177 * d_alloc - allocate a dcache entry
1178 * @parent: parent of entry to allocate
1179 * @name: qstr of the name
1180 *
1181 * Allocates a dentry. It returns %NULL if there is insufficient memory
1182 * available. On a success the dentry is returned. The name passed in is
1183 * copied and the copy passed in may be reused after this call.
1184 */
1185
1186struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1187{
1188 struct dentry *dentry;
1189 char *dname;
1190
Mel Gormane12ba742007-10-16 01:25:52 -07001191 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001192 if (!dentry)
1193 return NULL;
1194
1195 if (name->len > DNAME_INLINE_LEN-1) {
1196 dname = kmalloc(name->len + 1, GFP_KERNEL);
1197 if (!dname) {
1198 kmem_cache_free(dentry_cache, dentry);
1199 return NULL;
1200 }
1201 } else {
1202 dname = dentry->d_iname;
1203 }
1204 dentry->d_name.name = dname;
1205
1206 dentry->d_name.len = name->len;
1207 dentry->d_name.hash = name->hash;
1208 memcpy(dname, name->name, name->len);
1209 dname[name->len] = 0;
1210
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001211 dentry->d_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001212 dentry->d_flags = DCACHE_UNHASHED;
1213 spin_lock_init(&dentry->d_lock);
1214 dentry->d_inode = NULL;
1215 dentry->d_parent = NULL;
1216 dentry->d_sb = NULL;
1217 dentry->d_op = NULL;
1218 dentry->d_fsdata = NULL;
1219 dentry->d_mounted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 INIT_HLIST_NODE(&dentry->d_hash);
1221 INIT_LIST_HEAD(&dentry->d_lru);
1222 INIT_LIST_HEAD(&dentry->d_subdirs);
1223 INIT_LIST_HEAD(&dentry->d_alias);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001224 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001225
1226 if (parent) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001227 spin_lock(&dcache_lock);
1228 spin_lock(&parent->d_lock);
1229 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1230 dentry->d_parent = dget_dlock(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001231 dentry->d_sb = parent->d_sb;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001232 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001233 spin_unlock(&dentry->d_lock);
1234 spin_unlock(&parent->d_lock);
1235 spin_unlock(&dcache_lock);
1236 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237
Nick Piggin3e880fb2011-01-07 17:49:19 +11001238 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001239
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 return dentry;
1241}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001242EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001243
1244struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1245{
1246 struct qstr q;
1247
1248 q.name = name;
1249 q.len = strlen(name);
1250 q.hash = full_name_hash(q.name, q.len);
1251 return d_alloc(parent, &q);
1252}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001253EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001255/* the caller must hold dcache_lock */
1256static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1257{
1258 if (inode)
1259 list_add(&dentry->d_alias, &inode->i_dentry);
1260 dentry->d_inode = inode;
1261 fsnotify_d_instantiate(dentry, inode);
1262}
1263
Linus Torvalds1da177e2005-04-16 15:20:36 -07001264/**
1265 * d_instantiate - fill in inode information for a dentry
1266 * @entry: dentry to complete
1267 * @inode: inode to attach to this dentry
1268 *
1269 * Fill in inode information in the entry.
1270 *
1271 * This turns negative dentries into productive full members
1272 * of society.
1273 *
1274 * NOTE! This assumes that the inode count has been incremented
1275 * (or otherwise set) by the caller to indicate that it is now
1276 * in use by the dcache.
1277 */
1278
1279void d_instantiate(struct dentry *entry, struct inode * inode)
1280{
Eric Sesterhenn28133c72006-03-26 18:25:39 +02001281 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282 spin_lock(&dcache_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001283 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001284 spin_unlock(&dcache_lock);
1285 security_d_instantiate(entry, inode);
1286}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001287EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001288
1289/**
1290 * d_instantiate_unique - instantiate a non-aliased dentry
1291 * @entry: dentry to instantiate
1292 * @inode: inode to attach to this dentry
1293 *
1294 * Fill in inode information in the entry. On success, it returns NULL.
1295 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001296 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 *
1298 * Note that in order to avoid conflicts with rename() etc, the caller
1299 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001300 *
1301 * This also assumes that the inode count has been incremented
1302 * (or otherwise set) by the caller to indicate that it is now
1303 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 */
David Howells770bfad2006-08-22 20:06:07 -04001305static struct dentry *__d_instantiate_unique(struct dentry *entry,
1306 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001307{
1308 struct dentry *alias;
1309 int len = entry->d_name.len;
1310 const char *name = entry->d_name.name;
1311 unsigned int hash = entry->d_name.hash;
1312
David Howells770bfad2006-08-22 20:06:07 -04001313 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001314 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001315 return NULL;
1316 }
1317
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1319 struct qstr *qstr = &alias->d_name;
1320
1321 if (qstr->hash != hash)
1322 continue;
1323 if (alias->d_parent != entry->d_parent)
1324 continue;
1325 if (qstr->len != len)
1326 continue;
1327 if (memcmp(qstr->name, name, len))
1328 continue;
1329 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 return alias;
1331 }
David Howells770bfad2006-08-22 20:06:07 -04001332
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001333 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001334 return NULL;
1335}
David Howells770bfad2006-08-22 20:06:07 -04001336
1337struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1338{
1339 struct dentry *result;
1340
1341 BUG_ON(!list_empty(&entry->d_alias));
1342
1343 spin_lock(&dcache_lock);
1344 result = __d_instantiate_unique(entry, inode);
1345 spin_unlock(&dcache_lock);
1346
1347 if (!result) {
1348 security_d_instantiate(entry, inode);
1349 return NULL;
1350 }
1351
1352 BUG_ON(!d_unhashed(result));
1353 iput(inode);
1354 return result;
1355}
1356
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357EXPORT_SYMBOL(d_instantiate_unique);
1358
1359/**
1360 * d_alloc_root - allocate root dentry
1361 * @root_inode: inode to allocate the root for
1362 *
1363 * Allocate a root ("/") dentry for the inode given. The inode is
1364 * instantiated and returned. %NULL is returned if there is insufficient
1365 * memory or the inode passed is %NULL.
1366 */
1367
1368struct dentry * d_alloc_root(struct inode * root_inode)
1369{
1370 struct dentry *res = NULL;
1371
1372 if (root_inode) {
1373 static const struct qstr name = { .name = "/", .len = 1 };
1374
1375 res = d_alloc(NULL, &name);
1376 if (res) {
1377 res->d_sb = root_inode->i_sb;
1378 res->d_parent = res;
1379 d_instantiate(res, root_inode);
1380 }
1381 }
1382 return res;
1383}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001384EXPORT_SYMBOL(d_alloc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001385
1386static inline struct hlist_head *d_hash(struct dentry *parent,
1387 unsigned long hash)
1388{
1389 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1390 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1391 return dentry_hashtable + (hash & D_HASHMASK);
1392}
1393
1394/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001395 * d_obtain_alias - find or allocate a dentry for a given inode
1396 * @inode: inode to allocate the dentry for
1397 *
1398 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1399 * similar open by handle operations. The returned dentry may be anonymous,
1400 * or may have a full name (if the inode was already in the cache).
1401 *
1402 * When called on a directory inode, we must ensure that the inode only ever
1403 * has one dentry. If a dentry is found, that is returned instead of
1404 * allocating a new one.
1405 *
1406 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001407 * to the dentry. In case of an error the reference on the inode is released.
1408 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1409 * be passed in and will be the error will be propagate to the return value,
1410 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001411 */
1412struct dentry *d_obtain_alias(struct inode *inode)
1413{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001414 static const struct qstr anonstring = { .name = "" };
1415 struct dentry *tmp;
1416 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001417
1418 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001419 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001420 if (IS_ERR(inode))
1421 return ERR_CAST(inode);
1422
Christoph Hellwig9308a612008-08-11 15:49:12 +02001423 res = d_find_alias(inode);
1424 if (res)
1425 goto out_iput;
1426
1427 tmp = d_alloc(NULL, &anonstring);
1428 if (!tmp) {
1429 res = ERR_PTR(-ENOMEM);
1430 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001431 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001432 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1433
1434 spin_lock(&dcache_lock);
1435 res = __d_find_alias(inode, 0);
1436 if (res) {
1437 spin_unlock(&dcache_lock);
1438 dput(tmp);
1439 goto out_iput;
1440 }
1441
1442 /* attach a disconnected dentry */
1443 spin_lock(&tmp->d_lock);
1444 tmp->d_sb = inode->i_sb;
1445 tmp->d_inode = inode;
1446 tmp->d_flags |= DCACHE_DISCONNECTED;
1447 tmp->d_flags &= ~DCACHE_UNHASHED;
1448 list_add(&tmp->d_alias, &inode->i_dentry);
Nick Piggin789680d2011-01-07 17:49:30 +11001449 spin_lock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001450 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
Nick Piggin789680d2011-01-07 17:49:30 +11001451 spin_unlock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001452 spin_unlock(&tmp->d_lock);
1453
1454 spin_unlock(&dcache_lock);
1455 return tmp;
1456
1457 out_iput:
1458 iput(inode);
1459 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001460}
Benny Halevyadc48722009-02-27 14:02:59 -08001461EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001462
1463/**
1464 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1465 * @inode: the inode which may have a disconnected dentry
1466 * @dentry: a negative dentry which we want to point to the inode.
1467 *
1468 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1469 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1470 * and return it, else simply d_add the inode to the dentry and return NULL.
1471 *
1472 * This is needed in the lookup routine of any filesystem that is exportable
1473 * (via knfsd) so that we can build dcache paths to directories effectively.
1474 *
1475 * If a dentry was found and moved, then it is returned. Otherwise NULL
1476 * is returned. This matches the expected return value of ->lookup.
1477 *
1478 */
1479struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1480{
1481 struct dentry *new = NULL;
1482
NeilBrown21c0d8f2006-10-04 02:16:16 -07001483 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001484 spin_lock(&dcache_lock);
1485 new = __d_find_alias(inode, 1);
1486 if (new) {
1487 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1488 spin_unlock(&dcache_lock);
1489 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001490 d_move(new, dentry);
1491 iput(inode);
1492 } else {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001493 /* already taking dcache_lock, so d_add() by hand */
1494 __d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001495 spin_unlock(&dcache_lock);
1496 security_d_instantiate(dentry, inode);
1497 d_rehash(dentry);
1498 }
1499 } else
1500 d_add(dentry, inode);
1501 return new;
1502}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001503EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
Barry Naujok94035402008-05-21 16:50:46 +10001505/**
1506 * d_add_ci - lookup or allocate new dentry with case-exact name
1507 * @inode: the inode case-insensitive lookup has found
1508 * @dentry: the negative dentry that was passed to the parent's lookup func
1509 * @name: the case-exact name to be associated with the returned dentry
1510 *
1511 * This is to avoid filling the dcache with case-insensitive names to the
1512 * same inode, only the actual correct case is stored in the dcache for
1513 * case-insensitive filesystems.
1514 *
1515 * For a case-insensitive lookup match and if the the case-exact dentry
1516 * already exists in in the dcache, use it and return it.
1517 *
1518 * If no entry exists with the exact case name, allocate new dentry with
1519 * the exact case, and return the spliced entry.
1520 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001521struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001522 struct qstr *name)
1523{
1524 int error;
1525 struct dentry *found;
1526 struct dentry *new;
1527
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001528 /*
1529 * First check if a dentry matching the name already exists,
1530 * if not go ahead and create it now.
1531 */
Barry Naujok94035402008-05-21 16:50:46 +10001532 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001533 if (!found) {
1534 new = d_alloc(dentry->d_parent, name);
1535 if (!new) {
1536 error = -ENOMEM;
1537 goto err_out;
1538 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001539
Barry Naujok94035402008-05-21 16:50:46 +10001540 found = d_splice_alias(inode, new);
1541 if (found) {
1542 dput(new);
1543 return found;
1544 }
1545 return new;
1546 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001547
1548 /*
1549 * If a matching dentry exists, and it's not negative use it.
1550 *
1551 * Decrement the reference count to balance the iget() done
1552 * earlier on.
1553 */
Barry Naujok94035402008-05-21 16:50:46 +10001554 if (found->d_inode) {
1555 if (unlikely(found->d_inode != inode)) {
1556 /* This can't happen because bad inodes are unhashed. */
1557 BUG_ON(!is_bad_inode(inode));
1558 BUG_ON(!is_bad_inode(found->d_inode));
1559 }
Barry Naujok94035402008-05-21 16:50:46 +10001560 iput(inode);
1561 return found;
1562 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001563
Barry Naujok94035402008-05-21 16:50:46 +10001564 /*
1565 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001566 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001567 */
Barry Naujok94035402008-05-21 16:50:46 +10001568 spin_lock(&dcache_lock);
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001569 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001570 __d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001571 spin_unlock(&dcache_lock);
1572 security_d_instantiate(found, inode);
1573 return found;
1574 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001575
Barry Naujok94035402008-05-21 16:50:46 +10001576 /*
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001577 * In case a directory already has a (disconnected) entry grab a
1578 * reference to it, move it in place and use it.
Barry Naujok94035402008-05-21 16:50:46 +10001579 */
1580 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1581 dget_locked(new);
1582 spin_unlock(&dcache_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001583 security_d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001584 d_move(new, found);
Barry Naujok94035402008-05-21 16:50:46 +10001585 iput(inode);
Barry Naujok94035402008-05-21 16:50:46 +10001586 dput(found);
Barry Naujok94035402008-05-21 16:50:46 +10001587 return new;
1588
1589err_out:
1590 iput(inode);
1591 return ERR_PTR(error);
1592}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001593EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
1595/**
1596 * d_lookup - search for a dentry
1597 * @parent: parent dentry
1598 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001599 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001600 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001601 * d_lookup searches the children of the parent dentry for the name in
1602 * question. If the dentry is found its reference count is incremented and the
1603 * dentry is returned. The caller must use dput to free the entry when it has
1604 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001605 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001606struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1607{
1608 struct dentry * dentry = NULL;
1609 unsigned long seq;
1610
1611 do {
1612 seq = read_seqbegin(&rename_lock);
1613 dentry = __d_lookup(parent, name);
1614 if (dentry)
1615 break;
1616 } while (read_seqretry(&rename_lock, seq));
1617 return dentry;
1618}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001619EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001620
Nick Pigginb04f7842010-08-18 04:37:34 +10001621/*
1622 * __d_lookup - search for a dentry (racy)
1623 * @parent: parent dentry
1624 * @name: qstr of name we wish to find
1625 * Returns: dentry, or NULL
1626 *
1627 * __d_lookup is like d_lookup, however it may (rarely) return a
1628 * false-negative result due to unrelated rename activity.
1629 *
1630 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1631 * however it must be used carefully, eg. with a following d_lookup in
1632 * the case of failure.
1633 *
1634 * __d_lookup callers must be commented.
1635 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001636struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1637{
1638 unsigned int len = name->len;
1639 unsigned int hash = name->hash;
1640 const unsigned char *str = name->name;
1641 struct hlist_head *head = d_hash(parent,hash);
1642 struct dentry *found = NULL;
1643 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001644 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001645
Nick Pigginb04f7842010-08-18 04:37:34 +10001646 /*
1647 * The hash list is protected using RCU.
1648 *
1649 * Take d_lock when comparing a candidate dentry, to avoid races
1650 * with d_move().
1651 *
1652 * It is possible that concurrent renames can mess up our list
1653 * walk here and result in missing our dentry, resulting in the
1654 * false-negative result. d_lookup() protects against concurrent
1655 * renames using rename_lock seqlock.
1656 *
1657 * See Documentation/vfs/dcache-locking.txt for more details.
1658 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001659 rcu_read_lock();
1660
Paul E. McKenney665a7582005-11-07 00:59:17 -08001661 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001662 struct qstr *qstr;
1663
Linus Torvalds1da177e2005-04-16 15:20:36 -07001664 if (dentry->d_name.hash != hash)
1665 continue;
1666 if (dentry->d_parent != parent)
1667 continue;
1668
1669 spin_lock(&dentry->d_lock);
1670
1671 /*
1672 * Recheck the dentry after taking the lock - d_move may have
Nick Pigginb04f7842010-08-18 04:37:34 +10001673 * changed things. Don't bother checking the hash because
1674 * we're about to compare the whole name anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 */
1676 if (dentry->d_parent != parent)
1677 goto next;
1678
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001679 /* non-existing due to RCU? */
1680 if (d_unhashed(dentry))
1681 goto next;
1682
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683 /*
1684 * It is safe to compare names since d_move() cannot
1685 * change the qstr (protected by d_lock).
1686 */
1687 qstr = &dentry->d_name;
1688 if (parent->d_op && parent->d_op->d_compare) {
Nick Piggin621e1552011-01-07 17:49:27 +11001689 if (parent->d_op->d_compare(parent, parent->d_inode,
1690 dentry, dentry->d_inode,
1691 qstr->len, qstr->name, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001692 goto next;
1693 } else {
1694 if (qstr->len != len)
1695 goto next;
1696 if (memcmp(qstr->name, str, len))
1697 goto next;
1698 }
1699
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001700 dentry->d_count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001701 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 spin_unlock(&dentry->d_lock);
1703 break;
1704next:
1705 spin_unlock(&dentry->d_lock);
1706 }
1707 rcu_read_unlock();
1708
1709 return found;
1710}
1711
1712/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001713 * d_hash_and_lookup - hash the qstr then search for a dentry
1714 * @dir: Directory to search in
1715 * @name: qstr of name we wish to find
1716 *
1717 * On hash failure or on lookup failure NULL is returned.
1718 */
1719struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1720{
1721 struct dentry *dentry = NULL;
1722
1723 /*
1724 * Check for a fs-specific hash function. Note that we must
1725 * calculate the standard hash first, as the d_op->d_hash()
1726 * routine may choose to leave the hash value unchanged.
1727 */
1728 name->hash = full_name_hash(name->name, name->len);
1729 if (dir->d_op && dir->d_op->d_hash) {
Nick Pigginb1e6a012011-01-07 17:49:28 +11001730 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001731 goto out;
1732 }
1733 dentry = d_lookup(dir, name);
1734out:
1735 return dentry;
1736}
1737
1738/**
Nick Piggin786a5e12011-01-07 17:49:16 +11001739 * d_validate - verify dentry provided from insecure source (deprecated)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 * @dentry: The dentry alleged to be valid child of @dparent
1741 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001742 *
1743 * An insecure source has sent us a dentry, here we verify it and dget() it.
1744 * This is used by ncpfs in its readdir implementation.
1745 * Zero is returned in the dentry is invalid.
Nick Piggin786a5e12011-01-07 17:49:16 +11001746 *
1747 * This function is slow for big directories, and deprecated, do not use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001748 */
Nick Piggind3a23e12011-01-05 20:01:21 +11001749int d_validate(struct dentry *dentry, struct dentry *dparent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750{
Nick Piggin786a5e12011-01-07 17:49:16 +11001751 struct dentry *child;
Nick Piggind3a23e12011-01-05 20:01:21 +11001752
1753 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001754 spin_lock(&dparent->d_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001755 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1756 if (dentry == child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001757 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1758 __dget_locked_dlock(dentry);
1759 spin_unlock(&dentry->d_lock);
1760 spin_unlock(&dparent->d_lock);
Nick Piggind3a23e12011-01-05 20:01:21 +11001761 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001762 return 1;
1763 }
1764 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001765 spin_unlock(&dparent->d_lock);
Nick Piggind3a23e12011-01-05 20:01:21 +11001766 spin_unlock(&dcache_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001767
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 return 0;
1769}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001770EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001771
1772/*
1773 * When a file is deleted, we have two options:
1774 * - turn this dentry into a negative dentry
1775 * - unhash this dentry and free it.
1776 *
1777 * Usually, we want to just turn this into
1778 * a negative dentry, but if anybody else is
1779 * currently using the dentry or the inode
1780 * we can't do that and we fall back on removing
1781 * it from the hash queues and waiting for
1782 * it to be deleted later when it has no users
1783 */
1784
1785/**
1786 * d_delete - delete a dentry
1787 * @dentry: The dentry to delete
1788 *
1789 * Turn the dentry into a negative dentry if possible, otherwise
1790 * remove it from the hash queues so it can be deleted later
1791 */
1792
1793void d_delete(struct dentry * dentry)
1794{
John McCutchan7a91bf72005-08-08 13:52:16 -04001795 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 /*
1797 * Are we the only user?
1798 */
1799 spin_lock(&dcache_lock);
1800 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001801 isdir = S_ISDIR(dentry->d_inode->i_mode);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001802 if (dentry->d_count == 1) {
Al Viro13e3c5e2010-05-21 16:11:04 -04001803 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001804 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001805 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001806 return;
1807 }
1808
1809 if (!d_unhashed(dentry))
1810 __d_drop(dentry);
1811
1812 spin_unlock(&dentry->d_lock);
1813 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001814
1815 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001817EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001818
1819static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1820{
1821
1822 entry->d_flags &= ~DCACHE_UNHASHED;
1823 hlist_add_head_rcu(&entry->d_hash, list);
1824}
1825
David Howells770bfad2006-08-22 20:06:07 -04001826static void _d_rehash(struct dentry * entry)
1827{
1828 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1829}
1830
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831/**
1832 * d_rehash - add an entry back to the hash
1833 * @entry: dentry to add to the hash
1834 *
1835 * Adds a dentry to the hash according to its name.
1836 */
1837
1838void d_rehash(struct dentry * entry)
1839{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001840 spin_lock(&dcache_lock);
1841 spin_lock(&entry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +11001842 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04001843 _d_rehash(entry);
Nick Piggin789680d2011-01-07 17:49:30 +11001844 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001845 spin_unlock(&entry->d_lock);
1846 spin_unlock(&dcache_lock);
1847}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001848EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849
Nick Pigginfb2d5b82011-01-07 17:49:26 +11001850/**
1851 * dentry_update_name_case - update case insensitive dentry with a new name
1852 * @dentry: dentry to be updated
1853 * @name: new name
1854 *
1855 * Update a case insensitive dentry with new case of name.
1856 *
1857 * dentry must have been returned by d_lookup with name @name. Old and new
1858 * name lengths must match (ie. no d_compare which allows mismatched name
1859 * lengths).
1860 *
1861 * Parent inode i_mutex must be held over d_lookup and into this call (to
1862 * keep renames and concurrent inserts, and readdir(2) away).
1863 */
1864void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
1865{
1866 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
1867 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
1868
1869 spin_lock(&dcache_lock);
1870 spin_lock(&dentry->d_lock);
1871 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
1872 spin_unlock(&dentry->d_lock);
1873 spin_unlock(&dcache_lock);
1874}
1875EXPORT_SYMBOL(dentry_update_name_case);
1876
Linus Torvalds1da177e2005-04-16 15:20:36 -07001877static void switch_names(struct dentry *dentry, struct dentry *target)
1878{
1879 if (dname_external(target)) {
1880 if (dname_external(dentry)) {
1881 /*
1882 * Both external: swap the pointers
1883 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001884 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 } else {
1886 /*
1887 * dentry:internal, target:external. Steal target's
1888 * storage and make target internal.
1889 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07001890 memcpy(target->d_iname, dentry->d_name.name,
1891 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 dentry->d_name.name = target->d_name.name;
1893 target->d_name.name = target->d_iname;
1894 }
1895 } else {
1896 if (dname_external(dentry)) {
1897 /*
1898 * dentry:external, target:internal. Give dentry's
1899 * storage to target and make dentry internal
1900 */
1901 memcpy(dentry->d_iname, target->d_name.name,
1902 target->d_name.len + 1);
1903 target->d_name.name = dentry->d_name.name;
1904 dentry->d_name.name = dentry->d_iname;
1905 } else {
1906 /*
1907 * Both are internal. Just copy target to dentry
1908 */
1909 memcpy(dentry->d_iname, target->d_name.name,
1910 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05001911 dentry->d_name.len = target->d_name.len;
1912 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913 }
1914 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001915 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001916}
1917
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001918static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
1919{
1920 /*
1921 * XXXX: do we really need to take target->d_lock?
1922 */
1923 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
1924 spin_lock(&target->d_parent->d_lock);
1925 else {
1926 if (d_ancestor(dentry->d_parent, target->d_parent)) {
1927 spin_lock(&dentry->d_parent->d_lock);
1928 spin_lock_nested(&target->d_parent->d_lock,
1929 DENTRY_D_LOCK_NESTED);
1930 } else {
1931 spin_lock(&target->d_parent->d_lock);
1932 spin_lock_nested(&dentry->d_parent->d_lock,
1933 DENTRY_D_LOCK_NESTED);
1934 }
1935 }
1936 if (target < dentry) {
1937 spin_lock_nested(&target->d_lock, 2);
1938 spin_lock_nested(&dentry->d_lock, 3);
1939 } else {
1940 spin_lock_nested(&dentry->d_lock, 2);
1941 spin_lock_nested(&target->d_lock, 3);
1942 }
1943}
1944
1945static void dentry_unlock_parents_for_move(struct dentry *dentry,
1946 struct dentry *target)
1947{
1948 if (target->d_parent != dentry->d_parent)
1949 spin_unlock(&dentry->d_parent->d_lock);
1950 if (target->d_parent != target)
1951 spin_unlock(&target->d_parent->d_lock);
1952}
1953
Linus Torvalds1da177e2005-04-16 15:20:36 -07001954/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001955 * When switching names, the actual string doesn't strictly have to
1956 * be preserved in the target - because we're dropping the target
1957 * anyway. As such, we can just do a simple memcpy() to copy over
1958 * the new name before we switch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001959 *
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001960 * Note that we have to be a lot more careful about getting the hash
1961 * switched - we have to switch the hash value properly even if it
1962 * then no longer matches the actual (corrupted) string of the target.
1963 * The hash value has to match the hash queue that the dentry is on..
Linus Torvalds1da177e2005-04-16 15:20:36 -07001964 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001965/*
1966 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001967 * @dentry: entry to move
1968 * @target: new dentry
1969 *
1970 * Update the dcache to reflect the move of a file name. Negative
1971 * dcache entries should not be moved in this way.
1972 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001973static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001975 if (!dentry->d_inode)
1976 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1977
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001978 BUG_ON(d_ancestor(dentry, target));
1979 BUG_ON(d_ancestor(target, dentry));
1980
Linus Torvalds1da177e2005-04-16 15:20:36 -07001981 write_seqlock(&rename_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001982
1983 dentry_lock_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001984
1985 /* Move the dentry to the target hash queue, if on different bucket */
Nick Piggin789680d2011-01-07 17:49:30 +11001986 spin_lock(&dcache_hash_lock);
1987 if (!d_unhashed(dentry))
1988 hlist_del_rcu(&dentry->d_hash);
1989 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
1990 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001991
1992 /* Unhash the target: dput() will then get rid of it */
1993 __d_drop(target);
1994
Eric Dumazet5160ee62006-01-08 01:03:32 -08001995 list_del(&dentry->d_u.d_child);
1996 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001997
1998 /* Switch the names.. */
1999 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002000 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002001
2002 /* ... and switch the parents */
2003 if (IS_ROOT(dentry)) {
2004 dentry->d_parent = target->d_parent;
2005 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002006 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002007 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002008 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009
2010 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08002011 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 }
2013
Eric Dumazet5160ee62006-01-08 01:03:32 -08002014 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002015
2016 dentry_unlock_parents_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08002018 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019 spin_unlock(&dentry->d_lock);
2020 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002021}
2022
2023/**
2024 * d_move - move a dentry
2025 * @dentry: entry to move
2026 * @target: new dentry
2027 *
2028 * Update the dcache to reflect the move of a file name. Negative
2029 * dcache entries should not be moved in this way.
2030 */
2031
2032void d_move(struct dentry * dentry, struct dentry * target)
2033{
2034 spin_lock(&dcache_lock);
2035 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036 spin_unlock(&dcache_lock);
2037}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002038EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002039
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002040/**
2041 * d_ancestor - search for an ancestor
2042 * @p1: ancestor dentry
2043 * @p2: child dentry
2044 *
2045 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2046 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07002047 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002048struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002049{
2050 struct dentry *p;
2051
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09002052 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002053 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002054 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002055 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002056 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002057}
2058
2059/*
2060 * This helper attempts to cope with remotely renamed directories
2061 *
2062 * It assumes that the caller is already holding
2063 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
2064 *
2065 * Note: If ever the locking in lock_rename() changes, then please
2066 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07002067 */
2068static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002069 __releases(dcache_lock)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002070{
2071 struct mutex *m1 = NULL, *m2 = NULL;
2072 struct dentry *ret;
2073
2074 /* If alias and dentry share a parent, then no extra locks required */
2075 if (alias->d_parent == dentry->d_parent)
2076 goto out_unalias;
2077
2078 /* Check for loops */
2079 ret = ERR_PTR(-ELOOP);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002080 if (d_ancestor(alias, dentry))
Trond Myklebust9eaef272006-10-21 10:24:20 -07002081 goto out_err;
2082
2083 /* See lock_rename() */
2084 ret = ERR_PTR(-EBUSY);
2085 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2086 goto out_err;
2087 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2088 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2089 goto out_err;
2090 m2 = &alias->d_parent->d_inode->i_mutex;
2091out_unalias:
2092 d_move_locked(alias, dentry);
2093 ret = alias;
2094out_err:
2095 spin_unlock(&dcache_lock);
2096 if (m2)
2097 mutex_unlock(m2);
2098 if (m1)
2099 mutex_unlock(m1);
2100 return ret;
2101}
2102
2103/*
David Howells770bfad2006-08-22 20:06:07 -04002104 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2105 * named dentry in place of the dentry to be replaced.
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002106 * returns with anon->d_lock held!
David Howells770bfad2006-08-22 20:06:07 -04002107 */
2108static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2109{
2110 struct dentry *dparent, *aparent;
2111
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002112 dentry_lock_for_move(anon, dentry);
David Howells770bfad2006-08-22 20:06:07 -04002113
2114 dparent = dentry->d_parent;
2115 aparent = anon->d_parent;
2116
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002117 switch_names(dentry, anon);
2118 swap(dentry->d_name.hash, anon->d_name.hash);
2119
David Howells770bfad2006-08-22 20:06:07 -04002120 dentry->d_parent = (aparent == anon) ? dentry : aparent;
2121 list_del(&dentry->d_u.d_child);
2122 if (!IS_ROOT(dentry))
2123 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2124 else
2125 INIT_LIST_HEAD(&dentry->d_u.d_child);
2126
2127 anon->d_parent = (dparent == dentry) ? anon : dparent;
2128 list_del(&anon->d_u.d_child);
2129 if (!IS_ROOT(anon))
2130 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
2131 else
2132 INIT_LIST_HEAD(&anon->d_u.d_child);
2133
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002134 dentry_unlock_parents_for_move(anon, dentry);
2135 spin_unlock(&dentry->d_lock);
2136
2137 /* anon->d_lock still locked, returns locked */
David Howells770bfad2006-08-22 20:06:07 -04002138 anon->d_flags &= ~DCACHE_DISCONNECTED;
2139}
2140
2141/**
2142 * d_materialise_unique - introduce an inode into the tree
2143 * @dentry: candidate dentry
2144 * @inode: inode to bind to the dentry, to which aliases may be attached
2145 *
2146 * Introduces an dentry into the tree, substituting an extant disconnected
2147 * root directory alias in its place if there is one
2148 */
2149struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2150{
Trond Myklebust9eaef272006-10-21 10:24:20 -07002151 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04002152
2153 BUG_ON(!d_unhashed(dentry));
2154
2155 spin_lock(&dcache_lock);
2156
2157 if (!inode) {
2158 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09002159 __d_instantiate(dentry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04002160 goto found_lock;
2161 }
2162
Trond Myklebust9eaef272006-10-21 10:24:20 -07002163 if (S_ISDIR(inode->i_mode)) {
2164 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04002165
Trond Myklebust9eaef272006-10-21 10:24:20 -07002166 /* Does an aliased dentry already exist? */
2167 alias = __d_find_alias(inode, 0);
2168 if (alias) {
2169 actual = alias;
2170 /* Is this an anonymous mountpoint that we could splice
2171 * into our tree? */
2172 if (IS_ROOT(alias)) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002173 __d_materialise_dentry(dentry, alias);
2174 __d_drop(alias);
2175 goto found;
2176 }
2177 /* Nope, but we must(!) avoid directory aliasing */
2178 actual = __d_unalias(dentry, alias);
2179 if (IS_ERR(actual))
2180 dput(alias);
2181 goto out_nolock;
2182 }
David Howells770bfad2006-08-22 20:06:07 -04002183 }
2184
2185 /* Add a unique reference */
2186 actual = __d_instantiate_unique(dentry, inode);
2187 if (!actual)
2188 actual = dentry;
2189 else if (unlikely(!d_unhashed(actual)))
2190 goto shouldnt_be_hashed;
2191
2192found_lock:
2193 spin_lock(&actual->d_lock);
2194found:
Nick Piggin789680d2011-01-07 17:49:30 +11002195 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04002196 _d_rehash(actual);
Nick Piggin789680d2011-01-07 17:49:30 +11002197 spin_unlock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04002198 spin_unlock(&actual->d_lock);
2199 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002200out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04002201 if (actual == dentry) {
2202 security_d_instantiate(dentry, inode);
2203 return NULL;
2204 }
2205
2206 iput(inode);
2207 return actual;
2208
David Howells770bfad2006-08-22 20:06:07 -04002209shouldnt_be_hashed:
2210 spin_unlock(&dcache_lock);
2211 BUG();
David Howells770bfad2006-08-22 20:06:07 -04002212}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002213EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04002214
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002215static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01002216{
2217 *buflen -= namelen;
2218 if (*buflen < 0)
2219 return -ENAMETOOLONG;
2220 *buffer -= namelen;
2221 memcpy(*buffer, str, namelen);
2222 return 0;
2223}
2224
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002225static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2226{
2227 return prepend(buffer, buflen, name->name, name->len);
2228}
2229
Linus Torvalds1da177e2005-04-16 15:20:36 -07002230/**
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002231 * Prepend path string to a buffer
2232 *
2233 * @path: the dentry/vfsmount to report
2234 * @root: root vfsmnt/dentry (may be modified by this function)
2235 * @buffer: pointer to the end of the buffer
2236 * @buflen: pointer to buffer length
2237 *
2238 * Caller holds the dcache_lock.
2239 *
2240 * If path is not reachable from the supplied root, then the value of
2241 * root is changed (without modifying refcounts).
2242 */
2243static int prepend_path(const struct path *path, struct path *root,
2244 char **buffer, int *buflen)
2245{
2246 struct dentry *dentry = path->dentry;
2247 struct vfsmount *vfsmnt = path->mnt;
2248 bool slash = false;
2249 int error = 0;
2250
Nick Piggin99b7db72010-08-18 04:37:39 +10002251 br_read_lock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002252 while (dentry != root->dentry || vfsmnt != root->mnt) {
2253 struct dentry * parent;
2254
2255 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2256 /* Global root? */
2257 if (vfsmnt->mnt_parent == vfsmnt) {
2258 goto global_root;
2259 }
2260 dentry = vfsmnt->mnt_mountpoint;
2261 vfsmnt = vfsmnt->mnt_parent;
2262 continue;
2263 }
2264 parent = dentry->d_parent;
2265 prefetch(parent);
2266 error = prepend_name(buffer, buflen, &dentry->d_name);
2267 if (!error)
2268 error = prepend(buffer, buflen, "/", 1);
2269 if (error)
2270 break;
2271
2272 slash = true;
2273 dentry = parent;
2274 }
2275
2276out:
2277 if (!error && !slash)
2278 error = prepend(buffer, buflen, "/", 1);
2279
Nick Piggin99b7db72010-08-18 04:37:39 +10002280 br_read_unlock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002281 return error;
2282
2283global_root:
2284 /*
2285 * Filesystems needing to implement special "root names"
2286 * should do so with ->d_dname()
2287 */
2288 if (IS_ROOT(dentry) &&
2289 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2290 WARN(1, "Root dentry has weird name <%.*s>\n",
2291 (int) dentry->d_name.len, dentry->d_name.name);
2292 }
2293 root->mnt = vfsmnt;
2294 root->dentry = dentry;
2295 goto out;
2296}
2297
2298/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002299 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002300 * @path: the dentry/vfsmount to report
2301 * @root: root vfsmnt/dentry (may be modified by this function)
Randy Dunlapcd956a12010-08-14 13:05:31 -07002302 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 * @buflen: buffer length
2304 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002305 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002306 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002307 * Returns a pointer into the buffer or an error code if the
2308 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002309 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002310 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002311 *
2312 * If path is not reachable from the supplied root, then the value of
2313 * root is changed (without modifying refcounts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314 */
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002315char *__d_path(const struct path *path, struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002316 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002318 char *res = buf + buflen;
2319 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002320
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002321 prepend(&res, &buflen, "\0", 1);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002322 spin_lock(&dcache_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002323 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002324 spin_unlock(&dcache_lock);
2325
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002326 if (error)
2327 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002328 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002329}
2330
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002331/*
2332 * same as __d_path but appends "(deleted)" for unlinked files.
2333 */
2334static int path_with_deleted(const struct path *path, struct path *root,
2335 char **buf, int *buflen)
2336{
2337 prepend(buf, buflen, "\0", 1);
2338 if (d_unlinked(path->dentry)) {
2339 int error = prepend(buf, buflen, " (deleted)", 10);
2340 if (error)
2341 return error;
2342 }
2343
2344 return prepend_path(path, root, buf, buflen);
2345}
2346
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002347static int prepend_unreachable(char **buffer, int *buflen)
2348{
2349 return prepend(buffer, buflen, "(unreachable)", 13);
2350}
2351
Jan Bluncka03a8a702008-02-14 19:38:32 -08002352/**
2353 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002354 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002355 * @buf: buffer to return value in
2356 * @buflen: buffer length
2357 *
2358 * Convert a dentry into an ASCII path name. If the entry has been deleted
2359 * the string " (deleted)" is appended. Note that this is ambiguous.
2360 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002361 * Returns a pointer into the buffer or an error code if the path was
2362 * too long. Note: Callers should use the returned pointer, not the passed
2363 * in buffer, to use the name! The implementation often starts at an offset
2364 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002365 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002366 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002367 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002368char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002369{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002370 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002371 struct path root;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002372 struct path tmp;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002373 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002375 /*
2376 * We have various synthetic filesystems that never get mounted. On
2377 * these filesystems dentries are never used for lookup purposes, and
2378 * thus don't need to be hashed. They also don't need a name until a
2379 * user wants to identify the object in /proc/pid/fd/. The little hack
2380 * below allows us to generate a name for these objects on demand:
2381 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002382 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2383 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002384
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002385 get_fs_root(current->fs, &root);
Linus Torvalds552ce542007-02-13 12:08:18 -08002386 spin_lock(&dcache_lock);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002387 tmp = root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002388 error = path_with_deleted(path, &tmp, &res, &buflen);
2389 if (error)
2390 res = ERR_PTR(error);
Linus Torvalds552ce542007-02-13 12:08:18 -08002391 spin_unlock(&dcache_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002392 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393 return res;
2394}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002395EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002396
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002397/**
2398 * d_path_with_unreachable - return the path of a dentry
2399 * @path: path to report
2400 * @buf: buffer to return value in
2401 * @buflen: buffer length
2402 *
2403 * The difference from d_path() is that this prepends "(unreachable)"
2404 * to paths which are unreachable from the current process' root.
2405 */
2406char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2407{
2408 char *res = buf + buflen;
2409 struct path root;
2410 struct path tmp;
2411 int error;
2412
2413 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2414 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2415
2416 get_fs_root(current->fs, &root);
2417 spin_lock(&dcache_lock);
2418 tmp = root;
2419 error = path_with_deleted(path, &tmp, &res, &buflen);
2420 if (!error && !path_equal(&tmp, &root))
2421 error = prepend_unreachable(&res, &buflen);
2422 spin_unlock(&dcache_lock);
2423 path_put(&root);
2424 if (error)
2425 res = ERR_PTR(error);
2426
2427 return res;
2428}
2429
Linus Torvalds1da177e2005-04-16 15:20:36 -07002430/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002431 * Helper function for dentry_operations.d_dname() members
2432 */
2433char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2434 const char *fmt, ...)
2435{
2436 va_list args;
2437 char temp[64];
2438 int sz;
2439
2440 va_start(args, fmt);
2441 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2442 va_end(args);
2443
2444 if (sz > sizeof(temp) || sz > buflen)
2445 return ERR_PTR(-ENAMETOOLONG);
2446
2447 buffer += buflen - sz;
2448 return memcpy(buffer, temp, sz);
2449}
2450
2451/*
Ram Pai6092d042008-03-27 13:06:20 +01002452 * Write full pathname from the root of the filesystem into the buffer.
2453 */
Nick Pigginec2447c2011-01-07 17:49:29 +11002454static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002455{
2456 char *end = buf + buflen;
2457 char *retval;
2458
Ram Pai6092d042008-03-27 13:06:20 +01002459 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002460 if (buflen < 1)
2461 goto Elong;
2462 /* Get '/' right */
2463 retval = end-1;
2464 *retval = '/';
2465
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002466 while (!IS_ROOT(dentry)) {
2467 struct dentry *parent = dentry->d_parent;
Ram Pai6092d042008-03-27 13:06:20 +01002468
Ram Pai6092d042008-03-27 13:06:20 +01002469 prefetch(parent);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002470 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
Ram Pai6092d042008-03-27 13:06:20 +01002471 (prepend(&end, &buflen, "/", 1) != 0))
2472 goto Elong;
2473
2474 retval = end;
2475 dentry = parent;
2476 }
Al Viroc1031352010-06-06 22:31:14 -04002477 return retval;
2478Elong:
2479 return ERR_PTR(-ENAMETOOLONG);
2480}
Nick Pigginec2447c2011-01-07 17:49:29 +11002481
2482char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2483{
2484 char *retval;
2485
2486 spin_lock(&dcache_lock);
2487 retval = __dentry_path(dentry, buf, buflen);
2488 spin_unlock(&dcache_lock);
2489
2490 return retval;
2491}
2492EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04002493
2494char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2495{
2496 char *p = NULL;
2497 char *retval;
2498
2499 spin_lock(&dcache_lock);
2500 if (d_unlinked(dentry)) {
2501 p = buf + buflen;
2502 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2503 goto Elong;
2504 buflen++;
2505 }
2506 retval = __dentry_path(dentry, buf, buflen);
Ram Pai6092d042008-03-27 13:06:20 +01002507 spin_unlock(&dcache_lock);
Al Viroc1031352010-06-06 22:31:14 -04002508 if (!IS_ERR(retval) && p)
2509 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002510 return retval;
2511Elong:
2512 spin_unlock(&dcache_lock);
2513 return ERR_PTR(-ENAMETOOLONG);
2514}
2515
2516/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002517 * NOTE! The user-level library version returns a
2518 * character pointer. The kernel system call just
2519 * returns the length of the buffer filled (which
2520 * includes the ending '\0' character), or a negative
2521 * error value. So libc would do something like
2522 *
2523 * char *getcwd(char * buf, size_t size)
2524 * {
2525 * int retval;
2526 *
2527 * retval = sys_getcwd(buf, size);
2528 * if (retval >= 0)
2529 * return buf;
2530 * errno = -retval;
2531 * return NULL;
2532 * }
2533 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002534SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002535{
Linus Torvalds552ce542007-02-13 12:08:18 -08002536 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002537 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002538 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002539
2540 if (!page)
2541 return -ENOMEM;
2542
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002543 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002544
Linus Torvalds552ce542007-02-13 12:08:18 -08002545 error = -ENOENT;
Linus Torvalds552ce542007-02-13 12:08:18 -08002546 spin_lock(&dcache_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002547 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002548 unsigned long len;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002549 struct path tmp = root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002550 char *cwd = page + PAGE_SIZE;
2551 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002552
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002553 prepend(&cwd, &buflen, "\0", 1);
2554 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
Linus Torvalds552ce542007-02-13 12:08:18 -08002555 spin_unlock(&dcache_lock);
2556
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002557 if (error)
Linus Torvalds552ce542007-02-13 12:08:18 -08002558 goto out;
2559
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002560 /* Unreachable from current root */
2561 if (!path_equal(&tmp, &root)) {
2562 error = prepend_unreachable(&cwd, &buflen);
2563 if (error)
2564 goto out;
2565 }
2566
Linus Torvalds552ce542007-02-13 12:08:18 -08002567 error = -ERANGE;
2568 len = PAGE_SIZE + page - cwd;
2569 if (len <= size) {
2570 error = len;
2571 if (copy_to_user(buf, cwd, len))
2572 error = -EFAULT;
2573 }
2574 } else
2575 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002578 path_put(&pwd);
2579 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 free_page((unsigned long) page);
2581 return error;
2582}
2583
2584/*
2585 * Test whether new_dentry is a subdirectory of old_dentry.
2586 *
2587 * Trivially implemented using the dcache structure
2588 */
2589
2590/**
2591 * is_subdir - is new dentry a subdirectory of old_dentry
2592 * @new_dentry: new dentry
2593 * @old_dentry: old dentry
2594 *
2595 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2596 * Returns 0 otherwise.
2597 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2598 */
2599
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002600int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
2602 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002603 unsigned long seq;
2604
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002605 if (new_dentry == old_dentry)
2606 return 1;
2607
2608 /*
2609 * Need rcu_readlock to protect against the d_parent trashing
2610 * due to d_move
Linus Torvalds1da177e2005-04-16 15:20:36 -07002611 */
2612 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002613 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002614 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002615 seq = read_seqbegin(&rename_lock);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002616 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002618 else
2619 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620 } while (read_seqretry(&rename_lock, seq));
2621 rcu_read_unlock();
2622
2623 return result;
2624}
2625
Al Viro2096f752010-01-30 13:16:21 -05002626int path_is_under(struct path *path1, struct path *path2)
2627{
2628 struct vfsmount *mnt = path1->mnt;
2629 struct dentry *dentry = path1->dentry;
2630 int res;
Nick Piggin99b7db72010-08-18 04:37:39 +10002631
2632 br_read_lock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002633 if (mnt != path2->mnt) {
2634 for (;;) {
2635 if (mnt->mnt_parent == mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +10002636 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002637 return 0;
2638 }
2639 if (mnt->mnt_parent == path2->mnt)
2640 break;
2641 mnt = mnt->mnt_parent;
2642 }
2643 dentry = mnt->mnt_mountpoint;
2644 }
2645 res = is_subdir(dentry, path2->dentry);
Nick Piggin99b7db72010-08-18 04:37:39 +10002646 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002647 return res;
2648}
2649EXPORT_SYMBOL(path_is_under);
2650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651void d_genocide(struct dentry *root)
2652{
2653 struct dentry *this_parent = root;
2654 struct list_head *next;
2655
2656 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002657 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002658repeat:
2659 next = this_parent->d_subdirs.next;
2660resume:
2661 while (next != &this_parent->d_subdirs) {
2662 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002663 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 next = tmp->next;
Nick Pigginda502952011-01-07 17:49:33 +11002665 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2666 if (d_unhashed(dentry) || !dentry->d_inode) {
2667 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 continue;
Nick Pigginda502952011-01-07 17:49:33 +11002669 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002671 spin_unlock(&this_parent->d_lock);
2672 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002674 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 goto repeat;
2676 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002677 dentry->d_count--;
2678 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002679 }
2680 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08002681 next = this_parent->d_u.d_child.next;
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002682 this_parent->d_count--;
2683 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002684 this_parent = this_parent->d_parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002685 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002686 goto resume;
2687 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002688 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002689 spin_unlock(&dcache_lock);
2690}
2691
2692/**
2693 * find_inode_number - check for dentry with name
2694 * @dir: directory to check
2695 * @name: Name to find.
2696 *
2697 * Check whether a dentry already exists for the given name,
2698 * and return the inode number if it has an inode. Otherwise
2699 * 0 is returned.
2700 *
2701 * This routine is used to post-process directory listings for
2702 * filesystems using synthetic inode numbers, and is necessary
2703 * to keep getcwd() working.
2704 */
2705
2706ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2707{
2708 struct dentry * dentry;
2709 ino_t ino = 0;
2710
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002711 dentry = d_hash_and_lookup(dir, name);
2712 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002713 if (dentry->d_inode)
2714 ino = dentry->d_inode->i_ino;
2715 dput(dentry);
2716 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002717 return ino;
2718}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002719EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720
2721static __initdata unsigned long dhash_entries;
2722static int __init set_dhash_entries(char *str)
2723{
2724 if (!str)
2725 return 0;
2726 dhash_entries = simple_strtoul(str, &str, 0);
2727 return 1;
2728}
2729__setup("dhash_entries=", set_dhash_entries);
2730
2731static void __init dcache_init_early(void)
2732{
2733 int loop;
2734
2735 /* If hashes are distributed across NUMA nodes, defer
2736 * hash allocation until vmalloc space is available.
2737 */
2738 if (hashdist)
2739 return;
2740
2741 dentry_hashtable =
2742 alloc_large_system_hash("Dentry cache",
2743 sizeof(struct hlist_head),
2744 dhash_entries,
2745 13,
2746 HASH_EARLY,
2747 &d_hash_shift,
2748 &d_hash_mask,
2749 0);
2750
2751 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2752 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2753}
2754
Denis Cheng74bf17c2007-10-16 23:26:30 -07002755static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002756{
2757 int loop;
2758
2759 /*
2760 * A constructor could be added for stable state like the lists,
2761 * but it is probably not worth it because of the cache nature
2762 * of the dcache.
2763 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002764 dentry_cache = KMEM_CACHE(dentry,
2765 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002766
Rusty Russell8e1f9362007-07-17 04:03:17 -07002767 register_shrinker(&dcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002768
2769 /* Hash may have been set up in dcache_init_early */
2770 if (!hashdist)
2771 return;
2772
2773 dentry_hashtable =
2774 alloc_large_system_hash("Dentry cache",
2775 sizeof(struct hlist_head),
2776 dhash_entries,
2777 13,
2778 0,
2779 &d_hash_shift,
2780 &d_hash_mask,
2781 0);
2782
2783 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2784 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2785}
2786
2787/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002788struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002789EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002790
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791EXPORT_SYMBOL(d_genocide);
2792
Linus Torvalds1da177e2005-04-16 15:20:36 -07002793void __init vfs_caches_init_early(void)
2794{
2795 dcache_init_early();
2796 inode_init_early();
2797}
2798
2799void __init vfs_caches_init(unsigned long mempages)
2800{
2801 unsigned long reserve;
2802
2803 /* Base hash sizes on available memory, with a reserve equal to
2804 150% of current kernel size */
2805
2806 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2807 mempages -= reserve;
2808
2809 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002810 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002811
Denis Cheng74bf17c2007-10-16 23:26:30 -07002812 dcache_init();
2813 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002814 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07002815 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816 bdev_cache_init();
2817 chrdev_init();
2818}