blob: dc0551c9755d255becabdfde455616db5803e646 [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:
Nick Pigginb5c84bf2011-01-07 17:49:38 +110057 * dcache_inode_lock
58 * dentry->d_lock
59 * dcache_lru_lock
60 * dcache_hash_lock
Nick Piggin789680d2011-01-07 17:49:30 +110061 *
Nick Pigginda502952011-01-07 17:49:33 +110062 * If there is an ancestor relationship:
63 * dentry->d_parent->...->d_parent->d_lock
64 * ...
65 * dentry->d_parent->d_lock
66 * dentry->d_lock
67 *
68 * If no ancestor relationship:
Nick Piggin789680d2011-01-07 17:49:30 +110069 * if (dentry1 < dentry2)
70 * dentry1->d_lock
71 * dentry2->d_lock
72 */
Eric Dumazetfa3536c2006-03-26 01:37:24 -080073int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070074EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
75
Nick Pigginb23fb0a2011-01-07 17:49:35 +110076__cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_inode_lock);
Nick Piggin789680d2011-01-07 17:49:30 +110077static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_hash_lock);
Nick Piggin23044502011-01-07 17:49:31 +110078static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -040079__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070080
Nick Piggin949854d2011-01-07 17:49:37 +110081EXPORT_SYMBOL(rename_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +110082EXPORT_SYMBOL(dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083
Christoph Lametere18b8902006-12-06 20:33:20 -080084static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070085
86#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
87
88/*
89 * This is the single most critical data structure when it comes
90 * to the dcache: the hashtable for lookups. Somebody should try
91 * to make this good - I've just made it work.
92 *
93 * This hash-function tries to avoid losing too many bits of hash
94 * information, yet avoid using a prime hash-size or similar.
95 */
96#define D_HASHBITS d_hash_shift
97#define D_HASHMASK d_hash_mask
98
Eric Dumazetfa3536c2006-03-26 01:37:24 -080099static unsigned int d_hash_mask __read_mostly;
100static unsigned int d_hash_shift __read_mostly;
101static struct hlist_head *dentry_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102
103/* Statistics gathering. */
104struct dentry_stat_t dentry_stat = {
105 .age_limit = 45,
106};
107
Nick Piggin3e880fb2011-01-07 17:49:19 +1100108static DEFINE_PER_CPU(unsigned int, nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400109
110#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Nick Piggin3e880fb2011-01-07 17:49:19 +1100111static int get_nr_dentry(void)
112{
113 int i;
114 int sum = 0;
115 for_each_possible_cpu(i)
116 sum += per_cpu(nr_dentry, i);
117 return sum < 0 ? 0 : sum;
118}
119
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400120int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
121 size_t *lenp, loff_t *ppos)
122{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100123 dentry_stat.nr_dentry = get_nr_dentry();
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400124 return proc_dointvec(table, write, buffer, lenp, ppos);
125}
126#endif
127
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400128static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700129{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400130 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
131
Arjan van de Venfd217f42008-10-21 06:47:33 -0700132 WARN_ON(!list_empty(&dentry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700133 if (dname_external(dentry))
134 kfree(dentry->d_name.name);
135 kmem_cache_free(dentry_cache, dentry);
136}
137
138/*
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100139 * no locks, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 */
141static void d_free(struct dentry *dentry)
142{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100143 BUG_ON(dentry->d_count);
Nick Piggin3e880fb2011-01-07 17:49:19 +1100144 this_cpu_dec(nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (dentry->d_op && dentry->d_op->d_release)
146 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400147
Eric Dumazetb3423412006-12-06 20:38:48 -0800148 /* if dentry was never inserted into hash, immediate free is OK */
Akinobu Mitae8462ca2008-02-06 01:37:07 -0800149 if (hlist_unhashed(&dentry->d_hash))
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400150 __d_free(&dentry->d_u.d_rcu);
Eric Dumazetb3423412006-12-06 20:38:48 -0800151 else
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400152 call_rcu(&dentry->d_u.d_rcu, __d_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700153}
154
155/*
156 * Release the dentry's inode, using the filesystem
157 * d_iput() operation if defined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800159static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200160 __releases(dentry->d_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100161 __releases(dcache_inode_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700162{
163 struct inode *inode = dentry->d_inode;
164 if (inode) {
165 dentry->d_inode = NULL;
166 list_del_init(&dentry->d_alias);
167 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100168 spin_unlock(&dcache_inode_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700169 if (!inode->i_nlink)
170 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700171 if (dentry->d_op && dentry->d_op->d_iput)
172 dentry->d_op->d_iput(dentry, inode);
173 else
174 iput(inode);
175 } else {
176 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100177 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700178 }
179}
180
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700181/*
Nick Piggin23044502011-01-07 17:49:31 +1100182 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700183 */
184static void dentry_lru_add(struct dentry *dentry)
185{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400186 if (list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100187 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400188 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
189 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100190 dentry_stat.nr_unused++;
Nick Piggin23044502011-01-07 17:49:31 +1100191 spin_unlock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400192 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700193}
194
Nick Piggin23044502011-01-07 17:49:31 +1100195static void __dentry_lru_del(struct dentry *dentry)
196{
197 list_del_init(&dentry->d_lru);
198 dentry->d_sb->s_nr_dentry_unused--;
199 dentry_stat.nr_unused--;
200}
201
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700202static void dentry_lru_del(struct dentry *dentry)
203{
204 if (!list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100205 spin_lock(&dcache_lru_lock);
206 __dentry_lru_del(dentry);
207 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700208 }
209}
210
Christoph Hellwiga4633352010-10-10 05:36:26 -0400211static void dentry_lru_move_tail(struct dentry *dentry)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700212{
Nick Piggin23044502011-01-07 17:49:31 +1100213 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400214 if (list_empty(&dentry->d_lru)) {
215 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
216 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100217 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400218 } else {
219 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700220 }
Nick Piggin23044502011-01-07 17:49:31 +1100221 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700222}
223
Miklos Szeredid52b9082007-05-08 00:23:46 -0700224/**
225 * d_kill - kill dentry and return parent
226 * @dentry: dentry to kill
227 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200228 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700229 *
230 * If this is the root of the dentry tree, return NULL.
Nick Piggin23044502011-01-07 17:49:31 +1100231 *
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100232 * dentry->d_lock and parent->d_lock must be held by caller, and are dropped by
233 * d_kill.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700234 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100235static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200236 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100237 __releases(parent->d_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100238 __releases(dcache_inode_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700239{
Nick Piggin949854d2011-01-07 17:49:37 +1100240 dentry->d_parent = NULL;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700241 list_del(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100242 if (parent)
243 spin_unlock(&parent->d_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700244 dentry_iput(dentry);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100245 /*
246 * dentry_iput drops the locks, at which point nobody (except
247 * transient RCU lookups) can reach this dentry.
248 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700249 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900250 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700251}
252
Nick Piggin789680d2011-01-07 17:49:30 +1100253/**
254 * d_drop - drop a dentry
255 * @dentry: dentry to drop
256 *
257 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
258 * be found through a VFS lookup any more. Note that this is different from
259 * deleting the dentry - d_delete will try to mark the dentry negative if
260 * possible, giving a successful _negative_ lookup, while d_drop will
261 * just make the cache lookup fail.
262 *
263 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
264 * reason (NFS timeouts or autofs deletes).
265 *
266 * __d_drop requires dentry->d_lock.
267 */
268void __d_drop(struct dentry *dentry)
269{
270 if (!(dentry->d_flags & DCACHE_UNHASHED)) {
271 dentry->d_flags |= DCACHE_UNHASHED;
272 spin_lock(&dcache_hash_lock);
273 hlist_del_rcu(&dentry->d_hash);
274 spin_unlock(&dcache_hash_lock);
275 }
276}
277EXPORT_SYMBOL(__d_drop);
278
279void d_drop(struct dentry *dentry)
280{
Nick Piggin789680d2011-01-07 17:49:30 +1100281 spin_lock(&dentry->d_lock);
282 __d_drop(dentry);
283 spin_unlock(&dentry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +1100284}
285EXPORT_SYMBOL(d_drop);
286
Nick Piggin77812a12011-01-07 17:49:48 +1100287/*
288 * Finish off a dentry we've decided to kill.
289 * dentry->d_lock must be held, returns with it unlocked.
290 * If ref is non-zero, then decrement the refcount too.
291 * Returns dentry requiring refcount drop, or NULL if we're done.
292 */
293static inline struct dentry *dentry_kill(struct dentry *dentry, int ref)
294 __releases(dentry->d_lock)
295{
296 struct dentry *parent;
297
298 if (!spin_trylock(&dcache_inode_lock)) {
299relock:
300 spin_unlock(&dentry->d_lock);
301 cpu_relax();
302 return dentry; /* try again with same dentry */
303 }
304 if (IS_ROOT(dentry))
305 parent = NULL;
306 else
307 parent = dentry->d_parent;
308 if (parent && !spin_trylock(&parent->d_lock)) {
309 spin_unlock(&dcache_inode_lock);
310 goto relock;
311 }
312 if (ref)
313 dentry->d_count--;
314 /* if dentry was on the d_lru list delete it from there */
315 dentry_lru_del(dentry);
316 /* if it was on the hash then remove it */
317 __d_drop(dentry);
318 return d_kill(dentry, parent);
319}
320
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321/*
322 * This is dput
323 *
324 * This is complicated by the fact that we do not want to put
325 * dentries that are no longer on any hash chain on the unused
326 * list: we'd much rather just get rid of them immediately.
327 *
328 * However, that implies that we have to traverse the dentry
329 * tree upwards to the parents which might _also_ now be
330 * scheduled for deletion (it may have been only waiting for
331 * its last child to go away).
332 *
333 * This tail recursion is done by hand as we don't want to depend
334 * on the compiler to always get this right (gcc generally doesn't).
335 * Real recursion would eat up our stack space.
336 */
337
338/*
339 * dput - release a dentry
340 * @dentry: dentry to release
341 *
342 * Release a dentry. This will drop the usage count and if appropriate
343 * call the dentry unlink method as well as removing it from the queues and
344 * releasing its resources. If the parent dentries were scheduled for release
345 * they too may now get deleted.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700346 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700347void dput(struct dentry *dentry)
348{
349 if (!dentry)
350 return;
351
352repeat:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100353 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700354 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700355 spin_lock(&dentry->d_lock);
Nick Piggin61f3dee2011-01-07 17:49:40 +1100356 BUG_ON(!dentry->d_count);
357 if (dentry->d_count > 1) {
358 dentry->d_count--;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700359 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700360 return;
361 }
362
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 if (dentry->d_op && dentry->d_op->d_delete) {
364 if (dentry->d_op->d_delete(dentry))
Nick Piggin61f3dee2011-01-07 17:49:40 +1100365 goto kill_it;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 }
Nick Piggin265ac902010-10-10 05:36:24 -0400367
Linus Torvalds1da177e2005-04-16 15:20:36 -0700368 /* Unreachable? Get rid of it */
369 if (d_unhashed(dentry))
370 goto kill_it;
Nick Piggin265ac902010-10-10 05:36:24 -0400371
372 /* Otherwise leave it cached and ensure it's on the LRU */
373 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400374 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400375
Nick Piggin61f3dee2011-01-07 17:49:40 +1100376 dentry->d_count--;
377 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 return;
379
Miklos Szeredid52b9082007-05-08 00:23:46 -0700380kill_it:
Nick Piggin77812a12011-01-07 17:49:48 +1100381 dentry = dentry_kill(dentry, 1);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700382 if (dentry)
383 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700385EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386
387/**
388 * d_invalidate - invalidate a dentry
389 * @dentry: dentry to invalidate
390 *
391 * Try to invalidate the dentry if it turns out to be
392 * possible. If there are other dentries that can be
393 * reached through this one we can't delete it and we
394 * return -EBUSY. On success we return 0.
395 *
396 * no dcache lock.
397 */
398
399int d_invalidate(struct dentry * dentry)
400{
401 /*
402 * If it's already been dropped, return OK.
403 */
Nick Pigginda502952011-01-07 17:49:33 +1100404 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700405 if (d_unhashed(dentry)) {
Nick Pigginda502952011-01-07 17:49:33 +1100406 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700407 return 0;
408 }
409 /*
410 * Check whether to do a partial shrink_dcache
411 * to get rid of unused child entries.
412 */
413 if (!list_empty(&dentry->d_subdirs)) {
Nick Pigginda502952011-01-07 17:49:33 +1100414 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700415 shrink_dcache_parent(dentry);
Nick Pigginda502952011-01-07 17:49:33 +1100416 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700417 }
418
419 /*
420 * Somebody else still using it?
421 *
422 * If it's a directory, we can't drop it
423 * for fear of somebody re-populating it
424 * with children (even though dropping it
425 * would make it unreachable from the root,
426 * we might still populate it if it was a
427 * working directory or similar).
428 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100429 if (dentry->d_count > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700430 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
431 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 return -EBUSY;
433 }
434 }
435
436 __d_drop(dentry);
437 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return 0;
439}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700440EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700441
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100442/* This must be called with d_lock held */
Nick Piggindc0474b2011-01-07 17:49:43 +1100443static inline void __dget_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700444{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100445 dentry->d_count++;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700446}
447
Nick Piggindc0474b2011-01-07 17:49:43 +1100448static inline void __dget(struct dentry *dentry)
Nick Piggin23044502011-01-07 17:49:31 +1100449{
Nick Piggin23044502011-01-07 17:49:31 +1100450 spin_lock(&dentry->d_lock);
Nick Piggindc0474b2011-01-07 17:49:43 +1100451 __dget_dlock(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100452 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100453}
454
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100455struct dentry *dget_parent(struct dentry *dentry)
456{
457 struct dentry *ret;
458
459repeat:
Nick Piggina734eb42011-01-07 17:49:44 +1100460 /*
461 * Don't need rcu_dereference because we re-check it was correct under
462 * the lock.
463 */
464 rcu_read_lock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100465 ret = dentry->d_parent;
Nick Piggina734eb42011-01-07 17:49:44 +1100466 if (!ret) {
467 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100468 goto out;
469 }
Nick Piggina734eb42011-01-07 17:49:44 +1100470 spin_lock(&ret->d_lock);
471 if (unlikely(ret != dentry->d_parent)) {
472 spin_unlock(&ret->d_lock);
473 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100474 goto repeat;
475 }
Nick Piggina734eb42011-01-07 17:49:44 +1100476 rcu_read_unlock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100477 BUG_ON(!ret->d_count);
478 ret->d_count++;
479 spin_unlock(&ret->d_lock);
480out:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100481 return ret;
482}
483EXPORT_SYMBOL(dget_parent);
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485/**
486 * d_find_alias - grab a hashed alias of inode
487 * @inode: inode in question
488 * @want_discon: flag, used by d_splice_alias, to request
489 * that only a DISCONNECTED alias be returned.
490 *
491 * If inode has a hashed alias, or is a directory and has any alias,
492 * acquire the reference to alias and return it. Otherwise return NULL.
493 * Notice that if inode is a directory there can be only one alias and
494 * it can be unhashed only if it has no children, or if it is the root
495 * of a filesystem.
496 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700497 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700498 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700499 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700500 */
Nick Pigginda502952011-01-07 17:49:33 +1100501static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700502{
Nick Pigginda502952011-01-07 17:49:33 +1100503 struct dentry *alias, *discon_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700504
Nick Pigginda502952011-01-07 17:49:33 +1100505again:
506 discon_alias = NULL;
507 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
508 spin_lock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700509 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700510 if (IS_ROOT(alias) &&
Nick Pigginda502952011-01-07 17:49:33 +1100511 (alias->d_flags & DCACHE_DISCONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 discon_alias = alias;
Nick Pigginda502952011-01-07 17:49:33 +1100513 } else if (!want_discon) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100514 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100515 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700516 return alias;
517 }
518 }
Nick Pigginda502952011-01-07 17:49:33 +1100519 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520 }
Nick Pigginda502952011-01-07 17:49:33 +1100521 if (discon_alias) {
522 alias = discon_alias;
523 spin_lock(&alias->d_lock);
524 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
525 if (IS_ROOT(alias) &&
526 (alias->d_flags & DCACHE_DISCONNECTED)) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100527 __dget_dlock(alias);
Nick Pigginda502952011-01-07 17:49:33 +1100528 spin_unlock(&alias->d_lock);
529 return alias;
530 }
531 }
532 spin_unlock(&alias->d_lock);
533 goto again;
534 }
535 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700536}
537
Nick Pigginda502952011-01-07 17:49:33 +1100538struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539{
David Howells214fda12006-03-25 03:06:36 -0800540 struct dentry *de = NULL;
541
542 if (!list_empty(&inode->i_dentry)) {
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100543 spin_lock(&dcache_inode_lock);
David Howells214fda12006-03-25 03:06:36 -0800544 de = __d_find_alias(inode, 0);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100545 spin_unlock(&dcache_inode_lock);
David Howells214fda12006-03-25 03:06:36 -0800546 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 return de;
548}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700549EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550
551/*
552 * Try to kill dentries associated with this inode.
553 * WARNING: you must own a reference to inode.
554 */
555void d_prune_aliases(struct inode *inode)
556{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700557 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700558restart:
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100559 spin_lock(&dcache_inode_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700560 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100562 if (!dentry->d_count) {
Nick Piggindc0474b2011-01-07 17:49:43 +1100563 __dget_dlock(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 __d_drop(dentry);
565 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100566 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 dput(dentry);
568 goto restart;
569 }
570 spin_unlock(&dentry->d_lock);
571 }
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100572 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700573}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700574EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700575
576/*
Nick Piggin77812a12011-01-07 17:49:48 +1100577 * Try to throw away a dentry - free the inode, dput the parent.
578 * Requires dentry->d_lock is held, and dentry->d_count == 0.
579 * Releases dentry->d_lock.
Andrew Mortond702ccb2006-06-22 14:47:31 -0700580 *
Nick Piggin77812a12011-01-07 17:49:48 +1100581 * This may fail if locks cannot be acquired no problem, just try again.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 */
Nick Piggin77812a12011-01-07 17:49:48 +1100583static void try_prune_one_dentry(struct dentry *dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200584 __releases(dentry->d_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585{
Nick Piggin77812a12011-01-07 17:49:48 +1100586 struct dentry *parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700587
Nick Piggin77812a12011-01-07 17:49:48 +1100588 parent = dentry_kill(dentry, 0);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700589 /*
Nick Piggin77812a12011-01-07 17:49:48 +1100590 * If dentry_kill returns NULL, we have nothing more to do.
591 * if it returns the same dentry, trylocks failed. In either
592 * case, just loop again.
593 *
594 * Otherwise, we need to prune ancestors too. This is necessary
595 * to prevent quadratic behavior of shrink_dcache_parent(), but
596 * is also expected to be beneficial in reducing dentry cache
597 * fragmentation.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700598 */
Nick Piggin77812a12011-01-07 17:49:48 +1100599 if (!parent)
600 return;
601 if (parent == dentry)
602 return;
603
604 /* Prune ancestors. */
605 dentry = parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700606 while (dentry) {
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100607 spin_lock(&dentry->d_lock);
Nick Piggin89e60542011-01-07 17:49:45 +1100608 if (dentry->d_count > 1) {
609 dentry->d_count--;
610 spin_unlock(&dentry->d_lock);
611 return;
612 }
Nick Piggin77812a12011-01-07 17:49:48 +1100613 dentry = dentry_kill(dentry, 1);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700614 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615}
616
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400617static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700618{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700619 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700620
Nick Pigginec336792011-01-07 17:49:47 +1100621 rcu_read_lock();
622 for (;;) {
Nick Pigginec336792011-01-07 17:49:47 +1100623 dentry = list_entry_rcu(list->prev, struct dentry, d_lru);
624 if (&dentry->d_lru == list)
625 break; /* empty */
626 spin_lock(&dentry->d_lock);
627 if (dentry != list_entry(list->prev, struct dentry, d_lru)) {
628 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100629 continue;
630 }
631
Linus Torvalds1da177e2005-04-16 15:20:36 -0700632 /*
633 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700634 * the LRU because of laziness during lookup. Do not free
635 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700636 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100637 if (dentry->d_count) {
Nick Pigginec336792011-01-07 17:49:47 +1100638 dentry_lru_del(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700639 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700640 continue;
641 }
Nick Pigginec336792011-01-07 17:49:47 +1100642
Nick Pigginec336792011-01-07 17:49:47 +1100643 rcu_read_unlock();
Nick Piggin77812a12011-01-07 17:49:48 +1100644
645 try_prune_one_dentry(dentry);
646
Nick Pigginec336792011-01-07 17:49:47 +1100647 rcu_read_lock();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700648 }
Nick Pigginec336792011-01-07 17:49:47 +1100649 rcu_read_unlock();
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400650}
651
652/**
653 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
654 * @sb: superblock to shrink dentry LRU.
655 * @count: number of entries to prune
656 * @flags: flags to control the dentry processing
657 *
658 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
659 */
660static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
661{
662 /* called from prune_dcache() and shrink_dcache_parent() */
663 struct dentry *dentry;
664 LIST_HEAD(referenced);
665 LIST_HEAD(tmp);
666 int cnt = *count;
667
Nick Piggin23044502011-01-07 17:49:31 +1100668relock:
669 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400670 while (!list_empty(&sb->s_dentry_lru)) {
671 dentry = list_entry(sb->s_dentry_lru.prev,
672 struct dentry, d_lru);
673 BUG_ON(dentry->d_sb != sb);
674
Nick Piggin23044502011-01-07 17:49:31 +1100675 if (!spin_trylock(&dentry->d_lock)) {
676 spin_unlock(&dcache_lru_lock);
677 cpu_relax();
678 goto relock;
679 }
680
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400681 /*
682 * If we are honouring the DCACHE_REFERENCED flag and the
683 * dentry has this flag set, don't free it. Clear the flag
684 * and put it back on the LRU.
685 */
Nick Piggin23044502011-01-07 17:49:31 +1100686 if (flags & DCACHE_REFERENCED &&
687 dentry->d_flags & DCACHE_REFERENCED) {
688 dentry->d_flags &= ~DCACHE_REFERENCED;
689 list_move(&dentry->d_lru, &referenced);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400690 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100691 } else {
692 list_move_tail(&dentry->d_lru, &tmp);
693 spin_unlock(&dentry->d_lock);
694 if (!--cnt)
695 break;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400696 }
Nick Pigginec336792011-01-07 17:49:47 +1100697 cond_resched_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400698 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700699 if (!list_empty(&referenced))
700 list_splice(&referenced, &sb->s_dentry_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100701 spin_unlock(&dcache_lru_lock);
Nick Pigginec336792011-01-07 17:49:47 +1100702
703 shrink_dentry_list(&tmp);
704
705 *count = cnt;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700706}
707
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700708/**
709 * prune_dcache - shrink the dcache
710 * @count: number of entries to try to free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700711 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700712 * Shrink the dcache. This is done when we need more memory, or simply when we
713 * need to unmount something (at which point we need to unuse all dentries).
714 *
715 * This function may fail to free any resources if all the dentries are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700716 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700717static void prune_dcache(int count)
718{
Al Virodca33252010-07-25 02:31:46 +0400719 struct super_block *sb, *p = NULL;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700720 int w_count;
Nick Piggin86c87492011-01-07 17:49:18 +1100721 int unused = dentry_stat.nr_unused;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700722 int prune_ratio;
723 int pruned;
724
725 if (unused == 0 || count == 0)
726 return;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700727 if (count >= unused)
728 prune_ratio = 1;
729 else
730 prune_ratio = unused / count;
731 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400732 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400733 if (list_empty(&sb->s_instances))
734 continue;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700735 if (sb->s_nr_dentry_unused == 0)
736 continue;
737 sb->s_count++;
738 /* Now, we reclaim unused dentrins with fairness.
739 * We reclaim them same percentage from each superblock.
740 * We calculate number of dentries to scan on this sb
741 * as follows, but the implementation is arranged to avoid
742 * overflows:
743 * number of dentries to scan on this sb =
744 * count * (number of dentries on this sb /
745 * number of dentries in the machine)
746 */
747 spin_unlock(&sb_lock);
748 if (prune_ratio != 1)
749 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
750 else
751 w_count = sb->s_nr_dentry_unused;
752 pruned = w_count;
753 /*
754 * We need to be sure this filesystem isn't being unmounted,
755 * otherwise we could race with generic_shutdown_super(), and
756 * end up holding a reference to an inode while the filesystem
757 * is unmounted. So we try to get s_umount, and make sure
758 * s_root isn't NULL.
759 */
760 if (down_read_trylock(&sb->s_umount)) {
761 if ((sb->s_root != NULL) &&
762 (!list_empty(&sb->s_dentry_lru))) {
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700763 __shrink_dcache_sb(sb, &w_count,
764 DCACHE_REFERENCED);
765 pruned -= w_count;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700766 }
767 up_read(&sb->s_umount);
768 }
769 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400770 if (p)
771 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700772 count -= pruned;
Al Virodca33252010-07-25 02:31:46 +0400773 p = sb;
Al Viro79893c12010-03-22 20:27:55 -0400774 /* more work left to do? */
775 if (count <= 0)
776 break;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700777 }
Al Virodca33252010-07-25 02:31:46 +0400778 if (p)
779 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700780 spin_unlock(&sb_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700781}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700782
783/**
784 * shrink_dcache_sb - shrink dcache for a superblock
785 * @sb: superblock
786 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400787 * Shrink the dcache for the specified super block. This is used to free
788 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700789 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400790void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700791{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400792 LIST_HEAD(tmp);
793
Nick Piggin23044502011-01-07 17:49:31 +1100794 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400795 while (!list_empty(&sb->s_dentry_lru)) {
796 list_splice_init(&sb->s_dentry_lru, &tmp);
Nick Pigginec336792011-01-07 17:49:47 +1100797 spin_unlock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400798 shrink_dentry_list(&tmp);
Nick Pigginec336792011-01-07 17:49:47 +1100799 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400800 }
Nick Piggin23044502011-01-07 17:49:31 +1100801 spin_unlock(&dcache_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700802}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700803EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700804
805/*
David Howellsc636ebd2006-10-11 01:22:19 -0700806 * destroy a single subtree of dentries for unmount
807 * - see the comments on shrink_dcache_for_umount() for a description of the
808 * locking
809 */
810static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
811{
812 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700813 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700814
815 BUG_ON(!IS_ROOT(dentry));
816
817 /* detach this root from the system */
Nick Piggin23044502011-01-07 17:49:31 +1100818 spin_lock(&dentry->d_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400819 dentry_lru_del(dentry);
David Howellsc636ebd2006-10-11 01:22:19 -0700820 __d_drop(dentry);
Nick Pigginda502952011-01-07 17:49:33 +1100821 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700822
823 for (;;) {
824 /* descend to the first leaf in the current subtree */
825 while (!list_empty(&dentry->d_subdirs)) {
826 struct dentry *loop;
827
828 /* this is a branch with children - detach all of them
829 * from the system in one go */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100830 spin_lock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700831 list_for_each_entry(loop, &dentry->d_subdirs,
832 d_u.d_child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100833 spin_lock_nested(&loop->d_lock,
834 DENTRY_D_LOCK_NESTED);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400835 dentry_lru_del(loop);
David Howellsc636ebd2006-10-11 01:22:19 -0700836 __d_drop(loop);
Nick Pigginda502952011-01-07 17:49:33 +1100837 spin_unlock(&loop->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700838 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100839 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700840
841 /* move to the first child */
842 dentry = list_entry(dentry->d_subdirs.next,
843 struct dentry, d_u.d_child);
844 }
845
846 /* consume the dentries from this leaf up through its parents
847 * until we find one with children or run out altogether */
848 do {
849 struct inode *inode;
850
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100851 if (dentry->d_count != 0) {
David Howellsc636ebd2006-10-11 01:22:19 -0700852 printk(KERN_ERR
853 "BUG: Dentry %p{i=%lx,n=%s}"
854 " still in use (%d)"
855 " [unmount of %s %s]\n",
856 dentry,
857 dentry->d_inode ?
858 dentry->d_inode->i_ino : 0UL,
859 dentry->d_name.name,
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100860 dentry->d_count,
David Howellsc636ebd2006-10-11 01:22:19 -0700861 dentry->d_sb->s_type->name,
862 dentry->d_sb->s_id);
863 BUG();
864 }
865
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100866 if (IS_ROOT(dentry)) {
David Howellsc636ebd2006-10-11 01:22:19 -0700867 parent = NULL;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100868 list_del(&dentry->d_u.d_child);
869 } else {
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900870 parent = dentry->d_parent;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100871 spin_lock(&parent->d_lock);
872 parent->d_count--;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100873 list_del(&dentry->d_u.d_child);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100874 spin_unlock(&parent->d_lock);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900875 }
David Howellsc636ebd2006-10-11 01:22:19 -0700876
David Howellsf8713572006-10-28 10:38:46 -0700877 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700878
879 inode = dentry->d_inode;
880 if (inode) {
881 dentry->d_inode = NULL;
882 list_del_init(&dentry->d_alias);
883 if (dentry->d_op && dentry->d_op->d_iput)
884 dentry->d_op->d_iput(dentry, inode);
885 else
886 iput(inode);
887 }
888
889 d_free(dentry);
890
891 /* finished when we fall off the top of the tree,
892 * otherwise we ascend to the parent and move to the
893 * next sibling if there is one */
894 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400895 return;
David Howellsc636ebd2006-10-11 01:22:19 -0700896 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700897 } while (list_empty(&dentry->d_subdirs));
898
899 dentry = list_entry(dentry->d_subdirs.next,
900 struct dentry, d_u.d_child);
901 }
902}
903
904/*
905 * destroy the dentries attached to a superblock on unmounting
Nick Pigginb5c84bf2011-01-07 17:49:38 +1100906 * - we don't need to use dentry->d_lock because:
David Howellsc636ebd2006-10-11 01:22:19 -0700907 * - the superblock is detached from all mountings and open files, so the
908 * dentry trees will not be rearranged by the VFS
909 * - s_umount is write-locked, so the memory pressure shrinker will ignore
910 * any dentries belonging to this superblock that it comes across
911 * - the filesystem itself is no longer permitted to rearrange the dentries
912 * in this superblock
913 */
914void shrink_dcache_for_umount(struct super_block *sb)
915{
916 struct dentry *dentry;
917
918 if (down_read_trylock(&sb->s_umount))
919 BUG();
920
921 dentry = sb->s_root;
922 sb->s_root = NULL;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100923 spin_lock(&dentry->d_lock);
924 dentry->d_count--;
925 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700926 shrink_dcache_for_umount_subtree(dentry);
927
928 while (!hlist_empty(&sb->s_anon)) {
929 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
930 shrink_dcache_for_umount_subtree(dentry);
931 }
932}
933
934/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700935 * Search for at least 1 mount point in the dentry's subdirs.
936 * We descend to the next level whenever the d_subdirs
937 * list is non-empty and continue searching.
938 */
939
940/**
941 * have_submounts - check for mounts over a dentry
942 * @parent: dentry to check.
943 *
944 * Return true if the parent or its subdirectories contain
945 * a mount point
946 */
Linus Torvalds1da177e2005-04-16 15:20:36 -0700947int have_submounts(struct dentry *parent)
948{
Nick Piggin949854d2011-01-07 17:49:37 +1100949 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700950 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +1100951 unsigned seq;
Nick Piggin58db63d2011-01-07 17:49:39 +1100952 int locked = 0;
Nick Piggin949854d2011-01-07 17:49:37 +1100953
Nick Piggin949854d2011-01-07 17:49:37 +1100954 seq = read_seqbegin(&rename_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +1100955again:
956 this_parent = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700957
Linus Torvalds1da177e2005-04-16 15:20:36 -0700958 if (d_mountpoint(parent))
959 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100960 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700961repeat:
962 next = this_parent->d_subdirs.next;
963resume:
964 while (next != &this_parent->d_subdirs) {
965 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800966 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100968
969 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700970 /* Have we found a mount point ? */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100971 if (d_mountpoint(dentry)) {
972 spin_unlock(&dentry->d_lock);
973 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700974 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100975 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700976 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100977 spin_unlock(&this_parent->d_lock);
978 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700979 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100980 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 goto repeat;
982 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100983 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700984 }
985 /*
986 * All done at this level ... ascend and resume the search.
987 */
988 if (this_parent != parent) {
Nick Piggin949854d2011-01-07 17:49:37 +1100989 struct dentry *tmp;
990 struct dentry *child;
991
992 tmp = this_parent->d_parent;
993 rcu_read_lock();
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100994 spin_unlock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100995 child = this_parent;
996 this_parent = tmp;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100997 spin_lock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +1100998 /* might go back up the wrong parent if we have had a rename
999 * or deletion */
1000 if (this_parent != child->d_parent ||
Nick Piggin58db63d2011-01-07 17:49:39 +11001001 (!locked && read_seqretry(&rename_lock, seq))) {
Nick Piggin949854d2011-01-07 17:49:37 +11001002 spin_unlock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11001003 rcu_read_unlock();
1004 goto rename_retry;
1005 }
1006 rcu_read_unlock();
1007 next = child->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 goto resume;
1009 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001010 spin_unlock(&this_parent->d_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11001011 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001012 goto rename_retry;
Nick Piggin58db63d2011-01-07 17:49:39 +11001013 if (locked)
1014 write_sequnlock(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001015 return 0; /* No mount points found in tree */
1016positive:
Nick Piggin58db63d2011-01-07 17:49:39 +11001017 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001018 goto rename_retry;
Nick Piggin58db63d2011-01-07 17:49:39 +11001019 if (locked)
1020 write_sequnlock(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001021 return 1;
Nick Piggin58db63d2011-01-07 17:49:39 +11001022
1023rename_retry:
1024 locked = 1;
1025 write_seqlock(&rename_lock);
1026 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001027}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001028EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029
1030/*
1031 * Search the dentry child list for the specified parent,
1032 * and move any unused dentries to the end of the unused
1033 * list for prune_dcache(). We descend to the next level
1034 * whenever the d_subdirs list is non-empty and continue
1035 * searching.
1036 *
1037 * It returns zero iff there are no unused children,
1038 * otherwise it returns the number of children moved to
1039 * the end of the unused list. This may not be the total
1040 * number of unused children, because select_parent can
1041 * drop the lock and return early due to latency
1042 * constraints.
1043 */
1044static int select_parent(struct dentry * parent)
1045{
Nick Piggin949854d2011-01-07 17:49:37 +11001046 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001047 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11001048 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 int found = 0;
Nick Piggin58db63d2011-01-07 17:49:39 +11001050 int locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051
Nick Piggin949854d2011-01-07 17:49:37 +11001052 seq = read_seqbegin(&rename_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11001053again:
1054 this_parent = parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001055 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056repeat:
1057 next = this_parent->d_subdirs.next;
1058resume:
1059 while (next != &this_parent->d_subdirs) {
1060 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001061 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 next = tmp->next;
1063
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001064 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Nick Piggin23044502011-01-07 17:49:31 +11001065
Linus Torvalds1da177e2005-04-16 15:20:36 -07001066 /*
1067 * move only zero ref count dentries to the end
1068 * of the unused list for prune_dcache
1069 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001070 if (!dentry->d_count) {
Christoph Hellwiga4633352010-10-10 05:36:26 -04001071 dentry_lru_move_tail(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072 found++;
Christoph Hellwiga4633352010-10-10 05:36:26 -04001073 } else {
1074 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 }
1076
1077 /*
1078 * We can return to the caller if we have found some (this
1079 * ensures forward progress). We'll be coming back to find
1080 * the rest.
1081 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001082 if (found && need_resched()) {
1083 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 goto out;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001085 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001086
1087 /*
1088 * Descend a level if the d_subdirs list is non-empty.
1089 */
1090 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001091 spin_unlock(&this_parent->d_lock);
1092 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001093 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001094 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 goto repeat;
1096 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001097
1098 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001099 }
1100 /*
1101 * All done at this level ... ascend and resume the search.
1102 */
1103 if (this_parent != parent) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001104 struct dentry *tmp;
Nick Piggin949854d2011-01-07 17:49:37 +11001105 struct dentry *child;
1106
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001107 tmp = this_parent->d_parent;
Nick Piggin949854d2011-01-07 17:49:37 +11001108 rcu_read_lock();
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001109 spin_unlock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11001110 child = this_parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001111 this_parent = tmp;
1112 spin_lock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11001113 /* might go back up the wrong parent if we have had a rename
1114 * or deletion */
1115 if (this_parent != child->d_parent ||
Nick Piggin58db63d2011-01-07 17:49:39 +11001116 (!locked && read_seqretry(&rename_lock, seq))) {
Nick Piggin949854d2011-01-07 17:49:37 +11001117 spin_unlock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11001118 rcu_read_unlock();
1119 goto rename_retry;
1120 }
1121 rcu_read_unlock();
1122 next = child->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001123 goto resume;
1124 }
1125out:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001126 spin_unlock(&this_parent->d_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11001127 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11001128 goto rename_retry;
Nick Piggin58db63d2011-01-07 17:49:39 +11001129 if (locked)
1130 write_sequnlock(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001131 return found;
Nick Piggin58db63d2011-01-07 17:49:39 +11001132
1133rename_retry:
1134 if (found)
1135 return found;
1136 locked = 1;
1137 write_seqlock(&rename_lock);
1138 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139}
1140
1141/**
1142 * shrink_dcache_parent - prune dcache
1143 * @parent: parent of entries to prune
1144 *
1145 * Prune the dcache to remove unused children of the parent dentry.
1146 */
1147
1148void shrink_dcache_parent(struct dentry * parent)
1149{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001150 struct super_block *sb = parent->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 int found;
1152
1153 while ((found = select_parent(parent)) != 0)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001154 __shrink_dcache_sb(sb, &found, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001156EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001157
Linus Torvalds1da177e2005-04-16 15:20:36 -07001158/*
1159 * Scan `nr' dentries and return the number which remain.
1160 *
1161 * We need to avoid reentering the filesystem if the caller is performing a
1162 * GFP_NOFS allocation attempt. One example deadlock is:
1163 *
1164 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1165 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1166 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1167 *
1168 * In this case we return -1 to tell the caller that we baled.
1169 */
Dave Chinner7f8275d2010-07-19 14:56:17 +10001170static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001171{
1172 if (nr) {
1173 if (!(gfp_mask & __GFP_FS))
1174 return -1;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001175 prune_dcache(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176 }
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001177
Nick Piggin86c87492011-01-07 17:49:18 +11001178 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001179}
1180
Rusty Russell8e1f9362007-07-17 04:03:17 -07001181static struct shrinker dcache_shrinker = {
1182 .shrink = shrink_dcache_memory,
1183 .seeks = DEFAULT_SEEKS,
1184};
1185
Linus Torvalds1da177e2005-04-16 15:20:36 -07001186/**
1187 * d_alloc - allocate a dcache entry
1188 * @parent: parent of entry to allocate
1189 * @name: qstr of the name
1190 *
1191 * Allocates a dentry. It returns %NULL if there is insufficient memory
1192 * available. On a success the dentry is returned. The name passed in is
1193 * copied and the copy passed in may be reused after this call.
1194 */
1195
1196struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1197{
1198 struct dentry *dentry;
1199 char *dname;
1200
Mel Gormane12ba742007-10-16 01:25:52 -07001201 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001202 if (!dentry)
1203 return NULL;
1204
1205 if (name->len > DNAME_INLINE_LEN-1) {
1206 dname = kmalloc(name->len + 1, GFP_KERNEL);
1207 if (!dname) {
1208 kmem_cache_free(dentry_cache, dentry);
1209 return NULL;
1210 }
1211 } else {
1212 dname = dentry->d_iname;
1213 }
1214 dentry->d_name.name = dname;
1215
1216 dentry->d_name.len = name->len;
1217 dentry->d_name.hash = name->hash;
1218 memcpy(dname, name->name, name->len);
1219 dname[name->len] = 0;
1220
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001221 dentry->d_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001222 dentry->d_flags = DCACHE_UNHASHED;
1223 spin_lock_init(&dentry->d_lock);
1224 dentry->d_inode = NULL;
1225 dentry->d_parent = NULL;
1226 dentry->d_sb = NULL;
1227 dentry->d_op = NULL;
1228 dentry->d_fsdata = NULL;
1229 dentry->d_mounted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 INIT_HLIST_NODE(&dentry->d_hash);
1231 INIT_LIST_HEAD(&dentry->d_lru);
1232 INIT_LIST_HEAD(&dentry->d_subdirs);
1233 INIT_LIST_HEAD(&dentry->d_alias);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001234 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001235
1236 if (parent) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001237 spin_lock(&parent->d_lock);
Nick Piggin89ad4852011-01-07 17:49:41 +11001238 /*
1239 * don't need child lock because it is not subject
1240 * to concurrency here
1241 */
Nick Piggindc0474b2011-01-07 17:49:43 +11001242 __dget_dlock(parent);
1243 dentry->d_parent = parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001244 dentry->d_sb = parent->d_sb;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001245 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001246 spin_unlock(&parent->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001247 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248
Nick Piggin3e880fb2011-01-07 17:49:19 +11001249 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 return dentry;
1252}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001253EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001254
1255struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1256{
1257 struct qstr q;
1258
1259 q.name = name;
1260 q.len = strlen(name);
1261 q.hash = full_name_hash(q.name, q.len);
1262 return d_alloc(parent, &q);
1263}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001264EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001266static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1267{
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001268 spin_lock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001269 if (inode)
1270 list_add(&dentry->d_alias, &inode->i_dentry);
1271 dentry->d_inode = inode;
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001272 spin_unlock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001273 fsnotify_d_instantiate(dentry, inode);
1274}
1275
Linus Torvalds1da177e2005-04-16 15:20:36 -07001276/**
1277 * d_instantiate - fill in inode information for a dentry
1278 * @entry: dentry to complete
1279 * @inode: inode to attach to this dentry
1280 *
1281 * Fill in inode information in the entry.
1282 *
1283 * This turns negative dentries into productive full members
1284 * of society.
1285 *
1286 * NOTE! This assumes that the inode count has been incremented
1287 * (or otherwise set) by the caller to indicate that it is now
1288 * in use by the dcache.
1289 */
1290
1291void d_instantiate(struct dentry *entry, struct inode * inode)
1292{
Eric Sesterhenn28133c72006-03-26 18:25:39 +02001293 BUG_ON(!list_empty(&entry->d_alias));
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001294 spin_lock(&dcache_inode_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001295 __d_instantiate(entry, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001296 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001297 security_d_instantiate(entry, inode);
1298}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001299EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001300
1301/**
1302 * d_instantiate_unique - instantiate a non-aliased dentry
1303 * @entry: dentry to instantiate
1304 * @inode: inode to attach to this dentry
1305 *
1306 * Fill in inode information in the entry. On success, it returns NULL.
1307 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001308 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001309 *
1310 * Note that in order to avoid conflicts with rename() etc, the caller
1311 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001312 *
1313 * This also assumes that the inode count has been incremented
1314 * (or otherwise set) by the caller to indicate that it is now
1315 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 */
David Howells770bfad2006-08-22 20:06:07 -04001317static struct dentry *__d_instantiate_unique(struct dentry *entry,
1318 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001319{
1320 struct dentry *alias;
1321 int len = entry->d_name.len;
1322 const char *name = entry->d_name.name;
1323 unsigned int hash = entry->d_name.hash;
1324
David Howells770bfad2006-08-22 20:06:07 -04001325 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001326 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001327 return NULL;
1328 }
1329
Linus Torvalds1da177e2005-04-16 15:20:36 -07001330 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1331 struct qstr *qstr = &alias->d_name;
1332
Nick Piggin9abca362011-01-07 17:49:36 +11001333 /*
1334 * Don't need alias->d_lock here, because aliases with
1335 * d_parent == entry->d_parent are not subject to name or
1336 * parent changes, because the parent inode i_mutex is held.
1337 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001338 if (qstr->hash != hash)
1339 continue;
1340 if (alias->d_parent != entry->d_parent)
1341 continue;
1342 if (qstr->len != len)
1343 continue;
1344 if (memcmp(qstr->name, name, len))
1345 continue;
Nick Piggindc0474b2011-01-07 17:49:43 +11001346 __dget(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001347 return alias;
1348 }
David Howells770bfad2006-08-22 20:06:07 -04001349
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001350 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001351 return NULL;
1352}
David Howells770bfad2006-08-22 20:06:07 -04001353
1354struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1355{
1356 struct dentry *result;
1357
1358 BUG_ON(!list_empty(&entry->d_alias));
1359
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001360 spin_lock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04001361 result = __d_instantiate_unique(entry, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001362 spin_unlock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04001363
1364 if (!result) {
1365 security_d_instantiate(entry, inode);
1366 return NULL;
1367 }
1368
1369 BUG_ON(!d_unhashed(result));
1370 iput(inode);
1371 return result;
1372}
1373
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374EXPORT_SYMBOL(d_instantiate_unique);
1375
1376/**
1377 * d_alloc_root - allocate root dentry
1378 * @root_inode: inode to allocate the root for
1379 *
1380 * Allocate a root ("/") dentry for the inode given. The inode is
1381 * instantiated and returned. %NULL is returned if there is insufficient
1382 * memory or the inode passed is %NULL.
1383 */
1384
1385struct dentry * d_alloc_root(struct inode * root_inode)
1386{
1387 struct dentry *res = NULL;
1388
1389 if (root_inode) {
1390 static const struct qstr name = { .name = "/", .len = 1 };
1391
1392 res = d_alloc(NULL, &name);
1393 if (res) {
1394 res->d_sb = root_inode->i_sb;
1395 res->d_parent = res;
1396 d_instantiate(res, root_inode);
1397 }
1398 }
1399 return res;
1400}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001401EXPORT_SYMBOL(d_alloc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001402
1403static inline struct hlist_head *d_hash(struct dentry *parent,
1404 unsigned long hash)
1405{
1406 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1407 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1408 return dentry_hashtable + (hash & D_HASHMASK);
1409}
1410
1411/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001412 * d_obtain_alias - find or allocate a dentry for a given inode
1413 * @inode: inode to allocate the dentry for
1414 *
1415 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1416 * similar open by handle operations. The returned dentry may be anonymous,
1417 * or may have a full name (if the inode was already in the cache).
1418 *
1419 * When called on a directory inode, we must ensure that the inode only ever
1420 * has one dentry. If a dentry is found, that is returned instead of
1421 * allocating a new one.
1422 *
1423 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001424 * to the dentry. In case of an error the reference on the inode is released.
1425 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1426 * be passed in and will be the error will be propagate to the return value,
1427 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001428 */
1429struct dentry *d_obtain_alias(struct inode *inode)
1430{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001431 static const struct qstr anonstring = { .name = "" };
1432 struct dentry *tmp;
1433 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001434
1435 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001436 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001437 if (IS_ERR(inode))
1438 return ERR_CAST(inode);
1439
Christoph Hellwig9308a612008-08-11 15:49:12 +02001440 res = d_find_alias(inode);
1441 if (res)
1442 goto out_iput;
1443
1444 tmp = d_alloc(NULL, &anonstring);
1445 if (!tmp) {
1446 res = ERR_PTR(-ENOMEM);
1447 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001448 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001449 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1450
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001451
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001452 spin_lock(&dcache_inode_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001453 res = __d_find_alias(inode, 0);
1454 if (res) {
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001455 spin_unlock(&dcache_inode_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001456 dput(tmp);
1457 goto out_iput;
1458 }
1459
1460 /* attach a disconnected dentry */
1461 spin_lock(&tmp->d_lock);
1462 tmp->d_sb = inode->i_sb;
1463 tmp->d_inode = inode;
1464 tmp->d_flags |= DCACHE_DISCONNECTED;
1465 tmp->d_flags &= ~DCACHE_UNHASHED;
1466 list_add(&tmp->d_alias, &inode->i_dentry);
Nick Piggin789680d2011-01-07 17:49:30 +11001467 spin_lock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001468 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
Nick Piggin789680d2011-01-07 17:49:30 +11001469 spin_unlock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001470 spin_unlock(&tmp->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001471 spin_unlock(&dcache_inode_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001472
Christoph Hellwig9308a612008-08-11 15:49:12 +02001473 return tmp;
1474
1475 out_iput:
1476 iput(inode);
1477 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001478}
Benny Halevyadc48722009-02-27 14:02:59 -08001479EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001480
1481/**
1482 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1483 * @inode: the inode which may have a disconnected dentry
1484 * @dentry: a negative dentry which we want to point to the inode.
1485 *
1486 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1487 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1488 * and return it, else simply d_add the inode to the dentry and return NULL.
1489 *
1490 * This is needed in the lookup routine of any filesystem that is exportable
1491 * (via knfsd) so that we can build dcache paths to directories effectively.
1492 *
1493 * If a dentry was found and moved, then it is returned. Otherwise NULL
1494 * is returned. This matches the expected return value of ->lookup.
1495 *
1496 */
1497struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1498{
1499 struct dentry *new = NULL;
1500
NeilBrown21c0d8f2006-10-04 02:16:16 -07001501 if (inode && S_ISDIR(inode->i_mode)) {
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001502 spin_lock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001503 new = __d_find_alias(inode, 1);
1504 if (new) {
1505 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001506 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001507 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001508 d_move(new, dentry);
1509 iput(inode);
1510 } else {
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001511 /* already taking dcache_inode_lock, so d_add() by hand */
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001512 __d_instantiate(dentry, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001513 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001514 security_d_instantiate(dentry, inode);
1515 d_rehash(dentry);
1516 }
1517 } else
1518 d_add(dentry, inode);
1519 return new;
1520}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001521EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001522
Barry Naujok94035402008-05-21 16:50:46 +10001523/**
1524 * d_add_ci - lookup or allocate new dentry with case-exact name
1525 * @inode: the inode case-insensitive lookup has found
1526 * @dentry: the negative dentry that was passed to the parent's lookup func
1527 * @name: the case-exact name to be associated with the returned dentry
1528 *
1529 * This is to avoid filling the dcache with case-insensitive names to the
1530 * same inode, only the actual correct case is stored in the dcache for
1531 * case-insensitive filesystems.
1532 *
1533 * For a case-insensitive lookup match and if the the case-exact dentry
1534 * already exists in in the dcache, use it and return it.
1535 *
1536 * If no entry exists with the exact case name, allocate new dentry with
1537 * the exact case, and return the spliced entry.
1538 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001539struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001540 struct qstr *name)
1541{
1542 int error;
1543 struct dentry *found;
1544 struct dentry *new;
1545
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001546 /*
1547 * First check if a dentry matching the name already exists,
1548 * if not go ahead and create it now.
1549 */
Barry Naujok94035402008-05-21 16:50:46 +10001550 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001551 if (!found) {
1552 new = d_alloc(dentry->d_parent, name);
1553 if (!new) {
1554 error = -ENOMEM;
1555 goto err_out;
1556 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001557
Barry Naujok94035402008-05-21 16:50:46 +10001558 found = d_splice_alias(inode, new);
1559 if (found) {
1560 dput(new);
1561 return found;
1562 }
1563 return new;
1564 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001565
1566 /*
1567 * If a matching dentry exists, and it's not negative use it.
1568 *
1569 * Decrement the reference count to balance the iget() done
1570 * earlier on.
1571 */
Barry Naujok94035402008-05-21 16:50:46 +10001572 if (found->d_inode) {
1573 if (unlikely(found->d_inode != inode)) {
1574 /* This can't happen because bad inodes are unhashed. */
1575 BUG_ON(!is_bad_inode(inode));
1576 BUG_ON(!is_bad_inode(found->d_inode));
1577 }
Barry Naujok94035402008-05-21 16:50:46 +10001578 iput(inode);
1579 return found;
1580 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001581
Barry Naujok94035402008-05-21 16:50:46 +10001582 /*
1583 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001584 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001585 */
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001586 spin_lock(&dcache_inode_lock);
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001587 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001588 __d_instantiate(found, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001589 spin_unlock(&dcache_inode_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001590 security_d_instantiate(found, inode);
1591 return found;
1592 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001593
Barry Naujok94035402008-05-21 16:50:46 +10001594 /*
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001595 * In case a directory already has a (disconnected) entry grab a
1596 * reference to it, move it in place and use it.
Barry Naujok94035402008-05-21 16:50:46 +10001597 */
1598 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
Nick Piggindc0474b2011-01-07 17:49:43 +11001599 __dget(new);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001600 spin_unlock(&dcache_inode_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001601 security_d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001602 d_move(new, found);
Barry Naujok94035402008-05-21 16:50:46 +10001603 iput(inode);
Barry Naujok94035402008-05-21 16:50:46 +10001604 dput(found);
Barry Naujok94035402008-05-21 16:50:46 +10001605 return new;
1606
1607err_out:
1608 iput(inode);
1609 return ERR_PTR(error);
1610}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001611EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001612
1613/**
1614 * d_lookup - search for a dentry
1615 * @parent: parent dentry
1616 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001617 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001618 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001619 * d_lookup searches the children of the parent dentry for the name in
1620 * question. If the dentry is found its reference count is incremented and the
1621 * dentry is returned. The caller must use dput to free the entry when it has
1622 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001623 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001624struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1625{
1626 struct dentry * dentry = NULL;
Nick Piggin949854d2011-01-07 17:49:37 +11001627 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001628
1629 do {
1630 seq = read_seqbegin(&rename_lock);
1631 dentry = __d_lookup(parent, name);
1632 if (dentry)
1633 break;
1634 } while (read_seqretry(&rename_lock, seq));
1635 return dentry;
1636}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001637EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001638
Nick Pigginb04f7842010-08-18 04:37:34 +10001639/*
1640 * __d_lookup - search for a dentry (racy)
1641 * @parent: parent dentry
1642 * @name: qstr of name we wish to find
1643 * Returns: dentry, or NULL
1644 *
1645 * __d_lookup is like d_lookup, however it may (rarely) return a
1646 * false-negative result due to unrelated rename activity.
1647 *
1648 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1649 * however it must be used carefully, eg. with a following d_lookup in
1650 * the case of failure.
1651 *
1652 * __d_lookup callers must be commented.
1653 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1655{
1656 unsigned int len = name->len;
1657 unsigned int hash = name->hash;
1658 const unsigned char *str = name->name;
1659 struct hlist_head *head = d_hash(parent,hash);
1660 struct dentry *found = NULL;
1661 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001662 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001663
Nick Pigginb04f7842010-08-18 04:37:34 +10001664 /*
1665 * The hash list is protected using RCU.
1666 *
1667 * Take d_lock when comparing a candidate dentry, to avoid races
1668 * with d_move().
1669 *
1670 * It is possible that concurrent renames can mess up our list
1671 * walk here and result in missing our dentry, resulting in the
1672 * false-negative result. d_lookup() protects against concurrent
1673 * renames using rename_lock seqlock.
1674 *
1675 * See Documentation/vfs/dcache-locking.txt for more details.
1676 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001677 rcu_read_lock();
1678
Paul E. McKenney665a7582005-11-07 00:59:17 -08001679 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001680 struct qstr *qstr;
1681
Linus Torvalds1da177e2005-04-16 15:20:36 -07001682 if (dentry->d_name.hash != hash)
1683 continue;
1684 if (dentry->d_parent != parent)
1685 continue;
1686
1687 spin_lock(&dentry->d_lock);
1688
1689 /*
1690 * Recheck the dentry after taking the lock - d_move may have
Nick Pigginb04f7842010-08-18 04:37:34 +10001691 * changed things. Don't bother checking the hash because
1692 * we're about to compare the whole name anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 */
1694 if (dentry->d_parent != parent)
1695 goto next;
1696
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001697 /* non-existing due to RCU? */
1698 if (d_unhashed(dentry))
1699 goto next;
1700
Linus Torvalds1da177e2005-04-16 15:20:36 -07001701 /*
1702 * It is safe to compare names since d_move() cannot
1703 * change the qstr (protected by d_lock).
1704 */
1705 qstr = &dentry->d_name;
1706 if (parent->d_op && parent->d_op->d_compare) {
Nick Piggin621e1552011-01-07 17:49:27 +11001707 if (parent->d_op->d_compare(parent, parent->d_inode,
1708 dentry, dentry->d_inode,
1709 qstr->len, qstr->name, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 goto next;
1711 } else {
1712 if (qstr->len != len)
1713 goto next;
1714 if (memcmp(qstr->name, str, len))
1715 goto next;
1716 }
1717
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001718 dentry->d_count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001719 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001720 spin_unlock(&dentry->d_lock);
1721 break;
1722next:
1723 spin_unlock(&dentry->d_lock);
1724 }
1725 rcu_read_unlock();
1726
1727 return found;
1728}
1729
1730/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001731 * d_hash_and_lookup - hash the qstr then search for a dentry
1732 * @dir: Directory to search in
1733 * @name: qstr of name we wish to find
1734 *
1735 * On hash failure or on lookup failure NULL is returned.
1736 */
1737struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1738{
1739 struct dentry *dentry = NULL;
1740
1741 /*
1742 * Check for a fs-specific hash function. Note that we must
1743 * calculate the standard hash first, as the d_op->d_hash()
1744 * routine may choose to leave the hash value unchanged.
1745 */
1746 name->hash = full_name_hash(name->name, name->len);
1747 if (dir->d_op && dir->d_op->d_hash) {
Nick Pigginb1e6a012011-01-07 17:49:28 +11001748 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001749 goto out;
1750 }
1751 dentry = d_lookup(dir, name);
1752out:
1753 return dentry;
1754}
1755
1756/**
Nick Piggin786a5e12011-01-07 17:49:16 +11001757 * d_validate - verify dentry provided from insecure source (deprecated)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001758 * @dentry: The dentry alleged to be valid child of @dparent
1759 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001760 *
1761 * An insecure source has sent us a dentry, here we verify it and dget() it.
1762 * This is used by ncpfs in its readdir implementation.
1763 * Zero is returned in the dentry is invalid.
Nick Piggin786a5e12011-01-07 17:49:16 +11001764 *
1765 * This function is slow for big directories, and deprecated, do not use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001766 */
Nick Piggind3a23e12011-01-05 20:01:21 +11001767int d_validate(struct dentry *dentry, struct dentry *dparent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768{
Nick Piggin786a5e12011-01-07 17:49:16 +11001769 struct dentry *child;
Nick Piggind3a23e12011-01-05 20:01:21 +11001770
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001771 spin_lock(&dparent->d_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001772 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1773 if (dentry == child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001774 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Nick Piggindc0474b2011-01-07 17:49:43 +11001775 __dget_dlock(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001776 spin_unlock(&dentry->d_lock);
1777 spin_unlock(&dparent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001778 return 1;
1779 }
1780 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001781 spin_unlock(&dparent->d_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001782
Linus Torvalds1da177e2005-04-16 15:20:36 -07001783 return 0;
1784}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001785EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001786
1787/*
1788 * When a file is deleted, we have two options:
1789 * - turn this dentry into a negative dentry
1790 * - unhash this dentry and free it.
1791 *
1792 * Usually, we want to just turn this into
1793 * a negative dentry, but if anybody else is
1794 * currently using the dentry or the inode
1795 * we can't do that and we fall back on removing
1796 * it from the hash queues and waiting for
1797 * it to be deleted later when it has no users
1798 */
1799
1800/**
1801 * d_delete - delete a dentry
1802 * @dentry: The dentry to delete
1803 *
1804 * Turn the dentry into a negative dentry if possible, otherwise
1805 * remove it from the hash queues so it can be deleted later
1806 */
1807
1808void d_delete(struct dentry * dentry)
1809{
John McCutchan7a91bf72005-08-08 13:52:16 -04001810 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001811 /*
1812 * Are we the only user?
1813 */
Nick Piggin357f8e62011-01-07 17:49:42 +11001814again:
Linus Torvalds1da177e2005-04-16 15:20:36 -07001815 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001816 isdir = S_ISDIR(dentry->d_inode->i_mode);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001817 if (dentry->d_count == 1) {
Nick Piggin357f8e62011-01-07 17:49:42 +11001818 if (!spin_trylock(&dcache_inode_lock)) {
1819 spin_unlock(&dentry->d_lock);
1820 cpu_relax();
1821 goto again;
1822 }
Al Viro13e3c5e2010-05-21 16:11:04 -04001823 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001825 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001826 return;
1827 }
1828
1829 if (!d_unhashed(dentry))
1830 __d_drop(dentry);
1831
1832 spin_unlock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001833
1834 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001836EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001837
1838static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1839{
1840
1841 entry->d_flags &= ~DCACHE_UNHASHED;
1842 hlist_add_head_rcu(&entry->d_hash, list);
1843}
1844
David Howells770bfad2006-08-22 20:06:07 -04001845static void _d_rehash(struct dentry * entry)
1846{
1847 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1848}
1849
Linus Torvalds1da177e2005-04-16 15:20:36 -07001850/**
1851 * d_rehash - add an entry back to the hash
1852 * @entry: dentry to add to the hash
1853 *
1854 * Adds a dentry to the hash according to its name.
1855 */
1856
1857void d_rehash(struct dentry * entry)
1858{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001859 spin_lock(&entry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +11001860 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04001861 _d_rehash(entry);
Nick Piggin789680d2011-01-07 17:49:30 +11001862 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 spin_unlock(&entry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001865EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866
Nick Pigginfb2d5b82011-01-07 17:49:26 +11001867/**
1868 * dentry_update_name_case - update case insensitive dentry with a new name
1869 * @dentry: dentry to be updated
1870 * @name: new name
1871 *
1872 * Update a case insensitive dentry with new case of name.
1873 *
1874 * dentry must have been returned by d_lookup with name @name. Old and new
1875 * name lengths must match (ie. no d_compare which allows mismatched name
1876 * lengths).
1877 *
1878 * Parent inode i_mutex must be held over d_lookup and into this call (to
1879 * keep renames and concurrent inserts, and readdir(2) away).
1880 */
1881void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
1882{
1883 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
1884 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
1885
Nick Pigginfb2d5b82011-01-07 17:49:26 +11001886 spin_lock(&dentry->d_lock);
1887 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
1888 spin_unlock(&dentry->d_lock);
Nick Pigginfb2d5b82011-01-07 17:49:26 +11001889}
1890EXPORT_SYMBOL(dentry_update_name_case);
1891
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892static void switch_names(struct dentry *dentry, struct dentry *target)
1893{
1894 if (dname_external(target)) {
1895 if (dname_external(dentry)) {
1896 /*
1897 * Both external: swap the pointers
1898 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001899 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 } else {
1901 /*
1902 * dentry:internal, target:external. Steal target's
1903 * storage and make target internal.
1904 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07001905 memcpy(target->d_iname, dentry->d_name.name,
1906 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001907 dentry->d_name.name = target->d_name.name;
1908 target->d_name.name = target->d_iname;
1909 }
1910 } else {
1911 if (dname_external(dentry)) {
1912 /*
1913 * dentry:external, target:internal. Give dentry's
1914 * storage to target and make dentry internal
1915 */
1916 memcpy(dentry->d_iname, target->d_name.name,
1917 target->d_name.len + 1);
1918 target->d_name.name = dentry->d_name.name;
1919 dentry->d_name.name = dentry->d_iname;
1920 } else {
1921 /*
1922 * Both are internal. Just copy target to dentry
1923 */
1924 memcpy(dentry->d_iname, target->d_name.name,
1925 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05001926 dentry->d_name.len = target->d_name.len;
1927 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001928 }
1929 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001930 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001931}
1932
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001933static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
1934{
1935 /*
1936 * XXXX: do we really need to take target->d_lock?
1937 */
1938 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
1939 spin_lock(&target->d_parent->d_lock);
1940 else {
1941 if (d_ancestor(dentry->d_parent, target->d_parent)) {
1942 spin_lock(&dentry->d_parent->d_lock);
1943 spin_lock_nested(&target->d_parent->d_lock,
1944 DENTRY_D_LOCK_NESTED);
1945 } else {
1946 spin_lock(&target->d_parent->d_lock);
1947 spin_lock_nested(&dentry->d_parent->d_lock,
1948 DENTRY_D_LOCK_NESTED);
1949 }
1950 }
1951 if (target < dentry) {
1952 spin_lock_nested(&target->d_lock, 2);
1953 spin_lock_nested(&dentry->d_lock, 3);
1954 } else {
1955 spin_lock_nested(&dentry->d_lock, 2);
1956 spin_lock_nested(&target->d_lock, 3);
1957 }
1958}
1959
1960static void dentry_unlock_parents_for_move(struct dentry *dentry,
1961 struct dentry *target)
1962{
1963 if (target->d_parent != dentry->d_parent)
1964 spin_unlock(&dentry->d_parent->d_lock);
1965 if (target->d_parent != target)
1966 spin_unlock(&target->d_parent->d_lock);
1967}
1968
Linus Torvalds1da177e2005-04-16 15:20:36 -07001969/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001970 * When switching names, the actual string doesn't strictly have to
1971 * be preserved in the target - because we're dropping the target
1972 * anyway. As such, we can just do a simple memcpy() to copy over
1973 * the new name before we switch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001974 *
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001975 * Note that we have to be a lot more careful about getting the hash
1976 * switched - we have to switch the hash value properly even if it
1977 * then no longer matches the actual (corrupted) string of the target.
1978 * The hash value has to match the hash queue that the dentry is on..
Linus Torvalds1da177e2005-04-16 15:20:36 -07001979 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001980/*
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001981 * d_move - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001982 * @dentry: entry to move
1983 * @target: new dentry
1984 *
1985 * Update the dcache to reflect the move of a file name. Negative
1986 * dcache entries should not be moved in this way.
1987 */
Nick Pigginb5c84bf2011-01-07 17:49:38 +11001988void d_move(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001989{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001990 if (!dentry->d_inode)
1991 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1992
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001993 BUG_ON(d_ancestor(dentry, target));
1994 BUG_ON(d_ancestor(target, dentry));
1995
Linus Torvalds1da177e2005-04-16 15:20:36 -07001996 write_seqlock(&rename_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001997
1998 dentry_lock_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001999
2000 /* Move the dentry to the target hash queue, if on different bucket */
Nick Piggin789680d2011-01-07 17:49:30 +11002001 spin_lock(&dcache_hash_lock);
2002 if (!d_unhashed(dentry))
2003 hlist_del_rcu(&dentry->d_hash);
2004 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
2005 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002006
2007 /* Unhash the target: dput() will then get rid of it */
2008 __d_drop(target);
2009
Eric Dumazet5160ee62006-01-08 01:03:32 -08002010 list_del(&dentry->d_u.d_child);
2011 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002012
2013 /* Switch the names.. */
2014 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002015 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002016
2017 /* ... and switch the parents */
2018 if (IS_ROOT(dentry)) {
2019 dentry->d_parent = target->d_parent;
2020 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002021 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002022 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002023 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024
2025 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08002026 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002027 }
2028
Eric Dumazet5160ee62006-01-08 01:03:32 -08002029 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002030
2031 dentry_unlock_parents_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002032 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08002033 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034 spin_unlock(&dentry->d_lock);
2035 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002036}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002037EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002038
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002039/**
2040 * d_ancestor - search for an ancestor
2041 * @p1: ancestor dentry
2042 * @p2: child dentry
2043 *
2044 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2045 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07002046 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002047struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002048{
2049 struct dentry *p;
2050
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09002051 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002052 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002053 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002054 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002055 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002056}
2057
2058/*
2059 * This helper attempts to cope with remotely renamed directories
2060 *
2061 * It assumes that the caller is already holding
Nick Pigginb5c84bf2011-01-07 17:49:38 +11002062 * dentry->d_parent->d_inode->i_mutex and the dcache_inode_lock
Trond Myklebust9eaef272006-10-21 10:24:20 -07002063 *
2064 * Note: If ever the locking in lock_rename() changes, then please
2065 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07002066 */
2067static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002068 __releases(dcache_inode_lock)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002069{
2070 struct mutex *m1 = NULL, *m2 = NULL;
2071 struct dentry *ret;
2072
2073 /* If alias and dentry share a parent, then no extra locks required */
2074 if (alias->d_parent == dentry->d_parent)
2075 goto out_unalias;
2076
2077 /* Check for loops */
2078 ret = ERR_PTR(-ELOOP);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002079 if (d_ancestor(alias, dentry))
Trond Myklebust9eaef272006-10-21 10:24:20 -07002080 goto out_err;
2081
2082 /* See lock_rename() */
2083 ret = ERR_PTR(-EBUSY);
2084 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2085 goto out_err;
2086 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2087 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2088 goto out_err;
2089 m2 = &alias->d_parent->d_inode->i_mutex;
2090out_unalias:
Nick Pigginb5c84bf2011-01-07 17:49:38 +11002091 d_move(alias, dentry);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002092 ret = alias;
2093out_err:
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002094 spin_unlock(&dcache_inode_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002095 if (m2)
2096 mutex_unlock(m2);
2097 if (m1)
2098 mutex_unlock(m1);
2099 return ret;
2100}
2101
2102/*
David Howells770bfad2006-08-22 20:06:07 -04002103 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2104 * named dentry in place of the dentry to be replaced.
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002105 * returns with anon->d_lock held!
David Howells770bfad2006-08-22 20:06:07 -04002106 */
2107static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2108{
2109 struct dentry *dparent, *aparent;
2110
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002111 dentry_lock_for_move(anon, dentry);
David Howells770bfad2006-08-22 20:06:07 -04002112
2113 dparent = dentry->d_parent;
2114 aparent = anon->d_parent;
2115
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002116 switch_names(dentry, anon);
2117 swap(dentry->d_name.hash, anon->d_name.hash);
2118
David Howells770bfad2006-08-22 20:06:07 -04002119 dentry->d_parent = (aparent == anon) ? dentry : aparent;
2120 list_del(&dentry->d_u.d_child);
2121 if (!IS_ROOT(dentry))
2122 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2123 else
2124 INIT_LIST_HEAD(&dentry->d_u.d_child);
2125
2126 anon->d_parent = (dparent == dentry) ? anon : dparent;
2127 list_del(&anon->d_u.d_child);
2128 if (!IS_ROOT(anon))
2129 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
2130 else
2131 INIT_LIST_HEAD(&anon->d_u.d_child);
2132
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002133 dentry_unlock_parents_for_move(anon, dentry);
2134 spin_unlock(&dentry->d_lock);
2135
2136 /* anon->d_lock still locked, returns locked */
David Howells770bfad2006-08-22 20:06:07 -04002137 anon->d_flags &= ~DCACHE_DISCONNECTED;
2138}
2139
2140/**
2141 * d_materialise_unique - introduce an inode into the tree
2142 * @dentry: candidate dentry
2143 * @inode: inode to bind to the dentry, to which aliases may be attached
2144 *
2145 * Introduces an dentry into the tree, substituting an extant disconnected
2146 * root directory alias in its place if there is one
2147 */
2148struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2149{
Trond Myklebust9eaef272006-10-21 10:24:20 -07002150 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04002151
2152 BUG_ON(!d_unhashed(dentry));
2153
David Howells770bfad2006-08-22 20:06:07 -04002154 if (!inode) {
2155 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09002156 __d_instantiate(dentry, NULL);
Nick Piggin357f8e62011-01-07 17:49:42 +11002157 d_rehash(actual);
2158 goto out_nolock;
David Howells770bfad2006-08-22 20:06:07 -04002159 }
2160
Nick Piggin357f8e62011-01-07 17:49:42 +11002161 spin_lock(&dcache_inode_lock);
2162
Trond Myklebust9eaef272006-10-21 10:24:20 -07002163 if (S_ISDIR(inode->i_mode)) {
2164 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04002165
Trond Myklebust9eaef272006-10-21 10:24:20 -07002166 /* Does an aliased dentry already exist? */
2167 alias = __d_find_alias(inode, 0);
2168 if (alias) {
2169 actual = alias;
2170 /* Is this an anonymous mountpoint that we could splice
2171 * into our tree? */
2172 if (IS_ROOT(alias)) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002173 __d_materialise_dentry(dentry, alias);
2174 __d_drop(alias);
2175 goto found;
2176 }
2177 /* Nope, but we must(!) avoid directory aliasing */
2178 actual = __d_unalias(dentry, alias);
2179 if (IS_ERR(actual))
2180 dput(alias);
2181 goto out_nolock;
2182 }
David Howells770bfad2006-08-22 20:06:07 -04002183 }
2184
2185 /* Add a unique reference */
2186 actual = __d_instantiate_unique(dentry, inode);
2187 if (!actual)
2188 actual = dentry;
Nick Piggin357f8e62011-01-07 17:49:42 +11002189 else
2190 BUG_ON(!d_unhashed(actual));
David Howells770bfad2006-08-22 20:06:07 -04002191
David Howells770bfad2006-08-22 20:06:07 -04002192 spin_lock(&actual->d_lock);
2193found:
Nick Piggin789680d2011-01-07 17:49:30 +11002194 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04002195 _d_rehash(actual);
Nick Piggin789680d2011-01-07 17:49:30 +11002196 spin_unlock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04002197 spin_unlock(&actual->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002198 spin_unlock(&dcache_inode_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002199out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04002200 if (actual == dentry) {
2201 security_d_instantiate(dentry, inode);
2202 return NULL;
2203 }
2204
2205 iput(inode);
2206 return actual;
David Howells770bfad2006-08-22 20:06:07 -04002207}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002208EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04002209
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002210static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01002211{
2212 *buflen -= namelen;
2213 if (*buflen < 0)
2214 return -ENAMETOOLONG;
2215 *buffer -= namelen;
2216 memcpy(*buffer, str, namelen);
2217 return 0;
2218}
2219
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002220static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2221{
2222 return prepend(buffer, buflen, name->name, name->len);
2223}
2224
Linus Torvalds1da177e2005-04-16 15:20:36 -07002225/**
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002226 * Prepend path string to a buffer
2227 *
2228 * @path: the dentry/vfsmount to report
2229 * @root: root vfsmnt/dentry (may be modified by this function)
2230 * @buffer: pointer to the end of the buffer
2231 * @buflen: pointer to buffer length
2232 *
Nick Piggin949854d2011-01-07 17:49:37 +11002233 * Caller holds the rename_lock.
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002234 *
2235 * If path is not reachable from the supplied root, then the value of
2236 * root is changed (without modifying refcounts).
2237 */
2238static int prepend_path(const struct path *path, struct path *root,
2239 char **buffer, int *buflen)
2240{
2241 struct dentry *dentry = path->dentry;
2242 struct vfsmount *vfsmnt = path->mnt;
2243 bool slash = false;
2244 int error = 0;
2245
Nick Piggin99b7db72010-08-18 04:37:39 +10002246 br_read_lock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002247 while (dentry != root->dentry || vfsmnt != root->mnt) {
2248 struct dentry * parent;
2249
2250 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2251 /* Global root? */
2252 if (vfsmnt->mnt_parent == vfsmnt) {
2253 goto global_root;
2254 }
2255 dentry = vfsmnt->mnt_mountpoint;
2256 vfsmnt = vfsmnt->mnt_parent;
2257 continue;
2258 }
2259 parent = dentry->d_parent;
2260 prefetch(parent);
Nick Piggin9abca362011-01-07 17:49:36 +11002261 spin_lock(&dentry->d_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002262 error = prepend_name(buffer, buflen, &dentry->d_name);
Nick Piggin9abca362011-01-07 17:49:36 +11002263 spin_unlock(&dentry->d_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002264 if (!error)
2265 error = prepend(buffer, buflen, "/", 1);
2266 if (error)
2267 break;
2268
2269 slash = true;
2270 dentry = parent;
2271 }
2272
2273out:
2274 if (!error && !slash)
2275 error = prepend(buffer, buflen, "/", 1);
2276
Nick Piggin99b7db72010-08-18 04:37:39 +10002277 br_read_unlock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002278 return error;
2279
2280global_root:
2281 /*
2282 * Filesystems needing to implement special "root names"
2283 * should do so with ->d_dname()
2284 */
2285 if (IS_ROOT(dentry) &&
2286 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2287 WARN(1, "Root dentry has weird name <%.*s>\n",
2288 (int) dentry->d_name.len, dentry->d_name.name);
2289 }
2290 root->mnt = vfsmnt;
2291 root->dentry = dentry;
2292 goto out;
2293}
2294
2295/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002296 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002297 * @path: the dentry/vfsmount to report
2298 * @root: root vfsmnt/dentry (may be modified by this function)
Randy Dunlapcd956a12010-08-14 13:05:31 -07002299 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300 * @buflen: buffer length
2301 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002302 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002303 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002304 * Returns a pointer into the buffer or an error code if the
2305 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002306 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002307 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002308 *
2309 * If path is not reachable from the supplied root, then the value of
2310 * root is changed (without modifying refcounts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002311 */
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002312char *__d_path(const struct path *path, struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002313 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002314{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002315 char *res = buf + buflen;
2316 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002317
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002318 prepend(&res, &buflen, "\0", 1);
Nick Piggin949854d2011-01-07 17:49:37 +11002319 write_seqlock(&rename_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002320 error = prepend_path(path, root, &res, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002321 write_sequnlock(&rename_lock);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002322
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002323 if (error)
2324 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002325 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002326}
2327
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002328/*
2329 * same as __d_path but appends "(deleted)" for unlinked files.
2330 */
2331static int path_with_deleted(const struct path *path, struct path *root,
2332 char **buf, int *buflen)
2333{
2334 prepend(buf, buflen, "\0", 1);
2335 if (d_unlinked(path->dentry)) {
2336 int error = prepend(buf, buflen, " (deleted)", 10);
2337 if (error)
2338 return error;
2339 }
2340
2341 return prepend_path(path, root, buf, buflen);
2342}
2343
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002344static int prepend_unreachable(char **buffer, int *buflen)
2345{
2346 return prepend(buffer, buflen, "(unreachable)", 13);
2347}
2348
Jan Bluncka03a8a702008-02-14 19:38:32 -08002349/**
2350 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002351 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002352 * @buf: buffer to return value in
2353 * @buflen: buffer length
2354 *
2355 * Convert a dentry into an ASCII path name. If the entry has been deleted
2356 * the string " (deleted)" is appended. Note that this is ambiguous.
2357 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002358 * Returns a pointer into the buffer or an error code if the path was
2359 * too long. Note: Callers should use the returned pointer, not the passed
2360 * in buffer, to use the name! The implementation often starts at an offset
2361 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002362 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002363 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002364 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002365char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002366{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002367 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002368 struct path root;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002369 struct path tmp;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002370 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002372 /*
2373 * We have various synthetic filesystems that never get mounted. On
2374 * these filesystems dentries are never used for lookup purposes, and
2375 * thus don't need to be hashed. They also don't need a name until a
2376 * user wants to identify the object in /proc/pid/fd/. The little hack
2377 * below allows us to generate a name for these objects on demand:
2378 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002379 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2380 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002381
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002382 get_fs_root(current->fs, &root);
Nick Piggin949854d2011-01-07 17:49:37 +11002383 write_seqlock(&rename_lock);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002384 tmp = root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002385 error = path_with_deleted(path, &tmp, &res, &buflen);
2386 if (error)
2387 res = ERR_PTR(error);
Nick Piggin949854d2011-01-07 17:49:37 +11002388 write_sequnlock(&rename_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002389 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002390 return res;
2391}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002392EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002393
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002394/**
2395 * d_path_with_unreachable - return the path of a dentry
2396 * @path: path to report
2397 * @buf: buffer to return value in
2398 * @buflen: buffer length
2399 *
2400 * The difference from d_path() is that this prepends "(unreachable)"
2401 * to paths which are unreachable from the current process' root.
2402 */
2403char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2404{
2405 char *res = buf + buflen;
2406 struct path root;
2407 struct path tmp;
2408 int error;
2409
2410 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2411 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2412
2413 get_fs_root(current->fs, &root);
Nick Piggin949854d2011-01-07 17:49:37 +11002414 write_seqlock(&rename_lock);
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002415 tmp = root;
2416 error = path_with_deleted(path, &tmp, &res, &buflen);
2417 if (!error && !path_equal(&tmp, &root))
2418 error = prepend_unreachable(&res, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002419 write_sequnlock(&rename_lock);
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002420 path_put(&root);
2421 if (error)
2422 res = ERR_PTR(error);
2423
2424 return res;
2425}
2426
Linus Torvalds1da177e2005-04-16 15:20:36 -07002427/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002428 * Helper function for dentry_operations.d_dname() members
2429 */
2430char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2431 const char *fmt, ...)
2432{
2433 va_list args;
2434 char temp[64];
2435 int sz;
2436
2437 va_start(args, fmt);
2438 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2439 va_end(args);
2440
2441 if (sz > sizeof(temp) || sz > buflen)
2442 return ERR_PTR(-ENAMETOOLONG);
2443
2444 buffer += buflen - sz;
2445 return memcpy(buffer, temp, sz);
2446}
2447
2448/*
Ram Pai6092d042008-03-27 13:06:20 +01002449 * Write full pathname from the root of the filesystem into the buffer.
2450 */
Nick Pigginec2447c2011-01-07 17:49:29 +11002451static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002452{
2453 char *end = buf + buflen;
2454 char *retval;
2455
Ram Pai6092d042008-03-27 13:06:20 +01002456 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002457 if (buflen < 1)
2458 goto Elong;
2459 /* Get '/' right */
2460 retval = end-1;
2461 *retval = '/';
2462
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002463 while (!IS_ROOT(dentry)) {
2464 struct dentry *parent = dentry->d_parent;
Nick Piggin9abca362011-01-07 17:49:36 +11002465 int error;
Ram Pai6092d042008-03-27 13:06:20 +01002466
Ram Pai6092d042008-03-27 13:06:20 +01002467 prefetch(parent);
Nick Piggin9abca362011-01-07 17:49:36 +11002468 spin_lock(&dentry->d_lock);
2469 error = prepend_name(&end, &buflen, &dentry->d_name);
2470 spin_unlock(&dentry->d_lock);
2471 if (error != 0 || prepend(&end, &buflen, "/", 1) != 0)
Ram Pai6092d042008-03-27 13:06:20 +01002472 goto Elong;
2473
2474 retval = end;
2475 dentry = parent;
2476 }
Al Viroc1031352010-06-06 22:31:14 -04002477 return retval;
2478Elong:
2479 return ERR_PTR(-ENAMETOOLONG);
2480}
Nick Pigginec2447c2011-01-07 17:49:29 +11002481
2482char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2483{
2484 char *retval;
2485
Nick Piggin949854d2011-01-07 17:49:37 +11002486 write_seqlock(&rename_lock);
Nick Pigginec2447c2011-01-07 17:49:29 +11002487 retval = __dentry_path(dentry, buf, buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002488 write_sequnlock(&rename_lock);
Nick Pigginec2447c2011-01-07 17:49:29 +11002489
2490 return retval;
2491}
2492EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04002493
2494char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2495{
2496 char *p = NULL;
2497 char *retval;
2498
Nick Piggin949854d2011-01-07 17:49:37 +11002499 write_seqlock(&rename_lock);
Al Viroc1031352010-06-06 22:31:14 -04002500 if (d_unlinked(dentry)) {
2501 p = buf + buflen;
2502 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2503 goto Elong;
2504 buflen++;
2505 }
2506 retval = __dentry_path(dentry, buf, buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002507 write_sequnlock(&rename_lock);
Al Viroc1031352010-06-06 22:31:14 -04002508 if (!IS_ERR(retval) && p)
2509 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002510 return retval;
2511Elong:
Ram Pai6092d042008-03-27 13:06:20 +01002512 return ERR_PTR(-ENAMETOOLONG);
2513}
2514
2515/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002516 * NOTE! The user-level library version returns a
2517 * character pointer. The kernel system call just
2518 * returns the length of the buffer filled (which
2519 * includes the ending '\0' character), or a negative
2520 * error value. So libc would do something like
2521 *
2522 * char *getcwd(char * buf, size_t size)
2523 * {
2524 * int retval;
2525 *
2526 * retval = sys_getcwd(buf, size);
2527 * if (retval >= 0)
2528 * return buf;
2529 * errno = -retval;
2530 * return NULL;
2531 * }
2532 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002533SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002534{
Linus Torvalds552ce542007-02-13 12:08:18 -08002535 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002536 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002537 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538
2539 if (!page)
2540 return -ENOMEM;
2541
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002542 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002543
Linus Torvalds552ce542007-02-13 12:08:18 -08002544 error = -ENOENT;
Nick Piggin949854d2011-01-07 17:49:37 +11002545 write_seqlock(&rename_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002546 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002547 unsigned long len;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002548 struct path tmp = root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002549 char *cwd = page + PAGE_SIZE;
2550 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002551
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002552 prepend(&cwd, &buflen, "\0", 1);
2553 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002554 write_sequnlock(&rename_lock);
Linus Torvalds552ce542007-02-13 12:08:18 -08002555
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002556 if (error)
Linus Torvalds552ce542007-02-13 12:08:18 -08002557 goto out;
2558
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002559 /* Unreachable from current root */
2560 if (!path_equal(&tmp, &root)) {
2561 error = prepend_unreachable(&cwd, &buflen);
2562 if (error)
2563 goto out;
2564 }
2565
Linus Torvalds552ce542007-02-13 12:08:18 -08002566 error = -ERANGE;
2567 len = PAGE_SIZE + page - cwd;
2568 if (len <= size) {
2569 error = len;
2570 if (copy_to_user(buf, cwd, len))
2571 error = -EFAULT;
2572 }
Nick Piggin949854d2011-01-07 17:49:37 +11002573 } else {
2574 write_sequnlock(&rename_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002575 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002576
2577out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002578 path_put(&pwd);
2579 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 free_page((unsigned long) page);
2581 return error;
2582}
2583
2584/*
2585 * Test whether new_dentry is a subdirectory of old_dentry.
2586 *
2587 * Trivially implemented using the dcache structure
2588 */
2589
2590/**
2591 * is_subdir - is new dentry a subdirectory of old_dentry
2592 * @new_dentry: new dentry
2593 * @old_dentry: old dentry
2594 *
2595 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2596 * Returns 0 otherwise.
2597 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2598 */
2599
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002600int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002601{
2602 int result;
Nick Piggin949854d2011-01-07 17:49:37 +11002603 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002605 if (new_dentry == old_dentry)
2606 return 1;
2607
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002608 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002609 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002610 seq = read_seqbegin(&rename_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002611 /*
2612 * Need rcu_readlock to protect against the d_parent trashing
2613 * due to d_move
2614 */
2615 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002616 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002618 else
2619 result = 0;
Nick Piggin949854d2011-01-07 17:49:37 +11002620 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002621 } while (read_seqretry(&rename_lock, seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002622
2623 return result;
2624}
2625
Al Viro2096f752010-01-30 13:16:21 -05002626int path_is_under(struct path *path1, struct path *path2)
2627{
2628 struct vfsmount *mnt = path1->mnt;
2629 struct dentry *dentry = path1->dentry;
2630 int res;
Nick Piggin99b7db72010-08-18 04:37:39 +10002631
2632 br_read_lock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002633 if (mnt != path2->mnt) {
2634 for (;;) {
2635 if (mnt->mnt_parent == mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +10002636 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002637 return 0;
2638 }
2639 if (mnt->mnt_parent == path2->mnt)
2640 break;
2641 mnt = mnt->mnt_parent;
2642 }
2643 dentry = mnt->mnt_mountpoint;
2644 }
2645 res = is_subdir(dentry, path2->dentry);
Nick Piggin99b7db72010-08-18 04:37:39 +10002646 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002647 return res;
2648}
2649EXPORT_SYMBOL(path_is_under);
2650
Linus Torvalds1da177e2005-04-16 15:20:36 -07002651void d_genocide(struct dentry *root)
2652{
Nick Piggin949854d2011-01-07 17:49:37 +11002653 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11002655 unsigned seq;
Nick Piggin58db63d2011-01-07 17:49:39 +11002656 int locked = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002657
Nick Piggin949854d2011-01-07 17:49:37 +11002658 seq = read_seqbegin(&rename_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11002659again:
2660 this_parent = root;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002661 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002662repeat:
2663 next = this_parent->d_subdirs.next;
2664resume:
2665 while (next != &this_parent->d_subdirs) {
2666 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002667 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668 next = tmp->next;
Nick Piggin949854d2011-01-07 17:49:37 +11002669
Nick Pigginda502952011-01-07 17:49:33 +11002670 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2671 if (d_unhashed(dentry) || !dentry->d_inode) {
2672 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002673 continue;
Nick Pigginda502952011-01-07 17:49:33 +11002674 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002676 spin_unlock(&this_parent->d_lock);
2677 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002678 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002679 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 goto repeat;
2681 }
Nick Piggin949854d2011-01-07 17:49:37 +11002682 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
2683 dentry->d_flags |= DCACHE_GENOCIDE;
2684 dentry->d_count--;
2685 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002686 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002687 }
2688 if (this_parent != root) {
Nick Piggin949854d2011-01-07 17:49:37 +11002689 struct dentry *tmp;
2690 struct dentry *child;
2691
2692 tmp = this_parent->d_parent;
2693 if (!(this_parent->d_flags & DCACHE_GENOCIDE)) {
2694 this_parent->d_flags |= DCACHE_GENOCIDE;
2695 this_parent->d_count--;
2696 }
2697 rcu_read_lock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002698 spin_unlock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002699 child = this_parent;
2700 this_parent = tmp;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002701 spin_lock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002702 /* might go back up the wrong parent if we have had a rename
2703 * or deletion */
2704 if (this_parent != child->d_parent ||
Nick Piggin58db63d2011-01-07 17:49:39 +11002705 (!locked && read_seqretry(&rename_lock, seq))) {
Nick Piggin949854d2011-01-07 17:49:37 +11002706 spin_unlock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002707 rcu_read_unlock();
2708 goto rename_retry;
2709 }
2710 rcu_read_unlock();
2711 next = child->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002712 goto resume;
2713 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002714 spin_unlock(&this_parent->d_lock);
Nick Piggin58db63d2011-01-07 17:49:39 +11002715 if (!locked && read_seqretry(&rename_lock, seq))
Nick Piggin949854d2011-01-07 17:49:37 +11002716 goto rename_retry;
Nick Piggin58db63d2011-01-07 17:49:39 +11002717 if (locked)
2718 write_sequnlock(&rename_lock);
2719 return;
2720
2721rename_retry:
2722 locked = 1;
2723 write_seqlock(&rename_lock);
2724 goto again;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002725}
2726
2727/**
2728 * find_inode_number - check for dentry with name
2729 * @dir: directory to check
2730 * @name: Name to find.
2731 *
2732 * Check whether a dentry already exists for the given name,
2733 * and return the inode number if it has an inode. Otherwise
2734 * 0 is returned.
2735 *
2736 * This routine is used to post-process directory listings for
2737 * filesystems using synthetic inode numbers, and is necessary
2738 * to keep getcwd() working.
2739 */
2740
2741ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2742{
2743 struct dentry * dentry;
2744 ino_t ino = 0;
2745
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002746 dentry = d_hash_and_lookup(dir, name);
2747 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002748 if (dentry->d_inode)
2749 ino = dentry->d_inode->i_ino;
2750 dput(dentry);
2751 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002752 return ino;
2753}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002754EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002755
2756static __initdata unsigned long dhash_entries;
2757static int __init set_dhash_entries(char *str)
2758{
2759 if (!str)
2760 return 0;
2761 dhash_entries = simple_strtoul(str, &str, 0);
2762 return 1;
2763}
2764__setup("dhash_entries=", set_dhash_entries);
2765
2766static void __init dcache_init_early(void)
2767{
2768 int loop;
2769
2770 /* If hashes are distributed across NUMA nodes, defer
2771 * hash allocation until vmalloc space is available.
2772 */
2773 if (hashdist)
2774 return;
2775
2776 dentry_hashtable =
2777 alloc_large_system_hash("Dentry cache",
2778 sizeof(struct hlist_head),
2779 dhash_entries,
2780 13,
2781 HASH_EARLY,
2782 &d_hash_shift,
2783 &d_hash_mask,
2784 0);
2785
2786 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2787 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2788}
2789
Denis Cheng74bf17c2007-10-16 23:26:30 -07002790static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002791{
2792 int loop;
2793
2794 /*
2795 * A constructor could be added for stable state like the lists,
2796 * but it is probably not worth it because of the cache nature
2797 * of the dcache.
2798 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002799 dentry_cache = KMEM_CACHE(dentry,
2800 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002801
Rusty Russell8e1f9362007-07-17 04:03:17 -07002802 register_shrinker(&dcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002803
2804 /* Hash may have been set up in dcache_init_early */
2805 if (!hashdist)
2806 return;
2807
2808 dentry_hashtable =
2809 alloc_large_system_hash("Dentry cache",
2810 sizeof(struct hlist_head),
2811 dhash_entries,
2812 13,
2813 0,
2814 &d_hash_shift,
2815 &d_hash_mask,
2816 0);
2817
2818 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2819 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2820}
2821
2822/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002823struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002824EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002825
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826EXPORT_SYMBOL(d_genocide);
2827
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828void __init vfs_caches_init_early(void)
2829{
2830 dcache_init_early();
2831 inode_init_early();
2832}
2833
2834void __init vfs_caches_init(unsigned long mempages)
2835{
2836 unsigned long reserve;
2837
2838 /* Base hash sizes on available memory, with a reserve equal to
2839 150% of current kernel size */
2840
2841 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2842 mempages -= reserve;
2843
2844 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002845 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002846
Denis Cheng74bf17c2007-10-16 23:26:30 -07002847 dcache_init();
2848 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002849 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07002850 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851 bdev_cache_init();
2852 chrdev_init();
2853}