blob: a9bc4ecc21e161b8ab2b7dad70b515282ab52a3b [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 Piggin949854d2011-01-07 17:49:37 +110083EXPORT_SYMBOL(rename_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +110084EXPORT_SYMBOL(dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085EXPORT_SYMBOL(dcache_lock);
86
Christoph Lametere18b8902006-12-06 20:33:20 -080087static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070088
89#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
90
91/*
92 * This is the single most critical data structure when it comes
93 * to the dcache: the hashtable for lookups. Somebody should try
94 * to make this good - I've just made it work.
95 *
96 * This hash-function tries to avoid losing too many bits of hash
97 * information, yet avoid using a prime hash-size or similar.
98 */
99#define D_HASHBITS d_hash_shift
100#define D_HASHMASK d_hash_mask
101
Eric Dumazetfa3536c2006-03-26 01:37:24 -0800102static unsigned int d_hash_mask __read_mostly;
103static unsigned int d_hash_shift __read_mostly;
104static struct hlist_head *dentry_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700105
106/* Statistics gathering. */
107struct dentry_stat_t dentry_stat = {
108 .age_limit = 45,
109};
110
Nick Piggin3e880fb2011-01-07 17:49:19 +1100111static DEFINE_PER_CPU(unsigned int, nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400112
113#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Nick Piggin3e880fb2011-01-07 17:49:19 +1100114static int get_nr_dentry(void)
115{
116 int i;
117 int sum = 0;
118 for_each_possible_cpu(i)
119 sum += per_cpu(nr_dentry, i);
120 return sum < 0 ? 0 : sum;
121}
122
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400123int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
124 size_t *lenp, loff_t *ppos)
125{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100126 dentry_stat.nr_dentry = get_nr_dentry();
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400127 return proc_dointvec(table, write, buffer, lenp, ppos);
128}
129#endif
130
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400131static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700132{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400133 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
134
Arjan van de Venfd217f42008-10-21 06:47:33 -0700135 WARN_ON(!list_empty(&dentry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 if (dname_external(dentry))
137 kfree(dentry->d_name.name);
138 kmem_cache_free(dentry_cache, dentry);
139}
140
141/*
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400142 * no dcache_lock, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 */
144static void d_free(struct dentry *dentry)
145{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100146 BUG_ON(dentry->d_count);
Nick Piggin3e880fb2011-01-07 17:49:19 +1100147 this_cpu_dec(nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700148 if (dentry->d_op && dentry->d_op->d_release)
149 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400150
Eric Dumazetb3423412006-12-06 20:38:48 -0800151 /* if dentry was never inserted into hash, immediate free is OK */
Akinobu Mitae8462ca2008-02-06 01:37:07 -0800152 if (hlist_unhashed(&dentry->d_hash))
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400153 __d_free(&dentry->d_u.d_rcu);
Eric Dumazetb3423412006-12-06 20:38:48 -0800154 else
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400155 call_rcu(&dentry->d_u.d_rcu, __d_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156}
157
158/*
159 * Release the dentry's inode, using the filesystem
160 * d_iput() operation if defined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800162static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200163 __releases(dentry->d_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100164 __releases(dcache_inode_lock)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200165 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
167 struct inode *inode = dentry->d_inode;
168 if (inode) {
169 dentry->d_inode = NULL;
170 list_del_init(&dentry->d_alias);
171 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100172 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 spin_unlock(&dcache_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700174 if (!inode->i_nlink)
175 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (dentry->d_op && dentry->d_op->d_iput)
177 dentry->d_op->d_iput(dentry, inode);
178 else
179 iput(inode);
180 } else {
181 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100182 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700183 spin_unlock(&dcache_lock);
184 }
185}
186
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700187/*
Nick Piggin23044502011-01-07 17:49:31 +1100188 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700189 */
190static void dentry_lru_add(struct dentry *dentry)
191{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400192 if (list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100193 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400194 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
195 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100196 dentry_stat.nr_unused++;
Nick Piggin23044502011-01-07 17:49:31 +1100197 spin_unlock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400198 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700199}
200
Nick Piggin23044502011-01-07 17:49:31 +1100201static void __dentry_lru_del(struct dentry *dentry)
202{
203 list_del_init(&dentry->d_lru);
204 dentry->d_sb->s_nr_dentry_unused--;
205 dentry_stat.nr_unused--;
206}
207
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700208static void dentry_lru_del(struct dentry *dentry)
209{
210 if (!list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100211 spin_lock(&dcache_lru_lock);
212 __dentry_lru_del(dentry);
213 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700214 }
215}
216
Christoph Hellwiga4633352010-10-10 05:36:26 -0400217static void dentry_lru_move_tail(struct dentry *dentry)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700218{
Nick Piggin23044502011-01-07 17:49:31 +1100219 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400220 if (list_empty(&dentry->d_lru)) {
221 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
222 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100223 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400224 } else {
225 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700226 }
Nick Piggin23044502011-01-07 17:49:31 +1100227 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700228}
229
Miklos Szeredid52b9082007-05-08 00:23:46 -0700230/**
231 * d_kill - kill dentry and return parent
232 * @dentry: dentry to kill
233 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200234 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700235 *
236 * If this is the root of the dentry tree, return NULL.
Nick Piggin23044502011-01-07 17:49:31 +1100237 *
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100238 * dcache_lock and d_lock and d_parent->d_lock must be held by caller, and
239 * are dropped by d_kill.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700240 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100241static struct dentry *d_kill(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200242 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100243 __releases(parent->d_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100244 __releases(dcache_inode_lock)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200245 __releases(dcache_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700246{
Nick Piggin949854d2011-01-07 17:49:37 +1100247 dentry->d_parent = NULL;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700248 list_del(&dentry->d_u.d_child);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100249 if (parent)
250 spin_unlock(&parent->d_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700251 dentry_iput(dentry);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100252 /*
253 * dentry_iput drops the locks, at which point nobody (except
254 * transient RCU lookups) can reach this dentry.
255 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700256 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900257 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700258}
259
Nick Piggin789680d2011-01-07 17:49:30 +1100260/**
261 * d_drop - drop a dentry
262 * @dentry: dentry to drop
263 *
264 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
265 * be found through a VFS lookup any more. Note that this is different from
266 * deleting the dentry - d_delete will try to mark the dentry negative if
267 * possible, giving a successful _negative_ lookup, while d_drop will
268 * just make the cache lookup fail.
269 *
270 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
271 * reason (NFS timeouts or autofs deletes).
272 *
273 * __d_drop requires dentry->d_lock.
274 */
275void __d_drop(struct dentry *dentry)
276{
277 if (!(dentry->d_flags & DCACHE_UNHASHED)) {
278 dentry->d_flags |= DCACHE_UNHASHED;
279 spin_lock(&dcache_hash_lock);
280 hlist_del_rcu(&dentry->d_hash);
281 spin_unlock(&dcache_hash_lock);
282 }
283}
284EXPORT_SYMBOL(__d_drop);
285
286void d_drop(struct dentry *dentry)
287{
288 spin_lock(&dcache_lock);
289 spin_lock(&dentry->d_lock);
290 __d_drop(dentry);
291 spin_unlock(&dentry->d_lock);
292 spin_unlock(&dcache_lock);
293}
294EXPORT_SYMBOL(d_drop);
295
Linus Torvalds1da177e2005-04-16 15:20:36 -0700296/*
297 * This is dput
298 *
299 * This is complicated by the fact that we do not want to put
300 * dentries that are no longer on any hash chain on the unused
301 * list: we'd much rather just get rid of them immediately.
302 *
303 * However, that implies that we have to traverse the dentry
304 * tree upwards to the parents which might _also_ now be
305 * scheduled for deletion (it may have been only waiting for
306 * its last child to go away).
307 *
308 * This tail recursion is done by hand as we don't want to depend
309 * on the compiler to always get this right (gcc generally doesn't).
310 * Real recursion would eat up our stack space.
311 */
312
313/*
314 * dput - release a dentry
315 * @dentry: dentry to release
316 *
317 * Release a dentry. This will drop the usage count and if appropriate
318 * call the dentry unlink method as well as removing it from the queues and
319 * releasing its resources. If the parent dentries were scheduled for release
320 * they too may now get deleted.
321 *
322 * no dcache lock, please.
323 */
324
325void dput(struct dentry *dentry)
326{
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100327 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700328 if (!dentry)
329 return;
330
331repeat:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100332 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700333 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700334 spin_lock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100335 if (IS_ROOT(dentry))
336 parent = NULL;
337 else
338 parent = dentry->d_parent;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100339 if (dentry->d_count == 1) {
340 if (!spin_trylock(&dcache_lock)) {
341 /*
342 * Something of a livelock possibility we could avoid
343 * by taking dcache_lock and trying again, but we
344 * want to reduce dcache_lock anyway so this will
345 * get improved.
346 */
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100347drop1:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100348 spin_unlock(&dentry->d_lock);
349 goto repeat;
350 }
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100351 if (!spin_trylock(&dcache_inode_lock)) {
352drop2:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100353 spin_unlock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100354 goto drop1;
355 }
356 if (parent && !spin_trylock(&parent->d_lock)) {
357 spin_unlock(&dcache_inode_lock);
358 goto drop2;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100359 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100360 }
361 dentry->d_count--;
362 if (dentry->d_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700363 spin_unlock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100364 if (parent)
365 spin_unlock(&parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700366 spin_unlock(&dcache_lock);
367 return;
368 }
369
370 /*
371 * AV: ->d_delete() is _NOT_ allowed to block now.
372 */
373 if (dentry->d_op && dentry->d_op->d_delete) {
374 if (dentry->d_op->d_delete(dentry))
375 goto unhash_it;
376 }
Nick Piggin265ac902010-10-10 05:36:24 -0400377
Linus Torvalds1da177e2005-04-16 15:20:36 -0700378 /* Unreachable? Get rid of it */
379 if (d_unhashed(dentry))
380 goto kill_it;
Nick Piggin265ac902010-10-10 05:36:24 -0400381
382 /* Otherwise leave it cached and ensure it's on the LRU */
383 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400384 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400385
Linus Torvalds1da177e2005-04-16 15:20:36 -0700386 spin_unlock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100387 if (parent)
388 spin_unlock(&parent->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100389 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700390 spin_unlock(&dcache_lock);
391 return;
392
393unhash_it:
394 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700395kill_it:
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700396 /* if dentry was on the d_lru list delete it from there */
397 dentry_lru_del(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100398 dentry = d_kill(dentry, parent);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700399 if (dentry)
400 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700401}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700402EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700403
404/**
405 * d_invalidate - invalidate a dentry
406 * @dentry: dentry to invalidate
407 *
408 * Try to invalidate the dentry if it turns out to be
409 * possible. If there are other dentries that can be
410 * reached through this one we can't delete it and we
411 * return -EBUSY. On success we return 0.
412 *
413 * no dcache lock.
414 */
415
416int d_invalidate(struct dentry * dentry)
417{
418 /*
419 * If it's already been dropped, return OK.
420 */
421 spin_lock(&dcache_lock);
Nick Pigginda502952011-01-07 17:49:33 +1100422 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700423 if (d_unhashed(dentry)) {
Nick Pigginda502952011-01-07 17:49:33 +1100424 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700425 spin_unlock(&dcache_lock);
426 return 0;
427 }
428 /*
429 * Check whether to do a partial shrink_dcache
430 * to get rid of unused child entries.
431 */
432 if (!list_empty(&dentry->d_subdirs)) {
Nick Pigginda502952011-01-07 17:49:33 +1100433 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700434 spin_unlock(&dcache_lock);
435 shrink_dcache_parent(dentry);
436 spin_lock(&dcache_lock);
Nick Pigginda502952011-01-07 17:49:33 +1100437 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 }
439
440 /*
441 * Somebody else still using it?
442 *
443 * If it's a directory, we can't drop it
444 * for fear of somebody re-populating it
445 * with children (even though dropping it
446 * would make it unreachable from the root,
447 * we might still populate it if it was a
448 * working directory or similar).
449 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100450 if (dentry->d_count > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700451 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
452 spin_unlock(&dentry->d_lock);
453 spin_unlock(&dcache_lock);
454 return -EBUSY;
455 }
456 }
457
458 __d_drop(dentry);
459 spin_unlock(&dentry->d_lock);
460 spin_unlock(&dcache_lock);
461 return 0;
462}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700463EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700464
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100465/* This must be called with dcache_lock and d_lock held */
Nick Piggin23044502011-01-07 17:49:31 +1100466static inline struct dentry * __dget_locked_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700467{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100468 dentry->d_count++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400469 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700470 return dentry;
471}
472
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100473/* This should be called _only_ with dcache_lock held */
Nick Piggin23044502011-01-07 17:49:31 +1100474static inline struct dentry * __dget_locked(struct dentry *dentry)
475{
Nick Piggin23044502011-01-07 17:49:31 +1100476 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100477 __dget_locked_dlock(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100478 spin_unlock(&dentry->d_lock);
479 return dentry;
480}
481
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100482struct dentry * dget_locked_dlock(struct dentry *dentry)
483{
484 return __dget_locked_dlock(dentry);
485}
486
Linus Torvalds1da177e2005-04-16 15:20:36 -0700487struct dentry * dget_locked(struct dentry *dentry)
488{
489 return __dget_locked(dentry);
490}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700491EXPORT_SYMBOL(dget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700492
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100493struct dentry *dget_parent(struct dentry *dentry)
494{
495 struct dentry *ret;
496
497repeat:
498 spin_lock(&dentry->d_lock);
499 ret = dentry->d_parent;
500 if (!ret)
501 goto out;
502 if (dentry == ret) {
503 ret->d_count++;
504 goto out;
505 }
506 if (!spin_trylock(&ret->d_lock)) {
507 spin_unlock(&dentry->d_lock);
508 cpu_relax();
509 goto repeat;
510 }
511 BUG_ON(!ret->d_count);
512 ret->d_count++;
513 spin_unlock(&ret->d_lock);
514out:
515 spin_unlock(&dentry->d_lock);
516 return ret;
517}
518EXPORT_SYMBOL(dget_parent);
519
Linus Torvalds1da177e2005-04-16 15:20:36 -0700520/**
521 * d_find_alias - grab a hashed alias of inode
522 * @inode: inode in question
523 * @want_discon: flag, used by d_splice_alias, to request
524 * that only a DISCONNECTED alias be returned.
525 *
526 * If inode has a hashed alias, or is a directory and has any alias,
527 * acquire the reference to alias and return it. Otherwise return NULL.
528 * Notice that if inode is a directory there can be only one alias and
529 * it can be unhashed only if it has no children, or if it is the root
530 * of a filesystem.
531 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700532 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700533 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700534 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700535 */
Nick Pigginda502952011-01-07 17:49:33 +1100536static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700537{
Nick Pigginda502952011-01-07 17:49:33 +1100538 struct dentry *alias, *discon_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539
Nick Pigginda502952011-01-07 17:49:33 +1100540again:
541 discon_alias = NULL;
542 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
543 spin_lock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700544 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700545 if (IS_ROOT(alias) &&
Nick Pigginda502952011-01-07 17:49:33 +1100546 (alias->d_flags & DCACHE_DISCONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700547 discon_alias = alias;
Nick Pigginda502952011-01-07 17:49:33 +1100548 } else if (!want_discon) {
549 __dget_locked_dlock(alias);
550 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700551 return alias;
552 }
553 }
Nick Pigginda502952011-01-07 17:49:33 +1100554 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700555 }
Nick Pigginda502952011-01-07 17:49:33 +1100556 if (discon_alias) {
557 alias = discon_alias;
558 spin_lock(&alias->d_lock);
559 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
560 if (IS_ROOT(alias) &&
561 (alias->d_flags & DCACHE_DISCONNECTED)) {
562 __dget_locked_dlock(alias);
563 spin_unlock(&alias->d_lock);
564 return alias;
565 }
566 }
567 spin_unlock(&alias->d_lock);
568 goto again;
569 }
570 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700571}
572
Nick Pigginda502952011-01-07 17:49:33 +1100573struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700574{
David Howells214fda12006-03-25 03:06:36 -0800575 struct dentry *de = NULL;
576
577 if (!list_empty(&inode->i_dentry)) {
578 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100579 spin_lock(&dcache_inode_lock);
David Howells214fda12006-03-25 03:06:36 -0800580 de = __d_find_alias(inode, 0);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100581 spin_unlock(&dcache_inode_lock);
David Howells214fda12006-03-25 03:06:36 -0800582 spin_unlock(&dcache_lock);
583 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700584 return de;
585}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700586EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700587
588/*
589 * Try to kill dentries associated with this inode.
590 * WARNING: you must own a reference to inode.
591 */
592void d_prune_aliases(struct inode *inode)
593{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700594 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700595restart:
596 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100597 spin_lock(&dcache_inode_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700598 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700599 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100600 if (!dentry->d_count) {
Nick Piggin23044502011-01-07 17:49:31 +1100601 __dget_locked_dlock(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700602 __d_drop(dentry);
603 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100604 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700605 spin_unlock(&dcache_lock);
606 dput(dentry);
607 goto restart;
608 }
609 spin_unlock(&dentry->d_lock);
610 }
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100611 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612 spin_unlock(&dcache_lock);
613}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700614EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615
616/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700617 * Throw away a dentry - free the inode, dput the parent. This requires that
618 * the LRU list has already been removed.
619 *
Miklos Szeredi85864e12007-10-16 23:27:09 -0700620 * Try to prune ancestors as well. This is necessary to prevent
621 * quadratic behavior of shrink_dcache_parent(), but is also expected
622 * to be beneficial in reducing dentry cache fragmentation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700623 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100624static void prune_one_dentry(struct dentry *dentry, struct dentry *parent)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200625 __releases(dentry->d_lock)
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100626 __releases(parent->d_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100627 __releases(dcache_inode_lock)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200628 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700629{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 __d_drop(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100631 dentry = d_kill(dentry, parent);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700632
633 /*
634 * Prune ancestors. Locking is simpler than in dput(),
635 * because dcache_lock needs to be taken anyway.
636 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700637 while (dentry) {
Nick Piggin23044502011-01-07 17:49:31 +1100638 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100639 spin_lock(&dcache_inode_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100640again:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100641 spin_lock(&dentry->d_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100642 if (IS_ROOT(dentry))
643 parent = NULL;
644 else
645 parent = dentry->d_parent;
646 if (parent && !spin_trylock(&parent->d_lock)) {
647 spin_unlock(&dentry->d_lock);
648 goto again;
649 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100650 dentry->d_count--;
651 if (dentry->d_count) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100652 if (parent)
653 spin_unlock(&parent->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100654 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100655 spin_unlock(&dcache_inode_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100656 spin_unlock(&dcache_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700657 return;
Nick Piggin23044502011-01-07 17:49:31 +1100658 }
Miklos Szeredid52b9082007-05-08 00:23:46 -0700659
Christoph Hellwiga4633352010-10-10 05:36:26 -0400660 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700661 __d_drop(dentry);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100662 dentry = d_kill(dentry, parent);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700663 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700664}
665
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400666static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700667{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700668 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700669
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400670 while (!list_empty(list)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100671 struct dentry *parent;
672
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400673 dentry = list_entry(list->prev, struct dentry, d_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100674
675 if (!spin_trylock(&dentry->d_lock)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100676relock:
Nick Piggin23044502011-01-07 17:49:31 +1100677 spin_unlock(&dcache_lru_lock);
678 cpu_relax();
679 spin_lock(&dcache_lru_lock);
680 continue;
681 }
682
Linus Torvalds1da177e2005-04-16 15:20:36 -0700683 /*
684 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700685 * the LRU because of laziness during lookup. Do not free
686 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700687 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100688 if (dentry->d_count) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100689 __dentry_lru_del(dentry);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700690 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700691 continue;
692 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100693 if (IS_ROOT(dentry))
694 parent = NULL;
695 else
696 parent = dentry->d_parent;
697 if (parent && !spin_trylock(&parent->d_lock)) {
698 spin_unlock(&dentry->d_lock);
699 goto relock;
700 }
701 __dentry_lru_del(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100702 spin_unlock(&dcache_lru_lock);
703
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100704 prune_one_dentry(dentry, parent);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100705 /* dcache_lock, dcache_inode_lock and dentry->d_lock dropped */
Nick Piggin23044502011-01-07 17:49:31 +1100706 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100707 spin_lock(&dcache_inode_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100708 spin_lock(&dcache_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700709 }
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400710}
711
712/**
713 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
714 * @sb: superblock to shrink dentry LRU.
715 * @count: number of entries to prune
716 * @flags: flags to control the dentry processing
717 *
718 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
719 */
720static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
721{
722 /* called from prune_dcache() and shrink_dcache_parent() */
723 struct dentry *dentry;
724 LIST_HEAD(referenced);
725 LIST_HEAD(tmp);
726 int cnt = *count;
727
728 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100729 spin_lock(&dcache_inode_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100730relock:
731 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400732 while (!list_empty(&sb->s_dentry_lru)) {
733 dentry = list_entry(sb->s_dentry_lru.prev,
734 struct dentry, d_lru);
735 BUG_ON(dentry->d_sb != sb);
736
Nick Piggin23044502011-01-07 17:49:31 +1100737 if (!spin_trylock(&dentry->d_lock)) {
738 spin_unlock(&dcache_lru_lock);
739 cpu_relax();
740 goto relock;
741 }
742
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400743 /*
744 * If we are honouring the DCACHE_REFERENCED flag and the
745 * dentry has this flag set, don't free it. Clear the flag
746 * and put it back on the LRU.
747 */
Nick Piggin23044502011-01-07 17:49:31 +1100748 if (flags & DCACHE_REFERENCED &&
749 dentry->d_flags & DCACHE_REFERENCED) {
750 dentry->d_flags &= ~DCACHE_REFERENCED;
751 list_move(&dentry->d_lru, &referenced);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400752 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100753 } else {
754 list_move_tail(&dentry->d_lru, &tmp);
755 spin_unlock(&dentry->d_lock);
756 if (!--cnt)
757 break;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400758 }
Nick Piggin23044502011-01-07 17:49:31 +1100759 /* XXX: re-add cond_resched_lock when dcache_lock goes away */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400760 }
761
762 *count = cnt;
763 shrink_dentry_list(&tmp);
764
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700765 if (!list_empty(&referenced))
766 list_splice(&referenced, &sb->s_dentry_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100767 spin_unlock(&dcache_lru_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100768 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700769 spin_unlock(&dcache_lock);
770}
771
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700772/**
773 * prune_dcache - shrink the dcache
774 * @count: number of entries to try to free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700775 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700776 * Shrink the dcache. This is done when we need more memory, or simply when we
777 * need to unmount something (at which point we need to unuse all dentries).
778 *
779 * This function may fail to free any resources if all the dentries are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700780 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700781static void prune_dcache(int count)
782{
Al Virodca33252010-07-25 02:31:46 +0400783 struct super_block *sb, *p = NULL;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700784 int w_count;
Nick Piggin86c87492011-01-07 17:49:18 +1100785 int unused = dentry_stat.nr_unused;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700786 int prune_ratio;
787 int pruned;
788
789 if (unused == 0 || count == 0)
790 return;
791 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700792 if (count >= unused)
793 prune_ratio = 1;
794 else
795 prune_ratio = unused / count;
796 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400797 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400798 if (list_empty(&sb->s_instances))
799 continue;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700800 if (sb->s_nr_dentry_unused == 0)
801 continue;
802 sb->s_count++;
803 /* Now, we reclaim unused dentrins with fairness.
804 * We reclaim them same percentage from each superblock.
805 * We calculate number of dentries to scan on this sb
806 * as follows, but the implementation is arranged to avoid
807 * overflows:
808 * number of dentries to scan on this sb =
809 * count * (number of dentries on this sb /
810 * number of dentries in the machine)
811 */
812 spin_unlock(&sb_lock);
813 if (prune_ratio != 1)
814 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
815 else
816 w_count = sb->s_nr_dentry_unused;
817 pruned = w_count;
818 /*
819 * We need to be sure this filesystem isn't being unmounted,
820 * otherwise we could race with generic_shutdown_super(), and
821 * end up holding a reference to an inode while the filesystem
822 * is unmounted. So we try to get s_umount, and make sure
823 * s_root isn't NULL.
824 */
825 if (down_read_trylock(&sb->s_umount)) {
826 if ((sb->s_root != NULL) &&
827 (!list_empty(&sb->s_dentry_lru))) {
828 spin_unlock(&dcache_lock);
829 __shrink_dcache_sb(sb, &w_count,
830 DCACHE_REFERENCED);
831 pruned -= w_count;
832 spin_lock(&dcache_lock);
833 }
834 up_read(&sb->s_umount);
835 }
836 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400837 if (p)
838 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700839 count -= pruned;
Al Virodca33252010-07-25 02:31:46 +0400840 p = sb;
Al Viro79893c12010-03-22 20:27:55 -0400841 /* more work left to do? */
842 if (count <= 0)
843 break;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700844 }
Al Virodca33252010-07-25 02:31:46 +0400845 if (p)
846 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700847 spin_unlock(&sb_lock);
848 spin_unlock(&dcache_lock);
849}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700850
851/**
852 * shrink_dcache_sb - shrink dcache for a superblock
853 * @sb: superblock
854 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400855 * Shrink the dcache for the specified super block. This is used to free
856 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700857 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400858void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700859{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400860 LIST_HEAD(tmp);
861
862 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100863 spin_lock(&dcache_inode_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100864 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400865 while (!list_empty(&sb->s_dentry_lru)) {
866 list_splice_init(&sb->s_dentry_lru, &tmp);
867 shrink_dentry_list(&tmp);
868 }
Nick Piggin23044502011-01-07 17:49:31 +1100869 spin_unlock(&dcache_lru_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +1100870 spin_unlock(&dcache_inode_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400871 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700872}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700873EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700874
875/*
David Howellsc636ebd2006-10-11 01:22:19 -0700876 * destroy a single subtree of dentries for unmount
877 * - see the comments on shrink_dcache_for_umount() for a description of the
878 * locking
879 */
880static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
881{
882 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700883 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700884
885 BUG_ON(!IS_ROOT(dentry));
886
887 /* detach this root from the system */
888 spin_lock(&dcache_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100889 spin_lock(&dentry->d_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400890 dentry_lru_del(dentry);
David Howellsc636ebd2006-10-11 01:22:19 -0700891 __d_drop(dentry);
Nick Pigginda502952011-01-07 17:49:33 +1100892 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700893 spin_unlock(&dcache_lock);
894
895 for (;;) {
896 /* descend to the first leaf in the current subtree */
897 while (!list_empty(&dentry->d_subdirs)) {
898 struct dentry *loop;
899
900 /* this is a branch with children - detach all of them
901 * from the system in one go */
902 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100903 spin_lock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700904 list_for_each_entry(loop, &dentry->d_subdirs,
905 d_u.d_child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100906 spin_lock_nested(&loop->d_lock,
907 DENTRY_D_LOCK_NESTED);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400908 dentry_lru_del(loop);
David Howellsc636ebd2006-10-11 01:22:19 -0700909 __d_drop(loop);
Nick Pigginda502952011-01-07 17:49:33 +1100910 spin_unlock(&loop->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700911 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100912 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700913 spin_unlock(&dcache_lock);
914
915 /* move to the first child */
916 dentry = list_entry(dentry->d_subdirs.next,
917 struct dentry, d_u.d_child);
918 }
919
920 /* consume the dentries from this leaf up through its parents
921 * until we find one with children or run out altogether */
922 do {
923 struct inode *inode;
924
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100925 if (dentry->d_count != 0) {
David Howellsc636ebd2006-10-11 01:22:19 -0700926 printk(KERN_ERR
927 "BUG: Dentry %p{i=%lx,n=%s}"
928 " still in use (%d)"
929 " [unmount of %s %s]\n",
930 dentry,
931 dentry->d_inode ?
932 dentry->d_inode->i_ino : 0UL,
933 dentry->d_name.name,
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100934 dentry->d_count,
David Howellsc636ebd2006-10-11 01:22:19 -0700935 dentry->d_sb->s_type->name,
936 dentry->d_sb->s_id);
937 BUG();
938 }
939
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100940 if (IS_ROOT(dentry)) {
David Howellsc636ebd2006-10-11 01:22:19 -0700941 parent = NULL;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100942 list_del(&dentry->d_u.d_child);
943 } else {
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900944 parent = dentry->d_parent;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100945 spin_lock(&parent->d_lock);
946 parent->d_count--;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +1100947 list_del(&dentry->d_u.d_child);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100948 spin_unlock(&parent->d_lock);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900949 }
David Howellsc636ebd2006-10-11 01:22:19 -0700950
David Howellsf8713572006-10-28 10:38:46 -0700951 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700952
953 inode = dentry->d_inode;
954 if (inode) {
955 dentry->d_inode = NULL;
956 list_del_init(&dentry->d_alias);
957 if (dentry->d_op && dentry->d_op->d_iput)
958 dentry->d_op->d_iput(dentry, inode);
959 else
960 iput(inode);
961 }
962
963 d_free(dentry);
964
965 /* finished when we fall off the top of the tree,
966 * otherwise we ascend to the parent and move to the
967 * next sibling if there is one */
968 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400969 return;
David Howellsc636ebd2006-10-11 01:22:19 -0700970 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700971 } while (list_empty(&dentry->d_subdirs));
972
973 dentry = list_entry(dentry->d_subdirs.next,
974 struct dentry, d_u.d_child);
975 }
976}
977
978/*
979 * destroy the dentries attached to a superblock on unmounting
980 * - we don't need to use dentry->d_lock, and only need dcache_lock when
981 * removing the dentry from the system lists and hashes because:
982 * - the superblock is detached from all mountings and open files, so the
983 * dentry trees will not be rearranged by the VFS
984 * - s_umount is write-locked, so the memory pressure shrinker will ignore
985 * any dentries belonging to this superblock that it comes across
986 * - the filesystem itself is no longer permitted to rearrange the dentries
987 * in this superblock
988 */
989void shrink_dcache_for_umount(struct super_block *sb)
990{
991 struct dentry *dentry;
992
993 if (down_read_trylock(&sb->s_umount))
994 BUG();
995
996 dentry = sb->s_root;
997 sb->s_root = NULL;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100998 spin_lock(&dentry->d_lock);
999 dentry->d_count--;
1000 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -07001001 shrink_dcache_for_umount_subtree(dentry);
1002
1003 while (!hlist_empty(&sb->s_anon)) {
1004 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
1005 shrink_dcache_for_umount_subtree(dentry);
1006 }
1007}
1008
1009/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07001010 * Search for at least 1 mount point in the dentry's subdirs.
1011 * We descend to the next level whenever the d_subdirs
1012 * list is non-empty and continue searching.
1013 */
1014
1015/**
1016 * have_submounts - check for mounts over a dentry
1017 * @parent: dentry to check.
1018 *
1019 * Return true if the parent or its subdirectories contain
1020 * a mount point
1021 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001022int have_submounts(struct dentry *parent)
1023{
Nick Piggin949854d2011-01-07 17:49:37 +11001024 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001025 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11001026 unsigned seq;
1027
1028rename_retry:
1029 this_parent = parent;
1030 seq = read_seqbegin(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001031
1032 spin_lock(&dcache_lock);
1033 if (d_mountpoint(parent))
1034 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001035 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036repeat:
1037 next = this_parent->d_subdirs.next;
1038resume:
1039 while (next != &this_parent->d_subdirs) {
1040 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001041 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001042 next = tmp->next;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001043
1044 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001045 /* Have we found a mount point ? */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001046 if (d_mountpoint(dentry)) {
1047 spin_unlock(&dentry->d_lock);
1048 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 goto positive;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001050 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001051 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001052 spin_unlock(&this_parent->d_lock);
1053 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001054 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001055 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001056 goto repeat;
1057 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001058 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001059 }
1060 /*
1061 * All done at this level ... ascend and resume the search.
1062 */
1063 if (this_parent != parent) {
Nick Piggin949854d2011-01-07 17:49:37 +11001064 struct dentry *tmp;
1065 struct dentry *child;
1066
1067 tmp = this_parent->d_parent;
1068 rcu_read_lock();
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001069 spin_unlock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11001070 child = this_parent;
1071 this_parent = tmp;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001072 spin_lock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11001073 /* might go back up the wrong parent if we have had a rename
1074 * or deletion */
1075 if (this_parent != child->d_parent ||
1076 read_seqretry(&rename_lock, seq)) {
1077 spin_unlock(&this_parent->d_lock);
1078 spin_unlock(&dcache_lock);
1079 rcu_read_unlock();
1080 goto rename_retry;
1081 }
1082 rcu_read_unlock();
1083 next = child->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001084 goto resume;
1085 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001086 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001087 spin_unlock(&dcache_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11001088 if (read_seqretry(&rename_lock, seq))
1089 goto rename_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001090 return 0; /* No mount points found in tree */
1091positive:
1092 spin_unlock(&dcache_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11001093 if (read_seqretry(&rename_lock, seq))
1094 goto rename_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095 return 1;
1096}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001097EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001098
1099/*
1100 * Search the dentry child list for the specified parent,
1101 * and move any unused dentries to the end of the unused
1102 * list for prune_dcache(). We descend to the next level
1103 * whenever the d_subdirs list is non-empty and continue
1104 * searching.
1105 *
1106 * It returns zero iff there are no unused children,
1107 * otherwise it returns the number of children moved to
1108 * the end of the unused list. This may not be the total
1109 * number of unused children, because select_parent can
1110 * drop the lock and return early due to latency
1111 * constraints.
1112 */
1113static int select_parent(struct dentry * parent)
1114{
Nick Piggin949854d2011-01-07 17:49:37 +11001115 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001116 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11001117 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001118 int found = 0;
1119
Nick Piggin949854d2011-01-07 17:49:37 +11001120rename_retry:
1121 this_parent = parent;
1122 seq = read_seqbegin(&rename_lock);
1123
Linus Torvalds1da177e2005-04-16 15:20:36 -07001124 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001125 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126repeat:
1127 next = this_parent->d_subdirs.next;
1128resume:
1129 while (next != &this_parent->d_subdirs) {
1130 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001131 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001132 next = tmp->next;
1133
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001134 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Nick Piggin23044502011-01-07 17:49:31 +11001135
Linus Torvalds1da177e2005-04-16 15:20:36 -07001136 /*
1137 * move only zero ref count dentries to the end
1138 * of the unused list for prune_dcache
1139 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001140 if (!dentry->d_count) {
Christoph Hellwiga4633352010-10-10 05:36:26 -04001141 dentry_lru_move_tail(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001142 found++;
Christoph Hellwiga4633352010-10-10 05:36:26 -04001143 } else {
1144 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001145 }
1146
1147 /*
1148 * We can return to the caller if we have found some (this
1149 * ensures forward progress). We'll be coming back to find
1150 * the rest.
1151 */
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001152 if (found && need_resched()) {
1153 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 goto out;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001155 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001156
1157 /*
1158 * Descend a level if the d_subdirs list is non-empty.
1159 */
1160 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001161 spin_unlock(&this_parent->d_lock);
1162 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001163 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001164 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001165 goto repeat;
1166 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001167
1168 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 }
1170 /*
1171 * All done at this level ... ascend and resume the search.
1172 */
1173 if (this_parent != parent) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001174 struct dentry *tmp;
Nick Piggin949854d2011-01-07 17:49:37 +11001175 struct dentry *child;
1176
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001177 tmp = this_parent->d_parent;
Nick Piggin949854d2011-01-07 17:49:37 +11001178 rcu_read_lock();
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001179 spin_unlock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11001180 child = this_parent;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001181 this_parent = tmp;
1182 spin_lock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11001183 /* might go back up the wrong parent if we have had a rename
1184 * or deletion */
1185 if (this_parent != child->d_parent ||
1186 read_seqretry(&rename_lock, seq)) {
1187 spin_unlock(&this_parent->d_lock);
1188 spin_unlock(&dcache_lock);
1189 rcu_read_unlock();
1190 goto rename_retry;
1191 }
1192 rcu_read_unlock();
1193 next = child->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001194 goto resume;
1195 }
1196out:
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001197 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001198 spin_unlock(&dcache_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11001199 if (read_seqretry(&rename_lock, seq))
1200 goto rename_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001201 return found;
1202}
1203
1204/**
1205 * shrink_dcache_parent - prune dcache
1206 * @parent: parent of entries to prune
1207 *
1208 * Prune the dcache to remove unused children of the parent dentry.
1209 */
1210
1211void shrink_dcache_parent(struct dentry * parent)
1212{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001213 struct super_block *sb = parent->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001214 int found;
1215
1216 while ((found = select_parent(parent)) != 0)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001217 __shrink_dcache_sb(sb, &found, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001218}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001219EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001220
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221/*
1222 * Scan `nr' dentries and return the number which remain.
1223 *
1224 * We need to avoid reentering the filesystem if the caller is performing a
1225 * GFP_NOFS allocation attempt. One example deadlock is:
1226 *
1227 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1228 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1229 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1230 *
1231 * In this case we return -1 to tell the caller that we baled.
1232 */
Dave Chinner7f8275d2010-07-19 14:56:17 +10001233static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001234{
1235 if (nr) {
1236 if (!(gfp_mask & __GFP_FS))
1237 return -1;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001238 prune_dcache(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001239 }
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001240
Nick Piggin86c87492011-01-07 17:49:18 +11001241 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001242}
1243
Rusty Russell8e1f9362007-07-17 04:03:17 -07001244static struct shrinker dcache_shrinker = {
1245 .shrink = shrink_dcache_memory,
1246 .seeks = DEFAULT_SEEKS,
1247};
1248
Linus Torvalds1da177e2005-04-16 15:20:36 -07001249/**
1250 * d_alloc - allocate a dcache entry
1251 * @parent: parent of entry to allocate
1252 * @name: qstr of the name
1253 *
1254 * Allocates a dentry. It returns %NULL if there is insufficient memory
1255 * available. On a success the dentry is returned. The name passed in is
1256 * copied and the copy passed in may be reused after this call.
1257 */
1258
1259struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1260{
1261 struct dentry *dentry;
1262 char *dname;
1263
Mel Gormane12ba742007-10-16 01:25:52 -07001264 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001265 if (!dentry)
1266 return NULL;
1267
1268 if (name->len > DNAME_INLINE_LEN-1) {
1269 dname = kmalloc(name->len + 1, GFP_KERNEL);
1270 if (!dname) {
1271 kmem_cache_free(dentry_cache, dentry);
1272 return NULL;
1273 }
1274 } else {
1275 dname = dentry->d_iname;
1276 }
1277 dentry->d_name.name = dname;
1278
1279 dentry->d_name.len = name->len;
1280 dentry->d_name.hash = name->hash;
1281 memcpy(dname, name->name, name->len);
1282 dname[name->len] = 0;
1283
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001284 dentry->d_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001285 dentry->d_flags = DCACHE_UNHASHED;
1286 spin_lock_init(&dentry->d_lock);
1287 dentry->d_inode = NULL;
1288 dentry->d_parent = NULL;
1289 dentry->d_sb = NULL;
1290 dentry->d_op = NULL;
1291 dentry->d_fsdata = NULL;
1292 dentry->d_mounted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001293 INIT_HLIST_NODE(&dentry->d_hash);
1294 INIT_LIST_HEAD(&dentry->d_lru);
1295 INIT_LIST_HEAD(&dentry->d_subdirs);
1296 INIT_LIST_HEAD(&dentry->d_alias);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001297 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001298
1299 if (parent) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001300 spin_lock(&dcache_lock);
1301 spin_lock(&parent->d_lock);
1302 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1303 dentry->d_parent = dget_dlock(parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001304 dentry->d_sb = parent->d_sb;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001305 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001306 spin_unlock(&dentry->d_lock);
1307 spin_unlock(&parent->d_lock);
1308 spin_unlock(&dcache_lock);
1309 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07001310
Nick Piggin3e880fb2011-01-07 17:49:19 +11001311 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001312
Linus Torvalds1da177e2005-04-16 15:20:36 -07001313 return dentry;
1314}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001315EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001316
1317struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1318{
1319 struct qstr q;
1320
1321 q.name = name;
1322 q.len = strlen(name);
1323 q.hash = full_name_hash(q.name, q.len);
1324 return d_alloc(parent, &q);
1325}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001326EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001327
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001328/* the caller must hold dcache_lock */
1329static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1330{
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001331 spin_lock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001332 if (inode)
1333 list_add(&dentry->d_alias, &inode->i_dentry);
1334 dentry->d_inode = inode;
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001335 spin_unlock(&dentry->d_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001336 fsnotify_d_instantiate(dentry, inode);
1337}
1338
Linus Torvalds1da177e2005-04-16 15:20:36 -07001339/**
1340 * d_instantiate - fill in inode information for a dentry
1341 * @entry: dentry to complete
1342 * @inode: inode to attach to this dentry
1343 *
1344 * Fill in inode information in the entry.
1345 *
1346 * This turns negative dentries into productive full members
1347 * of society.
1348 *
1349 * NOTE! This assumes that the inode count has been incremented
1350 * (or otherwise set) by the caller to indicate that it is now
1351 * in use by the dcache.
1352 */
1353
1354void d_instantiate(struct dentry *entry, struct inode * inode)
1355{
Eric Sesterhenn28133c72006-03-26 18:25:39 +02001356 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001357 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001358 spin_lock(&dcache_inode_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001359 __d_instantiate(entry, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001360 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001361 spin_unlock(&dcache_lock);
1362 security_d_instantiate(entry, inode);
1363}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001364EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001365
1366/**
1367 * d_instantiate_unique - instantiate a non-aliased dentry
1368 * @entry: dentry to instantiate
1369 * @inode: inode to attach to this dentry
1370 *
1371 * Fill in inode information in the entry. On success, it returns NULL.
1372 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001373 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001374 *
1375 * Note that in order to avoid conflicts with rename() etc, the caller
1376 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001377 *
1378 * This also assumes that the inode count has been incremented
1379 * (or otherwise set) by the caller to indicate that it is now
1380 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001381 */
David Howells770bfad2006-08-22 20:06:07 -04001382static struct dentry *__d_instantiate_unique(struct dentry *entry,
1383 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001384{
1385 struct dentry *alias;
1386 int len = entry->d_name.len;
1387 const char *name = entry->d_name.name;
1388 unsigned int hash = entry->d_name.hash;
1389
David Howells770bfad2006-08-22 20:06:07 -04001390 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001391 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001392 return NULL;
1393 }
1394
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1396 struct qstr *qstr = &alias->d_name;
1397
Nick Piggin9abca362011-01-07 17:49:36 +11001398 /*
1399 * Don't need alias->d_lock here, because aliases with
1400 * d_parent == entry->d_parent are not subject to name or
1401 * parent changes, because the parent inode i_mutex is held.
1402 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001403 if (qstr->hash != hash)
1404 continue;
1405 if (alias->d_parent != entry->d_parent)
1406 continue;
1407 if (qstr->len != len)
1408 continue;
1409 if (memcmp(qstr->name, name, len))
1410 continue;
1411 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001412 return alias;
1413 }
David Howells770bfad2006-08-22 20:06:07 -04001414
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001415 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001416 return NULL;
1417}
David Howells770bfad2006-08-22 20:06:07 -04001418
1419struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1420{
1421 struct dentry *result;
1422
1423 BUG_ON(!list_empty(&entry->d_alias));
1424
1425 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001426 spin_lock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04001427 result = __d_instantiate_unique(entry, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001428 spin_unlock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04001429 spin_unlock(&dcache_lock);
1430
1431 if (!result) {
1432 security_d_instantiate(entry, inode);
1433 return NULL;
1434 }
1435
1436 BUG_ON(!d_unhashed(result));
1437 iput(inode);
1438 return result;
1439}
1440
Linus Torvalds1da177e2005-04-16 15:20:36 -07001441EXPORT_SYMBOL(d_instantiate_unique);
1442
1443/**
1444 * d_alloc_root - allocate root dentry
1445 * @root_inode: inode to allocate the root for
1446 *
1447 * Allocate a root ("/") dentry for the inode given. The inode is
1448 * instantiated and returned. %NULL is returned if there is insufficient
1449 * memory or the inode passed is %NULL.
1450 */
1451
1452struct dentry * d_alloc_root(struct inode * root_inode)
1453{
1454 struct dentry *res = NULL;
1455
1456 if (root_inode) {
1457 static const struct qstr name = { .name = "/", .len = 1 };
1458
1459 res = d_alloc(NULL, &name);
1460 if (res) {
1461 res->d_sb = root_inode->i_sb;
1462 res->d_parent = res;
1463 d_instantiate(res, root_inode);
1464 }
1465 }
1466 return res;
1467}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001468EXPORT_SYMBOL(d_alloc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001469
1470static inline struct hlist_head *d_hash(struct dentry *parent,
1471 unsigned long hash)
1472{
1473 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1474 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1475 return dentry_hashtable + (hash & D_HASHMASK);
1476}
1477
1478/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001479 * d_obtain_alias - find or allocate a dentry for a given inode
1480 * @inode: inode to allocate the dentry for
1481 *
1482 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1483 * similar open by handle operations. The returned dentry may be anonymous,
1484 * or may have a full name (if the inode was already in the cache).
1485 *
1486 * When called on a directory inode, we must ensure that the inode only ever
1487 * has one dentry. If a dentry is found, that is returned instead of
1488 * allocating a new one.
1489 *
1490 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001491 * to the dentry. In case of an error the reference on the inode is released.
1492 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1493 * be passed in and will be the error will be propagate to the return value,
1494 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001495 */
1496struct dentry *d_obtain_alias(struct inode *inode)
1497{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001498 static const struct qstr anonstring = { .name = "" };
1499 struct dentry *tmp;
1500 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001501
1502 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001503 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001504 if (IS_ERR(inode))
1505 return ERR_CAST(inode);
1506
Christoph Hellwig9308a612008-08-11 15:49:12 +02001507 res = d_find_alias(inode);
1508 if (res)
1509 goto out_iput;
1510
1511 tmp = d_alloc(NULL, &anonstring);
1512 if (!tmp) {
1513 res = ERR_PTR(-ENOMEM);
1514 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001515 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001516 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1517
1518 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001519 spin_lock(&dcache_inode_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001520 res = __d_find_alias(inode, 0);
1521 if (res) {
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001522 spin_unlock(&dcache_inode_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001523 spin_unlock(&dcache_lock);
1524 dput(tmp);
1525 goto out_iput;
1526 }
1527
1528 /* attach a disconnected dentry */
1529 spin_lock(&tmp->d_lock);
1530 tmp->d_sb = inode->i_sb;
1531 tmp->d_inode = inode;
1532 tmp->d_flags |= DCACHE_DISCONNECTED;
1533 tmp->d_flags &= ~DCACHE_UNHASHED;
1534 list_add(&tmp->d_alias, &inode->i_dentry);
Nick Piggin789680d2011-01-07 17:49:30 +11001535 spin_lock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001536 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
Nick Piggin789680d2011-01-07 17:49:30 +11001537 spin_unlock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001538 spin_unlock(&tmp->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001539 spin_unlock(&dcache_inode_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001540
1541 spin_unlock(&dcache_lock);
1542 return tmp;
1543
1544 out_iput:
1545 iput(inode);
1546 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001547}
Benny Halevyadc48722009-02-27 14:02:59 -08001548EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001549
1550/**
1551 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1552 * @inode: the inode which may have a disconnected dentry
1553 * @dentry: a negative dentry which we want to point to the inode.
1554 *
1555 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1556 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1557 * and return it, else simply d_add the inode to the dentry and return NULL.
1558 *
1559 * This is needed in the lookup routine of any filesystem that is exportable
1560 * (via knfsd) so that we can build dcache paths to directories effectively.
1561 *
1562 * If a dentry was found and moved, then it is returned. Otherwise NULL
1563 * is returned. This matches the expected return value of ->lookup.
1564 *
1565 */
1566struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1567{
1568 struct dentry *new = NULL;
1569
NeilBrown21c0d8f2006-10-04 02:16:16 -07001570 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001571 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001572 spin_lock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001573 new = __d_find_alias(inode, 1);
1574 if (new) {
1575 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001576 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001577 spin_unlock(&dcache_lock);
1578 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001579 d_move(new, dentry);
1580 iput(inode);
1581 } else {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001582 /* already taking dcache_lock, so d_add() by hand */
1583 __d_instantiate(dentry, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001584 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001585 spin_unlock(&dcache_lock);
1586 security_d_instantiate(dentry, inode);
1587 d_rehash(dentry);
1588 }
1589 } else
1590 d_add(dentry, inode);
1591 return new;
1592}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001593EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001594
Barry Naujok94035402008-05-21 16:50:46 +10001595/**
1596 * d_add_ci - lookup or allocate new dentry with case-exact name
1597 * @inode: the inode case-insensitive lookup has found
1598 * @dentry: the negative dentry that was passed to the parent's lookup func
1599 * @name: the case-exact name to be associated with the returned dentry
1600 *
1601 * This is to avoid filling the dcache with case-insensitive names to the
1602 * same inode, only the actual correct case is stored in the dcache for
1603 * case-insensitive filesystems.
1604 *
1605 * For a case-insensitive lookup match and if the the case-exact dentry
1606 * already exists in in the dcache, use it and return it.
1607 *
1608 * If no entry exists with the exact case name, allocate new dentry with
1609 * the exact case, and return the spliced entry.
1610 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001611struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001612 struct qstr *name)
1613{
1614 int error;
1615 struct dentry *found;
1616 struct dentry *new;
1617
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001618 /*
1619 * First check if a dentry matching the name already exists,
1620 * if not go ahead and create it now.
1621 */
Barry Naujok94035402008-05-21 16:50:46 +10001622 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001623 if (!found) {
1624 new = d_alloc(dentry->d_parent, name);
1625 if (!new) {
1626 error = -ENOMEM;
1627 goto err_out;
1628 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001629
Barry Naujok94035402008-05-21 16:50:46 +10001630 found = d_splice_alias(inode, new);
1631 if (found) {
1632 dput(new);
1633 return found;
1634 }
1635 return new;
1636 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001637
1638 /*
1639 * If a matching dentry exists, and it's not negative use it.
1640 *
1641 * Decrement the reference count to balance the iget() done
1642 * earlier on.
1643 */
Barry Naujok94035402008-05-21 16:50:46 +10001644 if (found->d_inode) {
1645 if (unlikely(found->d_inode != inode)) {
1646 /* This can't happen because bad inodes are unhashed. */
1647 BUG_ON(!is_bad_inode(inode));
1648 BUG_ON(!is_bad_inode(found->d_inode));
1649 }
Barry Naujok94035402008-05-21 16:50:46 +10001650 iput(inode);
1651 return found;
1652 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001653
Barry Naujok94035402008-05-21 16:50:46 +10001654 /*
1655 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001656 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001657 */
Barry Naujok94035402008-05-21 16:50:46 +10001658 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001659 spin_lock(&dcache_inode_lock);
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001660 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001661 __d_instantiate(found, inode);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001662 spin_unlock(&dcache_inode_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001663 spin_unlock(&dcache_lock);
1664 security_d_instantiate(found, inode);
1665 return found;
1666 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001667
Barry Naujok94035402008-05-21 16:50:46 +10001668 /*
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001669 * In case a directory already has a (disconnected) entry grab a
1670 * reference to it, move it in place and use it.
Barry Naujok94035402008-05-21 16:50:46 +10001671 */
1672 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1673 dget_locked(new);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001674 spin_unlock(&dcache_inode_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001675 spin_unlock(&dcache_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001676 security_d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001677 d_move(new, found);
Barry Naujok94035402008-05-21 16:50:46 +10001678 iput(inode);
Barry Naujok94035402008-05-21 16:50:46 +10001679 dput(found);
Barry Naujok94035402008-05-21 16:50:46 +10001680 return new;
1681
1682err_out:
1683 iput(inode);
1684 return ERR_PTR(error);
1685}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001686EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001687
1688/**
1689 * d_lookup - search for a dentry
1690 * @parent: parent dentry
1691 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001692 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001693 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001694 * d_lookup searches the children of the parent dentry for the name in
1695 * question. If the dentry is found its reference count is incremented and the
1696 * dentry is returned. The caller must use dput to free the entry when it has
1697 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001698 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1700{
1701 struct dentry * dentry = NULL;
Nick Piggin949854d2011-01-07 17:49:37 +11001702 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001703
1704 do {
1705 seq = read_seqbegin(&rename_lock);
1706 dentry = __d_lookup(parent, name);
1707 if (dentry)
1708 break;
1709 } while (read_seqretry(&rename_lock, seq));
1710 return dentry;
1711}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001712EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001713
Nick Pigginb04f7842010-08-18 04:37:34 +10001714/*
1715 * __d_lookup - search for a dentry (racy)
1716 * @parent: parent dentry
1717 * @name: qstr of name we wish to find
1718 * Returns: dentry, or NULL
1719 *
1720 * __d_lookup is like d_lookup, however it may (rarely) return a
1721 * false-negative result due to unrelated rename activity.
1722 *
1723 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1724 * however it must be used carefully, eg. with a following d_lookup in
1725 * the case of failure.
1726 *
1727 * __d_lookup callers must be commented.
1728 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001729struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1730{
1731 unsigned int len = name->len;
1732 unsigned int hash = name->hash;
1733 const unsigned char *str = name->name;
1734 struct hlist_head *head = d_hash(parent,hash);
1735 struct dentry *found = NULL;
1736 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001737 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001738
Nick Pigginb04f7842010-08-18 04:37:34 +10001739 /*
1740 * The hash list is protected using RCU.
1741 *
1742 * Take d_lock when comparing a candidate dentry, to avoid races
1743 * with d_move().
1744 *
1745 * It is possible that concurrent renames can mess up our list
1746 * walk here and result in missing our dentry, resulting in the
1747 * false-negative result. d_lookup() protects against concurrent
1748 * renames using rename_lock seqlock.
1749 *
1750 * See Documentation/vfs/dcache-locking.txt for more details.
1751 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001752 rcu_read_lock();
1753
Paul E. McKenney665a7582005-11-07 00:59:17 -08001754 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001755 struct qstr *qstr;
1756
Linus Torvalds1da177e2005-04-16 15:20:36 -07001757 if (dentry->d_name.hash != hash)
1758 continue;
1759 if (dentry->d_parent != parent)
1760 continue;
1761
1762 spin_lock(&dentry->d_lock);
1763
1764 /*
1765 * Recheck the dentry after taking the lock - d_move may have
Nick Pigginb04f7842010-08-18 04:37:34 +10001766 * changed things. Don't bother checking the hash because
1767 * we're about to compare the whole name anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 */
1769 if (dentry->d_parent != parent)
1770 goto next;
1771
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001772 /* non-existing due to RCU? */
1773 if (d_unhashed(dentry))
1774 goto next;
1775
Linus Torvalds1da177e2005-04-16 15:20:36 -07001776 /*
1777 * It is safe to compare names since d_move() cannot
1778 * change the qstr (protected by d_lock).
1779 */
1780 qstr = &dentry->d_name;
1781 if (parent->d_op && parent->d_op->d_compare) {
Nick Piggin621e1552011-01-07 17:49:27 +11001782 if (parent->d_op->d_compare(parent, parent->d_inode,
1783 dentry, dentry->d_inode,
1784 qstr->len, qstr->name, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001785 goto next;
1786 } else {
1787 if (qstr->len != len)
1788 goto next;
1789 if (memcmp(qstr->name, str, len))
1790 goto next;
1791 }
1792
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001793 dentry->d_count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001794 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001795 spin_unlock(&dentry->d_lock);
1796 break;
1797next:
1798 spin_unlock(&dentry->d_lock);
1799 }
1800 rcu_read_unlock();
1801
1802 return found;
1803}
1804
1805/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001806 * d_hash_and_lookup - hash the qstr then search for a dentry
1807 * @dir: Directory to search in
1808 * @name: qstr of name we wish to find
1809 *
1810 * On hash failure or on lookup failure NULL is returned.
1811 */
1812struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1813{
1814 struct dentry *dentry = NULL;
1815
1816 /*
1817 * Check for a fs-specific hash function. Note that we must
1818 * calculate the standard hash first, as the d_op->d_hash()
1819 * routine may choose to leave the hash value unchanged.
1820 */
1821 name->hash = full_name_hash(name->name, name->len);
1822 if (dir->d_op && dir->d_op->d_hash) {
Nick Pigginb1e6a012011-01-07 17:49:28 +11001823 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001824 goto out;
1825 }
1826 dentry = d_lookup(dir, name);
1827out:
1828 return dentry;
1829}
1830
1831/**
Nick Piggin786a5e12011-01-07 17:49:16 +11001832 * d_validate - verify dentry provided from insecure source (deprecated)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001833 * @dentry: The dentry alleged to be valid child of @dparent
1834 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001835 *
1836 * An insecure source has sent us a dentry, here we verify it and dget() it.
1837 * This is used by ncpfs in its readdir implementation.
1838 * Zero is returned in the dentry is invalid.
Nick Piggin786a5e12011-01-07 17:49:16 +11001839 *
1840 * This function is slow for big directories, and deprecated, do not use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001841 */
Nick Piggind3a23e12011-01-05 20:01:21 +11001842int d_validate(struct dentry *dentry, struct dentry *dparent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001843{
Nick Piggin786a5e12011-01-07 17:49:16 +11001844 struct dentry *child;
Nick Piggind3a23e12011-01-05 20:01:21 +11001845
1846 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001847 spin_lock(&dparent->d_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001848 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1849 if (dentry == child) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001850 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
1851 __dget_locked_dlock(dentry);
1852 spin_unlock(&dentry->d_lock);
1853 spin_unlock(&dparent->d_lock);
Nick Piggind3a23e12011-01-05 20:01:21 +11001854 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855 return 1;
1856 }
1857 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11001858 spin_unlock(&dparent->d_lock);
Nick Piggind3a23e12011-01-05 20:01:21 +11001859 spin_unlock(&dcache_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001860
Linus Torvalds1da177e2005-04-16 15:20:36 -07001861 return 0;
1862}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001863EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001864
1865/*
1866 * When a file is deleted, we have two options:
1867 * - turn this dentry into a negative dentry
1868 * - unhash this dentry and free it.
1869 *
1870 * Usually, we want to just turn this into
1871 * a negative dentry, but if anybody else is
1872 * currently using the dentry or the inode
1873 * we can't do that and we fall back on removing
1874 * it from the hash queues and waiting for
1875 * it to be deleted later when it has no users
1876 */
1877
1878/**
1879 * d_delete - delete a dentry
1880 * @dentry: The dentry to delete
1881 *
1882 * Turn the dentry into a negative dentry if possible, otherwise
1883 * remove it from the hash queues so it can be deleted later
1884 */
1885
1886void d_delete(struct dentry * dentry)
1887{
John McCutchan7a91bf72005-08-08 13:52:16 -04001888 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001889 /*
1890 * Are we the only user?
1891 */
1892 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001893 spin_lock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001894 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001895 isdir = S_ISDIR(dentry->d_inode->i_mode);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001896 if (dentry->d_count == 1) {
Al Viro13e3c5e2010-05-21 16:11:04 -04001897 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001898 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001899 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001900 return;
1901 }
1902
1903 if (!d_unhashed(dentry))
1904 __d_drop(dentry);
1905
1906 spin_unlock(&dentry->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11001907 spin_unlock(&dcache_inode_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001908 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001909
1910 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001911}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001912EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1915{
1916
1917 entry->d_flags &= ~DCACHE_UNHASHED;
1918 hlist_add_head_rcu(&entry->d_hash, list);
1919}
1920
David Howells770bfad2006-08-22 20:06:07 -04001921static void _d_rehash(struct dentry * entry)
1922{
1923 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1924}
1925
Linus Torvalds1da177e2005-04-16 15:20:36 -07001926/**
1927 * d_rehash - add an entry back to the hash
1928 * @entry: dentry to add to the hash
1929 *
1930 * Adds a dentry to the hash according to its name.
1931 */
1932
1933void d_rehash(struct dentry * entry)
1934{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001935 spin_lock(&dcache_lock);
1936 spin_lock(&entry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +11001937 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04001938 _d_rehash(entry);
Nick Piggin789680d2011-01-07 17:49:30 +11001939 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001940 spin_unlock(&entry->d_lock);
1941 spin_unlock(&dcache_lock);
1942}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001943EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001944
Nick Pigginfb2d5b82011-01-07 17:49:26 +11001945/**
1946 * dentry_update_name_case - update case insensitive dentry with a new name
1947 * @dentry: dentry to be updated
1948 * @name: new name
1949 *
1950 * Update a case insensitive dentry with new case of name.
1951 *
1952 * dentry must have been returned by d_lookup with name @name. Old and new
1953 * name lengths must match (ie. no d_compare which allows mismatched name
1954 * lengths).
1955 *
1956 * Parent inode i_mutex must be held over d_lookup and into this call (to
1957 * keep renames and concurrent inserts, and readdir(2) away).
1958 */
1959void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
1960{
1961 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
1962 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
1963
1964 spin_lock(&dcache_lock);
1965 spin_lock(&dentry->d_lock);
1966 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
1967 spin_unlock(&dentry->d_lock);
1968 spin_unlock(&dcache_lock);
1969}
1970EXPORT_SYMBOL(dentry_update_name_case);
1971
Linus Torvalds1da177e2005-04-16 15:20:36 -07001972static void switch_names(struct dentry *dentry, struct dentry *target)
1973{
1974 if (dname_external(target)) {
1975 if (dname_external(dentry)) {
1976 /*
1977 * Both external: swap the pointers
1978 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001979 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001980 } else {
1981 /*
1982 * dentry:internal, target:external. Steal target's
1983 * storage and make target internal.
1984 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07001985 memcpy(target->d_iname, dentry->d_name.name,
1986 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001987 dentry->d_name.name = target->d_name.name;
1988 target->d_name.name = target->d_iname;
1989 }
1990 } else {
1991 if (dname_external(dentry)) {
1992 /*
1993 * dentry:external, target:internal. Give dentry's
1994 * storage to target and make dentry internal
1995 */
1996 memcpy(dentry->d_iname, target->d_name.name,
1997 target->d_name.len + 1);
1998 target->d_name.name = dentry->d_name.name;
1999 dentry->d_name.name = dentry->d_iname;
2000 } else {
2001 /*
2002 * Both are internal. Just copy target to dentry
2003 */
2004 memcpy(dentry->d_iname, target->d_name.name,
2005 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05002006 dentry->d_name.len = target->d_name.len;
2007 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002008 }
2009 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002010 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002011}
2012
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002013static void dentry_lock_for_move(struct dentry *dentry, struct dentry *target)
2014{
2015 /*
2016 * XXXX: do we really need to take target->d_lock?
2017 */
2018 if (IS_ROOT(dentry) || dentry->d_parent == target->d_parent)
2019 spin_lock(&target->d_parent->d_lock);
2020 else {
2021 if (d_ancestor(dentry->d_parent, target->d_parent)) {
2022 spin_lock(&dentry->d_parent->d_lock);
2023 spin_lock_nested(&target->d_parent->d_lock,
2024 DENTRY_D_LOCK_NESTED);
2025 } else {
2026 spin_lock(&target->d_parent->d_lock);
2027 spin_lock_nested(&dentry->d_parent->d_lock,
2028 DENTRY_D_LOCK_NESTED);
2029 }
2030 }
2031 if (target < dentry) {
2032 spin_lock_nested(&target->d_lock, 2);
2033 spin_lock_nested(&dentry->d_lock, 3);
2034 } else {
2035 spin_lock_nested(&dentry->d_lock, 2);
2036 spin_lock_nested(&target->d_lock, 3);
2037 }
2038}
2039
2040static void dentry_unlock_parents_for_move(struct dentry *dentry,
2041 struct dentry *target)
2042{
2043 if (target->d_parent != dentry->d_parent)
2044 spin_unlock(&dentry->d_parent->d_lock);
2045 if (target->d_parent != target)
2046 spin_unlock(&target->d_parent->d_lock);
2047}
2048
Linus Torvalds1da177e2005-04-16 15:20:36 -07002049/*
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002050 * When switching names, the actual string doesn't strictly have to
2051 * be preserved in the target - because we're dropping the target
2052 * anyway. As such, we can just do a simple memcpy() to copy over
2053 * the new name before we switch.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002054 *
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002055 * Note that we have to be a lot more careful about getting the hash
2056 * switched - we have to switch the hash value properly even if it
2057 * then no longer matches the actual (corrupted) string of the target.
2058 * The hash value has to match the hash queue that the dentry is on..
Linus Torvalds1da177e2005-04-16 15:20:36 -07002059 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002060/*
2061 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07002062 * @dentry: entry to move
2063 * @target: new dentry
2064 *
2065 * Update the dcache to reflect the move of a file name. Negative
2066 * dcache entries should not be moved in this way.
2067 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07002068static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002069{
Linus Torvalds1da177e2005-04-16 15:20:36 -07002070 if (!dentry->d_inode)
2071 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
2072
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002073 BUG_ON(d_ancestor(dentry, target));
2074 BUG_ON(d_ancestor(target, dentry));
2075
Linus Torvalds1da177e2005-04-16 15:20:36 -07002076 write_seqlock(&rename_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002077
2078 dentry_lock_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002079
2080 /* Move the dentry to the target hash queue, if on different bucket */
Nick Piggin789680d2011-01-07 17:49:30 +11002081 spin_lock(&dcache_hash_lock);
2082 if (!d_unhashed(dentry))
2083 hlist_del_rcu(&dentry->d_hash);
2084 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
2085 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002086
2087 /* Unhash the target: dput() will then get rid of it */
2088 __d_drop(target);
2089
Eric Dumazet5160ee62006-01-08 01:03:32 -08002090 list_del(&dentry->d_u.d_child);
2091 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002092
2093 /* Switch the names.. */
2094 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002095 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002096
2097 /* ... and switch the parents */
2098 if (IS_ROOT(dentry)) {
2099 dentry->d_parent = target->d_parent;
2100 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002101 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002102 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002103 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002104
2105 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08002106 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002107 }
2108
Eric Dumazet5160ee62006-01-08 01:03:32 -08002109 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002110
2111 dentry_unlock_parents_for_move(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002112 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08002113 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002114 spin_unlock(&dentry->d_lock);
2115 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002116}
2117
2118/**
2119 * d_move - move a dentry
2120 * @dentry: entry to move
2121 * @target: new dentry
2122 *
2123 * Update the dcache to reflect the move of a file name. Negative
2124 * dcache entries should not be moved in this way.
2125 */
2126
2127void d_move(struct dentry * dentry, struct dentry * target)
2128{
2129 spin_lock(&dcache_lock);
2130 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002131 spin_unlock(&dcache_lock);
2132}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002133EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002135/**
2136 * d_ancestor - search for an ancestor
2137 * @p1: ancestor dentry
2138 * @p2: child dentry
2139 *
2140 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
2141 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07002142 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002143struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002144{
2145 struct dentry *p;
2146
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09002147 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002148 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002149 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002150 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002151 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07002152}
2153
2154/*
2155 * This helper attempts to cope with remotely renamed directories
2156 *
2157 * It assumes that the caller is already holding
2158 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
2159 *
2160 * Note: If ever the locking in lock_rename() changes, then please
2161 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07002162 */
2163static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002164 __releases(dcache_lock)
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002165 __releases(dcache_inode_lock)
Trond Myklebust9eaef272006-10-21 10:24:20 -07002166{
2167 struct mutex *m1 = NULL, *m2 = NULL;
2168 struct dentry *ret;
2169
2170 /* If alias and dentry share a parent, then no extra locks required */
2171 if (alias->d_parent == dentry->d_parent)
2172 goto out_unalias;
2173
2174 /* Check for loops */
2175 ret = ERR_PTR(-ELOOP);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002176 if (d_ancestor(alias, dentry))
Trond Myklebust9eaef272006-10-21 10:24:20 -07002177 goto out_err;
2178
2179 /* See lock_rename() */
2180 ret = ERR_PTR(-EBUSY);
2181 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
2182 goto out_err;
2183 m1 = &dentry->d_sb->s_vfs_rename_mutex;
2184 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
2185 goto out_err;
2186 m2 = &alias->d_parent->d_inode->i_mutex;
2187out_unalias:
2188 d_move_locked(alias, dentry);
2189 ret = alias;
2190out_err:
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002191 spin_unlock(&dcache_inode_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002192 spin_unlock(&dcache_lock);
2193 if (m2)
2194 mutex_unlock(m2);
2195 if (m1)
2196 mutex_unlock(m1);
2197 return ret;
2198}
2199
2200/*
David Howells770bfad2006-08-22 20:06:07 -04002201 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2202 * named dentry in place of the dentry to be replaced.
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002203 * returns with anon->d_lock held!
David Howells770bfad2006-08-22 20:06:07 -04002204 */
2205static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2206{
2207 struct dentry *dparent, *aparent;
2208
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002209 dentry_lock_for_move(anon, dentry);
David Howells770bfad2006-08-22 20:06:07 -04002210
2211 dparent = dentry->d_parent;
2212 aparent = anon->d_parent;
2213
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002214 switch_names(dentry, anon);
2215 swap(dentry->d_name.hash, anon->d_name.hash);
2216
David Howells770bfad2006-08-22 20:06:07 -04002217 dentry->d_parent = (aparent == anon) ? dentry : aparent;
2218 list_del(&dentry->d_u.d_child);
2219 if (!IS_ROOT(dentry))
2220 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2221 else
2222 INIT_LIST_HEAD(&dentry->d_u.d_child);
2223
2224 anon->d_parent = (dparent == dentry) ? anon : dparent;
2225 list_del(&anon->d_u.d_child);
2226 if (!IS_ROOT(anon))
2227 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
2228 else
2229 INIT_LIST_HEAD(&anon->d_u.d_child);
2230
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002231 dentry_unlock_parents_for_move(anon, dentry);
2232 spin_unlock(&dentry->d_lock);
2233
2234 /* anon->d_lock still locked, returns locked */
David Howells770bfad2006-08-22 20:06:07 -04002235 anon->d_flags &= ~DCACHE_DISCONNECTED;
2236}
2237
2238/**
2239 * d_materialise_unique - introduce an inode into the tree
2240 * @dentry: candidate dentry
2241 * @inode: inode to bind to the dentry, to which aliases may be attached
2242 *
2243 * Introduces an dentry into the tree, substituting an extant disconnected
2244 * root directory alias in its place if there is one
2245 */
2246struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2247{
Trond Myklebust9eaef272006-10-21 10:24:20 -07002248 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04002249
2250 BUG_ON(!d_unhashed(dentry));
2251
2252 spin_lock(&dcache_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002253 spin_lock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04002254
2255 if (!inode) {
2256 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09002257 __d_instantiate(dentry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04002258 goto found_lock;
2259 }
2260
Trond Myklebust9eaef272006-10-21 10:24:20 -07002261 if (S_ISDIR(inode->i_mode)) {
2262 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04002263
Trond Myklebust9eaef272006-10-21 10:24:20 -07002264 /* Does an aliased dentry already exist? */
2265 alias = __d_find_alias(inode, 0);
2266 if (alias) {
2267 actual = alias;
2268 /* Is this an anonymous mountpoint that we could splice
2269 * into our tree? */
2270 if (IS_ROOT(alias)) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07002271 __d_materialise_dentry(dentry, alias);
2272 __d_drop(alias);
2273 goto found;
2274 }
2275 /* Nope, but we must(!) avoid directory aliasing */
2276 actual = __d_unalias(dentry, alias);
2277 if (IS_ERR(actual))
2278 dput(alias);
2279 goto out_nolock;
2280 }
David Howells770bfad2006-08-22 20:06:07 -04002281 }
2282
2283 /* Add a unique reference */
2284 actual = __d_instantiate_unique(dentry, inode);
2285 if (!actual)
2286 actual = dentry;
2287 else if (unlikely(!d_unhashed(actual)))
2288 goto shouldnt_be_hashed;
2289
2290found_lock:
2291 spin_lock(&actual->d_lock);
2292found:
Nick Piggin789680d2011-01-07 17:49:30 +11002293 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04002294 _d_rehash(actual);
Nick Piggin789680d2011-01-07 17:49:30 +11002295 spin_unlock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04002296 spin_unlock(&actual->d_lock);
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002297 spin_unlock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04002298 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002299out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04002300 if (actual == dentry) {
2301 security_d_instantiate(dentry, inode);
2302 return NULL;
2303 }
2304
2305 iput(inode);
2306 return actual;
2307
David Howells770bfad2006-08-22 20:06:07 -04002308shouldnt_be_hashed:
Nick Pigginb23fb0a2011-01-07 17:49:35 +11002309 spin_unlock(&dcache_inode_lock);
David Howells770bfad2006-08-22 20:06:07 -04002310 spin_unlock(&dcache_lock);
2311 BUG();
David Howells770bfad2006-08-22 20:06:07 -04002312}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002313EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04002314
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002315static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01002316{
2317 *buflen -= namelen;
2318 if (*buflen < 0)
2319 return -ENAMETOOLONG;
2320 *buffer -= namelen;
2321 memcpy(*buffer, str, namelen);
2322 return 0;
2323}
2324
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002325static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2326{
2327 return prepend(buffer, buflen, name->name, name->len);
2328}
2329
Linus Torvalds1da177e2005-04-16 15:20:36 -07002330/**
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002331 * Prepend path string to a buffer
2332 *
2333 * @path: the dentry/vfsmount to report
2334 * @root: root vfsmnt/dentry (may be modified by this function)
2335 * @buffer: pointer to the end of the buffer
2336 * @buflen: pointer to buffer length
2337 *
Nick Piggin949854d2011-01-07 17:49:37 +11002338 * Caller holds the rename_lock.
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002339 *
2340 * If path is not reachable from the supplied root, then the value of
2341 * root is changed (without modifying refcounts).
2342 */
2343static int prepend_path(const struct path *path, struct path *root,
2344 char **buffer, int *buflen)
2345{
2346 struct dentry *dentry = path->dentry;
2347 struct vfsmount *vfsmnt = path->mnt;
2348 bool slash = false;
2349 int error = 0;
2350
Nick Piggin99b7db72010-08-18 04:37:39 +10002351 br_read_lock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002352 while (dentry != root->dentry || vfsmnt != root->mnt) {
2353 struct dentry * parent;
2354
2355 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2356 /* Global root? */
2357 if (vfsmnt->mnt_parent == vfsmnt) {
2358 goto global_root;
2359 }
2360 dentry = vfsmnt->mnt_mountpoint;
2361 vfsmnt = vfsmnt->mnt_parent;
2362 continue;
2363 }
2364 parent = dentry->d_parent;
2365 prefetch(parent);
Nick Piggin9abca362011-01-07 17:49:36 +11002366 spin_lock(&dentry->d_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002367 error = prepend_name(buffer, buflen, &dentry->d_name);
Nick Piggin9abca362011-01-07 17:49:36 +11002368 spin_unlock(&dentry->d_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002369 if (!error)
2370 error = prepend(buffer, buflen, "/", 1);
2371 if (error)
2372 break;
2373
2374 slash = true;
2375 dentry = parent;
2376 }
2377
2378out:
2379 if (!error && !slash)
2380 error = prepend(buffer, buflen, "/", 1);
2381
Nick Piggin99b7db72010-08-18 04:37:39 +10002382 br_read_unlock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002383 return error;
2384
2385global_root:
2386 /*
2387 * Filesystems needing to implement special "root names"
2388 * should do so with ->d_dname()
2389 */
2390 if (IS_ROOT(dentry) &&
2391 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2392 WARN(1, "Root dentry has weird name <%.*s>\n",
2393 (int) dentry->d_name.len, dentry->d_name.name);
2394 }
2395 root->mnt = vfsmnt;
2396 root->dentry = dentry;
2397 goto out;
2398}
2399
2400/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002401 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002402 * @path: the dentry/vfsmount to report
2403 * @root: root vfsmnt/dentry (may be modified by this function)
Randy Dunlapcd956a12010-08-14 13:05:31 -07002404 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002405 * @buflen: buffer length
2406 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002407 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002408 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002409 * Returns a pointer into the buffer or an error code if the
2410 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002411 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002412 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002413 *
2414 * If path is not reachable from the supplied root, then the value of
2415 * root is changed (without modifying refcounts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002416 */
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002417char *__d_path(const struct path *path, struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002418 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002419{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002420 char *res = buf + buflen;
2421 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002422
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002423 prepend(&res, &buflen, "\0", 1);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002424 spin_lock(&dcache_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002425 write_seqlock(&rename_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002426 error = prepend_path(path, root, &res, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002427 write_sequnlock(&rename_lock);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002428 spin_unlock(&dcache_lock);
2429
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002430 if (error)
2431 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002432 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002433}
2434
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002435/*
2436 * same as __d_path but appends "(deleted)" for unlinked files.
2437 */
2438static int path_with_deleted(const struct path *path, struct path *root,
2439 char **buf, int *buflen)
2440{
2441 prepend(buf, buflen, "\0", 1);
2442 if (d_unlinked(path->dentry)) {
2443 int error = prepend(buf, buflen, " (deleted)", 10);
2444 if (error)
2445 return error;
2446 }
2447
2448 return prepend_path(path, root, buf, buflen);
2449}
2450
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002451static int prepend_unreachable(char **buffer, int *buflen)
2452{
2453 return prepend(buffer, buflen, "(unreachable)", 13);
2454}
2455
Jan Bluncka03a8a702008-02-14 19:38:32 -08002456/**
2457 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002458 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002459 * @buf: buffer to return value in
2460 * @buflen: buffer length
2461 *
2462 * Convert a dentry into an ASCII path name. If the entry has been deleted
2463 * the string " (deleted)" is appended. Note that this is ambiguous.
2464 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002465 * Returns a pointer into the buffer or an error code if the path was
2466 * too long. Note: Callers should use the returned pointer, not the passed
2467 * in buffer, to use the name! The implementation often starts at an offset
2468 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002469 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002470 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002471 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002472char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002473{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002474 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002475 struct path root;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002476 struct path tmp;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002477 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002478
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002479 /*
2480 * We have various synthetic filesystems that never get mounted. On
2481 * these filesystems dentries are never used for lookup purposes, and
2482 * thus don't need to be hashed. They also don't need a name until a
2483 * user wants to identify the object in /proc/pid/fd/. The little hack
2484 * below allows us to generate a name for these objects on demand:
2485 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002486 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2487 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002488
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002489 get_fs_root(current->fs, &root);
Linus Torvalds552ce542007-02-13 12:08:18 -08002490 spin_lock(&dcache_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002491 write_seqlock(&rename_lock);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002492 tmp = root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002493 error = path_with_deleted(path, &tmp, &res, &buflen);
2494 if (error)
2495 res = ERR_PTR(error);
Nick Piggin949854d2011-01-07 17:49:37 +11002496 write_sequnlock(&rename_lock);
Linus Torvalds552ce542007-02-13 12:08:18 -08002497 spin_unlock(&dcache_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002498 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002499 return res;
2500}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002501EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002502
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002503/**
2504 * d_path_with_unreachable - return the path of a dentry
2505 * @path: path to report
2506 * @buf: buffer to return value in
2507 * @buflen: buffer length
2508 *
2509 * The difference from d_path() is that this prepends "(unreachable)"
2510 * to paths which are unreachable from the current process' root.
2511 */
2512char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2513{
2514 char *res = buf + buflen;
2515 struct path root;
2516 struct path tmp;
2517 int error;
2518
2519 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2520 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2521
2522 get_fs_root(current->fs, &root);
2523 spin_lock(&dcache_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002524 write_seqlock(&rename_lock);
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002525 tmp = root;
2526 error = path_with_deleted(path, &tmp, &res, &buflen);
2527 if (!error && !path_equal(&tmp, &root))
2528 error = prepend_unreachable(&res, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002529 write_sequnlock(&rename_lock);
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002530 spin_unlock(&dcache_lock);
2531 path_put(&root);
2532 if (error)
2533 res = ERR_PTR(error);
2534
2535 return res;
2536}
2537
Linus Torvalds1da177e2005-04-16 15:20:36 -07002538/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002539 * Helper function for dentry_operations.d_dname() members
2540 */
2541char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2542 const char *fmt, ...)
2543{
2544 va_list args;
2545 char temp[64];
2546 int sz;
2547
2548 va_start(args, fmt);
2549 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2550 va_end(args);
2551
2552 if (sz > sizeof(temp) || sz > buflen)
2553 return ERR_PTR(-ENAMETOOLONG);
2554
2555 buffer += buflen - sz;
2556 return memcpy(buffer, temp, sz);
2557}
2558
2559/*
Ram Pai6092d042008-03-27 13:06:20 +01002560 * Write full pathname from the root of the filesystem into the buffer.
2561 */
Nick Pigginec2447c2011-01-07 17:49:29 +11002562static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002563{
2564 char *end = buf + buflen;
2565 char *retval;
2566
Ram Pai6092d042008-03-27 13:06:20 +01002567 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002568 if (buflen < 1)
2569 goto Elong;
2570 /* Get '/' right */
2571 retval = end-1;
2572 *retval = '/';
2573
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002574 while (!IS_ROOT(dentry)) {
2575 struct dentry *parent = dentry->d_parent;
Nick Piggin9abca362011-01-07 17:49:36 +11002576 int error;
Ram Pai6092d042008-03-27 13:06:20 +01002577
Ram Pai6092d042008-03-27 13:06:20 +01002578 prefetch(parent);
Nick Piggin9abca362011-01-07 17:49:36 +11002579 spin_lock(&dentry->d_lock);
2580 error = prepend_name(&end, &buflen, &dentry->d_name);
2581 spin_unlock(&dentry->d_lock);
2582 if (error != 0 || prepend(&end, &buflen, "/", 1) != 0)
Ram Pai6092d042008-03-27 13:06:20 +01002583 goto Elong;
2584
2585 retval = end;
2586 dentry = parent;
2587 }
Al Viroc1031352010-06-06 22:31:14 -04002588 return retval;
2589Elong:
2590 return ERR_PTR(-ENAMETOOLONG);
2591}
Nick Pigginec2447c2011-01-07 17:49:29 +11002592
2593char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2594{
2595 char *retval;
2596
2597 spin_lock(&dcache_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002598 write_seqlock(&rename_lock);
Nick Pigginec2447c2011-01-07 17:49:29 +11002599 retval = __dentry_path(dentry, buf, buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002600 write_sequnlock(&rename_lock);
Nick Pigginec2447c2011-01-07 17:49:29 +11002601 spin_unlock(&dcache_lock);
2602
2603 return retval;
2604}
2605EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04002606
2607char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2608{
2609 char *p = NULL;
2610 char *retval;
2611
2612 spin_lock(&dcache_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002613 write_seqlock(&rename_lock);
Al Viroc1031352010-06-06 22:31:14 -04002614 if (d_unlinked(dentry)) {
2615 p = buf + buflen;
2616 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2617 goto Elong;
2618 buflen++;
2619 }
2620 retval = __dentry_path(dentry, buf, buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002621 write_sequnlock(&rename_lock);
Ram Pai6092d042008-03-27 13:06:20 +01002622 spin_unlock(&dcache_lock);
Al Viroc1031352010-06-06 22:31:14 -04002623 if (!IS_ERR(retval) && p)
2624 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002625 return retval;
2626Elong:
2627 spin_unlock(&dcache_lock);
2628 return ERR_PTR(-ENAMETOOLONG);
2629}
2630
2631/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002632 * NOTE! The user-level library version returns a
2633 * character pointer. The kernel system call just
2634 * returns the length of the buffer filled (which
2635 * includes the ending '\0' character), or a negative
2636 * error value. So libc would do something like
2637 *
2638 * char *getcwd(char * buf, size_t size)
2639 * {
2640 * int retval;
2641 *
2642 * retval = sys_getcwd(buf, size);
2643 * if (retval >= 0)
2644 * return buf;
2645 * errno = -retval;
2646 * return NULL;
2647 * }
2648 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002649SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002650{
Linus Torvalds552ce542007-02-13 12:08:18 -08002651 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002652 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002653 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002654
2655 if (!page)
2656 return -ENOMEM;
2657
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002658 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002659
Linus Torvalds552ce542007-02-13 12:08:18 -08002660 error = -ENOENT;
Linus Torvalds552ce542007-02-13 12:08:18 -08002661 spin_lock(&dcache_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002662 write_seqlock(&rename_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002663 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002664 unsigned long len;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002665 struct path tmp = root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002666 char *cwd = page + PAGE_SIZE;
2667 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002669 prepend(&cwd, &buflen, "\0", 1);
2670 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
Nick Piggin949854d2011-01-07 17:49:37 +11002671 write_sequnlock(&rename_lock);
Linus Torvalds552ce542007-02-13 12:08:18 -08002672 spin_unlock(&dcache_lock);
2673
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002674 if (error)
Linus Torvalds552ce542007-02-13 12:08:18 -08002675 goto out;
2676
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002677 /* Unreachable from current root */
2678 if (!path_equal(&tmp, &root)) {
2679 error = prepend_unreachable(&cwd, &buflen);
2680 if (error)
2681 goto out;
2682 }
2683
Linus Torvalds552ce542007-02-13 12:08:18 -08002684 error = -ERANGE;
2685 len = PAGE_SIZE + page - cwd;
2686 if (len <= size) {
2687 error = len;
2688 if (copy_to_user(buf, cwd, len))
2689 error = -EFAULT;
2690 }
Nick Piggin949854d2011-01-07 17:49:37 +11002691 } else {
2692 write_sequnlock(&rename_lock);
Linus Torvalds552ce542007-02-13 12:08:18 -08002693 spin_unlock(&dcache_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002694 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002695
2696out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002697 path_put(&pwd);
2698 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002699 free_page((unsigned long) page);
2700 return error;
2701}
2702
2703/*
2704 * Test whether new_dentry is a subdirectory of old_dentry.
2705 *
2706 * Trivially implemented using the dcache structure
2707 */
2708
2709/**
2710 * is_subdir - is new dentry a subdirectory of old_dentry
2711 * @new_dentry: new dentry
2712 * @old_dentry: old dentry
2713 *
2714 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2715 * Returns 0 otherwise.
2716 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2717 */
2718
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002719int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002720{
2721 int result;
Nick Piggin949854d2011-01-07 17:49:37 +11002722 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002723
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002724 if (new_dentry == old_dentry)
2725 return 1;
2726
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002727 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002728 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002729 seq = read_seqbegin(&rename_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002730 /*
2731 * Need rcu_readlock to protect against the d_parent trashing
2732 * due to d_move
2733 */
2734 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002735 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002736 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002737 else
2738 result = 0;
Nick Piggin949854d2011-01-07 17:49:37 +11002739 rcu_read_unlock();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002740 } while (read_seqretry(&rename_lock, seq));
Linus Torvalds1da177e2005-04-16 15:20:36 -07002741
2742 return result;
2743}
2744
Al Viro2096f752010-01-30 13:16:21 -05002745int path_is_under(struct path *path1, struct path *path2)
2746{
2747 struct vfsmount *mnt = path1->mnt;
2748 struct dentry *dentry = path1->dentry;
2749 int res;
Nick Piggin99b7db72010-08-18 04:37:39 +10002750
2751 br_read_lock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002752 if (mnt != path2->mnt) {
2753 for (;;) {
2754 if (mnt->mnt_parent == mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +10002755 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002756 return 0;
2757 }
2758 if (mnt->mnt_parent == path2->mnt)
2759 break;
2760 mnt = mnt->mnt_parent;
2761 }
2762 dentry = mnt->mnt_mountpoint;
2763 }
2764 res = is_subdir(dentry, path2->dentry);
Nick Piggin99b7db72010-08-18 04:37:39 +10002765 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002766 return res;
2767}
2768EXPORT_SYMBOL(path_is_under);
2769
Linus Torvalds1da177e2005-04-16 15:20:36 -07002770void d_genocide(struct dentry *root)
2771{
Nick Piggin949854d2011-01-07 17:49:37 +11002772 struct dentry *this_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002773 struct list_head *next;
Nick Piggin949854d2011-01-07 17:49:37 +11002774 unsigned seq;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002775
Nick Piggin949854d2011-01-07 17:49:37 +11002776rename_retry:
2777 this_parent = root;
2778 seq = read_seqbegin(&rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002779 spin_lock(&dcache_lock);
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002780 spin_lock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002781repeat:
2782 next = this_parent->d_subdirs.next;
2783resume:
2784 while (next != &this_parent->d_subdirs) {
2785 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002786 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002787 next = tmp->next;
Nick Piggin949854d2011-01-07 17:49:37 +11002788
Nick Pigginda502952011-01-07 17:49:33 +11002789 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2790 if (d_unhashed(dentry) || !dentry->d_inode) {
2791 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002792 continue;
Nick Pigginda502952011-01-07 17:49:33 +11002793 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002794 if (!list_empty(&dentry->d_subdirs)) {
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002795 spin_unlock(&this_parent->d_lock);
2796 spin_release(&dentry->d_lock.dep_map, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002797 this_parent = dentry;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002798 spin_acquire(&this_parent->d_lock.dep_map, 0, 1, _RET_IP_);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002799 goto repeat;
2800 }
Nick Piggin949854d2011-01-07 17:49:37 +11002801 if (!(dentry->d_flags & DCACHE_GENOCIDE)) {
2802 dentry->d_flags |= DCACHE_GENOCIDE;
2803 dentry->d_count--;
2804 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002805 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002806 }
2807 if (this_parent != root) {
Nick Piggin949854d2011-01-07 17:49:37 +11002808 struct dentry *tmp;
2809 struct dentry *child;
2810
2811 tmp = this_parent->d_parent;
2812 if (!(this_parent->d_flags & DCACHE_GENOCIDE)) {
2813 this_parent->d_flags |= DCACHE_GENOCIDE;
2814 this_parent->d_count--;
2815 }
2816 rcu_read_lock();
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002817 spin_unlock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002818 child = this_parent;
2819 this_parent = tmp;
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002820 spin_lock(&this_parent->d_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002821 /* might go back up the wrong parent if we have had a rename
2822 * or deletion */
2823 if (this_parent != child->d_parent ||
2824 read_seqretry(&rename_lock, seq)) {
2825 spin_unlock(&this_parent->d_lock);
2826 spin_unlock(&dcache_lock);
2827 rcu_read_unlock();
2828 goto rename_retry;
2829 }
2830 rcu_read_unlock();
2831 next = child->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002832 goto resume;
2833 }
Nick Piggin2fd6b7f2011-01-07 17:49:34 +11002834 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002835 spin_unlock(&dcache_lock);
Nick Piggin949854d2011-01-07 17:49:37 +11002836 if (read_seqretry(&rename_lock, seq))
2837 goto rename_retry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002838}
2839
2840/**
2841 * find_inode_number - check for dentry with name
2842 * @dir: directory to check
2843 * @name: Name to find.
2844 *
2845 * Check whether a dentry already exists for the given name,
2846 * and return the inode number if it has an inode. Otherwise
2847 * 0 is returned.
2848 *
2849 * This routine is used to post-process directory listings for
2850 * filesystems using synthetic inode numbers, and is necessary
2851 * to keep getcwd() working.
2852 */
2853
2854ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2855{
2856 struct dentry * dentry;
2857 ino_t ino = 0;
2858
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002859 dentry = d_hash_and_lookup(dir, name);
2860 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002861 if (dentry->d_inode)
2862 ino = dentry->d_inode->i_ino;
2863 dput(dentry);
2864 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002865 return ino;
2866}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002867EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002868
2869static __initdata unsigned long dhash_entries;
2870static int __init set_dhash_entries(char *str)
2871{
2872 if (!str)
2873 return 0;
2874 dhash_entries = simple_strtoul(str, &str, 0);
2875 return 1;
2876}
2877__setup("dhash_entries=", set_dhash_entries);
2878
2879static void __init dcache_init_early(void)
2880{
2881 int loop;
2882
2883 /* If hashes are distributed across NUMA nodes, defer
2884 * hash allocation until vmalloc space is available.
2885 */
2886 if (hashdist)
2887 return;
2888
2889 dentry_hashtable =
2890 alloc_large_system_hash("Dentry cache",
2891 sizeof(struct hlist_head),
2892 dhash_entries,
2893 13,
2894 HASH_EARLY,
2895 &d_hash_shift,
2896 &d_hash_mask,
2897 0);
2898
2899 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2900 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2901}
2902
Denis Cheng74bf17c2007-10-16 23:26:30 -07002903static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002904{
2905 int loop;
2906
2907 /*
2908 * A constructor could be added for stable state like the lists,
2909 * but it is probably not worth it because of the cache nature
2910 * of the dcache.
2911 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002912 dentry_cache = KMEM_CACHE(dentry,
2913 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002914
Rusty Russell8e1f9362007-07-17 04:03:17 -07002915 register_shrinker(&dcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002916
2917 /* Hash may have been set up in dcache_init_early */
2918 if (!hashdist)
2919 return;
2920
2921 dentry_hashtable =
2922 alloc_large_system_hash("Dentry cache",
2923 sizeof(struct hlist_head),
2924 dhash_entries,
2925 13,
2926 0,
2927 &d_hash_shift,
2928 &d_hash_mask,
2929 0);
2930
2931 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2932 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2933}
2934
2935/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002936struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002937EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002938
Linus Torvalds1da177e2005-04-16 15:20:36 -07002939EXPORT_SYMBOL(d_genocide);
2940
Linus Torvalds1da177e2005-04-16 15:20:36 -07002941void __init vfs_caches_init_early(void)
2942{
2943 dcache_init_early();
2944 inode_init_early();
2945}
2946
2947void __init vfs_caches_init(unsigned long mempages)
2948{
2949 unsigned long reserve;
2950
2951 /* Base hash sizes on available memory, with a reserve equal to
2952 150% of current kernel size */
2953
2954 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2955 mempages -= reserve;
2956
2957 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002958 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002959
Denis Cheng74bf17c2007-10-16 23:26:30 -07002960 dcache_init();
2961 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002962 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07002963 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002964 bdev_cache_init();
2965 chrdev_init();
2966}