blob: a09f0771fd27a3271245e29d073ecf1d4aaaa3a0 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/dcache.c
3 *
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
7 */
8
9/*
10 * Notes on the allocation strategy:
11 *
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
John McCutchan7a91bf72005-08-08 13:52:16 -040021#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/slab.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/hash.h>
25#include <linux/cache.h>
26#include <linux/module.h>
27#include <linux/mount.h>
28#include <linux/file.h>
29#include <asm/uaccess.h>
30#include <linux/security.h>
31#include <linux/seqlock.h>
32#include <linux/swap.h>
33#include <linux/bootmem.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Frederic Weisbecker613afbf2009-07-16 15:44:29 +020035#include <linux/hardirq.h>
David Howells07f3f052006-09-30 20:52:18 +020036#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Nick Piggin789680d2011-01-07 17:49:30 +110038/*
39 * Usage:
Nick Pigginb23fb0a2011-01-07 17:49:35 +110040 * dcache_inode_lock protects:
41 * - i_dentry, d_alias, d_inode
Nick Piggin23044502011-01-07 17:49:31 +110042 * dcache_hash_lock protects:
43 * - the dcache hash table, s_anon lists
44 * dcache_lru_lock protects:
45 * - the dcache lru lists and counters
46 * d_lock protects:
47 * - d_flags
48 * - d_name
49 * - d_lru
Nick Pigginb7ab39f2011-01-07 17:49:32 +110050 * - d_count
Nick Pigginda502952011-01-07 17:49:33 +110051 * - d_unhashed()
Nick Piggin2fd6b7f2011-01-07 17:49:34 +110052 * - d_parent and d_subdirs
53 * - childrens' d_child and d_parent
Nick Pigginb23fb0a2011-01-07 17:49:35 +110054 * - d_alias, d_inode
Nick Piggin789680d2011-01-07 17:49:30 +110055 *
56 * Ordering:
57 * dcache_lock
Nick Pigginb23fb0a2011-01-07 17:49:35 +110058 * dcache_inode_lock
59 * dentry->d_lock
60 * dcache_lru_lock
61 * dcache_hash_lock
Nick Piggin789680d2011-01-07 17:49:30 +110062 *
Nick Pigginda502952011-01-07 17:49:33 +110063 * If there is an ancestor relationship:
64 * dentry->d_parent->...->d_parent->d_lock
65 * ...
66 * dentry->d_parent->d_lock
67 * dentry->d_lock
68 *
69 * If no ancestor relationship:
Nick Piggin789680d2011-01-07 17:49:30 +110070 * if (dentry1 < dentry2)
71 * dentry1->d_lock
72 * dentry2->d_lock
73 */
Eric Dumazetfa3536c2006-03-26 01:37:24 -080074int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070075EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
76
Nick Pigginb23fb0a2011-01-07 17:49:35 +110077__cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_inode_lock);
Nick Piggin789680d2011-01-07 17:49:30 +110078static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_hash_lock);
Nick Piggin23044502011-01-07 17:49:31 +110079static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
Nick Piggin789680d2011-01-07 17:49:30 +110080__cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -040081__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070082
Nick Pigginb23fb0a2011-01-07 17:49:35 +110083EXPORT_SYMBOL(dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084EXPORT_SYMBOL(dcache_lock);
85
Christoph Lametere18b8902006-12-06 20:33:20 -080086static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070087
88#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
89
90/*
91 * This is the single most critical data structure when it comes
92 * to the dcache: the hashtable for lookups. Somebody should try
93 * to make this good - I've just made it work.
94 *
95 * This hash-function tries to avoid losing too many bits of hash
96 * information, yet avoid using a prime hash-size or similar.
97 */
98#define D_HASHBITS d_hash_shift
99#define D_HASHMASK d_hash_mask
100
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800101static unsigned int d_hash_mask __read_mostly;
102static unsigned int d_hash_shift __read_mostly;
103static struct hlist_head *dentry_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104
105/* Statistics gathering. */
106struct dentry_stat_t dentry_stat = {
107 .age_limit = 45,
108};
109
Nick Piggin3e880fb2011-01-07 17:49:19 +1100110static DEFINE_PER_CPU(unsigned int, nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400111
112#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Nick Piggin3e880fb2011-01-07 17:49:19 +1100113static int get_nr_dentry(void)
114{
115 int i;
116 int sum = 0;
117 for_each_possible_cpu(i)
118 sum += per_cpu(nr_dentry, i);
119 return sum < 0 ? 0 : sum;
120}
121
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400122int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
123 size_t *lenp, loff_t *ppos)
124{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100125 dentry_stat.nr_dentry = get_nr_dentry();
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400126 return proc_dointvec(table, write, buffer, lenp, ppos);
127}
128#endif
129
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400130static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700131{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400132 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
133
Arjan van de Venfd217f42008-10-21 06:47:33 -0700134 WARN_ON(!list_empty(&dentry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 if (dname_external(dentry))
136 kfree(dentry->d_name.name);
137 kmem_cache_free(dentry_cache, dentry);
138}
139
140/*
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400141 * no dcache_lock, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 */
143static void d_free(struct dentry *dentry)
144{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100145 BUG_ON(dentry->d_count);
Nick Piggin3e880fb2011-01-07 17:49:19 +1100146 this_cpu_dec(nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (dentry->d_op && dentry->d_op->d_release)
148 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400149
Eric Dumazetb3423412006-12-06 20:38:48 -0800150 /* if dentry was never inserted into hash, immediate free is OK */
Akinobu Mitae8462ca2008-02-06 01:37:07 -0800151 if (hlist_unhashed(&dentry->d_hash))
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400152 __d_free(&dentry->d_u.d_rcu);
Eric Dumazetb3423412006-12-06 20:38:48 -0800153 else
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400154 call_rcu(&dentry->d_u.d_rcu, __d_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157/*
158 * Release the dentry's inode, using the filesystem
159 * d_iput() operation if defined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800161static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200162 __releases(dentry->d_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100163 __releases(dcache_inode_lock)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200164 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165{
166 struct inode *inode = dentry->d_inode;
167 if (inode) {
168 dentry->d_inode = NULL;
169 list_del_init(&dentry->d_alias);
170 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100171 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 spin_unlock(&dcache_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700173 if (!inode->i_nlink)
174 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700175 if (dentry->d_op && dentry->d_op->d_iput)
176 dentry->d_op->d_iput(dentry, inode);
177 else
178 iput(inode);
179 } else {
180 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100181 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700182 spin_unlock(&dcache_lock);
183 }
184}
185
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700186/*
Nick Piggin23044502011-01-07 17:49:31 +1100187 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700188 */
189static void dentry_lru_add(struct dentry *dentry)
190{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400191 if (list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100192 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400193 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
194 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100195 dentry_stat.nr_unused++;
Nick Piggin23044502011-01-07 17:49:31 +1100196 spin_unlock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400197 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700198}
199
Nick Piggin23044502011-01-07 17:49:31 +1100200static void __dentry_lru_del(struct dentry *dentry)
201{
202 list_del_init(&dentry->d_lru);
203 dentry->d_sb->s_nr_dentry_unused--;
204 dentry_stat.nr_unused--;
205}
206
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700207static void dentry_lru_del(struct dentry *dentry)
208{
209 if (!list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100210 spin_lock(&dcache_lru_lock);
211 __dentry_lru_del(dentry);
212 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700213 }
214}
215
Christoph Hellwiga4633352010-10-10 05:36:26 -0400216static void dentry_lru_move_tail(struct dentry *dentry)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700217{
Nick Piggin23044502011-01-07 17:49:31 +1100218 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400219 if (list_empty(&dentry->d_lru)) {
220 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
221 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100222 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400223 } else {
224 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700225 }
Nick Piggin23044502011-01-07 17:49:31 +1100226 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700227}
228
Miklos Szeredid52b9082007-05-08 00:23:46 -0700229/**
230 * d_kill - kill dentry and return parent
231 * @dentry: dentry to kill
232 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200233 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700234 *
235 * If this is the root of the dentry tree, return NULL.
Nick Piggin23044502011-01-07 17:49:31 +1100236 *
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100237 * dcache_lock and d_lock and d_parent->d_lock must be held by caller, and
238 * are dropped by d_kill.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700239 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100240static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200241 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100242 __releases(parent->d_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100243 __releases(dcache_inode_lock)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200244 __releases(dcache_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700245{
Miklos Szeredid52b9082007-05-08 00:23:46 -0700246 list_del(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100247 if (parent)
248 spin_unlock(&parent->d_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700249 dentry_iput(dentry);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100250 /*
251 * dentry_iput drops the locks, at which point nobody (except
252 * transient RCU lookups) can reach this dentry.
253 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700254 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900255 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700256}
257
Nick Piggin789680d2011-01-07 17:49:30 +1100258/**
259 * d_drop - drop a dentry
260 * @dentry: dentry to drop
261 *
262 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
263 * be found through a VFS lookup any more. Note that this is different from
264 * deleting the dentry - d_delete will try to mark the dentry negative if
265 * possible, giving a successful _negative_ lookup, while d_drop will
266 * just make the cache lookup fail.
267 *
268 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
269 * reason (NFS timeouts or autofs deletes).
270 *
271 * __d_drop requires dentry->d_lock.
272 */
273void __d_drop(struct dentry *dentry)
274{
275 if (!(dentry->d_flags & DCACHE_UNHASHED)) {
276 dentry->d_flags |= DCACHE_UNHASHED;
277 spin_lock(&dcache_hash_lock);
278 hlist_del_rcu(&dentry->d_hash);
279 spin_unlock(&dcache_hash_lock);
280 }
281}
282EXPORT_SYMBOL(__d_drop);
283
284void d_drop(struct dentry *dentry)
285{
286 spin_lock(&dcache_lock);
287 spin_lock(&dentry->d_lock);
288 __d_drop(dentry);
289 spin_unlock(&dentry->d_lock);
290 spin_unlock(&dcache_lock);
291}
292EXPORT_SYMBOL(d_drop);
293
Linus Torvalds1da177e2005-04-16 15:20:36 -0700294/*
295 * This is dput
296 *
297 * This is complicated by the fact that we do not want to put
298 * dentries that are no longer on any hash chain on the unused
299 * list: we'd much rather just get rid of them immediately.
300 *
301 * However, that implies that we have to traverse the dentry
302 * tree upwards to the parents which might _also_ now be
303 * scheduled for deletion (it may have been only waiting for
304 * its last child to go away).
305 *
306 * This tail recursion is done by hand as we don't want to depend
307 * on the compiler to always get this right (gcc generally doesn't).
308 * Real recursion would eat up our stack space.
309 */
310
311/*
312 * dput - release a dentry
313 * @dentry: dentry to release
314 *
315 * Release a dentry. This will drop the usage count and if appropriate
316 * call the dentry unlink method as well as removing it from the queues and
317 * releasing its resources. If the parent dentries were scheduled for release
318 * they too may now get deleted.
319 *
320 * no dcache lock, please.
321 */
322
323void dput(struct dentry *dentry)
324{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100325 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700326 if (!dentry)
327 return;
328
329repeat:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100330 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700331 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700332 spin_lock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100333 if (IS_ROOT(dentry))
334 parent = NULL;
335 else
336 parent = dentry->d_parent;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100337 if (dentry->d_count == 1) {
338 if (!spin_trylock(&dcache_lock)) {
339 /*
340 * Something of a livelock possibility we could avoid
341 * by taking dcache_lock and trying again, but we
342 * want to reduce dcache_lock anyway so this will
343 * get improved.
344 */
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100345drop1:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100346 spin_unlock(&dentry->d_lock);
347 goto repeat;
348 }
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100349 if (!spin_trylock(&dcache_inode_lock)) {
350drop2:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100351 spin_unlock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100352 goto drop1;
353 }
354 if (parent && !spin_trylock(&parent->d_lock)) {
355 spin_unlock(&dcache_inode_lock);
356 goto drop2;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100357 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100358 }
359 dentry->d_count--;
360 if (dentry->d_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700361 spin_unlock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100362 if (parent)
363 spin_unlock(&parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700364 spin_unlock(&dcache_lock);
365 return;
366 }
367
368 /*
369 * AV: ->d_delete() is _NOT_ allowed to block now.
370 */
371 if (dentry->d_op && dentry->d_op->d_delete) {
372 if (dentry->d_op->d_delete(dentry))
373 goto unhash_it;
374 }
Nick Piggin265ac902010-10-10 05:36:24 -0400375
Linus Torvalds1da177e2005-04-16 15:20:36 -0700376 /* Unreachable? Get rid of it */
377 if (d_unhashed(dentry))
378 goto kill_it;
Nick Piggin265ac902010-10-10 05:36:24 -0400379
380 /* Otherwise leave it cached and ensure it's on the LRU */
381 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400382 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400383
Linus Torvalds1da177e2005-04-16 15:20:36 -0700384 spin_unlock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100385 if (parent)
386 spin_unlock(&parent->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100387 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700388 spin_unlock(&dcache_lock);
389 return;
390
391unhash_it:
392 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700393kill_it:
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700394 /* if dentry was on the d_lru list delete it from there */
395 dentry_lru_del(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100396 dentry = d_kill(dentry, parent);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700397 if (dentry)
398 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700399}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700400EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401
402/**
403 * d_invalidate - invalidate a dentry
404 * @dentry: dentry to invalidate
405 *
406 * Try to invalidate the dentry if it turns out to be
407 * possible. If there are other dentries that can be
408 * reached through this one we can't delete it and we
409 * return -EBUSY. On success we return 0.
410 *
411 * no dcache lock.
412 */
413
414int d_invalidate(struct dentry * dentry)
415{
416 /*
417 * If it's already been dropped, return OK.
418 */
419 spin_lock(&dcache_lock);
Nick Pigginda502952011-01-07 17:49:33 +1100420 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700421 if (d_unhashed(dentry)) {
Nick Pigginda502952011-01-07 17:49:33 +1100422 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 spin_unlock(&dcache_lock);
424 return 0;
425 }
426 /*
427 * Check whether to do a partial shrink_dcache
428 * to get rid of unused child entries.
429 */
430 if (!list_empty(&dentry->d_subdirs)) {
Nick Pigginda502952011-01-07 17:49:33 +1100431 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432 spin_unlock(&dcache_lock);
433 shrink_dcache_parent(dentry);
434 spin_lock(&dcache_lock);
Nick Pigginda502952011-01-07 17:49:33 +1100435 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700436 }
437
438 /*
439 * Somebody else still using it?
440 *
441 * If it's a directory, we can't drop it
442 * for fear of somebody re-populating it
443 * with children (even though dropping it
444 * would make it unreachable from the root,
445 * we might still populate it if it was a
446 * working directory or similar).
447 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100448 if (dentry->d_count > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700449 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
450 spin_unlock(&dentry->d_lock);
451 spin_unlock(&dcache_lock);
452 return -EBUSY;
453 }
454 }
455
456 __d_drop(dentry);
457 spin_unlock(&dentry->d_lock);
458 spin_unlock(&dcache_lock);
459 return 0;
460}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700461EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700462
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100463/* This must be called with dcache_lock and d_lock held */
Nick Piggin23044502011-01-07 17:49:31 +1100464static inline struct dentry * __dget_locked_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700465{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100466 dentry->d_count++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400467 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700468 return dentry;
469}
470
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100471/* This should be called _only_ with dcache_lock held */
Nick Piggin23044502011-01-07 17:49:31 +1100472static inline struct dentry * __dget_locked(struct dentry *dentry)
473{
Nick Piggin23044502011-01-07 17:49:31 +1100474 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100475 __dget_locked_dlock(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100476 spin_unlock(&dentry->d_lock);
477 return dentry;
478}
479
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100480struct dentry * dget_locked_dlock(struct dentry *dentry)
481{
482 return __dget_locked_dlock(dentry);
483}
484
Linus Torvalds1da177e2005-04-16 15:20:36 -0700485struct dentry * dget_locked(struct dentry *dentry)
486{
487 return __dget_locked(dentry);
488}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700489EXPORT_SYMBOL(dget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700490
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100491struct dentry *dget_parent(struct dentry *dentry)
492{
493 struct dentry *ret;
494
495repeat:
496 spin_lock(&dentry->d_lock);
497 ret = dentry->d_parent;
498 if (!ret)
499 goto out;
500 if (dentry == ret) {
501 ret->d_count++;
502 goto out;
503 }
504 if (!spin_trylock(&ret->d_lock)) {
505 spin_unlock(&dentry->d_lock);
506 cpu_relax();
507 goto repeat;
508 }
509 BUG_ON(!ret->d_count);
510 ret->d_count++;
511 spin_unlock(&ret->d_lock);
512out:
513 spin_unlock(&dentry->d_lock);
514 return ret;
515}
516EXPORT_SYMBOL(dget_parent);
517
Linus Torvalds1da177e2005-04-16 15:20:36 -0700518/**
519 * d_find_alias - grab a hashed alias of inode
520 * @inode: inode in question
521 * @want_discon: flag, used by d_splice_alias, to request
522 * that only a DISCONNECTED alias be returned.
523 *
524 * If inode has a hashed alias, or is a directory and has any alias,
525 * acquire the reference to alias and return it. Otherwise return NULL.
526 * Notice that if inode is a directory there can be only one alias and
527 * it can be unhashed only if it has no children, or if it is the root
528 * of a filesystem.
529 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700530 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700531 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700532 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 */
Nick Pigginda502952011-01-07 17:49:33 +1100534static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535{
Nick Pigginda502952011-01-07 17:49:33 +1100536 struct dentry *alias, *discon_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537
Nick Pigginda502952011-01-07 17:49:33 +1100538again:
539 discon_alias = NULL;
540 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
541 spin_lock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700543 if (IS_ROOT(alias) &&
Nick Pigginda502952011-01-07 17:49:33 +1100544 (alias->d_flags & DCACHE_DISCONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700545 discon_alias = alias;
Nick Pigginda502952011-01-07 17:49:33 +1100546 } else if (!want_discon) {
547 __dget_locked_dlock(alias);
548 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700549 return alias;
550 }
551 }
Nick Pigginda502952011-01-07 17:49:33 +1100552 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553 }
Nick Pigginda502952011-01-07 17:49:33 +1100554 if (discon_alias) {
555 alias = discon_alias;
556 spin_lock(&alias->d_lock);
557 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
558 if (IS_ROOT(alias) &&
559 (alias->d_flags & DCACHE_DISCONNECTED)) {
560 __dget_locked_dlock(alias);
561 spin_unlock(&alias->d_lock);
562 return alias;
563 }
564 }
565 spin_unlock(&alias->d_lock);
566 goto again;
567 }
568 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700569}
570
Nick Pigginda502952011-01-07 17:49:33 +1100571struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700572{
David Howells214fda12006-03-25 03:06:36 -0800573 struct dentry *de = NULL;
574
575 if (!list_empty(&inode->i_dentry)) {
576 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100577 spin_lock(&dcache_inode_lock);
David Howells214fda12006-03-25 03:06:36 -0800578 de = __d_find_alias(inode, 0);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100579 spin_unlock(&dcache_inode_lock);
David Howells214fda12006-03-25 03:06:36 -0800580 spin_unlock(&dcache_lock);
581 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700582 return de;
583}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700584EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700585
586/*
587 * Try to kill dentries associated with this inode.
588 * WARNING: you must own a reference to inode.
589 */
590void d_prune_aliases(struct inode *inode)
591{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700592 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700593restart:
594 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100595 spin_lock(&dcache_inode_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700596 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700597 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100598 if (!dentry->d_count) {
Nick Piggin23044502011-01-07 17:49:31 +1100599 __dget_locked_dlock(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700600 __d_drop(dentry);
601 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100602 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700603 spin_unlock(&dcache_lock);
604 dput(dentry);
605 goto restart;
606 }
607 spin_unlock(&dentry->d_lock);
608 }
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100609 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700610 spin_unlock(&dcache_lock);
611}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700612EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700613
614/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700615 * Throw away a dentry - free the inode, dput the parent. This requires that
616 * the LRU list has already been removed.
617 *
Miklos Szeredi85864e12007-10-16 23:27:09 -0700618 * Try to prune ancestors as well. This is necessary to prevent
619 * quadratic behavior of shrink_dcache_parent(), but is also expected
620 * to be beneficial in reducing dentry cache fragmentation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700621 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100622static void prune_one_dentry(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200623 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100624 __releases(parent->d_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100625 __releases(dcache_inode_lock)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200626 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700627{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700628 __d_drop(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100629 dentry = d_kill(dentry, parent);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700630
631 /*
632 * Prune ancestors. Locking is simpler than in dput(),
633 * because dcache_lock needs to be taken anyway.
634 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700635 while (dentry) {
Nick Piggin23044502011-01-07 17:49:31 +1100636 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100637 spin_lock(&dcache_inode_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100638again:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100639 spin_lock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100640 if (IS_ROOT(dentry))
641 parent = NULL;
642 else
643 parent = dentry->d_parent;
644 if (parent && !spin_trylock(&parent->d_lock)) {
645 spin_unlock(&dentry->d_lock);
646 goto again;
647 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100648 dentry->d_count--;
649 if (dentry->d_count) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100650 if (parent)
651 spin_unlock(&parent->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100652 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100653 spin_unlock(&dcache_inode_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100654 spin_unlock(&dcache_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700655 return;
Nick Piggin23044502011-01-07 17:49:31 +1100656 }
Miklos Szeredid52b9082007-05-08 00:23:46 -0700657
Christoph Hellwiga4633352010-10-10 05:36:26 -0400658 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700659 __d_drop(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100660 dentry = d_kill(dentry, parent);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700661 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700662}
663
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400664static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700665{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700666 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700667
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400668 while (!list_empty(list)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100669 struct dentry *parent;
670
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400671 dentry = list_entry(list->prev, struct dentry, d_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100672
673 if (!spin_trylock(&dentry->d_lock)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100674relock:
Nick Piggin23044502011-01-07 17:49:31 +1100675 spin_unlock(&dcache_lru_lock);
676 cpu_relax();
677 spin_lock(&dcache_lru_lock);
678 continue;
679 }
680
Linus Torvalds1da177e2005-04-16 15:20:36 -0700681 /*
682 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700683 * the LRU because of laziness during lookup. Do not free
684 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700685 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100686 if (dentry->d_count) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100687 __dentry_lru_del(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700688 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700689 continue;
690 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100691 if (IS_ROOT(dentry))
692 parent = NULL;
693 else
694 parent = dentry->d_parent;
695 if (parent && !spin_trylock(&parent->d_lock)) {
696 spin_unlock(&dentry->d_lock);
697 goto relock;
698 }
699 __dentry_lru_del(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100700 spin_unlock(&dcache_lru_lock);
701
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100702 prune_one_dentry(dentry, parent);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100703 /* dcache_lock, dcache_inode_lock and dentry->d_lock dropped */
Nick Piggin23044502011-01-07 17:49:31 +1100704 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100705 spin_lock(&dcache_inode_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100706 spin_lock(&dcache_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700707 }
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400708}
709
710/**
711 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
712 * @sb: superblock to shrink dentry LRU.
713 * @count: number of entries to prune
714 * @flags: flags to control the dentry processing
715 *
716 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
717 */
718static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
719{
720 /* called from prune_dcache() and shrink_dcache_parent() */
721 struct dentry *dentry;
722 LIST_HEAD(referenced);
723 LIST_HEAD(tmp);
724 int cnt = *count;
725
726 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100727 spin_lock(&dcache_inode_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100728relock:
729 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400730 while (!list_empty(&sb->s_dentry_lru)) {
731 dentry = list_entry(sb->s_dentry_lru.prev,
732 struct dentry, d_lru);
733 BUG_ON(dentry->d_sb != sb);
734
Nick Piggin23044502011-01-07 17:49:31 +1100735 if (!spin_trylock(&dentry->d_lock)) {
736 spin_unlock(&dcache_lru_lock);
737 cpu_relax();
738 goto relock;
739 }
740
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400741 /*
742 * If we are honouring the DCACHE_REFERENCED flag and the
743 * dentry has this flag set, don't free it. Clear the flag
744 * and put it back on the LRU.
745 */
Nick Piggin23044502011-01-07 17:49:31 +1100746 if (flags & DCACHE_REFERENCED &&
747 dentry->d_flags & DCACHE_REFERENCED) {
748 dentry->d_flags &= ~DCACHE_REFERENCED;
749 list_move(&dentry->d_lru, &referenced);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400750 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100751 } else {
752 list_move_tail(&dentry->d_lru, &tmp);
753 spin_unlock(&dentry->d_lock);
754 if (!--cnt)
755 break;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400756 }
Nick Piggin23044502011-01-07 17:49:31 +1100757 /* XXX: re-add cond_resched_lock when dcache_lock goes away */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400758 }
759
760 *count = cnt;
761 shrink_dentry_list(&tmp);
762
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700763 if (!list_empty(&referenced))
764 list_splice(&referenced, &sb->s_dentry_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100765 spin_unlock(&dcache_lru_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100766 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700767 spin_unlock(&dcache_lock);
768}
769
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700770/**
771 * prune_dcache - shrink the dcache
772 * @count: number of entries to try to free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700773 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700774 * Shrink the dcache. This is done when we need more memory, or simply when we
775 * need to unmount something (at which point we need to unuse all dentries).
776 *
777 * This function may fail to free any resources if all the dentries are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700778 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700779static void prune_dcache(int count)
780{
Al Virodca33252010-07-25 02:31:46 +0400781 struct super_block *sb, *p = NULL;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700782 int w_count;
Nick Piggin86c87492011-01-07 17:49:18 +1100783 int unused = dentry_stat.nr_unused;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700784 int prune_ratio;
785 int pruned;
786
787 if (unused == 0 || count == 0)
788 return;
789 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700790 if (count >= unused)
791 prune_ratio = 1;
792 else
793 prune_ratio = unused / count;
794 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400795 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400796 if (list_empty(&sb->s_instances))
797 continue;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700798 if (sb->s_nr_dentry_unused == 0)
799 continue;
800 sb->s_count++;
801 /* Now, we reclaim unused dentrins with fairness.
802 * We reclaim them same percentage from each superblock.
803 * We calculate number of dentries to scan on this sb
804 * as follows, but the implementation is arranged to avoid
805 * overflows:
806 * number of dentries to scan on this sb =
807 * count * (number of dentries on this sb /
808 * number of dentries in the machine)
809 */
810 spin_unlock(&sb_lock);
811 if (prune_ratio != 1)
812 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
813 else
814 w_count = sb->s_nr_dentry_unused;
815 pruned = w_count;
816 /*
817 * We need to be sure this filesystem isn't being unmounted,
818 * otherwise we could race with generic_shutdown_super(), and
819 * end up holding a reference to an inode while the filesystem
820 * is unmounted. So we try to get s_umount, and make sure
821 * s_root isn't NULL.
822 */
823 if (down_read_trylock(&sb->s_umount)) {
824 if ((sb->s_root != NULL) &&
825 (!list_empty(&sb->s_dentry_lru))) {
826 spin_unlock(&dcache_lock);
827 __shrink_dcache_sb(sb, &w_count,
828 DCACHE_REFERENCED);
829 pruned -= w_count;
830 spin_lock(&dcache_lock);
831 }
832 up_read(&sb->s_umount);
833 }
834 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400835 if (p)
836 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700837 count -= pruned;
Al Virodca33252010-07-25 02:31:46 +0400838 p = sb;
Al Viro79893c12010-03-22 20:27:55 -0400839 /* more work left to do? */
840 if (count <= 0)
841 break;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700842 }
Al Virodca33252010-07-25 02:31:46 +0400843 if (p)
844 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700845 spin_unlock(&sb_lock);
846 spin_unlock(&dcache_lock);
847}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700848
849/**
850 * shrink_dcache_sb - shrink dcache for a superblock
851 * @sb: superblock
852 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400853 * Shrink the dcache for the specified super block. This is used to free
854 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700855 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400856void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400858 LIST_HEAD(tmp);
859
860 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100861 spin_lock(&dcache_inode_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100862 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400863 while (!list_empty(&sb->s_dentry_lru)) {
864 list_splice_init(&sb->s_dentry_lru, &tmp);
865 shrink_dentry_list(&tmp);
866 }
Nick Piggin23044502011-01-07 17:49:31 +1100867 spin_unlock(&dcache_lru_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100868 spin_unlock(&dcache_inode_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400869 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700870}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700871EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872
873/*
David Howellsc636ebd2006-10-11 01:22:19 -0700874 * destroy a single subtree of dentries for unmount
875 * - see the comments on shrink_dcache_for_umount() for a description of the
876 * locking
877 */
878static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
879{
880 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700881 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700882
883 BUG_ON(!IS_ROOT(dentry));
884
885 /* detach this root from the system */
886 spin_lock(&dcache_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100887 spin_lock(&dentry->d_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400888 dentry_lru_del(dentry);
David Howellsc636ebd2006-10-11 01:22:19 -0700889 __d_drop(dentry);
Nick Pigginda502952011-01-07 17:49:33 +1100890 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700891 spin_unlock(&dcache_lock);
892
893 for (;;) {
894 /* descend to the first leaf in the current subtree */
895 while (!list_empty(&dentry->d_subdirs)) {
896 struct dentry *loop;
897
898 /* this is a branch with children - detach all of them
899 * from the system in one go */
900 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100901 spin_lock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700902 list_for_each_entry(loop, &dentry->d_subdirs,
903 d_u.d_child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100904 spin_lock_nested(&loop->d_lock,
905 DENTRY_D_LOCK_NESTED);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400906 dentry_lru_del(loop);
David Howellsc636ebd2006-10-11 01:22:19 -0700907 __d_drop(loop);
Nick Pigginda502952011-01-07 17:49:33 +1100908 spin_unlock(&loop->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700909 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100910 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700911 spin_unlock(&dcache_lock);
912
913 /* move to the first child */
914 dentry = list_entry(dentry->d_subdirs.next,
915 struct dentry, d_u.d_child);
916 }
917
918 /* consume the dentries from this leaf up through its parents
919 * until we find one with children or run out altogether */
920 do {
921 struct inode *inode;
922
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100923 if (dentry->d_count != 0) {
David Howellsc636ebd2006-10-11 01:22:19 -0700924 printk(KERN_ERR
925 "BUG: Dentry %p{i=%lx,n=%s}"
926 " still in use (%d)"
927 " [unmount of %s %s]\n",
928 dentry,
929 dentry->d_inode ?
930 dentry->d_inode->i_ino : 0UL,
931 dentry->d_name.name,
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100932 dentry->d_count,
David Howellsc636ebd2006-10-11 01:22:19 -0700933 dentry->d_sb->s_type->name,
934 dentry->d_sb->s_id);
935 BUG();
936 }
937
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100938 if (IS_ROOT(dentry)) {
David Howellsc636ebd2006-10-11 01:22:19 -0700939 parent = NULL;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100940 list_del(&dentry->d_u.d_child);
941 } else {
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900942 parent = dentry->d_parent;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100943 spin_lock(&parent->d_lock);
944 parent->d_count--;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100945 list_del(&dentry->d_u.d_child);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100946 spin_unlock(&parent->d_lock);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900947 }
David Howellsc636ebd2006-10-11 01:22:19 -0700948
David Howellsf8713572006-10-28 10:38:46 -0700949 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700950
951 inode = dentry->d_inode;
952 if (inode) {
953 dentry->d_inode = NULL;
954 list_del_init(&dentry->d_alias);
955 if (dentry->d_op && dentry->d_op->d_iput)
956 dentry->d_op->d_iput(dentry, inode);
957 else
958 iput(inode);
959 }
960
961 d_free(dentry);
962
963 /* finished when we fall off the top of the tree,
964 * otherwise we ascend to the parent and move to the
965 * next sibling if there is one */
966 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400967 return;
David Howellsc636ebd2006-10-11 01:22:19 -0700968 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700969 } while (list_empty(&dentry->d_subdirs));
970
971 dentry = list_entry(dentry->d_subdirs.next,
972 struct dentry, d_u.d_child);
973 }
974}
975
976/*
977 * destroy the dentries attached to a superblock on unmounting
978 * - we don't need to use dentry->d_lock, and only need dcache_lock when
979 * removing the dentry from the system lists and hashes because:
980 * - the superblock is detached from all mountings and open files, so the
981 * dentry trees will not be rearranged by the VFS
982 * - s_umount is write-locked, so the memory pressure shrinker will ignore
983 * any dentries belonging to this superblock that it comes across
984 * - the filesystem itself is no longer permitted to rearrange the dentries
985 * in this superblock
986 */
987void shrink_dcache_for_umount(struct super_block *sb)
988{
989 struct dentry *dentry;
990
991 if (down_read_trylock(&sb->s_umount))
992 BUG();
993
994 dentry = sb->s_root;
995 sb->s_root = NULL;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100996 spin_lock(&dentry->d_lock);
997 dentry->d_count--;
998 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700999 shrink_dcache_for_umount_subtree(dentry);
1000
1001 while (!hlist_empty(&sb->s_anon)) {
1002 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
1003 shrink_dcache_for_umount_subtree(dentry);
1004 }
1005}
1006
1007/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001008 * Search for at least 1 mount point in the dentry's subdirs.
1009 * We descend to the next level whenever the d_subdirs
1010 * list is non-empty and continue searching.
1011 */
1012
1013/**
1014 * have_submounts - check for mounts over a dentry
1015 * @parent: dentry to check.
1016 *
1017 * Return true if the parent or its subdirectories contain
1018 * a mount point
1019 */
1020
1021int have_submounts(struct dentry *parent)
1022{
1023 struct dentry *this_parent = parent;
1024 struct list_head *next;
1025
1026 spin_lock(&dcache_lock);
1027 if (d_mountpoint(parent))
1028 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001029 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001030repeat:
1031 next = this_parent->d_subdirs.next;
1032resume:
1033 while (next != &this_parent->d_subdirs) {
1034 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001035 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001037
1038 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001039 /* Have we found a mount point ? */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001040 if (d_mountpoint(dentry)) {
1041 spin_unlock(&dentry->d_lock);
1042 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001043 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001044 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001046 spin_unlock(&this_parent->d_lock);
1047 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001048 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001049 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001050 goto repeat;
1051 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001052 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001053 }
1054 /*
1055 * All done at this level ... ascend and resume the search.
1056 */
1057 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001058 next = this_parent->d_u.d_child.next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001059 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001060 this_parent = this_parent->d_parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001061 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001062 goto resume;
1063 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001064 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001065 spin_unlock(&dcache_lock);
1066 return 0; /* No mount points found in tree */
1067positive:
1068 spin_unlock(&dcache_lock);
1069 return 1;
1070}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001071EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001072
1073/*
1074 * Search the dentry child list for the specified parent,
1075 * and move any unused dentries to the end of the unused
1076 * list for prune_dcache(). We descend to the next level
1077 * whenever the d_subdirs list is non-empty and continue
1078 * searching.
1079 *
1080 * It returns zero iff there are no unused children,
1081 * otherwise it returns the number of children moved to
1082 * the end of the unused list. This may not be the total
1083 * number of unused children, because select_parent can
1084 * drop the lock and return early due to latency
1085 * constraints.
1086 */
1087static int select_parent(struct dentry * parent)
1088{
1089 struct dentry *this_parent = parent;
1090 struct list_head *next;
1091 int found = 0;
1092
1093 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001094 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095repeat:
1096 next = this_parent->d_subdirs.next;
1097resume:
1098 while (next != &this_parent->d_subdirs) {
1099 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001100 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001101 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001102 BUG_ON(this_parent == dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001104 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Nick Piggin23044502011-01-07 17:49:31 +11001105
Linus Torvalds1da177e2005-04-16 15:20:36 -07001106 /*
1107 * move only zero ref count dentries to the end
1108 * of the unused list for prune_dcache
1109 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001110 if (!dentry->d_count) {
Christoph Hellwiga4633352010-10-10 05:36:26 -04001111 dentry_lru_move_tail(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001112 found++;
Christoph Hellwiga4633352010-10-10 05:36:26 -04001113 } else {
1114 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001115 }
1116
1117 /*
1118 * We can return to the caller if we have found some (this
1119 * ensures forward progress). We'll be coming back to find
1120 * the rest.
1121 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001122 if (found && need_resched()) {
1123 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 goto out;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001125 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126
1127 /*
1128 * Descend a level if the d_subdirs list is non-empty.
1129 */
1130 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001131 spin_unlock(&this_parent->d_lock);
1132 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001133 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001134 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001135 goto repeat;
1136 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001137
1138 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001139 }
1140 /*
1141 * All done at this level ... ascend and resume the search.
1142 */
1143 if (this_parent != parent) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001144 struct dentry *tmp;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001145 next = this_parent->d_u.d_child.next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001146 tmp = this_parent->d_parent;
1147 spin_unlock(&this_parent->d_lock);
1148 BUG_ON(tmp == this_parent);
1149 this_parent = tmp;
1150 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001151 goto resume;
1152 }
1153out:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001154 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001155 spin_unlock(&dcache_lock);
1156 return found;
1157}
1158
1159/**
1160 * shrink_dcache_parent - prune dcache
1161 * @parent: parent of entries to prune
1162 *
1163 * Prune the dcache to remove unused children of the parent dentry.
1164 */
1165
1166void shrink_dcache_parent(struct dentry * parent)
1167{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001168 struct super_block *sb = parent->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 int found;
1170
1171 while ((found = select_parent(parent)) != 0)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001172 __shrink_dcache_sb(sb, &found, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001174EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001175
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176/*
1177 * Scan `nr' dentries and return the number which remain.
1178 *
1179 * We need to avoid reentering the filesystem if the caller is performing a
1180 * GFP_NOFS allocation attempt. One example deadlock is:
1181 *
1182 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1183 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1184 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1185 *
1186 * In this case we return -1 to tell the caller that we baled.
1187 */
Dave Chinner7f8275d2010-07-19 14:56:17 +10001188static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001189{
1190 if (nr) {
1191 if (!(gfp_mask & __GFP_FS))
1192 return -1;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001193 prune_dcache(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 }
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001195
Nick Piggin86c87492011-01-07 17:49:18 +11001196 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197}
1198
Rusty Russell8e1f9362007-07-17 04:03:17 -07001199static struct shrinker dcache_shrinker = {
1200 .shrink = shrink_dcache_memory,
1201 .seeks = DEFAULT_SEEKS,
1202};
1203
Linus Torvalds1da177e2005-04-16 15:20:36 -07001204/**
1205 * d_alloc - allocate a dcache entry
1206 * @parent: parent of entry to allocate
1207 * @name: qstr of the name
1208 *
1209 * Allocates a dentry. It returns %NULL if there is insufficient memory
1210 * available. On a success the dentry is returned. The name passed in is
1211 * copied and the copy passed in may be reused after this call.
1212 */
1213
1214struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1215{
1216 struct dentry *dentry;
1217 char *dname;
1218
Mel Gormane12ba742007-10-16 01:25:52 -07001219 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220 if (!dentry)
1221 return NULL;
1222
1223 if (name->len > DNAME_INLINE_LEN-1) {
1224 dname = kmalloc(name->len + 1, GFP_KERNEL);
1225 if (!dname) {
1226 kmem_cache_free(dentry_cache, dentry);
1227 return NULL;
1228 }
1229 } else {
1230 dname = dentry->d_iname;
1231 }
1232 dentry->d_name.name = dname;
1233
1234 dentry->d_name.len = name->len;
1235 dentry->d_name.hash = name->hash;
1236 memcpy(dname, name->name, name->len);
1237 dname[name->len] = 0;
1238
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001239 dentry->d_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240 dentry->d_flags = DCACHE_UNHASHED;
1241 spin_lock_init(&dentry->d_lock);
1242 dentry->d_inode = NULL;
1243 dentry->d_parent = NULL;
1244 dentry->d_sb = NULL;
1245 dentry->d_op = NULL;
1246 dentry->d_fsdata = NULL;
1247 dentry->d_mounted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001248 INIT_HLIST_NODE(&dentry->d_hash);
1249 INIT_LIST_HEAD(&dentry->d_lru);
1250 INIT_LIST_HEAD(&dentry->d_subdirs);
1251 INIT_LIST_HEAD(&dentry->d_alias);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001252 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001253
1254 if (parent) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001255 spin_lock(&dcache_lock);
1256 spin_lock(&parent->d_lock);
1257 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1258 dentry->d_parent = dget_dlock(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001259 dentry->d_sb = parent->d_sb;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001260 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001261 spin_unlock(&dentry->d_lock);
1262 spin_unlock(&parent->d_lock);
1263 spin_unlock(&dcache_lock);
1264 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265
Nick Piggin3e880fb2011-01-07 17:49:19 +11001266 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001267
Linus Torvalds1da177e2005-04-16 15:20:36 -07001268 return dentry;
1269}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001270EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001271
1272struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1273{
1274 struct qstr q;
1275
1276 q.name = name;
1277 q.len = strlen(name);
1278 q.hash = full_name_hash(q.name, q.len);
1279 return d_alloc(parent, &q);
1280}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001281EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001282
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001283/* the caller must hold dcache_lock */
1284static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1285{
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001286 spin_lock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001287 if (inode)
1288 list_add(&dentry->d_alias, &inode->i_dentry);
1289 dentry->d_inode = inode;
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001290 spin_unlock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001291 fsnotify_d_instantiate(dentry, inode);
1292}
1293
Linus Torvalds1da177e2005-04-16 15:20:36 -07001294/**
1295 * d_instantiate - fill in inode information for a dentry
1296 * @entry: dentry to complete
1297 * @inode: inode to attach to this dentry
1298 *
1299 * Fill in inode information in the entry.
1300 *
1301 * This turns negative dentries into productive full members
1302 * of society.
1303 *
1304 * NOTE! This assumes that the inode count has been incremented
1305 * (or otherwise set) by the caller to indicate that it is now
1306 * in use by the dcache.
1307 */
1308
1309void d_instantiate(struct dentry *entry, struct inode * inode)
1310{
Eric Sesterhenn28133c72006-03-26 18:25:39 +02001311 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001312 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001313 spin_lock(&dcache_inode_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001314 __d_instantiate(entry, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001315 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316 spin_unlock(&dcache_lock);
1317 security_d_instantiate(entry, inode);
1318}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001319EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001320
1321/**
1322 * d_instantiate_unique - instantiate a non-aliased dentry
1323 * @entry: dentry to instantiate
1324 * @inode: inode to attach to this dentry
1325 *
1326 * Fill in inode information in the entry. On success, it returns NULL.
1327 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001328 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001329 *
1330 * Note that in order to avoid conflicts with rename() etc, the caller
1331 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001332 *
1333 * This also assumes that the inode count has been incremented
1334 * (or otherwise set) by the caller to indicate that it is now
1335 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001336 */
David Howells770bfad2006-08-22 20:06:07 -04001337static struct dentry *__d_instantiate_unique(struct dentry *entry,
1338 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339{
1340 struct dentry *alias;
1341 int len = entry->d_name.len;
1342 const char *name = entry->d_name.name;
1343 unsigned int hash = entry->d_name.hash;
1344
David Howells770bfad2006-08-22 20:06:07 -04001345 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001346 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001347 return NULL;
1348 }
1349
Linus Torvalds1da177e2005-04-16 15:20:36 -07001350 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1351 struct qstr *qstr = &alias->d_name;
1352
Nick Piggin9abca362011-01-07 17:49:36 +11001353 /*
1354 * Don't need alias->d_lock here, because aliases with
1355 * d_parent == entry->d_parent are not subject to name or
1356 * parent changes, because the parent inode i_mutex is held.
1357 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001358 if (qstr->hash != hash)
1359 continue;
1360 if (alias->d_parent != entry->d_parent)
1361 continue;
1362 if (qstr->len != len)
1363 continue;
1364 if (memcmp(qstr->name, name, len))
1365 continue;
1366 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001367 return alias;
1368 }
David Howells770bfad2006-08-22 20:06:07 -04001369
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001370 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001371 return NULL;
1372}
David Howells770bfad2006-08-22 20:06:07 -04001373
1374struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1375{
1376 struct dentry *result;
1377
1378 BUG_ON(!list_empty(&entry->d_alias));
1379
1380 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001381 spin_lock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04001382 result = __d_instantiate_unique(entry, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001383 spin_unlock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04001384 spin_unlock(&dcache_lock);
1385
1386 if (!result) {
1387 security_d_instantiate(entry, inode);
1388 return NULL;
1389 }
1390
1391 BUG_ON(!d_unhashed(result));
1392 iput(inode);
1393 return result;
1394}
1395
Linus Torvalds1da177e2005-04-16 15:20:36 -07001396EXPORT_SYMBOL(d_instantiate_unique);
1397
1398/**
1399 * d_alloc_root - allocate root dentry
1400 * @root_inode: inode to allocate the root for
1401 *
1402 * Allocate a root ("/") dentry for the inode given. The inode is
1403 * instantiated and returned. %NULL is returned if there is insufficient
1404 * memory or the inode passed is %NULL.
1405 */
1406
1407struct dentry * d_alloc_root(struct inode * root_inode)
1408{
1409 struct dentry *res = NULL;
1410
1411 if (root_inode) {
1412 static const struct qstr name = { .name = "/", .len = 1 };
1413
1414 res = d_alloc(NULL, &name);
1415 if (res) {
1416 res->d_sb = root_inode->i_sb;
1417 res->d_parent = res;
1418 d_instantiate(res, root_inode);
1419 }
1420 }
1421 return res;
1422}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001423EXPORT_SYMBOL(d_alloc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001424
1425static inline struct hlist_head *d_hash(struct dentry *parent,
1426 unsigned long hash)
1427{
1428 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1429 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1430 return dentry_hashtable + (hash & D_HASHMASK);
1431}
1432
1433/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001434 * d_obtain_alias - find or allocate a dentry for a given inode
1435 * @inode: inode to allocate the dentry for
1436 *
1437 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1438 * similar open by handle operations. The returned dentry may be anonymous,
1439 * or may have a full name (if the inode was already in the cache).
1440 *
1441 * When called on a directory inode, we must ensure that the inode only ever
1442 * has one dentry. If a dentry is found, that is returned instead of
1443 * allocating a new one.
1444 *
1445 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001446 * to the dentry. In case of an error the reference on the inode is released.
1447 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1448 * be passed in and will be the error will be propagate to the return value,
1449 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001450 */
1451struct dentry *d_obtain_alias(struct inode *inode)
1452{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001453 static const struct qstr anonstring = { .name = "" };
1454 struct dentry *tmp;
1455 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001456
1457 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001458 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001459 if (IS_ERR(inode))
1460 return ERR_CAST(inode);
1461
Christoph Hellwig9308a612008-08-11 15:49:12 +02001462 res = d_find_alias(inode);
1463 if (res)
1464 goto out_iput;
1465
1466 tmp = d_alloc(NULL, &anonstring);
1467 if (!tmp) {
1468 res = ERR_PTR(-ENOMEM);
1469 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001470 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001471 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1472
1473 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001474 spin_lock(&dcache_inode_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001475 res = __d_find_alias(inode, 0);
1476 if (res) {
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001477 spin_unlock(&dcache_inode_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001478 spin_unlock(&dcache_lock);
1479 dput(tmp);
1480 goto out_iput;
1481 }
1482
1483 /* attach a disconnected dentry */
1484 spin_lock(&tmp->d_lock);
1485 tmp->d_sb = inode->i_sb;
1486 tmp->d_inode = inode;
1487 tmp->d_flags |= DCACHE_DISCONNECTED;
1488 tmp->d_flags &= ~DCACHE_UNHASHED;
1489 list_add(&tmp->d_alias, &inode->i_dentry);
Nick Piggin789680d2011-01-07 17:49:30 +11001490 spin_lock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001491 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
Nick Piggin789680d2011-01-07 17:49:30 +11001492 spin_unlock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001493 spin_unlock(&tmp->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001494 spin_unlock(&dcache_inode_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001495
1496 spin_unlock(&dcache_lock);
1497 return tmp;
1498
1499 out_iput:
1500 iput(inode);
1501 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001502}
Benny Halevyadc48722009-02-27 14:02:59 -08001503EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001504
1505/**
1506 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1507 * @inode: the inode which may have a disconnected dentry
1508 * @dentry: a negative dentry which we want to point to the inode.
1509 *
1510 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1511 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1512 * and return it, else simply d_add the inode to the dentry and return NULL.
1513 *
1514 * This is needed in the lookup routine of any filesystem that is exportable
1515 * (via knfsd) so that we can build dcache paths to directories effectively.
1516 *
1517 * If a dentry was found and moved, then it is returned. Otherwise NULL
1518 * is returned. This matches the expected return value of ->lookup.
1519 *
1520 */
1521struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1522{
1523 struct dentry *new = NULL;
1524
NeilBrown21c0d8f2006-10-04 02:16:16 -07001525 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001526 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001527 spin_lock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001528 new = __d_find_alias(inode, 1);
1529 if (new) {
1530 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001531 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001532 spin_unlock(&dcache_lock);
1533 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001534 d_move(new, dentry);
1535 iput(inode);
1536 } else {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001537 /* already taking dcache_lock, so d_add() by hand */
1538 __d_instantiate(dentry, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001539 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001540 spin_unlock(&dcache_lock);
1541 security_d_instantiate(dentry, inode);
1542 d_rehash(dentry);
1543 }
1544 } else
1545 d_add(dentry, inode);
1546 return new;
1547}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001548EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
Barry Naujok94035402008-05-21 16:50:46 +10001550/**
1551 * d_add_ci - lookup or allocate new dentry with case-exact name
1552 * @inode: the inode case-insensitive lookup has found
1553 * @dentry: the negative dentry that was passed to the parent's lookup func
1554 * @name: the case-exact name to be associated with the returned dentry
1555 *
1556 * This is to avoid filling the dcache with case-insensitive names to the
1557 * same inode, only the actual correct case is stored in the dcache for
1558 * case-insensitive filesystems.
1559 *
1560 * For a case-insensitive lookup match and if the the case-exact dentry
1561 * already exists in in the dcache, use it and return it.
1562 *
1563 * If no entry exists with the exact case name, allocate new dentry with
1564 * the exact case, and return the spliced entry.
1565 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001566struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001567 struct qstr *name)
1568{
1569 int error;
1570 struct dentry *found;
1571 struct dentry *new;
1572
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001573 /*
1574 * First check if a dentry matching the name already exists,
1575 * if not go ahead and create it now.
1576 */
Barry Naujok94035402008-05-21 16:50:46 +10001577 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001578 if (!found) {
1579 new = d_alloc(dentry->d_parent, name);
1580 if (!new) {
1581 error = -ENOMEM;
1582 goto err_out;
1583 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001584
Barry Naujok94035402008-05-21 16:50:46 +10001585 found = d_splice_alias(inode, new);
1586 if (found) {
1587 dput(new);
1588 return found;
1589 }
1590 return new;
1591 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001592
1593 /*
1594 * If a matching dentry exists, and it's not negative use it.
1595 *
1596 * Decrement the reference count to balance the iget() done
1597 * earlier on.
1598 */
Barry Naujok94035402008-05-21 16:50:46 +10001599 if (found->d_inode) {
1600 if (unlikely(found->d_inode != inode)) {
1601 /* This can't happen because bad inodes are unhashed. */
1602 BUG_ON(!is_bad_inode(inode));
1603 BUG_ON(!is_bad_inode(found->d_inode));
1604 }
Barry Naujok94035402008-05-21 16:50:46 +10001605 iput(inode);
1606 return found;
1607 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001608
Barry Naujok94035402008-05-21 16:50:46 +10001609 /*
1610 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001611 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001612 */
Barry Naujok94035402008-05-21 16:50:46 +10001613 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001614 spin_lock(&dcache_inode_lock);
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001615 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001616 __d_instantiate(found, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001617 spin_unlock(&dcache_inode_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001618 spin_unlock(&dcache_lock);
1619 security_d_instantiate(found, inode);
1620 return found;
1621 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001622
Barry Naujok94035402008-05-21 16:50:46 +10001623 /*
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001624 * In case a directory already has a (disconnected) entry grab a
1625 * reference to it, move it in place and use it.
Barry Naujok94035402008-05-21 16:50:46 +10001626 */
1627 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1628 dget_locked(new);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001629 spin_unlock(&dcache_inode_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001630 spin_unlock(&dcache_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001631 security_d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001632 d_move(new, found);
Barry Naujok94035402008-05-21 16:50:46 +10001633 iput(inode);
Barry Naujok94035402008-05-21 16:50:46 +10001634 dput(found);
Barry Naujok94035402008-05-21 16:50:46 +10001635 return new;
1636
1637err_out:
1638 iput(inode);
1639 return ERR_PTR(error);
1640}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001641EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001642
1643/**
1644 * d_lookup - search for a dentry
1645 * @parent: parent dentry
1646 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001647 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001648 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001649 * d_lookup searches the children of the parent dentry for the name in
1650 * question. If the dentry is found its reference count is incremented and the
1651 * dentry is returned. The caller must use dput to free the entry when it has
1652 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001653 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001654struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1655{
1656 struct dentry * dentry = NULL;
1657 unsigned long seq;
1658
1659 do {
1660 seq = read_seqbegin(&rename_lock);
1661 dentry = __d_lookup(parent, name);
1662 if (dentry)
1663 break;
1664 } while (read_seqretry(&rename_lock, seq));
1665 return dentry;
1666}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001667EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001668
Nick Pigginb04f7842010-08-18 04:37:34 +10001669/*
1670 * __d_lookup - search for a dentry (racy)
1671 * @parent: parent dentry
1672 * @name: qstr of name we wish to find
1673 * Returns: dentry, or NULL
1674 *
1675 * __d_lookup is like d_lookup, however it may (rarely) return a
1676 * false-negative result due to unrelated rename activity.
1677 *
1678 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1679 * however it must be used carefully, eg. with a following d_lookup in
1680 * the case of failure.
1681 *
1682 * __d_lookup callers must be commented.
1683 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001684struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1685{
1686 unsigned int len = name->len;
1687 unsigned int hash = name->hash;
1688 const unsigned char *str = name->name;
1689 struct hlist_head *head = d_hash(parent,hash);
1690 struct dentry *found = NULL;
1691 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001692 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693
Nick Pigginb04f7842010-08-18 04:37:34 +10001694 /*
1695 * The hash list is protected using RCU.
1696 *
1697 * Take d_lock when comparing a candidate dentry, to avoid races
1698 * with d_move().
1699 *
1700 * It is possible that concurrent renames can mess up our list
1701 * walk here and result in missing our dentry, resulting in the
1702 * false-negative result. d_lookup() protects against concurrent
1703 * renames using rename_lock seqlock.
1704 *
1705 * See Documentation/vfs/dcache-locking.txt for more details.
1706 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001707 rcu_read_lock();
1708
Paul E. McKenney665a7582005-11-07 00:59:17 -08001709 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001710 struct qstr *qstr;
1711
Linus Torvalds1da177e2005-04-16 15:20:36 -07001712 if (dentry->d_name.hash != hash)
1713 continue;
1714 if (dentry->d_parent != parent)
1715 continue;
1716
1717 spin_lock(&dentry->d_lock);
1718
1719 /*
1720 * Recheck the dentry after taking the lock - d_move may have
Nick Pigginb04f7842010-08-18 04:37:34 +10001721 * changed things. Don't bother checking the hash because
1722 * we're about to compare the whole name anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001723 */
1724 if (dentry->d_parent != parent)
1725 goto next;
1726
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001727 /* non-existing due to RCU? */
1728 if (d_unhashed(dentry))
1729 goto next;
1730
Linus Torvalds1da177e2005-04-16 15:20:36 -07001731 /*
1732 * It is safe to compare names since d_move() cannot
1733 * change the qstr (protected by d_lock).
1734 */
1735 qstr = &dentry->d_name;
1736 if (parent->d_op && parent->d_op->d_compare) {
Nick Piggin621e1552011-01-07 17:49:27 +11001737 if (parent->d_op->d_compare(parent, parent->d_inode,
1738 dentry, dentry->d_inode,
1739 qstr->len, qstr->name, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001740 goto next;
1741 } else {
1742 if (qstr->len != len)
1743 goto next;
1744 if (memcmp(qstr->name, str, len))
1745 goto next;
1746 }
1747
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001748 dentry->d_count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001749 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001750 spin_unlock(&dentry->d_lock);
1751 break;
1752next:
1753 spin_unlock(&dentry->d_lock);
1754 }
1755 rcu_read_unlock();
1756
1757 return found;
1758}
1759
1760/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001761 * d_hash_and_lookup - hash the qstr then search for a dentry
1762 * @dir: Directory to search in
1763 * @name: qstr of name we wish to find
1764 *
1765 * On hash failure or on lookup failure NULL is returned.
1766 */
1767struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1768{
1769 struct dentry *dentry = NULL;
1770
1771 /*
1772 * Check for a fs-specific hash function. Note that we must
1773 * calculate the standard hash first, as the d_op->d_hash()
1774 * routine may choose to leave the hash value unchanged.
1775 */
1776 name->hash = full_name_hash(name->name, name->len);
1777 if (dir->d_op && dir->d_op->d_hash) {
Nick Pigginb1e6a012011-01-07 17:49:28 +11001778 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001779 goto out;
1780 }
1781 dentry = d_lookup(dir, name);
1782out:
1783 return dentry;
1784}
1785
1786/**
Nick Piggin786a5e12011-01-07 17:49:16 +11001787 * d_validate - verify dentry provided from insecure source (deprecated)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001788 * @dentry: The dentry alleged to be valid child of @dparent
1789 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001790 *
1791 * An insecure source has sent us a dentry, here we verify it and dget() it.
1792 * This is used by ncpfs in its readdir implementation.
1793 * Zero is returned in the dentry is invalid.
Nick Piggin786a5e12011-01-07 17:49:16 +11001794 *
1795 * This function is slow for big directories, and deprecated, do not use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001796 */
Nick Piggind3a23e12011-01-05 20:01:21 +11001797int d_validate(struct dentry *dentry, struct dentry *dparent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001798{
Nick Piggin786a5e12011-01-07 17:49:16 +11001799 struct dentry *child;
Nick Piggind3a23e12011-01-05 20:01:21 +11001800
1801 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001802 spin_lock(&dparent->d_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001803 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1804 if (dentry == child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001805 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1806 __dget_locked_dlock(dentry);
1807 spin_unlock(&dentry->d_lock);
1808 spin_unlock(&dparent->d_lock);
Nick Piggind3a23e12011-01-05 20:01:21 +11001809 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001810 return 1;
1811 }
1812 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001813 spin_unlock(&dparent->d_lock);
Nick Piggind3a23e12011-01-05 20:01:21 +11001814 spin_unlock(&dcache_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001815
Linus Torvalds1da177e2005-04-16 15:20:36 -07001816 return 0;
1817}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001818EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001819
1820/*
1821 * When a file is deleted, we have two options:
1822 * - turn this dentry into a negative dentry
1823 * - unhash this dentry and free it.
1824 *
1825 * Usually, we want to just turn this into
1826 * a negative dentry, but if anybody else is
1827 * currently using the dentry or the inode
1828 * we can't do that and we fall back on removing
1829 * it from the hash queues and waiting for
1830 * it to be deleted later when it has no users
1831 */
1832
1833/**
1834 * d_delete - delete a dentry
1835 * @dentry: The dentry to delete
1836 *
1837 * Turn the dentry into a negative dentry if possible, otherwise
1838 * remove it from the hash queues so it can be deleted later
1839 */
1840
1841void d_delete(struct dentry * dentry)
1842{
John McCutchan7a91bf72005-08-08 13:52:16 -04001843 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001844 /*
1845 * Are we the only user?
1846 */
1847 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001848 spin_lock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001849 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001850 isdir = S_ISDIR(dentry->d_inode->i_mode);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001851 if (dentry->d_count == 1) {
Al Viro13e3c5e2010-05-21 16:11:04 -04001852 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001853 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001854 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return;
1856 }
1857
1858 if (!d_unhashed(dentry))
1859 __d_drop(dentry);
1860
1861 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001862 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001863 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001864
1865 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001866}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001867EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001868
1869static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1870{
1871
1872 entry->d_flags &= ~DCACHE_UNHASHED;
1873 hlist_add_head_rcu(&entry->d_hash, list);
1874}
1875
David Howells770bfad2006-08-22 20:06:07 -04001876static void _d_rehash(struct dentry * entry)
1877{
1878 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1879}
1880
Linus Torvalds1da177e2005-04-16 15:20:36 -07001881/**
1882 * d_rehash - add an entry back to the hash
1883 * @entry: dentry to add to the hash
1884 *
1885 * Adds a dentry to the hash according to its name.
1886 */
1887
1888void d_rehash(struct dentry * entry)
1889{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 spin_lock(&dcache_lock);
1891 spin_lock(&entry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +11001892 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04001893 _d_rehash(entry);
Nick Piggin789680d2011-01-07 17:49:30 +11001894 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 spin_unlock(&entry->d_lock);
1896 spin_unlock(&dcache_lock);
1897}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001898EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001899
Nick Pigginfb2d5b82011-01-07 17:49:26 +11001900/**
1901 * dentry_update_name_case - update case insensitive dentry with a new name
1902 * @dentry: dentry to be updated
1903 * @name: new name
1904 *
1905 * Update a case insensitive dentry with new case of name.
1906 *
1907 * dentry must have been returned by d_lookup with name @name. Old and new
1908 * name lengths must match (ie. no d_compare which allows mismatched name
1909 * lengths).
1910 *
1911 * Parent inode i_mutex must be held over d_lookup and into this call (to
1912 * keep renames and concurrent inserts, and readdir(2) away).
1913 */
1914void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
1915{
1916 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
1917 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
1918
1919 spin_lock(&dcache_lock);
1920 spin_lock(&dentry->d_lock);
1921 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
1922 spin_unlock(&dentry->d_lock);
1923 spin_unlock(&dcache_lock);
1924}
1925EXPORT_SYMBOL(dentry_update_name_case);
1926
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927static void switch_names(struct dentry *dentry, struct dentry *target)
1928{
1929 if (dname_external(target)) {
1930 if (dname_external(dentry)) {
1931 /*
1932 * Both external: swap the pointers
1933 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001934 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 } else {
1936 /*
1937 * dentry:internal, target:external. Steal target's
1938 * storage and make target internal.
1939 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07001940 memcpy(target->d_iname, dentry->d_name.name,
1941 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001942 dentry->d_name.name = target->d_name.name;
1943 target->d_name.name = target->d_iname;
1944 }
1945 } else {
1946 if (dname_external(dentry)) {
1947 /*
1948 * dentry:external, target:internal. Give dentry's
1949 * storage to target and make dentry internal
1950 */
1951 memcpy(dentry->d_iname, target->d_name.name,
1952 target->d_name.len + 1);
1953 target->d_name.name = dentry->d_name.name;
1954 dentry->d_name.name = dentry->d_iname;
1955 } else {
1956 /*
1957 * Both are internal. Just copy target to dentry
1958 */
1959 memcpy(dentry->d_iname, target->d_name.name,
1960 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05001961 dentry->d_name.len = target->d_name.len;
1962 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001963 }
1964 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001965 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001966}
1967
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001968static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
1969{
1970 /*
1971 * XXXX: do we really need to take target->d_lock?
1972 */
1973 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
1974 spin_lock(&target->d_parent->d_lock);
1975 else {
1976 if (d_ancestor(dentry->d_parent, target->d_parent)) {
1977 spin_lock(&dentry->d_parent->d_lock);
1978 spin_lock_nested(&target->d_parent->d_lock,
1979 DENTRY_D_LOCK_NESTED);
1980 } else {
1981 spin_lock(&target->d_parent->d_lock);
1982 spin_lock_nested(&dentry->d_parent->d_lock,
1983 DENTRY_D_LOCK_NESTED);
1984 }
1985 }
1986 if (target < dentry) {
1987 spin_lock_nested(&target->d_lock, 2);
1988 spin_lock_nested(&dentry->d_lock, 3);
1989 } else {
1990 spin_lock_nested(&dentry->d_lock, 2);
1991 spin_lock_nested(&target->d_lock, 3);
1992 }
1993}
1994
1995static void dentry_unlock_parents_for_move(struct dentry *dentry,
1996 struct dentry *target)
1997{
1998 if (target->d_parent != dentry->d_parent)
1999 spin_unlock(&dentry->d_parent->d_lock);
2000 if (target->d_parent != target)
2001 spin_unlock(&target->d_parent->d_lock);
2002}
2003
Linus Torvalds1da177e2005-04-16 15:20:36 -07002004/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002005 * When switching names, the actual string doesn't strictly have to
2006 * be preserved in the target - because we're dropping the target
2007 * anyway. As such, we can just do a simple memcpy() to copy over
2008 * the new name before we switch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002009 *
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002010 * Note that we have to be a lot more careful about getting the hash
2011 * switched - we have to switch the hash value properly even if it
2012 * then no longer matches the actual (corrupted) string of the target.
2013 * The hash value has to match the hash queue that the dentry is on..
Linus Torvalds1da177e2005-04-16 15:20:36 -07002014 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002015/*
2016 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07002017 * @dentry: entry to move
2018 * @target: new dentry
2019 *
2020 * Update the dcache to reflect the move of a file name. Negative
2021 * dcache entries should not be moved in this way.
2022 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002023static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002024{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002025 if (!dentry->d_inode)
2026 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2027
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002028 BUG_ON(d_ancestor(dentry, target));
2029 BUG_ON(d_ancestor(target, dentry));
2030
Linus Torvalds1da177e2005-04-16 15:20:36 -07002031 write_seqlock(&rename_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002032
2033 dentry_lock_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002034
2035 /* Move the dentry to the target hash queue, if on different bucket */
Nick Piggin789680d2011-01-07 17:49:30 +11002036 spin_lock(&dcache_hash_lock);
2037 if (!d_unhashed(dentry))
2038 hlist_del_rcu(&dentry->d_hash);
2039 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
2040 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002041
2042 /* Unhash the target: dput() will then get rid of it */
2043 __d_drop(target);
2044
Eric Dumazet5160ee62006-01-08 01:03:32 -08002045 list_del(&dentry->d_u.d_child);
2046 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002047
2048 /* Switch the names.. */
2049 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002050 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002051
2052 /* ... and switch the parents */
2053 if (IS_ROOT(dentry)) {
2054 dentry->d_parent = target->d_parent;
2055 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002056 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002057 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002058 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059
2060 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08002061 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 }
2063
Eric Dumazet5160ee62006-01-08 01:03:32 -08002064 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002065
2066 dentry_unlock_parents_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002067 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08002068 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069 spin_unlock(&dentry->d_lock);
2070 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002071}
2072
2073/**
2074 * d_move - move a dentry
2075 * @dentry: entry to move
2076 * @target: new dentry
2077 *
2078 * Update the dcache to reflect the move of a file name. Negative
2079 * dcache entries should not be moved in this way.
2080 */
2081
2082void d_move(struct dentry * dentry, struct dentry * target)
2083{
2084 spin_lock(&dcache_lock);
2085 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086 spin_unlock(&dcache_lock);
2087}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002088EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002089
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002090/**
2091 * d_ancestor - search for an ancestor
2092 * @p1: ancestor dentry
2093 * @p2: child dentry
2094 *
2095 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2096 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07002097 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002098struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002099{
2100 struct dentry *p;
2101
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09002102 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002103 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002104 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002105 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002106 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002107}
2108
2109/*
2110 * This helper attempts to cope with remotely renamed directories
2111 *
2112 * It assumes that the caller is already holding
2113 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
2114 *
2115 * Note: If ever the locking in lock_rename() changes, then please
2116 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07002117 */
2118static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002119 __releases(dcache_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002120 __releases(dcache_inode_lock)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002121{
2122 struct mutex *m1 = NULL, *m2 = NULL;
2123 struct dentry *ret;
2124
2125 /* If alias and dentry share a parent, then no extra locks required */
2126 if (alias->d_parent == dentry->d_parent)
2127 goto out_unalias;
2128
2129 /* Check for loops */
2130 ret = ERR_PTR(-ELOOP);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002131 if (d_ancestor(alias, dentry))
Trond Myklebust9eaef272006-10-21 10:24:20 -07002132 goto out_err;
2133
2134 /* See lock_rename() */
2135 ret = ERR_PTR(-EBUSY);
2136 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2137 goto out_err;
2138 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2139 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2140 goto out_err;
2141 m2 = &alias->d_parent->d_inode->i_mutex;
2142out_unalias:
2143 d_move_locked(alias, dentry);
2144 ret = alias;
2145out_err:
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002146 spin_unlock(&dcache_inode_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002147 spin_unlock(&dcache_lock);
2148 if (m2)
2149 mutex_unlock(m2);
2150 if (m1)
2151 mutex_unlock(m1);
2152 return ret;
2153}
2154
2155/*
David Howells770bfad2006-08-22 20:06:07 -04002156 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2157 * named dentry in place of the dentry to be replaced.
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002158 * returns with anon->d_lock held!
David Howells770bfad2006-08-22 20:06:07 -04002159 */
2160static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2161{
2162 struct dentry *dparent, *aparent;
2163
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002164 dentry_lock_for_move(anon, dentry);
David Howells770bfad2006-08-22 20:06:07 -04002165
2166 dparent = dentry->d_parent;
2167 aparent = anon->d_parent;
2168
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002169 switch_names(dentry, anon);
2170 swap(dentry->d_name.hash, anon->d_name.hash);
2171
David Howells770bfad2006-08-22 20:06:07 -04002172 dentry->d_parent = (aparent == anon) ? dentry : aparent;
2173 list_del(&dentry->d_u.d_child);
2174 if (!IS_ROOT(dentry))
2175 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2176 else
2177 INIT_LIST_HEAD(&dentry->d_u.d_child);
2178
2179 anon->d_parent = (dparent == dentry) ? anon : dparent;
2180 list_del(&anon->d_u.d_child);
2181 if (!IS_ROOT(anon))
2182 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
2183 else
2184 INIT_LIST_HEAD(&anon->d_u.d_child);
2185
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002186 dentry_unlock_parents_for_move(anon, dentry);
2187 spin_unlock(&dentry->d_lock);
2188
2189 /* anon->d_lock still locked, returns locked */
David Howells770bfad2006-08-22 20:06:07 -04002190 anon->d_flags &= ~DCACHE_DISCONNECTED;
2191}
2192
2193/**
2194 * d_materialise_unique - introduce an inode into the tree
2195 * @dentry: candidate dentry
2196 * @inode: inode to bind to the dentry, to which aliases may be attached
2197 *
2198 * Introduces an dentry into the tree, substituting an extant disconnected
2199 * root directory alias in its place if there is one
2200 */
2201struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2202{
Trond Myklebust9eaef272006-10-21 10:24:20 -07002203 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04002204
2205 BUG_ON(!d_unhashed(dentry));
2206
2207 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002208 spin_lock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04002209
2210 if (!inode) {
2211 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09002212 __d_instantiate(dentry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04002213 goto found_lock;
2214 }
2215
Trond Myklebust9eaef272006-10-21 10:24:20 -07002216 if (S_ISDIR(inode->i_mode)) {
2217 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04002218
Trond Myklebust9eaef272006-10-21 10:24:20 -07002219 /* Does an aliased dentry already exist? */
2220 alias = __d_find_alias(inode, 0);
2221 if (alias) {
2222 actual = alias;
2223 /* Is this an anonymous mountpoint that we could splice
2224 * into our tree? */
2225 if (IS_ROOT(alias)) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002226 __d_materialise_dentry(dentry, alias);
2227 __d_drop(alias);
2228 goto found;
2229 }
2230 /* Nope, but we must(!) avoid directory aliasing */
2231 actual = __d_unalias(dentry, alias);
2232 if (IS_ERR(actual))
2233 dput(alias);
2234 goto out_nolock;
2235 }
David Howells770bfad2006-08-22 20:06:07 -04002236 }
2237
2238 /* Add a unique reference */
2239 actual = __d_instantiate_unique(dentry, inode);
2240 if (!actual)
2241 actual = dentry;
2242 else if (unlikely(!d_unhashed(actual)))
2243 goto shouldnt_be_hashed;
2244
2245found_lock:
2246 spin_lock(&actual->d_lock);
2247found:
Nick Piggin789680d2011-01-07 17:49:30 +11002248 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04002249 _d_rehash(actual);
Nick Piggin789680d2011-01-07 17:49:30 +11002250 spin_unlock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04002251 spin_unlock(&actual->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002252 spin_unlock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04002253 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002254out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04002255 if (actual == dentry) {
2256 security_d_instantiate(dentry, inode);
2257 return NULL;
2258 }
2259
2260 iput(inode);
2261 return actual;
2262
David Howells770bfad2006-08-22 20:06:07 -04002263shouldnt_be_hashed:
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002264 spin_unlock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04002265 spin_unlock(&dcache_lock);
2266 BUG();
David Howells770bfad2006-08-22 20:06:07 -04002267}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002268EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04002269
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002270static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01002271{
2272 *buflen -= namelen;
2273 if (*buflen < 0)
2274 return -ENAMETOOLONG;
2275 *buffer -= namelen;
2276 memcpy(*buffer, str, namelen);
2277 return 0;
2278}
2279
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002280static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2281{
2282 return prepend(buffer, buflen, name->name, name->len);
2283}
2284
Linus Torvalds1da177e2005-04-16 15:20:36 -07002285/**
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002286 * Prepend path string to a buffer
2287 *
2288 * @path: the dentry/vfsmount to report
2289 * @root: root vfsmnt/dentry (may be modified by this function)
2290 * @buffer: pointer to the end of the buffer
2291 * @buflen: pointer to buffer length
2292 *
2293 * Caller holds the dcache_lock.
2294 *
2295 * If path is not reachable from the supplied root, then the value of
2296 * root is changed (without modifying refcounts).
2297 */
2298static int prepend_path(const struct path *path, struct path *root,
2299 char **buffer, int *buflen)
2300{
2301 struct dentry *dentry = path->dentry;
2302 struct vfsmount *vfsmnt = path->mnt;
2303 bool slash = false;
2304 int error = 0;
2305
Nick Piggin99b7db72010-08-18 04:37:39 +10002306 br_read_lock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002307 while (dentry != root->dentry || vfsmnt != root->mnt) {
2308 struct dentry * parent;
2309
2310 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2311 /* Global root? */
2312 if (vfsmnt->mnt_parent == vfsmnt) {
2313 goto global_root;
2314 }
2315 dentry = vfsmnt->mnt_mountpoint;
2316 vfsmnt = vfsmnt->mnt_parent;
2317 continue;
2318 }
2319 parent = dentry->d_parent;
2320 prefetch(parent);
Nick Piggin9abca362011-01-07 17:49:36 +11002321 spin_lock(&dentry->d_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002322 error = prepend_name(buffer, buflen, &dentry->d_name);
Nick Piggin9abca362011-01-07 17:49:36 +11002323 spin_unlock(&dentry->d_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002324 if (!error)
2325 error = prepend(buffer, buflen, "/", 1);
2326 if (error)
2327 break;
2328
2329 slash = true;
2330 dentry = parent;
2331 }
2332
2333out:
2334 if (!error && !slash)
2335 error = prepend(buffer, buflen, "/", 1);
2336
Nick Piggin99b7db72010-08-18 04:37:39 +10002337 br_read_unlock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002338 return error;
2339
2340global_root:
2341 /*
2342 * Filesystems needing to implement special "root names"
2343 * should do so with ->d_dname()
2344 */
2345 if (IS_ROOT(dentry) &&
2346 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2347 WARN(1, "Root dentry has weird name <%.*s>\n",
2348 (int) dentry->d_name.len, dentry->d_name.name);
2349 }
2350 root->mnt = vfsmnt;
2351 root->dentry = dentry;
2352 goto out;
2353}
2354
2355/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002356 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002357 * @path: the dentry/vfsmount to report
2358 * @root: root vfsmnt/dentry (may be modified by this function)
Randy Dunlapcd956a12010-08-14 13:05:31 -07002359 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002360 * @buflen: buffer length
2361 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002362 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002363 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002364 * Returns a pointer into the buffer or an error code if the
2365 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002366 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002367 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002368 *
2369 * If path is not reachable from the supplied root, then the value of
2370 * root is changed (without modifying refcounts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002371 */
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002372char *__d_path(const struct path *path, struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002373 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002374{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002375 char *res = buf + buflen;
2376 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002377
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002378 prepend(&res, &buflen, "\0", 1);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002379 spin_lock(&dcache_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002380 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002381 spin_unlock(&dcache_lock);
2382
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002383 if (error)
2384 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002385 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002386}
2387
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002388/*
2389 * same as __d_path but appends "(deleted)" for unlinked files.
2390 */
2391static int path_with_deleted(const struct path *path, struct path *root,
2392 char **buf, int *buflen)
2393{
2394 prepend(buf, buflen, "\0", 1);
2395 if (d_unlinked(path->dentry)) {
2396 int error = prepend(buf, buflen, " (deleted)", 10);
2397 if (error)
2398 return error;
2399 }
2400
2401 return prepend_path(path, root, buf, buflen);
2402}
2403
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002404static int prepend_unreachable(char **buffer, int *buflen)
2405{
2406 return prepend(buffer, buflen, "(unreachable)", 13);
2407}
2408
Jan Bluncka03a8a702008-02-14 19:38:32 -08002409/**
2410 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002411 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002412 * @buf: buffer to return value in
2413 * @buflen: buffer length
2414 *
2415 * Convert a dentry into an ASCII path name. If the entry has been deleted
2416 * the string " (deleted)" is appended. Note that this is ambiguous.
2417 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002418 * Returns a pointer into the buffer or an error code if the path was
2419 * too long. Note: Callers should use the returned pointer, not the passed
2420 * in buffer, to use the name! The implementation often starts at an offset
2421 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002422 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002423 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002424 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002425char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002426{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002427 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002428 struct path root;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002429 struct path tmp;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002430 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002431
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002432 /*
2433 * We have various synthetic filesystems that never get mounted. On
2434 * these filesystems dentries are never used for lookup purposes, and
2435 * thus don't need to be hashed. They also don't need a name until a
2436 * user wants to identify the object in /proc/pid/fd/. The little hack
2437 * below allows us to generate a name for these objects on demand:
2438 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002439 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2440 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002441
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002442 get_fs_root(current->fs, &root);
Linus Torvalds552ce542007-02-13 12:08:18 -08002443 spin_lock(&dcache_lock);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002444 tmp = root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002445 error = path_with_deleted(path, &tmp, &res, &buflen);
2446 if (error)
2447 res = ERR_PTR(error);
Linus Torvalds552ce542007-02-13 12:08:18 -08002448 spin_unlock(&dcache_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002449 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002450 return res;
2451}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002452EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002453
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002454/**
2455 * d_path_with_unreachable - return the path of a dentry
2456 * @path: path to report
2457 * @buf: buffer to return value in
2458 * @buflen: buffer length
2459 *
2460 * The difference from d_path() is that this prepends "(unreachable)"
2461 * to paths which are unreachable from the current process' root.
2462 */
2463char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2464{
2465 char *res = buf + buflen;
2466 struct path root;
2467 struct path tmp;
2468 int error;
2469
2470 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2471 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2472
2473 get_fs_root(current->fs, &root);
2474 spin_lock(&dcache_lock);
2475 tmp = root;
2476 error = path_with_deleted(path, &tmp, &res, &buflen);
2477 if (!error && !path_equal(&tmp, &root))
2478 error = prepend_unreachable(&res, &buflen);
2479 spin_unlock(&dcache_lock);
2480 path_put(&root);
2481 if (error)
2482 res = ERR_PTR(error);
2483
2484 return res;
2485}
2486
Linus Torvalds1da177e2005-04-16 15:20:36 -07002487/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002488 * Helper function for dentry_operations.d_dname() members
2489 */
2490char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2491 const char *fmt, ...)
2492{
2493 va_list args;
2494 char temp[64];
2495 int sz;
2496
2497 va_start(args, fmt);
2498 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2499 va_end(args);
2500
2501 if (sz > sizeof(temp) || sz > buflen)
2502 return ERR_PTR(-ENAMETOOLONG);
2503
2504 buffer += buflen - sz;
2505 return memcpy(buffer, temp, sz);
2506}
2507
2508/*
Ram Pai6092d042008-03-27 13:06:20 +01002509 * Write full pathname from the root of the filesystem into the buffer.
2510 */
Nick Pigginec2447c2011-01-07 17:49:29 +11002511static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002512{
2513 char *end = buf + buflen;
2514 char *retval;
2515
Ram Pai6092d042008-03-27 13:06:20 +01002516 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002517 if (buflen < 1)
2518 goto Elong;
2519 /* Get '/' right */
2520 retval = end-1;
2521 *retval = '/';
2522
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002523 while (!IS_ROOT(dentry)) {
2524 struct dentry *parent = dentry->d_parent;
Nick Piggin9abca362011-01-07 17:49:36 +11002525 int error;
Ram Pai6092d042008-03-27 13:06:20 +01002526
Ram Pai6092d042008-03-27 13:06:20 +01002527 prefetch(parent);
Nick Piggin9abca362011-01-07 17:49:36 +11002528 spin_lock(&dentry->d_lock);
2529 error = prepend_name(&end, &buflen, &dentry->d_name);
2530 spin_unlock(&dentry->d_lock);
2531 if (error != 0 || prepend(&end, &buflen, "/", 1) != 0)
Ram Pai6092d042008-03-27 13:06:20 +01002532 goto Elong;
2533
2534 retval = end;
2535 dentry = parent;
2536 }
Al Viroc1031352010-06-06 22:31:14 -04002537 return retval;
2538Elong:
2539 return ERR_PTR(-ENAMETOOLONG);
2540}
Nick Pigginec2447c2011-01-07 17:49:29 +11002541
2542char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2543{
2544 char *retval;
2545
2546 spin_lock(&dcache_lock);
2547 retval = __dentry_path(dentry, buf, buflen);
2548 spin_unlock(&dcache_lock);
2549
2550 return retval;
2551}
2552EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04002553
2554char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2555{
2556 char *p = NULL;
2557 char *retval;
2558
2559 spin_lock(&dcache_lock);
2560 if (d_unlinked(dentry)) {
2561 p = buf + buflen;
2562 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2563 goto Elong;
2564 buflen++;
2565 }
2566 retval = __dentry_path(dentry, buf, buflen);
Ram Pai6092d042008-03-27 13:06:20 +01002567 spin_unlock(&dcache_lock);
Al Viroc1031352010-06-06 22:31:14 -04002568 if (!IS_ERR(retval) && p)
2569 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002570 return retval;
2571Elong:
2572 spin_unlock(&dcache_lock);
2573 return ERR_PTR(-ENAMETOOLONG);
2574}
2575
2576/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002577 * NOTE! The user-level library version returns a
2578 * character pointer. The kernel system call just
2579 * returns the length of the buffer filled (which
2580 * includes the ending '\0' character), or a negative
2581 * error value. So libc would do something like
2582 *
2583 * char *getcwd(char * buf, size_t size)
2584 * {
2585 * int retval;
2586 *
2587 * retval = sys_getcwd(buf, size);
2588 * if (retval >= 0)
2589 * return buf;
2590 * errno = -retval;
2591 * return NULL;
2592 * }
2593 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002594SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002595{
Linus Torvalds552ce542007-02-13 12:08:18 -08002596 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002597 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002598 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002599
2600 if (!page)
2601 return -ENOMEM;
2602
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002603 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002604
Linus Torvalds552ce542007-02-13 12:08:18 -08002605 error = -ENOENT;
Linus Torvalds552ce542007-02-13 12:08:18 -08002606 spin_lock(&dcache_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002607 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002608 unsigned long len;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002609 struct path tmp = root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002610 char *cwd = page + PAGE_SIZE;
2611 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002612
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002613 prepend(&cwd, &buflen, "\0", 1);
2614 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
Linus Torvalds552ce542007-02-13 12:08:18 -08002615 spin_unlock(&dcache_lock);
2616
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002617 if (error)
Linus Torvalds552ce542007-02-13 12:08:18 -08002618 goto out;
2619
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002620 /* Unreachable from current root */
2621 if (!path_equal(&tmp, &root)) {
2622 error = prepend_unreachable(&cwd, &buflen);
2623 if (error)
2624 goto out;
2625 }
2626
Linus Torvalds552ce542007-02-13 12:08:18 -08002627 error = -ERANGE;
2628 len = PAGE_SIZE + page - cwd;
2629 if (len <= size) {
2630 error = len;
2631 if (copy_to_user(buf, cwd, len))
2632 error = -EFAULT;
2633 }
2634 } else
2635 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002636
2637out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002638 path_put(&pwd);
2639 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002640 free_page((unsigned long) page);
2641 return error;
2642}
2643
2644/*
2645 * Test whether new_dentry is a subdirectory of old_dentry.
2646 *
2647 * Trivially implemented using the dcache structure
2648 */
2649
2650/**
2651 * is_subdir - is new dentry a subdirectory of old_dentry
2652 * @new_dentry: new dentry
2653 * @old_dentry: old dentry
2654 *
2655 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2656 * Returns 0 otherwise.
2657 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2658 */
2659
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002660int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002661{
2662 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002663 unsigned long seq;
2664
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002665 if (new_dentry == old_dentry)
2666 return 1;
2667
2668 /*
2669 * Need rcu_readlock to protect against the d_parent trashing
2670 * due to d_move
Linus Torvalds1da177e2005-04-16 15:20:36 -07002671 */
2672 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002673 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002674 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002675 seq = read_seqbegin(&rename_lock);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002676 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002677 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002678 else
2679 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002680 } while (read_seqretry(&rename_lock, seq));
2681 rcu_read_unlock();
2682
2683 return result;
2684}
2685
Al Viro2096f752010-01-30 13:16:21 -05002686int path_is_under(struct path *path1, struct path *path2)
2687{
2688 struct vfsmount *mnt = path1->mnt;
2689 struct dentry *dentry = path1->dentry;
2690 int res;
Nick Piggin99b7db72010-08-18 04:37:39 +10002691
2692 br_read_lock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002693 if (mnt != path2->mnt) {
2694 for (;;) {
2695 if (mnt->mnt_parent == mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +10002696 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002697 return 0;
2698 }
2699 if (mnt->mnt_parent == path2->mnt)
2700 break;
2701 mnt = mnt->mnt_parent;
2702 }
2703 dentry = mnt->mnt_mountpoint;
2704 }
2705 res = is_subdir(dentry, path2->dentry);
Nick Piggin99b7db72010-08-18 04:37:39 +10002706 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002707 return res;
2708}
2709EXPORT_SYMBOL(path_is_under);
2710
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711void d_genocide(struct dentry *root)
2712{
2713 struct dentry *this_parent = root;
2714 struct list_head *next;
2715
2716 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002717 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002718repeat:
2719 next = this_parent->d_subdirs.next;
2720resume:
2721 while (next != &this_parent->d_subdirs) {
2722 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002723 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002724 next = tmp->next;
Nick Pigginda502952011-01-07 17:49:33 +11002725 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2726 if (d_unhashed(dentry) || !dentry->d_inode) {
2727 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 continue;
Nick Pigginda502952011-01-07 17:49:33 +11002729 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002730 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002731 spin_unlock(&this_parent->d_lock);
2732 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002733 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002734 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002735 goto repeat;
2736 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002737 dentry->d_count--;
2738 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002739 }
2740 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08002741 next = this_parent->d_u.d_child.next;
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002742 this_parent->d_count--;
2743 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002744 this_parent = this_parent->d_parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002745 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002746 goto resume;
2747 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002748 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002749 spin_unlock(&dcache_lock);
2750}
2751
2752/**
2753 * find_inode_number - check for dentry with name
2754 * @dir: directory to check
2755 * @name: Name to find.
2756 *
2757 * Check whether a dentry already exists for the given name,
2758 * and return the inode number if it has an inode. Otherwise
2759 * 0 is returned.
2760 *
2761 * This routine is used to post-process directory listings for
2762 * filesystems using synthetic inode numbers, and is necessary
2763 * to keep getcwd() working.
2764 */
2765
2766ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2767{
2768 struct dentry * dentry;
2769 ino_t ino = 0;
2770
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002771 dentry = d_hash_and_lookup(dir, name);
2772 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 if (dentry->d_inode)
2774 ino = dentry->d_inode->i_ino;
2775 dput(dentry);
2776 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002777 return ino;
2778}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002779EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002780
2781static __initdata unsigned long dhash_entries;
2782static int __init set_dhash_entries(char *str)
2783{
2784 if (!str)
2785 return 0;
2786 dhash_entries = simple_strtoul(str, &str, 0);
2787 return 1;
2788}
2789__setup("dhash_entries=", set_dhash_entries);
2790
2791static void __init dcache_init_early(void)
2792{
2793 int loop;
2794
2795 /* If hashes are distributed across NUMA nodes, defer
2796 * hash allocation until vmalloc space is available.
2797 */
2798 if (hashdist)
2799 return;
2800
2801 dentry_hashtable =
2802 alloc_large_system_hash("Dentry cache",
2803 sizeof(struct hlist_head),
2804 dhash_entries,
2805 13,
2806 HASH_EARLY,
2807 &d_hash_shift,
2808 &d_hash_mask,
2809 0);
2810
2811 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2812 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2813}
2814
Denis Cheng74bf17c2007-10-16 23:26:30 -07002815static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002816{
2817 int loop;
2818
2819 /*
2820 * A constructor could be added for stable state like the lists,
2821 * but it is probably not worth it because of the cache nature
2822 * of the dcache.
2823 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002824 dentry_cache = KMEM_CACHE(dentry,
2825 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002826
Rusty Russell8e1f9362007-07-17 04:03:17 -07002827 register_shrinker(&dcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002828
2829 /* Hash may have been set up in dcache_init_early */
2830 if (!hashdist)
2831 return;
2832
2833 dentry_hashtable =
2834 alloc_large_system_hash("Dentry cache",
2835 sizeof(struct hlist_head),
2836 dhash_entries,
2837 13,
2838 0,
2839 &d_hash_shift,
2840 &d_hash_mask,
2841 0);
2842
2843 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2844 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2845}
2846
2847/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002848struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002849EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002850
Linus Torvalds1da177e2005-04-16 15:20:36 -07002851EXPORT_SYMBOL(d_genocide);
2852
Linus Torvalds1da177e2005-04-16 15:20:36 -07002853void __init vfs_caches_init_early(void)
2854{
2855 dcache_init_early();
2856 inode_init_early();
2857}
2858
2859void __init vfs_caches_init(unsigned long mempages)
2860{
2861 unsigned long reserve;
2862
2863 /* Base hash sizes on available memory, with a reserve equal to
2864 150% of current kernel size */
2865
2866 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2867 mempages -= reserve;
2868
2869 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002870 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002871
Denis Cheng74bf17c2007-10-16 23:26:30 -07002872 dcache_init();
2873 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002874 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07002875 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002876 bdev_cache_init();
2877 chrdev_init();
2878}