blob: de38680ee0ed1ca5712792b26bb135c4c79793e6 [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 Pigginb23fb0a2011-01-07 17:49:35 +110040 * dcache_inode_lock protects:
41 * - i_dentry, d_alias, d_inode
Nick Piggin23044502011-01-07 17:49:31 +110042 * dcache_hash_lock protects:
43 * - the dcache hash table, s_anon lists
44 * dcache_lru_lock protects:
45 * - the dcache lru lists and counters
46 * d_lock protects:
47 * - d_flags
48 * - d_name
49 * - d_lru
Nick Pigginb7ab39f2011-01-07 17:49:32 +110050 * - d_count
Nick Pigginda502952011-01-07 17:49:33 +110051 * - d_unhashed()
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110052 * - d_parent and d_subdirs
53 * - childrens' d_child and d_parent
Nick Pigginb23fb0a2011-01-07 17:49:35 +110054 * - d_alias, d_inode
Nick Piggin789680d2011-01-07 17:49:30 +110055 *
56 * Ordering:
57 * dcache_lock
Nick Pigginb23fb0a2011-01-07 17:49:35 +110058 * dcache_inode_lock
59 * dentry->d_lock
60 * dcache_lru_lock
61 * dcache_hash_lock
Nick Piggin789680d2011-01-07 17:49:30 +110062 *
Nick Pigginda502952011-01-07 17:49:33 +110063 * If there is an ancestor relationship:
64 * dentry->d_parent->...->d_parent->d_lock
65 * ...
66 * dentry->d_parent->d_lock
67 * dentry->d_lock
68 *
69 * If no ancestor relationship:
Nick Piggin789680d2011-01-07 17:49:30 +110070 * if (dentry1 < dentry2)
71 * dentry1->d_lock
72 * dentry2->d_lock
73 */
Eric Dumazetfa3536c2006-03-26 01:37:24 -080074int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
76
Nick Pigginb23fb0a2011-01-07 17:49:35 +110077__cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_inode_lock);
Nick Piggin789680d2011-01-07 17:49:30 +110078static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_hash_lock);
Nick Piggin23044502011-01-07 17:49:31 +110079static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
Nick Piggin789680d2011-01-07 17:49:30 +110080__cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -040081__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Nick Pigginb23fb0a2011-01-07 17:49:35 +110083EXPORT_SYMBOL(dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084EXPORT_SYMBOL(dcache_lock);
85
Christoph Lametere18b8902006-12-06 20:33:20 -080086static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
89
90/*
91 * This is the single most critical data structure when it comes
92 * to the dcache: the hashtable for lookups. Somebody should try
93 * to make this good - I've just made it work.
94 *
95 * This hash-function tries to avoid losing too many bits of hash
96 * information, yet avoid using a prime hash-size or similar.
97 */
98#define D_HASHBITS d_hash_shift
99#define D_HASHMASK d_hash_mask
100
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800101static unsigned int d_hash_mask __read_mostly;
102static unsigned int d_hash_shift __read_mostly;
103static struct hlist_head *dentry_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/* Statistics gathering. */
106struct dentry_stat_t dentry_stat = {
107 .age_limit = 45,
108};
109
Nick Piggin3e880fb2011-01-07 17:49:19 +1100110static DEFINE_PER_CPU(unsigned int, nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400111
112#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Nick Piggin3e880fb2011-01-07 17:49:19 +1100113static int get_nr_dentry(void)
114{
115 int i;
116 int sum = 0;
117 for_each_possible_cpu(i)
118 sum += per_cpu(nr_dentry, i);
119 return sum < 0 ? 0 : sum;
120}
121
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400122int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
123 size_t *lenp, loff_t *ppos)
124{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100125 dentry_stat.nr_dentry = get_nr_dentry();
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400126 return proc_dointvec(table, write, buffer, lenp, ppos);
127}
128#endif
129
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400130static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400132 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
133
Arjan van de Venfd217f42008-10-21 06:47:33 -0700134 WARN_ON(!list_empty(&dentry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (dname_external(dentry))
136 kfree(dentry->d_name.name);
137 kmem_cache_free(dentry_cache, dentry);
138}
139
140/*
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400141 * no dcache_lock, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 */
143static void d_free(struct dentry *dentry)
144{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100145 BUG_ON(dentry->d_count);
Nick Piggin3e880fb2011-01-07 17:49:19 +1100146 this_cpu_dec(nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (dentry->d_op && dentry->d_op->d_release)
148 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400149
Eric Dumazetb3423412006-12-06 20:38:48 -0800150 /* if dentry was never inserted into hash, immediate free is OK */
Akinobu Mitae8462ca2008-02-06 01:37:07 -0800151 if (hlist_unhashed(&dentry->d_hash))
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400152 __d_free(&dentry->d_u.d_rcu);
Eric Dumazetb3423412006-12-06 20:38:48 -0800153 else
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400154 call_rcu(&dentry->d_u.d_rcu, __d_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/*
158 * Release the dentry's inode, using the filesystem
159 * d_iput() operation if defined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800161static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200162 __releases(dentry->d_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100163 __releases(dcache_inode_lock)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200164 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 struct inode *inode = dentry->d_inode;
167 if (inode) {
168 dentry->d_inode = NULL;
169 list_del_init(&dentry->d_alias);
170 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100171 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 spin_unlock(&dcache_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700173 if (!inode->i_nlink)
174 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (dentry->d_op && dentry->d_op->d_iput)
176 dentry->d_op->d_iput(dentry, inode);
177 else
178 iput(inode);
179 } else {
180 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100181 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 spin_unlock(&dcache_lock);
183 }
184}
185
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700186/*
Nick Piggin23044502011-01-07 17:49:31 +1100187 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700188 */
189static void dentry_lru_add(struct dentry *dentry)
190{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400191 if (list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100192 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400193 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
194 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100195 dentry_stat.nr_unused++;
Nick Piggin23044502011-01-07 17:49:31 +1100196 spin_unlock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400197 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700198}
199
Nick Piggin23044502011-01-07 17:49:31 +1100200static void __dentry_lru_del(struct dentry *dentry)
201{
202 list_del_init(&dentry->d_lru);
203 dentry->d_sb->s_nr_dentry_unused--;
204 dentry_stat.nr_unused--;
205}
206
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700207static void dentry_lru_del(struct dentry *dentry)
208{
209 if (!list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100210 spin_lock(&dcache_lru_lock);
211 __dentry_lru_del(dentry);
212 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700213 }
214}
215
Christoph Hellwiga4633352010-10-10 05:36:26 -0400216static void dentry_lru_move_tail(struct dentry *dentry)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700217{
Nick Piggin23044502011-01-07 17:49:31 +1100218 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400219 if (list_empty(&dentry->d_lru)) {
220 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
221 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100222 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400223 } else {
224 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700225 }
Nick Piggin23044502011-01-07 17:49:31 +1100226 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700227}
228
Miklos Szeredid52b9082007-05-08 00:23:46 -0700229/**
230 * d_kill - kill dentry and return parent
231 * @dentry: dentry to kill
232 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200233 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700234 *
235 * If this is the root of the dentry tree, return NULL.
Nick Piggin23044502011-01-07 17:49:31 +1100236 *
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100237 * dcache_lock and d_lock and d_parent->d_lock must be held by caller, and
238 * are dropped by d_kill.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700239 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100240static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200241 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100242 __releases(parent->d_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100243 __releases(dcache_inode_lock)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200244 __releases(dcache_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700245{
Miklos Szeredid52b9082007-05-08 00:23:46 -0700246 list_del(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100247 if (parent)
248 spin_unlock(&parent->d_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700249 dentry_iput(dentry);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100250 /*
251 * dentry_iput drops the locks, at which point nobody (except
252 * transient RCU lookups) can reach this dentry.
253 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700254 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900255 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700256}
257
Nick Piggin789680d2011-01-07 17:49:30 +1100258/**
259 * d_drop - drop a dentry
260 * @dentry: dentry to drop
261 *
262 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
263 * be found through a VFS lookup any more. Note that this is different from
264 * deleting the dentry - d_delete will try to mark the dentry negative if
265 * possible, giving a successful _negative_ lookup, while d_drop will
266 * just make the cache lookup fail.
267 *
268 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
269 * reason (NFS timeouts or autofs deletes).
270 *
271 * __d_drop requires dentry->d_lock.
272 */
273void __d_drop(struct dentry *dentry)
274{
275 if (!(dentry->d_flags & DCACHE_UNHASHED)) {
276 dentry->d_flags |= DCACHE_UNHASHED;
277 spin_lock(&dcache_hash_lock);
278 hlist_del_rcu(&dentry->d_hash);
279 spin_unlock(&dcache_hash_lock);
280 }
281}
282EXPORT_SYMBOL(__d_drop);
283
284void d_drop(struct dentry *dentry)
285{
286 spin_lock(&dcache_lock);
287 spin_lock(&dentry->d_lock);
288 __d_drop(dentry);
289 spin_unlock(&dentry->d_lock);
290 spin_unlock(&dcache_lock);
291}
292EXPORT_SYMBOL(d_drop);
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/*
295 * This is dput
296 *
297 * This is complicated by the fact that we do not want to put
298 * dentries that are no longer on any hash chain on the unused
299 * list: we'd much rather just get rid of them immediately.
300 *
301 * However, that implies that we have to traverse the dentry
302 * tree upwards to the parents which might _also_ now be
303 * scheduled for deletion (it may have been only waiting for
304 * its last child to go away).
305 *
306 * This tail recursion is done by hand as we don't want to depend
307 * on the compiler to always get this right (gcc generally doesn't).
308 * Real recursion would eat up our stack space.
309 */
310
311/*
312 * dput - release a dentry
313 * @dentry: dentry to release
314 *
315 * Release a dentry. This will drop the usage count and if appropriate
316 * call the dentry unlink method as well as removing it from the queues and
317 * releasing its resources. If the parent dentries were scheduled for release
318 * they too may now get deleted.
319 *
320 * no dcache lock, please.
321 */
322
323void dput(struct dentry *dentry)
324{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100325 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!dentry)
327 return;
328
329repeat:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100330 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 spin_lock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100333 if (IS_ROOT(dentry))
334 parent = NULL;
335 else
336 parent = dentry->d_parent;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100337 if (dentry->d_count == 1) {
338 if (!spin_trylock(&dcache_lock)) {
339 /*
340 * Something of a livelock possibility we could avoid
341 * by taking dcache_lock and trying again, but we
342 * want to reduce dcache_lock anyway so this will
343 * get improved.
344 */
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100345drop1:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100346 spin_unlock(&dentry->d_lock);
347 goto repeat;
348 }
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100349 if (!spin_trylock(&dcache_inode_lock)) {
350drop2:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100351 spin_unlock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100352 goto drop1;
353 }
354 if (parent && !spin_trylock(&parent->d_lock)) {
355 spin_unlock(&dcache_inode_lock);
356 goto drop2;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100357 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100358 }
359 dentry->d_count--;
360 if (dentry->d_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 spin_unlock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100362 if (parent)
363 spin_unlock(&parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 spin_unlock(&dcache_lock);
365 return;
366 }
367
368 /*
369 * AV: ->d_delete() is _NOT_ allowed to block now.
370 */
371 if (dentry->d_op && dentry->d_op->d_delete) {
372 if (dentry->d_op->d_delete(dentry))
373 goto unhash_it;
374 }
Nick Piggin265ac902010-10-10 05:36:24 -0400375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* Unreachable? Get rid of it */
377 if (d_unhashed(dentry))
378 goto kill_it;
Nick Piggin265ac902010-10-10 05:36:24 -0400379
380 /* Otherwise leave it cached and ensure it's on the LRU */
381 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400382 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 spin_unlock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100385 if (parent)
386 spin_unlock(&parent->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100387 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 spin_unlock(&dcache_lock);
389 return;
390
391unhash_it:
392 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700393kill_it:
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700394 /* if dentry was on the d_lru list delete it from there */
395 dentry_lru_del(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100396 dentry = d_kill(dentry, parent);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700397 if (dentry)
398 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700400EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402/**
403 * d_invalidate - invalidate a dentry
404 * @dentry: dentry to invalidate
405 *
406 * Try to invalidate the dentry if it turns out to be
407 * possible. If there are other dentries that can be
408 * reached through this one we can't delete it and we
409 * return -EBUSY. On success we return 0.
410 *
411 * no dcache lock.
412 */
413
414int d_invalidate(struct dentry * dentry)
415{
416 /*
417 * If it's already been dropped, return OK.
418 */
419 spin_lock(&dcache_lock);
Nick Pigginda502952011-01-07 17:49:33 +1100420 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (d_unhashed(dentry)) {
Nick Pigginda502952011-01-07 17:49:33 +1100422 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 spin_unlock(&dcache_lock);
424 return 0;
425 }
426 /*
427 * Check whether to do a partial shrink_dcache
428 * to get rid of unused child entries.
429 */
430 if (!list_empty(&dentry->d_subdirs)) {
Nick Pigginda502952011-01-07 17:49:33 +1100431 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 spin_unlock(&dcache_lock);
433 shrink_dcache_parent(dentry);
434 spin_lock(&dcache_lock);
Nick Pigginda502952011-01-07 17:49:33 +1100435 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
438 /*
439 * Somebody else still using it?
440 *
441 * If it's a directory, we can't drop it
442 * for fear of somebody re-populating it
443 * with children (even though dropping it
444 * would make it unreachable from the root,
445 * we might still populate it if it was a
446 * working directory or similar).
447 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100448 if (dentry->d_count > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
450 spin_unlock(&dentry->d_lock);
451 spin_unlock(&dcache_lock);
452 return -EBUSY;
453 }
454 }
455
456 __d_drop(dentry);
457 spin_unlock(&dentry->d_lock);
458 spin_unlock(&dcache_lock);
459 return 0;
460}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700461EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100463/* This must be called with dcache_lock and d_lock held */
Nick Piggin23044502011-01-07 17:49:31 +1100464static inline struct dentry * __dget_locked_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100466 dentry->d_count++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400467 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return dentry;
469}
470
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100471/* This should be called _only_ with dcache_lock held */
Nick Piggin23044502011-01-07 17:49:31 +1100472static inline struct dentry * __dget_locked(struct dentry *dentry)
473{
Nick Piggin23044502011-01-07 17:49:31 +1100474 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100475 __dget_locked_dlock(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100476 spin_unlock(&dentry->d_lock);
477 return dentry;
478}
479
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100480struct dentry * dget_locked_dlock(struct dentry *dentry)
481{
482 return __dget_locked_dlock(dentry);
483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485struct dentry * dget_locked(struct dentry *dentry)
486{
487 return __dget_locked(dentry);
488}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700489EXPORT_SYMBOL(dget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100491struct dentry *dget_parent(struct dentry *dentry)
492{
493 struct dentry *ret;
494
495repeat:
496 spin_lock(&dentry->d_lock);
497 ret = dentry->d_parent;
498 if (!ret)
499 goto out;
500 if (dentry == ret) {
501 ret->d_count++;
502 goto out;
503 }
504 if (!spin_trylock(&ret->d_lock)) {
505 spin_unlock(&dentry->d_lock);
506 cpu_relax();
507 goto repeat;
508 }
509 BUG_ON(!ret->d_count);
510 ret->d_count++;
511 spin_unlock(&ret->d_lock);
512out:
513 spin_unlock(&dentry->d_lock);
514 return ret;
515}
516EXPORT_SYMBOL(dget_parent);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/**
519 * d_find_alias - grab a hashed alias of inode
520 * @inode: inode in question
521 * @want_discon: flag, used by d_splice_alias, to request
522 * that only a DISCONNECTED alias be returned.
523 *
524 * If inode has a hashed alias, or is a directory and has any alias,
525 * acquire the reference to alias and return it. Otherwise return NULL.
526 * Notice that if inode is a directory there can be only one alias and
527 * it can be unhashed only if it has no children, or if it is the root
528 * of a filesystem.
529 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700530 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700532 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 */
Nick Pigginda502952011-01-07 17:49:33 +1100534static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Nick Pigginda502952011-01-07 17:49:33 +1100536 struct dentry *alias, *discon_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Nick Pigginda502952011-01-07 17:49:33 +1100538again:
539 discon_alias = NULL;
540 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
541 spin_lock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700543 if (IS_ROOT(alias) &&
Nick Pigginda502952011-01-07 17:49:33 +1100544 (alias->d_flags & DCACHE_DISCONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 discon_alias = alias;
Nick Pigginda502952011-01-07 17:49:33 +1100546 } else if (!want_discon) {
547 __dget_locked_dlock(alias);
548 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return alias;
550 }
551 }
Nick Pigginda502952011-01-07 17:49:33 +1100552 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Nick Pigginda502952011-01-07 17:49:33 +1100554 if (discon_alias) {
555 alias = discon_alias;
556 spin_lock(&alias->d_lock);
557 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
558 if (IS_ROOT(alias) &&
559 (alias->d_flags & DCACHE_DISCONNECTED)) {
560 __dget_locked_dlock(alias);
561 spin_unlock(&alias->d_lock);
562 return alias;
563 }
564 }
565 spin_unlock(&alias->d_lock);
566 goto again;
567 }
568 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Nick Pigginda502952011-01-07 17:49:33 +1100571struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
David Howells214fda12006-03-25 03:06:36 -0800573 struct dentry *de = NULL;
574
575 if (!list_empty(&inode->i_dentry)) {
576 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100577 spin_lock(&dcache_inode_lock);
David Howells214fda12006-03-25 03:06:36 -0800578 de = __d_find_alias(inode, 0);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100579 spin_unlock(&dcache_inode_lock);
David Howells214fda12006-03-25 03:06:36 -0800580 spin_unlock(&dcache_lock);
581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return de;
583}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700584EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586/*
587 * Try to kill dentries associated with this inode.
588 * WARNING: you must own a reference to inode.
589 */
590void d_prune_aliases(struct inode *inode)
591{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700592 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593restart:
594 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100595 spin_lock(&dcache_inode_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700596 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100598 if (!dentry->d_count) {
Nick Piggin23044502011-01-07 17:49:31 +1100599 __dget_locked_dlock(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 __d_drop(dentry);
601 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100602 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 spin_unlock(&dcache_lock);
604 dput(dentry);
605 goto restart;
606 }
607 spin_unlock(&dentry->d_lock);
608 }
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100609 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 spin_unlock(&dcache_lock);
611}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700612EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700615 * Throw away a dentry - free the inode, dput the parent. This requires that
616 * the LRU list has already been removed.
617 *
Miklos Szeredi85864e12007-10-16 23:27:09 -0700618 * Try to prune ancestors as well. This is necessary to prevent
619 * quadratic behavior of shrink_dcache_parent(), but is also expected
620 * to be beneficial in reducing dentry cache fragmentation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100622static void prune_one_dentry(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200623 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100624 __releases(parent->d_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100625 __releases(dcache_inode_lock)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200626 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 __d_drop(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100629 dentry = d_kill(dentry, parent);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700630
631 /*
632 * Prune ancestors. Locking is simpler than in dput(),
633 * because dcache_lock needs to be taken anyway.
634 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700635 while (dentry) {
Nick Piggin23044502011-01-07 17:49:31 +1100636 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100637 spin_lock(&dcache_inode_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100638again:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100639 spin_lock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100640 if (IS_ROOT(dentry))
641 parent = NULL;
642 else
643 parent = dentry->d_parent;
644 if (parent && !spin_trylock(&parent->d_lock)) {
645 spin_unlock(&dentry->d_lock);
646 goto again;
647 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100648 dentry->d_count--;
649 if (dentry->d_count) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100650 if (parent)
651 spin_unlock(&parent->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100652 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100653 spin_unlock(&dcache_inode_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100654 spin_unlock(&dcache_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700655 return;
Nick Piggin23044502011-01-07 17:49:31 +1100656 }
Miklos Szeredid52b9082007-05-08 00:23:46 -0700657
Christoph Hellwiga4633352010-10-10 05:36:26 -0400658 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700659 __d_drop(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100660 dentry = d_kill(dentry, parent);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400664static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700666 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700667
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400668 while (!list_empty(list)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100669 struct dentry *parent;
670
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400671 dentry = list_entry(list->prev, struct dentry, d_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100672
673 if (!spin_trylock(&dentry->d_lock)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100674relock:
Nick Piggin23044502011-01-07 17:49:31 +1100675 spin_unlock(&dcache_lru_lock);
676 cpu_relax();
677 spin_lock(&dcache_lru_lock);
678 continue;
679 }
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /*
682 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700683 * the LRU because of laziness during lookup. Do not free
684 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100686 if (dentry->d_count) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100687 __dentry_lru_del(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700688 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 continue;
690 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100691 if (IS_ROOT(dentry))
692 parent = NULL;
693 else
694 parent = dentry->d_parent;
695 if (parent && !spin_trylock(&parent->d_lock)) {
696 spin_unlock(&dentry->d_lock);
697 goto relock;
698 }
699 __dentry_lru_del(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100700 spin_unlock(&dcache_lru_lock);
701
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100702 prune_one_dentry(dentry, parent);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100703 /* dcache_lock, dcache_inode_lock and dentry->d_lock dropped */
Nick Piggin23044502011-01-07 17:49:31 +1100704 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100705 spin_lock(&dcache_inode_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100706 spin_lock(&dcache_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400708}
709
710/**
711 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
712 * @sb: superblock to shrink dentry LRU.
713 * @count: number of entries to prune
714 * @flags: flags to control the dentry processing
715 *
716 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
717 */
718static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
719{
720 /* called from prune_dcache() and shrink_dcache_parent() */
721 struct dentry *dentry;
722 LIST_HEAD(referenced);
723 LIST_HEAD(tmp);
724 int cnt = *count;
725
726 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100727 spin_lock(&dcache_inode_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100728relock:
729 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400730 while (!list_empty(&sb->s_dentry_lru)) {
731 dentry = list_entry(sb->s_dentry_lru.prev,
732 struct dentry, d_lru);
733 BUG_ON(dentry->d_sb != sb);
734
Nick Piggin23044502011-01-07 17:49:31 +1100735 if (!spin_trylock(&dentry->d_lock)) {
736 spin_unlock(&dcache_lru_lock);
737 cpu_relax();
738 goto relock;
739 }
740
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400741 /*
742 * If we are honouring the DCACHE_REFERENCED flag and the
743 * dentry has this flag set, don't free it. Clear the flag
744 * and put it back on the LRU.
745 */
Nick Piggin23044502011-01-07 17:49:31 +1100746 if (flags & DCACHE_REFERENCED &&
747 dentry->d_flags & DCACHE_REFERENCED) {
748 dentry->d_flags &= ~DCACHE_REFERENCED;
749 list_move(&dentry->d_lru, &referenced);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400750 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100751 } else {
752 list_move_tail(&dentry->d_lru, &tmp);
753 spin_unlock(&dentry->d_lock);
754 if (!--cnt)
755 break;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400756 }
Nick Piggin23044502011-01-07 17:49:31 +1100757 /* XXX: re-add cond_resched_lock when dcache_lock goes away */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400758 }
759
760 *count = cnt;
761 shrink_dentry_list(&tmp);
762
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700763 if (!list_empty(&referenced))
764 list_splice(&referenced, &sb->s_dentry_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100765 spin_unlock(&dcache_lru_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100766 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 spin_unlock(&dcache_lock);
768}
769
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700770/**
771 * prune_dcache - shrink the dcache
772 * @count: number of entries to try to free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700774 * Shrink the dcache. This is done when we need more memory, or simply when we
775 * need to unmount something (at which point we need to unuse all dentries).
776 *
777 * This function may fail to free any resources if all the dentries are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700779static void prune_dcache(int count)
780{
Al Virodca33252010-07-25 02:31:46 +0400781 struct super_block *sb, *p = NULL;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700782 int w_count;
Nick Piggin86c87492011-01-07 17:49:18 +1100783 int unused = dentry_stat.nr_unused;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700784 int prune_ratio;
785 int pruned;
786
787 if (unused == 0 || count == 0)
788 return;
789 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700790 if (count >= unused)
791 prune_ratio = 1;
792 else
793 prune_ratio = unused / count;
794 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400795 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400796 if (list_empty(&sb->s_instances))
797 continue;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700798 if (sb->s_nr_dentry_unused == 0)
799 continue;
800 sb->s_count++;
801 /* Now, we reclaim unused dentrins with fairness.
802 * We reclaim them same percentage from each superblock.
803 * We calculate number of dentries to scan on this sb
804 * as follows, but the implementation is arranged to avoid
805 * overflows:
806 * number of dentries to scan on this sb =
807 * count * (number of dentries on this sb /
808 * number of dentries in the machine)
809 */
810 spin_unlock(&sb_lock);
811 if (prune_ratio != 1)
812 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
813 else
814 w_count = sb->s_nr_dentry_unused;
815 pruned = w_count;
816 /*
817 * We need to be sure this filesystem isn't being unmounted,
818 * otherwise we could race with generic_shutdown_super(), and
819 * end up holding a reference to an inode while the filesystem
820 * is unmounted. So we try to get s_umount, and make sure
821 * s_root isn't NULL.
822 */
823 if (down_read_trylock(&sb->s_umount)) {
824 if ((sb->s_root != NULL) &&
825 (!list_empty(&sb->s_dentry_lru))) {
826 spin_unlock(&dcache_lock);
827 __shrink_dcache_sb(sb, &w_count,
828 DCACHE_REFERENCED);
829 pruned -= w_count;
830 spin_lock(&dcache_lock);
831 }
832 up_read(&sb->s_umount);
833 }
834 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400835 if (p)
836 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700837 count -= pruned;
Al Virodca33252010-07-25 02:31:46 +0400838 p = sb;
Al Viro79893c12010-03-22 20:27:55 -0400839 /* more work left to do? */
840 if (count <= 0)
841 break;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700842 }
Al Virodca33252010-07-25 02:31:46 +0400843 if (p)
844 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700845 spin_unlock(&sb_lock);
846 spin_unlock(&dcache_lock);
847}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849/**
850 * shrink_dcache_sb - shrink dcache for a superblock
851 * @sb: superblock
852 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400853 * Shrink the dcache for the specified super block. This is used to free
854 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400856void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400858 LIST_HEAD(tmp);
859
860 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100861 spin_lock(&dcache_inode_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100862 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400863 while (!list_empty(&sb->s_dentry_lru)) {
864 list_splice_init(&sb->s_dentry_lru, &tmp);
865 shrink_dentry_list(&tmp);
866 }
Nick Piggin23044502011-01-07 17:49:31 +1100867 spin_unlock(&dcache_lru_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100868 spin_unlock(&dcache_inode_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400869 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700871EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873/*
David Howellsc636ebd2006-10-11 01:22:19 -0700874 * destroy a single subtree of dentries for unmount
875 * - see the comments on shrink_dcache_for_umount() for a description of the
876 * locking
877 */
878static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
879{
880 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700881 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700882
883 BUG_ON(!IS_ROOT(dentry));
884
885 /* detach this root from the system */
886 spin_lock(&dcache_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100887 spin_lock(&dentry->d_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400888 dentry_lru_del(dentry);
David Howellsc636ebd2006-10-11 01:22:19 -0700889 __d_drop(dentry);
Nick Pigginda502952011-01-07 17:49:33 +1100890 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700891 spin_unlock(&dcache_lock);
892
893 for (;;) {
894 /* descend to the first leaf in the current subtree */
895 while (!list_empty(&dentry->d_subdirs)) {
896 struct dentry *loop;
897
898 /* this is a branch with children - detach all of them
899 * from the system in one go */
900 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100901 spin_lock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700902 list_for_each_entry(loop, &dentry->d_subdirs,
903 d_u.d_child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100904 spin_lock_nested(&loop->d_lock,
905 DENTRY_D_LOCK_NESTED);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400906 dentry_lru_del(loop);
David Howellsc636ebd2006-10-11 01:22:19 -0700907 __d_drop(loop);
Nick Pigginda502952011-01-07 17:49:33 +1100908 spin_unlock(&loop->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700909 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100910 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700911 spin_unlock(&dcache_lock);
912
913 /* move to the first child */
914 dentry = list_entry(dentry->d_subdirs.next,
915 struct dentry, d_u.d_child);
916 }
917
918 /* consume the dentries from this leaf up through its parents
919 * until we find one with children or run out altogether */
920 do {
921 struct inode *inode;
922
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100923 if (dentry->d_count != 0) {
David Howellsc636ebd2006-10-11 01:22:19 -0700924 printk(KERN_ERR
925 "BUG: Dentry %p{i=%lx,n=%s}"
926 " still in use (%d)"
927 " [unmount of %s %s]\n",
928 dentry,
929 dentry->d_inode ?
930 dentry->d_inode->i_ino : 0UL,
931 dentry->d_name.name,
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100932 dentry->d_count,
David Howellsc636ebd2006-10-11 01:22:19 -0700933 dentry->d_sb->s_type->name,
934 dentry->d_sb->s_id);
935 BUG();
936 }
937
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100938 if (IS_ROOT(dentry)) {
David Howellsc636ebd2006-10-11 01:22:19 -0700939 parent = NULL;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100940 list_del(&dentry->d_u.d_child);
941 } else {
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900942 parent = dentry->d_parent;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100943 spin_lock(&parent->d_lock);
944 parent->d_count--;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100945 list_del(&dentry->d_u.d_child);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100946 spin_unlock(&parent->d_lock);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900947 }
David Howellsc636ebd2006-10-11 01:22:19 -0700948
David Howellsf8713572006-10-28 10:38:46 -0700949 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700950
951 inode = dentry->d_inode;
952 if (inode) {
953 dentry->d_inode = NULL;
954 list_del_init(&dentry->d_alias);
955 if (dentry->d_op && dentry->d_op->d_iput)
956 dentry->d_op->d_iput(dentry, inode);
957 else
958 iput(inode);
959 }
960
961 d_free(dentry);
962
963 /* finished when we fall off the top of the tree,
964 * otherwise we ascend to the parent and move to the
965 * next sibling if there is one */
966 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400967 return;
David Howellsc636ebd2006-10-11 01:22:19 -0700968 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700969 } while (list_empty(&dentry->d_subdirs));
970
971 dentry = list_entry(dentry->d_subdirs.next,
972 struct dentry, d_u.d_child);
973 }
974}
975
976/*
977 * destroy the dentries attached to a superblock on unmounting
978 * - we don't need to use dentry->d_lock, and only need dcache_lock when
979 * removing the dentry from the system lists and hashes because:
980 * - the superblock is detached from all mountings and open files, so the
981 * dentry trees will not be rearranged by the VFS
982 * - s_umount is write-locked, so the memory pressure shrinker will ignore
983 * any dentries belonging to this superblock that it comes across
984 * - the filesystem itself is no longer permitted to rearrange the dentries
985 * in this superblock
986 */
987void shrink_dcache_for_umount(struct super_block *sb)
988{
989 struct dentry *dentry;
990
991 if (down_read_trylock(&sb->s_umount))
992 BUG();
993
994 dentry = sb->s_root;
995 sb->s_root = NULL;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100996 spin_lock(&dentry->d_lock);
997 dentry->d_count--;
998 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700999 shrink_dcache_for_umount_subtree(dentry);
1000
1001 while (!hlist_empty(&sb->s_anon)) {
1002 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
1003 shrink_dcache_for_umount_subtree(dentry);
1004 }
1005}
1006
1007/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 * Search for at least 1 mount point in the dentry's subdirs.
1009 * We descend to the next level whenever the d_subdirs
1010 * list is non-empty and continue searching.
1011 */
1012
1013/**
1014 * have_submounts - check for mounts over a dentry
1015 * @parent: dentry to check.
1016 *
1017 * Return true if the parent or its subdirectories contain
1018 * a mount point
1019 */
1020
1021int have_submounts(struct dentry *parent)
1022{
1023 struct dentry *this_parent = parent;
1024 struct list_head *next;
1025
1026 spin_lock(&dcache_lock);
1027 if (d_mountpoint(parent))
1028 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001029 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030repeat:
1031 next = this_parent->d_subdirs.next;
1032resume:
1033 while (next != &this_parent->d_subdirs) {
1034 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001035 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001037
1038 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /* Have we found a mount point ? */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001040 if (d_mountpoint(dentry)) {
1041 spin_unlock(&dentry->d_lock);
1042 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001046 spin_unlock(&this_parent->d_lock);
1047 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001049 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 goto repeat;
1051 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001052 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054 /*
1055 * All done at this level ... ascend and resume the search.
1056 */
1057 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001058 next = this_parent->d_u.d_child.next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001059 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 this_parent = this_parent->d_parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001061 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto resume;
1063 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001064 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 spin_unlock(&dcache_lock);
1066 return 0; /* No mount points found in tree */
1067positive:
1068 spin_unlock(&dcache_lock);
1069 return 1;
1070}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001071EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073/*
1074 * Search the dentry child list for the specified parent,
1075 * and move any unused dentries to the end of the unused
1076 * list for prune_dcache(). We descend to the next level
1077 * whenever the d_subdirs list is non-empty and continue
1078 * searching.
1079 *
1080 * It returns zero iff there are no unused children,
1081 * otherwise it returns the number of children moved to
1082 * the end of the unused list. This may not be the total
1083 * number of unused children, because select_parent can
1084 * drop the lock and return early due to latency
1085 * constraints.
1086 */
1087static int select_parent(struct dentry * parent)
1088{
1089 struct dentry *this_parent = parent;
1090 struct list_head *next;
1091 int found = 0;
1092
1093 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001094 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095repeat:
1096 next = this_parent->d_subdirs.next;
1097resume:
1098 while (next != &this_parent->d_subdirs) {
1099 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001100 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001102 BUG_ON(this_parent == dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001104 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Nick Piggin23044502011-01-07 17:49:31 +11001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /*
1107 * move only zero ref count dentries to the end
1108 * of the unused list for prune_dcache
1109 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001110 if (!dentry->d_count) {
Christoph Hellwiga4633352010-10-10 05:36:26 -04001111 dentry_lru_move_tail(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 found++;
Christoph Hellwiga4633352010-10-10 05:36:26 -04001113 } else {
1114 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116
1117 /*
1118 * We can return to the caller if we have found some (this
1119 * ensures forward progress). We'll be coming back to find
1120 * the rest.
1121 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001122 if (found && need_resched()) {
1123 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 goto out;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /*
1128 * Descend a level if the d_subdirs list is non-empty.
1129 */
1130 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001131 spin_unlock(&this_parent->d_lock);
1132 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001134 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 goto repeat;
1136 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001137
1138 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 /*
1141 * All done at this level ... ascend and resume the search.
1142 */
1143 if (this_parent != parent) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001144 struct dentry *tmp;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001145 next = this_parent->d_u.d_child.next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001146 tmp = this_parent->d_parent;
1147 spin_unlock(&this_parent->d_lock);
1148 BUG_ON(tmp == this_parent);
1149 this_parent = tmp;
1150 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 goto resume;
1152 }
1153out:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001154 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 spin_unlock(&dcache_lock);
1156 return found;
1157}
1158
1159/**
1160 * shrink_dcache_parent - prune dcache
1161 * @parent: parent of entries to prune
1162 *
1163 * Prune the dcache to remove unused children of the parent dentry.
1164 */
1165
1166void shrink_dcache_parent(struct dentry * parent)
1167{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001168 struct super_block *sb = parent->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int found;
1170
1171 while ((found = select_parent(parent)) != 0)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001172 __shrink_dcache_sb(sb, &found, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001174EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176/*
1177 * Scan `nr' dentries and return the number which remain.
1178 *
1179 * We need to avoid reentering the filesystem if the caller is performing a
1180 * GFP_NOFS allocation attempt. One example deadlock is:
1181 *
1182 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1183 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1184 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1185 *
1186 * In this case we return -1 to tell the caller that we baled.
1187 */
Dave Chinner7f8275d2010-07-19 14:56:17 +10001188static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 if (nr) {
1191 if (!(gfp_mask & __GFP_FS))
1192 return -1;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001193 prune_dcache(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001195
Nick Piggin86c87492011-01-07 17:49:18 +11001196 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Rusty Russell8e1f9362007-07-17 04:03:17 -07001199static struct shrinker dcache_shrinker = {
1200 .shrink = shrink_dcache_memory,
1201 .seeks = DEFAULT_SEEKS,
1202};
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204/**
1205 * d_alloc - allocate a dcache entry
1206 * @parent: parent of entry to allocate
1207 * @name: qstr of the name
1208 *
1209 * Allocates a dentry. It returns %NULL if there is insufficient memory
1210 * available. On a success the dentry is returned. The name passed in is
1211 * copied and the copy passed in may be reused after this call.
1212 */
1213
1214struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1215{
1216 struct dentry *dentry;
1217 char *dname;
1218
Mel Gormane12ba742007-10-16 01:25:52 -07001219 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (!dentry)
1221 return NULL;
1222
1223 if (name->len > DNAME_INLINE_LEN-1) {
1224 dname = kmalloc(name->len + 1, GFP_KERNEL);
1225 if (!dname) {
1226 kmem_cache_free(dentry_cache, dentry);
1227 return NULL;
1228 }
1229 } else {
1230 dname = dentry->d_iname;
1231 }
1232 dentry->d_name.name = dname;
1233
1234 dentry->d_name.len = name->len;
1235 dentry->d_name.hash = name->hash;
1236 memcpy(dname, name->name, name->len);
1237 dname[name->len] = 0;
1238
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001239 dentry->d_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 dentry->d_flags = DCACHE_UNHASHED;
1241 spin_lock_init(&dentry->d_lock);
1242 dentry->d_inode = NULL;
1243 dentry->d_parent = NULL;
1244 dentry->d_sb = NULL;
1245 dentry->d_op = NULL;
1246 dentry->d_fsdata = NULL;
1247 dentry->d_mounted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 INIT_HLIST_NODE(&dentry->d_hash);
1249 INIT_LIST_HEAD(&dentry->d_lru);
1250 INIT_LIST_HEAD(&dentry->d_subdirs);
1251 INIT_LIST_HEAD(&dentry->d_alias);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001252 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 if (parent) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001255 spin_lock(&dcache_lock);
1256 spin_lock(&parent->d_lock);
1257 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1258 dentry->d_parent = dget_dlock(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 dentry->d_sb = parent->d_sb;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001260 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001261 spin_unlock(&dentry->d_lock);
1262 spin_unlock(&parent->d_lock);
1263 spin_unlock(&dcache_lock);
1264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Nick Piggin3e880fb2011-01-07 17:49:19 +11001266 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return dentry;
1269}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001270EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1273{
1274 struct qstr q;
1275
1276 q.name = name;
1277 q.len = strlen(name);
1278 q.hash = full_name_hash(q.name, q.len);
1279 return d_alloc(parent, &q);
1280}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001281EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001283/* the caller must hold dcache_lock */
1284static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1285{
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001286 spin_lock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001287 if (inode)
1288 list_add(&dentry->d_alias, &inode->i_dentry);
1289 dentry->d_inode = inode;
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001290 spin_unlock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001291 fsnotify_d_instantiate(dentry, inode);
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294/**
1295 * d_instantiate - fill in inode information for a dentry
1296 * @entry: dentry to complete
1297 * @inode: inode to attach to this dentry
1298 *
1299 * Fill in inode information in the entry.
1300 *
1301 * This turns negative dentries into productive full members
1302 * of society.
1303 *
1304 * NOTE! This assumes that the inode count has been incremented
1305 * (or otherwise set) by the caller to indicate that it is now
1306 * in use by the dcache.
1307 */
1308
1309void d_instantiate(struct dentry *entry, struct inode * inode)
1310{
Eric Sesterhenn28133c72006-03-26 18:25:39 +02001311 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001313 spin_lock(&dcache_inode_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001314 __d_instantiate(entry, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001315 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 spin_unlock(&dcache_lock);
1317 security_d_instantiate(entry, inode);
1318}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001319EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321/**
1322 * d_instantiate_unique - instantiate a non-aliased dentry
1323 * @entry: dentry to instantiate
1324 * @inode: inode to attach to this dentry
1325 *
1326 * Fill in inode information in the entry. On success, it returns NULL.
1327 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001328 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 *
1330 * Note that in order to avoid conflicts with rename() etc, the caller
1331 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001332 *
1333 * This also assumes that the inode count has been incremented
1334 * (or otherwise set) by the caller to indicate that it is now
1335 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 */
David Howells770bfad2006-08-22 20:06:07 -04001337static struct dentry *__d_instantiate_unique(struct dentry *entry,
1338 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct dentry *alias;
1341 int len = entry->d_name.len;
1342 const char *name = entry->d_name.name;
1343 unsigned int hash = entry->d_name.hash;
1344
David Howells770bfad2006-08-22 20:06:07 -04001345 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001346 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001347 return NULL;
1348 }
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1351 struct qstr *qstr = &alias->d_name;
1352
1353 if (qstr->hash != hash)
1354 continue;
1355 if (alias->d_parent != entry->d_parent)
1356 continue;
1357 if (qstr->len != len)
1358 continue;
1359 if (memcmp(qstr->name, name, len))
1360 continue;
1361 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001362 return alias;
1363 }
David Howells770bfad2006-08-22 20:06:07 -04001364
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001365 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001366 return NULL;
1367}
David Howells770bfad2006-08-22 20:06:07 -04001368
1369struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1370{
1371 struct dentry *result;
1372
1373 BUG_ON(!list_empty(&entry->d_alias));
1374
1375 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001376 spin_lock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04001377 result = __d_instantiate_unique(entry, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001378 spin_unlock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04001379 spin_unlock(&dcache_lock);
1380
1381 if (!result) {
1382 security_d_instantiate(entry, inode);
1383 return NULL;
1384 }
1385
1386 BUG_ON(!d_unhashed(result));
1387 iput(inode);
1388 return result;
1389}
1390
Linus Torvalds1da177e2005-04-16 15:20:36 -07001391EXPORT_SYMBOL(d_instantiate_unique);
1392
1393/**
1394 * d_alloc_root - allocate root dentry
1395 * @root_inode: inode to allocate the root for
1396 *
1397 * Allocate a root ("/") dentry for the inode given. The inode is
1398 * instantiated and returned. %NULL is returned if there is insufficient
1399 * memory or the inode passed is %NULL.
1400 */
1401
1402struct dentry * d_alloc_root(struct inode * root_inode)
1403{
1404 struct dentry *res = NULL;
1405
1406 if (root_inode) {
1407 static const struct qstr name = { .name = "/", .len = 1 };
1408
1409 res = d_alloc(NULL, &name);
1410 if (res) {
1411 res->d_sb = root_inode->i_sb;
1412 res->d_parent = res;
1413 d_instantiate(res, root_inode);
1414 }
1415 }
1416 return res;
1417}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001418EXPORT_SYMBOL(d_alloc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001419
1420static inline struct hlist_head *d_hash(struct dentry *parent,
1421 unsigned long hash)
1422{
1423 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1424 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1425 return dentry_hashtable + (hash & D_HASHMASK);
1426}
1427
1428/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001429 * d_obtain_alias - find or allocate a dentry for a given inode
1430 * @inode: inode to allocate the dentry for
1431 *
1432 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1433 * similar open by handle operations. The returned dentry may be anonymous,
1434 * or may have a full name (if the inode was already in the cache).
1435 *
1436 * When called on a directory inode, we must ensure that the inode only ever
1437 * has one dentry. If a dentry is found, that is returned instead of
1438 * allocating a new one.
1439 *
1440 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001441 * to the dentry. In case of an error the reference on the inode is released.
1442 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1443 * be passed in and will be the error will be propagate to the return value,
1444 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001445 */
1446struct dentry *d_obtain_alias(struct inode *inode)
1447{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001448 static const struct qstr anonstring = { .name = "" };
1449 struct dentry *tmp;
1450 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001451
1452 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001453 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001454 if (IS_ERR(inode))
1455 return ERR_CAST(inode);
1456
Christoph Hellwig9308a612008-08-11 15:49:12 +02001457 res = d_find_alias(inode);
1458 if (res)
1459 goto out_iput;
1460
1461 tmp = d_alloc(NULL, &anonstring);
1462 if (!tmp) {
1463 res = ERR_PTR(-ENOMEM);
1464 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001465 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001466 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1467
1468 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001469 spin_lock(&dcache_inode_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001470 res = __d_find_alias(inode, 0);
1471 if (res) {
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001472 spin_unlock(&dcache_inode_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001473 spin_unlock(&dcache_lock);
1474 dput(tmp);
1475 goto out_iput;
1476 }
1477
1478 /* attach a disconnected dentry */
1479 spin_lock(&tmp->d_lock);
1480 tmp->d_sb = inode->i_sb;
1481 tmp->d_inode = inode;
1482 tmp->d_flags |= DCACHE_DISCONNECTED;
1483 tmp->d_flags &= ~DCACHE_UNHASHED;
1484 list_add(&tmp->d_alias, &inode->i_dentry);
Nick Piggin789680d2011-01-07 17:49:30 +11001485 spin_lock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001486 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
Nick Piggin789680d2011-01-07 17:49:30 +11001487 spin_unlock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001488 spin_unlock(&tmp->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001489 spin_unlock(&dcache_inode_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001490
1491 spin_unlock(&dcache_lock);
1492 return tmp;
1493
1494 out_iput:
1495 iput(inode);
1496 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001497}
Benny Halevyadc48722009-02-27 14:02:59 -08001498EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001499
1500/**
1501 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1502 * @inode: the inode which may have a disconnected dentry
1503 * @dentry: a negative dentry which we want to point to the inode.
1504 *
1505 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1506 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1507 * and return it, else simply d_add the inode to the dentry and return NULL.
1508 *
1509 * This is needed in the lookup routine of any filesystem that is exportable
1510 * (via knfsd) so that we can build dcache paths to directories effectively.
1511 *
1512 * If a dentry was found and moved, then it is returned. Otherwise NULL
1513 * is returned. This matches the expected return value of ->lookup.
1514 *
1515 */
1516struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1517{
1518 struct dentry *new = NULL;
1519
NeilBrown21c0d8f2006-10-04 02:16:16 -07001520 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001521 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001522 spin_lock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001523 new = __d_find_alias(inode, 1);
1524 if (new) {
1525 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001526 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527 spin_unlock(&dcache_lock);
1528 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001529 d_move(new, dentry);
1530 iput(inode);
1531 } else {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001532 /* already taking dcache_lock, so d_add() by hand */
1533 __d_instantiate(dentry, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001534 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001535 spin_unlock(&dcache_lock);
1536 security_d_instantiate(dentry, inode);
1537 d_rehash(dentry);
1538 }
1539 } else
1540 d_add(dentry, inode);
1541 return new;
1542}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001543EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001544
Barry Naujok94035402008-05-21 16:50:46 +10001545/**
1546 * d_add_ci - lookup or allocate new dentry with case-exact name
1547 * @inode: the inode case-insensitive lookup has found
1548 * @dentry: the negative dentry that was passed to the parent's lookup func
1549 * @name: the case-exact name to be associated with the returned dentry
1550 *
1551 * This is to avoid filling the dcache with case-insensitive names to the
1552 * same inode, only the actual correct case is stored in the dcache for
1553 * case-insensitive filesystems.
1554 *
1555 * For a case-insensitive lookup match and if the the case-exact dentry
1556 * already exists in in the dcache, use it and return it.
1557 *
1558 * If no entry exists with the exact case name, allocate new dentry with
1559 * the exact case, and return the spliced entry.
1560 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001561struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001562 struct qstr *name)
1563{
1564 int error;
1565 struct dentry *found;
1566 struct dentry *new;
1567
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001568 /*
1569 * First check if a dentry matching the name already exists,
1570 * if not go ahead and create it now.
1571 */
Barry Naujok94035402008-05-21 16:50:46 +10001572 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001573 if (!found) {
1574 new = d_alloc(dentry->d_parent, name);
1575 if (!new) {
1576 error = -ENOMEM;
1577 goto err_out;
1578 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001579
Barry Naujok94035402008-05-21 16:50:46 +10001580 found = d_splice_alias(inode, new);
1581 if (found) {
1582 dput(new);
1583 return found;
1584 }
1585 return new;
1586 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001587
1588 /*
1589 * If a matching dentry exists, and it's not negative use it.
1590 *
1591 * Decrement the reference count to balance the iget() done
1592 * earlier on.
1593 */
Barry Naujok94035402008-05-21 16:50:46 +10001594 if (found->d_inode) {
1595 if (unlikely(found->d_inode != inode)) {
1596 /* This can't happen because bad inodes are unhashed. */
1597 BUG_ON(!is_bad_inode(inode));
1598 BUG_ON(!is_bad_inode(found->d_inode));
1599 }
Barry Naujok94035402008-05-21 16:50:46 +10001600 iput(inode);
1601 return found;
1602 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001603
Barry Naujok94035402008-05-21 16:50:46 +10001604 /*
1605 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001606 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001607 */
Barry Naujok94035402008-05-21 16:50:46 +10001608 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001609 spin_lock(&dcache_inode_lock);
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001610 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001611 __d_instantiate(found, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001612 spin_unlock(&dcache_inode_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001613 spin_unlock(&dcache_lock);
1614 security_d_instantiate(found, inode);
1615 return found;
1616 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001617
Barry Naujok94035402008-05-21 16:50:46 +10001618 /*
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001619 * In case a directory already has a (disconnected) entry grab a
1620 * reference to it, move it in place and use it.
Barry Naujok94035402008-05-21 16:50:46 +10001621 */
1622 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1623 dget_locked(new);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001624 spin_unlock(&dcache_inode_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001625 spin_unlock(&dcache_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001626 security_d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001627 d_move(new, found);
Barry Naujok94035402008-05-21 16:50:46 +10001628 iput(inode);
Barry Naujok94035402008-05-21 16:50:46 +10001629 dput(found);
Barry Naujok94035402008-05-21 16:50:46 +10001630 return new;
1631
1632err_out:
1633 iput(inode);
1634 return ERR_PTR(error);
1635}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001636EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001637
1638/**
1639 * d_lookup - search for a dentry
1640 * @parent: parent dentry
1641 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001642 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001643 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001644 * d_lookup searches the children of the parent dentry for the name in
1645 * question. If the dentry is found its reference count is incremented and the
1646 * dentry is returned. The caller must use dput to free the entry when it has
1647 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001649struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1650{
1651 struct dentry * dentry = NULL;
1652 unsigned long seq;
1653
1654 do {
1655 seq = read_seqbegin(&rename_lock);
1656 dentry = __d_lookup(parent, name);
1657 if (dentry)
1658 break;
1659 } while (read_seqretry(&rename_lock, seq));
1660 return dentry;
1661}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001662EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Nick Pigginb04f7842010-08-18 04:37:34 +10001664/*
1665 * __d_lookup - search for a dentry (racy)
1666 * @parent: parent dentry
1667 * @name: qstr of name we wish to find
1668 * Returns: dentry, or NULL
1669 *
1670 * __d_lookup is like d_lookup, however it may (rarely) return a
1671 * false-negative result due to unrelated rename activity.
1672 *
1673 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1674 * however it must be used carefully, eg. with a following d_lookup in
1675 * the case of failure.
1676 *
1677 * __d_lookup callers must be commented.
1678 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001679struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1680{
1681 unsigned int len = name->len;
1682 unsigned int hash = name->hash;
1683 const unsigned char *str = name->name;
1684 struct hlist_head *head = d_hash(parent,hash);
1685 struct dentry *found = NULL;
1686 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001687 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001688
Nick Pigginb04f7842010-08-18 04:37:34 +10001689 /*
1690 * The hash list is protected using RCU.
1691 *
1692 * Take d_lock when comparing a candidate dentry, to avoid races
1693 * with d_move().
1694 *
1695 * It is possible that concurrent renames can mess up our list
1696 * walk here and result in missing our dentry, resulting in the
1697 * false-negative result. d_lookup() protects against concurrent
1698 * renames using rename_lock seqlock.
1699 *
1700 * See Documentation/vfs/dcache-locking.txt for more details.
1701 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001702 rcu_read_lock();
1703
Paul E. McKenney665a7582005-11-07 00:59:17 -08001704 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001705 struct qstr *qstr;
1706
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 if (dentry->d_name.hash != hash)
1708 continue;
1709 if (dentry->d_parent != parent)
1710 continue;
1711
1712 spin_lock(&dentry->d_lock);
1713
1714 /*
1715 * Recheck the dentry after taking the lock - d_move may have
Nick Pigginb04f7842010-08-18 04:37:34 +10001716 * changed things. Don't bother checking the hash because
1717 * we're about to compare the whole name anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001718 */
1719 if (dentry->d_parent != parent)
1720 goto next;
1721
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001722 /* non-existing due to RCU? */
1723 if (d_unhashed(dentry))
1724 goto next;
1725
Linus Torvalds1da177e2005-04-16 15:20:36 -07001726 /*
1727 * It is safe to compare names since d_move() cannot
1728 * change the qstr (protected by d_lock).
1729 */
1730 qstr = &dentry->d_name;
1731 if (parent->d_op && parent->d_op->d_compare) {
Nick Piggin621e1552011-01-07 17:49:27 +11001732 if (parent->d_op->d_compare(parent, parent->d_inode,
1733 dentry, dentry->d_inode,
1734 qstr->len, qstr->name, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001735 goto next;
1736 } else {
1737 if (qstr->len != len)
1738 goto next;
1739 if (memcmp(qstr->name, str, len))
1740 goto next;
1741 }
1742
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001743 dentry->d_count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001744 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001745 spin_unlock(&dentry->d_lock);
1746 break;
1747next:
1748 spin_unlock(&dentry->d_lock);
1749 }
1750 rcu_read_unlock();
1751
1752 return found;
1753}
1754
1755/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001756 * d_hash_and_lookup - hash the qstr then search for a dentry
1757 * @dir: Directory to search in
1758 * @name: qstr of name we wish to find
1759 *
1760 * On hash failure or on lookup failure NULL is returned.
1761 */
1762struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1763{
1764 struct dentry *dentry = NULL;
1765
1766 /*
1767 * Check for a fs-specific hash function. Note that we must
1768 * calculate the standard hash first, as the d_op->d_hash()
1769 * routine may choose to leave the hash value unchanged.
1770 */
1771 name->hash = full_name_hash(name->name, name->len);
1772 if (dir->d_op && dir->d_op->d_hash) {
Nick Pigginb1e6a012011-01-07 17:49:28 +11001773 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001774 goto out;
1775 }
1776 dentry = d_lookup(dir, name);
1777out:
1778 return dentry;
1779}
1780
1781/**
Nick Piggin786a5e12011-01-07 17:49:16 +11001782 * d_validate - verify dentry provided from insecure source (deprecated)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 * @dentry: The dentry alleged to be valid child of @dparent
1784 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 *
1786 * An insecure source has sent us a dentry, here we verify it and dget() it.
1787 * This is used by ncpfs in its readdir implementation.
1788 * Zero is returned in the dentry is invalid.
Nick Piggin786a5e12011-01-07 17:49:16 +11001789 *
1790 * This function is slow for big directories, and deprecated, do not use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001791 */
Nick Piggind3a23e12011-01-05 20:01:21 +11001792int d_validate(struct dentry *dentry, struct dentry *dparent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001793{
Nick Piggin786a5e12011-01-07 17:49:16 +11001794 struct dentry *child;
Nick Piggind3a23e12011-01-05 20:01:21 +11001795
1796 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001797 spin_lock(&dparent->d_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001798 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1799 if (dentry == child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001800 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1801 __dget_locked_dlock(dentry);
1802 spin_unlock(&dentry->d_lock);
1803 spin_unlock(&dparent->d_lock);
Nick Piggind3a23e12011-01-05 20:01:21 +11001804 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805 return 1;
1806 }
1807 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001808 spin_unlock(&dparent->d_lock);
Nick Piggind3a23e12011-01-05 20:01:21 +11001809 spin_unlock(&dcache_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001810
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 return 0;
1812}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001813EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001814
1815/*
1816 * When a file is deleted, we have two options:
1817 * - turn this dentry into a negative dentry
1818 * - unhash this dentry and free it.
1819 *
1820 * Usually, we want to just turn this into
1821 * a negative dentry, but if anybody else is
1822 * currently using the dentry or the inode
1823 * we can't do that and we fall back on removing
1824 * it from the hash queues and waiting for
1825 * it to be deleted later when it has no users
1826 */
1827
1828/**
1829 * d_delete - delete a dentry
1830 * @dentry: The dentry to delete
1831 *
1832 * Turn the dentry into a negative dentry if possible, otherwise
1833 * remove it from the hash queues so it can be deleted later
1834 */
1835
1836void d_delete(struct dentry * dentry)
1837{
John McCutchan7a91bf72005-08-08 13:52:16 -04001838 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001839 /*
1840 * Are we the only user?
1841 */
1842 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001843 spin_lock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001845 isdir = S_ISDIR(dentry->d_inode->i_mode);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001846 if (dentry->d_count == 1) {
Al Viro13e3c5e2010-05-21 16:11:04 -04001847 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001848 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001849 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850 return;
1851 }
1852
1853 if (!d_unhashed(dentry))
1854 __d_drop(dentry);
1855
1856 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001857 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001858 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001859
1860 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001862EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863
1864static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1865{
1866
1867 entry->d_flags &= ~DCACHE_UNHASHED;
1868 hlist_add_head_rcu(&entry->d_hash, list);
1869}
1870
David Howells770bfad2006-08-22 20:06:07 -04001871static void _d_rehash(struct dentry * entry)
1872{
1873 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1874}
1875
Linus Torvalds1da177e2005-04-16 15:20:36 -07001876/**
1877 * d_rehash - add an entry back to the hash
1878 * @entry: dentry to add to the hash
1879 *
1880 * Adds a dentry to the hash according to its name.
1881 */
1882
1883void d_rehash(struct dentry * entry)
1884{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001885 spin_lock(&dcache_lock);
1886 spin_lock(&entry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +11001887 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04001888 _d_rehash(entry);
Nick Piggin789680d2011-01-07 17:49:30 +11001889 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 spin_unlock(&entry->d_lock);
1891 spin_unlock(&dcache_lock);
1892}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001893EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894
Nick Pigginfb2d5b82011-01-07 17:49:26 +11001895/**
1896 * dentry_update_name_case - update case insensitive dentry with a new name
1897 * @dentry: dentry to be updated
1898 * @name: new name
1899 *
1900 * Update a case insensitive dentry with new case of name.
1901 *
1902 * dentry must have been returned by d_lookup with name @name. Old and new
1903 * name lengths must match (ie. no d_compare which allows mismatched name
1904 * lengths).
1905 *
1906 * Parent inode i_mutex must be held over d_lookup and into this call (to
1907 * keep renames and concurrent inserts, and readdir(2) away).
1908 */
1909void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
1910{
1911 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
1912 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
1913
1914 spin_lock(&dcache_lock);
1915 spin_lock(&dentry->d_lock);
1916 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
1917 spin_unlock(&dentry->d_lock);
1918 spin_unlock(&dcache_lock);
1919}
1920EXPORT_SYMBOL(dentry_update_name_case);
1921
Linus Torvalds1da177e2005-04-16 15:20:36 -07001922static void switch_names(struct dentry *dentry, struct dentry *target)
1923{
1924 if (dname_external(target)) {
1925 if (dname_external(dentry)) {
1926 /*
1927 * Both external: swap the pointers
1928 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001929 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001930 } else {
1931 /*
1932 * dentry:internal, target:external. Steal target's
1933 * storage and make target internal.
1934 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07001935 memcpy(target->d_iname, dentry->d_name.name,
1936 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001937 dentry->d_name.name = target->d_name.name;
1938 target->d_name.name = target->d_iname;
1939 }
1940 } else {
1941 if (dname_external(dentry)) {
1942 /*
1943 * dentry:external, target:internal. Give dentry's
1944 * storage to target and make dentry internal
1945 */
1946 memcpy(dentry->d_iname, target->d_name.name,
1947 target->d_name.len + 1);
1948 target->d_name.name = dentry->d_name.name;
1949 dentry->d_name.name = dentry->d_iname;
1950 } else {
1951 /*
1952 * Both are internal. Just copy target to dentry
1953 */
1954 memcpy(dentry->d_iname, target->d_name.name,
1955 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05001956 dentry->d_name.len = target->d_name.len;
1957 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001958 }
1959 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001960 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001961}
1962
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001963static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
1964{
1965 /*
1966 * XXXX: do we really need to take target->d_lock?
1967 */
1968 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
1969 spin_lock(&target->d_parent->d_lock);
1970 else {
1971 if (d_ancestor(dentry->d_parent, target->d_parent)) {
1972 spin_lock(&dentry->d_parent->d_lock);
1973 spin_lock_nested(&target->d_parent->d_lock,
1974 DENTRY_D_LOCK_NESTED);
1975 } else {
1976 spin_lock(&target->d_parent->d_lock);
1977 spin_lock_nested(&dentry->d_parent->d_lock,
1978 DENTRY_D_LOCK_NESTED);
1979 }
1980 }
1981 if (target < dentry) {
1982 spin_lock_nested(&target->d_lock, 2);
1983 spin_lock_nested(&dentry->d_lock, 3);
1984 } else {
1985 spin_lock_nested(&dentry->d_lock, 2);
1986 spin_lock_nested(&target->d_lock, 3);
1987 }
1988}
1989
1990static void dentry_unlock_parents_for_move(struct dentry *dentry,
1991 struct dentry *target)
1992{
1993 if (target->d_parent != dentry->d_parent)
1994 spin_unlock(&dentry->d_parent->d_lock);
1995 if (target->d_parent != target)
1996 spin_unlock(&target->d_parent->d_lock);
1997}
1998
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002000 * When switching names, the actual string doesn't strictly have to
2001 * be preserved in the target - because we're dropping the target
2002 * anyway. As such, we can just do a simple memcpy() to copy over
2003 * the new name before we switch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004 *
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002005 * Note that we have to be a lot more careful about getting the hash
2006 * switched - we have to switch the hash value properly even if it
2007 * then no longer matches the actual (corrupted) string of the target.
2008 * The hash value has to match the hash queue that the dentry is on..
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002010/*
2011 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012 * @dentry: entry to move
2013 * @target: new dentry
2014 *
2015 * Update the dcache to reflect the move of a file name. Negative
2016 * dcache entries should not be moved in this way.
2017 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002018static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002019{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002020 if (!dentry->d_inode)
2021 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2022
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002023 BUG_ON(d_ancestor(dentry, target));
2024 BUG_ON(d_ancestor(target, dentry));
2025
Linus Torvalds1da177e2005-04-16 15:20:36 -07002026 write_seqlock(&rename_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002027
2028 dentry_lock_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002029
2030 /* Move the dentry to the target hash queue, if on different bucket */
Nick Piggin789680d2011-01-07 17:49:30 +11002031 spin_lock(&dcache_hash_lock);
2032 if (!d_unhashed(dentry))
2033 hlist_del_rcu(&dentry->d_hash);
2034 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
2035 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002036
2037 /* Unhash the target: dput() will then get rid of it */
2038 __d_drop(target);
2039
Eric Dumazet5160ee62006-01-08 01:03:32 -08002040 list_del(&dentry->d_u.d_child);
2041 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002042
2043 /* Switch the names.. */
2044 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002045 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002046
2047 /* ... and switch the parents */
2048 if (IS_ROOT(dentry)) {
2049 dentry->d_parent = target->d_parent;
2050 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002051 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002052 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002053 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054
2055 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08002056 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 }
2058
Eric Dumazet5160ee62006-01-08 01:03:32 -08002059 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002060
2061 dentry_unlock_parents_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08002063 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002064 spin_unlock(&dentry->d_lock);
2065 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002066}
2067
2068/**
2069 * d_move - move a dentry
2070 * @dentry: entry to move
2071 * @target: new dentry
2072 *
2073 * Update the dcache to reflect the move of a file name. Negative
2074 * dcache entries should not be moved in this way.
2075 */
2076
2077void d_move(struct dentry * dentry, struct dentry * target)
2078{
2079 spin_lock(&dcache_lock);
2080 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002081 spin_unlock(&dcache_lock);
2082}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002083EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002084
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002085/**
2086 * d_ancestor - search for an ancestor
2087 * @p1: ancestor dentry
2088 * @p2: child dentry
2089 *
2090 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2091 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07002092 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002093struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002094{
2095 struct dentry *p;
2096
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09002097 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002098 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002099 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002100 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002101 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002102}
2103
2104/*
2105 * This helper attempts to cope with remotely renamed directories
2106 *
2107 * It assumes that the caller is already holding
2108 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
2109 *
2110 * Note: If ever the locking in lock_rename() changes, then please
2111 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07002112 */
2113static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002114 __releases(dcache_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002115 __releases(dcache_inode_lock)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002116{
2117 struct mutex *m1 = NULL, *m2 = NULL;
2118 struct dentry *ret;
2119
2120 /* If alias and dentry share a parent, then no extra locks required */
2121 if (alias->d_parent == dentry->d_parent)
2122 goto out_unalias;
2123
2124 /* Check for loops */
2125 ret = ERR_PTR(-ELOOP);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002126 if (d_ancestor(alias, dentry))
Trond Myklebust9eaef272006-10-21 10:24:20 -07002127 goto out_err;
2128
2129 /* See lock_rename() */
2130 ret = ERR_PTR(-EBUSY);
2131 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2132 goto out_err;
2133 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2134 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2135 goto out_err;
2136 m2 = &alias->d_parent->d_inode->i_mutex;
2137out_unalias:
2138 d_move_locked(alias, dentry);
2139 ret = alias;
2140out_err:
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002141 spin_unlock(&dcache_inode_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002142 spin_unlock(&dcache_lock);
2143 if (m2)
2144 mutex_unlock(m2);
2145 if (m1)
2146 mutex_unlock(m1);
2147 return ret;
2148}
2149
2150/*
David Howells770bfad2006-08-22 20:06:07 -04002151 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2152 * named dentry in place of the dentry to be replaced.
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002153 * returns with anon->d_lock held!
David Howells770bfad2006-08-22 20:06:07 -04002154 */
2155static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2156{
2157 struct dentry *dparent, *aparent;
2158
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002159 dentry_lock_for_move(anon, dentry);
David Howells770bfad2006-08-22 20:06:07 -04002160
2161 dparent = dentry->d_parent;
2162 aparent = anon->d_parent;
2163
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002164 switch_names(dentry, anon);
2165 swap(dentry->d_name.hash, anon->d_name.hash);
2166
David Howells770bfad2006-08-22 20:06:07 -04002167 dentry->d_parent = (aparent == anon) ? dentry : aparent;
2168 list_del(&dentry->d_u.d_child);
2169 if (!IS_ROOT(dentry))
2170 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2171 else
2172 INIT_LIST_HEAD(&dentry->d_u.d_child);
2173
2174 anon->d_parent = (dparent == dentry) ? anon : dparent;
2175 list_del(&anon->d_u.d_child);
2176 if (!IS_ROOT(anon))
2177 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
2178 else
2179 INIT_LIST_HEAD(&anon->d_u.d_child);
2180
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002181 dentry_unlock_parents_for_move(anon, dentry);
2182 spin_unlock(&dentry->d_lock);
2183
2184 /* anon->d_lock still locked, returns locked */
David Howells770bfad2006-08-22 20:06:07 -04002185 anon->d_flags &= ~DCACHE_DISCONNECTED;
2186}
2187
2188/**
2189 * d_materialise_unique - introduce an inode into the tree
2190 * @dentry: candidate dentry
2191 * @inode: inode to bind to the dentry, to which aliases may be attached
2192 *
2193 * Introduces an dentry into the tree, substituting an extant disconnected
2194 * root directory alias in its place if there is one
2195 */
2196struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2197{
Trond Myklebust9eaef272006-10-21 10:24:20 -07002198 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04002199
2200 BUG_ON(!d_unhashed(dentry));
2201
2202 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002203 spin_lock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04002204
2205 if (!inode) {
2206 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09002207 __d_instantiate(dentry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04002208 goto found_lock;
2209 }
2210
Trond Myklebust9eaef272006-10-21 10:24:20 -07002211 if (S_ISDIR(inode->i_mode)) {
2212 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04002213
Trond Myklebust9eaef272006-10-21 10:24:20 -07002214 /* Does an aliased dentry already exist? */
2215 alias = __d_find_alias(inode, 0);
2216 if (alias) {
2217 actual = alias;
2218 /* Is this an anonymous mountpoint that we could splice
2219 * into our tree? */
2220 if (IS_ROOT(alias)) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002221 __d_materialise_dentry(dentry, alias);
2222 __d_drop(alias);
2223 goto found;
2224 }
2225 /* Nope, but we must(!) avoid directory aliasing */
2226 actual = __d_unalias(dentry, alias);
2227 if (IS_ERR(actual))
2228 dput(alias);
2229 goto out_nolock;
2230 }
David Howells770bfad2006-08-22 20:06:07 -04002231 }
2232
2233 /* Add a unique reference */
2234 actual = __d_instantiate_unique(dentry, inode);
2235 if (!actual)
2236 actual = dentry;
2237 else if (unlikely(!d_unhashed(actual)))
2238 goto shouldnt_be_hashed;
2239
2240found_lock:
2241 spin_lock(&actual->d_lock);
2242found:
Nick Piggin789680d2011-01-07 17:49:30 +11002243 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04002244 _d_rehash(actual);
Nick Piggin789680d2011-01-07 17:49:30 +11002245 spin_unlock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04002246 spin_unlock(&actual->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002247 spin_unlock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04002248 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002249out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04002250 if (actual == dentry) {
2251 security_d_instantiate(dentry, inode);
2252 return NULL;
2253 }
2254
2255 iput(inode);
2256 return actual;
2257
David Howells770bfad2006-08-22 20:06:07 -04002258shouldnt_be_hashed:
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002259 spin_unlock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04002260 spin_unlock(&dcache_lock);
2261 BUG();
David Howells770bfad2006-08-22 20:06:07 -04002262}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002263EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04002264
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002265static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01002266{
2267 *buflen -= namelen;
2268 if (*buflen < 0)
2269 return -ENAMETOOLONG;
2270 *buffer -= namelen;
2271 memcpy(*buffer, str, namelen);
2272 return 0;
2273}
2274
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002275static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2276{
2277 return prepend(buffer, buflen, name->name, name->len);
2278}
2279
Linus Torvalds1da177e2005-04-16 15:20:36 -07002280/**
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002281 * Prepend path string to a buffer
2282 *
2283 * @path: the dentry/vfsmount to report
2284 * @root: root vfsmnt/dentry (may be modified by this function)
2285 * @buffer: pointer to the end of the buffer
2286 * @buflen: pointer to buffer length
2287 *
2288 * Caller holds the dcache_lock.
2289 *
2290 * If path is not reachable from the supplied root, then the value of
2291 * root is changed (without modifying refcounts).
2292 */
2293static int prepend_path(const struct path *path, struct path *root,
2294 char **buffer, int *buflen)
2295{
2296 struct dentry *dentry = path->dentry;
2297 struct vfsmount *vfsmnt = path->mnt;
2298 bool slash = false;
2299 int error = 0;
2300
Nick Piggin99b7db72010-08-18 04:37:39 +10002301 br_read_lock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002302 while (dentry != root->dentry || vfsmnt != root->mnt) {
2303 struct dentry * parent;
2304
2305 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2306 /* Global root? */
2307 if (vfsmnt->mnt_parent == vfsmnt) {
2308 goto global_root;
2309 }
2310 dentry = vfsmnt->mnt_mountpoint;
2311 vfsmnt = vfsmnt->mnt_parent;
2312 continue;
2313 }
2314 parent = dentry->d_parent;
2315 prefetch(parent);
2316 error = prepend_name(buffer, buflen, &dentry->d_name);
2317 if (!error)
2318 error = prepend(buffer, buflen, "/", 1);
2319 if (error)
2320 break;
2321
2322 slash = true;
2323 dentry = parent;
2324 }
2325
2326out:
2327 if (!error && !slash)
2328 error = prepend(buffer, buflen, "/", 1);
2329
Nick Piggin99b7db72010-08-18 04:37:39 +10002330 br_read_unlock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002331 return error;
2332
2333global_root:
2334 /*
2335 * Filesystems needing to implement special "root names"
2336 * should do so with ->d_dname()
2337 */
2338 if (IS_ROOT(dentry) &&
2339 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2340 WARN(1, "Root dentry has weird name <%.*s>\n",
2341 (int) dentry->d_name.len, dentry->d_name.name);
2342 }
2343 root->mnt = vfsmnt;
2344 root->dentry = dentry;
2345 goto out;
2346}
2347
2348/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002349 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002350 * @path: the dentry/vfsmount to report
2351 * @root: root vfsmnt/dentry (may be modified by this function)
Randy Dunlapcd956a12010-08-14 13:05:31 -07002352 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002353 * @buflen: buffer length
2354 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002355 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002356 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002357 * Returns a pointer into the buffer or an error code if the
2358 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002359 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002360 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002361 *
2362 * If path is not reachable from the supplied root, then the value of
2363 * root is changed (without modifying refcounts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002364 */
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002365char *__d_path(const struct path *path, struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002366 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002367{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002368 char *res = buf + buflen;
2369 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002370
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002371 prepend(&res, &buflen, "\0", 1);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002372 spin_lock(&dcache_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002373 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002374 spin_unlock(&dcache_lock);
2375
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002376 if (error)
2377 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002378 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002379}
2380
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002381/*
2382 * same as __d_path but appends "(deleted)" for unlinked files.
2383 */
2384static int path_with_deleted(const struct path *path, struct path *root,
2385 char **buf, int *buflen)
2386{
2387 prepend(buf, buflen, "\0", 1);
2388 if (d_unlinked(path->dentry)) {
2389 int error = prepend(buf, buflen, " (deleted)", 10);
2390 if (error)
2391 return error;
2392 }
2393
2394 return prepend_path(path, root, buf, buflen);
2395}
2396
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002397static int prepend_unreachable(char **buffer, int *buflen)
2398{
2399 return prepend(buffer, buflen, "(unreachable)", 13);
2400}
2401
Jan Bluncka03a8a702008-02-14 19:38:32 -08002402/**
2403 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002404 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002405 * @buf: buffer to return value in
2406 * @buflen: buffer length
2407 *
2408 * Convert a dentry into an ASCII path name. If the entry has been deleted
2409 * the string " (deleted)" is appended. Note that this is ambiguous.
2410 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002411 * Returns a pointer into the buffer or an error code if the path was
2412 * too long. Note: Callers should use the returned pointer, not the passed
2413 * in buffer, to use the name! The implementation often starts at an offset
2414 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002415 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002416 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002417 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002418char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002420 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002421 struct path root;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002422 struct path tmp;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002423 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002424
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002425 /*
2426 * We have various synthetic filesystems that never get mounted. On
2427 * these filesystems dentries are never used for lookup purposes, and
2428 * thus don't need to be hashed. They also don't need a name until a
2429 * user wants to identify the object in /proc/pid/fd/. The little hack
2430 * below allows us to generate a name for these objects on demand:
2431 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002432 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2433 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002434
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002435 get_fs_root(current->fs, &root);
Linus Torvalds552ce542007-02-13 12:08:18 -08002436 spin_lock(&dcache_lock);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002437 tmp = root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002438 error = path_with_deleted(path, &tmp, &res, &buflen);
2439 if (error)
2440 res = ERR_PTR(error);
Linus Torvalds552ce542007-02-13 12:08:18 -08002441 spin_unlock(&dcache_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002442 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443 return res;
2444}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002445EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002446
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002447/**
2448 * d_path_with_unreachable - return the path of a dentry
2449 * @path: path to report
2450 * @buf: buffer to return value in
2451 * @buflen: buffer length
2452 *
2453 * The difference from d_path() is that this prepends "(unreachable)"
2454 * to paths which are unreachable from the current process' root.
2455 */
2456char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2457{
2458 char *res = buf + buflen;
2459 struct path root;
2460 struct path tmp;
2461 int error;
2462
2463 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2464 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2465
2466 get_fs_root(current->fs, &root);
2467 spin_lock(&dcache_lock);
2468 tmp = root;
2469 error = path_with_deleted(path, &tmp, &res, &buflen);
2470 if (!error && !path_equal(&tmp, &root))
2471 error = prepend_unreachable(&res, &buflen);
2472 spin_unlock(&dcache_lock);
2473 path_put(&root);
2474 if (error)
2475 res = ERR_PTR(error);
2476
2477 return res;
2478}
2479
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002481 * Helper function for dentry_operations.d_dname() members
2482 */
2483char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2484 const char *fmt, ...)
2485{
2486 va_list args;
2487 char temp[64];
2488 int sz;
2489
2490 va_start(args, fmt);
2491 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2492 va_end(args);
2493
2494 if (sz > sizeof(temp) || sz > buflen)
2495 return ERR_PTR(-ENAMETOOLONG);
2496
2497 buffer += buflen - sz;
2498 return memcpy(buffer, temp, sz);
2499}
2500
2501/*
Ram Pai6092d042008-03-27 13:06:20 +01002502 * Write full pathname from the root of the filesystem into the buffer.
2503 */
Nick Pigginec2447c2011-01-07 17:49:29 +11002504static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002505{
2506 char *end = buf + buflen;
2507 char *retval;
2508
Ram Pai6092d042008-03-27 13:06:20 +01002509 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002510 if (buflen < 1)
2511 goto Elong;
2512 /* Get '/' right */
2513 retval = end-1;
2514 *retval = '/';
2515
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002516 while (!IS_ROOT(dentry)) {
2517 struct dentry *parent = dentry->d_parent;
Ram Pai6092d042008-03-27 13:06:20 +01002518
Ram Pai6092d042008-03-27 13:06:20 +01002519 prefetch(parent);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002520 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
Ram Pai6092d042008-03-27 13:06:20 +01002521 (prepend(&end, &buflen, "/", 1) != 0))
2522 goto Elong;
2523
2524 retval = end;
2525 dentry = parent;
2526 }
Al Viroc1031352010-06-06 22:31:14 -04002527 return retval;
2528Elong:
2529 return ERR_PTR(-ENAMETOOLONG);
2530}
Nick Pigginec2447c2011-01-07 17:49:29 +11002531
2532char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2533{
2534 char *retval;
2535
2536 spin_lock(&dcache_lock);
2537 retval = __dentry_path(dentry, buf, buflen);
2538 spin_unlock(&dcache_lock);
2539
2540 return retval;
2541}
2542EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04002543
2544char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2545{
2546 char *p = NULL;
2547 char *retval;
2548
2549 spin_lock(&dcache_lock);
2550 if (d_unlinked(dentry)) {
2551 p = buf + buflen;
2552 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2553 goto Elong;
2554 buflen++;
2555 }
2556 retval = __dentry_path(dentry, buf, buflen);
Ram Pai6092d042008-03-27 13:06:20 +01002557 spin_unlock(&dcache_lock);
Al Viroc1031352010-06-06 22:31:14 -04002558 if (!IS_ERR(retval) && p)
2559 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002560 return retval;
2561Elong:
2562 spin_unlock(&dcache_lock);
2563 return ERR_PTR(-ENAMETOOLONG);
2564}
2565
2566/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 * NOTE! The user-level library version returns a
2568 * character pointer. The kernel system call just
2569 * returns the length of the buffer filled (which
2570 * includes the ending '\0' character), or a negative
2571 * error value. So libc would do something like
2572 *
2573 * char *getcwd(char * buf, size_t size)
2574 * {
2575 * int retval;
2576 *
2577 * retval = sys_getcwd(buf, size);
2578 * if (retval >= 0)
2579 * return buf;
2580 * errno = -retval;
2581 * return NULL;
2582 * }
2583 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002584SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002585{
Linus Torvalds552ce542007-02-13 12:08:18 -08002586 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002587 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002588 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002589
2590 if (!page)
2591 return -ENOMEM;
2592
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002593 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002594
Linus Torvalds552ce542007-02-13 12:08:18 -08002595 error = -ENOENT;
Linus Torvalds552ce542007-02-13 12:08:18 -08002596 spin_lock(&dcache_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002597 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002598 unsigned long len;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002599 struct path tmp = root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002600 char *cwd = page + PAGE_SIZE;
2601 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002602
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002603 prepend(&cwd, &buflen, "\0", 1);
2604 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
Linus Torvalds552ce542007-02-13 12:08:18 -08002605 spin_unlock(&dcache_lock);
2606
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002607 if (error)
Linus Torvalds552ce542007-02-13 12:08:18 -08002608 goto out;
2609
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002610 /* Unreachable from current root */
2611 if (!path_equal(&tmp, &root)) {
2612 error = prepend_unreachable(&cwd, &buflen);
2613 if (error)
2614 goto out;
2615 }
2616
Linus Torvalds552ce542007-02-13 12:08:18 -08002617 error = -ERANGE;
2618 len = PAGE_SIZE + page - cwd;
2619 if (len <= size) {
2620 error = len;
2621 if (copy_to_user(buf, cwd, len))
2622 error = -EFAULT;
2623 }
2624 } else
2625 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002626
2627out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002628 path_put(&pwd);
2629 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002630 free_page((unsigned long) page);
2631 return error;
2632}
2633
2634/*
2635 * Test whether new_dentry is a subdirectory of old_dentry.
2636 *
2637 * Trivially implemented using the dcache structure
2638 */
2639
2640/**
2641 * is_subdir - is new dentry a subdirectory of old_dentry
2642 * @new_dentry: new dentry
2643 * @old_dentry: old dentry
2644 *
2645 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2646 * Returns 0 otherwise.
2647 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2648 */
2649
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002650int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651{
2652 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002653 unsigned long seq;
2654
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002655 if (new_dentry == old_dentry)
2656 return 1;
2657
2658 /*
2659 * Need rcu_readlock to protect against the d_parent trashing
2660 * due to d_move
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661 */
2662 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002663 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002664 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002665 seq = read_seqbegin(&rename_lock);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002666 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002667 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002668 else
2669 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002670 } while (read_seqretry(&rename_lock, seq));
2671 rcu_read_unlock();
2672
2673 return result;
2674}
2675
Al Viro2096f752010-01-30 13:16:21 -05002676int path_is_under(struct path *path1, struct path *path2)
2677{
2678 struct vfsmount *mnt = path1->mnt;
2679 struct dentry *dentry = path1->dentry;
2680 int res;
Nick Piggin99b7db72010-08-18 04:37:39 +10002681
2682 br_read_lock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002683 if (mnt != path2->mnt) {
2684 for (;;) {
2685 if (mnt->mnt_parent == mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +10002686 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002687 return 0;
2688 }
2689 if (mnt->mnt_parent == path2->mnt)
2690 break;
2691 mnt = mnt->mnt_parent;
2692 }
2693 dentry = mnt->mnt_mountpoint;
2694 }
2695 res = is_subdir(dentry, path2->dentry);
Nick Piggin99b7db72010-08-18 04:37:39 +10002696 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002697 return res;
2698}
2699EXPORT_SYMBOL(path_is_under);
2700
Linus Torvalds1da177e2005-04-16 15:20:36 -07002701void d_genocide(struct dentry *root)
2702{
2703 struct dentry *this_parent = root;
2704 struct list_head *next;
2705
2706 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002707 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002708repeat:
2709 next = this_parent->d_subdirs.next;
2710resume:
2711 while (next != &this_parent->d_subdirs) {
2712 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002713 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 next = tmp->next;
Nick Pigginda502952011-01-07 17:49:33 +11002715 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2716 if (d_unhashed(dentry) || !dentry->d_inode) {
2717 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718 continue;
Nick Pigginda502952011-01-07 17:49:33 +11002719 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002721 spin_unlock(&this_parent->d_lock);
2722 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002724 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725 goto repeat;
2726 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002727 dentry->d_count--;
2728 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 }
2730 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08002731 next = this_parent->d_u.d_child.next;
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002732 this_parent->d_count--;
2733 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002734 this_parent = this_parent->d_parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002735 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 goto resume;
2737 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002738 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 spin_unlock(&dcache_lock);
2740}
2741
2742/**
2743 * find_inode_number - check for dentry with name
2744 * @dir: directory to check
2745 * @name: Name to find.
2746 *
2747 * Check whether a dentry already exists for the given name,
2748 * and return the inode number if it has an inode. Otherwise
2749 * 0 is returned.
2750 *
2751 * This routine is used to post-process directory listings for
2752 * filesystems using synthetic inode numbers, and is necessary
2753 * to keep getcwd() working.
2754 */
2755
2756ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2757{
2758 struct dentry * dentry;
2759 ino_t ino = 0;
2760
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002761 dentry = d_hash_and_lookup(dir, name);
2762 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002763 if (dentry->d_inode)
2764 ino = dentry->d_inode->i_ino;
2765 dput(dentry);
2766 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002767 return ino;
2768}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002769EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770
2771static __initdata unsigned long dhash_entries;
2772static int __init set_dhash_entries(char *str)
2773{
2774 if (!str)
2775 return 0;
2776 dhash_entries = simple_strtoul(str, &str, 0);
2777 return 1;
2778}
2779__setup("dhash_entries=", set_dhash_entries);
2780
2781static void __init dcache_init_early(void)
2782{
2783 int loop;
2784
2785 /* If hashes are distributed across NUMA nodes, defer
2786 * hash allocation until vmalloc space is available.
2787 */
2788 if (hashdist)
2789 return;
2790
2791 dentry_hashtable =
2792 alloc_large_system_hash("Dentry cache",
2793 sizeof(struct hlist_head),
2794 dhash_entries,
2795 13,
2796 HASH_EARLY,
2797 &d_hash_shift,
2798 &d_hash_mask,
2799 0);
2800
2801 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2802 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2803}
2804
Denis Cheng74bf17c2007-10-16 23:26:30 -07002805static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806{
2807 int loop;
2808
2809 /*
2810 * A constructor could be added for stable state like the lists,
2811 * but it is probably not worth it because of the cache nature
2812 * of the dcache.
2813 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002814 dentry_cache = KMEM_CACHE(dentry,
2815 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816
Rusty Russell8e1f9362007-07-17 04:03:17 -07002817 register_shrinker(&dcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002818
2819 /* Hash may have been set up in dcache_init_early */
2820 if (!hashdist)
2821 return;
2822
2823 dentry_hashtable =
2824 alloc_large_system_hash("Dentry cache",
2825 sizeof(struct hlist_head),
2826 dhash_entries,
2827 13,
2828 0,
2829 &d_hash_shift,
2830 &d_hash_mask,
2831 0);
2832
2833 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2834 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2835}
2836
2837/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002838struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002839EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002840
Linus Torvalds1da177e2005-04-16 15:20:36 -07002841EXPORT_SYMBOL(d_genocide);
2842
Linus Torvalds1da177e2005-04-16 15:20:36 -07002843void __init vfs_caches_init_early(void)
2844{
2845 dcache_init_early();
2846 inode_init_early();
2847}
2848
2849void __init vfs_caches_init(unsigned long mempages)
2850{
2851 unsigned long reserve;
2852
2853 /* Base hash sizes on available memory, with a reserve equal to
2854 150% of current kernel size */
2855
2856 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2857 mempages -= reserve;
2858
2859 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002860 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861
Denis Cheng74bf17c2007-10-16 23:26:30 -07002862 dcache_init();
2863 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002864 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07002865 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002866 bdev_cache_init();
2867 chrdev_init();
2868}