blob: ee127f9ab274119b3021d88892ae151d6fbf06fb [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
2 * fs/dcache.c
3 *
4 * Complete reimplementation
5 * (C) 1997 Thomas Schoebel-Theuer,
6 * with heavy changes by Linus Torvalds
7 */
8
9/*
10 * Notes on the allocation strategy:
11 *
12 * The dcache is a master of the icache - whenever a dcache entry
13 * exists, the inode will always exist. "iput()" is done either when
14 * the dcache entry is deleted or garbage collected.
15 */
16
Linus Torvalds1da177e2005-04-16 15:20:36 -070017#include <linux/syscalls.h>
18#include <linux/string.h>
19#include <linux/mm.h>
20#include <linux/fs.h>
John McCutchan7a91bf72005-08-08 13:52:16 -040021#include <linux/fsnotify.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include <linux/slab.h>
23#include <linux/init.h>
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include <linux/hash.h>
25#include <linux/cache.h>
26#include <linux/module.h>
27#include <linux/mount.h>
28#include <linux/file.h>
29#include <asm/uaccess.h>
30#include <linux/security.h>
31#include <linux/seqlock.h>
32#include <linux/swap.h>
33#include <linux/bootmem.h>
Al Viro5ad4e532009-03-29 19:50:06 -040034#include <linux/fs_struct.h>
Frederic Weisbecker613afbf2009-07-16 15:44:29 +020035#include <linux/hardirq.h>
David Howells07f3f052006-09-30 20:52:18 +020036#include "internal.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070037
Nick Piggin789680d2011-01-07 17:49:30 +110038/*
39 * Usage:
Nick Piggin23044502011-01-07 17:49:31 +110040 * dcache_hash_lock protects:
41 * - the dcache hash table, s_anon lists
42 * dcache_lru_lock protects:
43 * - the dcache lru lists and counters
44 * d_lock protects:
45 * - d_flags
46 * - d_name
47 * - d_lru
Nick Pigginb7ab39f2011-01-07 17:49:32 +110048 * - d_count
Nick Pigginda502952011-01-07 17:49:33 +110049 * - d_unhashed()
Nick Piggin789680d2011-01-07 17:49:30 +110050 *
51 * Ordering:
52 * dcache_lock
53 * dentry->d_lock
Nick Piggin23044502011-01-07 17:49:31 +110054 * dcache_lru_lock
Nick Piggin789680d2011-01-07 17:49:30 +110055 * dcache_hash_lock
56 *
Nick Pigginda502952011-01-07 17:49:33 +110057 * If there is an ancestor relationship:
58 * dentry->d_parent->...->d_parent->d_lock
59 * ...
60 * dentry->d_parent->d_lock
61 * dentry->d_lock
62 *
63 * If no ancestor relationship:
Nick Piggin789680d2011-01-07 17:49:30 +110064 * if (dentry1 < dentry2)
65 * dentry1->d_lock
66 * dentry2->d_lock
67 */
Eric Dumazetfa3536c2006-03-26 01:37:24 -080068int sysctl_vfs_cache_pressure __read_mostly = 100;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069EXPORT_SYMBOL_GPL(sysctl_vfs_cache_pressure);
70
Nick Piggin789680d2011-01-07 17:49:30 +110071static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_hash_lock);
Nick Piggin23044502011-01-07 17:49:31 +110072static __cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lru_lock);
Nick Piggin789680d2011-01-07 17:49:30 +110073__cacheline_aligned_in_smp DEFINE_SPINLOCK(dcache_lock);
Al Viro74c3cbe2007-07-22 08:04:18 -040074__cacheline_aligned_in_smp DEFINE_SEQLOCK(rename_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -070075
76EXPORT_SYMBOL(dcache_lock);
77
Christoph Lametere18b8902006-12-06 20:33:20 -080078static struct kmem_cache *dentry_cache __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070079
80#define DNAME_INLINE_LEN (sizeof(struct dentry)-offsetof(struct dentry,d_iname))
81
82/*
83 * This is the single most critical data structure when it comes
84 * to the dcache: the hashtable for lookups. Somebody should try
85 * to make this good - I've just made it work.
86 *
87 * This hash-function tries to avoid losing too many bits of hash
88 * information, yet avoid using a prime hash-size or similar.
89 */
90#define D_HASHBITS d_hash_shift
91#define D_HASHMASK d_hash_mask
92
Eric Dumazetfa3536c2006-03-26 01:37:24 -080093static unsigned int d_hash_mask __read_mostly;
94static unsigned int d_hash_shift __read_mostly;
95static struct hlist_head *dentry_hashtable __read_mostly;
Linus Torvalds1da177e2005-04-16 15:20:36 -070096
97/* Statistics gathering. */
98struct dentry_stat_t dentry_stat = {
99 .age_limit = 45,
100};
101
Nick Piggin3e880fb2011-01-07 17:49:19 +1100102static DEFINE_PER_CPU(unsigned int, nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400103
104#if defined(CONFIG_SYSCTL) && defined(CONFIG_PROC_FS)
Nick Piggin3e880fb2011-01-07 17:49:19 +1100105static int get_nr_dentry(void)
106{
107 int i;
108 int sum = 0;
109 for_each_possible_cpu(i)
110 sum += per_cpu(nr_dentry, i);
111 return sum < 0 ? 0 : sum;
112}
113
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400114int proc_nr_dentry(ctl_table *table, int write, void __user *buffer,
115 size_t *lenp, loff_t *ppos)
116{
Nick Piggin3e880fb2011-01-07 17:49:19 +1100117 dentry_stat.nr_dentry = get_nr_dentry();
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400118 return proc_dointvec(table, write, buffer, lenp, ppos);
119}
120#endif
121
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400122static void __d_free(struct rcu_head *head)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700123{
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400124 struct dentry *dentry = container_of(head, struct dentry, d_u.d_rcu);
125
Arjan van de Venfd217f42008-10-21 06:47:33 -0700126 WARN_ON(!list_empty(&dentry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700127 if (dname_external(dentry))
128 kfree(dentry->d_name.name);
129 kmem_cache_free(dentry_cache, dentry);
130}
131
132/*
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400133 * no dcache_lock, please.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700134 */
135static void d_free(struct dentry *dentry)
136{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100137 BUG_ON(dentry->d_count);
Nick Piggin3e880fb2011-01-07 17:49:19 +1100138 this_cpu_dec(nr_dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 if (dentry->d_op && dentry->d_op->d_release)
140 dentry->d_op->d_release(dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400141
Eric Dumazetb3423412006-12-06 20:38:48 -0800142 /* if dentry was never inserted into hash, immediate free is OK */
Akinobu Mitae8462ca2008-02-06 01:37:07 -0800143 if (hlist_unhashed(&dentry->d_hash))
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400144 __d_free(&dentry->d_u.d_rcu);
Eric Dumazetb3423412006-12-06 20:38:48 -0800145 else
Christoph Hellwig9c82ab92010-10-10 05:36:22 -0400146 call_rcu(&dentry->d_u.d_rcu, __d_free);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147}
148
149/*
150 * Release the dentry's inode, using the filesystem
151 * d_iput() operation if defined.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700152 */
Arjan van de Ven858119e2006-01-14 13:20:43 -0800153static void dentry_iput(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200154 __releases(dentry->d_lock)
155 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700156{
157 struct inode *inode = dentry->d_inode;
158 if (inode) {
159 dentry->d_inode = NULL;
160 list_del_init(&dentry->d_alias);
161 spin_unlock(&dentry->d_lock);
162 spin_unlock(&dcache_lock);
Linus Torvaldsf805fbd2005-09-19 19:54:29 -0700163 if (!inode->i_nlink)
164 fsnotify_inoderemove(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 if (dentry->d_op && dentry->d_op->d_iput)
166 dentry->d_op->d_iput(dentry, inode);
167 else
168 iput(inode);
169 } else {
170 spin_unlock(&dentry->d_lock);
171 spin_unlock(&dcache_lock);
172 }
173}
174
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700175/*
Nick Piggin23044502011-01-07 17:49:31 +1100176 * dentry_lru_(add|del|move_tail) must be called with d_lock held.
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700177 */
178static void dentry_lru_add(struct dentry *dentry)
179{
Christoph Hellwiga4633352010-10-10 05:36:26 -0400180 if (list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100181 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400182 list_add(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
183 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100184 dentry_stat.nr_unused++;
Nick Piggin23044502011-01-07 17:49:31 +1100185 spin_unlock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400186 }
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700187}
188
Nick Piggin23044502011-01-07 17:49:31 +1100189static void __dentry_lru_del(struct dentry *dentry)
190{
191 list_del_init(&dentry->d_lru);
192 dentry->d_sb->s_nr_dentry_unused--;
193 dentry_stat.nr_unused--;
194}
195
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700196static void dentry_lru_del(struct dentry *dentry)
197{
198 if (!list_empty(&dentry->d_lru)) {
Nick Piggin23044502011-01-07 17:49:31 +1100199 spin_lock(&dcache_lru_lock);
200 __dentry_lru_del(dentry);
201 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700202 }
203}
204
Christoph Hellwiga4633352010-10-10 05:36:26 -0400205static void dentry_lru_move_tail(struct dentry *dentry)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700206{
Nick Piggin23044502011-01-07 17:49:31 +1100207 spin_lock(&dcache_lru_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400208 if (list_empty(&dentry->d_lru)) {
209 list_add_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
210 dentry->d_sb->s_nr_dentry_unused++;
Nick Piggin86c87492011-01-07 17:49:18 +1100211 dentry_stat.nr_unused++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400212 } else {
213 list_move_tail(&dentry->d_lru, &dentry->d_sb->s_dentry_lru);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700214 }
Nick Piggin23044502011-01-07 17:49:31 +1100215 spin_unlock(&dcache_lru_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700216}
217
Miklos Szeredid52b9082007-05-08 00:23:46 -0700218/**
219 * d_kill - kill dentry and return parent
220 * @dentry: dentry to kill
221 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200222 * The dentry must already be unhashed and removed from the LRU.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700223 *
224 * If this is the root of the dentry tree, return NULL.
Nick Piggin23044502011-01-07 17:49:31 +1100225 *
226 * dcache_lock and d_lock must be held by caller, are dropped by d_kill.
Miklos Szeredid52b9082007-05-08 00:23:46 -0700227 */
228static struct dentry *d_kill(struct dentry *dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200229 __releases(dentry->d_lock)
230 __releases(dcache_lock)
Miklos Szeredid52b9082007-05-08 00:23:46 -0700231{
232 struct dentry *parent;
233
234 list_del(&dentry->d_u.d_child);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700235 dentry_iput(dentry);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100236 /*
237 * dentry_iput drops the locks, at which point nobody (except
238 * transient RCU lookups) can reach this dentry.
239 */
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900240 if (IS_ROOT(dentry))
241 parent = NULL;
242 else
243 parent = dentry->d_parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700244 d_free(dentry);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900245 return parent;
Miklos Szeredid52b9082007-05-08 00:23:46 -0700246}
247
Nick Piggin789680d2011-01-07 17:49:30 +1100248/**
249 * d_drop - drop a dentry
250 * @dentry: dentry to drop
251 *
252 * d_drop() unhashes the entry from the parent dentry hashes, so that it won't
253 * be found through a VFS lookup any more. Note that this is different from
254 * deleting the dentry - d_delete will try to mark the dentry negative if
255 * possible, giving a successful _negative_ lookup, while d_drop will
256 * just make the cache lookup fail.
257 *
258 * d_drop() is used mainly for stuff that wants to invalidate a dentry for some
259 * reason (NFS timeouts or autofs deletes).
260 *
261 * __d_drop requires dentry->d_lock.
262 */
263void __d_drop(struct dentry *dentry)
264{
265 if (!(dentry->d_flags & DCACHE_UNHASHED)) {
266 dentry->d_flags |= DCACHE_UNHASHED;
267 spin_lock(&dcache_hash_lock);
268 hlist_del_rcu(&dentry->d_hash);
269 spin_unlock(&dcache_hash_lock);
270 }
271}
272EXPORT_SYMBOL(__d_drop);
273
274void d_drop(struct dentry *dentry)
275{
276 spin_lock(&dcache_lock);
277 spin_lock(&dentry->d_lock);
278 __d_drop(dentry);
279 spin_unlock(&dentry->d_lock);
280 spin_unlock(&dcache_lock);
281}
282EXPORT_SYMBOL(d_drop);
283
Linus Torvalds1da177e2005-04-16 15:20:36 -0700284/*
285 * This is dput
286 *
287 * This is complicated by the fact that we do not want to put
288 * dentries that are no longer on any hash chain on the unused
289 * list: we'd much rather just get rid of them immediately.
290 *
291 * However, that implies that we have to traverse the dentry
292 * tree upwards to the parents which might _also_ now be
293 * scheduled for deletion (it may have been only waiting for
294 * its last child to go away).
295 *
296 * This tail recursion is done by hand as we don't want to depend
297 * on the compiler to always get this right (gcc generally doesn't).
298 * Real recursion would eat up our stack space.
299 */
300
301/*
302 * dput - release a dentry
303 * @dentry: dentry to release
304 *
305 * Release a dentry. This will drop the usage count and if appropriate
306 * call the dentry unlink method as well as removing it from the queues and
307 * releasing its resources. If the parent dentries were scheduled for release
308 * they too may now get deleted.
309 *
310 * no dcache lock, please.
311 */
312
313void dput(struct dentry *dentry)
314{
315 if (!dentry)
316 return;
317
318repeat:
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100319 if (dentry->d_count == 1)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700320 might_sleep();
Linus Torvalds1da177e2005-04-16 15:20:36 -0700321 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100322 if (dentry->d_count == 1) {
323 if (!spin_trylock(&dcache_lock)) {
324 /*
325 * Something of a livelock possibility we could avoid
326 * by taking dcache_lock and trying again, but we
327 * want to reduce dcache_lock anyway so this will
328 * get improved.
329 */
330 spin_unlock(&dentry->d_lock);
331 goto repeat;
332 }
333 }
334 dentry->d_count--;
335 if (dentry->d_count) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700336 spin_unlock(&dentry->d_lock);
337 spin_unlock(&dcache_lock);
338 return;
339 }
340
341 /*
342 * AV: ->d_delete() is _NOT_ allowed to block now.
343 */
344 if (dentry->d_op && dentry->d_op->d_delete) {
345 if (dentry->d_op->d_delete(dentry))
346 goto unhash_it;
347 }
Nick Piggin265ac902010-10-10 05:36:24 -0400348
Linus Torvalds1da177e2005-04-16 15:20:36 -0700349 /* Unreachable? Get rid of it */
350 if (d_unhashed(dentry))
351 goto kill_it;
Nick Piggin265ac902010-10-10 05:36:24 -0400352
353 /* Otherwise leave it cached and ensure it's on the LRU */
354 dentry->d_flags |= DCACHE_REFERENCED;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400355 dentry_lru_add(dentry);
Nick Piggin265ac902010-10-10 05:36:24 -0400356
Linus Torvalds1da177e2005-04-16 15:20:36 -0700357 spin_unlock(&dentry->d_lock);
358 spin_unlock(&dcache_lock);
359 return;
360
361unhash_it:
362 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700363kill_it:
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700364 /* if dentry was on the d_lru list delete it from there */
365 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700366 dentry = d_kill(dentry);
367 if (dentry)
368 goto repeat;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700369}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700370EXPORT_SYMBOL(dput);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700371
372/**
373 * d_invalidate - invalidate a dentry
374 * @dentry: dentry to invalidate
375 *
376 * Try to invalidate the dentry if it turns out to be
377 * possible. If there are other dentries that can be
378 * reached through this one we can't delete it and we
379 * return -EBUSY. On success we return 0.
380 *
381 * no dcache lock.
382 */
383
384int d_invalidate(struct dentry * dentry)
385{
386 /*
387 * If it's already been dropped, return OK.
388 */
389 spin_lock(&dcache_lock);
Nick Pigginda502952011-01-07 17:49:33 +1100390 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700391 if (d_unhashed(dentry)) {
Nick Pigginda502952011-01-07 17:49:33 +1100392 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700393 spin_unlock(&dcache_lock);
394 return 0;
395 }
396 /*
397 * Check whether to do a partial shrink_dcache
398 * to get rid of unused child entries.
399 */
400 if (!list_empty(&dentry->d_subdirs)) {
Nick Pigginda502952011-01-07 17:49:33 +1100401 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700402 spin_unlock(&dcache_lock);
403 shrink_dcache_parent(dentry);
404 spin_lock(&dcache_lock);
Nick Pigginda502952011-01-07 17:49:33 +1100405 spin_lock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700406 }
407
408 /*
409 * Somebody else still using it?
410 *
411 * If it's a directory, we can't drop it
412 * for fear of somebody re-populating it
413 * with children (even though dropping it
414 * would make it unreachable from the root,
415 * we might still populate it if it was a
416 * working directory or similar).
417 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100418 if (dentry->d_count > 1) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700419 if (dentry->d_inode && S_ISDIR(dentry->d_inode->i_mode)) {
420 spin_unlock(&dentry->d_lock);
421 spin_unlock(&dcache_lock);
422 return -EBUSY;
423 }
424 }
425
426 __d_drop(dentry);
427 spin_unlock(&dentry->d_lock);
428 spin_unlock(&dcache_lock);
429 return 0;
430}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700431EXPORT_SYMBOL(d_invalidate);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700432
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100433/* This must be called with dcache_lock and d_lock held */
Nick Piggin23044502011-01-07 17:49:31 +1100434static inline struct dentry * __dget_locked_dlock(struct dentry *dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700435{
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100436 dentry->d_count++;
Christoph Hellwiga4633352010-10-10 05:36:26 -0400437 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700438 return dentry;
439}
440
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100441/* This should be called _only_ with dcache_lock held */
Nick Piggin23044502011-01-07 17:49:31 +1100442static inline struct dentry * __dget_locked(struct dentry *dentry)
443{
Nick Piggin23044502011-01-07 17:49:31 +1100444 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100445 __dget_locked_dlock(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100446 spin_unlock(&dentry->d_lock);
447 return dentry;
448}
449
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100450struct dentry * dget_locked_dlock(struct dentry *dentry)
451{
452 return __dget_locked_dlock(dentry);
453}
454
Linus Torvalds1da177e2005-04-16 15:20:36 -0700455struct dentry * dget_locked(struct dentry *dentry)
456{
457 return __dget_locked(dentry);
458}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700459EXPORT_SYMBOL(dget_locked);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700460
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100461struct dentry *dget_parent(struct dentry *dentry)
462{
463 struct dentry *ret;
464
465repeat:
466 spin_lock(&dentry->d_lock);
467 ret = dentry->d_parent;
468 if (!ret)
469 goto out;
470 if (dentry == ret) {
471 ret->d_count++;
472 goto out;
473 }
474 if (!spin_trylock(&ret->d_lock)) {
475 spin_unlock(&dentry->d_lock);
476 cpu_relax();
477 goto repeat;
478 }
479 BUG_ON(!ret->d_count);
480 ret->d_count++;
481 spin_unlock(&ret->d_lock);
482out:
483 spin_unlock(&dentry->d_lock);
484 return ret;
485}
486EXPORT_SYMBOL(dget_parent);
487
Linus Torvalds1da177e2005-04-16 15:20:36 -0700488/**
489 * d_find_alias - grab a hashed alias of inode
490 * @inode: inode in question
491 * @want_discon: flag, used by d_splice_alias, to request
492 * that only a DISCONNECTED alias be returned.
493 *
494 * If inode has a hashed alias, or is a directory and has any alias,
495 * acquire the reference to alias and return it. Otherwise return NULL.
496 * Notice that if inode is a directory there can be only one alias and
497 * it can be unhashed only if it has no children, or if it is the root
498 * of a filesystem.
499 *
NeilBrown21c0d8f2006-10-04 02:16:16 -0700500 * If the inode has an IS_ROOT, DCACHE_DISCONNECTED alias, then prefer
Linus Torvalds1da177e2005-04-16 15:20:36 -0700501 * any other hashed alias over that one unless @want_discon is set,
NeilBrown21c0d8f2006-10-04 02:16:16 -0700502 * in which case only return an IS_ROOT, DCACHE_DISCONNECTED alias.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700503 */
Nick Pigginda502952011-01-07 17:49:33 +1100504static struct dentry *__d_find_alias(struct inode *inode, int want_discon)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700505{
Nick Pigginda502952011-01-07 17:49:33 +1100506 struct dentry *alias, *discon_alias;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700507
Nick Pigginda502952011-01-07 17:49:33 +1100508again:
509 discon_alias = NULL;
510 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
511 spin_lock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700512 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
NeilBrown21c0d8f2006-10-04 02:16:16 -0700513 if (IS_ROOT(alias) &&
Nick Pigginda502952011-01-07 17:49:33 +1100514 (alias->d_flags & DCACHE_DISCONNECTED)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700515 discon_alias = alias;
Nick Pigginda502952011-01-07 17:49:33 +1100516 } else if (!want_discon) {
517 __dget_locked_dlock(alias);
518 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700519 return alias;
520 }
521 }
Nick Pigginda502952011-01-07 17:49:33 +1100522 spin_unlock(&alias->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700523 }
Nick Pigginda502952011-01-07 17:49:33 +1100524 if (discon_alias) {
525 alias = discon_alias;
526 spin_lock(&alias->d_lock);
527 if (S_ISDIR(inode->i_mode) || !d_unhashed(alias)) {
528 if (IS_ROOT(alias) &&
529 (alias->d_flags & DCACHE_DISCONNECTED)) {
530 __dget_locked_dlock(alias);
531 spin_unlock(&alias->d_lock);
532 return alias;
533 }
534 }
535 spin_unlock(&alias->d_lock);
536 goto again;
537 }
538 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700539}
540
Nick Pigginda502952011-01-07 17:49:33 +1100541struct dentry *d_find_alias(struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700542{
David Howells214fda12006-03-25 03:06:36 -0800543 struct dentry *de = NULL;
544
545 if (!list_empty(&inode->i_dentry)) {
546 spin_lock(&dcache_lock);
547 de = __d_find_alias(inode, 0);
548 spin_unlock(&dcache_lock);
549 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700550 return de;
551}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700552EXPORT_SYMBOL(d_find_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700553
554/*
555 * Try to kill dentries associated with this inode.
556 * WARNING: you must own a reference to inode.
557 */
558void d_prune_aliases(struct inode *inode)
559{
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700560 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700561restart:
562 spin_lock(&dcache_lock);
Domen Puncer0cdca3f2005-09-10 00:27:07 -0700563 list_for_each_entry(dentry, &inode->i_dentry, d_alias) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700564 spin_lock(&dentry->d_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100565 if (!dentry->d_count) {
Nick Piggin23044502011-01-07 17:49:31 +1100566 __dget_locked_dlock(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700567 __d_drop(dentry);
568 spin_unlock(&dentry->d_lock);
569 spin_unlock(&dcache_lock);
570 dput(dentry);
571 goto restart;
572 }
573 spin_unlock(&dentry->d_lock);
574 }
575 spin_unlock(&dcache_lock);
576}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700577EXPORT_SYMBOL(d_prune_aliases);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700578
579/*
Andrew Mortond702ccb2006-06-22 14:47:31 -0700580 * Throw away a dentry - free the inode, dput the parent. This requires that
581 * the LRU list has already been removed.
582 *
Miklos Szeredi85864e12007-10-16 23:27:09 -0700583 * Try to prune ancestors as well. This is necessary to prevent
584 * quadratic behavior of shrink_dcache_parent(), but is also expected
585 * to be beneficial in reducing dentry cache fragmentation.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700586 */
Miklos Szeredi85864e12007-10-16 23:27:09 -0700587static void prune_one_dentry(struct dentry * dentry)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +0200588 __releases(dentry->d_lock)
589 __releases(dcache_lock)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700590{
Linus Torvalds1da177e2005-04-16 15:20:36 -0700591 __d_drop(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700592 dentry = d_kill(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700593
594 /*
595 * Prune ancestors. Locking is simpler than in dput(),
596 * because dcache_lock needs to be taken anyway.
597 */
Miklos Szeredid52b9082007-05-08 00:23:46 -0700598 while (dentry) {
Nick Piggin23044502011-01-07 17:49:31 +1100599 spin_lock(&dcache_lock);
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100600 spin_lock(&dentry->d_lock);
601 dentry->d_count--;
602 if (dentry->d_count) {
603 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100604 spin_unlock(&dcache_lock);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700605 return;
Nick Piggin23044502011-01-07 17:49:31 +1100606 }
Miklos Szeredid52b9082007-05-08 00:23:46 -0700607
Christoph Hellwiga4633352010-10-10 05:36:26 -0400608 dentry_lru_del(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700609 __d_drop(dentry);
610 dentry = d_kill(dentry);
Miklos Szeredid52b9082007-05-08 00:23:46 -0700611 }
Linus Torvalds1da177e2005-04-16 15:20:36 -0700612}
613
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400614static void shrink_dentry_list(struct list_head *list)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700615{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700616 struct dentry *dentry;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700617
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400618 while (!list_empty(list)) {
619 dentry = list_entry(list->prev, struct dentry, d_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100620
621 if (!spin_trylock(&dentry->d_lock)) {
622 spin_unlock(&dcache_lru_lock);
623 cpu_relax();
624 spin_lock(&dcache_lru_lock);
625 continue;
626 }
627
628 __dentry_lru_del(dentry);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400629
Linus Torvalds1da177e2005-04-16 15:20:36 -0700630 /*
631 * We found an inuse dentry which was not removed from
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700632 * the LRU because of laziness during lookup. Do not free
633 * it - just keep it off the LRU list.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700634 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100635 if (dentry->d_count) {
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700636 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700637 continue;
638 }
Nick Piggin23044502011-01-07 17:49:31 +1100639 spin_unlock(&dcache_lru_lock);
640
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700641 prune_one_dentry(dentry);
Nick Piggin23044502011-01-07 17:49:31 +1100642 /* dcache_lock and dentry->d_lock dropped */
643 spin_lock(&dcache_lock);
644 spin_lock(&dcache_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700645 }
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400646}
647
648/**
649 * __shrink_dcache_sb - shrink the dentry LRU on a given superblock
650 * @sb: superblock to shrink dentry LRU.
651 * @count: number of entries to prune
652 * @flags: flags to control the dentry processing
653 *
654 * If flags contains DCACHE_REFERENCED reference dentries will not be pruned.
655 */
656static void __shrink_dcache_sb(struct super_block *sb, int *count, int flags)
657{
658 /* called from prune_dcache() and shrink_dcache_parent() */
659 struct dentry *dentry;
660 LIST_HEAD(referenced);
661 LIST_HEAD(tmp);
662 int cnt = *count;
663
664 spin_lock(&dcache_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100665relock:
666 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400667 while (!list_empty(&sb->s_dentry_lru)) {
668 dentry = list_entry(sb->s_dentry_lru.prev,
669 struct dentry, d_lru);
670 BUG_ON(dentry->d_sb != sb);
671
Nick Piggin23044502011-01-07 17:49:31 +1100672 if (!spin_trylock(&dentry->d_lock)) {
673 spin_unlock(&dcache_lru_lock);
674 cpu_relax();
675 goto relock;
676 }
677
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400678 /*
679 * If we are honouring the DCACHE_REFERENCED flag and the
680 * dentry has this flag set, don't free it. Clear the flag
681 * and put it back on the LRU.
682 */
Nick Piggin23044502011-01-07 17:49:31 +1100683 if (flags & DCACHE_REFERENCED &&
684 dentry->d_flags & DCACHE_REFERENCED) {
685 dentry->d_flags &= ~DCACHE_REFERENCED;
686 list_move(&dentry->d_lru, &referenced);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400687 spin_unlock(&dentry->d_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100688 } else {
689 list_move_tail(&dentry->d_lru, &tmp);
690 spin_unlock(&dentry->d_lock);
691 if (!--cnt)
692 break;
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400693 }
Nick Piggin23044502011-01-07 17:49:31 +1100694 /* XXX: re-add cond_resched_lock when dcache_lock goes away */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400695 }
696
697 *count = cnt;
698 shrink_dentry_list(&tmp);
699
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700700 if (!list_empty(&referenced))
701 list_splice(&referenced, &sb->s_dentry_lru);
Nick Piggin23044502011-01-07 17:49:31 +1100702 spin_unlock(&dcache_lru_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700703 spin_unlock(&dcache_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400704
Linus Torvalds1da177e2005-04-16 15:20:36 -0700705}
706
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700707/**
708 * prune_dcache - shrink the dcache
709 * @count: number of entries to try to free
Linus Torvalds1da177e2005-04-16 15:20:36 -0700710 *
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700711 * Shrink the dcache. This is done when we need more memory, or simply when we
712 * need to unmount something (at which point we need to unuse all dentries).
713 *
714 * This function may fail to free any resources if all the dentries are in use.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700715 */
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700716static void prune_dcache(int count)
717{
Al Virodca33252010-07-25 02:31:46 +0400718 struct super_block *sb, *p = NULL;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700719 int w_count;
Nick Piggin86c87492011-01-07 17:49:18 +1100720 int unused = dentry_stat.nr_unused;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700721 int prune_ratio;
722 int pruned;
723
724 if (unused == 0 || count == 0)
725 return;
726 spin_lock(&dcache_lock);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700727 if (count >= unused)
728 prune_ratio = 1;
729 else
730 prune_ratio = unused / count;
731 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400732 list_for_each_entry(sb, &super_blocks, s_list) {
Al Viro551de6f2010-03-22 19:36:35 -0400733 if (list_empty(&sb->s_instances))
734 continue;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700735 if (sb->s_nr_dentry_unused == 0)
736 continue;
737 sb->s_count++;
738 /* Now, we reclaim unused dentrins with fairness.
739 * We reclaim them same percentage from each superblock.
740 * We calculate number of dentries to scan on this sb
741 * as follows, but the implementation is arranged to avoid
742 * overflows:
743 * number of dentries to scan on this sb =
744 * count * (number of dentries on this sb /
745 * number of dentries in the machine)
746 */
747 spin_unlock(&sb_lock);
748 if (prune_ratio != 1)
749 w_count = (sb->s_nr_dentry_unused / prune_ratio) + 1;
750 else
751 w_count = sb->s_nr_dentry_unused;
752 pruned = w_count;
753 /*
754 * We need to be sure this filesystem isn't being unmounted,
755 * otherwise we could race with generic_shutdown_super(), and
756 * end up holding a reference to an inode while the filesystem
757 * is unmounted. So we try to get s_umount, and make sure
758 * s_root isn't NULL.
759 */
760 if (down_read_trylock(&sb->s_umount)) {
761 if ((sb->s_root != NULL) &&
762 (!list_empty(&sb->s_dentry_lru))) {
763 spin_unlock(&dcache_lock);
764 __shrink_dcache_sb(sb, &w_count,
765 DCACHE_REFERENCED);
766 pruned -= w_count;
767 spin_lock(&dcache_lock);
768 }
769 up_read(&sb->s_umount);
770 }
771 spin_lock(&sb_lock);
Al Virodca33252010-07-25 02:31:46 +0400772 if (p)
773 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700774 count -= pruned;
Al Virodca33252010-07-25 02:31:46 +0400775 p = sb;
Al Viro79893c12010-03-22 20:27:55 -0400776 /* more work left to do? */
777 if (count <= 0)
778 break;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700779 }
Al Virodca33252010-07-25 02:31:46 +0400780 if (p)
781 __put_super(p);
Kentaro Makitada3bbdd2008-07-23 21:27:13 -0700782 spin_unlock(&sb_lock);
783 spin_unlock(&dcache_lock);
784}
Linus Torvalds1da177e2005-04-16 15:20:36 -0700785
786/**
787 * shrink_dcache_sb - shrink dcache for a superblock
788 * @sb: superblock
789 *
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400790 * Shrink the dcache for the specified super block. This is used to free
791 * the dcache before unmounting a file system.
Linus Torvalds1da177e2005-04-16 15:20:36 -0700792 */
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400793void shrink_dcache_sb(struct super_block *sb)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700794{
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400795 LIST_HEAD(tmp);
796
797 spin_lock(&dcache_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100798 spin_lock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400799 while (!list_empty(&sb->s_dentry_lru)) {
800 list_splice_init(&sb->s_dentry_lru, &tmp);
801 shrink_dentry_list(&tmp);
802 }
Nick Piggin23044502011-01-07 17:49:31 +1100803 spin_unlock(&dcache_lru_lock);
Christoph Hellwig3049cfe2010-10-10 05:36:25 -0400804 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700805}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700806EXPORT_SYMBOL(shrink_dcache_sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700807
808/*
David Howellsc636ebd2006-10-11 01:22:19 -0700809 * destroy a single subtree of dentries for unmount
810 * - see the comments on shrink_dcache_for_umount() for a description of the
811 * locking
812 */
813static void shrink_dcache_for_umount_subtree(struct dentry *dentry)
814{
815 struct dentry *parent;
David Howellsf8713572006-10-28 10:38:46 -0700816 unsigned detached = 0;
David Howellsc636ebd2006-10-11 01:22:19 -0700817
818 BUG_ON(!IS_ROOT(dentry));
819
820 /* detach this root from the system */
821 spin_lock(&dcache_lock);
Nick Piggin23044502011-01-07 17:49:31 +1100822 spin_lock(&dentry->d_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400823 dentry_lru_del(dentry);
David Howellsc636ebd2006-10-11 01:22:19 -0700824 __d_drop(dentry);
Nick Pigginda502952011-01-07 17:49:33 +1100825 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700826 spin_unlock(&dcache_lock);
827
828 for (;;) {
829 /* descend to the first leaf in the current subtree */
830 while (!list_empty(&dentry->d_subdirs)) {
831 struct dentry *loop;
832
833 /* this is a branch with children - detach all of them
834 * from the system in one go */
835 spin_lock(&dcache_lock);
836 list_for_each_entry(loop, &dentry->d_subdirs,
837 d_u.d_child) {
Nick Piggin23044502011-01-07 17:49:31 +1100838 spin_lock(&loop->d_lock);
Christoph Hellwiga4633352010-10-10 05:36:26 -0400839 dentry_lru_del(loop);
David Howellsc636ebd2006-10-11 01:22:19 -0700840 __d_drop(loop);
Nick Pigginda502952011-01-07 17:49:33 +1100841 spin_unlock(&loop->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700842 cond_resched_lock(&dcache_lock);
843 }
844 spin_unlock(&dcache_lock);
845
846 /* move to the first child */
847 dentry = list_entry(dentry->d_subdirs.next,
848 struct dentry, d_u.d_child);
849 }
850
851 /* consume the dentries from this leaf up through its parents
852 * until we find one with children or run out altogether */
853 do {
854 struct inode *inode;
855
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100856 if (dentry->d_count != 0) {
David Howellsc636ebd2006-10-11 01:22:19 -0700857 printk(KERN_ERR
858 "BUG: Dentry %p{i=%lx,n=%s}"
859 " still in use (%d)"
860 " [unmount of %s %s]\n",
861 dentry,
862 dentry->d_inode ?
863 dentry->d_inode->i_ino : 0UL,
864 dentry->d_name.name,
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100865 dentry->d_count,
David Howellsc636ebd2006-10-11 01:22:19 -0700866 dentry->d_sb->s_type->name,
867 dentry->d_sb->s_id);
868 BUG();
869 }
870
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900871 if (IS_ROOT(dentry))
David Howellsc636ebd2006-10-11 01:22:19 -0700872 parent = NULL;
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900873 else {
874 parent = dentry->d_parent;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100875 spin_lock(&parent->d_lock);
876 parent->d_count--;
877 spin_unlock(&parent->d_lock);
OGAWA Hirofumi871c0062008-10-16 07:50:27 +0900878 }
David Howellsc636ebd2006-10-11 01:22:19 -0700879
880 list_del(&dentry->d_u.d_child);
David Howellsf8713572006-10-28 10:38:46 -0700881 detached++;
David Howellsc636ebd2006-10-11 01:22:19 -0700882
883 inode = dentry->d_inode;
884 if (inode) {
885 dentry->d_inode = NULL;
886 list_del_init(&dentry->d_alias);
887 if (dentry->d_op && dentry->d_op->d_iput)
888 dentry->d_op->d_iput(dentry, inode);
889 else
890 iput(inode);
891 }
892
893 d_free(dentry);
894
895 /* finished when we fall off the top of the tree,
896 * otherwise we ascend to the parent and move to the
897 * next sibling if there is one */
898 if (!parent)
Christoph Hellwig312d3ca2010-10-10 05:36:23 -0400899 return;
David Howellsc636ebd2006-10-11 01:22:19 -0700900 dentry = parent;
David Howellsc636ebd2006-10-11 01:22:19 -0700901 } while (list_empty(&dentry->d_subdirs));
902
903 dentry = list_entry(dentry->d_subdirs.next,
904 struct dentry, d_u.d_child);
905 }
906}
907
908/*
909 * destroy the dentries attached to a superblock on unmounting
910 * - we don't need to use dentry->d_lock, and only need dcache_lock when
911 * removing the dentry from the system lists and hashes because:
912 * - the superblock is detached from all mountings and open files, so the
913 * dentry trees will not be rearranged by the VFS
914 * - s_umount is write-locked, so the memory pressure shrinker will ignore
915 * any dentries belonging to this superblock that it comes across
916 * - the filesystem itself is no longer permitted to rearrange the dentries
917 * in this superblock
918 */
919void shrink_dcache_for_umount(struct super_block *sb)
920{
921 struct dentry *dentry;
922
923 if (down_read_trylock(&sb->s_umount))
924 BUG();
925
926 dentry = sb->s_root;
927 sb->s_root = NULL;
Nick Pigginb7ab39f2011-01-07 17:49:32 +1100928 spin_lock(&dentry->d_lock);
929 dentry->d_count--;
930 spin_unlock(&dentry->d_lock);
David Howellsc636ebd2006-10-11 01:22:19 -0700931 shrink_dcache_for_umount_subtree(dentry);
932
933 while (!hlist_empty(&sb->s_anon)) {
934 dentry = hlist_entry(sb->s_anon.first, struct dentry, d_hash);
935 shrink_dcache_for_umount_subtree(dentry);
936 }
937}
938
939/*
Linus Torvalds1da177e2005-04-16 15:20:36 -0700940 * Search for at least 1 mount point in the dentry's subdirs.
941 * We descend to the next level whenever the d_subdirs
942 * list is non-empty and continue searching.
943 */
944
945/**
946 * have_submounts - check for mounts over a dentry
947 * @parent: dentry to check.
948 *
949 * Return true if the parent or its subdirectories contain
950 * a mount point
951 */
952
953int have_submounts(struct dentry *parent)
954{
955 struct dentry *this_parent = parent;
956 struct list_head *next;
957
958 spin_lock(&dcache_lock);
959 if (d_mountpoint(parent))
960 goto positive;
961repeat:
962 next = this_parent->d_subdirs.next;
963resume:
964 while (next != &this_parent->d_subdirs) {
965 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -0800966 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700967 next = tmp->next;
968 /* Have we found a mount point ? */
969 if (d_mountpoint(dentry))
970 goto positive;
971 if (!list_empty(&dentry->d_subdirs)) {
972 this_parent = dentry;
973 goto repeat;
974 }
975 }
976 /*
977 * All done at this level ... ascend and resume the search.
978 */
979 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -0800980 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700981 this_parent = this_parent->d_parent;
982 goto resume;
983 }
984 spin_unlock(&dcache_lock);
985 return 0; /* No mount points found in tree */
986positive:
987 spin_unlock(&dcache_lock);
988 return 1;
989}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -0700990EXPORT_SYMBOL(have_submounts);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700991
992/*
993 * Search the dentry child list for the specified parent,
994 * and move any unused dentries to the end of the unused
995 * list for prune_dcache(). We descend to the next level
996 * whenever the d_subdirs list is non-empty and continue
997 * searching.
998 *
999 * It returns zero iff there are no unused children,
1000 * otherwise it returns the number of children moved to
1001 * the end of the unused list. This may not be the total
1002 * number of unused children, because select_parent can
1003 * drop the lock and return early due to latency
1004 * constraints.
1005 */
1006static int select_parent(struct dentry * parent)
1007{
1008 struct dentry *this_parent = parent;
1009 struct list_head *next;
1010 int found = 0;
1011
1012 spin_lock(&dcache_lock);
1013repeat:
1014 next = this_parent->d_subdirs.next;
1015resume:
1016 while (next != &this_parent->d_subdirs) {
1017 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001018 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001019 next = tmp->next;
1020
Nick Piggin23044502011-01-07 17:49:31 +11001021 spin_lock(&dentry->d_lock);
1022
Linus Torvalds1da177e2005-04-16 15:20:36 -07001023 /*
1024 * move only zero ref count dentries to the end
1025 * of the unused list for prune_dcache
1026 */
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001027 if (!dentry->d_count) {
Christoph Hellwiga4633352010-10-10 05:36:26 -04001028 dentry_lru_move_tail(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001029 found++;
Christoph Hellwiga4633352010-10-10 05:36:26 -04001030 } else {
1031 dentry_lru_del(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001032 }
1033
Nick Piggin23044502011-01-07 17:49:31 +11001034 spin_unlock(&dentry->d_lock);
1035
Linus Torvalds1da177e2005-04-16 15:20:36 -07001036 /*
1037 * We can return to the caller if we have found some (this
1038 * ensures forward progress). We'll be coming back to find
1039 * the rest.
1040 */
1041 if (found && need_resched())
1042 goto out;
1043
1044 /*
1045 * Descend a level if the d_subdirs list is non-empty.
1046 */
1047 if (!list_empty(&dentry->d_subdirs)) {
1048 this_parent = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001049 goto repeat;
1050 }
1051 }
1052 /*
1053 * All done at this level ... ascend and resume the search.
1054 */
1055 if (this_parent != parent) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001056 next = this_parent->d_u.d_child.next;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001057 this_parent = this_parent->d_parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001058 goto resume;
1059 }
1060out:
1061 spin_unlock(&dcache_lock);
1062 return found;
1063}
1064
1065/**
1066 * shrink_dcache_parent - prune dcache
1067 * @parent: parent of entries to prune
1068 *
1069 * Prune the dcache to remove unused children of the parent dentry.
1070 */
1071
1072void shrink_dcache_parent(struct dentry * parent)
1073{
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001074 struct super_block *sb = parent->d_sb;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001075 int found;
1076
1077 while ((found = select_parent(parent)) != 0)
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001078 __shrink_dcache_sb(sb, &found, 0);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001079}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001080EXPORT_SYMBOL(shrink_dcache_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001081
Linus Torvalds1da177e2005-04-16 15:20:36 -07001082/*
1083 * Scan `nr' dentries and return the number which remain.
1084 *
1085 * We need to avoid reentering the filesystem if the caller is performing a
1086 * GFP_NOFS allocation attempt. One example deadlock is:
1087 *
1088 * ext2_new_block->getblk->GFP->shrink_dcache_memory->prune_dcache->
1089 * prune_one_dentry->dput->dentry_iput->iput->inode->i_sb->s_op->put_inode->
1090 * ext2_discard_prealloc->ext2_free_blocks->lock_super->DEADLOCK.
1091 *
1092 * In this case we return -1 to tell the caller that we baled.
1093 */
Dave Chinner7f8275d2010-07-19 14:56:17 +10001094static int shrink_dcache_memory(struct shrinker *shrink, int nr, gfp_t gfp_mask)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001095{
1096 if (nr) {
1097 if (!(gfp_mask & __GFP_FS))
1098 return -1;
Kentaro Makitada3bbdd2008-07-23 21:27:13 -07001099 prune_dcache(nr);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001100 }
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001101
Nick Piggin86c87492011-01-07 17:49:18 +11001102 return (dentry_stat.nr_unused / 100) * sysctl_vfs_cache_pressure;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001103}
1104
Rusty Russell8e1f9362007-07-17 04:03:17 -07001105static struct shrinker dcache_shrinker = {
1106 .shrink = shrink_dcache_memory,
1107 .seeks = DEFAULT_SEEKS,
1108};
1109
Linus Torvalds1da177e2005-04-16 15:20:36 -07001110/**
1111 * d_alloc - allocate a dcache entry
1112 * @parent: parent of entry to allocate
1113 * @name: qstr of the name
1114 *
1115 * Allocates a dentry. It returns %NULL if there is insufficient memory
1116 * available. On a success the dentry is returned. The name passed in is
1117 * copied and the copy passed in may be reused after this call.
1118 */
1119
1120struct dentry *d_alloc(struct dentry * parent, const struct qstr *name)
1121{
1122 struct dentry *dentry;
1123 char *dname;
1124
Mel Gormane12ba742007-10-16 01:25:52 -07001125 dentry = kmem_cache_alloc(dentry_cache, GFP_KERNEL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001126 if (!dentry)
1127 return NULL;
1128
1129 if (name->len > DNAME_INLINE_LEN-1) {
1130 dname = kmalloc(name->len + 1, GFP_KERNEL);
1131 if (!dname) {
1132 kmem_cache_free(dentry_cache, dentry);
1133 return NULL;
1134 }
1135 } else {
1136 dname = dentry->d_iname;
1137 }
1138 dentry->d_name.name = dname;
1139
1140 dentry->d_name.len = name->len;
1141 dentry->d_name.hash = name->hash;
1142 memcpy(dname, name->name, name->len);
1143 dname[name->len] = 0;
1144
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001145 dentry->d_count = 1;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001146 dentry->d_flags = DCACHE_UNHASHED;
1147 spin_lock_init(&dentry->d_lock);
1148 dentry->d_inode = NULL;
1149 dentry->d_parent = NULL;
1150 dentry->d_sb = NULL;
1151 dentry->d_op = NULL;
1152 dentry->d_fsdata = NULL;
1153 dentry->d_mounted = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001154 INIT_HLIST_NODE(&dentry->d_hash);
1155 INIT_LIST_HEAD(&dentry->d_lru);
1156 INIT_LIST_HEAD(&dentry->d_subdirs);
1157 INIT_LIST_HEAD(&dentry->d_alias);
1158
1159 if (parent) {
1160 dentry->d_parent = dget(parent);
1161 dentry->d_sb = parent->d_sb;
1162 } else {
Eric Dumazet5160ee62006-01-08 01:03:32 -08001163 INIT_LIST_HEAD(&dentry->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001164 }
1165
1166 spin_lock(&dcache_lock);
1167 if (parent)
Eric Dumazet5160ee62006-01-08 01:03:32 -08001168 list_add(&dentry->d_u.d_child, &parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001169 spin_unlock(&dcache_lock);
1170
Nick Piggin3e880fb2011-01-07 17:49:19 +11001171 this_cpu_inc(nr_dentry);
Christoph Hellwig312d3ca2010-10-10 05:36:23 -04001172
Linus Torvalds1da177e2005-04-16 15:20:36 -07001173 return dentry;
1174}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001175EXPORT_SYMBOL(d_alloc);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001176
1177struct dentry *d_alloc_name(struct dentry *parent, const char *name)
1178{
1179 struct qstr q;
1180
1181 q.name = name;
1182 q.len = strlen(name);
1183 q.hash = full_name_hash(q.name, q.len);
1184 return d_alloc(parent, &q);
1185}
H Hartley Sweetenef26ca92009-09-29 20:09:42 -04001186EXPORT_SYMBOL(d_alloc_name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001187
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001188/* the caller must hold dcache_lock */
1189static void __d_instantiate(struct dentry *dentry, struct inode *inode)
1190{
1191 if (inode)
1192 list_add(&dentry->d_alias, &inode->i_dentry);
1193 dentry->d_inode = inode;
1194 fsnotify_d_instantiate(dentry, inode);
1195}
1196
Linus Torvalds1da177e2005-04-16 15:20:36 -07001197/**
1198 * d_instantiate - fill in inode information for a dentry
1199 * @entry: dentry to complete
1200 * @inode: inode to attach to this dentry
1201 *
1202 * Fill in inode information in the entry.
1203 *
1204 * This turns negative dentries into productive full members
1205 * of society.
1206 *
1207 * NOTE! This assumes that the inode count has been incremented
1208 * (or otherwise set) by the caller to indicate that it is now
1209 * in use by the dcache.
1210 */
1211
1212void d_instantiate(struct dentry *entry, struct inode * inode)
1213{
Eric Sesterhenn28133c72006-03-26 18:25:39 +02001214 BUG_ON(!list_empty(&entry->d_alias));
Linus Torvalds1da177e2005-04-16 15:20:36 -07001215 spin_lock(&dcache_lock);
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001216 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001217 spin_unlock(&dcache_lock);
1218 security_d_instantiate(entry, inode);
1219}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001220EXPORT_SYMBOL(d_instantiate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001221
1222/**
1223 * d_instantiate_unique - instantiate a non-aliased dentry
1224 * @entry: dentry to instantiate
1225 * @inode: inode to attach to this dentry
1226 *
1227 * Fill in inode information in the entry. On success, it returns NULL.
1228 * If an unhashed alias of "entry" already exists, then we return the
Oleg Drokine866cfa2006-01-09 20:52:51 -08001229 * aliased dentry instead and drop one reference to inode.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001230 *
1231 * Note that in order to avoid conflicts with rename() etc, the caller
1232 * had better be holding the parent directory semaphore.
Oleg Drokine866cfa2006-01-09 20:52:51 -08001233 *
1234 * This also assumes that the inode count has been incremented
1235 * (or otherwise set) by the caller to indicate that it is now
1236 * in use by the dcache.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001237 */
David Howells770bfad2006-08-22 20:06:07 -04001238static struct dentry *__d_instantiate_unique(struct dentry *entry,
1239 struct inode *inode)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001240{
1241 struct dentry *alias;
1242 int len = entry->d_name.len;
1243 const char *name = entry->d_name.name;
1244 unsigned int hash = entry->d_name.hash;
1245
David Howells770bfad2006-08-22 20:06:07 -04001246 if (!inode) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001247 __d_instantiate(entry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04001248 return NULL;
1249 }
1250
Linus Torvalds1da177e2005-04-16 15:20:36 -07001251 list_for_each_entry(alias, &inode->i_dentry, d_alias) {
1252 struct qstr *qstr = &alias->d_name;
1253
1254 if (qstr->hash != hash)
1255 continue;
1256 if (alias->d_parent != entry->d_parent)
1257 continue;
1258 if (qstr->len != len)
1259 continue;
1260 if (memcmp(qstr->name, name, len))
1261 continue;
1262 dget_locked(alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001263 return alias;
1264 }
David Howells770bfad2006-08-22 20:06:07 -04001265
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001266 __d_instantiate(entry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001267 return NULL;
1268}
David Howells770bfad2006-08-22 20:06:07 -04001269
1270struct dentry *d_instantiate_unique(struct dentry *entry, struct inode *inode)
1271{
1272 struct dentry *result;
1273
1274 BUG_ON(!list_empty(&entry->d_alias));
1275
1276 spin_lock(&dcache_lock);
1277 result = __d_instantiate_unique(entry, inode);
1278 spin_unlock(&dcache_lock);
1279
1280 if (!result) {
1281 security_d_instantiate(entry, inode);
1282 return NULL;
1283 }
1284
1285 BUG_ON(!d_unhashed(result));
1286 iput(inode);
1287 return result;
1288}
1289
Linus Torvalds1da177e2005-04-16 15:20:36 -07001290EXPORT_SYMBOL(d_instantiate_unique);
1291
1292/**
1293 * d_alloc_root - allocate root dentry
1294 * @root_inode: inode to allocate the root for
1295 *
1296 * Allocate a root ("/") dentry for the inode given. The inode is
1297 * instantiated and returned. %NULL is returned if there is insufficient
1298 * memory or the inode passed is %NULL.
1299 */
1300
1301struct dentry * d_alloc_root(struct inode * root_inode)
1302{
1303 struct dentry *res = NULL;
1304
1305 if (root_inode) {
1306 static const struct qstr name = { .name = "/", .len = 1 };
1307
1308 res = d_alloc(NULL, &name);
1309 if (res) {
1310 res->d_sb = root_inode->i_sb;
1311 res->d_parent = res;
1312 d_instantiate(res, root_inode);
1313 }
1314 }
1315 return res;
1316}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001317EXPORT_SYMBOL(d_alloc_root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001318
1319static inline struct hlist_head *d_hash(struct dentry *parent,
1320 unsigned long hash)
1321{
1322 hash += ((unsigned long) parent ^ GOLDEN_RATIO_PRIME) / L1_CACHE_BYTES;
1323 hash = hash ^ ((hash ^ GOLDEN_RATIO_PRIME) >> D_HASHBITS);
1324 return dentry_hashtable + (hash & D_HASHMASK);
1325}
1326
1327/**
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001328 * d_obtain_alias - find or allocate a dentry for a given inode
1329 * @inode: inode to allocate the dentry for
1330 *
1331 * Obtain a dentry for an inode resulting from NFS filehandle conversion or
1332 * similar open by handle operations. The returned dentry may be anonymous,
1333 * or may have a full name (if the inode was already in the cache).
1334 *
1335 * When called on a directory inode, we must ensure that the inode only ever
1336 * has one dentry. If a dentry is found, that is returned instead of
1337 * allocating a new one.
1338 *
1339 * On successful return, the reference to the inode has been transferred
Christoph Hellwig44003722008-08-11 15:49:04 +02001340 * to the dentry. In case of an error the reference on the inode is released.
1341 * To make it easier to use in export operations a %NULL or IS_ERR inode may
1342 * be passed in and will be the error will be propagate to the return value,
1343 * with a %NULL @inode replaced by ERR_PTR(-ESTALE).
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001344 */
1345struct dentry *d_obtain_alias(struct inode *inode)
1346{
Christoph Hellwig9308a612008-08-11 15:49:12 +02001347 static const struct qstr anonstring = { .name = "" };
1348 struct dentry *tmp;
1349 struct dentry *res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001350
1351 if (!inode)
Christoph Hellwig44003722008-08-11 15:49:04 +02001352 return ERR_PTR(-ESTALE);
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001353 if (IS_ERR(inode))
1354 return ERR_CAST(inode);
1355
Christoph Hellwig9308a612008-08-11 15:49:12 +02001356 res = d_find_alias(inode);
1357 if (res)
1358 goto out_iput;
1359
1360 tmp = d_alloc(NULL, &anonstring);
1361 if (!tmp) {
1362 res = ERR_PTR(-ENOMEM);
1363 goto out_iput;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001364 }
Christoph Hellwig9308a612008-08-11 15:49:12 +02001365 tmp->d_parent = tmp; /* make sure dput doesn't croak */
1366
1367 spin_lock(&dcache_lock);
1368 res = __d_find_alias(inode, 0);
1369 if (res) {
1370 spin_unlock(&dcache_lock);
1371 dput(tmp);
1372 goto out_iput;
1373 }
1374
1375 /* attach a disconnected dentry */
1376 spin_lock(&tmp->d_lock);
1377 tmp->d_sb = inode->i_sb;
1378 tmp->d_inode = inode;
1379 tmp->d_flags |= DCACHE_DISCONNECTED;
1380 tmp->d_flags &= ~DCACHE_UNHASHED;
1381 list_add(&tmp->d_alias, &inode->i_dentry);
Nick Piggin789680d2011-01-07 17:49:30 +11001382 spin_lock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001383 hlist_add_head(&tmp->d_hash, &inode->i_sb->s_anon);
Nick Piggin789680d2011-01-07 17:49:30 +11001384 spin_unlock(&dcache_hash_lock);
Christoph Hellwig9308a612008-08-11 15:49:12 +02001385 spin_unlock(&tmp->d_lock);
1386
1387 spin_unlock(&dcache_lock);
1388 return tmp;
1389
1390 out_iput:
1391 iput(inode);
1392 return res;
Christoph Hellwig4ea3ada2008-08-11 15:48:57 +02001393}
Benny Halevyadc48722009-02-27 14:02:59 -08001394EXPORT_SYMBOL(d_obtain_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001395
1396/**
1397 * d_splice_alias - splice a disconnected dentry into the tree if one exists
1398 * @inode: the inode which may have a disconnected dentry
1399 * @dentry: a negative dentry which we want to point to the inode.
1400 *
1401 * If inode is a directory and has a 'disconnected' dentry (i.e. IS_ROOT and
1402 * DCACHE_DISCONNECTED), then d_move that in place of the given dentry
1403 * and return it, else simply d_add the inode to the dentry and return NULL.
1404 *
1405 * This is needed in the lookup routine of any filesystem that is exportable
1406 * (via knfsd) so that we can build dcache paths to directories effectively.
1407 *
1408 * If a dentry was found and moved, then it is returned. Otherwise NULL
1409 * is returned. This matches the expected return value of ->lookup.
1410 *
1411 */
1412struct dentry *d_splice_alias(struct inode *inode, struct dentry *dentry)
1413{
1414 struct dentry *new = NULL;
1415
NeilBrown21c0d8f2006-10-04 02:16:16 -07001416 if (inode && S_ISDIR(inode->i_mode)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001417 spin_lock(&dcache_lock);
1418 new = __d_find_alias(inode, 1);
1419 if (new) {
1420 BUG_ON(!(new->d_flags & DCACHE_DISCONNECTED));
1421 spin_unlock(&dcache_lock);
1422 security_d_instantiate(new, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001423 d_move(new, dentry);
1424 iput(inode);
1425 } else {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001426 /* already taking dcache_lock, so d_add() by hand */
1427 __d_instantiate(dentry, inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001428 spin_unlock(&dcache_lock);
1429 security_d_instantiate(dentry, inode);
1430 d_rehash(dentry);
1431 }
1432 } else
1433 d_add(dentry, inode);
1434 return new;
1435}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001436EXPORT_SYMBOL(d_splice_alias);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001437
Barry Naujok94035402008-05-21 16:50:46 +10001438/**
1439 * d_add_ci - lookup or allocate new dentry with case-exact name
1440 * @inode: the inode case-insensitive lookup has found
1441 * @dentry: the negative dentry that was passed to the parent's lookup func
1442 * @name: the case-exact name to be associated with the returned dentry
1443 *
1444 * This is to avoid filling the dcache with case-insensitive names to the
1445 * same inode, only the actual correct case is stored in the dcache for
1446 * case-insensitive filesystems.
1447 *
1448 * For a case-insensitive lookup match and if the the case-exact dentry
1449 * already exists in in the dcache, use it and return it.
1450 *
1451 * If no entry exists with the exact case name, allocate new dentry with
1452 * the exact case, and return the spliced entry.
1453 */
Christoph Hellwige45b5902008-08-07 23:49:07 +02001454struct dentry *d_add_ci(struct dentry *dentry, struct inode *inode,
Barry Naujok94035402008-05-21 16:50:46 +10001455 struct qstr *name)
1456{
1457 int error;
1458 struct dentry *found;
1459 struct dentry *new;
1460
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001461 /*
1462 * First check if a dentry matching the name already exists,
1463 * if not go ahead and create it now.
1464 */
Barry Naujok94035402008-05-21 16:50:46 +10001465 found = d_hash_and_lookup(dentry->d_parent, name);
Barry Naujok94035402008-05-21 16:50:46 +10001466 if (!found) {
1467 new = d_alloc(dentry->d_parent, name);
1468 if (!new) {
1469 error = -ENOMEM;
1470 goto err_out;
1471 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001472
Barry Naujok94035402008-05-21 16:50:46 +10001473 found = d_splice_alias(inode, new);
1474 if (found) {
1475 dput(new);
1476 return found;
1477 }
1478 return new;
1479 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001480
1481 /*
1482 * If a matching dentry exists, and it's not negative use it.
1483 *
1484 * Decrement the reference count to balance the iget() done
1485 * earlier on.
1486 */
Barry Naujok94035402008-05-21 16:50:46 +10001487 if (found->d_inode) {
1488 if (unlikely(found->d_inode != inode)) {
1489 /* This can't happen because bad inodes are unhashed. */
1490 BUG_ON(!is_bad_inode(inode));
1491 BUG_ON(!is_bad_inode(found->d_inode));
1492 }
Barry Naujok94035402008-05-21 16:50:46 +10001493 iput(inode);
1494 return found;
1495 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001496
Barry Naujok94035402008-05-21 16:50:46 +10001497 /*
1498 * Negative dentry: instantiate it unless the inode is a directory and
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001499 * already has a dentry.
Barry Naujok94035402008-05-21 16:50:46 +10001500 */
Barry Naujok94035402008-05-21 16:50:46 +10001501 spin_lock(&dcache_lock);
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001502 if (!S_ISDIR(inode->i_mode) || list_empty(&inode->i_dentry)) {
OGAWA Hirofumi360da902008-10-16 07:50:28 +09001503 __d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001504 spin_unlock(&dcache_lock);
1505 security_d_instantiate(found, inode);
1506 return found;
1507 }
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001508
Barry Naujok94035402008-05-21 16:50:46 +10001509 /*
Christoph Hellwigb6520c82009-01-05 19:10:37 +01001510 * In case a directory already has a (disconnected) entry grab a
1511 * reference to it, move it in place and use it.
Barry Naujok94035402008-05-21 16:50:46 +10001512 */
1513 new = list_entry(inode->i_dentry.next, struct dentry, d_alias);
1514 dget_locked(new);
1515 spin_unlock(&dcache_lock);
Barry Naujok94035402008-05-21 16:50:46 +10001516 security_d_instantiate(found, inode);
Barry Naujok94035402008-05-21 16:50:46 +10001517 d_move(new, found);
Barry Naujok94035402008-05-21 16:50:46 +10001518 iput(inode);
Barry Naujok94035402008-05-21 16:50:46 +10001519 dput(found);
Barry Naujok94035402008-05-21 16:50:46 +10001520 return new;
1521
1522err_out:
1523 iput(inode);
1524 return ERR_PTR(error);
1525}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001526EXPORT_SYMBOL(d_add_ci);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001527
1528/**
1529 * d_lookup - search for a dentry
1530 * @parent: parent dentry
1531 * @name: qstr of name we wish to find
Nick Pigginb04f7842010-08-18 04:37:34 +10001532 * Returns: dentry, or NULL
Linus Torvalds1da177e2005-04-16 15:20:36 -07001533 *
Nick Pigginb04f7842010-08-18 04:37:34 +10001534 * d_lookup searches the children of the parent dentry for the name in
1535 * question. If the dentry is found its reference count is incremented and the
1536 * dentry is returned. The caller must use dput to free the entry when it has
1537 * finished using it. %NULL is returned if the dentry does not exist.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001538 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001539struct dentry * d_lookup(struct dentry * parent, struct qstr * name)
1540{
1541 struct dentry * dentry = NULL;
1542 unsigned long seq;
1543
1544 do {
1545 seq = read_seqbegin(&rename_lock);
1546 dentry = __d_lookup(parent, name);
1547 if (dentry)
1548 break;
1549 } while (read_seqretry(&rename_lock, seq));
1550 return dentry;
1551}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001552EXPORT_SYMBOL(d_lookup);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001553
Nick Pigginb04f7842010-08-18 04:37:34 +10001554/*
1555 * __d_lookup - search for a dentry (racy)
1556 * @parent: parent dentry
1557 * @name: qstr of name we wish to find
1558 * Returns: dentry, or NULL
1559 *
1560 * __d_lookup is like d_lookup, however it may (rarely) return a
1561 * false-negative result due to unrelated rename activity.
1562 *
1563 * __d_lookup is slightly faster by avoiding rename_lock read seqlock,
1564 * however it must be used carefully, eg. with a following d_lookup in
1565 * the case of failure.
1566 *
1567 * __d_lookup callers must be commented.
1568 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001569struct dentry * __d_lookup(struct dentry * parent, struct qstr * name)
1570{
1571 unsigned int len = name->len;
1572 unsigned int hash = name->hash;
1573 const unsigned char *str = name->name;
1574 struct hlist_head *head = d_hash(parent,hash);
1575 struct dentry *found = NULL;
1576 struct hlist_node *node;
Paul E. McKenney665a7582005-11-07 00:59:17 -08001577 struct dentry *dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001578
Nick Pigginb04f7842010-08-18 04:37:34 +10001579 /*
1580 * The hash list is protected using RCU.
1581 *
1582 * Take d_lock when comparing a candidate dentry, to avoid races
1583 * with d_move().
1584 *
1585 * It is possible that concurrent renames can mess up our list
1586 * walk here and result in missing our dentry, resulting in the
1587 * false-negative result. d_lookup() protects against concurrent
1588 * renames using rename_lock seqlock.
1589 *
1590 * See Documentation/vfs/dcache-locking.txt for more details.
1591 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07001592 rcu_read_lock();
1593
Paul E. McKenney665a7582005-11-07 00:59:17 -08001594 hlist_for_each_entry_rcu(dentry, node, head, d_hash) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001595 struct qstr *qstr;
1596
Linus Torvalds1da177e2005-04-16 15:20:36 -07001597 if (dentry->d_name.hash != hash)
1598 continue;
1599 if (dentry->d_parent != parent)
1600 continue;
1601
1602 spin_lock(&dentry->d_lock);
1603
1604 /*
1605 * Recheck the dentry after taking the lock - d_move may have
Nick Pigginb04f7842010-08-18 04:37:34 +10001606 * changed things. Don't bother checking the hash because
1607 * we're about to compare the whole name anyway.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001608 */
1609 if (dentry->d_parent != parent)
1610 goto next;
1611
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001612 /* non-existing due to RCU? */
1613 if (d_unhashed(dentry))
1614 goto next;
1615
Linus Torvalds1da177e2005-04-16 15:20:36 -07001616 /*
1617 * It is safe to compare names since d_move() cannot
1618 * change the qstr (protected by d_lock).
1619 */
1620 qstr = &dentry->d_name;
1621 if (parent->d_op && parent->d_op->d_compare) {
Nick Piggin621e1552011-01-07 17:49:27 +11001622 if (parent->d_op->d_compare(parent, parent->d_inode,
1623 dentry, dentry->d_inode,
1624 qstr->len, qstr->name, name))
Linus Torvalds1da177e2005-04-16 15:20:36 -07001625 goto next;
1626 } else {
1627 if (qstr->len != len)
1628 goto next;
1629 if (memcmp(qstr->name, str, len))
1630 goto next;
1631 }
1632
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001633 dentry->d_count++;
Linus Torvaldsd0185c02008-09-29 07:42:57 -07001634 found = dentry;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001635 spin_unlock(&dentry->d_lock);
1636 break;
1637next:
1638 spin_unlock(&dentry->d_lock);
1639 }
1640 rcu_read_unlock();
1641
1642 return found;
1643}
1644
1645/**
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001646 * d_hash_and_lookup - hash the qstr then search for a dentry
1647 * @dir: Directory to search in
1648 * @name: qstr of name we wish to find
1649 *
1650 * On hash failure or on lookup failure NULL is returned.
1651 */
1652struct dentry *d_hash_and_lookup(struct dentry *dir, struct qstr *name)
1653{
1654 struct dentry *dentry = NULL;
1655
1656 /*
1657 * Check for a fs-specific hash function. Note that we must
1658 * calculate the standard hash first, as the d_op->d_hash()
1659 * routine may choose to leave the hash value unchanged.
1660 */
1661 name->hash = full_name_hash(name->name, name->len);
1662 if (dir->d_op && dir->d_op->d_hash) {
Nick Pigginb1e6a012011-01-07 17:49:28 +11001663 if (dir->d_op->d_hash(dir, dir->d_inode, name) < 0)
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08001664 goto out;
1665 }
1666 dentry = d_lookup(dir, name);
1667out:
1668 return dentry;
1669}
1670
1671/**
Nick Piggin786a5e12011-01-07 17:49:16 +11001672 * d_validate - verify dentry provided from insecure source (deprecated)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001673 * @dentry: The dentry alleged to be valid child of @dparent
1674 * @dparent: The parent dentry (known to be valid)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001675 *
1676 * An insecure source has sent us a dentry, here we verify it and dget() it.
1677 * This is used by ncpfs in its readdir implementation.
1678 * Zero is returned in the dentry is invalid.
Nick Piggin786a5e12011-01-07 17:49:16 +11001679 *
1680 * This function is slow for big directories, and deprecated, do not use it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001681 */
Nick Piggind3a23e12011-01-05 20:01:21 +11001682int d_validate(struct dentry *dentry, struct dentry *dparent)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001683{
Nick Piggin786a5e12011-01-07 17:49:16 +11001684 struct dentry *child;
Nick Piggind3a23e12011-01-05 20:01:21 +11001685
1686 spin_lock(&dcache_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001687 list_for_each_entry(child, &dparent->d_subdirs, d_u.d_child) {
1688 if (dentry == child) {
Nick Piggind3a23e12011-01-05 20:01:21 +11001689 __dget_locked(dentry);
1690 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001691 return 1;
1692 }
1693 }
Nick Piggind3a23e12011-01-05 20:01:21 +11001694 spin_unlock(&dcache_lock);
Nick Piggin786a5e12011-01-07 17:49:16 +11001695
Linus Torvalds1da177e2005-04-16 15:20:36 -07001696 return 0;
1697}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001698EXPORT_SYMBOL(d_validate);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001699
1700/*
1701 * When a file is deleted, we have two options:
1702 * - turn this dentry into a negative dentry
1703 * - unhash this dentry and free it.
1704 *
1705 * Usually, we want to just turn this into
1706 * a negative dentry, but if anybody else is
1707 * currently using the dentry or the inode
1708 * we can't do that and we fall back on removing
1709 * it from the hash queues and waiting for
1710 * it to be deleted later when it has no users
1711 */
1712
1713/**
1714 * d_delete - delete a dentry
1715 * @dentry: The dentry to delete
1716 *
1717 * Turn the dentry into a negative dentry if possible, otherwise
1718 * remove it from the hash queues so it can be deleted later
1719 */
1720
1721void d_delete(struct dentry * dentry)
1722{
John McCutchan7a91bf72005-08-08 13:52:16 -04001723 int isdir = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001724 /*
1725 * Are we the only user?
1726 */
1727 spin_lock(&dcache_lock);
1728 spin_lock(&dentry->d_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001729 isdir = S_ISDIR(dentry->d_inode->i_mode);
Nick Pigginb7ab39f2011-01-07 17:49:32 +11001730 if (dentry->d_count == 1) {
Al Viro13e3c5e2010-05-21 16:11:04 -04001731 dentry->d_flags &= ~DCACHE_CANT_MOUNT;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001732 dentry_iput(dentry);
John McCutchan7a91bf72005-08-08 13:52:16 -04001733 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001734 return;
1735 }
1736
1737 if (!d_unhashed(dentry))
1738 __d_drop(dentry);
1739
1740 spin_unlock(&dentry->d_lock);
1741 spin_unlock(&dcache_lock);
John McCutchan7a91bf72005-08-08 13:52:16 -04001742
1743 fsnotify_nameremove(dentry, isdir);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001744}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001745EXPORT_SYMBOL(d_delete);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001746
1747static void __d_rehash(struct dentry * entry, struct hlist_head *list)
1748{
1749
1750 entry->d_flags &= ~DCACHE_UNHASHED;
1751 hlist_add_head_rcu(&entry->d_hash, list);
1752}
1753
David Howells770bfad2006-08-22 20:06:07 -04001754static void _d_rehash(struct dentry * entry)
1755{
1756 __d_rehash(entry, d_hash(entry->d_parent, entry->d_name.hash));
1757}
1758
Linus Torvalds1da177e2005-04-16 15:20:36 -07001759/**
1760 * d_rehash - add an entry back to the hash
1761 * @entry: dentry to add to the hash
1762 *
1763 * Adds a dentry to the hash according to its name.
1764 */
1765
1766void d_rehash(struct dentry * entry)
1767{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001768 spin_lock(&dcache_lock);
1769 spin_lock(&entry->d_lock);
Nick Piggin789680d2011-01-07 17:49:30 +11001770 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04001771 _d_rehash(entry);
Nick Piggin789680d2011-01-07 17:49:30 +11001772 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001773 spin_unlock(&entry->d_lock);
1774 spin_unlock(&dcache_lock);
1775}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001776EXPORT_SYMBOL(d_rehash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001777
Nick Pigginfb2d5b82011-01-07 17:49:26 +11001778/**
1779 * dentry_update_name_case - update case insensitive dentry with a new name
1780 * @dentry: dentry to be updated
1781 * @name: new name
1782 *
1783 * Update a case insensitive dentry with new case of name.
1784 *
1785 * dentry must have been returned by d_lookup with name @name. Old and new
1786 * name lengths must match (ie. no d_compare which allows mismatched name
1787 * lengths).
1788 *
1789 * Parent inode i_mutex must be held over d_lookup and into this call (to
1790 * keep renames and concurrent inserts, and readdir(2) away).
1791 */
1792void dentry_update_name_case(struct dentry *dentry, struct qstr *name)
1793{
1794 BUG_ON(!mutex_is_locked(&dentry->d_inode->i_mutex));
1795 BUG_ON(dentry->d_name.len != name->len); /* d_lookup gives this */
1796
1797 spin_lock(&dcache_lock);
1798 spin_lock(&dentry->d_lock);
1799 memcpy((unsigned char *)dentry->d_name.name, name->name, name->len);
1800 spin_unlock(&dentry->d_lock);
1801 spin_unlock(&dcache_lock);
1802}
1803EXPORT_SYMBOL(dentry_update_name_case);
1804
Linus Torvalds1da177e2005-04-16 15:20:36 -07001805/*
1806 * When switching names, the actual string doesn't strictly have to
1807 * be preserved in the target - because we're dropping the target
1808 * anyway. As such, we can just do a simple memcpy() to copy over
1809 * the new name before we switch.
1810 *
1811 * Note that we have to be a lot more careful about getting the hash
1812 * switched - we have to switch the hash value properly even if it
1813 * then no longer matches the actual (corrupted) string of the target.
1814 * The hash value has to match the hash queue that the dentry is on..
1815 */
1816static void switch_names(struct dentry *dentry, struct dentry *target)
1817{
1818 if (dname_external(target)) {
1819 if (dname_external(dentry)) {
1820 /*
1821 * Both external: swap the pointers
1822 */
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001823 swap(target->d_name.name, dentry->d_name.name);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001824 } else {
1825 /*
1826 * dentry:internal, target:external. Steal target's
1827 * storage and make target internal.
1828 */
J. Bruce Fields321bcf92007-10-21 16:41:38 -07001829 memcpy(target->d_iname, dentry->d_name.name,
1830 dentry->d_name.len + 1);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001831 dentry->d_name.name = target->d_name.name;
1832 target->d_name.name = target->d_iname;
1833 }
1834 } else {
1835 if (dname_external(dentry)) {
1836 /*
1837 * dentry:external, target:internal. Give dentry's
1838 * storage to target and make dentry internal
1839 */
1840 memcpy(dentry->d_iname, target->d_name.name,
1841 target->d_name.len + 1);
1842 target->d_name.name = dentry->d_name.name;
1843 dentry->d_name.name = dentry->d_iname;
1844 } else {
1845 /*
1846 * Both are internal. Just copy target to dentry
1847 */
1848 memcpy(dentry->d_iname, target->d_name.name,
1849 target->d_name.len + 1);
Al Virodc711ca2008-11-03 15:03:50 -05001850 dentry->d_name.len = target->d_name.len;
1851 return;
Linus Torvalds1da177e2005-04-16 15:20:36 -07001852 }
1853 }
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001854 swap(dentry->d_name.len, target->d_name.len);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001855}
1856
1857/*
1858 * We cannibalize "target" when moving dentry on top of it,
1859 * because it's going to be thrown away anyway. We could be more
1860 * polite about it, though.
1861 *
1862 * This forceful removal will result in ugly /proc output if
1863 * somebody holds a file open that got deleted due to a rename.
1864 * We could be nicer about the deleted file, and let it show
J. Bruce Fieldsbc154b12007-10-16 23:29:42 -07001865 * up under the name it had before it was deleted rather than
1866 * under the original name of the file that was moved on top of it.
Linus Torvalds1da177e2005-04-16 15:20:36 -07001867 */
1868
Trond Myklebust9eaef272006-10-21 10:24:20 -07001869/*
1870 * d_move_locked - move a dentry
Linus Torvalds1da177e2005-04-16 15:20:36 -07001871 * @dentry: entry to move
1872 * @target: new dentry
1873 *
1874 * Update the dcache to reflect the move of a file name. Negative
1875 * dcache entries should not be moved in this way.
1876 */
Trond Myklebust9eaef272006-10-21 10:24:20 -07001877static void d_move_locked(struct dentry * dentry, struct dentry * target)
Linus Torvalds1da177e2005-04-16 15:20:36 -07001878{
Linus Torvalds1da177e2005-04-16 15:20:36 -07001879 if (!dentry->d_inode)
1880 printk(KERN_WARNING "VFS: moving negative dcache entry\n");
1881
Linus Torvalds1da177e2005-04-16 15:20:36 -07001882 write_seqlock(&rename_lock);
1883 /*
1884 * XXXX: do we really need to take target->d_lock?
1885 */
Nick Pigginda502952011-01-07 17:49:33 +11001886 if (d_ancestor(dentry, target)) {
1887 spin_lock(&dentry->d_lock);
1888 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
1889 } else if (d_ancestor(target, dentry) || target < dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07001890 spin_lock(&target->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001891 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001892 } else {
1893 spin_lock(&dentry->d_lock);
Ingo Molnara90b9c02006-07-03 00:25:04 -07001894 spin_lock_nested(&target->d_lock, DENTRY_D_LOCK_NESTED);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001895 }
1896
1897 /* Move the dentry to the target hash queue, if on different bucket */
Nick Piggin789680d2011-01-07 17:49:30 +11001898 spin_lock(&dcache_hash_lock);
1899 if (!d_unhashed(dentry))
1900 hlist_del_rcu(&dentry->d_hash);
1901 __d_rehash(dentry, d_hash(target->d_parent, target->d_name.hash));
1902 spin_unlock(&dcache_hash_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001903
1904 /* Unhash the target: dput() will then get rid of it */
1905 __d_drop(target);
1906
Eric Dumazet5160ee62006-01-08 01:03:32 -08001907 list_del(&dentry->d_u.d_child);
1908 list_del(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001909
1910 /* Switch the names.. */
1911 switch_names(dentry, target);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001912 swap(dentry->d_name.hash, target->d_name.hash);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001913
1914 /* ... and switch the parents */
1915 if (IS_ROOT(dentry)) {
1916 dentry->d_parent = target->d_parent;
1917 target->d_parent = target;
Eric Dumazet5160ee62006-01-08 01:03:32 -08001918 INIT_LIST_HEAD(&target->d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001919 } else {
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08001920 swap(dentry->d_parent, target->d_parent);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001921
1922 /* And add them back to the (new) parent lists */
Eric Dumazet5160ee62006-01-08 01:03:32 -08001923 list_add(&target->d_u.d_child, &target->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001924 }
1925
Eric Dumazet5160ee62006-01-08 01:03:32 -08001926 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001927 spin_unlock(&target->d_lock);
Nick Pigginc32ccd82006-03-25 03:07:09 -08001928 fsnotify_d_move(dentry);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001929 spin_unlock(&dentry->d_lock);
1930 write_sequnlock(&rename_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07001931}
1932
1933/**
1934 * d_move - move a dentry
1935 * @dentry: entry to move
1936 * @target: new dentry
1937 *
1938 * Update the dcache to reflect the move of a file name. Negative
1939 * dcache entries should not be moved in this way.
1940 */
1941
1942void d_move(struct dentry * dentry, struct dentry * target)
1943{
1944 spin_lock(&dcache_lock);
1945 d_move_locked(dentry, target);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001946 spin_unlock(&dcache_lock);
1947}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07001948EXPORT_SYMBOL(d_move);
Linus Torvalds1da177e2005-04-16 15:20:36 -07001949
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001950/**
1951 * d_ancestor - search for an ancestor
1952 * @p1: ancestor dentry
1953 * @p2: child dentry
1954 *
1955 * Returns the ancestor dentry of p2 which is a child of p1, if p1 is
1956 * an ancestor of p2, else NULL.
Trond Myklebust9eaef272006-10-21 10:24:20 -07001957 */
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001958struct dentry *d_ancestor(struct dentry *p1, struct dentry *p2)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001959{
1960 struct dentry *p;
1961
OGAWA Hirofumi871c0062008-10-16 07:50:27 +09001962 for (p = p2; !IS_ROOT(p); p = p->d_parent) {
Trond Myklebust9eaef272006-10-21 10:24:20 -07001963 if (p->d_parent == p1)
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001964 return p;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001965 }
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001966 return NULL;
Trond Myklebust9eaef272006-10-21 10:24:20 -07001967}
1968
1969/*
1970 * This helper attempts to cope with remotely renamed directories
1971 *
1972 * It assumes that the caller is already holding
1973 * dentry->d_parent->d_inode->i_mutex and the dcache_lock
1974 *
1975 * Note: If ever the locking in lock_rename() changes, then please
1976 * remember to update this too...
Trond Myklebust9eaef272006-10-21 10:24:20 -07001977 */
1978static struct dentry *__d_unalias(struct dentry *dentry, struct dentry *alias)
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02001979 __releases(dcache_lock)
Trond Myklebust9eaef272006-10-21 10:24:20 -07001980{
1981 struct mutex *m1 = NULL, *m2 = NULL;
1982 struct dentry *ret;
1983
1984 /* If alias and dentry share a parent, then no extra locks required */
1985 if (alias->d_parent == dentry->d_parent)
1986 goto out_unalias;
1987
1988 /* Check for loops */
1989 ret = ERR_PTR(-ELOOP);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09001990 if (d_ancestor(alias, dentry))
Trond Myklebust9eaef272006-10-21 10:24:20 -07001991 goto out_err;
1992
1993 /* See lock_rename() */
1994 ret = ERR_PTR(-EBUSY);
1995 if (!mutex_trylock(&dentry->d_sb->s_vfs_rename_mutex))
1996 goto out_err;
1997 m1 = &dentry->d_sb->s_vfs_rename_mutex;
1998 if (!mutex_trylock(&alias->d_parent->d_inode->i_mutex))
1999 goto out_err;
2000 m2 = &alias->d_parent->d_inode->i_mutex;
2001out_unalias:
2002 d_move_locked(alias, dentry);
2003 ret = alias;
2004out_err:
2005 spin_unlock(&dcache_lock);
2006 if (m2)
2007 mutex_unlock(m2);
2008 if (m1)
2009 mutex_unlock(m1);
2010 return ret;
2011}
2012
2013/*
David Howells770bfad2006-08-22 20:06:07 -04002014 * Prepare an anonymous dentry for life in the superblock's dentry tree as a
2015 * named dentry in place of the dentry to be replaced.
2016 */
2017static void __d_materialise_dentry(struct dentry *dentry, struct dentry *anon)
2018{
2019 struct dentry *dparent, *aparent;
2020
2021 switch_names(dentry, anon);
Wu Fengguang9a8d5bb2009-01-07 18:09:14 -08002022 swap(dentry->d_name.hash, anon->d_name.hash);
David Howells770bfad2006-08-22 20:06:07 -04002023
2024 dparent = dentry->d_parent;
2025 aparent = anon->d_parent;
2026
2027 dentry->d_parent = (aparent == anon) ? dentry : aparent;
2028 list_del(&dentry->d_u.d_child);
2029 if (!IS_ROOT(dentry))
2030 list_add(&dentry->d_u.d_child, &dentry->d_parent->d_subdirs);
2031 else
2032 INIT_LIST_HEAD(&dentry->d_u.d_child);
2033
2034 anon->d_parent = (dparent == dentry) ? anon : dparent;
2035 list_del(&anon->d_u.d_child);
2036 if (!IS_ROOT(anon))
2037 list_add(&anon->d_u.d_child, &anon->d_parent->d_subdirs);
2038 else
2039 INIT_LIST_HEAD(&anon->d_u.d_child);
2040
2041 anon->d_flags &= ~DCACHE_DISCONNECTED;
2042}
2043
2044/**
2045 * d_materialise_unique - introduce an inode into the tree
2046 * @dentry: candidate dentry
2047 * @inode: inode to bind to the dentry, to which aliases may be attached
2048 *
2049 * Introduces an dentry into the tree, substituting an extant disconnected
2050 * root directory alias in its place if there is one
2051 */
2052struct dentry *d_materialise_unique(struct dentry *dentry, struct inode *inode)
2053{
Trond Myklebust9eaef272006-10-21 10:24:20 -07002054 struct dentry *actual;
David Howells770bfad2006-08-22 20:06:07 -04002055
2056 BUG_ON(!d_unhashed(dentry));
2057
2058 spin_lock(&dcache_lock);
2059
2060 if (!inode) {
2061 actual = dentry;
OGAWA Hirofumi360da902008-10-16 07:50:28 +09002062 __d_instantiate(dentry, NULL);
David Howells770bfad2006-08-22 20:06:07 -04002063 goto found_lock;
2064 }
2065
Trond Myklebust9eaef272006-10-21 10:24:20 -07002066 if (S_ISDIR(inode->i_mode)) {
2067 struct dentry *alias;
David Howells770bfad2006-08-22 20:06:07 -04002068
Trond Myklebust9eaef272006-10-21 10:24:20 -07002069 /* Does an aliased dentry already exist? */
2070 alias = __d_find_alias(inode, 0);
2071 if (alias) {
2072 actual = alias;
2073 /* Is this an anonymous mountpoint that we could splice
2074 * into our tree? */
2075 if (IS_ROOT(alias)) {
2076 spin_lock(&alias->d_lock);
2077 __d_materialise_dentry(dentry, alias);
2078 __d_drop(alias);
2079 goto found;
2080 }
2081 /* Nope, but we must(!) avoid directory aliasing */
2082 actual = __d_unalias(dentry, alias);
2083 if (IS_ERR(actual))
2084 dput(alias);
2085 goto out_nolock;
2086 }
David Howells770bfad2006-08-22 20:06:07 -04002087 }
2088
2089 /* Add a unique reference */
2090 actual = __d_instantiate_unique(dentry, inode);
2091 if (!actual)
2092 actual = dentry;
2093 else if (unlikely(!d_unhashed(actual)))
2094 goto shouldnt_be_hashed;
2095
2096found_lock:
2097 spin_lock(&actual->d_lock);
2098found:
Nick Piggin789680d2011-01-07 17:49:30 +11002099 spin_lock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04002100 _d_rehash(actual);
Nick Piggin789680d2011-01-07 17:49:30 +11002101 spin_unlock(&dcache_hash_lock);
David Howells770bfad2006-08-22 20:06:07 -04002102 spin_unlock(&actual->d_lock);
2103 spin_unlock(&dcache_lock);
Trond Myklebust9eaef272006-10-21 10:24:20 -07002104out_nolock:
David Howells770bfad2006-08-22 20:06:07 -04002105 if (actual == dentry) {
2106 security_d_instantiate(dentry, inode);
2107 return NULL;
2108 }
2109
2110 iput(inode);
2111 return actual;
2112
David Howells770bfad2006-08-22 20:06:07 -04002113shouldnt_be_hashed:
2114 spin_unlock(&dcache_lock);
2115 BUG();
David Howells770bfad2006-08-22 20:06:07 -04002116}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002117EXPORT_SYMBOL_GPL(d_materialise_unique);
David Howells770bfad2006-08-22 20:06:07 -04002118
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002119static int prepend(char **buffer, int *buflen, const char *str, int namelen)
Ram Pai6092d042008-03-27 13:06:20 +01002120{
2121 *buflen -= namelen;
2122 if (*buflen < 0)
2123 return -ENAMETOOLONG;
2124 *buffer -= namelen;
2125 memcpy(*buffer, str, namelen);
2126 return 0;
2127}
2128
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002129static int prepend_name(char **buffer, int *buflen, struct qstr *name)
2130{
2131 return prepend(buffer, buflen, name->name, name->len);
2132}
2133
Linus Torvalds1da177e2005-04-16 15:20:36 -07002134/**
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002135 * Prepend path string to a buffer
2136 *
2137 * @path: the dentry/vfsmount to report
2138 * @root: root vfsmnt/dentry (may be modified by this function)
2139 * @buffer: pointer to the end of the buffer
2140 * @buflen: pointer to buffer length
2141 *
2142 * Caller holds the dcache_lock.
2143 *
2144 * If path is not reachable from the supplied root, then the value of
2145 * root is changed (without modifying refcounts).
2146 */
2147static int prepend_path(const struct path *path, struct path *root,
2148 char **buffer, int *buflen)
2149{
2150 struct dentry *dentry = path->dentry;
2151 struct vfsmount *vfsmnt = path->mnt;
2152 bool slash = false;
2153 int error = 0;
2154
Nick Piggin99b7db72010-08-18 04:37:39 +10002155 br_read_lock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002156 while (dentry != root->dentry || vfsmnt != root->mnt) {
2157 struct dentry * parent;
2158
2159 if (dentry == vfsmnt->mnt_root || IS_ROOT(dentry)) {
2160 /* Global root? */
2161 if (vfsmnt->mnt_parent == vfsmnt) {
2162 goto global_root;
2163 }
2164 dentry = vfsmnt->mnt_mountpoint;
2165 vfsmnt = vfsmnt->mnt_parent;
2166 continue;
2167 }
2168 parent = dentry->d_parent;
2169 prefetch(parent);
2170 error = prepend_name(buffer, buflen, &dentry->d_name);
2171 if (!error)
2172 error = prepend(buffer, buflen, "/", 1);
2173 if (error)
2174 break;
2175
2176 slash = true;
2177 dentry = parent;
2178 }
2179
2180out:
2181 if (!error && !slash)
2182 error = prepend(buffer, buflen, "/", 1);
2183
Nick Piggin99b7db72010-08-18 04:37:39 +10002184 br_read_unlock(vfsmount_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002185 return error;
2186
2187global_root:
2188 /*
2189 * Filesystems needing to implement special "root names"
2190 * should do so with ->d_dname()
2191 */
2192 if (IS_ROOT(dentry) &&
2193 (dentry->d_name.len != 1 || dentry->d_name.name[0] != '/')) {
2194 WARN(1, "Root dentry has weird name <%.*s>\n",
2195 (int) dentry->d_name.len, dentry->d_name.name);
2196 }
2197 root->mnt = vfsmnt;
2198 root->dentry = dentry;
2199 goto out;
2200}
2201
2202/**
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002203 * __d_path - return the path of a dentry
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002204 * @path: the dentry/vfsmount to report
2205 * @root: root vfsmnt/dentry (may be modified by this function)
Randy Dunlapcd956a12010-08-14 13:05:31 -07002206 * @buf: buffer to return value in
Linus Torvalds1da177e2005-04-16 15:20:36 -07002207 * @buflen: buffer length
2208 *
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002209 * Convert a dentry into an ASCII path name.
Linus Torvalds1da177e2005-04-16 15:20:36 -07002210 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002211 * Returns a pointer into the buffer or an error code if the
2212 * path was too long.
Linus Torvalds552ce542007-02-13 12:08:18 -08002213 *
Christoph Hellwigbe148242010-10-10 05:36:21 -04002214 * "buflen" should be positive.
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002215 *
2216 * If path is not reachable from the supplied root, then the value of
2217 * root is changed (without modifying refcounts).
Linus Torvalds1da177e2005-04-16 15:20:36 -07002218 */
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002219char *__d_path(const struct path *path, struct path *root,
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002220 char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002221{
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002222 char *res = buf + buflen;
2223 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002224
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002225 prepend(&res, &buflen, "\0", 1);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002226 spin_lock(&dcache_lock);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002227 error = prepend_path(path, root, &res, &buflen);
Christoph Hellwigbe148242010-10-10 05:36:21 -04002228 spin_unlock(&dcache_lock);
2229
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002230 if (error)
2231 return ERR_PTR(error);
Miklos Szeredif2eb6572010-08-10 11:41:39 +02002232 return res;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002233}
2234
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002235/*
2236 * same as __d_path but appends "(deleted)" for unlinked files.
2237 */
2238static int path_with_deleted(const struct path *path, struct path *root,
2239 char **buf, int *buflen)
2240{
2241 prepend(buf, buflen, "\0", 1);
2242 if (d_unlinked(path->dentry)) {
2243 int error = prepend(buf, buflen, " (deleted)", 10);
2244 if (error)
2245 return error;
2246 }
2247
2248 return prepend_path(path, root, buf, buflen);
2249}
2250
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002251static int prepend_unreachable(char **buffer, int *buflen)
2252{
2253 return prepend(buffer, buflen, "(unreachable)", 13);
2254}
2255
Jan Bluncka03a8a702008-02-14 19:38:32 -08002256/**
2257 * d_path - return the path of a dentry
Jan Blunckcf28b482008-02-14 19:38:44 -08002258 * @path: path to report
Jan Bluncka03a8a702008-02-14 19:38:32 -08002259 * @buf: buffer to return value in
2260 * @buflen: buffer length
2261 *
2262 * Convert a dentry into an ASCII path name. If the entry has been deleted
2263 * the string " (deleted)" is appended. Note that this is ambiguous.
2264 *
Arjan van de Ven52afeef2008-12-01 14:35:00 -08002265 * Returns a pointer into the buffer or an error code if the path was
2266 * too long. Note: Callers should use the returned pointer, not the passed
2267 * in buffer, to use the name! The implementation often starts at an offset
2268 * into the buffer, and may leave 0 bytes at the start.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002269 *
Miklos Szeredi31f3e0b2008-06-23 18:11:52 +02002270 * "buflen" should be positive.
Jan Bluncka03a8a702008-02-14 19:38:32 -08002271 */
Jan Engelhardt20d4fdc2008-06-09 16:40:36 -07002272char *d_path(const struct path *path, char *buf, int buflen)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002273{
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002274 char *res = buf + buflen;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002275 struct path root;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002276 struct path tmp;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002277 int error;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002278
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002279 /*
2280 * We have various synthetic filesystems that never get mounted. On
2281 * these filesystems dentries are never used for lookup purposes, and
2282 * thus don't need to be hashed. They also don't need a name until a
2283 * user wants to identify the object in /proc/pid/fd/. The little hack
2284 * below allows us to generate a name for these objects on demand:
2285 */
Jan Blunckcf28b482008-02-14 19:38:44 -08002286 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2287 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002288
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002289 get_fs_root(current->fs, &root);
Linus Torvalds552ce542007-02-13 12:08:18 -08002290 spin_lock(&dcache_lock);
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002291 tmp = root;
Miklos Szerediffd1f4e2010-08-10 11:41:40 +02002292 error = path_with_deleted(path, &tmp, &res, &buflen);
2293 if (error)
2294 res = ERR_PTR(error);
Linus Torvalds552ce542007-02-13 12:08:18 -08002295 spin_unlock(&dcache_lock);
Jan Blunck6ac08c32008-02-14 19:34:38 -08002296 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002297 return res;
2298}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002299EXPORT_SYMBOL(d_path);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002300
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002301/**
2302 * d_path_with_unreachable - return the path of a dentry
2303 * @path: path to report
2304 * @buf: buffer to return value in
2305 * @buflen: buffer length
2306 *
2307 * The difference from d_path() is that this prepends "(unreachable)"
2308 * to paths which are unreachable from the current process' root.
2309 */
2310char *d_path_with_unreachable(const struct path *path, char *buf, int buflen)
2311{
2312 char *res = buf + buflen;
2313 struct path root;
2314 struct path tmp;
2315 int error;
2316
2317 if (path->dentry->d_op && path->dentry->d_op->d_dname)
2318 return path->dentry->d_op->d_dname(path->dentry, buf, buflen);
2319
2320 get_fs_root(current->fs, &root);
2321 spin_lock(&dcache_lock);
2322 tmp = root;
2323 error = path_with_deleted(path, &tmp, &res, &buflen);
2324 if (!error && !path_equal(&tmp, &root))
2325 error = prepend_unreachable(&res, &buflen);
2326 spin_unlock(&dcache_lock);
2327 path_put(&root);
2328 if (error)
2329 res = ERR_PTR(error);
2330
2331 return res;
2332}
2333
Linus Torvalds1da177e2005-04-16 15:20:36 -07002334/*
Eric Dumazetc23fbb62007-05-08 00:26:18 -07002335 * Helper function for dentry_operations.d_dname() members
2336 */
2337char *dynamic_dname(struct dentry *dentry, char *buffer, int buflen,
2338 const char *fmt, ...)
2339{
2340 va_list args;
2341 char temp[64];
2342 int sz;
2343
2344 va_start(args, fmt);
2345 sz = vsnprintf(temp, sizeof(temp), fmt, args) + 1;
2346 va_end(args);
2347
2348 if (sz > sizeof(temp) || sz > buflen)
2349 return ERR_PTR(-ENAMETOOLONG);
2350
2351 buffer += buflen - sz;
2352 return memcpy(buffer, temp, sz);
2353}
2354
2355/*
Ram Pai6092d042008-03-27 13:06:20 +01002356 * Write full pathname from the root of the filesystem into the buffer.
2357 */
Nick Pigginec2447c2011-01-07 17:49:29 +11002358static char *__dentry_path(struct dentry *dentry, char *buf, int buflen)
Ram Pai6092d042008-03-27 13:06:20 +01002359{
2360 char *end = buf + buflen;
2361 char *retval;
2362
Ram Pai6092d042008-03-27 13:06:20 +01002363 prepend(&end, &buflen, "\0", 1);
Ram Pai6092d042008-03-27 13:06:20 +01002364 if (buflen < 1)
2365 goto Elong;
2366 /* Get '/' right */
2367 retval = end-1;
2368 *retval = '/';
2369
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002370 while (!IS_ROOT(dentry)) {
2371 struct dentry *parent = dentry->d_parent;
Ram Pai6092d042008-03-27 13:06:20 +01002372
Ram Pai6092d042008-03-27 13:06:20 +01002373 prefetch(parent);
Miklos Szeredicdd16d02008-06-23 18:11:53 +02002374 if ((prepend_name(&end, &buflen, &dentry->d_name) != 0) ||
Ram Pai6092d042008-03-27 13:06:20 +01002375 (prepend(&end, &buflen, "/", 1) != 0))
2376 goto Elong;
2377
2378 retval = end;
2379 dentry = parent;
2380 }
Al Viroc1031352010-06-06 22:31:14 -04002381 return retval;
2382Elong:
2383 return ERR_PTR(-ENAMETOOLONG);
2384}
Nick Pigginec2447c2011-01-07 17:49:29 +11002385
2386char *dentry_path_raw(struct dentry *dentry, char *buf, int buflen)
2387{
2388 char *retval;
2389
2390 spin_lock(&dcache_lock);
2391 retval = __dentry_path(dentry, buf, buflen);
2392 spin_unlock(&dcache_lock);
2393
2394 return retval;
2395}
2396EXPORT_SYMBOL(dentry_path_raw);
Al Viroc1031352010-06-06 22:31:14 -04002397
2398char *dentry_path(struct dentry *dentry, char *buf, int buflen)
2399{
2400 char *p = NULL;
2401 char *retval;
2402
2403 spin_lock(&dcache_lock);
2404 if (d_unlinked(dentry)) {
2405 p = buf + buflen;
2406 if (prepend(&p, &buflen, "//deleted", 10) != 0)
2407 goto Elong;
2408 buflen++;
2409 }
2410 retval = __dentry_path(dentry, buf, buflen);
Ram Pai6092d042008-03-27 13:06:20 +01002411 spin_unlock(&dcache_lock);
Al Viroc1031352010-06-06 22:31:14 -04002412 if (!IS_ERR(retval) && p)
2413 *p = '/'; /* restore '/' overriden with '\0' */
Ram Pai6092d042008-03-27 13:06:20 +01002414 return retval;
2415Elong:
2416 spin_unlock(&dcache_lock);
2417 return ERR_PTR(-ENAMETOOLONG);
2418}
2419
2420/*
Linus Torvalds1da177e2005-04-16 15:20:36 -07002421 * NOTE! The user-level library version returns a
2422 * character pointer. The kernel system call just
2423 * returns the length of the buffer filled (which
2424 * includes the ending '\0' character), or a negative
2425 * error value. So libc would do something like
2426 *
2427 * char *getcwd(char * buf, size_t size)
2428 * {
2429 * int retval;
2430 *
2431 * retval = sys_getcwd(buf, size);
2432 * if (retval >= 0)
2433 * return buf;
2434 * errno = -retval;
2435 * return NULL;
2436 * }
2437 */
Heiko Carstens3cdad422009-01-14 14:14:22 +01002438SYSCALL_DEFINE2(getcwd, char __user *, buf, unsigned long, size)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002439{
Linus Torvalds552ce542007-02-13 12:08:18 -08002440 int error;
Jan Blunck6ac08c32008-02-14 19:34:38 -08002441 struct path pwd, root;
Linus Torvalds552ce542007-02-13 12:08:18 -08002442 char *page = (char *) __get_free_page(GFP_USER);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002443
2444 if (!page)
2445 return -ENOMEM;
2446
Miklos Szeredif7ad3c62010-08-10 11:41:36 +02002447 get_fs_root_and_pwd(current->fs, &root, &pwd);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002448
Linus Torvalds552ce542007-02-13 12:08:18 -08002449 error = -ENOENT;
Linus Torvalds552ce542007-02-13 12:08:18 -08002450 spin_lock(&dcache_lock);
Alexey Dobriyanf3da3922009-05-04 03:32:03 +04002451 if (!d_unlinked(pwd.dentry)) {
Linus Torvalds552ce542007-02-13 12:08:18 -08002452 unsigned long len;
Miklos Szeredi9d1bc6012008-03-27 13:06:21 +01002453 struct path tmp = root;
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002454 char *cwd = page + PAGE_SIZE;
2455 int buflen = PAGE_SIZE;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002456
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002457 prepend(&cwd, &buflen, "\0", 1);
2458 error = prepend_path(&pwd, &tmp, &cwd, &buflen);
Linus Torvalds552ce542007-02-13 12:08:18 -08002459 spin_unlock(&dcache_lock);
2460
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002461 if (error)
Linus Torvalds552ce542007-02-13 12:08:18 -08002462 goto out;
2463
Miklos Szeredi8df9d1a2010-08-10 11:41:41 +02002464 /* Unreachable from current root */
2465 if (!path_equal(&tmp, &root)) {
2466 error = prepend_unreachable(&cwd, &buflen);
2467 if (error)
2468 goto out;
2469 }
2470
Linus Torvalds552ce542007-02-13 12:08:18 -08002471 error = -ERANGE;
2472 len = PAGE_SIZE + page - cwd;
2473 if (len <= size) {
2474 error = len;
2475 if (copy_to_user(buf, cwd, len))
2476 error = -EFAULT;
2477 }
2478 } else
2479 spin_unlock(&dcache_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002480
2481out:
Jan Blunck6ac08c32008-02-14 19:34:38 -08002482 path_put(&pwd);
2483 path_put(&root);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002484 free_page((unsigned long) page);
2485 return error;
2486}
2487
2488/*
2489 * Test whether new_dentry is a subdirectory of old_dentry.
2490 *
2491 * Trivially implemented using the dcache structure
2492 */
2493
2494/**
2495 * is_subdir - is new dentry a subdirectory of old_dentry
2496 * @new_dentry: new dentry
2497 * @old_dentry: old dentry
2498 *
2499 * Returns 1 if new_dentry is a subdirectory of the parent (at any depth).
2500 * Returns 0 otherwise.
2501 * Caller must ensure that "new_dentry" is pinned before calling is_subdir()
2502 */
2503
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002504int is_subdir(struct dentry *new_dentry, struct dentry *old_dentry)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002505{
2506 int result;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002507 unsigned long seq;
2508
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002509 if (new_dentry == old_dentry)
2510 return 1;
2511
2512 /*
2513 * Need rcu_readlock to protect against the d_parent trashing
2514 * due to d_move
Linus Torvalds1da177e2005-04-16 15:20:36 -07002515 */
2516 rcu_read_lock();
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002517 do {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002518 /* for restarting inner loop in case of seq retry */
Linus Torvalds1da177e2005-04-16 15:20:36 -07002519 seq = read_seqbegin(&rename_lock);
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002520 if (d_ancestor(old_dentry, new_dentry))
Linus Torvalds1da177e2005-04-16 15:20:36 -07002521 result = 1;
OGAWA Hirofumie2761a12008-10-16 07:50:28 +09002522 else
2523 result = 0;
Linus Torvalds1da177e2005-04-16 15:20:36 -07002524 } while (read_seqretry(&rename_lock, seq));
2525 rcu_read_unlock();
2526
2527 return result;
2528}
2529
Al Viro2096f752010-01-30 13:16:21 -05002530int path_is_under(struct path *path1, struct path *path2)
2531{
2532 struct vfsmount *mnt = path1->mnt;
2533 struct dentry *dentry = path1->dentry;
2534 int res;
Nick Piggin99b7db72010-08-18 04:37:39 +10002535
2536 br_read_lock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002537 if (mnt != path2->mnt) {
2538 for (;;) {
2539 if (mnt->mnt_parent == mnt) {
Nick Piggin99b7db72010-08-18 04:37:39 +10002540 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002541 return 0;
2542 }
2543 if (mnt->mnt_parent == path2->mnt)
2544 break;
2545 mnt = mnt->mnt_parent;
2546 }
2547 dentry = mnt->mnt_mountpoint;
2548 }
2549 res = is_subdir(dentry, path2->dentry);
Nick Piggin99b7db72010-08-18 04:37:39 +10002550 br_read_unlock(vfsmount_lock);
Al Viro2096f752010-01-30 13:16:21 -05002551 return res;
2552}
2553EXPORT_SYMBOL(path_is_under);
2554
Linus Torvalds1da177e2005-04-16 15:20:36 -07002555void d_genocide(struct dentry *root)
2556{
2557 struct dentry *this_parent = root;
2558 struct list_head *next;
2559
2560 spin_lock(&dcache_lock);
2561repeat:
2562 next = this_parent->d_subdirs.next;
2563resume:
2564 while (next != &this_parent->d_subdirs) {
2565 struct list_head *tmp = next;
Eric Dumazet5160ee62006-01-08 01:03:32 -08002566 struct dentry *dentry = list_entry(tmp, struct dentry, d_u.d_child);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002567 next = tmp->next;
Nick Pigginda502952011-01-07 17:49:33 +11002568 spin_lock_nested(&dentry->d_lock, DENTRY_D_LOCK_NESTED);
2569 if (d_unhashed(dentry) || !dentry->d_inode) {
2570 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002571 continue;
Nick Pigginda502952011-01-07 17:49:33 +11002572 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002573 if (!list_empty(&dentry->d_subdirs)) {
Nick Pigginda502952011-01-07 17:49:33 +11002574 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002575 this_parent = dentry;
2576 goto repeat;
2577 }
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002578 dentry->d_count--;
2579 spin_unlock(&dentry->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002580 }
2581 if (this_parent != root) {
Eric Dumazet5160ee62006-01-08 01:03:32 -08002582 next = this_parent->d_u.d_child.next;
Nick Pigginb7ab39f2011-01-07 17:49:32 +11002583 spin_lock(&this_parent->d_lock);
2584 this_parent->d_count--;
2585 spin_unlock(&this_parent->d_lock);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002586 this_parent = this_parent->d_parent;
2587 goto resume;
2588 }
2589 spin_unlock(&dcache_lock);
2590}
2591
2592/**
2593 * find_inode_number - check for dentry with name
2594 * @dir: directory to check
2595 * @name: Name to find.
2596 *
2597 * Check whether a dentry already exists for the given name,
2598 * and return the inode number if it has an inode. Otherwise
2599 * 0 is returned.
2600 *
2601 * This routine is used to post-process directory listings for
2602 * filesystems using synthetic inode numbers, and is necessary
2603 * to keep getcwd() working.
2604 */
2605
2606ino_t find_inode_number(struct dentry *dir, struct qstr *name)
2607{
2608 struct dentry * dentry;
2609 ino_t ino = 0;
2610
Eric W. Biederman3e7e2412006-03-31 02:31:43 -08002611 dentry = d_hash_and_lookup(dir, name);
2612 if (dentry) {
Linus Torvalds1da177e2005-04-16 15:20:36 -07002613 if (dentry->d_inode)
2614 ino = dentry->d_inode->i_ino;
2615 dput(dentry);
2616 }
Linus Torvalds1da177e2005-04-16 15:20:36 -07002617 return ino;
2618}
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002619EXPORT_SYMBOL(find_inode_number);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002620
2621static __initdata unsigned long dhash_entries;
2622static int __init set_dhash_entries(char *str)
2623{
2624 if (!str)
2625 return 0;
2626 dhash_entries = simple_strtoul(str, &str, 0);
2627 return 1;
2628}
2629__setup("dhash_entries=", set_dhash_entries);
2630
2631static void __init dcache_init_early(void)
2632{
2633 int loop;
2634
2635 /* If hashes are distributed across NUMA nodes, defer
2636 * hash allocation until vmalloc space is available.
2637 */
2638 if (hashdist)
2639 return;
2640
2641 dentry_hashtable =
2642 alloc_large_system_hash("Dentry cache",
2643 sizeof(struct hlist_head),
2644 dhash_entries,
2645 13,
2646 HASH_EARLY,
2647 &d_hash_shift,
2648 &d_hash_mask,
2649 0);
2650
2651 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2652 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2653}
2654
Denis Cheng74bf17c2007-10-16 23:26:30 -07002655static void __init dcache_init(void)
Linus Torvalds1da177e2005-04-16 15:20:36 -07002656{
2657 int loop;
2658
2659 /*
2660 * A constructor could be added for stable state like the lists,
2661 * but it is probably not worth it because of the cache nature
2662 * of the dcache.
2663 */
Christoph Lameter0a31bd52007-05-06 14:49:57 -07002664 dentry_cache = KMEM_CACHE(dentry,
2665 SLAB_RECLAIM_ACCOUNT|SLAB_PANIC|SLAB_MEM_SPREAD);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002666
Rusty Russell8e1f9362007-07-17 04:03:17 -07002667 register_shrinker(&dcache_shrinker);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002668
2669 /* Hash may have been set up in dcache_init_early */
2670 if (!hashdist)
2671 return;
2672
2673 dentry_hashtable =
2674 alloc_large_system_hash("Dentry cache",
2675 sizeof(struct hlist_head),
2676 dhash_entries,
2677 13,
2678 0,
2679 &d_hash_shift,
2680 &d_hash_mask,
2681 0);
2682
2683 for (loop = 0; loop < (1 << d_hash_shift); loop++)
2684 INIT_HLIST_HEAD(&dentry_hashtable[loop]);
2685}
2686
2687/* SLAB cache for __getname() consumers */
Christoph Lametere18b8902006-12-06 20:33:20 -08002688struct kmem_cache *names_cachep __read_mostly;
H Hartley Sweetenec4f8602010-01-05 13:45:18 -07002689EXPORT_SYMBOL(names_cachep);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002690
Linus Torvalds1da177e2005-04-16 15:20:36 -07002691EXPORT_SYMBOL(d_genocide);
2692
Linus Torvalds1da177e2005-04-16 15:20:36 -07002693void __init vfs_caches_init_early(void)
2694{
2695 dcache_init_early();
2696 inode_init_early();
2697}
2698
2699void __init vfs_caches_init(unsigned long mempages)
2700{
2701 unsigned long reserve;
2702
2703 /* Base hash sizes on available memory, with a reserve equal to
2704 150% of current kernel size */
2705
2706 reserve = min((mempages - nr_free_pages()) * 3/2, mempages - 1);
2707 mempages -= reserve;
2708
2709 names_cachep = kmem_cache_create("names_cache", PATH_MAX, 0,
Paul Mundt20c2df82007-07-20 10:11:58 +09002710 SLAB_HWCACHE_ALIGN|SLAB_PANIC, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -07002711
Denis Cheng74bf17c2007-10-16 23:26:30 -07002712 dcache_init();
2713 inode_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002714 files_init(mempages);
Denis Cheng74bf17c2007-10-16 23:26:30 -07002715 mnt_init();
Linus Torvalds1da177e2005-04-16 15:20:36 -07002716 bdev_cache_init();
2717 chrdev_init();
2718}